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
40 changes: 40 additions & 0 deletions crates/execution/assets/undici-shims/crypto.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

const crypto = require("agentos-legacy-crypto-polyfill");

const RANDOM_INT_LIMIT = 2 ** 48;

function randomInt(min, max, callback) {
if (typeof max === "function") {
callback = max;
max = min;
min = 0;
} else if (max === undefined) {
max = min;
min = 0;
}
if (
!Number.isSafeInteger(min) ||
!Number.isSafeInteger(max) ||
min < 0 ||
max <= min ||
max - min > RANDOM_INT_LIMIT
) {
throw new RangeError("The value of max - min is out of range");
}
const range = max - min;
const unbiasedLimit = RANDOM_INT_LIMIT - (RANDOM_INT_LIMIT % range);
let value;
do {
value = crypto.randomBytes(6).readUIntBE(0, 6);
} while (value >= unbiasedLimit);
const result = min + (value % range);
if (typeof callback === "function") {
process.nextTick(callback, null, result);
return;
}
return result;
}

crypto.randomInt = randomInt;
module.exports = crypto;
56 changes: 5 additions & 51 deletions crates/execution/assets/undici-shims/web-streams-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,11 @@ import {
ReadableStream as WebReadableStream,
WritableStream as WebWritableStream,
TransformStream as WebTransformStream,
} from "web-streams-polyfill/ponyfill/es2018";

class FallbackTextEncoderStream {
constructor() {
const encoder = new globalThis.TextEncoder();
const stream = new WebTransformStream({
transform(chunk, controller) {
controller.enqueue(encoder.encode(chunk));
},
});

this.encoding = "utf-8";
this.readable = stream.readable;
this.writable = stream.writable;
}
}

class FallbackTextDecoderStream {
constructor(label = "utf-8", options = undefined) {
const decoder = new globalThis.TextDecoder(label, options);
const stream = new WebTransformStream({
transform(chunk, controller) {
const text = decoder.decode(chunk, { stream: true });
if (text.length > 0) {
controller.enqueue(text);
}
},
flush(controller) {
const text = decoder.decode();
if (text.length > 0) {
controller.enqueue(text);
}
},
});

this.encoding = decoder.encoding;
this.fatal = decoder.fatal;
this.ignoreBOM = decoder.ignoreBOM;
this.readable = stream.readable;
this.writable = stream.writable;
}
}

const WebTextEncoderStream =
typeof globalThis.TextEncoderStream === "function"
? globalThis.TextEncoderStream
: FallbackTextEncoderStream;
const WebTextDecoderStream =
typeof globalThis.TextDecoderStream === "function"
? globalThis.TextDecoderStream
: FallbackTextDecoderStream;
} from "web-streams-polyfill";
import {
TextDecoderStream as WebTextDecoderStream,
TextEncoderStream as WebTextEncoderStream,
} from "agentos-text-encoding-polyfill";

if (typeof globalThis.ReadableStream === "undefined") {
globalThis.ReadableStream = WebReadableStream;
Expand Down
57 changes: 56 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 All @@ -7525,6 +7565,7 @@ fn builtin_named_exports(module_name: &str) -> &'static [&'static str] {
"SlowBuffer",
"isAscii",
"isUtf8",
"resolveObjectURL",
],
"child_process" => &[
"ChildProcess",
Expand Down Expand Up @@ -8039,8 +8080,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
Loading