@orpc/shared 0.0.0-next.eb3d98a → 0.0.0-next.ebd6d63
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 +7 -2
- package/dist/index.d.mts +124 -9
- package/dist/index.d.ts +124 -9
- package/dist/index.mjs +249 -57
- package/package.json +13 -4
package/README.md
CHANGED
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
18
|
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
19
|
</a>
|
|
20
|
+
<a href="https://deepwiki.com/unnoq/orpc">
|
|
21
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
22
|
+
</a>
|
|
20
23
|
</div>
|
|
21
24
|
|
|
22
25
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
@@ -30,7 +33,8 @@
|
|
|
30
33
|
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
31
34
|
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
32
35
|
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
33
|
-
-
|
|
36
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
37
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
34
38
|
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
35
39
|
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
36
40
|
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
@@ -38,7 +42,6 @@
|
|
|
38
42
|
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
39
43
|
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
40
44
|
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
41
|
-
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
|
42
45
|
|
|
43
46
|
## Documentation
|
|
44
47
|
|
|
@@ -50,9 +53,11 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
50
53
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
51
54
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
55
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
56
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
53
57
|
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
54
58
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
55
59
|
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
60
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
56
61
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
57
62
|
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
58
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
2
|
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest';
|
|
3
|
-
|
|
3
|
+
import { Tracer, TraceAPI, ContextAPI, PropagationAPI, SpanOptions, Context, Span, AttributeValue, Exception } from '@opentelemetry/api';
|
|
4
|
+
export { group, guard, mapEntries, mapValues, omit, retry, sleep } from 'radash';
|
|
4
5
|
|
|
5
6
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
6
7
|
declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
|
|
@@ -27,6 +28,18 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
27
28
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
28
29
|
};
|
|
29
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.ebd6d63";
|
|
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
|
+
|
|
30
43
|
interface EventPublisherOptions {
|
|
31
44
|
/**
|
|
32
45
|
* Maximum number of events to buffer for async iterator subscribers.
|
|
@@ -47,7 +60,7 @@ interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
|
47
60
|
/**
|
|
48
61
|
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
49
62
|
*/
|
|
50
|
-
signal?: AbortSignal;
|
|
63
|
+
signal?: AbortSignal | undefined;
|
|
51
64
|
}
|
|
52
65
|
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
53
66
|
#private;
|
|
@@ -108,6 +121,7 @@ interface Registry {
|
|
|
108
121
|
type ThrowableError = Registry extends {
|
|
109
122
|
throwableError: infer T;
|
|
110
123
|
} ? T : Error;
|
|
124
|
+
type InferAsyncIterableYield<T> = T extends AsyncIterable<infer U> ? U : never;
|
|
111
125
|
|
|
112
126
|
type InterceptableOptions = Record<string, any>;
|
|
113
127
|
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
@@ -141,6 +155,72 @@ declare function onFinish<T, TOptions extends {
|
|
|
141
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']>>>;
|
|
142
156
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
143
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
|
+
|
|
144
224
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
145
225
|
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
226
|
(): Promise<IteratorResult<T, TReturn>>;
|
|
@@ -165,6 +245,13 @@ declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implemen
|
|
|
165
245
|
[Symbol.asyncIterator](): this;
|
|
166
246
|
}
|
|
167
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>;
|
|
168
255
|
|
|
169
256
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
170
257
|
declare function stringifyJSON<T>(value: T | {
|
|
@@ -176,6 +263,11 @@ declare function findDeepMatches(check: (value: unknown) => boolean, payload: un
|
|
|
176
263
|
maps: Segment[][];
|
|
177
264
|
values: unknown[];
|
|
178
265
|
};
|
|
266
|
+
/**
|
|
267
|
+
* Get constructor of the value
|
|
268
|
+
*
|
|
269
|
+
*/
|
|
270
|
+
declare function getConstructor(value: unknown): Function | null | undefined;
|
|
179
271
|
/**
|
|
180
272
|
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
181
273
|
*/
|
|
@@ -191,15 +283,41 @@ declare const NullProtoObj: ({
|
|
|
191
283
|
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
192
284
|
});
|
|
193
285
|
|
|
286
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
287
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
288
|
+
/**
|
|
289
|
+
* Returns the value if it is defined, otherwise returns the fallback
|
|
290
|
+
*/
|
|
291
|
+
declare function fallback<T>(value: T | undefined, fallback: T): T;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Prevents objects from being awaitable by intercepting the `then` method
|
|
295
|
+
* when called by the native await mechanism. This is useful for preventing
|
|
296
|
+
* accidental awaiting of objects that aren't meant to be promises.
|
|
297
|
+
*/
|
|
298
|
+
declare function preventNativeAwait<T extends object>(target: T): T;
|
|
299
|
+
/**
|
|
300
|
+
* Create a proxy that overlays one object (`overlay`) on top of another (`target`).
|
|
301
|
+
*
|
|
302
|
+
* - Properties from `overlay` take precedence.
|
|
303
|
+
* - Properties not in `overlay` fall back to `target`.
|
|
304
|
+
* - Methods from either object are bound to `overlay` so `this` is consistent.
|
|
305
|
+
*
|
|
306
|
+
* Useful when you want to override or extend behavior without fully copying/merging objects.
|
|
307
|
+
*/
|
|
308
|
+
declare function overlayProxy<T extends object, U extends object>(target: Value<T>, partial: U): U & Omit<T, keyof U>;
|
|
309
|
+
|
|
194
310
|
interface AsyncIdQueueCloseOptions {
|
|
195
311
|
id?: string;
|
|
196
312
|
reason?: unknown;
|
|
197
313
|
}
|
|
198
314
|
declare class AsyncIdQueue<T> {
|
|
199
315
|
private readonly openIds;
|
|
200
|
-
private readonly
|
|
201
|
-
private readonly
|
|
316
|
+
private readonly queues;
|
|
317
|
+
private readonly waiters;
|
|
202
318
|
get length(): number;
|
|
319
|
+
get waiterIds(): string[];
|
|
320
|
+
hasBufferedItems(id: string): boolean;
|
|
203
321
|
open(id: string): void;
|
|
204
322
|
isOpen(id: string): boolean;
|
|
205
323
|
push(id: string, item: T): void;
|
|
@@ -213,8 +331,5 @@ declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableS
|
|
|
213
331
|
|
|
214
332
|
declare function tryDecodeURIComponent(value: string): string;
|
|
215
333
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, tryDecodeURIComponent, value };
|
|
220
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
|
334
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, fallback, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, overlayProxy, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
335
|
+
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,6 +1,7 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
2
|
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest';
|
|
3
|
-
|
|
3
|
+
import { Tracer, TraceAPI, ContextAPI, PropagationAPI, SpanOptions, Context, Span, AttributeValue, Exception } from '@opentelemetry/api';
|
|
4
|
+
export { group, guard, mapEntries, mapValues, omit, retry, sleep } from 'radash';
|
|
4
5
|
|
|
5
6
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
6
7
|
declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
|
|
@@ -27,6 +28,18 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
27
28
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
28
29
|
};
|
|
29
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.ebd6d63";
|
|
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
|
+
|
|
30
43
|
interface EventPublisherOptions {
|
|
31
44
|
/**
|
|
32
45
|
* Maximum number of events to buffer for async iterator subscribers.
|
|
@@ -47,7 +60,7 @@ interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
|
47
60
|
/**
|
|
48
61
|
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
49
62
|
*/
|
|
50
|
-
signal?: AbortSignal;
|
|
63
|
+
signal?: AbortSignal | undefined;
|
|
51
64
|
}
|
|
52
65
|
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
53
66
|
#private;
|
|
@@ -108,6 +121,7 @@ interface Registry {
|
|
|
108
121
|
type ThrowableError = Registry extends {
|
|
109
122
|
throwableError: infer T;
|
|
110
123
|
} ? T : Error;
|
|
124
|
+
type InferAsyncIterableYield<T> = T extends AsyncIterable<infer U> ? U : never;
|
|
111
125
|
|
|
112
126
|
type InterceptableOptions = Record<string, any>;
|
|
113
127
|
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
@@ -141,6 +155,72 @@ declare function onFinish<T, TOptions extends {
|
|
|
141
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']>>>;
|
|
142
156
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
143
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
|
+
|
|
144
224
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
145
225
|
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
226
|
(): Promise<IteratorResult<T, TReturn>>;
|
|
@@ -165,6 +245,13 @@ declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implemen
|
|
|
165
245
|
[Symbol.asyncIterator](): this;
|
|
166
246
|
}
|
|
167
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>;
|
|
168
255
|
|
|
169
256
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
170
257
|
declare function stringifyJSON<T>(value: T | {
|
|
@@ -176,6 +263,11 @@ declare function findDeepMatches(check: (value: unknown) => boolean, payload: un
|
|
|
176
263
|
maps: Segment[][];
|
|
177
264
|
values: unknown[];
|
|
178
265
|
};
|
|
266
|
+
/**
|
|
267
|
+
* Get constructor of the value
|
|
268
|
+
*
|
|
269
|
+
*/
|
|
270
|
+
declare function getConstructor(value: unknown): Function | null | undefined;
|
|
179
271
|
/**
|
|
180
272
|
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
181
273
|
*/
|
|
@@ -191,15 +283,41 @@ declare const NullProtoObj: ({
|
|
|
191
283
|
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
192
284
|
});
|
|
193
285
|
|
|
286
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
287
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
288
|
+
/**
|
|
289
|
+
* Returns the value if it is defined, otherwise returns the fallback
|
|
290
|
+
*/
|
|
291
|
+
declare function fallback<T>(value: T | undefined, fallback: T): T;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Prevents objects from being awaitable by intercepting the `then` method
|
|
295
|
+
* when called by the native await mechanism. This is useful for preventing
|
|
296
|
+
* accidental awaiting of objects that aren't meant to be promises.
|
|
297
|
+
*/
|
|
298
|
+
declare function preventNativeAwait<T extends object>(target: T): T;
|
|
299
|
+
/**
|
|
300
|
+
* Create a proxy that overlays one object (`overlay`) on top of another (`target`).
|
|
301
|
+
*
|
|
302
|
+
* - Properties from `overlay` take precedence.
|
|
303
|
+
* - Properties not in `overlay` fall back to `target`.
|
|
304
|
+
* - Methods from either object are bound to `overlay` so `this` is consistent.
|
|
305
|
+
*
|
|
306
|
+
* Useful when you want to override or extend behavior without fully copying/merging objects.
|
|
307
|
+
*/
|
|
308
|
+
declare function overlayProxy<T extends object, U extends object>(target: Value<T>, partial: U): U & Omit<T, keyof U>;
|
|
309
|
+
|
|
194
310
|
interface AsyncIdQueueCloseOptions {
|
|
195
311
|
id?: string;
|
|
196
312
|
reason?: unknown;
|
|
197
313
|
}
|
|
198
314
|
declare class AsyncIdQueue<T> {
|
|
199
315
|
private readonly openIds;
|
|
200
|
-
private readonly
|
|
201
|
-
private readonly
|
|
316
|
+
private readonly queues;
|
|
317
|
+
private readonly waiters;
|
|
202
318
|
get length(): number;
|
|
319
|
+
get waiterIds(): string[];
|
|
320
|
+
hasBufferedItems(id: string): boolean;
|
|
203
321
|
open(id: string): void;
|
|
204
322
|
isOpen(id: string): boolean;
|
|
205
323
|
push(id: string, item: T): void;
|
|
@@ -213,8 +331,5 @@ declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableS
|
|
|
213
331
|
|
|
214
332
|
declare function tryDecodeURIComponent(value: string): string;
|
|
215
333
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, tryDecodeURIComponent, value };
|
|
220
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
|
334
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, fallback, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, overlayProxy, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
335
|
+
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
1
|
+
export { group, guard, mapEntries, mapValues, omit, retry, sleep } from 'radash';
|
|
2
2
|
|
|
3
3
|
function resolveMaybeOptionalOptions(rest) {
|
|
4
4
|
return rest[0] ?? {};
|
|
@@ -19,6 +19,17 @@ function readAsBuffer(source) {
|
|
|
19
19
|
return source.arrayBuffer();
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
const ORPC_NAME = "orpc";
|
|
23
|
+
const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
24
|
+
const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.ebd6d63";
|
|
25
|
+
|
|
26
|
+
class AbortError extends Error {
|
|
27
|
+
constructor(...rest) {
|
|
28
|
+
super(...rest);
|
|
29
|
+
this.name = "AbortError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
function once(fn) {
|
|
23
34
|
let cached;
|
|
24
35
|
return () => {
|
|
@@ -47,13 +58,112 @@ function defer(callback) {
|
|
|
47
58
|
}
|
|
48
59
|
}
|
|
49
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
|
+
|
|
50
154
|
class AsyncIdQueue {
|
|
51
155
|
openIds = /* @__PURE__ */ new Set();
|
|
52
|
-
|
|
53
|
-
|
|
156
|
+
queues = /* @__PURE__ */ new Map();
|
|
157
|
+
waiters = /* @__PURE__ */ new Map();
|
|
54
158
|
get length() {
|
|
55
159
|
return this.openIds.size;
|
|
56
160
|
}
|
|
161
|
+
get waiterIds() {
|
|
162
|
+
return Array.from(this.waiters.keys());
|
|
163
|
+
}
|
|
164
|
+
hasBufferedItems(id) {
|
|
165
|
+
return Boolean(this.queues.get(id)?.length);
|
|
166
|
+
}
|
|
57
167
|
open(id) {
|
|
58
168
|
this.openIds.add(id);
|
|
59
169
|
}
|
|
@@ -62,59 +172,57 @@ class AsyncIdQueue {
|
|
|
62
172
|
}
|
|
63
173
|
push(id, item) {
|
|
64
174
|
this.assertOpen(id);
|
|
65
|
-
const pending = this.
|
|
175
|
+
const pending = this.waiters.get(id);
|
|
66
176
|
if (pending?.length) {
|
|
67
177
|
pending.shift()[0](item);
|
|
68
178
|
if (pending.length === 0) {
|
|
69
|
-
this.
|
|
179
|
+
this.waiters.delete(id);
|
|
70
180
|
}
|
|
71
181
|
} else {
|
|
72
|
-
const items = this.
|
|
182
|
+
const items = this.queues.get(id);
|
|
73
183
|
if (items) {
|
|
74
184
|
items.push(item);
|
|
75
185
|
} else {
|
|
76
|
-
this.
|
|
186
|
+
this.queues.set(id, [item]);
|
|
77
187
|
}
|
|
78
188
|
}
|
|
79
189
|
}
|
|
80
190
|
async pull(id) {
|
|
81
191
|
this.assertOpen(id);
|
|
82
|
-
const items = this.
|
|
192
|
+
const items = this.queues.get(id);
|
|
83
193
|
if (items?.length) {
|
|
84
194
|
const item = items.shift();
|
|
85
195
|
if (items.length === 0) {
|
|
86
|
-
this.
|
|
196
|
+
this.queues.delete(id);
|
|
87
197
|
}
|
|
88
198
|
return item;
|
|
89
199
|
}
|
|
90
200
|
return new Promise((resolve, reject) => {
|
|
91
|
-
const waitingPulls = this.
|
|
201
|
+
const waitingPulls = this.waiters.get(id);
|
|
92
202
|
const pending = [resolve, reject];
|
|
93
203
|
if (waitingPulls) {
|
|
94
204
|
waitingPulls.push(pending);
|
|
95
205
|
} else {
|
|
96
|
-
this.
|
|
206
|
+
this.waiters.set(id, [pending]);
|
|
97
207
|
}
|
|
98
208
|
});
|
|
99
209
|
}
|
|
100
210
|
close({ id, reason } = {}) {
|
|
101
211
|
if (id === void 0) {
|
|
102
|
-
this.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
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));
|
|
106
215
|
});
|
|
107
|
-
this.
|
|
216
|
+
this.waiters.clear();
|
|
108
217
|
this.openIds.clear();
|
|
109
|
-
this.
|
|
218
|
+
this.queues.clear();
|
|
110
219
|
return;
|
|
111
220
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.pendingPulls.delete(id);
|
|
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);
|
|
116
224
|
this.openIds.delete(id);
|
|
117
|
-
this.
|
|
225
|
+
this.queues.delete(id);
|
|
118
226
|
}
|
|
119
227
|
assertOpen(id) {
|
|
120
228
|
if (!this.isOpen(id)) {
|
|
@@ -194,51 +302,81 @@ class AsyncIteratorClass {
|
|
|
194
302
|
}
|
|
195
303
|
function replicateAsyncIterator(source, count) {
|
|
196
304
|
const queue = new AsyncIdQueue();
|
|
197
|
-
const
|
|
198
|
-
let
|
|
305
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
306
|
+
let isSourceFinished = false;
|
|
199
307
|
const start = once(async () => {
|
|
200
308
|
try {
|
|
201
309
|
while (true) {
|
|
202
310
|
const item = await source.next();
|
|
203
|
-
|
|
204
|
-
if (queue.isOpen(id
|
|
205
|
-
queue.push(id
|
|
311
|
+
ids.forEach((id) => {
|
|
312
|
+
if (queue.isOpen(id)) {
|
|
313
|
+
queue.push(id, { next: item });
|
|
206
314
|
}
|
|
207
|
-
}
|
|
315
|
+
});
|
|
208
316
|
if (item.done) {
|
|
209
317
|
break;
|
|
210
318
|
}
|
|
211
319
|
}
|
|
212
|
-
} catch (
|
|
213
|
-
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ids.forEach((id) => {
|
|
322
|
+
if (queue.isOpen(id)) {
|
|
323
|
+
queue.push(id, { error });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
isSourceFinished = true;
|
|
214
328
|
}
|
|
215
329
|
});
|
|
216
|
-
|
|
217
|
-
queue.open(id
|
|
218
|
-
|
|
219
|
-
() => {
|
|
330
|
+
const replicated = ids.map((id) => {
|
|
331
|
+
queue.open(id);
|
|
332
|
+
return new AsyncIteratorClass(
|
|
333
|
+
async () => {
|
|
220
334
|
start();
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
});
|
|
335
|
+
const item = await queue.pull(id);
|
|
336
|
+
if (item.next) {
|
|
337
|
+
return item.next;
|
|
338
|
+
}
|
|
339
|
+
throw item.error;
|
|
229
340
|
},
|
|
230
341
|
async (reason) => {
|
|
231
|
-
queue.close({ id
|
|
232
|
-
if (reason !== "next") {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
342
|
+
queue.close({ id });
|
|
343
|
+
if (reason !== "next" && !queue.length && !isSourceFinished) {
|
|
344
|
+
isSourceFinished = true;
|
|
345
|
+
await source?.return?.();
|
|
236
346
|
}
|
|
237
347
|
}
|
|
238
|
-
)
|
|
239
|
-
}
|
|
348
|
+
);
|
|
349
|
+
});
|
|
240
350
|
return replicated;
|
|
241
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
|
+
}
|
|
242
380
|
|
|
243
381
|
class EventPublisher {
|
|
244
382
|
#listenersMap = /* @__PURE__ */ new Map();
|
|
@@ -404,6 +542,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
404
542
|
}
|
|
405
543
|
return { maps, values };
|
|
406
544
|
}
|
|
545
|
+
function getConstructor(value) {
|
|
546
|
+
if (!isTypescriptObject(value)) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
550
|
+
}
|
|
407
551
|
function isObject(value) {
|
|
408
552
|
if (!value || typeof value !== "object") {
|
|
409
553
|
return false;
|
|
@@ -449,6 +593,61 @@ const NullProtoObj = /* @__PURE__ */ (() => {
|
|
|
449
593
|
return e;
|
|
450
594
|
})();
|
|
451
595
|
|
|
596
|
+
function value(value2, ...args) {
|
|
597
|
+
if (typeof value2 === "function") {
|
|
598
|
+
return value2(...args);
|
|
599
|
+
}
|
|
600
|
+
return value2;
|
|
601
|
+
}
|
|
602
|
+
function fallback(value2, fallback2) {
|
|
603
|
+
return value2 === void 0 ? fallback2 : value2;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function preventNativeAwait(target) {
|
|
607
|
+
return new Proxy(target, {
|
|
608
|
+
get(target2, prop, receiver) {
|
|
609
|
+
const value2 = Reflect.get(target2, prop, receiver);
|
|
610
|
+
if (prop !== "then" || typeof value2 !== "function") {
|
|
611
|
+
return value2;
|
|
612
|
+
}
|
|
613
|
+
return new Proxy(value2, {
|
|
614
|
+
apply(targetFn, thisArg, args) {
|
|
615
|
+
if (args.length !== 2 || args.some((arg) => !isNativeFunction(arg))) {
|
|
616
|
+
return Reflect.apply(targetFn, thisArg, args);
|
|
617
|
+
}
|
|
618
|
+
let shouldOmit = true;
|
|
619
|
+
args[0].call(thisArg, preventNativeAwait(new Proxy(target2, {
|
|
620
|
+
get: (target3, prop2, receiver2) => {
|
|
621
|
+
if (shouldOmit && prop2 === "then") {
|
|
622
|
+
shouldOmit = false;
|
|
623
|
+
return void 0;
|
|
624
|
+
}
|
|
625
|
+
return Reflect.get(target3, prop2, receiver2);
|
|
626
|
+
}
|
|
627
|
+
})));
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
const NATIVE_FUNCTION_REGEX = /^\s*function\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/;
|
|
634
|
+
function isNativeFunction(fn) {
|
|
635
|
+
return typeof fn === "function" && NATIVE_FUNCTION_REGEX.test(fn.toString());
|
|
636
|
+
}
|
|
637
|
+
function overlayProxy(target, partial) {
|
|
638
|
+
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
639
|
+
get(_, prop) {
|
|
640
|
+
const targetValue = prop in partial ? partial : value(target);
|
|
641
|
+
const v = Reflect.get(targetValue, prop);
|
|
642
|
+
return typeof v === "function" ? v.bind(targetValue) : v;
|
|
643
|
+
},
|
|
644
|
+
has(_, prop) {
|
|
645
|
+
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
return proxy;
|
|
649
|
+
}
|
|
650
|
+
|
|
452
651
|
function streamToAsyncIteratorClass(stream) {
|
|
453
652
|
const reader = stream.getReader();
|
|
454
653
|
return new AsyncIteratorClass(
|
|
@@ -484,11 +683,4 @@ function tryDecodeURIComponent(value) {
|
|
|
484
683
|
}
|
|
485
684
|
}
|
|
486
685
|
|
|
487
|
-
|
|
488
|
-
if (typeof value2 === "function") {
|
|
489
|
-
return value2(...args);
|
|
490
|
-
}
|
|
491
|
-
return value2;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, tryDecodeURIComponent, value };
|
|
686
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, fallback, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, overlayProxy, 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.ebd6d63",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,14 +23,23 @@
|
|
|
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
35
|
"radash": "^12.1.1",
|
|
28
|
-
"type-fest": "^
|
|
36
|
+
"type-fest": "^5.0.1"
|
|
29
37
|
},
|
|
30
38
|
"devDependencies": {
|
|
31
|
-
"
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
40
|
+
"arktype": "2.1.22",
|
|
32
41
|
"valibot": "^1.1.0",
|
|
33
|
-
"zod": "^4.
|
|
42
|
+
"zod": "^4.1.11"
|
|
34
43
|
},
|
|
35
44
|
"scripts": {
|
|
36
45
|
"build": "unbuild",
|