Public declarations named types the entry points did not export, so a caller could invoke the API but could not name its parameters or results. The affected types were ModuleOptions, Ctor, BusLike, ArgSource, GraphNodeView, GraphRouteView, StandardIssue, StandardResult, the experimental mesh types, and the adapter option types: DenoServeOptions, BunServeOptions and EdgeCtx. Export them all. The changes are additive only, and no beta.2 name moves. Bun's private BunServeShortOptions becomes the exported BunServeOptions, and the ambient Bun.serve shape is renamed so the two cannot collide. channel() had started returning TappableChannel, which leaked the internal tap symbol into its public return type. Restore the beta.2 return type, Channel<T>. ws-core still finds the tap through the symbol, not through the signature. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
168 lines
5.0 KiB
TypeScript
168 lines
5.0 KiB
TypeScript
/**
|
|
* A push-driven async stream: producers call `push`/`close`/`fail`, consumers use `for await`.
|
|
* Each `for await` gets its own independent subscription (fan-out).
|
|
*/
|
|
export interface Channel<T> extends AsyncIterable<T> {
|
|
push(value: T): void;
|
|
close(): void;
|
|
fail(err: unknown): void;
|
|
readonly closed: boolean;
|
|
}
|
|
|
|
/** Type guard: true when `x` implements the async-iterable protocol. */
|
|
export function isAsyncIterable(x: unknown): x is AsyncIterable<unknown> {
|
|
return x != null && typeof (x as { [Symbol.asyncIterator]?: unknown })[Symbol.asyncIterator] === 'function';
|
|
}
|
|
|
|
/**
|
|
* Internal: tells a source its consumer has gone, without waiting for it to finish. An async
|
|
* generator runs `return()` only once its pending `next()` settles, which for an idle source may be
|
|
* never, and a `return()` may also throw or reject. None of that may hold up or break the cleanup
|
|
* that calls this (GitHub #40). Not exported from the package.
|
|
*/
|
|
export function releaseIterator(iterator: AsyncIterator<unknown> | undefined): void {
|
|
try {
|
|
void Promise.resolve(iterator?.return?.()).catch(() => {});
|
|
} catch {
|
|
/* a return() that throws synchronously */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Internal: reads a channel synchronously, inside `push`, rather than through an async iterator
|
|
* that only drains once the event loop yields. `boundInbound` needs it to bound a WebSocket burst
|
|
* frame by frame, before anything is queued (#107). Not exported from the package; `Symbol.for`
|
|
* because each tsup entry carries its own copy of this module.
|
|
*/
|
|
export const CHANNEL_TAP: unique symbol = Symbol.for('green-tea.channel-tap');
|
|
|
|
/** What {@link CHANNEL_TAP} calls, synchronously, for every push, close and fail. */
|
|
export interface ChannelSink<T> {
|
|
push(value: T): void;
|
|
end(): void;
|
|
fail(err: unknown): void;
|
|
}
|
|
|
|
/** A channel that can be tapped; every channel built by {@link channel} is one. */
|
|
export interface TappableChannel<T> extends Channel<T> {
|
|
[CHANNEL_TAP](sink: ChannelSink<T>): () => void;
|
|
}
|
|
|
|
interface Sub<T> {
|
|
iterator: AsyncIterator<T>;
|
|
push(value: T, cap: number): void;
|
|
end(): void;
|
|
fail(err: unknown): void;
|
|
}
|
|
|
|
function newSub<T>(onReturn: () => void): Sub<T> {
|
|
const queue: T[] = [];
|
|
let pending: { resolve: (r: IteratorResult<T>) => void; reject: (e: unknown) => void } | null = null;
|
|
let ended = false;
|
|
let failure: { err: unknown } | null = null;
|
|
|
|
return {
|
|
push(value, cap) {
|
|
if (ended) return;
|
|
|
|
if (pending) {
|
|
const waiter = pending;
|
|
pending = null;
|
|
waiter.resolve({ value, done: false });
|
|
return;
|
|
}
|
|
|
|
queue.push(value);
|
|
if (queue.length > cap) queue.shift(); // drop-oldest when buffer is full
|
|
},
|
|
end() {
|
|
if (ended) return;
|
|
ended = true;
|
|
|
|
if (pending) {
|
|
const waiter = pending;
|
|
pending = null;
|
|
waiter.resolve({ value: undefined as never, done: true });
|
|
}
|
|
},
|
|
fail(err) {
|
|
if (ended) return;
|
|
ended = true;
|
|
failure = { err };
|
|
|
|
if (pending) {
|
|
const waiter = pending;
|
|
pending = null;
|
|
waiter.reject(err);
|
|
}
|
|
},
|
|
iterator: {
|
|
next(): Promise<IteratorResult<T>> {
|
|
if (queue.length) return Promise.resolve({ value: queue.shift() as T, done: false });
|
|
if (failure) return Promise.reject(failure.err);
|
|
if (ended) return Promise.resolve({ value: undefined as never, done: true });
|
|
return new Promise<IteratorResult<T>>((resolve, reject) => {
|
|
pending = { resolve, reject };
|
|
});
|
|
},
|
|
return(): Promise<IteratorResult<T>> {
|
|
ended = true;
|
|
onReturn();
|
|
return Promise.resolve({ value: undefined as never, done: true });
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Creates a fan-out {@link Channel}. Values pushed are delivered to every active subscriber.
|
|
* @param opts.buffer Per-subscriber queue cap; drops oldest when exceeded (default: unbounded).
|
|
*/
|
|
export function channel<T>(opts: { buffer?: number } = {}): Channel<T> {
|
|
const cap = opts.buffer ?? Infinity;
|
|
const subs = new Set<Omit<Sub<T>, 'iterator'>>();
|
|
let done = false;
|
|
let closedFlag = false;
|
|
|
|
// Public type stays `Channel<T>` (as in beta.2): the tap is internal plumbing, found by `ws-core`
|
|
// through the symbol rather than through this signature.
|
|
const ch: TappableChannel<T> = {
|
|
get closed() {
|
|
return closedFlag;
|
|
},
|
|
push(value) {
|
|
if (done) return;
|
|
for (const sub of subs) sub.push(value, cap);
|
|
},
|
|
close() {
|
|
if (done) return;
|
|
done = true;
|
|
closedFlag = true;
|
|
for (const sub of subs) sub.end();
|
|
subs.clear();
|
|
},
|
|
fail(err) {
|
|
if (done) return;
|
|
done = true;
|
|
closedFlag = true;
|
|
for (const sub of subs) sub.fail(err);
|
|
subs.clear();
|
|
},
|
|
[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
const sub = newSub<T>(() => subs.delete(sub));
|
|
subs.add(sub);
|
|
return sub.iterator;
|
|
},
|
|
[CHANNEL_TAP](sink) {
|
|
if (done) {
|
|
sink.end();
|
|
return () => {};
|
|
}
|
|
|
|
subs.add(sink);
|
|
return () => subs.delete(sink);
|
|
},
|
|
};
|
|
return ch;
|
|
}
|