ram64
Version:
Multi-threaded 64bit memory cache database inspired by redis-like features
46 lines (45 loc) • 1.39 kB
JavaScript
;
var _promises = require("fs/promises");
var _hash = require("./util/hash");
class RAMFunction {
constructor(id, code, fn){
this.#id = id;
this.#code = code;
this.#fn = fn;
}
#id;
#code;
#fn;
get id() {
return this.#id;
}
get code() {
return this.#code;
}
get fn() {
return this.#fn;
}
static fromFile(filePath, testCache, testParams) {
return (0, _promises).readFile(filePath, 'utf8').then((code)=>RAMFunction.fromString(code, testCache, testParams));
}
static fromString(code, testCache, testParams) {
let fnHandler;
try {
fnHandler = new Function('cacheObject', 'params', code);
fnHandler(undefined, undefined);
fnHandler(undefined, {});
const frozenCache = {
value: null
};
Object.freeze(frozenCache); // throw (only on strict mode) if input is attempted to mutate
fnHandler(frozenCache, undefined);
fnHandler(testCache, testParams);
} catch (ex) {
throw new Error(`RAMFunction did not pass validation: ${ex.message}`);
}
const id = (0, _hash).getHash(code);
return new RAMFunction(id, code, fnHandler);
}
}
exports.RAMFunction = RAMFunction;
//# sourceMappingURL=ram-function.js.map