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
41 changes: 41 additions & 0 deletions crates/native-sidecar/tests/builtin_conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3338,16 +3338,23 @@ import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const events = require("events");
const nodeEvents = require("node:events");
const util = require("node:util");

const emitter = new EventEmitter();
class DerivedEmitter extends require("events") {}
const derived = new DerivedEmitter();
const constructed = new (require("events"))();
function LegacyEmitter() {
events.call(this);
}
util.inherits(LegacyEmitter, events);
const legacy = new LegacyEmitter();
const seen = [];
const metaNew = [];
const metaRemove = [];
const constructedSeen = [];
const derivedSeen = [];
const legacySeen = [];
const warningEvents = [];

function persistent(value) {
Expand Down Expand Up @@ -3422,6 +3429,11 @@ derived.on("tick", (value) => {
});
const derivedEmitHandled = derived.emit("tick", "delta");

legacy.on("tick", (value) => {
legacySeen.push(`legacy:${value}`);
});
const legacyEmitHandled = legacy.emit("tick", "epsilon");

process.on("warning", (warning) => {
warningEvents.push({
name: warning.name,
Expand Down Expand Up @@ -3460,6 +3472,9 @@ console.log(JSON.stringify({
derivedInstanceWorks: derived instanceof EventEmitter,
derivedEmitHandled,
derivedSeen,
legacyInstanceWorks: legacy instanceof EventEmitter,
legacyEmitHandled,
legacySeen,
visibleListenersIsArray: Array.isArray(visibleListeners),
visibleRawListenersIsArray: Array.isArray(visibleRawListeners),
listenersUnwrapOnce: visibleListeners?.[0] === onceVisible,
Expand Down Expand Up @@ -3769,6 +3784,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 +3806,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
29 changes: 10 additions & 19 deletions packages/build-tools/bridge-src/builtins/builtin-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,11 @@ function ensureBuiltinEventsStdlibModule() {
typeof builtinEventsStdlibModule === "function"
? builtinEventsStdlibModule
: builtinEventsStdlibModule?.EventEmitter;
if (typeof builtinEventsConstructor === "function") {
Object.assign(
builtinEventsConstructor.prototype,
eventsModule.EventEmitter.prototype,
);
Object.assign(
eventsModule.EventEmitter,
builtinEventsStdlibModule,
eventsModule,
);
builtinEventsStdlibModule = eventsModule.EventEmitter;
builtinEventsStdlibModule.EventEmitter = builtinEventsStdlibModule;
} else {
builtinEventsStdlibModule = {
...builtinEventsStdlibModule,
...eventsModule,
};
}
builtinEventsStdlibModule =
typeof builtinEventsConstructor === "function"
? Object.assign(builtinEventsConstructor, eventsModule)
: eventsModule;
builtinEventsStdlibModule.EventEmitter = builtinEventsStdlibModule;
return builtinEventsStdlibModule;
}
var builtinPathStdlibModule = cloneStdlibModule(pathStdlibModuleNs);
Expand Down Expand Up @@ -593,8 +580,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
Loading