import type * as Duration from "effect/Duration"; import * as Array from "effect/Array"; import * as Cause from "effect/Cause"; import * as Channel from "effect/Channel"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Function from "effect/Function"; import * as Pull from "effect/Pull"; import * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; import type { FromInputStreamOptions } from "../Stream.ts"; import * as internalBytes from "./bytes.ts"; interface ReceivedMessage { message: string; data?: Uint8Array | undefined; } /** * Frida's `recv` is a one-shot registration that cannot be cancelled, so a * registration whose waiter was interrupted would otherwise swallow the next * message. Registrations are shared through this module level state instead: a * message that arrives with no waiter is kept for the next one, in order. */ const recvState: { registered: number; readonly waiters: Array<(message: ReceivedMessage) => void>; readonly leftovers: Array; } = { registered: 0, waiters: [], leftovers: [] }; const onRecv = (message: string, data: ArrayBuffer | null): void => { recvState.registered--; const received: ReceivedMessage = { message, data: data !== null ? new Uint8Array(data) : undefined }; const waiter = recvState.waiters.shift(); if (waiter !== undefined) waiter(received); else recvState.leftovers.push(received); }; const recvOnce: Effect.Effect = Effect.callback((resume) => { const leftover = recvState.leftovers.shift(); if (leftover !== undefined) { resume(Effect.succeed(leftover)); return; } const waiter = (message: ReceivedMessage): void => resume(Effect.succeed(message)); recvState.waiters.push(waiter); if (recvState.registered < recvState.waiters.length) { recvState.registered++; recv(onRecv); } return Effect.sync(() => { const index = recvState.waiters.indexOf(waiter); if (index !== -1) recvState.waiters.splice(index, 1); }); }); /** @internal */ export const receiveStream = ( shareOptions: | { readonly capacity: "unbounded"; readonly replay?: number | undefined; readonly idleTimeToLive?: Duration.Input | undefined; } | { readonly capacity: number; readonly strategy?: "sliding" | "dropping" | "suspend" | undefined; readonly replay?: number | undefined; readonly idleTimeToLive?: Duration.Input | undefined; } ): Effect.Effect, never, Scope.Scope> => Stream.fromEffectRepeat(recvOnce).pipe(Stream.share(shareOptions)); /** @internal */ export const defaultChunkSize = 64 * 1024; /** @internal */ export const defaultOnError = (error: unknown): Cause.UnknownError => new Cause.UnknownError(error); /** @internal */ export const fromInputStream = ( options: FromInputStreamOptions & { readonly evaluate: Function.LazyArg; readonly onError?: ((error: unknown) => E) | undefined; } ): Stream.Stream => Stream.fromChannel( Channel.fromTransform((_upstream, scope) => Effect.suspend(() => { const resource = options.evaluate(); const onError = options.onError ?? (defaultOnError as (error: unknown) => E); const chunkSize = options.chunkSize ?? defaultChunkSize; const read = Effect.flatMap( Effect.tryPromise({ try: () => resource.read(chunkSize), catch: onError }), (buffer): Pull.Pull, E> => buffer.byteLength > 0 ? Effect.succeed(Array.of(new Uint8Array(buffer))) : Cause.done() ); return Effect.as( Scope.addFinalizer( scope, Effect.promise(() => resource.close().catch(Function.constVoid)) ), read ); }) ) ); /** @internal */ export const makeUnixInputStream = ( options: FromInputStreamOptions & UnixStreamOptions & { readonly fileDescriptor: number; readonly onError?: ((error: unknown) => E) | undefined; } ): Stream.Stream => fromInputStream({ evaluate: () => new UnixInputStream(options.fileDescriptor, options), onError: options.onError, chunkSize: options.chunkSize, }); /** @internal */ export const makeWin32InputStream = ( options: FromInputStreamOptions & WindowsStreamOptions & { readonly handle: NativePointerValue; readonly onError?: ((error: unknown) => E) | undefined; } ): Stream.Stream => fromInputStream({ evaluate: () => new Win32InputStream(options.handle, options), onError: options.onError, chunkSize: options.chunkSize, }); /** * Adapts an effect `Stream` to a Frida `InputStream`. Chunks are pulled from * the stream as needed and whatever doesn't fit in a read is kept for the next * one. * * @internal */ class StreamAdapter implements InputStream { private readonly scope: Scope.Closeable; private readonly context: Context.Context; private readonly stream: Stream.Stream; private pull: Promise, E, void>> | undefined; private buffered: Uint8Array = new Uint8Array(0); constructor(context: Context.Context, stream: Stream.Stream) { this.context = context; this.stream = stream; this.scope = Scope.makeUnsafe(); } /** Builds the pull on the first read, as the stream's setup can be async, * and reuses it for the following reads. */ private makePull(): Promise, E, void>> { this.pull ??= Effect.runPromiseWith(this.context)(Scope.provide(Stream.toPull(this.stream), this.scope)); return this.pull; } /** Resolves with the next non empty chunk, or undefined when the stream is * done. */ private async next(): Promise { const pull = await this.makePull(); while (true) { const exit = await Effect.runPromiseExitWith(this.context)(pull); if (Exit.isSuccess(exit)) { const bytes = internalBytes.concat(exit.value); if (bytes.byteLength > 0) return bytes; } else if (Pull.isDoneCause(exit.cause)) { return undefined; } else { await this.close(); throw new Error("failure in StreamAdapter", { cause: Cause.squash(exit.cause) }); } } } public async close(): Promise { const exit = await Effect.runPromiseExitWith(this.context)(Scope.close(this.scope, Exit.void)); if (Exit.isFailure(exit)) { throw Cause.squash(exit.cause) as any; } } public async read(size: number): Promise { if (this.buffered.byteLength === 0) { const next = await this.next(); if (next === undefined) return new ArrayBuffer(0); this.buffered = next; } const bytes = this.buffered.slice(0, size); this.buffered = this.buffered.subarray(bytes.byteLength); return bytes.buffer; } public async readAll(size: number): Promise { const bytes = new Uint8Array(size); let offset = 0; while (offset < size) { const data = await this.read(size - offset); if (data.byteLength === 0) { const error = new Error("Premature end of stream"); (error as any).partialData = bytes.slice(0, offset).buffer; throw error; } bytes.set(new Uint8Array(data), offset); offset += data.byteLength; } return bytes.buffer; } } /** @internal */ export const toInputStream = (stream: Stream.Stream): Effect.Effect => Effect.map(Effect.context(), (context) => new StreamAdapter(context, stream)); /** @internal */ export const toInputStreamNever = (stream: Stream.Stream): InputStream => new StreamAdapter(Context.empty(), stream);