summaryrefslogtreecommitdiff
path: root/packages/webapi
diff options
context:
space:
mode:
Diffstat (limited to 'packages/webapi')
-rw-r--r--packages/webapi/LICENSE4
-rw-r--r--packages/webapi/README.md4
-rw-r--r--packages/webapi/package.json5
-rw-r--r--packages/webapi/run/build.js20
-rw-r--r--packages/webapi/src/lib/fetch.ts102
-rw-r--r--packages/webapi/src/ponyfill.ts9
-rw-r--r--packages/webapi/src/types.d.ts2
-rw-r--r--packages/webapi/test/fetch.js60
8 files changed, 19 insertions, 187 deletions
diff --git a/packages/webapi/LICENSE b/packages/webapi/LICENSE
index 9dda027eb..7dc74ec38 100644
--- a/packages/webapi/LICENSE
+++ b/packages/webapi/LICENSE
@@ -31,7 +31,3 @@ Code from [event-target-shim](https://www.npmjs.com/package/event-target-shim) i
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.
-
-Code from [node-fetch](https://www.npmjs.com/package/node-fetch) is licensed under the MIT License (MIT), Copyright Node Fetch Team.
-
-Code from [web-streams-polyfill](https://www.npmjs.com/package/web-streams-polyfill) is licensed under the MIT License (MIT), Copyright Mattias Buelens and Diwank Singh Tomer.
diff --git a/packages/webapi/README.md b/packages/webapi/README.md
index 8e1c66280..2f7726e9a 100644
--- a/packages/webapi/README.md
+++ b/packages/webapi/README.md
@@ -173,7 +173,3 @@ Code from [event-target-shim](https://www.npmjs.com/package/event-target-shim) i
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.
-
-Code from [node-fetch](https://www.npmjs.com/package/node-fetch) is licensed under the MIT License (MIT), Copyright Node Fetch Team.
-
-Code from [web-streams-polyfill](https://www.npmjs.com/package/web-streams-polyfill) is licensed under the MIT License (MIT), Copyright Mattias Buelens and Diwank Singh Tomer.
diff --git a/packages/webapi/package.json b/packages/webapi/package.json
index f4e417b47..e712e541d 100644
--- a/packages/webapi/package.json
+++ b/packages/webapi/package.json
@@ -51,7 +51,7 @@
"homepage": "https://github.com/withastro/astro/tree/main/packages/webapi#readme",
"dependencies": {
"global-agent": "^3.0.0",
- "node-fetch": "^3.2.5"
+ "undici": "^5.14.0"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.9",
@@ -74,8 +74,7 @@
"rollup-plugin-terser": "^7.0.2",
"tslib": "^2.4.0",
"typescript": "~4.7.3",
- "urlpattern-polyfill": "^1.0.0-rc5",
- "web-streams-polyfill": "^3.2.1"
+ "urlpattern-polyfill": "^1.0.0-rc5"
},
"scripts": {
"build": "node run/build.js",
diff --git a/packages/webapi/run/build.js b/packages/webapi/run/build.js
index 63e17e84c..154d6ffbd 100644
--- a/packages/webapi/run/build.js
+++ b/packages/webapi/run/build.js
@@ -1,17 +1,17 @@
-import { rollup } from 'rollup'
+import { default as alias } from '@rollup/plugin-alias'
+import { default as inject } from '@rollup/plugin-inject'
import { nodeResolve } from '@rollup/plugin-node-resolve'
-import path from 'node:path'
-import { createRequire } from 'node:module'
+import { default as typescript } from '@rollup/plugin-typescript'
+import { default as MagicString } from 'magic-string'
import {
readFile as nodeReadFile,
rename,
rm,
writeFile,
} from 'node:fs/promises'
-import { default as MagicString } from 'magic-string'
-import { default as alias } from '@rollup/plugin-alias'
-import { default as inject } from '@rollup/plugin-inject'
-import { default as typescript } from '@rollup/plugin-typescript'
+import { createRequire } from 'node:module'
+import path from 'node:path'
+import { rollup } from 'rollup'
const readFileCache = Object.create(null)
const require = createRequire(import.meta.url)
@@ -76,13 +76,13 @@ const plugins = [
MediaQueryList: ['./MediaQueryList', 'MediaQueryList'],
Node: ['./Node', 'Node'],
ReadableStream: [
- 'web-streams-polyfill/dist/ponyfill.es6.mjs',
+ 'node:stream/web',
'ReadableStream',
],
ShadowRoot: ['./Node', 'ShadowRoot'],
Window: ['./Window', 'Window'],
'globalThis.ReadableStream': [
- 'web-streams-polyfill/dist/ponyfill.es6.mjs',
+ 'node:stream/web',
'ReadableStream',
],
}),
@@ -178,7 +178,7 @@ async function build() {
inputOptions: {
input: 'src/polyfill.ts',
plugins: plugins,
- external: ['node-fetch', 'global-agent'],
+ external: ['undici', 'global-agent'],
onwarn(warning, warn) {
if (warning.code !== 'UNRESOLVED_IMPORT') warn(warning)
},
diff --git a/packages/webapi/src/lib/fetch.ts b/packages/webapi/src/lib/fetch.ts
deleted file mode 100644
index f8500a846..000000000
--- a/packages/webapi/src/lib/fetch.ts
+++ /dev/null
@@ -1,102 +0,0 @@
-import { bootstrap as bootstrapGlobalAgent } from 'global-agent'
-import type { RequestInit } from 'node-fetch'
-import { default as nodeFetch, Headers, Request, Response } from 'node-fetch'
-import Stream from 'node:stream'
-import * as _ from './utils'
-
-bootstrapGlobalAgent({
- environmentVariableNamespace: '',
-})
-
-export { Headers, Request, Response }
-
-export const fetch = {
- fetch(
- resource: string | Request,
- init?: Partial<FetchInit>
- ): Promise<Response> {
- const resourceURL = new URL(
- _.__object_isPrototypeOf(Request.prototype, resource)
- ? (resource as Request).url
- : _.pathToPosix(resource),
- typeof Object(globalThis.process).cwd === 'function'
- ? 'file:' + _.pathToPosix(process.cwd()) + '/'
- : 'file:'
- )
-
- if (resourceURL.protocol.toLowerCase() === 'file:') {
- return import('node:fs').then((fs) => {
- try {
- const stats = fs.statSync(resourceURL)
- const body = fs.createReadStream(resourceURL)
-
- return new Response(body, {
- status: 200,
- statusText: '',
- headers: {
- 'content-length': String(stats.size),
- date: new Date().toUTCString(),
- 'last-modified': new Date(stats.mtimeMs).toUTCString(),
- },
- })
- } catch (error) {
- const body = new Stream.Readable()
-
- body._read = () => {}
- body.push(null)
-
- return new Response(body, {
- status: 404,
- statusText: '',
- headers: {
- date: new Date().toUTCString(),
- },
- })
- }
- })
- } else {
- return nodeFetch(resource, init)
- }
- },
-}.fetch
-
-type USVString = {} & string
-
-interface FetchInit {
- body: RequestInit['body']
- cache:
- | 'default'
- | 'no-store'
- | 'reload'
- | 'no-cache'
- | 'force-cache'
- | 'only-if-cached'
- credentials: 'omit' | 'same-origin' | 'include'
- headers: Headers | Record<string, string>
- method:
- | 'GET'
- | 'HEAD'
- | 'POST'
- | 'PUT'
- | 'DELETE'
- | 'CONNECT'
- | 'OPTIONS'
- | 'TRACE'
- | 'PATCH'
- | USVString
- mode: 'cors' | 'no-cors' | 'same-origin' | USVString
- redirect: 'follow' | 'manual' | 'error'
- referrer: USVString
- referrerPolicy:
- | 'no-referrer'
- | 'no-referrer-when-downgrade'
- | 'same-origin'
- | 'origin'
- | 'strict-origin'
- | 'origin-when-cross-origin'
- | 'strict-origin-when-cross-origin'
- | 'unsafe-url'
- integrity: USVString
- keepalive: boolean
- signal: AbortSignal
-}
diff --git a/packages/webapi/src/ponyfill.ts b/packages/webapi/src/ponyfill.ts
index fc92975b5..a1088bdb4 100644
--- a/packages/webapi/src/ponyfill.ts
+++ b/packages/webapi/src/ponyfill.ts
@@ -7,6 +7,7 @@ import {
import { Event, EventTarget } from 'event-target-shim'
import { Blob, File } from 'fetch-blob/from.js'
import { FormData } from 'formdata-polyfill/esm.min.js'
+import * as undici from 'undici'
import { URLPattern } from 'urlpattern-polyfill'
import {
ByteLengthQueuingStrategy,
@@ -21,7 +22,7 @@ import {
WritableStream,
WritableStreamDefaultController,
WritableStreamDefaultWriter,
-} from 'web-streams-polyfill/dist/ponyfill.es6.mjs'
+} from 'node:stream/web'
import {
cancelAnimationFrame,
requestAnimationFrame,
@@ -30,7 +31,6 @@ import { atob, btoa } from './lib/Base64'
import { CharacterData, Comment, Text } from './lib/CharacterData'
import { CustomEvent } from './lib/CustomEvent'
import { DOMException } from './lib/DOMException'
-import { fetch, Headers, Request, Response } from './lib/fetch'
import { cancelIdleCallback, requestIdleCallback } from './lib/IdleCallback'
import structuredClone from './lib/structuredClone'
import { clearTimeout, setTimeout } from './lib/Timeout'
@@ -83,6 +83,11 @@ 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
+
export {
AbortController,
AbortSignal,
diff --git a/packages/webapi/src/types.d.ts b/packages/webapi/src/types.d.ts
index 09c57387b..2597566ee 100644
--- a/packages/webapi/src/types.d.ts
+++ b/packages/webapi/src/types.d.ts
@@ -3,5 +3,3 @@ 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'
-declare module 'node-fetch/src/index.js'
-declare module 'web-streams-polyfill/dist/ponyfill.es6.mjs'
diff --git a/packages/webapi/test/fetch.js b/packages/webapi/test/fetch.js
index ae5ae0386..49aab31dc 100644
--- a/packages/webapi/test/fetch.js
+++ b/packages/webapi/test/fetch.js
@@ -22,66 +22,6 @@ describe('Fetch', () => {
expect(json).to.be.an('array')
})
- it('Fetch with file', async () => {
- const { fetch } = target
-
- const url = new URL('../package.json', import.meta.url)
-
- const response = await fetch(url)
-
- expect(response.constructor).to.equal(target.Response)
-
- expect(response.status).to.equal(200)
- expect(response.statusText).to.be.empty
- expect(response.headers.has('date')).to.equal(true)
- expect(response.headers.has('content-length')).to.equal(true)
- expect(response.headers.has('last-modified')).to.equal(true)
-
- const json = await response.json()
-
- expect(json.name).to.equal('@astrojs/webapi')
- })
-
- it('Fetch with missing file', async () => {
- const { fetch } = target
-
- const url = new URL('../missing.json', import.meta.url)
-
- const response = await fetch(url)
-
- expect(response.constructor).to.equal(target.Response)
-
- expect(response.status).to.equal(404)
- expect(response.statusText).to.be.empty
- expect(response.headers.has('date')).to.equal(true)
- expect(response.headers.has('content-length')).to.equal(false)
- expect(response.headers.has('last-modified')).to.equal(false)
- })
-
- it('Fetch with (file) Request', async () => {
- const { Request, fetch } = target
-
- const request = new Request(new URL('../package.json', import.meta.url))
-
- const response = await fetch(request)
-
- expect(response.constructor).to.equal(target.Response)
-
- const json = await response.json()
-
- expect(json.name).to.equal('@astrojs/webapi')
- })
-
- it('Fetch with relative file', async () => {
- const { fetch } = target
-
- const response = await fetch('package.json')
-
- const json = await response.json()
-
- expect(json.name).to.equal('@astrojs/webapi')
- })
-
it('Fetch with data', async () => {
const { fetch } = target