@orkestrel/queue 0.0.16 → 0.0.18

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.
@@ -180,9 +180,12 @@ export declare function isQueueConcurrency(value: unknown): value is number;
180
180
  * Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
181
181
  * value.
182
182
  *
183
+ * @remarks
184
+ * Recognizes the shared brand and declared members across package copies, including subclasses.
185
+ * Refuses a transparent proxy because its brand holds the target's identity.
186
+ *
183
187
  * @param value - The value to inspect
184
- * @returns True if the value is a real `QueueError` instance; false otherwise, including for
185
- * a hostile value
188
+ * @returns True if a branded error or subclass has valid members; false otherwise.
186
189
  *
187
190
  * @example
188
191
  * ```ts
@@ -180,9 +180,12 @@ export declare function isQueueConcurrency(value: unknown): value is number;
180
180
  * Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
181
181
  * value.
182
182
  *
183
+ * @remarks
184
+ * Recognizes the shared brand and declared members across package copies, including subclasses.
185
+ * Refuses a transparent proxy because its brand holds the target's identity.
186
+ *
183
187
  * @param value - The value to inspect
184
- * @returns True if the value is a real `QueueError` instance; false otherwise, including for
185
- * a hostile value
188
+ * @returns True if a branded error or subclass has valid members; false otherwise.
186
189
  *
187
190
  * @example
188
191
  * ```ts
@@ -1,9 +1,9 @@
1
- import { cloneJSONValue, createContract, holds, integerShape, isError, isFiniteNumber, isInstance, isInteger, isRecord, isString, preview, stringShape } from "@orkestrel/contract";
1
+ import { INTRINSICS, cloneJSONValue, createContract, holds, integerShape, isError, isFiniteNumber, isFunction, isInteger, isObject, isRecord, isString, preview, stringShape } from "@orkestrel/contract";
2
2
  import { MAX_TIMEOUT_MS, createTimeout } from "@orkestrel/timeout";
3
3
  import { createAbort } from "@orkestrel/abort";
4
4
  import { Emitter } from "@orkestrel/emitter";
5
5
  import { createDatabase } from "@orkestrel/database";
6
- //#region src/core/errors.ts
6
+ // src/core/errors.ts
7
7
  /**
8
8
  * Represents a queue failure carrying a lowercase machine-readable `code`, optional
9
9
  * structured context, and an optional cause.
@@ -26,6 +26,7 @@ var QueueError = class extends Error {
26
26
  constructor(message, options) {
27
27
  const cause = options.cause;
28
28
  super(message, cause === void 0 ? void 0 : { cause });
29
+ INTRINSICS.define(this, Symbol.for("@orkestrel/queue.QueueError"), { value: this });
29
30
  this.code = options.code;
30
31
  this.context = options.context === void 0 ? void 0 : Object.freeze({ ...options.context });
31
32
  }
@@ -34,9 +35,12 @@ var QueueError = class extends Error {
34
35
  * Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
35
36
  * value.
36
37
  *
38
+ * @remarks
39
+ * Recognizes the shared brand and declared members across package copies, including subclasses.
40
+ * Refuses a transparent proxy because its brand holds the target's identity.
41
+ *
37
42
  * @param value - The value to inspect
38
- * @returns True if the value is a real `QueueError` instance; false otherwise, including for
39
- * a hostile value
43
+ * @returns True if a branded error or subclass has valid members; false otherwise.
40
44
  *
41
45
  * @example
42
46
  * ```ts
@@ -48,10 +52,24 @@ var QueueError = class extends Error {
48
52
  * ```
49
53
  */
50
54
  function isQueueError(value) {
51
- return isInstance(value, QueueError);
55
+ return holds(() => {
56
+ if (!isError(value) || INTRINSICS.prototype(value) === Error.prototype) return false;
57
+ const brand = INTRINSICS.describe(value, Symbol.for("@orkestrel/queue.QueueError"));
58
+ if (brand === void 0 || brand.value !== value) return false;
59
+ const code = INTRINSICS.reflect.read(value, "code");
60
+ if (code !== "invalid" && code !== "duplicate" && code !== "stopped" && code !== "aborted" && code !== "destroyed" && code !== "cleared" && code !== "timeout" && code !== "store" && code !== "cleanup") return false;
61
+ const context = INTRINSICS.reflect.read(value, "context");
62
+ if (context === void 0) return true;
63
+ if (!isObject(context) && !isFunction(context)) return false;
64
+ const id = INTRINSICS.reflect.read(context, "id");
65
+ if (id !== void 0 && !isString(id)) return false;
66
+ const option = INTRINSICS.reflect.read(context, "option");
67
+ if (option !== void 0 && option !== "id" && option !== "concurrency" && option !== "retries" && option !== "timeout" && option !== "signal") return false;
68
+ const operation = INTRINSICS.reflect.read(context, "operation");
69
+ return operation === void 0 || operation === "save" || operation === "remove" || operation === "load" || operation === "clear";
70
+ });
52
71
  }
53
- //#endregion
54
- //#region src/core/validators.ts
72
+ // src/core/validators.ts
55
73
  /**
56
74
  * Determines whether a value is a valid queue concurrency — a positive safe integer.
57
75
  *
@@ -142,8 +160,7 @@ function isQueueSignal(value) {
142
160
  function isStoredEntry(value) {
143
161
  return holds(() => isRecord(value) && isString(value.id) && "input" in value && isQueueRetries(value.attempts));
144
162
  }
145
- //#endregion
146
- //#region src/core/helpers.ts
163
+ // src/core/helpers.ts
147
164
  /**
148
165
  * Reads one named option from a caller-supplied entry options object exactly once,
149
166
  * containing a throwing getter as a coded failure.
@@ -201,8 +218,35 @@ function validateOption(value, guard, option, message) {
201
218
  }
202
219
  });
203
220
  }
204
- //#endregion
205
- //#region src/core/Queue.ts
221
+ // src/core/QueueEntry.ts
222
+ /** Holds one queue reservation's execution and settlement state. */
223
+ var QueueEntry = class {
224
+ id;
225
+ input;
226
+ options;
227
+ #attempts;
228
+ #settled = false;
229
+ constructor(id, input, options, attempts) {
230
+ this.id = id;
231
+ this.input = input;
232
+ this.options = options;
233
+ this.#attempts = attempts;
234
+ }
235
+ get attempts() {
236
+ return this.#attempts;
237
+ }
238
+ get settled() {
239
+ return this.#settled;
240
+ }
241
+ complete() {
242
+ this.#attempts += 1;
243
+ return this.#attempts;
244
+ }
245
+ settle() {
246
+ this.#settled = true;
247
+ }
248
+ };
249
+ // src/core/Queue.ts
206
250
  /**
207
251
  * Represents a concurrent, cooperative FIFO job queue — a wake-park worker loop with
208
252
  * retries, a per-attempt timeout, abort, and optional outstanding-work persistence.
@@ -325,30 +369,8 @@ var Queue = class {
325
369
  async restore() {
326
370
  if (this.#store === void 0 || this.stopped || this.#destroyPromise !== void 0) return;
327
371
  const generation = this.#generation;
328
- const entries = [];
329
- let malformed = false;
330
- try {
331
- const loaded = await this.#store.load();
332
- for (const stored of loaded) {
333
- const candidate = {
334
- id: stored.id,
335
- input: stored.input,
336
- attempts: stored.attempts
337
- };
338
- if (!isStoredEntry(candidate)) {
339
- malformed = true;
340
- break;
341
- }
342
- entries.push(Object.freeze(candidate));
343
- }
344
- } catch (error) {
345
- throw new QueueError("queue store load failed", {
346
- code: "store",
347
- cause: error,
348
- context: { operation: "load" }
349
- });
350
- }
351
- if (malformed) throw new QueueError("queue store returned an invalid entry", {
372
+ const entries = await this.#load(this.#store);
373
+ if (entries === void 0) throw new QueueError("queue store returned an invalid entry", {
352
374
  code: "store",
353
375
  context: { operation: "load" }
354
376
  });
@@ -361,6 +383,33 @@ var Queue = class {
361
383
  token.promise.catch(() => {});
362
384
  }
363
385
  }
386
+ async #load(store) {
387
+ try {
388
+ const loaded = await store.load();
389
+ const entries = [...this.#snapshot(loaded)];
390
+ return entries.some((entry) => entry === void 0) ? void 0 : entries.filter((entry) => entry !== void 0);
391
+ } catch (error) {
392
+ throw new QueueError("queue store load failed", {
393
+ code: "store",
394
+ cause: error,
395
+ context: { operation: "load" }
396
+ });
397
+ }
398
+ }
399
+ *#snapshot(loaded) {
400
+ for (const stored of loaded) {
401
+ const candidate = {
402
+ id: stored.id,
403
+ input: stored.input,
404
+ attempts: stored.attempts
405
+ };
406
+ if (!isStoredEntry(candidate)) {
407
+ yield void 0;
408
+ return;
409
+ }
410
+ yield Object.freeze(candidate);
411
+ }
412
+ }
364
413
  /** Begins or restarts worker execution unless the queue is terminal. */
365
414
  start() {
366
415
  if (this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) return;
@@ -465,13 +514,7 @@ var Queue = class {
465
514
  }
466
515
  #reserve(id, input, options, attempts) {
467
516
  const token = Promise.withResolvers();
468
- this.#entries.set(token, {
469
- id,
470
- input,
471
- options,
472
- attempts,
473
- settled: false
474
- });
517
+ this.#entries.set(token, new QueueEntry(id, input, options, attempts));
475
518
  this.#live.set(id, token);
476
519
  this.#drained = false;
477
520
  return token;
@@ -661,12 +704,11 @@ var Queue = class {
661
704
  const retries = entry.options?.retries ?? this.#retries;
662
705
  const timeout = entry.options?.timeout ?? this.#timeout;
663
706
  const signal = entry.options?.signal;
664
- let completed = entry.attempts;
665
707
  this.#emitter.emit("start", entry.id);
666
708
  while (true) {
667
709
  if (await this.#interrupt(token, signal)) return;
668
710
  const outcome = await this.#attempt(entry.id, entry.input, timeout, signal);
669
- completed += 1;
711
+ const completed = entry.complete();
670
712
  if (outcome.success) {
671
713
  await this.#settle(token, outcome);
672
714
  return;
@@ -755,7 +797,7 @@ var Queue = class {
755
797
  #settleToken(token, outcome) {
756
798
  const entry = this.#entries.get(token);
757
799
  if (entry === void 0 || entry.settled) return false;
758
- entry.settled = true;
800
+ entry.settle();
759
801
  if (outcome.success) token.resolve(outcome.value);
760
802
  else token.reject(outcome.error);
761
803
  if (this.#live.get(entry.id) !== token) this.#entries.delete(token);
@@ -771,16 +813,12 @@ var Queue = class {
771
813
  }
772
814
  async #attempt(id, input, timeout, signal) {
773
815
  const cancel = signal === void 0 ? this.#abort.signal : AbortSignal.any([this.#abort.signal, signal]);
774
- let deadline;
775
- let attemptSignal = cancel;
776
- if (timeout > 0) {
777
- deadline = createTimeout({
778
- ms: timeout,
779
- signal: cancel
780
- });
781
- deadline.start();
782
- attemptSignal = AbortSignal.any([cancel, deadline.signal]);
783
- }
816
+ const deadline = timeout > 0 ? createTimeout({
817
+ ms: timeout,
818
+ signal: cancel
819
+ }) : void 0;
820
+ deadline?.start();
821
+ const attemptSignal = deadline === void 0 ? cancel : AbortSignal.any([cancel, deadline.signal]);
784
822
  try {
785
823
  return {
786
824
  success: true,
@@ -850,12 +888,7 @@ var Queue = class {
850
888
  }
851
889
  }
852
890
  async #settleDestroy(barrier, aborting) {
853
- let failure;
854
- try {
855
- await aborting;
856
- } catch (error) {
857
- failure = error;
858
- }
891
+ const failure = await aborting.catch((error) => error);
859
892
  try {
860
893
  await this.#finish([
861
894
  ...this.#workers,
@@ -864,11 +897,12 @@ var Queue = class {
864
897
  ...this.#cleanOrphans(true)
865
898
  ], "queue destroy cleanup failed");
866
899
  } catch (error) {
867
- failure = failure === void 0 ? error : new QueueError("queue destroy cleanup failed", {
900
+ barrier.reject(failure === void 0 ? error : new QueueError("queue destroy cleanup failed", {
868
901
  code: "cleanup",
869
902
  cause: new AggregateError([failure, error], "queue destroy cleanup failed"),
870
903
  context: { operation: "remove" }
871
- });
904
+ }));
905
+ return;
872
906
  } finally {
873
907
  this.#emitter.destroy();
874
908
  }
@@ -876,8 +910,7 @@ var Queue = class {
876
910
  else barrier.reject(failure);
877
911
  }
878
912
  };
879
- //#endregion
880
- //#region src/core/stores/DatabaseQueueStore.ts
913
+ // src/core/stores/DatabaseQueueStore.ts
881
914
  /**
882
915
  * Represents the opt-in durable store for a queue's outstanding entries, backed by one table
883
916
  * of the `@orkestrel/database` layer — a queue's durable state is a table, so persistence
@@ -935,8 +968,7 @@ var DatabaseQueueStore = class {
935
968
  await this.#table.clear();
936
969
  }
937
970
  };
938
- //#endregion
939
- //#region src/core/stores/MemoryQueueStore.ts
971
+ // src/core/stores/MemoryQueueStore.ts
940
972
  /**
941
973
  * Represents an in-memory store of a queue's outstanding entries — a process-lifetime `Map`
942
974
  * owning validated, immutable JSON snapshots.
@@ -1031,8 +1063,7 @@ var MemoryQueueStore = class {
1031
1063
  });
1032
1064
  }
1033
1065
  };
1034
- //#endregion
1035
- //#region src/core/factories.ts
1066
+ // src/core/factories.ts
1036
1067
  /**
1037
1068
  * Creates a concurrent, cooperative job queue — a bounded-concurrency engine that
1038
1069
  * runs each enqueued input through a handler, with retries and a per-attempt
@@ -1120,7 +1151,6 @@ function createDatabaseQueueStore(input, driver) {
1120
1151
  function createMemoryQueueStore(input) {
1121
1152
  return new MemoryQueueStore(input);
1122
1153
  }
1123
- //#endregion
1124
1154
  export { DatabaseQueueStore, MemoryQueueStore, Queue, QueueError, createDatabaseQueueStore, createMemoryQueueStore, createQueue, isQueueConcurrency, isQueueError, isQueueRetries, isQueueSignal, isQueueTimeout, isStoredEntry, readOption, validateOption };
1125
1155
 
1126
1156
  //# sourceMappingURL=index.js.map