@orpc/shared 2.0.0-beta.14 → 2.0.0-beta.16
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 -2
- package/dist/index.d.mts +62 -8
- package/dist/index.d.ts +62 -8
- package/dist/index.mjs +258 -199
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -107,7 +107,6 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
107
107
|
</tr>
|
|
108
108
|
<tr>
|
|
109
109
|
<td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td>
|
|
110
|
-
<td align="center"><a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&v=4" width="139" alt="Peter Adam"/><br />Peter Adam</a></td>
|
|
111
110
|
<td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
112
111
|
<td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="139" alt="shota"/><br />shota</a></td>
|
|
113
112
|
</tr>
|
|
@@ -168,6 +167,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
168
167
|
<a href="https://github.com/nguyenlc1993?ref=orpc" target="_blank" rel="noopener" title="Lê Cao Nguyên"><img src="https://avatars.githubusercontent.com/u/13871971?u=83c8b69d9e35b589c4e1f066cc113b1d9461386f&v=4" width="32" height="32" alt="Lê Cao Nguyên" /></a>
|
|
169
168
|
<a href="https://github.com/wobsoriano?ref=orpc" target="_blank" rel="noopener" title="Robert Soriano"><img src="https://avatars.githubusercontent.com/u/13049130?u=6d72104182e7c9ed25934815313fb69107332111&v=4" width="32" height="32" alt="Robert Soriano" /></a>
|
|
170
169
|
<a href="https://github.com/SKostyukovich?ref=orpc" target="_blank" rel="noopener" title="SKostyukovich"><img src="https://avatars.githubusercontent.com/u/10700067?v=4" width="32" height="32" alt="SKostyukovich" /></a>
|
|
170
|
+
<a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&v=4" width="32" height="32" alt="Peter Adam" /></a>
|
|
171
171
|
<a href="https://github.com/FabworksHQ?ref=orpc" target="_blank" rel="noopener" title="Fabworks"><img src="https://avatars.githubusercontent.com/u/160179500?v=4" width="32" height="32" alt="Fabworks" /></a>
|
|
172
172
|
<a href="https://github.com/NovakAnton?ref=orpc" target="_blank" rel="noopener" title="Novak Antonijevic"><img src="https://avatars.githubusercontent.com/u/157126729?u=ae49fa22292d55c0434ff0ca008206155b18663b&v=4" width="32" height="32" alt="Novak Antonijevic" /></a>
|
|
173
173
|
<a href="https://github.com/laduniestu?ref=orpc" target="_blank" rel="noopener" title="Laduni Estu Syalwa"><img src="https://avatars.githubusercontent.com/u/44757637?u=a2fc1ea8f7d827a96721176f79d30592d1c48059&v=4" width="32" height="32" alt="Laduni Estu Syalwa" /></a>
|
|
@@ -175,7 +175,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
175
175
|
<a href="https://github.com/illarionvk?ref=orpc" target="_blank" rel="noopener" title="Illarion Koperski"><img src="https://avatars.githubusercontent.com/u/5012724?u=7cfa13652f7ac5fb3c56d880e3eb3fbe40c3ea34&v=4" width="32" height="32" alt="Illarion Koperski" /></a>
|
|
176
176
|
<a href="https://github.com/steelbrain?ref=orpc" target="_blank" rel="noopener" title="Anees Iqbal"><img src="https://avatars.githubusercontent.com/u/4278113?u=22b80b5399eed68ac76cd58b02961b0481f1db11&v=4" width="32" height="32" alt="Anees Iqbal" /></a>
|
|
177
177
|
<a href="https://github.com/Scrumplex?ref=orpc" target="_blank" rel="noopener" title="Sefa Eyeoglu"><img src="https://avatars.githubusercontent.com/u/11587657?u=ab503582165c0bbff0cca47ce31c9450bb1553c9&v=4" width="32" height="32" alt="Sefa Eyeoglu" /></a>
|
|
178
|
-
<a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="
|
|
178
|
+
<a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="NΛTT"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="32" height="32" alt="NΛTT" /></a>
|
|
179
179
|
<a href="https://github.com/ChromeGG?ref=orpc" target="_blank" rel="noopener" title="Adam Tkaczyk"><img src="https://avatars.githubusercontent.com/u/39050595?u=a58ca6042a6950e94e6e92442db76ef584279bc0&v=4" width="32" height="32" alt="Adam Tkaczyk" /></a>
|
|
180
180
|
<a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a>
|
|
181
181
|
</p>
|
package/dist/index.d.mts
CHANGED
|
@@ -41,6 +41,7 @@ declare function normalizeHttpPath(path: string): `/${string}`;
|
|
|
41
41
|
declare function mergeHttpPath(a: `/${string}`, b: `/${string}`): `/${string}`;
|
|
42
42
|
declare function matchesHttpPathPrefix(url: `/${string}`, prefix: `/${string}`): boolean;
|
|
43
43
|
declare function matchesHttpPath(url: `/${string}`, path: `/${string}`): boolean;
|
|
44
|
+
declare function isCompressibleContentType(contentType: string | null | undefined): boolean;
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
47
|
* Compares two sequential IDs.
|
|
@@ -102,6 +103,30 @@ type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess
|
|
|
102
103
|
declare function onFinish<T, TOptions extends {
|
|
103
104
|
next: () => any;
|
|
104
105
|
}, 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']>>>;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a middleware or interceptor that invokes a callback when the returned async
|
|
108
|
+
* iterator object throws an error while being consumed.
|
|
109
|
+
*
|
|
110
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
111
|
+
* initial call (before the interceptor returns the iterator), whereas this
|
|
112
|
+
* callback only fires while consuming the iterator. Use both together to
|
|
113
|
+
* catch all possible errors.
|
|
114
|
+
*/
|
|
115
|
+
declare function onAsyncIteratorObjectError<T, TOptions extends {
|
|
116
|
+
next: () => any;
|
|
117
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (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']>>>;
|
|
118
|
+
/**
|
|
119
|
+
* Creates an interceptor that invokes a callback when the returned readable
|
|
120
|
+
* stream errors while being consumed.
|
|
121
|
+
*
|
|
122
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
123
|
+
* initial call (before the interceptor returns the stream), whereas this
|
|
124
|
+
* callback only fires while consuming the stream. Use both together to catch
|
|
125
|
+
* all possible errors.
|
|
126
|
+
*/
|
|
127
|
+
declare function onReadableStreamError<T, TOptions extends {
|
|
128
|
+
next: () => any;
|
|
129
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (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']>>>;
|
|
105
130
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: undefined | Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
106
131
|
|
|
107
132
|
/**
|
|
@@ -147,8 +172,8 @@ interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>
|
|
|
147
172
|
*/
|
|
148
173
|
runWith?: <T>(run: () => Promise<T>) => Promise<T>;
|
|
149
174
|
mapResult?: (result: IteratorResult<TYield, TReturn>) => Promisable<IteratorResult<TMappedYield, TMappedReturn>>;
|
|
150
|
-
mapError?: (error:
|
|
151
|
-
onError?: (error:
|
|
175
|
+
mapError?: (error: ThrowableError) => Promisable<ThrowableError>;
|
|
176
|
+
onError?: (error: ThrowableError) => Promisable<void>;
|
|
152
177
|
/**
|
|
153
178
|
* Execute after the stream finishes or is cancelled.
|
|
154
179
|
*/
|
|
@@ -157,6 +182,35 @@ interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>
|
|
|
157
182
|
declare function wrapAsyncIterator<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { runWith, mapResult, mapError, onError, onFinish }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): NoInfer<AsyncIteratorClass<TMappedYield, TMappedReturn>>;
|
|
158
183
|
declare function traceAsyncIterator<T, TReturn, TNext>(options: StartSpanOptions | string, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
159
184
|
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
185
|
+
interface ConsumeAsyncIteratorOptions<T, TReturn, TError> {
|
|
186
|
+
/**
|
|
187
|
+
* Called on each event
|
|
188
|
+
*/
|
|
189
|
+
onEvent: (event: T) => void;
|
|
190
|
+
/**
|
|
191
|
+
* Called once error happens
|
|
192
|
+
*/
|
|
193
|
+
onError?: (error: TError) => void;
|
|
194
|
+
/**
|
|
195
|
+
* Called once AsyncIteratorObject is done
|
|
196
|
+
*
|
|
197
|
+
* @info If iterator is canceled, `undefined` can be passed on success
|
|
198
|
+
*/
|
|
199
|
+
onSuccess?: (value: TReturn | undefined) => void;
|
|
200
|
+
/**
|
|
201
|
+
* Called once after onError or onSuccess
|
|
202
|
+
*
|
|
203
|
+
* @info If iterator is canceled, `undefined` can be passed on success
|
|
204
|
+
*/
|
|
205
|
+
onFinish?: (state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Consumes an AsyncIteratorObject with lifecycle callbacks
|
|
209
|
+
*
|
|
210
|
+
* @warning If no `onError` or `onFinish` is provided, error will be thrown into unhandled rejection channel.
|
|
211
|
+
* @return unsubscribe callback
|
|
212
|
+
*/
|
|
213
|
+
declare function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeAsyncIteratorOptions<T, TReturn, TError | ThrowableError>): () => Promise<void>;
|
|
160
214
|
|
|
161
215
|
type Segment = string | number;
|
|
162
216
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -275,8 +329,8 @@ interface WrapReadableStreamOptions<T, TMapped> {
|
|
|
275
329
|
*/
|
|
276
330
|
runWith?: <T>(run: () => Promise<T>) => Promise<T>;
|
|
277
331
|
mapResult?: (result: ReadableStreamReadResult<T>) => Promisable<ReadableStreamReadResult<TMapped>>;
|
|
278
|
-
mapError?: (error:
|
|
279
|
-
onError?: (error:
|
|
332
|
+
mapError?: (error: ThrowableError) => Promisable<ThrowableError>;
|
|
333
|
+
onError?: (error: ThrowableError) => Promisable<void>;
|
|
280
334
|
/**
|
|
281
335
|
* Guaranteed to execute exactly once after the stream finishes or is cancelled.
|
|
282
336
|
*/
|
|
@@ -285,9 +339,9 @@ interface WrapReadableStreamOptions<T, TMapped> {
|
|
|
285
339
|
declare function wrapReadableStream<T, TMapped = T>(stream: ReadableStream<T>, { runWith, mapResult, mapError, onError, onFinish }: WrapReadableStreamOptions<T, TMapped>): NoInfer<ReadableStream<TMapped>>;
|
|
286
340
|
declare function traceReadableStream<T>(options: StartSpanOptions | string, stream: ReadableStream<T>): ReadableStream<T>;
|
|
287
341
|
/**
|
|
288
|
-
* Converts a
|
|
342
|
+
* Converts a {@link ReadableStream} into an {@link AsyncIteratorClass}.
|
|
289
343
|
*/
|
|
290
|
-
declare function
|
|
344
|
+
declare function streamToAsyncIteratorObject<T>(stream: ReadableStream<T>, { signal }?: {
|
|
291
345
|
signal?: undefined | AbortSignal;
|
|
292
346
|
}): AsyncIteratorClass<T>;
|
|
293
347
|
/**
|
|
@@ -302,5 +356,5 @@ declare function asyncIteratorToUnproxiedDataStream<T>(iterator: AsyncIterator<T
|
|
|
302
356
|
|
|
303
357
|
declare function tryDecodeURIComponent(value: string): string;
|
|
304
358
|
|
|
305
|
-
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onError, onFinish, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan,
|
|
306
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OnFinishState, OpenTelemetryConfig, OrderablePlugin, PromiseWithError, Public, ReadableStreamReadResult, Registry, RunWithSpanOptions, Segment, StartSpanOptions, ThrowableError, Value, WrapAsyncIteratorOptions, WrapReadableStreamOptions };
|
|
359
|
+
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, consumeAsyncIterator, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isCompressibleContentType, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan, streamToAsyncIteratorObject, toOtelException, toSpanAttributeValue, toStringOrBytes, traceAsyncIterator, traceReadableStream, tryDecodeURIComponent, tryOrUndefined, value, wrapAsyncIterator, wrapReadableStream };
|
|
360
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, ConsumeAsyncIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OnFinishState, OpenTelemetryConfig, OrderablePlugin, PromiseWithError, Public, ReadableStreamReadResult, Registry, RunWithSpanOptions, Segment, StartSpanOptions, ThrowableError, Value, WrapAsyncIteratorOptions, WrapReadableStreamOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ declare function normalizeHttpPath(path: string): `/${string}`;
|
|
|
41
41
|
declare function mergeHttpPath(a: `/${string}`, b: `/${string}`): `/${string}`;
|
|
42
42
|
declare function matchesHttpPathPrefix(url: `/${string}`, prefix: `/${string}`): boolean;
|
|
43
43
|
declare function matchesHttpPath(url: `/${string}`, path: `/${string}`): boolean;
|
|
44
|
+
declare function isCompressibleContentType(contentType: string | null | undefined): boolean;
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
47
|
* Compares two sequential IDs.
|
|
@@ -102,6 +103,30 @@ type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess
|
|
|
102
103
|
declare function onFinish<T, TOptions extends {
|
|
103
104
|
next: () => any;
|
|
104
105
|
}, 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']>>>;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a middleware or interceptor that invokes a callback when the returned async
|
|
108
|
+
* iterator object throws an error while being consumed.
|
|
109
|
+
*
|
|
110
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
111
|
+
* initial call (before the interceptor returns the iterator), whereas this
|
|
112
|
+
* callback only fires while consuming the iterator. Use both together to
|
|
113
|
+
* catch all possible errors.
|
|
114
|
+
*/
|
|
115
|
+
declare function onAsyncIteratorObjectError<T, TOptions extends {
|
|
116
|
+
next: () => any;
|
|
117
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (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']>>>;
|
|
118
|
+
/**
|
|
119
|
+
* Creates an interceptor that invokes a callback when the returned readable
|
|
120
|
+
* stream errors while being consumed.
|
|
121
|
+
*
|
|
122
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
123
|
+
* initial call (before the interceptor returns the stream), whereas this
|
|
124
|
+
* callback only fires while consuming the stream. Use both together to catch
|
|
125
|
+
* all possible errors.
|
|
126
|
+
*/
|
|
127
|
+
declare function onReadableStreamError<T, TOptions extends {
|
|
128
|
+
next: () => any;
|
|
129
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (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']>>>;
|
|
105
130
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: undefined | Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
106
131
|
|
|
107
132
|
/**
|
|
@@ -147,8 +172,8 @@ interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>
|
|
|
147
172
|
*/
|
|
148
173
|
runWith?: <T>(run: () => Promise<T>) => Promise<T>;
|
|
149
174
|
mapResult?: (result: IteratorResult<TYield, TReturn>) => Promisable<IteratorResult<TMappedYield, TMappedReturn>>;
|
|
150
|
-
mapError?: (error:
|
|
151
|
-
onError?: (error:
|
|
175
|
+
mapError?: (error: ThrowableError) => Promisable<ThrowableError>;
|
|
176
|
+
onError?: (error: ThrowableError) => Promisable<void>;
|
|
152
177
|
/**
|
|
153
178
|
* Execute after the stream finishes or is cancelled.
|
|
154
179
|
*/
|
|
@@ -157,6 +182,35 @@ interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>
|
|
|
157
182
|
declare function wrapAsyncIterator<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { runWith, mapResult, mapError, onError, onFinish }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): NoInfer<AsyncIteratorClass<TMappedYield, TMappedReturn>>;
|
|
158
183
|
declare function traceAsyncIterator<T, TReturn, TNext>(options: StartSpanOptions | string, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
159
184
|
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
185
|
+
interface ConsumeAsyncIteratorOptions<T, TReturn, TError> {
|
|
186
|
+
/**
|
|
187
|
+
* Called on each event
|
|
188
|
+
*/
|
|
189
|
+
onEvent: (event: T) => void;
|
|
190
|
+
/**
|
|
191
|
+
* Called once error happens
|
|
192
|
+
*/
|
|
193
|
+
onError?: (error: TError) => void;
|
|
194
|
+
/**
|
|
195
|
+
* Called once AsyncIteratorObject is done
|
|
196
|
+
*
|
|
197
|
+
* @info If iterator is canceled, `undefined` can be passed on success
|
|
198
|
+
*/
|
|
199
|
+
onSuccess?: (value: TReturn | undefined) => void;
|
|
200
|
+
/**
|
|
201
|
+
* Called once after onError or onSuccess
|
|
202
|
+
*
|
|
203
|
+
* @info If iterator is canceled, `undefined` can be passed on success
|
|
204
|
+
*/
|
|
205
|
+
onFinish?: (state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Consumes an AsyncIteratorObject with lifecycle callbacks
|
|
209
|
+
*
|
|
210
|
+
* @warning If no `onError` or `onFinish` is provided, error will be thrown into unhandled rejection channel.
|
|
211
|
+
* @return unsubscribe callback
|
|
212
|
+
*/
|
|
213
|
+
declare function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeAsyncIteratorOptions<T, TReturn, TError | ThrowableError>): () => Promise<void>;
|
|
160
214
|
|
|
161
215
|
type Segment = string | number;
|
|
162
216
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
@@ -275,8 +329,8 @@ interface WrapReadableStreamOptions<T, TMapped> {
|
|
|
275
329
|
*/
|
|
276
330
|
runWith?: <T>(run: () => Promise<T>) => Promise<T>;
|
|
277
331
|
mapResult?: (result: ReadableStreamReadResult<T>) => Promisable<ReadableStreamReadResult<TMapped>>;
|
|
278
|
-
mapError?: (error:
|
|
279
|
-
onError?: (error:
|
|
332
|
+
mapError?: (error: ThrowableError) => Promisable<ThrowableError>;
|
|
333
|
+
onError?: (error: ThrowableError) => Promisable<void>;
|
|
280
334
|
/**
|
|
281
335
|
* Guaranteed to execute exactly once after the stream finishes or is cancelled.
|
|
282
336
|
*/
|
|
@@ -285,9 +339,9 @@ interface WrapReadableStreamOptions<T, TMapped> {
|
|
|
285
339
|
declare function wrapReadableStream<T, TMapped = T>(stream: ReadableStream<T>, { runWith, mapResult, mapError, onError, onFinish }: WrapReadableStreamOptions<T, TMapped>): NoInfer<ReadableStream<TMapped>>;
|
|
286
340
|
declare function traceReadableStream<T>(options: StartSpanOptions | string, stream: ReadableStream<T>): ReadableStream<T>;
|
|
287
341
|
/**
|
|
288
|
-
* Converts a
|
|
342
|
+
* Converts a {@link ReadableStream} into an {@link AsyncIteratorClass}.
|
|
289
343
|
*/
|
|
290
|
-
declare function
|
|
344
|
+
declare function streamToAsyncIteratorObject<T>(stream: ReadableStream<T>, { signal }?: {
|
|
291
345
|
signal?: undefined | AbortSignal;
|
|
292
346
|
}): AsyncIteratorClass<T>;
|
|
293
347
|
/**
|
|
@@ -302,5 +356,5 @@ declare function asyncIteratorToUnproxiedDataStream<T>(iterator: AsyncIterator<T
|
|
|
302
356
|
|
|
303
357
|
declare function tryDecodeURIComponent(value: string): string;
|
|
304
358
|
|
|
305
|
-
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onError, onFinish, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan,
|
|
306
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OnFinishState, OpenTelemetryConfig, OrderablePlugin, PromiseWithError, Public, ReadableStreamReadResult, Registry, RunWithSpanOptions, Segment, StartSpanOptions, ThrowableError, Value, WrapAsyncIteratorOptions, WrapReadableStreamOptions };
|
|
359
|
+
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, consumeAsyncIterator, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isCompressibleContentType, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan, streamToAsyncIteratorObject, toOtelException, toSpanAttributeValue, toStringOrBytes, traceAsyncIterator, traceReadableStream, tryDecodeURIComponent, tryOrUndefined, value, wrapAsyncIterator, wrapReadableStream };
|
|
360
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, ConsumeAsyncIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OnFinishState, OpenTelemetryConfig, OrderablePlugin, PromiseWithError, Public, ReadableStreamReadResult, Registry, RunWithSpanOptions, Segment, StartSpanOptions, ThrowableError, Value, WrapAsyncIteratorOptions, WrapReadableStreamOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbortError, AsyncIteratorClass, isTypescriptObject,
|
|
1
|
+
import { AbortError, AsyncIteratorClass, getOrBind, isTypescriptObject, isAsyncIteratorObject } from '@standardserver/shared';
|
|
2
2
|
export { AbortError, AsyncIteratorClass, SequentialIdGenerator, getOrBind, isAsyncIteratorObject, isTypescriptObject, parseEmptyableJSON, sequential, sleep, stringifyJSON, toArray } from '@standardserver/shared';
|
|
3
3
|
|
|
4
4
|
function resolveMaybeOptionalOptions(rest) {
|
|
@@ -176,6 +176,17 @@ function matchesHttpPath(url, path) {
|
|
|
176
176
|
}
|
|
177
177
|
return charAfterPrefix === void 0 || charAfterPrefix === "?" || charAfterPrefix === "#";
|
|
178
178
|
}
|
|
179
|
+
const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
180
|
+
const MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH = 1024;
|
|
181
|
+
function isCompressibleContentType(contentType) {
|
|
182
|
+
if (contentType === null || contentType === void 0) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
if (contentType.length > MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return COMPRESSIBLE_CONTENT_TYPE_REGEX.test(contentType);
|
|
189
|
+
}
|
|
179
190
|
|
|
180
191
|
function compareSequentialIds(a, b) {
|
|
181
192
|
if (a.length !== b.length) {
|
|
@@ -184,61 +195,6 @@ function compareSequentialIds(a, b) {
|
|
|
184
195
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
185
196
|
}
|
|
186
197
|
|
|
187
|
-
function onStart(callback) {
|
|
188
|
-
return async (options, ...rest) => {
|
|
189
|
-
await callback(options, ...rest);
|
|
190
|
-
return await options.next();
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
function onSuccess(callback) {
|
|
194
|
-
return async (options, ...rest) => {
|
|
195
|
-
const result = await options.next();
|
|
196
|
-
await callback(result, options, ...rest);
|
|
197
|
-
return result;
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function onError(callback) {
|
|
201
|
-
return async (options, ...rest) => {
|
|
202
|
-
try {
|
|
203
|
-
return await options.next();
|
|
204
|
-
} catch (error) {
|
|
205
|
-
await callback(error, options, ...rest);
|
|
206
|
-
throw error;
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
function onFinish(callback) {
|
|
211
|
-
let state;
|
|
212
|
-
return async (options, ...rest) => {
|
|
213
|
-
try {
|
|
214
|
-
const result = await options.next();
|
|
215
|
-
state = [null, result, true];
|
|
216
|
-
return result;
|
|
217
|
-
} catch (error) {
|
|
218
|
-
state = [error, void 0, false];
|
|
219
|
-
throw error;
|
|
220
|
-
} finally {
|
|
221
|
-
await callback(state, options, ...rest);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function intercept(interceptors, options, main) {
|
|
226
|
-
if (!interceptors?.length) {
|
|
227
|
-
return main(options);
|
|
228
|
-
}
|
|
229
|
-
const next = (options2, index) => {
|
|
230
|
-
const interceptor = interceptors[index];
|
|
231
|
-
if (!interceptor) {
|
|
232
|
-
return main(options2);
|
|
233
|
-
}
|
|
234
|
-
return interceptor({
|
|
235
|
-
...options2,
|
|
236
|
-
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
237
|
-
});
|
|
238
|
-
};
|
|
239
|
-
return next(options, 0);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
198
|
const SPAN_ERROR_STATUS = 2;
|
|
243
199
|
const OPENTELEMETRY_CONFIG_SYMBOL = Symbol.for("ORPC_OPENTELEMETRY_CONFIG");
|
|
244
200
|
function setOpenTelemetryConfig(config) {
|
|
@@ -511,6 +467,52 @@ function replicateAsyncIterator(source, count) {
|
|
|
511
467
|
});
|
|
512
468
|
return replicated;
|
|
513
469
|
}
|
|
470
|
+
function consumeAsyncIterator(iterator, options) {
|
|
471
|
+
void (async () => {
|
|
472
|
+
let onFinishState;
|
|
473
|
+
try {
|
|
474
|
+
const resolvedIterator = await iterator;
|
|
475
|
+
while (true) {
|
|
476
|
+
const { done, value } = await resolvedIterator.next();
|
|
477
|
+
if (done) {
|
|
478
|
+
const realValue = value;
|
|
479
|
+
onFinishState = [null, realValue, true];
|
|
480
|
+
options.onSuccess?.(realValue);
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
options.onEvent(value);
|
|
484
|
+
}
|
|
485
|
+
} catch (error) {
|
|
486
|
+
onFinishState = [error, void 0, false];
|
|
487
|
+
options.onError?.(error);
|
|
488
|
+
} finally {
|
|
489
|
+
options.onFinish?.(onFinishState);
|
|
490
|
+
}
|
|
491
|
+
})();
|
|
492
|
+
return async () => {
|
|
493
|
+
await (await iterator)?.return?.();
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function value(value2, ...args) {
|
|
498
|
+
if (typeof value2 === "function") {
|
|
499
|
+
return value2(...args);
|
|
500
|
+
}
|
|
501
|
+
return value2;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function override(target, partial) {
|
|
505
|
+
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
506
|
+
get(_, prop) {
|
|
507
|
+
const targetValue = prop in partial ? partial : value(target);
|
|
508
|
+
return getOrBind(targetValue, prop);
|
|
509
|
+
},
|
|
510
|
+
has(_, prop) {
|
|
511
|
+
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
return proxy;
|
|
515
|
+
}
|
|
514
516
|
|
|
515
517
|
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
516
518
|
if (check(payload)) {
|
|
@@ -623,84 +625,6 @@ function bindMethods(obj) {
|
|
|
623
625
|
return methods;
|
|
624
626
|
}
|
|
625
627
|
|
|
626
|
-
function sortPlugins(plugins) {
|
|
627
|
-
const pluginCount = plugins.length;
|
|
628
|
-
const pluginIdToIndices = /* @__PURE__ */ new Map();
|
|
629
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
630
|
-
const plugin = plugins[i];
|
|
631
|
-
const indices = pluginIdToIndices.get(plugin.name);
|
|
632
|
-
if (indices === void 0) {
|
|
633
|
-
pluginIdToIndices.set(plugin.name, [i]);
|
|
634
|
-
} else {
|
|
635
|
-
indices.push(i);
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
const graph = Array.from(
|
|
639
|
-
{ length: pluginCount },
|
|
640
|
-
() => /* @__PURE__ */ new Set()
|
|
641
|
-
);
|
|
642
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
643
|
-
const plugin = plugins[i];
|
|
644
|
-
const beforeList = plugin.before;
|
|
645
|
-
if (beforeList !== void 0) {
|
|
646
|
-
for (const beforeId of beforeList) {
|
|
647
|
-
const beforeIndices = pluginIdToIndices.get(beforeId);
|
|
648
|
-
if (beforeIndices === void 0)
|
|
649
|
-
continue;
|
|
650
|
-
for (const beforeIndex of beforeIndices) {
|
|
651
|
-
const beforeGraph = graph[beforeIndex];
|
|
652
|
-
if (beforeGraph !== void 0) {
|
|
653
|
-
beforeGraph.add(i);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
const afterList = plugin.after;
|
|
659
|
-
if (afterList !== void 0) {
|
|
660
|
-
const currentGraph = graph[i];
|
|
661
|
-
if (currentGraph !== void 0) {
|
|
662
|
-
for (const afterId of afterList) {
|
|
663
|
-
const afterIndices = pluginIdToIndices.get(afterId);
|
|
664
|
-
if (afterIndices === void 0)
|
|
665
|
-
continue;
|
|
666
|
-
for (const afterIndex of afterIndices) {
|
|
667
|
-
currentGraph.add(afterIndex);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
const sorted = [];
|
|
674
|
-
const visiting = /* @__PURE__ */ new Set();
|
|
675
|
-
const visited = /* @__PURE__ */ new Set();
|
|
676
|
-
function visit(index) {
|
|
677
|
-
if (visited.has(index))
|
|
678
|
-
return;
|
|
679
|
-
if (visiting.has(index)) {
|
|
680
|
-
const plugin2 = plugins[index];
|
|
681
|
-
const pluginId = plugin2 !== void 0 ? plugin2.name : "unknown";
|
|
682
|
-
throw new Error(`Circular dependency detected involving plugin "${pluginId}"`);
|
|
683
|
-
}
|
|
684
|
-
visiting.add(index);
|
|
685
|
-
const deps = graph[index];
|
|
686
|
-
if (deps !== void 0) {
|
|
687
|
-
for (const depIndex of deps) {
|
|
688
|
-
visit(depIndex);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
visiting.delete(index);
|
|
692
|
-
visited.add(index);
|
|
693
|
-
const plugin = plugins[index];
|
|
694
|
-
if (plugin !== void 0) {
|
|
695
|
-
sorted.push(plugin);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
699
|
-
visit(i);
|
|
700
|
-
}
|
|
701
|
-
return sorted;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
628
|
function promiseWithResolvers() {
|
|
705
629
|
const result = {};
|
|
706
630
|
result.promise = new Promise((resolve, reject) => {
|
|
@@ -710,69 +634,6 @@ function promiseWithResolvers() {
|
|
|
710
634
|
return result;
|
|
711
635
|
}
|
|
712
636
|
|
|
713
|
-
function value(value2, ...args) {
|
|
714
|
-
if (typeof value2 === "function") {
|
|
715
|
-
return value2(...args);
|
|
716
|
-
}
|
|
717
|
-
return value2;
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
function override(target, partial) {
|
|
721
|
-
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
722
|
-
get(_, prop) {
|
|
723
|
-
const targetValue = prop in partial ? partial : value(target);
|
|
724
|
-
return getOrBind(targetValue, prop);
|
|
725
|
-
},
|
|
726
|
-
has(_, prop) {
|
|
727
|
-
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
return proxy;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
function allAbortSignal(signals) {
|
|
734
|
-
const realSignals = signals.filter((signal) => signal !== void 0);
|
|
735
|
-
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
736
|
-
return void 0;
|
|
737
|
-
}
|
|
738
|
-
const controller = new AbortController();
|
|
739
|
-
const abortIfAllAborted = () => {
|
|
740
|
-
if (realSignals.every((signal) => signal.aborted)) {
|
|
741
|
-
controller.abort();
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
abortIfAllAborted();
|
|
745
|
-
for (const signal of realSignals) {
|
|
746
|
-
signal.addEventListener("abort", () => {
|
|
747
|
-
abortIfAllAborted();
|
|
748
|
-
}, {
|
|
749
|
-
once: true,
|
|
750
|
-
signal: controller.signal
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
return controller.signal;
|
|
754
|
-
}
|
|
755
|
-
async function runWithSignal(signal, fn) {
|
|
756
|
-
if (!signal) {
|
|
757
|
-
return fn();
|
|
758
|
-
}
|
|
759
|
-
signal.throwIfAborted();
|
|
760
|
-
const { promise, reject, resolve } = promiseWithResolvers();
|
|
761
|
-
let abortListener;
|
|
762
|
-
signal.addEventListener("abort", abortListener = () => {
|
|
763
|
-
reject(signal.reason);
|
|
764
|
-
abortListener = void 0;
|
|
765
|
-
});
|
|
766
|
-
try {
|
|
767
|
-
fn().then(resolve, reject);
|
|
768
|
-
return await promise;
|
|
769
|
-
} finally {
|
|
770
|
-
if (abortListener) {
|
|
771
|
-
signal.removeEventListener("abort", abortListener);
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
|
|
776
637
|
function replicateReadableStream(stream, count) {
|
|
777
638
|
if (count <= 0) {
|
|
778
639
|
return [];
|
|
@@ -843,7 +704,7 @@ function traceReadableStream(options, stream) {
|
|
|
843
704
|
}
|
|
844
705
|
});
|
|
845
706
|
}
|
|
846
|
-
function
|
|
707
|
+
function streamToAsyncIteratorObject(stream, { signal } = {}) {
|
|
847
708
|
const reader = stream.getReader();
|
|
848
709
|
let cancelledBySignal = false;
|
|
849
710
|
return new AsyncIteratorClass(
|
|
@@ -909,4 +770,202 @@ function asyncIteratorToUnproxiedDataStream(iterator) {
|
|
|
909
770
|
});
|
|
910
771
|
}
|
|
911
772
|
|
|
912
|
-
|
|
773
|
+
function onStart(callback) {
|
|
774
|
+
return async (options, ...rest) => {
|
|
775
|
+
await callback(options, ...rest);
|
|
776
|
+
return await options.next();
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
function onSuccess(callback) {
|
|
780
|
+
return async (options, ...rest) => {
|
|
781
|
+
const result = await options.next();
|
|
782
|
+
await callback(result, options, ...rest);
|
|
783
|
+
return result;
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function onError(callback) {
|
|
787
|
+
return async (options, ...rest) => {
|
|
788
|
+
try {
|
|
789
|
+
return await options.next();
|
|
790
|
+
} catch (error) {
|
|
791
|
+
await callback(error, options, ...rest);
|
|
792
|
+
throw error;
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
function onFinish(callback) {
|
|
797
|
+
let state;
|
|
798
|
+
return async (options, ...rest) => {
|
|
799
|
+
try {
|
|
800
|
+
const result = await options.next();
|
|
801
|
+
state = [null, result, true];
|
|
802
|
+
return result;
|
|
803
|
+
} catch (error) {
|
|
804
|
+
state = [error, void 0, false];
|
|
805
|
+
throw error;
|
|
806
|
+
} finally {
|
|
807
|
+
await callback(state, options, ...rest);
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function onAsyncIteratorObjectError(callback) {
|
|
812
|
+
return async (options, ...rest) => {
|
|
813
|
+
const output = await options.next();
|
|
814
|
+
if (!isAsyncIteratorObject(output)) {
|
|
815
|
+
return output;
|
|
816
|
+
}
|
|
817
|
+
return override(output, wrapAsyncIterator(output, {
|
|
818
|
+
onError: (error) => callback(error, options, ...rest)
|
|
819
|
+
}));
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
function onReadableStreamError(callback) {
|
|
823
|
+
return async (options, ...rest) => {
|
|
824
|
+
const output = await options.next();
|
|
825
|
+
if (!(output instanceof ReadableStream)) {
|
|
826
|
+
return output;
|
|
827
|
+
}
|
|
828
|
+
return override(output, wrapReadableStream(output, {
|
|
829
|
+
onError: (error) => callback(error, options, ...rest)
|
|
830
|
+
}));
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function intercept(interceptors, options, main) {
|
|
834
|
+
if (!interceptors?.length) {
|
|
835
|
+
return main(options);
|
|
836
|
+
}
|
|
837
|
+
const next = (options2, index) => {
|
|
838
|
+
const interceptor = interceptors[index];
|
|
839
|
+
if (!interceptor) {
|
|
840
|
+
return main(options2);
|
|
841
|
+
}
|
|
842
|
+
return interceptor({
|
|
843
|
+
...options2,
|
|
844
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
845
|
+
});
|
|
846
|
+
};
|
|
847
|
+
return next(options, 0);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function sortPlugins(plugins) {
|
|
851
|
+
const pluginCount = plugins.length;
|
|
852
|
+
const pluginIdToIndices = /* @__PURE__ */ new Map();
|
|
853
|
+
for (let i = 0; i < pluginCount; i++) {
|
|
854
|
+
const plugin = plugins[i];
|
|
855
|
+
const indices = pluginIdToIndices.get(plugin.name);
|
|
856
|
+
if (indices === void 0) {
|
|
857
|
+
pluginIdToIndices.set(plugin.name, [i]);
|
|
858
|
+
} else {
|
|
859
|
+
indices.push(i);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
const graph = Array.from(
|
|
863
|
+
{ length: pluginCount },
|
|
864
|
+
() => /* @__PURE__ */ new Set()
|
|
865
|
+
);
|
|
866
|
+
for (let i = 0; i < pluginCount; i++) {
|
|
867
|
+
const plugin = plugins[i];
|
|
868
|
+
const beforeList = plugin.before;
|
|
869
|
+
if (beforeList !== void 0) {
|
|
870
|
+
for (const beforeId of beforeList) {
|
|
871
|
+
const beforeIndices = pluginIdToIndices.get(beforeId);
|
|
872
|
+
if (beforeIndices === void 0)
|
|
873
|
+
continue;
|
|
874
|
+
for (const beforeIndex of beforeIndices) {
|
|
875
|
+
const beforeGraph = graph[beforeIndex];
|
|
876
|
+
if (beforeGraph !== void 0) {
|
|
877
|
+
beforeGraph.add(i);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
const afterList = plugin.after;
|
|
883
|
+
if (afterList !== void 0) {
|
|
884
|
+
const currentGraph = graph[i];
|
|
885
|
+
if (currentGraph !== void 0) {
|
|
886
|
+
for (const afterId of afterList) {
|
|
887
|
+
const afterIndices = pluginIdToIndices.get(afterId);
|
|
888
|
+
if (afterIndices === void 0)
|
|
889
|
+
continue;
|
|
890
|
+
for (const afterIndex of afterIndices) {
|
|
891
|
+
currentGraph.add(afterIndex);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
const sorted = [];
|
|
898
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
899
|
+
const visited = /* @__PURE__ */ new Set();
|
|
900
|
+
function visit(index) {
|
|
901
|
+
if (visited.has(index))
|
|
902
|
+
return;
|
|
903
|
+
if (visiting.has(index)) {
|
|
904
|
+
const plugin2 = plugins[index];
|
|
905
|
+
const pluginId = plugin2 !== void 0 ? plugin2.name : "unknown";
|
|
906
|
+
throw new Error(`Circular dependency detected involving plugin "${pluginId}"`);
|
|
907
|
+
}
|
|
908
|
+
visiting.add(index);
|
|
909
|
+
const deps = graph[index];
|
|
910
|
+
if (deps !== void 0) {
|
|
911
|
+
for (const depIndex of deps) {
|
|
912
|
+
visit(depIndex);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
visiting.delete(index);
|
|
916
|
+
visited.add(index);
|
|
917
|
+
const plugin = plugins[index];
|
|
918
|
+
if (plugin !== void 0) {
|
|
919
|
+
sorted.push(plugin);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
for (let i = 0; i < pluginCount; i++) {
|
|
923
|
+
visit(i);
|
|
924
|
+
}
|
|
925
|
+
return sorted;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function allAbortSignal(signals) {
|
|
929
|
+
const realSignals = signals.filter((signal) => signal !== void 0);
|
|
930
|
+
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
931
|
+
return void 0;
|
|
932
|
+
}
|
|
933
|
+
const controller = new AbortController();
|
|
934
|
+
const abortIfAllAborted = () => {
|
|
935
|
+
if (realSignals.every((signal) => signal.aborted)) {
|
|
936
|
+
controller.abort();
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
abortIfAllAborted();
|
|
940
|
+
for (const signal of realSignals) {
|
|
941
|
+
signal.addEventListener("abort", () => {
|
|
942
|
+
abortIfAllAborted();
|
|
943
|
+
}, {
|
|
944
|
+
once: true,
|
|
945
|
+
signal: controller.signal
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
return controller.signal;
|
|
949
|
+
}
|
|
950
|
+
async function runWithSignal(signal, fn) {
|
|
951
|
+
if (!signal) {
|
|
952
|
+
return fn();
|
|
953
|
+
}
|
|
954
|
+
signal.throwIfAborted();
|
|
955
|
+
const { promise, reject, resolve } = promiseWithResolvers();
|
|
956
|
+
let abortListener;
|
|
957
|
+
signal.addEventListener("abort", abortListener = () => {
|
|
958
|
+
reject(signal.reason);
|
|
959
|
+
abortListener = void 0;
|
|
960
|
+
});
|
|
961
|
+
try {
|
|
962
|
+
fn().then(resolve, reject);
|
|
963
|
+
return await promise;
|
|
964
|
+
} finally {
|
|
965
|
+
if (abortListener) {
|
|
966
|
+
signal.removeEventListener("abort", abortListener);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, consumeAsyncIterator, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isCompressibleContentType, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan, streamToAsyncIteratorObject, toOtelException, toSpanAttributeValue, toStringOrBytes, traceAsyncIterator, traceReadableStream, tryDecodeURIComponent, tryOrUndefined, value, wrapAsyncIterator, wrapReadableStream };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.16",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@standardserver/shared": "^0.0
|
|
36
|
+
"@standardserver/shared": "^0.2.0",
|
|
37
37
|
"radash": "^12.1.1",
|
|
38
38
|
"type-fest": "^5.3.1"
|
|
39
39
|
},
|