@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.
- package/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
package/src/common/stores.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { Level } from 'level';
|
|
2
|
-
|
|
3
|
-
import type { KeyValueStore } from './types.js';
|
|
4
|
-
|
|
5
|
-
export class LevelStore implements KeyValueStore<string, any> {
|
|
6
|
-
private store: Level<string, string>;
|
|
7
|
-
|
|
8
|
-
constructor(location = 'data/DATASTORE') {
|
|
9
|
-
this.store = new Level(location);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async clear(): Promise<void> {
|
|
13
|
-
await this.store.clear();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async close(): Promise<void> {
|
|
17
|
-
await this.store.close();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async delete(key: string): Promise<boolean> {
|
|
21
|
-
await this.store.del(key);
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async get(key: string): Promise<any> {
|
|
26
|
-
return await this.store.get(key);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async set(key: string, value: any): Promise<void> {
|
|
30
|
-
await this.store.put(key, value);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The `MemoryStore` class is an implementation of
|
|
36
|
-
* `KeyValueStore` that holds data in memory.
|
|
37
|
-
*
|
|
38
|
-
* It provides a basic key-value store that works synchronously and keeps all
|
|
39
|
-
* data in memory. This can be used for testing, or for handling small amounts
|
|
40
|
-
* of data with simple key-value semantics.
|
|
41
|
-
*
|
|
42
|
-
* Example usage:
|
|
43
|
-
*
|
|
44
|
-
* ```ts
|
|
45
|
-
* const memoryStore = new MemoryStore<string, number>();
|
|
46
|
-
* await memoryStore.set("key1", 1);
|
|
47
|
-
* const value = await memoryStore.get("key1");
|
|
48
|
-
* console.log(value); // 1
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* @public
|
|
52
|
-
*/
|
|
53
|
-
export class MemoryStore<K, V> implements KeyValueStore<K, V> {
|
|
54
|
-
/**
|
|
55
|
-
* A private field that contains the Map used as the key-value store.
|
|
56
|
-
*/
|
|
57
|
-
private store: Map<K, V> = new Map();
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Clears all entries in the key-value store.
|
|
61
|
-
*
|
|
62
|
-
* @returns A Promise that resolves when the operation is complete.
|
|
63
|
-
*/
|
|
64
|
-
async clear(): Promise<void> {
|
|
65
|
-
this.store.clear();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* This operation is no-op for `MemoryStore`
|
|
70
|
-
* and will log a warning if called.
|
|
71
|
-
*/
|
|
72
|
-
async close(): Promise<void> {
|
|
73
|
-
/** no-op */
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Deletes an entry from the key-value store by its key.
|
|
78
|
-
*
|
|
79
|
-
* @param id - The key of the entry to delete.
|
|
80
|
-
* @returns A Promise that resolves to a boolean indicating whether the entry was successfully deleted.
|
|
81
|
-
*/
|
|
82
|
-
async delete(id: K): Promise<boolean> {
|
|
83
|
-
return this.store.delete(id);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Retrieves the value of an entry by its key.
|
|
88
|
-
*
|
|
89
|
-
* @param id - The key of the entry to retrieve.
|
|
90
|
-
* @returns A Promise that resolves to the value of the entry, or `undefined` if the entry does not exist.
|
|
91
|
-
*/
|
|
92
|
-
async get(id: K): Promise<V | undefined> {
|
|
93
|
-
return this.store.get(id);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Checks for the presence of an entry by key.
|
|
98
|
-
*
|
|
99
|
-
* @param id - The key to check for the existence of.
|
|
100
|
-
* @returns A Promise that resolves to a boolean indicating whether an element with the specified key exists or not.
|
|
101
|
-
*/
|
|
102
|
-
async has(id: K): Promise<boolean> {
|
|
103
|
-
return this.store.has(id);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Retrieves all values in the key-value store.
|
|
108
|
-
*
|
|
109
|
-
* @returns A Promise that resolves to an array of all values in the store.
|
|
110
|
-
*/
|
|
111
|
-
async list(): Promise<V[]> {
|
|
112
|
-
return Array.from(this.store.values());
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Sets the value of an entry in the key-value store.
|
|
117
|
-
*
|
|
118
|
-
* @param id - The key of the entry to set.
|
|
119
|
-
* @param key - The new value for the entry.
|
|
120
|
-
* @returns A Promise that resolves when the operation is complete.
|
|
121
|
-
*/
|
|
122
|
-
async set(id: K, key: V): Promise<void> {
|
|
123
|
-
this.store.set(id, key);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
import type { Duplex, ReadableStateOptions, Transform, Writable } from 'readable-stream';
|
|
2
|
-
|
|
3
|
-
import { Readable } from 'readable-stream';
|
|
4
|
-
import { Stream } from './stream.js';
|
|
5
|
-
import { Convert } from './convert.js';
|
|
6
|
-
|
|
7
|
-
export { Readable } from 'readable-stream';
|
|
8
|
-
|
|
9
|
-
export class NodeStream {
|
|
10
|
-
/**
|
|
11
|
-
* Consumes a `Readable` stream and returns its contents as an `ArrayBuffer`.
|
|
12
|
-
*
|
|
13
|
-
* This method reads all data from a Node.js `Readable` stream, collects it, and converts it into
|
|
14
|
-
* an `ArrayBuffer`.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const nodeReadable = getReadableStreamSomehow();
|
|
19
|
-
* const arrayBuffer = await NodeStream.consumeToArrayBuffer({ readable: nodeReadable });
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @param readable - The Node.js Readable stream whose data will be consumed.
|
|
23
|
-
* @returns A Promise that resolves to an `ArrayBuffer` containing all the data from the stream.
|
|
24
|
-
*/
|
|
25
|
-
public static async consumeToArrayBuffer({ readable }: { readable: Readable}): Promise<ArrayBuffer> {
|
|
26
|
-
const arrayBuffer = await Convert.asyncIterable(readable).toArrayBufferAsync();
|
|
27
|
-
|
|
28
|
-
return arrayBuffer;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Consumes a `Readable` stream and returns its contents as a `Blob`.
|
|
33
|
-
*
|
|
34
|
-
* This method reads all data from a Node.js `Readable` stream, collects it, and converts it into
|
|
35
|
-
* a `Blob`.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* const nodeReadable = getReadableStreamSomehow();
|
|
40
|
-
* const blob = await NodeStream.consumeToBlob({ readable: nodeReadable });
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @param readableStream - The Node.js `Readable` stream whose data will be consumed.
|
|
44
|
-
* @returns A Promise that resolves to a `Blob` containing all the data from the stream.
|
|
45
|
-
*/
|
|
46
|
-
public static async consumeToBlob({ readable }: { readable: Readable }): Promise<Blob> {
|
|
47
|
-
const blob = await Convert.asyncIterable(readable).toBlobAsync();
|
|
48
|
-
|
|
49
|
-
return blob;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Consumes a `Readable` stream and returns its contents as a `Uint8Array`.
|
|
54
|
-
*
|
|
55
|
-
* This method reads all data from a Node.js `Readable`, collects it, and converts it into a
|
|
56
|
-
* `Uint8Array`.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* const nodeReadable = getReadableStreamSomehow();
|
|
61
|
-
* const bytes = await NodeStream.consumeToBytes({ readable: nodeReadable });
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @param readableStream - The Node.js `Readable` stream whose data will be consumed.
|
|
65
|
-
* @returns A Promise that resolves to a `Uint8Array` containing all the data from the stream.
|
|
66
|
-
*/
|
|
67
|
-
public static async consumeToBytes({ readable }: { readable: Readable }): Promise<Uint8Array> {
|
|
68
|
-
const bytes = await Convert.asyncIterable(readable).toUint8ArrayAsync();
|
|
69
|
-
|
|
70
|
-
return bytes;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Consumes a `Readable` stream and parses its contents as JSON.
|
|
75
|
-
*
|
|
76
|
-
* This method reads all the data from the stream, converts it to a text string, and then parses
|
|
77
|
-
* it as JSON, returning the resulting object.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* const nodeReadable = getReadableStreamSomehow();
|
|
82
|
-
* const jsonData = await NodeStream.consumeToJson({ readable: nodeReadable });
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* @param readableStream - The Node.js `Readable` stream whose JSON content will be consumed.
|
|
86
|
-
* @returns A Promise that resolves to the parsed JSON object from the stream's data.
|
|
87
|
-
*/
|
|
88
|
-
public static async consumeToJson({ readable }: { readable: Readable }): Promise<any> {
|
|
89
|
-
const object = await Convert.asyncIterable(readable).toObjectAsync();
|
|
90
|
-
|
|
91
|
-
return object;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Consumes a `Readable` stream and returns its contents as a text string.
|
|
96
|
-
*
|
|
97
|
-
* This method reads all the data from the stream, converting it into a single string.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```ts
|
|
101
|
-
* const nodeReadable = getReadableStreamSomehow();
|
|
102
|
-
* const text = await NodeStream.consumeToText({ readable: nodeReadable });
|
|
103
|
-
* ```
|
|
104
|
-
*
|
|
105
|
-
* @param readableStream - The Node.js `Readable` stream whose text content will be consumed.
|
|
106
|
-
* @returns A Promise that resolves to a string containing all the data from the stream.
|
|
107
|
-
*/
|
|
108
|
-
public static async consumeToText({ readable }: { readable: Readable}): Promise<string> {
|
|
109
|
-
const text = await Convert.asyncIterable(readable).toStringAsync();
|
|
110
|
-
|
|
111
|
-
return text;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Converts a Web `ReadableStream` to a Node.js `Readable` stream.
|
|
116
|
-
*
|
|
117
|
-
* This method takes a Web `ReadableStream` and converts it to a Node.js `Readable` stream.
|
|
118
|
-
* The conversion is done by reading chunks from the Web `ReadableStream` and pushing them
|
|
119
|
-
* into the Node.js `Readable` stream.
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```ts
|
|
123
|
-
* const webReadableStream = getWebReadableStreamSomehow();
|
|
124
|
-
* const nodeReadableStream = NodeStream.fromWebReadable({ readableStream: webReadableStream });
|
|
125
|
-
* ```
|
|
126
|
-
*
|
|
127
|
-
* @param readableStream - The Web `ReadableStream` to be converted.
|
|
128
|
-
* @param readableOptions - Optional `Readable` stream options for the Node.js stream.
|
|
129
|
-
* @returns The Node.js `Readable` stream.
|
|
130
|
-
*/
|
|
131
|
-
public static fromWebReadable({ readableStream, readableOptions }: {
|
|
132
|
-
readableStream: ReadableStream,
|
|
133
|
-
readableOptions?: ReadableStateOptions
|
|
134
|
-
}): Readable {
|
|
135
|
-
if (!Stream.isReadableStream(readableStream)) {
|
|
136
|
-
throw new TypeError(`NodeStream.fromWebReadable: 'readableStream' is not a Web ReadableStream.`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const reader = readableStream.getReader();
|
|
140
|
-
let closed = false;
|
|
141
|
-
|
|
142
|
-
const nodeReadable = new Readable({
|
|
143
|
-
...readableOptions,
|
|
144
|
-
|
|
145
|
-
read: function () {
|
|
146
|
-
reader.read().then(({ done, value }) => {
|
|
147
|
-
if (done) {
|
|
148
|
-
this.push(null); // Push null to signify end of stream.
|
|
149
|
-
} else {
|
|
150
|
-
if (!this.push(value)) {
|
|
151
|
-
// When push returns false, we should stop reading until _read is called again.
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}).catch((error) => {
|
|
156
|
-
// If an error occurs while reading, destroy the stream.
|
|
157
|
-
this.destroy(error);
|
|
158
|
-
});
|
|
159
|
-
},
|
|
160
|
-
|
|
161
|
-
destroy: function (error, callback) {
|
|
162
|
-
function done() {
|
|
163
|
-
callback(error);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (!closed) {
|
|
167
|
-
reader.cancel(error)
|
|
168
|
-
.then(done)
|
|
169
|
-
.catch(done);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
done();
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
reader.closed
|
|
177
|
-
.then(() => {
|
|
178
|
-
closed = true; // Prevents reader.cancel() from being called in destroy()
|
|
179
|
-
})
|
|
180
|
-
.catch((error) => {
|
|
181
|
-
closed = true; // Prevents reader.cancel() from being called in destroy()
|
|
182
|
-
nodeReadable.destroy(error);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
return nodeReadable;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Checks if a Node.js stream (`Readable`, `Writable`, `Duplex`, or `Transform`) has been destroyed.
|
|
190
|
-
*
|
|
191
|
-
* This method determines whether the provided Node.js stream has been destroyed. A stream
|
|
192
|
-
* is considered destroyed if its 'destroyed' property is set to true or if its internal state
|
|
193
|
-
* indicates it has been destroyed.
|
|
194
|
-
*
|
|
195
|
-
* @example
|
|
196
|
-
* ```ts
|
|
197
|
-
* const stream = getStreamSomehow();
|
|
198
|
-
* stream.destroy(); // Destroy the stream.
|
|
199
|
-
* const isDestroyed = NodeStream.isDestroyed({ stream });
|
|
200
|
-
* console.log(isDestroyed); // Output: true
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* @param stream - The Node.js stream to check.
|
|
204
|
-
* @returns `true` if the stream has been destroyed; otherwise, `false`.
|
|
205
|
-
*/
|
|
206
|
-
public static isDestroyed({ stream }: { stream: Readable | Writable | Duplex | Transform }): boolean {
|
|
207
|
-
if (!NodeStream.isStream(stream)) {
|
|
208
|
-
throw new TypeError(`NodeStream.isDestroyed: 'stream' is not a Node stream.`);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const writableState = '_writableState' in stream ? stream._writableState : undefined;
|
|
212
|
-
const readableState = stream._readableState;
|
|
213
|
-
const state = writableState || readableState;
|
|
214
|
-
|
|
215
|
-
return !!(stream.destroyed || state.destroyed);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Checks if a Node.js `Readable` stream is still readable.
|
|
220
|
-
*
|
|
221
|
-
* This method checks if a Node.js `Readable` stream is still in a state that allows reading from
|
|
222
|
-
* it. A stream is considered readable if it has not ended, has not been destroyed, and is not
|
|
223
|
-
* currently paused.
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```ts
|
|
227
|
-
* const readableStream = new Readable();
|
|
228
|
-
* const isReadable = NodeStream.isReadable({ readable: readableStream });
|
|
229
|
-
* console.log(isReadable); // Output: true or false
|
|
230
|
-
* ```
|
|
231
|
-
*
|
|
232
|
-
* @param readable - The Node.js `Readable` stream to be checked.
|
|
233
|
-
* @returns `true` if the stream is still readable; otherwise, `false`.
|
|
234
|
-
*/
|
|
235
|
-
public static isReadable({ readable }: { readable: Readable }): boolean {
|
|
236
|
-
// Check if the object is a Node Readable stream.
|
|
237
|
-
if (!NodeStream.isReadableStream(readable)) {
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Check if the stream is still readable.
|
|
242
|
-
return (
|
|
243
|
-
readable.readable && // Is the stream readable?
|
|
244
|
-
(typeof readable._readableState.ended === 'boolean' && !readable._readableState.ended) && // Has the 'end' method been called?
|
|
245
|
-
(typeof readable._readableState.endEmitted === 'boolean' && !readable._readableState.endEmitted) && // Has the 'end' event been emitted?
|
|
246
|
-
!readable.destroyed && // Has the 'destroy' method been called?
|
|
247
|
-
!readable.isPaused() // Is the stream paused?
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Checks if an object is a Node.js `Readable` stream.
|
|
253
|
-
*
|
|
254
|
-
* This method verifies if the provided object is a Node.js `Readable` stream by checking for
|
|
255
|
-
* specific properties and methods typical of a `Readable` stream in Node.js.
|
|
256
|
-
*
|
|
257
|
-
* @example
|
|
258
|
-
* ```ts
|
|
259
|
-
* const obj = getSomeObject();
|
|
260
|
-
* if (NodeStream.isReadableStream(obj)) {
|
|
261
|
-
* // obj is a Node.js Readable stream
|
|
262
|
-
* }
|
|
263
|
-
* ```
|
|
264
|
-
*
|
|
265
|
-
* @param obj - The object to be checked.
|
|
266
|
-
* @returns `true` if `obj` is a Node.js `Readable` stream; otherwise, `false`.
|
|
267
|
-
*/
|
|
268
|
-
static isReadableStream(obj: unknown): obj is Readable {
|
|
269
|
-
return (
|
|
270
|
-
typeof obj === 'object' &&
|
|
271
|
-
obj !== null &&
|
|
272
|
-
('pipe' in obj && typeof obj.pipe === 'function') &&
|
|
273
|
-
('on' in obj && typeof obj.on === 'function') &&
|
|
274
|
-
(!('_writableState' in obj) && '_readableState' in obj)
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Checks if the provided object is a Node.js stream (`Duplex`, `Readable`, `Writable`, or `Transform`).
|
|
280
|
-
*
|
|
281
|
-
* This method checks for the presence of internal properties specific to Node.js streams:
|
|
282
|
-
* `_readableState` and `_writableState`. These properties are present in Node.js stream
|
|
283
|
-
* instances, allowing identification of the stream type.
|
|
284
|
-
*
|
|
285
|
-
* The `_readableState` property is found in `Readable` and `Duplex` streams (including
|
|
286
|
-
* `Transform` streams, which are a type of `Duplex` stream), indicating that the stream can be
|
|
287
|
-
* read from. The `_writableState` property is found in `Writable` and `Duplex` streams,
|
|
288
|
-
* indicating that the stream can be written to.
|
|
289
|
-
*
|
|
290
|
-
* @example
|
|
291
|
-
* ```ts
|
|
292
|
-
* const { Readable, Writable, Duplex, Transform } = require('stream');
|
|
293
|
-
*
|
|
294
|
-
* const readableStream = new Readable();
|
|
295
|
-
* console.log(NodeStream.isStream(readableStream)); // Output: true
|
|
296
|
-
*
|
|
297
|
-
* const writableStream = new Writable();
|
|
298
|
-
* console.log(NodeStream.isStream(writableStream)); // Output: true
|
|
299
|
-
*
|
|
300
|
-
* const duplexStream = new Duplex();
|
|
301
|
-
* console.log(NodeStream.isStream(duplexStream)); // Output: true
|
|
302
|
-
*
|
|
303
|
-
* const transformStream = new Transform();
|
|
304
|
-
* console.log(NodeStream.isStream(transformStream)); // Output: true
|
|
305
|
-
*
|
|
306
|
-
* const nonStreamObject = {};
|
|
307
|
-
* console.log(NodeStream.isStream(nonStreamObject)); // Output: false
|
|
308
|
-
* ```
|
|
309
|
-
*
|
|
310
|
-
* @remarks
|
|
311
|
-
* - This method does not differentiate between the different types of streams (Readable,
|
|
312
|
-
* Writable, Duplex, Transform). It simply checks if the object is any kind of Node.js stream.
|
|
313
|
-
* - While this method can identify standard Node.js streams, it may not recognize custom or
|
|
314
|
-
* third-party stream-like objects that do not inherit directly from Node.js's stream classes
|
|
315
|
-
* or do not have these internal state properties. This is intentional as many of the methods
|
|
316
|
-
* in this library are designed to work with standard Node.js streams.
|
|
317
|
-
*
|
|
318
|
-
* @param obj - The object to be checked for being a Node.js stream.
|
|
319
|
-
* @returns `true` if the object is a Node.js stream (`Duplex`, `Readable`, `Writable`, or `Transform`); otherwise, `false`.
|
|
320
|
-
*/
|
|
321
|
-
public static isStream(obj: unknown): obj is Duplex | Readable | Writable | Transform {
|
|
322
|
-
return (
|
|
323
|
-
typeof obj === 'object' && obj !== null &&
|
|
324
|
-
('_readableState' in obj || '_writableState' in obj)
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Converts a Node.js `Readable` stream to a Web `ReadableStream`.
|
|
330
|
-
*
|
|
331
|
-
* This method provides a bridge between Node.js streams and the Web Streams API by converting a
|
|
332
|
-
* Node.js `Readable` stream into a Web `ReadableStream`. It listens for 'data', 'end', and 'error'
|
|
333
|
-
* events on the Node.js stream and appropriately enqueues data, closes, or errors the Web
|
|
334
|
-
* `ReadableStream`.
|
|
335
|
-
*
|
|
336
|
-
* If the Node.js stream is already destroyed, the method returns an immediately cancelled
|
|
337
|
-
* Web `ReadableStream`.
|
|
338
|
-
*
|
|
339
|
-
* @example
|
|
340
|
-
* ```ts
|
|
341
|
-
* const nodeReadable = getNodeReadableStreamSomehow();
|
|
342
|
-
* const webReadableStream = NodeStream.toWebReadable({ readable: nodeReadable });
|
|
343
|
-
* ```
|
|
344
|
-
*
|
|
345
|
-
* @param readable - The Node.js `Readable` stream to be converted.
|
|
346
|
-
* @returns A Web `ReadableStream` corresponding to the provided Node.js `Readable` stream.
|
|
347
|
-
* @throws TypeError if `readable` is not a Node.js `Readable` stream.
|
|
348
|
-
* @throws Error if the Node.js `Readable` stream is already destroyed.
|
|
349
|
-
*/
|
|
350
|
-
static toWebReadable({ readable }: { readable: Readable }): ReadableStream {
|
|
351
|
-
if (!NodeStream.isReadableStream(readable)) {
|
|
352
|
-
throw new TypeError(`NodeStream.toWebReadable: 'readable' is not a Node Readable stream.`);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (NodeStream.isDestroyed({ stream: readable })) {
|
|
356
|
-
const readable = new ReadableStream();
|
|
357
|
-
readable.cancel();
|
|
358
|
-
return readable;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return new ReadableStream({
|
|
362
|
-
start(controller) {
|
|
363
|
-
readable.on('data', (chunk) => {
|
|
364
|
-
controller.enqueue(chunk);
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
readable.on('end', () => {
|
|
368
|
-
controller.close();
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
readable.on('error', (err) => {
|
|
372
|
-
controller.error(err);
|
|
373
|
-
});
|
|
374
|
-
},
|
|
375
|
-
|
|
376
|
-
cancel() {
|
|
377
|
-
readable.destroy();
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
}
|