From 38ab7fe300ad255012bbab5760f93447d9d3a79b Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 11:19:55 +0100 Subject: [PATCH 1/9] fmt Signed-off-by: duart38 --- examples/example_async_support.ts | 4 ++-- examples/example_simple.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/example_async_support.ts b/examples/example_async_support.ts index 4f46df9..c3de391 100644 --- a/examples/example_async_support.ts +++ b/examples/example_async_support.ts @@ -9,12 +9,12 @@ import Thread from "../Thread.ts"; const thread = new Thread(async (e) => { console.log("Worker: Message received from main script"); const result = e.data[0] * e.data[1]; - await new Promise((resolve) => setTimeout(resolve, 5 * 1000)) + await new Promise((resolve) => setTimeout(resolve, 5 * 1000)); if (isNaN(result)) { return 0; } else { console.log("Worker: Posting message back to main script"); - return (result); + return result; } }, "module"); diff --git a/examples/example_simple.ts b/examples/example_simple.ts index 6b7cca0..64f56af 100644 --- a/examples/example_simple.ts +++ b/examples/example_simple.ts @@ -7,7 +7,7 @@ const thread = new Thread((e) => { return 0; } else { console.log("Worker: Posting message back to main script"); - return (result); + return result; } }); From 8f7205293d1de3df2caa08a85018cf89a30a1302 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 11:24:41 +0100 Subject: [PATCH 2/9] add debugging system Signed-off-by: duart38 --- Thread.ts | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/Thread.ts b/Thread.ts index ab9646c..0f2a57b 100644 --- a/Thread.ts +++ b/Thread.ts @@ -8,6 +8,8 @@ export default class Thread { private imports: Array; private blob: Promise; private blobURL = ""; + public debugMode: boolean; + /** * Tells if the worker has been stopped */ @@ -23,10 +25,12 @@ export default class Thread { ) => T | Promise, type?: "classic" | "module", imports?: Array, + opts: { debug?: boolean } = { debug: false }, ) { this.imports = imports || []; this.blob = this.populateFile(operation); this.worker = this.makeWorker(type); + this.debugMode = opts.debug ?? false; } private async makeWorker(type?: "classic" | "module") { @@ -44,17 +48,18 @@ export default class Thread { const imported = this.imports?.flatMap(async (val) => (await this.copyDep(val)).join("\n") ); - return new Blob([` - ${(await Promise.all(imported)).join("\n")} - - var global = {}; - var userCode = ${code.toString()} - - onmessage = async function(e) { - postMessage(await userCode(e, global)); - } - - `]); + const blobContent = ` +${(await Promise.all(imported)).join("\n")} + +var global = {}; +var userCode = ${code.toString()} + +onmessage = async function(e) { + postMessage(await userCode(e, global)); +} +`; + this.debug(`\n\n\nBlob content:\n ${blobContent}\n\n\n`); + return new Blob([blobContent]); } /** @@ -65,6 +70,8 @@ export default class Thread { const importPathRegex = /('|"|`)(.+(\.js|\.ts))(\1)/ig; // for the path string ("lorem/ipsum.js") const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; // for the instruction before the path (import {som} from) const matchedPath = importPathRegex.exec(str) || ""; + this.debug("attempting to import: ", str); + let file = false; let fqfn = ""; @@ -74,6 +81,7 @@ export default class Thread { ) { file = true; fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); + this.debug("file identified as local file"); } const matchedIns = importInsRegex.exec(str) || ""; // matchedIns[0] > import {sss} from @@ -85,18 +93,30 @@ export default class Thread { } if (file) { + this.debug("importing file: ", fqfn); const x = await import(fqfn); //Deno.realPathSync(fqfn) + this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); return Object.keys(x).map((v) => x[v].toString()); } else { const filePath = matchedPath[0].replaceAll(/'|"/g, ""); + this.debug("importing from the net: ", filePath); if (filePath.endsWith(".ts")) { + this.debug("filePath ends with .ts, returning: ", str); return [str]; // dont import the content if ts just paste import string } const x = await import(filePath); + this.debug( + "imported from the net, inlining the following: ", + Object.keys(x).join(","), + ); return Object.keys(x).map((v) => x[v].toString()); } } + private debug(...msg: unknown[]) { + if (this.debugMode) console.log(`[${new Date()}]\t`, ...msg); + } + /** * Sends data to the Thread * @param msg From f5b82c808130fb43fc5ef86ec1a450f2e497dbae Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 11:30:40 +0100 Subject: [PATCH 3/9] attempting to resolve path Signed-off-by: duart38 --- Thread.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Thread.ts b/Thread.ts index 0f2a57b..3f752d7 100644 --- a/Thread.ts +++ b/Thread.ts @@ -93,8 +93,8 @@ onmessage = async function(e) { } if (file) { - this.debug("importing file: ", fqfn); - const x = await import(fqfn); //Deno.realPathSync(fqfn) + this.debug("importing file: ", fqfn, "real path: ", Deno.realPathSync(fqfn)); + const x = await import(Deno.realPathSync(fqfn)); //Deno.realPathSync(fqfn) this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); return Object.keys(x).map((v) => x[v].toString()); } else { From 3d9a5eaaea09e1dd5037742504712ad31f7c11a3 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 11:53:00 +0100 Subject: [PATCH 4/9] . Signed-off-by: duart38 --- Thread.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Thread.ts b/Thread.ts index 3f752d7..5a96ef5 100644 --- a/Thread.ts +++ b/Thread.ts @@ -27,10 +27,13 @@ export default class Thread { imports?: Array, opts: { debug?: boolean } = { debug: false }, ) { + this.debugMode = opts.debug ?? false; this.imports = imports || []; + this.debug("import.meta: ", import.meta); + + // these methods are asynchronous, because we're in the constructor, we must make sure they're at the end this.blob = this.populateFile(operation); this.worker = this.makeWorker(type); - this.debugMode = opts.debug ?? false; } private async makeWorker(type?: "classic" | "module") { @@ -58,7 +61,7 @@ onmessage = async function(e) { postMessage(await userCode(e, global)); } `; - this.debug(`\n\n\nBlob content:\n ${blobContent}\n\n\n`); + this.debug(`Blob content:${blobContent}\n\n\n`); return new Blob([blobContent]); } @@ -114,7 +117,7 @@ onmessage = async function(e) { } private debug(...msg: unknown[]) { - if (this.debugMode) console.log(`[${new Date()}]\t`, ...msg); + if (this.debugMode) console.debug(`[${new Date()}]\t`, ...msg); } /** From a3b8ab8cbed16388fec17f27b7614c62999b06a5 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 12:04:16 +0100 Subject: [PATCH 5/9] . Signed-off-by: duart38 --- Thread.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Thread.ts b/Thread.ts index 5a96ef5..7258f57 100644 --- a/Thread.ts +++ b/Thread.ts @@ -96,8 +96,8 @@ onmessage = async function(e) { } if (file) { - this.debug("importing file: ", fqfn, "real path: ", Deno.realPathSync(fqfn)); - const x = await import(Deno.realPathSync(fqfn)); //Deno.realPathSync(fqfn) + this.debug("importing file: ", fqfn, Deno.realPathSync(fqfn), import.meta.resolve(fqfn)); + const x = await import("file://"+Deno.realPathSync(fqfn)); //Deno.realPathSync(fqfn) this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); return Object.keys(x).map((v) => x[v].toString()); } else { @@ -105,7 +105,7 @@ onmessage = async function(e) { this.debug("importing from the net: ", filePath); if (filePath.endsWith(".ts")) { this.debug("filePath ends with .ts, returning: ", str); - return [str]; // dont import the content if ts just paste import string + return [str]; // do nothing if plain import string } const x = await import(filePath); this.debug( From 3ac998fc4bcb436e09a4f22a7411b8a3cdcedd90 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 12:15:02 +0100 Subject: [PATCH 6/9] fix Signed-off-by: duart38 --- Thread.bundle.js | 51 ++++++++++++++++++++++++++++++++++-------------- Thread.ts | 12 +++++++++--- egg.json | 2 +- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Thread.bundle.js b/Thread.bundle.js index e41558d..31767fc 100644 --- a/Thread.bundle.js +++ b/Thread.bundle.js @@ -2,14 +2,23 @@ // deno-lint-ignore-file // This code was bundled using `deno bundle` and it's not recommended to edit it manually +const importMeta = { + url: "file:///Users/duartsnel/local/personal_projects/Thread/Thread.ts", + main: import.meta.main +}; class Thread { worker; imports; blob; blobURL = ""; + debugMode; stopped = false; - constructor(operation, type, imports){ + constructor(operation, type, imports, opts = { + debug: false + }){ + this.debugMode = opts.debug ?? false; this.imports = imports || []; + this.debug("import.meta: ", importMeta); this.blob = this.populateFile(operation); this.worker = this.makeWorker(type); } @@ -21,48 +30,59 @@ class Thread { } async populateFile(code) { const imported = this.imports?.flatMap(async (val)=>(await this.copyDep(val)).join("\n")); + const blobContent = ` +${(await Promise.all(imported)).join("\n")} + +var global = {}; +var userCode = ${code.toString()} + +onmessage = async function(e) { + postMessage(await userCode(e, global)); +} +`; + this.debug(`Blob content:${blobContent}\n\n\n`); return new Blob([ - ` - ${(await Promise.all(imported)).join("\n")} - - var global = {}; - var userCode = ${code.toString()} - - onmessage = async function(e) { - postMessage(await userCode(e, global)); - } - - ` + blobContent ]); } async copyDep(str) { const importPathRegex = /('|"|`)(.+(\.js|\.ts))(\1)/ig; const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; const matchedPath = importPathRegex.exec(str) || ""; + this.debug("attempting to import: ", str); let file = false; let fqfn = ""; if (!matchedPath[0].includes("http://") && !matchedPath[0].includes("https://")) { file = true; fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); + this.debug("file identified as local file"); } const matchedIns = importInsRegex.exec(str) || ""; if (!matchedIns) { throw new Error("The import instruction seems to be unreadable try formatting it, for example: \n" + "import { something } from './somet.js' \n "); } if (file) { - const x = await import(fqfn); + this.debug("importing file: ", importMeta.resolve("file://" + Deno.realPathSync(fqfn))); + const x = await import("file://" + Deno.realPathSync(fqfn)); + this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); return Object.keys(x).map((v)=>x[v].toString()); } else { const filePath = matchedPath[0].replaceAll(/'|"/g, ""); + this.debug("importing from the net: ", filePath); if (filePath.endsWith(".ts")) { + this.debug("filePath ends with .ts, returning: ", str); return [ str ]; } - const x1 = await import(filePath); - return Object.keys(x1).map((v)=>x1[v].toString()); + const x = await import(filePath); + this.debug("imported from the net, inlining the following: ", Object.keys(x).join(",")); + return Object.keys(x).map((v)=>x[v].toString()); } } + debug(...msg) { + if (this.debugMode) console.debug(`[${new Date()}]\t`, ...msg); + } postMessage(msg) { this.worker.then((w)=>w.postMessage(msg)); return this; @@ -81,3 +101,4 @@ class Thread { } } export { Thread as default }; + diff --git a/Thread.ts b/Thread.ts index 7258f57..c680c01 100644 --- a/Thread.ts +++ b/Thread.ts @@ -96,9 +96,15 @@ onmessage = async function(e) { } if (file) { - this.debug("importing file: ", fqfn, Deno.realPathSync(fqfn), import.meta.resolve(fqfn)); - const x = await import("file://"+Deno.realPathSync(fqfn)); //Deno.realPathSync(fqfn) - this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); + this.debug( + "importing file: ", + import.meta.resolve("file://" + Deno.realPathSync(fqfn)), + ); + const x = await import("file://" + Deno.realPathSync(fqfn)); + this.debug( + "file imported, inlining the following: ", + Object.keys(x).join(","), + ); return Object.keys(x).map((v) => x[v].toString()); } else { const filePath = matchedPath[0].replaceAll(/'|"/g, ""); diff --git a/egg.json b/egg.json index 2263ad4..ec152ea 100644 --- a/egg.json +++ b/egg.json @@ -4,7 +4,7 @@ "entry": "./Thread.ts", "description": "Type-safe multi-threading made 'easier'", "homepage": "https://github.com/duart38/Thread", - "version": "4.1.0", + "version": "4.2.0", "releaseType": null, "unstable": false, "unlisted": false, From 6900f62d924e450d5eb9a1338feb8aab941644be Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 12:19:49 +0100 Subject: [PATCH 7/9] attempt to remove deno dep Signed-off-by: duart38 --- Thread.bundle.js | 9 ++------- Thread.ts | 5 ++--- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Thread.bundle.js b/Thread.bundle.js index 31767fc..9a6dcda 100644 --- a/Thread.bundle.js +++ b/Thread.bundle.js @@ -2,10 +2,6 @@ // deno-lint-ignore-file // This code was bundled using `deno bundle` and it's not recommended to edit it manually -const importMeta = { - url: "file:///Users/duartsnel/local/personal_projects/Thread/Thread.ts", - main: import.meta.main -}; class Thread { worker; imports; @@ -18,7 +14,6 @@ class Thread { }){ this.debugMode = opts.debug ?? false; this.imports = imports || []; - this.debug("import.meta: ", importMeta); this.blob = this.populateFile(operation); this.worker = this.makeWorker(type); } @@ -62,8 +57,8 @@ onmessage = async function(e) { throw new Error("The import instruction seems to be unreadable try formatting it, for example: \n" + "import { something } from './somet.js' \n "); } if (file) { - this.debug("importing file: ", importMeta.resolve("file://" + Deno.realPathSync(fqfn))); - const x = await import("file://" + Deno.realPathSync(fqfn)); + this.debug("importing file: ", "file://" + fqfn); + const x = await import("file://" + fqfn); this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); return Object.keys(x).map((v)=>x[v].toString()); } else { diff --git a/Thread.ts b/Thread.ts index c680c01..ce16579 100644 --- a/Thread.ts +++ b/Thread.ts @@ -29,7 +29,6 @@ export default class Thread { ) { this.debugMode = opts.debug ?? false; this.imports = imports || []; - this.debug("import.meta: ", import.meta); // these methods are asynchronous, because we're in the constructor, we must make sure they're at the end this.blob = this.populateFile(operation); @@ -98,9 +97,9 @@ onmessage = async function(e) { if (file) { this.debug( "importing file: ", - import.meta.resolve("file://" + Deno.realPathSync(fqfn)), + "file://" + fqfn ); - const x = await import("file://" + Deno.realPathSync(fqfn)); + const x = await import("file://" + fqfn); this.debug( "file imported, inlining the following: ", Object.keys(x).join(","), From a464ab18d5c26690bccc8c475cfc1e90a0cd240e Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 12:27:15 +0100 Subject: [PATCH 8/9] delete bundle (due to deprecation soon) Signed-off-by: duart38 --- Thread.bundle.js | 99 ------------------------------------------------ 1 file changed, 99 deletions(-) delete mode 100644 Thread.bundle.js diff --git a/Thread.bundle.js b/Thread.bundle.js deleted file mode 100644 index 9a6dcda..0000000 --- a/Thread.bundle.js +++ /dev/null @@ -1,99 +0,0 @@ -// deno-fmt-ignore-file -// deno-lint-ignore-file -// This code was bundled using `deno bundle` and it's not recommended to edit it manually - -class Thread { - worker; - imports; - blob; - blobURL = ""; - debugMode; - stopped = false; - constructor(operation, type, imports, opts = { - debug: false - }){ - this.debugMode = opts.debug ?? false; - this.imports = imports || []; - this.blob = this.populateFile(operation); - this.worker = this.makeWorker(type); - } - async makeWorker(type) { - this.blobURL = URL.createObjectURL(await this.blob); - return new Worker(this.blobURL, { - type: type || "module" - }); - } - async populateFile(code) { - const imported = this.imports?.flatMap(async (val)=>(await this.copyDep(val)).join("\n")); - const blobContent = ` -${(await Promise.all(imported)).join("\n")} - -var global = {}; -var userCode = ${code.toString()} - -onmessage = async function(e) { - postMessage(await userCode(e, global)); -} -`; - this.debug(`Blob content:${blobContent}\n\n\n`); - return new Blob([ - blobContent - ]); - } - async copyDep(str) { - const importPathRegex = /('|"|`)(.+(\.js|\.ts))(\1)/ig; - const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; - const matchedPath = importPathRegex.exec(str) || ""; - this.debug("attempting to import: ", str); - let file = false; - let fqfn = ""; - if (!matchedPath[0].includes("http://") && !matchedPath[0].includes("https://")) { - file = true; - fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); - this.debug("file identified as local file"); - } - const matchedIns = importInsRegex.exec(str) || ""; - if (!matchedIns) { - throw new Error("The import instruction seems to be unreadable try formatting it, for example: \n" + "import { something } from './somet.js' \n "); - } - if (file) { - this.debug("importing file: ", "file://" + fqfn); - const x = await import("file://" + fqfn); - this.debug("file imported, inlining the following: ", Object.keys(x).join(",")); - return Object.keys(x).map((v)=>x[v].toString()); - } else { - const filePath = matchedPath[0].replaceAll(/'|"/g, ""); - this.debug("importing from the net: ", filePath); - if (filePath.endsWith(".ts")) { - this.debug("filePath ends with .ts, returning: ", str); - return [ - str - ]; - } - const x = await import(filePath); - this.debug("imported from the net, inlining the following: ", Object.keys(x).join(",")); - return Object.keys(x).map((v)=>x[v].toString()); - } - } - debug(...msg) { - if (this.debugMode) console.debug(`[${new Date()}]\t`, ...msg); - } - postMessage(msg) { - this.worker.then((w)=>w.postMessage(msg)); - return this; - } - async stop() { - this.stopped = true; - (await this.worker).terminate(); - } - async remove() { - if (this.stopped == false) await this.stop(); - URL.revokeObjectURL(this.blobURL); - } - onMessage(callback) { - this.worker.then((w)=>w.onmessage = (e)=>callback(e.data)); - return this; - } -} -export { Thread as default }; - From 1184fa12143f30579d2ce595db74fb9ec178929f Mon Sep 17 00:00:00 2001 From: duart38 Date: Sat, 2 Mar 2024 12:27:34 +0100 Subject: [PATCH 9/9] revert removal of deno dep Signed-off-by: duart38 --- Thread.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Thread.ts b/Thread.ts index ce16579..e9e86c4 100644 --- a/Thread.ts +++ b/Thread.ts @@ -97,9 +97,9 @@ onmessage = async function(e) { if (file) { this.debug( "importing file: ", - "file://" + fqfn + import.meta.resolve("file://" + Deno.realPathSync(fqfn)), ); - const x = await import("file://" + fqfn); + const x = await import("file://" + Deno.realPathSync(fqfn)); this.debug( "file imported, inlining the following: ", Object.keys(x).join(","),