nano-spawn-compat 2.0.1 → 2.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nano-spawn-compat",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Tiny process execution for humans — a better child_process",
5
5
  "license": "MIT",
6
6
  "repository": "leonsilicon/nano-spawn-compat",
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- > This repo is a fork of [nano-spawn](https://github.com/sindresorhus/nano-spawn) by [@sindresorhus](https://github.com/sindresorhus) without depending on `node:readline` and `node:util` in order to support the [QuickJS-based LLRT runtime](https://github.com/awslabs/llrt).
1
+ > This repo is a fork of [nano-spawn](https://github.com/sindresorhus/nano-spawn) by [@sindresorhus](https://github.com/sindresorhus) without depending on `node:readline`, `node:events`, and `node:util` in order to support the [QuickJS-based LLRT runtime](https://github.com/awslabs/llrt).
2
2
  >
3
3
  > Why? Because this package is otherwise perfect for the LLRT runtime since it only relies on `child_process.spawn` which is currently the only [implemented export in `llrt_child_process`](https://github.com/awslabs/llrt/blob/36bd4a837d3fd85b5e3246906f23b1873c067503/modules/llrt_child_process/src/lib.rs#L580).
4
4
 
package/source/once.js ADDED
@@ -0,0 +1,68 @@
1
+ export function once(emitter, event) {
2
+ return new Promise((resolve, reject) => {
3
+ function onEvent(...arguments_) {
4
+ cleanup();
5
+ resolve(arguments_);
6
+ }
7
+
8
+ function onError(error) {
9
+ cleanup();
10
+ reject(error);
11
+ }
12
+
13
+ function cleanup() {
14
+ if (emitter.off) {
15
+ emitter.off(event, onEvent);
16
+ emitter.off('error', onError);
17
+ }
18
+
19
+ if (emitter.removeListener) {
20
+ emitter.removeListener(event, onEvent);
21
+ emitter.removeListener('error', onError);
22
+ }
23
+ }
24
+
25
+ emitter.on(event, onEvent);
26
+
27
+ if (event !== 'error') {
28
+ emitter.on('error', onError);
29
+ }
30
+ });
31
+ }
32
+
33
+ export async function * on(emitter, event) {
34
+ const queue = [];
35
+ let resolve_;
36
+
37
+ function handler(...arguments_) {
38
+ if (resolve_) {
39
+ resolve_(arguments_);
40
+ resolve_ = null;
41
+ } else {
42
+ queue.push(arguments_);
43
+ }
44
+ }
45
+
46
+ emitter.on(event, handler);
47
+
48
+ try {
49
+ while (true) {
50
+ if (queue.length > 0) {
51
+ yield queue.shift();
52
+ } else {
53
+ // eslint-disable-next-line no-await-in-loop
54
+ yield await new Promise(resolve => {
55
+ resolve_ = resolve;
56
+ });
57
+ }
58
+ }
59
+ } finally {
60
+ if (emitter.off) {
61
+ emitter.off(event, handler);
62
+ }
63
+
64
+ if (emitter.removeListener) {
65
+ emitter.removeListener(event, handler);
66
+ }
67
+ }
68
+ }
package/source/result.js CHANGED
@@ -1,5 +1,5 @@
1
- import {once, on} from 'node:events';
2
1
  import process from 'node:process';
2
+ import {once, on} from './once.js';
3
3
 
4
4
  export const getResult = async (nodeChildProcess, {input}, context) => {
5
5
  const instance = await nodeChildProcess;
package/source/spawn.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import {spawn} from 'node:child_process';
2
- import {once} from 'node:events';
3
- import process from 'node:process';
2
+ import {once} from './once.js';
4
3
  import {applyForceShell} from './windows.js';
5
4
  import {getResultError} from './result.js';
6
5
 
@@ -10,10 +9,10 @@ export const spawnSubprocess = async (file, commandArguments, options, context)
10
9
  // Not applied with file paths to `.../node` since those indicate a clear intent to use a specific Node version.
11
10
  // This also provides a way to opting out, e.g. using `process.execPath` instead of `node` to discard current CLI flags.
12
11
  // Does not work with shebangs, but those don't work cross-platform anyway.
13
- if (['node', 'node.exe'].includes(file.toLowerCase())) {
14
- file = process.execPath;
15
- commandArguments = [...process.execArgv.filter(flag => !flag.startsWith('--inspect')), ...commandArguments];
16
- }
12
+ // if (['node', 'node.exe'].includes(file.toLowerCase())) {
13
+ // file = process.execPath;
14
+ // commandArguments = [...process.execArgv.filter(flag => !flag.startsWith('--inspect')), ...commandArguments];
15
+ // }
17
16
 
18
17
  [file, commandArguments, options] = await applyForceShell(file, commandArguments, options);
19
18
  [file, commandArguments, options] = concatenateShell(file, commandArguments, options);