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;
84 changes: 15 additions & 69 deletions crates/execution/assets/undici-shims/web-streams-global.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,23 @@
"use strict";

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;
const WebReadableStream = globalThis.ReadableStream;
const WebWritableStream = globalThis.WritableStream;
const WebTransformStream = globalThis.TransformStream;
const WebTextEncoderStream = globalThis.TextEncoderStream;
const WebTextDecoderStream = globalThis.TextDecoderStream;

for (const [name, constructor] of Object.entries({
ReadableStream: WebReadableStream,
WritableStream: WebWritableStream,
TransformStream: WebTransformStream,
TextEncoderStream: WebTextEncoderStream,
TextDecoderStream: WebTextDecoderStream,
})) {
if (typeof constructor !== "function") {
throw new Error(`${name} was not installed by the web-platform bootstrap`);
}
}

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;

if (typeof globalThis.ReadableStream === "undefined") {
globalThis.ReadableStream = WebReadableStream;
}
if (typeof globalThis.WritableStream === "undefined") {
globalThis.WritableStream = WebWritableStream;
}
if (typeof globalThis.TransformStream === "undefined") {
globalThis.TransformStream = WebTransformStream;
}
if (typeof globalThis.TextEncoderStream === "undefined") {
globalThis.TextEncoderStream = WebTextEncoderStream;
}
if (typeof globalThis.TextDecoderStream === "undefined") {
globalThis.TextDecoderStream = WebTextDecoderStream;
}

export {
WebReadableStream,
WebWritableStream,
Expand Down
Loading