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",
"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": [

41
node.js
View File

@ -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() {

BIN
test/binary Normal file

Binary file not shown.

View File

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

View File

@ -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
];
});