Skip to content

Commit

Permalink
Remove more unnecessary polyfills from webapi for Node 16 (#5814)
Browse files Browse the repository at this point in the history
* feat(webapi): Remove unnecessary polyfills now that we dropped support for Node 14

* feat(webapi): Removed more unnecessary polyfills for Node 16

* chore: changeset
  • Loading branch information
Princesseuh committed Jan 10, 2023
1 parent cf5dc2a commit c55fbcb
Show file tree
Hide file tree
Showing 20 changed files with 23 additions and 422 deletions.
5 changes: 5 additions & 0 deletions .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
4 changes: 0 additions & 4 deletions packages/webapi/LICENSE
Expand Up @@ -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.
26 changes: 5 additions & 21 deletions 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
Expand All @@ -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)
Expand Down Expand Up @@ -78,9 +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)
- [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)
- [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)
Expand All @@ -89,20 +84,14 @@ 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)
- [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)

## Usage

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`.
Expand Down Expand Up @@ -149,11 +138,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` |
Expand All @@ -166,10 +154,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.
2 changes: 1 addition & 1 deletion 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;
Expand Down
5 changes: 0 additions & 5 deletions packages/webapi/package.json
Expand Up @@ -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": {
Expand All @@ -59,19 +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",
"fetch-blob": "^3.1.5",
"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"
Expand Down
8 changes: 1 addition & 7 deletions packages/webapi/run/build.js
Expand Up @@ -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'],
Expand Down Expand Up @@ -172,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)
},
Expand Down
3 changes: 0 additions & 3 deletions packages/webapi/src/exclusions.ts
Expand Up @@ -28,7 +28,6 @@ const exclusionsForNode = [
...exclusionsForElement,
] as const
const exclusionsForEventTarget = [
'AbortSignal',
'Event',
'CustomEvent',
'EventTarget',
Expand All @@ -38,7 +37,6 @@ const exclusionsForEventTarget = [
...exclusionsForNode,
] as const
const exclusionsForEvent = [
'AbortSignal',
'Event',
'CustomEvent',
'EventTarget',
Expand All @@ -49,7 +47,6 @@ const exclusionsForEvent = [
] as const

export const exclusions = {
'Blob+': ['Blob', 'File'],
'Document+': exclusionsForDocument,
'Element+': exclusionsForElement,
'Event+': exclusionsForEvent,
Expand Down
1 change: 0 additions & 1 deletion packages/webapi/src/inheritance.ts
Expand Up @@ -5,7 +5,6 @@ export const inheritance = {
Document: 'Node',
DocumentFragment: 'Node',
Element: 'Node',
File: 'Blob',
HTMLDocument: 'Document',
HTMLElement: 'Element',
HTMLBodyElement: 'HTMLElement',
Expand Down
7 changes: 0 additions & 7 deletions packages/webapi/src/lib/Base64.ts

This file was deleted.

57 changes: 0 additions & 57 deletions packages/webapi/src/lib/ContextEvent.ts

This file was deleted.

21 changes: 0 additions & 21 deletions packages/webapi/src/lib/Object.ts

This file was deleted.

28 changes: 0 additions & 28 deletions packages/webapi/src/lib/Promise.ts

This file was deleted.

30 changes: 0 additions & 30 deletions packages/webapi/src/lib/String.ts

This file was deleted.

35 changes: 0 additions & 35 deletions 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
Expand All @@ -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 <TArgs extends any, TFunc extends (...args: TArgs[]) => any>(
callback: TFunc,
thisArg: unknown,
...args: TArgs[]
) => ReturnType<TFunc>

/** Returns an object with the specified prototype. */
export const __object_create = Object.create as {
<T extends any = any>(value: T): any extends T ? Record<any, any> : T
}

/** Returns whether an object has a property with the specified name. */
export const __object_hasOwnProperty = Function.call.bind(
Object.prototype.hasOwnProperty
) as {
<T1 extends object, T2>(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
Expand All @@ -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<unknown, any>()

Expand Down

0 comments on commit c55fbcb

Please sign in to comment.