From 152728b05b6c18656011e9be55393751ded44022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nikoli=C4=87?= Date: Wed, 20 Nov 2024 09:51:30 +0100 Subject: [PATCH] Proxy `process` with additional exports Closes #33. --- README.md | 2 +- index.js | 2 +- proxy/process.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++ rollup.config.js | 4 ++ test/index.js | 4 +- types/lib.d.ts | 9 ++++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 proxy/process.js diff --git a/README.md b/README.md index dfdaada..6be0ccd 100644 --- a/README.md +++ b/README.md @@ -271,7 +271,7 @@ const b = browserify( | `net` | | [net](mock/net.js) | | `os` | [os-browserify](https://github.com/CoderPuppy/os-browserify) | | | `path` | [path-browserify](https://github.com/browserify/path-browserify) | | -| `process` | [process](https://github.com/defunctzombie/node-process) | [process](mock/process.js) | +| `process` | [process](https://github.com/defunctzombie/node-process) | [process](mock/process.js) | Contains additional exports from newer Node | | `punycode` | [punycode](https://github.com/bestiejs/punycode.js) | | `punycode@1` for browser support | | `querystring` | [querystring-es3](https://github.com/mike-spainhower/querystring) | | Contains additional exports from newer Node versions | | `readline` | | | diff --git a/index.js b/index.js index bae32d3..1e62454 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ const net = resolvePath('./mock/empty.js'); const os = resolvePath('os-browserify/browser.js'); const path = resolvePath('path-browserify'); const punycode = resolvePath('punycode/'); -const _process = resolvePath('process/browser.js'); +const _process = resolvePath('./proxy/querystring.js'); const querystring = resolvePath('./proxy/querystring.js'); const readline = resolvePath('./mock/empty.js'); const repl = resolvePath('./mock/empty.js'); diff --git a/proxy/process.js b/proxy/process.js new file mode 100644 index 0000000..7d2a6c2 --- /dev/null +++ b/proxy/process.js @@ -0,0 +1,114 @@ +import _process from 'process/browser.js'; + +function noop() {} + +const nextTick = _process.nextTick; +const title = _process.title; +const browser = _process.browser; +const environment = _process.env; +const argv = _process.argv; +const version = _process.version; +const versions = _process.versions; +const on = _process.on; +const addListener = _process.addListener; +const once = _process.once; +const off = _process.off; +const removeListener = _process.removeListener; +const removeAllListeners = _process.removeAllListeners; +const emit = _process.emit; +const emitWarning = noop; +const prependListener = _process.prependListener; +const prependOnceListener = _process.prependOnceListener; +const listeners = _process.listeners; +const binding = _process.binding; +const cwd = _process.cwd; +const chdir = _process.chdir; +const umask = _process.umask; +const exit = noop; +const pid = 1; +const features = {}; +const kill = noop; +const dlopen = noop; +const uptime = noop; +const memoryUsage = noop; +const uvCounters = noop; +const platform = 'browser'; +const arch = 'browser'; +const execPath = 'browser'; +const execArgv = /** @type {string[]} */ ([]); + +const api = { + nextTick, + title, + browser, + env: environment, + argv, + version, + versions, + on, + addListener, + once, + off, + removeListener, + removeAllListeners, + emit, + emitWarning, + prependListener, + prependOnceListener, + listeners, + binding, + cwd, + chdir, + umask, + exit, + pid, + features, + kill, + dlopen, + uptime, + memoryUsage, + uvCounters, + platform, + arch, + execPath, + execArgv +}; + +export default api; + +export { + nextTick, + title, + browser, + environment as env, + argv, + version, + versions, + on, + addListener, + once, + off, + removeListener, + removeAllListeners, + emit, + emitWarning, + prependListener, + prependOnceListener, + listeners, + binding, + cwd, + chdir, + umask, + exit, + pid, + features, + kill, + dlopen, + uptime, + memoryUsage, + uvCounters, + platform, + arch, + execPath, + execArgv +}; diff --git a/rollup.config.js b/rollup.config.js index 87c2e80..2ea4275 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -163,6 +163,10 @@ module.exports = [ [ 'proxy/querystring.js', { cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' } + ], + [ + 'proxy/process.js', + { cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' } ] ].map((entry) => { const [filename, options = {}] = [].concat(entry); diff --git a/test/index.js b/test/index.js index 9aa8db5..ad55b83 100644 --- a/test/index.js +++ b/test/index.js @@ -37,7 +37,7 @@ const packages = { net: 'mock/empty.js', os: 'node_modules/os-browserify', path: 'node_modules/path-browserify', - process: 'node_modules/process', + process: 'proxy/process.js', punycode: 'node_modules/punycode', querystring: 'proxy/querystring.js', readline: 'mock/empty.js', @@ -415,6 +415,8 @@ describe('`querystring` additional exports', function () { }); }); +describe('`process` additional exports', function () {}); + const nodeVersion = parseNodeVersion(process.version); const shouldBundle = nodeVersion.major >= 12; const shouldBundleESM = nodeVersion.major >= 16; diff --git a/types/lib.d.ts b/types/lib.d.ts index 56757e7..8178dc7 100644 --- a/types/lib.d.ts +++ b/types/lib.d.ts @@ -2,3 +2,12 @@ declare module 'querystring-es3' { import { decode, encode, parse, stringify } from 'querystring'; export { decode, encode, parse, stringify }; }; + +declare module 'process/browser.js' { + interface BrowserProcess extends NodeJS.Process { + browser: string + binding: Function + } + const process: BrowserProcess; + export = process; +};