Skip to content

Commit 6a1c5a6

Browse files
authored
fix(runtime): support node 14+ (#135)
1 parent 36060cd commit 6a1c5a6

File tree

3 files changed

+36
-72
lines changed

3 files changed

+36
-72
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
node: [16]
13+
node: [14, 16, 18]
1414
steps:
1515
- uses: actions/checkout@v1
1616
- uses: actions/setup-node@v1

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@pkgjs/parseargs",
33
"version": "0.9.0",
44
"description": "Polyfill of future proposal for `util.parseArgs()`",
5+
"engines": {
6+
"node": ">=14"
7+
},
58
"main": "index.js",
69
"exports": {
710
".": "./index.js",

primordials.js

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/*
2+
This file is copied from https://github.com/nodejs/node/blob/v14.19.3/lib/internal/per_context/primordials.js
3+
under the following license:
4+
5+
Copyright Node.js contributors. All rights reserved.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to
9+
deal in the Software without restriction, including without limitation the
10+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
sell copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
IN THE SOFTWARE.
24+
*/
25+
126
'use strict';
227

328
/* eslint-disable node-core/prefer-primordials */
@@ -169,7 +194,6 @@ function copyPrototype(src, dest, prefix) {
169194

170195
// Create copies of intrinsic objects
171196
[
172-
'AggregateError',
173197
'Array',
174198
'ArrayBuffer',
175199
'BigInt',
@@ -180,7 +204,6 @@ function copyPrototype(src, dest, prefix) {
180204
'Date',
181205
'Error',
182206
'EvalError',
183-
'FinalizationRegistry',
184207
'Float32Array',
185208
'Float64Array',
186209
'Function',
@@ -204,7 +227,6 @@ function copyPrototype(src, dest, prefix) {
204227
'Uint8Array',
205228
'Uint8ClampedArray',
206229
'WeakMap',
207-
'WeakRef',
208230
'WeakSet',
209231
].forEach((name) => {
210232
// eslint-disable-next-line no-restricted-globals
@@ -232,16 +254,12 @@ function copyPrototype(src, dest, prefix) {
232254
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
233255
[
234256
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
235-
{
236-
name: 'ArrayIterator', original: {
237-
prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
238-
}
239-
},
240-
{
241-
name: 'StringIterator', original: {
242-
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
243-
}
244-
},
257+
{ name: 'ArrayIterator', original: {
258+
prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
259+
} },
260+
{ name: 'StringIterator', original: {
261+
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
262+
} },
245263
].forEach(({ name, original }) => {
246264
primordials[name] = original;
247265
// The static %TypedArray% methods require a valid `this`, but can't be bound,
@@ -254,17 +272,13 @@ function copyPrototype(src, dest, prefix) {
254272

255273
const {
256274
ArrayPrototypeForEach,
257-
FinalizationRegistry,
258275
FunctionPrototypeCall,
259276
Map,
260277
ObjectFreeze,
261278
ObjectSetPrototypeOf,
262-
Promise,
263-
PromisePrototypeThen,
264279
Set,
265280
SymbolIterator,
266281
WeakMap,
267-
WeakRef,
268282
WeakSet,
269283
} = primordials;
270284

@@ -309,9 +323,6 @@ const copyProps = (src, dest) => {
309323
});
310324
};
311325

312-
/**
313-
* @type {typeof primordials.makeSafe}
314-
*/
315326
const makeSafe = (unsafe, safe) => {
316327
if (SymbolIterator in unsafe.prototype) {
317328
const dummy = new unsafe();
@@ -326,7 +337,7 @@ const makeSafe = (unsafe, safe) => {
326337
SymbolIterator in (FunctionPrototypeCall(desc.value, dummy) ?? {})
327338
) {
328339
const createIterator = uncurryThis(desc.value);
329-
next ??= uncurryThis(createIterator(dummy).next);
340+
next = next ?? uncurryThis(createIterator(dummy).next);
330341
const SafeIterator = createSafeIterator(createIterator, next);
331342
desc.value = function() {
332343
return new SafeIterator(this);
@@ -363,7 +374,6 @@ primordials.SafeWeakMap = makeSafe(
363374
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
364375
}
365376
);
366-
367377
primordials.SafeSet = makeSafe(
368378
Set,
369379
class SafeSet extends Set {
@@ -377,55 +387,6 @@ primordials.SafeWeakSet = makeSafe(
377387
}
378388
);
379389

380-
primordials.SafeFinalizationRegistry = makeSafe(
381-
FinalizationRegistry,
382-
class SafeFinalizationRegistry extends FinalizationRegistry {
383-
// eslint-disable-next-line no-useless-constructor
384-
constructor(cleanupCallback) { super(cleanupCallback); }
385-
}
386-
);
387-
primordials.SafeWeakRef = makeSafe(
388-
WeakRef,
389-
class SafeWeakRef extends WeakRef {
390-
// eslint-disable-next-line no-useless-constructor
391-
constructor(target) { super(target); }
392-
}
393-
);
394-
395-
const SafePromise = makeSafe(
396-
Promise,
397-
class SafePromise extends Promise {
398-
// eslint-disable-next-line no-useless-constructor
399-
constructor(executor) { super(executor); }
400-
}
401-
);
402-
403-
primordials.PromisePrototypeCatch = (thisPromise, onRejected) =>
404-
PromisePrototypeThen(thisPromise, undefined, onRejected);
405-
406-
/**
407-
* Attaches a callback that is invoked when the Promise is settled (fulfilled or
408-
* rejected). The resolved value cannot be modified from the callback.
409-
* Prefer using async functions when possible.
410-
* @param {Promise<any>} thisPromise
411-
* @param {() => void) | undefined | null} onFinally The callback to execute
412-
* when the Promise is settled (fulfilled or rejected).
413-
* @returns {Promise} A Promise for the completion of the callback.
414-
*/
415-
primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) =>
416-
// Wrapping on a new Promise is necessary to not expose the SafePromise
417-
// prototype to user-land.
418-
new Promise((a, b) =>
419-
new SafePromise((a, b) => PromisePrototypeThen(thisPromise, a, b))
420-
.finally(onFinally)
421-
.then(a, b)
422-
);
423-
424-
primordials.AsyncIteratorPrototype =
425-
primordials.ReflectGetPrototypeOf(
426-
primordials.ReflectGetPrototypeOf(
427-
async function* () { }).prototype);
428-
429390
ObjectSetPrototypeOf(primordials, null);
430391
ObjectFreeze(primordials);
431392

0 commit comments

Comments
 (0)