@orpc/shared 0.0.0-next.39c8cfb → 0.0.0-next.3b15fc2

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 CHANGED
@@ -30,7 +30,7 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
30
30
 
31
31
  declare const ORPC_NAME = "orpc";
32
32
  declare const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
33
- declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.39c8cfb";
33
+ declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.3b15fc2";
34
34
 
35
35
  /**
36
36
  * Error thrown when an operation is aborted.
@@ -60,7 +60,7 @@ interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
60
60
  /**
61
61
  * Aborts the async iterator. Throws if aborted before or during pulling.
62
62
  */
63
- signal?: AbortSignal;
63
+ signal?: AbortSignal | undefined;
64
64
  }
65
65
  declare class EventPublisher<T extends Record<PropertyKey, any>> {
66
66
  #private;
@@ -263,6 +263,11 @@ declare function findDeepMatches(check: (value: unknown) => boolean, payload: un
263
263
  maps: Segment[][];
264
264
  values: unknown[];
265
265
  };
266
+ /**
267
+ * Get constructor of the value
268
+ *
269
+ */
270
+ declare function getConstructor(value: unknown): Function | null | undefined;
266
271
  /**
267
272
  * Check if the value is an object even it created by `Object.create(null)` or more tricky way.
268
273
  */
@@ -291,9 +296,11 @@ interface AsyncIdQueueCloseOptions {
291
296
  }
292
297
  declare class AsyncIdQueue<T> {
293
298
  private readonly openIds;
294
- private readonly items;
295
- private readonly pendingPulls;
299
+ private readonly queues;
300
+ private readonly waiters;
296
301
  get length(): number;
302
+ get waiterIds(): string[];
303
+ hasBufferedItems(id: string): boolean;
297
304
  open(id: string): void;
298
305
  isOpen(id: string): boolean;
299
306
  push(id: string, item: T): void;
@@ -310,5 +317,5 @@ declare function tryDecodeURIComponent(value: string): string;
310
317
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
311
318
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
312
319
 
313
- 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, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
320
+ export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
314
321
  export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InferAsyncIterableYield, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
package/dist/index.d.ts CHANGED
@@ -30,7 +30,7 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
30
30
 
31
31
  declare const ORPC_NAME = "orpc";
32
32
  declare const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
33
- declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.39c8cfb";
33
+ declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.3b15fc2";
34
34
 
35
35
  /**
36
36
  * Error thrown when an operation is aborted.
@@ -60,7 +60,7 @@ interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
60
60
  /**
61
61
  * Aborts the async iterator. Throws if aborted before or during pulling.
62
62
  */
63
- signal?: AbortSignal;
63
+ signal?: AbortSignal | undefined;
64
64
  }
65
65
  declare class EventPublisher<T extends Record<PropertyKey, any>> {
66
66
  #private;
@@ -263,6 +263,11 @@ declare function findDeepMatches(check: (value: unknown) => boolean, payload: un
263
263
  maps: Segment[][];
264
264
  values: unknown[];
265
265
  };
266
+ /**
267
+ * Get constructor of the value
268
+ *
269
+ */
270
+ declare function getConstructor(value: unknown): Function | null | undefined;
266
271
  /**
267
272
  * Check if the value is an object even it created by `Object.create(null)` or more tricky way.
268
273
  */
@@ -291,9 +296,11 @@ interface AsyncIdQueueCloseOptions {
291
296
  }
292
297
  declare class AsyncIdQueue<T> {
293
298
  private readonly openIds;
294
- private readonly items;
295
- private readonly pendingPulls;
299
+ private readonly queues;
300
+ private readonly waiters;
296
301
  get length(): number;
302
+ get waiterIds(): string[];
303
+ hasBufferedItems(id: string): boolean;
297
304
  open(id: string): void;
298
305
  isOpen(id: string): boolean;
299
306
  push(id: string, item: T): void;
@@ -310,5 +317,5 @@ declare function tryDecodeURIComponent(value: string): string;
310
317
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
311
318
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
312
319
 
313
- 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, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
320
+ export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
314
321
  export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InferAsyncIterableYield, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
package/dist/index.mjs CHANGED
@@ -21,7 +21,7 @@ function readAsBuffer(source) {
21
21
 
22
22
  const ORPC_NAME = "orpc";
23
23
  const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
24
- const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.39c8cfb";
24
+ const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.3b15fc2";
25
25
 
26
26
  class AbortError extends Error {
27
27
  constructor(...rest) {
@@ -153,11 +153,17 @@ async function runInSpanContext(span, fn) {
153
153
 
154
154
  class AsyncIdQueue {
155
155
  openIds = /* @__PURE__ */ new Set();
156
- items = /* @__PURE__ */ new Map();
157
- pendingPulls = /* @__PURE__ */ new Map();
156
+ queues = /* @__PURE__ */ new Map();
157
+ waiters = /* @__PURE__ */ new Map();
158
158
  get length() {
159
159
  return this.openIds.size;
160
160
  }
161
+ get waiterIds() {
162
+ return Array.from(this.waiters.keys());
163
+ }
164
+ hasBufferedItems(id) {
165
+ return Boolean(this.queues.get(id)?.length);
166
+ }
161
167
  open(id) {
162
168
  this.openIds.add(id);
163
169
  }
@@ -166,59 +172,57 @@ class AsyncIdQueue {
166
172
  }
167
173
  push(id, item) {
168
174
  this.assertOpen(id);
169
- const pending = this.pendingPulls.get(id);
175
+ const pending = this.waiters.get(id);
170
176
  if (pending?.length) {
171
177
  pending.shift()[0](item);
172
178
  if (pending.length === 0) {
173
- this.pendingPulls.delete(id);
179
+ this.waiters.delete(id);
174
180
  }
175
181
  } else {
176
- const items = this.items.get(id);
182
+ const items = this.queues.get(id);
177
183
  if (items) {
178
184
  items.push(item);
179
185
  } else {
180
- this.items.set(id, [item]);
186
+ this.queues.set(id, [item]);
181
187
  }
182
188
  }
183
189
  }
184
190
  async pull(id) {
185
191
  this.assertOpen(id);
186
- const items = this.items.get(id);
192
+ const items = this.queues.get(id);
187
193
  if (items?.length) {
188
194
  const item = items.shift();
189
195
  if (items.length === 0) {
190
- this.items.delete(id);
196
+ this.queues.delete(id);
191
197
  }
192
198
  return item;
193
199
  }
194
200
  return new Promise((resolve, reject) => {
195
- const waitingPulls = this.pendingPulls.get(id);
201
+ const waitingPulls = this.waiters.get(id);
196
202
  const pending = [resolve, reject];
197
203
  if (waitingPulls) {
198
204
  waitingPulls.push(pending);
199
205
  } else {
200
- this.pendingPulls.set(id, [pending]);
206
+ this.waiters.set(id, [pending]);
201
207
  }
202
208
  });
203
209
  }
204
210
  close({ id, reason } = {}) {
205
211
  if (id === void 0) {
206
- this.pendingPulls.forEach((pendingPulls, id2) => {
207
- pendingPulls.forEach(([, reject]) => {
208
- reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
209
- });
212
+ this.waiters.forEach((pendingPulls, id2) => {
213
+ const error2 = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`);
214
+ pendingPulls.forEach(([, reject]) => reject(error2));
210
215
  });
211
- this.pendingPulls.clear();
216
+ this.waiters.clear();
212
217
  this.openIds.clear();
213
- this.items.clear();
218
+ this.queues.clear();
214
219
  return;
215
220
  }
216
- this.pendingPulls.get(id)?.forEach(([, reject]) => {
217
- reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
218
- });
219
- this.pendingPulls.delete(id);
221
+ const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
222
+ this.waiters.get(id)?.forEach(([, reject]) => reject(error));
223
+ this.waiters.delete(id);
220
224
  this.openIds.delete(id);
221
- this.items.delete(id);
225
+ this.queues.delete(id);
222
226
  }
223
227
  assertOpen(id) {
224
228
  if (!this.isOpen(id)) {
@@ -298,49 +302,51 @@ class AsyncIteratorClass {
298
302
  }
299
303
  function replicateAsyncIterator(source, count) {
300
304
  const queue = new AsyncIdQueue();
301
- const replicated = [];
302
- let error;
305
+ const ids = Array.from({ length: count }, (_, i) => i.toString());
306
+ let isSourceFinished = false;
303
307
  const start = once(async () => {
304
308
  try {
305
309
  while (true) {
306
310
  const item = await source.next();
307
- for (let id = 0; id < count; id++) {
308
- if (queue.isOpen(id.toString())) {
309
- queue.push(id.toString(), item);
311
+ ids.forEach((id) => {
312
+ if (queue.isOpen(id)) {
313
+ queue.push(id, { next: item });
310
314
  }
311
- }
315
+ });
312
316
  if (item.done) {
313
317
  break;
314
318
  }
315
319
  }
316
- } catch (e) {
317
- error = { value: e };
320
+ } catch (error) {
321
+ ids.forEach((id) => {
322
+ if (queue.isOpen(id)) {
323
+ queue.push(id, { error });
324
+ }
325
+ });
326
+ } finally {
327
+ isSourceFinished = true;
318
328
  }
319
329
  });
320
- for (let id = 0; id < count; id++) {
321
- queue.open(id.toString());
322
- replicated.push(new AsyncIteratorClass(
323
- () => {
330
+ const replicated = ids.map((id) => {
331
+ queue.open(id);
332
+ return new AsyncIteratorClass(
333
+ async () => {
324
334
  start();
325
- return new Promise((resolve, reject) => {
326
- queue.pull(id.toString()).then(resolve).catch(reject);
327
- defer(() => {
328
- if (error) {
329
- reject(error.value);
330
- }
331
- });
332
- });
335
+ const item = await queue.pull(id);
336
+ if (item.next) {
337
+ return item.next;
338
+ }
339
+ throw item.error;
333
340
  },
334
341
  async (reason) => {
335
- queue.close({ id: id.toString() });
336
- if (reason !== "next") {
337
- if (replicated.every((_, id2) => !queue.isOpen(id2.toString()))) {
338
- await source?.return?.();
339
- }
342
+ queue.close({ id });
343
+ if (reason !== "next" && !queue.length && !isSourceFinished) {
344
+ isSourceFinished = true;
345
+ await source?.return?.();
340
346
  }
341
347
  }
342
- ));
343
- }
348
+ );
349
+ });
344
350
  return replicated;
345
351
  }
346
352
  function asyncIteratorWithSpan({ name, ...options }, iterator) {
@@ -536,6 +542,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
536
542
  }
537
543
  return { maps, values };
538
544
  }
545
+ function getConstructor(value) {
546
+ if (!isTypescriptObject(value)) {
547
+ return null;
548
+ }
549
+ return Object.getPrototypeOf(value)?.constructor;
550
+ }
539
551
  function isObject(value) {
540
552
  if (!value || typeof value !== "object") {
541
553
  return false;
@@ -655,4 +667,4 @@ function value(value2, ...args) {
655
667
  return value2;
656
668
  }
657
669
 
658
- 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, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
670
+ export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/shared",
3
3
  "type": "module",
4
- "version": "0.0.0-next.39c8cfb",
4
+ "version": "0.0.0-next.3b15fc2",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -37,9 +37,9 @@
37
37
  },
38
38
  "devDependencies": {
39
39
  "@opentelemetry/api": "^1.9.0",
40
- "arktype": "2.1.20",
40
+ "arktype": "2.1.21",
41
41
  "valibot": "^1.1.0",
42
- "zod": "^4.0.17"
42
+ "zod": "^4.1.5"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "unbuild",