Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214/** Adapters exposing one uniform wrap-and-call surface over this package's fork and upstream `make-asynchronous`, so the same generated spec can drive both despite their different call shapes. @module */
import makeAsynchronousUpstream, { makeAsynchronousIterable as makeAsynchronousIterableUpstream,} from 'make-asynchronous';
import { makeAsynchronous, makeAsynchronousIterable,} from '@monochromatic-dev/module-make-asynchronous-fork/ts';
//region Types
/** One implementation's wrap-and-call surface. */export type AsyncAdapter = { /** Implementation name used in oracle reports. */ readonly name: string; /** Wraps a single-call function and resolves one argument tuple. @param fn - Fixture function to wrap. @param options - Runtime options record. @param args - Caller arguments forwarded to the worker. @returns Promise settling with the worker's restored result. */ call: ( fn: (...callArguments: never[]) => unknown, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ) => Promise<unknown>; /** Wraps an iterable function and drains one iteration. @param fn - Fixture generator to wrap. @param options - Runtime options record. @param args - Caller arguments forwarded with the first message. @returns Values in yield order. */ iterate: ( fn: (...callArguments: never[]) => Generator<unknown, void, unknown>, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ) => Promise<readonly unknown[]>;};
//endregion Types
//region Adapters
/** Tests whether a drained value is an async iterable. @param value - Value returned from the wrapped call. @returns Whether the value exposes `Symbol.asyncIterator`. @example ```ts isAsyncIterable(iterable); // => true ``` */function isAsyncIterable(value: unknown,): value is AsyncIterable<unknown> { return ((typeof value) === 'object') && ((value !== null)) && (Symbol.asyncIterator in value);}
/** Adapter over this package's fork: wraps through `makeAsynchronous({ fn, options })` and calls with the `{ args }` tuple shape. */export const forkAdapter: AsyncAdapter = { name: 'fork', call: async function callFork( fn: (...callArguments: never[]) => unknown, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ): Promise<unknown> { /** Wrapped function under comparison. */ const wrapped = makeAsynchronous({ fn, // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- mis-typed option records are part of the generated input space and must reach the implementation unvalidated, exactly like upstream's untyped runtime options: options as never, },); return await Reflect.apply( wrapped as (...callArguments: never[]) => unknown, undefined, [ { args: [...args,], }, ], ); }, iterate: async function iterateFork( fn: (...callArguments: never[]) => Generator<unknown, void, unknown>, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ): Promise<readonly unknown[]> { /** Wrapped iterable under comparison. */ const wrapped = makeAsynchronousIterable({ fn, // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- mis-typed option records are part of the generated input space and must reach the implementation unvalidated, exactly like upstream's untyped runtime options: options as never, },); /** Values drained in yield order. */ const values: unknown[] = []; /** Iterable drained through the `{ args }` tuple shape. */ const iterable: unknown = Reflect.apply( wrapped as (...callArguments: never[]) => unknown, undefined, [ { args: [...args,], }, ], ); if (!isAsyncIterable(iterable,)) throw new TypeError('Fork adapter did not return an async iterable',); for await (const value of iterable) values.push(value,); return values; },};
/** Adapter over upstream `make-asynchronous`: wraps through `makeAsynchronous(fn, options)` and calls with the rest-argument shape. */export const upstreamAdapter: AsyncAdapter = { name: 'upstream', call: async function callUpstream( fn: (...callArguments: never[]) => unknown, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ): Promise<unknown> { /** Wrapped function under comparison. */ const wrapped = makeAsynchronousUpstream( // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- fixture functions use the fork's narrow `never[]` signature; upstream accepts any function shape and serializes it the same way fn as (...callArguments: never[]) => unknown as never, // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- mis-typed option records are part of the generated input space and must reach upstream unvalidated, exactly like its untyped runtime options as never, ); return await Reflect.apply( wrapped as (...callArguments: never[]) => unknown, undefined, [...args,], ); }, iterate: async function iterateUpstream( fn: (...callArguments: never[]) => unknown, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- options records are possibly absent by construction; the adapters forward them unvalidated exactly like both untyped runtimes options: Record<string, unknown> | undefined, args: readonly unknown[], ): Promise<readonly unknown[]> { /** Wrapped iterable under comparison. */ const wrapped = makeAsynchronousIterableUpstream( // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- fixture generators use the fork's narrow `never[]` signature; upstream accepts any iterable-function shape and serializes it the same way fn as (...callArguments: never[]) => unknown as never, // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- mis-typed option records are part of the generated input space and must reach upstream unvalidated, exactly like its untyped runtime options as never, ); /** Values drained in yield order. */ const values: unknown[] = []; /** Iterable drained through the rest-argument shape. */ const iterable: unknown = Reflect.apply( wrapped as (...callArguments: never[]) => unknown, undefined, [...args,], ); if (!isAsyncIterable(iterable,)) throw new TypeError('Upstream adapter did not return an async iterable',); for await (const value of iterable) values.push(value,); return values; },};
//endregion Adapters