/
index.ts
588 lines (533 loc) · 18.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import {
Actor,
ActorConfig,
ActorSubclass,
Cbor as cbor,
Certificate,
compare,
getDefaultAgent,
HashTree,
lookup_path,
lookupResultToBuffer,
reconstruct,
uint8ToBuf,
} from '@dfinity/agent';
import { lebDecode } from '@dfinity/candid';
import { PipeArrayBuffer } from '@dfinity/candid/lib/cjs/utils/buffer';
import { AssetsCanisterRecord, getAssetsCanister } from './canisters/assets';
import { sha256 } from '@noble/hashes/sha256';
import { BatchOperationKind } from './canisters/assets_service';
import * as base64Arraybuffer from 'base64-arraybuffer';
import { isReadable, Readable } from './readable/readable';
import { ReadableFile } from './readable/readableFile';
import { ReadableBlob } from './readable/readableBlob';
import { ReadablePath } from './readable/readablePath';
import { ReadableBytes } from './readable/readableBytes';
import { limit, LimitFn } from './utils/limit';
import fs from 'fs';
/**
* Supported content encodings by asset canister
*/
export type ContentEncoding = 'identity' | 'gzip' | 'compress' | 'deflate' | 'br';
/**
* Upload progress in bytes
*/
export interface Progress {
current: number;
total: number;
}
/**
* Configuration that can be passed to set and override defaults and add progress callback
*/
export interface StoreConfig {
/**
* File name
* @default File object name or name in file path
*/
fileName?: string;
/**
* File path that file will be uploaded to
* @default '/'
*/
path?: string;
/**
* File content type
* @default File/Blob object type or type from file name extension
*/
contentType?: string;
/**
* Content encoding
* @default 'identity'
*/
contentEncoding?: ContentEncoding;
/**
* File hash generation will be skipped if hash is provided
*/
sha256?: Uint8Array;
/**
* Callback method to get upload progress in bytes (current / total)
*/
onProgress?: (progress: Progress) => void;
}
export type StoreReadableArgs = [readable: Readable, config?: StoreConfig];
export type StoreFileArgs = [file: File, config?: StoreConfig];
export type StoreBlobArgs = [
blob: Blob,
config: Omit<StoreConfig, 'fileName'> & Required<Pick<StoreConfig, 'fileName'>>,
];
export type StorePathArgs = [path: string, config?: StoreConfig];
export type StoreBytesArgs = [
bytes: Uint8Array | ArrayBuffer | number[],
config: Omit<StoreConfig, 'fileName'> & Required<Pick<StoreConfig, 'fileName'>>,
];
/**
* Arguments to store an asset in asset manager
*/
export type StoreArgs =
| StoreReadableArgs
| StoreFileArgs
| StoreBlobArgs
| StorePathArgs
| StoreBytesArgs;
/**
* Arguments to commit batch in asset manager
*/
export interface CommitBatchArgs {
onProgress?: (progress: Progress) => void;
}
/**
* Configuration that can be passed to set the canister id of the
* assets canister to be managed, inherits actor configuration and
* has additional asset manager specific configuration options.
*/
export interface AssetManagerConfig extends ActorConfig {
/**
* Max number of concurrent requests to the Internet Computer
* @default 16
*/
concurrency?: number;
/**
* Max file size in bytes that the asset manager shouldn't chunk
* @default 1900000
*/
maxSingleFileSize?: number;
/**
* Size of each chunk in bytes when the asset manager has to chunk a file
* @default 1900000
*/
maxChunkSize?: number;
}
export class AssetManager {
private readonly _actor: ActorSubclass<AssetsCanisterRecord>;
private readonly _limit: LimitFn;
private readonly _maxSingleFileSize: number;
private readonly _maxChunkSize: number;
/**
* Create assets canister manager instance
* @param config Additional configuration options, canister id is required
*/
constructor(config: AssetManagerConfig) {
const { concurrency, maxSingleFileSize, maxChunkSize, ...actorConfig } = config;
this._actor = getAssetsCanister(actorConfig);
this._limit = limit(concurrency ?? 16);
this._maxSingleFileSize = maxSingleFileSize ?? 1900000;
this._maxChunkSize = maxChunkSize ?? 1900000;
}
/**
* Create readable from store arguments
* @param args Arguments with either a file, blob, path, bytes or custom Readable implementation
*/
static async toReadable(...args: StoreArgs): Promise<Readable> {
if (typeof File === 'function' && args[0] instanceof File) {
return new ReadableFile(args[0]);
}
if (typeof Blob === 'function' && args[0] instanceof Blob && args[1]?.fileName) {
return new ReadableBlob(args[1].fileName, args[0]);
}
if (typeof args[0] === 'string') {
return await ReadablePath.create(args[0]);
}
if (
(Array.isArray(args[0]) || args[0] instanceof Uint8Array || args[0] instanceof ArrayBuffer) &&
args[1]?.fileName
) {
return new ReadableBytes(args[1].fileName, args[0]);
}
if (isReadable(args[0])) {
return args[0];
}
throw new Error('Invalid arguments, readable could not be created');
}
/**
* Get list of all files in assets canister
* @returns All files in asset canister
*/
public async list(): ReturnType<AssetsCanisterRecord['list']> {
return this._actor.list({});
}
/**
* Store data on assets canister
* @param args Arguments with either a file, blob, path, bytes or custom Readable implementation
*/
public async store(...args: StoreArgs): Promise<string> {
const readable = await AssetManager.toReadable(...args);
const [, config] = args;
const key = [config?.path ?? '', config?.fileName ?? readable.fileName].join('/');
// If asset is small enough upload in one request else upload in chunks (batch)
if (readable.length <= this._maxSingleFileSize) {
config?.onProgress?.({ current: 0, total: readable.length });
await this._limit(async () => {
await readable.open();
const bytes = await readable.slice(0, readable.length);
await readable.close();
const hash = config?.sha256 ?? sha256.create().update(new Uint8Array(bytes)).digest();
return this._actor.store({
key,
content: bytes,
content_type: readable.contentType,
sha256: [hash],
content_encoding: config?.contentEncoding ?? 'identity',
});
});
config?.onProgress?.({ current: readable.length, total: readable.length });
} else {
// Create batch to upload asset in chunks
const batch = this.batch();
await batch.store(readable, config);
await batch.commit();
}
return key;
}
/**
* Delete file from assets canister
* @param key The path to the file on the assets canister e.g. /folder/to/my_file.txt
*/
public async delete(key: string): Promise<void> {
await this._actor.delete_asset({ key });
}
/**
* Delete all files from assets canister
*/
public async clear(): Promise<void> {
await this._actor.clear({});
}
/**
* Get asset instance from assets canister
* @param key The path to the file on the assets canister e.g. /folder/to/my_file.txt
* @param acceptEncodings The accepted content encodings, defaults to ['identity']
*/
public async get(key: string, acceptEncodings?: ContentEncoding[]): Promise<Asset> {
const data = await this._actor.get({
key,
accept_encodings: acceptEncodings ?? ['identity'],
});
return new Asset(
this._actor,
this._limit,
this._maxSingleFileSize,
this._maxChunkSize,
key,
acceptEncodings ?? ['identity'],
data.content,
data.content_type,
Number(data.total_length),
data.content_encoding,
data.content.length,
data.sha256[0],
);
}
/**
* Create a batch assets operations instance, commit multiple operations in a single request
*/
public batch(): AssetManagerBatch {
return new AssetManagerBatch(this._actor, this._limit, this._maxChunkSize);
}
}
// Required since the sha256 type is not exported
const hasher = sha256.create();
type SHA256TYPE = typeof hasher;
class AssetManagerBatch {
private _scheduledOperations: Array<
(batch_id: bigint, onProgress?: (progress: Progress) => void) => Promise<BatchOperationKind[]>
> = [];
private _sha256: { [key: string]: SHA256TYPE } = {};
private _progress: { [key: string]: Progress } = {};
constructor(
private readonly _actor: ActorSubclass<AssetsCanisterRecord>,
private readonly _limit: LimitFn,
private readonly _maxChunkSize: number,
) {}
/**
* Insert batch operation to store data on assets canister
* @param args Arguments with either a file, blob, path, bytes or custom Readable implementation
*/
public async store(...args: StoreArgs): Promise<string> {
const readable = await AssetManager.toReadable(...args);
const [, config] = args;
const key = [config?.path ?? '', config?.fileName ?? readable.fileName].join('/');
if (!config?.sha256) {
this._sha256[key] = sha256.create();
}
this._progress[key] = { current: 0, total: readable.length };
config?.onProgress?.(this._progress[key]);
this._scheduledOperations.push(async (batch_id, onProgress) => {
await readable.open();
const chunkCount = Math.ceil(readable.length / this._maxChunkSize);
const chunkIds: bigint[] = await Promise.all(
Array.from({ length: chunkCount }).map(async (_, index) => {
const content = await readable.slice(
index * this._maxChunkSize,
Math.min((index + 1) * this._maxChunkSize, readable.length),
);
if (!config?.sha256) {
this._sha256[key].update(content);
}
const { chunk_id } = await this._limit(() =>
this._actor.create_chunk({
content,
batch_id,
}),
);
this._progress[key].current += content.length;
config?.onProgress?.(this._progress[key]);
onProgress?.({
current: Object.values(this._progress).reduce((acc, val) => acc + val.current, 0),
total: Object.values(this._progress).reduce((acc, val) => acc + val.total, 0),
});
return chunk_id;
}),
);
await readable.close();
return [
{
CreateAsset: { key, content_type: config?.contentType ?? readable.contentType },
},
{
SetAssetContent: {
key,
sha256: [config?.sha256 ?? new Uint8Array(this._sha256[key].digest())],
chunk_ids: chunkIds,
content_encoding: config?.contentEncoding ?? 'identity',
},
},
];
});
return key;
}
/**
* Insert batch operation to delete file from assets canister
* @param key The path to the file on the assets canister e.g. /folder/to/my_file.txt
*/
public delete(key: string): void {
this._scheduledOperations.push(async () => [{ DeleteAsset: { key } }]);
}
/**
* Commit all batch operations to assets canister
* @param args Optional arguments with optional progress callback for commit progress
*/
public async commit(args?: CommitBatchArgs): Promise<void> {
// Create batch
const { batch_id } = await this._limit(() => this._actor.create_batch({}));
// Progress callback
args?.onProgress?.({
current: Object.values(this._progress).reduce((acc, val) => acc + val.current, 0),
total: Object.values(this._progress).reduce((acc, val) => acc + val.total, 0),
});
// Execute scheduled operations
const operations = (
await Promise.all(
this._scheduledOperations.map(scheduled_operation =>
scheduled_operation(batch_id, args?.onProgress),
),
)
).flat();
// Commit batch
await this._limit(() => this._actor.commit_batch({ batch_id, operations }));
// Cleanup
this._scheduledOperations = [];
this._sha256 = {};
this._progress = {};
}
}
class Asset {
constructor(
private readonly _actor: ActorSubclass<AssetsCanisterRecord>,
private readonly _limit: LimitFn,
private readonly _maxSingleFileSize: number,
private readonly _maxChunkSize: number,
private readonly _key: string,
private readonly _acceptEncodings: ContentEncoding[],
private readonly _content: Uint8Array,
public readonly contentType: string,
public readonly length: number,
public readonly contentEncoding: string,
public readonly chunkSize: number,
public readonly sha256?: Uint8Array,
) {}
/**
* Get asset content as blob (web), most browsers are able to use disk storage for larger blobs
*/
public async toBlob(): Promise<Blob> {
const blobs = Array.from<Blob>({ length: Math.ceil(this.length / this.chunkSize) });
await this.getChunks((index, chunk) => (blobs[index] = new Blob([chunk])));
return new Blob([...blobs]);
}
/**
* Get asset content as unsigned 8-bit integer array, use `toBlob` (web) or `write` (Node.js) for larger files
*/
public async toUint8Array(): Promise<Uint8Array> {
const bytes = new Uint8Array(this.length);
await this.getChunks((index, chunk) => bytes.set(chunk, index * this.chunkSize));
return bytes;
}
/**
* Get asset content as number array, use `toBlob` (web) or `write` (Node.js) for larger files
*/
public async toNumberArray(): Promise<number[]> {
const chunks = Array.from<number[]>({ length: Math.ceil(this.length / this.chunkSize) });
await this.getChunks((index, chunk) => (chunks[index] = Array.from(chunk)));
return chunks.flat();
}
/**
* Write asset content to file (Node.js)
* @param path File path to write to
*/
public async write(path: string): Promise<void> {
const fd = await new Promise<number>((resolve, reject) =>
fs.open(path, 'w', (err: unknown, fd: number) => {
if (err) {
reject(err);
return;
}
resolve(fd);
}),
);
await this.getChunks(
(index, chunk) =>
new Promise<void>((resolve, reject) =>
fs.write(fd, chunk, 0, chunk.length, index * this.chunkSize, (err: unknown) => {
if (err) {
reject(err);
return;
}
resolve();
}),
),
);
await new Promise<void>(resolve => fs.close(fd, () => resolve()));
}
/**
* Get All chunks of asset through `onChunk` callback, can be used for a custom storage implementation
* @param onChunk Called on each received chunk
* @param sequential Chunks are received in sequential order when true or `concurrency` is `1` in config
*/
public async getChunks(
onChunk: (index: number, chunk: Uint8Array) => void,
sequential?: boolean,
) {
onChunk(0, this._content);
const chunkLimit = sequential ? limit(1) : this._limit;
await Promise.all(
Array.from({ length: Math.ceil(this.length / this.chunkSize) - 1 }).map((_, index) =>
chunkLimit(async () => {
const { content } = await this._actor.get_chunk({
key: this._key,
content_encoding: this.contentEncoding,
index: BigInt(index + 1),
sha256: this.sha256 ? [this.sha256] : [],
});
onChunk(index + 1, content);
}),
),
);
}
/**
* Check if asset has been certified, which means that the content's hash is in the canister hash tree
*/
public async isCertified(): Promise<boolean> {
// Below implementation is based on Internet Computer service worker
const agent = Actor.agentOf(this._actor) ?? getDefaultAgent();
const canisterId = Actor.canisterIdOf(this._actor);
if (!agent.rootKey) {
throw Error('Agent is missing root key');
}
const response = await this._limit(() =>
this._actor.http_request({
method: 'get',
url: this._key,
headers: [['Accept-Encoding', this._acceptEncodings.join(', ')]],
body: new Uint8Array(),
}),
);
let certificate: ArrayBuffer | undefined;
let tree: ArrayBuffer | undefined;
const certificateHeader = response.headers.find(
([key]) => key.trim().toLowerCase() === 'ic-certificate',
);
if (!certificateHeader) {
return false;
}
const fields = certificateHeader[1].split(/,/);
for (const f of fields) {
const [, name, b64Value] = [...(f.match(/^(.*)=:(.*):$/) ?? [])].map(x => x.trim());
const value = base64Arraybuffer.decode(b64Value);
if (name === 'certificate') {
certificate = value;
} else if (name === 'tree') {
tree = value;
}
}
if (!certificate || !tree) {
// No certificate or tree in response header
return false;
}
const cert = await Certificate.create({
certificate: new Uint8Array(certificate),
rootKey: agent.rootKey,
canisterId,
}).catch(() => Promise.resolve());
if (!cert) {
// Certificate is not valid
return false;
}
// Check certificate time
const decodedTime = lebDecode(new PipeArrayBuffer(cert.lookup(['time'])));
const certTime = Number(decodedTime / BigInt(1_000_000)); // Convert from nanos to millis
const now = Date.now();
const maxCertTimeOffset = 300_000; // 5 min
if (certTime - maxCertTimeOffset > now || certTime + maxCertTimeOffset < now) {
return false;
}
const hashTree: HashTree = cbor.decode(new Uint8Array(tree));
const reconstructed = await reconstruct(hashTree);
const witness = cert.lookup(['canister', canisterId.toUint8Array(), 'certified_data']);
if (!witness) {
// Could not find certified data for this canister in the certificate
return false;
}
// First validate that the Tree is as good as the certification
if (compare(witness, reconstructed) !== 0) {
// Witness != Tree passed in ic-certification
return false;
}
// Lookup hash of asset in tree
const treeSha = lookupResultToBuffer(lookup_path(['http_assets', this._key], hashTree));
return !!treeSha && !!this.sha256 && compare(this.sha256.buffer, treeSha) === 0;
}
/**
* Check if the hash of the asset data is equal to the hash that has been certified
* @param bytes Optionally pass data to hash instead of waiting for asset data to be fetched and hashed
*/
public async verifySha256(bytes?: Uint8Array | number[]): Promise<boolean> {
if (!this.sha256?.buffer) {
return false;
}
const hash = sha256.create();
if (bytes) {
hash.update(Array.isArray(bytes) ? new Uint8Array(bytes) : bytes);
} else {
await this.getChunks((_, chunk) => hash.update(chunk), true);
}
return compare(uint8ToBuf(this.sha256), uint8ToBuf(hash.digest())) === 0;
}
}