Giter Club home page Giter Club logo

hazardous's Introduction

Hazardous

This module overloads some functions of the path module of Electron in order to workaround a painful behaviour with the asar files.

The problem concerns the cases where a .asar.unpacked/* file must be passed to an executable somewhere on the filesystem. This executable can not access to the files packed in the .asar archive. In this case, when the .asar archive is created, it's possible to specify directories to keep unpacked, but it's not sufficient.

Example

An example will be, Perl scripts. You cannot run .pl scripts from Electron, but you can spawn perl and pass the .pl script as argument. Imagine that the .pl script is in a node_modules and this one is in the .asar archive. When you spawn perl, you catch an error because perl cannot access to app.asar/node_modules/perl-module/script.pl.

Then you try to package by this way:

asar pack app app.asar --unpack-dir "**/node_modules/perl-module/**"

The result looks good. You can see the app.asar file and the app.asar.unpacked directory with the perl-module and the perl scripts.

But when you try to use your app, you continue to receive an error because perl cannot find script.pl.

What is the real purpose of .asar.unpacked?

It seems that it's only useful with executables. If you have an executable in a node_modules, it makes sense to use the unpack way because the spawn and exec functions of child_process are aware of .asar.unpacked. Then your executable can be used transparently.

Hazardous workaround

The idea is to overload three functions of path (join (), normalize () and resolve ()).

These functions are wrapped by hazardous in order to detect if the location is in .asar.unpacked or not. If it's impossible to guess, it just returns the original responses of the real path functions.

Note that only absolute locations are considered by hazardous. With relative locations it's impossible to know if the user wants the __dirname of the caller function or the current working dir (cwd ()).

How to use

npm i --save hazardous

Just insert (at the beginning of your main script):

'use strict';

require ('hazardous');
const path = require ('path');

const script = path.join (__dirname, 'script.pl');
/* script = /home/foo/bar/app.asar.unpacked/node_modules/perl-module/script.pl */
/* -----------------------------------^                                        */

The path functions must be used only after that hazardous has been loaded.

If you use the previous code without require ('hazardous'), then the script value will be:

/* script = /home/foo/bar/app.asar/node_modules/perl-module/script.pl */
/* ------------------------------^                                    */

hazardous's People

Contributors

skywalker13 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

hazardous's Issues

Error: Can't resolve original-fs

When including this package in my Electron project, I get an error trying to resolve "original-fs" on line 44:

const fs = process.versions.electron
     ? require ('original-fs')
     : require ('fs');

Looking at the package.json, I see that original-fs is not included as a dependency. All this package does is wrap a require("fs") in a module.exports, which seems unnecessary. I confirmed by modifying these lines to the following and it worked as expected.

const fs = require ('fs');

Fatal exception on Electron package for Windows: stack(...).some is not a function

Hello @Skywalker13 , thanks for maintening this package.

I'd like to report an error with the dependency callsite used.
When installed and launched on some computers, Win10 and Win7 as well, hazardous is not working properly and throwing a fatal exception that crashes my electron app.

Here is the stacktrace it logs:

C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\hazardous\lib\index.js:69
      .some ((site) => {
       ^

TypeError: stack(...).some is not a function
    at hazardous (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\hazardous\lib\index.js:69:8)
    at Object.hazarPath.normalize (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\hazardous\lib\index.js:87:10)
    at splitPath (ELECTRON_ASAR.js:58:14)
    at Object.fs.existsSync (ELECTRON_ASAR.js:377:44)
    at Array.<anonymous> (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:81:17)
    at C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:53:24
    at retrieveSourceMapURL (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:120:14)
    at Array.<anonymous> (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:137:26)
    at C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:53:24
    at mapSourcePosition (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:167:21)
    at wrapCallSite (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:338:20)
    at C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:373:26
    at Array.map (native)
    at Function.prepareStackTrace (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:372:24)
    at process.emit (C:\Users\Admin\AppData\Local\Programs\myelectronapp\resources\app.asar\node_modules\source-map-support\source-map-support.js:422:51)
    at process._fatalException (bootstrap_node.js:308:26)

I presume that stack () is returning null or something like that, and I believe it should either be handled, or patched in the original package (I'll try to see with them as well).

Has anybody faced the same problem and uncovered the reason why callsite does not return an array?

Why skip monkey patching when electron method in stack?

There is a comment as below that skips monkey patching when an electron method is in the callstack but no reasoning behind this decision.
/* Skip monkey patching when an electron method is in the callstack. */

Can you explain why you skip patching in this case as it means sqlite3 cannot be used within Electron with asar enabled and sqlite3 unpacked.

Error I am seeing:
{ Error: Cannot find module 'C:\Users\FlyingSquad\AppData\Local\EMRConnect\app-1.4.0\resources\app.asar\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node' at Module._resolveFilename (module.js:455:15) at Function.Module._resolveFilename (C:\Users\FlyingSquad\AppData\Local\EMRConnect\app-1.4.0\resources\electron.asar\common\reset-search-paths.js:35:12) at Function.Module._load (module.js:403:25) at Module.require (module.js:483:17) at require (internal/module.js:20:19) at Object.<anonymous> (C:\Users\FlyingSquad\AppData\Local\EMRConnect\app-1.4.0\resources\app.asar\node_modules\sqlite3\lib\sqlite3.js:6:15) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) code: 'MODULE_NOT_FOUND' }

How to unpack multiple directories using hazardous

I have a requirement in my electron app. I want to unpack multiple directories. Currently I am using this inside package.json

"builder": " xxxx --asar.unpackDir=node_modules/regedit"

But what I want to do here is

"builder": " xxxx --asar.unpackDir=node_modules/regedit , node_modules/@types/express"

Can I do this with hazardous?

I <3 this.

Thank you, just that. Sorry for the spam.

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.