Skip to content

Commit

Permalink
module loading
Browse files Browse the repository at this point in the history
  • Loading branch information
steam0r committed Nov 8, 2024
1 parent 0cf7922 commit f2a021a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
27 changes: 21 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"nodeVersion": "20.13.1",
"asar": true,
"includeSubNodeModules": true,
"electronLanguages": ["en-US"],
"electronLanguages": [
"en-US"
],
"files": [
"cables.json",
"src/**/*",
Expand Down Expand Up @@ -126,6 +128,7 @@
"jsonfile": "6.1.0",
"marked": "12.0.2",
"md5-file": "5.0.0",
"mime": "^4.0.4",
"mkdirp": "2.1.3",
"moment-mini": "2.29.4",
"npm": "10.5.2",
Expand Down
26 changes: 26 additions & 0 deletions src/electron/electron_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { promisify } from "util";
import jsonfile from "jsonfile";
import sanitizeFileName from "sanitize-filename";
import { utilProvider } from "cables-shared-api";
import { createRequire } from "module";
import cables from "../cables.js";
import logger from "../utils/logger.js";
import doc from "../utils/doc_util.js";
Expand Down Expand Up @@ -73,6 +74,31 @@ class ElectronApi
const opDir = opsUtil.getOpAbsolutePath(opName);
event.returnValue = path.join(opDir, "node_modules", data.moduleName);
});

ipcMain.on("getOpModuleFile", (event, data) =>
{
let opName = opsUtil.getOpNameById(data.opId);
if (!opName) opName = data.opName;
const opDir = opsUtil.getOpAbsolutePath(opName);
const moduleDir = path.join(opDir, "node_modules");
const moduleRequire = createRequire(moduleDir);
if (moduleRequire)
{
try
{
event.returnValue = helper.pathToFileURL(moduleRequire.resolve(data.moduleName));
}
catch (e)
{
this._log.error(e.message);
event.returnValue = null;
}
}
else
{
event.returnValue = null;
}
});
}

async talkerMessage(cmd, data, topicConfig = {})
Expand Down
7 changes: 7 additions & 0 deletions src/electron/electron_endpoint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { protocol, session, net, shell } from "electron";
import fs from "fs";
import path from "path";
import mime from "mime";

import cables from "../cables.js";
import logger from "../utils/logger.js";
Expand Down Expand Up @@ -444,6 +445,12 @@ class ElectronEndpoint
response.headers.append("Content-Range", "bytes 0-" + stats.size + "/" + (stats.size + 1));
response.headers.append("Last-Modified", stats.mtime.toUTCString());
}
let mimeType = mime.getType(existingFile);
if (mimeType)
{
if (mimeType === "application/node") mimeType = "text/javascript";
response.headers.set("Content-Type", mimeType);
}
}
catch (e) {}
return response;
Expand Down
10 changes: 8 additions & 2 deletions src_client/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,15 @@ export default class CablesStandalone
}
catch (e2)
{
const errorMessage = "failed to load node module: " + moduleName;
const errorMessage = "require by name:\n" + e2 + "\n\nrequire by path:\n" + e;
const options = {
"title": "failed to load node module: " + moduleName,
"codeText": errorMessage.replace(/(.{80})/g, "$1\n")
};

new this.CABLES.UI.ModalError(options);
if (op) op.setUiError("oprequire", errorMessage);
this._log.error(errorMessage, e2, e);
this._log.warn(errorMessage, e2, e);
return "";
}
}
Expand Down

0 comments on commit f2a021a

Please sign in to comment.