@dwn-protocol/id-sdk 0.2.5 → 0.2.6

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 (99) hide show
  1. package/package.json +2 -3
  2. package/src/agent/app-data-store.ts +0 -365
  3. package/src/agent/did-manager.ts +0 -393
  4. package/src/agent/dwn-manager.ts +0 -548
  5. package/src/agent/identity-manager.ts +0 -165
  6. package/src/agent/index.ts +0 -19
  7. package/src/agent/json-rpc.ts +0 -107
  8. package/src/agent/key-manager.ts +0 -302
  9. package/src/agent/kms-local.ts +0 -412
  10. package/src/agent/outbox.ts +0 -128
  11. package/src/agent/rpc-client.ts +0 -223
  12. package/src/agent/store-managed-did.ts +0 -295
  13. package/src/agent/store-managed-identity.ts +0 -243
  14. package/src/agent/store-managed-key.ts +0 -754
  15. package/src/agent/sync-manager.ts +0 -631
  16. package/src/agent/test-managed-agent.ts +0 -299
  17. package/src/agent/types/agent.ts +0 -145
  18. package/src/agent/types/managed-key.ts +0 -442
  19. package/src/agent/utils.ts +0 -190
  20. package/src/common/convert.ts +0 -424
  21. package/src/common/index.ts +0 -9
  22. package/src/common/multicodec.ts +0 -176
  23. package/src/common/object.ts +0 -43
  24. package/src/common/stores.ts +0 -125
  25. package/src/common/stream-node.ts +0 -381
  26. package/src/common/stream.ts +0 -406
  27. package/src/common/type-utils.ts +0 -117
  28. package/src/common/types.ts +0 -48
  29. package/src/credentials/credential-bbs.ts +0 -419
  30. package/src/credentials/credential.ts +0 -324
  31. package/src/credentials/index.ts +0 -5
  32. package/src/credentials/presentation.ts +0 -182
  33. package/src/credentials/status-list.ts +0 -365
  34. package/src/credentials/utils.ts +0 -58
  35. package/src/credentials/validators.ts +0 -52
  36. package/src/crypto/algorithms-api/aes/base.ts +0 -49
  37. package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
  38. package/src/crypto/algorithms-api/aes/index.ts +0 -2
  39. package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
  40. package/src/crypto/algorithms-api/crypto-key.ts +0 -56
  41. package/src/crypto/algorithms-api/ec/base.ts +0 -39
  42. package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
  43. package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
  44. package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
  45. package/src/crypto/algorithms-api/ec/index.ts +0 -4
  46. package/src/crypto/algorithms-api/errors.ts +0 -29
  47. package/src/crypto/algorithms-api/index.ts +0 -6
  48. package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
  49. package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
  50. package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
  51. package/src/crypto/crypto-algorithms/bbs.ts +0 -110
  52. package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
  53. package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
  54. package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
  55. package/src/crypto/crypto-algorithms/index.ts +0 -6
  56. package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
  57. package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
  58. package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
  59. package/src/crypto/crypto-primitives/bbs.ts +0 -183
  60. package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
  61. package/src/crypto/crypto-primitives/ed25519.ts +0 -201
  62. package/src/crypto/crypto-primitives/index.ts +0 -10
  63. package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
  64. package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
  65. package/src/crypto/crypto-primitives/x25519.ts +0 -101
  66. package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
  67. package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
  68. package/src/crypto/index.ts +0 -8
  69. package/src/crypto/jose.ts +0 -948
  70. package/src/crypto/types/crypto-key.ts +0 -4
  71. package/src/crypto/types/iddwn-crypto.ts +0 -119
  72. package/src/crypto/utils.ts +0 -200
  73. package/src/did-api.ts +0 -72
  74. package/src/dids/dht.ts +0 -412
  75. package/src/dids/did-dht.ts +0 -436
  76. package/src/dids/did-ion.ts +0 -613
  77. package/src/dids/did-key.ts +0 -791
  78. package/src/dids/did-resolver.ts +0 -107
  79. package/src/dids/index.ts +0 -9
  80. package/src/dids/resolver-cache-level.ts +0 -82
  81. package/src/dids/resolver-cache-noop.ts +0 -25
  82. package/src/dids/types.ts +0 -278
  83. package/src/dids/utils.ts +0 -129
  84. package/src/dwn-api.ts +0 -584
  85. package/src/iddwn.ts +0 -241
  86. package/src/identity-agent/index.ts +0 -270
  87. package/src/index.ts +0 -26
  88. package/src/interfaces/metadata.ts +0 -163
  89. package/src/interfaces/queue.ts +0 -108
  90. package/src/interfaces/services.ts +0 -122
  91. package/src/interfaces/transactions.ts +0 -220
  92. package/src/protocol.ts +0 -68
  93. package/src/proxy-agent/index.ts +0 -255
  94. package/src/record.ts +0 -521
  95. package/src/service-options.ts +0 -62
  96. package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
  97. package/src/user-agent/index.ts +0 -295
  98. package/src/utils.ts +0 -29
  99. package/src/vc-api.ts +0 -505
@@ -1,406 +0,0 @@
1
- import { Convert } from './convert.js';
2
-
3
- export class Stream {
4
- /**
5
- * Transforms a `ReadableStream` into an `AsyncIterable`. This allows for the asynchronous
6
- * iteration over the stream's data chunks.
7
- *
8
- * This method creates an async iterator from a `ReadableStream`, enabling the use of
9
- * `for await...of` loops to process stream data. It reads from the stream until it's closed or
10
- * errored, yielding each chunk as it becomes available.
11
- *
12
- * @example
13
- * ```ts
14
- * const readableStream = new ReadableStream({ ... });
15
- * for await (const chunk of Stream.asAsyncIterator(readableStream)) {
16
- * // process each chunk
17
- * }
18
- * ```
19
- *
20
- * @remarks
21
- * - The method ensures proper cleanup by releasing the reader lock when iteration is completed or
22
- * if an error occurs.
23
- *
24
- * @param readableStream - The Web `ReadableStream` to be transformed into an `AsyncIterable`.
25
- * @returns An `AsyncIterable` that yields data chunks from the `ReadableStream`.
26
- */
27
- public static async * asAsyncIterator<T>(readableStream: ReadableStream<T>): AsyncIterable<T> {
28
- const reader = readableStream.getReader();
29
- try {
30
- while (true) {
31
- const { done, value } = await reader.read();
32
- if (done) break;
33
- yield value;
34
- }
35
- } finally {
36
- reader.releaseLock();
37
- }
38
- }
39
-
40
- /**
41
- * Consumes a `ReadableStream` and returns its contents as an `ArrayBuffer`.
42
- *
43
- * This method reads all data from a `ReadableStream`, collects it, and converts it into an
44
- * `ArrayBuffer`.
45
- *
46
- * @example
47
- * ```ts
48
- * const readableStream = new ReadableStream({ ... });
49
- * const arrayBuffer = await Stream.consumeToArrayBuffer({ readableStream });
50
- * ```
51
- *
52
- * @param readableStream - The Web `ReadableStream` whose data will be consumed.
53
- * @returns A Promise that resolves to an `ArrayBuffer` containing all the data from the stream.
54
- */
55
- public static async consumeToArrayBuffer({ readableStream }: { readableStream: ReadableStream}): Promise<ArrayBuffer> {
56
- const iterableStream = Stream.asAsyncIterator(readableStream);
57
- const arrayBuffer = await Convert.asyncIterable(iterableStream).toArrayBufferAsync();
58
-
59
- return arrayBuffer;
60
- }
61
-
62
- /**
63
- * Consumes a `ReadableStream` and returns its contents as a `Blob`.
64
- *
65
- * This method reads all data from a `ReadableStream`, collects it, and converts it into a `Blob`.
66
- *
67
- * @example
68
- * ```ts
69
- * const readableStream = new ReadableStream({ ... });
70
- * const blob = await Stream.consumeToBlob({ readableStream });
71
- * ```
72
- *
73
- * @param readableStream - The Web `ReadableStream` whose data will be consumed.
74
- * @returns A Promise that resolves to a `Blob` containing all the data from the stream.
75
- */
76
- public static async consumeToBlob({ readableStream }: { readableStream: ReadableStream}): Promise<Blob> {
77
- const iterableStream = Stream.asAsyncIterator(readableStream);
78
- const blob = await Convert.asyncIterable(iterableStream).toBlobAsync();
79
-
80
- return blob;
81
- }
82
-
83
- /**
84
- * Consumes a `ReadableStream` and returns its contents as a `Uint8Array`.
85
- *
86
- * This method reads all data from a `ReadableStream`, collects it, and converts it into a
87
- * `Uint8Array`.
88
- *
89
- * @example
90
- * ```ts
91
- * const readableStream = new ReadableStream({ ... });
92
- * const bytes = await Stream.consumeToBytes({ readableStream });
93
- * ```
94
- *
95
- * @param readableStream - The Web `ReadableStream` whose data will be consumed.
96
- * @returns A Promise that resolves to a `Uint8Array` containing all the data from the stream.
97
- */
98
- public static async consumeToBytes({ readableStream }: { readableStream: ReadableStream }): Promise<Uint8Array> {
99
- const iterableStream = Stream.asAsyncIterator(readableStream);
100
- const bytes = await Convert.asyncIterable(iterableStream).toUint8ArrayAsync();
101
-
102
- return bytes;
103
- }
104
-
105
- /**
106
- * Consumes a `ReadableStream` and parses its contents as JSON.
107
- *
108
- * This method reads all the data from the stream, converts it to a text string, and then parses
109
- * it as JSON, returning the resulting object.
110
- *
111
- * @example
112
- * ```ts
113
- * const readableStream = new ReadableStream({ ... });
114
- * const jsonData = await Stream.consumeToJson({ readableStream });
115
- * ```
116
- *
117
- * @param readableStream - The Web `ReadableStream` whose JSON content will be consumed.
118
- * @returns A Promise that resolves to the parsed JSON object from the stream's data.
119
- */
120
- public static async consumeToJson({ readableStream }: { readableStream: ReadableStream}): Promise<any> {
121
- const iterableStream = Stream.asAsyncIterator(readableStream);
122
- const object = await Convert.asyncIterable(iterableStream).toObjectAsync();
123
-
124
- return object;
125
- }
126
-
127
- /**
128
- * Consumes a `ReadableStream` and returns its contents as a text string.
129
- *
130
- * This method reads all the data from the stream, converting it into a single string.
131
- *
132
- * @example
133
- * ```ts
134
- * const readableStream = new ReadableStream({ ... });
135
- * const text = await Stream.consumeToText({ readableStream });
136
- * ```
137
- *
138
- * @param readableStream - The Web `ReadableStream` whose text content will be consumed.
139
- * @returns A Promise that resolves to a string containing all the data from the stream.
140
- */
141
- public static async consumeToText({ readableStream }: { readableStream: ReadableStream}): Promise<string> {
142
- const iterableStream = Stream.asAsyncIterator(readableStream);
143
- const text = await Convert.asyncIterable(iterableStream).toStringAsync();
144
-
145
- return text;
146
- }
147
-
148
- /**
149
- * Generates a `ReadableStream` of `Uint8Array` chunks with customizable length and fill value.
150
- *
151
- * This method creates a `ReadableStream` that emits `Uint8Array` chunks. You can specify the
152
- * total length of the stream, the length of individual chunks, and a fill value or range for the
153
- * chunks. It's useful for testing or when specific binary data streams are required.
154
- *
155
- * @example
156
- * ```ts
157
- * // Create a stream of 1000 bytes with 100-byte chunks filled with 0xAA.
158
- * const byteStream = Stream.generateByteStream({
159
- * streamLength: 1000,
160
- * chunkLength: 100,
161
- * fillValue: 0xAA
162
- * });
163
- *
164
- * // Create an unending stream of 100KB chunks filled with values that range from 1 to 99.
165
- * const byteStream = Stream.generateByteStream({
166
- * chunkLength: 100 * 1024,
167
- * fillValue: [1, 99]
168
- * });
169
- * ```
170
- *
171
- * @param streamLength - The total length of the stream in bytes. If omitted, the stream is infinite.
172
- * @param chunkLength - The length of each chunk. If omitted, each chunk is the size of `streamLength`.
173
- * @param fillValue - A value or range to fill the chunks with. Can be a single number or a tuple [min, max].
174
- * @returns A `ReadableStream` that emits `Uint8Array` chunks.
175
- */
176
- public static generateByteStream({ streamLength, chunkLength, fillValue }: {
177
- streamLength?: number,
178
- chunkLength?: number,
179
- fillValue?: number | [number, number]
180
- }): ReadableStream<Uint8Array> {
181
- let bytesRemaining = streamLength ?? Infinity;
182
- let controller: ReadableStreamDefaultController<Uint8Array>;
183
-
184
- function enqueueChunk() {
185
- const currentChunkLength = Math.min(bytesRemaining, chunkLength ?? Infinity);
186
- bytesRemaining -= currentChunkLength;
187
-
188
- let chunk: Uint8Array;
189
-
190
- if (typeof fillValue === 'number') {
191
- chunk = new Uint8Array(currentChunkLength).fill(fillValue);
192
-
193
- } else if (Array.isArray(fillValue)) {
194
- chunk = new Uint8Array(currentChunkLength);
195
- const [min, max] = fillValue;
196
- const range = max - min + 1;
197
- for (let i = 0; i < currentChunkLength; i++) {
198
- chunk[i] = Math.floor(Math.random() * range) + min;
199
- }
200
-
201
- } else {
202
- chunk = new Uint8Array(currentChunkLength);
203
- }
204
-
205
- controller.enqueue(chunk);
206
-
207
- // If there are no more bytes to send, close the stream
208
- if (bytesRemaining <= 0) {
209
- controller.close();
210
- }
211
- }
212
-
213
- return new ReadableStream<Uint8Array>({
214
- start(c) {
215
- controller = c;
216
- enqueueChunk();
217
- },
218
- pull() {
219
- enqueueChunk();
220
- },
221
- });
222
- }
223
-
224
- /**
225
- * Checks if the provided Web `ReadableStream` is in a readable state.
226
- *
227
- * After verifying that the stream is a Web {@link https://streams.spec.whatwg.org/#rs-model | ReadableStream},
228
- * this method checks the {@link https://streams.spec.whatwg.org/#readablestream-locked | locked}
229
- * property of the ReadableStream. The `locked` property is `true` if a reader is currently
230
- * active, meaning the stream is either being read or has already been read (and hence is not in a
231
- * readable state). If `locked` is `false`, it means the stream is still in a state where it can
232
- * be read.
233
- *
234
- * In the case where a `ReadableStream` has been unlocked but is no longer readable (for example,
235
- * if it has been fully read or cancelled), additional checks are needed beyond just examining the
236
- * locked property. The ReadableStream API does not provide a direct way to check if the stream
237
- * has data left or if it's in a readable state once it's been unlocked.
238
- *
239
- * Per {@link https://streams.spec.whatwg.org/#other-specs-rs-introspect | WHATWG Streams, Section 9.1.3. Introspection}:
240
- *
241
- * > ...note that apart from checking whether or not the stream is locked, this direct
242
- * > introspection is not possible via the public JavaScript API, and so specifications should
243
- * > instead use the algorithms in §9.1.2 Reading. (For example, instead of testing if the stream
244
- * > is readable, attempt to get a reader and handle any exception.)
245
- *
246
- * This implementation employs the technique suggested by the WHATWG Streams standard by
247
- * attempting to acquire a reader and checking the state of the reader. If acquiring a reader
248
- * succeeds, it immediately releases the lock and returns `true`, indicating the stream is
249
- * readable. If an error occurs while trying to get a reader (which can happen if the stream is
250
- * already closed or errored), it catches the error and returns `false`, indicating the stream is
251
- * not readable.
252
- *
253
- * @example
254
- * ```ts
255
- * const readableStream = new ReadableStream({ ... });
256
- * const isStreamReadable = Stream.isReadable({ readableStream });
257
- * console.log(isStreamReadable); // Output: true or false
258
- * ```
259
- *
260
- * @remarks
261
- * - This method does not check whether the stream has data left to read; it only checks if the
262
- * stream is in a state that allows reading. It is possible for a stream to be unlocked but
263
- * still have no data left if it has never been locked to a reader.
264
- *
265
- * @param readableStream - The Web `ReadableStream` to be checked for readability.
266
- *
267
- * @returns `true` if the stream is a `ReadableStream` and is in a readable state (not locked and
268
- * no error on getting a reader); otherwise, `false`.
269
- */
270
- public static isReadable({ readableStream }: { readableStream: ReadableStream }): boolean {
271
- // Check if the stream is a WHATWG `ReadableStream`.
272
- if (!Stream.isReadableStream(readableStream)) {
273
- return false;
274
- }
275
-
276
- // Check if the stream is locked.
277
- if (readableStream.locked) {
278
- return false;
279
- }
280
-
281
- try {
282
- // Try to get a reader to check if the stream is readable.
283
- const reader = readableStream.getReader();
284
- // If successful, immediately release the lock.
285
- reader.releaseLock();
286
- return true;
287
- } catch (error) {
288
- // If an error occurs (e.g., the stream is not readable), return false.
289
- return false;
290
- }
291
- }
292
-
293
- /**
294
- * Checks if an object is a Web `ReadableStream`.
295
- *
296
- * This method verifies whether the given object is a `ReadableStream` by checking its type and
297
- * the presence of the `getReader` function.
298
- *
299
- * @example
300
- * ```ts
301
- * const obj = getSomeObject();
302
- * if (Stream.isReadableStream(obj)) {
303
- * // obj is a ReadableStream
304
- * }
305
- * ```
306
- *
307
- * @param obj - The object to be checked.
308
- * @returns `true` if `obj` is a `ReadableStream`; otherwise, `false`.
309
- */
310
- public static isReadableStream(obj: unknown): obj is ReadableStream {
311
- return (
312
- typeof obj === 'object' && obj !== null &&
313
- 'getReader' in obj && typeof obj.getReader === 'function'
314
- );
315
- }
316
-
317
- /**
318
- * Checks if an object is a Web `ReadableStream`, `WritableStream`, or `TransformStream`.
319
- *
320
- * This method verifies the type of a given object to determine if it is one of the standard
321
- * stream types in the Web Streams API: `ReadableStream`, `WritableStream`, or `TransformStream`.
322
- * It employs type-checking strategies that are specific to each stream type.
323
- *
324
- * The method checks for the specific functions and properties associated with each stream type:
325
- * - `ReadableStream`: Identified by the presence of a `getReader` method.
326
- * - `WritableStream`: Identified by the presence of a `getWriter` and `abort` methods.
327
- * - `TransformStream`: Identified by having both `readable` and `writable` properties.
328
- *
329
- * @example
330
- * ```ts
331
- * const readableStream = new ReadableStream();
332
- * console.log(Stream.isStream(readableStream)); // Output: true
333
- *
334
- * const writableStream = new WritableStream();
335
- * console.log(Stream.isStream(writableStream)); // Output: true
336
- *
337
- * const transformStream = new TransformStream();
338
- * console.log(Stream.isStream(transformStream)); // Output: true
339
- *
340
- * const nonStreamObject = {};
341
- * console.log(Stream.isStream(nonStreamObject)); // Output: false
342
- * ```
343
- *
344
- * @remarks
345
- * - This method does not differentiate between `ReadableStream`, `WritableStream`, and
346
- * `TransformStream`. It checks if the object conforms to any of these types.
347
- * - This method is specific to the Web Streams API and may not recognize non-standard or custom
348
- * stream-like objects that do not adhere to the Web Streams API specifications.
349
- *
350
- * @param obj - The object to be checked for being a Web `ReadableStream`, `WritableStream`, or `TransformStream`.
351
- * @returns `true` if the object is a `ReadableStream`, `WritableStream`, or `TransformStream`; otherwise, `false`.
352
- */
353
- public static isStream(obj: unknown): obj is ReadableStream | WritableStream | TransformStream {
354
- return Stream.isReadableStream(obj) || Stream.isWritableStream(obj) || Stream.isTransformStream(obj);
355
- }
356
-
357
- /**
358
- * Checks if an object is a `TransformStream`.
359
- *
360
- * This method verifies whether the given object is a `TransformStream` by checking its type and
361
- * the presence of `readable` and `writable` properties.
362
- *
363
- * @example
364
- * ```ts
365
- * const obj = getSomeObject();
366
- * if (Stream.isTransformStream(obj)) {
367
- * // obj is a TransformStream
368
- * }
369
- * ```
370
- *
371
- * @param obj - The object to be checked.
372
- * @returns `true` if `obj` is a `TransformStream`; otherwise, `false`.
373
- */
374
- public static isTransformStream(obj: unknown): obj is TransformStream {
375
- return (
376
- typeof obj === 'object' && obj !== null &&
377
- 'readable' in obj && typeof obj.readable === 'object' &&
378
- 'writable' in obj && typeof obj.writable === 'object'
379
- );
380
- }
381
-
382
- /**
383
- * Checks if an object is a `WritableStream`.
384
- *
385
- * This method determines whether the given object is a `WritableStream` by verifying its type and
386
- * the presence of the `getWriter` and `abort` functions.
387
- *
388
- * @example
389
- * ```ts
390
- * const obj = getSomeObject();
391
- * if (Stream.isWritableStream(obj)) {
392
- * // obj is a WritableStream
393
- * }
394
- * ```
395
- *
396
- * @param obj - The object to be checked.
397
- * @returns `true` if `obj` is a `TransformStream`; otherwise, `false`.
398
- */
399
- public static isWritableStream(obj: unknown): obj is WritableStream {
400
- return (
401
- typeof obj === 'object' && obj !== null &&
402
- 'getWriter' in obj && typeof obj.getWriter === 'function' &&
403
- 'abort' in obj && typeof obj.abort === 'function'
404
- );
405
- }
406
- }
@@ -1,117 +0,0 @@
1
- /**
2
- * isArrayBufferSlice
3
- *
4
- * Checks if the ArrayBufferView represents a slice (subarray or a subview)
5
- * of an ArrayBuffer.
6
- *
7
- * An ArrayBufferView (TypedArray or DataView) can represent a portion of an
8
- * ArrayBuffer - such a view is said to be a "slice" of the original buffer.
9
- * This can occur when the `subarray` or `slice` method is called on a
10
- * TypedArray or when a DataView is created with a byteOffset and/or
11
- * byteLength that doesn't cover the full ArrayBuffer.
12
- *
13
- * @param arrayBufferView - The ArrayBufferView to be checked
14
- * @returns true if the ArrayBufferView represents a slice of an ArrayBuffer; false otherwise.
15
- */
16
- export function isArrayBufferSlice(arrayBufferView: ArrayBufferView): boolean {
17
- return arrayBufferView.byteOffset !== 0 || arrayBufferView.byteLength !== arrayBufferView.buffer.byteLength;
18
- }
19
-
20
- /**
21
- * Checks if the given object is an AsyncIterable.
22
- *
23
- * An AsyncIterable is an object that implements the AsyncIterable protocol,
24
- * which means it has a [Symbol.asyncIterator] method. This function checks
25
- * if the provided object conforms to this protocol by verifying the presence
26
- * and type of the [Symbol.asyncIterator] method.
27
- *
28
- * @param obj - The object to be checked for AsyncIterable conformity.
29
- * @returns True if the object is an AsyncIterable, false otherwise.
30
- *
31
- * @example
32
- * ```ts
33
- * // Returns true for a valid AsyncIterable
34
- * const asyncIterable = {
35
- * async *[Symbol.asyncIterator]() {
36
- * yield 1;
37
- * yield 2;
38
- * }
39
- * };
40
- * console.log(isAsyncIterable(asyncIterable)); // true
41
- * ```
42
- *
43
- * @example
44
- * ```ts
45
- * // Returns false for a regular object
46
- * console.log(isAsyncIterable({ a: 1, b: 2 })); // false
47
- * ```
48
- */
49
- export function isAsyncIterable(obj: any): obj is AsyncIterable<any> {
50
- if (typeof obj !== 'object' || obj === null) {
51
- return false;
52
- }
53
-
54
- return typeof obj[Symbol.asyncIterator] === 'function';
55
- }
56
-
57
- /**
58
- * isDefined
59
- *
60
- * Utility function to check if a variable is neither null nor undefined.
61
- * This function helps in making TypeScript infer the type of the variable
62
- * as being defined, excluding `null` and `undefined`.
63
- *
64
- * The function uses strict equality (`!==`) for the comparison, ensuring
65
- * that the variable is not just falsy (like an empty string or zero),
66
- * but is truly either `null` or `undefined`.
67
- *
68
- * @param arg - The variable to be checked
69
- * @returns true if the variable is neither `null` nor `undefined`
70
- */
71
- export function isDefined<T>(arg: T): arg is Exclude<T, null | undefined> {
72
- return arg !== null && typeof arg !== 'undefined';
73
- }
74
-
75
- /**
76
- * universalTypeOf
77
- *
78
- * Why does this function exist?
79
- *
80
- * You can typically check if a value is of a particular type, such as
81
- * Uint8Array or ArrayBuffer, by using the `instanceof` operator. The
82
- * `instanceof` operator checks the prototype property of a constructor
83
- * in the object's prototype chain.
84
- *
85
- * However, there is a caveat with the `instanceof` check if the value
86
- * was created from a different JavaScript context (like an iframe or
87
- * a web worker). In those cases, the `instanceof` check might fail
88
- * because each context has a different global object, and therefore,
89
- * different built-in constructor functions.
90
- *
91
- * The `typeof` operator provides information about the type of the
92
- * operand in a less detailed way. For basic data types like number,
93
- * string, boolean, and undefined, the `typeof` operator works as
94
- * expected. However, for objects, including arrays and null,
95
- * it always returns "object". For functions, it returns "function".
96
- * So, while `typeof` is good for basic type checking, it doesn't
97
- * give detailed information about complex data types.
98
- *
99
- * Unlike `instanceof` and `typeof`, `Object.prototype.toString.call(value)`
100
- * can ensure a consistent result across different JavaScript
101
- * contexts.
102
- *
103
- * Credit for inspiration:
104
- * Angus Croll
105
- * https://github.com/angus-c
106
- * https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
107
- */
108
- export function universalTypeOf(value: unknown) {
109
- // Returns '[Object Type]' string.
110
- const typeString = Object.prototype.toString.call(value);
111
- // Returns ['Object', 'Type'] array or null.
112
- const match = typeString.match(/\s([a-zA-Z0-9]+)/);
113
- // Deconstructs the array and gets just the type from index 1.
114
- const [_, type] = match as RegExpMatchArray;
115
-
116
- return type;
117
- }
@@ -1,48 +0,0 @@
1
- /**
2
- * Interface for a generic key-value store.
3
- */
4
- export interface KeyValueStore<K, V> {
5
- /**
6
- * Clears the store, removing all key-value pairs.
7
- *
8
- * @returns A promise that resolves when the store has been cleared.
9
- */
10
- clear(): Promise<void>;
11
-
12
- /**
13
- * Closes the store, freeing up any resources used. After calling this method, no other operations can be performed on the store.
14
- *
15
- * @returns A promise that resolves when the store has been closed.
16
- */
17
- close(): Promise<void>;
18
-
19
- /**
20
- * Deletes a key-value pair from the store.
21
- *
22
- * @param key - The key of the value to delete.
23
- * @returns A promise that resolves to true if the element existed and has been removed, or false if the element does not exist.
24
- */
25
- delete(key: K): Promise<boolean | void>;
26
-
27
- /**
28
- * Fetches a value from the store given its key.
29
- *
30
- * @param key - The key of the value to retrieve.
31
- * @returns A promise that resolves with the value associated with the key, or `undefined` if no value exists for that key.
32
- */
33
- get(key: K): Promise<V | undefined>;
34
-
35
- /**
36
- * Sets the value for a key in the store.
37
- *
38
- * @param key - The key under which to store the value.
39
- * @param value - The value to be stored.
40
- * @returns A promise that resolves when the value has been set.
41
- */
42
- set(key: K, value: V): Promise<void>;
43
- }
44
-
45
- /**
46
- * Represents an object type where a subset of keys are required and everything else is optional.
47
- */
48
- export type RequireOnly<T, K extends keyof T, O extends keyof T = never> = Required<Pick<T, K>> & Omit<Partial<T>, O>;