From 6cfa808f7f2d9fadc24798c95e6382d9e131eba5 Mon Sep 17 00:00:00 2001 From: Oj Date: Sat, 11 Dec 2021 00:45:56 +0000 Subject: [PATCH] [Polyfills] Rewrite --- .github/workflows/release_nightly.yml | 18 ++++++------------ injectPolyfills.js | 16 ++++++++++++++++ {src/polyfills => polyfills}/mime-types.js | 0 {src/polyfills => polyfills}/request.js | 6 +++--- src/index.js | 7 ------- test.sh | 1 + 6 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 injectPolyfills.js rename {src/polyfills => polyfills}/mime-types.js (100%) rename {src/polyfills => polyfills}/request.js (88%) diff --git a/.github/workflows/release_nightly.yml b/.github/workflows/release_nightly.yml index 77b9d09..be452e3 100644 --- a/.github/workflows/release_nightly.yml +++ b/.github/workflows/release_nightly.yml @@ -21,18 +21,12 @@ jobs: - name: NPM Install run: cd src; npm i; cd .. - - name: Install Asar - run: npm i -g asar - - - name: Pack Asar - run: npx asar pack src openasar.asar - - - name: Make Deps Asar + - name: Pack run: | - cd src - npm install request - cd .. - npx asar pack src openasar-with-deps.asar + npm i -g asar + node injectPolyfills.js + rm -rf src/package-lock.json src/node_modules/.package-lock.json src/splash/splashScreen.js.self + npx asar pack src openasar.asar - name: GitHub Release uses: ncipollo/release-action@v1 @@ -40,7 +34,7 @@ jobs: tag: "nightly" allowUpdates: true prerelease: true - artifacts: "openasar.asar,openasar-with-deps.asar" + artifacts: "openasar.asar" name: "Nightly" diff --git a/injectPolyfills.js b/injectPolyfills.js new file mode 100644 index 0000000..880d699 --- /dev/null +++ b/injectPolyfills.js @@ -0,0 +1,16 @@ +const { readdirSync, mkdirSync, copyFileSync } = require('fs'); +const { join } = require('path'); + +const polyfillsDir = join(__dirname, 'polyfills'); +for (const file of readdirSync(polyfillsDir)) { + const [ name ] = file.split('.'); + const jsPath = join(polyfillsDir, file); + + const moduleDir = join(__dirname, 'src', 'node_modules', name); + + try { + mkdirSync(moduleDir); + } catch (e) {} + + copyFileSync(jsPath, join(moduleDir, 'index.js')); +} \ No newline at end of file diff --git a/src/polyfills/mime-types.js b/polyfills/mime-types.js similarity index 100% rename from src/polyfills/mime-types.js rename to polyfills/mime-types.js diff --git a/src/polyfills/request.js b/polyfills/request.js similarity index 88% rename from src/polyfills/request.js rename to polyfills/request.js index 8118021..bcfb84e 100644 --- a/src/polyfills/request.js +++ b/polyfills/request.js @@ -8,7 +8,7 @@ const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => { const req = https.request(fullUrl, { method, headers, - timeout: timeout != null ? timeout : DEFAULT_REQUEST_TIMEOUT + timeout }, async (res) => { if (res.statusCode === 301 || res.statusCode === 302) { // Redirect, recall function return resolve(await nodeReq({ @@ -38,13 +38,13 @@ module.exports = (options, callback) => { }; } - log('Polyfill > Request', options.method, options.url); + // log('Polyfill > Request', options.method, options.url); const listener = {}; nodeReq(options).then((res) => { // No error handling because yes if (callback) callback(undefined, res, res.body); - listener['response'](res); + if (listener['response']) listener['response'](res); }); return { diff --git a/src/index.js b/src/index.js index 80a5baf..8e1c7fe 100644 --- a/src/index.js +++ b/src/index.js @@ -4,13 +4,6 @@ global.oaVersion = '0.2'; log('Init', 'OpenAsar v' + oaVersion); -const NodeModule = require('module'); -const { join } = require('path'); - -NodeModule.globalPaths.push(join(__dirname, 'polyfills')); - -log('Polyfills', 'Set up polyfills usage'); - const appSettings = require('./appSettings'); global.oaConfig = appSettings.getSettings().get('openasar', {}); diff --git a/test.sh b/test.sh index 7569c16..cb766e6 100755 --- a/test.sh +++ b/test.sh @@ -1,6 +1,7 @@ #!/bin/sh echo "Packing asar..." +node injectPolyfills.js asar pack src app.asar # Package asar # asar list app.asar # List asar for debugging / testing