Skip to content

Commit a8d585a

Browse files
authored
Revert "Undici (#5117)"
This reverts commit 7ed3c95.
1 parent 7ed3c95 commit a8d585a

File tree

15 files changed

+71
-117
lines changed

15 files changed

+71
-117
lines changed

.changeset/slimy-ways-stare.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

documentation/docs/01-web-standards.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ export function get(event) {
4545
}
4646
```
4747

48-
### Stream APIs
49-
50-
Most of the time, your endpoints will return complete data, as in the `userAgent` example above. Sometimes, you may need to return a response that's too large to fit in memory in one go, or is delivered in chunks, and for this the platform provides [streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)[ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream), [WritableStream](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream) and [TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream).
51-
5248
### URL APIs
5349

5450
URLs are represented by the [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) interface, which includes useful properties like `origin` and `pathname` (and, in the browser, `hash`). This interface shows up in various places — `event.url` in [hooks](/docs/hooks) and [endpoints](/docs/routing#endpoints), [`$page.url`](/docs/modules#$app-stores) in [pages](/docs/routing#pages), `from` and `to` in [`beforeNavigate` and `afterNavigate`](/docs/modules#$app-navigation) and so on.

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"locate-character": "^2.0.5",
3434
"marked": "^4.0.16",
3535
"mime": "^3.0.0",
36+
"node-fetch": "^3.2.4",
3637
"port-authority": "^1.2.0",
3738
"rollup": "^2.75.3",
3839
"selfsigned": "^2.0.1",
@@ -44,7 +45,6 @@
4445
"svelte2tsx": "~0.5.10",
4546
"tiny-glob": "^0.2.9",
4647
"typescript": "^4.7.2",
47-
"undici": "^5.4.0",
4848
"uvu": "^0.5.3"
4949
},
5050
"peerDependencies": {

packages/kit/src/node/index.js

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Readable } from 'stream';
12
import * as set_cookie_parser from 'set-cookie-parser';
23

34
/** @param {import('http').IncomingMessage} req */
@@ -63,7 +64,6 @@ export async function getRequest(base, req) {
6364
delete headers[':authority'];
6465
delete headers[':scheme'];
6566
}
66-
6767
return new Request(base + req.url, {
6868
method: req.method,
6969
headers,
@@ -85,38 +85,13 @@ export async function setResponse(res, response) {
8585

8686
res.writeHead(response.status, headers);
8787

88-
if (response.body) {
89-
let cancelled = false;
90-
91-
const reader = response.body.getReader();
92-
93-
res.on('close', () => {
94-
reader.cancel();
95-
cancelled = true;
96-
});
97-
98-
const next = async () => {
99-
const { done, value } = await reader.read();
100-
101-
if (cancelled) return;
102-
103-
if (done) {
104-
res.end();
105-
return;
106-
}
107-
108-
res.write(Buffer.from(value), (error) => {
109-
if (error) {
110-
console.error('Error writing stream', error);
111-
res.end();
112-
} else {
113-
next();
114-
}
115-
});
116-
};
117-
118-
next();
88+
if (response.body instanceof Readable) {
89+
response.body.pipe(res);
11990
} else {
91+
if (response.body) {
92+
res.write(new Uint8Array(await response.arrayBuffer()));
93+
}
94+
12095
res.end();
12196
}
12297
}

packages/kit/src/node/polyfills.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { fetch, Response, Request, Headers } from 'undici';
2-
import { ReadableStream, TransformStream, WritableStream } from 'stream/web';
1+
import fetch, { Response, Request, Headers } from 'node-fetch';
32
import { webcrypto as crypto } from 'crypto';
43

54
/** @type {Record<string, any>} */
@@ -8,17 +7,13 @@ const globals = {
87
fetch,
98
Response,
109
Request,
11-
Headers,
12-
ReadableStream,
13-
TransformStream,
14-
WritableStream
10+
Headers
1511
};
1612

1713
// exported for dev/preview and node environments
1814
export function installPolyfills() {
1915
for (const name in globals) {
20-
if (name in globalThis) continue;
21-
16+
// TODO use built-in fetch once https://github.com/nodejs/undici/issues/1262 is resolved
2217
Object.defineProperty(globalThis, name, {
2318
enumerable: true,
2419
configurable: true,

packages/kit/src/runtime/server/endpoint.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const text_types = new Set([
2121
'multipart/form-data'
2222
]);
2323

24-
const bodyless_status_codes = new Set([101, 204, 205, 304]);
25-
2624
/**
2725
* Decides how the body should be parsed based on its mime type
2826
*
@@ -126,11 +124,8 @@ export async function render_endpoint(event, mod) {
126124
}
127125
}
128126

129-
return new Response(
130-
method !== 'head' && !bodyless_status_codes.has(status) ? normalized_body : undefined,
131-
{
132-
status,
133-
headers
134-
}
135-
);
127+
return new Response(method !== 'head' ? normalized_body : undefined, {
128+
status,
129+
headers
130+
});
136131
}

packages/kit/src/runtime/server/page/load_node.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,6 @@ export async function load_node({
250250
if (cookie) opts.headers.set('cookie', cookie);
251251
}
252252

253-
// we need to delete the connection header, as explained here:
254-
// https://github.com/nodejs/undici/issues/1470#issuecomment-1140798467
255-
// TODO this may be a case for being selective about which headers we let through
256-
opts.headers.delete('connection');
257-
258253
const external_request = new Request(requested, /** @type {RequestInit} */ (opts));
259254
response = await options.hooks.externalFetch.call(null, external_request);
260255
}

packages/kit/src/runtime/server/utils.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ export function is_pojo(body) {
3939
if (body) {
4040
if (body instanceof Uint8Array) return false;
4141

42-
// if body is a node Readable, throw an error
43-
// TODO remove this for 1.0
44-
if (body._readableState && typeof body.pipe === 'function') {
45-
throw new Error('Node streams are no longer supported — use a ReadableStream instead');
46-
}
47-
48-
if (body instanceof ReadableStream) return false;
42+
// body could be a node Readable, but we don't want to import
43+
// node built-ins, so we use duck typing
44+
if (body._readableState && typeof body.pipe === 'function') return false;
4945

5046
// similarly, it could be a web ReadableStream
5147
if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;

packages/kit/src/utils/http.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { test } from 'uvu';
22
import * as assert from 'uvu/assert';
33
import { to_headers } from './http.js';
4-
import { Headers } from 'undici';
5-
6-
// @ts-ignore
4+
import { Headers } from 'node-fetch';
75
globalThis.Headers = Headers;
86

97
test('handle header string value', () => {

packages/kit/test/apps/basics/src/routes/load/large-response/index.svelte renamed to packages/kit/test/apps/basics/src/routes/load/large-response.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script context="module">
22
/** @type {import('@sveltejs/kit').Load} */
3-
export async function load({ fetch }) {
4-
const res = await fetch('/load/large-response/text.txt');
3+
export async function load({ url, fetch }) {
4+
const res = await fetch(`http://localhost:${url.searchParams.get('port')}/large-response.json`);
55
const text = await res.text();
66
77
return {

0 commit comments

Comments
 (0)