@orpc/shared 1.7.10 → 1.8.0
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 +2 -1
- package/dist/index.d.mts +88 -2
- package/dist/index.d.ts +88 -2
- package/dist/index.mjs +133 -1
- package/package.json +10 -1
package/README.md
CHANGED
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
31
31
|
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
32
32
|
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
33
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
33
34
|
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
|
|
34
35
|
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
35
36
|
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
@@ -38,7 +39,6 @@
|
|
|
38
39
|
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
39
40
|
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
40
41
|
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
41
|
-
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
|
42
42
|
|
|
43
43
|
## Documentation
|
|
44
44
|
|
|
@@ -50,6 +50,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
50
50
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
51
51
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
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.
|
|
53
54
|
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
54
55
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
55
56
|
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
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];
|
|
@@ -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 = "1.8.0";
|
|
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.
|
|
@@ -141,6 +154,72 @@ declare function onFinish<T, TOptions extends {
|
|
|
141
154
|
}, 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
155
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
143
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Only import types from @opentelemetry/api to avoid runtime dependencies.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
interface OtelConfig {
|
|
162
|
+
tracer: Tracer;
|
|
163
|
+
trace: TraceAPI;
|
|
164
|
+
context: ContextAPI;
|
|
165
|
+
propagation: PropagationAPI;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sets the global OpenTelemetry config.
|
|
169
|
+
* Call this once at app startup. Use `undefined` to disable tracing.
|
|
170
|
+
*/
|
|
171
|
+
declare function setGlobalOtelConfig(config: OtelConfig | undefined): void;
|
|
172
|
+
/**
|
|
173
|
+
* Gets the global OpenTelemetry config.
|
|
174
|
+
* Returns `undefined` if OpenTelemetry is not configured, initialized, or enabled.
|
|
175
|
+
*/
|
|
176
|
+
declare function getGlobalOtelConfig(): OtelConfig | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* Starts a new OpenTelemetry span with the given name and options.
|
|
179
|
+
*
|
|
180
|
+
* @returns The new span, or `undefined` if no tracer is set.
|
|
181
|
+
*/
|
|
182
|
+
declare function startSpan(name: string, options?: SpanOptions, context?: Context): Span | undefined;
|
|
183
|
+
interface SetSpanErrorOptions {
|
|
184
|
+
/**
|
|
185
|
+
* Span error status is not set if error is due to cancellation by the signal.
|
|
186
|
+
*/
|
|
187
|
+
signal?: AbortSignal;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Records and sets the error status on the given span.
|
|
191
|
+
* If the span is `undefined`, it does nothing.
|
|
192
|
+
*/
|
|
193
|
+
declare function setSpanError(span: Span | undefined, error: unknown, options?: SetSpanErrorOptions): void;
|
|
194
|
+
declare function setSpanAttribute(span: Span | undefined, key: string, value: AttributeValue | undefined): void;
|
|
195
|
+
/**
|
|
196
|
+
* Converts an error to an OpenTelemetry Exception.
|
|
197
|
+
*/
|
|
198
|
+
declare function toOtelException(error: unknown): Exclude<Exception, string>;
|
|
199
|
+
/**
|
|
200
|
+
* Converts a value to a string suitable for OpenTelemetry span attributes.
|
|
201
|
+
*/
|
|
202
|
+
declare function toSpanAttributeValue(data: unknown): string;
|
|
203
|
+
interface RunWithSpanOptions extends SpanOptions, SetSpanErrorOptions {
|
|
204
|
+
/**
|
|
205
|
+
* The name of the span to create.
|
|
206
|
+
*/
|
|
207
|
+
name: string;
|
|
208
|
+
/**
|
|
209
|
+
* Context to use for the span.
|
|
210
|
+
*/
|
|
211
|
+
context?: Context;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Runs a function within the context of a new OpenTelemetry span.
|
|
215
|
+
* The span is ended automatically, and errors are recorded to the span.
|
|
216
|
+
*/
|
|
217
|
+
declare function runWithSpan<T>({ name, context, ...options }: RunWithSpanOptions, fn: (span?: Span) => Promisable<T>): Promise<T>;
|
|
218
|
+
/**
|
|
219
|
+
* Runs a function within the context of an existing OpenTelemetry span.
|
|
220
|
+
*/
|
|
221
|
+
declare function runInSpanContext<T>(span: Span | undefined, fn: () => Promisable<T>): Promise<T>;
|
|
222
|
+
|
|
144
223
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
145
224
|
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
225
|
(): Promise<IteratorResult<T, TReturn>>;
|
|
@@ -165,6 +244,13 @@ declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implemen
|
|
|
165
244
|
[Symbol.asyncIterator](): this;
|
|
166
245
|
}
|
|
167
246
|
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
247
|
+
interface AsyncIteratorWithSpanOptions extends SetSpanErrorOptions {
|
|
248
|
+
/**
|
|
249
|
+
* The name of the span to create.
|
|
250
|
+
*/
|
|
251
|
+
name: string;
|
|
252
|
+
}
|
|
253
|
+
declare function asyncIteratorWithSpan<T, TReturn, TNext>({ name, ...options }: AsyncIteratorWithSpanOptions, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
168
254
|
|
|
169
255
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
170
256
|
declare function stringifyJSON<T>(value: T | {
|
|
@@ -216,5 +302,5 @@ declare function tryDecodeURIComponent(value: string): string;
|
|
|
216
302
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
217
303
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
218
304
|
|
|
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 };
|
|
305
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
306
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, 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
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];
|
|
@@ -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 = "1.8.0";
|
|
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.
|
|
@@ -141,6 +154,72 @@ declare function onFinish<T, TOptions extends {
|
|
|
141
154
|
}, 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
155
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
143
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Only import types from @opentelemetry/api to avoid runtime dependencies.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
interface OtelConfig {
|
|
162
|
+
tracer: Tracer;
|
|
163
|
+
trace: TraceAPI;
|
|
164
|
+
context: ContextAPI;
|
|
165
|
+
propagation: PropagationAPI;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sets the global OpenTelemetry config.
|
|
169
|
+
* Call this once at app startup. Use `undefined` to disable tracing.
|
|
170
|
+
*/
|
|
171
|
+
declare function setGlobalOtelConfig(config: OtelConfig | undefined): void;
|
|
172
|
+
/**
|
|
173
|
+
* Gets the global OpenTelemetry config.
|
|
174
|
+
* Returns `undefined` if OpenTelemetry is not configured, initialized, or enabled.
|
|
175
|
+
*/
|
|
176
|
+
declare function getGlobalOtelConfig(): OtelConfig | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* Starts a new OpenTelemetry span with the given name and options.
|
|
179
|
+
*
|
|
180
|
+
* @returns The new span, or `undefined` if no tracer is set.
|
|
181
|
+
*/
|
|
182
|
+
declare function startSpan(name: string, options?: SpanOptions, context?: Context): Span | undefined;
|
|
183
|
+
interface SetSpanErrorOptions {
|
|
184
|
+
/**
|
|
185
|
+
* Span error status is not set if error is due to cancellation by the signal.
|
|
186
|
+
*/
|
|
187
|
+
signal?: AbortSignal;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Records and sets the error status on the given span.
|
|
191
|
+
* If the span is `undefined`, it does nothing.
|
|
192
|
+
*/
|
|
193
|
+
declare function setSpanError(span: Span | undefined, error: unknown, options?: SetSpanErrorOptions): void;
|
|
194
|
+
declare function setSpanAttribute(span: Span | undefined, key: string, value: AttributeValue | undefined): void;
|
|
195
|
+
/**
|
|
196
|
+
* Converts an error to an OpenTelemetry Exception.
|
|
197
|
+
*/
|
|
198
|
+
declare function toOtelException(error: unknown): Exclude<Exception, string>;
|
|
199
|
+
/**
|
|
200
|
+
* Converts a value to a string suitable for OpenTelemetry span attributes.
|
|
201
|
+
*/
|
|
202
|
+
declare function toSpanAttributeValue(data: unknown): string;
|
|
203
|
+
interface RunWithSpanOptions extends SpanOptions, SetSpanErrorOptions {
|
|
204
|
+
/**
|
|
205
|
+
* The name of the span to create.
|
|
206
|
+
*/
|
|
207
|
+
name: string;
|
|
208
|
+
/**
|
|
209
|
+
* Context to use for the span.
|
|
210
|
+
*/
|
|
211
|
+
context?: Context;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Runs a function within the context of a new OpenTelemetry span.
|
|
215
|
+
* The span is ended automatically, and errors are recorded to the span.
|
|
216
|
+
*/
|
|
217
|
+
declare function runWithSpan<T>({ name, context, ...options }: RunWithSpanOptions, fn: (span?: Span) => Promisable<T>): Promise<T>;
|
|
218
|
+
/**
|
|
219
|
+
* Runs a function within the context of an existing OpenTelemetry span.
|
|
220
|
+
*/
|
|
221
|
+
declare function runInSpanContext<T>(span: Span | undefined, fn: () => Promisable<T>): Promise<T>;
|
|
222
|
+
|
|
144
223
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
145
224
|
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
146
225
|
(): Promise<IteratorResult<T, TReturn>>;
|
|
@@ -165,6 +244,13 @@ declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implemen
|
|
|
165
244
|
[Symbol.asyncIterator](): this;
|
|
166
245
|
}
|
|
167
246
|
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
247
|
+
interface AsyncIteratorWithSpanOptions extends SetSpanErrorOptions {
|
|
248
|
+
/**
|
|
249
|
+
* The name of the span to create.
|
|
250
|
+
*/
|
|
251
|
+
name: string;
|
|
252
|
+
}
|
|
253
|
+
declare function asyncIteratorWithSpan<T, TReturn, TNext>({ name, ...options }: AsyncIteratorWithSpanOptions, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
168
254
|
|
|
169
255
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
170
256
|
declare function stringifyJSON<T>(value: T | {
|
|
@@ -216,5 +302,5 @@ declare function tryDecodeURIComponent(value: string): string;
|
|
|
216
302
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
217
303
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
218
304
|
|
|
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 };
|
|
305
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
306
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -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 = "1.8.0";
|
|
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,6 +58,99 @@ 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
156
|
items = /* @__PURE__ */ new Map();
|
|
@@ -239,6 +343,34 @@ function replicateAsyncIterator(source, count) {
|
|
|
239
343
|
}
|
|
240
344
|
return replicated;
|
|
241
345
|
}
|
|
346
|
+
function asyncIteratorWithSpan({ name, ...options }, iterator) {
|
|
347
|
+
let span;
|
|
348
|
+
return new AsyncIteratorClass(
|
|
349
|
+
async () => {
|
|
350
|
+
span ??= startSpan(name);
|
|
351
|
+
try {
|
|
352
|
+
const result = await runInSpanContext(span, () => iterator.next());
|
|
353
|
+
span?.addEvent(result.done ? "completed" : "yielded");
|
|
354
|
+
return result;
|
|
355
|
+
} catch (err) {
|
|
356
|
+
setSpanError(span, err, options);
|
|
357
|
+
throw err;
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
async (reason) => {
|
|
361
|
+
try {
|
|
362
|
+
if (reason !== "next") {
|
|
363
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
364
|
+
}
|
|
365
|
+
} catch (err) {
|
|
366
|
+
setSpanError(span, err, options);
|
|
367
|
+
throw err;
|
|
368
|
+
} finally {
|
|
369
|
+
span?.end();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
}
|
|
242
374
|
|
|
243
375
|
class EventPublisher {
|
|
244
376
|
#listenersMap = /* @__PURE__ */ new Map();
|
|
@@ -491,4 +623,4 @@ function value(value2, ...args) {
|
|
|
491
623
|
return value2;
|
|
492
624
|
}
|
|
493
625
|
|
|
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 };
|
|
626
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, 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": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,11 +23,20 @@
|
|
|
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
36
|
"type-fest": "^4.39.1"
|
|
29
37
|
},
|
|
30
38
|
"devDependencies": {
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
31
40
|
"arktype": "2.1.20",
|
|
32
41
|
"valibot": "^1.1.0",
|
|
33
42
|
"zod": "^4.0.14"
|