@whatwg-node/node-fetch 0.7.8-alpha-20250122230954-3105b2fac861fe650311d5dc0f4a88470234b0b5 → 0.7.8-alpha-20250124215951-ab82e441881dc42c7b236e47e5cbd62a5e2a359d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,187 +0,0 @@
1
- import { PassThrough, Readable } from 'node:stream';
2
- import { isUint8Array } from 'node:util/types';
3
- import { createBrotliDecompress, createGunzip, createInflate, createInflateRaw } from 'node:zlib';
4
- import { fetchNodeHttp } from './fetchNodeHttp.js';
5
- import { PonyfillRequest } from './Request.js';
6
- import { PonyfillResponse } from './Response.js';
7
- import { PonyfillURL } from './URL.js';
8
- import { createDeferredPromise, getHeadersObj, isNodeReadable } from './utils.js';
9
- export function createFetchUndici(getGlobalDispatcher) {
10
- return function fetchUndici(fetchRequest) {
11
- const dispatcher = fetchRequest.dispatcher || getGlobalDispatcher();
12
- if (!dispatcher) {
13
- if (process.env.DEBUG) {
14
- console.debug('[@whatwg-node/node-fetch] - native undici dispatcher not available, falling back to node:http');
15
- }
16
- return fetchNodeHttp(fetchRequest);
17
- }
18
- const deferred = createDeferredPromise();
19
- let abortListener;
20
- let passthrough;
21
- let response;
22
- let body = null;
23
- const bodyInit = fetchRequest['bodyInit'];
24
- if (bodyInit != null) {
25
- if (typeof bodyInit === 'string' ||
26
- Buffer.isBuffer(bodyInit) ||
27
- isUint8Array(bodyInit) ||
28
- bodyInit instanceof Readable) {
29
- body = bodyInit;
30
- }
31
- else if (fetchRequest.body != null) {
32
- if (isNodeReadable(fetchRequest.body?.readable)) {
33
- body = fetchRequest.body.readable;
34
- }
35
- else {
36
- body = Readable.from(fetchRequest.body);
37
- }
38
- }
39
- }
40
- function setPassthrough(contentEncoding) {
41
- switch (contentEncoding) {
42
- case 'x-gzip':
43
- case 'gzip':
44
- passthrough = createGunzip();
45
- break;
46
- case 'x-deflate':
47
- case 'deflate':
48
- passthrough = createInflate();
49
- break;
50
- case 'x-deflate-raw':
51
- case 'deflate-raw':
52
- passthrough = createInflateRaw();
53
- break;
54
- case 'br':
55
- passthrough = createBrotliDecompress();
56
- break;
57
- default:
58
- passthrough = new PassThrough();
59
- }
60
- return passthrough;
61
- }
62
- function onAbort(e) {
63
- fetchRequest['_signal']?.removeEventListener('abort', abortListener);
64
- passthrough?.destroy(e);
65
- deferred.reject(e);
66
- }
67
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
68
- const nodeHeaders = headersSerializer(fetchRequest.headers);
69
- if (nodeHeaders['accept-encoding'] == null) {
70
- nodeHeaders['accept-encoding'] = 'gzip, deflate, br';
71
- }
72
- const dispatcherReturn = dispatcher.dispatch({
73
- origin: fetchRequest.parsedUrl.origin,
74
- path: fetchRequest.parsedUrl.pathname,
75
- query: Object.fromEntries(fetchRequest.parsedUrl.searchParams),
76
- method: fetchRequest.method,
77
- headers: nodeHeaders,
78
- body,
79
- }, {
80
- onRequestStart(controller) {
81
- abortListener = function abortListener() {
82
- onAbort(fetchRequest['_signal']?.reason);
83
- controller.abort(fetchRequest['_signal']?.reason);
84
- };
85
- fetchRequest['_signal']?.addEventListener('abort', abortListener, { once: true });
86
- },
87
- onRequestUpgrade(_controller, statusCode, headers, socket) {
88
- response = new PonyfillResponse(socket, {
89
- status: statusCode,
90
- headers: headers,
91
- url: fetchRequest.url,
92
- });
93
- deferred.resolve(response);
94
- fetchRequest['_signal']?.removeEventListener('abort', abortListener);
95
- },
96
- onResponseStart(controller, statusCode, headers, statusMessage) {
97
- if (headers.location) {
98
- if (fetchRequest.redirect === 'error') {
99
- const redirectError = new Error('Redirects are not allowed');
100
- deferred.reject(redirectError);
101
- controller.resume();
102
- return;
103
- }
104
- if (fetchRequest.redirect === 'follow') {
105
- const redirectedUrl = new PonyfillURL(headers.location, fetchRequest.parsedUrl || fetchRequest.url);
106
- const redirectResponse$ = fetchUndici(new PonyfillRequest(redirectedUrl, fetchRequest));
107
- deferred.resolve(redirectResponse$.then(redirectResponse => {
108
- redirectResponse.redirected = true;
109
- return redirectResponse;
110
- }));
111
- controller.resume();
112
- return;
113
- }
114
- }
115
- passthrough = setPassthrough(headers['content-encoding']);
116
- deferred.resolve(new PonyfillResponse(passthrough, {
117
- status: statusCode,
118
- statusText: statusMessage,
119
- headers: headers,
120
- url: fetchRequest.url,
121
- }));
122
- },
123
- onResponseData(controller, chunk) {
124
- passthrough.write(chunk);
125
- if (controller.reason) {
126
- onAbort(controller.reason);
127
- }
128
- },
129
- onResponseEnd(controller, _trailers) {
130
- if (controller.reason) {
131
- onAbort(controller.reason);
132
- }
133
- else {
134
- passthrough.end();
135
- fetchRequest['_signal']?.removeEventListener('abort', abortListener);
136
- }
137
- },
138
- onResponseError(controller, error) {
139
- onAbort(error || controller.reason);
140
- },
141
- // Old Undici support
142
- onConnect(abort) {
143
- abortListener = function abortListener() {
144
- abort(fetchRequest['_signal']?.reason);
145
- onAbort(fetchRequest['_signal']?.reason);
146
- };
147
- fetchRequest['_signal']?.addEventListener('abort', abortListener, { once: true });
148
- },
149
- onError(error) {
150
- onAbort(error);
151
- },
152
- // TODO: onUpgrade
153
- onHeaders(statusCode, headersBuf, _resume, statusText) {
154
- const headers = headersBuf.map(headerBuf => {
155
- const header = headerBuf
156
- .toString('utf-8')
157
- .split(/:\s(.+)/)
158
- .slice(0, 2);
159
- if (header[0] === 'content-encoding') {
160
- const contentEncoding = header[1];
161
- setPassthrough(contentEncoding);
162
- }
163
- return header;
164
- });
165
- passthrough ||= new PassThrough();
166
- deferred.resolve(new PonyfillResponse(passthrough, {
167
- status: statusCode,
168
- statusText,
169
- headers,
170
- url: fetchRequest.url,
171
- }));
172
- return true;
173
- },
174
- onData(chunk) {
175
- return passthrough.write(chunk);
176
- },
177
- onComplete() {
178
- passthrough.end();
179
- fetchRequest['_signal']?.removeEventListener('abort', abortListener);
180
- },
181
- });
182
- if (!dispatcherReturn) {
183
- console.warn('Undici dispatcher returned false');
184
- }
185
- return deferred.promise;
186
- };
187
- }
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest } from './Request.cjs';
2
- import { PonyfillResponse } from './Response.cjs';
3
- export declare function createFetchUndici(getGlobalDispatcher: () => import('undici').Dispatcher): <TResponseJSON = any, TRequestJSON = any>(fetchRequest: PonyfillRequest<TRequestJSON>) => Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest } from './Request.js';
2
- import { PonyfillResponse } from './Response.js';
3
- export declare function createFetchUndici(getGlobalDispatcher: () => import('undici').Dispatcher): <TResponseJSON = any, TRequestJSON = any>(fetchRequest: PonyfillRequest<TRequestJSON>) => Promise<PonyfillResponse<TResponseJSON>>;