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
56 changes: 55 additions & 1 deletion crates/execution/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5483,6 +5483,19 @@ fn polyfill_expression(request: &str) -> Option<String> {
}

fn build_builtin_module_wrapper(module_name: &str) -> String {
if matches!(
module_name,
"assert"
| "assert/strict"
| "path"
| "path/posix"
| "path/win32"
| "string_decoder"
| "url"
) {
return build_delegating_builtin_module_wrapper(module_name);
}

if module_name == "assert" || module_name == "assert/strict" {
return String::from(
r#"class AssertionError extends Error {
Expand Down Expand Up @@ -7489,6 +7502,10 @@ export default {
);
}

build_delegating_builtin_module_wrapper(module_name)
}

fn build_delegating_builtin_module_wrapper(module_name: &str) -> String {
let default_target = format!(
"globalThis._requireFrom({}, \"/\")",
serde_json::to_string(&format!("node:{module_name}"))
Expand All @@ -7510,6 +7527,29 @@ export default {

fn builtin_named_exports(module_name: &str) -> &'static [&'static str] {
match module_name {
"assert" | "assert/strict" => &[
"AssertionError",
"CallTracker",
"deepEqual",
"deepStrictEqual",
"doesNotMatch",
"doesNotReject",
"doesNotThrow",
"equal",
"fail",
"ifError",
"match",
"notDeepEqual",
"notDeepStrictEqual",
"notEqual",
"notStrictEqual",
"ok",
"partialDeepStrictEqual",
"rejects",
"strict",
"strictEqual",
"throws",
],
"async_hooks" => &[
"AsyncLocalStorage",
"AsyncResource",
Expand Down Expand Up @@ -8039,8 +8079,22 @@ fn builtin_named_exports(module_name: &str) -> &'static [&'static str] {
"rootCertificates",
],
"stream/promises" => &["finished", "pipeline"],
"string_decoder" => &["StringDecoder"],
"timers/promises" => &["scheduler", "setImmediate", "setInterval", "setTimeout"],
"url" => &["URL", "fileURLToPath", "format", "parse", "pathToFileURL"],
"url" => &[
"URL",
"URLSearchParams",
"Url",
"domainToASCII",
"domainToUnicode",
"fileURLToPath",
"format",
"parse",
"pathToFileURL",
"resolve",
"resolveObject",
"urlToHttpOptions",
],
"util" => &[
"MIMEType",
"MIMEParams",
Expand Down
3 changes: 2 additions & 1 deletion crates/native-sidecar/src/execution/javascript/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::state::DeferredRpcError;
const HTTP_LOOPBACK_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
const VM_FETCH_STREAM_CHUNK_MAX_BYTES: usize = 64 * 1024;
const VM_FETCH_STREAM_COUNT_LIMIT: usize = 256;
type VmFetchResponseHead = (u16, String, Vec<(String, String)>, VmFetchBodyMode);

fn http_loopback_request_timeout() -> Duration {
std::env::var(HTTP_LOOPBACK_REQUEST_TIMEOUT_MS_ENV)
Expand Down Expand Up @@ -272,7 +273,7 @@ fn parse_stream_response_head(
bytes: &[u8],
request_method: &str,
max_response_bytes: usize,
) -> Result<(u16, String, Vec<(String, String)>, VmFetchBodyMode), SidecarError> {
) -> Result<VmFetchResponseHead, SidecarError> {
let text = std::str::from_utf8(bytes).map_err(|error| {
SidecarError::Execution(format!(
"ERR_AGENTOS_VM_FETCH_INVALID_RESPONSE: response headers were not UTF-8: {error}"
Expand Down
26 changes: 26 additions & 0 deletions crates/native-sidecar/tests/builtin_conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3769,6 +3769,16 @@ fn stdlib_polyfill_conformance_matches_host_node() {
"stdlib-polyfills",
r#"
import { createRequire } from "node:module";
import assertDefault, * as assertModule from "node:assert";
import strictAssertDefault from "node:assert/strict";
import pathDefault, * as pathModule from "node:path";
import pathPosixDefault from "node:path/posix";
import pathWin32Default from "node:path/win32";
import stringDecoderDefault, { StringDecoder } from "node:string_decoder";
import urlDefault, {
URL as ImportedURL,
URLSearchParams as ImportedURLSearchParams,
} from "node:url";

const require = createRequire(import.meta.url);
const assert = require("node:assert");
Expand All @@ -3781,6 +3791,22 @@ const util = require("node:util");
const utilTypes = require("node:util/types");
const zlib = require("node:zlib");

assert.strictEqual(assertDefault, assert);
assert.strictEqual(assertModule.default, assert);
assert.strictEqual(strictAssertDefault, require("node:assert/strict"));
assert.strictEqual(pathDefault, path);
assert.strictEqual(pathModule.default, path);
assert.strictEqual(pathPosixDefault, require("node:path/posix"));
assert.strictEqual(pathWin32Default, require("node:path/win32"));
assert.strictEqual(stringDecoderDefault, stringDecoder);
assert.strictEqual(StringDecoder, stringDecoder.StringDecoder);
assert.strictEqual(urlDefault, require("node:url"));
assert.strictEqual(ImportedURL, require("node:url").URL);
assert.strictEqual(
ImportedURLSearchParams,
require("node:url").URLSearchParams,
);

assert.deepStrictEqual(path.normalize?.("/alpha/../beta"), "/beta");
assert.notStrictEqual(1, 2);
assert.strictEqual(typeof assert.fail, "function");
Expand Down
6 changes: 5 additions & 1 deletion packages/build-tools/bridge-src/builtins/builtin-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,12 @@ function loadBuiltinModule(request) {
const normalized = rejectRestrictedBuiltinRequest(request);
switch (normalized) {
case "assert":
case "assert/strict":
return globalThis.__secureExecBuiltinAssertModule;
case "assert/strict":
return (
globalThis.__secureExecBuiltinAssertModule?.strict ??
globalThis.__secureExecBuiltinAssertModule
);
case "async_hooks":
return builtinAsyncHooksModule;
case "buffer":
Expand Down
36 changes: 0 additions & 36 deletions packages/build-tools/bridge-src/builtins/url-resolution.ts

This file was deleted.

Loading