@substrate-system/crypto-stream 0.0.33
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/LICENSE +99 -0
- package/README.md +366 -0
- package/dist/concat-streams.cjs +59 -0
- package/dist/ece.cjs +375 -0
- package/dist/extract-transformer.cjs +55 -0
- package/dist/index.cjs +28 -0
- package/dist/keychain.cjs +344 -0
- package/dist/slice-transformer.cjs +75 -0
- package/dist/src/concat-streams.d.ts +9 -0
- package/dist/src/concat-streams.d.ts.map +1 -0
- package/dist/src/concat-streams.js +46 -0
- package/dist/src/concat-streams.js.map +1 -0
- package/dist/src/ece.d.ts +66 -0
- package/dist/src/ece.d.ts.map +1 -0
- package/dist/src/ece.js +373 -0
- package/dist/src/ece.js.map +1 -0
- package/dist/src/extract-transformer.d.ts +18 -0
- package/dist/src/extract-transformer.d.ts.map +1 -0
- package/dist/src/extract-transformer.js +40 -0
- package/dist/src/extract-transformer.js.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/keychain.d.ts +103 -0
- package/dist/src/keychain.d.ts.map +1 -0
- package/dist/src/keychain.js +267 -0
- package/dist/src/keychain.js.map +1 -0
- package/dist/src/slice-transformer.d.ts +19 -0
- package/dist/src/slice-transformer.d.ts.map +1 -0
- package/dist/src/slice-transformer.js +58 -0
- package/dist/src/slice-transformer.js.map +1 -0
- package/dist/src/transform-stream.d.ts +11 -0
- package/dist/src/transform-stream.d.ts.map +1 -0
- package/dist/src/transform-stream.js +136 -0
- package/dist/src/transform-stream.js.map +1 -0
- package/dist/src/util.d.ts +27 -0
- package/dist/src/util.d.ts.map +1 -0
- package/dist/src/util.js +49 -0
- package/dist/src/util.js.map +1 -0
- package/dist/transform-stream.cjs +159 -0
- package/dist/util.cjs +57 -0
- package/package.json +86 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { webcrypto } from '@substrate-system/one-webcrypto';
|
|
2
|
+
import * as u from 'uint8arrays';
|
|
3
|
+
import { decryptStream, decryptStreamRange, encryptStream, KEY_LENGTH, } from './ece.js';
|
|
4
|
+
import { randomBuf, joinBufs, asBufferSource } from './util.js';
|
|
5
|
+
export { encryptedSize, plaintextSize } from './ece.js';
|
|
6
|
+
const IV_LENGTH = 12;
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
export class Keychain {
|
|
9
|
+
key;
|
|
10
|
+
salt;
|
|
11
|
+
mainKeyPromise;
|
|
12
|
+
metaKeyPromise;
|
|
13
|
+
authTokenPromise;
|
|
14
|
+
constructor(key, salt) {
|
|
15
|
+
this.key = decodeBits(key);
|
|
16
|
+
this.salt = decodeBits(salt);
|
|
17
|
+
this.mainKeyPromise = webcrypto.subtle.importKey('raw', this.key, 'HKDF', false, ['deriveBits', 'deriveKey']);
|
|
18
|
+
this.metaKeyPromise = this.mainKeyPromise
|
|
19
|
+
.then(mainKey => webcrypto.subtle.deriveKey({
|
|
20
|
+
name: 'HKDF',
|
|
21
|
+
hash: 'SHA-256',
|
|
22
|
+
salt: this.salt,
|
|
23
|
+
info: encoder.encode('metadata')
|
|
24
|
+
}, mainKey, {
|
|
25
|
+
name: 'AES-GCM',
|
|
26
|
+
length: 128
|
|
27
|
+
}, false, ['encrypt', 'decrypt']));
|
|
28
|
+
this.authTokenPromise = this.mainKeyPromise
|
|
29
|
+
.then(mainKey => webcrypto.subtle.deriveBits({
|
|
30
|
+
name: 'HKDF',
|
|
31
|
+
hash: 'SHA-256',
|
|
32
|
+
salt: this.salt,
|
|
33
|
+
info: encoder.encode('authentication')
|
|
34
|
+
}, mainKey, 128))
|
|
35
|
+
.then(authTokenBuf => new Uint8Array(authTokenBuf));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get an authentication header as a static method.
|
|
39
|
+
*/
|
|
40
|
+
static async AuthHeader(secretKey, salt) {
|
|
41
|
+
const key = decodeBits(secretKey);
|
|
42
|
+
const mainKey = await webcrypto.subtle.importKey('raw', key, 'HKDF', false, ['deriveBits', 'deriveKey']);
|
|
43
|
+
const token = await webcrypto.subtle.deriveBits({
|
|
44
|
+
name: 'HKDF',
|
|
45
|
+
hash: 'SHA-256',
|
|
46
|
+
salt: decodeBits(salt),
|
|
47
|
+
info: encoder.encode('authentication')
|
|
48
|
+
}, mainKey, 128);
|
|
49
|
+
return Keychain.Header(arrayToB64(new Uint8Array(token)));
|
|
50
|
+
}
|
|
51
|
+
static Header(writeToken) {
|
|
52
|
+
return `Bearer sync-v1 ${writeToken}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the main key as a `base64url` encoded string
|
|
56
|
+
*/
|
|
57
|
+
get keyB64() {
|
|
58
|
+
return arrayToB64Url(this.key);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the salt as base64 string
|
|
62
|
+
*/
|
|
63
|
+
get saltB64() {
|
|
64
|
+
return arrayToB64(this.salt);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* get a promise for the auth token.
|
|
68
|
+
* @returns {Promise<Uint8Array>}
|
|
69
|
+
*/
|
|
70
|
+
async authToken() {
|
|
71
|
+
return await this.authTokenPromise;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the auth token as a base64 string
|
|
75
|
+
*/
|
|
76
|
+
async authTokenB64() {
|
|
77
|
+
const authToken = await this.authToken();
|
|
78
|
+
return arrayToB64(authToken);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get a header string: `Bearer sync-v1 ${authTokenB64}`.
|
|
82
|
+
* Pass in a token, or else this will use the `authToken` derived from
|
|
83
|
+
* the main key.
|
|
84
|
+
*/
|
|
85
|
+
async authHeader(tokenString) {
|
|
86
|
+
if (tokenString) {
|
|
87
|
+
return `Bearer sync-v1 ${tokenString}`;
|
|
88
|
+
}
|
|
89
|
+
const authTokenB64 = await this.authTokenB64();
|
|
90
|
+
return `Bearer sync-v1 ${authTokenB64}`;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Set the auth token
|
|
94
|
+
* @param authToken The new token
|
|
95
|
+
*/
|
|
96
|
+
setAuthToken(authToken) {
|
|
97
|
+
this.authTokenPromise = Promise.resolve(decodeBits(authToken));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Take a stream, return an encrypted stream.
|
|
101
|
+
* @param stream Input stream
|
|
102
|
+
* @returns {Promise<ReadableStream>}
|
|
103
|
+
*/
|
|
104
|
+
async encryptStream(stream) {
|
|
105
|
+
if (!(stream instanceof ReadableStream)) {
|
|
106
|
+
throw new TypeError('This is not a readable stream');
|
|
107
|
+
}
|
|
108
|
+
const mainKey = await this.mainKeyPromise;
|
|
109
|
+
return encryptStream(stream, mainKey);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Encrypt and return some data; don't stream.
|
|
113
|
+
*
|
|
114
|
+
* NOTE: This generates a new key each time it is called, via
|
|
115
|
+
* `this.generateKey`.
|
|
116
|
+
*
|
|
117
|
+
* @param bytes
|
|
118
|
+
* @param {{ iv?:Uint8Array, size?:number }} [opts] Optional params,
|
|
119
|
+
* `iv` and `size`. If `size` is omitted, default is 16 bytes. `iv` is
|
|
120
|
+
* a random 12 bits, will be generated if not passed in.
|
|
121
|
+
* @returns {Promise<Uint8Array>}
|
|
122
|
+
*/
|
|
123
|
+
async encryptBytes(bytes, opts) {
|
|
124
|
+
const iv = opts?.iv || randomBuf(12);
|
|
125
|
+
const encryptedRecordBuf = await webcrypto.subtle.encrypt({
|
|
126
|
+
name: 'AES-GCM',
|
|
127
|
+
iv: asBufferSource(iv),
|
|
128
|
+
}, await this.generateKey(opts?.size), asBufferSource(bytes));
|
|
129
|
+
return joinBufs(iv, encryptedRecordBuf);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Decrypt in memory, not streaming.
|
|
133
|
+
*/
|
|
134
|
+
async decryptBytes(bytes) {
|
|
135
|
+
const key = await this.generateKey();
|
|
136
|
+
// `iv` is prepended to the encrypted text
|
|
137
|
+
const iv = bytes.slice(0, 12);
|
|
138
|
+
const cipherBytes = bytes.slice(12);
|
|
139
|
+
const msgBuf = await webcrypto.subtle.decrypt({
|
|
140
|
+
name: 'AES-GCM',
|
|
141
|
+
iv
|
|
142
|
+
}, key, cipherBytes);
|
|
143
|
+
return msgBuf;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Derive a new AES-GCM key from the main key.
|
|
147
|
+
*
|
|
148
|
+
* @param {number} [keyLength] Optional size for the key, in bytes, eg,
|
|
149
|
+
* `16` or `32`.
|
|
150
|
+
* @returns {Promise<CryptoKey>}
|
|
151
|
+
*/
|
|
152
|
+
async generateKey(keyLength) {
|
|
153
|
+
const keySize = (keyLength || KEY_LENGTH) * 8;
|
|
154
|
+
return webcrypto.subtle.deriveKey({
|
|
155
|
+
name: 'HKDF',
|
|
156
|
+
hash: 'SHA-256',
|
|
157
|
+
salt: this.salt,
|
|
158
|
+
info: encoder.encode('Content-Encoding: aes128gcm\0')
|
|
159
|
+
}, (await this.mainKeyPromise), {
|
|
160
|
+
name: 'AES-GCM',
|
|
161
|
+
length: keySize
|
|
162
|
+
}, false, ['encrypt', 'decrypt']);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Take an encrypted stream, return a decrypted stream.
|
|
166
|
+
* @param encryptedStream The input (encrypted) stream
|
|
167
|
+
* @returns The decrypted stream
|
|
168
|
+
*/
|
|
169
|
+
async decryptStream(encryptedStream) {
|
|
170
|
+
if (!(encryptedStream instanceof ReadableStream)) {
|
|
171
|
+
throw new TypeError('encryptedStream is not a ReadableStream');
|
|
172
|
+
}
|
|
173
|
+
const mainKey = await this.mainKeyPromise;
|
|
174
|
+
return decryptStream(encryptedStream, mainKey);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Returns an object containing `ranges`, an array of objects
|
|
178
|
+
* containing `offset` and `length` integers specifying the encrypted byte
|
|
179
|
+
* ranges that are needed to decrypt the client's specified range, and a
|
|
180
|
+
* `decrypt` function.
|
|
181
|
+
*
|
|
182
|
+
* @param {number} offset Integer
|
|
183
|
+
* @param {number} length Integer
|
|
184
|
+
* @param {number} totalEncryptedLength Integer
|
|
185
|
+
* @returns {Promise<{ ranges, decrypt }>}
|
|
186
|
+
*/
|
|
187
|
+
async decryptStreamRange(offset, length, totalEncryptedLength) {
|
|
188
|
+
if (!Number.isInteger(offset)) {
|
|
189
|
+
throw new TypeError('offset');
|
|
190
|
+
}
|
|
191
|
+
if (!Number.isInteger(length)) {
|
|
192
|
+
throw new TypeError('length');
|
|
193
|
+
}
|
|
194
|
+
if (!Number.isInteger(totalEncryptedLength)) {
|
|
195
|
+
throw new TypeError('totalEncryptedLength');
|
|
196
|
+
}
|
|
197
|
+
const mainKey = await this.mainKeyPromise;
|
|
198
|
+
return decryptStreamRange(mainKey, offset, length, totalEncryptedLength);
|
|
199
|
+
}
|
|
200
|
+
async encryptMeta(meta) {
|
|
201
|
+
if (!(meta instanceof Uint8Array)) {
|
|
202
|
+
throw new TypeError('`meta` should be Uint8Array');
|
|
203
|
+
}
|
|
204
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
205
|
+
const metaKey = await this.metaKeyPromise;
|
|
206
|
+
const encryptedMetaBuf = await webcrypto.subtle.encrypt({
|
|
207
|
+
name: 'AES-GCM',
|
|
208
|
+
iv,
|
|
209
|
+
tagLength: 128
|
|
210
|
+
}, metaKey, asBufferSource(meta));
|
|
211
|
+
const encryptedMeta = new Uint8Array(encryptedMetaBuf);
|
|
212
|
+
const ivEncryptedMeta = new Uint8Array(IV_LENGTH + encryptedMeta.byteLength);
|
|
213
|
+
ivEncryptedMeta.set(iv, 0);
|
|
214
|
+
ivEncryptedMeta.set(encryptedMeta, IV_LENGTH);
|
|
215
|
+
return ivEncryptedMeta;
|
|
216
|
+
}
|
|
217
|
+
async decryptMeta(ivEncryptedMeta) {
|
|
218
|
+
if (!(ivEncryptedMeta instanceof Uint8Array)) {
|
|
219
|
+
throw new Error('ivEncryptedMeta');
|
|
220
|
+
}
|
|
221
|
+
const iv = ivEncryptedMeta.slice(0, IV_LENGTH);
|
|
222
|
+
const encryptedMeta = ivEncryptedMeta.slice(IV_LENGTH);
|
|
223
|
+
const metaKey = await this.metaKeyPromise;
|
|
224
|
+
const metaBuf = await webcrypto.subtle.decrypt({
|
|
225
|
+
name: 'AES-GCM',
|
|
226
|
+
iv,
|
|
227
|
+
tagLength: 128
|
|
228
|
+
}, metaKey, encryptedMeta);
|
|
229
|
+
const meta = new Uint8Array(metaBuf);
|
|
230
|
+
return meta;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function arrayToB64(array) {
|
|
234
|
+
return u.toString(array, 'base64pad');
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Return the given Uint8Array as a base64url string.
|
|
238
|
+
* @param array Uint8Array
|
|
239
|
+
* @returns `base64url` encoded string
|
|
240
|
+
*/
|
|
241
|
+
function arrayToB64Url(array) {
|
|
242
|
+
return u.toString(array, 'base64url');
|
|
243
|
+
}
|
|
244
|
+
function b64ToArray(str) {
|
|
245
|
+
// Accept both base64 and base64url input by normalizing url-safe chars.
|
|
246
|
+
return u.fromString(str.replace(/-/g, '+').replace(/_/g, '/'), 'base64');
|
|
247
|
+
}
|
|
248
|
+
function decodeBits(bitsB64) {
|
|
249
|
+
let result;
|
|
250
|
+
if (bitsB64 instanceof Uint8Array) {
|
|
251
|
+
result = asBufferSource(bitsB64);
|
|
252
|
+
}
|
|
253
|
+
else if (typeof bitsB64 === 'string') {
|
|
254
|
+
result = b64ToArray(bitsB64);
|
|
255
|
+
}
|
|
256
|
+
else if (bitsB64 == null) {
|
|
257
|
+
result = webcrypto.getRandomValues(new Uint8Array(16));
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
throw new Error('Must be Uint8Array, string, or nullish');
|
|
261
|
+
}
|
|
262
|
+
if (result.byteLength !== 16) {
|
|
263
|
+
throw new Error('Invalid byteLength: must be 16 bytes');
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
//# sourceMappingURL=keychain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keychain.js","sourceRoot":"","sources":["../../src/keychain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAC3D,OAAO,KAAK,CAAC,MAAM,aAAa,CAAA;AAChC,OAAO,EACH,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,UAAU,GACb,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE/D,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAEvD,MAAM,SAAS,GAAG,EAAE,CAAA;AACpB,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC,MAAM,OAAO,QAAQ;IACjB,GAAG,CAAwB;IAC3B,IAAI,CAAwB;IAC5B,cAAc,CAAmB;IACjC,cAAc,CAAmB;IACjC,gBAAgB,CAAoB;IAEpC,YAAa,GAAsB,EAAE,IAAuB;QACxD,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,CAAA;QAC1B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,CAAA;QAE5B,IAAI,CAAC,cAAc,GAAG,SAAS,CAAC,MAAM,CAAC,SAAS,CAC5C,KAAK,EACL,IAAI,CAAC,GAAG,EACR,MAAM,EACN,KAAK,EACL,CAAC,YAAY,EAAE,WAAW,CAAC,CAC9B,CAAA;QAED,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc;aACpC,IAAI,CAAC,OAAO,CAAC,EAAE,CACZ,SAAS,CAAC,MAAM,CAAC,SAAS,CACtB;YACI,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final chunk may be
|
|
3
|
+
* less than the desired size.
|
|
4
|
+
*
|
|
5
|
+
* firstChunkSize: The size of the first chunk
|
|
6
|
+
* restChunkSize: The size of all subsequent chunks, optional. If omitted, the
|
|
7
|
+
* size of subsequenct chunks will be same as `firstChunkSize`
|
|
8
|
+
*/
|
|
9
|
+
export declare class SliceTransformer {
|
|
10
|
+
chunkSize: number;
|
|
11
|
+
restChunkSize: number;
|
|
12
|
+
partialChunk: Uint8Array;
|
|
13
|
+
offset: number;
|
|
14
|
+
constructor(firstChunkSize: number, restChunkSize?: number);
|
|
15
|
+
send(record: Uint8Array, controller: TransformStreamDefaultController): void;
|
|
16
|
+
transform(chunk: Uint8Array, controller: TransformStreamDefaultController): void;
|
|
17
|
+
flush(controller: TransformStreamDefaultController): void;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=slice-transformer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slice-transformer.d.ts","sourceRoot":"","sources":["../../src/slice-transformer.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,qBAAa,gBAAgB;IACzB,SAAS,EAAC,MAAM,CAAA;IAChB,aAAa,EAAC,MAAM,CAAA;IACpB,YAAY,EAAC,UAAU,CAAA;IACvB,MAAM,EAAC,MAAM,CAAA;gBAEA,cAAc,EAAC,MAAM,EAAE,aAAa,CAAC,EAAC,MAAM;IAQzD,IAAI,CAAE,MAAM,EAAC,UAAU,EAAE,UAAU,EAAC,gCAAgC,GAAE,IAAI;IAO1E,SAAS,CACL,KAAK,EAAC,UAAU,EAChB,UAAU,EAAC,gCAAgC,GAC7C,IAAI;IA6BN,KAAK,CAAE,UAAU,EAAC,gCAAgC,GAAE,IAAI;CAK3D"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tranform stream that outputs chunks of a specific size. Final chunk may be
|
|
3
|
+
* less than the desired size.
|
|
4
|
+
*
|
|
5
|
+
* firstChunkSize: The size of the first chunk
|
|
6
|
+
* restChunkSize: The size of all subsequent chunks, optional. If omitted, the
|
|
7
|
+
* size of subsequenct chunks will be same as `firstChunkSize`
|
|
8
|
+
*/
|
|
9
|
+
export class SliceTransformer {
|
|
10
|
+
chunkSize;
|
|
11
|
+
restChunkSize;
|
|
12
|
+
partialChunk;
|
|
13
|
+
offset;
|
|
14
|
+
constructor(firstChunkSize, restChunkSize) {
|
|
15
|
+
this.chunkSize = firstChunkSize;
|
|
16
|
+
this.restChunkSize = restChunkSize || firstChunkSize;
|
|
17
|
+
this.partialChunk = new Uint8Array(this.chunkSize);
|
|
18
|
+
this.offset = 0; // offset into `partialChunk`
|
|
19
|
+
}
|
|
20
|
+
send(record, controller) {
|
|
21
|
+
controller.enqueue(record);
|
|
22
|
+
this.chunkSize = this.restChunkSize;
|
|
23
|
+
this.partialChunk = new Uint8Array(this.chunkSize);
|
|
24
|
+
this.offset = 0;
|
|
25
|
+
}
|
|
26
|
+
transform(chunk, controller) {
|
|
27
|
+
let i = 0; // offset into `chunk`
|
|
28
|
+
if (this.offset > 0) {
|
|
29
|
+
const len = Math.min(chunk.byteLength, this.chunkSize - this.offset);
|
|
30
|
+
this.partialChunk.set(chunk.subarray(0, len), this.offset);
|
|
31
|
+
this.offset += len;
|
|
32
|
+
i += len;
|
|
33
|
+
if (this.offset === this.chunkSize) {
|
|
34
|
+
this.send(this.partialChunk, controller);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
while (i < chunk.byteLength) {
|
|
38
|
+
const remainingBytes = chunk.byteLength - i;
|
|
39
|
+
if (remainingBytes >= this.chunkSize) {
|
|
40
|
+
const record = chunk.slice(i, i + this.chunkSize);
|
|
41
|
+
i += this.chunkSize;
|
|
42
|
+
this.send(record, controller);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const end = chunk.slice(i, i + remainingBytes);
|
|
46
|
+
i += end.byteLength;
|
|
47
|
+
this.partialChunk.set(end);
|
|
48
|
+
this.offset = end.byteLength;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
flush(controller) {
|
|
53
|
+
if (this.offset > 0) {
|
|
54
|
+
controller.enqueue(this.partialChunk.subarray(0, this.offset));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=slice-transformer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slice-transformer.js","sourceRoot":"","sources":["../../src/slice-transformer.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,OAAO,gBAAgB;IACzB,SAAS,CAAO;IAChB,aAAa,CAAO;IACpB,YAAY,CAAW;IACvB,MAAM,CAAO;IAEb,YAAa,cAAqB,EAAE,aAAqB;QACrD,IAAI,CAAC,SAAS,GAAG,cAAc,CAAA;QAC/B,IAAI,CAAC,aAAa,GAAG,aAAa,IAAI,cAAc,CAAA;QAEpD,IAAI,CAAC,YAAY,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAClD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA,CAAE,6BAA6B;IAClD,CAAC;IAED,IAAI,CAAE,MAAiB,EAAE,UAA2C;QAChE,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,aAAa,CAAA;QACnC,IAAI,CAAC,YAAY,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAClD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA;IACnB,CAAC;IAED,SAAS,CACL,KAAgB,EAChB,UAA2C;QAE3C,IAAI,CAAC,GAAG,CAAC,CAAA,CAAE,sBAAsB;QAEjC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,GA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C,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAA;QAClE,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pipe a readable stream through a transformer. Returns a result, where
|
|
3
|
+
* result.readable is the readable end of the TransformStream and
|
|
4
|
+
* result.done is a promise that fulfills or rejects once the stream is done.
|
|
5
|
+
* Includes a shim for environments where TransformStream is not available.
|
|
6
|
+
*/
|
|
7
|
+
export declare function transformStream(sourceReadable: ReadableStream, transformer: Transformer): {
|
|
8
|
+
readable: ReadableStream;
|
|
9
|
+
done: Promise<unknown>;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=transform-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform-stream.d.ts","sourceRoot":"","sources":["../../src/transform-stream.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,eAAe,CAC3B,cAAc,EAAC,cAAc,EAC7B,WAAW,EAAC,WAAW,GACzB;IACE,QAAQ,EAAC,cAAc,CAAC;IACxB,IAAI,EAAC,OAAO,CAAC,OAAO,CAAC,CAAA;CACxB,CAyCA"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pipe a readable stream through a transformer. Returns a result, where
|
|
3
|
+
* result.readable is the readable end of the TransformStream and
|
|
4
|
+
* result.done is a promise that fulfills or rejects once the stream is done.
|
|
5
|
+
* Includes a shim for environments where TransformStream is not available.
|
|
6
|
+
*/
|
|
7
|
+
export function transformStream(sourceReadable, transformer) {
|
|
8
|
+
let transformedReadable;
|
|
9
|
+
let done;
|
|
10
|
+
if (typeof TransformStream !== 'undefined') {
|
|
11
|
+
// Chrome, Edge, Safari 14.1+
|
|
12
|
+
const transform = new TransformStream(transformer);
|
|
13
|
+
// .pipeTo
|
|
14
|
+
// returns a Promise that fulfills when the piping process
|
|
15
|
+
// completes successfully
|
|
16
|
+
done = sourceReadable.pipeTo(transform.writable);
|
|
17
|
+
transformedReadable = transform.readable;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// Firefox, Safari 14 and older
|
|
21
|
+
let resolveDone;
|
|
22
|
+
let rejectDone;
|
|
23
|
+
done = new Promise((resolve, reject) => {
|
|
24
|
+
resolveDone = resolve;
|
|
25
|
+
rejectDone = reject;
|
|
26
|
+
});
|
|
27
|
+
const transformSource = new TransformStreamSource(sourceReadable, transformer, {
|
|
28
|
+
resolveDone,
|
|
29
|
+
rejectDone
|
|
30
|
+
});
|
|
31
|
+
transformedReadable = new ReadableStream(transformSource);
|
|
32
|
+
}
|
|
33
|
+
// Ensure the caller doesn't need to catch errors
|
|
34
|
+
done.catch(() => { });
|
|
35
|
+
return {
|
|
36
|
+
readable: transformedReadable,
|
|
37
|
+
done
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
class TransformStreamSource {
|
|
41
|
+
readable;
|
|
42
|
+
transformer;
|
|
43
|
+
resolveDone;
|
|
44
|
+
rejectDone;
|
|
45
|
+
reader;
|
|
46
|
+
progressMade;
|
|
47
|
+
wrappedController;
|
|
48
|
+
type = 'bytes';
|
|
49
|
+
constructor(readable, transformer, { resolveDone, rejectDone }) {
|
|
50
|
+
this.readable = readable;
|
|
51
|
+
this.transformer = transformer;
|
|
52
|
+
this.resolveDone = resolveDone;
|
|
53
|
+
this.rejectDone = rejectDone;
|
|
54
|
+
this.reader = readable.getReader();
|
|
55
|
+
this.progressMade = false; // reset on each pull
|
|
56
|
+
this.wrappedController = null;
|
|
57
|
+
}
|
|
58
|
+
// async start (controller:ReadableStreamController<T>) {
|
|
59
|
+
async start(controller) {
|
|
60
|
+
this.wrappedController = {
|
|
61
|
+
desiredSize: controller.desiredSize,
|
|
62
|
+
enqueue: (value) => {
|
|
63
|
+
this.progressMade = true;
|
|
64
|
+
controller.enqueue(value);
|
|
65
|
+
},
|
|
66
|
+
error: (reason) => {
|
|
67
|
+
this.progressMade = true;
|
|
68
|
+
if (!(reason instanceof Error)) {
|
|
69
|
+
reason = new Error(`stream errored; reason: ${reason}`);
|
|
70
|
+
}
|
|
71
|
+
controller.error(reason);
|
|
72
|
+
this.reader.cancel(reason).catch(() => { });
|
|
73
|
+
this.rejectDone(reason);
|
|
74
|
+
},
|
|
75
|
+
terminate: () => {
|
|
76
|
+
this.progressMade = true;
|
|
77
|
+
controller.close();
|
|
78
|
+
this.reader.cancel(new Error('stream terminated')).catch(() => { });
|
|
79
|
+
this.resolveDone();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
if (this.transformer.start) {
|
|
83
|
+
try {
|
|
84
|
+
await this.transformer.start(this.wrappedController);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
this.rejectDone(err);
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async pull(controller) {
|
|
93
|
+
this.progressMade = false;
|
|
94
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
95
|
+
while (!this.progressMade) {
|
|
96
|
+
try {
|
|
97
|
+
const data = await this.reader.read();
|
|
98
|
+
if (!this.wrappedController) {
|
|
99
|
+
throw new Error('not this.wrappedController');
|
|
100
|
+
}
|
|
101
|
+
if (data.done) {
|
|
102
|
+
if (this.transformer.flush) {
|
|
103
|
+
await this.transformer.flush(this.wrappedController);
|
|
104
|
+
}
|
|
105
|
+
controller.close();
|
|
106
|
+
this.resolveDone();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (this.transformer.transform) {
|
|
110
|
+
await this.transformer.transform(data.value, this.wrappedController);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
if (!this.wrappedController) {
|
|
114
|
+
throw new Error('not wrapped controller');
|
|
115
|
+
}
|
|
116
|
+
this.wrappedController.enqueue(data.value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
this.rejectDone(err);
|
|
121
|
+
this.reader.cancel(err).catch(() => { });
|
|
122
|
+
throw err;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async cancel(reason) {
|
|
127
|
+
await this.reader.cancel(reason);
|
|
128
|
+
if (reason instanceof Error) {
|
|
129
|
+
this.rejectDone(reason);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
this.rejectDone(new Error(`stream cancelled; reason: ${reason}`));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=transform-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform-stream.js","sourceRoot":"","sources":["../../src/transform-stream.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAC3B,cAA6B,EAC7B,WAAuB;IAKvB,IAAI,mBAA8C,CAAA;IAClD,IAAI,IAAkB,CAAA;IAEtB,IAAI,OAAO,eAAe,KAAK,WAAW,EAAE,CAAC;QACzC,6BAA6B;QAC7B,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,WAAW,CAAC,CAAA;QAElD,UAAU;QACV,2DAA2D;QAC3D,0BAA0B;QAC1B,IAAI,GAAG,cAAc,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;QAChD,mBAAmB,GAAG,SAAS,CAAC,QAAQ,CAAA;IAC5C,CAAC;SAAM,CAAC;QACJ,+BAA+B;QAC/B,IAAI,WAAW,CAAA;QACf,IAAI,UAAU,CAAA;QACd,IAAI,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACnC,WAAW,GAAG,OAAO,CAAA;YACrB,UAAU,GAAG,MAAM,CAAA;QACvB,CAAC,CAAC,CAAA;QAEF,MAAM,eAAe,GAAG,IAAI,qBAAqB,CAC7C,cAAc,EACd,WAAW,EACX;YACI,WAAW;YACX,UAAU;SACb,CACJ,CAAA;QAED,mBAAmB,GAAG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E,CAAC,CAAC,CAAA;QACrE,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare function generateSalt(len: number): Uint8Array<ArrayBuffer>;
|
|
2
|
+
/**
|
|
3
|
+
* Return a `Uint8Array` of the given length filled with random bytes.
|
|
4
|
+
* @param length Number of random bytes to return
|
|
5
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
6
|
+
*/
|
|
7
|
+
export declare function randomBuf(length: number): Uint8Array<ArrayBuffer>;
|
|
8
|
+
/**
|
|
9
|
+
* Concatenate two buffers into a single `Uint8Array`.
|
|
10
|
+
* @param fst The first buffer
|
|
11
|
+
* @param snd The second buffer
|
|
12
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
13
|
+
*/
|
|
14
|
+
export declare function joinBufs(fst: ArrayBuffer | Uint8Array, snd: ArrayBuffer | Uint8Array): Uint8Array<ArrayBuffer>;
|
|
15
|
+
/**
|
|
16
|
+
* Return a `Uint8Array` backed by a plain `ArrayBuffer`.
|
|
17
|
+
*
|
|
18
|
+
* The WebCrypto and `Blob` APIs require a `BufferSource` backed by an
|
|
19
|
+
* `ArrayBuffer`, but the default `Uint8Array` type is backed by
|
|
20
|
+
* `ArrayBufferLike` (which includes `SharedArrayBuffer`). Copy only when
|
|
21
|
+
* the backing buffer is not already a plain `ArrayBuffer`.
|
|
22
|
+
*
|
|
23
|
+
* @param data The buffer to coerce
|
|
24
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
25
|
+
*/
|
|
26
|
+
export declare function asBufferSource(data: ArrayBuffer | Uint8Array): Uint8Array<ArrayBuffer>;
|
|
27
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAE,GAAG,EAAC,MAAM,GAAE,UAAU,CAAC,WAAW,CAAC,CAIhE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAE,MAAM,EAAC,MAAM,GAAE,UAAU,CAAC,WAAW,CAAC,CAEhE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CACpB,GAAG,EAAC,WAAW,GAAC,UAAU,EAC1B,GAAG,EAAC,WAAW,GAAC,UAAU,GAC5B,UAAU,CAAC,WAAW,CAAC,CAOxB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAC,WAAW,GAAC,UAAU,GAC7B,UAAU,CAAC,WAAW,CAAC,CAQxB"}
|
package/dist/src/util.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { webcrypto } from '@substrate-system/one-webcrypto';
|
|
2
|
+
export function generateSalt(len) {
|
|
3
|
+
const salt = new Uint8Array(len);
|
|
4
|
+
webcrypto.getRandomValues(salt);
|
|
5
|
+
return salt;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Return a `Uint8Array` of the given length filled with random bytes.
|
|
9
|
+
* @param length Number of random bytes to return
|
|
10
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
11
|
+
*/
|
|
12
|
+
export function randomBuf(length) {
|
|
13
|
+
return webcrypto.getRandomValues(new Uint8Array(length));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Concatenate two buffers into a single `Uint8Array`.
|
|
17
|
+
* @param fst The first buffer
|
|
18
|
+
* @param snd The second buffer
|
|
19
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
20
|
+
*/
|
|
21
|
+
export function joinBufs(fst, snd) {
|
|
22
|
+
const view1 = new Uint8Array(fst);
|
|
23
|
+
const view2 = new Uint8Array(snd);
|
|
24
|
+
const joined = new Uint8Array(view1.length + view2.length);
|
|
25
|
+
joined.set(view1);
|
|
26
|
+
joined.set(view2, view1.length);
|
|
27
|
+
return joined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Return a `Uint8Array` backed by a plain `ArrayBuffer`.
|
|
31
|
+
*
|
|
32
|
+
* The WebCrypto and `Blob` APIs require a `BufferSource` backed by an
|
|
33
|
+
* `ArrayBuffer`, but the default `Uint8Array` type is backed by
|
|
34
|
+
* `ArrayBufferLike` (which includes `SharedArrayBuffer`). Copy only when
|
|
35
|
+
* the backing buffer is not already a plain `ArrayBuffer`.
|
|
36
|
+
*
|
|
37
|
+
* @param data The buffer to coerce
|
|
38
|
+
* @returns {Uint8Array<ArrayBuffer>}
|
|
39
|
+
*/
|
|
40
|
+
export function asBufferSource(data) {
|
|
41
|
+
if (data instanceof ArrayBuffer) {
|
|
42
|
+
return new Uint8Array(data);
|
|
43
|
+
}
|
|
44
|
+
if (data.buffer instanceof ArrayBuffer) {
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
return new Uint8Array(data);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAE3D,MAAM,UAAU,YAAY,CAAE,GAAU;IACpC,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IAChC,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;IAC/B,OAAO,IAAI,CAAA;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAE,MAAa;IACpC,OAAO,SAAS,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CACpB,GAA0B,EAC1B,GAA0B;IAE1B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IACjC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IACjC,MAAM,MAAM,GAA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