From 7692d2e7b9f3a5f9373e65b982474c87dab0451b Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Fri, 26 May 2023 01:04:19 +0900 Subject: [PATCH] cluster: use ObjectPrototypeHasOwnProperty Signed-off-by: Daeyeon Jeong PR-URL: https://github.com/nodejs/node/pull/48141 Reviewed-By: Rich Trott Reviewed-By: Yagiz Nizipli --- lib/cluster.js | 6 +++++- test/parallel/test-cluster-basic.js | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/cluster.js b/lib/cluster.js index 7ca8b532ee243f..ff410235b7f0fd 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -21,5 +21,9 @@ 'use strict'; -const childOrPrimary = 'NODE_UNIQUE_ID' in process.env ? 'child' : 'primary'; +const { + ObjectPrototypeHasOwnProperty: ObjectHasOwn, +} = primordials; + +const childOrPrimary = ObjectHasOwn(process.env, 'NODE_UNIQUE_ID') ? 'child' : 'primary'; module.exports = require(`internal/cluster/${childOrPrimary}`); diff --git a/test/parallel/test-cluster-basic.js b/test/parallel/test-cluster-basic.js index e53b208ead4963..306b4d7f58788d 100644 --- a/test/parallel/test-cluster-basic.js +++ b/test/parallel/test-cluster-basic.js @@ -22,13 +22,26 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); -const cluster = require('cluster'); +const assert = require('node:assert'); +const cluster = require('node:cluster'); +const { spawnSync } = require('node:child_process'); assert.strictEqual('NODE_UNIQUE_ID' in process.env, false, `NODE_UNIQUE_ID (${process.env.NODE_UNIQUE_ID}) ` + 'should be removed on startup'); +{ + const { status } = spawnSync(process.execPath, [ + '-e', + ` + const { strictEqual } = require('node:assert'); + Object.setPrototypeOf(process.env, { NODE_UNIQUE_ID: 0 }); + strictEqual(require('cluster').isPrimary, true); + `, + ]); + assert.strictEqual(status, 0); +} + function forEach(obj, fn) { Object.keys(obj).forEach((name, index) => { fn(obj[name], name, index);