[Scripts > InjectPoly] Rename dir, rm node_modules each time prior

This commit is contained in:
Ducko 2022-02-15 12:48:27 +00:00
parent 9e5fb88397
commit 615d7b8838
5 changed files with 2 additions and 1 deletions

2
poly/mime-types.js Normal file
View file

@ -0,0 +1,2 @@
// Stub
exports.lookup = (file) => 'text/plain';

14
poly/mkdirp.js Normal file
View file

@ -0,0 +1,14 @@
// Minimal wrapper mimicking mkdirp package
const fs = require('fs');
const mk = (path, callback) => { // async
fs.mkdir(path, { recursive: true }, () => callback()); // Never error
};
mk.sync = (path) => { // sync
try {
fs.mkdirSync(path, { recursive: true });
} catch (e) { } // Never error
};
module.exports = mk;

88
poly/request.js Normal file
View file

@ -0,0 +1,88 @@
const https = require('https');
const querystring = require("querystring");
// Generic polyfill for "request" npm package, wrapper for https
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve) => {
const fullUrl = `${url}${qs != null ? `?${querystring.stringify(qs)}` : ''}`; // With query string
let req;
try {
req = https.request(fullUrl, { method, headers, timeout }, async (res) => {
if (res.statusCode === 301 || res.statusCode === 302) return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream }));
resolve(res);
});
} catch (e) {
return resolve(e);
}
req.on('error', resolve);
if (body) req.write(body); // Write POST body if included
req.end();
});
const request = (...args) => {
let options, callback;
switch (args.length) {
case 3: // request(url, options, callback)
options = {
url: args[0],
...args[1]
};
callback = args[2];
break;
default: // request(url, callback) / request(options, callback)
options = args[0];
callback = args[1];
}
if (typeof options === 'string') {
options = {
url: options
};
}
const listeners = {};
nodeReq(options).then(async (res) => {
if (!res.statusCode) {
listeners['error']?.(res);
callback?.(res, null, null);
return;
}
listeners['response']?.(res);
let data = [];
res.on('data', (chunk) => {
data.push(chunk);
listeners['data']?.(chunk);
});
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
const buf = Buffer.concat(data);
callback?.(undefined, res, options.encoding !== null ? buf.toString() : buf);
});
const ret = {
on: (type, handler) => {
listeners[type] = handler;
return ret; // Return self
}
};
return ret;
};
for (const m of [ 'get', 'post', 'put', 'patch', 'delete', 'head', 'options' ]) {
request[m] = (url, callback) => request({ url, method: m }, callback);
}
request.del = request.delete; // Special case
module.exports = request;

45
poly/yauzl.js Normal file
View file

@ -0,0 +1,45 @@
// Jank replacement for yauzl by just using unzip where it expects and skipping (speed++, size--, jank++)
const { execFile } = require('child_process');
const { mkdirSync } = require('fs');
const { join, basename } = require('path');
exports.open = async (zipPath, _options, callback) => {
const extractPath = join(global.moduleDataPath, basename(zipPath).split('.')[0].split('-')[0]);
const listeners = [];
log('Yauzl', 'Zip path:', zipPath, 'Extract path:', extractPath);
const errorOut = (err) => {
listeners.error(err);
};
callback(null, {
on: (event, listener) => {
listeners[event] = listener;
},
readEntry: () => {}, // Stubs as not used
openReadStream: () => {},
close: () => {}
});
mkdirSync(extractPath, { recursive: true });
const proc = execFile('unzip', ['-o', zipPath.replaceAll('"', ''), '-d', extractPath]);
log('Yauzl', 'Spawned');
proc.stderr.on('data', errorOut);
proc.on('error', (err) => {
if (err.code === 'ENOENT') {
require('electron').dialog.showErrorBox('Failed Dependency', 'Please install "unzip", exiting');
process.exit(1); // Close now
}
errorOut(err);
});
proc.on('close', async () => {
log('Yauzl', 'Closed');
listeners.end();
});
};