From 7d0c2d079b614a60708abb2877e4eb1b39a2f3fc Mon Sep 17 00:00:00 2001 From: blue Date: Mon, 2 Oct 2023 14:10:00 -0300 Subject: [PATCH] binary files in node version possible solution to node location problem --- mason.json | 16 +++++++--------- node.js | 41 +++++++++++++++++++++-------------------- test/binary | Bin 0 -> 32 bytes test/index.js | 2 +- test/module.js | 25 ++++++++++++++++++++++--- 5 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 test/binary diff --git a/mason.json b/mason.json index a2c809a..14b3f70 100644 --- a/mason.json +++ b/mason.json @@ -2,16 +2,14 @@ "version": "0.0.1", "name": "mimicry", "dependencies": [], + "targets": ["browser", "node"], + "defaultTarget": "browser", "build": [ - { - "files": [ - "dependency.js", - "errors.js", - "loadingFile.js", - "module.js", - "queue.js" - ] - }, + "dependency.js", + "errors.js", + "loadingFile.js", + "module.js", + "queue.js", { "targets": ["browser"], "files": [ diff --git a/node.js b/node.js index 81faf1f..04f3304 100644 --- a/node.js +++ b/node.js @@ -20,6 +20,7 @@ class Mimicry { constructor (config) { instances.push(this); + this._callerPath = path.dirname(_getCallerFile()); this._queue = new Queue(); this._loadedFiles = new Set(); this._config = Object.create(null); @@ -109,7 +110,7 @@ class Mimicry { * @param {Function} [callback] - module body that will be executed as soon all dependencies are resolved */ module (dependencies, callback) { - const href = getCurrentHref(); + const href = getCurrentHref(this._callerPath); if (!href.length) throw new Error("Couldn't understand module path"); @@ -335,41 +336,40 @@ function scheduleLoad (path, callback, error, contentType, originalName) { notifyProgressSubscribers.call(this); } -function executeLoad (/*String*/path, /*LoadingFile*/loadingFile) { - this._loadingFiles.set(path, loadingFile); +function executeLoad (/*String*/relativePath, /*LoadingFile*/loadingFile) { + this._loadingFiles.set(relativePath, loadingFile); - const boundLoad = onLoaded.bind(this, path); - const boundError = onError.bind(this, path); switch (loadingFile.contentType) { case Type.js: try { - require("./" + path); - this._loadedFiles.add(path); - setTimeout(boundLoad, 0); + require(path.join(this._callerPath, relativePath)); + this._loadedFiles.add(relativePath); + setTimeout(onLoaded.bind(this, relativePath), 0); } catch (e) { - this._errors.set(path, e); - setTimeout(boundError, 0); + this._errors.set(relativePath, e); + setTimeout(onError.bind(this, relativePath), 0); } break; case Type.json: try { - const json = require("./" + path); - this._loadedFiles.add(path); - setTimeout(onLoaded.bind(this, path, json), 0); + const json = require(path.join(this._callerPath, relativePath)); + this._loadedFiles.add(relativePath); + setTimeout(onLoaded.bind(this, relativePath, json), 0); } catch (e) { - this._errors.set(path, e); - setTimeout(boundError, 0); + this._errors.set(relativePath, e); + setTimeout(onError.bind(this, relativePath), 0); } break; case Type.css: - setTimeout(boundLoad, 0); //todo just for now, don't know what to do yet with css here + setTimeout(onLoaded.bind(this, relativePath), 0); //todo just for now, don't know what to do yet with css here //getStylesheetWithTag(path, boundLoad, boundError, this._config.seed); break; case Type.binary: + fs.readFile(path.join(this._callerPath, relativePath), onFileRead.bind(this, relativePath)); //getByXHR(path, boundLoad, boundError, "arraybuffer", this._config.seed); break; case Type.text: - fs.readFile(path, "utf8", onFileRead.bind(this, path)); + fs.readFile(path.join(this._callerPath, relativePath), "utf8", onFileRead.bind(this, relativePath)); //getByXHR(path, boundLoad, boundError, "text", this._config.seed); break; } @@ -391,8 +391,9 @@ function onLoaded(/*String*/path, /*any*/value) { const loading = this._loadingFiles.get(path); this._loadedFiles.add(path); switch (loading.contentType) { - case Type.json: case Type.binary: + value = value.buffer; //[fallthrough] + case Type.json: case Type.text: this._loadedAssets.set(loading.originalName, value); break; @@ -503,8 +504,8 @@ function checkNextElement () { } } -function getCurrentHref() { - return path.relative(__dirname, _getCallerFile()); +function getCurrentHref(caller = "") { + return path.relative(caller || __dirname, _getCallerFile()); } function _getCallerFile() { diff --git a/test/binary b/test/binary new file mode 100644 index 0000000000000000000000000000000000000000..fde12999e8f4fd12429fb4a25c5ca650f4e10929 GIT binary patch literal 32 ncmZQzWMXDvWn)ju%u6h)R7lRxD@n~O;o#)r=Hcbz7Z3yhXD$Xh literal 0 HcmV?d00001 diff --git a/test/index.js b/test/index.js index 7fc6f75..17ad0f1 100644 --- a/test/index.js +++ b/test/index.js @@ -15,7 +15,7 @@ function test () { let mimicry; try { mimicry = new Mimicry({ - baseUrl: "test" + //baseUrl: "test" }); global.mimicry = mimicry; global.Mimicry = Mimicry; diff --git a/test/module.js b/test/module.js index ec26b75..2e06d95 100644 --- a/test/module.js +++ b/test/module.js @@ -3,11 +3,30 @@ mimicry.module([ "terminalModule", "simple", Mimicry.Type.json, "background", Mimicry.Type.css, - "text", Mimicry.Type.text -], function (global, [terminalModule, simple, background, text]) { + "text", Mimicry.Type.text, + "binary", Mimicry.Type.binary +], function (global, [terminalModule, simple, background, text, binary]) { + let bnr = false; + if (binary instanceof ArrayBuffer) { + const view = new Uint8Array(binary); + if ( + view.length === 32 && + view[0] === 0 && + view[1] === 1 && + view[3] === 3 && + view[31] === 0x11 && + view[30] === 0x10 && + view[10] === 0x6e && + view[12] === 0x72 && + view[21] === 0x74 + ) { + bnr = true; + } + } return [ terminalModule === "terminal", simple.a[4] === 115, - text === "Lorem ipsum, I guess" + text === "Lorem ipsum, I guess", + bnr ]; }); \ No newline at end of file