@chaos-overlords/storage 0.8.0 → 0.8.2

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 (59) hide show
  1. package/dist/node/index.d.ts +11 -3
  2. package/dist/node/index.d.ts.map +1 -1
  3. package/dist/node/index.js +26 -7
  4. package/dist/node/index.js.map +1 -1
  5. package/dist/postgres/database.d.ts +10 -0
  6. package/dist/postgres/database.d.ts.map +1 -0
  7. package/dist/postgres/database.js +2 -0
  8. package/dist/postgres/database.js.map +1 -0
  9. package/dist/postgres/events.d.ts +9 -0
  10. package/dist/postgres/events.d.ts.map +1 -0
  11. package/dist/postgres/events.js +66 -0
  12. package/dist/postgres/events.js.map +1 -0
  13. package/dist/postgres/repositories.d.ts +2 -3
  14. package/dist/postgres/repositories.d.ts.map +1 -1
  15. package/dist/postgres/repositories.js +151 -183
  16. package/dist/postgres/repositories.js.map +1 -1
  17. package/dist/postgres/schema.d.ts +26 -9
  18. package/dist/postgres/schema.d.ts.map +1 -1
  19. package/dist/postgres/schema.js +6 -0
  20. package/dist/postgres/schema.js.map +1 -1
  21. package/dist/postgres/takeovers.d.ts +12 -0
  22. package/dist/postgres/takeovers.d.ts.map +1 -0
  23. package/dist/postgres/takeovers.js +88 -0
  24. package/dist/postgres/takeovers.js.map +1 -0
  25. package/dist/shared/constraints.d.ts +10 -0
  26. package/dist/shared/constraints.d.ts.map +1 -1
  27. package/dist/shared/constraints.js +13 -0
  28. package/dist/shared/constraints.js.map +1 -1
  29. package/dist/shared/mappers.d.ts +28 -5
  30. package/dist/shared/mappers.d.ts.map +1 -1
  31. package/dist/shared/mappers.js +31 -0
  32. package/dist/shared/mappers.js.map +1 -1
  33. package/dist/sqlite/database.d.ts +12 -0
  34. package/dist/sqlite/database.d.ts.map +1 -0
  35. package/dist/sqlite/database.js +2 -0
  36. package/dist/sqlite/database.js.map +1 -0
  37. package/dist/sqlite/events.d.ts +9 -0
  38. package/dist/sqlite/events.d.ts.map +1 -0
  39. package/dist/sqlite/events.js +66 -0
  40. package/dist/sqlite/events.js.map +1 -0
  41. package/dist/sqlite/repositories.d.ts +2 -8
  42. package/dist/sqlite/repositories.d.ts.map +1 -1
  43. package/dist/sqlite/repositories.js +177 -183
  44. package/dist/sqlite/repositories.js.map +1 -1
  45. package/dist/sqlite/schema.d.ts +26 -9
  46. package/dist/sqlite/schema.d.ts.map +1 -1
  47. package/dist/sqlite/schema.js +6 -0
  48. package/dist/sqlite/schema.js.map +1 -1
  49. package/dist/sqlite/takeovers.d.ts +12 -0
  50. package/dist/sqlite/takeovers.d.ts.map +1 -0
  51. package/dist/sqlite/takeovers.js +88 -0
  52. package/dist/sqlite/takeovers.js.map +1 -0
  53. package/migrations/postgres/0005_old_husk.sql +6 -0
  54. package/migrations/postgres/meta/0005_snapshot.json +854 -0
  55. package/migrations/postgres/meta/_journal.json +8 -1
  56. package/migrations/sqlite/0005_lumpy_stingray.sql +6 -0
  57. package/migrations/sqlite/meta/0005_snapshot.json +844 -0
  58. package/migrations/sqlite/meta/_journal.json +8 -1
  59. package/package.json +7 -7
@@ -17,8 +17,16 @@ export interface OpenedStorage {
17
17
  * WAL mode keeps a self-hosted server responsive while the event stream reads during writes.
18
18
  */
19
19
  export declare function openSqliteStorage(filename: string): OpenedStorage;
20
- /** Opens a Postgres pool, applies pending migrations, and returns the storage ports. */
21
- export declare function openPostgresStorage(connectionString: string): Promise<OpenedStorage>;
20
+ /**
21
+ * Opens a Postgres pool, applies pending migrations, and returns the storage ports.
22
+ *
23
+ * @param onPoolError Told when an idle pooled connection dies. Required in effect, not in type: an
24
+ * `EventEmitter` `error` with no listener is thrown, and node-postgres emits one on the pool every
25
+ * time a Postgres restart, a failover or a NAT idle-reset drops a pooled connection. That reached
26
+ * `process.on('uncaughtException')` in the Node runtime, which shut the server down — for an event
27
+ * the pool would have recovered from on the next query.
28
+ */
29
+ export declare function openPostgresStorage(connectionString: string, onPoolError?: (error: unknown) => void): Promise<OpenedStorage>;
22
30
  export type StorageTarget = {
23
31
  kind: 'sqlite';
24
32
  filename: string;
@@ -31,5 +39,5 @@ export type StorageTarget = {
31
39
  * `sqlite::memory:` for SQLite. Anything else is refused rather than guessed.
32
40
  */
33
41
  export declare function parseStorageTarget(databaseUrl: string): StorageTarget;
34
- export declare function openStorage(target: StorageTarget): Promise<OpenedStorage>;
42
+ export declare function openStorage(target: StorageTarget, onPoolError?: (error: unknown) => void): Promise<OpenedStorage>;
35
43
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAYjE,+FAA+F;AAC/F,eAAO,MAAM,qBAAqB,QAEjC,CAAA;AACD,eAAO,MAAM,uBAAuB,QAEnC,CAAA;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,kBAAkB,CAAA;IAC3B;;;;OAIG;IACH,OAAO,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AASD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,CAiBjE;AAED,wFAAwF;AACxF,wBAAsB,mBAAmB,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAgB1F;AAED,MAAM,MAAM,aAAa,GACrB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAA;CAAE,CAAA;AAElD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,aAAa,CAUrE;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAIzE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAYjE,+FAA+F;AAC/F,eAAO,MAAM,qBAAqB,QAEjC,CAAA;AACD,eAAO,MAAM,uBAAuB,QAEnC,CAAA;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,kBAAkB,CAAA;IAC3B;;;;OAIG;IACH,OAAO,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AASD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,CAiBjE;AAED;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,gBAAgB,EAAE,MAAM,EACxB,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GACrC,OAAO,CAAC,aAAa,CAAC,CAwBxB;AAED,MAAM,MAAM,aAAa,GACrB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAA;CAAE,CAAA;AAElD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,aAAa,CAUrE;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,aAAa,EACrB,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GACrC,OAAO,CAAC,aAAa,CAAC,CAIxB"}
@@ -1,4 +1,5 @@
1
- import { fileURLToPath } from 'node:url';
1
+ // URL too: this file also sees @cloudflare/workers-types' global URL, which node:url rejects.
2
+ import { fileURLToPath, URL } from 'node:url';
2
3
  import BetterSqlite3 from 'better-sqlite3';
3
4
  import { drizzle as drizzleSqlite } from 'drizzle-orm/better-sqlite3';
4
5
  import { migrate as migrateSqlite } from 'drizzle-orm/better-sqlite3/migrator';
@@ -40,9 +41,25 @@ export function openSqliteStorage(filename) {
40
41
  },
41
42
  };
42
43
  }
43
- /** Opens a Postgres pool, applies pending migrations, and returns the storage ports. */
44
- export async function openPostgresStorage(connectionString) {
45
- const pool = new pg.Pool({ connectionString });
44
+ /**
45
+ * Opens a Postgres pool, applies pending migrations, and returns the storage ports.
46
+ *
47
+ * @param onPoolError Told when an idle pooled connection dies. Required in effect, not in type: an
48
+ * `EventEmitter` `error` with no listener is thrown, and node-postgres emits one on the pool every
49
+ * time a Postgres restart, a failover or a NAT idle-reset drops a pooled connection. That reached
50
+ * `process.on('uncaughtException')` in the Node runtime, which shut the server down — for an event
51
+ * the pool would have recovered from on the next query.
52
+ */
53
+ export async function openPostgresStorage(connectionString, onPoolError) {
54
+ const pool = new pg.Pool({
55
+ connectionString,
56
+ // Both default to waiting forever. A database that has stopped answering should produce 500s
57
+ // and log lines, not requests that hang until the client gives up.
58
+ connectionTimeoutMillis: 10_000,
59
+ idleTimeoutMillis: 30_000,
60
+ statement_timeout: 30_000,
61
+ });
62
+ pool.on('error', (error) => onPoolError?.(error));
46
63
  const db = drizzlePostgres(pool, { schema: postgresSchema });
47
64
  const lock = await pool.connect();
48
65
  try {
@@ -70,11 +87,13 @@ export function parseStorageTarget(databaseUrl) {
70
87
  if (databaseUrl.startsWith('sqlite:')) {
71
88
  return { kind: 'sqlite', filename: databaseUrl.slice('sqlite:'.length) || ':memory:' };
72
89
  }
73
- throw new Error(`Unsupported DATABASE_URL "${databaseUrl}": expected postgres://… or sqlite:<path>`);
90
+ // The URL is not repeated: the natural mistake here is a `mysql://user:password@host/db`, and
91
+ // this message goes to stderr at startup and into whatever collects the container's logs.
92
+ throw new Error('Unsupported DATABASE_URL: expected postgres://… or sqlite:<path>');
74
93
  }
75
- export function openStorage(target) {
94
+ export function openStorage(target, onPoolError) {
76
95
  return target.kind === 'postgres'
77
- ? openPostgresStorage(target.connectionString)
96
+ ? openPostgresStorage(target.connectionString, onPoolError)
78
97
  : Promise.resolve(openSqliteStorage(target.filename));
79
98
  }
80
99
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,aAAa,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,4BAA4B,CAAA;AACrE,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,qCAAqC,CAAA;AAC9E,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,2BAA2B,CAAA;AACtE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,oCAAoC,CAAA;AAC/E,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,KAAK,cAAc,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,kBAAkB,CAAA;AAEhD,+FAA+F;AAC/F,MAAM,CAAC,MAAM,qBAAqB,GAAG,aAAa,CAChD,IAAI,GAAG,CAAC,yBAAyB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CACpD,CAAA;AACD,MAAM,CAAC,MAAM,uBAAuB,GAAG,aAAa,CAClD,IAAI,GAAG,CAAC,2BAA2B,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CACtD,CAAA;AAaD;;;;GAIG;AACH,MAAM,kBAAkB,GAAG,iBAAsB,CAAA;AAEjD;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAAgB;IAChD,MAAM,UAAU,GAAG,IAAI,aAAa,CAAC,QAAQ,CAAC,CAAA;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IAAI,qBAAqB,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;QAC5C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,gBAAgB,EAAE,WAAW,EAAE,CAAA;IAC5D,CAAC;IACD,IAAI,WAAW,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QACtC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,UAAU,EAAE,CAAA;IACxF,CAAC;IACD,8FAA8F;IAC9F,0FAA0F;IAC1F,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAA;AACrF,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,MAAqB,EACrB,WAAsC;IAEtC,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU;QAC/B,CAAC,CAAC,mBAAmB,CAAC,MAAM,CAAC,gBAAgB,EAAE,WAAW,CAAC;QAC3D,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;AACzD,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { PgDatabase, PgQueryResultHKT } from 'drizzle-orm/pg-core';
2
+ import type * as schema from './schema';
3
+ /**
4
+ * Any driver Drizzle wraps as a `PgDatabase` (node-postgres in the Node runtime).
5
+ *
6
+ * It lives here rather than beside the repositories so the modules those repositories are split
7
+ * across can name it without importing each other.
8
+ */
9
+ export type PostgresDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;
10
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/postgres/database.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AACvE,OAAO,KAAK,KAAK,MAAM,MAAM,UAAU,CAAA;AAEvC;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,gBAAgB,EAAE,OAAO,MAAM,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/postgres/database.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ import type { EventRepository } from '@chaos-overlords/kernel';
2
+ import type { PostgresDatabase } from './database';
3
+ /**
4
+ * The match event log in the Postgres dialect: the gapless, self-allocating sequence every stream
5
+ * cursor moves along. Its own module because the allocation rule — `max(seq) + 1` inside the
6
+ * insert, retried on the primary key — is the one invariant the whole delivery contract rests on.
7
+ */
8
+ export declare function postgresEventRepository(db: PostgresDatabase): EventRepository;
9
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/postgres/events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,yBAAyB,CAAA;AAI9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAGlD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,gBAAgB,GAAG,eAAe,CAoD7E"}
@@ -0,0 +1,66 @@
1
+ import { and, asc, desc, eq, sql } from 'drizzle-orm';
2
+ import { APPEND_ATTEMPTS, appendBackoff, isUniqueViolation } from '../shared/constraints';
3
+ import { toEvent } from '../shared/mappers';
4
+ import * as schema from './schema';
5
+ /**
6
+ * The match event log in the Postgres dialect: the gapless, self-allocating sequence every stream
7
+ * cursor moves along. Its own module because the allocation rule — `max(seq) + 1` inside the
8
+ * insert, retried on the primary key — is the one invariant the whole delivery contract rests on.
9
+ */
10
+ export function postgresEventRepository(db) {
11
+ const { matchEvents } = schema;
12
+ return {
13
+ /**
14
+ * The sequence number comes from the log itself inside the insert, so the allocation cannot be
15
+ * separated from the write: a committed `seq` therefore implies every lower one is committed,
16
+ * which is the invariant a stream cursor relies on. Concurrent appends collide on the primary
17
+ * key and the loser simply re-reads the maximum.
18
+ */
19
+ async append(event) {
20
+ const nextSeq = sql `(select coalesce(max(${matchEvents.seq}), 0) + 1 from ${matchEvents} where ${eq(matchEvents.matchId, event.matchId)})`;
21
+ for (let attempt = 1; attempt <= APPEND_ATTEMPTS; attempt += 1) {
22
+ try {
23
+ const rows = await db
24
+ .insert(matchEvents)
25
+ .values({
26
+ matchId: event.matchId,
27
+ seq: nextSeq,
28
+ type: event.type,
29
+ payload: event.payload,
30
+ createdAt: new Date(event.createdAt),
31
+ })
32
+ .returning({ seq: matchEvents.seq });
33
+ const row = rows[0];
34
+ if (!row)
35
+ throw new Error(`append to ${event.matchId} reported no row`);
36
+ return { ...event, seq: row.seq };
37
+ }
38
+ catch (error) {
39
+ if (!isUniqueViolation(error) || attempt === APPEND_ATTEMPTS)
40
+ throw error;
41
+ await appendBackoff(attempt);
42
+ }
43
+ }
44
+ throw new Error(`could not append an event for ${event.matchId}`);
45
+ },
46
+ async listAfter(matchId, afterSeq, limit) {
47
+ const rows = await db
48
+ .select()
49
+ .from(matchEvents)
50
+ .where(and(eq(matchEvents.matchId, matchId), sql `${matchEvents.seq} > ${afterSeq}`))
51
+ .orderBy(asc(matchEvents.seq))
52
+ .limit(limit);
53
+ return rows.map(toEvent);
54
+ },
55
+ async lastSeq(matchId) {
56
+ const rows = await db
57
+ .select({ seq: matchEvents.seq })
58
+ .from(matchEvents)
59
+ .where(eq(matchEvents.matchId, matchId))
60
+ .orderBy(desc(matchEvents.seq))
61
+ .limit(1);
62
+ return rows[0]?.seq ?? 0;
63
+ },
64
+ };
65
+ }
66
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/postgres/events.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE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,10 @@
1
1
  import type { MultiplayerStorage } from '@chaos-overlords/kernel';
2
- import type { PgDatabase, PgQueryResultHKT } from 'drizzle-orm/pg-core';
3
- import * as schema from './schema';
2
+ import type { PostgresDatabase } from './database';
4
3
  /**
5
4
  * The Postgres twin of `sqlite/repositories.ts`: the same statements over the pg-core schema.
6
5
  * Any driver Drizzle wraps as a `PgDatabase` works (node-postgres in the Node runtime). The two
7
6
  * files are kept in step by the conformance suite, not by sharing code across dialect types.
8
7
  */
9
- export type PostgresDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;
8
+ export type { PostgresDatabase };
10
9
  export declare function createPostgresStorage(db: PostgresDatabase): MultiplayerStorage;
11
10
  //# sourceMappingURL=repositories.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"repositories.d.ts","sourceRoot":"","sources":["../../src/postgres/repositories.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,kBAAkB,EAMnB,MAAM,yBAAyB,CAAA;AAiBhC,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAevE,OAAO,KAAK,MAAM,MAAM,UAAU,CAAA;AAElC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,gBAAgB,EAAE,OAAO,MAAM,CAAC,CAAA;AAE1E,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,gBAAgB,GAAG,kBAAkB,CAS9E"}
1
+ {"version":3,"file":"repositories.d.ts","sourceRoot":"","sources":["../../src/postgres/repositories.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAKnB,MAAM,yBAAyB,CAAA;AA+BhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAKlD;;;;GAIG;AACH,YAAY,EAAE,gBAAgB,EAAE,CAAA;AAEhC,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,gBAAgB,GAAG,kBAAkB,CAS9E"}