@gjsify/worker_threads 0.3.19 → 0.3.21

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{MessagePort as e}from"./message-port.js";import{Worker as t}from"./worker.js";const n=globalThis.__gjsify_worker_context,r=!n,i=n?.parentPort??null,a=n?.workerData??null,o=n?.threadId??0,s={},c=Symbol(`worker_threads.SHARE_ENV`);var l=class{port1;port2;constructor(){this.port1=new e,this.port2=new e,this.port1._otherPort=this.port2,this.port2._otherPort=this.port1}};const u=new Map;var d=class{name;_closed=!1;onmessage=null;onmessageerror=null;constructor(e){this.name=String(e),u.has(this.name)||u.set(this.name,new Set),u.get(this.name).add(this)}postMessage(e){if(this._closed)throw Error(`BroadcastChannel is closed`);let t=u.get(this.name);if(t){for(let n of t)if(n!==this&&!n._closed){let t=structuredClone(e);Promise.resolve().then(()=>{!n._closed&&n.onmessage&&n.onmessage({data:t})})}}}close(){if(this._closed)return;this._closed=!0;let e=u.get(this.name);e&&(e.delete(this),e.size===0&&u.delete(this.name)),this.onmessage=null,this.onmessageerror=null}addEventListener(e,t){e===`message`?this.onmessage=t:e===`messageerror`&&(this.onmessageerror=t)}removeEventListener(e,t){e===`message`?this.onmessage=null:e===`messageerror`&&(this.onmessageerror=null)}dispatchEvent(e){return!0}};const f=new Map;function p(e,t){t===void 0?f.delete(e):f.set(e,t)}function m(e){return f.get(e)}function h(e){if(e._hasQueuedMessages)return{message:e._dequeueMessage()}}function g(e){}function _(e){}function v(e,t){return e}var y={isMainThread:r,parentPort:i,workerData:a,threadId:o,resourceLimits:s,SHARE_ENV:c,Worker:t,MessageChannel:l,MessagePort:e,BroadcastChannel:d,setEnvironmentData:p,getEnvironmentData:m,receiveMessageOnPort:h,markAsUntransferable:g,markAsUncloneable:_,moveMessagePortToContext:v};export{d as BroadcastChannel,l as MessageChannel,e as MessagePort,c as SHARE_ENV,t as Worker,y as default,m as getEnvironmentData,r as isMainThread,_ as markAsUncloneable,g as markAsUntransferable,v as moveMessagePortToContext,i as parentPort,h as receiveMessageOnPort,s as resourceLimits,p as setEnvironmentData,o as threadId,a as workerData};
1
+ import{MessagePort as e}from"./message-port.js";import{Worker as t}from"./worker.js";const n=globalThis.__gjsify_worker_context,r=!n,i=n?.parentPort??null,a=n?.workerData??null,o=n?.threadId??0,s={},c=Symbol(`worker_threads.SHARE_ENV`);var MessageChannel=class{port1;port2;constructor(){this.port1=new e,this.port2=new e,this.port1._otherPort=this.port2,this.port2._otherPort=this.port1}};const l=new Map;var BroadcastChannel=class{name;_closed=!1;onmessage=null;onmessageerror=null;constructor(e){this.name=String(e),l.has(this.name)||l.set(this.name,new Set),l.get(this.name).add(this)}postMessage(e){if(this._closed)throw Error(`BroadcastChannel is closed`);let t=l.get(this.name);if(t){for(let n of t)if(n!==this&&!n._closed){let t=structuredClone(e);Promise.resolve().then(()=>{!n._closed&&n.onmessage&&n.onmessage({data:t})})}}}close(){if(this._closed)return;this._closed=!0;let e=l.get(this.name);e&&(e.delete(this),e.size===0&&l.delete(this.name)),this.onmessage=null,this.onmessageerror=null}addEventListener(e,t){e===`message`?this.onmessage=t:e===`messageerror`&&(this.onmessageerror=t)}removeEventListener(e,t){e===`message`?this.onmessage=null:e===`messageerror`&&(this.onmessageerror=null)}dispatchEvent(e){return!0}};const u=new Map;function setEnvironmentData(e,t){t===void 0?u.delete(e):u.set(e,t)}function getEnvironmentData(e){return u.get(e)}function receiveMessageOnPort(e){if(e._hasQueuedMessages)return{message:e._dequeueMessage()}}function markAsUntransferable(e){}function markAsUncloneable(e){}function moveMessagePortToContext(e,t){return e}var d={isMainThread:r,parentPort:i,workerData:a,threadId:o,resourceLimits:s,SHARE_ENV:c,Worker:t,MessageChannel,MessagePort:e,BroadcastChannel,setEnvironmentData,getEnvironmentData,receiveMessageOnPort,markAsUntransferable,markAsUncloneable,moveMessagePortToContext};export{BroadcastChannel,MessageChannel,e as MessagePort,c as SHARE_ENV,t as Worker,d as default,getEnvironmentData,r as isMainThread,markAsUncloneable,markAsUntransferable,moveMessagePortToContext,i as parentPort,receiveMessageOnPort,s as resourceLimits,setEnvironmentData,o as threadId,a as workerData};
@@ -1 +1 @@
1
- import{EventEmitter as e}from"node:events";var t=class extends e{_started=!1;_closed=!1;_messageQueue=[];_otherPort=null;_aeWrappers=new Map;start(){this._started||this._closed||(this._started=!0,this._drainQueue())}close(){if(this._closed)return;this._closed=!0;let e=this._otherPort;this._otherPort=null,e&&(e._otherPort=null),this.emit(`close`),this.removeAllListeners()}postMessage(e,t){if(this._closed)return;let n=this._otherPort;if(!n)return;let r;try{r=structuredClone(e)}catch(e){this.emit(`messageerror`,e instanceof Error?e:Error(`Could not clone message`));return}n._receiveMessage(r)}ref(){return this}unref(){return this}_receiveMessage(e){if(!this._closed){if(!this._started){this._messageQueue.push(e);return}this._dispatchMessage(e)}}get _hasQueuedMessages(){return this._messageQueue.length>0}_dequeueMessage(){return this._messageQueue.shift()}_drainQueue(){for(;this._messageQueue.length>0;)this._dispatchMessage(this._messageQueue.shift())}_dispatchMessage(e){Promise.resolve().then(()=>{this._closed||this.emit(`message`,e)})}addEventListener(e,t){if(t)if(e===`message`){let e=e=>{t({data:e,type:`message`})};this._aeWrappers.set(t,e),super.on(`message`,e)}else super.on(e,t)}removeEventListener(e,t){if(t)if(e===`message`){let e=this._aeWrappers.get(t);e&&(super.off(`message`,e),this._aeWrappers.delete(t))}else super.off(e,t)}on(e,t){return super.on(e,t),e===`message`&&!this._started&&this.start(),this}addListener(e,t){return this.on(e,t)}once(e,t){return super.once(e,t),e===`message`&&!this._started&&this.start(),this}};export{t as MessagePort};
1
+ import{EventEmitter as e}from"node:events";var MessagePort=class extends e{_started=!1;_closed=!1;_messageQueue=[];_otherPort=null;_aeWrappers=new Map;start(){this._started||this._closed||(this._started=!0,this._drainQueue())}close(){if(this._closed)return;this._closed=!0;let e=this._otherPort;this._otherPort=null,e&&(e._otherPort=null),this.emit(`close`),this.removeAllListeners()}postMessage(e,t){if(this._closed)return;let n=this._otherPort;if(!n)return;let r;try{r=structuredClone(e)}catch(e){this.emit(`messageerror`,e instanceof Error?e:Error(`Could not clone message`));return}n._receiveMessage(r)}ref(){return this}unref(){return this}_receiveMessage(e){if(!this._closed){if(!this._started){this._messageQueue.push(e);return}this._dispatchMessage(e)}}get _hasQueuedMessages(){return this._messageQueue.length>0}_dequeueMessage(){return this._messageQueue.shift()}_drainQueue(){for(;this._messageQueue.length>0;)this._dispatchMessage(this._messageQueue.shift())}_dispatchMessage(e){Promise.resolve().then(()=>{this._closed||this.emit(`message`,e)})}addEventListener(e,t){if(t)if(e===`message`){let wrapper=e=>{t({data:e,type:`message`})};this._aeWrappers.set(t,wrapper),super.on(`message`,wrapper)}else super.on(e,t)}removeEventListener(e,t){if(t)if(e===`message`){let e=this._aeWrappers.get(t);e&&(super.off(`message`,e),this._aeWrappers.delete(t))}else super.off(e,t)}on(e,t){return super.on(e,t),e===`message`&&!this._started&&this.start(),this}addListener(e,t){return this.on(e,t)}once(e,t){return super.once(e,t),e===`message`&&!this._started&&this.start(),this}};export{MessagePort};
package/lib/esm/worker.js CHANGED
@@ -81,8 +81,8 @@ try {
81
81
  }
82
82
 
83
83
  loop.run();
84
- `);var o=class o extends e{threadId;resourceLimits;_subprocess=null;_stdinPipe=null;_exited=!1;_bootstrapFile=null;constructor(e,s){super(),this.threadId=r++,this.resourceLimits=s?.resourceLimits||{};let c=s?.eval===!0,l=o._resolveFilename(e,c),u=`${n.get_tmp_dir()}/gjsify-worker-${this.threadId}-${Date.now()}.mjs`;this._bootstrapFile=t.File.new_for_path(u);try{this._bootstrapFile.replace_contents(a,null,!1,t.FileCreateFlags.REPLACE_DESTINATION,null)}catch(e){throw Error(`Failed to create worker bootstrap: ${e instanceof Error?e.message:e}`)}let d=new t.SubprocessLauncher({flags:t.SubprocessFlags.STDIN_PIPE|t.SubprocessFlags.STDOUT_PIPE|t.SubprocessFlags.STDERR_PIPE});if(s?.env&&typeof s.env==`object`)for(let[e,t]of Object.entries(s.env))d.setenv(e,String(t),!0);try{this._subprocess=d.spawnv([`gjs`,`-m`,u])}catch(e){throw this._cleanup(),Error(`Failed to spawn worker: ${e instanceof Error?e.message:e}`)}this._stdinPipe=this._subprocess.get_stdin_pipe();let f=this._subprocess.get_stdout_pipe();if(!c){let e=l.startsWith(`file://`)?l.slice(7):l;if(!t.File.new_for_path(e).query_exists(null)){this._cleanup();let t=Error(`Cannot find module '${e}'`);t.code=`ERR_MODULE_NOT_FOUND`,Promise.resolve().then(()=>{this.emit(`error`,t),this._exited=!0,this.emit(`exit`,1)});return}}let p=JSON.stringify({threadId:this.threadId,workerData:s?.workerData??null,eval:c,filename:c?void 0:l,code:c?l:void 0})+`
85
- `;try{this._stdinPipe.write_all(i.encode(p),null)}catch(e){throw this._cleanup(),Error(`Failed to send init data: ${e instanceof Error?e.message:e}`)}if(f){let e=t.DataInputStream.new(f);this._readMessages(e)}let m=this._subprocess.get_stderr_pipe();m&&this._readStderr(t.DataInputStream.new(m)),this._subprocess.wait_async(null,()=>{this._onExit()})}postMessage(e,t){if(!(this._exited||!this._stdinPipe))try{let t=JSON.stringify({type:`message`,data:e})+`
84
+ `);var o=class Worker extends e{threadId;resourceLimits;_subprocess=null;_stdinPipe=null;_exited=!1;_bootstrapFile=null;constructor(e,o){super(),this.threadId=r++,this.resourceLimits=o?.resourceLimits||{};let s=o?.eval===!0,c=Worker._resolveFilename(e,s),l=`${n.get_tmp_dir()}/gjsify-worker-${this.threadId}-${Date.now()}.mjs`;this._bootstrapFile=t.File.new_for_path(l);try{this._bootstrapFile.replace_contents(a,null,!1,t.FileCreateFlags.REPLACE_DESTINATION,null)}catch(e){throw Error(`Failed to create worker bootstrap: ${e instanceof Error?e.message:e}`)}let u=new t.SubprocessLauncher({flags:t.SubprocessFlags.STDIN_PIPE|t.SubprocessFlags.STDOUT_PIPE|t.SubprocessFlags.STDERR_PIPE});if(o?.env&&typeof o.env==`object`)for(let[e,t]of Object.entries(o.env))u.setenv(e,String(t),!0);try{this._subprocess=u.spawnv([`gjs`,`-m`,l])}catch(e){throw this._cleanup(),Error(`Failed to spawn worker: ${e instanceof Error?e.message:e}`)}this._stdinPipe=this._subprocess.get_stdin_pipe();let d=this._subprocess.get_stdout_pipe();if(!s){let e=c.startsWith(`file://`)?c.slice(7):c;if(!t.File.new_for_path(e).query_exists(null)){this._cleanup();let t=Error(`Cannot find module '${e}'`);t.code=`ERR_MODULE_NOT_FOUND`,Promise.resolve().then(()=>{this.emit(`error`,t),this._exited=!0,this.emit(`exit`,1)});return}}let f=JSON.stringify({threadId:this.threadId,workerData:o?.workerData??null,eval:s,filename:s?void 0:c,code:s?c:void 0})+`
85
+ `;try{this._stdinPipe.write_all(i.encode(f),null)}catch(e){throw this._cleanup(),Error(`Failed to send init data: ${e instanceof Error?e.message:e}`)}if(d){let e=t.DataInputStream.new(d);this._readMessages(e)}let p=this._subprocess.get_stderr_pipe();p&&this._readStderr(t.DataInputStream.new(p)),this._subprocess.wait_async(null,()=>{this._onExit()})}postMessage(e,t){if(!(this._exited||!this._stdinPipe))try{let t=JSON.stringify({type:`message`,data:e})+`
86
86
  `;this._stdinPipe.write_all(i.encode(t),null)}catch{}}terminate(){if(this._exited)return Promise.resolve(0);let e=new Promise(e=>{this.once(`exit`,t=>e(t))});try{if(this._stdinPipe){let e=JSON.stringify({type:`terminate`})+`
87
87
  `;this._stdinPipe.write_all(i.encode(e),null)}}catch{}return setTimeout(()=>{!this._exited&&this._subprocess&&this._subprocess.force_exit()},500),e}ref(){return this}unref(){return this}static _resolveFilename(e,r){if(r)return String(e);if(e instanceof URL)return e.href;let i=String(e);if(i.startsWith(`file://`)||i.startsWith(`http://`)||i.startsWith(`https://`))return i;if(i.startsWith(`/`))return`file://`+i;if(i.startsWith(`./`)||i.startsWith(`../`)||!i.includes(`/`)){let e=n.get_current_dir(),r=n.build_filenamev([e,i]);return`file://`+(t.File.new_for_path(r).get_path()||r)}return`file://`+i}_readMessages(e){e.read_line_async(n.PRIORITY_DEFAULT,null,(t,n)=>{try{let[t]=e.read_line_finish_utf8(n);if(t===null)return;let r=JSON.parse(t);switch(r.type){case`online`:this.emit(`online`);break;case`message`:this.emit(`message`,r.data);break;case`error`:{let e=Error(r.message);r.stack&&(e.stack=r.stack),this.emit(`error`,e);break}}this._readMessages(e)}catch{}})}_stderrChunks=[];_readStderr(e){e.read_line_async(n.PRIORITY_DEFAULT,null,(t,n)=>{try{let[t]=e.read_line_finish_utf8(n);if(t===null){if(this._stderrChunks.length>0){let e=this._stderrChunks.join(`
88
88
  `);this.listenerCount(`error`)===0&&this.emit(`error`,Error(e))}return}this._stderrChunks.push(t),this._readStderr(e)}catch{}})}_onExit(){if(this._exited)return;this._exited=!0;let e=this._subprocess?.get_if_exited()?this._subprocess.get_exit_status():1;this._cleanup(),this.emit(`exit`,e)}_cleanup(){if(this._bootstrapFile){try{this._bootstrapFile.delete(null)}catch{}this._bootstrapFile=null}if(this._stdinPipe){try{this._stdinPipe.close(null)}catch{}this._stdinPipe=null}this._subprocess=null}};export{o as Worker};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/worker_threads",
3
- "version": "0.3.19",
3
+ "version": "0.3.21",
4
4
  "description": "Node.js worker_threads module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -30,15 +30,15 @@
30
30
  "worker_threads"
31
31
  ],
32
32
  "devDependencies": {
33
- "@gjsify/cli": "^0.3.19",
34
- "@gjsify/node-globals": "^0.3.19",
35
- "@gjsify/unit": "^0.3.19",
33
+ "@gjsify/cli": "^0.3.21",
34
+ "@gjsify/node-globals": "^0.3.21",
35
+ "@gjsify/unit": "^0.3.21",
36
36
  "@types/node": "^25.6.2",
37
37
  "typescript": "^6.0.3"
38
38
  },
39
39
  "dependencies": {
40
40
  "@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
41
41
  "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
42
- "@gjsify/events": "^0.3.19"
42
+ "@gjsify/events": "^0.3.21"
43
43
  }
44
44
  }