@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.1-alpha-20230117084928-a052439 to 0.0.1-alpha-20230117095738-14df726
@@ -9,2 +9,4 @@ import { EventTarget } from '@whatwg-node/events'; | ||
set onabort(value: ((this: AbortSignal, ev: Event) => any) | null); | ||
abort(reason?: any): void; | ||
static timeout(milliseconds: number): PonyfillAbortSignal; | ||
} |
100
index.js
@@ -19,3 +19,3 @@ 'use strict'; | ||
constructor(reason) { | ||
super('The operation was aborted.', { | ||
super('The operation was aborted' + reason || '', { | ||
cause: reason, | ||
@@ -50,2 +50,10 @@ }); | ||
} | ||
abort(reason) { | ||
this.dispatchEvent(new CustomEvent('abort', { detail: reason })); | ||
} | ||
static timeout(milliseconds) { | ||
const signal = new PonyfillAbortSignal(); | ||
setTimeout(() => signal.abort(`Operation timed out`), milliseconds); | ||
return signal; | ||
} | ||
} | ||
@@ -308,2 +316,6 @@ | ||
} | ||
else { | ||
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`)); | ||
controller.close(); | ||
} | ||
}, | ||
@@ -546,2 +558,35 @@ }); | ||
} | ||
if ('stream' in bodyInit) { | ||
const bodyStream = bodyInit.stream(); | ||
const body = new PonyfillReadableStream(bodyStream); | ||
return { | ||
contentType: bodyInit.type, | ||
contentLength: bodyInit.size, | ||
body, | ||
}; | ||
} | ||
if (bodyInit instanceof URLSearchParams) { | ||
const contentType = 'application/x-www-form-urlencoded;charset=UTF-8'; | ||
const body = new PonyfillReadableStream(stream.Readable.from(bodyInit.toString())); | ||
return { | ||
bodyType: BodyInitType.String, | ||
contentType, | ||
contentLength: null, | ||
body, | ||
}; | ||
} | ||
if ('forEach' in bodyInit) { | ||
const formData = new PonyfillFormData(); | ||
bodyInit.forEach((value, key) => { | ||
formData.append(key, value); | ||
}); | ||
const boundary = Math.random().toString(36).substr(2); | ||
const contentType = `multipart/form-data; boundary=${boundary}`; | ||
const body = formData.stream(boundary); | ||
return { | ||
contentType, | ||
contentLength: null, | ||
body, | ||
}; | ||
} | ||
throw new Error('Unknown body type'); | ||
@@ -627,2 +672,3 @@ } | ||
constructor(input, options) { | ||
var _a; | ||
let url; | ||
@@ -654,3 +700,3 @@ let bodyInit = null; | ||
this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) || true; | ||
this.method = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.method) || 'GET'; | ||
this.method = ((_a = requestInit === null || requestInit === void 0 ? void 0 : requestInit.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'GET'; | ||
this.mode = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.mode) || 'cors'; | ||
@@ -761,5 +807,5 @@ this.redirect = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.redirect) || 'follow'; | ||
switch (protocol) { | ||
case 'http': | ||
case 'http:': | ||
return http.request; | ||
case 'https': | ||
case 'https:': | ||
return https.request; | ||
@@ -777,9 +823,35 @@ } | ||
try { | ||
const protocol = fetchRequest.url.split('://')[0]; | ||
if (protocol === 'file') { | ||
const response = getResponseForFile(fetchRequest.url); | ||
const url = new URL(fetchRequest.url, 'http://localhost'); | ||
if (url.protocol === 'data:') { | ||
const [mimeType = 'text/plain', base64FlagOrText, base64String] = url.pathname.split(','); | ||
if (base64FlagOrText === 'base64' && base64String) { | ||
const buffer = Buffer.from(base64String, 'base64'); | ||
const response = new PonyfillResponse(buffer, { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: { | ||
'content-type': mimeType, | ||
}, | ||
}); | ||
resolve(response); | ||
return; | ||
} | ||
if (base64FlagOrText) { | ||
const response = new PonyfillResponse(base64FlagOrText, { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: { | ||
'content-type': mimeType, | ||
}, | ||
}); | ||
resolve(response); | ||
return; | ||
} | ||
} | ||
if (url.protocol === 'file:') { | ||
const response = getResponseForFile(url); | ||
resolve(response); | ||
return; | ||
} | ||
const requestFn = getRequestFnForProtocol(protocol); | ||
const requestFn = getRequestFnForProtocol(url.protocol); | ||
const nodeReadable = (fetchRequest.body != null | ||
@@ -793,6 +865,12 @@ ? 'pipe' in fetchRequest.body | ||
nodeRequest.destroy(); | ||
reject(new PonyfillAbortError(event.detail)); | ||
const reason = event.detail; | ||
if (reason instanceof Error) { | ||
reject(reason); | ||
} | ||
else { | ||
reject(new PonyfillAbortError(reason)); | ||
} | ||
}; | ||
fetchRequest.signal.addEventListener('abort', abortListener); | ||
const nodeRequest = requestFn(fetchRequest.url, { | ||
const nodeRequest = requestFn(url, { | ||
// signal: fetchRequest.signal will be added when v14 reaches EOL | ||
@@ -811,3 +889,3 @@ method: fetchRequest.method, | ||
if (fetchRequest.redirect === 'follow') { | ||
const redirectedUrl = new URL(nodeResponse.headers.location, fetchRequest.url); | ||
const redirectedUrl = new URL(nodeResponse.headers.location, url); | ||
const redirectResponse$ = fetchPonyfill(redirectedUrl, info); | ||
@@ -814,0 +892,0 @@ resolve(redirectResponse$.then(redirectResponse => { |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.1-alpha-20230117084928-a052439", | ||
"version": "0.0.1-alpha-20230117095738-14df726", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
76993
2068