Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support automated app.asar detection and require #3

Open
thiscantbeserious opened this issue Apr 23, 2022 · 1 comment
Open

Support automated app.asar detection and require #3

thiscantbeserious opened this issue Apr 23, 2022 · 1 comment

Comments

@thiscantbeserious
Copy link

thiscantbeserious commented Apr 23, 2022

It seems like you added support for automatic search for module inside node_modules.asar via addAsarToLookupPaths as in hooking Module._resolveLookupPaths - however not in app.asar which is the default bundle for electron(-builder) given that might also include node_modules.

In my case I'm calling child_process.fork inside an Electron-App for a background-server that I want to require node_modules and dependencies inside app.asar.

Furthermore I'm unsure if your code even properly works - I had to refactor it quite a bit to make it work.

See below example code - if you want I can refine this into a PR to add a configuration option to search in an array-list of files so that your current addAsarToLookupPaths can be replaced by a more generic utility for all cases.

Do you want a PR? :)

// top of your entryPoint.js or whatever module that you want to fork
const asar = require('asar-node');
const path = require('path');
const Module = require('module');

asar.register();

const baseDir = path.join(__dirname, "../");

const _orgResolveLookupPaths = Module._resolveLookupPaths; 

(function hookModule () {
  if(Module.isHooked) return;

  function appendPaths (request, paths) {
    let _possibleAppendedPaths = [path.join(baseDir, "/app.asar/", "/node_modules/", request)];
    for (let i = 0; i < paths.length; i++) {
      const _path = paths[i];
      const isBaseDir = _path.indexOf(baseDir);
      if(isBaseDir > -1 && _path.indexOf("app.asar") < 0) {
        _possibleAppendedPaths.push(path.join(baseDir, "/app.asar/", _path.substr(isBaseDir+baseDir.length)))
      }
    }
    return paths = paths.concat(_possibleAppendedPaths);
  }
  Module._resolveLookupPaths = function (request, parent) {
    if(parent && parent.paths) {
      parent.paths = appendPaths(request, parent.paths);
    }
    return _orgResolveLookupPaths.call(this, request, parent)
  }
  Module.isHooked = true

})();

//----- your regular dependencies afterwards

const express = require('express'); //will be loaded from ./app.asar/node_modules/express successfully
@toyobayashi
Copy link
Owner

@thiscantbeserious Sorry to reply late. That sounds great! Feel free to open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants