fs-zoo
Version:
File system abstractions and implementations
103 lines (102 loc) • 4.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrudExtended = void 0;
const util_1 = require("./util");
const fromStream_1 = require("@jsonjoy.com/util/lib/streams/fromStream");
const toUint8Array_1 = require("@jsonjoy.com/buffers/lib/toUint8Array");
class CrudExtended {
constructor(_fs) {
this._fs = _fs;
this.put = (path, data, options) => {
return this._fs.put(path, data, options);
};
this.putUtf8 = async (path, text, options) => {
const data = new TextEncoder().encode(text);
return this.put(path, data, options);
};
this.getStream = (path) => {
return this._fs.getStream(path);
};
this.get = async (path) => {
const stream = await this.getStream(path);
const buf = await (0, fromStream_1.fromStream)(stream);
return (0, toUint8Array_1.toUint8Array)(buf);
};
this.getUtf8 = async (path) => {
const data = await this.get(path);
return new TextDecoder().decode(data);
};
this.getFile = async (path) => {
const [, id] = (0, util_1.parseId)(path);
const data = await this.get(path);
return new File([new Blob([data])], id);
};
this.del = (path, silent) => {
return this._fs.del(path, silent);
};
this.info = (path) => {
return this._fs.info(path);
};
this.drop = (path, silent) => {
return this._fs.drop(path, silent);
};
this.scan = (path) => {
return this._fs.scan(path);
};
this.list = (path) => {
return this._fs.list(path);
};
this.from = async (path) => {
const child = await this._fs.from(path);
return new CrudExtended(child);
};
this.cp = async (path, target) => {
const srcInfo = await this.info(path);
const srcParts = (0, util_1.parseParts)(path);
const tgtParts = (0, util_1.parseParts)(target);
const isSamePath = srcParts.length === tgtParts.length && srcParts.every((p, i) => p === tgtParts[i]);
if (isSamePath)
throw new DOMException('Resource already exists', 'Exists');
if (srcInfo.type === 'collection') {
const isNestedIntoSelf = tgtParts.length > srcParts.length && srcParts.every((p, i) => p === tgtParts[i]);
if (isNestedIntoSelf)
throw new DOMException('Resource already exists', 'Exists');
}
const targetExists = await this.info(target)
.then(() => true)
.catch(err => {
const name = err && typeof err === 'object' ? err.name : undefined;
if (name === 'ResourceNotFound' || name === 'CollectionNotFound')
return false;
throw err;
});
if (targetExists)
throw new DOMException('Resource already exists', 'Exists');
if (srcInfo.type === 'resource') {
const data = await this.get(path);
await this.put(target, data, { throwIf: 'exists' });
return;
}
await this.put(target, undefined, { throwIf: 'exists' });
const src = await this.from(path);
const dst = await this.from(target);
const copyDir = async (s, d) => {
const entries = await s.list('');
for (const entry of entries) {
if (entry.type === 'resource') {
const data = await s.get(entry.id);
await d.put(entry.id, data, { throwIf: 'exists' });
}
else {
await d.put(entry.id, undefined, { throwIf: 'exists' });
const s2 = await s.from(entry.id);
const d2 = await d.from(entry.id);
await copyDir(s2, d2);
}
}
};
await copyDir(src, dst);
};
}
}
exports.CrudExtended = CrudExtended;