Skip to content

Commit

Permalink
Proxy process with additional exports
Browse files Browse the repository at this point in the history
Closes #33, #34.
  • Loading branch information
niksy committed Nov 21, 2024
1 parent 4032e50 commit d8c0a0e
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Pin `domain-browser` to 4.22.0
- Add `require` check for path resolver
- Proxy `process` with additional exports
([#34](https://github.com/niksy/node-stdlib-browser/pull/34))

## [1.2.1][] - 2024-09-16

Expand All @@ -19,7 +21,7 @@
### Added

- Support for `node:` protocol in Webpack
([#12](https://github.com/niksy/node-stdlib-browser/))
([#12](https://github.com/niksy/node-stdlib-browser/issues/12))
- Rollup warning helper function

## [1.1.0][] - 2021-11-16
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | | |
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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/process').replace('.js', '');
const querystring = resolvePath('./proxy/querystring.js');
const readline = resolvePath('./mock/empty.js');
const repl = resolvePath('./mock/empty.js');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"README.md"
],
"scripts": {
"build": "rollup --config rollup.config.js && babel helpers/esbuild/shim.src.js --out-file=helpers/esbuild/shim.js",
"build": "del '{esm/,cjs/}' && rollup --config rollup.config.js && babel helpers/esbuild/shim.src.js --out-file=helpers/esbuild/shim.js",
"lint": "eslint '{index,lib/**/*,test/**/*,helpers/**/*,example/**/*}.{js,mjs}'",
"lint:types": "tsc",
"module-check": "node -e 'require(\"node-stdlib-browser\"); require(\"node-stdlib-browser/helpers/esbuild/plugin\");' && node --input-type=module -e 'import \"node-stdlib-browser\"; import \"node-stdlib-browser/helpers/esbuild/plugin\";'",
Expand Down Expand Up @@ -94,6 +94,7 @@
"core-js": "^2.6.5",
"cpy": "^8.1.2",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"esbuild": "^0.13.14",
"eslint": "^7.31.0",
"eslint-config-niksy": "^10.0.0",
Expand Down
119 changes: 119 additions & 0 deletions proxy/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {
nextTick,
title,
env as environment,
argv,
version,
versions,
on,
addListener,
once,
off,
removeListener,
removeAllListeners,
emit,
prependListener,
prependOnceListener,
listeners,
cwd,
chdir,
umask,
// @ts-ignore
browser as _browser,
// @ts-ignore
binding as _binding
} from 'process/browser.js';

function noop() {}

const browser = /** @type {boolean} */ (_browser);
const emitWarning = noop;
const binding = /** @type {Function} */ (_binding);
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
};
5 changes: 5 additions & 0 deletions proxy/process/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import api from '../process.js';

export default api;

export * from '../process.js';
11 changes: 11 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function getConfig(filename, options = {}) {
if (source === 'url') {
return require.resolve('url/');
}
if (source === 'process/browser.js') {
return require.resolve('process/browser.js');
}
if (
source === 'path' &&
importer.includes('proxy/url.js')
Expand Down Expand Up @@ -163,6 +166,14 @@ module.exports = [
[
'proxy/querystring.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
],
[
'proxy/process.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
],
[
'proxy/process/browser.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
]
].map((entry) => {
const [filename, options = {}] = [].concat(entry);
Expand Down
14 changes: 13 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import parseNodeVersion from 'parse-node-version';
import api from '../index';
import url from '../proxy/url';
import qs from '../proxy/querystring';
import _process from '../proxy/process';

/** @typedef {import('../index').PackageNames} PackageNames */

Expand Down Expand Up @@ -37,7 +38,7 @@ const packages = {
net: 'mock/empty.js',
os: 'node_modules/os-browserify',
path: 'node_modules/path-browserify',
process: 'node_modules/process',
process: 'proxy/process',
punycode: 'node_modules/punycode',
querystring: 'proxy/querystring.js',
readline: 'mock/empty.js',
Expand Down Expand Up @@ -415,6 +416,17 @@ describe('`querystring` additional exports', function () {
});
});

describe('`process` additional exports', function () {
it('has exports for browser environment', function () {
assert.equal(_process.title, 'browser');
assert.equal(_process.browser, true);
assert.equal(_process.arch, 'browser');
assert.equal(_process.platform, 'browser');
assert.ok(Array.isArray(_process.execArgv));
assert.ok(typeof _process.emitWarning !== 'undefined');
});
});

const nodeVersion = parseNodeVersion(process.version);
const shouldBundle = nodeVersion.major >= 12;
const shouldBundleESM = nodeVersion.major >= 16;
Expand Down
5 changes: 5 additions & 0 deletions types/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ declare module 'querystring-es3' {
import { decode, encode, parse, stringify } from 'querystring';
export { decode, encode, parse, stringify };
};

declare module 'process/browser.js' {
const process: NodeJS.Process;
export = process;
};

0 comments on commit d8c0a0e

Please sign in to comment.