@orpc/shared 0.0.0-next.8c618ab → 0.0.0-next.8e119a1
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 -7
- package/dist/index.d.mts +94 -15
- package/dist/index.d.ts +94 -15
- package/dist/index.mjs +199 -116
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
36
36
|
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
37
37
|
- **📝 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.
|
|
38
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
|
|
39
39
|
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
40
40
|
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
41
41
|
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
@@ -54,14 +54,12 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
54
54
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
55
55
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
56
56
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
57
|
-
- [@orpc/
|
|
57
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
58
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
59
|
- [@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).
|
|
60
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
63
61
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
64
|
-
- [@orpc/
|
|
62
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
65
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
66
64
|
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
67
65
|
- [@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];
|
|
@@ -20,9 +20,68 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
20
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
interface EventPublisherOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
26
|
+
*
|
|
27
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
28
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
29
|
+
*
|
|
30
|
+
* Set to:
|
|
31
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
32
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
33
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
34
|
+
*
|
|
35
|
+
* @default 100
|
|
36
|
+
*/
|
|
37
|
+
maxBufferedEvents?: number;
|
|
38
|
+
}
|
|
39
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
42
|
+
*/
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
46
|
+
#private;
|
|
47
|
+
constructor(options?: EventPublisherOptions);
|
|
48
|
+
get size(): number;
|
|
49
|
+
/**
|
|
50
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
51
|
+
*/
|
|
52
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Subscribes to a specific event using a callback function.
|
|
55
|
+
* Returns an unsubscribe function to remove the listener.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
60
|
+
* console.log(payload)
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* // Later
|
|
64
|
+
* unsubscribe()
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Subscribes to a specific event using an async iterator.
|
|
70
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
75
|
+
* console.log(payload)
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
80
|
+
}
|
|
81
|
+
|
|
23
82
|
declare class SequentialIdGenerator {
|
|
24
|
-
private
|
|
25
|
-
generate():
|
|
83
|
+
private index;
|
|
84
|
+
generate(): string;
|
|
26
85
|
}
|
|
27
86
|
|
|
28
87
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -76,14 +135,34 @@ declare function onFinish<T, TOptions extends {
|
|
|
76
135
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
77
136
|
|
|
78
137
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
79
|
-
interface
|
|
138
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
139
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
140
|
+
}
|
|
141
|
+
interface AsyncIteratorClassCleanupFn {
|
|
80
142
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
81
143
|
}
|
|
82
|
-
declare
|
|
83
|
-
declare
|
|
144
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
145
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
146
|
+
asyncDispose: infer T;
|
|
147
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
148
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
149
|
+
#private;
|
|
150
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
151
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
152
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
153
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
154
|
+
/**
|
|
155
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
156
|
+
*/
|
|
157
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
158
|
+
[Symbol.asyncIterator](): this;
|
|
159
|
+
}
|
|
160
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
84
161
|
|
|
85
162
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
86
|
-
declare function stringifyJSON<T>(value: T
|
|
163
|
+
declare function stringifyJSON<T>(value: T | {
|
|
164
|
+
toJSON(): T;
|
|
165
|
+
}): undefined extends T ? undefined | string : string;
|
|
87
166
|
|
|
88
167
|
type Segment = string | number;
|
|
89
168
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -106,7 +185,7 @@ declare const NullProtoObj: ({
|
|
|
106
185
|
});
|
|
107
186
|
|
|
108
187
|
interface AsyncIdQueueCloseOptions {
|
|
109
|
-
id?:
|
|
188
|
+
id?: string;
|
|
110
189
|
reason?: unknown;
|
|
111
190
|
}
|
|
112
191
|
declare class AsyncIdQueue<T> {
|
|
@@ -114,16 +193,16 @@ declare class AsyncIdQueue<T> {
|
|
|
114
193
|
private readonly items;
|
|
115
194
|
private readonly pendingPulls;
|
|
116
195
|
get length(): number;
|
|
117
|
-
open(id:
|
|
118
|
-
isOpen(id:
|
|
119
|
-
push(id:
|
|
120
|
-
pull(id:
|
|
196
|
+
open(id: string): void;
|
|
197
|
+
isOpen(id: string): boolean;
|
|
198
|
+
push(id: string, item: T): void;
|
|
199
|
+
pull(id: string): Promise<T>;
|
|
121
200
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
122
|
-
assertOpen(id:
|
|
201
|
+
assertOpen(id: string): void;
|
|
123
202
|
}
|
|
124
203
|
|
|
125
204
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
126
205
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
127
206
|
|
|
128
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone,
|
|
129
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
207
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
208
|
+
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];
|
|
@@ -20,9 +20,68 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
20
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
interface EventPublisherOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
26
|
+
*
|
|
27
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
28
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
29
|
+
*
|
|
30
|
+
* Set to:
|
|
31
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
32
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
33
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
34
|
+
*
|
|
35
|
+
* @default 100
|
|
36
|
+
*/
|
|
37
|
+
maxBufferedEvents?: number;
|
|
38
|
+
}
|
|
39
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
42
|
+
*/
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
46
|
+
#private;
|
|
47
|
+
constructor(options?: EventPublisherOptions);
|
|
48
|
+
get size(): number;
|
|
49
|
+
/**
|
|
50
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
51
|
+
*/
|
|
52
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Subscribes to a specific event using a callback function.
|
|
55
|
+
* Returns an unsubscribe function to remove the listener.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
60
|
+
* console.log(payload)
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* // Later
|
|
64
|
+
* unsubscribe()
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Subscribes to a specific event using an async iterator.
|
|
70
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
75
|
+
* console.log(payload)
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
80
|
+
}
|
|
81
|
+
|
|
23
82
|
declare class SequentialIdGenerator {
|
|
24
|
-
private
|
|
25
|
-
generate():
|
|
83
|
+
private index;
|
|
84
|
+
generate(): string;
|
|
26
85
|
}
|
|
27
86
|
|
|
28
87
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -76,14 +135,34 @@ declare function onFinish<T, TOptions extends {
|
|
|
76
135
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
77
136
|
|
|
78
137
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
79
|
-
interface
|
|
138
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
139
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
140
|
+
}
|
|
141
|
+
interface AsyncIteratorClassCleanupFn {
|
|
80
142
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
81
143
|
}
|
|
82
|
-
declare
|
|
83
|
-
declare
|
|
144
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
145
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
146
|
+
asyncDispose: infer T;
|
|
147
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
148
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
149
|
+
#private;
|
|
150
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
151
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
152
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
153
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
154
|
+
/**
|
|
155
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
156
|
+
*/
|
|
157
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
158
|
+
[Symbol.asyncIterator](): this;
|
|
159
|
+
}
|
|
160
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
84
161
|
|
|
85
162
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
86
|
-
declare function stringifyJSON<T>(value: T
|
|
163
|
+
declare function stringifyJSON<T>(value: T | {
|
|
164
|
+
toJSON(): T;
|
|
165
|
+
}): undefined extends T ? undefined | string : string;
|
|
87
166
|
|
|
88
167
|
type Segment = string | number;
|
|
89
168
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -106,7 +185,7 @@ declare const NullProtoObj: ({
|
|
|
106
185
|
});
|
|
107
186
|
|
|
108
187
|
interface AsyncIdQueueCloseOptions {
|
|
109
|
-
id?:
|
|
188
|
+
id?: string;
|
|
110
189
|
reason?: unknown;
|
|
111
190
|
}
|
|
112
191
|
declare class AsyncIdQueue<T> {
|
|
@@ -114,16 +193,16 @@ declare class AsyncIdQueue<T> {
|
|
|
114
193
|
private readonly items;
|
|
115
194
|
private readonly pendingPulls;
|
|
116
195
|
get length(): number;
|
|
117
|
-
open(id:
|
|
118
|
-
isOpen(id:
|
|
119
|
-
push(id:
|
|
120
|
-
pull(id:
|
|
196
|
+
open(id: string): void;
|
|
197
|
+
isOpen(id: string): boolean;
|
|
198
|
+
push(id: string, item: T): void;
|
|
199
|
+
pull(id: string): Promise<T>;
|
|
121
200
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
122
|
-
assertOpen(id:
|
|
201
|
+
assertOpen(id: string): void;
|
|
123
202
|
}
|
|
124
203
|
|
|
125
204
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
126
205
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
127
206
|
|
|
128
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone,
|
|
129
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
207
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
208
|
+
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
|
@@ -33,76 +33,13 @@ function sequential(fn) {
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
function defer(callback) {
|
|
36
|
-
if (
|
|
37
|
-
|
|
36
|
+
if (typeof setTimeout === "function") {
|
|
37
|
+
setTimeout(callback, 0);
|
|
38
38
|
} else {
|
|
39
39
|
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
class SequentialIdGenerator {
|
|
44
|
-
nextId = 0;
|
|
45
|
-
generate() {
|
|
46
|
-
if (this.nextId === Number.MAX_SAFE_INTEGER) {
|
|
47
|
-
this.nextId = 0;
|
|
48
|
-
return Number.MAX_SAFE_INTEGER;
|
|
49
|
-
}
|
|
50
|
-
return this.nextId++;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function onStart(callback) {
|
|
55
|
-
return async (options, ...rest) => {
|
|
56
|
-
await callback(options, ...rest);
|
|
57
|
-
return await options.next();
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
function onSuccess(callback) {
|
|
61
|
-
return async (options, ...rest) => {
|
|
62
|
-
const result = await options.next();
|
|
63
|
-
await callback(result, options, ...rest);
|
|
64
|
-
return result;
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
function onError(callback) {
|
|
68
|
-
return async (options, ...rest) => {
|
|
69
|
-
try {
|
|
70
|
-
return await options.next();
|
|
71
|
-
} catch (error) {
|
|
72
|
-
await callback(error, options, ...rest);
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
function onFinish(callback) {
|
|
78
|
-
let state;
|
|
79
|
-
return async (options, ...rest) => {
|
|
80
|
-
try {
|
|
81
|
-
const result = await options.next();
|
|
82
|
-
state = [null, result, true];
|
|
83
|
-
return result;
|
|
84
|
-
} catch (error) {
|
|
85
|
-
state = [error, void 0, false];
|
|
86
|
-
throw error;
|
|
87
|
-
} finally {
|
|
88
|
-
await callback(state, options, ...rest);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
function intercept(interceptors, options, main) {
|
|
93
|
-
const next = (options2, index) => {
|
|
94
|
-
const interceptor = interceptors[index];
|
|
95
|
-
if (!interceptor) {
|
|
96
|
-
return main(options2);
|
|
97
|
-
}
|
|
98
|
-
return interceptor({
|
|
99
|
-
...options2,
|
|
100
|
-
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
101
|
-
});
|
|
102
|
-
};
|
|
103
|
-
return next(options, 0);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
43
|
class AsyncIdQueue {
|
|
107
44
|
openIds = /* @__PURE__ */ new Set();
|
|
108
45
|
items = /* @__PURE__ */ new Map();
|
|
@@ -183,63 +120,70 @@ function isAsyncIteratorObject(maybe) {
|
|
|
183
120
|
if (!maybe || typeof maybe !== "object") {
|
|
184
121
|
return false;
|
|
185
122
|
}
|
|
186
|
-
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
123
|
+
return "next" in maybe && typeof maybe.next === "function" && Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
187
124
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
125
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
126
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
127
|
+
class AsyncIteratorClass {
|
|
128
|
+
#isDone = false;
|
|
129
|
+
#isExecuteComplete = false;
|
|
130
|
+
#cleanup;
|
|
131
|
+
#next;
|
|
132
|
+
constructor(next, cleanup) {
|
|
133
|
+
this.#cleanup = cleanup;
|
|
134
|
+
this.#next = sequential(async () => {
|
|
135
|
+
if (this.#isDone) {
|
|
194
136
|
return { done: true, value: void 0 };
|
|
195
137
|
}
|
|
196
138
|
try {
|
|
197
139
|
const result = await next();
|
|
198
140
|
if (result.done) {
|
|
199
|
-
isDone = true;
|
|
141
|
+
this.#isDone = true;
|
|
200
142
|
}
|
|
201
143
|
return result;
|
|
202
144
|
} catch (err) {
|
|
203
|
-
isDone = true;
|
|
145
|
+
this.#isDone = true;
|
|
204
146
|
throw err;
|
|
205
147
|
} finally {
|
|
206
|
-
if (isDone && !isExecuteComplete) {
|
|
207
|
-
isExecuteComplete = true;
|
|
208
|
-
await cleanup("next");
|
|
148
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
149
|
+
this.#isExecuteComplete = true;
|
|
150
|
+
await this.#cleanup("next");
|
|
209
151
|
}
|
|
210
152
|
}
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
isDone = true;
|
|
222
|
-
if (!isExecuteComplete) {
|
|
223
|
-
isExecuteComplete = true;
|
|
224
|
-
await cleanup("throw");
|
|
225
|
-
}
|
|
226
|
-
throw err;
|
|
227
|
-
},
|
|
228
|
-
/**
|
|
229
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
230
|
-
*/
|
|
231
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
232
|
-
isDone = true;
|
|
233
|
-
if (!isExecuteComplete) {
|
|
234
|
-
isExecuteComplete = true;
|
|
235
|
-
await cleanup("dispose");
|
|
236
|
-
}
|
|
237
|
-
},
|
|
238
|
-
[Symbol.asyncIterator]() {
|
|
239
|
-
return iterator;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
next() {
|
|
156
|
+
return this.#next();
|
|
157
|
+
}
|
|
158
|
+
async return(value) {
|
|
159
|
+
this.#isDone = true;
|
|
160
|
+
if (!this.#isExecuteComplete) {
|
|
161
|
+
this.#isExecuteComplete = true;
|
|
162
|
+
await this.#cleanup("return");
|
|
240
163
|
}
|
|
241
|
-
|
|
242
|
-
|
|
164
|
+
return { done: true, value };
|
|
165
|
+
}
|
|
166
|
+
async throw(err) {
|
|
167
|
+
this.#isDone = true;
|
|
168
|
+
if (!this.#isExecuteComplete) {
|
|
169
|
+
this.#isExecuteComplete = true;
|
|
170
|
+
await this.#cleanup("throw");
|
|
171
|
+
}
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
176
|
+
*/
|
|
177
|
+
async [asyncDisposeSymbol]() {
|
|
178
|
+
this.#isDone = true;
|
|
179
|
+
if (!this.#isExecuteComplete) {
|
|
180
|
+
this.#isExecuteComplete = true;
|
|
181
|
+
await this.#cleanup("dispose");
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
[Symbol.asyncIterator]() {
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
243
187
|
}
|
|
244
188
|
function replicateAsyncIterator(source, count) {
|
|
245
189
|
const queue = new AsyncIdQueue();
|
|
@@ -250,8 +194,8 @@ function replicateAsyncIterator(source, count) {
|
|
|
250
194
|
while (true) {
|
|
251
195
|
const item = await source.next();
|
|
252
196
|
for (let id = 0; id < count; id++) {
|
|
253
|
-
if (queue.isOpen(id)) {
|
|
254
|
-
queue.push(id, item);
|
|
197
|
+
if (queue.isOpen(id.toString())) {
|
|
198
|
+
queue.push(id.toString(), item);
|
|
255
199
|
}
|
|
256
200
|
}
|
|
257
201
|
if (item.done) {
|
|
@@ -263,12 +207,12 @@ function replicateAsyncIterator(source, count) {
|
|
|
263
207
|
}
|
|
264
208
|
});
|
|
265
209
|
for (let id = 0; id < count; id++) {
|
|
266
|
-
queue.open(id);
|
|
267
|
-
replicated.push(
|
|
210
|
+
queue.open(id.toString());
|
|
211
|
+
replicated.push(new AsyncIteratorClass(
|
|
268
212
|
() => {
|
|
269
213
|
start();
|
|
270
214
|
return new Promise((resolve, reject) => {
|
|
271
|
-
queue.pull(id).then(resolve).catch(reject);
|
|
215
|
+
queue.pull(id.toString()).then(resolve).catch(reject);
|
|
272
216
|
defer(() => {
|
|
273
217
|
if (error) {
|
|
274
218
|
reject(error.value);
|
|
@@ -277,9 +221,9 @@ function replicateAsyncIterator(source, count) {
|
|
|
277
221
|
});
|
|
278
222
|
},
|
|
279
223
|
async (reason) => {
|
|
280
|
-
queue.close({ id });
|
|
224
|
+
queue.close({ id: id.toString() });
|
|
281
225
|
if (reason !== "next") {
|
|
282
|
-
if (replicated.every((_, id2) => !queue.isOpen(id2))) {
|
|
226
|
+
if (replicated.every((_, id2) => !queue.isOpen(id2.toString()))) {
|
|
283
227
|
await source?.return?.();
|
|
284
228
|
}
|
|
285
229
|
}
|
|
@@ -289,6 +233,145 @@ function replicateAsyncIterator(source, count) {
|
|
|
289
233
|
return replicated;
|
|
290
234
|
}
|
|
291
235
|
|
|
236
|
+
class EventPublisher {
|
|
237
|
+
#listenersMap = /* @__PURE__ */ new Map();
|
|
238
|
+
#maxBufferedEvents;
|
|
239
|
+
constructor(options = {}) {
|
|
240
|
+
this.#maxBufferedEvents = options.maxBufferedEvents ?? 100;
|
|
241
|
+
}
|
|
242
|
+
get size() {
|
|
243
|
+
return this.#listenersMap.size;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
247
|
+
*/
|
|
248
|
+
publish(event, payload) {
|
|
249
|
+
const listeners = this.#listenersMap.get(event);
|
|
250
|
+
if (!listeners) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
for (const listener of listeners) {
|
|
254
|
+
listener(payload);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
subscribe(event, listenerOrOptions) {
|
|
258
|
+
if (typeof listenerOrOptions === "function") {
|
|
259
|
+
let listeners = this.#listenersMap.get(event);
|
|
260
|
+
if (!listeners) {
|
|
261
|
+
this.#listenersMap.set(event, listeners = /* @__PURE__ */ new Set());
|
|
262
|
+
}
|
|
263
|
+
listeners.add(listenerOrOptions);
|
|
264
|
+
return () => {
|
|
265
|
+
listeners.delete(listenerOrOptions);
|
|
266
|
+
if (listeners.size === 0) {
|
|
267
|
+
this.#listenersMap.delete(event);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
const signal = listenerOrOptions?.signal;
|
|
272
|
+
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
273
|
+
signal?.throwIfAborted();
|
|
274
|
+
const bufferedEvents = [];
|
|
275
|
+
const pullResolvers = [];
|
|
276
|
+
const unsubscribe = this.subscribe(event, (payload) => {
|
|
277
|
+
const resolver = pullResolvers.shift();
|
|
278
|
+
if (resolver) {
|
|
279
|
+
resolver[0]({ done: false, value: payload });
|
|
280
|
+
} else {
|
|
281
|
+
bufferedEvents.push(payload);
|
|
282
|
+
if (bufferedEvents.length > maxBufferedEvents) {
|
|
283
|
+
bufferedEvents.shift();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
const abortListener = (event2) => {
|
|
288
|
+
unsubscribe();
|
|
289
|
+
pullResolvers.forEach((resolver) => resolver[1](event2.target.reason));
|
|
290
|
+
pullResolvers.length = 0;
|
|
291
|
+
bufferedEvents.length = 0;
|
|
292
|
+
};
|
|
293
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
294
|
+
return new AsyncIteratorClass(async () => {
|
|
295
|
+
if (signal?.aborted) {
|
|
296
|
+
throw signal.reason;
|
|
297
|
+
}
|
|
298
|
+
if (bufferedEvents.length > 0) {
|
|
299
|
+
return { done: false, value: bufferedEvents.shift() };
|
|
300
|
+
}
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
pullResolvers.push([resolve, reject]);
|
|
303
|
+
});
|
|
304
|
+
}, async () => {
|
|
305
|
+
unsubscribe();
|
|
306
|
+
signal?.removeEventListener("abort", abortListener);
|
|
307
|
+
pullResolvers.forEach((resolver) => resolver[0]({ done: true, value: void 0 }));
|
|
308
|
+
pullResolvers.length = 0;
|
|
309
|
+
bufferedEvents.length = 0;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
class SequentialIdGenerator {
|
|
315
|
+
index = BigInt(0);
|
|
316
|
+
generate() {
|
|
317
|
+
const id = this.index.toString(32);
|
|
318
|
+
this.index++;
|
|
319
|
+
return id;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function onStart(callback) {
|
|
324
|
+
return async (options, ...rest) => {
|
|
325
|
+
await callback(options, ...rest);
|
|
326
|
+
return await options.next();
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function onSuccess(callback) {
|
|
330
|
+
return async (options, ...rest) => {
|
|
331
|
+
const result = await options.next();
|
|
332
|
+
await callback(result, options, ...rest);
|
|
333
|
+
return result;
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function onError(callback) {
|
|
337
|
+
return async (options, ...rest) => {
|
|
338
|
+
try {
|
|
339
|
+
return await options.next();
|
|
340
|
+
} catch (error) {
|
|
341
|
+
await callback(error, options, ...rest);
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function onFinish(callback) {
|
|
347
|
+
let state;
|
|
348
|
+
return async (options, ...rest) => {
|
|
349
|
+
try {
|
|
350
|
+
const result = await options.next();
|
|
351
|
+
state = [null, result, true];
|
|
352
|
+
return result;
|
|
353
|
+
} catch (error) {
|
|
354
|
+
state = [error, void 0, false];
|
|
355
|
+
throw error;
|
|
356
|
+
} finally {
|
|
357
|
+
await callback(state, options, ...rest);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
function intercept(interceptors, options, main) {
|
|
362
|
+
const next = (options2, index) => {
|
|
363
|
+
const interceptor = interceptors[index];
|
|
364
|
+
if (!interceptor) {
|
|
365
|
+
return main(options2);
|
|
366
|
+
}
|
|
367
|
+
return interceptor({
|
|
368
|
+
...options2,
|
|
369
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
return next(options, 0);
|
|
373
|
+
}
|
|
374
|
+
|
|
292
375
|
function parseEmptyableJSON(text) {
|
|
293
376
|
if (!text) {
|
|
294
377
|
return void 0;
|
|
@@ -366,4 +449,4 @@ function value(value2, ...args) {
|
|
|
366
449
|
return value2;
|
|
367
450
|
}
|
|
368
451
|
|
|
369
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone,
|
|
452
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, 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.8e119a1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -24,13 +24,13 @@
|
|
|
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
30
|
"devDependencies": {
|
|
31
31
|
"arktype": "2.1.20",
|
|
32
32
|
"valibot": "^1.1.0",
|
|
33
|
-
"zod": "^3.25.
|
|
33
|
+
"zod": "^3.25.75"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "unbuild",
|