Skip to content

Commit

Permalink
feat: stub cluster module in bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Nov 9, 2024
1 parent 720dc3c commit a0dcd04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions bundler/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const build = async (...files) => {
buffer: hasBuffer ? api('node-buffer.cjs') : dirname(resolveRequire('buffer/')),
child_process: api('child_process.cjs'),
constants: resolveRequire('constants-browserify'),
cluster: api('cluster.cjs'),
crypto: api('crypto.cjs'),
events: dirname(resolveRequire('events/')),
fs: api('fs.cjs'),
Expand Down
27 changes: 27 additions & 0 deletions bundler/modules/cluster.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const EventEmitter = require('events')

const makeMethod = (key) => {
// Not an arrow as Worker is a class and can be called with new
return function () {
throw new Error(`cluster.${key} unsupported in bundled mode`)
}
}

const cluster = new EventEmitter()

Object.assign(cluster, {
isWorker: false,
isPrimary: true,
workers: {},
settings: {},
SCHED_NONE: 1,
SCHED_RR: 2,
schedulingPolicy: 2,
})

for (const key of ['Worker', 'setupPrimary', 'fork', 'disconnect']) cluster[key] = makeMethod(key)

cluster.isMaster = cluster.isPrimary
cluster.setupMaster = cluster.setupPrimary

module.exports = cluster
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"bundler/modules/ansi-styles.cjs",
"bundler/modules/assert-strict.cjs",
"bundler/modules/child_process.cjs",
"bundler/modules/cluster.cjs",
"bundler/modules/crypto.cjs",
"bundler/modules/fs.cjs",
"bundler/modules/fs-promises.cjs",
Expand Down

0 comments on commit a0dcd04

Please sign in to comment.