-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
71 lines (59 loc) · 1.95 KB
/
generate.js
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
import { car } from '@helia/car';
import { unixfs } from '@helia/unixfs';
import { CarWriter } from '@ipld/car';
import { createHelia } from 'helia';
import fs from 'node:fs/promises';
import path from 'node:path';
async function carWriterOutToBuffer(out) {
const parts = [];
for await (const part of out) {
parts.push(part);
}
return Buffer.concat(parts);
}
async function getFilesRecursive(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(
entries.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
return entry.isDirectory() ? await getFilesRecursive(fullPath) : fullPath;
})
);
return files.flat();
}
export async function generate(srcDir, dstDir) {
if (!srcDir || !dstDir) {
throw new Error('Missing arguments. Usage: node generateCarBlob.js <srcDir> <dstDir>');
}
const allFiles = await getFilesRecursive(srcDir);
if (!allFiles.some((file) => file.endsWith('index.html'))) {
throw new Error('Directory must contain an "index.html" file');
}
const inputFiles = await Promise.all(
allFiles.map(async (file) => {
return {
path: file,
content: new Uint8Array(await fs.readFile(file)),
}
})
);
const helia = await createHelia({ start: false });
const heliaUnixfs = unixfs(helia);
let rootCID = null;
for await (const entry of heliaUnixfs.addAll(inputFiles)) {
rootCID = entry.cid;
}
if (!rootCID) {
throw new Error('Failed to generate rootCID from files');
}
const c = car(helia);
const { writer, out } = await CarWriter.create(rootCID);
const carBufferPromise = carWriterOutToBuffer(out);
await c.export(rootCID, writer);
const carBlob = await carBufferPromise;
const fileName = `${rootCID.toString()}.car`
const filePath = path.resolve(dstDir, fileName);
await fs.rm(filePath, { recursive: true, force: true });
await fs.writeFile(filePath, carBlob);
return fileName;
}