Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(js-sdk): detect web platform objects by shape, not by class (#1618)

Mish Ushakov1ebe925ee082
diff --git a/.changeset/lucky-dolphins-listen.md b/.changeset/lucky-dolphins-listen.md
new file mode 100644
index 000000000..bddc33910
--- /dev/null
+++ b/.changeset/lucky-dolphins-listen.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+Recognize web platform objects by what they are, not by which class minted them. Libraries replace the web globals the same way they replace `globalThis.fetch` (`@hono/node-server` installs its own `Request`, remix's `installGlobals()` swaps `Request`/`Blob`/`File`, `web-streams-polyfill` swaps `ReadableStream`, jsdom-style test environments bring their own copies), and values also cross realms — so a perfectly good `Request`, `Blob` or `ReadableStream` could fail the SDK's `instanceof` checks and take the wrong branch. This fixes: every API call crashing with `Failed to parse URL from [object Request]`; the abort signal of such a `Request` being ignored while it waited for an in-flight slot; uploads of a foreign `Blob` or `ReadableStream` — including the body of such a `Request` — silently containing the text `"[object Blob]"`/`"[object ReadableStream]"`; gzipped uploads of a foreign stream hanging; a foreign stream being buffered into memory instead of streamed; and `volume.readFile()` returning empty data.
diff --git a/packages/js-sdk/src/api/inflight.ts b/packages/js-sdk/src/api/inflight.ts
index 3dc41ba72..16401467a 100644
--- a/packages/js-sdk/src/api/inflight.ts
+++ b/packages/js-sdk/src/api/inflight.ts
@@ -1,3 +1,5 @@
+import { isRequestLike } from '../is'
+
/**
* Simple FIFO semaphore used to cap the number of in-flight requests sent
* through a fetch dispatcher.
@@ -66,8 +68,10 @@ export function limitConcurrency(
const sem = new Semaphore(max)
return (async (input, init) => {
+ // A Request the current global class disowns still carries the signal we
+ // have to honor while it waits for a slot.
const signal =
- init?.signal ?? (input instanceof Request ? input.signal : undefined)
+ init?.signal ?? (isRequestLike(input) ? input.signal : undefined)
const release = await sem.acquire(signal)
try {
return await fetcher(input, init)
diff --git a/packages/js-sdk/src/is.ts b/packages/js-sdk/src/is.ts
new file mode 100644
index 000000000..06b5d10b3
--- /dev/null
+++ b/packages/js-sdk/src/is.ts
@@ -0,0 +1,116 @@
+/**
+ * Class-agnostic checks for web platform objects.
+ *
+ * `value instanceof Blob` does not answer "is this a Blob", it answers "was
+ * this minted by *the* `Blob` class this module happens to see". In a Node
+ * process those are different questions: libraries replace the web globals the
+ * same way they replace `globalThis.fetch` — `@hono/node-server` installs its
+ * own `Request`, remix's `installGlobals()` swaps `Request`/`Blob`/`File`,
+ * `web-streams-polyfill` swaps `ReadableStream`, jsdom-based test environments
+ * bring their own copies of all of them — and values also cross realms
+ * (`node:vm`, `worker_threads`). A perfectly good Blob then fails that check —
+ * a *brand* check, in spec terms — and the SDK silently takes the wrong branch.
+ *
+ * The failure modes are not theoretical; each one is covered by a test:
+ * - a Request the current global class disowns is handed to undici verbatim and
+ * every API call dies with `Failed to parse URL from [object Request]`;
+ * - a foreign `Blob` or `ReadableStream` body is stringified by the platform,
+ * so the upload silently contains the text `[object Blob]`;
+ * - a foreign `ReadableStream` upload is buffered into memory instead of
+ * streamed, or hangs when piped through `CompressionStream`;
+ * - a foreign `Blob` response body reads back as an empty file.
+ *
+ * So ask what a value *is*, not who made it: keep `instanceof` as the fast
+ * path, then fall back to the members and `Symbol.toStringTag` the platform
+ * guarantees. Detection is only half of it — see `toBlob`/`toUploadBody` in
+ * `utils.ts`, which convert what they detect into a native equivalent before
+ * handing it to the platform.
+ */
+
+function isObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null
+}
+
+/**
+ * The value's `Symbol.toStringTag`, e.g. `'Blob'` for anything implementing the
+ * `Blob` interface. Spec'd for every web platform interface and inherited by
+ * subclasses, so it survives both realm and class swaps. Same one-liner
+ * `@sindresorhus/is` uses for the types it covers (`Blob`, `ArrayBuffer`; it has
+ * no `Request` or `ReadableStream` check, which is why this module exists).
+ */
+function platformTag(value: object): string {
+ return Object.prototype.toString.call(value).slice('[object '.length, -1)
+}
+
+/**
+ * Whether `value` should be treated as a `Request`.
+ *
+ * Duck-typed on `url` + `method` + `clone` rather than on the tag, because
+ * older `fetch` ponyfills predate `Symbol.toStringTag`; `clone` is what
+ * separates a `Request` from other `{ url, method }` carriers such as Node's
+ * `IncomingMessage`.
+ */
+export function isRequestLike(value: unknown): value is Request {
+ return (
+ value instanceof Request ||
+ (isObject(value) &&
+ typeof value.url === 'string' &&
+ typeof value.method === 'string' &&
+ typeof value.clone === 'function')
+ )
+}
+
+/**
+ * Whether `value` should be treated as a `Blob` (or a `File`, which is a
+ * `Blob`).
+ *
+ * `arrayBuffer` is the only member required beyond the tag, because reading the
+ * bytes is all the SDK ever does with a Blob it didn't make — asking for
+ * `stream` too would turn implementations that lack it into corrupted uploads
+ * for no gain.
+ */
+export function isBlobLike(value: unknown): value is Blob {
+ if (value instanceof Blob) {
+ return true
+ }
+ if (!isObject(value)) {
+ return false
+ }
+
+ const tag = platformTag(value)
+ return (
+ (tag === 'Blob' || tag === 'File') &&
+ typeof value.arrayBuffer === 'function'
+ )
+}
+
+/**
+ * Whether `value` should be treated as a `ReadableStream`.
+ *
+ * `getReader` + `tee` + `cancel` is unmistakable enough to skip the tag, which
+ * keeps this working for stream implementations that only got a
+ * `Symbol.toStringTag` in later versions.
+ */
+export function isReadableStreamLike(value: unknown): value is ReadableStream {
+ return (
+ value instanceof ReadableStream ||
+ (isObject(value) &&
+ typeof value.getReader === 'function' &&
+ typeof value.tee === 'function' &&
+ typeof value.cancel === 'function')
+ )
+}
+
+/**
+ * Whether `value` should be treated as an `ArrayBuffer`.
+ *
+ * Unlike the others this needs no conversion afterwards: the platform detects
+ * buffer sources through V8 rather than by brand, so a cross-realm
+ * `ArrayBuffer` is already accepted everywhere a native one is.
+ */
+export function isArrayBufferLike(value: unknown): value is ArrayBuffer {
+ return (
+ value instanceof ArrayBuffer ||
+ (isObject(value) && platformTag(value) === 'ArrayBuffer')
+ )
+}
diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.ts
index d68a794bf..453b0012a 100644
--- a/packages/js-sdk/src/sandbox/filesystem/index.ts
+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts
@@ -52,6 +52,7 @@ import {
InvalidArgumentError,
TemplateError,
} from '../../errors'
+import { isReadableStreamLike } from '../../is'
import { runtime, toBlob, toUploadBody } from '../../utils'
const FILESYSTEM_HTTP_ERROR_MAP: Record<number, (message: string) => Error> = {
@@ -656,7 +657,7 @@ export class Filesystem {
// applies to the whole request body). An explicit `useOctetStream` wins.
const hasStreamableData =
runtime !== 'browser' &&
- writeFiles.some((file) => file.data instanceof ReadableStream)
+ writeFiles.some((file) => isReadableStreamLike(file.data))
const useOctetStream =
((writeOpts?.useOctetStream ?? hasStreamableData) || useGzip) &&
supportsOctetStream
@@ -688,14 +689,13 @@ export class Filesystem {
const uploadResults = await Promise.all(
writeFiles.map(async (file) => {
const filePath = path ?? (file as WriteEntry).path
- const body = await toUploadBody(file.data, useGzip)
- const isStream = body instanceof ReadableStream
+ const { body, streamed } = await toUploadBody(file.data, useGzip)
// A streamed upload carries no client-side timeout: the socket-write
// "wire" isn't observable through fetch, and a stalled producer is
// the caller's own code, so a stuck streamed upload is bounded
// server-side (or via `writeOpts.signal`). Buffered uploads keep the
// normal request timeout.
- const signal = isStream
+ const signal = streamed
? writeOpts?.signal
: this.connectionConfig.getSignal(
writeOpts?.requestTimeoutMs,
@@ -715,7 +715,7 @@ export class Filesystem {
signal,
body: {},
// Streaming request bodies require half-duplex mode.
- ...(isStream && {
+ ...(streamed && {
duplex: 'half' as const,
}),
})
diff --git a/packages/js-sdk/src/undici.ts b/packages/js-sdk/src/undici.ts
index 01728470f..74a2fd102 100644
--- a/packages/js-sdk/src/undici.ts
+++ b/packages/js-sdk/src/undici.ts
@@ -1,7 +1,8 @@
import { compareVersions } from 'compare-versions'
import { limitConcurrency } from './api/inflight'
-import { dynamicImport } from './utils'
+import { isReadableStreamLike, isRequestLike } from './is'
+import { dynamicImport, toDispatchableStream } from './utils'
type UndiciRequestInit = RequestInit & {
dispatcher?: unknown
@@ -136,12 +137,21 @@ function toUndiciRequestInput(
input: RequestInfo | URL,
init?: RequestInit
): { input: RequestInfo | URL; init?: RequestInit & { duplex?: 'half' } } {
- if (!(input instanceof Request)) {
+ // Every Request has to be taken apart here, including one the current global
+ // class disowns: undici brand-checks against its own `Request`, so anything
+ // it didn't mint itself — even a native one — is coerced to a URL string and
+ // fails with `Failed to parse URL from [object Request]`.
+ if (!isRequestLike(input)) {
return { input, init }
}
const requestInit: RequestInit & { duplex?: 'half' } = {
- body: input.body,
+ // A Request from another implementation exposes that implementation's
+ // stream as its body, which undici would stringify just like the Request
+ // itself. Native bodies pass through untouched.
+ body: isReadableStreamLike(input.body)
+ ? toDispatchableStream(input.body)
+ : input.body,
cache: input.cache,
credentials: input.credentials,
headers: input.headers,
diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.ts
index ce4f61f3e..f05abeebb 100644
--- a/packages/js-sdk/src/utils.ts
+++ b/packages/js-sdk/src/utils.ts
@@ -1,5 +1,7 @@
import platform from 'platform'
+import { isBlobLike, isReadableStreamLike } from './is'
+
declare let window: any
type Runtime =
@@ -99,22 +101,80 @@ export function stripAnsi(text: string): string {
return text.replace(ansiRegex(), '')
}
+/**
+ * Adopt a stream from a polyfill, a replaced global, or another realm into the
+ * current `ReadableStream` class by pumping it through a new one. Reading
+ * through the public reader is the portable part of a stream.
+ *
+ * Needed wherever the stream is handed to another platform primitive that
+ * brand-checks it — `pipeThrough(new CompressionStream(…))` on a foreign stream
+ * never settles.
+ */
+function toNativeStream(stream: ReadableStream): ReadableStream {
+ // The `instanceof` is kept out of the `if` condition on purpose: as a type
+ // guard it would narrow the rest of the function to `never`, and a stream
+ // whose class disagrees with the type is the whole reason this exists.
+ const isNative: boolean = stream instanceof ReadableStream
+ if (isNative) {
+ return stream
+ }
+
+ const reader = stream.getReader()
+ return new ReadableStream({
+ async pull(controller) {
+ const { done, value } = await reader.read()
+ if (done) {
+ controller.close()
+ return
+ }
+ controller.enqueue(value)
+ },
+ cancel(reason) {
+ return reader.cancel(reason)
+ },
+ })
+}
+
+/**
+ * Adopt a stream only if the platform would not accept it as a request body —
+ * handed one it doesn't accept, it stringifies it to
+ * `"[object ReadableStream]"`.
+ *
+ * Two kinds are accepted: the platform's own stream class, and any async
+ * iterable. Async iterability is the half that survives a replaced global — a
+ * native stream stays async-iterable even when `globalThis.ReadableStream` is a
+ * polyfill — so re-wrapping one of those would only trade a stream the platform
+ * accepts for one it may not.
+ */
+export function toDispatchableStream(stream: ReadableStream): ReadableStream {
+ const isDispatchable: boolean =
+ stream instanceof ReadableStream || Symbol.asyncIterator in stream
+
+ return isDispatchable ? stream : toNativeStream(stream)
+}
+
/**
* Convert data to a Blob, avoiding unnecessary conversions when possible.
*/
-export function toBlob(
+export async function toBlob(
data: string | ArrayBuffer | Blob | ReadableStream
-): Blob | Promise<Blob> {
- // Already a Blob - use directly
+): Promise<Blob> {
+ // Already a native Blob - use directly
if (data instanceof Blob) {
return data
}
- // String or ArrayBuffer - create Blob
- if (typeof data === 'string' || data instanceof ArrayBuffer) {
- return new Blob([data])
+ // A Blob from another Blob class must be copied: the platform brand-checks
+ // blob bodies and would stringify this one to "[object Blob]".
+ if (isBlobLike(data)) {
+ return new Blob([await data.arrayBuffer()], { type: data.type })
}
// ReadableStream - must consume to get Blob
- return new Response(data).blob()
+ if (isReadableStreamLike(data)) {
+ return new Response(toDispatchableStream(data)).blob()
+ }
+ // String or ArrayBuffer - create Blob. A cross-realm ArrayBuffer needs no
+ // special handling: buffer sources are recognized by V8, not by brand.
+ return new Blob([data])
}
// Characters that are safe to leave unquoted in a POSIX shell, matching the
@@ -140,31 +200,44 @@ export function shellQuote(s: string): string {
}
/**
- * Prepare data for upload as a BodyInit, optionally gzip-compressed.
+ * The body to upload, plus whether it is streamed to the API instead of being
+ * buffered in memory. Callers need `streamed` to set half-duplex mode and to
+ * skip the client-side request timeout, so it is reported here rather than
+ * re-derived from the body — only this function knows the decision it made.
+ */
+export type UploadBody = {
+ body: BodyInit
+ streamed: boolean
+}
+
+/**
+ * Prepare data for upload, optionally gzip-compressed.
*
- * Outside the browser, streams (and gzip-compressed data) are returned as
- * `ReadableStream` so they can be uploaded without buffering in memory.
- * Browsers don't support streaming request bodies, so data is buffered into
- * a Blob there.
+ * Outside the browser, streams (and gzip-compressed data) are uploaded as a
+ * `ReadableStream` so they don't have to be buffered in memory. Browsers don't
+ * support streaming request bodies, so data is buffered into a Blob there.
*/
export async function toUploadBody(
data: string | ArrayBuffer | Blob | ReadableStream,
gzip?: boolean
-): Promise<BodyInit> {
+): Promise<UploadBody> {
if (gzip) {
- const stream =
- data instanceof ReadableStream
- ? data
- : data instanceof Blob
- ? data.stream()
- : new Blob([data]).stream()
+ // `toNativeStream`, not `toDispatchableStream`: `pipeThrough` below is the
+ // stricter consumer — it only accepts a stream of its own class. Everything
+ // that isn't already a stream goes through `toBlob`, whose result is always
+ // a native Blob and so always streams natively.
+ const stream = isReadableStreamLike(data)
+ ? toNativeStream(data)
+ : (await toBlob(data)).stream()
const compressed = stream.pipeThrough(new CompressionStream('gzip'))
- return runtime === 'browser' ? new Response(compressed).blob() : compressed
+ return runtime === 'browser'
+ ? { body: await new Response(compressed).blob(), streamed: false }
+ : { body: compressed, streamed: true }
}
- if (data instanceof ReadableStream && runtime !== 'browser') {
- return data
+ if (isReadableStreamLike(data) && runtime !== 'browser') {
+ return { body: toDispatchableStream(data), streamed: true }
}
- return toBlob(data)
+ return { body: await toBlob(data), streamed: false }
}
diff --git a/packages/js-sdk/src/volume/index.ts b/packages/js-sdk/src/volume/index.ts
index 8b6738971..3f4e6ad58 100644
--- a/packages/js-sdk/src/volume/index.ts
+++ b/packages/js-sdk/src/volume/index.ts
@@ -12,6 +12,7 @@ import {
setupRequestController,
wrapStreamWithConnectionCleanup,
} from '../connectionConfig'
+import { isArrayBufferLike, isBlobLike } from '../is'
import { NotFoundError, VolumeError } from '../errors'
import { toUploadBody } from '../utils'
import { VolumeFileType } from './types'
@@ -623,7 +624,7 @@ export class Volume {
// When the file is empty, `res.data` is `undefined`, so empty values are synthesized below.
if (format === 'bytes') {
- return res.data instanceof ArrayBuffer
+ return isArrayBufferLike(res.data)
? new Uint8Array(res.data)
: new Uint8Array()
}
@@ -633,7 +634,7 @@ export class Volume {
}
// format === 'blob'
- return res.data instanceof Blob ? res.data : new Blob([])
+ return isBlobLike(res.data) ? res.data : new Blob([])
}
/**
@@ -661,16 +662,15 @@ export class Volume {
})
const client = new VolumeApiClient(config)
- // `toUploadBody` returns a `ReadableStream` only when the body should be
- // streamed (non-browser stream input); otherwise it buffers into a Blob.
- const body = await toUploadBody(data)
- const isStream = body instanceof ReadableStream
+ // `toUploadBody` streams only for non-browser stream input; otherwise it
+ // buffers into a Blob.
+ const { body, streamed } = await toUploadBody(data)
// A streamed upload carries no client-side timeout: the socket-write
// "wire" isn't observable through fetch, and a stalled producer is the
// caller's own code, so a stuck streamed upload is bounded server-side (or
// via `opts.signal`). Buffered uploads keep the normal request timeout.
- const signal = isStream ? opts?.signal : config.getSignal()
+ const signal = streamed ? opts?.signal : config.getSignal()
const res = await client.api.PUT('/volumecontent/{volumeID}/file', {
params: {
@@ -692,7 +692,7 @@ export class Volume {
},
signal,
// Streaming request bodies require half-duplex mode.
- ...(isStream && { duplex: 'half' as const }),
+ ...(streamed && { duplex: 'half' as const }),
})
if (res.response.status === 404) {
diff --git a/packages/js-sdk/tests/api/inflight.test.ts b/packages/js-sdk/tests/api/inflight.test.ts
index f9ea00bdd..bd23eb34c 100644
--- a/packages/js-sdk/tests/api/inflight.test.ts
+++ b/packages/js-sdk/tests/api/inflight.test.ts
@@ -1,6 +1,7 @@
import { expect, test, vi } from 'vitest'
import { limitConcurrency } from '../../src/api/inflight'
+import { foreignRequestClasses } from '../foreignPlatformObjects'
function deferred<T>() {
let resolve!: (value: T) => void
@@ -73,3 +74,28 @@ test('limitConcurrency aborts queued requests when their signal fires', async ()
const resp = await first
expect(await resp.text()).toBe('done')
})
+
+test('limitConcurrency honors the signal of a Request the global class disowns', async () => {
+ const { MintingRequest, GlobalShimRequest } = foreignRequestClasses()
+
+ const inner = vi.fn(async () => new Response('ok')) as unknown as typeof fetch
+ const limited = limitConcurrency(inner, 1)
+
+ const controller = new AbortController()
+ controller.abort()
+ const request = new MintingRequest('https://example.com/aborted', {
+ signal: controller.signal,
+ })
+
+ vi.stubGlobal('Request', GlobalShimRequest)
+ try {
+ expect(request instanceof globalThis.Request).toBe(false)
+ await expect(limited(request)).rejects.toMatchObject({
+ name: 'AbortError',
+ })
+ } finally {
+ vi.unstubAllGlobals()
+ }
+
+ expect(inner).not.toHaveBeenCalled()
+})
diff --git a/packages/js-sdk/tests/foreignPlatformObjects.ts b/packages/js-sdk/tests/foreignPlatformObjects.ts
new file mode 100644
index 000000000..2a8c6b43e
--- /dev/null
+++ b/packages/js-sdk/tests/foreignPlatformObjects.ts
@@ -0,0 +1,117 @@
+/**
+ * Stand-ins for web platform objects that were *not* minted by the classes the
+ * SDK sees on `globalThis` — what a polyfill (`web-streams-polyfill`,
+ * `fetch-blob`), a replaced global (`@hono/node-server`, remix's
+ * `installGlobals`), a jsdom-style test environment, or another realm
+ * (`node:vm`, `worker_threads`) hands you.
+ *
+ * `ForeignBlob` and `foreignReadableStream` are separate implementations rather
+ * than subclasses on purpose: a subclass still passes `instanceof`, so it would
+ * exercise nothing. Requests are the exception — the real-world case is two
+ * sibling shim classes (see `foreignRequestClasses`), and a from-scratch
+ * `Request` would not be a usable one.
+ */
+
+/** A `Blob` from a different `Blob` implementation. */
+export class ForeignBlob {
+ private readonly bytes: Uint8Array
+
+ constructor(
+ parts: string[],
+ readonly type = ''
+ ) {
+ this.bytes = new TextEncoder().encode(parts.join(''))
+ }
+
+ get size(): number {
+ return this.bytes.byteLength
+ }
+
+ get [Symbol.toStringTag](): string {
+ return 'Blob'
+ }
+
+ async arrayBuffer(): Promise<ArrayBuffer> {
+ return this.bytes.slice().buffer as ArrayBuffer
+ }
+
+ async text(): Promise<string> {
+ return new TextDecoder().decode(this.bytes)
+ }
+
+ stream(): ReadableStream {
+ const bytes = this.bytes
+ return foreignReadableStream([bytes])
+ }
+}
+
+/**
+ * A `ReadableStream` from a different stream implementation: readable through
+ * the standard reader interface, but not a native stream.
+ *
+ * By default it is not async-iterable either, like the implementations that
+ * predate async iteration — that is the case platform APIs silently stringify
+ * to `"[object ReadableStream]"`. Pass `asyncIterable` for the other kind: the
+ * platform accepts those as a body, but `pipeThrough` still rejects them.
+ */
+export function foreignReadableStream(
+ chunks: Uint8Array[],
+ { asyncIterable = false }: { asyncIterable?: boolean } = {}
+): ReadableStream {
+ let index = 0
+ let cancelled: unknown
+
+ const stream = {
+ get [Symbol.toStringTag]() {
+ return 'ReadableStream'
+ },
+ get cancelledWith() {
+ return cancelled
+ },
+ getReader() {
+ return {
+ async read() {
+ return index < chunks.length
+ ? { done: false, value: chunks[index++] }
+ : { done: true, value: undefined }
+ },
+ async cancel(reason?: unknown) {
+ cancelled = reason
+ },
+ releaseLock() {},
+ }
+ },
+ tee() {
+ return [stream, stream]
+ },
+ async cancel(reason?: unknown) {
+ cancelled = reason
+ },
+ ...(asyncIterable && {
+ async *[Symbol.asyncIterator]() {
+ for (const chunk of chunks) {
+ yield chunk
+ }
+ },
+ }),
+ }
+
+ return stream as unknown as ReadableStream
+}
+
+/**
+ * Two sibling `Request` classes, both extending the native one. An instance of
+ * either is a fully functional `Request` that the *other* class disowns — which
+ * is what `@hono/node-server` and friends produce once a client (openapi-fetch
+ * captures `globalThis.Request` when the client is created) outlives a swap of
+ * the global.
+ */
+export function foreignRequestClasses(): {
+ MintingRequest: typeof Request
+ GlobalShimRequest: typeof Request
+} {
+ const NativeRequest = globalThis.Request
+ class MintingRequest extends NativeRequest {}
+ class GlobalShimRequest extends NativeRequest {}
+ return { MintingRequest, GlobalShimRequest }
+}
diff --git a/packages/js-sdk/tests/is.test.ts b/packages/js-sdk/tests/is.test.ts
new file mode 100644
index 000000000..f30303399
--- /dev/null
+++ b/packages/js-sdk/tests/is.test.ts
@@ -0,0 +1,85 @@
+import { expect, test, vi } from 'vitest'
+
+import {
+ isArrayBufferLike,
+ isBlobLike,
+ isReadableStreamLike,
+ isRequestLike,
+} from '../src/is'
+import {
+ ForeignBlob,
+ foreignReadableStream,
+ foreignRequestClasses,
+} from './foreignPlatformObjects'
+
+test('isRequestLike accepts a Request the current global class disowns', () => {
+ const { MintingRequest, GlobalShimRequest } = foreignRequestClasses()
+ const request = new MintingRequest('https://example.com/')
+
+ vi.stubGlobal('Request', GlobalShimRequest)
+ try {
+ // The premise: a real Request that `instanceof` rejects.
+ expect(request instanceof globalThis.Request).toBe(false)
+ expect(isRequestLike(request)).toBe(true)
+ } finally {
+ vi.unstubAllGlobals()
+ }
+})
+
+test('isRequestLike accepts a native Request and rejects other URL carriers', () => {
+ expect(isRequestLike(new Request('https://example.com/'))).toBe(true)
+
+ expect(isRequestLike('https://example.com/')).toBe(false)
+ expect(isRequestLike(new URL('https://example.com/'))).toBe(false)
+ // Node's IncomingMessage shape: a `url`/`method`/`headers` carrier that is
+ // not a Request.
+ expect(isRequestLike({ url: '/files', method: 'GET', headers: {} })).toBe(
+ false
+ )
+ expect(isRequestLike(null)).toBe(false)
+ expect(isRequestLike(undefined)).toBe(false)
+})
+
+test('isBlobLike accepts native and foreign Blobs', () => {
+ expect(isBlobLike(new Blob(['hi']))).toBe(true)
+ expect(isBlobLike(new File(['hi'], 'hi.txt'))).toBe(true)
+ expect(isBlobLike(new ForeignBlob(['hi']))).toBe(true)
+ // Readable bytes and the tag are enough: implementations without `stream()`
+ // still count.
+ expect(
+ isBlobLike({
+ [Symbol.toStringTag]: 'Blob',
+ size: 2,
+ arrayBuffer: async () => new ArrayBuffer(2),
+ })
+ ).toBe(true)
+
+ expect(isBlobLike('hi')).toBe(false)
+ expect(isBlobLike(new ArrayBuffer(2))).toBe(false)
+ expect(isBlobLike(foreignReadableStream([]))).toBe(false)
+ // The tag is what separates a Blob from anything else that can hand out bytes.
+ expect(isBlobLike({ arrayBuffer: () => {}, stream: () => {} })).toBe(false)
+ expect(isBlobLike(new Response('hi'))).toBe(false)
+})
+
+test('isReadableStreamLike accepts native and foreign streams', () => {
+ expect(isReadableStreamLike(new Blob(['hi']).stream())).toBe(true)
+ expect(isReadableStreamLike(foreignReadableStream([]))).toBe(true)
+
+ expect(isReadableStreamLike(new Blob(['hi']))).toBe(false)
+ expect(isReadableStreamLike('hi')).toBe(false)
+ expect(isReadableStreamLike({ getReader: () => {} })).toBe(false)
+})
+
+test('isArrayBufferLike accepts native and cross-realm ArrayBuffers', () => {
+ expect(isArrayBufferLike(new ArrayBuffer(2))).toBe(true)
+ // A cross-realm ArrayBuffer (`vm.runInContext('new ArrayBuffer(3)')`) keeps
+ // the tag while failing `instanceof`.
+ expect(
+ isArrayBufferLike(Object.create(ArrayBuffer.prototype) as ArrayBuffer)
+ ).toBe(true)
+
+ expect(isArrayBufferLike(new Uint8Array(2))).toBe(false)
+ expect(isArrayBufferLike(new Blob(['hi']))).toBe(false)
+ expect(isArrayBufferLike('hi')).toBe(false)
+})
diff --git a/packages/js-sdk/tests/undici.test.ts b/packages/js-sdk/tests/undici.test.ts
index bbf44966e..1d32d9c03 100644
--- a/packages/js-sdk/tests/undici.test.ts
+++ b/packages/js-sdk/tests/undici.test.ts
@@ -1,11 +1,17 @@
import { expect, test, vi } from 'vitest'
import {
+ buildDispatchedFetch,
createRuntimeFetch,
getUndiciPackageCandidates,
loadUndici,
+ type UndiciModule,
} from '../src/undici'
import { runtime } from '../src/utils'
+import {
+ foreignReadableStream,
+ foreignRequestClasses,
+} from './foreignPlatformObjects'
test.each([
['20.20.2', ['undici']],
@@ -53,6 +59,87 @@ test('a stale awaiter of a failed build does not clobber a newer successful buil
expect(build).toHaveBeenCalledTimes(2)
})
+test('takes apart a Request the current global Request class disowns', async () => {
+ const { MintingRequest, GlobalShimRequest } = foreignRequestClasses()
+
+ const seen: Array<{ input: unknown; init?: RequestInit }> = []
+ const fakeUndici = {
+ Agent: class {},
+ ProxyAgent: class {},
+ fetch: async (input: unknown, init?: RequestInit) => {
+ seen.push({ input, init })
+ return new Response('ok')
+ },
+ } as unknown as UndiciModule
+
+ const request = new MintingRequest('https://api.example.test/sandboxes', {
+ method: 'POST',
+ headers: { 'x-api-key': 'secret' },
+ })
+
+ vi.stubGlobal('Request', GlobalShimRequest)
+ try {
+ expect(request instanceof globalThis.Request).toBe(false)
+
+ const fetcher = await buildDispatchedFetch({
+ connections: 1,
+ inflightLimit: 0,
+ loadUndici: async () => fakeUndici,
+ })
+ await fetcher(request)
+ } finally {
+ vi.unstubAllGlobals()
+ }
+
+ // Handed the Request verbatim, undici coerces it to a URL string and every
+ // API call dies with `Failed to parse URL from [object Request]`.
+ expect(seen).toHaveLength(1)
+ expect(seen[0].input).toBe('https://api.example.test/sandboxes')
+ expect(seen[0].init?.method).toBe('POST')
+ expect(new Headers(seen[0].init?.headers).get('x-api-key')).toBe('secret')
+})
+
+test('adopts the body of a Request from another fetch implementation', async () => {
+ // A Request minted elsewhere exposes that implementation's stream as its
+ // body (node-fetch hands you a Node stream, a polyfill its own class), and
+ // undici stringifies those exactly like it stringifies the Request itself.
+ const seen: Array<{ input: unknown; init?: RequestInit }> = []
+ const fakeUndici = {
+ Agent: class {},
+ ProxyAgent: class {},
+ fetch: async (input: unknown, init?: RequestInit) => {
+ seen.push({ input, init })
+ return new Response('ok')
+ },
+ } as unknown as UndiciModule
+
+ const foreignRequest = {
+ url: 'https://api.example.test/files',
+ method: 'POST',
+ headers: new Headers({ 'content-type': 'application/octet-stream' }),
+ body: foreignReadableStream([
+ new TextEncoder().encode('hel'),
+ new TextEncoder().encode('lo'),
+ ]),
+ clone() {
+ return this
+ },
+ }
+
+ const fetcher = await buildDispatchedFetch({
+ connections: 1,
+ inflightLimit: 0,
+ loadUndici: async () => fakeUndici,
+ })
+ await fetcher(foreignRequest as unknown as Request)
+
+ expect(seen).toHaveLength(1)
+ expect(seen[0].input).toBe('https://api.example.test/files')
+ // Whatever arrives has to be something the platform can read; verbatim, it
+ // would have been sent as the text "[object ReadableStream]".
+ expect(await new Response(seen[0].init?.body).text()).toBe('hello')
+})
+
// loadUndici is only reached in production when the runtime is Node; other
// runtimes (Bun, Deno) use their global fetch instead.
test.skipIf(runtime !== 'node')(
@@ -65,3 +152,68 @@ test.skipIf(runtime !== 'node')(
expect(typeof undici?.Agent).toBe('function')
}
)
+
+// The end of the story the fake undici above can only imply: real undici,
+// asked to send a Request it did not mint, throws before touching the network.
+test.skipIf(runtime !== 'node')(
+ 'sends a disowned Request through the real undici fetch',
+ async () => {
+ const undici = (await loadUndici()) as UndiciModule & {
+ MockAgent: new (options: { connections?: number }) => {
+ get(origin: string): {
+ intercept(options: {
+ path: string
+ method: string
+ headers: Record<string, string>
+ }): { reply(status: number, body: string): void }
+ }
+ disableNetConnect(): void
+ assertNoPendingInterceptors(): void
+ close(): Promise<void>
+ }
+ }
+ const mockAgent = new undici.MockAgent({ connections: 1 })
+ mockAgent.disableNetConnect()
+ // An unmatched request throws, so this is the assertion: real undici has to
+ // see the method, path and headers of the Request it was handed.
+ mockAgent
+ .get('https://api.example.test')
+ .intercept({
+ path: '/sandboxes',
+ method: 'POST',
+ headers: { 'x-api-key': 'secret' },
+ })
+ .reply(201, 'created')
+
+ const { MintingRequest, GlobalShimRequest } = foreignRequestClasses()
+ const request = new MintingRequest('https://api.example.test/sandboxes', {
+ method: 'POST',
+ headers: { 'x-api-key': 'secret' },
+ })
+
+ vi.stubGlobal('Request', GlobalShimRequest)
+ try {
+ const fetcher = await buildDispatchedFetch({
+ connections: 1,
+ inflightLimit: 0,
+ loadUndici: async () => ({
+ ...undici,
+ // buildDispatchedFetch builds its own dispatcher; hand it the mock.
+ Agent: class {
+ constructor() {
+ return mockAgent
+ }
+ } as unknown as UndiciModule['Agent'],
+ }),
+ })
+
+ const response = await fetcher(request)
+ expect(response.status).toBe(201)
+ expect(await response.text()).toBe('created')
+ } finally {
+ vi.unstubAllGlobals()
+ mockAgent.assertNoPendingInterceptors()
+ await mockAgent.close()
+ }
+ }
+)
diff --git a/packages/js-sdk/tests/utils.test.ts b/packages/js-sdk/tests/utils.test.ts
index 5bebb6622..8cae88d14 100644
--- a/packages/js-sdk/tests/utils.test.ts
+++ b/packages/js-sdk/tests/utils.test.ts
@@ -1,9 +1,177 @@
-import { expect, test } from 'vitest'
+import { expect, test, vi } from 'vitest'
-import { sha256 } from '../src/utils'
+import { runtime, sha256, toBlob, toUploadBody } from '../src/utils'
+import { ForeignBlob, foreignReadableStream } from './foreignPlatformObjects'
+
+const encode = (text: string) => new TextEncoder().encode(text)
+
+// Browsers can't stream request bodies, so everything is buffered there.
+const streams = runtime !== 'browser'
+
+async function readBody(body: BodyInit): Promise<string> {
+ return await new Response(body).text()
+}
test('sha256 hashes with WebCrypto', async () => {
expect(await sha256('hello')).toBe(
'LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ='
)
})
+
+test('toBlob passes a native Blob through untouched', async () => {
+ const blob = new Blob(['hello'])
+ expect(await toBlob(blob)).toBe(blob)
+})
+
+test('toBlob copies the bytes of a Blob from another Blob class', async () => {
+ // Regression: a foreign Blob used to reach the platform unrecognized, which
+ // stringified it, so the upload contained the text "[object Blob]".
+ const blob = await toBlob(new ForeignBlob(['hello'], 'text/plain'))
+
+ expect(blob).toBeInstanceOf(Blob)
+ expect(await blob.text()).toBe('hello')
+ // Not an equality check: Bun appends a charset to the media type.
+ expect(blob.type).toMatch(/^text\/plain/)
+})
+
+test('toBlob reads a stream from another stream implementation', async () => {
+ const blob = await toBlob(
+ foreignReadableStream([encode('hel'), encode('lo')])
+ )
+
+ expect(await blob.text()).toBe('hello')
+})
+
+test('toBlob converts strings and buffers', async () => {
+ expect(await (await toBlob('hello')).text()).toBe('hello')
+ const buffer = encode('hello').buffer as ArrayBuffer
+ expect(await (await toBlob(buffer)).text()).toBe('hello')
+})
+
+test('toUploadBody buffers strings, buffers and Blobs', async () => {
+ const blob = new Blob(['hello'])
+ expect(await toUploadBody(blob)).toEqual({ body: blob, streamed: false })
+
+ const fromString = await toUploadBody('hello')
+ expect(fromString.streamed).toBe(false)
+ expect(await readBody(fromString.body)).toBe('hello')
+})
+
+test('toUploadBody streams a native stream', async () => {
+ const { body, streamed } = await toUploadBody(new Blob(['hello']).stream())
+
+ expect(streamed).toBe(streams)
+ expect(await readBody(body)).toBe('hello')
+})
+
+test('toUploadBody streams a stream from another stream implementation', async () => {
+ // Regression: a foreign stream failed the brand check, so it was buffered
+ // into memory instead of streamed — and, once detected, it still has to be
+ // adopted, or the platform stringifies it to "[object ReadableStream]".
+ const { body, streamed } = await toUploadBody(
+ foreignReadableStream([encode('hel'), encode('lo')])
+ )
+
+ expect(streamed).toBe(streams)
+ if (streamed) {
+ expect(body).toBeInstanceOf(ReadableStream)
+ }
+ expect(await readBody(body)).toBe('hello')
+})
+
+test.skipIf(!streams)(
+ 'an adopted foreign stream forwards cancellation to its source',
+ async () => {
+ const stream = foreignReadableStream([encode('hello')])
+ const { body } = await toUploadBody(stream)
+
+ await (body as ReadableStream).cancel('aborted upload')
+
+ expect(
+ (stream as unknown as { cancelledWith: unknown }).cancelledWith
+ ).toBe('aborted upload')
+ }
+)
+
+test('toUploadBody leaves an async-iterable foreign stream alone', async () => {
+ // The platform accepts any async iterable as a body, so adopting one would
+ // only add a layer.
+ const stream = foreignReadableStream([encode('hel'), encode('lo')], {
+ asyncIterable: true,
+ })
+ const { body, streamed } = await toUploadBody(stream)
+
+ expect(streamed).toBe(streams)
+ if (streamed) {
+ expect(body).toBe(stream)
+ }
+ expect(await readBody(body)).toBe('hello')
+})
+
+test('toUploadBody does not re-wrap a native stream when the global class was replaced', async () => {
+ // A polyfilled `globalThis.ReadableStream` used to make the adoption step
+ // wrap a perfectly good native stream into a polyfill instance the platform
+ // then stringifies — worse than doing nothing.
+ const nativeStream = new Blob(['hello']).stream()
+ class PolyfillStream {
+ getReader() {}
+ tee() {}
+ cancel() {}
+ }
+
+ vi.stubGlobal('ReadableStream', PolyfillStream)
+ let body: BodyInit
+ let streamed: boolean
+ try {
+ expect(nativeStream instanceof globalThis.ReadableStream).toBe(false)
+ ;({ body, streamed } = await toUploadBody(nativeStream))
+ } finally {
+ vi.unstubAllGlobals()
+ }
+
+ expect(streamed).toBe(streams)
+ if (streamed) {
+ expect(body).toBe(nativeStream)
+ }
+ expect(await readBody(body)).toBe('hello')
+})
+
+test('toUploadBody copies the bytes of a Blob from another Blob class', async () => {
+ const { body, streamed } = await toUploadBody(new ForeignBlob(['hello']))
+
+ expect(streamed).toBe(false)
+ expect(body).toBeInstanceOf(Blob)
+ expect(await readBody(body)).toBe('hello')
+})
+
+test.each([
+ ['native stream', () => new Blob(['hello']).stream()],
+ [
+ 'foreign stream',
+ () => foreignReadableStream([encode('hel'), encode('lo')]),
+ ],
+ [
+ 'async-iterable foreign stream',
+ () =>
+ foreignReadableStream([encode('hel'), encode('lo')], {
+ asyncIterable: true,
+ }),
+ ],
+ ['foreign Blob', () => new ForeignBlob(['hello'])],
+ ['string', () => 'hello'],
+])('toUploadBody gzips a %s', async (_name, makeData) => {
+ // Regression: piping a foreign stream through a native CompressionStream
+ // never settles, so gzip uploads hung for anything but a native stream.
+ // Unlike a body, `pipeThrough` is not satisfied by async iterability, so
+ // every foreign stream has to be adopted here.
+ const { body, streamed } = await toUploadBody(makeData(), true)
+
+ expect(streamed).toBe(streams)
+ const compressed = streamed
+ ? (body as ReadableStream)
+ : (body as Blob).stream()
+ const text = await readBody(
+ compressed.pipeThrough(new DecompressionStream('gzip'))
+ )
+ expect(text).toBe('hello')
+})