UNPKG

@coko/server

Version:

Reusable server for use by Coko's projects

40 lines 1.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = __importDefault(require("../configManager/config")); const FileStorage_1 = __importDefault(require("./FileStorage")); const FileStorageNoop_1 = __importDefault(require("./FileStorageNoop")); class FileStorageManager { instance; init() { if (this.instance) return; this.instance = config_1.default.get('fileStorage') ? new FileStorage_1.default() : new FileStorageNoop_1.default(); } } const manager = new FileStorageManager(); /** * The Proxy intercepts all calls. * Before init() is called, instance is null. * After init() is called, instance is the specific implementation. */ const fileStorageProxy = new Proxy(manager, { get(target, prop) { // 1. Allow calling .init() on the proxy itself if (prop === 'init') return target.init.bind(target); // 2. Ensure the instance is ready if (!target.instance) { throw new Error('FileStorage has not been initialized. Please call fileStorage.init() first.'); } // 3. Forward the property/method access to the actual instance const value = target.instance[prop]; return typeof value === 'function' ? value.bind(target.instance) : value; }, }); exports.default = fileStorageProxy; //# sourceMappingURL=index.js.map