Coder Social home page Coder Social logo

toyobayashi / asar-node Goto Github PK

View Code? Open in Web Editor NEW
16.0 1.0 6.0 1.81 MB

Enable require('./path/to/any-node-project.asar') & require('./path/to/any-node-project.asar/any/file'). Or just run asar-node ./path/to/any-node-project.asar

License: MIT License

JavaScript 99.42% HTML 0.55% CSS 0.03%

asar-node's Introduction

asar-node

Enable require('./path/to/any-node-project.asar') & require('./path/to/any-node-project.asar/any/file') in your nodejs app.

Usage

CLI

$ npm install -g asar-node
$ asar-node ./path/to/any-node-project
$ asar-node ./path/to/any-node-project.asar

$ asar-node ./path/to/any-node-project.asar/any/file
$ asar-node ./path/to/any-node-project.asar/any/file.js
$ asar-node ./path/to/any-node-project.asar/any/file.json
$ asar-node ./path/to/any-node-project.asar/any/file.node

Programming

$ npm install asar-node
require('asar-node').register()
// Equivalent to require('asar-node/lib/register.js').register()

require('./path/to/any-node-project') // like require a nodejs directory
// or require('./path/to/any-node-project.asar')
require('./path/to/any-node-project.asar/any/file')

If require a asar file, make sure there is package.json and main field or index.js / index.json / index.node in the asar root.

You can also pack node_modules into node_modules.asar instead of packing the hole project folder into an asar file.

To let node find modules from node_modules.asar, You should

const { register, addAsarToLookupPaths } = require('asar-node')
// Equivalent to 
// const register = require('asar-node/lib/register.js').register
// const addAsarToLookupPaths = require('asar-node/lib/lookup.js').addAsarToLookupPaths

register()
addAsarToLookupPaths()

const Koa = require('koa') // koa is in node_modules.asar

In an electron project, it's unnecessary to call register() but you can also call addAsarToLookupPaths() to enable node_modules.asar support.

To disable asar support, you can set process.noAsar = true or ELECTRON_NO_ASAR environmnet variable.

Migration

v1.x

require('asar-node')

v2.x / v3.x

require('asar-node/lib/autorun/index')

Standalone builds

  • node_modules/asar-node/dist/index.js

    Library bundle.

    const {
      register,
      addAsarToLookupPaths
    } = require('asar-node/dist/index.js')
    // Equivalent to require('asar-node')
  • node_modules/asar-node/dist/asar-node.js

    CLI bundle.

    $ node ./node_modules/asar-node/dist/asar-node.js [...]
    # Equivalent to
    $ asar-node [...]
  • node_modules/asar-node/dist/autorun.js

    Preload bundle.

    $ node -r ./node_modules/asar-node/dist/autorun.js [...]
    require('asar-node/dist/autorun.js')
    require('./path/to/app.asar/index.js')
    // app.asar/index.js
    require('mod') // mod could be in node_modules.asar
  • node_modules/asar-node/dist/autorun-register.js

    Preload bundle without node_modules.asar support.

    $ node -r ./node_modules/asar-node/dist/autorun-register.js [...]
    require('asar-node/dist/autorun-register.js')
    require('./path/to/app.asar/index.js')
    // app.asar/index.js
    require('mod') // throws error if mod is in node_modules.asar
  • node_modules/asar-node/dist/autorun-lookup.js

    Preload bundle with node_modules.asar support only, useful for electron environment.

    // webpack electron target
    import 'asar-node/dist/autorun-lookup.js' // 1KB minified

Available APIs inside asar

  • require('original-fs')
  • fs.readFileSync / fs.readFile / fs.promises.readFile
  • fs.statSync / fs.stat / fs.promises.stat
  • fs.lstatSync / fs.lstat / fs.promises.lstat
  • fs.readdirSync / fs.readdir / fs.promises.readdir
  • fs.existsSync / fs.exists
  • fs.accessSync / fs.access / fs.promises.access
  • fs.realpathSync / fs.realpath / fs.realpathSync.native / fs.realpath.native / fs.promises.realpath
  • fs.copyFileSync / fs.copyFile / fs.promises.copyFile
  • fs.openSync / fs.open / fs.promises.open
  • fs.createReadStream
  • child_process.execFile
  • child_process.execFileSync

Note

  • If your nodejs project use C++ native addons, please unpack it from asar file by specifying --unpack=*.node to asar CLI
  • Express or Koa serving static file in asar file is not supported, but you can unpack the static file folder.
  • Node.js ESM module is not support

asar-node's People

Contributors

andywer avatar hrueger avatar toyobayashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

asar-node's Issues

Support automated app.asar detection and require

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

Import support?

Initially dynamic imports should be more than ok. Is there support for this yet? Will it have?

Native imports would be awesome. If not mistaken, esm package can handle things in this sense, so there must be a way to do so.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.