From 1b8f105ad77b481075e58062e5a66beac2931c87 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Tue, 10 Jan 2023 11:29:20 +0100 Subject: [PATCH 1/3] feat(webapi): Remove unnecessary polyfills now that we dropped support for Node 14 --- packages/webapi/LICENSE | 4 ---- packages/webapi/README.md | 19 ++++--------------- packages/webapi/package.json | 1 - packages/webapi/run/build.js | 6 ------ packages/webapi/src/exclusions.ts | 3 --- packages/webapi/src/inheritance.ts | 1 - packages/webapi/src/lib/Base64.ts | 7 ------- packages/webapi/src/lib/String.ts | 30 ------------------------------ packages/webapi/src/polyfill.ts | 15 --------------- packages/webapi/src/ponyfill.ts | 13 +------------ packages/webapi/src/types.d.ts | 1 - packages/webapi/test/base64.js | 29 ----------------------------- packages/webapi/test/basic.js | 16 ---------------- 13 files changed, 5 insertions(+), 140 deletions(-) delete mode 100644 packages/webapi/src/lib/Base64.ts delete mode 100644 packages/webapi/src/lib/String.ts delete mode 100644 packages/webapi/test/base64.js diff --git a/packages/webapi/LICENSE b/packages/webapi/LICENSE index 7dc74ec3874e..bb084e8bdb0f 100644 --- a/packages/webapi/LICENSE +++ b/packages/webapi/LICENSE @@ -24,10 +24,6 @@ THE SOFTWARE. Code from [@astrocommunity/webapi](https://www.npmjs.com/@astrocommunity/webapi) is licensed under the CC0-1.0 License. -Code from [abort-controller](https://www.npmjs.com/package/abort-controller) is licensed under the MIT License (MIT), Copyright Toru Nagashima. - Code from [event-target-shim](https://www.npmjs.com/package/event-target-shim) is licensed under the MIT License (MIT), Copyright Toru Nagashima. -Code from [fetch-blob](https://www.npmjs.com/package/fetch-blob) is licensed under the MIT License (MIT), Copyright Jimmy Wärting. - Code from [formdata-polyfill](https://www.npmjs.com/package/formdata-polyfill) is licensed under the MIT License (MIT), Copyright Jimmy Wärting. diff --git a/packages/webapi/README.md b/packages/webapi/README.md index 2f7726e9a839..e05b9ad06071 100644 --- a/packages/webapi/README.md +++ b/packages/webapi/README.md @@ -1,6 +1,6 @@ # WebAPI -**WebAPI** lets you use Web APIs in Node v12, v14, and v16. +**WebAPI** lets you use Web APIs not present in Node v16 and later. ```sh npm install @astrojs/webapi @@ -23,9 +23,6 @@ These APIs are combined from popular open source projects and configured to shar ## Features -- [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) -- [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) -- [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) - [ByteLengthQueuingStrategy](https://developer.mozilla.org/en-US/docs/Web/API/ByteLengthQueuingStrategy) - [CanvasRenderingContext2D](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) - [CSSStyleSheet](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet) @@ -78,8 +75,6 @@ These APIs are combined from popular open source projects and configured to shar - [WritableStreamDefaultController](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultController) - [WritableStreamDefaultWriter](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter) - [Window](https://developer.mozilla.org/en-US/docs/Web/API/Window) -- [atob](https://developer.mozilla.org/en-US/docs/Web/API/atob) -- [btoa](https://developer.mozilla.org/en-US/docs/Web/API/btoa) - [cancelAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/cancelAnimationFrame) - [cancelIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/cancelIdleCallback) - [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) @@ -93,7 +88,6 @@ These APIs are combined from popular open source projects and configured to shar - [Promise.any](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) - [Array.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at) - [String.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at) -- [String.prototype.replaceAll](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) - [TypedArray.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at) - [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) @@ -102,7 +96,7 @@ These APIs are combined from popular open source projects and configured to shar You can use WebAPIs as individual exports. ```js -import { AbortController, Blob, Event, EventTarget, File, fetch, Response } from '@astrojs/webapi' +import { Event, EventTarget, File, fetch, Response } from '@astrojs/webapi' ``` You can apply WebAPIs to an object, like `globalThis`. @@ -149,11 +143,10 @@ polyfill(globalThis, { | Shorthand | Excludes | |:-------------- |:-------- | -| `Blob+` | `Blob`, `File` | | `Document+` | `Document`, `HTMLDocument` | | `Element+` | `Element`, and exclusions from `HTMLElement+` | -| `Event+` | `Event`, `CustomEvent`, `EventTarget`, `AbortSignal`, `MediaQueryList`, `Window`, and exclusions from `Node+` | -| `EventTarget+` | `Event`, `CustomEvent`, `EventTarget`, `AbortSignal`, `MediaQueryList`, `Window`, and exclusions from `Node+` | +| `Event+` | `Event`, `CustomEvent`, `EventTarget`, `MediaQueryList`, `Window`, and exclusions from `Node+` | +| `EventTarget+` | `Event`, `CustomEvent`, `EventTarget`, `MediaQueryList`, `Window`, and exclusions from `Node+` | | `HTMLElement+` | `CustomElementsRegistry`, `HTMLElement`, `HTMLBodyElement`, `HTMLCanvasElement`, `HTMLDivElement`, `HTMLHeadElement`, `HTMLHtmlElement`, `HTMLImageElement`, `HTMLStyleElement`, `HTMLTemplateElement`, `HTMLUnknownElement`, `Image` | | `Node+` | `Node`, `DocumentFragment`, `ShadowRoot`, `Document`, `HTMLDocument`, and exclusions from `Element+` | | `StyleSheet+` | `StyleSheet`, `CSSStyleSheet` | @@ -166,10 +159,6 @@ polyfill(globalThis, { Thank you to Jon Neal for his work on the original [webapi](https://github.com/astro-community/webapi) project that this package is forked from. Licensed under the CC0-1.0 License. -Code from [abort-controller](https://www.npmjs.com/package/abort-controller) is licensed under the MIT License (MIT), Copyright Toru Nagashima. - Code from [event-target-shim](https://www.npmjs.com/package/event-target-shim) is licensed under the MIT License (MIT), Copyright Toru Nagashima. -Code from [fetch-blob](https://www.npmjs.com/package/fetch-blob) is licensed under the MIT License (MIT), Copyright Jimmy Wärting. - Code from [formdata-polyfill](https://www.npmjs.com/package/formdata-polyfill) is licensed under the MIT License (MIT), Copyright Jimmy Wärting. diff --git a/packages/webapi/package.json b/packages/webapi/package.json index e712e541d89c..14bae792f84c 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -66,7 +66,6 @@ "abort-controller": "^3.0.0", "chai": "^4.3.6", "event-target-shim": "^6.0.2", - "fetch-blob": "^3.1.5", "formdata-polyfill": "^4.0.10", "magic-string": "^0.25.9", "mocha": "^9.2.2", diff --git a/packages/webapi/run/build.js b/packages/webapi/run/build.js index 2ec835644c53..1eec6672bdea 100644 --- a/packages/webapi/run/build.js +++ b/packages/webapi/run/build.js @@ -58,12 +58,6 @@ const plugins = [ inject({ // import { Promise as P } from 'es6-promise' // P: [ 'es6-promise', 'Promise' ], - - AbortController: [ - 'abort-controller/dist/abort-controller.mjs', - 'AbortController', - ], - Blob: ['fetch-blob/from.js', 'Blob'], DOMException: [pathToDOMException, 'DOMException'], Document: ['./Document', 'Document'], Element: ['./Element', 'Element'], diff --git a/packages/webapi/src/exclusions.ts b/packages/webapi/src/exclusions.ts index edd8274b1fee..a664483dc026 100644 --- a/packages/webapi/src/exclusions.ts +++ b/packages/webapi/src/exclusions.ts @@ -28,7 +28,6 @@ const exclusionsForNode = [ ...exclusionsForElement, ] as const const exclusionsForEventTarget = [ - 'AbortSignal', 'Event', 'CustomEvent', 'EventTarget', @@ -38,7 +37,6 @@ const exclusionsForEventTarget = [ ...exclusionsForNode, ] as const const exclusionsForEvent = [ - 'AbortSignal', 'Event', 'CustomEvent', 'EventTarget', @@ -49,7 +47,6 @@ const exclusionsForEvent = [ ] as const export const exclusions = { - 'Blob+': ['Blob', 'File'], 'Document+': exclusionsForDocument, 'Element+': exclusionsForElement, 'Event+': exclusionsForEvent, diff --git a/packages/webapi/src/inheritance.ts b/packages/webapi/src/inheritance.ts index 713c2492884d..6e3165633681 100644 --- a/packages/webapi/src/inheritance.ts +++ b/packages/webapi/src/inheritance.ts @@ -5,7 +5,6 @@ export const inheritance = { Document: 'Node', DocumentFragment: 'Node', Element: 'Node', - File: 'Blob', HTMLDocument: 'Document', HTMLElement: 'Element', HTMLBodyElement: 'HTMLElement', diff --git a/packages/webapi/src/lib/Base64.ts b/packages/webapi/src/lib/Base64.ts deleted file mode 100644 index 593c56ae04a0..000000000000 --- a/packages/webapi/src/lib/Base64.ts +++ /dev/null @@ -1,7 +0,0 @@ -export function atob(data: string): string { - return Buffer.from(data, 'base64').toString('binary') -} - -export function btoa(data: string): string { - return Buffer.from(data, 'binary').toString('base64') -} diff --git a/packages/webapi/src/lib/String.ts b/packages/webapi/src/lib/String.ts deleted file mode 100644 index 92d39f39a160..000000000000 --- a/packages/webapi/src/lib/String.ts +++ /dev/null @@ -1,30 +0,0 @@ -import * as _ from './utils' - -export const replaceAll = { - replaceAll( - this: string, - searchValue: RegExp | string, - replaceValue: string | ((substring: string, ...args: any[]) => string) - ) { - return _.__object_isPrototypeOf(RegExp.prototype, searchValue) - ? this.replace(searchValue as RegExp, replaceValue as string) - : this.replace( - new RegExp(_.__string_escapeRegExp(searchValue as string), 'g'), - replaceValue as string - ) - }, -}.replaceAll - -export const initString = (target: any, exclude: Set) => { - if (exclude.has('String') || exclude.has('replaceAll')) return - - const Class = target.String || globalThis.String - - if (!Class.prototype.replaceAll) - Object.defineProperty(Class.prototype, 'replaceAll', { - value: replaceAll, - writable: true, - enumerable: false, - configurable: true, - }) -} diff --git a/packages/webapi/src/polyfill.ts b/packages/webapi/src/polyfill.ts index 13ad0eb10d60..6de0925b0daf 100644 --- a/packages/webapi/src/polyfill.ts +++ b/packages/webapi/src/polyfill.ts @@ -1,10 +1,5 @@ import { - AbortController, - AbortSignal, alert, - atob, - Blob, - btoa, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, @@ -87,12 +82,7 @@ import { inheritance } from './inheritance' export { pathToPosix } from './lib/utils' export { - AbortController, - AbortSignal, alert, - atob, - Blob, - btoa, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, @@ -162,9 +152,6 @@ export { export const polyfill = (target: any, options?: PolyfillOptions) => { const webAPIs = { - AbortController, - AbortSignal, - Blob, ByteLengthQueuingStrategy, CanvasRenderingContext2D, CharacterData, @@ -225,8 +212,6 @@ export const polyfill = (target: any, options?: PolyfillOptions) => { Window, alert, - atob, - btoa, cancelAnimationFrame, cancelIdleCallback, clearTimeout, diff --git a/packages/webapi/src/ponyfill.ts b/packages/webapi/src/ponyfill.ts index efaad8cd34ae..98a40c7449e0 100644 --- a/packages/webapi/src/ponyfill.ts +++ b/packages/webapi/src/ponyfill.ts @@ -1,11 +1,5 @@ // @ts-check - -import { - AbortController, - AbortSignal, -} from 'abort-controller/dist/abort-controller.mjs' import { Event, EventTarget } from 'event-target-shim' -import { Blob, File } from 'fetch-blob/from.js' import { FormData } from 'formdata-polyfill/esm.min.js' import { ByteLengthQueuingStrategy, @@ -27,7 +21,6 @@ import { cancelAnimationFrame, requestAnimationFrame, } from './lib/AnimationFrame' -import { atob, btoa } from './lib/Base64' import { CharacterData, Comment, Text } from './lib/CharacterData' import { CustomEvent } from './lib/CustomEvent' import { DOMException } from './lib/DOMException' @@ -87,11 +80,9 @@ const fetch = undici.fetch const Headers = undici.Headers const Response = undici.Response const Request = undici.Request +const File = undici.File export { - AbortController, - AbortSignal, - Blob, ByteLengthQueuingStrategy, CanvasRenderingContext2D, CharacterData, @@ -151,8 +142,6 @@ export { WritableStreamDefaultWriter, Window, alert, - atob, - btoa, cancelAnimationFrame, cancelIdleCallback, clearTimeout, diff --git a/packages/webapi/src/types.d.ts b/packages/webapi/src/types.d.ts index 2597566eedd8..3592b7d5af40 100644 --- a/packages/webapi/src/types.d.ts +++ b/packages/webapi/src/types.d.ts @@ -2,4 +2,3 @@ declare module 'node:*' declare module '@ungap/structured-clone/esm/index.js' declare module '@ungap/structured-clone/esm/deserialize.js' declare module '@ungap/structured-clone/esm/serialize.js' -declare module 'abort-controller/dist/abort-controller.mjs' diff --git a/packages/webapi/test/base64.js b/packages/webapi/test/base64.js deleted file mode 100644 index 8881784dcb4a..000000000000 --- a/packages/webapi/test/base64.js +++ /dev/null @@ -1,29 +0,0 @@ -import { expect } from 'chai' -import { polyfill } from '../mod.js' - -describe('Base64', () => { - const target = {} - - before(() => polyfill(target)) - - it('Supports Base64 Methods', () => { - expect(target).to.have.property('atob').that.is.a('function') - expect(target).to.have.property('btoa').that.is.a('function') - }) - - it('Supports atob(data)', () => { - const a = 'SGVsbG8sIHdvcmxk' - const b = target.atob(a) - - expect(a).to.equal('SGVsbG8sIHdvcmxk') - expect(b).to.equal('Hello, world') - }) - - it('Supports btoa(data)', () => { - const b = 'Hello, world' - const a = target.btoa(b) - - expect(a).to.equal('SGVsbG8sIHdvcmxk') - expect(b).to.equal('Hello, world') - }) -}) diff --git a/packages/webapi/test/basic.js b/packages/webapi/test/basic.js index 5ee48d0333f5..02b809035293 100644 --- a/packages/webapi/test/basic.js +++ b/packages/webapi/test/basic.js @@ -6,9 +6,6 @@ describe('Basic', () => { it('Globals exist', () => { const webAPIs = [ - 'AbortController', - 'AbortSignal', - 'Blob', 'ByteLengthQueuingStrategy', 'CSSStyleSheet', 'CountQueuingStrategy', @@ -169,17 +166,4 @@ describe('Basic', () => { expect(result).to.equal(42) }) }) - - it('String#replaceAll', () => { - expect(String.prototype.replaceAll).to.be.a('function') - expect(String.prototype.replaceAll.length).to.equal(2) - - const t1 = - 'Of all the sorcerers in Harry Potter, Halo is my favorite sorcerer.' - const t2 = t1.replaceAll('sorcerer', 'philosopher') - const t3 = - 'Of all the philosophers in Harry Potter, Halo is my favorite philosopher.' - - expect(t2).to.equal(t3) - }) }) From 06f283052ac291367a26db045110f4c139d10ee4 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Tue, 10 Jan 2023 12:37:05 +0100 Subject: [PATCH 2/3] feat(webapi): Removed more unnecessary polyfills for Node 16 --- packages/webapi/README.md | 7 +-- packages/webapi/mod.d.ts | 2 +- packages/webapi/package.json | 4 -- packages/webapi/run/build.js | 2 +- packages/webapi/src/lib/ContextEvent.ts | 57 ----------------- packages/webapi/src/lib/Object.ts | 21 ------- packages/webapi/src/lib/Promise.ts | 28 --------- packages/webapi/src/lib/utils.ts | 35 ----------- packages/webapi/src/polyfill.ts | 12 ---- packages/webapi/src/ponyfill.ts | 9 --- packages/webapi/test/basic.js | 82 ------------------------- pnpm-lock.yaml | 36 +++-------- 12 files changed, 13 insertions(+), 282 deletions(-) delete mode 100644 packages/webapi/src/lib/ContextEvent.ts delete mode 100644 packages/webapi/src/lib/Object.ts delete mode 100644 packages/webapi/src/lib/Promise.ts diff --git a/packages/webapi/README.md b/packages/webapi/README.md index e05b9ad06071..aadc6db8d987 100644 --- a/packages/webapi/README.md +++ b/packages/webapi/README.md @@ -75,7 +75,7 @@ These APIs are combined from popular open source projects and configured to shar - [WritableStreamDefaultController](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultController) - [WritableStreamDefaultWriter](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter) - [Window](https://developer.mozilla.org/en-US/docs/Web/API/Window) -- [cancelAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/cancelAnimationFrame) +- [cancelAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame) - [cancelIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/cancelIdleCallback) - [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) - [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) @@ -84,11 +84,6 @@ These APIs are combined from popular open source projects and configured to shar - [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/requestIdleCallback) - [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) - [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) -- [Object.hasOwn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn) -- [Promise.any](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) -- [Array.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at) -- [String.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at) -- [TypedArray.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at) - [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) ## Usage diff --git a/packages/webapi/mod.d.ts b/packages/webapi/mod.d.ts index 88a2a5f52004..ecc90236488d 100644 --- a/packages/webapi/mod.d.ts +++ b/packages/webapi/mod.d.ts @@ -1,6 +1,6 @@ // organize-imports-ignore export { pathToPosix } from './lib/utils'; -export { AbortController, AbortSignal, alert, atob, Blob, btoa, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from './mod.js'; +export { alert, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from './mod.js'; export declare const polyfill: { (target: any, options?: PolyfillOptions): any; internals(target: any, name: string): any; diff --git a/packages/webapi/package.json b/packages/webapi/package.json index 14bae792f84c..aafdbc119d42 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -50,7 +50,6 @@ "bugs": "https://github.com/withastro/astro/issues", "homepage": "https://github.com/withastro/astro/tree/main/packages/webapi#readme", "dependencies": { - "global-agent": "^3.0.0", "undici": "^5.14.0" }, "devDependencies": { @@ -59,18 +58,15 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@rollup/plugin-typescript": "^8.3.2", "@types/chai": "^4.3.1", - "@types/global-agent": "^2.1.1", "@types/mocha": "^9.1.1", "@types/node": "^14.18.21", "@ungap/structured-clone": "^0.3.4", - "abort-controller": "^3.0.0", "chai": "^4.3.6", "event-target-shim": "^6.0.2", "formdata-polyfill": "^4.0.10", "magic-string": "^0.25.9", "mocha": "^9.2.2", "rollup": "^2.79.1", - "rollup-plugin-terser": "^7.0.2", "tslib": "^2.4.0", "typescript": "~4.7.3", "urlpattern-polyfill": "^1.0.0-rc5" diff --git a/packages/webapi/run/build.js b/packages/webapi/run/build.js index 1eec6672bdea..01782074639c 100644 --- a/packages/webapi/run/build.js +++ b/packages/webapi/run/build.js @@ -166,7 +166,7 @@ async function build() { inputOptions: { input: 'src/polyfill.ts', plugins: plugins, - external: ['undici', 'global-agent'], + external: ['undici'], onwarn(warning, warn) { if (warning.code !== 'UNRESOLVED_IMPORT') warn(warning) }, diff --git a/packages/webapi/src/lib/ContextEvent.ts b/packages/webapi/src/lib/ContextEvent.ts deleted file mode 100644 index e712c4da182d..000000000000 --- a/packages/webapi/src/lib/ContextEvent.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Event } from 'event-target-shim' - -/** An event fired by a context requester to signal it desires a named context. */ -export class ContextEvent extends Event<'context-request'> { - constructor(init: ContextEventInit) { - super('context-request', { bubbles: true, composed: true }) - - init = Object(init) as Required> - - this.context = init.context - } - - context!: Context - multiple!: boolean - callback!: ContextCallback> -} - -interface ContextEventInit { - context: Context - multiple?: boolean - callback: ContextCallback> -} - -/** A Context object defines an optional initial value for a Context, as well as a name identifier for debugging purposes. */ -export type Context = { - name: string - initialValue?: T -} - -/** A helper type which can extract a Context value type from a Context type. */ -export type ContextType = T extends Context - ? Y - : never - -/** A function which creates a Context value object */ -export function createContext( - name: string, - initialValue?: T -): Readonly> { - return { - name, - initialValue, - } -} - -/** A callback which is provided by a context requester and is called with the value satisfying the request. */ -export type ContextCallback = ( - value: ValueType, - dispose?: () => void -) => void - -declare global { - interface HTMLElementEventMap { - /** A 'context-request' event can be emitted by any element which desires a context value to be injected by an external provider. */ - 'context-request': ContextEvent - } -} diff --git a/packages/webapi/src/lib/Object.ts b/packages/webapi/src/lib/Object.ts deleted file mode 100644 index cfc1ea4a4e7c..000000000000 --- a/packages/webapi/src/lib/Object.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as _ from './utils' - -export const hasOwn = { - hasOwn(instance: object, property: any) { - return _.__object_hasOwnProperty(instance, property) - }, -}.hasOwn - -export const initObject = (target: any, exclude: Set) => { - if (exclude.has('Object') || exclude.has('object') || exclude.has('hasOwn')) - return - - const Class = target.Object || globalThis.Object - - Object.defineProperty(Class, 'hasOwn', { - value: hasOwn, - writable: true, - enumerable: false, - configurable: true, - }) -} diff --git a/packages/webapi/src/lib/Promise.ts b/packages/webapi/src/lib/Promise.ts deleted file mode 100644 index 2591fb56a305..000000000000 --- a/packages/webapi/src/lib/Promise.ts +++ /dev/null @@ -1,28 +0,0 @@ -export const any = { - async any(iterable: Iterable>): Promise { - return Promise.all( - [...iterable].map((promise) => { - return new Promise((resolve, reject) => - Promise.resolve(promise).then(reject, resolve) - ) - }) - ).then( - (errors) => Promise.reject(errors), - (value) => Promise.resolve(value) - ) - }, -}.any - -export const initPromise = (target: any, exclude: Set) => { - if (exclude.has('Promise') || exclude.has('any')) return - - const Class = target.Promise || globalThis.Promise - - if (!Class.any) - Object.defineProperty(Class, 'any', { - value: any, - writable: true, - enumerable: false, - configurable: true, - }) -} diff --git a/packages/webapi/src/lib/utils.ts b/packages/webapi/src/lib/utils.ts index 0086f75b6742..61a78e97be71 100644 --- a/packages/webapi/src/lib/utils.ts +++ b/packages/webapi/src/lib/utils.ts @@ -1,8 +1,5 @@ import { performance } from 'node:perf_hooks' -/** Returns the milliseconds elapsed since January 1, 1970 00:00:00 UTC. */ -export const __date_now = Date.now - /** Returns the function bound to the given object. */ export const __function_bind = Function.bind.bind( Function.call as unknown as any @@ -12,34 +9,6 @@ export const __function_bind = Function.bind.bind( ...args: TArgs ) => TFunc -/** Returns the function called with the specified values. */ -export const __function_call = Function.call.bind( - Function.call as unknown as any -) as any>( - callback: TFunc, - thisArg: unknown, - ...args: TArgs[] -) => ReturnType - -/** Returns an object with the specified prototype. */ -export const __object_create = Object.create as { - (value: T): any extends T ? Record : T -} - -/** Returns whether an object has a property with the specified name. */ -export const __object_hasOwnProperty = Function.call.bind( - Object.prototype.hasOwnProperty -) as { - (object: T1, key: T2): T2 extends keyof T1 - ? true - : false -} - -/** Returns a string representation of an object. */ -export const __object_toString = Function.call.bind( - Object.prototype.toString -) as { (value: any): string } - /** Returns whether the object prototype exists in another object. */ export const __object_isPrototypeOf = Function.call.bind( Object.prototype.isPrototypeOf @@ -48,10 +17,6 @@ export const __object_isPrototypeOf = Function.call.bind( /** Current high resolution millisecond timestamp. */ export const __performance_now = performance.now as () => number -/** Returns the string escaped for use inside regular expressions. */ -export const __string_escapeRegExp = (value: string) => - value.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') - // @ts-ignore export const INTERNALS = new WeakMap() diff --git a/packages/webapi/src/polyfill.ts b/packages/webapi/src/polyfill.ts index 6de0925b0daf..4d1531a3fede 100644 --- a/packages/webapi/src/polyfill.ts +++ b/packages/webapi/src/polyfill.ts @@ -38,11 +38,7 @@ import { initCustomElementRegistry, initDocument, initMediaQueryList, - initObject, - initPromise, - initRelativeIndexingMethod, initStorage, - initString, initWindow, IntersectionObserver, MediaQueryList, @@ -299,12 +295,8 @@ export const polyfill = (target: any, options?: PolyfillOptions) => { } } - initObject(target, excludeOptions) initMediaQueryList(target, excludeOptions) - initPromise(target, excludeOptions) - initRelativeIndexingMethod(target, excludeOptions) initStorage(target, excludeOptions) - initString(target, excludeOptions) initWindow(target, excludeOptions) return target @@ -315,11 +307,7 @@ polyfill.internals = (target: any, name: string) => { CustomElementRegistry: initCustomElementRegistry, Document: initDocument, MediaQueryList: initMediaQueryList, - Object: initObject, - Promise: initPromise, - RelativeIndexingMethod: initRelativeIndexingMethod, Storage: initStorage, - String: initString, Window: initWindow, } diff --git a/packages/webapi/src/ponyfill.ts b/packages/webapi/src/ponyfill.ts index 98a40c7449e0..429951e152c1 100644 --- a/packages/webapi/src/ponyfill.ts +++ b/packages/webapi/src/ponyfill.ts @@ -71,11 +71,6 @@ import { initWindow, Window } from './lib/Window' import { alert } from './lib/Alert' -import { initObject } from './lib/Object' -import { initPromise } from './lib/Promise' -import { initRelativeIndexingMethod } from './lib/RelativeIndexingMethod' -import { initString } from './lib/String' - const fetch = undici.fetch const Headers = undici.Headers const Response = undici.Response @@ -153,10 +148,6 @@ export { initCustomElementRegistry, initDocument, initMediaQueryList, - initObject, - initPromise, - initRelativeIndexingMethod, initStorage, - initString, initWindow, } diff --git a/packages/webapi/test/basic.js b/packages/webapi/test/basic.js index 02b809035293..9eb5864d59ad 100644 --- a/packages/webapi/test/basic.js +++ b/packages/webapi/test/basic.js @@ -63,32 +63,6 @@ describe('Basic', () => { } }) - it('Constructs an Event', () => { - const e = new Event('test') - - expect(e.type).to.equal('test') - }) - - it('Constructs an EventTarget', () => { - const _t = new EventTarget() - }) - - it('Dispatches an Event on an EventTarget', () => { - const t = new EventTarget() - - let pass = false - - t.addEventListener('test', (event) => { - pass = true - }) - - const e = new Event('test') - - t.dispatchEvent(e) - - expect(pass).to.equal(true) - }) - it('Classes extend as expected', () => { expect(HTMLElement.prototype).to.be.an.instanceof(Element) expect(Element.prototype).to.be.an.instanceof(Node) @@ -110,60 +84,4 @@ describe('Basic', () => { it('globalThis.window === globalThis', () => { expect(globalThis.window).to.equal(globalThis) }) - - it('Relative Indexing Method (String#at)', () => { - expect(String.prototype.at).to.be.a('function') - expect(String.prototype.at.length).to.equal(1) - - const example = 'The quick brown fox jumps over the lazy dog.' - - expect(example.at(2)).to.equal('e') - expect(example.at(-2)).to.equal('g') - }) - - it('Relative Indexing Method (Array#at)', () => { - expect(Array.prototype.at).to.be.a('function') - expect(Array.prototype.at.length).to.equal(1) - - const example = [1, 3, 5, 7, 9] - - expect(example.at(1)).to.equal(3) - expect(example.at(-1)).to.equal(9) - }) - - it('Relative Indexing Method (TypedArray#at)', () => { - expect(Int8Array.prototype.at).to.be.a('function') - expect(Int8Array.prototype.at.length).to.equal(1) - - const example = new Int8Array([1, 3, 5, 7, 9]) - - expect(example.at(1)).to.equal(3) - expect(example.at(-1)).to.equal(9) - }) - - it('Object.hasOwn', () => { - expect(Object.hasOwn).to.be.a('function') - expect(Object.hasOwn.length).to.equal(2) - - const example = {} - - expect(Object.hasOwn(example, 'prop')).to.equal(false) - - example.prop = 'exists' - - expect(Object.hasOwn(example, 'prop')).to.equal(true) - }) - - it('Promise.any', () => { - expect(Promise.any).to.be.a('function') - expect(Promise.any.length).to.equal(1) - - Promise.any([ - Promise.resolve(42), - Promise.reject(-1), - Promise.reject(Infinity), - ]).then((result) => { - expect(result).to.equal(42) - }) - }) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 339f46529747..2757d8fa3c7c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3545,26 +3545,20 @@ importers: '@rollup/plugin-node-resolve': ^13.3.0 '@rollup/plugin-typescript': ^8.3.2 '@types/chai': ^4.3.1 - '@types/global-agent': ^2.1.1 '@types/mocha': ^9.1.1 '@types/node': ^14.18.21 '@ungap/structured-clone': ^0.3.4 - abort-controller: ^3.0.0 chai: ^4.3.6 event-target-shim: ^6.0.2 - fetch-blob: ^3.1.5 formdata-polyfill: ^4.0.10 - global-agent: ^3.0.0 magic-string: ^0.25.9 mocha: ^9.2.2 rollup: ^2.79.1 - rollup-plugin-terser: ^7.0.2 tslib: ^2.4.0 typescript: ~4.7.3 undici: ^5.14.0 urlpattern-polyfill: ^1.0.0-rc5 dependencies: - global-agent: 3.0.0 undici: 5.14.0 devDependencies: '@rollup/plugin-alias': 3.1.9_rollup@2.79.1 @@ -3572,19 +3566,15 @@ importers: '@rollup/plugin-node-resolve': 13.3.0_rollup@2.79.1 '@rollup/plugin-typescript': 8.5.0_5wimikmvjdwpoz7hebq2gjuhnq '@types/chai': 4.3.4 - '@types/global-agent': 2.1.1 '@types/mocha': 9.1.1 '@types/node': 14.18.36 '@ungap/structured-clone': 0.3.4 - abort-controller: 3.0.0 chai: 4.3.7 event-target-shim: 6.0.2 - fetch-blob: 3.2.0 formdata-polyfill: 4.0.10 magic-string: 0.25.9 mocha: 9.2.2 rollup: 2.79.1 - rollup-plugin-terser: 7.0.2_rollup@2.79.1 tslib: 2.4.1 typescript: 4.7.4 urlpattern-polyfill: 1.0.0-rc5 @@ -6062,29 +6052,35 @@ packages: '@jridgewell/set-array': 1.1.2 '@jridgewell/sourcemap-codec': 1.4.14 '@jridgewell/trace-mapping': 0.3.17 + dev: false /@jridgewell/resolve-uri/3.1.0: resolution: {integrity: sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w==} engines: {node: '>=6.0.0'} + dev: false /@jridgewell/set-array/1.1.2: resolution: {integrity: sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==} engines: {node: '>=6.0.0'} + dev: false /@jridgewell/source-map/0.3.2: resolution: {integrity: sha512-m7O9o2uR8k2ObDysZYzdfhb08VuEml5oWGiosa1VdaPZ/A6QyPkAJuwN0Q1lhULOf6B7MtQmHENS743hWtCrgw==} dependencies: '@jridgewell/gen-mapping': 0.3.2 '@jridgewell/trace-mapping': 0.3.17 + dev: false /@jridgewell/sourcemap-codec/1.4.14: resolution: {integrity: sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==} + dev: false /@jridgewell/trace-mapping/0.3.17: resolution: {integrity: sha512-MCNzAp77qzKca9+W/+I0+sEpaUnZoeasnghNeVc41VZCEKaCH73Vq3BZZ/SzWIgrqE4H4ceI+p+b6C0mHf9T4g==} dependencies: '@jridgewell/resolve-uri': 3.1.0 '@jridgewell/sourcemap-codec': 1.4.14 + dev: false /@jsdevtools/rehype-toc/3.0.2: resolution: {integrity: sha512-n5JEf16Wr4mdkRMZ8wMP/wN9/sHmTjRPbouXjJH371mZ2LEGDl72t8tEsMRNFerQN/QJtivOxqK1frdGa4QK5Q==} @@ -6956,10 +6952,6 @@ packages: '@types/node': 18.11.18 dev: true - /@types/global-agent/2.1.1: - resolution: {integrity: sha512-sVox8Phk1UKgP6LQPAdeRxfww6vHKt7Bf59dXzYLsQBUEMEn8S10a+ESp/yO0i4fJ3WS4+CIuz42hgJcuA+3mA==} - dev: true - /@types/hast/2.3.4: resolution: {integrity: sha512-wLEm0QvaoawEDoTRwzTXp4b4jpwiJDvR5KMnFnVodm3scufTlBOWRD6N1OBf9TZMhjlNsSfcO5V+7AF4+Vy+9g==} dependencies: @@ -7631,13 +7623,6 @@ packages: resolution: {integrity: sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==} dev: false - /abort-controller/3.0.0: - resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} - engines: {node: '>=6.5'} - dependencies: - event-target-shim: 5.0.1 - dev: true - /accepts/1.3.8: resolution: {integrity: sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==} engines: {node: '>= 0.6'} @@ -9837,11 +9822,6 @@ packages: engines: {node: '>= 0.6'} dev: false - /event-target-shim/5.0.1: - resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==} - engines: {node: '>=6'} - dev: true - /event-target-shim/6.0.2: resolution: {integrity: sha512-8q3LsZjRezbFZ2PN+uP+Q7pnHUMmAOziU2vA2OwoFaKIXxlxl38IylhSSgUorWu/rf4er67w0ikBqjBFk/pomA==} engines: {node: '>=10.13.0'} @@ -11085,6 +11065,7 @@ packages: '@types/node': 18.11.18 merge-stream: 2.0.0 supports-color: 7.2.0 + dev: false /jiti/1.16.1: resolution: {integrity: sha512-kJUp4Bj44uTaZAwG6R2/GjbodOWHULn8Swue0B7tY8v5BpTkUvDR+zBM5tsbC4x/jCeYDZ+mAdrUIScwIo4oPw==} @@ -13791,6 +13772,7 @@ packages: rollup: 2.79.1 serialize-javascript: 4.0.0 terser: 5.16.1 + dev: false /rollup-pluginutils/2.8.2: resolution: {integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==} @@ -13941,6 +13923,7 @@ packages: resolution: {integrity: sha512-GaNA54380uFefWghODBWEGisLZFj00nS5ACs6yHa9nLqlLpVLO8ChDGeKRjZnV4Nh4n0Qi7nhYZD/9fCPzEqkw==} dependencies: randombytes: 2.1.0 + dev: false /serialize-javascript/6.0.0: resolution: {integrity: sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==} @@ -14545,6 +14528,7 @@ packages: acorn: 8.8.1 commander: 2.20.3 source-map-support: 0.5.21 + dev: false /text-table/0.2.0: resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} From 3247cb14a553c07ab2aa87d480a9523af4ba94f7 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Tue, 10 Jan 2023 13:08:52 +0100 Subject: [PATCH 3/3] chore: changeset --- .changeset/shaggy-bags-attend.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shaggy-bags-attend.md diff --git a/.changeset/shaggy-bags-attend.md b/.changeset/shaggy-bags-attend.md new file mode 100644 index 000000000000..4a48d24ba086 --- /dev/null +++ b/.changeset/shaggy-bags-attend.md @@ -0,0 +1,5 @@ +--- +'@astrojs/webapi': major +--- + +Moved target to Node 16. Removed polyfills for AbortController, AbortSignal, atob, btoa, Object.hasOwn, Promise.all, Array.at and String.replaceAll