@tstdl/base 0.93.138 → 0.93.140

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 (138) hide show
  1. package/README.md +166 -0
  2. package/ai/genkit/multi-region.plugin.js +5 -3
  3. package/ai/genkit/tests/multi-region.test.d.ts +1 -0
  4. package/ai/genkit/tests/multi-region.test.js +5 -2
  5. package/ai/parser/parser.js +2 -2
  6. package/ai/prompts/build.js +1 -0
  7. package/ai/prompts/instructions-formatter.d.ts +15 -2
  8. package/ai/prompts/instructions-formatter.js +36 -31
  9. package/ai/prompts/prompt-builder.js +5 -5
  10. package/ai/prompts/steering.d.ts +3 -2
  11. package/ai/prompts/steering.js +3 -1
  12. package/ai/tests/instructions-formatter.test.js +1 -0
  13. package/api/README.md +403 -0
  14. package/api/client/client.js +7 -13
  15. package/api/client/tests/api-client.test.js +10 -10
  16. package/api/default-error-handlers.js +1 -1
  17. package/api/response.d.ts +2 -2
  18. package/api/response.js +22 -33
  19. package/api/server/api-controller.d.ts +1 -1
  20. package/api/server/api-controller.js +3 -3
  21. package/api/server/api-request-token.provider.d.ts +1 -0
  22. package/api/server/api-request-token.provider.js +1 -0
  23. package/api/server/middlewares/allowed-methods.middleware.js +2 -1
  24. package/api/server/middlewares/content-type.middleware.js +2 -1
  25. package/api/types.d.ts +3 -2
  26. package/application/README.md +240 -0
  27. package/application/application.js +2 -2
  28. package/audit/README.md +267 -0
  29. package/authentication/README.md +288 -0
  30. package/authentication/client/authentication.service.d.ts +12 -11
  31. package/authentication/client/authentication.service.js +21 -21
  32. package/authentication/client/http-client.middleware.js +2 -2
  33. package/authentication/tests/authentication.client-error-handling.test.js +2 -1
  34. package/authentication/tests/authentication.client-service-refresh.test.js +5 -3
  35. package/browser/README.md +401 -0
  36. package/cancellation/README.md +156 -0
  37. package/cancellation/tests/coverage.test.d.ts +1 -0
  38. package/cancellation/tests/coverage.test.js +49 -0
  39. package/cancellation/tests/leak.test.d.ts +1 -0
  40. package/cancellation/tests/leak.test.js +35 -0
  41. package/cancellation/tests/token.test.d.ts +1 -0
  42. package/cancellation/tests/token.test.js +136 -0
  43. package/cancellation/token.d.ts +53 -177
  44. package/cancellation/token.js +132 -201
  45. package/context/README.md +174 -0
  46. package/cookie/README.md +161 -0
  47. package/css/README.md +157 -0
  48. package/data-structures/README.md +320 -0
  49. package/decorators/README.md +140 -0
  50. package/distributed-loop/README.md +231 -0
  51. package/distributed-loop/distributed-loop.js +1 -1
  52. package/document-management/README.md +403 -0
  53. package/document-management/server/services/document-management.service.js +9 -7
  54. package/document-management/tests/document-management-core.test.js +2 -7
  55. package/document-management/tests/document-management.api.test.js +6 -7
  56. package/document-management/tests/document-statistics.service.test.js +11 -12
  57. package/document-management/tests/document.service.test.js +3 -3
  58. package/document-management/tests/enum-helpers.test.js +2 -3
  59. package/dom/README.md +213 -0
  60. package/enumerable/README.md +259 -0
  61. package/enumeration/README.md +121 -0
  62. package/errors/README.md +267 -0
  63. package/file/README.md +191 -0
  64. package/formats/README.md +210 -0
  65. package/function/README.md +144 -0
  66. package/http/README.md +318 -0
  67. package/http/client/adapters/undici.adapter.js +1 -1
  68. package/http/client/http-client-request.d.ts +6 -5
  69. package/http/client/http-client-request.js +8 -9
  70. package/http/server/node/node-http-server.js +1 -2
  71. package/image-service/README.md +137 -0
  72. package/injector/README.md +491 -0
  73. package/injector/injector.d.ts +1 -0
  74. package/injector/injector.js +17 -5
  75. package/injector/tests/leak.test.d.ts +1 -0
  76. package/injector/tests/leak.test.js +45 -0
  77. package/intl/README.md +113 -0
  78. package/json-path/README.md +182 -0
  79. package/jsx/README.md +154 -0
  80. package/key-value-store/README.md +191 -0
  81. package/lock/README.md +249 -0
  82. package/lock/web/web-lock.js +119 -47
  83. package/logger/README.md +287 -0
  84. package/mail/README.md +256 -0
  85. package/memory/README.md +144 -0
  86. package/message-bus/README.md +244 -0
  87. package/message-bus/message-bus-base.js +1 -1
  88. package/module/README.md +182 -0
  89. package/module/module.d.ts +1 -1
  90. package/module/module.js +77 -17
  91. package/module/modules/web-server.module.js +1 -1
  92. package/notification/tests/notification-type.service.test.js +24 -15
  93. package/object-storage/README.md +300 -0
  94. package/openid-connect/README.md +274 -0
  95. package/orm/README.md +423 -0
  96. package/package.json +8 -6
  97. package/password/README.md +164 -0
  98. package/pdf/README.md +246 -0
  99. package/polyfills.js +1 -0
  100. package/pool/README.md +198 -0
  101. package/process/README.md +237 -0
  102. package/promise/README.md +252 -0
  103. package/promise/cancelable-promise.js +1 -1
  104. package/random/README.md +193 -0
  105. package/reflection/README.md +305 -0
  106. package/rpc/README.md +386 -0
  107. package/rxjs-utils/README.md +262 -0
  108. package/schema/README.md +342 -0
  109. package/serializer/README.md +342 -0
  110. package/signals/implementation/README.md +134 -0
  111. package/sse/README.md +278 -0
  112. package/task-queue/README.md +300 -0
  113. package/task-queue/postgres/task-queue.d.ts +2 -1
  114. package/task-queue/postgres/task-queue.js +32 -2
  115. package/task-queue/task-context.js +1 -1
  116. package/task-queue/task-queue.d.ts +17 -0
  117. package/task-queue/task-queue.js +103 -44
  118. package/task-queue/tests/complex.test.js +4 -4
  119. package/task-queue/tests/dependencies.test.js +4 -2
  120. package/task-queue/tests/queue.test.js +111 -0
  121. package/task-queue/tests/worker.test.js +21 -13
  122. package/templates/README.md +287 -0
  123. package/testing/README.md +157 -0
  124. package/text/README.md +346 -0
  125. package/threading/README.md +238 -0
  126. package/types/README.md +311 -0
  127. package/utils/README.md +322 -0
  128. package/utils/async-iterable-helpers/observable-iterable.d.ts +1 -1
  129. package/utils/async-iterable-helpers/observable-iterable.js +4 -8
  130. package/utils/async-iterable-helpers/take-until.js +4 -4
  131. package/utils/backoff.js +89 -30
  132. package/utils/retry-with-backoff.js +1 -1
  133. package/utils/timer.d.ts +1 -1
  134. package/utils/timer.js +5 -7
  135. package/utils/timing.d.ts +1 -1
  136. package/utils/timing.js +2 -4
  137. package/utils/z-base32.d.ts +1 -0
  138. package/utils/z-base32.js +1 -0
@@ -0,0 +1,136 @@
1
+ import '../../polyfills.js';
2
+ import { firstValueFrom, from } from 'rxjs';
3
+ import { describe, expect, it, vi } from 'vitest';
4
+ import { CancellationSignal, CancellationToken } from '../token.js';
5
+ describe('CancellationToken', () => {
6
+ it('should be unset by default', () => {
7
+ const token = new CancellationToken();
8
+ expect(token.isSet).toBe(false);
9
+ expect(token.isUnset).toBe(true);
10
+ });
11
+ it('should be set when set() is called', () => {
12
+ const token = new CancellationToken();
13
+ token.set('reason');
14
+ expect(token.isSet).toBe(true);
15
+ expect(token.isUnset).toBe(false);
16
+ expect(token.reason).toBeInstanceOf(Error);
17
+ expect(token.reason.message).toBe('Operation cancelled');
18
+ expect(token.reason.cause).toBe('reason');
19
+ });
20
+ it('should fork a new token', () => {
21
+ const parent = new CancellationToken();
22
+ const child = parent.fork();
23
+ expect(child).toBeInstanceOf(CancellationToken);
24
+ expect(child.isSet).toBe(false);
25
+ parent.set();
26
+ expect(child.isSet).toBe(true);
27
+ });
28
+ it('should support multiple parents', () => {
29
+ const parent1 = new CancellationToken();
30
+ const parent2 = new CancellationToken();
31
+ const child = new CancellationToken([parent1, parent2]);
32
+ expect(child.isSet).toBe(false);
33
+ parent2.set();
34
+ expect(child.isSet).toBe(true);
35
+ });
36
+ it('should support single parent', () => {
37
+ const parent = new CancellationToken();
38
+ const child = new CancellationToken(parent);
39
+ expect(child.isSet).toBe(false);
40
+ parent.set();
41
+ expect(child.isSet).toBe(true);
42
+ });
43
+ it('should support AbortController as a source', () => {
44
+ const controller = new AbortController();
45
+ const signal = CancellationSignal.from(controller);
46
+ expect(signal.isSet).toBe(false);
47
+ controller.abort();
48
+ expect(signal.isSet).toBe(true);
49
+ });
50
+ it('should support AbortSignal as a source', () => {
51
+ const controller = new AbortController();
52
+ const signal = CancellationSignal.from(controller.signal);
53
+ expect(signal.isSet).toBe(false);
54
+ controller.abort();
55
+ expect(signal.isSet).toBe(true);
56
+ });
57
+ it('should wait for cancellation (resolve)', async () => {
58
+ const token = new CancellationToken();
59
+ setTimeout(() => token.set(), 10);
60
+ await token.wait();
61
+ expect(token.isSet).toBe(true);
62
+ });
63
+ it('should wait and throw (waitThrow)', async () => {
64
+ const token = new CancellationToken();
65
+ setTimeout(() => token.set('foo'), 10);
66
+ await expect(token.waitThrow()).rejects.toThrow('Operation cancelled');
67
+ });
68
+ it('should resolve immediately if already set', async () => {
69
+ const token = new CancellationToken();
70
+ token.set();
71
+ await token.wait();
72
+ expect(token.isSet).toBe(true);
73
+ });
74
+ it('should throw immediately if already set with waitThrow', async () => {
75
+ const token = new CancellationToken();
76
+ token.set();
77
+ await expect(token.waitThrow()).rejects.toThrow();
78
+ });
79
+ it('should throw immediately if already set', async () => {
80
+ const token = new CancellationToken();
81
+ token.set();
82
+ expect(() => token.throwIfSet()).toThrow();
83
+ });
84
+ it('should not throw if not set', () => {
85
+ const token = new CancellationToken();
86
+ expect(() => token.throwIfSet()).not.toThrow();
87
+ });
88
+ it('should support timeouts', async () => {
89
+ const token = new CancellationToken();
90
+ const timedSignal = token.withTimeout(10);
91
+ expect(timedSignal.isSet).toBe(false);
92
+ await timedSignal.wait();
93
+ expect(timedSignal.isSet).toBe(true);
94
+ expect(token.isSet).toBe(false);
95
+ });
96
+ it('should be observable', async () => {
97
+ const token = new CancellationToken();
98
+ const promise = firstValueFrom(from(token));
99
+ token.set();
100
+ await promise;
101
+ });
102
+ it('should support subscribe', () => {
103
+ const token = new CancellationToken();
104
+ const next = vi.fn();
105
+ const complete = vi.fn();
106
+ const subscription = token.subscribe({ next, complete });
107
+ token.set();
108
+ expect(next).toHaveBeenCalled();
109
+ expect(complete).toHaveBeenCalled();
110
+ subscription.unsubscribe();
111
+ });
112
+ it('should support Symbol.observable', () => {
113
+ const token = new CancellationToken();
114
+ const result = token[Symbol.observable]();
115
+ expect(result).toBe(token);
116
+ });
117
+ it('should return self as signal', () => {
118
+ const token = new CancellationToken();
119
+ expect(token.signal).toBe(token);
120
+ });
121
+ it('should handle subscribe when already set', () => {
122
+ const token = new CancellationToken();
123
+ token.set();
124
+ const next = vi.fn();
125
+ token.subscribe({ next });
126
+ expect(next).toHaveBeenCalled();
127
+ });
128
+ it('should support unsubscribe', () => {
129
+ const token = new CancellationToken();
130
+ const next = vi.fn();
131
+ const subscription = token.subscribe({ next });
132
+ subscription.unsubscribe();
133
+ token.set();
134
+ expect(next).not.toHaveBeenCalled();
135
+ });
136
+ });
@@ -1,207 +1,83 @@
1
- import { BehaviorSubject, type Observable, type Observer, type Subscribable, type Unsubscribable } from 'rxjs';
2
- /**
3
- * Configuration for connecting a source to a CancellationToken.
4
- */
5
- export type ConnectConfig = {
6
- /**
7
- * Propagate 'set' (true) states to the child token.
8
- * @default true
9
- */
10
- set?: boolean;
11
- /**
12
- * Propagate 'unset' (false) states to the child token.
13
- * @default true
14
- */
15
- unset?: boolean;
16
- /**
17
- * Propagate the 'complete' event to the child token.
18
- * @default true
19
- */
20
- complete?: boolean;
21
- /**
22
- * Propagate errors to the child token.
23
- * @default true
24
- */
25
- error?: boolean;
1
+ import type { InteropObservable, Observer, Subscribable, Unsubscribable } from 'rxjs';
2
+ import type { OneOrMany } from '../types/types.js';
3
+ export type CancellationSource = CancellationToken | CancellationSignal | AbortController | AbortSignal;
4
+ export type CancellationSignalWaitOptions = {
26
5
  /**
27
- * Immediately apply the source's current state upon connection.
28
- * If false, waits for the next state change.
29
- * @default true
30
- */
31
- immediate?: boolean;
32
- /**
33
- * Only propagate the first applicable state change, then disconnect.
6
+ * If true, the promise returned by `wait()` will reject with the error when the token is set.
7
+ * If false, it will resolve normally when the token is set.
34
8
  * @default false
35
9
  */
36
- once?: boolean;
10
+ throw?: boolean;
37
11
  };
38
12
  /**
39
- * A read-only view of a CancellationToken's state.
40
- *
41
- * It allows consumers to subscribe to state changes, await cancellation,
42
- * or convert to other signal types (like AbortSignal) without being able
43
- * to change the token's state.
44
- *
45
- * It is both `PromiseLike<void>` (resolves when set) and `Subscribable<void>`
46
- * (emits when set).
13
+ * A CancellationSignal represents a signal that can be used to indicate cancellation of an operation.
14
+ * It is immutable and can be shared across multiple operations.
15
+ * It can be created from one or more cancellation sources, such as other signals or AbortControllers.
47
16
  */
48
- export declare class CancellationSignal implements PromiseLike<void>, Subscribable<void> {
17
+ export declare class CancellationSignal implements Disposable, Subscribable<void>, InteropObservable<void> {
49
18
  #private;
50
- protected readonly _stateSubject: BehaviorSubject<boolean>;
51
- /**
52
- * Observable which emits the current state (true for set, false for unset)
53
- * and any subsequent state changes.
54
- */
55
- readonly state$: Observable<boolean>;
56
- /**
57
- * Observable which emits `void` only when the token becomes set.
58
- */
59
- readonly set$: Observable<void>;
60
- /**
61
- * Observable which emits `void` only when the token becomes unset.
62
- */
63
- readonly unset$: Observable<void>;
64
- /**
65
- * A promise that resolves with the next state of the token.
66
- */
67
- get $state(): Promise<boolean>;
68
- /**
69
- * A promise that resolves when the token is next set.
70
- * If the token is already set, it resolves immediately.
71
- */
72
- get $set(): Promise<void>;
73
- /**
74
- * A promise that resolves when the token is next unset.
75
- * If the token is already unset, it resolves immediately.
76
- */
77
- get $unset(): Promise<void>;
78
- /**
79
- * The current state of the token.
80
- * @returns `true` if set, `false` if unset.
81
- */
82
- get state(): boolean;
83
- /**
84
- * Whether the token is currently in a 'set' state.
85
- */
19
+ get abortSignal(): AbortSignal;
86
20
  get isSet(): boolean;
87
- /**
88
- * Whether the token is currently in an 'unset' state.
89
- */
90
21
  get isUnset(): boolean;
22
+ get reason(): any;
23
+ constructor(sources: OneOrMany<CancellationSource>);
24
+ static from(sources: OneOrMany<CancellationSource>): CancellationSignal;
25
+ static timeout(milliseconds: number): CancellationSignal;
26
+ [Symbol.dispose](): void;
27
+ [Symbol.observable](): Subscribable<void>;
28
+ dispose(): void;
91
29
  /**
92
- * @internal
30
+ * Creates a new CancellationToken that is also set when this signal is set.
31
+ * If this signal is already set, the new token will also be set.
93
32
  */
94
- constructor(stateSubject: BehaviorSubject<boolean>);
33
+ fork(): CancellationToken;
95
34
  /**
96
- * Returns a standard `AbortSignal` that is aborted when this token is set.
97
- * Useful for interoperability with APIs like `fetch`.
35
+ * Creates a new CancellationSignal that is set when either this signal or any of the specified parent signals are set.
36
+ * If any of the parent signals are already set, the new signal will also be set.
37
+ * @param parents The parent token or signal(s) to inherit from.
98
38
  */
99
- get abortSignal(): AbortSignal;
39
+ inherit(parents: OneOrMany<CancellationSource>): CancellationSignal;
100
40
  /**
101
- * Returns a standard `AbortSignal` that is aborted when this token is set.
102
- * Useful for interoperability with APIs like `fetch`.
41
+ * Creates a new CancellationSignal that is set when either this signal or any of the specified parent signals are set.
42
+ * If any of the parent signals are already set, the new signal will also be set.
43
+ * If no parents are provided, it simply returns this signal.
44
+ * @param parents The parent token or signal(s) to optionally inherit from.
103
45
  */
104
- asAbortSignal(): AbortSignal;
46
+ optionallyInherit(parents?: OneOrMany<CancellationSource>): CancellationSignal;
105
47
  /**
106
- * Creates a new `CancellationToken` that is connected to this one.
107
- * State changes from this token will propagate to the new child token.
108
- * @see {@link connect}
48
+ * Creates a new CancellationSignal that is set when either this signal is set or the specified timeout elapses.
49
+ * @param milliseconds The timeout duration in milliseconds.
109
50
  */
110
- createChild(config?: ConnectConfig): CancellationToken;
51
+ withTimeout(milliseconds: number): CancellationSignal;
111
52
  /**
112
- * Propagates state changes from this token to a target `CancellationToken`.
113
- * @param target The token to receive state updates.
114
- * @param config Configuration for the connection.
53
+ * Creates a new CancellationSignal that is set when either this signal is set or the specified timeout elapses.
54
+ * If no timeout is provided, it simply returns this signal.
55
+ * @param milliseconds The timeout duration in milliseconds.
115
56
  */
116
- connect(target: CancellationToken, config?: ConnectConfig): void;
117
- /**
118
- * Allows this object to be awaited. The promise resolves when the token is set.
119
- * Implements the `PromiseLike<void>` interface.
120
- */
121
- then<TResult>(onfulfilled?: ((value: void) => TResult | PromiseLike<TResult>) | null): Promise<TResult>;
57
+ withOptionalTimeout(milliseconds?: number): CancellationSignal;
58
+ subscribe(observer: Partial<Observer<void>>): Unsubscribable;
122
59
  /**
123
- * Subscribes to notifications for when the token becomes set.
124
- * This is an alias for `this.set$.subscribe(observer)`.
125
- * Implements the `Subscribable<void>` interface.
60
+ * Waits for the token to be set. Resolves or rejects when the token is set.
61
+ * @param options Configuration for the wait behavior.
126
62
  */
127
- subscribe(observer: Partial<Observer<void>>): Unsubscribable;
63
+ wait(options?: CancellationSignalWaitOptions): Promise<void>;
64
+ waitThrow(): Promise<void>;
65
+ throwIfSet(): void;
128
66
  }
129
67
  /**
130
- * A CancellationToken represents a unit of work that can be cancelled.
131
- * It can be explicitly set/unset, completed, or errored.
132
- *
133
- * You can create child tokens, connect them to other async primitives
134
- * (Promises, AbortSignals), and pass around its read-only `signal`
135
- * to consumers that should only react to cancellation, not trigger it.
68
+ * A CancellationToken is a mutable cancellation source that can be set to signal cancellation.
69
+ * It extends CancellationSignal, so it can be used wherever a CancellationSignal is expected.
70
+ * It provides a `set()` method to signal cancellation, which will trigger any subscribers or waiters on the token or any tokens that inherit from it.
136
71
  */
137
72
  export declare class CancellationToken extends CancellationSignal {
138
73
  #private;
139
- /**
140
- * A read-only view of this token.
141
- * Useful for passing to functions that should only be able to check for
142
- * cancellation but not trigger it.
143
- */
144
74
  get signal(): CancellationSignal;
145
75
  /**
146
- * @param initialState The initial state of the token. `false` (unset) by default.
147
- */
148
- constructor(initialState?: boolean);
149
- /**
150
- * Creates a token that is set when the source signals.
151
- * @param source The event source to listen to.
152
- * @example
153
- * // From a fetch promise
154
- * const token = CancellationToken.from(fetch('/api/data'));
155
- *
156
- * // From a browser AbortSignal
157
- * const controller = new AbortController();
158
- * const token = CancellationToken.from(controller.signal);
159
- */
160
- static from(source: AbortSignal | PromiseLike<any> | Observable<void>, config?: ConnectConfig): CancellationToken;
161
- /**
162
- * Creates a token and connects it to an `Observable<boolean>` source.
163
- * The token's state will mirror the boolean values emitted by the observable.
164
- * @param source The observable source.
165
- * @param config Configuration for the connection.
166
- */
167
- static from(source: Observable<boolean>, config?: ConnectConfig): CancellationToken;
168
- /**
169
- * Connects an observable state source to a target token.
170
- * @param source$ The observable providing boolean state.
171
- * @param target The CancellationToken to update.
172
- * @param config The connection configuration.
173
- */
174
- static connect(source$: Observable<boolean>, target: CancellationToken, config?: ConnectConfig): void;
175
- /**
176
- * Makes this token a child of a parent token.
177
- * State changes from the parent will propagate to this token.
178
- * @param parent The token to inherit state from.
179
- * @param config Configuration for the connection.
180
- */
181
- inherit(parent: CancellationToken | CancellationSignal, config?: ConnectConfig): this;
182
- /**
183
- * Sets the token's state to `true`.
184
- * This signals cancellation or completion of the associated operation.
185
- */
186
- set(): void;
187
- /**
188
- * Sets the token's state to `false`.
189
- * This can be used to reset the token.
190
- */
191
- unset(): void;
192
- /**
193
- * Explicitly sets the state of the token.
194
- * @param state `true` for set, `false` for unset.
195
- */
196
- setState(state: boolean): void;
197
- /**
198
- * Puts the token into an errored state.
199
- * All current and future subscribers will receive this error.
200
- */
201
- error(error: Error): void;
202
- /**
203
- * Completes the token's lifecycle.
204
- * No further state changes can occur. Active awaits may throw an error.
76
+ * Creates a new CancellationToken that is set when the parent signals.
77
+ * If the parent is already set, the new token will also be set.
78
+ * @param parents The parent token or signal(s) to inherit from.
205
79
  */
206
- complete(): void;
80
+ constructor(parents?: OneOrMany<CancellationSource>);
81
+ set(reason?: any): void;
207
82
  }
83
+ export declare function sourcesToAbortSignal(...sources: OneOrMany<CancellationSource>[]): AbortSignal;