@gjsify/child_process 0.4.13 → 0.4.15

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/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- import"./_virtual/_rolldown/runtime.js";import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{EventEmitter as n}from"node:events";import{Buffer as r}from"node:buffer";import{Readable as i}from"node:stream";import{deferEmit as a,ensureMainLoop as o,gbytesToUint8Array as s}from"@gjsify/utils";var GioInputStreamReadable=class extends i{_stream;_cancellable=new e.Cancellable;constructor(e){super(),this._stream=e}_read(e){this._stream.read_bytes_async(Math.max(e,4096),t.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{let e=this._stream.read_bytes_finish(t).get_data();!e||e.length===0?this.push(null):this.push(r.from(e))}catch(e){this._cancellable.is_cancelled()||this.destroy(e)}})}_destroy(e,t){this._cancellable.cancel(),t(e)}};const c=new Set;var ChildProcess=class extends n{pid;exitCode=null;signalCode=null;killed=!1;connected=!1;stdin=null;stdout=null;stderr=null;_subprocess=null;_setSubprocess(e){this._subprocess=e;let t=e.get_identifier();t&&(this.pid=parseInt(t,10))}kill(e){if(!this._subprocess)return!1;try{return e===`SIGKILL`||e===9?this._subprocess.force_exit():this._subprocess.send_signal(typeof e==`number`?e:15),this.killed=!0,!0}catch{return!1}}ref(){return this}unref(){return this}};function _spawnSubprocess(t,n,r){let i=new e.SubprocessLauncher({flags:n});if(r?.cwd&&i.set_cwd(r.cwd),r?.env)for(let[e,t]of Object.entries(r.env))i.setenv(e,t,!0);return i.spawnv(t)}function execSync(n,i){let a=i?.encoding,o=i?.input,c=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(o?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),l=_spawnSubprocess([typeof i?.shell==`string`?i.shell:`/bin/sh`,`-c`,n],c,i),u=o?new t.Bytes(typeof o==`string`?new TextEncoder().encode(o):o):null,[,d,f]=l.communicate(u,null),p=l.get_exit_status();if(p!==0){let e=f?new TextDecoder().decode(s(f)):``,t=Error(`Command failed: ${n}\n${e}`);throw t.status=p,t.stderr=e,t.stdout=d?new TextDecoder().decode(s(d)):``,t}if(!d)return a&&a!==`buffer`?``:r.alloc(0);let m=s(d);return a&&a!==`buffer`?new TextDecoder().decode(m):r.from(m)}function _exec(t,n,r){typeof n==`function`&&(r=n,n={});let i=n||{},a=new ChildProcess,s=typeof i.shell==`string`?i.shell:`/bin/sh`,c=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE;try{let e=_spawnSubprocess([s,`-c`,t],c,i);a._setSubprocess(e),o(),e.communicate_utf8_async(null,null,(n,i)=>{try{let[,n,o]=e.communicate_utf8_finish(i),s=e.get_exit_status();if(a.exitCode=s,s!==0){let e=Error(`Command failed: ${t}`);e.code=s,e.killed=a.killed,e.stdout=n||``,e.stderr=o||``,r&&r(e,n||``,o||``)}else r&&r(null,n||``,o||``);a.emit(`close`,s,null),a.emit(`exit`,s,null)}catch(e){let t=e instanceof Error?e:Error(String(e));r&&r(t,``,``),a.emit(`error`,t)}})}catch(e){let t=e instanceof Error?e:Error(String(e));setTimeout(()=>{r&&r(t,``,``),a.emit(`error`,t)},0)}return a}function execFile(t,n,r,i){let a=[],s={},c;typeof n==`function`?c=n:Array.isArray(n)&&(a=n,typeof r==`function`?c=r:(s=r||{},c=i));let l=new ChildProcess,u=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE;try{let e=_spawnSubprocess([t,...a],u,s);l._setSubprocess(e),o(),e.communicate_utf8_async(null,null,(n,r)=>{try{let[,n,i]=e.communicate_utf8_finish(r),a=e.get_exit_status();if(l.exitCode=a,a!==0){let e=Error(`Command failed: ${t}`);e.code=a,e.stdout=n||``,e.stderr=i||``,c&&c(e,n||``,i||``)}else c&&c(null,n||``,i||``);l.emit(`close`,a,null),l.emit(`exit`,a,null)}catch(e){let t=e instanceof Error?e:Error(String(e));c&&c(t,``,``),l.emit(`error`,t)}})}catch(e){let t=e instanceof Error?e:Error(String(e));setTimeout(()=>{c&&c(t,``,``),l.emit(`error`,t)},0)}return l}function execFileSync(n,i,a){let o=i||[],c=a?.encoding,l=a?.input,u=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(l?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),d=_spawnSubprocess([n,...o],u,a),f=l?new t.Bytes(typeof l==`string`?new TextEncoder().encode(l):l):null,[,p,m]=d.communicate(f,null),h=d.get_exit_status();if(h!==0){let e=m?new TextDecoder().decode(s(m)):``,t=Error(`Command failed: ${n} ${o.join(` `)}`);throw t.status=h,t.stderr=e,t}if(!p)return c&&c!==`buffer`?``:r.alloc(0);let g=s(p);return c&&c!==`buffer`?new TextDecoder().decode(g):r.from(g)}function spawn(t,n,r){let i,s;Array.isArray(n)?(i=n,s=r):(i=void 0,s=n);let l=i||[],u=new ChildProcess,d=s?.shell,f;f=d?[typeof d==`string`?d:`/bin/sh`,`-c`,[t,...l].join(` `)]:[t,...l];let p=s?.stdio,m=Array.isArray(p)?[typeof p[0]==`string`?p[0]:`pipe`,typeof p[1]==`string`?p[1]:`pipe`,typeof p[2]==`string`?p[2]:`pipe`]:typeof p==`string`?[p,p,p]:[`pipe`,`pipe`,`pipe`],h=e.SubprocessFlags.NONE;m[0]===`pipe`&&(h|=e.SubprocessFlags.STDIN_PIPE),m[1]===`pipe`&&(h|=e.SubprocessFlags.STDOUT_PIPE),m[1]===`ignore`&&(h|=e.SubprocessFlags.STDOUT_SILENCE),m[2]===`pipe`&&(h|=e.SubprocessFlags.STDERR_PIPE),m[2]===`ignore`&&(h|=e.SubprocessFlags.STDERR_SILENCE);try{let e=_spawnSubprocess(f,h,s);u._setSubprocess(e),c.add(u);let t=e.get_stdout_pipe();t&&(u.stdout=new GioInputStreamReadable(t));let n=e.get_stderr_pipe();n&&(u.stderr=new GioInputStreamReadable(n));let r=s?.signal,i=null,emitAbortError=()=>{let e=s?.killSignal??`SIGTERM`;u.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,u.emit(`error`,t)};r&&(r.aborted?queueMicrotask(emitAbortError):(i=emitAbortError,r.addEventListener(`abort`,i,{once:!0}))),o(),e.wait_async(null,(t,n)=>{try{e.wait_finish(n);let t=e.get_if_exited()?e.get_exit_status():null,a=e.get_if_signaled()?`SIGTERM`:null;u.exitCode=t,u.signalCode=a,r&&i&&r.removeEventListener(`abort`,i),u.emit(`exit`,t,a),u.emit(`close`,t,a)}catch(e){u.emit(`error`,e instanceof Error?e:Error(String(e)))}c.delete(u)}),a(u,`spawn`)}catch(e){a(u,`error`,e instanceof Error?e:Error(String(e)))}return u}function spawnSync(n,i,a){let o,c;Array.isArray(i)?(o=i,c=a):(o=void 0,c=i);let l=o||[],u=c?.shell,d=c?.input,f;f=u?[typeof u==`string`?u:`/bin/sh`,`-c`,[n,...l].join(` `)]:[n,...l];let p=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(d?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE);try{let e=_spawnSubprocess(f,p,c),n=e.get_identifier(),i=d?new t.Bytes(typeof d==`string`?new TextEncoder().encode(d):d):null,[,a,o]=e.communicate(i,null),l=a?r.from(s(a)):r.alloc(0),u=o?r.from(s(o)):r.alloc(0),m=c?.encoding,h=m&&m!==`buffer`?new TextDecoder().decode(l):l,g=m&&m!==`buffer`?new TextDecoder().decode(u):u,_=e.get_if_exited()?e.get_exit_status():null,v=e.get_if_signaled()?`SIGTERM`:null;return{pid:n?parseInt(n,10):0,output:[null,h,g],stdout:h,stderr:g,status:_,signal:v}}catch(e){let t=c?.encoding&&c.encoding!==`buffer`?``:r.alloc(0);return{pid:0,output:[null,t,t],stdout:t,stderr:t,status:null,signal:null,error:e instanceof Error?e:Error(String(e))}}}var l={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,l as default,_exec as exec,execFile,execFileSync,execSync,spawn,spawnSync};
1
+ import"./_virtual/_rolldown/runtime.js";import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{EventEmitter as n}from"node:events";import{Buffer as r}from"node:buffer";import{Readable as i,Writable as a}from"node:stream";import{deferEmit as o,ensureMainLoop as s,gbytesToUint8Array as c}from"@gjsify/utils";var GioInputStreamReadable=class extends i{_stream;_cancellable=new e.Cancellable;constructor(e){super(),this._stream=e}_read(e){this._stream.read_bytes_async(Math.max(e,4096),t.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{let e=this._stream.read_bytes_finish(t).get_data();!e||e.length===0?this.push(null):this.push(r.from(e))}catch(e){this._cancellable.is_cancelled()||this.destroy(e)}})}_destroy(e,t){this._cancellable.cancel(),t(e)}},GioOutputStreamWritable=class extends a{_stream;_cancellable=new e.Cancellable;_closed=!1;constructor(e){super(),this._stream=e}_write(e,n,i){if(this._closed){i(Error(`write after end`));return}let a=typeof e==`string`?r.from(e):e instanceof Uint8Array?e:r.from(e);this._stream.write_bytes_async(new t.Bytes(a),t.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{this._stream.write_bytes_finish(t),i()}catch(e){this._cancellable.is_cancelled()?i():i(e)}})}_final(e){if(this._closed){e();return}this._closed=!0;try{this._stream.close(null)}catch{}e()}_destroy(e,t){if(this._cancellable.cancel(),!this._closed){this._closed=!0;try{this._stream.close(null)}catch{}}t(e)}};const l=new Set;var ChildProcess=class extends n{pid;exitCode=null;signalCode=null;killed=!1;connected=!1;stdin=null;stdout=null;stderr=null;get stdio(){return[this.stdin,this.stdout,this.stderr]}_subprocess=null;_setSubprocess(e){this._subprocess=e;let t=e.get_identifier();t&&(this.pid=parseInt(t,10))}kill(e){if(!this._subprocess)return!1;try{return e===`SIGKILL`||e===9?this._subprocess.force_exit():this._subprocess.send_signal(typeof e==`number`?e:15),this.killed=!0,!0}catch{return!1}}ref(){return this}unref(){return this}};function _spawnSubprocess(t,n,r){let i=new e.SubprocessLauncher({flags:n});if(r?.cwd&&i.set_cwd(r.cwd),r?.env)for(let[e,t]of Object.entries(r.env))i.setenv(e,t,!0);return i.spawnv(t)}function execSync(n,i){let a=i?.encoding,o=i?.input,s=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(o?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),l=_spawnSubprocess([typeof i?.shell==`string`?i.shell:`/bin/sh`,`-c`,n],s,i),u=o?new t.Bytes(typeof o==`string`?new TextEncoder().encode(o):o):null,[,d,f]=l.communicate(u,null),p=l.get_exit_status();if(p!==0){let e=f?new TextDecoder().decode(c(f)):``,t=Error(`Command failed: ${n}\n${e}`);throw t.status=p,t.stderr=e,t.stdout=d?new TextDecoder().decode(c(d)):``,t}if(!d)return a&&a!==`buffer`?``:r.alloc(0);let m=c(d);return a&&a!==`buffer`?new TextDecoder().decode(m):r.from(m)}function _exec(t,n,r){typeof n==`function`&&(r=n,n={});let i=n||{},a=new ChildProcess,o=typeof i.shell==`string`?i.shell:`/bin/sh`,c=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE;try{let e=_spawnSubprocess([o,`-c`,t],c,i);a._setSubprocess(e),s(),e.communicate_utf8_async(null,null,(n,i)=>{try{let[,n,o]=e.communicate_utf8_finish(i),s=e.get_exit_status();if(a.exitCode=s,s!==0){let e=Error(`Command failed: ${t}`);e.code=s,e.killed=a.killed,e.stdout=n||``,e.stderr=o||``,r&&r(e,n||``,o||``)}else r&&r(null,n||``,o||``);a.emit(`close`,s,null),a.emit(`exit`,s,null)}catch(e){let t=e instanceof Error?e:Error(String(e));r&&r(t,``,``),a.emit(`error`,t)}})}catch(e){let t=e instanceof Error?e:Error(String(e));setTimeout(()=>{r&&r(t,``,``),a.emit(`error`,t)},0)}return a}function execFile(t,n,r,i){let a=[],o={},c;typeof n==`function`?c=n:Array.isArray(n)&&(a=n,typeof r==`function`?c=r:(o=r||{},c=i));let l=new ChildProcess,u=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE;try{let e=_spawnSubprocess([t,...a],u,o);l._setSubprocess(e),s(),e.communicate_utf8_async(null,null,(n,r)=>{try{let[,n,i]=e.communicate_utf8_finish(r),a=e.get_exit_status();if(l.exitCode=a,a!==0){let e=Error(`Command failed: ${t}`);e.code=a,e.stdout=n||``,e.stderr=i||``,c&&c(e,n||``,i||``)}else c&&c(null,n||``,i||``);l.emit(`close`,a,null),l.emit(`exit`,a,null)}catch(e){let t=e instanceof Error?e:Error(String(e));c&&c(t,``,``),l.emit(`error`,t)}})}catch(e){let t=e instanceof Error?e:Error(String(e));setTimeout(()=>{c&&c(t,``,``),l.emit(`error`,t)},0)}return l}function execFileSync(n,i,a){let o=i||[],s=a?.encoding,l=a?.input,u=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(l?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),d=_spawnSubprocess([n,...o],u,a),f=l?new t.Bytes(typeof l==`string`?new TextEncoder().encode(l):l):null,[,p,m]=d.communicate(f,null),h=d.get_exit_status();if(h!==0){let e=m?new TextDecoder().decode(c(m)):``,t=Error(`Command failed: ${n} ${o.join(` `)}`);throw t.status=h,t.stderr=e,t}if(!p)return s&&s!==`buffer`?``:r.alloc(0);let g=c(p);return s&&s!==`buffer`?new TextDecoder().decode(g):r.from(g)}function spawn(t,n,r){let i,a;Array.isArray(n)?(i=n,a=r):(i=void 0,a=n);let c=i||[],u=new ChildProcess,d=a?.shell,f;f=d?[typeof d==`string`?d:`/bin/sh`,`-c`,[t,...c].join(` `)]:[t,...c];let p=a?.stdio,m=Array.isArray(p)?[typeof p[0]==`string`?p[0]:`pipe`,typeof p[1]==`string`?p[1]:`pipe`,typeof p[2]==`string`?p[2]:`pipe`]:typeof p==`string`?[p,p,p]:[`pipe`,`pipe`,`pipe`],h=e.SubprocessFlags.NONE;m[0]===`pipe`&&(h|=e.SubprocessFlags.STDIN_PIPE),m[1]===`pipe`&&(h|=e.SubprocessFlags.STDOUT_PIPE),m[1]===`ignore`&&(h|=e.SubprocessFlags.STDOUT_SILENCE),m[2]===`pipe`&&(h|=e.SubprocessFlags.STDERR_PIPE),m[2]===`ignore`&&(h|=e.SubprocessFlags.STDERR_SILENCE);try{let e=_spawnSubprocess(f,h,a);if(u._setSubprocess(e),l.add(u),m[0]===`pipe`){let t=e.get_stdin_pipe();t&&(u.stdin=new GioOutputStreamWritable(t))}let t=e.get_stdout_pipe();t&&(u.stdout=new GioInputStreamReadable(t));let n=e.get_stderr_pipe();n&&(u.stderr=new GioInputStreamReadable(n));let r=a?.signal,i=null,emitAbortError=()=>{let e=a?.killSignal??`SIGTERM`;u.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,u.emit(`error`,t)};r&&(r.aborted?queueMicrotask(emitAbortError):(i=emitAbortError,r.addEventListener(`abort`,i,{once:!0}))),s(),e.wait_async(null,(t,n)=>{try{e.wait_finish(n);let t=e.get_if_exited()?e.get_exit_status():null,a=e.get_if_signaled()?`SIGTERM`:null;u.exitCode=t,u.signalCode=a,r&&i&&r.removeEventListener(`abort`,i),u.stdin&&!u.stdin.destroyed&&u.stdin.destroy(),u.emit(`exit`,t,a),u.emit(`close`,t,a)}catch(e){u.emit(`error`,e instanceof Error?e:Error(String(e)))}l.delete(u)}),o(u,`spawn`)}catch(e){o(u,`error`,e instanceof Error?e:Error(String(e)))}return u}function spawnSync(n,i,a){let o,s;Array.isArray(i)?(o=i,s=a):(o=void 0,s=i);let l=o||[],u=s?.shell,d=s?.input,f;f=u?[typeof u==`string`?u:`/bin/sh`,`-c`,[n,...l].join(` `)]:[n,...l];let p=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(d?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE);try{let e=_spawnSubprocess(f,p,s),n=e.get_identifier(),i=d?new t.Bytes(typeof d==`string`?new TextEncoder().encode(d):d):null,[,a,o]=e.communicate(i,null),l=a?r.from(c(a)):r.alloc(0),u=o?r.from(c(o)):r.alloc(0),m=s?.encoding,h=m&&m!==`buffer`?new TextDecoder().decode(l):l,g=m&&m!==`buffer`?new TextDecoder().decode(u):u,_=e.get_if_exited()?e.get_exit_status():null,v=e.get_if_signaled()?`SIGTERM`:null;return{pid:n?parseInt(n,10):0,output:[null,h,g],stdout:h,stderr:g,status:_,signal:v}}catch(e){let t=s?.encoding&&s.encoding!==`buffer`?``:r.alloc(0);return{pid:0,output:[null,t,t],stdout:t,stderr:t,status:null,signal:null,error:e instanceof Error?e:Error(String(e))}}}var u={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,u as default,_exec as exec,execFile,execFileSync,execSync,spawn,spawnSync};
@@ -1,8 +1,7 @@
1
1
  import Gio from '@girs/gio-2.0';
2
2
  import { EventEmitter } from 'node:events';
3
3
  import { Buffer } from 'node:buffer';
4
- import { Readable } from 'node:stream';
5
- import type { Writable } from 'node:stream';
4
+ import { Readable, Writable } from 'node:stream';
6
5
  export interface ExecOptions {
7
6
  cwd?: string;
8
7
  env?: Record<string, string>;
@@ -62,6 +61,16 @@ export declare class ChildProcess extends EventEmitter {
62
61
  stdin: Writable | null;
63
62
  stdout: Readable | null;
64
63
  stderr: Readable | null;
64
+ /**
65
+ * Node-compatible `stdio` tuple — `[stdin, stdout, stderr, ...extraFds]`.
66
+ * Index 0/1/2 mirror the named streams above. Slots 3+ are reserved for
67
+ * `options.stdio = ['pipe', 'pipe', 'pipe', 'pipe']` extra fds, which we
68
+ * don't surface yet — `null` placeholders match Node's shape when extras
69
+ * weren't requested. Consumers like execa iterate this array (e.g. to
70
+ * dispose streams on subprocess exit), so the property MUST exist even
71
+ * when nothing was piped.
72
+ */
73
+ get stdio(): Array<Writable | Readable | null>;
65
74
  private _subprocess;
66
75
  /** @internal Set the underlying Gio.Subprocess and extract PID. */
67
76
  _setSubprocess(proc: Gio.Subprocess): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/child_process",
3
- "version": "0.4.13",
3
+ "version": "0.4.15",
4
4
  "description": "Node.js child_process module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -33,17 +33,17 @@
33
33
  "child_process"
34
34
  ],
35
35
  "devDependencies": {
36
- "@gjsify/cli": "^0.4.13",
37
- "@gjsify/dom-elements": "^0.4.13",
38
- "@gjsify/unit": "^0.4.13",
36
+ "@gjsify/cli": "^0.4.15",
37
+ "@gjsify/dom-elements": "^0.4.15",
38
+ "@gjsify/unit": "^0.4.15",
39
39
  "@types/node": "^25.6.2",
40
40
  "typescript": "^6.0.3"
41
41
  },
42
42
  "dependencies": {
43
43
  "@girs/gio-2.0": "2.88.0-4.0.0-rc.15",
44
44
  "@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
45
- "@gjsify/buffer": "^0.4.13",
46
- "@gjsify/events": "^0.4.13",
47
- "@gjsify/utils": "^0.4.13"
45
+ "@gjsify/buffer": "^0.4.15",
46
+ "@gjsify/events": "^0.4.15",
47
+ "@gjsify/utils": "^0.4.15"
48
48
  }
49
49
  }