@ethersphere/bee-js
Version:
Javascript client for Bee
85 lines • 2.83 kB
JavaScript
import { AsyncQueue, MerkleTree, Strings } from 'cafe-utility';
import { NULL_ADDRESS } from "../index.js";
import { MantarayNode } from "../manifest/manifest.js";
import { totalChunks } from "./chunk-size.js";
import { makeFilePath } from "./collection.js";
import { mimes } from "./mime.js";
import { BatchId } from "./typed-bytes.js";
export async function hashDirectory(_dir) {
throw new Error('Hashing directories is not supported in browsers!');
}
export async function streamDirectory(_bee, _dir, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
throw new Error('Streaming directories is not supported in browsers!');
}
export async function streamFiles(bee, files, postageBatchId, onUploadProgress, options, requestOptions) {
const queue = new AsyncQueue(64, 64);
let total = 0;
let processed = 0;
for (const file of files) {
total += totalChunks(file.size);
}
postageBatchId = new BatchId(postageBatchId);
async function onChunk(chunk) {
await queue.enqueue(async () => {
await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
onUploadProgress?.({
total,
processed: ++processed
});
});
}
const mantaray = new MantarayNode();
for (const file of files) {
const rootChunk = await new Promise((resolve, reject) => {
const tree = new MerkleTree(onChunk);
let offset = 0;
const reader = new FileReader();
reader.onerror = () => {
reject(reader.error);
};
const readNextChunk = async () => {
if (offset >= file.size) {
const rootChunk = await tree.finalize();
resolve(rootChunk);
return;
}
const slice = file.slice(offset, offset + 4096);
reader.readAsArrayBuffer(slice);
};
reader.onload = async event => {
if (!event.target) {
reject('No event target');
return;
}
const data = event.target.result;
if (data) {
await tree.append(new Uint8Array(data));
offset += 4096;
}
readNextChunk();
};
readNextChunk();
});
await queue.drain();
const {
filename,
extension
} = Strings.parseFilename(file.name);
mantaray.addFork(makeFilePath(file), rootChunk.hash(), {
'Content-Type': maybeEnrichMime(mimes[extension.toLowerCase()] || 'application/octet-stream'),
Filename: filename
});
if (file.name === 'index.html') {
mantaray.addFork('/', NULL_ADDRESS, {
'website-index-document': 'index.html'
});
}
}
return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
}
function maybeEnrichMime(mime) {
if (['text/html', 'text/css'].includes(mime)) {
return `${mime}; charset=utf-8`;
}
return mime;
}