diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index cfb392e..5905863 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node: [16] + node: [14, 16, 18] steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 diff --git a/package.json b/package.json index 69906b5..dcce3be 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "@pkgjs/parseargs", "version": "0.9.0", "description": "Polyfill of future proposal for `util.parseArgs()`", + "engines": { + "node": ">=14" + }, "main": "index.js", "exports": { ".": "./index.js", diff --git a/primordials.js b/primordials.js index e2af4e8..63e23ab 100644 --- a/primordials.js +++ b/primordials.js @@ -1,3 +1,28 @@ +/* +This file is copied from https://github.com/nodejs/node/blob/v14.19.3/lib/internal/per_context/primordials.js +under the following license: + +Copyright Node.js contributors. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. +*/ + 'use strict'; /* eslint-disable node-core/prefer-primordials */ @@ -169,7 +194,6 @@ function copyPrototype(src, dest, prefix) { // Create copies of intrinsic objects [ - 'AggregateError', 'Array', 'ArrayBuffer', 'BigInt', @@ -180,7 +204,6 @@ function copyPrototype(src, dest, prefix) { 'Date', 'Error', 'EvalError', - 'FinalizationRegistry', 'Float32Array', 'Float64Array', 'Function', @@ -204,7 +227,6 @@ function copyPrototype(src, dest, prefix) { 'Uint8Array', 'Uint8ClampedArray', 'WeakMap', - 'WeakRef', 'WeakSet', ].forEach((name) => { // eslint-disable-next-line no-restricted-globals @@ -232,16 +254,12 @@ function copyPrototype(src, dest, prefix) { // Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object [ { name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) }, - { - name: 'ArrayIterator', original: { - prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()), - } - }, - { - name: 'StringIterator', original: { - prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()), - } - }, + { name: 'ArrayIterator', original: { + prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()), + } }, + { name: 'StringIterator', original: { + prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()), + } }, ].forEach(({ name, original }) => { primordials[name] = original; // The static %TypedArray% methods require a valid `this`, but can't be bound, @@ -254,17 +272,13 @@ function copyPrototype(src, dest, prefix) { const { ArrayPrototypeForEach, - FinalizationRegistry, FunctionPrototypeCall, Map, ObjectFreeze, ObjectSetPrototypeOf, - Promise, - PromisePrototypeThen, Set, SymbolIterator, WeakMap, - WeakRef, WeakSet, } = primordials; @@ -309,9 +323,6 @@ const copyProps = (src, dest) => { }); }; -/** - * @type {typeof primordials.makeSafe} - */ const makeSafe = (unsafe, safe) => { if (SymbolIterator in unsafe.prototype) { const dummy = new unsafe(); @@ -326,7 +337,7 @@ const makeSafe = (unsafe, safe) => { SymbolIterator in (FunctionPrototypeCall(desc.value, dummy) ?? {}) ) { const createIterator = uncurryThis(desc.value); - next ??= uncurryThis(createIterator(dummy).next); + next = next ?? uncurryThis(createIterator(dummy).next); const SafeIterator = createSafeIterator(createIterator, next); desc.value = function() { return new SafeIterator(this); @@ -363,7 +374,6 @@ primordials.SafeWeakMap = makeSafe( constructor(i) { super(i); } // eslint-disable-line no-useless-constructor } ); - primordials.SafeSet = makeSafe( Set, class SafeSet extends Set { @@ -377,55 +387,6 @@ primordials.SafeWeakSet = makeSafe( } ); -primordials.SafeFinalizationRegistry = makeSafe( - FinalizationRegistry, - class SafeFinalizationRegistry extends FinalizationRegistry { - // eslint-disable-next-line no-useless-constructor - constructor(cleanupCallback) { super(cleanupCallback); } - } -); -primordials.SafeWeakRef = makeSafe( - WeakRef, - class SafeWeakRef extends WeakRef { - // eslint-disable-next-line no-useless-constructor - constructor(target) { super(target); } - } -); - -const SafePromise = makeSafe( - Promise, - class SafePromise extends Promise { - // eslint-disable-next-line no-useless-constructor - constructor(executor) { super(executor); } - } -); - -primordials.PromisePrototypeCatch = (thisPromise, onRejected) => - PromisePrototypeThen(thisPromise, undefined, onRejected); - -/** - * Attaches a callback that is invoked when the Promise is settled (fulfilled or - * rejected). The resolved value cannot be modified from the callback. - * Prefer using async functions when possible. - * @param {Promise} thisPromise - * @param {() => void) | undefined | null} onFinally The callback to execute - * when the Promise is settled (fulfilled or rejected). - * @returns {Promise} A Promise for the completion of the callback. - */ -primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) => - // Wrapping on a new Promise is necessary to not expose the SafePromise - // prototype to user-land. - new Promise((a, b) => - new SafePromise((a, b) => PromisePrototypeThen(thisPromise, a, b)) - .finally(onFinally) - .then(a, b) - ); - -primordials.AsyncIteratorPrototype = - primordials.ReflectGetPrototypeOf( - primordials.ReflectGetPrototypeOf( - async function* () { }).prototype); - ObjectSetPrototypeOf(primordials, null); ObjectFreeze(primordials);