@geekmidas/testkit 10.0.0-alpha.20 → 10.0.0-alpha.21

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transactions.d.cts","names":[],"sources":["../src/transactions.ts"],"mappings":";;;iBAqBiB;EAChB;EACA;;;;;;;;;wBAUe,iBAAiB,cAAc;;iBAa9B;;WAEP,IAAI;;EAEb,YAAY;;;wBAIS,qBACrB,cACE,QAAQ;;;;;;;;qBAmEE,oBAAoB;mBACf;EAEjB,IAAI,KAAK,MAAM,cAAc,QAAQ;EAS/B,eAAe"}
@@ -0,0 +1,38 @@
1
+ import { Kysely } from "kysely";
2
+ //#region src/transactions.d.ts
3
+ /** How to reach one database: a URL, and its search path as a startup option. */
4
+ export interface ConnectionConfig {
5
+ connectionString: string;
6
+ options?: string;
7
+ }
8
+ /**
9
+ * The connection settings for a database URL.
10
+ *
11
+ * `?search_path=` is not a libpq parameter: a URL carrying it connects and then
12
+ * resolves every name against `public`. Postgres takes it as a startup option,
13
+ * which is what a schema tenant's URL needs to reach its own tables.
14
+ */
15
+ export declare function connectionConfig(url: string): ConnectionConfig;
16
+ /** One database's connection, inside a transaction the test will roll back. */
17
+ export interface BoundTransaction {
18
+ /** Everything queried through this runs inside the test's transaction. */
19
+ readonly db: Kysely<any>;
20
+ /** Undo everything, and let the connection go. */
21
+ rollback(): Promise<void>;
22
+ }
23
+ /** Open a connection to `url` and begin the transaction a test will roll back. */
24
+ export declare function openBoundTransaction(url: string): Promise<BoundTransaction>;
25
+ /**
26
+ * The transactions one test has opened, by database.
27
+ *
28
+ * `get` opens a database's transaction the first time it is asked for and
29
+ * hands back the same one after that — including to a caller that asks while
30
+ * it is still opening. `rollbackAll` ends every one of them.
31
+ */
32
+ export declare class TransactionRegistry<TKey = string> {
33
+ private readonly open;
34
+ get(key: TKey, url: string): Promise<Kysely<any>>;
35
+ rollbackAll(): Promise<void>;
36
+ }
37
+ //#endregion
38
+ //# sourceMappingURL=transactions.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transactions.d.mts","names":[],"sources":["../src/transactions.ts"],"mappings":";;;iBAqBiB;EAChB;EACA;;;;;;;;;wBAUe,iBAAiB,cAAc;;iBAa9B;;WAEP,IAAI;;EAEb,YAAY;;;wBAIS,qBACrB,cACE,QAAQ;;;;;;;;qBAmEE,oBAAoB;mBACf;EAEjB,IAAI,KAAK,MAAM,cAAc,QAAQ;EAS/B,eAAe"}
@@ -0,0 +1,109 @@
1
+ import pg from "pg";
2
+ import { Kysely, PostgresDialect } from "kysely";
3
+ //#region src/transactions.ts
4
+ /**
5
+ * A transaction per database, per test — as each runs deployed.
6
+ *
7
+ * An app's database and a schema tenant such as the auth server's are separate
8
+ * databases to the code using them: separate connections, credentials, roles
9
+ * and search paths. A test keeps them that way. Each gets its own connection,
10
+ * opened into a transaction the first time the test touches it, and every one
11
+ * is rolled back when the test ends.
12
+ *
13
+ * Code under test must be free to use transactions itself, and better-auth
14
+ * does — it also calls `db.connection()`, which Kysely refuses on a
15
+ * `Transaction`. So what a test hands over is not a `Transaction` but a Kysely
16
+ * over the one connection already inside the test's transaction, on which a
17
+ * `BEGIN` becomes a savepoint and a `COMMIT` releases it. Nothing the code does
18
+ * can end the transaction the test will roll back.
19
+ */
20
+ /**
21
+ * The connection settings for a database URL.
22
+ *
23
+ * `?search_path=` is not a libpq parameter: a URL carrying it connects and then
24
+ * resolves every name against `public`. Postgres takes it as a startup option,
25
+ * which is what a schema tenant's URL needs to reach its own tables.
26
+ */
27
+ function connectionConfig(url) {
28
+ const parsed = new URL(url);
29
+ const searchPath = parsed.searchParams.get("search_path");
30
+ if (!searchPath) return { connectionString: url };
31
+ parsed.searchParams.delete("search_path");
32
+ return {
33
+ connectionString: parsed.toString(),
34
+ options: `-c search_path=${searchPath}`
35
+ };
36
+ }
37
+ /** Open a connection to `url` and begin the transaction a test will roll back. */
38
+ async function openBoundTransaction(url) {
39
+ const client = new pg.Client(connectionConfig(url));
40
+ await client.connect();
41
+ await client.query("BEGIN");
42
+ return {
43
+ db: new Kysely({ dialect: new PostgresDialect({ pool: {
44
+ connect: async () => savepointing(client),
45
+ end: async () => {}
46
+ } }) }),
47
+ async rollback() {
48
+ try {
49
+ await client.query("ROLLBACK");
50
+ } finally {
51
+ await client.end();
52
+ }
53
+ }
54
+ };
55
+ }
56
+ /**
57
+ * The client, with transaction control turned into savepoints.
58
+ *
59
+ * Kysely issues `begin`/`commit`/`rollback` as plain statements; this is the
60
+ * one place they can be caught. A depth counter names the savepoints, so
61
+ * nested transactions nest.
62
+ */
63
+ function savepointing(client) {
64
+ let depth = 0;
65
+ const query = client.query.bind(client);
66
+ const wrapped = Object.create(client);
67
+ Object.assign(wrapped, {
68
+ query: (text, ...rest) => {
69
+ const statement = typeof text === "string" ? text.trim().toLowerCase() : void 0;
70
+ if (statement === "begin" || statement?.startsWith("start transaction")) {
71
+ depth++;
72
+ return query(`SAVEPOINT test_sp_${depth}`);
73
+ }
74
+ if (statement === "commit") return query(`RELEASE SAVEPOINT test_sp_${depth--}`);
75
+ if (statement === "rollback") return query(`ROLLBACK TO SAVEPOINT test_sp_${depth--}`);
76
+ return query(text, ...rest);
77
+ },
78
+ release: () => {}
79
+ });
80
+ return wrapped;
81
+ }
82
+ /**
83
+ * The transactions one test has opened, by database.
84
+ *
85
+ * `get` opens a database's transaction the first time it is asked for and
86
+ * hands back the same one after that — including to a caller that asks while
87
+ * it is still opening. `rollbackAll` ends every one of them.
88
+ */
89
+ var TransactionRegistry = class {
90
+ open = /* @__PURE__ */ new Map();
91
+ get(key, url) {
92
+ let transaction = this.open.get(key);
93
+ if (!transaction) {
94
+ transaction = openBoundTransaction(url);
95
+ this.open.set(key, transaction);
96
+ }
97
+ return transaction.then(({ db }) => db);
98
+ }
99
+ async rollbackAll() {
100
+ const transactions = [...this.open.values()];
101
+ this.open.clear();
102
+ const failed = (await Promise.allSettled(transactions.map(async (transaction) => (await transaction).rollback()))).find((result) => result.status === "rejected");
103
+ if (failed) throw failed.reason;
104
+ }
105
+ };
106
+ //#endregion
107
+ export { TransactionRegistry, connectionConfig, openBoundTransaction };
108
+
109
+ //# sourceMappingURL=transactions.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transactions.mjs","names":[],"sources":["../src/transactions.ts"],"sourcesContent":["/**\n * A transaction per database, per test — as each runs deployed.\n *\n * An app's database and a schema tenant such as the auth server's are separate\n * databases to the code using them: separate connections, credentials, roles\n * and search paths. A test keeps them that way. Each gets its own connection,\n * opened into a transaction the first time the test touches it, and every one\n * is rolled back when the test ends.\n *\n * Code under test must be free to use transactions itself, and better-auth\n * does — it also calls `db.connection()`, which Kysely refuses on a\n * `Transaction`. So what a test hands over is not a `Transaction` but a Kysely\n * over the one connection already inside the test's transaction, on which a\n * `BEGIN` becomes a savepoint and a `COMMIT` releases it. Nothing the code does\n * can end the transaction the test will roll back.\n */\n\nimport { Kysely, PostgresDialect } from 'kysely';\nimport pg from 'pg';\n\n/** How to reach one database: a URL, and its search path as a startup option. */\nexport interface ConnectionConfig {\n\tconnectionString: string;\n\toptions?: string;\n}\n\n/**\n * The connection settings for a database URL.\n *\n * `?search_path=` is not a libpq parameter: a URL carrying it connects and then\n * resolves every name against `public`. Postgres takes it as a startup option,\n * which is what a schema tenant's URL needs to reach its own tables.\n */\nexport function connectionConfig(url: string): ConnectionConfig {\n\tconst parsed = new URL(url);\n\tconst searchPath = parsed.searchParams.get('search_path');\n\tif (!searchPath) return { connectionString: url };\n\n\tparsed.searchParams.delete('search_path');\n\treturn {\n\t\tconnectionString: parsed.toString(),\n\t\toptions: `-c search_path=${searchPath}`,\n\t};\n}\n\n/** One database's connection, inside a transaction the test will roll back. */\nexport interface BoundTransaction {\n\t/** Everything queried through this runs inside the test's transaction. */\n\treadonly db: Kysely<any>;\n\t/** Undo everything, and let the connection go. */\n\trollback(): Promise<void>;\n}\n\n/** Open a connection to `url` and begin the transaction a test will roll back. */\nexport async function openBoundTransaction(\n\turl: string,\n): Promise<BoundTransaction> {\n\tconst client = new pg.Client(connectionConfig(url));\n\tawait client.connect();\n\tawait client.query('BEGIN');\n\n\treturn {\n\t\tdb: new Kysely({\n\t\t\tdialect: new PostgresDialect({\n\t\t\t\t// A \"pool\" of the one connection. Released never, ended never: the\n\t\t\t\t// test owns it, and it closes in `rollback`.\n\t\t\t\tpool: {\n\t\t\t\t\tconnect: async () => savepointing(client),\n\t\t\t\t\tend: async () => {},\n\t\t\t\t} as unknown as pg.Pool,\n\t\t\t}),\n\t\t}),\n\t\tasync rollback() {\n\t\t\ttry {\n\t\t\t\tawait client.query('ROLLBACK');\n\t\t\t} finally {\n\t\t\t\tawait client.end();\n\t\t\t}\n\t\t},\n\t};\n}\n\n/**\n * The client, with transaction control turned into savepoints.\n *\n * Kysely issues `begin`/`commit`/`rollback` as plain statements; this is the\n * one place they can be caught. A depth counter names the savepoints, so\n * nested transactions nest.\n */\nfunction savepointing(client: pg.Client): pg.PoolClient {\n\tlet depth = 0;\n\tconst query = client.query.bind(client) as (...args: unknown[]) => unknown;\n\n\tconst wrapped = Object.create(client) as pg.PoolClient;\n\tObject.assign(wrapped, {\n\t\tquery: (text: unknown, ...rest: unknown[]) => {\n\t\t\tconst statement =\n\t\t\t\ttypeof text === 'string' ? text.trim().toLowerCase() : undefined;\n\n\t\t\tif (statement === 'begin' || statement?.startsWith('start transaction')) {\n\t\t\t\tdepth++;\n\t\t\t\treturn query(`SAVEPOINT test_sp_${depth}`);\n\t\t\t}\n\t\t\tif (statement === 'commit') {\n\t\t\t\treturn query(`RELEASE SAVEPOINT test_sp_${depth--}`);\n\t\t\t}\n\t\t\tif (statement === 'rollback') {\n\t\t\t\treturn query(`ROLLBACK TO SAVEPOINT test_sp_${depth--}`);\n\t\t\t}\n\t\t\treturn query(text, ...rest);\n\t\t},\n\t\trelease: () => {},\n\t});\n\treturn wrapped;\n}\n\n/**\n * The transactions one test has opened, by database.\n *\n * `get` opens a database's transaction the first time it is asked for and\n * hands back the same one after that — including to a caller that asks while\n * it is still opening. `rollbackAll` ends every one of them.\n */\nexport class TransactionRegistry<TKey = string> {\n\tprivate readonly open = new Map<TKey, Promise<BoundTransaction>>();\n\n\tget(key: TKey, url: string): Promise<Kysely<any>> {\n\t\tlet transaction = this.open.get(key);\n\t\tif (!transaction) {\n\t\t\ttransaction = openBoundTransaction(url);\n\t\t\tthis.open.set(key, transaction);\n\t\t}\n\t\treturn transaction.then(({ db }) => db);\n\t}\n\n\tasync rollbackAll(): Promise<void> {\n\t\tconst transactions = [...this.open.values()];\n\t\tthis.open.clear();\n\n\t\tconst results = await Promise.allSettled(\n\t\t\ttransactions.map(async (transaction) => (await transaction).rollback()),\n\t\t);\n\t\tconst failed = results.find(\n\t\t\t(result): result is PromiseRejectedResult => result.status === 'rejected',\n\t\t);\n\t\tif (failed) throw failed.reason;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,SAAgB,iBAAiB,KAA+B;CAC/D,MAAM,SAAS,IAAI,IAAI,GAAG;CAC1B,MAAM,aAAa,OAAO,aAAa,IAAI,aAAa;CACxD,IAAI,CAAC,YAAY,OAAO,EAAE,kBAAkB,IAAI;CAEhD,OAAO,aAAa,OAAO,aAAa;CACxC,OAAO;EACN,kBAAkB,OAAO,SAAS;EAClC,SAAS,kBAAkB;CAC5B;AACD;;AAWA,eAAsB,qBACrB,KAC4B;CAC5B,MAAM,SAAS,IAAI,GAAG,OAAO,iBAAiB,GAAG,CAAC;CAClD,MAAM,OAAO,QAAQ;CACrB,MAAM,OAAO,MAAM,OAAO;CAE1B,OAAO;EACN,IAAI,IAAI,OAAO,EACd,SAAS,IAAI,gBAAgB,EAG5B,MAAM;GACL,SAAS,YAAY,aAAa,MAAM;GACxC,KAAK,YAAY,CAAC;EACnB,EACD,CAAC,EACF,CAAC;EACD,MAAM,WAAW;GAChB,IAAI;IACH,MAAM,OAAO,MAAM,UAAU;GAC9B,UAAU;IACT,MAA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.20",
3
+ "version": "10.0.0-alpha.21",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -113,6 +113,46 @@
113
113
  "types": "./dist/requestContext.d.cts",
114
114
  "default": "./dist/requestContext.cjs"
115
115
  }
116
+ },
117
+ "./browser": {
118
+ "import": {
119
+ "types": "./dist/browser.d.mts",
120
+ "default": "./dist/browser.mjs"
121
+ },
122
+ "require": {
123
+ "types": "./dist/browser.d.cts",
124
+ "default": "./dist/browser.cjs"
125
+ }
126
+ },
127
+ "./context": {
128
+ "import": {
129
+ "types": "./dist/context.d.mts",
130
+ "default": "./dist/context.mjs"
131
+ },
132
+ "require": {
133
+ "types": "./dist/context.d.cts",
134
+ "default": "./dist/context.cjs"
135
+ }
136
+ },
137
+ "./mailbox": {
138
+ "import": {
139
+ "types": "./dist/mailbox.d.mts",
140
+ "default": "./dist/mailbox.mjs"
141
+ },
142
+ "require": {
143
+ "types": "./dist/mailbox.d.cts",
144
+ "default": "./dist/mailbox.cjs"
145
+ }
146
+ },
147
+ "./transactions": {
148
+ "import": {
149
+ "types": "./dist/transactions.d.mts",
150
+ "default": "./dist/transactions.mjs"
151
+ },
152
+ "require": {
153
+ "types": "./dist/transactions.d.cts",
154
+ "default": "./dist/transactions.cjs"
155
+ }
116
156
  }
117
157
  },
118
158
  "files": [
@@ -120,17 +160,18 @@
120
160
  ],
121
161
  "dependencies": {
122
162
  "@faker-js/faker": "~10.6.0",
123
- "@geekmidas/envkit": "^10.0.0-alpha.20",
124
- "@geekmidas/logger": "^10.0.0-alpha.20",
125
- "@geekmidas/services": "^10.0.0-alpha.20"
163
+ "tough-cookie": "~6.0.2",
164
+ "@geekmidas/logger": "^10.0.0-alpha.21",
165
+ "@geekmidas/envkit": "^10.0.0-alpha.21",
166
+ "@geekmidas/services": "^10.0.0-alpha.21"
126
167
  },
127
168
  "devDependencies": {
128
169
  "@better-auth/test-utils": "~1.7.6",
129
170
  "@types/pg": "~8.23.1",
130
171
  "better-auth": "~1.7.6",
131
- "@geekmidas/envkit": "^10.0.0-alpha.20",
132
- "@geekmidas/logger": "^10.0.0-alpha.20",
133
- "@geekmidas/services": "^10.0.0-alpha.20"
172
+ "@geekmidas/logger": "^10.0.0-alpha.21",
173
+ "@geekmidas/envkit": "^10.0.0-alpha.21",
174
+ "@geekmidas/services": "^10.0.0-alpha.21"
134
175
  },
135
176
  "repository": {
136
177
  "type": "git",