Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions elide/runtime/js/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,43 @@ TS_MODULES = ["%s:%s" % (TS_MODULE_PACKAGE, t) for t in [
## -------------
## Registers each Node API built-in module.
NODE_BUILTINS = [
"assert",
"assert/strict",
#"assert",
#"assert/strict",
"buffer",
"child_process",
#"child_process",
"cluster",
"console",
"crypto",
"dgram",
"diagnostics_channel",
#"diagnostics_channel",
"domain",
"dns",
"dns/promises",
"events",
#"events",
"express",
"fs",
"fs/promises",
#"fs",
#"fs/promises",
"http",
"http2",
"https",
"inspector",
"inspector/promises",
"module",
"net",
"os",
"path",
#"os",
#"path",
"path/posix",
"path/win32",
"perf_hooks",
"process",
#"process",
"querystring",
"readline",
"readline/promises",
"stream",
"stream/consumers",
"stream/promises",
#"stream/consumers",
#"stream/promises",
"stream/web",
"string_decoder",
#"string_decoder",
"test",
"timers",
"timers/promises",
Expand All @@ -115,7 +115,7 @@ NODE_BUILTINS = [
"vm",
"wasi",
"worker",
"zlib",
#"zlib",
]

## Elide Builtins
Expand Down
17 changes: 16 additions & 1 deletion elide/runtime/js/modules/stream/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,25 @@
* License for the specific language governing permissions and limitations under the License.
*/

// @ts-ignore
const { node_stream } = primordials;
const intrinsic = node_stream();

/**
* Intrinsic: Stream.
*
* Stream handling primitives and stream implementations.
*/

export {};
if (!globalThis['stream'] || !globalThis['stream']['Readable']) {
throw new Error("Cannot load `stream` module before polyfills");
}
const stream = globalThis['stream'] || {};
export const Readable = intrinsic['Readable'] || stream['Readable'];
export const Writable = intrinsic['Writable'] || stream['Writable'];
export const Transform = intrinsic['Transform'] || stream['Transform'];
export const Duplex = intrinsic['Duplex'] || stream['Duplex'];
export const pipeline = intrinsic['pipeline'] || stream['pipeline'];
export const finished = intrinsic['finished'] || stream['finished'];

export default stream;
6 changes: 5 additions & 1 deletion elide/runtime/js/polyfills/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ package(
## -- Polyfills -- ##

POLYFILLS = {
"readable-stream": ["readable-stream.mjs"],
"node-streams": ["node-streams.mjs"],
"fetch": ["fetch.js"],
# --- Disabled (Implemented Natively) -------
# "abort-controller": [],
# "buffer": ["base64.js", "ieee754.js"],
# "event": ["event-target.js"],
# "once": ["once.js"],
# "readable-stream": ["readable-stream.mjs"],
# "text-encoder": ["textencoder.js"],
}

Expand Down Expand Up @@ -49,6 +50,9 @@ esbuild(
target = JS_TARGET,
sourcemap = "external",
visibility = ["//visibility:public"],
deps = [
"@npm//readable-stream",
],
)

alias(
Expand Down
3 changes: 2 additions & 1 deletion elide/runtime/js/polyfills/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module.exports = {
target: "es2021",
treeShaking: false,
legalComments: "none",
platform: "neutral"
platform: "neutral",
mainFields: ["module", "main"],
};
27 changes: 13 additions & 14 deletions elide/runtime/js/polyfills/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class Fetch {

let getRequestPrivate; // Private field accessor

class Request {
class FetchLikeRequest {
#req;
#bodyUsed = false;

Expand Down Expand Up @@ -414,7 +414,7 @@ class Fetch {
if (this.#bodyUsed) {
throw new TypeError("Body has already been consumed");
}
return new Request(this);
return new FetchLikeRequest(this);
}

#consumeBody() {
Expand Down Expand Up @@ -443,7 +443,7 @@ class Fetch {
return asBlob(body);
}
}
defineToStringTag(Request);
defineToStringTag(FetchLikeRequest);

function makeResponse(init) {
return {
Expand All @@ -456,7 +456,7 @@ class Fetch {
};
}

class Response {
class FetchLikeResponse {
#res;
#body;
#bodyUsed = false;
Expand All @@ -469,7 +469,7 @@ class Fetch {
}

static error() {
return new Response(null, {type: 'error', status: 0, statusText: ''});
return new FetchLikeResponse(null, {type: 'error', status: 0, statusText: ''});
}

static json(body, init = {}) {
Expand All @@ -478,7 +478,7 @@ class Fetch {
if (init.headers != null) {
fillHeaders(headers, init.headers);
}
return new Response(body, {...init, headers});
return new FetchLikeResponse(body, {...init, headers});
}

static redirect(url, status) {
Expand All @@ -488,7 +488,7 @@ class Fetch {
if (!isRedirect(status)) {
throw new RangeError(`Invalid status code ${status}`);
}
return new Response(null, {status, statusText: getStatusText(status), headers: {'Location': url}});
return new FetchLikeResponse(null, {status, statusText: getStatusText(status), headers: {'Location': url}});
}

get url() {
Expand Down Expand Up @@ -525,7 +525,7 @@ class Fetch {
if (this.#bodyUsed) {
throw new TypeError("Body has already been consumed");
}
let clone = new Response(this.#body);
let clone = new FetchLikeResponse(this.#body);
clone.#res = makeResponse(this.#res);
return clone;
}
Expand Down Expand Up @@ -556,7 +556,7 @@ class Fetch {
return asBlob(body);
}
}
defineToStringTag(Response);
defineToStringTag(FetchLikeResponse);

function FetchError(message, code) {
const error = new TypeError(message);
Expand Down Expand Up @@ -597,7 +597,7 @@ class Fetch {

async function fetch(input, init={}) {
console.log("[fetch] checkpoint 2.1")
let request = new Request(input, init);
let request = new FetchLikeRequest(input, init);
let requestPrivate = getRequestPrivate(request);

let scheme = requestPrivate.uri.getScheme();
Expand Down Expand Up @@ -673,7 +673,7 @@ class Fetch {
mimeType = "text/plain;charset=US-ASCII";
}

let response = new Response(body, {
let response = new FetchLikeResponse(body, {
status: 200,
statusText: 'OK',
url: request.url,
Expand Down Expand Up @@ -872,7 +872,7 @@ class Fetch {

console.log("[fetch] checkpoint 6.1")

let response = new Response(body, {
let response = new FetchLikeResponse(body, {
status,
headers,
url: request.url,
Expand Down Expand Up @@ -957,12 +957,11 @@ class Fetch {
default: return "";
};
}
Object.entries({"fetch": fetch, "Headers": Headers, "Request": Request, "Response": Response}).forEach((entry) => {
Object.entries({"fetch": fetch}).forEach((entry) => {
const [name, fn] = entry;
Object.defineProperty(globalThis, name, {value: fn, configurable: true, writable: true, enumerable: false});
});
}
};

globalThis['Fetch'] = Fetch;
export { Fetch };
42 changes: 42 additions & 0 deletions elide/runtime/js/polyfills/node-streams.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2025 Elide Technologies, Inc.
*
* Licensed under the MIT license (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://opensource.org/license/mit/
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under the License.
*/

import {
Readable,
Writable,
Transform,
Duplex,
pipeline,
finished,
} from 'readable-stream/lib/stream.js';

function buildStreamsGlobal() {
return {
"Readable": Readable,
"Writable": Writable,
"Transform": Transform,
"Duplex": Duplex,
"pipeline": pipeline,
"finished": finished,
}
}

function initStreams(target) {
target['Readable'] = Readable;
target['Writable'] = Writable;
target['Transform'] = Transform;
target['Duplex'] = Duplex;
target['stream'] = buildStreamsGlobal();
}

initStreams(globalThis);
3 changes: 2 additions & 1 deletion elide/runtime/js/polyfills/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
* License for the specific language governing permissions and limitations under the License.
*/

import "./readable-stream.mjs";
import "./node-streams.mjs";
import "./fetch.js";

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@bazel/typescript": "4.6.2",
"@types/node": "20.14.2",
"@types/source-map-support": "0.5.10",
"esbuild": "0.21.2",
"esbuild": "0.25.1",
"google-closure-library-types": "0.1.17",
"karma": "6.4.3",
"karma-chrome-launcher": "3.2.0",
Expand All @@ -30,6 +30,11 @@
"typescript": "4.9.5"
},
"resolutions": {
"esbuild": "0.21.2"
"esbuild": "0.25.1"
},
"dependencies": {
"readable-stream": "4.5.2",
"readable-web-to-node-stream": "5.0.0",
"web-streams-polyfill": "3.3.3"
}
}
Loading
Loading