binary files in node version

possible solution to node location problem
This commit is contained in:
Blue 2023-10-02 14:10:00 -03:00
parent d4f8b736ed
commit 7d0c2d079b
Signed by: blue
GPG Key ID: 9B203B252A63EE38
5 changed files with 51 additions and 33 deletions

View File

@ -2,16 +2,14 @@
"version": "0.0.1", "version": "0.0.1",
"name": "mimicry", "name": "mimicry",
"dependencies": [], "dependencies": [],
"targets": ["browser", "node"],
"defaultTarget": "browser",
"build": [ "build": [
{
"files": [
"dependency.js", "dependency.js",
"errors.js", "errors.js",
"loadingFile.js", "loadingFile.js",
"module.js", "module.js",
"queue.js" "queue.js",
]
},
{ {
"targets": ["browser"], "targets": ["browser"],
"files": [ "files": [

41
node.js
View File

@ -20,6 +20,7 @@ class Mimicry {
constructor (config) { constructor (config) {
instances.push(this); instances.push(this);
this._callerPath = path.dirname(_getCallerFile());
this._queue = new Queue(); this._queue = new Queue();
this._loadedFiles = new Set(); this._loadedFiles = new Set();
this._config = Object.create(null); 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 * @param {Function} [callback] - module body that will be executed as soon all dependencies are resolved
*/ */
module (dependencies, callback) { module (dependencies, callback) {
const href = getCurrentHref(); const href = getCurrentHref(this._callerPath);
if (!href.length) if (!href.length)
throw new Error("Couldn't understand module path"); throw new Error("Couldn't understand module path");
@ -335,41 +336,40 @@ function scheduleLoad (path, callback, error, contentType, originalName) {
notifyProgressSubscribers.call(this); notifyProgressSubscribers.call(this);
} }
function executeLoad (/*String*/path, /*LoadingFile*/loadingFile) { function executeLoad (/*String*/relativePath, /*LoadingFile*/loadingFile) {
this._loadingFiles.set(path, loadingFile); this._loadingFiles.set(relativePath, loadingFile);
const boundLoad = onLoaded.bind(this, path);
const boundError = onError.bind(this, path);
switch (loadingFile.contentType) { switch (loadingFile.contentType) {
case Type.js: case Type.js:
try { try {
require("./" + path); require(path.join(this._callerPath, relativePath));
this._loadedFiles.add(path); this._loadedFiles.add(relativePath);
setTimeout(boundLoad, 0); setTimeout(onLoaded.bind(this, relativePath), 0);
} catch (e) { } catch (e) {
this._errors.set(path, e); this._errors.set(relativePath, e);
setTimeout(boundError, 0); setTimeout(onError.bind(this, relativePath), 0);
} }
break; break;
case Type.json: case Type.json:
try { try {
const json = require("./" + path); const json = require(path.join(this._callerPath, relativePath));
this._loadedFiles.add(path); this._loadedFiles.add(relativePath);
setTimeout(onLoaded.bind(this, path, json), 0); setTimeout(onLoaded.bind(this, relativePath, json), 0);
} catch (e) { } catch (e) {
this._errors.set(path, e); this._errors.set(relativePath, e);
setTimeout(boundError, 0); setTimeout(onError.bind(this, relativePath), 0);
} }
break; break;
case Type.css: 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); //getStylesheetWithTag(path, boundLoad, boundError, this._config.seed);
break; break;
case Type.binary: case Type.binary:
fs.readFile(path.join(this._callerPath, relativePath), onFileRead.bind(this, relativePath));
//getByXHR(path, boundLoad, boundError, "arraybuffer", this._config.seed); //getByXHR(path, boundLoad, boundError, "arraybuffer", this._config.seed);
break; break;
case Type.text: 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); //getByXHR(path, boundLoad, boundError, "text", this._config.seed);
break; break;
} }
@ -391,8 +391,9 @@ function onLoaded(/*String*/path, /*any*/value) {
const loading = this._loadingFiles.get(path); const loading = this._loadingFiles.get(path);
this._loadedFiles.add(path); this._loadedFiles.add(path);
switch (loading.contentType) { switch (loading.contentType) {
case Type.json:
case Type.binary: case Type.binary:
value = value.buffer; //[fallthrough]
case Type.json:
case Type.text: case Type.text:
this._loadedAssets.set(loading.originalName, value); this._loadedAssets.set(loading.originalName, value);
break; break;
@ -503,8 +504,8 @@ function checkNextElement () {
} }
} }
function getCurrentHref() { function getCurrentHref(caller = "") {
return path.relative(__dirname, _getCallerFile()); return path.relative(caller || __dirname, _getCallerFile());
} }
function _getCallerFile() { function _getCallerFile() {

BIN
test/binary Normal file

Binary file not shown.

View File

@ -15,7 +15,7 @@ function test () {
let mimicry; let mimicry;
try { try {
mimicry = new Mimicry({ mimicry = new Mimicry({
baseUrl: "test" //baseUrl: "test"
}); });
global.mimicry = mimicry; global.mimicry = mimicry;
global.Mimicry = Mimicry; global.Mimicry = Mimicry;

View File

@ -3,11 +3,30 @@ mimicry.module([
"terminalModule", "terminalModule",
"simple", Mimicry.Type.json, "simple", Mimicry.Type.json,
"background", Mimicry.Type.css, "background", Mimicry.Type.css,
"text", Mimicry.Type.text "text", Mimicry.Type.text,
], function (global, [terminalModule, simple, background, 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 [ return [
terminalModule === "terminal", terminalModule === "terminal",
simple.a[4] === 115, simple.a[4] === 115,
text === "Lorem ipsum, I guess" text === "Lorem ipsum, I guess",
bnr
]; ];
}); });