Created
November 23, 2019 07:57
-
-
Save koush/5dca753a2e492f915bca52c703aa178b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as std from 'std'; | |
import * as os from 'os'; | |
function getCallingScript(depth) { | |
depth = depth || 0; | |
try { | |
throw new Error(); | |
} | |
catch (e) { | |
try { | |
let line = e.stack.split('\n')[depth + 1]; | |
var regex = /\((.*?)\)/g; | |
var result = regex.exec(line)[1].split(':', 2); | |
return result[0]; | |
} | |
catch (e) { | |
return undefined; | |
} | |
} | |
} | |
function readFile(filename) { | |
const stat = os.stat(filename); | |
if (stat[1] !== 0) | |
return undefined; | |
if (stat[0].mode & os.S_IFDIR) | |
return undefined; | |
const ab = new ArrayBuffer(stat[0].size); | |
const file = std.open(filename, 'r'); | |
file.read(ab, 0, ab.byteLength); | |
file.close(); | |
return String.fromCharCode.apply(null, new Uint8Array(ab)); | |
} | |
function readJsonFile(filename) { | |
const string = readFile(filename); | |
if (string === undefined) | |
return undefined; | |
return JSON.parse(string); | |
} | |
function requireLoadInternal(scriptString, exports, __require, module, filename, dirname) { | |
if (scriptString === undefined) | |
return undefined; | |
if (filename.endsWith('.json')) | |
return { exports: JSON.parse(scriptString), }; | |
const wrapped = `(function(exports, __require, module, __filename, __dirname){${scriptString}})`; | |
const moduleFunc = std.evalScript(wrapped, filename); | |
moduleFunc(exports, __require, module, filename, dirname); | |
return module; | |
} | |
function requireLoad(scriptString, filename, module) { | |
return requireLoadInternal(scriptString, module.exports, null, module, filename, path.dirname(filename)); | |
} | |
const builtins = { | |
inherits: { | |
name: 'inherits', | |
main: './inherits_browser.js', | |
}, | |
zlib: 'browserify-zlib', | |
assert: 'assert', | |
url: 'url', | |
'builtin-status-codes': { | |
name: 'builtin-status-codes', | |
main: './browser.js', | |
}, | |
process: { | |
name: 'process', | |
main: './browser.js', | |
}, | |
os: "os-browserify", | |
path: "path-browserify", | |
buffer: "buffer", | |
https: "https-browserify", | |
stream: "stream-browserify", | |
http: "stream-http", | |
events: 'events', | |
util: 'util', | |
}; | |
let rootRequire = undefined; | |
function requireLoadSingleFile(fullname) { | |
const found = requireCache[fullname]; | |
if (found !== undefined) | |
return found; | |
const fileString = readFile(fullname) | |
if (fileString === undefined) | |
return undefined; | |
const ret = requireLoad(fileString, fullname, createModule(fullname)); | |
return ret; | |
} | |
function appendScriptExtension(file) { | |
return `${file}.js`; | |
} | |
function requireLoadFile(fullname) { | |
let ret = requireLoadSingleFile(appendScriptExtension(fullname)); | |
if (ret) | |
return ret; | |
return requireLoadSingleFile(fullname); | |
} | |
function createModule(fullname) { | |
const module = { | |
exports: {} | |
}; | |
requireCache[fullname] = module; | |
return module; | |
} | |
function requireLoadPackage(fullpath, main) { | |
const found = requireCache[fullpath]; | |
if (found !== undefined) | |
return found; | |
const packageJson = readJsonFile(path.join(fullpath, 'package.json')); | |
if (!packageJson) | |
return undefined; | |
main = path.join(fullpath, main || packageJson.main || 'index.js'); | |
let fileString = readFile(main); | |
if (fileString === undefined) | |
fileString = readFile(appendScriptExtension(main)); | |
const ret = requireLoad(fileString, main, createModule(fullpath)); | |
return ret; | |
} | |
function requireFind(name, directory, isPath) { | |
let fullname = path.join(directory, name); | |
let ret = requireLoadFile(fullname); | |
if (ret || isPath) | |
return ret; | |
let fullpath = path.join(directory, 'node_modules', name); | |
ret = requireLoadPackage(fullpath); | |
if (ret) | |
return ret; | |
if (isPath || directory == rootRequire) | |
throw new Error(`unable to load ${name}`); | |
const parent = path.dirname(directory); | |
return requireFind(name, parent); | |
} | |
function requireBuiltin(moduleName) { | |
const modulePath = path.join(requireDirectory, 'node_modules', builtins[moduleName].name || builtins[moduleName]); | |
var ret = requireLoadPackage(modulePath, builtins[moduleName].main); | |
requireCache[moduleName] = ret; | |
return ret; | |
} | |
export function require(moduleName) { | |
const callingScript = getCallingScript(1); | |
const currentPath = path.dirname(callingScript); | |
const isPath = moduleName.startsWith('/') || moduleName.startsWith('.') || moduleName.startsWith('\\'); | |
let unsetRootRequire = !rootRequire; | |
if (unsetRootRequire) | |
rootRequire = currentPath; | |
try { | |
// require cache can be set externally, so do a straight name check here. | |
if (requireCache[moduleName] !== undefined) | |
return requireCache[moduleName].exports; | |
if (builtins[moduleName]) | |
return requireBuiltin(moduleName).exports; | |
return requireFind(moduleName, currentPath, isPath).exports; | |
} | |
finally { | |
if (unsetRootRequire) | |
rootRequire = undefined; | |
} | |
} | |
const requireCache = {}; | |
let requireDirectory; | |
function loadPath() { | |
const requirePath = getCallingScript(); | |
const parts = requirePath.split('/'); | |
parts.splice(parts.length - 1, 1); | |
requireDirectory = os.realpath(parts.join('/') || '.')[0]; | |
const pathDir = `${requireDirectory}/node_modules/path-browserify`; | |
const pathPath = `${pathDir}/index.js`; | |
const pathScript = readFile(pathPath); | |
const module = createModule(pathPath); | |
const ret = requireLoadInternal(pathScript, module.exports, null, module, pathPath, pathDir); | |
console.log(pathPath) | |
if (ret === undefined) | |
throw new Error('unable to load path module'); | |
requireCache[`${requireDirectory}/node_modules/path-browserify`] = ret; | |
requireCache['path'] = ret; | |
return ret.exports; | |
} | |
const path = loadPath(); | |
// const requireDirectory = path.dirname(getCallingScript()); | |
console.log(requireDirectory); | |
require.cache = requireCache; | |
globalThis.require = require; | |
globalThis.global = globalThis; | |
const process = require('process'); | |
// for debug module | |
process.type = 'renderer'; | |
global.process = process; | |
global.Buffer = require('buffer').Buffer; | |
const oldToString = Object.prototype.toString; | |
Object.prototype.toString = function() { | |
if (this === process) | |
return '[object process]'; | |
return oldToString.apply(this); | |
} | |
global.location = { | |
protocol: 'https:' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment