Files
electron/lib/browser/api/net-fetch.ts
Shelley Vohr 3345edd2bf chore: upgrade Node.js to v24.10.0 (#48613)
* chore: upgrade Node.js to v24.10.0

* chore: fixup crypto patch

* chore: fixup crypto test patch

* src: prepare for v8 sandboxing

https://github.com/nodejs/node/pull/58376

* esm: fix module.exports export on CJS modules

https://github.com/nodejs/node/pull/57366

* chore: fixup lazyload fs patch

* esm: Source Phase Imports for WebAssembly

https://github.com/nodejs/node/pull/56919

* module: remove --experimental-default-type

https://github.com/nodejs/node/pull/56092

* lib,src: refactor assert to load error source from memory

https://github.com/nodejs/node/pull/59751

* src: add source location to v8::TaskRunner

https://github.com/nodejs/node/pull/54077

* src: remove dependency on wrapper-descriptor-based CppHeap

https://github.com/nodejs/node/pull/54077

* src: do not use soon-to-be-deprecated V8 API

https://github.com/nodejs/node/pull/53174

* src: stop using deprecated fields of v8::FastApiCallbackOptions

https://github.com/nodejs/node/pull/54077

* test: update v8-stats test for V8 12.6

https://github.com/nodejs/node/pull/54077

* esm: unflag --experimental-wasm-modules

https://github.com/nodejs/node/pull/57038

* test: adapt assert tests to stack trace changes

https://github.com/nodejs/node/pull/58070

* src,test: unregister the isolate after disposal and before freeing

https://github.com/nodejs/node/pull/58070

* src: use cppgc to manage ContextifyContext

https://github.com/nodejs/node/pull/56522

* src: replace uses of FastApiTypedArray

https://github.com/nodejs/node/pull/58070

* module: integrate TypeScript into compile cache

https://github.com/nodejs/node/pull/56629

* deps: update ada to 3.2.7

https://github.com/nodejs/node/pull/59336

* src: make minor cleanups in encoding_binding.cc

https://github.com/nodejs/node/pull/57448

* src: switch from `Get/SetPrototype` to `Get/SetPrototypeV2`

https://github.com/nodejs/node/pull/55453

* src: use non-deprecated Get/SetPrototype methods

https://github.com/nodejs/node/pull/59671

* src: simplify string_bytes with views

https://github.com/nodejs/node/pull/54876

* src: improve utf8 string generation performance

https://github.com/nodejs/node/pull/54873

* src: use non-deprecated Utf8LengthV2() method

https://github.com/nodejs/node/pull/58070

* src: use non-deprecated WriteUtf8V2() method

https://github.com/nodejs/node/pull/58070

* src: refactor WriteUCS2 and remove flags argument

https://github.com/nodejs/node/pull/58163

* src: use String::WriteV2() in TwoByteValue

https://github.com/nodejs/node/pull/58164

* node-api: use WriteV2 in napi_get_value_string_utf16

https://github.com/nodejs/node/pull/58165

* node-api: use WriteOneByteV2 in napi_get_value_string_latin1

https://github.com/nodejs/node/pull/58325

* src: migrate WriteOneByte to WriteOneByteV2

https://github.com/nodejs/node/pull/59634

* fs: introduce dirent\.parentPath

https://github.com/nodejs/node/pull/50976

* src: avoid copy by using std::views::keys

https://github.com/nodejs/node/pull/56080

* chore: fixup patch indices

* fix: errant use of context->GetIsolate()

* fix: tweak BoringSSL compat patch for new changes

* fix: add back missing isolate dtor declaration

* fixup! esm: fix module.exports export on CJS modules

* cli: remove --no-experimental-fetch flag

https://github.com/nodejs/node/pull/52611/files

* esm: Source Phase Imports for WebAssembly

https://github.com/nodejs/node/pull/56919

* fixup! src: prepare for v8 sandboxing

* chore: bump @types/node to v24

* chore: fix const assignment in crypto test

* fix: sandbox pointer patch issues

* chore: rework source phase import patch

* src: add percentage support to --max-old-space-size

https://github.com/nodejs/node/pull/59082

* chore: fixup crypto tests

* chore: HostImportModuleWithPhaseDynamically todo

* fix: cjs esm failures

* fix: v8::Object::Wrappable issues

- b72a615754
- 490bac2496
- 4896a0dd69

* chore: remove deleted specs

* src: use v8::ExternalMemoryAccounter

https://github.com/nodejs/node/pull/58070

* fs: port SonicBoom module to fs module as FastUtf8Stream

https://github.com/nodejs/node/pull/58897

* chore: tweak sandboxed pr patch

* test: disable parallel/test-os-checked-function

* test: use WHATWG URL instead of url.parse

* fix: OPENSSL_secure_zalloc doesn't work in BoringSSL

* chore: fix accidental extra line

* 7017517: [defer-import-eval] Parse import defer syntax

https://chromium-review.googlesource.com/c/v8/v8/+/7017517
2025-10-30 19:16:48 +01:00

129 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { allowAnyProtocol } from '@electron/internal/common/api/net-client-request';
import { ClientRequestConstructorOptions, ClientRequest, IncomingMessage, Session as SessionT } from 'electron/main';
import { Readable, Writable, isReadable } from 'stream';
function createDeferredPromise<T, E extends Error = Error> (): { promise: Promise<T>; resolve: (x: T) => void; reject: (e: E) => void; } {
let res: (x: T) => void;
let rej: (e: E) => void;
const promise = new Promise<T>((resolve, reject) => {
res = resolve;
rej = reject;
});
return { promise, resolve: res!, reject: rej! };
}
export function fetchWithSession (input: RequestInfo, init: (RequestInit & {bypassCustomProtocolHandlers?: boolean}) | undefined, session: SessionT | undefined,
request: (options: ClientRequestConstructorOptions | string) => ClientRequest) {
const p = createDeferredPromise<Response>();
let req: Request;
try {
req = new Request(input, init);
} catch (e: any) {
p.reject(e);
return p.promise;
}
if (req.signal.aborted) {
// 1. Abort the fetch() call with p, request, null, and
// requestObjects signals abort reason.
const error = (req.signal as any).reason ?? new DOMException('The operation was aborted.', 'AbortError');
p.reject(error);
if (req.body != null && isReadable(req.body as unknown as NodeJS.ReadableStream)) {
req.body.cancel(error).catch((err) => {
if (err.code === 'ERR_INVALID_STATE') {
// Node bug?
return;
}
throw err;
});
}
// 2. Return p.
return p.promise;
}
let locallyAborted = false;
req.signal.addEventListener(
'abort',
() => {
// 1. Set locallyAborted to true.
locallyAborted = true;
// 2. Abort the fetch() call with p, request, responseObject,
// and requestObjects signals abort reason.
const error = (req.signal as any).reason ?? new DOMException('The operation was aborted.', 'AbortError');
p.reject(error);
if (req.body != null && isReadable(req.body as unknown as NodeJS.ReadableStream)) {
req.body.cancel(error).catch((err) => {
if (err.code === 'ERR_INVALID_STATE') {
// Node bug?
return;
}
throw err;
});
}
r.abort();
},
{ once: true }
);
const origin = req.headers.get('origin') ?? undefined;
// We can't set credentials to same-origin unless there's an origin set.
const credentials = req.credentials === 'same-origin' && !origin ? 'include' : req.credentials;
const r = request(allowAnyProtocol({
session,
method: req.method,
url: req.url,
origin,
credentials,
cache: req.cache,
referrerPolicy: req.referrerPolicy,
redirect: req.redirect
}));
(r as any)._urlLoaderOptions.bypassCustomProtocolHandlers = !!init?.bypassCustomProtocolHandlers;
// cors is the default mode, but we can't set mode=cors without an origin.
if (req.mode && (req.mode !== 'cors' || origin)) {
r.setHeader('Sec-Fetch-Mode', req.mode);
}
for (const [k, v] of req.headers) {
r.setHeader(k, v);
}
r.on('response', (resp: IncomingMessage) => {
if (locallyAborted) return;
const headers = new Headers();
for (const [k, v] of Object.entries(resp.headers)) {
headers.set(k, Array.isArray(v) ? v.join(', ') : v);
}
const nullBodyStatus = [101, 204, 205, 304];
const body = nullBodyStatus.includes(resp.statusCode) || req.method === 'HEAD' ? null : Readable.toWeb(resp as unknown as Readable) as ReadableStream;
const rResp = new Response(body, {
headers,
status: resp.statusCode,
statusText: resp.statusMessage
});
(rResp as any).__original_resp = resp;
p.resolve(rResp);
});
r.on('error', (err) => {
p.reject(err);
});
// pipeTo expects a WritableStream<Uint8Array>. Node.js' Writable.toWeb returns WritableStream<any>,
// which causes a TS structural mismatch.
const writable = Writable.toWeb(r as unknown as Writable) as unknown as WritableStream<Uint8Array>;
if (!req.body?.pipeTo(writable).then(() => r.end())) { r.end(); }
return p.promise;
}