penambahan web socket

This commit is contained in:
2025-09-18 19:01:22 +07:00
parent 1d053646a9
commit d7bb2eb5bb
15070 changed files with 2402916 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
# @parcel/watcher
A native C++ Node module for querying and subscribing to filesystem events. Used by [Parcel 2](https://github.com/parcel-bundler/parcel).
## Features
- **Watch** - subscribe to realtime recursive directory change notifications when files or directories are created, updated, or deleted.
- **Query** - performantly query for historical change events in a directory, even when your program is not running.
- **Native** - implemented in C++ for performance and low-level integration with the operating system.
- **Cross platform** - includes backends for macOS, Linux, Windows, FreeBSD, and Watchman.
- **Performant** - events are throttled in C++ so the JavaScript thread is not overwhelmed during large filesystem changes (e.g. `git checkout` or `npm install`).
- **Scalable** - tens of thousands of files can be watched or queried at once with good performance.
## Example
```javascript
const watcher = require('@parcel/watcher');
const path = require('path');
// Subscribe to events
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
console.log(events);
});
// later on...
await subscription.unsubscribe();
// Get events since some saved snapshot in the past
let snapshotPath = path.join(process.cwd(), 'snapshot.txt');
let events = await watcher.getEventsSince(process.cwd(), snapshotPath);
// Save a snapshot for later
await watcher.writeSnapshot(process.cwd(), snapshotPath);
```
## Watching
`@parcel/watcher` supports subscribing to realtime notifications of changes in a directory. It works recursively, so changes in sub-directories will also be emitted.
Events are throttled and coalesced for performance during large changes like `git checkout` or `npm install`, and a single notification will be emitted with all of the events at the end.
Only one notification will be emitted per file. For example, if a file was both created and updated since the last event, you'll get only a `create` event. If a file is both created and deleted, you will not be notifed of that file. Renames cause two events: a `delete` for the old name, and a `create` for the new name.
```javascript
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
console.log(events);
});
```
Events have two properties:
- `type` - the event type: `create`, `update`, or `delete`.
- `path` - the absolute path to the file or directory.
To unsubscribe from change notifications, call the `unsubscribe` method on the returned subscription object.
```javascript
await subscription.unsubscribe();
```
`@parcel/watcher` has the following watcher backends, listed in priority order:
- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
- [Watchman](https://facebook.github.io/watchman/) if installed
- [inotify](http://man7.org/linux/man-pages/man7/inotify.7.html) on Linux
- [ReadDirectoryChangesW](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v%3Dvs.85%29.aspx) on Windows
- [kqueue](https://man.freebsd.org/cgi/man.cgi?kqueue) on FreeBSD, or as an alternative to FSEvents on macOS
You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
## Querying
`@parcel/watcher` also supports querying for historical changes made in a directory, even when your program is not running. This makes it easy to invalidate a cache and re-build only the files that have changed, for example. It can be **significantly** faster than traversing the entire filesystem to determine what files changed, depending on the platform.
In order to query for historical changes, you first need a previous snapshot to compare to. This can be saved to a file with the `writeSnapshot` function, e.g. just before your program exits.
```javascript
await watcher.writeSnapshot(dirPath, snapshotPath);
```
When your program starts up, you can query for changes that have occurred since that snapshot using the `getEventsSince` function.
```javascript
let events = await watcher.getEventsSince(dirPath, snapshotPath);
```
The events returned are exactly the same as the events that would be passed to the `subscribe` callback (see above).
`@parcel/watcher` has the following watcher backends, listed in priority order:
- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
- [Watchman](https://facebook.github.io/watchman/) if installed
- [fts](http://man7.org/linux/man-pages/man3/fts.3.html) (brute force) on Linux and FreeBSD
- [FindFirstFile](https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea) (brute force) on Windows
The FSEvents (macOS) and Watchman backends are significantly more performant than the brute force backends used by default on Linux and Windows, for example returning results in miliseconds instead of seconds for large directory trees. This is because a background daemon monitoring filesystem changes on those platforms allows us to query cached data rather than traversing the filesystem manually (brute force).
macOS has good performance with FSEvents by default. For the best performance on other platforms, install [Watchman](https://facebook.github.io/watchman/) and it will be used by `@parcel/watcher` automatically.
You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
## Options
All of the APIs in `@parcel/watcher` support the following options, which are passed as an object as the last function argument.
- `ignore` - an array of paths or glob patterns to ignore. uses [`is-glob`](https://github.com/micromatch/is-glob) to distinguish paths from globs. glob patterns are parsed with [`micromatch`](https://github.com/micromatch/micromatch) (see [features](https://github.com/micromatch/micromatch#matching-features)).
- paths can be relative or absolute and can either be files or directories. No events will be emitted about these files or directories or their children.
- glob patterns match on relative paths from the root that is watched. No events will be emitted for matching paths.
- `backend` - the name of an explicitly chosen backend to use. Allowed options are `"fs-events"`, `"watchman"`, `"inotify"`, `"kqueue"`, `"windows"`, or `"brute-force"` (only for querying). If the specified backend is not available on the current platform, the default backend will be used instead.
## WASM
The `@parcel/watcher-wasm` package can be used in place of `@parcel/watcher` on unsupported platforms. It relies on the Node `fs` module, so in non-Node environments such as browsers, an `fs` polyfill will be needed.
**Note**: the WASM implementation is significantly less efficient than the native implementations because it must crawl the file system to watch each directory individually. Use the native `@parcel/watcher` package wherever possible.
```js
import {subscribe} from '@parcel/watcher-wasm';
// Use the module as documented above.
subscribe(/* ... */);
```
## Who is using this?
- [Parcel 2](https://parceljs.org/)
- [VSCode](https://code.visualstudio.com/updates/v1_62#_file-watching-changes)
- [Tailwind CSS Intellisense](https://github.com/tailwindlabs/tailwindcss-intellisense)
- [Gatsby Cloud](https://twitter.com/chatsidhartha/status/1435647412828196867)
- [Nx](https://nx.dev)
- [Nuxt](https://nuxt.com)
## License
MIT

View File

@@ -0,0 +1,440 @@
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// wasm/import.meta.url-polyfill.js
var import_meta_url;
var init_import_meta_url_polyfill = __esm({
"wasm/import.meta.url-polyfill.js"() {
import_meta_url = typeof document === "undefined" ? new (require("url".replace("", ""))).URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
}
});
// npm/wasm/wrapper.js
var require_wrapper = __commonJS({
"npm/wasm/wrapper.js"(exports) {
init_import_meta_url_polyfill();
var path = require("path");
var micromatch = require("micromatch");
var isGlob = require("is-glob");
function normalizeOptions(dir, opts = {}) {
const { ignore, ...rest } = opts;
if (Array.isArray(ignore)) {
opts = { ...rest };
for (const value of ignore) {
if (isGlob(value)) {
if (!opts.ignoreGlobs) {
opts.ignoreGlobs = [];
}
const regex = micromatch.makeRe(value, {
// We set `dot: true` to workaround an issue with the
// regular expression on Linux where the resulting
// negative lookahead `(?!(\\/|^)` was never matching
// in some cases. See also https://bit.ly/3UZlQDm
dot: true,
// C++ does not support lookbehind regex patterns, they
// were only added later to JavaScript engines
// (https://bit.ly/3V7S6UL)
lookbehinds: false
});
opts.ignoreGlobs.push(regex.source);
} else {
if (!opts.ignorePaths) {
opts.ignorePaths = [];
}
opts.ignorePaths.push(path.resolve(dir, value));
}
}
}
return opts;
}
exports.createWrapper = (binding) => {
return {
writeSnapshot(dir, snapshot, opts) {
return binding.writeSnapshot(
path.resolve(dir),
path.resolve(snapshot),
normalizeOptions(dir, opts)
);
},
getEventsSince(dir, snapshot, opts) {
return binding.getEventsSince(
path.resolve(dir),
path.resolve(snapshot),
normalizeOptions(dir, opts)
);
},
async subscribe(dir, fn, opts) {
dir = path.resolve(dir);
opts = normalizeOptions(dir, opts);
await binding.subscribe(dir, fn, opts);
return {
unsubscribe() {
return binding.unsubscribe(dir, fn, opts);
}
};
},
unsubscribe(dir, fn, opts) {
return binding.unsubscribe(
path.resolve(dir),
fn,
normalizeOptions(dir, opts)
);
}
};
};
}
});
// npm/wasm/index.mjs
var wasm_exports = {};
__export(wasm_exports, {
getEventsSince: () => getEventsSince,
subscribe: () => subscribe,
unsubscribe: () => unsubscribe,
writeSnapshot: () => writeSnapshot
});
module.exports = __toCommonJS(wasm_exports);
init_import_meta_url_polyfill();
var import_napi_wasm = require("napi-wasm");
var import_fs = __toESM(require("fs"), 1);
var import_path = __toESM(require("path"), 1);
var import_wrapper = __toESM(require_wrapper(), 1);
var env;
var encoder = new TextEncoder();
var constants = {
O_ACCMODE: 3,
O_RDONLY: 0,
O_WRONLY: 1,
O_RDWR: 2,
O_CREAT: 64,
O_EXCL: 128,
O_NOCTTY: 256,
O_TRUNC: 512,
O_APPEND: 1024,
O_NONBLOCK: 2048,
O_SYNC: 4096,
FASYNC: 8192,
O_DIRECT: 16384,
O_LARGEFILE: 32768,
O_DIRECTORY: 65536,
O_NOFOLLOW: 131072,
O_NOATIME: 262144,
O_CLOEXEC: 524288
};
import_napi_wasm.napi.napi_get_last_error_info = () => {
};
var fds = /* @__PURE__ */ new Map();
var dirs = /* @__PURE__ */ new Map();
var regexCache = /* @__PURE__ */ new Map();
var watches = [null];
var wasm_env = {
__syscall_newfstatat(dirfd, path, buf, flags) {
let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd).path;
let p = import_path.default.resolve(dir, env.getString(path));
let nofollow = flags & 256;
try {
let stat = nofollow ? import_fs.default.lstatSync(p, { bigint: true }) : import_fs.default.statSync(p, { bigint: true });
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_lstat64(path, buf) {
let p = env.getString(path);
try {
let stat = import_fs.default.lstatSync(p, { bigint: true });
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_fstat64(fd, buf) {
try {
let stat = import_fs.default.fstatSync(fd, { bigint: true });
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_stat64(path, buf) {
let p = env.getString(path);
try {
let stat = import_fs.default.statSync(p, { bigint: true });
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_getdents64(fd, dirp, count) {
let p = fds.get(fd).path;
let dir = dirs.get(fd);
let entries = dir?.entries;
if (!entries) {
try {
entries = import_fs.default.readdirSync(p, { withFileTypes: true });
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
}
let start = dirp;
let i = dir?.index || 0;
for (; i < entries.length; i++) {
let entry = entries[i];
let type = entry.isFIFO() ? 1 : entry.isCharacterDevice() ? 2 : entry.isDirectory() ? 4 : entry.isBlockDevice() ? 6 : entry.isFile() ? 8 : entry.isSymbolicLink() ? 10 : entry.isSocket() ? 12 : 0;
let len = align(utf8Length(entry.name) + 20, 8);
if (dirp - start + len > count) {
break;
}
env.u64[dirp >> 3] = 1n;
env.u64[dirp + 8 >> 3] = BigInt(dirp - start + len);
env.u16[dirp + 16 >> 1] = len;
env.memory[dirp + 18] = type;
let { written } = encoder.encodeInto(entry.name, env.memory.subarray(dirp + 19));
env.memory[dirp + 19 + written] = 0;
dirp += len;
}
dirs.set(fd, { index: i, entries });
return dirp - start;
},
__syscall_openat(dirfd, path, flags, mode) {
let f = 0;
for (let c in constants) {
if (flags & constants[c]) {
f |= import_fs.default.constants[c] || 0;
}
}
let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd)?.path;
if (!dir) {
env.i32[env.instance.exports.__errno_location >> 2] = 9970;
return -1;
}
let p = import_path.default.resolve(dir, env.getString(path));
try {
let fd = import_fs.default.openSync(p, f);
fds.set(fd, { path: p, flags });
return fd;
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_fcntl64(fd, cmd) {
switch (cmd) {
case 3:
return fds.get(fd).flags;
case 2:
return 0;
default:
throw new Error("Unknown fcntl64 call: " + cmd);
}
},
__syscall_ioctl() {
},
emscripten_resize_heap() {
return 0;
},
_abort_js() {
},
wasm_backend_add_watch(filename, backend) {
let path = env.getString(filename);
let watch = import_fs.default.watch(path, { encoding: "buffer" }, (eventType, filename2) => {
if (filename2) {
let type = eventType === "change" ? 1 : 2;
let fptr = env.instance.exports.malloc(filename2.byteLength + 1);
env.memory.set(filename2, fptr);
env.memory[fptr + filename2.byteLength] = 0;
env.instance.exports.wasm_backend_event_handler(backend, wd, type, fptr);
env.instance.exports.free(fptr);
}
});
let wd = watches.length;
watches.push(watch);
return wd;
},
wasm_backend_remove_watch(wd) {
watches[wd].close();
watches[wd] = void 0;
},
set_timeout(ms, ctx) {
return setTimeout(() => {
env.instance.exports.on_timeout(ctx);
}, ms);
},
clear_timeout(t) {
clearTimeout(t);
},
_setitimer_js() {
},
emscripten_date_now() {
return Date.now();
},
_emscripten_get_now_is_monotonic() {
return true;
},
_emscripten_runtime_keepalive_clear() {
},
emscripten_get_now() {
return performance.now();
},
wasm_regex_match(string, regex) {
let re = regexCache.get(regex);
if (!re) {
re = new RegExp(env.getString(regex));
regexCache.set(regex, re);
}
return re.test(env.getString(string)) ? 1 : 0;
}
};
var wasi = {
fd_close(fd) {
import_fs.default.closeSync(fd);
fds.delete(fd);
dirs.delete(fd);
return 0;
},
fd_seek(fd, offset_low, offset_high, whence, newOffset) {
return 0;
},
fd_write(fd, iov, iovcnt, pnum) {
let buffers = [];
for (let i = 0; i < iovcnt; i++) {
let ptr = env.u32[iov >> 2];
let len = env.u32[iov + 4 >> 2];
iov += 8;
if (len > 0) {
buffers.push(env.memory.subarray(ptr, ptr + len));
}
}
let wrote = import_fs.default.writevSync(fd, buffers);
env.u32[pnum >> 2] = wrote;
return 0;
},
fd_read(fd, iov, iovcnt, pnum) {
let buffers = [];
for (let i = 0; i < iovcnt; i++) {
let ptr = env.u32[iov >> 2];
let len = env.u32[iov + 4 >> 2];
iov += 8;
if (len > 0) {
buffers.push(env.memory.subarray(ptr, ptr + len));
}
}
let read = import_fs.default.readvSync(fd, buffers);
env.u32[pnum >> 2] = read;
return 0;
},
proc_exit() {
},
clock_time_get() {
}
};
function writeStat(stat, buf) {
env.i32[buf >> 2] = Number(stat.dev);
env.i32[buf + 4 >> 2] = Number(stat.mode);
env.u32[buf + 8 >> 2] = Number(stat.nlink);
env.i32[buf + 12 >> 2] = Number(stat.uid);
env.i32[buf + 16 >> 2] = Number(stat.gid);
env.i32[buf + 20 >> 2] = Number(stat.rdev);
env.u64[buf + 24 >> 3] = stat.size;
env.i32[buf + 32 >> 2] = Number(stat.blksize);
env.i32[buf + 36 >> 2] = Number(stat.blocks);
env.u64[buf + 40 >> 3] = stat.atimeMs;
env.u32[buf + 48 >> 2] = Number(stat.atimeNs);
env.u64[buf + 56 >> 3] = stat.mtimeMs;
env.u32[buf + 64 >> 2] = Number(stat.mtimeNs);
env.u64[buf + 72 >> 3] = stat.ctimeMs;
env.u32[buf + 80 >> 2] = Number(stat.ctimeNs);
env.u64[buf + 88 >> 3] = stat.ino;
return 0;
}
function utf8Length(string) {
let len = 0;
for (let i = 0; i < string.length; i++) {
let c = string.charCodeAt(i);
if (c >= 55296 && c <= 56319 && i < string.length - 1) {
let c2 = string.charCodeAt(++i);
if ((c2 & 64512) === 56320) {
c = ((c & 1023) << 10) + (c2 & 1023) + 65536;
} else {
i--;
}
}
if ((c & 4294967168) === 0) {
len++;
} else if ((c & 4294965248) === 0) {
len += 2;
} else if ((c & 4294901760) === 0) {
len += 3;
} else if ((c & 4292870144) === 0) {
len += 4;
}
}
return len;
}
function align(len, p) {
return Math.ceil(len / p) * p;
}
var wasmBytes = import_fs.default.readFileSync(new URL("watcher.wasm", import_meta_url));
var wasmModule = new WebAssembly.Module(wasmBytes);
var instance = new WebAssembly.Instance(wasmModule, {
napi: import_napi_wasm.napi,
env: wasm_env,
wasi_snapshot_preview1: wasi
});
env = new import_napi_wasm.Environment(instance);
var wrapper = (0, import_wrapper.createWrapper)(env.exports);
function writeSnapshot(dir, snapshot, opts) {
return wrapper.writeSnapshot(dir, snapshot, opts);
}
function getEventsSince(dir, snapshot, opts) {
return wrapper.getEventsSince(dir, snapshot, opts);
}
function subscribe(dir, fn, opts) {
return wrapper.subscribe(dir, fn, opts);
}
function unsubscribe(dir, fn, opts) {
return wrapper.unsubscribe(dir, fn, opts);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getEventsSince,
subscribe,
unsubscribe,
writeSnapshot
});

View File

@@ -0,0 +1,51 @@
declare type FilePath = string;
declare type GlobPattern = string;
declare namespace ParcelWatcher {
export type BackendType =
| 'fs-events'
| 'watchman'
| 'inotify'
| 'windows'
| 'brute-force';
export type EventType = 'create' | 'update' | 'delete';
export interface Options {
ignore?: (FilePath|GlobPattern)[];
backend?: BackendType;
}
export type SubscribeCallback = (
err: Error | null,
events: Event[]
) => unknown;
export interface AsyncSubscription {
unsubscribe(): Promise<void>;
}
export interface Event {
path: FilePath;
type: EventType;
}
export function getEventsSince(
dir: FilePath,
snapshot: FilePath,
opts?: Options
): Promise<Event[]>;
export function subscribe(
dir: FilePath,
fn: SubscribeCallback,
opts?: Options
): Promise<AsyncSubscription>;
export function unsubscribe(
dir: FilePath,
fn: SubscribeCallback,
opts?: Options
): Promise<void>;
export function writeSnapshot(
dir: FilePath,
snapshot: FilePath,
opts?: Options
): Promise<FilePath>;
}
export = ParcelWatcher;
/** Initializes the web assembly module. */
export default function init(input?: string | URL | Request): Promise<void>;

View File

@@ -0,0 +1,330 @@
import { Environment, napi } from 'napi-wasm';
import fs from 'fs';
import Path from 'path';
import { createWrapper } from './wrapper.js';
let env;
let encoder = new TextEncoder;
let constants = {
O_ACCMODE: 0o00000003,
O_RDONLY: 0,
O_WRONLY: 0o00000001,
O_RDWR: 0o00000002,
O_CREAT: 0o00000100,
O_EXCL: 0o00000200,
O_NOCTTY: 0o00000400,
O_TRUNC: 0o00001000,
O_APPEND: 0o00002000,
O_NONBLOCK: 0o00004000,
O_SYNC: 0o00010000,
FASYNC: 0o00020000,
O_DIRECT: 0o00040000,
O_LARGEFILE: 0o00100000,
O_DIRECTORY: 0o00200000,
O_NOFOLLOW: 0o00400000,
O_NOATIME: 0o01000000,
O_CLOEXEC: 0o02000000
};
napi.napi_get_last_error_info = () => {};
const fds = new Map();
const dirs = new Map();
const regexCache = new Map();
const watches = [null];
const wasm_env = {
__syscall_newfstatat(dirfd, path, buf, flags) {
let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd).path;
let p = Path.resolve(dir, env.getString(path));
let nofollow = flags & 256;
try {
let stat = nofollow ? fs.lstatSync(p, {bigint: true}) : fs.statSync(p, {bigint: true});
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_lstat64(path, buf) {
let p = env.getString(path);
try {
let stat = fs.lstatSync(p, {bigint: true});
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_fstat64(fd, buf) {
try {
let stat = fs.fstatSync(fd, {bigint: true});
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_stat64(path, buf) {
let p = env.getString(path);
try {
let stat = fs.statSync(p, {bigint: true});
return writeStat(stat, buf);
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_getdents64(fd, dirp, count) {
let p = fds.get(fd).path;
let dir = dirs.get(fd);
let entries = dir?.entries;
if (!entries) {
try {
entries = fs.readdirSync(p, {withFileTypes: true});
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
}
let start = dirp;
let i = dir?.index || 0;
for (; i < entries.length; i++) {
let entry = entries[i];
let type = entry.isFIFO() ? 1
: entry.isCharacterDevice() ? 2
: entry.isDirectory() ? 4
: entry.isBlockDevice() ? 6
: entry.isFile() ? 8
: entry.isSymbolicLink() ? 10
: entry.isSocket() ? 12
: 0;
let len = align(utf8Length(entry.name) + 20, 8);
if ((dirp - start + len) > count) {
break;
}
// Write a linux_dirent64 struct into wasm memory.
env.u64[dirp >> 3] = 1n; // ino
env.u64[(dirp + 8) >> 3] = BigInt((dirp - start) + len); // offset
env.u16[(dirp + 16) >> 1] = len;
env.memory[dirp + 18] = type;
let {written} = encoder.encodeInto(entry.name, env.memory.subarray(dirp + 19));
env.memory[dirp + 19 + written] = 0; // null terminate
dirp += len;
}
dirs.set(fd, {index: i, entries});
return dirp - start;
},
__syscall_openat(dirfd, path, flags, mode) {
// Convert flags to Node values.
let f = 0;
for (let c in constants) {
if (flags & constants[c]) {
f |= fs.constants[c] || 0;
}
}
let dir = dirfd === -100 ? process.cwd() : fds.get(dirfd)?.path;
if (!dir) {
env.i32[env.instance.exports.__errno_location >> 2] = 9970; // ENOTDIR
return -1;
}
let p = Path.resolve(dir, env.getString(path));
try {
let fd = fs.openSync(p, f);
fds.set(fd, {path: p, flags});
return fd;
} catch (err) {
env.i32[env.instance.exports.__errno_location >> 2] = err.errno;
return -1;
}
},
__syscall_fcntl64(fd, cmd) {
switch (cmd) {
case 3:
return fds.get(fd).flags;
case 2:
return 0;
default:
throw new Error('Unknown fcntl64 call: ' + cmd);
}
},
__syscall_ioctl() {},
emscripten_resize_heap() {
return 0;
},
_abort_js() {},
wasm_backend_add_watch(filename, backend) {
let path = env.getString(filename);
let watch = fs.watch(path, {encoding: 'buffer'}, (eventType, filename) => {
if (filename) {
let type = eventType === 'change' ? 1 : 2;
let fptr = env.instance.exports.malloc(filename.byteLength + 1);
env.memory.set(filename, fptr);
env.memory[fptr + filename.byteLength] = 0;
env.instance.exports.wasm_backend_event_handler(backend, wd, type, fptr);
env.instance.exports.free(fptr);
}
});
let wd = watches.length;
watches.push(watch);
return wd;
},
wasm_backend_remove_watch(wd) {
watches[wd].close();
watches[wd] = undefined;
},
set_timeout(ms, ctx) {
return setTimeout(() => {
env.instance.exports.on_timeout(ctx);
}, ms);
},
clear_timeout(t) {
clearTimeout(t);
},
_setitimer_js() {},
emscripten_date_now() {
return Date.now();
},
_emscripten_get_now_is_monotonic() {
return true;
},
_emscripten_runtime_keepalive_clear() {},
emscripten_get_now() {
return performance.now();
},
wasm_regex_match(string, regex) {
let re = regexCache.get(regex);
if (!re) {
re = new RegExp(env.getString(regex));
regexCache.set(regex, re);
}
return re.test(env.getString(string)) ? 1 : 0;
}
};
const wasi = {
fd_close(fd) {
fs.closeSync(fd);
fds.delete(fd);
dirs.delete(fd);
return 0;
},
fd_seek(fd, offset_low, offset_high, whence, newOffset) {
return 0;
},
fd_write(fd, iov, iovcnt, pnum) {
let buffers = [];
for (let i = 0; i < iovcnt; i++) {
let ptr = env.u32[iov >> 2];
let len = env.u32[(iov + 4) >> 2];
iov += 8;
if (len > 0) {
buffers.push(env.memory.subarray(ptr, ptr + len));
}
}
let wrote = fs.writevSync(fd, buffers);
env.u32[pnum >> 2] = wrote;
return 0;
},
fd_read(fd, iov, iovcnt, pnum) {
let buffers = [];
for (let i = 0; i < iovcnt; i++) {
let ptr = env.u32[iov >> 2];
let len = env.u32[(iov + 4) >> 2];
iov += 8;
if (len > 0) {
buffers.push(env.memory.subarray(ptr, ptr + len));
}
}
let read = fs.readvSync(fd, buffers);
env.u32[pnum >> 2] = read;
return 0;
},
proc_exit() {},
clock_time_get() {}
};
function writeStat(stat, buf) {
env.i32[buf >> 2] = Number(stat.dev);
env.i32[(buf + 4) >> 2] = Number(stat.mode);
env.u32[(buf + 8) >> 2] = Number(stat.nlink);
env.i32[(buf + 12) >> 2] = Number(stat.uid);
env.i32[(buf + 16) >> 2] = Number(stat.gid);
env.i32[(buf + 20) >> 2] = Number(stat.rdev);
env.u64[(buf + 24) >> 3] = stat.size;
env.i32[(buf + 32) >> 2] = Number(stat.blksize);
env.i32[(buf + 36) >> 2] = Number(stat.blocks);
env.u64[(buf + 40) >> 3] = stat.atimeMs;
env.u32[(buf + 48) >> 2] = Number(stat.atimeNs);
env.u64[(buf + 56) >> 3] = stat.mtimeMs;
env.u32[(buf + 64) >> 2] = Number(stat.mtimeNs);
env.u64[(buf + 72) >> 3] = stat.ctimeMs;
env.u32[(buf + 80) >> 2] = Number(stat.ctimeNs);
env.u64[(buf + 88) >> 3] = stat.ino;
return 0;
}
function utf8Length(string) {
let len = 0;
for (let i = 0; i < string.length; i++) {
let c = string.charCodeAt(i);
if (c >= 0xd800 && c <= 0xdbff && i < string.length - 1) {
let c2 = string.charCodeAt(++i);
if ((c2 & 0xfc00) === 0xdc00) {
c = ((c & 0x3ff) << 10) + (c2 & 0x3ff) + 0x10000;
} else {
// unmatched surrogate.
i--;
}
}
if ((c & 0xffffff80) === 0) {
len++;
} else if ((c & 0xfffff800) === 0) {
len += 2;
} else if ((c & 0xffff0000) === 0) {
len += 3;
} else if ((c & 0xffe00000) === 0) {
len += 4;
}
}
return len;
}
function align(len, p) {
return Math.ceil(len / p) * p;
}
let wasmBytes = fs.readFileSync(new URL('watcher.wasm', import.meta.url));
let wasmModule = new WebAssembly.Module(wasmBytes);
let instance = new WebAssembly.Instance(wasmModule, {
napi,
env: wasm_env,
wasi_snapshot_preview1: wasi
});
env = new Environment(instance);
let wrapper = createWrapper(env.exports);
export function writeSnapshot(dir, snapshot, opts) {
return wrapper.writeSnapshot(dir, snapshot, opts);
}
export function getEventsSince(dir, snapshot, opts) {
return wrapper.getEventsSince(dir, snapshot, opts);
}
export function subscribe(dir, fn, opts) {
return wrapper.subscribe(dir, fn, opts);
}
export function unsubscribe(dir, fn, opts) {
return wrapper.unsubscribe(dir, fn, opts);
}

View File

@@ -0,0 +1,100 @@
# napi-wasm
An implementation of the [napi](https://nodejs.org/api/n-api.html) API for WASM. Enables using some native Node modules in browsers and other environments.
## Setup
To use napi-wasm, there are a few requirements:
1. Configure your linker to export an indirect function table. With ldd, this is the `--export-table` flag. This enables JavaScript to call callback functions registered by WASM. It is exposed in the WebAssembly exports as `__indirect_function_table`.
2. Export a function from your WASM build named `napi_register_module_v1` (Node's default), or `napi_register_wasm_v1` for WASM-specific builds. This is called during initialization to setup the `exports` object for your module. It receives an environment and an exports object pointer as arguments, which you can add properties to.
3. Include a function named `napi_wasm_malloc` in your WASM build. This is called from JavaScript by napi-wasm to allocate memory in the WASM heap. It should accept a `uint32` size argument indicating the number of bytes to allocate, and return a `uint8` pointer to allocated memory.
4. Compile for the `wasm32-unknown-unknown` target.
### In Rust
The above steps should apply for any programming language, but here's an example in Rust. First, define a `napi_wasm_malloc` function so JavaScript can allocate memory in the WASM heap using the default allocator.
```rust
use std::alloc::{alloc, Layout};
#[no_mangle]
pub extern "C" fn napi_wasm_malloc(size: usize) -> *mut u8 {
let align = std::mem::align_of::<usize>();
if let Ok(layout) = Layout::from_size_align(size, align) {
unsafe {
if layout.size() > 0 {
let ptr = alloc(layout);
if !ptr.is_null() {
return ptr;
}
} else {
return align as *mut u8;
}
}
}
std::process::abort();
}
```
Next, implement `napi_register_wasm_v1` to register your module exports. We'll use the [napi-rs](https://github.com/napi-rs/napi-rs) bindings in this example to make it a bit nicer than calling C APIs directly. Note that the napi-rs `#[module_exports]` macro currently doesn't work in WASM because Rust doesn't support ctor setup functions in WASM targets yet, so we'll need to do this manually.
```rust
use napi::{Env, JsObject, NapiValue};
#[no_mangle]
pub unsafe extern "C" fn napi_register_wasm_v1(raw_env: napi::sys::napi_env, raw_exports: napi::sys::napi_value) {
let env = Env::from_raw(raw_env);
let exports = JsObject::from_raw_unchecked(raw_env, raw_exports);
exports.create_named_method("transform", transform);
}
#[js_function(1)]
fn transform(ctx: CallContext) -> napi::Result<JsUnknown> {
// ...
}
```
To compile, you need to export a function table and use the correct target.
```shell
RUSTFLAGS="-C link-arg=--export-table" cargo build --target wasm32-unknown-unknown
```
This will output a file in `target/wasm32-unknown-unknown/debug/YOUR_CRATE.wasm` which you can load in a JavaScript environment.
You can also put the rust flags in a `.cargo/config.toml` file so you don't need to provide the environment variable each time you run `cargo build`.
```toml
[target.wasm32-unknown-unknown]
rustflags = ["-C", "link-arg=--export-table"]
```
### Loading
To load a WASM file and initialize a napi environment, you'll need to import the `napi-wasm` package. You instantiate a WASM module as usual, providing `napi` as the `env` import key. This provides the napi functions for your WASM module to use.
Then, pass the WASM instance to the `Environment` constructor to setup a napi environment. This will call `napi_register_wasm_v1` or `napi_register_module_v1` to setup the exports object. Then you can call functions on the exports object as you would in Node.
```js
import { Environment, napi } from 'napi-wasm';
// Construct a URL and instantiate a WebAssembly module as usual.
const url = new URL('path/to/lib.wasm', import.meta.url);
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
env: napi
});
// Create an environment.
let env = new Environment(instance);
let exports = env.exports;
// Use exports as usual!
exports.transform({
// ...
});
```
When you are done with an `Environment`, call the `destroy()` function to clean up memory.

View File

File diff suppressed because it is too large Load Diff

View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,33 @@
{
"name": "napi-wasm",
"version": "1.1.0",
"description": "An implementation of napi for wasm",
"main": "index.mjs",
"exports": {
"import": "./index.mjs",
"require": "./index.js"
},
"files": [
"index.js",
"index.mjs"
],
"scripts": {
"prepublishOnly": "sed 's/^export //g' index.mjs > index.js && echo '\nexports.Environment = Environment;\nexports.napi = napi;' >> index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/devongovett/napi-wasm.git"
},
"keywords": [
"napi",
"wasm",
"node-api",
"rust"
],
"author": "Devon Govett <devongovett@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/devongovett/napi-wasm/issues"
},
"homepage": "https://github.com/devongovett/napi-wasm#readme"
}

View File

@@ -0,0 +1,44 @@
{
"name": "@parcel/watcher-wasm",
"version": "2.5.1",
"main": "index.mjs",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/watcher.git"
},
"description": "A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/parcel"
},
"files": [
"*.js",
"*.cjs",
"*.mjs",
"*.d.ts",
"*.wasm"
],
"engines": {
"node": ">= 10.0.0"
},
"dependencies": {
"napi-wasm": "^1.1.0",
"is-glob": "^4.0.3",
"micromatch": "^4.0.5"
},
"module": "index.mjs",
"sideEffects": false,
"exports": {
"types": "./index.d.ts",
"import": "./index.mjs",
"require": "./index.cjs"
},
"bundledDependencies": [
"napi-wasm"
]
}

View File

Binary file not shown.

View File

@@ -0,0 +1,77 @@
const path = require('path');
const micromatch = require('micromatch');
const isGlob = require('is-glob');
function normalizeOptions(dir, opts = {}) {
const { ignore, ...rest } = opts;
if (Array.isArray(ignore)) {
opts = { ...rest };
for (const value of ignore) {
if (isGlob(value)) {
if (!opts.ignoreGlobs) {
opts.ignoreGlobs = [];
}
const regex = micromatch.makeRe(value, {
// We set `dot: true` to workaround an issue with the
// regular expression on Linux where the resulting
// negative lookahead `(?!(\\/|^)` was never matching
// in some cases. See also https://bit.ly/3UZlQDm
dot: true,
// C++ does not support lookbehind regex patterns, they
// were only added later to JavaScript engines
// (https://bit.ly/3V7S6UL)
lookbehinds: false
});
opts.ignoreGlobs.push(regex.source);
} else {
if (!opts.ignorePaths) {
opts.ignorePaths = [];
}
opts.ignorePaths.push(path.resolve(dir, value));
}
}
}
return opts;
}
exports.createWrapper = (binding) => {
return {
writeSnapshot(dir, snapshot, opts) {
return binding.writeSnapshot(
path.resolve(dir),
path.resolve(snapshot),
normalizeOptions(dir, opts),
);
},
getEventsSince(dir, snapshot, opts) {
return binding.getEventsSince(
path.resolve(dir),
path.resolve(snapshot),
normalizeOptions(dir, opts),
);
},
async subscribe(dir, fn, opts) {
dir = path.resolve(dir);
opts = normalizeOptions(dir, opts);
await binding.subscribe(dir, fn, opts);
return {
unsubscribe() {
return binding.unsubscribe(dir, fn, opts);
},
};
},
unsubscribe(dir, fn, opts) {
return binding.unsubscribe(
path.resolve(dir),
fn,
normalizeOptions(dir, opts),
);
}
};
};