@wtf/platform-node 1.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -6
- package/dist/module.d.ts +10 -0
- package/dist/module.js +73 -0
- package/dist/module.js.map +1 -0
- package/dist/process.d.ts +4 -2
- package/dist/process.js +87 -22
- package/dist/process.js.map +1 -1
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -19,13 +19,15 @@ package in the repo that imports `node:` modules.
|
|
|
19
19
|
| `./terminal` | `nodeTerminal({ stdin, stdout, env })`: reads lines from stdin with `node:readline` |
|
|
20
20
|
| `./open` | `nodeOpen({ env })`, `spawnOpen({ spawn, platform, browser })`, `openerOf`: `BROWSER`, else the platform's app |
|
|
21
21
|
| `./process` | `nodeSpawn({ env })`: `spawn` from `node:child_process`, its pipes Web Streams by `Readable.toWeb` |
|
|
22
|
+
| `./module` | `nodeLoadModule(root, beside?)`: a module by its path from `root`, or a package `root` installs, else `beside` |
|
|
22
23
|
| `./runtime` | `runMain(program, { fetch, storage })`, `processInterrupts({ signals })`; `processExitCode` |
|
|
23
24
|
|
|
24
25
|
The standard members are Node's own: `setTimeout` and `clearTimeout` from `node:timers`,
|
|
25
26
|
`performance` from `node:perf_hooks`, `webcrypto` from `node:crypto`, the `console` from
|
|
26
27
|
`node:console`, and `BroadcastChannel` and `MessageChannel` from `node:worker_threads`. Node has no
|
|
27
|
-
`navigator.storage`, so your entry point passes one, such as
|
|
28
|
-
|
|
28
|
+
`navigator.storage`, so your entry point passes one, such as `nodeStorage()` from
|
|
29
|
+
`@wtf/platform-fs-node/storage`, whose root is `/`; this package depends on no file system. Node
|
|
30
|
+
has `fetch` only as a global, which nothing reads, so your entry point passes that too. `dirs.cwd` is
|
|
29
31
|
`process.cwd()`, read each time, so a `chdir` shows. Where Node differs from the standard, the
|
|
30
32
|
package adapts it: `nodeLocks` releases the lock when a callback throws, which Node 24 keeps held,
|
|
31
33
|
and `NodeWorker` makes a thread behave like a `Worker`, an `EventTarget` whose script is a URL.
|
|
@@ -39,14 +41,18 @@ each time it opens a URL, so a test sets it there:
|
|
|
39
41
|
`spawn` starts a process with `spawn` from `node:child_process`, with no shell, and turns its pipes
|
|
40
42
|
into Web Streams with `Readable.toWeb` and `Writable.toWeb`. A process gets the platform's `env`,
|
|
41
43
|
read as it starts, unless you give it an `env` of its own, and an aborted `signal` ends it with
|
|
42
|
-
`SIGTERM
|
|
44
|
+
`SIGTERM`; `kill(signal)` sends it one, as `SIGKILL` to a process that ignores `SIGTERM`. A command
|
|
45
|
+
that is not there rejects its `status` with a `SystemError` of `NotFound`; any other process Node
|
|
46
|
+
cannot start rejects it too, as of a link that loops or of arguments too long, where Node's own
|
|
47
|
+
`spawn` throws. Its `stdin` takes bytes alone, where `Writable.toWeb` takes text too: a chunk that
|
|
48
|
+
is no `Uint8Array` errors it, and closes the pipe.
|
|
43
49
|
|
|
44
50
|
```ts
|
|
45
51
|
import type { Platform } from '@wtf/platform'
|
|
46
52
|
import { exec } from '@wtf/platform/process'
|
|
47
53
|
import { runMain } from '@wtf/platform-node/runtime'
|
|
54
|
+
import { nodeStorage } from '@wtf/platform-fs-node/storage'
|
|
48
55
|
|
|
49
|
-
declare const storage: Platform['navigator']['storage'] // the root of your file system
|
|
50
56
|
declare const serve: (platform: Platform, options: { signal: AbortSignal }) => Promise<void>
|
|
51
57
|
|
|
52
58
|
await runMain(
|
|
@@ -55,7 +61,7 @@ await runMain(
|
|
|
55
61
|
platform.console.log('Serving %s at %s', stdout.trim(), platform.env['PORT'] ?? '3000')
|
|
56
62
|
await serve(platform, { signal })
|
|
57
63
|
},
|
|
58
|
-
{ fetch, storage }
|
|
64
|
+
{ fetch, storage: nodeStorage() }
|
|
59
65
|
)
|
|
60
66
|
```
|
|
61
67
|
|
|
@@ -64,5 +70,11 @@ give it. It aborts the program's signal at the first `SIGINT` or `SIGTERM`; a se
|
|
|
64
70
|
process, as Node does by default. It sets `process.exitCode` from how the program ended: 0, or the
|
|
65
71
|
`exitCode` of what it threw, or 1.
|
|
66
72
|
|
|
73
|
+
`nodeLoadModule(root, beside)` loads a module that a configuration names, as a plugin, by
|
|
74
|
+
`import()`. It reads a path from `root`, the root of your project. It looks for a package in
|
|
75
|
+
`root`, and else beside `beside`, as your tool gives its own `import.meta.url`, so a plugin
|
|
76
|
+
installed globally beside the tool is found too. A package found in neither is a `SystemError` of
|
|
77
|
+
`NotFound`. The function it gives is a `LoadModule` of `@wtf/platform/module`.
|
|
78
|
+
|
|
67
79
|
It uses Node's own modules and runs only in Node. The file system is for your entry point to pass
|
|
68
|
-
in
|
|
80
|
+
in, from `@wtf/platform-fs-node`.
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LoadModule } from '@wtf/platform/module';
|
|
2
|
+
/**
|
|
3
|
+
* Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as
|
|
4
|
+
* it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry
|
|
5
|
+
* point gives it; a package, the one `root` installs, else the one installed beside `beside`, as
|
|
6
|
+
* a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is
|
|
7
|
+
* found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is
|
|
8
|
+
* imported fails of the error of the import.
|
|
9
|
+
*/
|
|
10
|
+
export declare const nodeLoadModule: (root: string, beside?: string | URL) => LoadModule;
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Node's loader of modules: {@link nodeLoadModule}, which loads the module a name or a path names
|
|
11
|
+
* by Node's resolution and `import()`, looked for from the root of a project, then beside a tool,
|
|
12
|
+
* as a configuration names a plugin of the tool. It reads nothing of the process: the entry point
|
|
13
|
+
* gives the root.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { nodeLoadModule } from '@wtf/platform-node/module'
|
|
17
|
+
*
|
|
18
|
+
* declare const root: string
|
|
19
|
+
*
|
|
20
|
+
* const load = nodeLoadModule(root, import.meta.url)
|
|
21
|
+
* const formatters = await load('./formatters.mjs')
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
import { createRequire } from 'node:module';
|
|
27
|
+
import { isAbsolute, join } from 'node:path';
|
|
28
|
+
import { pathToFileURL } from 'node:url';
|
|
29
|
+
import { SystemError } from '@wtf/platform/errors';
|
|
30
|
+
/**
|
|
31
|
+
* The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or
|
|
32
|
+
* `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else
|
|
33
|
+
* the one installed beside `beside`, as of a global install of a tool and its plugins. A package
|
|
34
|
+
* found in neither is a `SystemError` of `NotFound`, of the error of `root`'s resolve.
|
|
35
|
+
*/
|
|
36
|
+
function fileOf(module, root, beside) {
|
|
37
|
+
if (isAbsolute(module))
|
|
38
|
+
return module;
|
|
39
|
+
if (/^\.{1,2}\//.test(module))
|
|
40
|
+
return join(root, module);
|
|
41
|
+
try {
|
|
42
|
+
return createRequire(join(root, 'package.json')).resolve(module);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (beside !== undefined)
|
|
46
|
+
try {
|
|
47
|
+
return createRequire(beside).resolve(module);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Not beside the tool either: the cause is the root's, where a project installs it.
|
|
51
|
+
}
|
|
52
|
+
throw new SystemError({
|
|
53
|
+
reason: 'NotFound',
|
|
54
|
+
module: 'LoadModule',
|
|
55
|
+
method: 'load',
|
|
56
|
+
pathOrDescriptor: module,
|
|
57
|
+
description: beside === undefined
|
|
58
|
+
? `No package ${module} is installed in ${root}.`
|
|
59
|
+
: `No package ${module} is installed in ${root}, nor beside ${String(beside)}.`,
|
|
60
|
+
cause: error
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as
|
|
66
|
+
* it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry
|
|
67
|
+
* point gives it; a package, the one `root` installs, else the one installed beside `beside`, as
|
|
68
|
+
* a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is
|
|
69
|
+
* found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is
|
|
70
|
+
* imported fails of the error of the import.
|
|
71
|
+
*/
|
|
72
|
+
export const nodeLoadModule = (root, beside) => async (module) => import(__rewriteRelativeImportExtension(pathToFileURL(fileOf(module, root, beside)).href));
|
|
73
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAGlD;;;;;GAKG;AACH,SAAS,MAAM,CAAC,MAAc,EAAE,IAAY,EAAE,MAAgC;IAC5E,IAAI,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAA;IACrC,IAAI,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACxD,IAAI,CAAC;QACH,OAAO,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAClE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,KAAK,SAAS;YACtB,IAAI,CAAC;gBACH,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;YAC9C,CAAC;YAAC,MAAM,CAAC;gBACP,oFAAoF;YACtF,CAAC;QACH,MAAM,IAAI,WAAW,CAAC;YACpB,MAAM,EAAE,UAAU;YAClB,MAAM,EAAE,YAAY;YACpB,MAAM,EAAE,MAAM;YACd,gBAAgB,EAAE,MAAM;YACxB,WAAW,EACT,MAAM,KAAK,SAAS;gBAClB,CAAC,CAAC,cAAc,MAAM,oBAAoB,IAAI,GAAG;gBACjD,CAAC,CAAC,cAAc,MAAM,oBAAoB,IAAI,gBAAgB,MAAM,CAAC,MAAM,CAAC,GAAG;YACnF,KAAK,EAAE,KAAK;SACb,CAAC,CAAA;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,CAAC,IAAY,EAAE,MAAqB,EAAc,EAAE,CACpD,KAAK,EAAE,MAAM,EAAE,EAAE,CACf,MAAM,kCAAC,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,EAAC,CAAA","sourcesContent":["/**\n * Node's loader of modules: {@link nodeLoadModule}, which loads the module a name or a path names\n * by Node's resolution and `import()`, looked for from the root of a project, then beside a tool,\n * as a configuration names a plugin of the tool. It reads nothing of the process: the entry point\n * gives the root.\n *\n * ```ts\n * import { nodeLoadModule } from '@wtf/platform-node/module'\n *\n * declare const root: string\n *\n * const load = nodeLoadModule(root, import.meta.url)\n * const formatters = await load('./formatters.mjs')\n * ```\n *\n * @packageDocumentation\n */\nimport { createRequire } from 'node:module'\nimport { isAbsolute, join } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nimport { SystemError } from '@wtf/platform/errors'\nimport type { LoadModule } from '@wtf/platform/module'\n\n/**\n * The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or\n * `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else\n * the one installed beside `beside`, as of a global install of a tool and its plugins. A package\n * found in neither is a `SystemError` of `NotFound`, of the error of `root`'s resolve.\n */\nfunction fileOf(module: string, root: string, beside: string | URL | undefined): string {\n if (isAbsolute(module)) return module\n if (/^\\.{1,2}\\//.test(module)) return join(root, module)\n try {\n return createRequire(join(root, 'package.json')).resolve(module)\n } catch (error) {\n if (beside !== undefined)\n try {\n return createRequire(beside).resolve(module)\n } catch {\n // Not beside the tool either: the cause is the root's, where a project installs it.\n }\n throw new SystemError({\n reason: 'NotFound',\n module: 'LoadModule',\n method: 'load',\n pathOrDescriptor: module,\n description:\n beside === undefined\n ? `No package ${module} is installed in ${root}.`\n : `No package ${module} is installed in ${root}, nor beside ${String(beside)}.`,\n cause: error\n })\n }\n}\n\n/**\n * Node's {@link LoadModule}, which loads a module by `import()` of its file: an absolute path as\n * it is; a path from the root, `./` or `../`, of `root`, the root of the project, as the entry\n * point gives it; a package, the one `root` installs, else the one installed beside `beside`, as\n * a tool gives its own `import.meta.url`, so a package a global install puts beside the tool is\n * found too. A package found in neither is a `SystemError` of `NotFound`; what fails as it is\n * imported fails of the error of the import.\n */\nexport const nodeLoadModule =\n (root: string, beside?: string | URL): LoadModule =>\n async (module) =>\n import(pathToFileURL(fileOf(module, root, beside)).href)\n"]}
|
package/dist/process.d.ts
CHANGED
|
@@ -5,8 +5,10 @@ import { type Spawn } from '@wtf/platform/process';
|
|
|
5
5
|
* pipes a Web Stream, by `Writable.toWeb` and `Readable.toWeb`. A process is given its options'
|
|
6
6
|
* `env` whole, or else `env` of the platform, its variables that are set, or else Node's own; and
|
|
7
7
|
* works in its options' `cwd`, or else the process's. Its `signal`, aborted, ends it by `SIGTERM`,
|
|
8
|
-
* and its status says so: `{ code: 143, signal: 'SIGTERM' }
|
|
9
|
-
*
|
|
8
|
+
* and its status says so: `{ code: 143, signal: 'SIGTERM' }`; its `kill` sends it a signal, as
|
|
9
|
+
* `SIGKILL` to one that ignores `SIGTERM`. One that could not start rejects its status with a
|
|
10
|
+
* `SystemError`, `NotFound` of a command not there, whether Node gives its error as an event or
|
|
11
|
+
* throws it, as of `ELOOP` or `E2BIG`. Its stdin takes bytes alone.
|
|
10
12
|
*
|
|
11
13
|
* ```ts
|
|
12
14
|
* const spawn = nodeSpawn({ env: platform.env })
|
package/dist/process.js
CHANGED
|
@@ -1,27 +1,78 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Node's processes: {@link nodeSpawn}, the `spawn` of `@wtf/platform/process` over `spawn` of
|
|
3
|
-
* `node:child_process`, its pipes Web Streams by `Readable.toWeb` and `Writable.toWeb
|
|
3
|
+
* `node:child_process`, its pipes Web Streams by `Readable.toWeb` and `Writable.toWeb`, and its
|
|
4
|
+
* `kill` the child's.
|
|
4
5
|
*
|
|
5
6
|
* @packageDocumentation
|
|
6
7
|
*/
|
|
7
8
|
import { spawn as spawnChild } from 'node:child_process';
|
|
8
9
|
import { constants } from 'node:os';
|
|
9
10
|
import { Readable, Writable } from 'node:stream';
|
|
10
|
-
import { notStarted } from '@wtf/platform/process';
|
|
11
|
+
import { notStarted, sendable } from '@wtf/platform/process';
|
|
11
12
|
/** The variables of `env` that are set, as a process is given them. */
|
|
12
13
|
const definedOf = (env) => Object.fromEntries(Object.entries(env).filter((entry) => entry[1] !== undefined));
|
|
13
14
|
/** The reason of an error of a process that did not start, by its code. */
|
|
14
15
|
const reasonOf = (error) => {
|
|
15
|
-
const code = Reflect.get(error, 'code');
|
|
16
|
+
const code = typeof error === 'object' && error !== null ? Reflect.get(error, 'code') : undefined;
|
|
16
17
|
return code === 'ENOENT' ? 'NotFound' : code === 'EACCES' ? 'PermissionDenied' : 'Unknown';
|
|
17
18
|
};
|
|
19
|
+
/** Whether `chunk` is bytes, a `Uint8Array`, a `Buffer` too, of any realm. */
|
|
20
|
+
const isBytes = (chunk) => ArrayBuffer.isView(chunk) && Object.prototype.toString.call(chunk) === '[object Uint8Array]';
|
|
21
|
+
/**
|
|
22
|
+
* The stdin of a child, `Writable.toWeb` of its pipe, of bytes alone: a chunk that is none rejects
|
|
23
|
+
* its write with a `TypeError`, errors the stream, and closes the pipe, what was written before
|
|
24
|
+
* flushed, so the process sees its input end; as `Writable.toWeb` alone takes text, and leaves the
|
|
25
|
+
* pipe open of a chunk it refuses.
|
|
26
|
+
*/
|
|
27
|
+
const stdinOf = (pipe) => {
|
|
28
|
+
const writer = Writable.toWeb(pipe).getWriter();
|
|
29
|
+
return new WritableStream({
|
|
30
|
+
write: async (chunk) => {
|
|
31
|
+
if (!isBytes(chunk)) {
|
|
32
|
+
// A process that ended has closed its pipe already.
|
|
33
|
+
await writer.close().catch(() => { });
|
|
34
|
+
throw new TypeError('A process reads bytes: the chunk is no Uint8Array.');
|
|
35
|
+
}
|
|
36
|
+
await writer.write(chunk);
|
|
37
|
+
},
|
|
38
|
+
close: () => writer.close(),
|
|
39
|
+
abort: (reason) => writer.abort(reason)
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
/** A stream of bytes that has ended, of none. */
|
|
43
|
+
const ended = () => new ReadableStream({
|
|
44
|
+
start: (controller) => controller.close()
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* The process of `command`, which Node refused to start as it was spawned, by `error`: its outputs
|
|
48
|
+
* ended, its stdin errored, and its status rejected, as of one that could not start.
|
|
49
|
+
*/
|
|
50
|
+
const notSpawned = (command, error) => {
|
|
51
|
+
const failure = notStarted(command, reasonOf(error), error);
|
|
52
|
+
const status = Promise.reject(failure);
|
|
53
|
+
// A caller that reads the output alone hears of a process that could not start by its end.
|
|
54
|
+
status.catch(() => { });
|
|
55
|
+
return {
|
|
56
|
+
stdin: new WritableStream({
|
|
57
|
+
start: (controller) => controller.error(failure)
|
|
58
|
+
}),
|
|
59
|
+
stdout: ended(),
|
|
60
|
+
stderr: ended(),
|
|
61
|
+
status,
|
|
62
|
+
kill: (signal) => {
|
|
63
|
+
sendable(signal ?? 'SIGTERM');
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
18
67
|
/**
|
|
19
68
|
* Node's `spawn`: starts `command` by `spawn` of `node:child_process`, with no shell, each of its
|
|
20
69
|
* pipes a Web Stream, by `Writable.toWeb` and `Readable.toWeb`. A process is given its options'
|
|
21
70
|
* `env` whole, or else `env` of the platform, its variables that are set, or else Node's own; and
|
|
22
71
|
* works in its options' `cwd`, or else the process's. Its `signal`, aborted, ends it by `SIGTERM`,
|
|
23
|
-
* and its status says so: `{ code: 143, signal: 'SIGTERM' }
|
|
24
|
-
*
|
|
72
|
+
* and its status says so: `{ code: 143, signal: 'SIGTERM' }`; its `kill` sends it a signal, as
|
|
73
|
+
* `SIGKILL` to one that ignores `SIGTERM`. One that could not start rejects its status with a
|
|
74
|
+
* `SystemError`, `NotFound` of a command not there, whether Node gives its error as an event or
|
|
75
|
+
* throws it, as of `ELOOP` or `E2BIG`. Its stdin takes bytes alone.
|
|
25
76
|
*
|
|
26
77
|
* ```ts
|
|
27
78
|
* const spawn = nodeSpawn({ env: platform.env })
|
|
@@ -29,34 +80,48 @@ const reasonOf = (error) => {
|
|
|
29
80
|
* ```
|
|
30
81
|
*/
|
|
31
82
|
export const nodeSpawn = ({ env } = {}) => (command, args, { cwd, env: given, signal } = {}) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
?
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
83
|
+
let child;
|
|
84
|
+
try {
|
|
85
|
+
child = spawnChild(command, [...args], {
|
|
86
|
+
...(cwd === undefined ? {} : { cwd }),
|
|
87
|
+
...(given === undefined
|
|
88
|
+
? env === undefined
|
|
89
|
+
? {}
|
|
90
|
+
: { env: definedOf(env) }
|
|
91
|
+
: { env: { ...given } }),
|
|
92
|
+
stdio: 'pipe',
|
|
93
|
+
windowsHide: true
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
// Node throws some errors of a process it could not start, and gives the others as an event.
|
|
98
|
+
return notSpawned(command, error);
|
|
99
|
+
}
|
|
100
|
+
const kill = (name) => {
|
|
101
|
+
const sent = sendable(name ?? 'SIGTERM');
|
|
102
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
103
|
+
child.kill(sent);
|
|
104
|
+
};
|
|
105
|
+
const abort = () => kill('SIGTERM');
|
|
43
106
|
const status = new Promise((resolve, reject) => {
|
|
44
|
-
|
|
107
|
+
// Each error, not the first alone: Node gives one of a signal it could not send too.
|
|
108
|
+
child.on('error', (error) => reject(notStarted(command, reasonOf(error), error)));
|
|
45
109
|
child.once('exit', (code, name) => resolve(name === null
|
|
46
110
|
? { code: code ?? 0 }
|
|
47
111
|
: { code: 128 + (constants.signals[name] ?? 0), signal: name }));
|
|
48
|
-
}).finally(() => signal?.removeEventListener('abort',
|
|
112
|
+
}).finally(() => signal?.removeEventListener('abort', abort));
|
|
49
113
|
// A caller that reads the output alone hears of a process that could not start by its end.
|
|
50
114
|
status.catch(() => { });
|
|
51
115
|
if (signal?.aborted === true)
|
|
52
|
-
|
|
116
|
+
abort();
|
|
53
117
|
else
|
|
54
|
-
signal?.addEventListener('abort',
|
|
118
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
55
119
|
return {
|
|
56
|
-
stdin:
|
|
120
|
+
stdin: stdinOf(child.stdin),
|
|
57
121
|
stdout: Readable.toWeb(child.stdout),
|
|
58
122
|
stderr: Readable.toWeb(child.stderr),
|
|
59
|
-
status
|
|
123
|
+
status,
|
|
124
|
+
kill
|
|
60
125
|
};
|
|
61
126
|
};
|
|
62
127
|
//# sourceMappingURL=process.js.map
|
package/dist/process.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAGhD,OAAO,EAAE,UAAU,EAAkC,MAAM,uBAAuB,CAAA;AAElF,uEAAuE;AACvE,MAAM,SAAS,GAAG,CAAC,GAAQ,EAA0B,EAAE,CACrD,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAA6B,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CACzF,CAAA;AAEH,2EAA2E;AAC3E,MAAM,QAAQ,GAAG,CAAC,KAAY,EAAE,EAAE;IAChC,MAAM,IAAI,GAAY,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;IAChD,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,SAAS,CAAA;AAC5F,CAAC,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,SAAS,GACpB,CAAC,EAAE,GAAG,KAA6B,EAAE,EAAS,EAAE,CAChD,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE;IAClD,MAAM,KA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AC,CAAC,CAAA;IAC5D,2FAA2F;IAC3F,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACtB,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI;QAAE,IAAI,EAAE,CAAA;;QAC/B,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAC5D,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAA+B;QAChE,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAA+B;QAClE,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAA+B;QAClE,MAAM;KACP,CAAA;AACH,CAAC,CAAA","sourcesContent":["/**\n * Node's processes: {@link nodeSpawn}, the `spawn` of `@wtf/platform/process` over `spawn` of\n * `node:child_process`, its pipes Web Streams by `Readable.toWeb` and `Writable.toWeb`.\n *\n * @packageDocumentation\n */\nimport { spawn as spawnChild } from 'node:child_process'\nimport { constants } from 'node:os'\nimport { Readable, Writable } from 'node:stream'\n\nimport type { Env } from '@wtf/platform/env'\nimport { notStarted, type ProcessStatus, type Spawn } from '@wtf/platform/process'\n\n/** The variables of `env` that are set, as a process is given them. */\nconst definedOf = (env: Env): Record<string, string> =>\n Object.fromEntries(\n Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined)\n )\n\n/** The reason of an error of a process that did not start, by its code. */\nconst reasonOf = (error: Error) => {\n const code: unknown = Reflect.get(error, 'code')\n return code === 'ENOENT' ? 'NotFound' : code === 'EACCES' ? 'PermissionDenied' : 'Unknown'\n}\n\n/**\n * Node's `spawn`: starts `command` by `spawn` of `node:child_process`, with no shell, each of its\n * pipes a Web Stream, by `Writable.toWeb` and `Readable.toWeb`. A process is given its options'\n * `env` whole, or else `env` of the platform, its variables that are set, or else Node's own; and\n * works in its options' `cwd`, or else the process's. Its `signal`, aborted, ends it by `SIGTERM`,\n * and its status says so: `{ code: 143, signal: 'SIGTERM' }`. One that could not start rejects\n * its status with a `SystemError`, `NotFound` of a command not there.\n *\n * ```ts\n * const spawn = nodeSpawn({ env: platform.env })\n * const { stdout } = await exec(spawn, 'git', ['rev-parse', 'HEAD'])\n * ```\n */\nexport const nodeSpawn =\n ({ env }: { readonly env?: Env } = {}): Spawn =>\n (command, args, { cwd, env: given, signal } = {}) => {\n const child = spawnChild(command, [...args], {\n ...(cwd === undefined ? {} : { cwd }),\n ...(given === undefined\n ? env === undefined\n ? {}\n : { env: definedOf(env) }\n : { env: { ...given } }),\n stdio: 'pipe',\n windowsHide: true\n })\n const kill = () => child.kill('SIGTERM')\n const status = new Promise<ProcessStatus>((resolve, reject) => {\n child.once('error', (error) => reject(notStarted(command, reasonOf(error), error)))\n child.once('exit', (code, name) =>\n resolve(\n name === null\n ? { code: code ?? 0 }\n : { code: 128 + (constants.signals[name] ?? 0), signal: name }\n )\n )\n }).finally(() => signal?.removeEventListener('abort', kill))\n // A caller that reads the output alone hears of a process that could not start by its end.\n status.catch(() => {})\n if (signal?.aborted === true) kill()\n else signal?.addEventListener('abort', kill, { once: true })\n return {\n stdin: Writable.toWeb(child.stdin) as WritableStream<Uint8Array>,\n stdout: Readable.toWeb(child.stdout) as ReadableStream<Uint8Array>,\n stderr: Readable.toWeb(child.stderr) as ReadableStream<Uint8Array>,\n status\n }\n }\n"]}
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAGhD,OAAO,EAEL,UAAU,EAEV,QAAQ,EAET,MAAM,uBAAuB,CAAA;AA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AAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAA;QACjF,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAChC,OAAO,CACL,IAAI,KAAK,IAAI;YACX,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC,EAAE;YACrB,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CACjE,CACF,CAAA;IACH,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAA;IAC7D,2FAA2F;IAC3F,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACtB,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI;QAAE,KAAK,EAAE,CAAA;;QAChC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAC7D,OAAO;QACL,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;QAC3B,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAA+B;QAClE,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAA+B;QAClE,MAAM;QACN,IAAI;KACL,CAAA;AACH,CAAC,CAAA","sourcesContent":["/**\n * Node's processes: {@link nodeSpawn}, the `spawn` of `@wtf/platform/process` over `spawn` of\n * `node:child_process`, its pipes Web Streams by `Readable.toWeb` and `Writable.toWeb`, and its\n * `kill` the child's.\n *\n * @packageDocumentation\n */\nimport { spawn as spawnChild } from 'node:child_process'\nimport { constants } from 'node:os'\nimport { Readable, Writable } from 'node:stream'\n\nimport type { Env } from '@wtf/platform/env'\nimport {\n type ChildProcess,\n notStarted,\n type ProcessStatus,\n sendable,\n type Spawn\n} from '@wtf/platform/process'\n\n/** The variables of `env` that are set, as a process is given them. */\nconst definedOf = (env: Env): Record<string, string> =>\n Object.fromEntries(\n Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined)\n )\n\n/** The reason of an error of a process that did not start, by its code. */\nconst reasonOf = (error: unknown) => {\n const code: unknown =\n typeof error === 'object' && error !== null ? Reflect.get(error, 'code') : undefined\n return code === 'ENOENT' ? 'NotFound' : code === 'EACCES' ? 'PermissionDenied' : 'Unknown'\n}\n\n/** Whether `chunk` is bytes, a `Uint8Array`, a `Buffer` too, of any realm. */\nconst isBytes = (chunk: unknown): chunk is Uint8Array =>\n ArrayBuffer.isView(chunk) && Object.prototype.toString.call(chunk) === '[object Uint8Array]'\n\n/**\n * The stdin of a child, `Writable.toWeb` of its pipe, of bytes alone: a chunk that is none rejects\n * its write with a `TypeError`, errors the stream, and closes the pipe, what was written before\n * flushed, so the process sees its input end; as `Writable.toWeb` alone takes text, and leaves the\n * pipe open of a chunk it refuses.\n */\nconst stdinOf = (pipe: Writable): WritableStream<Uint8Array> => {\n const writer = (Writable.toWeb(pipe) as WritableStream<Uint8Array>).getWriter()\n return new WritableStream<Uint8Array>({\n write: async (chunk: unknown) => {\n if (!isBytes(chunk)) {\n // A process that ended has closed its pipe already.\n await writer.close().catch(() => {})\n throw new TypeError('A process reads bytes: the chunk is no Uint8Array.')\n }\n await writer.write(chunk)\n },\n close: () => writer.close(),\n abort: (reason) => writer.abort(reason)\n })\n}\n\n/** A stream of bytes that has ended, of none. */\nconst ended = () =>\n new ReadableStream<Uint8Array>({\n start: (controller) => controller.close()\n })\n\n/**\n * The process of `command`, which Node refused to start as it was spawned, by `error`: its outputs\n * ended, its stdin errored, and its status rejected, as of one that could not start.\n */\nconst notSpawned = (command: string, error: unknown): ChildProcess => {\n const failure = notStarted(command, reasonOf(error), error)\n const status = Promise.reject(failure)\n // A caller that reads the output alone hears of a process that could not start by its end.\n status.catch(() => {})\n return {\n stdin: new WritableStream<Uint8Array>({\n start: (controller) => controller.error(failure)\n }),\n stdout: ended(),\n stderr: ended(),\n status,\n kill: (signal) => {\n sendable(signal ?? 'SIGTERM')\n }\n }\n}\n\n/**\n * Node's `spawn`: starts `command` by `spawn` of `node:child_process`, with no shell, each of its\n * pipes a Web Stream, by `Writable.toWeb` and `Readable.toWeb`. A process is given its options'\n * `env` whole, or else `env` of the platform, its variables that are set, or else Node's own; and\n * works in its options' `cwd`, or else the process's. Its `signal`, aborted, ends it by `SIGTERM`,\n * and its status says so: `{ code: 143, signal: 'SIGTERM' }`; its `kill` sends it a signal, as\n * `SIGKILL` to one that ignores `SIGTERM`. One that could not start rejects its status with a\n * `SystemError`, `NotFound` of a command not there, whether Node gives its error as an event or\n * throws it, as of `ELOOP` or `E2BIG`. Its stdin takes bytes alone.\n *\n * ```ts\n * const spawn = nodeSpawn({ env: platform.env })\n * const { stdout } = await exec(spawn, 'git', ['rev-parse', 'HEAD'])\n * ```\n */\nexport const nodeSpawn =\n ({ env }: { readonly env?: Env } = {}): Spawn =>\n (command, args, { cwd, env: given, signal } = {}) => {\n let child\n try {\n child = spawnChild(command, [...args], {\n ...(cwd === undefined ? {} : { cwd }),\n ...(given === undefined\n ? env === undefined\n ? {}\n : { env: definedOf(env) }\n : { env: { ...given } }),\n stdio: 'pipe',\n windowsHide: true\n })\n } catch (error) {\n // Node throws some errors of a process it could not start, and gives the others as an event.\n return notSpawned(command, error)\n }\n const kill: ChildProcess['kill'] = (name) => {\n const sent = sendable(name ?? 'SIGTERM')\n if (child.exitCode === null && child.signalCode === null) child.kill(sent)\n }\n const abort = () => kill('SIGTERM')\n const status = new Promise<ProcessStatus>((resolve, reject) => {\n // Each error, not the first alone: Node gives one of a signal it could not send too.\n child.on('error', (error) => reject(notStarted(command, reasonOf(error), error)))\n child.once('exit', (code, name) =>\n resolve(\n name === null\n ? { code: code ?? 0 }\n : { code: 128 + (constants.signals[name] ?? 0), signal: name }\n )\n )\n }).finally(() => signal?.removeEventListener('abort', abort))\n // A caller that reads the output alone hears of a process that could not start by its end.\n status.catch(() => {})\n if (signal?.aborted === true) abort()\n else signal?.addEventListener('abort', abort, { once: true })\n return {\n stdin: stdinOf(child.stdin),\n stdout: Readable.toWeb(child.stdout) as ReadableStream<Uint8Array>,\n stderr: Readable.toWeb(child.stderr) as ReadableStream<Uint8Array>,\n status,\n kill\n }\n }\n"]}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtf/platform-node",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "Node's platform: nodePlatform({ fetch, storage }) builds it from Node's own modules, with spawn over node:child_process, and runMain runs your program with it, stopping on SIGINT or SIGTERM.",
|
|
5
|
-
"homepage": "https://gitlab.com/wetryfun/platform/platform/-/tree/main/packages/node",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Node's platform: nodePlatform({ fetch, storage }) builds it from Node's own modules, with spawn over node:child_process, and runMain runs your program with it, stopping on SIGINT or SIGTERM; nodeLoadModule loads a module by its path or package name, from a project's root.",
|
|
5
|
+
"homepage": "https://gitlab.com/wetryfun/platform/platform/-/tree/main/packages/platform-node",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://gitlab.com/wetryfun/platform/platform/-/issues"
|
|
8
8
|
},
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
13
|
"url": "git+https://gitlab.com/wetryfun/platform/platform.git",
|
|
14
|
-
"directory": "packages/node"
|
|
14
|
+
"directory": "packages/platform-node"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"dist"
|
|
@@ -50,6 +50,10 @@
|
|
|
50
50
|
"types": "./dist/open.d.ts",
|
|
51
51
|
"default": "./dist/open.js"
|
|
52
52
|
},
|
|
53
|
+
"./module": {
|
|
54
|
+
"types": "./dist/module.d.ts",
|
|
55
|
+
"default": "./dist/module.js"
|
|
56
|
+
},
|
|
53
57
|
"./process": {
|
|
54
58
|
"types": "./dist/process.d.ts",
|
|
55
59
|
"default": "./dist/process.js"
|
|
@@ -74,9 +78,10 @@
|
|
|
74
78
|
"build": "tsc -b"
|
|
75
79
|
},
|
|
76
80
|
"dependencies": {
|
|
77
|
-
"@wtf/platform": "1.0
|
|
81
|
+
"@wtf/platform": "2.1.0"
|
|
78
82
|
},
|
|
79
83
|
"devDependencies": {
|
|
84
|
+
"@wtf/platform-suite": "2.1.0",
|
|
80
85
|
"@wtf/project-wpt": "^1.0.0"
|
|
81
86
|
},
|
|
82
87
|
"engines": {
|