Remove more unnecessary polyfills from webapi for Node 16 (#5814)
* feat(webapi): Remove unnecessary polyfills now that we dropped support for Node 14 * feat(webapi): Removed more unnecessary polyfills for Node 16 * chore: changeset
This commit is contained in:
parent
cf5dc2adae
commit
c55fbcb8ed
20 changed files with 23 additions and 422 deletions
5
.changeset/shaggy-bags-attend.md
Normal file
5
.changeset/shaggy-bags-attend.md
Normal file
|
@ -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
|
|
@ -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.
|
||||
|
|
|
@ -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,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)
|
||||
|
@ -89,12 +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)
|
||||
- [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
|
||||
|
@ -102,7 +91,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 +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` |
|
||||
|
@ -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
packages/webapi/mod.d.ts
vendored
2
packages/webapi/mod.d.ts
vendored
|
@ -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;
|
||||
|
|
|
@ -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,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"
|
||||
|
|
|
@ -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'],
|
||||
|
@ -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)
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -5,7 +5,6 @@ export const inheritance = {
|
|||
Document: 'Node',
|
||||
DocumentFragment: 'Node',
|
||||
Element: 'Node',
|
||||
File: 'Blob',
|
||||
HTMLDocument: 'Document',
|
||||
HTMLElement: 'Element',
|
||||
HTMLBodyElement: 'HTMLElement',
|
||||
|
|
|
@ -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')
|
||||
}
|
|
@ -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<T = unknown> extends Event<'context-request'> {
|
||||
constructor(init: ContextEventInit<T>) {
|
||||
super('context-request', { bubbles: true, composed: true })
|
||||
|
||||
init = Object(init) as Required<ContextEventInit<T>>
|
||||
|
||||
this.context = init.context
|
||||
}
|
||||
|
||||
context!: Context<T>
|
||||
multiple!: boolean
|
||||
callback!: ContextCallback<Context<T>>
|
||||
}
|
||||
|
||||
interface ContextEventInit<T = unknown> {
|
||||
context: Context<T>
|
||||
multiple?: boolean
|
||||
callback: ContextCallback<Context<T>>
|
||||
}
|
||||
|
||||
/** A Context object defines an optional initial value for a Context, as well as a name identifier for debugging purposes. */
|
||||
export type Context<T = unknown> = {
|
||||
name: string
|
||||
initialValue?: T
|
||||
}
|
||||
|
||||
/** A helper type which can extract a Context value type from a Context type. */
|
||||
export type ContextType<T extends Context> = T extends Context<infer Y>
|
||||
? Y
|
||||
: never
|
||||
|
||||
/** A function which creates a Context value object */
|
||||
export function createContext<T>(
|
||||
name: string,
|
||||
initialValue?: T
|
||||
): Readonly<Context<T>> {
|
||||
return {
|
||||
name,
|
||||
initialValue,
|
||||
}
|
||||
}
|
||||
|
||||
/** A callback which is provided by a context requester and is called with the value satisfying the request. */
|
||||
export type ContextCallback<ValueType> = (
|
||||
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
|
||||
}
|
||||
}
|
|
@ -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<string>) => {
|
||||
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,
|
||||
})
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
export const any = {
|
||||
async any<T>(iterable: Iterable<T | PromiseLike<T>>): Promise<T> {
|
||||
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<T>(value)
|
||||
)
|
||||
},
|
||||
}.any
|
||||
|
||||
export const initPromise = (target: any, exclude: Set<string>) => {
|
||||
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,
|
||||
})
|
||||
}
|
|
@ -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<string>) => {
|
||||
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,
|
||||
})
|
||||
}
|
|
@ -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 <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
|
||||
|
@ -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>()
|
||||
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
import {
|
||||
AbortController,
|
||||
AbortSignal,
|
||||
alert,
|
||||
atob,
|
||||
Blob,
|
||||
btoa,
|
||||
ByteLengthQueuingStrategy,
|
||||
cancelAnimationFrame,
|
||||
cancelIdleCallback,
|
||||
|
@ -43,11 +38,7 @@ import {
|
|||
initCustomElementRegistry,
|
||||
initDocument,
|
||||
initMediaQueryList,
|
||||
initObject,
|
||||
initPromise,
|
||||
initRelativeIndexingMethod,
|
||||
initStorage,
|
||||
initString,
|
||||
initWindow,
|
||||
IntersectionObserver,
|
||||
MediaQueryList,
|
||||
|
@ -87,12 +78,7 @@ import { inheritance } from './inheritance'
|
|||
|
||||
export { pathToPosix } from './lib/utils'
|
||||
export {
|
||||
AbortController,
|
||||
AbortSignal,
|
||||
alert,
|
||||
atob,
|
||||
Blob,
|
||||
btoa,
|
||||
ByteLengthQueuingStrategy,
|
||||
cancelAnimationFrame,
|
||||
cancelIdleCallback,
|
||||
|
@ -162,9 +148,6 @@ export {
|
|||
|
||||
export const polyfill = (target: any, options?: PolyfillOptions) => {
|
||||
const webAPIs = {
|
||||
AbortController,
|
||||
AbortSignal,
|
||||
Blob,
|
||||
ByteLengthQueuingStrategy,
|
||||
CanvasRenderingContext2D,
|
||||
CharacterData,
|
||||
|
@ -225,8 +208,6 @@ export const polyfill = (target: any, options?: PolyfillOptions) => {
|
|||
Window,
|
||||
|
||||
alert,
|
||||
atob,
|
||||
btoa,
|
||||
cancelAnimationFrame,
|
||||
cancelIdleCallback,
|
||||
clearTimeout,
|
||||
|
@ -314,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
|
||||
|
@ -330,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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
@ -78,20 +71,13 @@ 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
|
||||
const Request = undici.Request
|
||||
const File = undici.File
|
||||
|
||||
export {
|
||||
AbortController,
|
||||
AbortSignal,
|
||||
Blob,
|
||||
ByteLengthQueuingStrategy,
|
||||
CanvasRenderingContext2D,
|
||||
CharacterData,
|
||||
|
@ -151,8 +137,6 @@ export {
|
|||
WritableStreamDefaultWriter,
|
||||
Window,
|
||||
alert,
|
||||
atob,
|
||||
btoa,
|
||||
cancelAnimationFrame,
|
||||
cancelIdleCallback,
|
||||
clearTimeout,
|
||||
|
@ -164,10 +148,6 @@ export {
|
|||
initCustomElementRegistry,
|
||||
initDocument,
|
||||
initMediaQueryList,
|
||||
initObject,
|
||||
initPromise,
|
||||
initRelativeIndexingMethod,
|
||||
initStorage,
|
||||
initString,
|
||||
initWindow,
|
||||
}
|
||||
|
|
1
packages/webapi/src/types.d.ts
vendored
1
packages/webapi/src/types.d.ts
vendored
|
@ -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'
|
||||
|
|
|
@ -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')
|
||||
})
|
||||
})
|
|
@ -6,9 +6,6 @@ describe('Basic', () => {
|
|||
|
||||
it('Globals exist', () => {
|
||||
const webAPIs = [
|
||||
'AbortController',
|
||||
'AbortSignal',
|
||||
'Blob',
|
||||
'ByteLengthQueuingStrategy',
|
||||
'CSSStyleSheet',
|
||||
'CountQueuingStrategy',
|
||||
|
@ -66,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)
|
||||
|
@ -113,73 +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)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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==}
|
||||
|
|
Loading…
Reference in a new issue