aveazul 1.0.2 → 2.0.0

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/README.md CHANGED
@@ -16,7 +16,7 @@ Further, if you like Bluebird's API but want to use native Promises, AveAzul giv
16
16
 
17
17
  ## Requirements
18
18
 
19
- - node.js version >= 12
19
+ - node.js version >= 22.12
20
20
 
21
21
  ## Installation
22
22
 
package/cjs-entry.cjs ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ // ESM-only build; this shim keeps `require("aveazul")` returning the AveAzul class,
4
+ // because require(esm) yields the module namespace rather than the default export.
5
+ const m = require("./dist/index.js");
6
+
7
+ module.exports = Object.assign(m.default, m);
package/dist/any.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { AveAzulClass } from "./aveazul.js";
2
+ export declare function addStaticAny(AveAzul: AveAzulClass, force?: boolean): void;
package/dist/any.js ADDED
@@ -0,0 +1,45 @@
1
+ import { toArray, isPromise } from "./util.js";
2
+ import { AggregateError } from "@jchip/error";
3
+ export function addStaticAny(AveAzul, force = false) {
4
+ if (force || !AveAzul.any) {
5
+ AveAzul.any = function (args) {
6
+ let argsArray;
7
+ try {
8
+ argsArray = toArray(args);
9
+ }
10
+ catch (error) {
11
+ return AveAzul.reject(error);
12
+ }
13
+ if (argsArray.length === 0) {
14
+ return AveAzul.reject(new RangeError("Input array must contain at least 1 items but contains only 0 items"));
15
+ }
16
+ return new AveAzul((resolve, reject) => {
17
+ const len = argsArray.length;
18
+ let settled = false;
19
+ const errors = [];
20
+ const doFinish = (value) => {
21
+ if (settled)
22
+ return;
23
+ settled = true;
24
+ resolve(value);
25
+ };
26
+ const addError = (err) => {
27
+ errors.push(err);
28
+ if (!settled && errors.length >= len) {
29
+ settled = true;
30
+ reject(new AggregateError(errors));
31
+ }
32
+ };
33
+ for (let i = 0; i < len; i++) {
34
+ const arg = argsArray[i];
35
+ if (isPromise(arg)) {
36
+ arg.then(doFinish, addError);
37
+ }
38
+ else {
39
+ doFinish(arg);
40
+ }
41
+ }
42
+ });
43
+ };
44
+ }
45
+ }
@@ -0,0 +1,344 @@
1
+ import { PromisifyOptions } from "./promisify.js";
2
+ import { PromisifyAllOptions } from "./promisify-all.js";
3
+ import { Disposer } from "./disposer.js";
4
+ import { triggerUncaughtException } from "./util.js";
5
+ import { OperationalError, isOperationalError, isProgrammerError } from "./operational-error.js";
6
+ export interface MapOptions {
7
+ concurrency?: number;
8
+ }
9
+ export interface AsCallbackOptions {
10
+ spread?: boolean;
11
+ }
12
+ export interface FromCallbackOptions {
13
+ multiArgs?: boolean;
14
+ }
15
+ export interface Deferred<T> {
16
+ promise: AveAzul<T>;
17
+ resolve: (value: T | PromiseLike<T>) => void;
18
+ reject: (reason?: unknown) => void;
19
+ }
20
+ export interface AveAzulClass {
21
+ new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void) => void): AveAzul<T>;
22
+ resolve<T>(value: T | PromiseLike<T>): AveAzul<T>;
23
+ resolve(): AveAzul<void>;
24
+ reject<T = never>(reason?: unknown): AveAzul<T>;
25
+ all<T>(values: Iterable<T | PromiseLike<T>>): AveAzul<Awaited<T>[]>;
26
+ delay<T>(ms: number, value?: T): AveAzul<T>;
27
+ map<T, U>(value: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => U | PromiseLike<U>, options?: MapOptions): AveAzul<U[]>;
28
+ mapSeries<T, U>(value: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => U | PromiseLike<U>): AveAzul<U[]>;
29
+ try<T>(fn: () => T | PromiseLike<T>): AveAzul<T>;
30
+ props<T extends object>(obj: T): AveAzul<{
31
+ [K in keyof T]: Awaited<T[K]>;
32
+ }>;
33
+ defer<T>(): Deferred<T>;
34
+ each<T>(items: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => unknown): AveAzul<T[]>;
35
+ reduce<T, U>(array: Iterable<T | PromiseLike<T>>, fn: (value: U, item: T, index: number, length: number) => U | PromiseLike<U>, initialValue?: U): AveAzul<U>;
36
+ promisify<T = unknown>(fn: (...args: any[]) => void, options?: PromisifyOptions): (...args: any[]) => AveAzul<T>;
37
+ promisifyAll<T extends object>(target: T, options?: PromisifyAllOptions): T;
38
+ method<T, Args extends any[]>(fn: (...args: Args) => T | PromiseLike<T>): (...args: Args) => AveAzul<T>;
39
+ using<R>(...args: any[]): AveAzul<R>;
40
+ join(...args: any[]): AveAzul<any>;
41
+ fromCallback<T>(fn: (callback: (err: Error | null, result?: T) => void) => void, options?: FromCallbackOptions): AveAzul<T>;
42
+ fromNode<T>(fn: (callback: (err: Error | null, result?: T) => void) => void, options?: FromCallbackOptions): AveAzul<T>;
43
+ some<T>(promises: Iterable<T | PromiseLike<T>>, count: number): AveAzul<T[]>;
44
+ any<T>(args: Iterable<T | PromiseLike<T>>): AveAzul<T>;
45
+ ___throwUncaughtError(error: unknown): void;
46
+ OperationalError: typeof OperationalError;
47
+ __notImplementedInstance?: string[];
48
+ __notImplementedStatic?: string[];
49
+ }
50
+ export type AveAzulInstance<T> = AveAzul<T>;
51
+ /**
52
+ * @fileoverview
53
+ * AveAzul ("Blue Bird" in Spanish) - Extended Promise class that provides Bluebird like utility methods
54
+ * This implementation is inspired by and provides similar APIs to the Bluebird Promise library,
55
+ * but built on top of native Promises. The name is a Spanish play on words referencing Bluebird.
56
+ * @extends Promise
57
+ */
58
+ declare class AveAzul<T> extends Promise<T> {
59
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void) => void);
60
+ /**
61
+ * Note: Per ECMAScript specification, when extending Promise, both .then() and static methods
62
+ * (resolve, reject, all, etc) must return instances of the derived class (AveAzul), so there's
63
+ * no need to explicitly wrap returns in new AveAzul(). This behavior is standard across all
64
+ * spec-compliant JS engines (V8, SpiderMonkey, JavaScriptCore, etc).
65
+ */
66
+ /**
67
+ * Bluebird-style tap() method that lets you perform side effects in a chain
68
+ * Similar to Bluebird's Promise.prototype.tap()
69
+ * @param fn - Function to execute with the resolved value
70
+ * @returns Promise that resolves with the original value
71
+ */
72
+ tap(fn: (value: T) => unknown): AveAzul<T>;
73
+ /**
74
+ * Bluebird-style filter() method for array operations
75
+ * Similar to Bluebird's Promise.prototype.filter()
76
+ * @param fn - Filter function to apply to each element
77
+ * @returns Promise that resolves with the filtered array
78
+ */
79
+ filter(fn: (item: any, index: number, length: number) => any): AveAzul<any[]>;
80
+ /**
81
+ * Bluebird-style map() method for array operations
82
+ * Similar to Bluebird's Promise.prototype.map()
83
+ * @param fn - Map function to apply to each element
84
+ * @returns Promise that resolves with the mapped array
85
+ */
86
+ map<U>(fn: (item: any, index: number, length: number) => U | PromiseLike<U>, options?: MapOptions): AveAzul<U[]>;
87
+ /**
88
+ * Bluebird-style mapSeries() method for array operations
89
+ * Similar to Bluebird's Promise.prototype.mapSeries()
90
+ * @param fn - Map function to apply to each element
91
+ * @returns Promise that resolves with the mapped array
92
+ */
93
+ mapSeries<U>(fn: (item: any, index: number, length: number) => U | PromiseLike<U>): AveAzul<U[]>;
94
+ /**
95
+ * Bluebird-style return() method to inject a value into the chain
96
+ * Similar to Bluebird's Promise.prototype.return()
97
+ * @param value - Value to return
98
+ * @returns Promise that resolves with the new value
99
+ */
100
+ return<U>(value: U): AveAzul<U>;
101
+ /**
102
+ * Bluebird-style any() method for waiting for any promises to resolve
103
+ * @returns Promise that resolves with the first resolved promise
104
+ */
105
+ any(): AveAzul<any>;
106
+ /**
107
+ * Bluebird-style each() method for array iteration
108
+ * Similar to Bluebird's Promise.prototype.each()
109
+ * @param fn - Function to execute for each element
110
+ * @returns Promise that resolves when iteration is complete
111
+ */
112
+ each(fn: (item: any, index: number, length: number) => unknown): AveAzul<any[]>;
113
+ /**
114
+ * Bluebird-style delay() method
115
+ * @param ms - Milliseconds to delay
116
+ * @returns Promise that resolves after the delay
117
+ */
118
+ delay(ms: number): AveAzul<void>;
119
+ /**
120
+ * Bluebird-style timeout() method
121
+ * @param ms - Milliseconds before timeout
122
+ * @param message - Optional error message
123
+ * @returns Promise that rejects if timeout occurs
124
+ */
125
+ timeout(ms: number, message?: string): AveAzul<T>;
126
+ /**
127
+ * Bluebird-style props() for object properties
128
+ * @returns Promise that resolves with an object of resolved values
129
+ */
130
+ props(): AveAzul<any>;
131
+ /**
132
+ * Bluebird-style tapCatch() for side effects on rejection
133
+ * @param fn - Function to execute on rejection
134
+ * @returns Promise that maintains the rejection
135
+ */
136
+ tapCatch(fn: (err: Error) => unknown): AveAzul<T>;
137
+ /**
138
+ * Bluebird-style reduce() method for array reduction
139
+ * Similar to Bluebird's Promise.prototype.reduce()
140
+ * @param fn - Reducer function to apply to each element
141
+ * @param initialValue - Optional initial value
142
+ * @returns Promise that resolves with the final reduced value
143
+ */
144
+ reduce<U>(fn: (value: U, item: any, index: number, length: number) => U | PromiseLike<U>, initialValue?: U): AveAzul<U>;
145
+ /**
146
+ * Bluebird-style throw() that returns a rejected promise with the given reason
147
+ * @param reason - Value to reject the promise with
148
+ * @returns Promise that rejects with the given reason
149
+ */
150
+ throw(reason: unknown): AveAzul<never>;
151
+ /**
152
+ * Bluebird-style catchThrow() that catches an error and throws a new one
153
+ * @param reason - Value to reject the promise with
154
+ * @returns Promise that rejects with the new reason
155
+ */
156
+ catchThrow(reason: unknown): AveAzul<T>;
157
+ /**
158
+ * Bluebird-style catchReturn() that catches an error and returns a value instead
159
+ * @param value - Value to return
160
+ * @returns Promise that resolves with the given value
161
+ */
162
+ catchReturn<U>(value: U): AveAzul<T | U>;
163
+ /**
164
+ * Bluebird-style get() for retrieving a property value
165
+ * @param key - Key to retrieve
166
+ * @returns Promise that resolves with the property value
167
+ */
168
+ get<K extends keyof T>(key: K): AveAzul<T[K]>;
169
+ /**
170
+ * Bluebird-style disposer() for resource cleanup
171
+ * @param fn - Cleanup function
172
+ * @returns Disposer object
173
+ */
174
+ disposer(fn: (resource: T) => void | Promise<void>): Disposer<T>;
175
+ /**
176
+ * Bluebird-style spread() method for handling array arguments
177
+ * Similar to Bluebird's Promise.prototype.spread()
178
+ * @param fn - Function to apply to the array arguments
179
+ * @returns Promise that resolves with the function's return value
180
+ */
181
+ spread<U>(fn: (...args: any[]) => U | PromiseLike<U>): AveAzul<U>;
182
+ some(count: number): AveAzul<any[]>;
183
+ /**
184
+ * Bluebird-style all() method for array operations
185
+ * Similar to Promise.all() but operates on the resolved value of this promise
186
+ * @returns Promise that resolves when all items in the array resolve
187
+ */
188
+ all(): AveAzul<any[]>;
189
+ /**
190
+ * Bluebird-style asCallback() method
191
+ * Attaches a callback to the promise and returns the promise.
192
+ * The callback is invoked when the promise is resolved or rejected.
193
+ *
194
+ * @param cb - Node.js-style callback function (err, value)
195
+ * @param options - Additional options
196
+ * @returns The same promise instance
197
+ */
198
+ asCallback(cb: ((err: Error | null, value?: T) => void) | undefined | null, options?: AsCallbackOptions): AveAzul<T>;
199
+ nodeify(cb: ((err: Error | null, value?: T) => void) | undefined | null, options?: AsCallbackOptions): AveAzul<T>;
200
+ /**
201
+ * Bluebird-style call() method for calling a method on the resolved value
202
+ * @param methodName - Name of the method to call
203
+ * @param args - Arguments to pass to the method
204
+ * @returns Promise that resolves with the method's return value
205
+ */
206
+ call(methodName: string, ...args: any[]): AveAzul<any>;
207
+ /**
208
+ * Catches only operational errors and passes them to the handler.
209
+ * Programmer errors (non-operational) are rethrown.
210
+ * @param handler - Function to handle operational errors
211
+ * @returns Promise with the error handled or rethrown
212
+ */
213
+ error(handler: (err: Error) => unknown): AveAzul<T>;
214
+ /**
215
+ * Static helper methods
216
+ */
217
+ /**
218
+ * Bluebird-style delay() that resolves after specified milliseconds
219
+ * @param ms - Milliseconds to delay
220
+ * @param value - Optional value to resolve with
221
+ * @returns Promise that resolves after the delay
222
+ */
223
+ static delay<U>(ms: number, value?: U): AveAzul<U>;
224
+ /**
225
+ * Bluebird-style map() for array operations
226
+ * @param value - Array to map over
227
+ * @param fn - Map function to apply to each element
228
+ * @returns Promise that resolves with the mapped array
229
+ */
230
+ static map<T, U>(value: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => U | PromiseLike<U>, options?: MapOptions): AveAzul<U[]>;
231
+ /**
232
+ * Bluebird-style mapSeries() for array operations
233
+ * @param value - Array to map over
234
+ * @param fn - Map function to apply to each element
235
+ * @returns Promise that resolves with the mapped array
236
+ */
237
+ static mapSeries<T, U>(value: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => U | PromiseLike<U>): AveAzul<U[]>;
238
+ /**
239
+ * Bluebird-style try() for wrapping sync/async functions
240
+ * @param fn - Function to execute
241
+ * @returns Promise that resolves with the function's return value
242
+ */
243
+ static try<T>(fn: () => T | PromiseLike<T>): AveAzul<T>;
244
+ /**
245
+ * Bluebird-style props() for object properties
246
+ * @param obj - Object with promise values
247
+ * @returns Promise that resolves with an object of resolved values
248
+ */
249
+ static props<T extends object>(obj: T): AveAzul<{
250
+ [K in keyof T]: Awaited<T[K]>;
251
+ }>;
252
+ /**
253
+ * Bluebird-style defer() for creating a deferred promise
254
+ * @returns Deferred object with promise, resolve, and reject methods
255
+ */
256
+ static defer<T>(): Deferred<T>;
257
+ /**
258
+ * Bluebird-style each() for array iteration
259
+ * @param items - Array to iterate over
260
+ * @param fn - Iterator function to call for each item
261
+ * @returns Promise that resolves when iteration is complete
262
+ */
263
+ static each<T>(items: Iterable<T | PromiseLike<T>>, fn: (item: T, index: number, length: number) => unknown): AveAzul<T[]>;
264
+ /**
265
+ * Bluebird-style reduce() for array reduction
266
+ * @param array - Array to reduce
267
+ * @param fn - Reducer function (value, item, index, length)
268
+ * @param initialValue - Optional initial value
269
+ * @returns Promise that resolves with the final reduced value
270
+ */
271
+ static reduce<T, U>(array: Iterable<T | PromiseLike<T>>, fn: (value: U, item: T, index: number, length: number) => U | PromiseLike<U>, initialValue?: U): AveAzul<U>;
272
+ /**
273
+ * Bluebird-style promisify() for converting callback-based functions to promises
274
+ * @param fn - Function to promisify
275
+ * @param options - Options object
276
+ * @returns Promisified function
277
+ */
278
+ static promisify<T = unknown>(fn: (...args: any[]) => void, options?: PromisifyOptions): (...args: any[]) => AveAzul<T>;
279
+ /**
280
+ * Bluebird-style promisifyAll() for converting callback-based functions to promises
281
+ * @param target - Object to promisify
282
+ * @param options - Options object
283
+ * @returns Object with promisified methods
284
+ */
285
+ static promisifyAll<T extends object>(target: T, options?: PromisifyAllOptions): T;
286
+ /**
287
+ * Bluebird-style method() for creating a method that returns a promise
288
+ * @param fn - Function to create a method for
289
+ * @returns Method function that returns a promise
290
+ */
291
+ static method<T, Args extends any[]>(fn: (...args: Args) => T | PromiseLike<T>): (...args: Args) => AveAzul<T>;
292
+ /**
293
+ * Bluebird-style using() for resource management. There is only a static version of this method.
294
+ * After the handler finish and returns, regardless of whether it resolves or rejects, the resources will be disposed.
295
+ *
296
+ * @param resources - Resource disposers, either an array of disposers or a variadic argument list
297
+ * @param args - Handler function that will receive the resources as arguments
298
+ * @returns Promise that resolves with handler result
299
+ */
300
+ static using<R>(resources: any, ...args: any[]): AveAzul<R>;
301
+ /**
302
+ * Bluebird-style join() for joining promises
303
+ *
304
+ * @param args - Promises to join
305
+ * @returns Promise that resolves with the handler's return value
306
+ */
307
+ static join(...args: any[]): AveAzul<any>;
308
+ /**
309
+ * Bluebird-style fromCallback() for converting callback-based functions to promises
310
+ * @param fn - Function to convert
311
+ * @param options - Options object
312
+ * @returns Promise that resolves with the function's return value
313
+ */
314
+ static fromCallback<T>(fn: (callback: (err: Error | null, ...args: T[]) => void) => void, options?: FromCallbackOptions): AveAzul<T>;
315
+ static fromNode: typeof AveAzul.fromCallback;
316
+ /**
317
+ * When fatal error and AveAzul needs to crash the process,
318
+ * this method is used to throw the error.
319
+ *
320
+ * @param error - The error to throw.
321
+ */
322
+ static ___throwUncaughtError: typeof triggerUncaughtException;
323
+ /**
324
+ * Bluebird-style some() for waiting for some promises to resolve
325
+ * @param promises - Array or iterable of promises
326
+ * @param count - Number of promises that need to resolve
327
+ * @returns Promise that resolves when count promises have resolved
328
+ */
329
+ static some<T>(promises: Iterable<T | PromiseLike<T>>, count: number): AveAzul<T[]>;
330
+ /**
331
+ * Bluebird-style any() for waiting for any promise to resolve
332
+ * @param args - Array or iterable of promises
333
+ * @returns Promise that resolves with the first resolved value
334
+ */
335
+ static any<T>(args: Iterable<T | PromiseLike<T>>): AveAzul<T>;
336
+ static OperationalError: typeof OperationalError;
337
+ static isOperationalError: typeof isOperationalError;
338
+ static isProgrammerError: typeof isProgrammerError;
339
+ static Disposer: typeof Disposer;
340
+ static __notImplementedInstance?: string[];
341
+ static __notImplementedStatic?: string[];
342
+ }
343
+ export { AveAzul };
344
+ export default AveAzul;