Skip to content

Commit

Permalink
remove static things from c-code (#342)
Browse files Browse the repository at this point in the history
* Remove all static things

* Package Version Bump

* Expose since parameter

* Ammend ts declarations to allow since

* Add node8 and remove node13 from tests

Co-authored-by: Philipp Dunkel <pdunkel1@bloomberg.net>
  • Loading branch information
pipobscure and pdunkel committed Nov 3, 2020
1 parent 662406c commit 6a3bb59
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 364 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: macOS-latest
strategy:
matrix:
node-version: [10, 12, 13, 14, 15]
node-version: [8, 10, 12, 14, 15]

steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion binding.gyp
Expand Up @@ -3,7 +3,7 @@
['OS=="mac"', {
"targets": [{
"target_name": "fsevents",
"sources": [ "src/fsevents.c", "src/rawfsevents.c" ],
"sources": [ "src/fsevents.c"],
"xcode_settings": {
"OTHER_LDFLAGS": [
"-Wl,-bind_at_load",
Expand Down
85 changes: 38 additions & 47 deletions fsevents.d.ts
@@ -1,55 +1,46 @@
declare type Event =
| 'created'
| 'cloned'
| 'modified'
| 'deleted'
| 'moved'
| 'root-changed'
| 'unknown';
declare type Type = 'file' | 'directory' | 'symlink';
declare type Event = "created" | "cloned" | "modified" | "deleted" | "moved" | "root-changed" | "unknown";
declare type Type = "file" | "directory" | "symlink";
declare type FileChanges = {
inode: boolean;
finder: boolean;
access: boolean;
xattrs: boolean;
inode: boolean;
finder: boolean;
access: boolean;
xattrs: boolean;
};
declare type Info = {
event: Event;
path: string;
type: Type;
changes: FileChanges;
flags: number;
event: Event;
path: string;
type: Type;
changes: FileChanges;
flags: number;
};
declare type WatchHandler = (path: string, flags: number, id: string) => void;
export declare function watch(
path: string,
handler: WatchHandler,
): () => Promise<void>;
export declare function watch(path: string, handler: WatchHandler): () => Promise<void>;
export declare function watch(path: string, since: number, handler: WatchHandler);
export declare function getInfo(path: string, flags: number): Info;
export declare const constants: {
None: 0x00000000;
MustScanSubDirs: 0x00000001;
UserDropped: 0x00000002;
KernelDropped: 0x00000004;
EventIdsWrapped: 0x00000008;
HistoryDone: 0x00000010;
RootChanged: 0x00000020;
Mount: 0x00000040;
Unmount: 0x00000080;
ItemCreated: 0x00000100;
ItemRemoved: 0x00000200;
ItemInodeMetaMod: 0x00000400;
ItemRenamed: 0x00000800;
ItemModified: 0x00001000;
ItemFinderInfoMod: 0x00002000;
ItemChangeOwner: 0x00004000;
ItemXattrMod: 0x00008000;
ItemIsFile: 0x00010000;
ItemIsDir: 0x00020000;
ItemIsSymlink: 0x00040000;
ItemIsHardlink: 0x00100000;
ItemIsLastHardlink: 0x00200000;
OwnEvent: 0x00080000;
ItemCloned: 0x00400000;
None: 0x00000000;
MustScanSubDirs: 0x00000001;
UserDropped: 0x00000002;
KernelDropped: 0x00000004;
EventIdsWrapped: 0x00000008;
HistoryDone: 0x00000010;
RootChanged: 0x00000020;
Mount: 0x00000040;
Unmount: 0x00000080;
ItemCreated: 0x00000100;
ItemRemoved: 0x00000200;
ItemInodeMetaMod: 0x00000400;
ItemRenamed: 0x00000800;
ItemModified: 0x00001000;
ItemFinderInfoMod: 0x00002000;
ItemChangeOwner: 0x00004000;
ItemXattrMod: 0x00008000;
ItemIsFile: 0x00010000;
ItemIsDir: 0x00020000;
ItemIsSymlink: 0x00040000;
ItemIsHardlink: 0x00100000;
ItemIsLastHardlink: 0x00200000;
OwnEvent: 0x00080000;
ItemCloned: 0x00400000;
};
export {}
export {};
53 changes: 29 additions & 24 deletions fsevents.js
Expand Up @@ -4,29 +4,34 @@
*/

/* jshint node:true */
'use strict';
"use strict";

if (process.platform !== 'darwin') {
if (process.platform !== "darwin") {
throw new Error(`Module 'fsevents' is not compatible with platform '${process.platform}'`);
}

const Native = require('./fsevents.node');
const Native = require("./fsevents.node");
const events = Native.constants;

function watch(path, handler) {
if (typeof path !== 'string') {
function watch(path, since, handler) {
if (typeof path !== "string") {
throw new TypeError(`fsevents argument 1 must be a string and not a ${typeof path}`);
}
if (typeof handler !== 'function') {
throw new TypeError(`fsevents argument 2 must be a function and not a ${typeof handler}`);
if ("function" === typeof since && "undefined" === typeof handler) {
handler = since;
since = Native.flags.SinceNow;
}
if (typeof since !== "number") {
throw new TypeError(`fsevents argument 2 must be a number and not a ${typeof since}`);
}
if (typeof handler !== "function") {
throw new TypeError(`fsevents argument 3 must be a function and not a ${typeof handler}`);
}

let instance = Native.start(path, handler);
let instance = Native.start(Native.global, path, since, handler);
if (!instance) throw new Error(`could not watch: ${path}`);
return () => {
const result = instance
? Promise.resolve(instance).then(Native.stop)
: Promise.resolve(undefined);
const result = instance ? Promise.resolve(instance).then(Native.stop) : Promise.resolve(undefined);
instance = undefined;
return result;
};
Expand All @@ -38,14 +43,14 @@ function getInfo(path, flags) {
flags,
event: getEventType(flags),
type: getFileType(flags),
changes: getFileChanges(flags)
changes: getFileChanges(flags),
};
}

function getFileType(flags) {
if (events.ItemIsFile & flags) return 'file';
if (events.ItemIsDir & flags) return 'directory';
if (events.ItemIsSymlink & flags) return 'symlink';
if (events.ItemIsFile & flags) return "file";
if (events.ItemIsDir & flags) return "directory";
if (events.ItemIsSymlink & flags) return "symlink";
}
function anyIsTrue(obj) {
for (let key in obj) {
Expand All @@ -54,21 +59,21 @@ function anyIsTrue(obj) {
return false;
}
function getEventType(flags) {
if (events.ItemRemoved & flags) return 'deleted';
if (events.ItemRenamed & flags) return 'moved';
if (events.ItemCreated & flags) return 'created';
if (events.ItemModified & flags) return 'modified';
if (events.RootChanged & flags) return 'root-changed';
if (events.ItemCloned & flags) return 'cloned';
if (anyIsTrue(flags)) return 'modified';
return 'unknown';
if (events.ItemRemoved & flags) return "deleted";
if (events.ItemRenamed & flags) return "moved";
if (events.ItemCreated & flags) return "created";
if (events.ItemModified & flags) return "modified";
if (events.RootChanged & flags) return "root-changed";
if (events.ItemCloned & flags) return "cloned";
if (anyIsTrue(flags)) return "modified";
return "unknown";
}
function getFileChanges(flags) {
return {
inode: !!(events.ItemInodeMetaMod & flags),
finder: !!(events.ItemFinderInfoMod & flags),
access: !!(events.ItemChangeOwner & flags),
xattrs: !!(events.ItemXattrMod & flags)
xattrs: !!(events.ItemXattrMod & flags),
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "fsevents",
"version": "2.1.3",
"version": "2.2.0",
"description": "Native Access to MacOS FSEvents",
"main": "fsevents.js",
"types": "fsevents.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/constants.h
Expand Up @@ -2,6 +2,7 @@
#define __constants_h

#include "CoreFoundation/CoreFoundation.h"
#include "CoreServices/CoreServices.h"

// constants from https://developer.apple.com/library/mac/documentation/Darwin/Reference/FSEvents_Ref/index.html#//apple_ref/doc/constant_group/FSEventStreamEventFlags
#ifndef kFSEventStreamEventFlagNone
Expand Down

0 comments on commit 6a3bb59

Please sign in to comment.