From 2e3c8b31005e73b80f5c663a199b9374fc6905ee Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 13 Sep 2022 21:45:45 -0700 Subject: [PATCH] [Fix] avoid crashing with `--disable-proto=throw` Fixes #26. --- .github/workflows/node-noproto.yml | 18 ++++++++++++++++++ implementation.js | 12 +++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/node-noproto.yml diff --git a/.github/workflows/node-noproto.yml b/.github/workflows/node-noproto.yml new file mode 100644 index 0000000..3866cd3 --- /dev/null +++ b/.github/workflows/node-noproto.yml @@ -0,0 +1,18 @@ +name: 'Tests: node --disable-proto=throw' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 12.17' + type: minors + command: 'NODE_OPTIONS="--disable-proto=throw" npx tape "test/**/*.js"' + + node: + name: 'node --disable-proto=throw' + needs: [tests] + runs-on: ubuntu-latest + steps: + - run: 'echo tests completed' diff --git a/implementation.js b/implementation.js index 3532e5e..da426cc 100644 --- a/implementation.js +++ b/implementation.js @@ -4,10 +4,12 @@ var forEach = require('for-each'); var isES5 = typeof Object.defineProperty === 'function'; -var hasProto = require('has-proto')(); +var gPO = Object.getPrototypeOf; +var sPO = Object.setPrototypeOf; +var hasProto = require('has-proto')() || (typeof gPO === 'function' && gPO([]) === Array.prototype); if (!isES5 || !hasProto) { - throw new TypeError('util.promisify requires a true ES5 environment, that also supports `__proto__`'); + throw new TypeError('util.promisify requires a true ES5+ environment, that also supports `__proto__` and/or `Object.getPrototypeOf`'); } var getOwnPropertyDescriptors = require('object.getownpropertydescriptors'); @@ -85,7 +87,11 @@ module.exports = function promisify(orig) { }); }; - promisified.__proto__ = orig.__proto__; // eslint-disable-line no-proto + if (typeof sPO === 'function' && typeof gPO === 'function') { + sPO(promisified, gPO(orig)); + } else { + promisified.__proto__ = orig.__proto__; // eslint-disable-line no-proto + } oDP(promisified, kCustomPromisifiedSymbol, { configurable: true,