Skip to content

Commit

Permalink
Merge branch 'main' into memory-leak-keepAlive
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Aug 28, 2023
2 parents 72f3e1d + e156a6e commit 9064ba2
Show file tree
Hide file tree
Showing 27 changed files with 534 additions and 73 deletions.
9 changes: 9 additions & 0 deletions .changeset/align-method-to-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@web-std/fetch": patch
---

Align with [spec](https://fetch.spec.whatwg.org/#methods) for `new Request()` `method` normalization

- Only `DELETE`, `GET`, `HEAD`, `OPTIONS`, `POST`, `PUT` get automatically uppercased
- Note that `method: "patch"` will no longer be automatically uppercased
- Throw a `TypeError` for `CONNECT`, `TRACE`, and `TRACK`
5 changes: 5 additions & 0 deletions .changeset/electro-disco.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

Fix generated types to work with node ESM / NodeNext
5 changes: 5 additions & 0 deletions .changeset/gentle-fans-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

Make `Request` signal handling follow spec: https://fetch.spec.whatwg.org/#ref-for-map-exists%E2%91%A0%E2%91%A3
5 changes: 5 additions & 0 deletions .changeset/good-badgers-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

Fix `headers.entries`/`values`/`forEach` iteration for `Set-Cookie` headers
6 changes: 6 additions & 0 deletions .changeset/lemon-nails-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@web-std/form-data": patch
---

Fix submitting form data when file input is empty. Addresses https://github.com/remix-run/remix/pull/3576

5 changes: 5 additions & 0 deletions .changeset/polite-poets-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": minor
---

expose `RequestExtraOptions` to `fetch` & add `HTTPs.agent` to types
5 changes: 5 additions & 0 deletions .changeset/real-news-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/form-data": minor
---

allow passing a `form` to `FormData`. implementation taken from https://github.com/tchak/remix-router-turbo/blob/main/test/setup-test-env.ts, thank you @tchak
9 changes: 9 additions & 0 deletions .changeset/silver-sloths-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@web-std/blob": patch
"@web-std/fetch": patch
"@web-std/file": patch
"@web-std/form-data": patch
"@web-std/stream": patch
---

Move types conditional export to the top of the list to align with [the node guidance](https://nodejs.org/api/packages.html#community-conditions-definitions)
5 changes: 5 additions & 0 deletions .changeset/soft-gorillas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": feat
---

Add support for custom `credentials` value. Nothing is done with them at the moment but they pass through for the consumer of the request to access if needed.
5 changes: 5 additions & 0 deletions .changeset/tasty-peas-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

allow clone of request and responses will `null` body
4 changes: 2 additions & 2 deletions packages/blob/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"types": "./dist/src/lib.d.ts",
"exports": {
".": {
"types": "./dist/src/lib.d.ts",
"browser": "./src/lib.js",
"require": "./dist/src/lib.node.cjs",
"import": "./src/lib.node.js",
"types": "./dist/src/lib.d.ts"
"import": "./src/lib.node.js"
}
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/fetch/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Web API compatible [fetch API][] for nodejs.
#### [node-fetch][]

The reason this fork exists is because [node-fetch][] chooses to compromise
Web API compatibility and by useing nodejs native [Readable][] stream. They way
Web API compatibility and by using nodejs native [Readable][] stream. They way
they put it is:

>
Expand Down
22 changes: 12 additions & 10 deletions packages/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
"type": "module",
"exports": {
".": {
"types": "./dist/src/lib.node.d.ts",
"browser": "./src/lib.js",
"require": "./dist/lib.node.cjs",
"import": "./src/lib.node.js",
"types": "./dist/src/lib.node.d.ts"
"import": "./src/lib.node.js"
},
"./package.json": "./package.json",
"./body": {
"import": "./src/body.js",
"types": "./dist/src/body.d.ts"
"types": "./dist/src/body.d.ts",
"import": "./src/body.js"
},
"./src/request.js": {
"import": "./src/request.js",
"types": "./dist/src/request.d.ts"
"types": "./dist/src/request.d.ts",
"import": "./src/request.js"
},
"./src/response.js": {
"import": "./src/response.js",
"types": "./dist/src/response.d.ts"
"types": "./dist/src/response.d.ts",
"import": "./src/response.js"
},
"./src/headers.js": {
"import": "./src/headers.js",
"types": "./dist/src/headers.d.ts"
"types": "./dist/src/headers.d.ts",
"import": "./src/headers.js"
}
},
"files": [
Expand Down Expand Up @@ -78,6 +78,7 @@
"@types/chai-as-promised": "^7.1.5",
"@types/chai-string": "^1.4.2",
"abort-controller": "^3.0.0",
"@web-std/file": "^3.0.2",
"abortcontroller-polyfill": "^1.7.1",
"busboy": "^0.3.1",
"c8": "^7.3.0",
Expand All @@ -100,6 +101,7 @@
"@web-std/blob": "^3.0.3",
"@web-std/form-data": "^3.0.2",
"@web-std/stream": "^1.0.1",
"abort-controller": "^3.0.0",
"data-uri-to-buffer": "^3.0.1",
"mrmime": "^1.0.0",
"@web3-storage/multipart-parser": "^1.0.0"
Expand Down
7 changes: 5 additions & 2 deletions packages/fetch/src/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async function consumeBody(data) {
* Clone body given Res/Req instance
*
* @param {Body} instance Response or Request instance
* @return {ReadableStream<Uint8Array>}
* @return {ReadableStream<Uint8Array> | null}
*/
export const clone = instance => {
const {body} = instance;
Expand All @@ -284,7 +284,10 @@ export const clone = instance => {
throw new Error('cannot clone body after it is used');
}

// @ts-expect-error - could be null
if (!body) {
return null;
}

const [left, right] = body.tee();
instance[INTERNALS].body = left;
return right;
Expand Down
14 changes: 7 additions & 7 deletions packages/fetch/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const supportedSchemas = new Set(['data:', 'http:', 'https:', 'file:']);
/**
* Fetch function
*
* @param {string | URL | import('./request').default} url - Absolute url or Request instance
* @param {RequestInit} [options_] - Fetch options
* @return {Promise<import('./response').default>}
* @param {string | URL | import('./request.js').default} url - Absolute url or Request instance
* @param {RequestInit & import('./request.js').RequestExtraOptions} [options_] - Fetch options
* @return {Promise<import('./response.js').default>}
*/
async function fetch(url, options_ = {}) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -191,7 +191,7 @@ async function fetch(url, options_ = {}) {
// Note: We can not use `request.body` because send would have
// consumed it already.
body: options_.body,
signal: request.signal,
signal: signal,
size: request.size
};

Expand Down Expand Up @@ -319,9 +319,9 @@ async function fetch(url, options_ = {}) {
}

/**
*
* @param {import('http').ClientRequest} request
* @param {(error:Error) => void} errorCallback
*
* @param {import('http').ClientRequest} request
* @param {(error:Error) => void} errorCallback
*/
function fixResponseChunkedTransferBadEnding(request, errorCallback) {
const LAST_CHUNK = Buffer.from('0\r\n\r\n');
Expand Down
27 changes: 24 additions & 3 deletions packages/fetch/src/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ export default class Headers extends URLSearchParams {
*/
forEach(callback, thisArg = undefined) {
for (const name of this.keys()) {
Reflect.apply(callback, thisArg, [this.get(name), name, this]);
if (name.toLowerCase() === 'set-cookie') {
let cookies = this.getAll(name);
while (cookies.length > 0) {
Reflect.apply(callback, thisArg, [cookies.shift(), name, this])
}
} else {
Reflect.apply(callback, thisArg, [this.get(name), name, this]);
}
}
}

Expand All @@ -199,7 +206,14 @@ export default class Headers extends URLSearchParams {
*/
* values() {
for (const name of this.keys()) {
yield /** @type {string} */(this.get(name));
if (name.toLowerCase() === 'set-cookie') {
let cookies = this.getAll(name);
while (cookies.length > 0) {
yield /** @type {string} */(cookies.shift());
}
} else {
yield /** @type {string} */(this.get(name));
}
}
}

Expand All @@ -208,7 +222,14 @@ export default class Headers extends URLSearchParams {
*/
* entries() {
for (const name of this.keys()) {
yield [name, /** @type {string} */(this.get(name))];
if (name.toLowerCase() === 'set-cookie') {
let cookies = this.getAll(name);
while (cookies.length > 0) {
yield [name, /** @type {string} */(cookies.shift())];
}
} else {
yield [name, /** @type {string} */(this.get(name))];
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/fetch/src/lib.node.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as WebFetch from "./fetch.js"
import { fetch as webFetch, Headers as WebHeaders, Request as WebRequest, Response as WebResponse } from "./fetch.js"

export { ReadableStream, Blob, FormData } from './package.js';
// Electron-renderer should get the browser implementation instead of node
// Browser configuration is not enough

// Marking export as a DOM File object instead of custom class.
export const fetch = /** @type {typeof globalThis.fetch} */
(typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : WebFetch.fetch)
(typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : webFetch)

export const Headers = globalThis.Headers || WebFetch.Headers
export const Request = globalThis.Request || WebFetch.Request
export const Response = globalThis.Response || WebFetch.Response
export const Headers = globalThis.Headers || WebHeaders
export const Request = globalThis.Request || WebRequest
export const Response = globalThis.Response || WebResponse

export default fetch

0 comments on commit 9064ba2

Please sign in to comment.