UNPKG

@fireproof/partykit

Version:

PartyKit gateway for Fireproof

339 lines (337 loc) 11.9 kB
// src/connection-from-store.ts import { BuildURI, runtimeFn, URI } from "@adviser/cement"; import { bs, ensureLogger } from "@fireproof/core"; var ConnectionFromStore = class extends bs.ConnectionBase { constructor(sthis, url) { const logger = 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 = 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, URI.from(iurl)); } function makeKeyBagUrlExtractable(sthis) { let base = sthis.env.get("FP_KEYBAG_URL"); if (!base) { if (runtimeFn().isBrowser) { base = "indexdb://fp-keybag"; } else { base = "file://./dist/kb-dir-partykit"; } } const kbUrl = 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 import { PartySocket } from "partysocket"; import { Result, URI as URI2, BuildURI as BuildURI2, KeyedResolvOnce, runtimeFn as runtimeFn2, exception2Result } from "@adviser/cement"; import { bs as bs2, ensureLogger as ensureLogger2, getStore, rt } from "@fireproof/core"; var PartyKitGateway = class { constructor(sthis) { this.subscriberCallbacks = /* @__PURE__ */ new Set(); this.sthis = sthis; this.id = sthis.nextId().str; this.logger = ensureLogger2(sthis, "PartyKitGateway", { url: () => this.url?.toString(), this: this.id }); this.logger.Debug().Msg("constructor"); } async buildUrl(baseUrl, key) { return 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 (runtimeFn2().isNodeIsh) { const { WebSocket } = await import("ws"); partySockOpts.WebSocket = WebSocket; } this.pso = partySockOpts; return 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 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 Result.Ok(void 0); } async put(uri, body) { await this.ready(); const { store } = getStore(uri, this.sthis, (...args) => args.join("/")); if (store === "meta") { const bodyRes = await bs2.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 Result.Err(rkey.Err()); const key = rkey.Ok(); const uploadUrl = store === "meta" ? pkMetaURL(uri, key) : pkCarURL(uri, key); return 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 Result.Err(new Error("store must be meta")); } this.subscriberCallbacks.add(callback); return Result.Ok(() => { this.subscriberCallbacks.delete(callback); }); } async get(uri) { await this.ready(); return exception2Result(async () => { const { store } = 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 bs2.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 exception2Result(async () => { const { store } = 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 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 Result.Ok(void 0); }); } }; var pkSockets = new 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 BuildURI2.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 BuildURI2.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 = ensureLogger2(sthis, "PartyKitTestStore"); this.gateway = gw; } async get(uri, key) { const url = uri.build().setParam("key", key).URI(); const dbFile = this.sthis.pathOps.join(rt.getPath(url, this.sthis), 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 KeyedResolvOnce(); function registerPartyKitStoreProtocol(protocol = "partykit:", overrideBaseURL) { return onceRegisterPartyKitStoreProtocol.get(protocol).once(() => { URI2.protocolHasHostpart(protocol); return bs2.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 import { BuildURI as BuildURI3, KeyedResolvOnce as KeyedResolvOnce2, runtimeFn as runtimeFn3 } from "@adviser/cement"; if (!runtimeFn3().isBrowser) { const url = BuildURI3.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 KeyedResolvOnce2(); 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 = BuildURI3.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; }); }; export { connect }; //# sourceMappingURL=index.js.map