Skip to content

Commit

Permalink
lib: basic support for primordials in undici
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Nov 8, 2024
1 parent 37c941b commit 9f54cac
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
37 changes: 23 additions & 14 deletions deps/undici/undici.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module.exports = function ({ // pull in primordials
WebAssembly,
globalThis,
AbortController,
URL
}) {
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
Expand Down Expand Up @@ -13462,27 +13468,30 @@ var require_eventsource = __commonJS({
var { getGlobalDispatcher, setGlobalDispatcher } = require_global2();
var EnvHttpProxyAgent = require_env_http_proxy_agent();
var fetchImpl = require_fetch().fetch;
module.exports.fetch = /* @__PURE__ */ __name(function fetch(resource, init = void 0) {
var module_exports = {};
module_exports.fetch = /* @__PURE__ */ __name(function fetch(resource, init = void 0) {
return fetchImpl(resource, init).catch((err) => {
if (err && typeof err === "object") {
Error.captureStackTrace(err);
}
throw err;
});
}, "fetch");
module.exports.FormData = require_formdata().FormData;
module.exports.Headers = require_headers().Headers;
module.exports.Response = require_response().Response;
module.exports.Request = require_request2().Request;
module_exports.FormData = require_formdata().FormData;
module_exports.Headers = require_headers().Headers;
module_exports.Response = require_response().Response;
module_exports.Request = require_request2().Request;
var { CloseEvent, ErrorEvent, MessageEvent, createFastMessageEvent } = require_events();
module.exports.WebSocket = require_websocket().WebSocket;
module.exports.CloseEvent = CloseEvent;
module.exports.ErrorEvent = ErrorEvent;
module.exports.MessageEvent = MessageEvent;
module.exports.createFastMessageEvent = createFastMessageEvent;
module.exports.EventSource = require_eventsource().EventSource;
module.exports.EnvHttpProxyAgent = EnvHttpProxyAgent;
module.exports.getGlobalDispatcher = getGlobalDispatcher;
module.exports.setGlobalDispatcher = setGlobalDispatcher;
module_exports.WebSocket = require_websocket().WebSocket;
module_exports.CloseEvent = CloseEvent;
module_exports.ErrorEvent = ErrorEvent;
module_exports.MessageEvent = MessageEvent;
module_exports.createFastMessageEvent = createFastMessageEvent;
module_exports.EventSource = require_eventsource().EventSource;
module_exports.EnvHttpProxyAgent = EnvHttpProxyAgent;
module_exports.getGlobalDispatcher = getGlobalDispatcher;
module_exports.setGlobalDispatcher = setGlobalDispatcher;
/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
/*! ws. MIT License. Einar Otto Stangvik <einaros@gmail.com> */
return module_exports;
};
2 changes: 1 addition & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function get(url, options, cb) {
* @returns {object} An object containing WebSocket, CloseEvent, and MessageEvent classes.
*/
function lazyUndici() {
return undici ??= require('internal/deps/undici/undici');
return undici ??= require('internal/deps/undici/undici')(primordials);
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ObjectDefineProperty(globalThis, 'fetch', {
writable: true,
value: function fetch(input, init = undefined) { // eslint-disable-line func-name-matching
if (!fetchImpl) { // Implement lazy loading of undici module for fetch function
const undiciModule = require('internal/deps/undici/undici');
const undiciModule = require('internal/deps/undici/undici')(primordials);
fetchImpl = undiciModule.fetch;
}
return fetchImpl(input, init);
Expand All @@ -86,13 +86,13 @@ ObjectDefineProperty(globalThis, 'fetch', {
// https://fetch.spec.whatwg.org/#headers-class
// https://fetch.spec.whatwg.org/#request-class
// https://fetch.spec.whatwg.org/#response-class
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [ // TODO
'FormData', 'Headers', 'Request', 'Response', 'MessageEvent', 'CloseEvent',
]);

// https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.org/
// https://websockets.spec.whatwg.org/
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['EventSource', 'WebSocket']);
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['EventSource', 'WebSocket']); // TODO

// The WebAssembly Web API which relies on Response.
// https:// webassembly.github.io/spec/web-api/#streaming-modules
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function copyPrototype(src, dest, prefix) {

// Create copies of intrinsic objects
[
'AbortController',
'AggregateError',
'Array',
'ArrayBuffer',
Expand Down Expand Up @@ -218,9 +219,12 @@ function copyPrototype(src, dest, prefix) {
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
'URL',
'URLSearchParams',
'WeakMap',
'WeakRef',
'WeakSet',
'WebAssembly',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
const original = globalThis[name];
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/wasm_web_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {

let undici;
function lazyUndici() {
return undici ??= require('internal/deps/undici/undici');
return undici ??= require('internal/deps/undici/undici')(primordials);
}

// This is essentially an implementation of a v8::WasmStreamingCallback, except
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const messageTypes = {
// passed are known to be valid.
let fastCreateMessageEvent;
function lazyMessageEvent(type, init) {
fastCreateMessageEvent ??= require('internal/deps/undici/undici').createFastMessageEvent;
fastCreateMessageEvent ??= require('internal/deps/undici/undici')(primordials).createFastMessageEvent;
return fastCreateMessageEvent(type, init);
}

Expand Down

0 comments on commit 9f54cac

Please sign in to comment.