@gjsify/child_process 0.53.0 → 0.55.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{communicateWithTimeout as e}from"./communicate.js";import{applyArgv0 as t,detachedPrefix as n,killProcess as r,shellArgv as i}from"./platform/index.js";import a from"@girs/glib-2.0";import o from"@girs/gio-2.0";import{EventEmitter as s}from"node:events";import{Buffer as c}from"node:buffer";import{Readable as l,Writable as u}from"node:stream";import{fileURLToPath as d}from"node:url";import{deferEmit as f,ensureMainLoop as p,gbytesToUint8Array as m}from"@gjsify/utils";var GioInputStreamReadable=class extends l{_stream;_cancellable=new o.Cancellable;constructor(e){super(),this._stream=e}_read(e){this._stream.read_bytes_async(Math.max(e,4096),a.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(c.from(e))}catch(e){this._cancellable.is_cancelled()||this.destroy(e)}})}_destroy(e,t){this._cancellable.cancel(),t(e)}},GioOutputStreamWritable=class extends u{_stream;_cancellable=new o.Cancellable;_closed=!1;constructor(e){super(),this._stream=e}_write(e,t,n){if(this._closed){n(Error(`write after end`));return}let r=typeof e==`string`?c.from(e):e instanceof Uint8Array?e:c.from(e);this._stream.write_bytes_async(new a.Bytes(r),a.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{this._stream.write_bytes_finish(t),n()}catch(e){this._cancellable.is_cancelled()?n():n(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,t,n){let r=e??Error(`Unknown spawn error`),i=r,o=r.code,s=r.matches,c=!1;if(typeof s==`function`)try{c=s.call(r,a.SpawnError,a.SpawnError.NOENT)===!0}catch{}if(!c&&o===a.SpawnError.NOENT&&(c=!0),!c&&typeof r.message==`string`&&/No such file or directory|ENOENT|Failed to execute|nicht gefunden|nicht ausgeführt/i.test(r.message)&&(c=!0),c){let e=Error(`spawn ${t} ENOENT`);return e.code=`ENOENT`,e.errno=-2,e.syscall=`spawn ${t}`,e.path=t,e.spawnargs=n.slice(),e}if(r instanceof Error)return i;let l=r,u=typeof l.message==`string`?l.message:String(r),d=Error(u);return(typeof o==`number`||typeof o==`string`)&&(d.code=o),d}const h=new Set;var ChildProcess=class extends s{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{return r(this._subprocess,e??`SIGTERM`),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 d(t)}return e instanceof Uint8Array?new TextDecoder().decode(e):String(e)}}function _encodeEnvValue(e){if(e!==void 0)return`${e}`}function _spawnSubprocess(e,r,i){let a=new o.SubprocessLauncher({flags:r}),s=_normalizeCwd(i?.cwd);if(s!==void 0&&a.set_cwd(s),i?.env){let e=i.env;a.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&&a.setenv(n,r,!0)}}let c=e;if(i?.argv0!==void 0&&i.argv0!==null){if(typeof i.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof i.argv0}`);c=t(i.argv0,e)}if(i?.detached===!0){let e=n();e!==null&&(c=[...e,...c])}let l=a.spawnv(c);return{proc:l,pid:_capturePidAtSpawn(l)}}function _capturePidAtSpawn(e){let t=e.get_identifier();if(t){let e=parseInt(t,10);if(e>0)return e}return 0}function _communicateSync(t,n,i,a){if(i<=0){let e=t.communicate(n,null);return{stdout:e[1]??null,stderr:e[2]??null,timedOut:!1}}let o=e(t,n,i,()=>{r(t,a??`SIGTERM`)});if(o.error)throw o.error;return{stdout:o.stdout,stderr:o.stderr,timedOut:o.timedOut}}function _syncTimeoutError(e,t,n,r,i){let a=t?m(t):new Uint8Array,o=n?m(n):new Uint8Array,s=new TextDecoder().decode(o),l=Error(`Command failed: ${e}\n${s}`);return l.code=`ETIMEDOUT`,l.killed=!0,l.signal=typeof i==`string`?i:`SIGTERM`,l.status=null,r===`buffer`||r===null?(l.stdout=c.from(a),l.stderr=c.from(o)):(l.stdout=new TextDecoder().decode(a),l.stderr=s),l}function execSync(e,t){let n=t?.encoding,r=t?.input,s=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(r?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),l=i(e,t?.shell),u;try{({proc:u}=_spawnSubprocess(l,s,t))}catch(e){throw _gioErrorToNodeError(e,l[0],l.slice(1))}let d=r?new a.Bytes(typeof r==`string`?new TextEncoder().encode(r):r):null,f=t?.timeout&&t.timeout>0?t.timeout:0,{stdout:p,stderr:h,timedOut:g}=_communicateSync(u,d,f,t?.killSignal);if(g)throw _syncTimeoutError(e,p,h,n,t?.killSignal);let _=u.get_exit_status();if(_!==0){let t=h?new TextDecoder().decode(m(h)):``,r=p?new TextDecoder().decode(m(p)):``,i=Error(`Command failed: ${e}\n${t}`);throw i.status=_,n===`buffer`||n===null?(i.stderr=h?c.from(m(h)):c.alloc(0),i.stdout=p?c.from(m(p)):c.alloc(0)):(i.stderr=t,i.stdout=r),i}if(!p)return n===`buffer`||n==null?c.alloc(0):``;let v=m(p);if(n===`buffer`||n==null)return c.from(v);let y=c.isEncoding(n)?n:`utf8`;return c.from(v).toString(y)}function _decodeExecOutput(e,t){let n=e??new Uint8Array;if(t===`buffer`||t===null)return c.from(n);let r=t&&c.isEncoding(t)?t:`utf8`;return c.from(n).toString(r)}function _normalizeKillSignal(e){return e??`SIGTERM`}function _exec(e,t,n){typeof t==`function`&&(n=t,t={});let r=t||{},a=new ChildProcess,s=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE,c=r.maxBuffer??1024*1024,l=r.timeout&&r.timeout>0?r.timeout:0,u=_normalizeKillSignal(r.killSignal),d=r.signal;return _execImpl(a,i(e,r.shell),s,r,{cmd:e,encoding:r.encoding,maxBuffer:c,timeoutMs:l,killSignal:u,abortSignal:d,callback:n})}function _absorbExecError(){}function _execImpl(e,t,n,i,a){e.on(`error`,_absorbExecError);let o=null,s=!1,c=null,l=null,armTimeout=t=>{a.timeoutMs&&(o=setTimeout(()=>{s=!0;try{r(t,a.killSignal)}catch{}e.killed=!0},a.timeoutMs))},disarmTimeout=()=>{o!==null&&(clearTimeout(o),o=null)},armAbort=t=>{if(!a.abortSignal)return;let fire=()=>{c=Error(`The operation was aborted`),c.name=`AbortError`;try{r(t,a.killSignal)}catch{}e.killed=!0};if(a.abortSignal.aborted){queueMicrotask(fire);return}l=fire,a.abortSignal.addEventListener(`abort`,l,{once:!0})},disarmAbort=()=>{a.abortSignal&&l&&(a.abortSignal.removeEventListener(`abort`,l),l=null)};try{let{proc:r,pid:o}=_spawnSubprocess(t,n,i);e._setSubprocess(r,o),h.add(e),p(),armTimeout(r),armAbort(r),r.communicate_async(null,null,(t,n)=>{disarmTimeout(),disarmAbort(),h.delete(e);try{let[,t,i]=r.communicate_finish(n),o=t?m(t):null,l=i?m(i):null,u=null;a.maxBuffer!==1/0&&o&&o.length>a.maxBuffer&&(o=o.subarray(0,a.maxBuffer),u=RangeError(`stdout maxBuffer length exceeded`),u.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`),!u&&a.maxBuffer!==1/0&&l&&l.length>a.maxBuffer&&(l=l.subarray(0,a.maxBuffer),u=RangeError(`stderr maxBuffer length exceeded`),u.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`);let d=_decodeExecOutput(o,a.encoding),f=_decodeExecOutput(l,a.encoding),p=r.get_if_exited()?r.get_exit_status():null,h=r.get_if_signaled()?typeof a.killSignal==`string`?a.killSignal:`SIGTERM`:null;e.exitCode=p,e.signalCode=h;let g=null;if(c)g=c,g.killed=!0,g.signal=h,g.stdout=d,g.stderr=f,g.cmd=a.cmd;else if(u)g=u,g.killed=e.killed,g.signal=h,g.stdout=d,g.stderr=f,g.cmd=a.cmd;else if(s)g=Error(`Command failed: ${a.cmd}`),g.killed=!0,g.code=null,g.signal=typeof a.killSignal==`string`?a.killSignal:`SIGTERM`,g.stdout=d,g.stderr=f,g.cmd=a.cmd;else if(p!==0&&p!==null){let t=typeof f==`string`?f:f.toString();g=Error(`Command failed: ${a.cmd}\n${t}`),g.code=p,g.killed=e.killed,g.signal=h,g.stdout=d,g.stderr=f,g.cmd=a.cmd}else h&&(g=Error(`Command failed: ${a.cmd}`),g.killed=!0,g.signal=h,g.code=null,g.stdout=d,g.stderr=f,g.cmd=a.cmd);a.callback&&a.callback(g,d,f),e.emit(`exit`,p,h),e.emit(`close`,p,h)}catch(t){let n=t instanceof Error?t:Error(String(t));a.callback&&a.callback(n,``,``),e.emit(`error`,n)}}),queueMicrotask(()=>e.emit(`spawn`))}catch(n){disarmTimeout(),disarmAbort();let r=_gioErrorToNodeError(n,t[0]??``,t.slice(1));setTimeout(()=>{a.callback&&a.callback(r,``,``),e.emit(`error`,r)},0)}return e}function execFile(e,t,n,r){let i=[],a={},s;typeof t==`function`?s=t:Array.isArray(t)?(i=t,typeof n==`function`?s=n:(a=n||{},s=r)):t&&typeof t==`object`&&(a=t,s=typeof n==`function`?n:r);let c=new ChildProcess,l=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE,u=a.maxBuffer??1024*1024,d=a.timeout&&a.timeout>0?a.timeout:0,f=_normalizeKillSignal(a.killSignal);return _execImpl(c,[e,...i],l,a,{cmd:e,encoding:a.encoding,maxBuffer:u,timeoutMs:d,killSignal:f,abortSignal:a.signal,callback:s})}function execFileSync(e,t,n){let r=[],i;Array.isArray(t)?(r=t,i=n):t&&typeof t==`object`&&(i=t);let s=i?.encoding,l=i?.input,u=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(l?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),d;try{({proc:d}=_spawnSubprocess([e,...r],u,i))}catch(t){throw _gioErrorToNodeError(t,e,r)}let f=l?new a.Bytes(typeof l==`string`?new TextEncoder().encode(l):l):null,p=i?.timeout&&i.timeout>0?i.timeout:0,{stdout:h,stderr:g,timedOut:_}=_communicateSync(d,f,p,i?.killSignal);if(_)throw _syncTimeoutError(`${e} ${r.join(` `)}`.trim(),h,g,s,i?.killSignal);let v=d.get_exit_status();if(v!==0){let t=g?new TextDecoder().decode(m(g)):``,n=h?new TextDecoder().decode(m(h)):``,i=Error(`Command failed: ${e} ${r.join(` `)}`);throw i.status=v,s===`buffer`||s===null?(i.stderr=g?c.from(m(g)):c.alloc(0),i.stdout=h?c.from(m(h)):c.alloc(0)):(i.stderr=t,i.stdout=n),i}if(!h)return s===`buffer`||s==null?c.alloc(0):``;let y=m(h);if(s===`buffer`||s==null)return c.from(y);let b=c.isEncoding(s)?s:`utf8`;return c.from(y).toString(b)}function spawn(e,t,n){let r,a;if(Array.isArray(t))r=t,a=n;else if(t!=null){if(typeof t!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof t}`);r=void 0,a=t}else r=void 0,a=t;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 s=r||[],c=new ChildProcess,l=a?.shell,u;u=l?i([e,...s].join(` `),l):[e,...s];let d=a?.stdio,m=Array.isArray(d)?[typeof d[0]==`string`?d[0]:`pipe`,typeof d[1]==`string`?d[1]:`pipe`,typeof d[2]==`string`?d[2]:`pipe`]:typeof d==`string`?[d,d,d]:[`pipe`,`pipe`,`pipe`],g=o.SubprocessFlags.NONE;m[0]===`pipe`?g|=o.SubprocessFlags.STDIN_PIPE:m[0]===`inherit`&&(g|=o.SubprocessFlags.STDIN_INHERIT),m[1]===`pipe`&&(g|=o.SubprocessFlags.STDOUT_PIPE),m[1]===`ignore`&&(g|=o.SubprocessFlags.STDOUT_SILENCE),m[2]===`pipe`&&(g|=o.SubprocessFlags.STDERR_PIPE),m[2]===`ignore`&&(g|=o.SubprocessFlags.STDERR_SILENCE);try{let{proc:e,pid:t}=_spawnSubprocess(u,g,a);if(c._setSubprocess(e,t),h.add(c),m[0]===`pipe`){let t=e.get_stdin_pipe();t&&(c.stdin=new GioOutputStreamWritable(t))}let n=e.get_stdout_pipe();n&&(c.stdout=new GioInputStreamReadable(n));let r=e.get_stderr_pipe();r&&(c.stderr=new GioInputStreamReadable(r));let i=a?.signal,o=null,emitAbortError=()=>{let e=a?.killSignal??`SIGTERM`;c.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,c.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(()=>{c.kill(e),s=null},a.timeout)}p(),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`}c.exitCode=t,c.signalCode=r,s!==null&&(clearTimeout(s),s=null),i&&o&&i.removeEventListener(`abort`,o),c.stdin&&!c.stdin.destroyed&&c.stdin.destroy(),c.emit(`exit`,t,r),c.emit(`close`,t,r)}catch(e){c.emit(`error`,e instanceof Error?e:Error(String(e)))}h.delete(c)}),queueMicrotask(()=>c.emit(`spawn`))}catch(t){f(c,`error`,_gioErrorToNodeError(t,l?e:u[0]??``,l?s:u.slice(1)))}return c}function spawnSync(t,n,s){let l,u;if(Array.isArray(n))l=n,u=s;else if(n!=null){if(typeof n!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof n}`);l=void 0,u=n}else l=void 0,u=n;if(u!=null&&typeof u!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof u}`);if(u?.argv0!==void 0&&u.argv0!==null&&typeof u.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof u.argv0}`);u?.cwd!==void 0&&_normalizeCwd(u.cwd);let d=l||[],f=u?.shell,p=u?.input,h;h=f?i([t,...d].join(` `),f):[t,...d];let g=u?.timeout&&u.timeout>0?u.timeout:0,_=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(p?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),v=p;if(v!=null&&typeof v!=`string`&&!(v instanceof Uint8Array)&&!(v instanceof ArrayBuffer)&&!ArrayBuffer.isView(v))throw TypeError(`The "input" property must be of type string, Buffer, TypedArray, or DataView. Received type ${typeof v}`);let y=u?.encoding,b=y===`buffer`||y==null?c.alloc(0):``,x,S=0;try{({proc:x,pid:S}=_spawnSubprocess(h,_,u))}catch(e){let n=f?t:h[0]??``,r=f?d:h.slice(1);return{pid:0,output:[null,b,b],stdout:b,stderr:b,status:null,signal:null,error:_gioErrorToNodeError(e,n,r)}}let C=p?new a.Bytes(typeof p==`string`?new TextEncoder().encode(p):p):null,w,T,E=null,D=!1;try{if(g>0){let t=e(x,C,g,()=>{r(x,u?.killSignal??`SIGTERM`)});if(D=t.timedOut,t.error)throw t.error;w=t.stdout,T=t.stderr}else{let e=x.communicate(C,null);w=e[1]??null,T=e[2]??null}}catch(e){E=e instanceof Error?e:Error(String(e)),w=null,T=null}let O=w?c.from(m(w)):c.alloc(0),k=T?c.from(m(T)):c.alloc(0),decode=e=>{if(y===`buffer`||y==null)return e;let t=c.isEncoding(y)?y:`utf8`;return e.toString(t)},A=decode(O),j=decode(k),M=x.get_if_exited()?x.get_exit_status():null,N=null;if(x.get_if_signaled()){let e=u?.killSignal;N=typeof e==`string`?e:`SIGTERM`}D&&(N=typeof u?.killSignal==`string`?u.killSignal:`SIGTERM`);let P={pid:S,output:[null,A,j],stdout:A,stderr:j,status:D?null:M,signal:N};if(D){let e=Error(`Command failed: ${h.join(` `)}\n${typeof j==`string`?j:j.toString()}`);e.code=`ETIMEDOUT`,e.killed=!0,e.signal=N,P.error=e}else E&&(P.error=E);return P}var g={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,g as default,_exec as exec,execFile,execFileSync,execSync,spawn,spawnSync};
1
+ import"./_virtual/_rolldown/runtime.js";import{communicateWithTimeout as e}from"./communicate.js";import{applyArgv0 as t,detachedPrefix as n,killProcess as r,shellArgv as i}from"./platform/index.js";import a from"@girs/glib-2.0";import o from"@girs/gio-2.0";import{EventEmitter as s}from"node:events";import{Buffer as c}from"node:buffer";import{Readable as l,Writable as u}from"node:stream";import{fileURLToPath as d}from"node:url";import{deferEmit as f,ensureMainLoop as p,gbytesToUint8Array as m}from"@gjsify/utils";var GioInputStreamReadable=class extends l{_stream;_cancellable=new o.Cancellable;constructor(e){super(),this._stream=e}_read(e){this._stream.read_bytes_async(Math.max(e,4096),a.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(c.from(e))}catch(e){this._cancellable.is_cancelled()||this.destroy(e)}})}_destroy(e,t){this._cancellable.cancel(),t(e)}},GioOutputStreamWritable=class extends u{_stream;_cancellable=new o.Cancellable;_closed=!1;constructor(e){super(),this._stream=e}_write(e,t,n){if(this._closed){n(Error(`write after end`));return}let r=typeof e==`string`?c.from(e):e instanceof Uint8Array?e:c.from(e);this._stream.write_bytes_async(new a.Bytes(r),a.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{this._stream.write_bytes_finish(t),n()}catch(e){this._cancellable.is_cancelled()?n():n(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,t,n){let r=e??Error(`Unknown spawn error`),i=r,o=r.code,s=r.matches,c=!1;if(typeof s==`function`)try{c=s.call(r,a.SpawnError,a.SpawnError.NOENT)===!0}catch{}if(!c&&o===a.SpawnError.NOENT&&(c=!0),!c&&typeof r.message==`string`&&/No such file or directory|ENOENT|Failed to execute|nicht gefunden|nicht ausgeführt/i.test(r.message)&&(c=!0),c){let e=Error(`spawn ${t} ENOENT`);return e.code=`ENOENT`,e.errno=-2,e.syscall=`spawn ${t}`,e.path=t,e.spawnargs=n.slice(),e}if(r instanceof Error)return i;let l=r,u=typeof l.message==`string`?l.message:String(r),d=Error(u);return(typeof o==`number`||typeof o==`string`)&&(d.code=o),d}const h=new Set;var ChildProcess=class extends s{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{return r(this._subprocess,e??`SIGTERM`),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 d(t)}return e instanceof Uint8Array?new TextDecoder().decode(e):String(e)}}function _encodeEnvValue(e){if(e!==void 0)return`${e}`}function _spawnSubprocess(e,r,i){let a=new o.SubprocessLauncher({flags:r}),s=_normalizeCwd(i?.cwd);if(s!==void 0&&a.set_cwd(s),i?.env){let e=i.env;a.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&&a.setenv(n,r,!0)}}let c=e;if(i?.argv0!==void 0&&i.argv0!==null){if(typeof i.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof i.argv0}`);c=t(i.argv0,e)}if(i?.detached===!0){let e=n();e!==null&&(c=[...e,...c])}let l=a.spawnv(c);return{proc:l,pid:_capturePidAtSpawn(l)}}function _capturePidAtSpawn(e){let t=e.get_identifier();if(t){let e=parseInt(t,10);if(e>0)return e}return 0}function _watchStdioForClose(e){let t=[e.stdout,e.stderr].filter(e=>e!==null);if(t.length===0)return(t,n)=>{e.emit(`exit`,t,n),e.emit(`close`,t,n)};let n=t.length,r=!1,i=null,a=null,closeIfDone=()=>{r&&n===0&&e.emit(`close`,i,a)};for(let e of t){let t=!1,settleOnce=()=>{t||(t=!0,--n,closeIfDone())};e.once(`end`,settleOnce),e.once(`close`,settleOnce)}return(n,o)=>{i=n,a=o,r=!0,e.emit(`exit`,n,o),queueMicrotask(()=>{for(let e of t)e.destroyed||e.readableEnded||e.listenerCount(`data`)>0||e.listenerCount(`readable`)>0||e.resume()}),closeIfDone()}}function _communicateSync(t,n,i,a){if(i<=0){let e=t.communicate(n,null);return{stdout:e[1]??null,stderr:e[2]??null,timedOut:!1}}let o=e(t,n,i,()=>{r(t,a??`SIGTERM`)});if(o.error)throw o.error;return{stdout:o.stdout,stderr:o.stderr,timedOut:o.timedOut}}function _syncTimeoutError(e,t,n,r,i){let a=t?m(t):new Uint8Array,o=n?m(n):new Uint8Array,s=new TextDecoder().decode(o),l=Error(`Command failed: ${e}\n${s}`);return l.code=`ETIMEDOUT`,l.killed=!0,l.signal=typeof i==`string`?i:`SIGTERM`,l.status=null,r===`buffer`||r===null?(l.stdout=c.from(a),l.stderr=c.from(o)):(l.stdout=new TextDecoder().decode(a),l.stderr=s),l}function execSync(e,t){let n=t?.encoding,r=t?.input,s=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(r?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),l=i(e,t?.shell),u;try{({proc:u}=_spawnSubprocess(l,s,t))}catch(e){throw _gioErrorToNodeError(e,l[0],l.slice(1))}let d=r?new a.Bytes(typeof r==`string`?new TextEncoder().encode(r):r):null,f=t?.timeout&&t.timeout>0?t.timeout:0,{stdout:p,stderr:h,timedOut:g}=_communicateSync(u,d,f,t?.killSignal);if(g)throw _syncTimeoutError(e,p,h,n,t?.killSignal);let _=u.get_exit_status();if(_!==0){let t=h?new TextDecoder().decode(m(h)):``,r=p?new TextDecoder().decode(m(p)):``,i=Error(`Command failed: ${e}\n${t}`);throw i.status=_,n===`buffer`||n===null?(i.stderr=h?c.from(m(h)):c.alloc(0),i.stdout=p?c.from(m(p)):c.alloc(0)):(i.stderr=t,i.stdout=r),i}if(!p)return n===`buffer`||n==null?c.alloc(0):``;let v=m(p);if(n===`buffer`||n==null)return c.from(v);let y=c.isEncoding(n)?n:`utf8`;return c.from(v).toString(y)}function _decodeExecOutput(e,t){let n=e??new Uint8Array;if(t===`buffer`||t===null)return c.from(n);let r=t&&c.isEncoding(t)?t:`utf8`;return c.from(n).toString(r)}function _normalizeKillSignal(e){return e??`SIGTERM`}function _exec(e,t,n){typeof t==`function`&&(n=t,t={});let r=t||{},a=new ChildProcess,s=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE,c=r.maxBuffer??1024*1024,l=r.timeout&&r.timeout>0?r.timeout:0,u=_normalizeKillSignal(r.killSignal),d=r.signal;return _execImpl(a,i(e,r.shell),s,r,{cmd:e,encoding:r.encoding,maxBuffer:c,timeoutMs:l,killSignal:u,abortSignal:d,callback:n})}function _absorbExecError(){}function _execImpl(e,t,n,i,a){e.on(`error`,_absorbExecError);let o=null,s=!1,c=null,l=null,armTimeout=t=>{a.timeoutMs&&(o=setTimeout(()=>{s=!0;try{r(t,a.killSignal)}catch{}e.killed=!0},a.timeoutMs))},disarmTimeout=()=>{o!==null&&(clearTimeout(o),o=null)},armAbort=t=>{if(!a.abortSignal)return;let fire=()=>{c=Error(`The operation was aborted`),c.name=`AbortError`;try{r(t,a.killSignal)}catch{}e.killed=!0};if(a.abortSignal.aborted){queueMicrotask(fire);return}l=fire,a.abortSignal.addEventListener(`abort`,l,{once:!0})},disarmAbort=()=>{a.abortSignal&&l&&(a.abortSignal.removeEventListener(`abort`,l),l=null)};try{let{proc:r,pid:o}=_spawnSubprocess(t,n,i);e._setSubprocess(r,o),h.add(e),p(),armTimeout(r),armAbort(r);let l=_watchStdioForClose(e);r.communicate_async(null,null,(t,n)=>{disarmTimeout(),disarmAbort(),h.delete(e);try{let[,t,i]=r.communicate_finish(n),o=t?m(t):null,u=i?m(i):null,d=null;a.maxBuffer!==1/0&&o&&o.length>a.maxBuffer&&(o=o.subarray(0,a.maxBuffer),d=RangeError(`stdout maxBuffer length exceeded`),d.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`),!d&&a.maxBuffer!==1/0&&u&&u.length>a.maxBuffer&&(u=u.subarray(0,a.maxBuffer),d=RangeError(`stderr maxBuffer length exceeded`),d.code=`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`);let f=_decodeExecOutput(o,a.encoding),p=_decodeExecOutput(u,a.encoding),h=r.get_if_exited()?r.get_exit_status():null,g=r.get_if_signaled()?typeof a.killSignal==`string`?a.killSignal:`SIGTERM`:null;e.exitCode=h,e.signalCode=g;let _=null;if(c)_=c,_.killed=!0,_.signal=g,_.stdout=f,_.stderr=p,_.cmd=a.cmd;else if(d)_=d,_.killed=e.killed,_.signal=g,_.stdout=f,_.stderr=p,_.cmd=a.cmd;else if(s)_=Error(`Command failed: ${a.cmd}`),_.killed=!0,_.code=null,_.signal=typeof a.killSignal==`string`?a.killSignal:`SIGTERM`,_.stdout=f,_.stderr=p,_.cmd=a.cmd;else if(h!==0&&h!==null){let t=typeof p==`string`?p:p.toString();_=Error(`Command failed: ${a.cmd}\n${t}`),_.code=h,_.killed=e.killed,_.signal=g,_.stdout=f,_.stderr=p,_.cmd=a.cmd}else g&&(_=Error(`Command failed: ${a.cmd}`),_.killed=!0,_.signal=g,_.code=null,_.stdout=f,_.stderr=p,_.cmd=a.cmd);a.callback&&a.callback(_,f,p),l(h,g)}catch(t){let n=t instanceof Error?t:Error(String(t));a.callback&&a.callback(n,``,``),e.emit(`error`,n)}}),queueMicrotask(()=>e.emit(`spawn`))}catch(n){disarmTimeout(),disarmAbort();let r=_gioErrorToNodeError(n,t[0]??``,t.slice(1));setTimeout(()=>{a.callback&&a.callback(r,``,``),e.emit(`error`,r)},0)}return e}function execFile(e,t,n,r){let i=[],a={},s;typeof t==`function`?s=t:Array.isArray(t)?(i=t,typeof n==`function`?s=n:(a=n||{},s=r)):t&&typeof t==`object`&&(a=t,s=typeof n==`function`?n:r);let c=new ChildProcess,l=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE,u=a.maxBuffer??1024*1024,d=a.timeout&&a.timeout>0?a.timeout:0,f=_normalizeKillSignal(a.killSignal);return _execImpl(c,[e,...i],l,a,{cmd:e,encoding:a.encoding,maxBuffer:u,timeoutMs:d,killSignal:f,abortSignal:a.signal,callback:s})}function execFileSync(e,t,n){let r=[],i;Array.isArray(t)?(r=t,i=n):t&&typeof t==`object`&&(i=t);let s=i?.encoding,l=i?.input,u=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(l?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),d;try{({proc:d}=_spawnSubprocess([e,...r],u,i))}catch(t){throw _gioErrorToNodeError(t,e,r)}let f=l?new a.Bytes(typeof l==`string`?new TextEncoder().encode(l):l):null,p=i?.timeout&&i.timeout>0?i.timeout:0,{stdout:h,stderr:g,timedOut:_}=_communicateSync(d,f,p,i?.killSignal);if(_)throw _syncTimeoutError(`${e} ${r.join(` `)}`.trim(),h,g,s,i?.killSignal);let v=d.get_exit_status();if(v!==0){let t=g?new TextDecoder().decode(m(g)):``,n=h?new TextDecoder().decode(m(h)):``,i=Error(`Command failed: ${e} ${r.join(` `)}`);throw i.status=v,s===`buffer`||s===null?(i.stderr=g?c.from(m(g)):c.alloc(0),i.stdout=h?c.from(m(h)):c.alloc(0)):(i.stderr=t,i.stdout=n),i}if(!h)return s===`buffer`||s==null?c.alloc(0):``;let y=m(h);if(s===`buffer`||s==null)return c.from(y);let b=c.isEncoding(s)?s:`utf8`;return c.from(y).toString(b)}function spawn(e,t,n){let r,a;if(Array.isArray(t))r=t,a=n;else if(t!=null){if(typeof t!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof t}`);r=void 0,a=t}else r=void 0,a=t;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 s=r||[],c=new ChildProcess,l=a?.shell,u;u=l?i([e,...s].join(` `),l):[e,...s];let d=a?.stdio,m=Array.isArray(d)?[typeof d[0]==`string`?d[0]:`pipe`,typeof d[1]==`string`?d[1]:`pipe`,typeof d[2]==`string`?d[2]:`pipe`]:typeof d==`string`?[d,d,d]:[`pipe`,`pipe`,`pipe`],g=o.SubprocessFlags.NONE;m[0]===`pipe`?g|=o.SubprocessFlags.STDIN_PIPE:m[0]===`inherit`&&(g|=o.SubprocessFlags.STDIN_INHERIT),m[1]===`pipe`&&(g|=o.SubprocessFlags.STDOUT_PIPE),m[1]===`ignore`&&(g|=o.SubprocessFlags.STDOUT_SILENCE),m[2]===`pipe`&&(g|=o.SubprocessFlags.STDERR_PIPE),m[2]===`ignore`&&(g|=o.SubprocessFlags.STDERR_SILENCE);try{let{proc:e,pid:t}=_spawnSubprocess(u,g,a);if(c._setSubprocess(e,t),h.add(c),m[0]===`pipe`){let t=e.get_stdin_pipe();t&&(c.stdin=new GioOutputStreamWritable(t))}let n=e.get_stdout_pipe();n&&(c.stdout=new GioInputStreamReadable(n));let r=e.get_stderr_pipe();r&&(c.stderr=new GioInputStreamReadable(r));let i=_watchStdioForClose(c),o=a?.signal,s=null,emitAbortError=()=>{let e=a?.killSignal??`SIGTERM`;c.kill(e);let t=Error(`The operation was aborted`);t.name=`AbortError`,c.emit(`error`,t)};o&&(o.aborted?queueMicrotask(emitAbortError):(s=emitAbortError,o.addEventListener(`abort`,s,{once:!0})));let l=null;if(a?.timeout&&a.timeout>0){let e=a.killSignal??`SIGTERM`;l=setTimeout(()=>{c.kill(e),l=null},a.timeout)}p(),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`}c.exitCode=t,c.signalCode=r,l!==null&&(clearTimeout(l),l=null),o&&s&&o.removeEventListener(`abort`,s),c.stdin&&!c.stdin.destroyed&&c.stdin.destroy(),i(t,r)}catch(e){c.emit(`error`,e instanceof Error?e:Error(String(e)))}h.delete(c)}),queueMicrotask(()=>c.emit(`spawn`))}catch(t){f(c,`error`,_gioErrorToNodeError(t,l?e:u[0]??``,l?s:u.slice(1)))}return c}function spawnSync(t,n,s){let l,u;if(Array.isArray(n))l=n,u=s;else if(n!=null){if(typeof n!=`object`)throw TypeError(`The "args" argument must be of type object. Received type ${typeof n}`);l=void 0,u=n}else l=void 0,u=n;if(u!=null&&typeof u!=`object`)throw TypeError(`The "options" argument must be of type object. Received type ${typeof u}`);if(u?.argv0!==void 0&&u.argv0!==null&&typeof u.argv0!=`string`)throw TypeError(`The "options.argv0" property must be of type string. Received type ${typeof u.argv0}`);u?.cwd!==void 0&&_normalizeCwd(u.cwd);let d=l||[],f=u?.shell,p=u?.input,h;h=f?i([t,...d].join(` `),f):[t,...d];let g=u?.timeout&&u.timeout>0?u.timeout:0,_=o.SubprocessFlags.STDOUT_PIPE|o.SubprocessFlags.STDERR_PIPE|(p?o.SubprocessFlags.STDIN_PIPE:o.SubprocessFlags.NONE),v=p;if(v!=null&&typeof v!=`string`&&!(v instanceof Uint8Array)&&!(v instanceof ArrayBuffer)&&!ArrayBuffer.isView(v))throw TypeError(`The "input" property must be of type string, Buffer, TypedArray, or DataView. Received type ${typeof v}`);let y=u?.encoding,b=y===`buffer`||y==null?c.alloc(0):``,x,S=0;try{({proc:x,pid:S}=_spawnSubprocess(h,_,u))}catch(e){let n=f?t:h[0]??``,r=f?d:h.slice(1);return{pid:0,output:[null,b,b],stdout:b,stderr:b,status:null,signal:null,error:_gioErrorToNodeError(e,n,r)}}let C=p?new a.Bytes(typeof p==`string`?new TextEncoder().encode(p):p):null,w,T,E=null,D=!1;try{if(g>0){let t=e(x,C,g,()=>{r(x,u?.killSignal??`SIGTERM`)});if(D=t.timedOut,t.error)throw t.error;w=t.stdout,T=t.stderr}else{let e=x.communicate(C,null);w=e[1]??null,T=e[2]??null}}catch(e){E=e instanceof Error?e:Error(String(e)),w=null,T=null}let O=w?c.from(m(w)):c.alloc(0),k=T?c.from(m(T)):c.alloc(0),decode=e=>{if(y===`buffer`||y==null)return e;let t=c.isEncoding(y)?y:`utf8`;return e.toString(t)},A=decode(O),j=decode(k),M=x.get_if_exited()?x.get_exit_status():null,N=null;if(x.get_if_signaled()){let e=u?.killSignal;N=typeof e==`string`?e:`SIGTERM`}D&&(N=typeof u?.killSignal==`string`?u.killSignal:`SIGTERM`);let P={pid:S,output:[null,A,j],stdout:A,stderr:j,status:D?null:M,signal:N};if(D){let e=Error(`Command failed: ${h.join(` `)}\n${typeof j==`string`?j:j.toString()}`);e.code=`ETIMEDOUT`,e.killed=!0,e.signal=N,P.error=e}else E&&(P.error=E);return P}var g={ChildProcess,exec:_exec,execSync,execFile,execFileSync,spawn,spawnSync};export{ChildProcess,g as default,_exec as exec,execFile,execFileSync,execSync,spawn,spawnSync};
@@ -77,6 +77,38 @@ export declare function printEnvTemplate(template: string): Command;
77
77
  * live when `env` carries an `undefined` value.
78
78
  */
79
79
  export declare function printEnvThenCat(name: string): Command;
80
+ /**
81
+ * Write `EARLY`, exit at once, and leave a GRANDCHILD holding the same stdout
82
+ * pipe that writes `LATE` after `delayMs` — the scenario that separates `exit`
83
+ * from `close`.
84
+ *
85
+ * The grandchild is spawned with the parent's own stdout INHERITED, so the pipe
86
+ * has no EOF while it runs: Node holds `close` until that pipe closes, and a
87
+ * consumer that ends on `close` still receives `LATE`. `sh -c '( sleep 0.5;
88
+ * printf LATE ) & printf EARLY; exit 0'` is the same shape, but it needs a
89
+ * POSIX shell — the reason every other command here is driven through the
90
+ * interpreter running this suite instead.
91
+ *
92
+ * `unref()` in the Node dialect is what makes it the same shape AT ALL: a
93
+ * referenced child handle keeps the parent's event loop alive until the
94
+ * grandchild exits, so the direct child would sit there past its own `exit` and
95
+ * the ordering under test could never occur. Gio has no equivalent — a
96
+ * `Gio.Subprocess` does not hold the GJS main loop — so only Node needs it.
97
+ *
98
+ * PASS 500, not a token delay. The window has to survive the direct child
99
+ * being spawned, its interpreter booting and its write reaching the pipe before
100
+ * the grandchild's timer is even started, and the tightest budget for that is
101
+ * the Windows CI leg: a loaded runner there can take hundreds of ms just to
102
+ * schedule the next process, which is the margin that has to absorb it. Too
103
+ * small and the grandchild writes before the direct child is reaped, the pipe
104
+ * closes with `exit` and `close` together, and the test silently stops testing
105
+ * anything. Too large only costs wall clock — the whole bundle budget is 30 s.
106
+ *
107
+ * The grandchild's source travels as an extra ARGV entry instead of being
108
+ * nested inside the parent's, because the two dialects quote differently and a
109
+ * literal-in-a-literal is how that goes wrong.
110
+ */
111
+ export declare function earlyThenLateStdout(delayMs: number): Command;
80
112
  /** Print an environment variable, then the working directory on the next line. */
81
113
  export declare function printEnvThenPwd(name: string): Command;
82
114
  /** Running under a `cmd.exe` shell rather than a POSIX one? */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/child_process",
3
- "version": "0.53.0",
3
+ "version": "0.55.0",
4
4
  "description": "Node.js child_process module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -37,18 +37,18 @@
37
37
  "child_process"
38
38
  ],
39
39
  "devDependencies": {
40
- "@gjsify/cli": "^0.53.0",
41
- "@gjsify/dom-elements": "^0.53.0",
42
- "@gjsify/unit": "^0.53.0",
40
+ "@gjsify/cli": "^0.55.0",
41
+ "@gjsify/dom-elements": "^0.55.0",
42
+ "@gjsify/unit": "^0.55.0",
43
43
  "@types/node": "^25.9.2",
44
44
  "typescript": "^6.0.3"
45
45
  },
46
46
  "dependencies": {
47
47
  "@girs/gio-2.0": "5.4.0",
48
48
  "@girs/glib-2.0": "5.4.0",
49
- "@gjsify/buffer": "^0.53.0",
50
- "@gjsify/events": "^0.53.0",
51
- "@gjsify/utils": "^0.53.0"
49
+ "@gjsify/buffer": "^0.55.0",
50
+ "@gjsify/events": "^0.55.0",
51
+ "@gjsify/utils": "^0.55.0"
52
52
  },
53
53
  "gjsify": {
54
54
  "runtimes": {