ipfs-unixfs-importer
Version:
JavaScript implementation of the UnixFs importer used by IPFS
41 lines • 1.3 kB
JavaScript
import DirSharded from './dir-sharded.js';
import { DirFlat } from './dir-flat.js';
export async function flatToShard(child, dir, threshold, options) {
let newDir = dir;
if (dir instanceof DirFlat && dir.estimateNodeSize() > threshold) {
newDir = await convertToShard(dir, options);
}
const parent = newDir.parent;
if (parent != null) {
if (newDir !== dir) {
if (child != null) {
child.parent = newDir;
}
if (newDir.parentKey == null) {
throw new Error('No parent key found');
}
await parent.put(newDir.parentKey, newDir);
}
return await flatToShard(newDir, parent, threshold, options);
}
// @ts-expect-error
return newDir;
}
async function convertToShard(oldDir, options) {
const newDir = new DirSharded({
root: oldDir.root,
dir: true,
parent: oldDir.parent,
parentKey: oldDir.parentKey,
path: oldDir.path,
dirty: oldDir.dirty,
flat: false,
mtime: oldDir.mtime,
mode: oldDir.mode
}, options);
for await (const { key, child } of oldDir.eachChildSeries()) {
await newDir.put(key, child);
}
return newDir;
}
//# sourceMappingURL=flat-to-shard.js.map