35 lines
836 B
JavaScript
35 lines
836 B
JavaScript
import crypto from 'node:crypto';
|
|
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
export class BlobStore {
|
|
constructor(root) { this.root = root; }
|
|
|
|
async hash(buf) {
|
|
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
}
|
|
|
|
path(sha) {
|
|
return path.join(this.root, sha.slice(0, 2), sha);
|
|
}
|
|
|
|
async write(buf) {
|
|
const sha = await this.hash(buf);
|
|
const dest = this.path(sha);
|
|
try {
|
|
await fs.access(dest);
|
|
} catch {
|
|
await fs.mkdir(path.dirname(dest), { recursive: true });
|
|
await fs.writeFile(dest, buf);
|
|
}
|
|
return { sha, path: dest };
|
|
}
|
|
}
|
|
|
|
let _default = null;
|
|
export function defaultStore() {
|
|
const root = process.env.BLOB_ROOT || '/var/lib/void/blobs';
|
|
if (!_default || _default.root !== root) _default = new BlobStore(root);
|
|
return _default;
|
|
}
|