@anabranch/queue 0.1.3 → 0.1.5

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.
Files changed (39) hide show
  1. package/esm/{queue/adapter.d.ts → adapter.d.ts} +7 -1
  2. package/esm/adapter.d.ts.map +1 -0
  3. package/esm/errors.d.ts.map +1 -0
  4. package/esm/in-memory.d.ts.map +1 -0
  5. package/esm/{queue/in-memory.js → in-memory.js} +7 -0
  6. package/esm/{queue/index.d.ts → index.d.ts} +3 -3
  7. package/esm/index.d.ts.map +1 -0
  8. package/esm/{queue/index.js → index.js} +1 -1
  9. package/esm/{queue/queue.d.ts → queue.d.ts} +2 -1
  10. package/esm/queue.d.ts.map +1 -0
  11. package/esm/{queue/queue.js → queue.js} +6 -2
  12. package/package.json +3 -3
  13. package/esm/anabranch/index.d.ts +0 -44
  14. package/esm/anabranch/index.d.ts.map +0 -1
  15. package/esm/anabranch/index.js +0 -41
  16. package/esm/anabranch/streams/channel.d.ts +0 -15
  17. package/esm/anabranch/streams/channel.d.ts.map +0 -1
  18. package/esm/anabranch/streams/channel.js +0 -122
  19. package/esm/anabranch/streams/source.d.ts +0 -75
  20. package/esm/anabranch/streams/source.d.ts.map +0 -1
  21. package/esm/anabranch/streams/source.js +0 -77
  22. package/esm/anabranch/streams/stream.d.ts +0 -431
  23. package/esm/anabranch/streams/stream.d.ts.map +0 -1
  24. package/esm/anabranch/streams/stream.js +0 -627
  25. package/esm/anabranch/streams/task.d.ts +0 -117
  26. package/esm/anabranch/streams/task.d.ts.map +0 -1
  27. package/esm/anabranch/streams/task.js +0 -419
  28. package/esm/anabranch/streams/util.d.ts +0 -33
  29. package/esm/anabranch/streams/util.d.ts.map +0 -1
  30. package/esm/anabranch/streams/util.js +0 -18
  31. package/esm/queue/adapter.d.ts.map +0 -1
  32. package/esm/queue/errors.d.ts.map +0 -1
  33. package/esm/queue/in-memory.d.ts.map +0 -1
  34. package/esm/queue/index.d.ts.map +0 -1
  35. package/esm/queue/queue.d.ts.map +0 -1
  36. /package/esm/{queue/adapter.js → adapter.js} +0 -0
  37. /package/esm/{queue/errors.d.ts → errors.d.ts} +0 -0
  38. /package/esm/{queue/errors.js → errors.js} +0 -0
  39. /package/esm/{queue/in-memory.d.ts → in-memory.d.ts} +0 -0
@@ -18,7 +18,10 @@ export interface QueueMessage<T = unknown> {
18
18
  /** Timestamp when the message was first enqueued */
19
19
  timestamp: number;
20
20
  /** Optional metadata from the broker */
21
- metadata?: Record<string, unknown>;
21
+ metadata?: {
22
+ headers?: Record<string, string>;
23
+ [key: string]: unknown;
24
+ };
22
25
  }
23
26
  /** Options for sending a message with delay or scheduling. */
24
27
  export interface SendOptions {
@@ -30,6 +33,8 @@ export interface SendOptions {
30
33
  deadLetterQueue?: string;
31
34
  /** Message priority (higher = more important, if supported) */
32
35
  priority?: number;
36
+ /** Arbitrary key-value pairs to attach to the message */
37
+ headers?: Record<string, string>;
33
38
  }
34
39
  /** Options for negative acknowledgment. */
35
40
  export interface NackOptions {
@@ -58,6 +63,7 @@ export interface QueueAdapter {
58
63
  export interface StreamAdapter extends QueueAdapter {
59
64
  subscribe<T>(queue: string, options?: {
60
65
  signal?: AbortSignal;
66
+ prefetch?: number;
61
67
  }): AsyncIterable<QueueMessage<T>>;
62
68
  }
63
69
  /**
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACvC,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,sBAAsB;IACtB,IAAI,EAAE,CAAC,CAAC;IACR,sDAAsD;IACtD,OAAO,EAAE,MAAM,CAAC;IAChB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAC;IAClB,wCAAwC;IACxC,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,iBAAiB,CA+S3E;AAkBD,yCAAyC;AACzC,MAAM,WAAW,eAAe;IAC9B,4DAA4D;IAC5D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,gEAAgE;IAChE,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IAClC,8BAA8B;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CACvC;AAED,iCAAiC;AACjC,MAAM,WAAW,iBAAkB,SAAQ,cAAc;IACvD,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;IACjC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB"}
@@ -83,6 +83,10 @@ export function createInMemory(options) {
83
83
  .flat()
84
84
  .find((m) => m.id === id));
85
85
  };
86
+ /**
87
+ * Routes to DLQ when attempt > maxAttempts (not >=).
88
+ * A message with maxAttempts=2 will be delivered 2x before DLQ.
89
+ */
86
90
  const handleNack = (queue, id, nackOptions) => {
87
91
  const existing = findMessage(queue, id);
88
92
  if (nackOptions?.deadLetter) {
@@ -163,6 +167,9 @@ export function createInMemory(options) {
163
167
  data,
164
168
  attempt: 1,
165
169
  timestamp: Date.now(),
170
+ metadata: sendOptions?.headers
171
+ ? { headers: sendOptions.headers }
172
+ : undefined,
166
173
  };
167
174
  if (delayMs > 0) {
168
175
  const bucket = Math.ceil((Date.now() + delayMs) / 1000);
@@ -85,10 +85,10 @@
85
85
  */
86
86
  export { Queue } from "./queue.js";
87
87
  export type { QueueMessage } from "./adapter.js";
88
- export type { NackOptions, QueueAdapter, QueueConnector, QueueOptions, SendOptions, } from "./adapter.js";
88
+ export type { NackOptions, QueueAdapter, QueueConnector, QueueOptions, SendOptions, StreamAdapter, } from "./adapter.js";
89
89
  export * from "./errors.js";
90
90
  export { createInMemory } from "./in-memory.js";
91
91
  export type { InMemoryConnector } from "./in-memory.js";
92
- export { Task } from "../anabranch/index.js";
93
- export type { Source, Stream } from "../anabranch/index.js";
92
+ export { Task } from "anabranch";
93
+ export type { Source, Stream } from "anabranch";
94
94
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,EACV,WAAW,EACX,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,WAAW,EACX,aAAa,GACd,MAAM,cAAc,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC"}
@@ -86,4 +86,4 @@
86
86
  export { Queue } from "./queue.js";
87
87
  export * from "./errors.js";
88
88
  export { createInMemory } from "./in-memory.js";
89
- export { Task } from "../anabranch/index.js";
89
+ export { Task } from "anabranch";
@@ -1,4 +1,4 @@
1
- import { Source, Task } from "../anabranch/index.js";
1
+ import { Source, Task } from "anabranch";
2
2
  import type { QueueAdapter, QueueConnector, QueueMessage, SendOptions } from "./adapter.js";
3
3
  import { QueueAckFailed, QueueCloseFailed, QueueConnectionFailed, QueueMaxAttemptsExceeded, QueueReceiveFailed, QueueSendFailed } from "./errors.js";
4
4
  /**
@@ -132,6 +132,7 @@ export declare class Queue {
132
132
  continuousStream<T>(queue: string, options?: {
133
133
  count?: number;
134
134
  signal?: AbortSignal;
135
+ prefetch?: number;
135
136
  }): Source<QueueMessage<T>, QueueReceiveFailed>;
136
137
  /**
137
138
  * Acknowledge one or more messages as successfully processed.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../src/queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,KAAK,EACV,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,WAAW,EAEZ,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,wBAAwB,EACxB,kBAAkB,EAClB,eAAe,EAChB,MAAM,aAAa,CAAC;AAiBrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBAAa,KAAK;IACJ,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAElD;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CACZ,SAAS,EAAE,cAAc,GACxB,IAAI,CAAC,KAAK,EAAE,qBAAqB,CAAC;IAarC;;;;;;;OAOG;IACH,KAAK,IAAI,IAAI,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAarC;;;;;;;OAOG;IACH,IAAI,CAAC,CAAC,EACJ,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,CAAC,EACP,OAAO,CAAC,EAAE,WAAW,GACpB,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC;IAchC;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,CAAC,EACT,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,CAAC,EAAE,EACT,OAAO,CAAC,EAAE,WAAW,GAAG;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GAC7C,IAAI,CAAC,MAAM,EAAE,EAAE,eAAe,CAAC;IA0BlC;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,CAAC,EACN,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,GACjD,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC;IAqB9C;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,gBAAgB,CAAC,CAAC,EAChB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,GACpE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC;IAuD9C;;;;;;;OAOG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC;IAgBhE;;;;;;;;;;;;OAYG;IACH,IAAI,CACF,KAAK,EAAE,MAAM,EACb,EAAE,EAAE,MAAM,EACV,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,UAAU,CAAC,EAAE,OAAO,CAAC;KACtB,GACA,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,wBAAwB,CAAC;CAczD;AAED,6CAA6C;AAC7C,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC"}
@@ -1,4 +1,4 @@
1
- import { Source, Task } from "../anabranch/index.js";
1
+ import { Source, Task } from "anabranch";
2
2
  import { QueueAckFailed, QueueCloseFailed, QueueConnectionFailed, QueueReceiveFailed, QueueSendFailed, } from "./errors.js";
3
3
  async function sleep(ms, signal) {
4
4
  if (signal?.aborted)
@@ -200,12 +200,16 @@ export class Queue {
200
200
  continuousStream(queue, options) {
201
201
  const count = options?.count ?? 10;
202
202
  const signal = options?.signal;
203
+ const prefetch = options?.prefetch;
203
204
  const adapter = this.adapter;
204
205
  const isStreamAdapter = "subscribe" in adapter;
205
206
  return Source.fromResults(async function* () {
206
207
  if (isStreamAdapter) {
207
208
  const streamAdapter = adapter;
208
- const iterable = streamAdapter.subscribe(queue, { signal });
209
+ const iterable = streamAdapter.subscribe(queue, {
210
+ signal,
211
+ prefetch,
212
+ });
209
213
  for await (const msg of iterable) {
210
214
  if (signal?.aborted)
211
215
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anabranch/queue",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Message queue with dead letter queue and visibility timeout support",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,10 +10,10 @@
10
10
  "bugs": {
11
11
  "url": "https://github.com/frodi-karlsson/anabranch/issues"
12
12
  },
13
- "module": "./esm/queue/index.js",
13
+ "module": "./esm/index.js",
14
14
  "exports": {
15
15
  ".": {
16
- "import": "./esm/queue/index.js"
16
+ "import": "./esm/index.js"
17
17
  }
18
18
  },
19
19
  "scripts": {},
@@ -1,44 +0,0 @@
1
- /**
2
- * Anabranch is a TypeScript library for error-tolerant async streams.
3
- *
4
- * Instead of throwing on the first error, it collects errors alongside
5
- * successful values so you can process a stream to completion and deal with
6
- * failures at the end — or not at all.
7
- *
8
- * The entry point is {@link Source}. Once you have a stream, chain operations
9
- * like {@link Stream.map map}, {@link Stream.filter filter},
10
- * {@link Stream.flatMap flatMap}, and {@link Stream.fold fold} to transform it,
11
- * then consume it with {@link Stream.collect collect} or
12
- * {@link Stream.partition partition}.
13
- *
14
- * @example Fetch a list of URLs concurrently, collect results and failures separately
15
- * ```ts
16
- * import { Source } from "anabranch";
17
- *
18
- * const { successes, errors } = await Source.from<string, Error>(
19
- * async function* () {
20
- * yield "https://example.com/1";
21
- * yield "https://example.com/2";
22
- * yield "https://example.com/3";
23
- * },
24
- * )
25
- * .withConcurrency(3)
26
- * .map(async (url) => {
27
- * const res = await fetch(url);
28
- * if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
29
- * return res.json();
30
- * })
31
- * .partition();
32
- *
33
- * console.log(`${successes.length} succeeded, ${errors.length} failed`);
34
- * ```
35
- *
36
- * @module
37
- */
38
- export { Source } from "./streams/source.js";
39
- export { Task } from "./streams/task.js";
40
- export { Channel } from "./streams/channel.js";
41
- export type { Stream } from "./streams/stream.js";
42
- export { AggregateError } from "./streams/util.js";
43
- export type { ErrorResult, Promisable, Result, SuccessResult, } from "./streams/util.js";
44
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/anabranch/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,YAAY,EACV,WAAW,EACX,UAAU,EACV,MAAM,EACN,aAAa,GACd,MAAM,mBAAmB,CAAC"}
@@ -1,41 +0,0 @@
1
- /**
2
- * Anabranch is a TypeScript library for error-tolerant async streams.
3
- *
4
- * Instead of throwing on the first error, it collects errors alongside
5
- * successful values so you can process a stream to completion and deal with
6
- * failures at the end — or not at all.
7
- *
8
- * The entry point is {@link Source}. Once you have a stream, chain operations
9
- * like {@link Stream.map map}, {@link Stream.filter filter},
10
- * {@link Stream.flatMap flatMap}, and {@link Stream.fold fold} to transform it,
11
- * then consume it with {@link Stream.collect collect} or
12
- * {@link Stream.partition partition}.
13
- *
14
- * @example Fetch a list of URLs concurrently, collect results and failures separately
15
- * ```ts
16
- * import { Source } from "anabranch";
17
- *
18
- * const { successes, errors } = await Source.from<string, Error>(
19
- * async function* () {
20
- * yield "https://example.com/1";
21
- * yield "https://example.com/2";
22
- * yield "https://example.com/3";
23
- * },
24
- * )
25
- * .withConcurrency(3)
26
- * .map(async (url) => {
27
- * const res = await fetch(url);
28
- * if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
29
- * return res.json();
30
- * })
31
- * .partition();
32
- *
33
- * console.log(`${successes.length} succeeded, ${errors.length} failed`);
34
- * ```
35
- *
36
- * @module
37
- */
38
- export { Source } from "./streams/source.js";
39
- export { Task } from "./streams/task.js";
40
- export { Channel } from "./streams/channel.js";
41
- export { AggregateError } from "./streams/util.js";
@@ -1,15 +0,0 @@
1
- import { _StreamImpl } from "./stream.js";
2
- interface ChannelOptions<T> {
3
- bufferSize?: number;
4
- onDrop?: (value: T) => void;
5
- onClose?: () => void;
6
- }
7
- export declare class Channel<T, E = never> extends _StreamImpl<T, E> {
8
- private sourceImpl;
9
- constructor(options?: ChannelOptions<T>);
10
- send(value: T): void;
11
- fail(error: E): void;
12
- close(): void;
13
- }
14
- export {};
15
- //# sourceMappingURL=channel.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../../src/anabranch/streams/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG1C,UAAU,cAAc,CAAC,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB;AAgFD,qBAAa,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAE,SAAQ,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1D,OAAO,CAAC,UAAU,CAAsB;gBAE5B,OAAO,GAAE,cAAc,CAAC,CAAC,CAAM;IAM3C,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAIpB,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAIpB,KAAK,IAAI,IAAI;CAGd"}
@@ -1,122 +0,0 @@
1
- import { _StreamImpl } from "./stream.js";
2
- class ChannelSource {
3
- constructor(options = {}) {
4
- Object.defineProperty(this, "queue", {
5
- enumerable: true,
6
- configurable: true,
7
- writable: true,
8
- value: []
9
- });
10
- Object.defineProperty(this, "closed", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: false
15
- });
16
- Object.defineProperty(this, "consumers", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: []
21
- });
22
- Object.defineProperty(this, "bufferSize", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- Object.defineProperty(this, "onDrop", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: void 0
33
- });
34
- Object.defineProperty(this, "onClose", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: void 0
39
- });
40
- this.bufferSize = Number.isFinite(options.bufferSize)
41
- ? Math.max(1, options.bufferSize)
42
- : Infinity;
43
- this.onDrop = options.onDrop;
44
- this.onClose = options.onClose;
45
- }
46
- send(value) {
47
- if (this.closed) {
48
- return;
49
- }
50
- if (this.queue.length >= this.bufferSize && this.bufferSize !== Infinity) {
51
- this.onDrop?.(value);
52
- return;
53
- }
54
- this.queue.push({ type: "success", value });
55
- this.wake();
56
- }
57
- fail(error) {
58
- if (this.closed) {
59
- return;
60
- }
61
- this.queue.push({ type: "error", error });
62
- this.wake();
63
- }
64
- close() {
65
- if (this.closed) {
66
- return;
67
- }
68
- this.closed = true;
69
- this.wake();
70
- }
71
- wake() {
72
- while (this.consumers.length > 0) {
73
- const consumer = this.consumers.shift();
74
- if (consumer)
75
- consumer();
76
- }
77
- }
78
- async *generator() {
79
- try {
80
- while (true) {
81
- while (this.queue.length > 0) {
82
- yield this.queue.shift();
83
- }
84
- if (this.closed && this.queue.length === 0) {
85
- return;
86
- }
87
- if (this.queue.length === 0) {
88
- await new Promise((resolve) => {
89
- this.consumers.push(resolve);
90
- });
91
- if (this.queue.length > 0)
92
- continue;
93
- }
94
- }
95
- }
96
- finally {
97
- this.onClose?.();
98
- }
99
- }
100
- }
101
- export class Channel extends _StreamImpl {
102
- constructor(options = {}) {
103
- const sourceImpl = new ChannelSource(options);
104
- super(() => sourceImpl.generator(), Infinity, Infinity);
105
- Object.defineProperty(this, "sourceImpl", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: void 0
110
- });
111
- this.sourceImpl = sourceImpl;
112
- }
113
- send(value) {
114
- this.sourceImpl.send(value);
115
- }
116
- fail(error) {
117
- this.sourceImpl.fail(error);
118
- }
119
- close() {
120
- this.sourceImpl.close();
121
- }
122
- }
@@ -1,75 +0,0 @@
1
- import { _StreamImpl } from "./stream.js";
2
- import type { Result } from "./util.js";
3
- /**
4
- * The entry point for creating a {@link Stream}. Wraps an async generator so
5
- * that yielded values become success results and any thrown error becomes a
6
- * single error result.
7
- *
8
- * Use {@link Source.from} to create a source from an existing `AsyncIterable`
9
- * or generator function. Use {@link Source.withConcurrency} and
10
- * {@link Source.withBufferSize} to configure parallel execution.
11
- *
12
- * @example
13
- * ```ts
14
- * import { Source } from "anabranch";
15
- *
16
- * const stream = Source.from<number, Error>(async function* () {
17
- * yield 1;
18
- * yield 2;
19
- * yield 3;
20
- * });
21
- *
22
- * const results = await stream.collect();
23
- * ```
24
- */
25
- export declare class Source<T, E> extends _StreamImpl<T, E> {
26
- private readonly resultSource;
27
- /**
28
- * @param source An async generator function. Each yielded value becomes a
29
- * success result; any thrown error becomes an error result and terminates
30
- * the source.
31
- * @param concurrency Maximum number of concurrent operations. Defaults to `Infinity`.
32
- * @param bufferSize Maximum number of buffered results before backpressure is applied. Defaults to `Infinity`.
33
- */
34
- private constructor();
35
- /**
36
- * Creates a {@link Source} from an existing `AsyncIterable` or async
37
- * generator function. Each value emitted becomes a success result; any
38
- * thrown error becomes an error result.
39
- *
40
- * @example
41
- * ```ts
42
- * import { Source } from "anabranch";
43
- *
44
- * // From a generator function
45
- * const stream = Source.from<number, Error>(async function* () {
46
- * yield 1;
47
- * yield 2;
48
- * });
49
- *
50
- * // From an AsyncIterable
51
- * async function* generate() {
52
- * yield 1;
53
- * yield 2;
54
- * }
55
- * const stream2 = Source.from<number, Error>(generate());
56
- * ```
57
- */
58
- static from<T, E>(source: AsyncIterable<T>): Source<T, E>;
59
- static from<T, E>(fn: () => AsyncGenerator<T>): Source<T, E>;
60
- /**
61
- * Creates a {@link Source} from an async generator that yields {@link Result}
62
- * values directly. This is useful when you want to yield both successes and
63
- * errors from the source without terminating it on the first error.
64
- */
65
- static fromResults<T, E>(source: () => AsyncGenerator<Result<T, E>>): Source<T, E>;
66
- /**
67
- * Sets the maximum number of concurrent operations for the stream.
68
- */
69
- withConcurrency(n: number): Source<T, E>;
70
- /**
71
- * Sets the maximum number of buffered results before backpressure is applied to the stream. If the buffer is full, the stream will pause until there is space in the buffer for new results.
72
- */
73
- withBufferSize(n: number): Source<T, E>;
74
- }
75
- //# sourceMappingURL=source.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../../../src/anabranch/streams/source.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,MAAM,CAAC,CAAC,EAAE,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;IAS/C,OAAO,CAAC,QAAQ,CAAC,YAAY;IAR/B;;;;;;OAMG;IACH,OAAO;IAQP;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IACzD,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,cAAc,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAoB5D;;;;OAIG;IACH,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,EACrB,MAAM,EAAE,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GACzC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAIf;;OAEG;IACH,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAIxC;;OAEG;IACH,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;CAGxC"}
@@ -1,77 +0,0 @@
1
- import { _StreamImpl } from "./stream.js";
2
- /**
3
- * The entry point for creating a {@link Stream}. Wraps an async generator so
4
- * that yielded values become success results and any thrown error becomes a
5
- * single error result.
6
- *
7
- * Use {@link Source.from} to create a source from an existing `AsyncIterable`
8
- * or generator function. Use {@link Source.withConcurrency} and
9
- * {@link Source.withBufferSize} to configure parallel execution.
10
- *
11
- * @example
12
- * ```ts
13
- * import { Source } from "anabranch";
14
- *
15
- * const stream = Source.from<number, Error>(async function* () {
16
- * yield 1;
17
- * yield 2;
18
- * yield 3;
19
- * });
20
- *
21
- * const results = await stream.collect();
22
- * ```
23
- */
24
- export class Source extends _StreamImpl {
25
- /**
26
- * @param source An async generator function. Each yielded value becomes a
27
- * success result; any thrown error becomes an error result and terminates
28
- * the source.
29
- * @param concurrency Maximum number of concurrent operations. Defaults to `Infinity`.
30
- * @param bufferSize Maximum number of buffered results before backpressure is applied. Defaults to `Infinity`.
31
- */
32
- constructor(resultSource, concurrency = Infinity, bufferSize = Infinity) {
33
- super(resultSource, concurrency, bufferSize);
34
- Object.defineProperty(this, "resultSource", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: resultSource
39
- });
40
- }
41
- static from(source) {
42
- const fn = typeof source === "function" ? source : async function* () {
43
- yield* source;
44
- };
45
- const resultSource = async function* () {
46
- try {
47
- for await (const value of fn()) {
48
- yield { type: "success", value };
49
- }
50
- }
51
- catch (error) {
52
- yield { type: "error", error: error };
53
- }
54
- };
55
- return new Source(resultSource);
56
- }
57
- /**
58
- * Creates a {@link Source} from an async generator that yields {@link Result}
59
- * values directly. This is useful when you want to yield both successes and
60
- * errors from the source without terminating it on the first error.
61
- */
62
- static fromResults(source) {
63
- return new Source(source);
64
- }
65
- /**
66
- * Sets the maximum number of concurrent operations for the stream.
67
- */
68
- withConcurrency(n) {
69
- return new Source(this.resultSource, n, this.bufferSize);
70
- }
71
- /**
72
- * Sets the maximum number of buffered results before backpressure is applied to the stream. If the buffer is full, the stream will pause until there is space in the buffer for new results.
73
- */
74
- withBufferSize(n) {
75
- return new Source(this.resultSource, this.concurrency, n);
76
- }
77
- }