abi.js
Version:
[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]
73 lines (70 loc) • 1.63 kB
JavaScript
// src/engine.ts
import { serialize, deserialize } from "node:v8";
import { inflateSync, deflateSync } from "node:zlib";
// src/runtime.ts
import {
existsSync,
readFileSync,
writeFileSync
} from "node:fs";
var runtime = process.versions != null && process.versions.node != null ? "Node.js" : "Bun";
var cwd = process.cwd();
// src/session.ts
var SessionHandler = class {
constructor(path = `${import.meta.dirname}/.sessions`, prefix = "session_") {
this.path = path;
this.prefix = prefix;
}
read(id) {
const file = this.getPath(id);
const raw = existsSync(file) ? readFileSync(file) : "";
const data = deflateSync(raw.toString());
return deserialize(data);
}
getPath(id) {
return `${this.path}/${this.prefix}-${id}`;
}
write(id, data) {
const raw = inflateSync(serialize(data)).toString();
writeFileSync(this.getPath(id), raw);
}
};
var Session = class {
constructor(handler, id) {
this.handler = handler;
this.id = id ?? crypto.randomUUID();
}
id;
data;
load() {
this.data = this.handler.read(this.id) || {};
}
set(key, value) {
if (!this.data) {
this.data = {};
}
this.data[key] = value;
return this;
}
get(key, defaultValue) {
return this.data && this.data[key] !== void 0 ? this.data[key] : defaultValue;
}
has(key) {
return this.data ? this.data[key] !== void 0 : false;
}
remove(key) {
if (this.data) {
delete this.data[key];
}
return this;
}
save() {
if (this.data) {
this.handler.write(this.id, this.data);
}
}
};
export {
Session,
SessionHandler
};