@ethersphere/bee-js
Version:
Javascript client for Bee
97 lines (96 loc) • 4.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.streamFiles = exports.streamDirectory = exports.hashDirectory = void 0;
const cafe_utility_1 = require("cafe-utility");
const fs_1 = require("fs");
const __1 = require("..");
const manifest_1 = require("../manifest/manifest");
const chunk_size_1 = require("./chunk-size");
const collection_node_1 = require("./collection.node");
const mime_1 = require("./mime");
const typed_bytes_1 = require("./typed-bytes");
async function hashDirectory(dir) {
const files = await (0, collection_node_1.makeCollectionFromFS)(dir);
const mantaray = new manifest_1.MantarayNode();
for (const file of files) {
const tree = new cafe_utility_1.MerkleTree(cafe_utility_1.MerkleTree.NOOP);
if (!file.fsPath) {
throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
}
const readStream = (0, fs_1.createReadStream)(file.fsPath);
for await (const data of readStream) {
await tree.append(data);
}
const rootChunk = await tree.finalize();
const { filename, extension } = cafe_utility_1.Strings.parseFilename(file.path);
mantaray.addFork(file.path, rootChunk.hash(), {
'Content-Type': maybeEnrichMime(mime_1.mimes[extension.toLowerCase()] || 'application/octet-stream'),
Filename: filename,
});
}
return mantaray.calculateSelfAddress();
}
exports.hashDirectory = hashDirectory;
async function streamDirectory(bee, dir, postageBatchId, onUploadProgress, options, requestOptions) {
const queue = new cafe_utility_1.AsyncQueue(64, 64);
let total = 0;
let processed = 0;
postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
const files = await (0, collection_node_1.makeCollectionFromFS)(dir);
for (const file of files) {
total += (0, chunk_size_1.totalChunks)(file.size);
}
let hasIndexHtml = false;
async function onChunk(chunk) {
await queue.enqueue(async () => {
await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
onUploadProgress?.({ total, processed: ++processed });
});
}
const mantaray = new manifest_1.MantarayNode();
for (const file of files) {
if (!file.fsPath) {
throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
}
const readStream = (0, fs_1.createReadStream)(file.fsPath);
const tree = new cafe_utility_1.MerkleTree(onChunk);
for await (const data of readStream) {
await tree.append(data);
}
const rootChunk = await tree.finalize();
await queue.drain();
const { filename, extension } = cafe_utility_1.Strings.parseFilename(file.path);
mantaray.addFork(file.path, rootChunk.hash(), {
'Content-Type': maybeEnrichMime(mime_1.mimes[extension.toLowerCase()] || 'application/octet-stream'),
Filename: filename,
});
if (file.path === 'index.html') {
hasIndexHtml = true;
}
}
if (hasIndexHtml || options?.indexDocument || options?.errorDocument) {
const metadata = {};
if (options?.indexDocument) {
metadata['website-index-document'] = options.indexDocument;
}
else if (hasIndexHtml) {
metadata['website-index-document'] = 'index.html';
}
if (options?.errorDocument) {
metadata['website-error-document'] = options.errorDocument;
}
mantaray.addFork('/', __1.NULL_ADDRESS, metadata);
}
return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
}
exports.streamDirectory = streamDirectory;
function maybeEnrichMime(mime) {
if (['text/html', 'text/css'].includes(mime)) {
return `${mime}; charset=utf-8`;
}
return mime;
}
async function streamFiles(_bee, _files, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
throw new Error('Streaming files is not supported in Node.js');
}
exports.streamFiles = streamFiles;