UNPKG

@jimpick/fireproof-partykit

Version:

PartyKit gateway for Fireproof

372 lines (368 loc) 14 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/partykit/index.ts var partykit_exports = {}; __export(partykit_exports, { connect: () => connect }); module.exports = __toCommonJS(partykit_exports); // src/connection-from-store.ts var import_cement = require("@adviser/cement"); var import_fireproof_core = require("@jimpick/fireproof-core"); var ConnectionFromStore = class extends import_fireproof_core.bs.ConnectionBase { constructor(sthis, url) { const logger = (0, import_fireproof_core.ensureLogger)(sthis, "ConnectionFromStore", { url: () => url.toString(), this: 1, log: 1 }); super(url, logger); this.stores = void 0; this.sthis = sthis; } async onConnect() { this.logger.Debug().Msg("onConnect-start"); const stores = { base: this.url // data: this.urlData, // meta: this.urlMeta, }; const rName = this.url.getParamResult("name"); if (rName.isErr()) { throw this.logger.Error().Err(rName).Msg("missing Parameter").AsError(); } const storeRuntime = import_fireproof_core.bs.toStoreRuntime({ stores }, this.sthis); const loader = { name: rName.Ok(), ebOpts: { logger: this.logger, store: { stores }, storeRuntime }, sthis: this.sthis }; this.stores = { data: await storeRuntime.makeDataStore(loader), meta: await storeRuntime.makeMetaStore(loader) }; this.logger.Debug().Msg("onConnect-done"); return; } }; function connectionFactory(sthis, iurl) { return new ConnectionFromStore(sthis, import_cement.URI.from(iurl)); } function makeKeyBagUrlExtractable(sthis) { let base = sthis.env.get("FP_KEYBAG_URL"); if (!base) { if ((0, import_cement.runtimeFn)().isBrowser) { base = "indexdb://fp-keybag"; } else { base = "file:///tmp/dist/kb-dir-partykit"; } } const kbUrl = import_cement.BuildURI.from(base); kbUrl.defParam("extractKey", "_deprecated_internal_api"); sthis.env.set("FP_KEYBAG_URL", kbUrl.toString()); sthis.logger.Debug().Url(kbUrl, "keyBagUrl").Msg("Make keybag url extractable"); } // src/partykit/gateway.ts var import_partysocket = require("partysocket"); var import_cement2 = require("@adviser/cement"); var import_fireproof_core2 = require("@jimpick/fireproof-core"); var PartyKitGateway = class { constructor(sthis) { this.subscriberCallbacks = /* @__PURE__ */ new Set(); this.sthis = sthis; this.id = sthis.nextId().str; this.logger = (0, import_fireproof_core2.ensureLogger)(sthis, "PartyKitGateway", { url: () => this.url?.toString(), this: this.id }); this.logger.Debug().Msg("constructor"); } async buildUrl(baseUrl, key) { return import_cement2.Result.Ok(baseUrl.build().setParam("key", key).URI()); } async start(uri) { this.logger.Debug().Msg("Starting PartyKitGateway with URI: " + uri.toString()); await this.sthis.start(); this.url = uri; const ret = uri.build().defParam("version", "v0.1-partykit"); const rName = uri.getParamResult("name"); if (rName.isErr()) { return this.logger.Error().Err(rName).Msg("name not found").ResultError(); } let dbName = rName.Ok(); if (this.url.hasParam("index")) { dbName = dbName + "-idx"; } ret.defParam("party", "fireproof"); ret.defParam("protocol", "wss"); let possibleUndef = { protocol: ret.getParam("protocol") }; const protocolsStr = uri.getParam("protocols"); if (protocolsStr) { const ps = protocolsStr.split(",").map((x) => x.trim()).filter((x) => x); if (ps.length > 0) { possibleUndef = { ...possibleUndef, protocols: ps }; } } const prefixStr = uri.getParam("prefix"); if (prefixStr) { possibleUndef = { ...possibleUndef, prefix: prefixStr }; } const query = {}; const partySockOpts = { id: this.id, host: this.url.host, room: dbName, party: ret.getParam("party"), ...possibleUndef, query, path: this.url.pathname.replace(/^\//, "") }; if ((0, import_cement2.runtimeFn)().isNodeIsh) { const { WebSocket } = await import("ws"); partySockOpts.WebSocket = WebSocket; } this.pso = partySockOpts; return import_cement2.Result.Ok(ret.URI()); } async ready() { this.logger.Debug().Msg("ready"); } async connectPartyKit() { const pkKeyThis = pkKey(this.pso); return pkSockets.get(pkKeyThis).once(async () => { if (!this.pso) { throw new Error("Party socket options not found"); } this.party = new import_partysocket.PartySocket(this.pso); let exposedResolve; const openFn = () => { this.logger.Debug().Msg("party open"); this.party?.addEventListener("message", async (event) => { this.logger.Debug().Msg(`got message: ${event.data}`); const mbin = this.sthis.txt.encode(event.data); this.notifySubscribers(mbin); }); exposedResolve(true); }; return await new Promise((resolve) => { exposedResolve = resolve; this.party?.addEventListener("open", openFn); }); }); } async close() { await this.ready(); this.logger.Debug().Msg("close"); this.party?.close(); return import_cement2.Result.Ok(void 0); } async put(uri, body) { await this.ready(); const { store } = (0, import_fireproof_core2.getStore)(uri, this.sthis, (...args) => args.join("/")); if (store === "meta") { const bodyRes = await import_fireproof_core2.bs.addCryptoKeyToGatewayMetaPayload(uri, this.sthis, body); if (bodyRes.isErr()) { this.logger.Error().Err(bodyRes.Err()).Msg("Error in addCryptoKeyToGatewayMetaPayload"); throw bodyRes.Err(); } body = bodyRes.Ok(); } const rkey = uri.getParamResult("key"); if (rkey.isErr()) return import_cement2.Result.Err(rkey.Err()); const key = rkey.Ok(); const uploadUrl = store === "meta" ? pkMetaURL(uri, key) : pkCarURL(uri, key); return (0, import_cement2.exception2Result)(async () => { const response = await fetch(uploadUrl.asURL(), { method: "PUT", body }); if (response.status === 404) { throw this.logger.Error().Url(uploadUrl).Msg(`Failure in uploading ${store}!`).AsError(); } }); } notifySubscribers(data) { for (const callback of this.subscriberCallbacks) { try { callback(data); } catch (error) { this.logger.Error().Err(error).Msg("Error in subscriber callback execution"); } } } async subscribe(uri, callback) { await this.ready(); await this.connectPartyKit(); const store = uri.getParam("store"); if (store !== "meta") { return import_cement2.Result.Err(new Error("store must be meta")); } this.subscriberCallbacks.add(callback); return import_cement2.Result.Ok(() => { this.subscriberCallbacks.delete(callback); }); } async get(uri) { await this.ready(); return (0, import_cement2.exception2Result)(async () => { const { store } = (0, import_fireproof_core2.getStore)(uri, this.sthis, (...args) => args.join("/")); const key = uri.getParam("key"); if (!key) throw new Error("key not found"); const downloadUrl = store === "meta" ? pkMetaURL(uri, key) : pkCarURL(uri, key); const response = await fetch(downloadUrl.toString(), { method: "GET" }); if (response.status === 404) { throw new Error(`Failure in downloading ${store}!`); } const body = new Uint8Array(await response.arrayBuffer()); if (store === "meta") { const resKeyInfo = await import_fireproof_core2.bs.setCryptoKeyFromGatewayMetaPayload(uri, this.sthis, body); if (resKeyInfo.isErr()) { this.logger.Error().Url(uri).Err(resKeyInfo).Any("body", body).Msg("Error in setCryptoKeyFromGatewayMetaPayload"); throw resKeyInfo.Err(); } } return body; }); } async delete(uri) { await this.ready(); return (0, import_cement2.exception2Result)(async () => { const { store } = (0, import_fireproof_core2.getStore)(uri, this.sthis, (...args) => args.join("/")); const key = uri.getParam("key"); if (!key) throw new Error("key not found"); if (store === "meta") throw new Error("Cannot delete from meta store"); const deleteUrl = pkCarURL(uri, key); const response = await fetch(deleteUrl.toString(), { method: "DELETE" }); if (response.status === 404) { throw new Error(`Failure in deleting ${store}!`); } }); } async destroy(uri) { await this.ready(); return (0, import_cement2.exception2Result)(async () => { const deleteUrl = pkBaseURL(uri); const response = await fetch(deleteUrl.asURL(), { method: "DELETE" }); if (response.status === 404) { throw new Error("Failure in deleting data!"); } return import_cement2.Result.Ok(void 0); }); } }; var pkSockets = new import_cement2.KeyedResolvOnce(); function pkKey(set) { const ret = JSON.stringify( Object.entries(set || {}).sort(([a], [b]) => a.localeCompare(b)).filter(([k]) => k !== "id").map(([k, v]) => ({ [k]: v })) ); return ret; } function pkURL(uri, key, type) { const host = uri.host; const name = uri.getParam("name"); const idx = uri.getParam("index") || ""; const protocol = uri.getParam("protocol") === "ws" ? "http" : "https"; const path = `/parties/fireproof/${name}${idx}`; return import_cement2.BuildURI.from(`${protocol}://${host}${path}`).setParam(type, key).URI(); } function pkBaseURL(uri) { const host = uri.host; const name = uri.getParam("name"); const idx = uri.getParam("index") || ""; const protocol = uri.getParam("protocol") === "ws" ? "http" : "https"; const path = `/parties/fireproof/${name}${idx}`; return import_cement2.BuildURI.from(`${protocol}://${host}${path}`).URI(); } function pkCarURL(uri, key) { return pkURL(uri, key, "car"); } function pkMetaURL(uri, key) { return pkURL(uri, key, "meta"); } var PartyKitTestStore = class { constructor(gw, sthis) { this.sthis = sthis; this.logger = (0, import_fireproof_core2.ensureLogger)(sthis, "PartyKitTestStore"); this.gateway = gw; } async get(uri, key) { const url = uri.build().setParam("key", key).URI(); const dbFile = this.sthis.pathOps.join(import_fireproof_core2.rt.getPath(url, this.sthis), import_fireproof_core2.rt.getFileName(url, this.sthis)); this.logger.Debug().Url(url).Str("dbFile", dbFile).Msg("get"); const buffer = await this.gateway.get(url); this.logger.Debug().Url(url).Str("dbFile", dbFile).Len(buffer).Msg("got"); return buffer.Ok(); } }; var onceRegisterPartyKitStoreProtocol = new import_cement2.KeyedResolvOnce(); function registerPartyKitStoreProtocol(protocol = "partykit:", overrideBaseURL) { return onceRegisterPartyKitStoreProtocol.get(protocol).once(() => { import_cement2.URI.protocolHasHostpart(protocol); return import_fireproof_core2.bs.registerStoreProtocol({ protocol, overrideBaseURL, gateway: async (sthis) => { return new PartyKitGateway(sthis); }, test: async (sthis) => { const gateway = new PartyKitGateway(sthis); return new PartyKitTestStore(gateway, sthis); } }); }); } // src/partykit/index.ts var import_cement3 = require("@adviser/cement"); if (!(0, import_cement3.runtimeFn)().isBrowser) { const url = import_cement3.BuildURI.from(process.env.FP_KEYBAG_URL || "file://./dist/kb-dir-partykit"); url.setParam("extractKey", "_deprecated_internal_api"); process.env.FP_KEYBAG_URL = url.toString(); } registerPartyKitStoreProtocol(); var connectionCache = new import_cement3.KeyedResolvOnce(); var connect = (db, remoteDbName = "", url = "http://localhost:1999?protocol=ws") => { const { sthis, blockstore, name: dbName } = db; if (!dbName) { throw new Error("dbName is required"); } const urlObj = import_cement3.BuildURI.from(url); const existingName = urlObj.getParam("name"); urlObj.defParam("name", remoteDbName || existingName || dbName); urlObj.defParam("localName", dbName); urlObj.defParam("storekey", `@${dbName}:data@`); const fpUrl = urlObj.toString().replace("http://", "partykit://").replace("https://", "partykit://"); return connectionCache.get(fpUrl).once(() => { makeKeyBagUrlExtractable(sthis); const connection = connectionFactory(sthis, fpUrl); connection.connect_X(blockstore); return connection; }); }; //# sourceMappingURL=index.cjs.map