UNPKG

abi.js

Version:

[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]

98 lines (93 loc) 2.81 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/session.ts var session_exports = {}; __export(session_exports, { Session: () => Session, SessionHandler: () => SessionHandler }); module.exports = __toCommonJS(session_exports); // src/engine.ts var import_node_v8 = require("node:v8"); var import_node_zlib = require("node:zlib"); // src/runtime.ts var import_node_fs = require("node:fs"); var runtime = process.versions != null && process.versions.node != null ? "Node.js" : "Bun"; var cwd = process.cwd(); // src/session.ts var import_meta = {}; 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 = (0, import_node_fs.existsSync)(file) ? (0, import_node_fs.readFileSync)(file) : ""; const data = (0, import_node_zlib.deflateSync)(raw.toString()); return (0, import_node_v8.deserialize)(data); } getPath(id) { return `${this.path}/${this.prefix}-${id}`; } write(id, data) { const raw = (0, import_node_zlib.inflateSync)((0, import_node_v8.serialize)(data)).toString(); (0, import_node_fs.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); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Session, SessionHandler });