@gjsify/child_process 0.11.0 → 0.13.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/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,Writable as a}from"node:stream";import{fileURLToPath as o}from"node:url";import{deferEmit as s,ensureMainLoop as c,gbytesToUint8Array as l}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)}};function _gioErrorToNodeError(e,n,r){let i=e??Error(`Unknown spawn error`),a=i,o=i.code,s=i.matches,c=!1;if(typeof s==`function`)try{c=s.call(i,t.SpawnError,t.SpawnError.NOENT)===!0}catch{}if(!c&&o===t.SpawnError.NOENT&&(c=!0),!c&&typeof i.message==`string`&&/No such file or directory|ENOENT|Failed to execute|nicht gefunden|nicht ausgeführt/i.test(i.message)&&(c=!0),c){let e=Error(`spawn ${n} ENOENT`);return e.code=`ENOENT`,e.errno=-2,e.syscall=`spawn ${n}`,e.path=n,e.spawnargs=r.slice(),e}if(i instanceof Error)return a;let l=i,u=typeof l.message==`string`?l.message:String(i),d=Error(u);return(typeof o==`number`||typeof o==`string`)&&(d.code=o),d}const u=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{let t=e??`SIGTERM`;return t===`SIGKILL`||t===9?this._subprocess.force_exit():typeof t==`number`?this._subprocess.send_signal(t):this._subprocess.send_signal({SIGHUP:1,SIGINT:2,SIGQUIT:3,SIGABRT:6,SIGKILL:9,SIGUSR1:10,SIGUSR2:12,SIGTERM:15}[t]??15),this.killed=!0,!0}catch{return!1}}ref(){return this}unref(){return this}};function _normalizeCwd(e){if(e!=null){if(typeof e==`string`)return e;if(typeof e==`object`&&e&&typeof e.href==`string`&&typeof e.protocol==`string`){let t=e;if(t.protocol!==`file:`)throw TypeError(`The URL must be of scheme file:; received '${t.protocol}' for 'options.cwd'`);return o(t)}return e instanceof Uint8Array?new TextDecoder().decode(e):String(e)}}function _encodeEnvValue(e){if(e!==void 0)return`${e}`}function _spawnSubprocess(t,n,r){let i=new e.SubprocessLauncher({flags:n}),a=_normalizeCwd(r?.cwd);if(a!==void 0&&i.set_cwd(a),r?.env){let e=r.env;i.set_environ([]);let t=new Set;for(let n in e){if(t.has(n))continue;t.add(n);let r=_encodeEnvValue(e[n]);r!==void 0&&i.setenv(n,r,!0)}}let o=t;if(r?.argv0!==void 0&&r.argv0!==null){if(typeof r.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof r.argv0}`);o=[`/bin/sh`,`-c`,`exec -a "$0" "$@"`,r.argv0,...t]}return r?.detached===!0&&(o=[`/usr/bin/setsid`,...o]),i.spawnv(o)}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),c=typeof i?.shell==`string`?i.shell:`/bin/sh`,u;try{u=_spawnSubprocess([c,`-c`,n],s,i)}catch(e){throw _gioErrorToNodeError(e,c,[`-c`,n])}let d=o?new t.Bytes(typeof o==`string`?new TextEncoder().encode(o):o):null,[,f,p]=u.communicate(d,null),m=u.get_exit_status();if(m!==0){let e=p?new TextDecoder().decode(l(p)):``,t=f?new TextDecoder().decode(l(f)):``,i=Error(`Command failed: ${n}\n${e}`);throw i.status=m,a===`buffer`||a===null?(i.stderr=p?r.from(l(p)):r.alloc(0),i.stdout=f?r.from(l(f)):r.alloc(0)):(i.stderr=e,i.stdout=t),i}if(!f)return a===`buffer`||a==null?r.alloc(0):``;let h=l(f);if(a===`buffer`||a==null)return r.from(h);let g=r.isEncoding(a)?a:`utf8`;return r.from(h).toString(g)}function _decodeExecOutput(e,t){let n=e??new Uint8Array;if(t===`buffer`||t===null)return r.from(n);let i=t&&r.isEncoding(t)?t:`utf8`;return r.from(n).toString(i)}function _normalizeKillSignal(e){return e??`SIGTERM`}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`,s=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE,c=i.maxBuffer??1024*1024,l=i.timeout&&i.timeout>0?i.timeout:0,u=_normalizeKillSignal(i.killSignal),d=i.signal;return _execImpl(a,[o,`-c`,t],s,i,{cmd:t,encoding:i.encoding,maxBuffer:c,timeoutMs:l,killSignal:u,abortSignal:d,callback:r})}function _execImpl(e,t,n,r,i){let a=null,o=!1,d=null,f=null,armTimeout=t=>{i.timeoutMs&&(a=setTimeout(()=>{o=!0;try{t.send_signal(typeof i.killSignal==`number`?i.killSignal:15)}catch{}e.killed=!0},i.timeoutMs))},disarmTimeout=()=>{a!==null&&(clearTimeout(a),a=null)},armAbort=t=>{if(!i.abortSignal)return;let fire=()=>{d=Error(`The operation was aborted`),d.name=`AbortError`;try{t.send_signal(typeof i.killSignal==`number`?i.killSignal:15)}catch{}e.killed=!0};if(i.abortSignal.aborted){queueMicrotask(fire);return}f=fire,i.abortSignal.addEventListener(`abort`,f,{once:!0})},disarmAbort=()=>{i.abortSignal&&f&&(i.abortSignal.removeEventListener(`abort`,f),f=null)};try{let a=_spawnSubprocess(t,n,r);e._setSubprocess(a),u.add(e),c(),armTimeout(a),armAbort(a),a.communicate_async(null,null,(t,n)=>{disarmTimeout(),disarmAbort(),u.delete(e);try{let[,t,r]=a.communicate_finish(n),s=t?l(t):null,c=r?l(r):null,u=null;i.maxBuffer!==1/0&&s&&s.length>i.maxBuffer&&(s=s.subarray(0,i.maxBuffer),u=RangeError(`stdout maxBuffer length exceeded`),u.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`),!u&&i.maxBuffer!==1/0&&c&&c.length>i.maxBuffer&&(c=c.subarray(0,i.maxBuffer),u=RangeError(`stderr maxBuffer length exceeded`),u.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`);let f=_decodeExecOutput(s,i.encoding),p=_decodeExecOutput(c,i.encoding),m=a.get_if_exited()?a.get_exit_status():null,h=a.get_if_signaled()?typeof i.killSignal==`string`?i.killSignal:`SIGTERM`:null;e.exitCode=m,e.signalCode=h;let g=null;if(d)g=d,g.killed=!0,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(u)g=u,g.killed=e.killed,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(o)g=Error(`Command failed: ${i.cmd}`),g.killed=!0,g.code=null,g.signal=typeof i.killSignal==`string`?i.killSignal:`SIGTERM`,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(m!==0&&m!==null){let t=typeof p==`string`?p:p.toString();g=Error(`Command failed: ${i.cmd}\n${t}`),g.code=m,g.killed=e.killed,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd}else h&&(g=Error(`Command failed: ${i.cmd}`),g.killed=!0,g.signal=h,g.code=null,g.stdout=f,g.stderr=p,g.cmd=i.cmd);i.callback&&i.callback(g,f,p),e.emit(`exit`,m,h),e.emit(`close`,m,h)}catch(t){let n=t instanceof Error?t:Error(String(t));i.callback&&i.callback(n,``,``),e.emit(`error`,n)}}),s(e,`spawn`)}catch(n){disarmTimeout(),disarmAbort();let r=_gioErrorToNodeError(n,t[0]??``,t.slice(1));setTimeout(()=>{i.callback&&i.callback(r,``,``),e.emit(`error`,r)},0)}return e}function execFile(t,n,r,i){let a=[],o={},s;typeof n==`function`?s=n:Array.isArray(n)?(a=n,typeof r==`function`?s=r:(o=r||{},s=i)):n&&typeof n==`object`&&(o=n,s=typeof r==`function`?r:i);let c=new ChildProcess,l=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE,u=o.maxBuffer??1024*1024,d=o.timeout&&o.timeout>0?o.timeout:0,f=_normalizeKillSignal(o.killSignal);return _execImpl(c,[t,...a],l,o,{cmd:t,encoding:o.encoding,maxBuffer:u,timeoutMs:d,killSignal:f,abortSignal:o.signal,callback:s})}function execFileSync(n,i,a){let o=[],s;Array.isArray(i)?(o=i,s=a):i&&typeof i==`object`&&(s=i);let c=s?.encoding,u=s?.input,d=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(u?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),f;try{f=_spawnSubprocess([n,...o],d,s)}catch(e){throw _gioErrorToNodeError(e,n,o)}let p=u?new t.Bytes(typeof u==`string`?new TextEncoder().encode(u):u):null,[,m,h]=f.communicate(p,null),g=f.get_exit_status();if(g!==0){let e=h?new TextDecoder().decode(l(h)):``,t=m?new TextDecoder().decode(l(m)):``,i=Error(`Command failed: ${n} ${o.join(` `)}`);throw i.status=g,c===`buffer`||c===null?(i.stderr=h?r.from(l(h)):r.alloc(0),i.stdout=m?r.from(l(m)):r.alloc(0)):(i.stderr=e,i.stdout=t),i}if(!m)return c===`buffer`||c==null?r.alloc(0):``;let _=l(m);if(c===`buffer`||c==null)return r.from(_);let v=r.isEncoding(c)?c:`utf8`;return r.from(_).toString(v)}function spawn(t,n,r){let i,a;if(Array.isArray(n))i=n,a=r;else if(n!=null){if(typeof n!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof n}`);i=void 0,a=n}else i=void 0,a=n;if(a!=null&&typeof a!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof a}`);if(a?.argv0!==void 0&&a.argv0!==null&&typeof a.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof a.argv0}`);a?.cwd!==void 0&&_normalizeCwd(a.cwd);let o=i||[],l=new ChildProcess,d=a?.shell,f;f=d?[typeof d==`string`?d:`/bin/sh`,`-c`,[t,...o].join(` `)]:[t,...o];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[0]===`inherit`&&(h|=e.SubprocessFlags.STDIN_INHERIT),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(l._setSubprocess(e),u.add(l),m[0]===`pipe`){let t=e.get_stdin_pipe();t&&(l.stdin=new GioOutputStreamWritable(t))}let t=e.get_stdout_pipe();t&&(l.stdout=new GioInputStreamReadable(t));let n=e.get_stderr_pipe();n&&(l.stderr=new GioInputStreamReadable(n));let r=a?.signal,i=null,emitAbortError=()=>{let e=a?.killSignal??`SIGTERM`;l.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,l.emit(`error`,t)};r&&(r.aborted?queueMicrotask(emitAbortError):(i=emitAbortError,r.addEventListener(`abort`,i,{once:!0})));let o=null;if(a?.timeout&&a.timeout>0){let e=a.killSignal??`SIGTERM`;o=setTimeout(()=>{l.kill(e),o=null},a.timeout)}c(),e.wait_async(null,(t,n)=>{try{e.wait_finish(n);let t=e.get_if_exited()?e.get_exit_status():null,s=null;if(e.get_if_signaled()){let e=a?.killSignal;s=typeof e==`string`?e:`SIGTERM`}l.exitCode=t,l.signalCode=s,o!==null&&(clearTimeout(o),o=null),r&&i&&r.removeEventListener(`abort`,i),l.stdin&&!l.stdin.destroyed&&l.stdin.destroy(),l.emit(`exit`,t,s),l.emit(`close`,t,s)}catch(e){l.emit(`error`,e instanceof Error?e:Error(String(e)))}u.delete(l)}),s(l,`spawn`)}catch(e){s(l,`error`,_gioErrorToNodeError(e,d?t:f[0]??``,d?o:f.slice(1)))}return l}function spawnSync(n,i,a){let o,s;if(Array.isArray(i))o=i,s=a;else if(i!=null){if(typeof i!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof i}`);o=void 0,s=i}else o=void 0,s=i;if(s!=null&&typeof s!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof s}`);if(s?.argv0!==void 0&&s.argv0!==null&&typeof s.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof s.argv0}`);s?.cwd!==void 0&&_normalizeCwd(s.cwd);let c=o||[],u=s?.shell,d=s?.input,f;f=u?[typeof u==`string`?u:`/bin/sh`,`-c`,[n,...c].join(` `)]:[n,...c];let p=!1;if(s?.timeout&&s.timeout>0){let e=s.timeout/1e3,t=s.killSignal;f=[`/usr/bin/timeout`,`-s`,typeof t==`string`?t.replace(/^SIG/,``):`TERM`,String(e),...f],p=!0}let m=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(d?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),h=d;if(h!=null&&typeof h!=`string`&&!(h instanceof Uint8Array)&&!(h instanceof ArrayBuffer)&&!ArrayBuffer.isView(h))throw TypeError(`The "input" property must be of type string, Buffer, TypedArray, or DataView. Received type ${typeof h}`);let g=s?.encoding,_=g===`buffer`||g==null?r.alloc(0):``,v;try{v=_spawnSubprocess(f,m,s)}catch(e){let t=u?n:f[0]??``,r=u?c:f.slice(1);return{pid:0,output:[null,_,_],stdout:_,stderr:_,status:null,signal:null,error:_gioErrorToNodeError(e,t,r)}}let y=v.get_identifier(),b=d?new t.Bytes(typeof d==`string`?new TextEncoder().encode(d):d):null,x,S,C=null;try{let e=v.communicate(b,null);x=e[1]??null,S=e[2]??null}catch(e){C=e instanceof Error?e:Error(String(e)),x=null,S=null}let w=x?r.from(l(x)):r.alloc(0),T=S?r.from(l(S)):r.alloc(0),decode=e=>{if(g===`buffer`||g==null)return e;let t=r.isEncoding(g)?g:`utf8`;return e.toString(t)},E=decode(w),D=decode(T),O=v.get_if_exited()?v.get_exit_status():null,k=null;if(v.get_if_signaled()){let e=s?.killSignal;k=typeof e==`string`?e:`SIGTERM`}let A=!1;p&&(O===124||O===137)&&(A=!0,k=typeof s?.killSignal==`string`?s.killSignal:`SIGTERM`);let j={pid:y?parseInt(y,10):0,output:[null,E,D],stdout:E,stderr:D,status:A?null:O,signal:k};if(A){let e=Error(`Command failed: ${f.join(` `)}\n${typeof D==`string`?D:D.toString()}`);e.code=`ETIMEDOUT`,e.killed=!0,e.signal=k,j.error=e}else C&&(j.error=C);return j}var d={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,d 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{fileURLToPath as o}from"node:url";import{deferEmit as s,ensureMainLoop as c,gbytesToUint8Array as l}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)}};function _gioErrorToNodeError(e,n,r){let i=e??Error(`Unknown spawn error`),a=i,o=i.code,s=i.matches,c=!1;if(typeof s==`function`)try{c=s.call(i,t.SpawnError,t.SpawnError.NOENT)===!0}catch{}if(!c&&o===t.SpawnError.NOENT&&(c=!0),!c&&typeof i.message==`string`&&/No such file or directory|ENOENT|Failed to execute|nicht gefunden|nicht ausgeführt/i.test(i.message)&&(c=!0),c){let e=Error(`spawn ${n} ENOENT`);return e.code=`ENOENT`,e.errno=-2,e.syscall=`spawn ${n}`,e.path=n,e.spawnargs=r.slice(),e}if(i instanceof Error)return a;let l=i,u=typeof l.message==`string`?l.message:String(i),d=Error(u);return(typeof o==`number`||typeof o==`string`)&&(d.code=o),d}const u=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,t){this._subprocess=e,t>0&&(this.pid=t)}kill(e){if(!this._subprocess)return!1;try{let t=e??`SIGTERM`;return t===`SIGKILL`||t===9?this._subprocess.force_exit():typeof t==`number`?this._subprocess.send_signal(t):this._subprocess.send_signal({SIGHUP:1,SIGINT:2,SIGQUIT:3,SIGABRT:6,SIGKILL:9,SIGUSR1:10,SIGUSR2:12,SIGTERM:15}[t]??15),this.killed=!0,!0}catch{return!1}}ref(){return this}unref(){return this}};function _normalizeCwd(e){if(e!=null){if(typeof e==`string`)return e;if(typeof e==`object`&&e&&typeof e.href==`string`&&typeof e.protocol==`string`){let t=e;if(t.protocol!==`file:`)throw TypeError(`The URL must be of scheme file:; received '${t.protocol}' for 'options.cwd'`);return o(t)}return e instanceof Uint8Array?new TextDecoder().decode(e):String(e)}}function _encodeEnvValue(e){if(e!==void 0)return`${e}`}function _spawnSubprocess(t,n,r){let i=new e.SubprocessLauncher({flags:n}),a=_normalizeCwd(r?.cwd);if(a!==void 0&&i.set_cwd(a),r?.env){let e=r.env;i.set_environ([]);let t=new Set;for(let n in e){if(t.has(n))continue;t.add(n);let r=_encodeEnvValue(e[n]);r!==void 0&&i.setenv(n,r,!0)}}let o=t;if(r?.argv0!==void 0&&r.argv0!==null){if(typeof r.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof r.argv0}`);o=[`/bin/sh`,`-c`,`exec -a "$0" "$@"`,r.argv0,...t]}r?.detached===!0&&(o=[`/usr/bin/setsid`,...o]);let s=i.spawnv(o);return{proc:s,pid:_capturePidAtSpawn(s)}}function _capturePidAtSpawn(e){let t=e.get_identifier();if(t){let e=parseInt(t,10);if(e>0)return e}return 0}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),c=typeof i?.shell==`string`?i.shell:`/bin/sh`,u;try{({proc:u}=_spawnSubprocess([c,`-c`,n],s,i))}catch(e){throw _gioErrorToNodeError(e,c,[`-c`,n])}let d=o?new t.Bytes(typeof o==`string`?new TextEncoder().encode(o):o):null,[,f,p]=u.communicate(d,null),m=u.get_exit_status();if(m!==0){let e=p?new TextDecoder().decode(l(p)):``,t=f?new TextDecoder().decode(l(f)):``,i=Error(`Command failed: ${n}\n${e}`);throw i.status=m,a===`buffer`||a===null?(i.stderr=p?r.from(l(p)):r.alloc(0),i.stdout=f?r.from(l(f)):r.alloc(0)):(i.stderr=e,i.stdout=t),i}if(!f)return a===`buffer`||a==null?r.alloc(0):``;let h=l(f);if(a===`buffer`||a==null)return r.from(h);let g=r.isEncoding(a)?a:`utf8`;return r.from(h).toString(g)}function _decodeExecOutput(e,t){let n=e??new Uint8Array;if(t===`buffer`||t===null)return r.from(n);let i=t&&r.isEncoding(t)?t:`utf8`;return r.from(n).toString(i)}function _normalizeKillSignal(e){return e??`SIGTERM`}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`,s=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE,c=i.maxBuffer??1024*1024,l=i.timeout&&i.timeout>0?i.timeout:0,u=_normalizeKillSignal(i.killSignal),d=i.signal;return _execImpl(a,[o,`-c`,t],s,i,{cmd:t,encoding:i.encoding,maxBuffer:c,timeoutMs:l,killSignal:u,abortSignal:d,callback:r})}function _execImpl(e,t,n,r,i){let a=null,o=!1,s=null,d=null,armTimeout=t=>{i.timeoutMs&&(a=setTimeout(()=>{o=!0;try{t.send_signal(typeof i.killSignal==`number`?i.killSignal:15)}catch{}e.killed=!0},i.timeoutMs))},disarmTimeout=()=>{a!==null&&(clearTimeout(a),a=null)},armAbort=t=>{if(!i.abortSignal)return;let fire=()=>{s=Error(`The operation was aborted`),s.name=`AbortError`;try{t.send_signal(typeof i.killSignal==`number`?i.killSignal:15)}catch{}e.killed=!0};if(i.abortSignal.aborted){queueMicrotask(fire);return}d=fire,i.abortSignal.addEventListener(`abort`,d,{once:!0})},disarmAbort=()=>{i.abortSignal&&d&&(i.abortSignal.removeEventListener(`abort`,d),d=null)};try{let{proc:a,pid:d}=_spawnSubprocess(t,n,r);e._setSubprocess(a,d),u.add(e),c(),armTimeout(a),armAbort(a),a.communicate_async(null,null,(t,n)=>{disarmTimeout(),disarmAbort(),u.delete(e);try{let[,t,r]=a.communicate_finish(n),c=t?l(t):null,u=r?l(r):null,d=null;i.maxBuffer!==1/0&&c&&c.length>i.maxBuffer&&(c=c.subarray(0,i.maxBuffer),d=RangeError(`stdout maxBuffer length exceeded`),d.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`),!d&&i.maxBuffer!==1/0&&u&&u.length>i.maxBuffer&&(u=u.subarray(0,i.maxBuffer),d=RangeError(`stderr maxBuffer length exceeded`),d.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`);let f=_decodeExecOutput(c,i.encoding),p=_decodeExecOutput(u,i.encoding),m=a.get_if_exited()?a.get_exit_status():null,h=a.get_if_signaled()?typeof i.killSignal==`string`?i.killSignal:`SIGTERM`:null;e.exitCode=m,e.signalCode=h;let g=null;if(s)g=s,g.killed=!0,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(d)g=d,g.killed=e.killed,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(o)g=Error(`Command failed: ${i.cmd}`),g.killed=!0,g.code=null,g.signal=typeof i.killSignal==`string`?i.killSignal:`SIGTERM`,g.stdout=f,g.stderr=p,g.cmd=i.cmd;else if(m!==0&&m!==null){let t=typeof p==`string`?p:p.toString();g=Error(`Command failed: ${i.cmd}\n${t}`),g.code=m,g.killed=e.killed,g.signal=h,g.stdout=f,g.stderr=p,g.cmd=i.cmd}else h&&(g=Error(`Command failed: ${i.cmd}`),g.killed=!0,g.signal=h,g.code=null,g.stdout=f,g.stderr=p,g.cmd=i.cmd);i.callback&&i.callback(g,f,p),e.emit(`exit`,m,h),e.emit(`close`,m,h)}catch(t){let n=t instanceof Error?t:Error(String(t));i.callback&&i.callback(n,``,``),e.emit(`error`,n)}}),queueMicrotask(()=>e.emit(`spawn`))}catch(n){disarmTimeout(),disarmAbort();let r=_gioErrorToNodeError(n,t[0]??``,t.slice(1));setTimeout(()=>{i.callback&&i.callback(r,``,``),e.emit(`error`,r)},0)}return e}function execFile(t,n,r,i){let a=[],o={},s;typeof n==`function`?s=n:Array.isArray(n)?(a=n,typeof r==`function`?s=r:(o=r||{},s=i)):n&&typeof n==`object`&&(o=n,s=typeof r==`function`?r:i);let c=new ChildProcess,l=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE,u=o.maxBuffer??1024*1024,d=o.timeout&&o.timeout>0?o.timeout:0,f=_normalizeKillSignal(o.killSignal);return _execImpl(c,[t,...a],l,o,{cmd:t,encoding:o.encoding,maxBuffer:u,timeoutMs:d,killSignal:f,abortSignal:o.signal,callback:s})}function execFileSync(n,i,a){let o=[],s;Array.isArray(i)?(o=i,s=a):i&&typeof i==`object`&&(s=i);let c=s?.encoding,u=s?.input,d=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(u?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),f;try{({proc:f}=_spawnSubprocess([n,...o],d,s))}catch(e){throw _gioErrorToNodeError(e,n,o)}let p=u?new t.Bytes(typeof u==`string`?new TextEncoder().encode(u):u):null,[,m,h]=f.communicate(p,null),g=f.get_exit_status();if(g!==0){let e=h?new TextDecoder().decode(l(h)):``,t=m?new TextDecoder().decode(l(m)):``,i=Error(`Command failed: ${n} ${o.join(` `)}`);throw i.status=g,c===`buffer`||c===null?(i.stderr=h?r.from(l(h)):r.alloc(0),i.stdout=m?r.from(l(m)):r.alloc(0)):(i.stderr=e,i.stdout=t),i}if(!m)return c===`buffer`||c==null?r.alloc(0):``;let _=l(m);if(c===`buffer`||c==null)return r.from(_);let v=r.isEncoding(c)?c:`utf8`;return r.from(_).toString(v)}function spawn(t,n,r){let i,a;if(Array.isArray(n))i=n,a=r;else if(n!=null){if(typeof n!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof n}`);i=void 0,a=n}else i=void 0,a=n;if(a!=null&&typeof a!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof a}`);if(a?.argv0!==void 0&&a.argv0!==null&&typeof a.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof a.argv0}`);a?.cwd!==void 0&&_normalizeCwd(a.cwd);let o=i||[],l=new ChildProcess,d=a?.shell,f;f=d?[typeof d==`string`?d:`/bin/sh`,`-c`,[t,...o].join(` `)]:[t,...o];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[0]===`inherit`&&(h|=e.SubprocessFlags.STDIN_INHERIT),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{proc:e,pid:t}=_spawnSubprocess(f,h,a);if(l._setSubprocess(e,t),u.add(l),m[0]===`pipe`){let t=e.get_stdin_pipe();t&&(l.stdin=new GioOutputStreamWritable(t))}let n=e.get_stdout_pipe();n&&(l.stdout=new GioInputStreamReadable(n));let r=e.get_stderr_pipe();r&&(l.stderr=new GioInputStreamReadable(r));let i=a?.signal,o=null,emitAbortError=()=>{let e=a?.killSignal??`SIGTERM`;l.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,l.emit(`error`,t)};i&&(i.aborted?queueMicrotask(emitAbortError):(o=emitAbortError,i.addEventListener(`abort`,o,{once:!0})));let s=null;if(a?.timeout&&a.timeout>0){let e=a.killSignal??`SIGTERM`;s=setTimeout(()=>{l.kill(e),s=null},a.timeout)}c(),e.wait_async(null,(t,n)=>{try{e.wait_finish(n);let t=e.get_if_exited()?e.get_exit_status():null,r=null;if(e.get_if_signaled()){let e=a?.killSignal;r=typeof e==`string`?e:`SIGTERM`}l.exitCode=t,l.signalCode=r,s!==null&&(clearTimeout(s),s=null),i&&o&&i.removeEventListener(`abort`,o),l.stdin&&!l.stdin.destroyed&&l.stdin.destroy(),l.emit(`exit`,t,r),l.emit(`close`,t,r)}catch(e){l.emit(`error`,e instanceof Error?e:Error(String(e)))}u.delete(l)}),queueMicrotask(()=>l.emit(`spawn`))}catch(e){s(l,`error`,_gioErrorToNodeError(e,d?t:f[0]??``,d?o:f.slice(1)))}return l}function spawnSync(n,i,a){let o,s;if(Array.isArray(i))o=i,s=a;else if(i!=null){if(typeof i!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof i}`);o=void 0,s=i}else o=void 0,s=i;if(s!=null&&typeof s!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof s}`);if(s?.argv0!==void 0&&s.argv0!==null&&typeof s.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof s.argv0}`);s?.cwd!==void 0&&_normalizeCwd(s.cwd);let c=o||[],u=s?.shell,d=s?.input,f;f=u?[typeof u==`string`?u:`/bin/sh`,`-c`,[n,...c].join(` `)]:[n,...c];let p=!1;if(s?.timeout&&s.timeout>0){let e=s.timeout/1e3,t=s.killSignal;f=[`/usr/bin/timeout`,`-s`,typeof t==`string`?t.replace(/^SIG/,``):`TERM`,String(e),...f],p=!0}let m=e.SubprocessFlags.STDOUT_PIPE|e.SubprocessFlags.STDERR_PIPE|(d?e.SubprocessFlags.STDIN_PIPE:e.SubprocessFlags.NONE),h=d;if(h!=null&&typeof h!=`string`&&!(h instanceof Uint8Array)&&!(h instanceof ArrayBuffer)&&!ArrayBuffer.isView(h))throw TypeError(`The "input" property must be of type string, Buffer, TypedArray, or DataView. Received type ${typeof h}`);let g=s?.encoding,_=g===`buffer`||g==null?r.alloc(0):``,v,y=0;try{({proc:v,pid:y}=_spawnSubprocess(f,m,s))}catch(e){let t=u?n:f[0]??``,r=u?c:f.slice(1);return{pid:0,output:[null,_,_],stdout:_,stderr:_,status:null,signal:null,error:_gioErrorToNodeError(e,t,r)}}let b=d?new t.Bytes(typeof d==`string`?new TextEncoder().encode(d):d):null,x,S,C=null;try{let e=v.communicate(b,null);x=e[1]??null,S=e[2]??null}catch(e){C=e instanceof Error?e:Error(String(e)),x=null,S=null}let w=x?r.from(l(x)):r.alloc(0),T=S?r.from(l(S)):r.alloc(0),decode=e=>{if(g===`buffer`||g==null)return e;let t=r.isEncoding(g)?g:`utf8`;return e.toString(t)},E=decode(w),D=decode(T),O=v.get_if_exited()?v.get_exit_status():null,k=null;if(v.get_if_signaled()){let e=s?.killSignal;k=typeof e==`string`?e:`SIGTERM`}let A=!1;p&&(O===124||O===137)&&(A=!0,k=typeof s?.killSignal==`string`?s.killSignal:`SIGTERM`);let j={pid:y,output:[null,E,D],stdout:E,stderr:D,status:A?null:O,signal:k};if(A){let e=Error(`Command failed: ${f.join(` `)}\n${typeof D==`string`?D:D.toString()}`);e.code=`ETIMEDOUT`,e.killed=!0,e.signal=k,j.error=e}else C&&(j.error=C);return j}var d={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,d as default,_exec as exec,execFile,execFileSync,execSync,spawn,spawnSync};
@@ -114,8 +114,23 @@ export declare class ChildProcess extends EventEmitter {
114
114
  */
115
115
  get stdio(): Array<Writable | Readable | null>;
116
116
  private _subprocess;
117
- /** @internal Set the underlying Gio.Subprocess and extract PID. */
118
- _setSubprocess(proc: Gio.Subprocess): void;
117
+ /**
118
+ * @internal Adopt the underlying Gio.Subprocess.
119
+ *
120
+ * `pid` is supplied by the caller (`_spawnSubprocess`) rather than read
121
+ * here via `proc.get_identifier()`, because that accessor is RACY for an
122
+ * instant-exit child: `g_subprocess_get_identifier()` returns the pid only
123
+ * while `subprocess->pid != 0`, and GSubprocess's child-watch (running on
124
+ * the GLib *worker thread*, not the main context — see
125
+ * `g_subprocess_exited` in glib's `gio/gsubprocess.c`) sets `pid = 0` the
126
+ * instant it reaps the child. For a fast child like `echo`, that reap can
127
+ * win the race against any synchronous JS read, leaving `get_identifier()`
128
+ * permanently null. Node guarantees `cp.pid` is a positive integer
129
+ * synchronously, so we capture the pid at spawn time — the one moment it
130
+ * is provably still alive — and hand it in here. See
131
+ * `_capturePidAtSpawn` / `_spawnSubprocess`.
132
+ */
133
+ _setSubprocess(proc: Gio.Subprocess, pid: number): void;
119
134
  /**
120
135
  * Send a signal to the child process.
121
136
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/child_process",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Node.js child_process module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -33,18 +33,18 @@
33
33
  "child_process"
34
34
  ],
35
35
  "devDependencies": {
36
- "@gjsify/cli": "^0.11.0",
37
- "@gjsify/dom-elements": "^0.11.0",
38
- "@gjsify/unit": "^0.11.0",
36
+ "@gjsify/cli": "^0.13.0",
37
+ "@gjsify/dom-elements": "^0.13.0",
38
+ "@gjsify/unit": "^0.13.0",
39
39
  "@types/node": "^25.9.2",
40
40
  "typescript": "^6.0.3"
41
41
  },
42
42
  "dependencies": {
43
43
  "@girs/gio-2.0": "2.88.0-4.0.4",
44
44
  "@girs/glib-2.0": "2.88.0-4.0.4",
45
- "@gjsify/buffer": "^0.11.0",
46
- "@gjsify/events": "^0.11.0",
47
- "@gjsify/utils": "^0.11.0"
45
+ "@gjsify/buffer": "^0.13.0",
46
+ "@gjsify/events": "^0.13.0",
47
+ "@gjsify/utils": "^0.13.0"
48
48
  },
49
49
  "gjsify": {
50
50
  "runtimes": {