Initial commit

This commit is contained in:
schihei committed 2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 files changed
+5280367

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
import type { FsPromisesApi } from '../node/types';
import type * as crud from '../crud/types';
export interface NodeCrudOptions {
readonly fs: FsPromisesApi;
readonly dir: string;
readonly separator?: string;
}
export declare class NodeCrud implements crud.CrudApi {
protected readonly options: NodeCrudOptions;
protected readonly fs: FsPromisesApi;
protected readonly dir: string;
protected readonly separator: string;
constructor(options: NodeCrudOptions);
protected checkDir(collection: crud.CrudCollection): Promise<string>;
readonly put: (collection: crud.CrudCollection, id: string, data: Uint8Array, options?: crud.CrudPutOptions) => Promise<void>;
readonly get: (collection: crud.CrudCollection, id: string) => Promise<Uint8Array>;
readonly del: (collection: crud.CrudCollection, id: string, silent?: boolean) => Promise<void>;
readonly info: (collection: crud.CrudCollection, id?: string) => Promise<crud.CrudResourceInfo>;
readonly drop: (collection: crud.CrudCollection, silent?: boolean) => Promise<void>;
readonly scan: (collection: crud.CrudCollection) => AsyncIterableIterator<crud.CrudCollectionEntry>;
readonly list: (collection: crud.CrudCollection) => Promise<crud.CrudCollectionEntry[]>;
readonly from: (collection: crud.CrudCollection) => Promise<crud.CrudApi>;
}
+253
View File
@@ -0,0 +1,253 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCrud = void 0;
const tslib_1 = require("tslib");
const util_1 = require("../node-to-fsa/util");
const util_2 = require("../crud/util");
const util_3 = require("../fsa-to-crud/util");
class NodeCrud {
constructor(options) {
var _a;
this.options = options;
this.put = async (collection, id, data, options) => {
(0, util_2.assertType)(collection, 'put', 'crudfs');
(0, util_1.assertName)(id, 'put', 'crudfs');
const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
const fs = this.fs;
if (dir.length > 1)
await fs.mkdir(dir, { recursive: true });
const filename = dir + id;
switch (options === null || options === void 0 ? void 0 : options.throwIf) {
case 'exists': {
try {
await fs.writeFile(filename, data, { flag: 64 /* FLAG.O_CREAT */ | 128 /* FLAG.O_EXCL */ });
}
catch (error) {
if (error && typeof error === 'object' && error.code === 'EEXIST')
throw (0, util_3.newExistsError)();
throw error;
}
break;
}
case 'missing': {
try {
await fs.writeFile(filename, data, { flag: 2 /* FLAG.O_RDWR */ });
}
catch (error) {
if (error && typeof error === 'object' && error.code === 'ENOENT')
throw (0, util_3.newMissingError)();
throw error;
}
break;
}
default: {
await fs.writeFile(filename, data);
}
}
};
this.get = async (collection, id) => {
(0, util_2.assertType)(collection, 'get', 'crudfs');
(0, util_1.assertName)(id, 'get', 'crudfs');
const dir = await this.checkDir(collection);
const filename = dir + id;
const fs = this.fs;
try {
const buf = (await fs.readFile(filename));
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
catch (error) {
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT':
throw (0, util_3.newFile404Error)(collection, id);
}
}
throw error;
}
};
this.del = async (collection, id, silent) => {
(0, util_2.assertType)(collection, 'del', 'crudfs');
(0, util_1.assertName)(id, 'del', 'crudfs');
try {
const dir = await this.checkDir(collection);
const filename = dir + id;
await this.fs.unlink(filename);
}
catch (error) {
if (!!silent)
return;
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT':
throw (0, util_3.newFile404Error)(collection, id);
}
}
throw error;
}
};
this.info = async (collection, id) => {
(0, util_2.assertType)(collection, 'info', 'crudfs');
if (id) {
(0, util_1.assertName)(id, 'info', 'crudfs');
await this.checkDir(collection);
try {
const stats = await this.fs.stat(this.dir + collection.join(this.separator) + this.separator + id);
if (!stats.isFile())
throw (0, util_3.newFile404Error)(collection, id);
return {
type: 'resource',
id,
size: stats.size,
modified: stats.mtimeMs,
};
}
catch (error) {
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT':
throw (0, util_3.newFile404Error)(collection, id);
}
}
throw error;
}
}
else {
await this.checkDir(collection);
try {
const stats = await this.fs.stat(this.dir + collection.join(this.separator));
if (!stats.isDirectory())
throw (0, util_3.newFolder404Error)(collection);
return {
type: 'collection',
id: '',
};
}
catch (error) {
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT':
case 'ENOTDIR':
throw (0, util_3.newFolder404Error)(collection);
}
}
throw error;
}
}
};
this.drop = async (collection, silent) => {
(0, util_2.assertType)(collection, 'drop', 'crudfs');
try {
const dir = await this.checkDir(collection);
const isRoot = dir === this.dir;
if (isRoot) {
const list = (await this.fs.readdir(dir));
for (const entry of list)
await this.fs.rmdir(dir + entry, { recursive: true });
}
else {
await this.fs.rmdir(dir, { recursive: true });
}
}
catch (error) {
if (!silent)
throw error;
}
};
this.scan = function (collection) {
return tslib_1.__asyncGenerator(this, arguments, function* () {
var _a, e_1, _b, _c;
(0, util_2.assertType)(collection, 'scan', 'crudfs');
const dir = yield tslib_1.__await(this.checkDir(collection));
const dirents = (yield tslib_1.__await(this.fs.readdir(dir, { withFileTypes: true })));
try {
for (var _d = true, dirents_1 = tslib_1.__asyncValues(dirents), dirents_1_1; dirents_1_1 = yield tslib_1.__await(dirents_1.next()), _a = dirents_1_1.done, !_a; _d = true) {
_c = dirents_1_1.value;
_d = false;
const entry = _c;
if (entry.isFile()) {
yield yield tslib_1.__await({
type: 'resource',
id: '' + entry.name,
});
}
else if (entry.isDirectory()) {
yield yield tslib_1.__await({
type: 'collection',
id: '' + entry.name,
});
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = dirents_1.return)) yield tslib_1.__await(_b.call(dirents_1));
}
finally { if (e_1) throw e_1.error; }
}
});
};
this.list = async (collection) => {
var _a, e_2, _b, _c;
const entries = [];
try {
for (var _d = true, _e = tslib_1.__asyncValues(this.scan(collection)), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const entry = _c;
entries.push(entry);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
}
finally { if (e_2) throw e_2.error; }
}
return entries;
};
this.from = async (collection) => {
(0, util_2.assertType)(collection, 'from', 'crudfs');
const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
const fs = this.fs;
if (dir.length > 1)
await fs.mkdir(dir, { recursive: true });
await this.checkDir(collection);
return new NodeCrud({
dir,
fs: this.fs,
separator: this.separator,
});
};
this.separator = (_a = options.separator) !== null && _a !== void 0 ? _a : '/';
let dir = options.dir;
const last = dir[dir.length - 1];
if (last !== this.separator)
dir = dir + this.separator;
this.dir = dir;
this.fs = options.fs;
}
async checkDir(collection) {
const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
const fs = this.fs;
try {
const stats = await fs.stat(dir);
if (!stats.isDirectory())
throw (0, util_3.newFolder404Error)(collection);
return dir;
}
catch (error) {
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT':
case 'ENOTDIR':
throw (0, util_3.newFolder404Error)(collection);
}
}
throw error;
}
}
}
exports.NodeCrud = NodeCrud;
//# sourceMappingURL=NodeCrud.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"NodeCrud.js","sourceRoot":"","sources":["../../src/node-to-crud/NodeCrud.ts"],"names":[],"mappings":";;;;AAAA,8CAAiD;AACjD,uCAA0C;AAE1C,8CAA0G;AAW1G,MAAa,QAAQ;IAKnB,YAAsC,OAAwB;;QAAxB,YAAO,GAAP,OAAO,CAAiB;QA4B9C,QAAG,GAAG,KAAK,EACzB,UAA+B,EAC/B,EAAU,EACV,IAAgB,EAChB,OAA6B,EACd,EAAE;YACjB,IAAA,iBAAU,EAAC,UAAU,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;YACxC,IAAA,iBAAU,EAAC,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;YAChC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,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,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;gBAC5C,MAAM,MAAM,GAAG,GAAG,KAAK,IAAI,CAAC,GAAG,CAAC;gBAChC,IAAI,MAAM,EAAE,CAAC;oBACX,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAa,CAAC;oBACtD,KAAK,MAAM,KAAK,IAAI,IAAI;wBAAE,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,GAAG,KAAK,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAALine truncated
+1
View File
@@ -0,0 +1 @@
export { NodeCrud, NodeCrudOptions } from './NodeCrud';
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCrud = void 0;
var NodeCrud_1 = require("./NodeCrud");
Object.defineProperty(exports, "NodeCrud", { enumerable: true, get: function () { return NodeCrud_1.NodeCrud; } });
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node-to-crud/index.ts"],"names":[],"mappings":";;;AAAA,uCAAuD;AAA9C,oGAAA,QAAQ,OAAA"}