@orpc/shared 1.3.0 → 1.4.1
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/dist/index.d.mts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.mjs +138 -77
- package/package.json +6 -1
package/dist/index.d.mts
CHANGED
|
@@ -11,6 +11,10 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
13
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
14
|
+
/**
|
|
15
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
16
|
+
*/
|
|
17
|
+
declare function defer(callback: () => void): void;
|
|
14
18
|
|
|
15
19
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
@@ -76,6 +80,7 @@ interface CreateAsyncIteratorObjectCleanupFn {
|
|
|
76
80
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
81
|
}
|
|
78
82
|
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
83
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>)[];
|
|
79
84
|
|
|
80
85
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
86
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -96,10 +101,13 @@ declare function isTypescriptObject(value: unknown): value is object & Record<Pr
|
|
|
96
101
|
declare function clone<T>(value: T): T;
|
|
97
102
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
98
103
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
104
|
+
declare const NullProtoObj: ({
|
|
105
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
106
|
+
});
|
|
99
107
|
|
|
100
108
|
interface AsyncIdQueueCloseOptions {
|
|
101
109
|
id?: number;
|
|
102
|
-
reason?:
|
|
110
|
+
reason?: unknown;
|
|
103
111
|
}
|
|
104
112
|
declare class AsyncIdQueue<T> {
|
|
105
113
|
private readonly openIds;
|
|
@@ -117,5 +125,5 @@ declare class AsyncIdQueue<T> {
|
|
|
117
125
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
126
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
127
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
128
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
121
129
|
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
13
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
14
|
+
/**
|
|
15
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
16
|
+
*/
|
|
17
|
+
declare function defer(callback: () => void): void;
|
|
14
18
|
|
|
15
19
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
@@ -76,6 +80,7 @@ interface CreateAsyncIteratorObjectCleanupFn {
|
|
|
76
80
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
81
|
}
|
|
78
82
|
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
83
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>)[];
|
|
79
84
|
|
|
80
85
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
86
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -96,10 +101,13 @@ declare function isTypescriptObject(value: unknown): value is object & Record<Pr
|
|
|
96
101
|
declare function clone<T>(value: T): T;
|
|
97
102
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
98
103
|
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
104
|
+
declare const NullProtoObj: ({
|
|
105
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
106
|
+
});
|
|
99
107
|
|
|
100
108
|
interface AsyncIdQueueCloseOptions {
|
|
101
109
|
id?: number;
|
|
102
|
-
reason?:
|
|
110
|
+
reason?: unknown;
|
|
103
111
|
}
|
|
104
112
|
declare class AsyncIdQueue<T> {
|
|
105
113
|
private readonly openIds;
|
|
@@ -117,5 +125,5 @@ declare class AsyncIdQueue<T> {
|
|
|
117
125
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
118
126
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
119
127
|
|
|
120
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
128
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
121
129
|
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -32,6 +32,13 @@ function sequential(fn) {
|
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
function defer(callback) {
|
|
36
|
+
if ("setTimeout" in globalThis && typeof globalThis.setTimeout === "function") {
|
|
37
|
+
globalThis.setTimeout(callback, 0);
|
|
38
|
+
} else {
|
|
39
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
35
42
|
|
|
36
43
|
class SequentialIdGenerator {
|
|
37
44
|
nextId = 0;
|
|
@@ -96,6 +103,82 @@ function intercept(interceptors, options, main) {
|
|
|
96
103
|
return next(options, 0);
|
|
97
104
|
}
|
|
98
105
|
|
|
106
|
+
class AsyncIdQueue {
|
|
107
|
+
openIds = /* @__PURE__ */ new Set();
|
|
108
|
+
items = /* @__PURE__ */ new Map();
|
|
109
|
+
pendingPulls = /* @__PURE__ */ new Map();
|
|
110
|
+
get length() {
|
|
111
|
+
return this.openIds.size;
|
|
112
|
+
}
|
|
113
|
+
open(id) {
|
|
114
|
+
this.openIds.add(id);
|
|
115
|
+
}
|
|
116
|
+
isOpen(id) {
|
|
117
|
+
return this.openIds.has(id);
|
|
118
|
+
}
|
|
119
|
+
push(id, item) {
|
|
120
|
+
this.assertOpen(id);
|
|
121
|
+
const pending = this.pendingPulls.get(id);
|
|
122
|
+
if (pending?.length) {
|
|
123
|
+
pending.shift()[0](item);
|
|
124
|
+
if (pending.length === 0) {
|
|
125
|
+
this.pendingPulls.delete(id);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
const items = this.items.get(id);
|
|
129
|
+
if (items) {
|
|
130
|
+
items.push(item);
|
|
131
|
+
} else {
|
|
132
|
+
this.items.set(id, [item]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async pull(id) {
|
|
137
|
+
this.assertOpen(id);
|
|
138
|
+
const items = this.items.get(id);
|
|
139
|
+
if (items?.length) {
|
|
140
|
+
const item = items.shift();
|
|
141
|
+
if (items.length === 0) {
|
|
142
|
+
this.items.delete(id);
|
|
143
|
+
}
|
|
144
|
+
return item;
|
|
145
|
+
}
|
|
146
|
+
return new Promise((resolve, reject) => {
|
|
147
|
+
const waitingPulls = this.pendingPulls.get(id);
|
|
148
|
+
const pending = [resolve, reject];
|
|
149
|
+
if (waitingPulls) {
|
|
150
|
+
waitingPulls.push(pending);
|
|
151
|
+
} else {
|
|
152
|
+
this.pendingPulls.set(id, [pending]);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
close({ id, reason } = {}) {
|
|
157
|
+
if (id === void 0) {
|
|
158
|
+
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
159
|
+
pendingPulls.forEach(([, reject]) => {
|
|
160
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
this.pendingPulls.clear();
|
|
164
|
+
this.openIds.clear();
|
|
165
|
+
this.items.clear();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
169
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
170
|
+
});
|
|
171
|
+
this.pendingPulls.delete(id);
|
|
172
|
+
this.openIds.delete(id);
|
|
173
|
+
this.items.delete(id);
|
|
174
|
+
}
|
|
175
|
+
assertOpen(id) {
|
|
176
|
+
if (!this.isOpen(id)) {
|
|
177
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
99
182
|
function isAsyncIteratorObject(maybe) {
|
|
100
183
|
if (!maybe || typeof maybe !== "object") {
|
|
101
184
|
return false;
|
|
@@ -158,6 +241,53 @@ function createAsyncIteratorObject(next, cleanup) {
|
|
|
158
241
|
};
|
|
159
242
|
return iterator;
|
|
160
243
|
}
|
|
244
|
+
function replicateAsyncIterator(source, count) {
|
|
245
|
+
const queue = new AsyncIdQueue();
|
|
246
|
+
const replicated = [];
|
|
247
|
+
let error;
|
|
248
|
+
const start = once(async () => {
|
|
249
|
+
try {
|
|
250
|
+
while (true) {
|
|
251
|
+
const item = await source.next();
|
|
252
|
+
for (let id = 0; id < count; id++) {
|
|
253
|
+
if (queue.isOpen(id)) {
|
|
254
|
+
queue.push(id, item);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (item.done) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
} catch (e) {
|
|
262
|
+
error = { value: e };
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
for (let id = 0; id < count; id++) {
|
|
266
|
+
queue.open(id);
|
|
267
|
+
replicated.push(createAsyncIteratorObject(
|
|
268
|
+
() => {
|
|
269
|
+
start();
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
queue.pull(id).then(resolve).catch(reject);
|
|
272
|
+
defer(() => {
|
|
273
|
+
if (error) {
|
|
274
|
+
reject(error.value);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
async (reason) => {
|
|
280
|
+
queue.close({ id });
|
|
281
|
+
if (reason !== "next") {
|
|
282
|
+
if (replicated.every((_, id2) => !queue.isOpen(id2))) {
|
|
283
|
+
await source?.return?.();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
));
|
|
288
|
+
}
|
|
289
|
+
return replicated;
|
|
290
|
+
}
|
|
161
291
|
|
|
162
292
|
function parseEmptyableJSON(text) {
|
|
163
293
|
if (!text) {
|
|
@@ -221,82 +351,13 @@ function isPropertyKey(value) {
|
|
|
221
351
|
const type = typeof value;
|
|
222
352
|
return type === "string" || type === "number" || type === "symbol";
|
|
223
353
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
open(id) {
|
|
233
|
-
this.openIds.add(id);
|
|
234
|
-
}
|
|
235
|
-
isOpen(id) {
|
|
236
|
-
return this.openIds.has(id);
|
|
237
|
-
}
|
|
238
|
-
push(id, item) {
|
|
239
|
-
this.assertOpen(id);
|
|
240
|
-
const pending = this.pendingPulls.get(id);
|
|
241
|
-
if (pending?.length) {
|
|
242
|
-
pending.shift()[0](item);
|
|
243
|
-
if (pending.length === 0) {
|
|
244
|
-
this.pendingPulls.delete(id);
|
|
245
|
-
}
|
|
246
|
-
} else {
|
|
247
|
-
const items = this.items.get(id);
|
|
248
|
-
if (items) {
|
|
249
|
-
items.push(item);
|
|
250
|
-
} else {
|
|
251
|
-
this.items.set(id, [item]);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
async pull(id) {
|
|
256
|
-
this.assertOpen(id);
|
|
257
|
-
const items = this.items.get(id);
|
|
258
|
-
if (items?.length) {
|
|
259
|
-
const item = items.shift();
|
|
260
|
-
if (items.length === 0) {
|
|
261
|
-
this.items.delete(id);
|
|
262
|
-
}
|
|
263
|
-
return item;
|
|
264
|
-
}
|
|
265
|
-
return new Promise((resolve, reject) => {
|
|
266
|
-
const waitingPulls = this.pendingPulls.get(id);
|
|
267
|
-
const pending = [resolve, reject];
|
|
268
|
-
if (waitingPulls) {
|
|
269
|
-
waitingPulls.push(pending);
|
|
270
|
-
} else {
|
|
271
|
-
this.pendingPulls.set(id, [pending]);
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
close({ id, reason } = {}) {
|
|
276
|
-
if (id === void 0) {
|
|
277
|
-
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
278
|
-
pendingPulls.forEach(([, reject]) => {
|
|
279
|
-
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
this.pendingPulls.clear();
|
|
283
|
-
this.openIds.clear();
|
|
284
|
-
this.items.clear();
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
288
|
-
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
289
|
-
});
|
|
290
|
-
this.pendingPulls.delete(id);
|
|
291
|
-
this.openIds.delete(id);
|
|
292
|
-
this.items.delete(id);
|
|
293
|
-
}
|
|
294
|
-
assertOpen(id) {
|
|
295
|
-
if (!this.isOpen(id)) {
|
|
296
|
-
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
354
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
355
|
+
const e = function() {
|
|
356
|
+
};
|
|
357
|
+
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
358
|
+
Object.freeze(e.prototype);
|
|
359
|
+
return e;
|
|
360
|
+
})();
|
|
300
361
|
|
|
301
362
|
function value(value2, ...args) {
|
|
302
363
|
if (typeof value2 === "function") {
|
|
@@ -305,4 +366,4 @@ function value(value2, ...args) {
|
|
|
305
366
|
return value2;
|
|
306
367
|
}
|
|
307
368
|
|
|
308
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
369
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.4.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -27,6 +27,11 @@
|
|
|
27
27
|
"radash": "^12.1.0",
|
|
28
28
|
"type-fest": "^4.39.1"
|
|
29
29
|
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"arktype": "2.1.20",
|
|
32
|
+
"valibot": "^1.1.0",
|
|
33
|
+
"zod": "^3.25.49"
|
|
34
|
+
},
|
|
30
35
|
"scripts": {
|
|
31
36
|
"build": "unbuild",
|
|
32
37
|
"build:watch": "pnpm run build --watch",
|