@latticexyz/store-indexer 2.2.18-9fa07c8489f1fbf167d0db01cd9aaa645a29c8e2 → 2.2.18-c2ad22c7feb566e1731ff16e8be291746bdffb3e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/bin/postgres-decoded-indexer.js +1 -92
  2. package/dist/bin/postgres-decoded-indexer.js.map +1 -1
  3. package/dist/bin/postgres-frontend.js +5 -258
  4. package/dist/bin/postgres-frontend.js.map +1 -1
  5. package/dist/bin/postgres-indexer.js +1 -106
  6. package/dist/bin/postgres-indexer.js.map +1 -1
  7. package/dist/bin/sqlite-indexer.js +1 -243
  8. package/dist/bin/sqlite-indexer.js.map +1 -1
  9. package/dist/chunk-7O2ZWWUX.js +2 -0
  10. package/dist/{chunk-SJLOWI5M.js.map → chunk-7O2ZWWUX.js.map} +1 -1
  11. package/dist/chunk-AYPBOJNL.js +2 -0
  12. package/dist/{chunk-GDNGJPVT.js.map → chunk-AYPBOJNL.js.map} +1 -1
  13. package/dist/chunk-ED45N3IT.js +2 -0
  14. package/dist/{chunk-O4XAWAXU.js.map → chunk-ED45N3IT.js.map} +1 -1
  15. package/dist/chunk-JDWVOODJ.js +2 -0
  16. package/dist/{chunk-R7UQFYRA.js.map → chunk-JDWVOODJ.js.map} +1 -1
  17. package/dist/chunk-O2SDU7EQ.js +7 -0
  18. package/dist/{chunk-L5CWEDU6.js.map → chunk-O2SDU7EQ.js.map} +1 -1
  19. package/dist/chunk-R7HX5BT2.js +2 -0
  20. package/dist/{chunk-66BWQNF7.js.map → chunk-R7HX5BT2.js.map} +1 -1
  21. package/dist/chunk-YQ7E5W26.js +2 -0
  22. package/dist/{chunk-CGE4ONKA.js.map → chunk-YQ7E5W26.js.map} +1 -1
  23. package/dist/healthcheck-57YETUEX.js +2 -0
  24. package/dist/helloWorld-4VT4FZ7F.js +2 -0
  25. package/dist/metrics-4BMCDEZZ.js +2 -0
  26. package/package.json +9 -17
  27. package/dist/bin/postgres-decoded-indexer.cjs +0 -322
  28. package/dist/bin/postgres-decoded-indexer.cjs.map +0 -1
  29. package/dist/bin/postgres-decoded-indexer.d.cts +0 -1
  30. package/dist/bin/postgres-frontend.cjs +0 -567
  31. package/dist/bin/postgres-frontend.cjs.map +0 -1
  32. package/dist/bin/postgres-frontend.d.cts +0 -1
  33. package/dist/bin/postgres-indexer.cjs +0 -368
  34. package/dist/bin/postgres-indexer.cjs.map +0 -1
  35. package/dist/bin/postgres-indexer.d.cts +0 -1
  36. package/dist/bin/sqlite-indexer.cjs +0 -567
  37. package/dist/bin/sqlite-indexer.cjs.map +0 -1
  38. package/dist/bin/sqlite-indexer.d.cts +0 -1
  39. package/dist/chunk-66BWQNF7.js +0 -38
  40. package/dist/chunk-CGE4ONKA.js +0 -44
  41. package/dist/chunk-GDNGJPVT.js +0 -16
  42. package/dist/chunk-L5CWEDU6.js +0 -53
  43. package/dist/chunk-O4XAWAXU.js +0 -72
  44. package/dist/chunk-R7UQFYRA.js +0 -99
  45. package/dist/chunk-SJLOWI5M.js +0 -31
  46. package/dist/healthcheck-2DQWYXPX.js +0 -7
  47. package/dist/helloWorld-6IXGINV6.js +0 -7
  48. package/dist/index.cjs +0 -2
  49. package/dist/index.d.cts +0 -2
  50. package/dist/metrics-HO5SO4EX.js +0 -7
  51. package/dist/metrics-HO5SO4EX.js.map +0 -1
  52. /package/dist/{healthcheck-2DQWYXPX.js.map → healthcheck-57YETUEX.js.map} +0 -0
  53. /package/dist/{helloWorld-6IXGINV6.js.map → helloWorld-4VT4FZ7F.js.map} +0 -0
  54. /package/dist/{index.cjs.map → metrics-4BMCDEZZ.js.map} +0 -0
@@ -1,108 +1,3 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- getClientOptions
4
- } from "../chunk-CGE4ONKA.js";
5
- import {
6
- indexerEnvSchema,
7
- parseEnv
8
- } from "../chunk-L5CWEDU6.js";
9
-
10
- // src/bin/postgres-indexer.ts
11
- import "dotenv/config";
12
- import { z } from "zod";
13
- import { eq } from "drizzle-orm";
14
- import { combineLatest, filter, first } from "rxjs";
15
- import { drizzle } from "drizzle-orm/postgres-js";
16
- import postgres from "postgres";
17
- import { cleanDatabase, createStorageAdapter, shouldCleanDatabase } from "@latticexyz/store-sync/postgres";
18
- import { createStoreSync } from "@latticexyz/store-sync";
19
- import { getBlock, getChainId } from "viem/actions";
20
- import { getRpcClient } from "@latticexyz/block-logs-stream";
21
- (async () => {
22
- const env = parseEnv(
23
- z.intersection(
24
- indexerEnvSchema,
25
- z.object({
26
- DATABASE_URL: z.string(),
27
- HEALTHCHECK_HOST: z.string().optional(),
28
- HEALTHCHECK_PORT: z.coerce.number().optional()
29
- })
30
- )
31
- );
32
- const clientOptions = await getClientOptions(env);
33
- const chainId = await getChainId(getRpcClient(clientOptions));
34
- const database = drizzle(postgres(env.DATABASE_URL, { prepare: false }));
35
- if (await shouldCleanDatabase(database, chainId)) {
36
- console.log("outdated database detected, clearing data to start fresh");
37
- await cleanDatabase(database);
38
- }
39
- const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database });
40
- let startBlock = env.START_BLOCK;
41
- async function getLatestStoredBlockNumber() {
42
- try {
43
- const chainState = await database.select().from(tables.configTable).where(eq(tables.configTable.chainId, chainId)).limit(1).execute().then((rows) => rows.find(() => true));
44
- return chainState?.blockNumber;
45
- } catch (error) {
46
- }
47
- }
48
- async function getDistanceFromFollowBlock() {
49
- const [latestStoredBlockNumber2, latestFollowBlock] = await Promise.all([
50
- getLatestStoredBlockNumber(),
51
- getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG })
52
- ]);
53
- return latestFollowBlock.number - (latestStoredBlockNumber2 ?? -1n);
54
- }
55
- const latestStoredBlockNumber = await getLatestStoredBlockNumber();
56
- if (latestStoredBlockNumber != null) {
57
- startBlock = latestStoredBlockNumber + 1n;
58
- console.log("resuming from block number", startBlock);
59
- }
60
- const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({
61
- ...clientOptions,
62
- storageAdapter,
63
- followBlockTag: env.FOLLOW_BLOCK_TAG,
64
- startBlock,
65
- maxBlockRange: env.MAX_BLOCK_RANGE,
66
- address: env.STORE_ADDRESS
67
- });
68
- storedBlockLogs$.subscribe();
69
- let isCaughtUp = false;
70
- combineLatest([latestBlockNumber$, storedBlockLogs$]).pipe(
71
- filter(
72
- ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => latestBlockNumber === lastBlockNumberProcessed
73
- ),
74
- first()
75
- ).subscribe(() => {
76
- isCaughtUp = true;
77
- console.log("all caught up");
78
- });
79
- if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {
80
- const { default: Koa } = await import("koa");
81
- const { default: cors } = await import("@koa/cors");
82
- const { healthcheck } = await import("../healthcheck-2DQWYXPX.js");
83
- const { metrics } = await import("../metrics-HO5SO4EX.js");
84
- const { helloWorld } = await import("../helloWorld-6IXGINV6.js");
85
- const server = new Koa();
86
- server.use(cors());
87
- server.use(
88
- healthcheck({
89
- isReady: () => isCaughtUp
90
- })
91
- );
92
- server.use(
93
- metrics({
94
- isHealthy: () => true,
95
- isReady: () => isCaughtUp,
96
- getLatestStoredBlockNumber,
97
- getDistanceFromFollowBlock,
98
- followBlockTag: env.FOLLOW_BLOCK_TAG
99
- })
100
- );
101
- server.use(helloWorld());
102
- server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT });
103
- console.log(
104
- `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`
105
- );
106
- }
107
- })();
2
+ import{a as p}from"../chunk-YQ7E5W26.js";import{b as m,c as u}from"../chunk-O2SDU7EQ.js";import"dotenv/config";import{z as r}from"zod";import{eq as h}from"drizzle-orm";import{combineLatest as A,filter as E,first as O}from"rxjs";import{drizzle as w}from"drizzle-orm/postgres-js";import _ from"postgres";import{cleanDatabase as B,createStorageAdapter as S,shouldCleanDatabase as k}from"@latticexyz/store-sync/postgres";import{createStoreSync as K}from"@latticexyz/store-sync";import{getBlock as R,getChainId as y}from"viem/actions";import{getRpcClient as g}from"@latticexyz/block-logs-stream";var t=u(r.intersection(m,r.object({DATABASE_URL:r.string(),HEALTHCHECK_HOST:r.string().optional(),HEALTHCHECK_PORT:r.coerce.number().optional()}))),n=await p(t),b=await y(g(n)),i=w(_(t.DATABASE_URL,{prepare:!1}));await k(i,b)&&(console.log("outdated database detected, clearing data to start fresh"),await B(i));var{storageAdapter:D,tables:d}=await S({...n,database:i}),s=t.START_BLOCK;async function c(){try{return(await i.select().from(d.configTable).where(h(d.configTable.chainId,b)).limit(1).execute().then(o=>o.find(()=>!0)))?.blockNumber}catch{}}async function N(){let[e,o]=await Promise.all([c(),R(g(n),{blockTag:t.FOLLOW_BLOCK_TAG})]);return o.number-(e??-1n)}var f=await c();f!=null&&(s=f+1n,console.log("resuming from block number",s));var{latestBlockNumber$:P,storedBlockLogs$:H}=await K({...n,storageAdapter:D,followBlockTag:t.FOLLOW_BLOCK_TAG,startBlock:s,maxBlockRange:t.MAX_BLOCK_RANGE,address:t.STORE_ADDRESS});H.subscribe();var l=!1;A([P,H]).pipe(E(([e,{blockNumber:o}])=>e===o),O()).subscribe(()=>{l=!0,console.log("all caught up")});if(t.HEALTHCHECK_HOST!=null||t.HEALTHCHECK_PORT!=null){let{default:e}=await import("koa"),{default:o}=await import("@koa/cors"),{healthcheck:T}=await import("../healthcheck-57YETUEX.js"),{metrics:C}=await import("../metrics-4BMCDEZZ.js"),{helloWorld:L}=await import("../helloWorld-4VT4FZ7F.js"),a=new e;a.use(o()),a.use(T({isReady:()=>l})),a.use(C({isHealthy:()=>!0,isReady:()=>l,getLatestStoredBlockNumber:c,getDistanceFromFollowBlock:N,followBlockTag:t.FOLLOW_BLOCK_TAG})),a.use(L()),a.listen({host:t.HEALTHCHECK_HOST,port:t.HEALTHCHECK_PORT}),console.log(`postgres indexer healthcheck server listening on http://${t.HEALTHCHECK_HOST}:${t.HEALTHCHECK_PORT}`)}
108
3
  //# sourceMappingURL=postgres-indexer.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/bin/postgres-indexer.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport { z } from \"zod\";\nimport { eq } from \"drizzle-orm\";\nimport { combineLatest, filter, first } from \"rxjs\";\nimport { drizzle } from \"drizzle-orm/postgres-js\";\nimport postgres from \"postgres\";\nimport { cleanDatabase, createStorageAdapter, shouldCleanDatabase } from \"@latticexyz/store-sync/postgres\";\nimport { createStoreSync } from \"@latticexyz/store-sync\";\nimport { indexerEnvSchema, parseEnv } from \"./parseEnv\";\nimport { getClientOptions } from \"./getClientOptions\";\nimport { getBlock, getChainId } from \"viem/actions\";\nimport { getRpcClient } from \"@latticexyz/block-logs-stream\";\n\n// Workaround for:\n// Top-level await is currently not supported with the \"cjs\" output format\n(async (): Promise<void> => {\n const env = parseEnv(\n z.intersection(\n indexerEnvSchema,\n z.object({\n DATABASE_URL: z.string(),\n HEALTHCHECK_HOST: z.string().optional(),\n HEALTHCHECK_PORT: z.coerce.number().optional(),\n }),\n ),\n );\n\n const clientOptions = await getClientOptions(env);\n\n const chainId = await getChainId(getRpcClient(clientOptions));\n const database = drizzle(postgres(env.DATABASE_URL, { prepare: false }));\n\n if (await shouldCleanDatabase(database, chainId)) {\n console.log(\"outdated database detected, clearing data to start fresh\");\n await cleanDatabase(database);\n }\n\n const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database });\n\n let startBlock = env.START_BLOCK;\n\n async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {\n // Fetch latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n // TODO: query if the DB exists instead of try/catch\n try {\n const chainState = await database\n .select()\n .from(tables.configTable)\n .where(eq(tables.configTable.chainId, chainId))\n .limit(1)\n .execute()\n // Get the first record in a way that returns a possible `undefined`\n // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true`\n .then((rows) => rows.find(() => true));\n\n return chainState?.blockNumber;\n } catch (error) {\n // ignore errors for now\n }\n }\n\n async function getDistanceFromFollowBlock(): Promise<bigint> {\n const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([\n getLatestStoredBlockNumber(),\n getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }),\n ]);\n return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n);\n }\n\n const latestStoredBlockNumber = await getLatestStoredBlockNumber();\n if (latestStoredBlockNumber != null) {\n startBlock = latestStoredBlockNumber + 1n;\n console.log(\"resuming from block number\", startBlock);\n }\n\n const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({\n ...clientOptions,\n storageAdapter,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n startBlock,\n maxBlockRange: env.MAX_BLOCK_RANGE,\n address: env.STORE_ADDRESS,\n });\n\n storedBlockLogs$.subscribe();\n\n let isCaughtUp = false;\n combineLatest([latestBlockNumber$, storedBlockLogs$])\n .pipe(\n filter(\n ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) =>\n latestBlockNumber === lastBlockNumberProcessed,\n ),\n first(),\n )\n .subscribe(() => {\n isCaughtUp = true;\n console.log(\"all caught up\");\n });\n\n if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {\n const { default: Koa } = await import(\"koa\");\n const { default: cors } = await import(\"@koa/cors\");\n const { healthcheck } = await import(\"../koa-middleware/healthcheck\");\n const { metrics } = await import(\"../koa-middleware/metrics\");\n const { helloWorld } = await import(\"../koa-middleware/helloWorld\");\n\n const server = new Koa();\n\n server.use(cors());\n server.use(\n healthcheck({\n isReady: () => isCaughtUp,\n }),\n );\n server.use(\n metrics({\n isHealthy: () => true,\n isReady: () => isCaughtUp,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n }),\n );\n server.use(helloWorld());\n\n server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT });\n console.log(\n `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`,\n );\n }\n})();\n"],"mappings":";;;;;;;;;;AACA,OAAO;AACP,SAAS,SAAS;AAClB,SAAS,UAAU;AACnB,SAAS,eAAe,QAAQ,aAAa;AAC7C,SAAS,eAAe;AACxB,OAAO,cAAc;AACrB,SAAS,eAAe,sBAAsB,2BAA2B;AACzE,SAAS,uBAAuB;AAGhC,SAAS,UAAU,kBAAkB;AACrC,SAAS,oBAAoB;AAAA,CAI5B,YAA2B;AAC1B,QAAM,MAAM;AAAA,IACV,EAAE;AAAA,MACA;AAAA,MACA,EAAE,OAAO;AAAA,QACP,cAAc,EAAE,OAAO;AAAA,QACvB,kBAAkB,EAAE,OAAO,EAAE,SAAS;AAAA,QACtC,kBAAkB,EAAE,OAAO,OAAO,EAAE,SAAS;AAAA,MAC/C,CAAC;AAAA,IACH;AAAA,EACF;AAEA,QAAM,gBAAgB,MAAM,iBAAiB,GAAG;AAEhD,QAAM,UAAU,MAAM,WAAW,aAAa,aAAa,CAAC;AAC5D,QAAM,WAAW,QAAQ,SAAS,IAAI,cAAc,EAAE,SAAS,MAAM,CAAC,CAAC;AAEvE,MAAI,MAAM,oBAAoB,UAAU,OAAO,GAAG;AAChD,YAAQ,IAAI,0DAA0D;AACtE,UAAM,cAAc,QAAQ;AAAA,EAC9B;AAEA,QAAM,EAAE,gBAAgB,OAAO,IAAI,MAAM,qBAAqB,EAAE,GAAG,eAAe,SAAS,CAAC;AAE5F,MAAI,aAAa,IAAI;AAErB,iBAAe,6BAA0D;AAGvE,QAAI;AACF,YAAM,aAAa,MAAM,SACtB,OAAO,EACP,KAAK,OAAO,WAAW,EACvB,MAAM,GAAG,OAAO,YAAY,SAAS,OAAO,CAAC,EAC7C,MAAM,CAAC,EACP,QAAQ,EAGR,KAAK,CAAC,SAAS,KAAK,KAA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M,OAAO,2BAA8B;AAElE,UAAM,SAAS,IAAI,IAAI;AAEvB,WAAO,IAAI,KAAK,CAAC;AACjB,WAAO;AAAA,MACL,YAAY;AAAA,QACV,SAAS,MAAM;AAAA,MACjB,CAAC;AAAA,IACH;AACA,WAAO;AAAA,MACL,QAAQ;AAAA,QACN,WAAW,MAAM;AAAA,QACjB,SAAS,MAAM;AAAA,QACf;AAAA,QACA;AAAA,QACA,gBAAgB,IAAI;AAAA,MACtB,CAAC;AAAA,IACH;AACA,WAAO,IAAI,WAAW,CAAC;AAEvB,WAAO,OAAO,EAAE,MAAM,IAAI,kBAAkB,MAAM,IAAI,iBAAiB,CAAC;AACxE,YAAQ;AAAA,MACN,2DAA2D,IAAI,gBAAgB,IAAI,IAAI,gBAAgB;AAAA,IACzG;AAAA,EACF;AACF,GAAG;","names":["latestStoredBlockNumber"]}
1
+ {"version":3,"sources":["../../src/bin/postgres-indexer.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport { z } from \"zod\";\nimport { eq } from \"drizzle-orm\";\nimport { combineLatest, filter, first } from \"rxjs\";\nimport { drizzle } from \"drizzle-orm/postgres-js\";\nimport postgres from \"postgres\";\nimport { cleanDatabase, createStorageAdapter, shouldCleanDatabase } from \"@latticexyz/store-sync/postgres\";\nimport { createStoreSync } from \"@latticexyz/store-sync\";\nimport { indexerEnvSchema, parseEnv } from \"./parseEnv\";\nimport { getClientOptions } from \"./getClientOptions\";\nimport { getBlock, getChainId } from \"viem/actions\";\nimport { getRpcClient } from \"@latticexyz/block-logs-stream\";\n\nconst env = parseEnv(\n z.intersection(\n indexerEnvSchema,\n z.object({\n DATABASE_URL: z.string(),\n HEALTHCHECK_HOST: z.string().optional(),\n HEALTHCHECK_PORT: z.coerce.number().optional(),\n }),\n ),\n);\n\nconst clientOptions = await getClientOptions(env);\n\nconst chainId = await getChainId(getRpcClient(clientOptions));\nconst database = drizzle(postgres(env.DATABASE_URL, { prepare: false }));\n\nif (await shouldCleanDatabase(database, chainId)) {\n console.log(\"outdated database detected, clearing data to start fresh\");\n await cleanDatabase(database);\n}\n\nconst { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database });\n\nlet startBlock = env.START_BLOCK;\n\nasync function getLatestStoredBlockNumber(): Promise<bigint | undefined> {\n // Fetch latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n // TODO: query if the DB exists instead of try/catch\n try {\n const chainState = await database\n .select()\n .from(tables.configTable)\n .where(eq(tables.configTable.chainId, chainId))\n .limit(1)\n .execute()\n // Get the first record in a way that returns a possible `undefined`\n // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true`\n .then((rows) => rows.find(() => true));\n\n return chainState?.blockNumber;\n } catch (error) {\n // ignore errors for now\n }\n}\n\nasync function getDistanceFromFollowBlock(): Promise<bigint> {\n const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([\n getLatestStoredBlockNumber(),\n getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }),\n ]);\n return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n);\n}\n\nconst latestStoredBlockNumber = await getLatestStoredBlockNumber();\nif (latestStoredBlockNumber != null) {\n startBlock = latestStoredBlockNumber + 1n;\n console.log(\"resuming from block number\", startBlock);\n}\n\nconst { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({\n ...clientOptions,\n storageAdapter,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n startBlock,\n maxBlockRange: env.MAX_BLOCK_RANGE,\n address: env.STORE_ADDRESS,\n});\n\nstoredBlockLogs$.subscribe();\n\nlet isCaughtUp = false;\ncombineLatest([latestBlockNumber$, storedBlockLogs$])\n .pipe(\n filter(\n ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) =>\n latestBlockNumber === lastBlockNumberProcessed,\n ),\n first(),\n )\n .subscribe(() => {\n isCaughtUp = true;\n console.log(\"all caught up\");\n });\n\nif (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {\n const { default: Koa } = await import(\"koa\");\n const { default: cors } = await import(\"@koa/cors\");\n const { healthcheck } = await import(\"../koa-middleware/healthcheck\");\n const { metrics } = await import(\"../koa-middleware/metrics\");\n const { helloWorld } = await import(\"../koa-middleware/helloWorld\");\n\n const server = new Koa();\n\n server.use(cors());\n server.use(\n healthcheck({\n isReady: () => isCaughtUp,\n }),\n );\n server.use(\n metrics({\n isHealthy: () => true,\n isReady: () => isCaughtUp,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n }),\n );\n server.use(helloWorld());\n\n server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT });\n console.log(\n `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`,\n );\n}\n"],"mappings":";yFACA,MAAO,gBACP,OAAS,KAAAA,MAAS,MAClB,OAAS,MAAAC,MAAU,cACnB,OAAS,iBAAAC,EAAe,UAAAC,EAAQ,SAAAC,MAAa,OAC7C,OAAS,WAAAC,MAAe,0BACxB,OAAOC,MAAc,WACrB,OAAS,iBAAAC,EAAe,wBAAAC,EAAsB,uBAAAC,MAA2B,kCACzE,OAAS,mBAAAC,MAAuB,yBAGhC,OAAS,YAAAC,EAAU,cAAAC,MAAkB,eACrC,OAAS,gBAAAC,MAAoB,gCAE7B,IAAMC,EAAMC,EACVC,EAAE,aACAC,EACAD,EAAE,OAAO,CACP,aAAcA,EAAE,OAAO,EACvB,iBAAkBA,EAAE,OAAO,EAAE,SAAS,EACtC,iBAAkBA,EAAE,OAAO,OAAO,EAAE,SAAS,CAC/C,CAAC,CACH,CACF,EAEME,EAAgB,MAAMC,EAAiBL,CAAG,EAE1CM,EAAU,MAAMR,EAAWC,EAAaK,CAAa,CAAC,EACtDG,EAAWC,EAAQC,EAAST,EAAI,aAAc,CAAE,QAAS,EAAM,CAAC,C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AAe,CAC7B,CAAC,EAEH,GAAIzB,EAAI,kBAAoB,MAAQA,EAAI,kBAAoB,KAAM,CAChE,GAAM,CAAE,QAAS+B,CAAI,EAAI,KAAM,QAAO,KAAK,EACrC,CAAE,QAASC,CAAK,EAAI,KAAM,QAAO,WAAW,EAC5C,CAAE,YAAAC,CAAY,EAAI,KAAM,QAAO,4BAA+B,EAC9D,CAAE,QAAAC,CAAQ,EAAI,KAAM,QAAO,wBAA2B,EACtD,CAAE,WAAAC,CAAW,EAAI,KAAM,QAAO,2BAA8B,EAE5DC,EAAS,IAAIL,EAEnBK,EAAO,IAAIJ,EAAK,CAAC,EACjBI,EAAO,IACLH,EAAY,CACV,QAAS,IAAMR,CACjB,CAAC,CACH,EACAW,EAAO,IACLF,EAAQ,CACN,UAAW,IAAM,GACjB,QAAS,IAAMT,EACf,2BAAAT,EACA,2BAAAG,EACA,eAAgBnB,EAAI,gBACtB,CAAC,CACH,EACAoC,EAAO,IAAID,EAAW,CAAC,EAEvBC,EAAO,OAAO,CAAE,KAAMpC,EAAI,iBAAkB,KAAMA,EAAI,gBAAiB,CAAC,EACxE,QAAQ,IACN,2DAA2DA,EAAI,gBAAgB,IAAIA,EAAI,gBAAgB,EACzG,CACF","names":["z","eq","combineLatest","filter","first","drizzle","postgres","cleanDatabase","createStorageAdapter","shouldCleanDatabase","createStoreSync","getBlock","getChainId","getRpcClient","env","parseEnv","z","indexerEnvSchema","clientOptions","getClientOptions","chainId","database","drizzle","postgres","shouldCleanDatabase","cleanDatabase","storageAdapter","tables","createStorageAdapter","startBlock","getLatestStoredBlockNumber","eq","rows","getDistanceFromFollowBlock","latestStoredBlockNumber","latestFollowBlock","latestBlockNumber$","storedBlockLogs$","createStoreSync","isCaughtUp","combineLatest","filter","latestBlockNumber","lastBlockNumberProcessed","first","Koa","cors","healthcheck","metrics","helloWorld","server"]}
@@ -1,245 +1,3 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- compress
4
- } from "../chunk-66BWQNF7.js";
5
- import {
6
- debug,
7
- sentry
8
- } from "../chunk-R7UQFYRA.js";
9
- import {
10
- getClientOptions
11
- } from "../chunk-CGE4ONKA.js";
12
- import {
13
- frontendEnvSchema,
14
- indexerEnvSchema,
15
- parseEnv
16
- } from "../chunk-L5CWEDU6.js";
17
- import {
18
- healthcheck
19
- } from "../chunk-SJLOWI5M.js";
20
- import {
21
- helloWorld
22
- } from "../chunk-GDNGJPVT.js";
23
- import {
24
- metrics
25
- } from "../chunk-O4XAWAXU.js";
26
-
27
- // src/bin/sqlite-indexer.ts
28
- import "dotenv/config";
29
- import fs from "node:fs";
30
- import { z } from "zod";
31
- import { eq as eq2 } from "drizzle-orm";
32
- import { drizzle } from "drizzle-orm/better-sqlite3";
33
- import Database from "better-sqlite3";
34
- import Koa from "koa";
35
- import cors from "@koa/cors";
36
- import { createKoaMiddleware } from "trpc-koa-adapter";
37
- import { createAppRouter } from "@latticexyz/store-sync/trpc-indexer";
38
- import { chainState as chainState2, schemaVersion, syncToSqlite } from "@latticexyz/store-sync/sqlite";
39
-
40
- // src/sqlite/getTablesWithRecords.ts
41
- import { asc, eq } from "drizzle-orm";
42
- import { buildTable, chainState, getTables } from "@latticexyz/store-sync/sqlite";
43
- import { getAddress } from "viem";
44
- import { decodeDynamicField } from "@latticexyz/protocol-parser/internal";
45
- import { hexToResource } from "@latticexyz/common";
46
- import { mapObject } from "@latticexyz/common/utils";
47
- function getTablesWithRecords(database, {
48
- chainId,
49
- address,
50
- filters = []
51
- }) {
52
- const metadata = database.select().from(chainState).where(eq(chainState.chainId, chainId)).limit(1).all().find(() => true);
53
- const tableIds = Array.from(new Set(filters.map((filter2) => filter2.tableId)));
54
- const tables = getTables(database).filter((table) => address == null || getAddress(address) === getAddress(table.address)).filter((table) => !tableIds.length || tableIds.includes(table.tableId));
55
- const tablesWithRecords = tables.map((table) => {
56
- const sqliteTable = buildTable(table);
57
- const records = database.select().from(sqliteTable).where(eq(sqliteTable.__isDeleted, false)).orderBy(
58
- asc(sqliteTable.__lastUpdatedBlockNumber)
59
- // TODO: add logIndex (https://github.com/latticexyz/mud/issues/1979)
60
- ).all();
61
- const filteredRecords = !filters.length ? records : records.filter((record) => {
62
- const keyTuple = decodeDynamicField("bytes32[]", record.__key);
63
- return filters.some(
64
- (filter2) => filter2.tableId === table.tableId && (filter2.key0 == null || filter2.key0 === keyTuple[0]) && (filter2.key1 == null || filter2.key1 === keyTuple[1])
65
- );
66
- });
67
- const resource = hexToResource(table.tableId);
68
- return {
69
- ...table,
70
- type: resource.type,
71
- schema: mapObject({ ...table.keySchema, ...table.valueSchema }, (type) => ({ type, internalType: type })),
72
- key: Object.keys(table.keySchema),
73
- records: filteredRecords.map((record) => {
74
- const key = Object.fromEntries(Object.entries(table.keySchema).map(([name]) => [name, record[name]]));
75
- const value = Object.fromEntries(Object.entries(table.valueSchema).map(([name]) => [name, record[name]]));
76
- return { key, value, fields: { ...key, ...value } };
77
- })
78
- };
79
- });
80
- return {
81
- blockNumber: metadata?.lastUpdatedBlockNumber ?? null,
82
- tables: tablesWithRecords
83
- };
84
- }
85
-
86
- // src/sqlite/createQueryAdapter.ts
87
- import { tablesWithRecordsToLogs } from "@latticexyz/store-sync";
88
- async function createQueryAdapter(database) {
89
- const adapter = {
90
- async getLogs(opts) {
91
- const { blockNumber, tables } = getTablesWithRecords(database, opts);
92
- const logs = tablesWithRecordsToLogs(tables);
93
- return { blockNumber: blockNumber ?? 0n, logs };
94
- },
95
- async findAll(opts) {
96
- return getTablesWithRecords(database, opts);
97
- }
98
- };
99
- return adapter;
100
- }
101
-
102
- // src/bin/sqlite-indexer.ts
103
- import { combineLatest, filter, first } from "rxjs";
104
-
105
- // src/sqlite/apiRoutes.ts
106
- import Router from "@koa/router";
107
- import compose from "koa-compose";
108
- import { input } from "@latticexyz/store-sync/indexer-client";
109
- import { schemasTable, tablesWithRecordsToLogs as tablesWithRecordsToLogs2 } from "@latticexyz/store-sync";
110
- import { createBenchmark } from "@latticexyz/common";
111
- function apiRoutes(database) {
112
- const router = new Router();
113
- router.get("/api/logs", compress(), async (ctx) => {
114
- const benchmark = createBenchmark("sqlite:logs");
115
- let options;
116
- try {
117
- options = input.parse(typeof ctx.query.input === "string" ? JSON.parse(ctx.query.input) : {});
118
- } catch (error) {
119
- ctx.status = 400;
120
- ctx.body = JSON.stringify(error);
121
- debug(error);
122
- return;
123
- }
124
- try {
125
- options.filters = options.filters.length > 0 ? [...options.filters, { tableId: schemasTable.tableId }] : [];
126
- benchmark("parse config");
127
- const { blockNumber, tables } = getTablesWithRecords(database, options);
128
- benchmark("query tables with records");
129
- const logs = tablesWithRecordsToLogs2(tables);
130
- benchmark("convert records to logs");
131
- ctx.body = JSON.stringify({ blockNumber: blockNumber?.toString() ?? "-1", logs });
132
- ctx.status = 200;
133
- } catch (error) {
134
- ctx.status = 500;
135
- ctx.body = JSON.stringify(error);
136
- debug(error);
137
- }
138
- });
139
- return compose([router.routes(), router.allowedMethods()]);
140
- }
141
-
142
- // src/bin/sqlite-indexer.ts
143
- import { getRpcClient } from "@latticexyz/block-logs-stream";
144
- import { getBlock, getChainId } from "viem/actions";
145
- (async () => {
146
- const env = parseEnv(
147
- z.intersection(
148
- z.intersection(indexerEnvSchema, frontendEnvSchema),
149
- z.object({
150
- SQLITE_FILENAME: z.string().default("indexer.db"),
151
- SENTRY_DSN: z.string().optional()
152
- })
153
- )
154
- );
155
- const clientOptions = await getClientOptions(env);
156
- const chainId = await getChainId(getRpcClient(clientOptions));
157
- const database = drizzle(new Database(env.SQLITE_FILENAME));
158
- let startBlock = env.START_BLOCK;
159
- async function getCurrentChainState() {
160
- try {
161
- const currentChainStates = database.select().from(chainState2).where(eq2(chainState2.chainId, chainId)).all();
162
- const currentChainState2 = currentChainStates[0];
163
- return currentChainState2;
164
- } catch (error) {
165
- }
166
- }
167
- async function getLatestStoredBlockNumber() {
168
- const currentChainState2 = await getCurrentChainState();
169
- return currentChainState2?.lastUpdatedBlockNumber ?? void 0;
170
- }
171
- async function getDistanceFromFollowBlock() {
172
- const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([
173
- getLatestStoredBlockNumber(),
174
- getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG })
175
- ]);
176
- return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n);
177
- }
178
- const currentChainState = await getCurrentChainState();
179
- if (currentChainState) {
180
- if (currentChainState.schemaVersion != schemaVersion) {
181
- console.log(
182
- "schema version changed from",
183
- currentChainState.schemaVersion,
184
- "to",
185
- schemaVersion,
186
- "recreating database"
187
- );
188
- fs.truncateSync(env.SQLITE_FILENAME);
189
- } else if (currentChainState.lastUpdatedBlockNumber != null) {
190
- console.log("resuming from block number", currentChainState.lastUpdatedBlockNumber + 1n);
191
- startBlock = currentChainState.lastUpdatedBlockNumber + 1n;
192
- }
193
- }
194
- const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({
195
- ...clientOptions,
196
- database,
197
- followBlockTag: env.FOLLOW_BLOCK_TAG,
198
- startBlock,
199
- maxBlockRange: env.MAX_BLOCK_RANGE,
200
- address: env.STORE_ADDRESS
201
- });
202
- let isCaughtUp = false;
203
- combineLatest([latestBlockNumber$, storedBlockLogs$]).pipe(
204
- filter(
205
- ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => latestBlockNumber === lastBlockNumberProcessed
206
- ),
207
- first()
208
- ).subscribe(() => {
209
- isCaughtUp = true;
210
- console.log("all caught up");
211
- });
212
- const server = new Koa();
213
- if (env.SENTRY_DSN) {
214
- server.use(sentry(env.SENTRY_DSN));
215
- }
216
- server.use(cors());
217
- server.use(
218
- healthcheck({
219
- isReady: () => isCaughtUp
220
- })
221
- );
222
- server.use(
223
- metrics({
224
- isHealthy: () => true,
225
- isReady: () => isCaughtUp,
226
- getLatestStoredBlockNumber,
227
- getDistanceFromFollowBlock,
228
- followBlockTag: env.FOLLOW_BLOCK_TAG
229
- })
230
- );
231
- server.use(helloWorld());
232
- server.use(apiRoutes(database));
233
- server.use(
234
- createKoaMiddleware({
235
- prefix: "/trpc",
236
- router: createAppRouter(),
237
- createContext: async () => ({
238
- queryAdapter: await createQueryAdapter(database)
239
- })
240
- })
241
- );
242
- server.listen({ host: env.HOST, port: env.PORT });
243
- console.log(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`);
244
- })();
2
+ import{a as C}from"../chunk-R7HX5BT2.js";import{a as S,c as w}from"../chunk-JDWVOODJ.js";import{a as A}from"../chunk-YQ7E5W26.js";import{a as R,b as L,c as O}from"../chunk-O2SDU7EQ.js";import{a as _}from"../chunk-7O2ZWWUX.js";import{a as E}from"../chunk-AYPBOJNL.js";import{a as I}from"../chunk-ED45N3IT.js";import"dotenv/config";import se from"node:fs";import{z as b}from"zod";import{eq as ne}from"drizzle-orm";import{drizzle as ie}from"drizzle-orm/better-sqlite3";import ce from"better-sqlite3";import le from"koa";import me from"@koa/cors";import{createKoaMiddleware as ue}from"trpc-koa-adapter";import{createAppRouter as de}from"@latticexyz/store-sync/trpc-indexer";import{chainState as q,schemaVersion as M,syncToSqlite as pe}from"@latticexyz/store-sync/sqlite";import{asc as G,eq as D}from"drizzle-orm";import{buildTable as J,chainState as Q,getTables as V}from"@latticexyz/store-sync/sqlite";import{getAddress as W}from"viem";import{decodeDynamicField as $}from"@latticexyz/protocol-parser/internal";import{hexToResource as z}from"@latticexyz/common";import{mapObject as Y}from"@latticexyz/common/utils";function f(e,{chainId:s,address:t,filters:n=[]}){let i=e.select().from(Q).where(D(Q.chainId,s)).limit(1).all().find(()=>!0),a=Array.from(new Set(n.map(r=>r.tableId))),h=V(e).filter(r=>t==null||W(t)===W(r.address)).filter(r=>!a.length||a.includes(r.tableId)).map(r=>{let g=J(r),N=e.select().from(g).where(D(g.__isDeleted,!1)).orderBy(G(g.__lastUpdatedBlockNumber)).all(),x=n.length?N.filter(m=>{let d=$("bytes32[]",m.__key);return n.some(c=>c.tableId===r.tableId&&(c.key0==null||c.key0===d[0])&&(c.key1==null||c.key1===d[1]))}):N,H=z(r.tableId);return{...r,type:H.type,schema:Y({...r.keySchema,...r.valueSchema},m=>({type:m,internalType:m})),key:Object.keys(r.keySchema),records:x.map(m=>{let d=Object.fromEntries(Object.entries(r.keySchema).map(([p])=>[p,m[p]])),c=Object.fromEntries(Object.entries(r.valueSchema).map(([p])=>[p,m[p]]));return{key:d,value:c,fields:{...d,...c}}})}});return{blockNumber:i?.lastUpdatedBlockNumber??null,tables:h}}import{tablesWithRecordsToLogs as X}from"@latticexyz/store-sync";async function v(e){return{async getLogs(t){let{blockNumber:n,tables:i}=f(e,t),a=X(i);return{blockNumber:n??0n,logs:a}},async findAll(t){return f(e,t)}}}import{combineLatest as fe,filter as be,first as ye}from"rxjs";import Z from"@koa/router";import ee from"koa-compose";import{input as te}from"@latticexyz/store-sync/indexer-client";import{schemasTable as re,tablesWithRecordsToLogs as oe}from"@latticexyz/store-sync";import{createBenchmark as ae}from"@latticexyz/common";function F(e){let s=new Z;return s.get("/api/logs",C(),async t=>{let n=ae("sqlite:logs"),i;try{i=te.parse(typeof t.query.input=="string"?JSON.parse(t.query.input):{})}catch(a){t.status=400,t.body=JSON.stringify(a),S(a);return}try{i.filters=i.filters.length>0?[...i.filters,{tableId:re.tableId}]:[],n("parse config");let{blockNumber:a,tables:B}=f(e,i);n("query tables with records");let h=oe(B);n("convert records to logs"),t.body=JSON.stringify({blockNumber:a?.toString()??"-1",logs:h}),t.status=200}catch(a){t.status=500,t.body=JSON.stringify(a),S(a)}}),ee([s.routes(),s.allowedMethods()])}import{getRpcClient as U}from"@latticexyz/block-logs-stream";import{getBlock as he,getChainId as ge}from"viem/actions";var o=O(b.intersection(b.intersection(L,R),b.object({SQLITE_FILENAME:b.string().default("indexer.db"),SENTRY_DSN:b.string().optional()}))),k=await A(o),Se=await ge(U(k)),y=ie(new ce(o.SQLITE_FILENAME)),j=o.START_BLOCK;async function K(){try{return y.select().from(q).where(ne(q.chainId,Se)).all()[0]}catch{}}async function P(){return(await K())?.lastUpdatedBlockNumber??void 0}async function ke(){let[e,s]=await Promise.all([P(),he(U(k),{blockTag:o.FOLLOW_BLOCK_TAG})]);return s.number-(e??-1n)}var u=await K();u&&(u.schemaVersion!=M?(console.log("schema version changed from",u.schemaVersion,"to",M,"recreating database"),se.truncateSync(o.SQLITE_FILENAME)):u.lastUpdatedBlockNumber!=null&&(console.log("resuming from block number",u.lastUpdatedBlockNumber+1n),j=u.lastUpdatedBlockNumber+1n));var{latestBlockNumber$:Te,storedBlockLogs$:Be}=await pe({...k,database:y,followBlockTag:o.FOLLOW_BLOCK_TAG,startBlock:j,maxBlockRange:o.MAX_BLOCK_RANGE,address:o.STORE_ADDRESS}),T=!1;fe([Te,Be]).pipe(be(([e,{blockNumber:s}])=>e===s),ye()).subscribe(()=>{T=!0,console.log("all caught up")});var l=new le;o.SENTRY_DSN&&l.use(w(o.SENTRY_DSN));l.use(me());l.use(_({isReady:()=>T}));l.use(I({isHealthy:()=>!0,isReady:()=>T,getLatestStoredBlockNumber:P,getDistanceFromFollowBlock:ke,followBlockTag:o.FOLLOW_BLOCK_TAG}));l.use(E());l.use(F(y));l.use(ue({prefix:"/trpc",router:de(),createContext:async()=>({queryAdapter:await v(y)})}));l.listen({host:o.HOST,port:o.PORT});console.log(`sqlite indexer frontend listening on http://${o.HOST}:${o.PORT}`);
245
3
  //# sourceMappingURL=sqlite-indexer.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/bin/sqlite-indexer.ts","../../src/sqlite/getTablesWithRecords.ts","../../src/sqlite/createQueryAdapter.ts","../../src/sqlite/apiRoutes.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport fs from \"node:fs\";\nimport { z } from \"zod\";\nimport { eq } from \"drizzle-orm\";\nimport { drizzle } from \"drizzle-orm/better-sqlite3\";\nimport Database from \"better-sqlite3\";\nimport Koa from \"koa\";\nimport cors from \"@koa/cors\";\nimport { createKoaMiddleware } from \"trpc-koa-adapter\";\nimport { createAppRouter } from \"@latticexyz/store-sync/trpc-indexer\";\nimport { chainState, schemaVersion, syncToSqlite } from \"@latticexyz/store-sync/sqlite\";\nimport { createQueryAdapter } from \"../sqlite/createQueryAdapter\";\nimport { combineLatest, filter, first } from \"rxjs\";\nimport { frontendEnvSchema, indexerEnvSchema, parseEnv } from \"./parseEnv\";\nimport { healthcheck } from \"../koa-middleware/healthcheck\";\nimport { helloWorld } from \"../koa-middleware/helloWorld\";\nimport { apiRoutes } from \"../sqlite/apiRoutes\";\nimport { sentry } from \"../koa-middleware/sentry\";\nimport { metrics } from \"../koa-middleware/metrics\";\nimport { getClientOptions } from \"./getClientOptions\";\nimport { getRpcClient } from \"@latticexyz/block-logs-stream\";\nimport { getBlock, getChainId } from \"viem/actions\";\n\n// Workaround for:\n// Top-level await is currently not supported with the \"cjs\" output format\n(async (): Promise<void> => {\n const env = parseEnv(\n z.intersection(\n z.intersection(indexerEnvSchema, frontendEnvSchema),\n z.object({\n SQLITE_FILENAME: z.string().default(\"indexer.db\"),\n SENTRY_DSN: z.string().optional(),\n }),\n ),\n );\n\n const clientOptions = await getClientOptions(env);\n\n const chainId = await getChainId(getRpcClient(clientOptions));\n const database = drizzle(new Database(env.SQLITE_FILENAME));\n\n let startBlock = env.START_BLOCK;\n\n async function getCurrentChainState(): Promise<\n | {\n schemaVersion: number;\n chainId: number;\n lastUpdatedBlockNumber: bigint | null;\n lastError: string | null;\n }\n | undefined\n > {\n // This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n try {\n const currentChainStates = database.select().from(chainState).where(eq(chainState.chainId, chainId)).all();\n // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212)\n const currentChainState: (typeof currentChainStates)[number] | undefined = currentChainStates[0];\n return currentChainState;\n } catch (error) {\n // ignore errors, this is optional\n }\n }\n\n async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {\n const currentChainState = await getCurrentChainState();\n return currentChainState?.lastUpdatedBlockNumber ?? undefined;\n }\n\n async function getDistanceFromFollowBlock(): Promise<bigint> {\n const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([\n getLatestStoredBlockNumber(),\n getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }),\n ]);\n return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n);\n }\n\n const currentChainState = await getCurrentChainState();\n if (currentChainState) {\n // Reset the db if the version changed\n if (currentChainState.schemaVersion != schemaVersion) {\n console.log(\n \"schema version changed from\",\n currentChainState.schemaVersion,\n \"to\",\n schemaVersion,\n \"recreating database\",\n );\n fs.truncateSync(env.SQLITE_FILENAME);\n } else if (currentChainState.lastUpdatedBlockNumber != null) {\n // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n console.log(\"resuming from block number\", currentChainState.lastUpdatedBlockNumber + 1n);\n startBlock = currentChainState.lastUpdatedBlockNumber + 1n;\n }\n }\n\n const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({\n ...clientOptions,\n database,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n startBlock,\n maxBlockRange: env.MAX_BLOCK_RANGE,\n address: env.STORE_ADDRESS,\n });\n\n let isCaughtUp = false;\n combineLatest([latestBlockNumber$, storedBlockLogs$])\n .pipe(\n filter(\n ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) =>\n latestBlockNumber === lastBlockNumberProcessed,\n ),\n first(),\n )\n .subscribe(() => {\n isCaughtUp = true;\n console.log(\"all caught up\");\n });\n\n const server = new Koa();\n\n if (env.SENTRY_DSN) {\n server.use(sentry(env.SENTRY_DSN));\n }\n\n server.use(cors());\n server.use(\n healthcheck({\n isReady: () => isCaughtUp,\n }),\n );\n server.use(\n metrics({\n isHealthy: () => true,\n isReady: () => isCaughtUp,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n }),\n );\n server.use(helloWorld());\n server.use(apiRoutes(database));\n\n server.use(\n createKoaMiddleware({\n prefix: \"/trpc\",\n router: createAppRouter(),\n createContext: async () => ({\n queryAdapter: await createQueryAdapter(database),\n }),\n }),\n );\n\n server.listen({ host: env.HOST, port: env.PORT });\n console.log(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`);\n})();\n","import { asc, eq } from \"drizzle-orm\";\nimport { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\nimport { buildTable, chainState, getTables } from \"@latticexyz/store-sync/sqlite\";\nimport { Hex, getAddress } from \"viem\";\nimport { decodeDynamicField } from \"@latticexyz/protocol-parser/internal\";\nimport { SyncFilter, TableRecord, TableWithRecords } from \"@latticexyz/store-sync\";\nimport { hexToResource } from \"@latticexyz/common\";\nimport { mapObject } from \"@latticexyz/common/utils\";\n\n// TODO: refactor sqlite and replace this with getLogs to match postgres (https://github.com/latticexyz/mud/issues/1970)\n\n/**\n * @deprecated\n * */\nexport function getTablesWithRecords(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n database: BaseSQLiteDatabase<\"sync\", any>,\n {\n chainId,\n address,\n filters = [],\n }: {\n readonly chainId: number;\n readonly address?: Hex;\n readonly filters?: readonly SyncFilter[];\n },\n): { blockNumber: bigint | null; tables: readonly TableWithRecords[] } {\n const metadata = database\n .select()\n .from(chainState)\n .where(eq(chainState.chainId, chainId))\n .limit(1)\n .all()\n .find(() => true);\n\n // If _any_ filter has a table ID, this will filter down all data to just those tables. Which mean we can't yet mix table filters with key-only filters.\n // TODO: improve this so we can express this in the query (need to be able to query data across tables more easily)\n const tableIds = Array.from(new Set(filters.map((filter) => filter.tableId)));\n const tables = getTables(database)\n .filter((table) => address == null || getAddress(address) === getAddress(table.address))\n .filter((table) => !tableIds.length || tableIds.includes(table.tableId));\n\n const tablesWithRecords = tables.map((table) => {\n const sqliteTable = buildTable(table);\n const records = database\n .select()\n .from(sqliteTable)\n .where(eq(sqliteTable.__isDeleted, false))\n .orderBy(\n asc(sqliteTable.__lastUpdatedBlockNumber),\n // TODO: add logIndex (https://github.com/latticexyz/mud/issues/1979)\n )\n .all();\n const filteredRecords = !filters.length\n ? records\n : records.filter((record) => {\n const keyTuple = decodeDynamicField(\"bytes32[]\", record.__key);\n return filters.some(\n (filter) =>\n filter.tableId === table.tableId &&\n (filter.key0 == null || filter.key0 === keyTuple[0]) &&\n (filter.key1 == null || filter.key1 === keyTuple[1]),\n );\n });\n const resource = hexToResource(table.tableId);\n return {\n ...table,\n type: resource.type as never,\n schema: mapObject({ ...table.keySchema, ...table.valueSchema }, (type) => ({ type, internalType: type })),\n key: Object.keys(table.keySchema),\n records: filteredRecords.map((record): TableRecord => {\n const key = Object.fromEntries(Object.entries(table.keySchema).map(([name]) => [name, record[name]]));\n const value = Object.fromEntries(Object.entries(table.valueSchema).map(([name]) => [name, record[name]]));\n return { key, value, fields: { ...key, ...value } };\n }),\n } satisfies TableWithRecords;\n });\n\n return {\n blockNumber: metadata?.lastUpdatedBlockNumber ?? null,\n tables: tablesWithRecords,\n };\n}\n","import { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\nimport { QueryAdapter } from \"@latticexyz/store-sync/trpc-indexer\";\nimport { getTablesWithRecords } from \"./getTablesWithRecords\";\nimport { tablesWithRecordsToLogs } from \"@latticexyz/store-sync\";\n\n/**\n * Creates a storage adapter for the tRPC server/client to query data from SQLite.\n *\n * @param {BaseSQLiteDatabase<\"sync\", any>} database SQLite database object from Drizzle\n * @returns {Promise<QueryAdapter>} A set of methods used by tRPC endpoints.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function createQueryAdapter(database: BaseSQLiteDatabase<\"sync\", any>): Promise<QueryAdapter> {\n const adapter: QueryAdapter = {\n async getLogs(opts) {\n const { blockNumber, tables } = getTablesWithRecords(database, opts);\n const logs = tablesWithRecordsToLogs(tables);\n return { blockNumber: blockNumber ?? 0n, logs };\n },\n async findAll(opts) {\n return getTablesWithRecords(database, opts);\n },\n };\n return adapter;\n}\n","import { Middleware } from \"koa\";\nimport Router from \"@koa/router\";\nimport compose from \"koa-compose\";\nimport { input } from \"@latticexyz/store-sync/indexer-client\";\nimport { schemasTable, tablesWithRecordsToLogs } from \"@latticexyz/store-sync\";\nimport { debug } from \"../debug\";\nimport { createBenchmark } from \"@latticexyz/common\";\nimport { compress } from \"../koa-middleware/compress\";\nimport { getTablesWithRecords } from \"./getTablesWithRecords\";\nimport { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function apiRoutes(database: BaseSQLiteDatabase<\"sync\", any>): Middleware {\n const router = new Router();\n\n router.get(\"/api/logs\", compress(), async (ctx) => {\n const benchmark = createBenchmark(\"sqlite:logs\");\n\n let options: ReturnType<typeof input.parse>;\n\n try {\n options = input.parse(typeof ctx.query.input === \"string\" ? JSON.parse(ctx.query.input) : {});\n } catch (error) {\n ctx.status = 400;\n ctx.body = JSON.stringify(error);\n debug(error);\n return;\n }\n\n try {\n options.filters = options.filters.length > 0 ? [...options.filters, { tableId: schemasTable.tableId }] : [];\n benchmark(\"parse config\");\n const { blockNumber, tables } = getTablesWithRecords(database, options);\n benchmark(\"query tables with records\");\n const logs = tablesWithRecordsToLogs(tables);\n benchmark(\"convert records to logs\");\n\n ctx.body = JSON.stringify({ blockNumber: blockNumber?.toString() ?? \"-1\", logs });\n ctx.status = 200;\n } catch (error) {\n ctx.status = 500;\n ctx.body = JSON.stringify(error);\n debug(error);\n }\n });\n\n return compose([router.routes(), router.allowedMethods()]) as Middleware;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO;AACP,OAAO,QAAQ;AACf,SAAS,SAAS;AAClB,SAAS,MAAAA,WAAU;AACnB,SAAS,eAAe;AACxB,OAAO,cAAc;AACrB,OAAO,SAAS;AAChB,OAAO,UAAU;AACjB,SAAS,2BAA2B;AACpC,SAAS,uBAAuB;AAChC,SAAS,cAAAC,aAAY,eAAe,oBAAoB;;;ACXxD,SAAS,KAA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AAAA,IACnB,SAAS,IAAI;AAAA,EACf,CAAC;AAED,MAAI,aAAa;AACjB,gBAAc,CAAC,oBAAoB,gBAAgB,CAAC,EACjD;AAAA,IACC;AAAA,MACE,CAAC,CAAC,mBAAmB,EAAE,aAAa,yBAAyB,CAAC,MAC5D,sBAAsB;AAAA,IAC1B;AAAA,IACA,MAAM;AAAA,EACR,EACC,UAAU,MAAM;AACf,iBAAa;AACb,YAAQ,IAAI,eAAe;AAAA,EAC7B,CAAC;AAEH,QAAM,SAAS,IAAI,IAAI;AAEvB,MAAI,IAAI,YAAY;AAClB,WAAO,IAAI,OAAO,IAAI,UAAU,CAAC;AAAA,EACnC;AAEA,SAAO,IAAI,KAAK,CAAC;AACjB,SAAO;AAAA,IACL,YAAY;AAAA,MACV,SAAS,MAAM;AAAA,IACjB,CAAC;AAAA,EACH;AACA,SAAO;AAAA,IACL,QAAQ;AAAA,MACN,WAAW,MAAM;AAAA,MACjB,SAAS,MAAM;AAAA,MACf;AAAA,MACA;AAAA,MACA,gBAAgB,IAAI;AAAA,IACtB,CAAC;AAAA,EACH;AACA,SAAO,IAAI,WAAW,CAAC;AACvB,SAAO,IAAI,UAAU,QAAQ,CAAC;AAE9B,SAAO;AAAA,IACL,oBAAoB;AAAA,MAClB,QAAQ;AAAA,MACR,QAAQ,gBAAgB;AAAA,MACxB,eAAe,aAAa;AAAA,QAC1B,cAAc,MAAM,mBAAmB,QAAQ;AAAA,MACjD;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,OAAO,EAAE,MAAM,IAAI,MAAM,MAAM,IAAI,KAAK,CAAC;AAChD,UAAQ,IAAI,+CAA+C,IAAI,IAAI,IAAI,IAAI,IAAI,EAAE;AACnF,GAAG;","names":["eq","chainState","filter","tablesWithRecordsToLogs","tablesWithRecordsToLogs","chainState","eq","currentChainState"]}
1
+ {"version":3,"sources":["../../src/bin/sqlite-indexer.ts","../../src/sqlite/getTablesWithRecords.ts","../../src/sqlite/createQueryAdapter.ts","../../src/sqlite/apiRoutes.ts"],"sourcesContent":["#!/usr/bin/env node\nimport \"dotenv/config\";\nimport fs from \"node:fs\";\nimport { z } from \"zod\";\nimport { eq } from \"drizzle-orm\";\nimport { drizzle } from \"drizzle-orm/better-sqlite3\";\nimport Database from \"better-sqlite3\";\nimport Koa from \"koa\";\nimport cors from \"@koa/cors\";\nimport { createKoaMiddleware } from \"trpc-koa-adapter\";\nimport { createAppRouter } from \"@latticexyz/store-sync/trpc-indexer\";\nimport { chainState, schemaVersion, syncToSqlite } from \"@latticexyz/store-sync/sqlite\";\nimport { createQueryAdapter } from \"../sqlite/createQueryAdapter\";\nimport { combineLatest, filter, first } from \"rxjs\";\nimport { frontendEnvSchema, indexerEnvSchema, parseEnv } from \"./parseEnv\";\nimport { healthcheck } from \"../koa-middleware/healthcheck\";\nimport { helloWorld } from \"../koa-middleware/helloWorld\";\nimport { apiRoutes } from \"../sqlite/apiRoutes\";\nimport { sentry } from \"../koa-middleware/sentry\";\nimport { metrics } from \"../koa-middleware/metrics\";\nimport { getClientOptions } from \"./getClientOptions\";\nimport { getRpcClient } from \"@latticexyz/block-logs-stream\";\nimport { getBlock, getChainId } from \"viem/actions\";\n\nconst env = parseEnv(\n z.intersection(\n z.intersection(indexerEnvSchema, frontendEnvSchema),\n z.object({\n SQLITE_FILENAME: z.string().default(\"indexer.db\"),\n SENTRY_DSN: z.string().optional(),\n }),\n ),\n);\n\nconst clientOptions = await getClientOptions(env);\n\nconst chainId = await getChainId(getRpcClient(clientOptions));\nconst database = drizzle(new Database(env.SQLITE_FILENAME));\n\nlet startBlock = env.START_BLOCK;\n\nasync function getCurrentChainState(): Promise<\n | {\n schemaVersion: number;\n chainId: number;\n lastUpdatedBlockNumber: bigint | null;\n lastError: string | null;\n }\n | undefined\n> {\n // This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n try {\n const currentChainStates = database.select().from(chainState).where(eq(chainState.chainId, chainId)).all();\n // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212)\n const currentChainState: (typeof currentChainStates)[number] | undefined = currentChainStates[0];\n return currentChainState;\n } catch (error) {\n // ignore errors, this is optional\n }\n}\n\nasync function getLatestStoredBlockNumber(): Promise<bigint | undefined> {\n const currentChainState = await getCurrentChainState();\n return currentChainState?.lastUpdatedBlockNumber ?? undefined;\n}\n\nasync function getDistanceFromFollowBlock(): Promise<bigint> {\n const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([\n getLatestStoredBlockNumber(),\n getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }),\n ]);\n return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n);\n}\n\nconst currentChainState = await getCurrentChainState();\nif (currentChainState) {\n // Reset the db if the version changed\n if (currentChainState.schemaVersion != schemaVersion) {\n console.log(\n \"schema version changed from\",\n currentChainState.schemaVersion,\n \"to\",\n schemaVersion,\n \"recreating database\",\n );\n fs.truncateSync(env.SQLITE_FILENAME);\n } else if (currentChainState.lastUpdatedBlockNumber != null) {\n // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.\n console.log(\"resuming from block number\", currentChainState.lastUpdatedBlockNumber + 1n);\n startBlock = currentChainState.lastUpdatedBlockNumber + 1n;\n }\n}\n\nconst { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({\n ...clientOptions,\n database,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n startBlock,\n maxBlockRange: env.MAX_BLOCK_RANGE,\n address: env.STORE_ADDRESS,\n});\n\nlet isCaughtUp = false;\ncombineLatest([latestBlockNumber$, storedBlockLogs$])\n .pipe(\n filter(\n ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) =>\n latestBlockNumber === lastBlockNumberProcessed,\n ),\n first(),\n )\n .subscribe(() => {\n isCaughtUp = true;\n console.log(\"all caught up\");\n });\n\nconst server = new Koa();\n\nif (env.SENTRY_DSN) {\n server.use(sentry(env.SENTRY_DSN));\n}\n\nserver.use(cors());\nserver.use(\n healthcheck({\n isReady: () => isCaughtUp,\n }),\n);\nserver.use(\n metrics({\n isHealthy: () => true,\n isReady: () => isCaughtUp,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag: env.FOLLOW_BLOCK_TAG,\n }),\n);\nserver.use(helloWorld());\nserver.use(apiRoutes(database));\n\nserver.use(\n createKoaMiddleware({\n prefix: \"/trpc\",\n router: createAppRouter(),\n createContext: async () => ({\n queryAdapter: await createQueryAdapter(database),\n }),\n }),\n);\n\nserver.listen({ host: env.HOST, port: env.PORT });\nconsole.log(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`);\n","import { asc, eq } from \"drizzle-orm\";\nimport { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\nimport { buildTable, chainState, getTables } from \"@latticexyz/store-sync/sqlite\";\nimport { Hex, getAddress } from \"viem\";\nimport { decodeDynamicField } from \"@latticexyz/protocol-parser/internal\";\nimport { SyncFilter, TableRecord, TableWithRecords } from \"@latticexyz/store-sync\";\nimport { hexToResource } from \"@latticexyz/common\";\nimport { mapObject } from \"@latticexyz/common/utils\";\n\n// TODO: refactor sqlite and replace this with getLogs to match postgres (https://github.com/latticexyz/mud/issues/1970)\n\n/**\n * @deprecated\n * */\nexport function getTablesWithRecords(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n database: BaseSQLiteDatabase<\"sync\", any>,\n {\n chainId,\n address,\n filters = [],\n }: {\n readonly chainId: number;\n readonly address?: Hex;\n readonly filters?: readonly SyncFilter[];\n },\n): { blockNumber: bigint | null; tables: readonly TableWithRecords[] } {\n const metadata = database\n .select()\n .from(chainState)\n .where(eq(chainState.chainId, chainId))\n .limit(1)\n .all()\n .find(() => true);\n\n // If _any_ filter has a table ID, this will filter down all data to just those tables. Which mean we can't yet mix table filters with key-only filters.\n // TODO: improve this so we can express this in the query (need to be able to query data across tables more easily)\n const tableIds = Array.from(new Set(filters.map((filter) => filter.tableId)));\n const tables = getTables(database)\n .filter((table) => address == null || getAddress(address) === getAddress(table.address))\n .filter((table) => !tableIds.length || tableIds.includes(table.tableId));\n\n const tablesWithRecords = tables.map((table) => {\n const sqliteTable = buildTable(table);\n const records = database\n .select()\n .from(sqliteTable)\n .where(eq(sqliteTable.__isDeleted, false))\n .orderBy(\n asc(sqliteTable.__lastUpdatedBlockNumber),\n // TODO: add logIndex (https://github.com/latticexyz/mud/issues/1979)\n )\n .all();\n const filteredRecords = !filters.length\n ? records\n : records.filter((record) => {\n const keyTuple = decodeDynamicField(\"bytes32[]\", record.__key);\n return filters.some(\n (filter) =>\n filter.tableId === table.tableId &&\n (filter.key0 == null || filter.key0 === keyTuple[0]) &&\n (filter.key1 == null || filter.key1 === keyTuple[1]),\n );\n });\n const resource = hexToResource(table.tableId);\n return {\n ...table,\n type: resource.type as never,\n schema: mapObject({ ...table.keySchema, ...table.valueSchema }, (type) => ({ type, internalType: type })),\n key: Object.keys(table.keySchema),\n records: filteredRecords.map((record): TableRecord => {\n const key = Object.fromEntries(Object.entries(table.keySchema).map(([name]) => [name, record[name]]));\n const value = Object.fromEntries(Object.entries(table.valueSchema).map(([name]) => [name, record[name]]));\n return { key, value, fields: { ...key, ...value } };\n }),\n } satisfies TableWithRecords;\n });\n\n return {\n blockNumber: metadata?.lastUpdatedBlockNumber ?? null,\n tables: tablesWithRecords,\n };\n}\n","import { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\nimport { QueryAdapter } from \"@latticexyz/store-sync/trpc-indexer\";\nimport { getTablesWithRecords } from \"./getTablesWithRecords\";\nimport { tablesWithRecordsToLogs } from \"@latticexyz/store-sync\";\n\n/**\n * Creates a storage adapter for the tRPC server/client to query data from SQLite.\n *\n * @param {BaseSQLiteDatabase<\"sync\", any>} database SQLite database object from Drizzle\n * @returns {Promise<QueryAdapter>} A set of methods used by tRPC endpoints.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function createQueryAdapter(database: BaseSQLiteDatabase<\"sync\", any>): Promise<QueryAdapter> {\n const adapter: QueryAdapter = {\n async getLogs(opts) {\n const { blockNumber, tables } = getTablesWithRecords(database, opts);\n const logs = tablesWithRecordsToLogs(tables);\n return { blockNumber: blockNumber ?? 0n, logs };\n },\n async findAll(opts) {\n return getTablesWithRecords(database, opts);\n },\n };\n return adapter;\n}\n","import { Middleware } from \"koa\";\nimport Router from \"@koa/router\";\nimport compose from \"koa-compose\";\nimport { input } from \"@latticexyz/store-sync/indexer-client\";\nimport { schemasTable, tablesWithRecordsToLogs } from \"@latticexyz/store-sync\";\nimport { debug } from \"../debug\";\nimport { createBenchmark } from \"@latticexyz/common\";\nimport { compress } from \"../koa-middleware/compress\";\nimport { getTablesWithRecords } from \"./getTablesWithRecords\";\nimport { BaseSQLiteDatabase } from \"drizzle-orm/sqlite-core\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function apiRoutes(database: BaseSQLiteDatabase<\"sync\", any>): Middleware {\n const router = new Router();\n\n router.get(\"/api/logs\", compress(), async (ctx) => {\n const benchmark = createBenchmark(\"sqlite:logs\");\n\n let options: ReturnType<typeof input.parse>;\n\n try {\n options = input.parse(typeof ctx.query.input === \"string\" ? JSON.parse(ctx.query.input) : {});\n } catch (error) {\n ctx.status = 400;\n ctx.body = JSON.stringify(error);\n debug(error);\n return;\n }\n\n try {\n options.filters = options.filters.length > 0 ? [...options.filters, { tableId: schemasTable.tableId }] : [];\n benchmark(\"parse config\");\n const { blockNumber, tables } = getTablesWithRecords(database, options);\n benchmark(\"query tables with records\");\n const logs = tablesWithRecordsToLogs(tables);\n benchmark(\"convert records to logs\");\n\n ctx.body = JSON.stringify({ blockNumber: blockNumber?.toString() ?? \"-1\", logs });\n ctx.status = 200;\n } catch (error) {\n ctx.status = 500;\n ctx.body = JSON.stringify(error);\n debug(error);\n }\n });\n\n return compose([router.routes(), router.allowedMethods()]) as Middleware;\n}\n"],"mappings":";oTACA,MAAO,gBACP,OAAOA,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EAAI,iBACpB,WAAAW,EACA,cAAeX,EAAI,gBACnB,QAASA,EAAI,aACf,CAAC,EAEGyB,EAAa,GACjBC,GAAc,CAACJ,GAAoBC,EAAgB,CAAC,EACjD,KACCI,GACE,CAAC,CAACC,EAAmB,CAAE,YAAaC,CAAyB,CAAC,IAC5DD,IAAsBC,CAC1B,EACAC,GAAM,CACR,EACC,UAAU,IAAM,CACfL,EAAa,GACb,QAAQ,IAAI,eAAe,CAC7B,CAAC,EAEH,IAAMM,EAAS,IAAIC,GAEfhC,EAAI,YACN+B,EAAO,IAAIE,EAAOjC,EAAI,UAAU,CAAC,EAGnC+B,EAAO,IAAIG,GAAK,CAAC,EACjBH,EAAO,IACLI,EAAY,CACV,QAAS,IAAMV,CACjB,CAAC,CACH,EACAM,EAAO,IACLK,EAAQ,CACN,UAAW,IAAM,GACjB,QAAS,IAAMX,EACf,2BAAAV,EACA,2BAAAC,GACA,eAAgBhB,EAAI,gBACtB,CAAC,CACH,EACA+B,EAAO,IAAIM,EAAW,CAAC,EACvBN,EAAO,IAAIO,EAAU9B,CAAQ,CAAC,EAE9BuB,EAAO,IACLQ,GAAoB,CAClB,OAAQ,QACR,OAAQC,GAAgB,EACxB,cAAe,UAAa,CAC1B,aAAc,MAAMC,EAAmBjC,CAAQ,CACjD,EACF,CAAC,CACH,EAEAuB,EAAO,OAAO,CAAE,KAAM/B,EAAI,KAAM,KAAMA,EAAI,IAAK,CAAC,EAChD,QAAQ,IAAI,+CAA+CA,EAAI,IAAI,IAAIA,EAAI,IAAI,EAAE","names":["fs","z","eq","drizzle","Database","Koa","cors","createKoaMiddleware","createAppRouter","chainState","schemaVersion","syncToSqlite","asc","eq","buildTable","chainState","getTables","getAddress","decodeDynamicField","hexToResource","mapObject","getTablesWithRecords","database","chainId","address","filters","metadata","tableIds","filter","tablesWithRecords","table","sqliteTable","records","filteredRecords","record","keyTuple","resource","type","key","name","value","tablesWithRecordsToLogs","createQueryAdapter","database","opts","blockNumber","tables","getTablesWithRecords","logs","combineLatest","filter","first","Router","compose","input","schemasTable","tablesWithRecordsToLogs","createBenchmark","apiRoutes","database","router","Router","compress","ctx","benchmark","createBenchmark","options","input","error","debug","schemasTable","blockNumber","tables","getTablesWithRecords","logs","tablesWithRecordsToLogs","compose","getRpcClient","getBlock","getChainId","env","parseEnv","z","indexerEnvSchema","frontendEnvSchema","clientOptions","getClientOptions","chainId","database","drizzle","Database","startBlock","getCurrentChainState","chainState","eq","getLatestStoredBlockNumber","getDistanceFromFollowBlock","latestStoredBlockNumber","latestFollowBlock","currentChainState","schemaVersion","fs","latestBlockNumber$","storedBlockLogs$","syncToSqlite","isCaughtUp","combineLatest","filter","latestBlockNumber","lastBlockNumberProcessed","first","server","Koa","sentry","cors","healthcheck","metrics","helloWorld","apiRoutes","createKoaMiddleware","createAppRouter","createQueryAdapter"]}
@@ -0,0 +1,2 @@
1
+ function l({isHealthy:a,isReady:t}={}){return async function(e,o){if(e.path==="/healthz"){a==null||a()?(e.status=200,e.body="healthy"):(e.status=503,e.body="not healthy");return}if(e.path==="/readyz"){t==null||t()?(e.status=200,e.body="ready"):(e.status=503,e.body="not ready");return}await o()}}export{l as a};
2
+ //# sourceMappingURL=chunk-7O2ZWWUX.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/koa-middleware/healthcheck.ts"],"sourcesContent":["import { Middleware } from \"koa\";\n\ntype HealthcheckOptions = {\n isHealthy?: () => boolean;\n isReady?: () => boolean;\n};\n\n/**\n * Middleware to add Kubernetes healthcheck endpoints\n */\nexport function healthcheck({ isHealthy, isReady }: HealthcheckOptions = {}): Middleware {\n return async function healthcheckMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/healthz\") {\n if (isHealthy == null || isHealthy()) {\n ctx.status = 200;\n ctx.body = \"healthy\";\n } else {\n ctx.status = 503;\n ctx.body = \"not healthy\";\n }\n return;\n }\n\n if (ctx.path === \"/readyz\") {\n if (isReady == null || isReady()) {\n ctx.status = 200;\n ctx.body = \"ready\";\n } else {\n ctx.status = 503;\n ctx.body = \"not ready\";\n }\n return;\n }\n\n await next();\n };\n}\n"],"mappings":";AAUO,SAAS,YAAY,EAAE,WAAW,QAAQ,IAAwB,CAAC,GAAe;AACvF,SAAO,eAAe,sBAAsB,KAAK,MAAqB;AACpE,QAAI,IAAI,SAAS,YAAY;AAC3B,UAAI,aAAa,QAAQ,UAAU,GAAG;AACpC,YAAI,SAAS;AACb,YAAI,OAAO;AAAA,MACb,OAAO;AACL,YAAI,SAAS;AACb,YAAI,OAAO;AAAA,MACb;AACA;AAAA,IACF;AAEA,QAAI,IAAI,SAAS,WAAW;AAC1B,UAAI,WAAW,QAAQ,QAAQ,GAAG;AAChC,YAAI,SAAS;AACb,YAAI,OAAO;AAAA,MACb,OAAO;AACL,YAAI,SAAS;AACb,YAAI,OAAO;AAAA,MACb;AACA;AAAA,IACF;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/koa-middleware/healthcheck.ts"],"sourcesContent":["import { Middleware } from \"koa\";\n\ntype HealthcheckOptions = {\n isHealthy?: () => boolean;\n isReady?: () => boolean;\n};\n\n/**\n * Middleware to add Kubernetes healthcheck endpoints\n */\nexport function healthcheck({ isHealthy, isReady }: HealthcheckOptions = {}): Middleware {\n return async function healthcheckMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/healthz\") {\n if (isHealthy == null || isHealthy()) {\n ctx.status = 200;\n ctx.body = \"healthy\";\n } else {\n ctx.status = 503;\n ctx.body = \"not healthy\";\n }\n return;\n }\n\n if (ctx.path === \"/readyz\") {\n if (isReady == null || isReady()) {\n ctx.status = 200;\n ctx.body = \"ready\";\n } else {\n ctx.status = 503;\n ctx.body = \"not ready\";\n }\n return;\n }\n\n await next();\n };\n}\n"],"mappings":"AAUO,SAASA,EAAY,CAAE,UAAAC,EAAW,QAAAC,CAAQ,EAAwB,CAAC,EAAe,CACvF,OAAO,eAAqCC,EAAKC,EAAqB,CACpE,GAAID,EAAI,OAAS,WAAY,CACvBF,GAAa,MAAQA,EAAU,GACjCE,EAAI,OAAS,IACbA,EAAI,KAAO,YAEXA,EAAI,OAAS,IACbA,EAAI,KAAO,eAEb,MACF,CAEA,GAAIA,EAAI,OAAS,UAAW,CACtBD,GAAW,MAAQA,EAAQ,GAC7BC,EAAI,OAAS,IACbA,EAAI,KAAO,UAEXA,EAAI,OAAS,IACbA,EAAI,KAAO,aAEb,MACF,CAEA,MAAMC,EAAK,CACb,CACF","names":["healthcheck","isHealthy","isReady","ctx","next"]}
@@ -0,0 +1,2 @@
1
+ function l(){return async function(e,o){if(e.path==="/"){e.status=200,e.body="emit HelloWorld();";return}await o()}}export{l as a};
2
+ //# sourceMappingURL=chunk-AYPBOJNL.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/koa-middleware/helloWorld.ts"],"sourcesContent":["import { Middleware } from \"koa\";\n\nexport function helloWorld(): Middleware {\n return async function helloWorldMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/\") {\n ctx.status = 200;\n ctx.body = \"emit HelloWorld();\";\n return;\n }\n await next();\n };\n}\n"],"mappings":";AAEO,SAAS,aAAyB;AACvC,SAAO,eAAe,qBAAqB,KAAK,MAAqB;AACnE,QAAI,IAAI,SAAS,KAAK;AACpB,UAAI,SAAS;AACb,UAAI,OAAO;AACX;AAAA,IACF;AACA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/koa-middleware/helloWorld.ts"],"sourcesContent":["import { Middleware } from \"koa\";\n\nexport function helloWorld(): Middleware {\n return async function helloWorldMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/\") {\n ctx.status = 200;\n ctx.body = \"emit HelloWorld();\";\n return;\n }\n await next();\n };\n}\n"],"mappings":"AAEO,SAASA,GAAyB,CACvC,OAAO,eAAoCC,EAAKC,EAAqB,CACnE,GAAID,EAAI,OAAS,IAAK,CACpBA,EAAI,OAAS,IACbA,EAAI,KAAO,qBACX,MACF,CACA,MAAMC,EAAK,CACb,CACF","names":["helloWorld","ctx","next"]}
@@ -0,0 +1,2 @@
1
+ import e from"prom-client";function u({isHealthy:i,isReady:a,getLatestStoredBlockNumber:s,getDistanceFromFollowBlock:l,followBlockTag:o}={}){if(e.collectDefaultMetrics(),i!=null&&new e.Gauge({name:"health_status",help:"Health status (0 = unhealthy, 1 = healthy)",collect(){this.set(Number(i()))}}),a!=null&&new e.Gauge({name:"readiness_status",help:"Readiness status (whether the service is ready to receive requests, 0 = not ready, 1 = ready)",collect(){this.set(Number(a()))}}),s!=null&&new e.Gauge({name:"latest_stored_block_number",help:"Latest block number stored in the database",async collect(){this.set(Number(await s()))}}),o!=null){let n=new e.Gauge({name:"follow_block_tag",help:"Block tag the indexer is following (0 = finalized, 1 = safe, 2 = latest)"}),t={finalized:0,safe:1,latest:2};n.set(t[o])}return l!=null&&new e.Gauge({name:"distance_from_follow_block",help:"Block distance from the block tag this the indexer is following",async collect(){this.set(Number(await l()))}}),async function(t,r){if(t.path==="/metrics"){t.status=200,t.body=await e.register.metrics();return}await r()}}export{u as a};
2
+ //# sourceMappingURL=chunk-ED45N3IT.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/koa-middleware/metrics.ts"],"sourcesContent":["import { Middleware } from \"koa\";\nimport promClient from \"prom-client\";\n\ntype MetricsOptions = {\n isHealthy?: () => boolean;\n isReady?: () => boolean;\n getLatestStoredBlockNumber?: () => Promise<bigint | undefined>;\n getDistanceFromFollowBlock?: () => Promise<bigint>;\n followBlockTag?: \"latest\" | \"safe\" | \"finalized\";\n};\n\n/**\n * Middleware to add Prometheus metrics endpoints\n */\nexport function metrics({\n isHealthy,\n isReady,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag,\n}: MetricsOptions = {}): Middleware {\n promClient.collectDefaultMetrics();\n if (isHealthy != null) {\n new promClient.Gauge({\n name: \"health_status\",\n help: \"Health status (0 = unhealthy, 1 = healthy)\",\n collect(): void {\n this.set(Number(isHealthy()));\n },\n });\n }\n\n if (isReady != null) {\n new promClient.Gauge({\n name: \"readiness_status\",\n help: \"Readiness status (whether the service is ready to receive requests, 0 = not ready, 1 = ready)\",\n collect(): void {\n this.set(Number(isReady()));\n },\n });\n }\n\n if (getLatestStoredBlockNumber != null) {\n new promClient.Gauge({\n name: \"latest_stored_block_number\",\n help: \"Latest block number stored in the database\",\n async collect(): Promise<void> {\n this.set(Number(await getLatestStoredBlockNumber()));\n },\n });\n }\n\n if (followBlockTag != null) {\n const blockTagGauge = new promClient.Gauge({\n name: \"follow_block_tag\",\n help: \"Block tag the indexer is following (0 = finalized, 1 = safe, 2 = latest)\",\n });\n const blockTagToValue = {\n finalized: 0,\n safe: 1,\n latest: 2,\n };\n blockTagGauge.set(blockTagToValue[followBlockTag]);\n }\n\n if (getDistanceFromFollowBlock != null) {\n new promClient.Gauge({\n name: \"distance_from_follow_block\",\n help: \"Block distance from the block tag this the indexer is following\",\n async collect(): Promise<void> {\n this.set(Number(await getDistanceFromFollowBlock()));\n },\n });\n }\n\n return async function metricsMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/metrics\") {\n ctx.status = 200;\n ctx.body = await promClient.register.metrics();\n return;\n }\n\n await next();\n };\n}\n"],"mappings":";AACA,OAAO,gBAAgB;AAahB,SAAS,QAAQ;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAoB,CAAC,GAAe;AAClC,aAAW,sBAAsB;AACjC,MAAI,aAAa,MAAM;AACrB,QAAI,WAAW,MAAM;AAAA,MACnB,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAgB;AACd,aAAK,IAAI,OAAO,UAAU,CAAC,CAAC;AAAA,MAC9B;AAAA,IACF,CAAC;AAAA,EACH;AAEA,MAAI,WAAW,MAAM;AACnB,QAAI,WAAW,MAAM;AAAA,MACnB,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAgB;AACd,aAAK,IAAI,OAAO,QAAQ,CAAC,CAAC;AAAA,MAC5B;AAAA,IACF,CAAC;AAAA,EACH;AAEA,MAAI,8BAA8B,MAAM;AACtC,QAAI,WAAW,MAAM;AAAA,MACnB,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM,UAAyB;AAC7B,aAAK,IAAI,OAAO,MAAM,2BAA2B,CAAC,CAAC;AAAA,MACrD;AAAA,IACF,CAAC;AAAA,EACH;AAEA,MAAI,kBAAkB,MAAM;AAC1B,UAAM,gBAAgB,IAAI,WAAW,MAAM;AAAA,MACzC,MAAM;AAAA,MACN,MAAM;AAAA,IACR,CAAC;AACD,UAAM,kBAAkB;AAAA,MACtB,WAAW;AAAA,MACX,MAAM;AAAA,MACN,QAAQ;AAAA,IACV;AACA,kBAAc,IAAI,gBAAgB,cAAc,CAAC;AAAA,EACnD;AAEA,MAAI,8BAA8B,MAAM;AACtC,QAAI,WAAW,MAAM;AAAA,MACnB,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM,UAAyB;AAC7B,aAAK,IAAI,OAAO,MAAM,2BAA2B,CAAC,CAAC;AAAA,MACrD;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,eAAe,kBAAkB,KAAK,MAAqB;AAChE,QAAI,IAAI,SAAS,YAAY;AAC3B,UAAI,SAAS;AACb,UAAI,OAAO,MAAM,WAAW,SAAS,QAAQ;AAC7C;AAAA,IACF;AAEA,UAAM,KAAK;AAAA,EACb;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/koa-middleware/metrics.ts"],"sourcesContent":["import { Middleware } from \"koa\";\nimport promClient from \"prom-client\";\n\ntype MetricsOptions = {\n isHealthy?: () => boolean;\n isReady?: () => boolean;\n getLatestStoredBlockNumber?: () => Promise<bigint | undefined>;\n getDistanceFromFollowBlock?: () => Promise<bigint>;\n followBlockTag?: \"latest\" | \"safe\" | \"finalized\";\n};\n\n/**\n * Middleware to add Prometheus metrics endpoints\n */\nexport function metrics({\n isHealthy,\n isReady,\n getLatestStoredBlockNumber,\n getDistanceFromFollowBlock,\n followBlockTag,\n}: MetricsOptions = {}): Middleware {\n promClient.collectDefaultMetrics();\n if (isHealthy != null) {\n new promClient.Gauge({\n name: \"health_status\",\n help: \"Health status (0 = unhealthy, 1 = healthy)\",\n collect(): void {\n this.set(Number(isHealthy()));\n },\n });\n }\n\n if (isReady != null) {\n new promClient.Gauge({\n name: \"readiness_status\",\n help: \"Readiness status (whether the service is ready to receive requests, 0 = not ready, 1 = ready)\",\n collect(): void {\n this.set(Number(isReady()));\n },\n });\n }\n\n if (getLatestStoredBlockNumber != null) {\n new promClient.Gauge({\n name: \"latest_stored_block_number\",\n help: \"Latest block number stored in the database\",\n async collect(): Promise<void> {\n this.set(Number(await getLatestStoredBlockNumber()));\n },\n });\n }\n\n if (followBlockTag != null) {\n const blockTagGauge = new promClient.Gauge({\n name: \"follow_block_tag\",\n help: \"Block tag the indexer is following (0 = finalized, 1 = safe, 2 = latest)\",\n });\n const blockTagToValue = {\n finalized: 0,\n safe: 1,\n latest: 2,\n };\n blockTagGauge.set(blockTagToValue[followBlockTag]);\n }\n\n if (getDistanceFromFollowBlock != null) {\n new promClient.Gauge({\n name: \"distance_from_follow_block\",\n help: \"Block distance from the block tag this the indexer is following\",\n async collect(): Promise<void> {\n this.set(Number(await getDistanceFromFollowBlock()));\n },\n });\n }\n\n return async function metricsMiddleware(ctx, next): Promise<void> {\n if (ctx.path === \"/metrics\") {\n ctx.status = 200;\n ctx.body = await promClient.register.metrics();\n return;\n }\n\n await next();\n };\n}\n"],"mappings":"AACA,OAAOA,MAAgB,cAahB,SAASC,EAAQ,CACtB,UAAAC,EACA,QAAAC,EACA,2BAAAC,EACA,2BAAAC,EACA,eAAAC,CACF,EAAoB,CAAC,EAAe,CAgClC,GA/BAN,EAAW,sBAAsB,EAC7BE,GAAa,MACf,IAAIF,EAAW,MAAM,CACnB,KAAM,gBACN,KAAM,6CACN,SAAgB,CACd,KAAK,IAAI,OAAOE,EAAU,CAAC,CAAC,CAC9B,CACF,CAAC,EAGCC,GAAW,MACb,IAAIH,EAAW,MAAM,CACnB,KAAM,mBACN,KAAM,gGACN,SAAgB,CACd,KAAK,IAAI,OAAOG,EAAQ,CAAC,CAAC,CAC5B,CACF,CAAC,EAGCC,GAA8B,MAChC,IAAIJ,EAAW,MAAM,CACnB,KAAM,6BACN,KAAM,6CACN,MAAM,SAAyB,CAC7B,KAAK,IAAI,OAAO,MAAMI,EAA2B,CAAC,CAAC,CACrD,CACF,CAAC,EAGCE,GAAkB,KAAM,CAC1B,IAAMC,EAAgB,IAAIP,EAAW,MAAM,CACzC,KAAM,mBACN,KAAM,0EACR,CAAC,EACKQ,EAAkB,CACtB,UAAW,EACX,KAAM,EACN,OAAQ,CACV,EACAD,EAAc,IAAIC,EAAgBF,CAAc,CAAC,CACnD,CAEA,OAAID,GAA8B,MAChC,IAAIL,EAAW,MAAM,CACnB,KAAM,6BACN,KAAM,kEACN,MAAM,SAAyB,CAC7B,KAAK,IAAI,OAAO,MAAMK,EAA2B,CAAC,CAAC,CACrD,CACF,CAAC,EAGI,eAAiCI,EAAKC,EAAqB,CAChE,GAAID,EAAI,OAAS,WAAY,CAC3BA,EAAI,OAAS,IACbA,EAAI,KAAO,MAAMT,EAAW,SAAS,QAAQ,EAC7C,MACF,CAEA,MAAMU,EAAK,CACb,CACF","names":["promClient","metrics","isHealthy","isReady","getLatestStoredBlockNumber","getDistanceFromFollowBlock","followBlockTag","blockTagGauge","blockTagToValue","ctx","next"]}
@@ -0,0 +1,2 @@
1
+ import*as e from"@sentry/node";import{ProfilingIntegration as m}from"@sentry/profiling-node";import{stripUrlQueryAndFragment as l}from"@sentry/utils";import c from"debug";var d=c("mud:store-indexer"),p=c("mud:store-indexer");d.log=console.debug.bind(console);p.log=console.error.bind(console);import f from"koa-compose";function y(){return async function(r,o){try{await o()}catch(t){throw e.withScope(a=>{a.addEventProcessor(n=>e.addRequestDataToEvent(n,r.request)),e.captureException(t)}),t}}}function g(){return async function(r,o){await e.runWithAsyncContext(async()=>{e.getCurrentHub().configureScope(a=>a.addEventProcessor(n=>e.addRequestDataToEvent(n,r.request,{include:{user:!1}}))),await o()})}}function S(){return async function(r,o){let t=(r.method||"").toUpperCase(),a=r.url&&l(r.url),n;r.request.get("sentry-trace")&&(n=e.extractTraceparentData(r.request.get("sentry-trace")));let i=e.startTransaction({name:`${t} ${a}`,op:"http.server",...n});r.__sentry_transaction=i,e.getCurrentHub().configureScope(u=>{u.setSpan(i)}),r.res.on("finish",()=>{setImmediate(()=>{if(r._matchedRoute){let u=r.mountPath||"";i.setName(`${t} ${u}${r._matchedRoute}`)}i.setHttpStatus(r.status),i.finish()})}),await o()}}function v(s){return d("Initializing Sentry"),e.init({dsn:s,integrations:[...e.autoDiscoverNodePerformanceMonitoringIntegrations(),new m],tracesSampleRate:1,profilesSampleRate:1}),f([y(),g(),S()])}export{d as a,p as b,v as c};
2
+ //# sourceMappingURL=chunk-JDWVOODJ.js.map