@orpc/shared 0.0.0-next.fd6982a → 0.0.0-next.fe5c63f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -12
- package/dist/index.d.mts +113 -16
- package/dist/index.d.ts +113 -16
- package/dist/index.mjs +318 -140
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
> [!WARNING]
|
|
2
|
-
>
|
|
3
|
-
> `@orpc/shared` is an internal dependency of oRPC packages. It does not follow semver and may change at any time without notice.
|
|
4
|
-
> Please do not use it in your project.
|
|
5
|
-
|
|
6
1
|
<div align="center">
|
|
7
2
|
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
8
3
|
</div>
|
|
@@ -35,7 +30,7 @@
|
|
|
35
30
|
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
36
31
|
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
37
32
|
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
38
|
-
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
|
|
33
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
|
|
39
34
|
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
40
35
|
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
41
36
|
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
@@ -54,14 +49,12 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
54
49
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
55
50
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
56
51
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
57
|
-
- [@orpc/
|
|
52
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
53
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
54
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
59
|
-
- [@orpc/
|
|
60
|
-
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
61
|
-
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
62
|
-
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
|
55
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
63
56
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
64
|
-
- [@orpc/
|
|
57
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
65
58
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
66
59
|
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
67
60
|
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
|
-
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
2
|
+
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest';
|
|
3
3
|
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
4
|
|
|
5
5
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
@@ -8,17 +8,87 @@ declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>):
|
|
|
8
8
|
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
9
|
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array).
|
|
13
|
+
*
|
|
14
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
15
|
+
*/
|
|
16
|
+
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>;
|
|
17
|
+
|
|
11
18
|
type AnyFunction = (...args: any[]) => any;
|
|
12
19
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
20
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
21
|
+
/**
|
|
22
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
23
|
+
*/
|
|
24
|
+
declare function defer(callback: () => void): void;
|
|
14
25
|
|
|
15
26
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
27
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
17
28
|
};
|
|
18
29
|
|
|
30
|
+
interface EventPublisherOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
33
|
+
*
|
|
34
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
35
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
36
|
+
*
|
|
37
|
+
* Set to:
|
|
38
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
39
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
40
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
41
|
+
*
|
|
42
|
+
* @default 100
|
|
43
|
+
*/
|
|
44
|
+
maxBufferedEvents?: number;
|
|
45
|
+
}
|
|
46
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
49
|
+
*/
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
}
|
|
52
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
53
|
+
#private;
|
|
54
|
+
constructor(options?: EventPublisherOptions);
|
|
55
|
+
get size(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
58
|
+
*/
|
|
59
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
60
|
+
/**
|
|
61
|
+
* Subscribes to a specific event using a callback function.
|
|
62
|
+
* Returns an unsubscribe function to remove the listener.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
67
|
+
* console.log(payload)
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* // Later
|
|
71
|
+
* unsubscribe()
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Subscribes to a specific event using an async iterator.
|
|
77
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
82
|
+
* console.log(payload)
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
87
|
+
}
|
|
88
|
+
|
|
19
89
|
declare class SequentialIdGenerator {
|
|
20
|
-
private
|
|
21
|
-
generate():
|
|
90
|
+
private index;
|
|
91
|
+
generate(): string;
|
|
22
92
|
}
|
|
23
93
|
|
|
24
94
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -72,13 +142,34 @@ declare function onFinish<T, TOptions extends {
|
|
|
72
142
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
143
|
|
|
74
144
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
-
interface
|
|
145
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
147
|
+
}
|
|
148
|
+
interface AsyncIteratorClassCleanupFn {
|
|
76
149
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
150
|
}
|
|
78
|
-
declare
|
|
151
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
152
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
153
|
+
asyncDispose: infer T;
|
|
154
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
155
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
156
|
+
#private;
|
|
157
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
158
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
159
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
160
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
161
|
+
/**
|
|
162
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
163
|
+
*/
|
|
164
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
165
|
+
[Symbol.asyncIterator](): this;
|
|
166
|
+
}
|
|
167
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
79
168
|
|
|
80
169
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
|
-
declare function stringifyJSON<T>(value: T
|
|
170
|
+
declare function stringifyJSON<T>(value: T | {
|
|
171
|
+
toJSON(): T;
|
|
172
|
+
}): undefined extends T ? undefined | string : string;
|
|
82
173
|
|
|
83
174
|
type Segment = string | number;
|
|
84
175
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -94,28 +185,34 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
94
185
|
*/
|
|
95
186
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
187
|
declare function clone<T>(value: T): T;
|
|
97
|
-
declare function get(object:
|
|
188
|
+
declare function get(object: unknown, path: readonly string[]): unknown;
|
|
98
189
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
190
|
+
declare const NullProtoObj: ({
|
|
191
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
192
|
+
});
|
|
99
193
|
|
|
100
194
|
interface AsyncIdQueueCloseOptions {
|
|
101
|
-
id?:
|
|
102
|
-
reason?:
|
|
195
|
+
id?: string;
|
|
196
|
+
reason?: unknown;
|
|
103
197
|
}
|
|
104
198
|
declare class AsyncIdQueue<T> {
|
|
105
199
|
private readonly openIds;
|
|
106
200
|
private readonly items;
|
|
107
201
|
private readonly pendingPulls;
|
|
108
202
|
get length(): number;
|
|
109
|
-
open(id:
|
|
110
|
-
isOpen(id:
|
|
111
|
-
push(id:
|
|
112
|
-
pull(id:
|
|
203
|
+
open(id: string): void;
|
|
204
|
+
isOpen(id: string): boolean;
|
|
205
|
+
push(id: string, item: T): void;
|
|
206
|
+
pull(id: string): Promise<T>;
|
|
113
207
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
114
|
-
assertOpen(id:
|
|
208
|
+
assertOpen(id: string): void;
|
|
115
209
|
}
|
|
116
210
|
|
|
211
|
+
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>;
|
|
212
|
+
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
213
|
+
|
|
117
214
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
215
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
216
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
121
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
217
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, value };
|
|
218
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
|
-
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
2
|
+
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest';
|
|
3
3
|
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
4
|
|
|
5
5
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
@@ -8,17 +8,87 @@ declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>):
|
|
|
8
8
|
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
9
|
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array).
|
|
13
|
+
*
|
|
14
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
15
|
+
*/
|
|
16
|
+
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>;
|
|
17
|
+
|
|
11
18
|
type AnyFunction = (...args: any[]) => any;
|
|
12
19
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
20
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
21
|
+
/**
|
|
22
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
23
|
+
*/
|
|
24
|
+
declare function defer(callback: () => void): void;
|
|
14
25
|
|
|
15
26
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
27
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
17
28
|
};
|
|
18
29
|
|
|
30
|
+
interface EventPublisherOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
33
|
+
*
|
|
34
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
35
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
36
|
+
*
|
|
37
|
+
* Set to:
|
|
38
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
39
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
40
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
41
|
+
*
|
|
42
|
+
* @default 100
|
|
43
|
+
*/
|
|
44
|
+
maxBufferedEvents?: number;
|
|
45
|
+
}
|
|
46
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
49
|
+
*/
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
}
|
|
52
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
53
|
+
#private;
|
|
54
|
+
constructor(options?: EventPublisherOptions);
|
|
55
|
+
get size(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
58
|
+
*/
|
|
59
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
60
|
+
/**
|
|
61
|
+
* Subscribes to a specific event using a callback function.
|
|
62
|
+
* Returns an unsubscribe function to remove the listener.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
67
|
+
* console.log(payload)
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* // Later
|
|
71
|
+
* unsubscribe()
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Subscribes to a specific event using an async iterator.
|
|
77
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
82
|
+
* console.log(payload)
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
87
|
+
}
|
|
88
|
+
|
|
19
89
|
declare class SequentialIdGenerator {
|
|
20
|
-
private
|
|
21
|
-
generate():
|
|
90
|
+
private index;
|
|
91
|
+
generate(): string;
|
|
22
92
|
}
|
|
23
93
|
|
|
24
94
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -72,13 +142,34 @@ declare function onFinish<T, TOptions extends {
|
|
|
72
142
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
143
|
|
|
74
144
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
-
interface
|
|
145
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
147
|
+
}
|
|
148
|
+
interface AsyncIteratorClassCleanupFn {
|
|
76
149
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
150
|
}
|
|
78
|
-
declare
|
|
151
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
152
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
153
|
+
asyncDispose: infer T;
|
|
154
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
155
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
156
|
+
#private;
|
|
157
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
158
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
159
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
160
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
161
|
+
/**
|
|
162
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
163
|
+
*/
|
|
164
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
165
|
+
[Symbol.asyncIterator](): this;
|
|
166
|
+
}
|
|
167
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
79
168
|
|
|
80
169
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
|
-
declare function stringifyJSON<T>(value: T
|
|
170
|
+
declare function stringifyJSON<T>(value: T | {
|
|
171
|
+
toJSON(): T;
|
|
172
|
+
}): undefined extends T ? undefined | string : string;
|
|
82
173
|
|
|
83
174
|
type Segment = string | number;
|
|
84
175
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -94,28 +185,34 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
94
185
|
*/
|
|
95
186
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
187
|
declare function clone<T>(value: T): T;
|
|
97
|
-
declare function get(object:
|
|
188
|
+
declare function get(object: unknown, path: readonly string[]): unknown;
|
|
98
189
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
190
|
+
declare const NullProtoObj: ({
|
|
191
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
192
|
+
});
|
|
99
193
|
|
|
100
194
|
interface AsyncIdQueueCloseOptions {
|
|
101
|
-
id?:
|
|
102
|
-
reason?:
|
|
195
|
+
id?: string;
|
|
196
|
+
reason?: unknown;
|
|
103
197
|
}
|
|
104
198
|
declare class AsyncIdQueue<T> {
|
|
105
199
|
private readonly openIds;
|
|
106
200
|
private readonly items;
|
|
107
201
|
private readonly pendingPulls;
|
|
108
202
|
get length(): number;
|
|
109
|
-
open(id:
|
|
110
|
-
isOpen(id:
|
|
111
|
-
push(id:
|
|
112
|
-
pull(id:
|
|
203
|
+
open(id: string): void;
|
|
204
|
+
isOpen(id: string): boolean;
|
|
205
|
+
push(id: string, item: T): void;
|
|
206
|
+
pull(id: string): Promise<T>;
|
|
113
207
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
114
|
-
assertOpen(id:
|
|
208
|
+
assertOpen(id: string): void;
|
|
115
209
|
}
|
|
116
210
|
|
|
211
|
+
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>;
|
|
212
|
+
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
213
|
+
|
|
117
214
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
215
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
216
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
121
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
217
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, value };
|
|
218
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -12,6 +12,13 @@ function splitInHalf(arr) {
|
|
|
12
12
|
return [arr.slice(0, half), arr.slice(half)];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
function readAsBuffer(source) {
|
|
16
|
+
if (typeof source.bytes === "function") {
|
|
17
|
+
return source.bytes();
|
|
18
|
+
}
|
|
19
|
+
return source.arrayBuffer();
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
function once(fn) {
|
|
16
23
|
let cached;
|
|
17
24
|
return () => {
|
|
@@ -32,15 +39,291 @@ function sequential(fn) {
|
|
|
32
39
|
});
|
|
33
40
|
};
|
|
34
41
|
}
|
|
42
|
+
function defer(callback) {
|
|
43
|
+
if (typeof setTimeout === "function") {
|
|
44
|
+
setTimeout(callback, 0);
|
|
45
|
+
} else {
|
|
46
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class AsyncIdQueue {
|
|
51
|
+
openIds = /* @__PURE__ */ new Set();
|
|
52
|
+
items = /* @__PURE__ */ new Map();
|
|
53
|
+
pendingPulls = /* @__PURE__ */ new Map();
|
|
54
|
+
get length() {
|
|
55
|
+
return this.openIds.size;
|
|
56
|
+
}
|
|
57
|
+
open(id) {
|
|
58
|
+
this.openIds.add(id);
|
|
59
|
+
}
|
|
60
|
+
isOpen(id) {
|
|
61
|
+
return this.openIds.has(id);
|
|
62
|
+
}
|
|
63
|
+
push(id, item) {
|
|
64
|
+
this.assertOpen(id);
|
|
65
|
+
const pending = this.pendingPulls.get(id);
|
|
66
|
+
if (pending?.length) {
|
|
67
|
+
pending.shift()[0](item);
|
|
68
|
+
if (pending.length === 0) {
|
|
69
|
+
this.pendingPulls.delete(id);
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
const items = this.items.get(id);
|
|
73
|
+
if (items) {
|
|
74
|
+
items.push(item);
|
|
75
|
+
} else {
|
|
76
|
+
this.items.set(id, [item]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async pull(id) {
|
|
81
|
+
this.assertOpen(id);
|
|
82
|
+
const items = this.items.get(id);
|
|
83
|
+
if (items?.length) {
|
|
84
|
+
const item = items.shift();
|
|
85
|
+
if (items.length === 0) {
|
|
86
|
+
this.items.delete(id);
|
|
87
|
+
}
|
|
88
|
+
return item;
|
|
89
|
+
}
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
const waitingPulls = this.pendingPulls.get(id);
|
|
92
|
+
const pending = [resolve, reject];
|
|
93
|
+
if (waitingPulls) {
|
|
94
|
+
waitingPulls.push(pending);
|
|
95
|
+
} else {
|
|
96
|
+
this.pendingPulls.set(id, [pending]);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
close({ id, reason } = {}) {
|
|
101
|
+
if (id === void 0) {
|
|
102
|
+
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
103
|
+
pendingPulls.forEach(([, reject]) => {
|
|
104
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
this.pendingPulls.clear();
|
|
108
|
+
this.openIds.clear();
|
|
109
|
+
this.items.clear();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
113
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
114
|
+
});
|
|
115
|
+
this.pendingPulls.delete(id);
|
|
116
|
+
this.openIds.delete(id);
|
|
117
|
+
this.items.delete(id);
|
|
118
|
+
}
|
|
119
|
+
assertOpen(id) {
|
|
120
|
+
if (!this.isOpen(id)) {
|
|
121
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isAsyncIteratorObject(maybe) {
|
|
127
|
+
if (!maybe || typeof maybe !== "object") {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return "next" in maybe && typeof maybe.next === "function" && Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
131
|
+
}
|
|
132
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
133
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
134
|
+
class AsyncIteratorClass {
|
|
135
|
+
#isDone = false;
|
|
136
|
+
#isExecuteComplete = false;
|
|
137
|
+
#cleanup;
|
|
138
|
+
#next;
|
|
139
|
+
constructor(next, cleanup) {
|
|
140
|
+
this.#cleanup = cleanup;
|
|
141
|
+
this.#next = sequential(async () => {
|
|
142
|
+
if (this.#isDone) {
|
|
143
|
+
return { done: true, value: void 0 };
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const result = await next();
|
|
147
|
+
if (result.done) {
|
|
148
|
+
this.#isDone = true;
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
} catch (err) {
|
|
152
|
+
this.#isDone = true;
|
|
153
|
+
throw err;
|
|
154
|
+
} finally {
|
|
155
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
156
|
+
this.#isExecuteComplete = true;
|
|
157
|
+
await this.#cleanup("next");
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
next() {
|
|
163
|
+
return this.#next();
|
|
164
|
+
}
|
|
165
|
+
async return(value) {
|
|
166
|
+
this.#isDone = true;
|
|
167
|
+
if (!this.#isExecuteComplete) {
|
|
168
|
+
this.#isExecuteComplete = true;
|
|
169
|
+
await this.#cleanup("return");
|
|
170
|
+
}
|
|
171
|
+
return { done: true, value };
|
|
172
|
+
}
|
|
173
|
+
async throw(err) {
|
|
174
|
+
this.#isDone = true;
|
|
175
|
+
if (!this.#isExecuteComplete) {
|
|
176
|
+
this.#isExecuteComplete = true;
|
|
177
|
+
await this.#cleanup("throw");
|
|
178
|
+
}
|
|
179
|
+
throw err;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
183
|
+
*/
|
|
184
|
+
async [asyncDisposeSymbol]() {
|
|
185
|
+
this.#isDone = true;
|
|
186
|
+
if (!this.#isExecuteComplete) {
|
|
187
|
+
this.#isExecuteComplete = true;
|
|
188
|
+
await this.#cleanup("dispose");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
[Symbol.asyncIterator]() {
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function replicateAsyncIterator(source, count) {
|
|
196
|
+
const queue = new AsyncIdQueue();
|
|
197
|
+
const replicated = [];
|
|
198
|
+
let error;
|
|
199
|
+
const start = once(async () => {
|
|
200
|
+
try {
|
|
201
|
+
while (true) {
|
|
202
|
+
const item = await source.next();
|
|
203
|
+
for (let id = 0; id < count; id++) {
|
|
204
|
+
if (queue.isOpen(id.toString())) {
|
|
205
|
+
queue.push(id.toString(), item);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (item.done) {
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} catch (e) {
|
|
213
|
+
error = { value: e };
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
for (let id = 0; id < count; id++) {
|
|
217
|
+
queue.open(id.toString());
|
|
218
|
+
replicated.push(new AsyncIteratorClass(
|
|
219
|
+
() => {
|
|
220
|
+
start();
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
queue.pull(id.toString()).then(resolve).catch(reject);
|
|
223
|
+
defer(() => {
|
|
224
|
+
if (error) {
|
|
225
|
+
reject(error.value);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
async (reason) => {
|
|
231
|
+
queue.close({ id: id.toString() });
|
|
232
|
+
if (reason !== "next") {
|
|
233
|
+
if (replicated.every((_, id2) => !queue.isOpen(id2.toString()))) {
|
|
234
|
+
await source?.return?.();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
));
|
|
239
|
+
}
|
|
240
|
+
return replicated;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
class EventPublisher {
|
|
244
|
+
#listenersMap = /* @__PURE__ */ new Map();
|
|
245
|
+
#maxBufferedEvents;
|
|
246
|
+
constructor(options = {}) {
|
|
247
|
+
this.#maxBufferedEvents = options.maxBufferedEvents ?? 100;
|
|
248
|
+
}
|
|
249
|
+
get size() {
|
|
250
|
+
return this.#listenersMap.size;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
254
|
+
*/
|
|
255
|
+
publish(event, payload) {
|
|
256
|
+
const listeners = this.#listenersMap.get(event);
|
|
257
|
+
if (!listeners) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
for (const listener of listeners) {
|
|
261
|
+
listener(payload);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
subscribe(event, listenerOrOptions) {
|
|
265
|
+
if (typeof listenerOrOptions === "function") {
|
|
266
|
+
let listeners = this.#listenersMap.get(event);
|
|
267
|
+
if (!listeners) {
|
|
268
|
+
this.#listenersMap.set(event, listeners = /* @__PURE__ */ new Set());
|
|
269
|
+
}
|
|
270
|
+
listeners.add(listenerOrOptions);
|
|
271
|
+
return () => {
|
|
272
|
+
listeners.delete(listenerOrOptions);
|
|
273
|
+
if (listeners.size === 0) {
|
|
274
|
+
this.#listenersMap.delete(event);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
const signal = listenerOrOptions?.signal;
|
|
279
|
+
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
280
|
+
signal?.throwIfAborted();
|
|
281
|
+
const bufferedEvents = [];
|
|
282
|
+
const pullResolvers = [];
|
|
283
|
+
const unsubscribe = this.subscribe(event, (payload) => {
|
|
284
|
+
const resolver = pullResolvers.shift();
|
|
285
|
+
if (resolver) {
|
|
286
|
+
resolver[0]({ done: false, value: payload });
|
|
287
|
+
} else {
|
|
288
|
+
bufferedEvents.push(payload);
|
|
289
|
+
if (bufferedEvents.length > maxBufferedEvents) {
|
|
290
|
+
bufferedEvents.shift();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
const abortListener = (event2) => {
|
|
295
|
+
unsubscribe();
|
|
296
|
+
pullResolvers.forEach((resolver) => resolver[1](event2.target.reason));
|
|
297
|
+
pullResolvers.length = 0;
|
|
298
|
+
bufferedEvents.length = 0;
|
|
299
|
+
};
|
|
300
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
301
|
+
return new AsyncIteratorClass(async () => {
|
|
302
|
+
if (signal?.aborted) {
|
|
303
|
+
throw signal.reason;
|
|
304
|
+
}
|
|
305
|
+
if (bufferedEvents.length > 0) {
|
|
306
|
+
return { done: false, value: bufferedEvents.shift() };
|
|
307
|
+
}
|
|
308
|
+
return new Promise((resolve, reject) => {
|
|
309
|
+
pullResolvers.push([resolve, reject]);
|
|
310
|
+
});
|
|
311
|
+
}, async () => {
|
|
312
|
+
unsubscribe();
|
|
313
|
+
signal?.removeEventListener("abort", abortListener);
|
|
314
|
+
pullResolvers.forEach((resolver) => resolver[0]({ done: true, value: void 0 }));
|
|
315
|
+
pullResolvers.length = 0;
|
|
316
|
+
bufferedEvents.length = 0;
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
35
320
|
|
|
36
321
|
class SequentialIdGenerator {
|
|
37
|
-
|
|
322
|
+
index = BigInt(0);
|
|
38
323
|
generate() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
return this.nextId++;
|
|
324
|
+
const id = this.index.toString(32);
|
|
325
|
+
this.index++;
|
|
326
|
+
return id;
|
|
44
327
|
}
|
|
45
328
|
}
|
|
46
329
|
|
|
@@ -96,69 +379,6 @@ function intercept(interceptors, options, main) {
|
|
|
96
379
|
return next(options, 0);
|
|
97
380
|
}
|
|
98
381
|
|
|
99
|
-
function isAsyncIteratorObject(maybe) {
|
|
100
|
-
if (!maybe || typeof maybe !== "object") {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
104
|
-
}
|
|
105
|
-
function createAsyncIteratorObject(next, cleanup) {
|
|
106
|
-
let isExecuteComplete = false;
|
|
107
|
-
let isDone = false;
|
|
108
|
-
const iterator = {
|
|
109
|
-
next: sequential(async () => {
|
|
110
|
-
if (isDone) {
|
|
111
|
-
return { done: true, value: void 0 };
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
const result = await next();
|
|
115
|
-
if (result.done) {
|
|
116
|
-
isDone = true;
|
|
117
|
-
}
|
|
118
|
-
return result;
|
|
119
|
-
} catch (err) {
|
|
120
|
-
isDone = true;
|
|
121
|
-
throw err;
|
|
122
|
-
} finally {
|
|
123
|
-
if (isDone && !isExecuteComplete) {
|
|
124
|
-
isExecuteComplete = true;
|
|
125
|
-
await cleanup("next");
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}),
|
|
129
|
-
async return(value) {
|
|
130
|
-
isDone = true;
|
|
131
|
-
if (!isExecuteComplete) {
|
|
132
|
-
isExecuteComplete = true;
|
|
133
|
-
await cleanup("return");
|
|
134
|
-
}
|
|
135
|
-
return { done: true, value };
|
|
136
|
-
},
|
|
137
|
-
async throw(err) {
|
|
138
|
-
isDone = true;
|
|
139
|
-
if (!isExecuteComplete) {
|
|
140
|
-
isExecuteComplete = true;
|
|
141
|
-
await cleanup("throw");
|
|
142
|
-
}
|
|
143
|
-
throw err;
|
|
144
|
-
},
|
|
145
|
-
/**
|
|
146
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
147
|
-
*/
|
|
148
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
149
|
-
isDone = true;
|
|
150
|
-
if (!isExecuteComplete) {
|
|
151
|
-
isExecuteComplete = true;
|
|
152
|
-
await cleanup("dispose");
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
[Symbol.asyncIterator]() {
|
|
156
|
-
return iterator;
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
return iterator;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
382
|
function parseEmptyableJSON(text) {
|
|
163
383
|
if (!text) {
|
|
164
384
|
return void 0;
|
|
@@ -221,81 +441,39 @@ function isPropertyKey(value) {
|
|
|
221
441
|
const type = typeof value;
|
|
222
442
|
return type === "string" || type === "number" || type === "symbol";
|
|
223
443
|
}
|
|
444
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
445
|
+
const e = function() {
|
|
446
|
+
};
|
|
447
|
+
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
448
|
+
Object.freeze(e.prototype);
|
|
449
|
+
return e;
|
|
450
|
+
})();
|
|
224
451
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
this.openIds.add(id);
|
|
234
|
-
}
|
|
235
|
-
isOpen(id) {
|
|
236
|
-
return this.openIds.has(id);
|
|
237
|
-
}
|
|
238
|
-
push(id, item) {
|
|
239
|
-
this.assertOpen(id);
|
|
240
|
-
const pending = this.pendingPulls.get(id);
|
|
241
|
-
if (pending?.length) {
|
|
242
|
-
pending.shift()[0](item);
|
|
243
|
-
if (pending.length === 0) {
|
|
244
|
-
this.pendingPulls.delete(id);
|
|
245
|
-
}
|
|
246
|
-
} else {
|
|
247
|
-
const items = this.items.get(id);
|
|
248
|
-
if (items) {
|
|
249
|
-
items.push(item);
|
|
250
|
-
} else {
|
|
251
|
-
this.items.set(id, [item]);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
async pull(id) {
|
|
256
|
-
this.assertOpen(id);
|
|
257
|
-
const items = this.items.get(id);
|
|
258
|
-
if (items?.length) {
|
|
259
|
-
const item = items.shift();
|
|
260
|
-
if (items.length === 0) {
|
|
261
|
-
this.items.delete(id);
|
|
262
|
-
}
|
|
263
|
-
return item;
|
|
452
|
+
function streamToAsyncIteratorClass(stream) {
|
|
453
|
+
const reader = stream.getReader();
|
|
454
|
+
return new AsyncIteratorClass(
|
|
455
|
+
async () => {
|
|
456
|
+
return reader.read();
|
|
457
|
+
},
|
|
458
|
+
async () => {
|
|
459
|
+
await reader.cancel();
|
|
264
460
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
function asyncIteratorToStream(iterator) {
|
|
464
|
+
return new ReadableStream({
|
|
465
|
+
async pull(controller) {
|
|
466
|
+
const { done, value } = await iterator.next();
|
|
467
|
+
if (done) {
|
|
468
|
+
controller.close();
|
|
270
469
|
} else {
|
|
271
|
-
|
|
470
|
+
controller.enqueue(value);
|
|
272
471
|
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (id === void 0) {
|
|
277
|
-
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
278
|
-
pendingPulls.forEach(([, reject]) => {
|
|
279
|
-
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
this.pendingPulls.clear();
|
|
283
|
-
this.openIds.clear();
|
|
284
|
-
this.items.clear();
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
288
|
-
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
289
|
-
});
|
|
290
|
-
this.pendingPulls.delete(id);
|
|
291
|
-
this.openIds.delete(id);
|
|
292
|
-
this.items.delete(id);
|
|
293
|
-
}
|
|
294
|
-
assertOpen(id) {
|
|
295
|
-
if (!this.isOpen(id)) {
|
|
296
|
-
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
472
|
+
},
|
|
473
|
+
async cancel() {
|
|
474
|
+
await iterator.return?.();
|
|
297
475
|
}
|
|
298
|
-
}
|
|
476
|
+
});
|
|
299
477
|
}
|
|
300
478
|
|
|
301
479
|
function value(value2, ...args) {
|
|
@@ -305,4 +483,4 @@ function value(value2, ...args) {
|
|
|
305
483
|
return value2;
|
|
306
484
|
}
|
|
307
485
|
|
|
308
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
486
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, value };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.fe5c63f",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -24,9 +24,14 @@
|
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"radash": "^12.1.
|
|
27
|
+
"radash": "^12.1.1",
|
|
28
28
|
"type-fest": "^4.39.1"
|
|
29
29
|
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"arktype": "2.1.20",
|
|
32
|
+
"valibot": "^1.1.0",
|
|
33
|
+
"zod": "^4.0.5"
|
|
34
|
+
},
|
|
30
35
|
"scripts": {
|
|
31
36
|
"build": "unbuild",
|
|
32
37
|
"build:watch": "pnpm run build --watch",
|