@orpc/shared 0.0.0-next.fb0d07c → 0.0.0-next.fc1ae52
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 +8 -13
- package/dist/index.d.mts +218 -18
- package/dist/index.d.ts +218 -18
- package/dist/index.mjs +499 -137
- package/package.json +16 -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,8 @@
|
|
|
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
|
-
-
|
|
33
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
34
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
39
35
|
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
40
36
|
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
41
37
|
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
@@ -43,7 +39,6 @@
|
|
|
43
39
|
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
44
40
|
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
45
41
|
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
46
|
-
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
|
47
42
|
|
|
48
43
|
## Documentation
|
|
49
44
|
|
|
@@ -54,14 +49,14 @@ 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/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
54
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
55
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
59
|
-
- [@orpc/
|
|
60
|
-
- [@orpc/
|
|
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).
|
|
56
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
57
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
63
58
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
64
|
-
- [@orpc/
|
|
59
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
65
60
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
66
61
|
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
67
62
|
- [@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,6 @@
|
|
|
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
|
+
import { Tracer, TraceAPI, ContextAPI, PropagationAPI, SpanOptions, Context, Span, AttributeValue, Exception } from '@opentelemetry/api';
|
|
3
4
|
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
5
|
|
|
5
6
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
@@ -8,17 +9,99 @@ declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>):
|
|
|
8
9
|
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
10
|
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array).
|
|
14
|
+
*
|
|
15
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
16
|
+
*/
|
|
17
|
+
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>;
|
|
18
|
+
|
|
11
19
|
type AnyFunction = (...args: any[]) => any;
|
|
12
20
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
21
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
22
|
+
/**
|
|
23
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
24
|
+
*/
|
|
25
|
+
declare function defer(callback: () => void): void;
|
|
14
26
|
|
|
15
27
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
28
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
17
29
|
};
|
|
18
30
|
|
|
31
|
+
declare const ORPC_NAME = "orpc";
|
|
32
|
+
declare const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
33
|
+
declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.fc1ae52";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when an operation is aborted.
|
|
37
|
+
* Uses the standardized 'AbortError' name for consistency with JavaScript APIs.
|
|
38
|
+
*/
|
|
39
|
+
declare class AbortError extends Error {
|
|
40
|
+
constructor(...rest: ConstructorParameters<typeof Error>);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface EventPublisherOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
46
|
+
*
|
|
47
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
48
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
49
|
+
*
|
|
50
|
+
* Set to:
|
|
51
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
52
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
53
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
54
|
+
*
|
|
55
|
+
* @default 100
|
|
56
|
+
*/
|
|
57
|
+
maxBufferedEvents?: number;
|
|
58
|
+
}
|
|
59
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
62
|
+
*/
|
|
63
|
+
signal?: AbortSignal | undefined;
|
|
64
|
+
}
|
|
65
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
66
|
+
#private;
|
|
67
|
+
constructor(options?: EventPublisherOptions);
|
|
68
|
+
get size(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
71
|
+
*/
|
|
72
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
73
|
+
/**
|
|
74
|
+
* Subscribes to a specific event using a callback function.
|
|
75
|
+
* Returns an unsubscribe function to remove the listener.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
80
|
+
* console.log(payload)
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* // Later
|
|
84
|
+
* unsubscribe()
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
88
|
+
/**
|
|
89
|
+
* Subscribes to a specific event using an async iterator.
|
|
90
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
95
|
+
* console.log(payload)
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
100
|
+
}
|
|
101
|
+
|
|
19
102
|
declare class SequentialIdGenerator {
|
|
20
|
-
private
|
|
21
|
-
generate():
|
|
103
|
+
private index;
|
|
104
|
+
generate(): string;
|
|
22
105
|
}
|
|
23
106
|
|
|
24
107
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -38,6 +121,7 @@ interface Registry {
|
|
|
38
121
|
type ThrowableError = Registry extends {
|
|
39
122
|
throwableError: infer T;
|
|
40
123
|
} ? T : Error;
|
|
124
|
+
type InferAsyncIterableYield<T> = T extends AsyncIterable<infer U> ? U : never;
|
|
41
125
|
|
|
42
126
|
type InterceptableOptions = Record<string, any>;
|
|
43
127
|
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
@@ -71,20 +155,119 @@ declare function onFinish<T, TOptions extends {
|
|
|
71
155
|
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
72
156
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Only import types from @opentelemetry/api to avoid runtime dependencies.
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
interface OtelConfig {
|
|
163
|
+
tracer: Tracer;
|
|
164
|
+
trace: TraceAPI;
|
|
165
|
+
context: ContextAPI;
|
|
166
|
+
propagation: PropagationAPI;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Sets the global OpenTelemetry config.
|
|
170
|
+
* Call this once at app startup. Use `undefined` to disable tracing.
|
|
171
|
+
*/
|
|
172
|
+
declare function setGlobalOtelConfig(config: OtelConfig | undefined): void;
|
|
173
|
+
/**
|
|
174
|
+
* Gets the global OpenTelemetry config.
|
|
175
|
+
* Returns `undefined` if OpenTelemetry is not configured, initialized, or enabled.
|
|
176
|
+
*/
|
|
177
|
+
declare function getGlobalOtelConfig(): OtelConfig | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Starts a new OpenTelemetry span with the given name and options.
|
|
180
|
+
*
|
|
181
|
+
* @returns The new span, or `undefined` if no tracer is set.
|
|
182
|
+
*/
|
|
183
|
+
declare function startSpan(name: string, options?: SpanOptions, context?: Context): Span | undefined;
|
|
184
|
+
interface SetSpanErrorOptions {
|
|
185
|
+
/**
|
|
186
|
+
* Span error status is not set if error is due to cancellation by the signal.
|
|
187
|
+
*/
|
|
188
|
+
signal?: AbortSignal;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Records and sets the error status on the given span.
|
|
192
|
+
* If the span is `undefined`, it does nothing.
|
|
193
|
+
*/
|
|
194
|
+
declare function setSpanError(span: Span | undefined, error: unknown, options?: SetSpanErrorOptions): void;
|
|
195
|
+
declare function setSpanAttribute(span: Span | undefined, key: string, value: AttributeValue | undefined): void;
|
|
196
|
+
/**
|
|
197
|
+
* Converts an error to an OpenTelemetry Exception.
|
|
198
|
+
*/
|
|
199
|
+
declare function toOtelException(error: unknown): Exclude<Exception, string>;
|
|
200
|
+
/**
|
|
201
|
+
* Converts a value to a string suitable for OpenTelemetry span attributes.
|
|
202
|
+
*/
|
|
203
|
+
declare function toSpanAttributeValue(data: unknown): string;
|
|
204
|
+
interface RunWithSpanOptions extends SpanOptions, SetSpanErrorOptions {
|
|
205
|
+
/**
|
|
206
|
+
* The name of the span to create.
|
|
207
|
+
*/
|
|
208
|
+
name: string;
|
|
209
|
+
/**
|
|
210
|
+
* Context to use for the span.
|
|
211
|
+
*/
|
|
212
|
+
context?: Context;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Runs a function within the context of a new OpenTelemetry span.
|
|
216
|
+
* The span is ended automatically, and errors are recorded to the span.
|
|
217
|
+
*/
|
|
218
|
+
declare function runWithSpan<T>({ name, context, ...options }: RunWithSpanOptions, fn: (span?: Span) => Promisable<T>): Promise<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Runs a function within the context of an existing OpenTelemetry span.
|
|
221
|
+
*/
|
|
222
|
+
declare function runInSpanContext<T>(span: Span | undefined, fn: () => Promisable<T>): Promise<T>;
|
|
223
|
+
|
|
74
224
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
-
interface
|
|
225
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
226
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
227
|
+
}
|
|
228
|
+
interface AsyncIteratorClassCleanupFn {
|
|
76
229
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
230
|
}
|
|
78
|
-
declare
|
|
231
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
232
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
233
|
+
asyncDispose: infer T;
|
|
234
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
235
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
236
|
+
#private;
|
|
237
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
238
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
239
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
240
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
241
|
+
/**
|
|
242
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
243
|
+
*/
|
|
244
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
245
|
+
[Symbol.asyncIterator](): this;
|
|
246
|
+
}
|
|
247
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
248
|
+
interface AsyncIteratorWithSpanOptions extends SetSpanErrorOptions {
|
|
249
|
+
/**
|
|
250
|
+
* The name of the span to create.
|
|
251
|
+
*/
|
|
252
|
+
name: string;
|
|
253
|
+
}
|
|
254
|
+
declare function asyncIteratorWithSpan<T, TReturn, TNext>({ name, ...options }: AsyncIteratorWithSpanOptions, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
79
255
|
|
|
80
256
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
|
-
declare function stringifyJSON<T>(value: T
|
|
257
|
+
declare function stringifyJSON<T>(value: T | {
|
|
258
|
+
toJSON(): T;
|
|
259
|
+
}): undefined extends T ? undefined | string : string;
|
|
82
260
|
|
|
83
261
|
type Segment = string | number;
|
|
84
262
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
85
263
|
maps: Segment[][];
|
|
86
264
|
values: unknown[];
|
|
87
265
|
};
|
|
266
|
+
/**
|
|
267
|
+
* Get constructor of the value
|
|
268
|
+
*
|
|
269
|
+
*/
|
|
270
|
+
declare function getConstructor(value: unknown): Function | null | undefined;
|
|
88
271
|
/**
|
|
89
272
|
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
90
273
|
*/
|
|
@@ -94,28 +277,45 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
94
277
|
*/
|
|
95
278
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
279
|
declare function clone<T>(value: T): T;
|
|
97
|
-
declare function get(object:
|
|
280
|
+
declare function get(object: unknown, path: readonly string[]): unknown;
|
|
98
281
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
282
|
+
declare const NullProtoObj: ({
|
|
283
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Prevents objects from being awaitable by intercepting the `then` method
|
|
288
|
+
* when called by the native await mechanism. This is useful for preventing
|
|
289
|
+
* accidental awaiting of objects that aren't meant to be promises.
|
|
290
|
+
*/
|
|
291
|
+
declare function preventNativeAwait<T extends object>(target: T): T;
|
|
99
292
|
|
|
100
293
|
interface AsyncIdQueueCloseOptions {
|
|
101
|
-
id?:
|
|
102
|
-
reason?:
|
|
294
|
+
id?: string;
|
|
295
|
+
reason?: unknown;
|
|
103
296
|
}
|
|
104
297
|
declare class AsyncIdQueue<T> {
|
|
105
298
|
private readonly openIds;
|
|
106
|
-
private readonly
|
|
107
|
-
private readonly
|
|
299
|
+
private readonly queues;
|
|
300
|
+
private readonly waiters;
|
|
108
301
|
get length(): number;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
302
|
+
get waiterIds(): string[];
|
|
303
|
+
hasBufferedItems(id: string): boolean;
|
|
304
|
+
open(id: string): void;
|
|
305
|
+
isOpen(id: string): boolean;
|
|
306
|
+
push(id: string, item: T): void;
|
|
307
|
+
pull(id: string): Promise<T>;
|
|
113
308
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
114
|
-
assertOpen(id:
|
|
309
|
+
assertOpen(id: string): void;
|
|
115
310
|
}
|
|
116
311
|
|
|
312
|
+
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>;
|
|
313
|
+
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
314
|
+
|
|
315
|
+
declare function tryDecodeURIComponent(value: string): string;
|
|
316
|
+
|
|
117
317
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
318
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
319
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
121
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
320
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
321
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InferAsyncIterableYield, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
|
+
import { Tracer, TraceAPI, ContextAPI, PropagationAPI, SpanOptions, Context, Span, AttributeValue, Exception } from '@opentelemetry/api';
|
|
3
4
|
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
5
|
|
|
5
6
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
@@ -8,17 +9,99 @@ declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>):
|
|
|
8
9
|
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
10
|
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array).
|
|
14
|
+
*
|
|
15
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
16
|
+
*/
|
|
17
|
+
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>;
|
|
18
|
+
|
|
11
19
|
type AnyFunction = (...args: any[]) => any;
|
|
12
20
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
21
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
22
|
+
/**
|
|
23
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
24
|
+
*/
|
|
25
|
+
declare function defer(callback: () => void): void;
|
|
14
26
|
|
|
15
27
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
28
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
17
29
|
};
|
|
18
30
|
|
|
31
|
+
declare const ORPC_NAME = "orpc";
|
|
32
|
+
declare const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
33
|
+
declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.fc1ae52";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when an operation is aborted.
|
|
37
|
+
* Uses the standardized 'AbortError' name for consistency with JavaScript APIs.
|
|
38
|
+
*/
|
|
39
|
+
declare class AbortError extends Error {
|
|
40
|
+
constructor(...rest: ConstructorParameters<typeof Error>);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface EventPublisherOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
46
|
+
*
|
|
47
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
48
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
49
|
+
*
|
|
50
|
+
* Set to:
|
|
51
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
52
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
53
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
54
|
+
*
|
|
55
|
+
* @default 100
|
|
56
|
+
*/
|
|
57
|
+
maxBufferedEvents?: number;
|
|
58
|
+
}
|
|
59
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
62
|
+
*/
|
|
63
|
+
signal?: AbortSignal | undefined;
|
|
64
|
+
}
|
|
65
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
66
|
+
#private;
|
|
67
|
+
constructor(options?: EventPublisherOptions);
|
|
68
|
+
get size(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
71
|
+
*/
|
|
72
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
73
|
+
/**
|
|
74
|
+
* Subscribes to a specific event using a callback function.
|
|
75
|
+
* Returns an unsubscribe function to remove the listener.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
80
|
+
* console.log(payload)
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* // Later
|
|
84
|
+
* unsubscribe()
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
88
|
+
/**
|
|
89
|
+
* Subscribes to a specific event using an async iterator.
|
|
90
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
95
|
+
* console.log(payload)
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
100
|
+
}
|
|
101
|
+
|
|
19
102
|
declare class SequentialIdGenerator {
|
|
20
|
-
private
|
|
21
|
-
generate():
|
|
103
|
+
private index;
|
|
104
|
+
generate(): string;
|
|
22
105
|
}
|
|
23
106
|
|
|
24
107
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -38,6 +121,7 @@ interface Registry {
|
|
|
38
121
|
type ThrowableError = Registry extends {
|
|
39
122
|
throwableError: infer T;
|
|
40
123
|
} ? T : Error;
|
|
124
|
+
type InferAsyncIterableYield<T> = T extends AsyncIterable<infer U> ? U : never;
|
|
41
125
|
|
|
42
126
|
type InterceptableOptions = Record<string, any>;
|
|
43
127
|
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
@@ -71,20 +155,119 @@ declare function onFinish<T, TOptions extends {
|
|
|
71
155
|
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
72
156
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Only import types from @opentelemetry/api to avoid runtime dependencies.
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
interface OtelConfig {
|
|
163
|
+
tracer: Tracer;
|
|
164
|
+
trace: TraceAPI;
|
|
165
|
+
context: ContextAPI;
|
|
166
|
+
propagation: PropagationAPI;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Sets the global OpenTelemetry config.
|
|
170
|
+
* Call this once at app startup. Use `undefined` to disable tracing.
|
|
171
|
+
*/
|
|
172
|
+
declare function setGlobalOtelConfig(config: OtelConfig | undefined): void;
|
|
173
|
+
/**
|
|
174
|
+
* Gets the global OpenTelemetry config.
|
|
175
|
+
* Returns `undefined` if OpenTelemetry is not configured, initialized, or enabled.
|
|
176
|
+
*/
|
|
177
|
+
declare function getGlobalOtelConfig(): OtelConfig | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Starts a new OpenTelemetry span with the given name and options.
|
|
180
|
+
*
|
|
181
|
+
* @returns The new span, or `undefined` if no tracer is set.
|
|
182
|
+
*/
|
|
183
|
+
declare function startSpan(name: string, options?: SpanOptions, context?: Context): Span | undefined;
|
|
184
|
+
interface SetSpanErrorOptions {
|
|
185
|
+
/**
|
|
186
|
+
* Span error status is not set if error is due to cancellation by the signal.
|
|
187
|
+
*/
|
|
188
|
+
signal?: AbortSignal;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Records and sets the error status on the given span.
|
|
192
|
+
* If the span is `undefined`, it does nothing.
|
|
193
|
+
*/
|
|
194
|
+
declare function setSpanError(span: Span | undefined, error: unknown, options?: SetSpanErrorOptions): void;
|
|
195
|
+
declare function setSpanAttribute(span: Span | undefined, key: string, value: AttributeValue | undefined): void;
|
|
196
|
+
/**
|
|
197
|
+
* Converts an error to an OpenTelemetry Exception.
|
|
198
|
+
*/
|
|
199
|
+
declare function toOtelException(error: unknown): Exclude<Exception, string>;
|
|
200
|
+
/**
|
|
201
|
+
* Converts a value to a string suitable for OpenTelemetry span attributes.
|
|
202
|
+
*/
|
|
203
|
+
declare function toSpanAttributeValue(data: unknown): string;
|
|
204
|
+
interface RunWithSpanOptions extends SpanOptions, SetSpanErrorOptions {
|
|
205
|
+
/**
|
|
206
|
+
* The name of the span to create.
|
|
207
|
+
*/
|
|
208
|
+
name: string;
|
|
209
|
+
/**
|
|
210
|
+
* Context to use for the span.
|
|
211
|
+
*/
|
|
212
|
+
context?: Context;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Runs a function within the context of a new OpenTelemetry span.
|
|
216
|
+
* The span is ended automatically, and errors are recorded to the span.
|
|
217
|
+
*/
|
|
218
|
+
declare function runWithSpan<T>({ name, context, ...options }: RunWithSpanOptions, fn: (span?: Span) => Promisable<T>): Promise<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Runs a function within the context of an existing OpenTelemetry span.
|
|
221
|
+
*/
|
|
222
|
+
declare function runInSpanContext<T>(span: Span | undefined, fn: () => Promisable<T>): Promise<T>;
|
|
223
|
+
|
|
74
224
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
-
interface
|
|
225
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
226
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
227
|
+
}
|
|
228
|
+
interface AsyncIteratorClassCleanupFn {
|
|
76
229
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
230
|
}
|
|
78
|
-
declare
|
|
231
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
232
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
233
|
+
asyncDispose: infer T;
|
|
234
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
235
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
236
|
+
#private;
|
|
237
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
238
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
239
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
240
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
241
|
+
/**
|
|
242
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
243
|
+
*/
|
|
244
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
245
|
+
[Symbol.asyncIterator](): this;
|
|
246
|
+
}
|
|
247
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
248
|
+
interface AsyncIteratorWithSpanOptions extends SetSpanErrorOptions {
|
|
249
|
+
/**
|
|
250
|
+
* The name of the span to create.
|
|
251
|
+
*/
|
|
252
|
+
name: string;
|
|
253
|
+
}
|
|
254
|
+
declare function asyncIteratorWithSpan<T, TReturn, TNext>({ name, ...options }: AsyncIteratorWithSpanOptions, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
79
255
|
|
|
80
256
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
|
-
declare function stringifyJSON<T>(value: T
|
|
257
|
+
declare function stringifyJSON<T>(value: T | {
|
|
258
|
+
toJSON(): T;
|
|
259
|
+
}): undefined extends T ? undefined | string : string;
|
|
82
260
|
|
|
83
261
|
type Segment = string | number;
|
|
84
262
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
85
263
|
maps: Segment[][];
|
|
86
264
|
values: unknown[];
|
|
87
265
|
};
|
|
266
|
+
/**
|
|
267
|
+
* Get constructor of the value
|
|
268
|
+
*
|
|
269
|
+
*/
|
|
270
|
+
declare function getConstructor(value: unknown): Function | null | undefined;
|
|
88
271
|
/**
|
|
89
272
|
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
90
273
|
*/
|
|
@@ -94,28 +277,45 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
94
277
|
*/
|
|
95
278
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
279
|
declare function clone<T>(value: T): T;
|
|
97
|
-
declare function get(object:
|
|
280
|
+
declare function get(object: unknown, path: readonly string[]): unknown;
|
|
98
281
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
282
|
+
declare const NullProtoObj: ({
|
|
283
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Prevents objects from being awaitable by intercepting the `then` method
|
|
288
|
+
* when called by the native await mechanism. This is useful for preventing
|
|
289
|
+
* accidental awaiting of objects that aren't meant to be promises.
|
|
290
|
+
*/
|
|
291
|
+
declare function preventNativeAwait<T extends object>(target: T): T;
|
|
99
292
|
|
|
100
293
|
interface AsyncIdQueueCloseOptions {
|
|
101
|
-
id?:
|
|
102
|
-
reason?:
|
|
294
|
+
id?: string;
|
|
295
|
+
reason?: unknown;
|
|
103
296
|
}
|
|
104
297
|
declare class AsyncIdQueue<T> {
|
|
105
298
|
private readonly openIds;
|
|
106
|
-
private readonly
|
|
107
|
-
private readonly
|
|
299
|
+
private readonly queues;
|
|
300
|
+
private readonly waiters;
|
|
108
301
|
get length(): number;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
302
|
+
get waiterIds(): string[];
|
|
303
|
+
hasBufferedItems(id: string): boolean;
|
|
304
|
+
open(id: string): void;
|
|
305
|
+
isOpen(id: string): boolean;
|
|
306
|
+
push(id: string, item: T): void;
|
|
307
|
+
pull(id: string): Promise<T>;
|
|
113
308
|
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
114
|
-
assertOpen(id:
|
|
309
|
+
assertOpen(id: string): void;
|
|
115
310
|
}
|
|
116
311
|
|
|
312
|
+
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>;
|
|
313
|
+
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
314
|
+
|
|
315
|
+
declare function tryDecodeURIComponent(value: string): string;
|
|
316
|
+
|
|
117
317
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
318
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
319
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
121
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
320
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
321
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InferAsyncIterableYield, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -12,6 +12,24 @@ 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
|
+
|
|
22
|
+
const ORPC_NAME = "orpc";
|
|
23
|
+
const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
24
|
+
const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.fc1ae52";
|
|
25
|
+
|
|
26
|
+
class AbortError extends Error {
|
|
27
|
+
constructor(...rest) {
|
|
28
|
+
super(...rest);
|
|
29
|
+
this.name = "AbortError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
15
33
|
function once(fn) {
|
|
16
34
|
let cached;
|
|
17
35
|
return () => {
|
|
@@ -32,15 +50,418 @@ function sequential(fn) {
|
|
|
32
50
|
});
|
|
33
51
|
};
|
|
34
52
|
}
|
|
53
|
+
function defer(callback) {
|
|
54
|
+
if (typeof setTimeout === "function") {
|
|
55
|
+
setTimeout(callback, 0);
|
|
56
|
+
} else {
|
|
57
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const SPAN_ERROR_STATUS = 2;
|
|
62
|
+
const GLOBAL_OTEL_CONFIG_KEY = `__${ORPC_SHARED_PACKAGE_NAME}@${ORPC_SHARED_PACKAGE_VERSION}/otel/config__`;
|
|
63
|
+
function setGlobalOtelConfig(config) {
|
|
64
|
+
globalThis[GLOBAL_OTEL_CONFIG_KEY] = config;
|
|
65
|
+
}
|
|
66
|
+
function getGlobalOtelConfig() {
|
|
67
|
+
return globalThis[GLOBAL_OTEL_CONFIG_KEY];
|
|
68
|
+
}
|
|
69
|
+
function startSpan(name, options = {}, context) {
|
|
70
|
+
const tracer = getGlobalOtelConfig()?.tracer;
|
|
71
|
+
return tracer?.startSpan(name, options, context);
|
|
72
|
+
}
|
|
73
|
+
function setSpanError(span, error, options = {}) {
|
|
74
|
+
if (!span) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const exception = toOtelException(error);
|
|
78
|
+
span.recordException(exception);
|
|
79
|
+
if (!options.signal?.aborted || options.signal.reason !== error) {
|
|
80
|
+
span.setStatus({
|
|
81
|
+
code: SPAN_ERROR_STATUS,
|
|
82
|
+
message: exception.message
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function setSpanAttribute(span, key, value) {
|
|
87
|
+
if (!span || value === void 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
span.setAttribute(key, value);
|
|
91
|
+
}
|
|
92
|
+
function toOtelException(error) {
|
|
93
|
+
if (error instanceof Error) {
|
|
94
|
+
const exception = {
|
|
95
|
+
message: error.message,
|
|
96
|
+
name: error.name,
|
|
97
|
+
stack: error.stack
|
|
98
|
+
};
|
|
99
|
+
if ("code" in error && (typeof error.code === "string" || typeof error.code === "number")) {
|
|
100
|
+
exception.code = error.code;
|
|
101
|
+
}
|
|
102
|
+
return exception;
|
|
103
|
+
}
|
|
104
|
+
return { message: String(error) };
|
|
105
|
+
}
|
|
106
|
+
function toSpanAttributeValue(data) {
|
|
107
|
+
if (data === void 0) {
|
|
108
|
+
return "undefined";
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
return JSON.stringify(data, (_, value) => {
|
|
112
|
+
if (typeof value === "bigint") {
|
|
113
|
+
return value.toString();
|
|
114
|
+
}
|
|
115
|
+
if (value instanceof Map || value instanceof Set) {
|
|
116
|
+
return Array.from(value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
} catch {
|
|
121
|
+
return String(data);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function runWithSpan({ name, context, ...options }, fn) {
|
|
125
|
+
const tracer = getGlobalOtelConfig()?.tracer;
|
|
126
|
+
if (!tracer) {
|
|
127
|
+
return fn();
|
|
128
|
+
}
|
|
129
|
+
const callback = async (span) => {
|
|
130
|
+
try {
|
|
131
|
+
return await fn(span);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
setSpanError(span, e, options);
|
|
134
|
+
throw e;
|
|
135
|
+
} finally {
|
|
136
|
+
span.end();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
if (context) {
|
|
140
|
+
return tracer.startActiveSpan(name, options, context, callback);
|
|
141
|
+
} else {
|
|
142
|
+
return tracer.startActiveSpan(name, options, callback);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async function runInSpanContext(span, fn) {
|
|
146
|
+
const otelConfig = getGlobalOtelConfig();
|
|
147
|
+
if (!span || !otelConfig) {
|
|
148
|
+
return fn();
|
|
149
|
+
}
|
|
150
|
+
const ctx = otelConfig.trace.setSpan(otelConfig.context.active(), span);
|
|
151
|
+
return otelConfig.context.with(ctx, fn);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
class AsyncIdQueue {
|
|
155
|
+
openIds = /* @__PURE__ */ new Set();
|
|
156
|
+
queues = /* @__PURE__ */ new Map();
|
|
157
|
+
waiters = /* @__PURE__ */ new Map();
|
|
158
|
+
get length() {
|
|
159
|
+
return this.openIds.size;
|
|
160
|
+
}
|
|
161
|
+
get waiterIds() {
|
|
162
|
+
return Array.from(this.waiters.keys());
|
|
163
|
+
}
|
|
164
|
+
hasBufferedItems(id) {
|
|
165
|
+
return Boolean(this.queues.get(id)?.length);
|
|
166
|
+
}
|
|
167
|
+
open(id) {
|
|
168
|
+
this.openIds.add(id);
|
|
169
|
+
}
|
|
170
|
+
isOpen(id) {
|
|
171
|
+
return this.openIds.has(id);
|
|
172
|
+
}
|
|
173
|
+
push(id, item) {
|
|
174
|
+
this.assertOpen(id);
|
|
175
|
+
const pending = this.waiters.get(id);
|
|
176
|
+
if (pending?.length) {
|
|
177
|
+
pending.shift()[0](item);
|
|
178
|
+
if (pending.length === 0) {
|
|
179
|
+
this.waiters.delete(id);
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
const items = this.queues.get(id);
|
|
183
|
+
if (items) {
|
|
184
|
+
items.push(item);
|
|
185
|
+
} else {
|
|
186
|
+
this.queues.set(id, [item]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async pull(id) {
|
|
191
|
+
this.assertOpen(id);
|
|
192
|
+
const items = this.queues.get(id);
|
|
193
|
+
if (items?.length) {
|
|
194
|
+
const item = items.shift();
|
|
195
|
+
if (items.length === 0) {
|
|
196
|
+
this.queues.delete(id);
|
|
197
|
+
}
|
|
198
|
+
return item;
|
|
199
|
+
}
|
|
200
|
+
return new Promise((resolve, reject) => {
|
|
201
|
+
const waitingPulls = this.waiters.get(id);
|
|
202
|
+
const pending = [resolve, reject];
|
|
203
|
+
if (waitingPulls) {
|
|
204
|
+
waitingPulls.push(pending);
|
|
205
|
+
} else {
|
|
206
|
+
this.waiters.set(id, [pending]);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
close({ id, reason } = {}) {
|
|
211
|
+
if (id === void 0) {
|
|
212
|
+
this.waiters.forEach((pendingPulls, id2) => {
|
|
213
|
+
const error2 = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`);
|
|
214
|
+
pendingPulls.forEach(([, reject]) => reject(error2));
|
|
215
|
+
});
|
|
216
|
+
this.waiters.clear();
|
|
217
|
+
this.openIds.clear();
|
|
218
|
+
this.queues.clear();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
|
|
222
|
+
this.waiters.get(id)?.forEach(([, reject]) => reject(error));
|
|
223
|
+
this.waiters.delete(id);
|
|
224
|
+
this.openIds.delete(id);
|
|
225
|
+
this.queues.delete(id);
|
|
226
|
+
}
|
|
227
|
+
assertOpen(id) {
|
|
228
|
+
if (!this.isOpen(id)) {
|
|
229
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function isAsyncIteratorObject(maybe) {
|
|
235
|
+
if (!maybe || typeof maybe !== "object") {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
return "next" in maybe && typeof maybe.next === "function" && Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
239
|
+
}
|
|
240
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
241
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
242
|
+
class AsyncIteratorClass {
|
|
243
|
+
#isDone = false;
|
|
244
|
+
#isExecuteComplete = false;
|
|
245
|
+
#cleanup;
|
|
246
|
+
#next;
|
|
247
|
+
constructor(next, cleanup) {
|
|
248
|
+
this.#cleanup = cleanup;
|
|
249
|
+
this.#next = sequential(async () => {
|
|
250
|
+
if (this.#isDone) {
|
|
251
|
+
return { done: true, value: void 0 };
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const result = await next();
|
|
255
|
+
if (result.done) {
|
|
256
|
+
this.#isDone = true;
|
|
257
|
+
}
|
|
258
|
+
return result;
|
|
259
|
+
} catch (err) {
|
|
260
|
+
this.#isDone = true;
|
|
261
|
+
throw err;
|
|
262
|
+
} finally {
|
|
263
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
264
|
+
this.#isExecuteComplete = true;
|
|
265
|
+
await this.#cleanup("next");
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
next() {
|
|
271
|
+
return this.#next();
|
|
272
|
+
}
|
|
273
|
+
async return(value) {
|
|
274
|
+
this.#isDone = true;
|
|
275
|
+
if (!this.#isExecuteComplete) {
|
|
276
|
+
this.#isExecuteComplete = true;
|
|
277
|
+
await this.#cleanup("return");
|
|
278
|
+
}
|
|
279
|
+
return { done: true, value };
|
|
280
|
+
}
|
|
281
|
+
async throw(err) {
|
|
282
|
+
this.#isDone = true;
|
|
283
|
+
if (!this.#isExecuteComplete) {
|
|
284
|
+
this.#isExecuteComplete = true;
|
|
285
|
+
await this.#cleanup("throw");
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
291
|
+
*/
|
|
292
|
+
async [asyncDisposeSymbol]() {
|
|
293
|
+
this.#isDone = true;
|
|
294
|
+
if (!this.#isExecuteComplete) {
|
|
295
|
+
this.#isExecuteComplete = true;
|
|
296
|
+
await this.#cleanup("dispose");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
[Symbol.asyncIterator]() {
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function replicateAsyncIterator(source, count) {
|
|
304
|
+
const queue = new AsyncIdQueue();
|
|
305
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
306
|
+
let isSourceFinished = false;
|
|
307
|
+
const start = once(async () => {
|
|
308
|
+
try {
|
|
309
|
+
while (true) {
|
|
310
|
+
const item = await source.next();
|
|
311
|
+
ids.forEach((id) => {
|
|
312
|
+
if (queue.isOpen(id)) {
|
|
313
|
+
queue.push(id, { next: item });
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
if (item.done) {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ids.forEach((id) => {
|
|
322
|
+
if (queue.isOpen(id)) {
|
|
323
|
+
queue.push(id, { error });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
isSourceFinished = true;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
const replicated = ids.map((id) => {
|
|
331
|
+
queue.open(id);
|
|
332
|
+
return new AsyncIteratorClass(
|
|
333
|
+
async () => {
|
|
334
|
+
start();
|
|
335
|
+
const item = await queue.pull(id);
|
|
336
|
+
if (item.next) {
|
|
337
|
+
return item.next;
|
|
338
|
+
}
|
|
339
|
+
throw item.error;
|
|
340
|
+
},
|
|
341
|
+
async (reason) => {
|
|
342
|
+
queue.close({ id });
|
|
343
|
+
if (reason !== "next" && !queue.length && !isSourceFinished) {
|
|
344
|
+
isSourceFinished = true;
|
|
345
|
+
await source?.return?.();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
});
|
|
350
|
+
return replicated;
|
|
351
|
+
}
|
|
352
|
+
function asyncIteratorWithSpan({ name, ...options }, iterator) {
|
|
353
|
+
let span;
|
|
354
|
+
return new AsyncIteratorClass(
|
|
355
|
+
async () => {
|
|
356
|
+
span ??= startSpan(name);
|
|
357
|
+
try {
|
|
358
|
+
const result = await runInSpanContext(span, () => iterator.next());
|
|
359
|
+
span?.addEvent(result.done ? "completed" : "yielded");
|
|
360
|
+
return result;
|
|
361
|
+
} catch (err) {
|
|
362
|
+
setSpanError(span, err, options);
|
|
363
|
+
throw err;
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
async (reason) => {
|
|
367
|
+
try {
|
|
368
|
+
if (reason !== "next") {
|
|
369
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
370
|
+
}
|
|
371
|
+
} catch (err) {
|
|
372
|
+
setSpanError(span, err, options);
|
|
373
|
+
throw err;
|
|
374
|
+
} finally {
|
|
375
|
+
span?.end();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
class EventPublisher {
|
|
382
|
+
#listenersMap = /* @__PURE__ */ new Map();
|
|
383
|
+
#maxBufferedEvents;
|
|
384
|
+
constructor(options = {}) {
|
|
385
|
+
this.#maxBufferedEvents = options.maxBufferedEvents ?? 100;
|
|
386
|
+
}
|
|
387
|
+
get size() {
|
|
388
|
+
return this.#listenersMap.size;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
392
|
+
*/
|
|
393
|
+
publish(event, payload) {
|
|
394
|
+
const listeners = this.#listenersMap.get(event);
|
|
395
|
+
if (!listeners) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
for (const listener of listeners) {
|
|
399
|
+
listener(payload);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
subscribe(event, listenerOrOptions) {
|
|
403
|
+
if (typeof listenerOrOptions === "function") {
|
|
404
|
+
let listeners = this.#listenersMap.get(event);
|
|
405
|
+
if (!listeners) {
|
|
406
|
+
this.#listenersMap.set(event, listeners = /* @__PURE__ */ new Set());
|
|
407
|
+
}
|
|
408
|
+
listeners.add(listenerOrOptions);
|
|
409
|
+
return () => {
|
|
410
|
+
listeners.delete(listenerOrOptions);
|
|
411
|
+
if (listeners.size === 0) {
|
|
412
|
+
this.#listenersMap.delete(event);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
const signal = listenerOrOptions?.signal;
|
|
417
|
+
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
418
|
+
signal?.throwIfAborted();
|
|
419
|
+
const bufferedEvents = [];
|
|
420
|
+
const pullResolvers = [];
|
|
421
|
+
const unsubscribe = this.subscribe(event, (payload) => {
|
|
422
|
+
const resolver = pullResolvers.shift();
|
|
423
|
+
if (resolver) {
|
|
424
|
+
resolver[0]({ done: false, value: payload });
|
|
425
|
+
} else {
|
|
426
|
+
bufferedEvents.push(payload);
|
|
427
|
+
if (bufferedEvents.length > maxBufferedEvents) {
|
|
428
|
+
bufferedEvents.shift();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
const abortListener = (event2) => {
|
|
433
|
+
unsubscribe();
|
|
434
|
+
pullResolvers.forEach((resolver) => resolver[1](event2.target.reason));
|
|
435
|
+
pullResolvers.length = 0;
|
|
436
|
+
bufferedEvents.length = 0;
|
|
437
|
+
};
|
|
438
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
439
|
+
return new AsyncIteratorClass(async () => {
|
|
440
|
+
if (signal?.aborted) {
|
|
441
|
+
throw signal.reason;
|
|
442
|
+
}
|
|
443
|
+
if (bufferedEvents.length > 0) {
|
|
444
|
+
return { done: false, value: bufferedEvents.shift() };
|
|
445
|
+
}
|
|
446
|
+
return new Promise((resolve, reject) => {
|
|
447
|
+
pullResolvers.push([resolve, reject]);
|
|
448
|
+
});
|
|
449
|
+
}, async () => {
|
|
450
|
+
unsubscribe();
|
|
451
|
+
signal?.removeEventListener("abort", abortListener);
|
|
452
|
+
pullResolvers.forEach((resolver) => resolver[0]({ done: true, value: void 0 }));
|
|
453
|
+
pullResolvers.length = 0;
|
|
454
|
+
bufferedEvents.length = 0;
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
35
458
|
|
|
36
459
|
class SequentialIdGenerator {
|
|
37
|
-
|
|
460
|
+
index = BigInt(0);
|
|
38
461
|
generate() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
return this.nextId++;
|
|
462
|
+
const id = this.index.toString(32);
|
|
463
|
+
this.index++;
|
|
464
|
+
return id;
|
|
44
465
|
}
|
|
45
466
|
}
|
|
46
467
|
|
|
@@ -96,69 +517,6 @@ function intercept(interceptors, options, main) {
|
|
|
96
517
|
return next(options, 0);
|
|
97
518
|
}
|
|
98
519
|
|
|
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
520
|
function parseEmptyableJSON(text) {
|
|
163
521
|
if (!text) {
|
|
164
522
|
return void 0;
|
|
@@ -184,6 +542,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
184
542
|
}
|
|
185
543
|
return { maps, values };
|
|
186
544
|
}
|
|
545
|
+
function getConstructor(value) {
|
|
546
|
+
if (!isTypescriptObject(value)) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
550
|
+
}
|
|
187
551
|
function isObject(value) {
|
|
188
552
|
if (!value || typeof value !== "object") {
|
|
189
553
|
return false;
|
|
@@ -221,80 +585,78 @@ function isPropertyKey(value) {
|
|
|
221
585
|
const type = typeof value;
|
|
222
586
|
return type === "string" || type === "number" || type === "symbol";
|
|
223
587
|
}
|
|
588
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
589
|
+
const e = function() {
|
|
590
|
+
};
|
|
591
|
+
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
592
|
+
Object.freeze(e.prototype);
|
|
593
|
+
return e;
|
|
594
|
+
})();
|
|
224
595
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
open(id) {
|
|
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]);
|
|
596
|
+
function preventNativeAwait(target) {
|
|
597
|
+
return new Proxy(target, {
|
|
598
|
+
get(target2, prop, receiver) {
|
|
599
|
+
const value = Reflect.get(target2, prop, receiver);
|
|
600
|
+
if (prop !== "then" || typeof value !== "function") {
|
|
601
|
+
return value;
|
|
252
602
|
}
|
|
603
|
+
return new Proxy(value, {
|
|
604
|
+
apply(targetFn, thisArg, args) {
|
|
605
|
+
if (args.length !== 2 || args.some((arg) => !isNativeFunction(arg))) {
|
|
606
|
+
return Reflect.apply(targetFn, thisArg, args);
|
|
607
|
+
}
|
|
608
|
+
let shouldOmit = true;
|
|
609
|
+
args[0].call(thisArg, preventNativeAwait(new Proxy(target2, {
|
|
610
|
+
get: (target3, prop2, receiver2) => {
|
|
611
|
+
if (shouldOmit && prop2 === "then") {
|
|
612
|
+
shouldOmit = false;
|
|
613
|
+
return void 0;
|
|
614
|
+
}
|
|
615
|
+
return Reflect.get(target3, prop2, receiver2);
|
|
616
|
+
}
|
|
617
|
+
})));
|
|
618
|
+
}
|
|
619
|
+
});
|
|
253
620
|
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
const NATIVE_FUNCTION_REGEX = /^\s*function\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/;
|
|
624
|
+
function isNativeFunction(fn) {
|
|
625
|
+
return typeof fn === "function" && NATIVE_FUNCTION_REGEX.test(fn.toString());
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function streamToAsyncIteratorClass(stream) {
|
|
629
|
+
const reader = stream.getReader();
|
|
630
|
+
return new AsyncIteratorClass(
|
|
631
|
+
async () => {
|
|
632
|
+
return reader.read();
|
|
633
|
+
},
|
|
634
|
+
async () => {
|
|
635
|
+
await reader.cancel();
|
|
264
636
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
function asyncIteratorToStream(iterator) {
|
|
640
|
+
return new ReadableStream({
|
|
641
|
+
async pull(controller) {
|
|
642
|
+
const { done, value } = await iterator.next();
|
|
643
|
+
if (done) {
|
|
644
|
+
controller.close();
|
|
270
645
|
} else {
|
|
271
|
-
|
|
646
|
+
controller.enqueue(value);
|
|
272
647
|
}
|
|
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.`);
|
|
648
|
+
},
|
|
649
|
+
async cancel() {
|
|
650
|
+
await iterator.return?.();
|
|
297
651
|
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function tryDecodeURIComponent(value) {
|
|
656
|
+
try {
|
|
657
|
+
return decodeURIComponent(value);
|
|
658
|
+
} catch {
|
|
659
|
+
return value;
|
|
298
660
|
}
|
|
299
661
|
}
|
|
300
662
|
|
|
@@ -305,4 +667,4 @@ function value(value2, ...args) {
|
|
|
305
667
|
return value2;
|
|
306
668
|
}
|
|
307
669
|
|
|
308
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone,
|
|
670
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, 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.fc1ae52",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,10 +23,24 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@opentelemetry/api": ">=1.9.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@opentelemetry/api": {
|
|
31
|
+
"optional": true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
26
34
|
"dependencies": {
|
|
27
|
-
"radash": "^12.1.
|
|
35
|
+
"radash": "^12.1.1",
|
|
28
36
|
"type-fest": "^4.39.1"
|
|
29
37
|
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
40
|
+
"arktype": "2.1.21",
|
|
41
|
+
"valibot": "^1.1.0",
|
|
42
|
+
"zod": "^4.1.5"
|
|
43
|
+
},
|
|
30
44
|
"scripts": {
|
|
31
45
|
"build": "unbuild",
|
|
32
46
|
"build:watch": "pnpm run build --watch",
|