@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.31
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.
- package/README.md +74 -0
- package/dist/Factory.cjs +51 -2
- package/dist/Factory.cjs.map +1 -0
- package/dist/Factory.d.cts +143 -3
- package/dist/Factory.d.cts.map +1 -0
- package/dist/Factory.d.mts +143 -3
- package/dist/Factory.d.mts.map +1 -0
- package/dist/Factory.mjs +50 -2
- package/dist/Factory.mjs.map +1 -0
- package/dist/KyselyFactory.cjs +237 -4
- package/dist/KyselyFactory.cjs.map +1 -0
- package/dist/KyselyFactory.d.cts +224 -4
- package/dist/KyselyFactory.d.cts.map +1 -0
- package/dist/KyselyFactory.d.mts +224 -4
- package/dist/KyselyFactory.d.mts.map +1 -0
- package/dist/KyselyFactory.mjs +236 -4
- package/dist/KyselyFactory.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +265 -4
- package/dist/ObjectionFactory.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +237 -4
- package/dist/ObjectionFactory.d.cts.map +1 -0
- package/dist/ObjectionFactory.d.mts +237 -4
- package/dist/ObjectionFactory.d.mts.map +1 -0
- package/dist/ObjectionFactory.mjs +264 -4
- package/dist/ObjectionFactory.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +94 -3
- package/dist/PostgresKyselyMigrator.cjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.cts +80 -3
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +80 -3
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.mjs +93 -3
- package/dist/PostgresKyselyMigrator.mjs.map +1 -0
- package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +178 -2
- package/dist/PostgresMigrator.cjs.map +1 -0
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +175 -2
- package/dist/PostgresMigrator.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +118 -3
- package/dist/PostgresObjectionMigrator.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator.d.cts +75 -3
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
- package/dist/PostgresObjectionMigrator.d.mts +75 -3
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
- package/dist/PostgresObjectionMigrator.mjs +117 -3
- package/dist/PostgresObjectionMigrator.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
- package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
- package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
- package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
- package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +187 -4
- package/dist/VitestTransactionIsolator.cjs.map +1 -0
- package/dist/VitestTransactionIsolator.d.cts +219 -2
- package/dist/VitestTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.d.mts +219 -2
- package/dist/VitestTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator.mjs +184 -2
- package/dist/VitestTransactionIsolator.mjs.map +1 -0
- package/dist/aws.cjs +3 -4
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +3 -8
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +3 -8
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +1 -2
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs +2 -2
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +4 -6
- package/dist/benchmark.d.cts.map +1 -1
- package/dist/benchmark.d.mts +4 -6
- package/dist/benchmark.d.mts.map +1 -1
- package/dist/benchmark.mjs +1 -1
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +31 -15
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +13142 -4
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts +13142 -4
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/better-auth.mjs +29 -13
- package/dist/better-auth.mjs.map +1 -1
- package/dist/browser.cjs +127 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +47 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +47 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +125 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/context.cjs +80 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +55 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.mts +55 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.mjs +73 -0
- package/dist/context.mjs.map +1 -0
- package/dist/faker.cjs +262 -8
- package/dist/faker.cjs.map +1 -0
- package/dist/faker.d.cts +162 -2
- package/dist/faker.d.cts.map +1 -0
- package/dist/faker.d.mts +162 -2
- package/dist/faker.d.mts.map +1 -0
- package/dist/faker.mjs +255 -2
- package/dist/faker.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +1 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +1 -2
- package/dist/helpers.mjs.map +1 -1
- package/dist/initScript.cjs +15 -15
- package/dist/initScript.cjs.map +1 -1
- package/dist/initScript.d.cts +2 -4
- package/dist/initScript.d.cts.map +1 -1
- package/dist/initScript.d.mts +2 -4
- package/dist/initScript.d.mts.map +1 -1
- package/dist/initScript.mjs +10 -12
- package/dist/initScript.mjs.map +1 -1
- package/dist/kysely.cjs +10 -13
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +10 -14
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +10 -14
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +10 -14
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -5
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.mts +1 -5
- package/dist/logger.d.mts.map +1 -1
- package/dist/logger.mjs +1 -2
- package/dist/logger.mjs.map +1 -1
- package/dist/mailbox.cjs +95 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +58 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +58 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +92 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/objection.cjs +10 -13
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +10 -14
- package/dist/objection.d.cts.map +1 -1
- package/dist/objection.d.mts +10 -14
- package/dist/objection.d.mts.map +1 -1
- package/dist/objection.mjs +10 -14
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.cjs +31 -2
- package/dist/os/directory.cjs.map +1 -0
- package/dist/os/directory.d.cts +9 -2
- package/dist/os/directory.d.cts.map +1 -0
- package/dist/os/directory.d.mts +9 -2
- package/dist/os/directory.d.mts.map +1 -0
- package/dist/os/directory.mjs +25 -2
- package/dist/os/directory.mjs.map +1 -0
- package/dist/os/index.cjs +3 -3
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/os/index.mjs +2 -3
- package/dist/requestContext.cjs +10 -11
- package/dist/requestContext.cjs.map +1 -1
- package/dist/requestContext.d.cts +4 -8
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts +4 -8
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/requestContext.mjs +1 -2
- package/dist/requestContext.mjs.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
- package/dist/timer.cjs +2 -2
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +1 -4
- package/dist/timer.d.cts.map +1 -1
- package/dist/timer.d.mts +1 -4
- package/dist/timer.d.mts.map +1 -1
- package/dist/timer.mjs +1 -1
- package/dist/timer.mjs.map +1 -1
- package/dist/transactions.cjs +123 -0
- package/dist/transactions.cjs.map +1 -0
- package/dist/transactions.d.cts +44 -0
- package/dist/transactions.d.cts.map +1 -0
- package/dist/transactions.d.mts +44 -0
- package/dist/transactions.d.mts.map +1 -0
- package/dist/transactions.mjs +118 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +59 -16
- package/dist/Factory-BFVnMMCC.mjs +0 -51
- package/dist/Factory-BFVnMMCC.mjs.map +0 -1
- package/dist/Factory-BOX312yd.d.cts +0 -147
- package/dist/Factory-BOX312yd.d.cts.map +0 -1
- package/dist/Factory-BhjUOBWN.cjs +0 -57
- package/dist/Factory-BhjUOBWN.cjs.map +0 -1
- package/dist/Factory-C6W78ulZ.d.mts +0 -147
- package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
- package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
- package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
- package/dist/KyselyFactory-DMswpwji.mjs +0 -241
- package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
- package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
- package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
- package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
- package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
- package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
- package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
- package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
- package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
- package/dist/directory-BxjqmtCT.mjs +0 -27
- package/dist/directory-BxjqmtCT.mjs.map +0 -1
- package/dist/directory-DD_xQoSh.cjs +0 -33
- package/dist/directory-DD_xQoSh.cjs.map +0 -1
- package/dist/directory-DGOcVlKD.d.cts +0 -13
- package/dist/directory-DGOcVlKD.d.cts.map +0 -1
- package/dist/directory-DiQ_DkaF.d.mts +0 -13
- package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
- package/dist/faker-B14IEMIN.cjs +0 -304
- package/dist/faker-B14IEMIN.cjs.map +0 -1
- package/dist/faker-BGKYFoCT.mjs +0 -262
- package/dist/faker-BGKYFoCT.mjs.map +0 -1
- package/dist/faker-Cg76aFNO.d.cts +0 -166
- package/dist/faker-Cg76aFNO.d.cts.map +0 -1
- package/dist/faker-D9gz7KjY.d.mts +0 -166
- package/dist/faker-D9gz7KjY.d.mts.map +0 -1
package/dist/better-auth.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"better-auth.mjs","names":["initialData?: Record<string, any[]>","data: Map<string, any>","modelName: string","result: Record<string, any[]>","record: any","where?: Where[]","result: boolean | null","records: any[]","sortBy?: SortBy","config: MemoryAdapterConfig","toDelete: string[]"],"sources":["../src/better-auth.ts"],"sourcesContent":["import {\n\tcreateAdapterFactory,\n\ttype DBAdapterDebugLogOption,\n\ttype Where,\n} from 'better-auth/adapters';\n\ninterface MemoryAdapterConfig {\n\tdebugLogs?: DBAdapterDebugLogOption;\n\tusePlural?: boolean;\n\tinitialData?: Record<string, any[]>;\n}\n\nclass MemoryStore {\n\tconstructor(\n\t\tinitialData?: Record<string, any[]>,\n\t\tprivate readonly data: Map<string, any> = new Map(),\n\t) {\n\t\tif (initialData) {\n\t\t\tfor (const [model, records] of Object.entries(initialData)) {\n\t\t\t\tconst modelData = new Map();\n\t\t\t\tfor (const record of records) {\n\t\t\t\t\tmodelData.set(record.id, { ...record });\n\t\t\t\t}\n\t\t\t\tthis.data.set(model, modelData);\n\t\t\t}\n\t\t}\n\t}\n\n\tgetModel(modelName: string): Map<string, any> {\n\t\tif (!this.data.has(modelName)) {\n\t\t\tthis.data.set(modelName, new Map());\n\t\t}\n\t\treturn this.data.get(modelName)!;\n\t}\n\n\tclear() {\n\t\tthis.data.clear();\n\t}\n\n\tgetAllData() {\n\t\tconst result: Record<string, any[]> = {};\n\t\tfor (const [model, records] of this.data.entries()) {\n\t\t\tresult[model] = Array.from(records.values());\n\t\t}\n\t\treturn result;\n\t}\n}\n\nfunction matchesWhere(record: any, where?: Where[]): boolean {\n\tif (!where || where.length === 0) return true;\n\n\tlet result: boolean | null = null;\n\n\tfor (const condition of where) {\n\t\tconst { field, value, operator = 'eq', connector = 'AND' } = condition;\n\t\tconst recordValue = record[field];\n\n\t\tlet matches = false;\n\n\t\tswitch (operator) {\n\t\t\tcase 'eq':\n\t\t\t\tmatches = recordValue === value;\n\t\t\t\tbreak;\n\t\t\tcase 'ne':\n\t\t\t\tmatches = recordValue !== value;\n\t\t\t\tbreak;\n\t\t\tcase 'lt':\n\t\t\t\tmatches = value != null && recordValue < value;\n\t\t\t\tbreak;\n\t\t\tcase 'lte':\n\t\t\t\tmatches = value != null && recordValue <= value;\n\t\t\t\tbreak;\n\t\t\tcase 'gt':\n\t\t\t\tmatches = value != null && recordValue > value;\n\t\t\t\tbreak;\n\t\t\tcase 'gte':\n\t\t\t\tmatches = value != null && recordValue >= value;\n\t\t\t\tbreak;\n\t\t\tcase 'in':\n\t\t\t\tmatches =\n\t\t\t\t\tArray.isArray(value) && (value as unknown[]).includes(recordValue);\n\t\t\t\tbreak;\n\t\t\tcase 'not_in':\n\t\t\t\tmatches =\n\t\t\t\t\tArray.isArray(value) && !(value as unknown[]).includes(recordValue);\n\t\t\t\tbreak;\n\t\t\tcase 'contains':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.includes(value);\n\t\t\t\tbreak;\n\t\t\tcase 'starts_with':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.startsWith(value);\n\t\t\t\tbreak;\n\t\t\tcase 'ends_with':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.endsWith(value);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tmatches = recordValue === value;\n\t\t}\n\n\t\t// Apply connector logic\n\t\tif (result === null) {\n\t\t\tresult = matches;\n\t\t} else if (connector === 'OR') {\n\t\t\tresult = result || matches;\n\t\t} else {\n\t\t\t// Default: AND\n\t\t\tresult = result && matches;\n\t\t}\n\t}\n\n\treturn result ?? true;\n}\ntype SortBy = {\n\tfield: string;\n\tdirection: 'asc' | 'desc';\n};\n\nfunction applySorting(records: any[], sortBy?: SortBy): any[] {\n\tif (!sortBy) return records;\n\n\tconst { field, direction } = sortBy;\n\n\treturn records.sort((a, b) => {\n\t\tconst aVal = a[field];\n\t\tconst bVal = b[field];\n\n\t\tif (aVal === bVal) return 0;\n\t\tif (aVal == null) return direction === 'asc' ? -1 : 1;\n\t\tif (bVal == null) return direction === 'asc' ? 1 : -1;\n\n\t\tconst comparison = aVal < bVal ? -1 : 1;\n\t\treturn direction === 'asc' ? comparison : -comparison;\n\t});\n}\n\nexport const memoryAdapter = (\n\tconfig: MemoryAdapterConfig = {},\n\tstore = new Map(),\n) => {\n\tconst storeInstance = new MemoryStore(config.initialData, store);\n\n\tconst adapterInstance = createAdapterFactory({\n\t\tconfig: {\n\t\t\tadapterId: 'memory-adapter',\n\t\t\tadapterName: 'Memory Adapter',\n\t\t\tusePlural: config.usePlural ?? false,\n\t\t\tdebugLogs: config.debugLogs ?? false,\n\t\t\tsupportsJSON: true,\n\t\t\tsupportsDates: true,\n\t\t\tsupportsBooleans: true,\n\t\t\tsupportsNumericIds: false,\n\t\t},\n\n\t\t// Note: The framework automatically transforms field names in both directions:\n\t\t// - Input data is already transformed before reaching the adapter (e.g., email -> email_address)\n\t\t// - Output data is automatically transformed after the adapter returns (e.g., email_address -> email)\n\t\t// Therefore, we should NOT call transformInput or transformOutput ourselves.\n\t\tadapter: ({ debugLog, getModelName, transformWhereClause }) => ({\n\t\t\tcreate: async ({ data, model }) => {\n\t\t\t\tdebugLog('CREATE', { model, data });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\n\t\t\t\t// Data is already transformed by the framework - store directly\n\t\t\t\tconst record = data as Record<string, unknown>;\n\t\t\t\tif (!record.id) {\n\t\t\t\t\trecord.id = crypto.randomUUID();\n\t\t\t\t}\n\n\t\t\t\tconst id = record.id as string;\n\t\t\t\tmodelData.set(id, { ...record });\n\t\t\t\treturn modelData.get(id);\n\t\t\t},\n\n\t\t\tfindOne: async ({ where, model }) => {\n\t\t\t\tdebugLog('FIND_ONE', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tfor (const record of modelData.values()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\treturn record;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t},\n\n\t\t\tfindMany: async ({ where, model, limit, offset, sortBy }) => {\n\t\t\t\tdebugLog('FIND_MANY', { model, where });\n\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tlet results = Array.from(modelData.values()).filter((record) =>\n\t\t\t\t\tmatchesWhere(record, transformedWhere),\n\t\t\t\t);\n\n\t\t\t\tif (sortBy) {\n\t\t\t\t\tresults = applySorting(results, sortBy);\n\t\t\t\t}\n\n\t\t\t\tif (offset) {\n\t\t\t\t\tresults = results.slice(offset);\n\t\t\t\t}\n\t\t\t\tif (limit) {\n\t\t\t\t\tresults = results.slice(0, limit);\n\t\t\t\t}\n\n\t\t\t\treturn results;\n\t\t\t},\n\n\t\t\tupdate: async ({ where, update, model }) => {\n\t\t\t\tdebugLog('UPDATE', { model, where });\n\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\t// Update data is already transformed by the framework\n\t\t\t\t\t\tconst updated = { ...record, ...update };\n\t\t\t\t\t\tmodelData.set(id, updated);\n\t\t\t\t\t\treturn updated;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t},\n\n\t\t\tupdateMany: async ({ where, update, model }) => {\n\t\t\t\tdebugLog('UPDATE_MANY', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tlet count = 0;\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\t// Update data is already transformed by the framework\n\t\t\t\t\t\tmodelData.set(id, { ...record, ...update });\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn count;\n\t\t\t},\n\n\t\t\tdelete: async ({ where, model }) => {\n\t\t\t\tdebugLog('DELETE', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\tmodelData.delete(id);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tdeleteMany: async ({ where, model }) => {\n\t\t\t\tdebugLog('DELETE_MANY', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\tconst toDelete: string[] = [];\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\ttoDelete.push(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor (const id of toDelete) {\n\t\t\t\t\tmodelData.delete(id);\n\t\t\t\t}\n\t\t\t\treturn toDelete.length;\n\t\t\t},\n\n\t\t\tcount: async ({ where, model }) => {\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({ model, where });\n\n\t\t\t\treturn Array.from(modelData.values()).filter((record) =>\n\t\t\t\t\tmatchesWhere(record, transformedWhere),\n\t\t\t\t).length;\n\t\t\t},\n\t\t}),\n\t});\n\n\t// Add utility methods to the adapter\n\treturn Object.assign(adapterInstance, {\n\t\tclear: () => store.clear(),\n\t\tgetAllData: () => storeInstance.getAllData(),\n\t\tgetStore: () => store,\n\t});\n};\n"],"mappings":";;;AAYA,IAAM,cAAN,MAAkB;CACjB,YACCA,aACiBC,uBAAyB,IAAI,OAC7C;EADgB;AAEjB,MAAI,YACH,MAAK,MAAM,CAAC,OAAO,QAAQ,IAAI,OAAO,QAAQ,YAAY,EAAE;GAC3D,MAAM,4BAAY,IAAI;AACtB,QAAK,MAAM,UAAU,QACpB,WAAU,IAAI,OAAO,IAAI,EAAE,GAAG,OAAQ,EAAC;AAExC,QAAK,KAAK,IAAI,OAAO,UAAU;EAC/B;CAEF;CAED,SAASC,WAAqC;AAC7C,OAAK,KAAK,KAAK,IAAI,UAAU,CAC5B,MAAK,KAAK,IAAI,2BAAW,IAAI,MAAM;AAEpC,SAAO,KAAK,KAAK,IAAI,UAAU;CAC/B;CAED,QAAQ;AACP,OAAK,KAAK,OAAO;CACjB;CAED,aAAa;EACZ,MAAMC,SAAgC,CAAE;AACxC,OAAK,MAAM,CAAC,OAAO,QAAQ,IAAI,KAAK,KAAK,SAAS,CACjD,QAAO,SAAS,MAAM,KAAK,QAAQ,QAAQ,CAAC;AAE7C,SAAO;CACP;AACD;AAED,SAAS,aAAaC,QAAaC,OAA0B;AAC5D,MAAK,SAAS,MAAM,WAAW,EAAG,QAAO;CAEzC,IAAIC,SAAyB;AAE7B,MAAK,MAAM,aAAa,OAAO;EAC9B,MAAM,EAAE,OAAO,OAAO,WAAW,MAAM,YAAY,OAAO,GAAG;EAC7D,MAAM,cAAc,OAAO;EAE3B,IAAI,UAAU;AAEd,UAAQ,UAAR;GACC,KAAK;AACJ,cAAU,gBAAgB;AAC1B;GACD,KAAK;AACJ,cAAU,gBAAgB;AAC1B;GACD,KAAK;AACJ,cAAU,SAAS,QAAQ,cAAc;AACzC;GACD,KAAK;AACJ,cAAU,SAAS,QAAQ,eAAe;AAC1C;GACD,KAAK;AACJ,cA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S,eAAe;KAAE;KAAO;IAAO,EAAC;IACzC,MAAM,YAAY,aAAa,MAAM;IACrC,MAAM,YAAY,cAAc,SAAS,UAAU;IACnD,MAAM,mBAAmB,qBAAqB;KAAE;KAAO;IAAO,EAAC;IAE/D,MAAMC,WAAqB,CAAE;AAC7B,SAAK,MAAM,CAAC,IAAI,OAAO,IAAI,UAAU,SAAS,CAC7C,KAAI,aAAa,QAAQ,iBAAiB,CACzC,UAAS,KAAK,GAAG;AAInB,SAAK,MAAM,MAAM,SAChB,WAAU,OAAO,GAAG;AAErB,WAAO,SAAS;GAChB;GAED,OAAO,OAAO,EAAE,OAAO,OAAO,KAAK;IAClC,MAAM,YAAY,aAAa,MAAM;IACrC,MAAM,YAAY,cAAc,SAAS,UAAU;IACnD,MAAM,mBAAmB,qBAAqB;KAAE;KAAO;IAAO,EAAC;AAE/D,WAAO,MAAM,KAAK,UAAU,QAAQ,CAAC,CAAC,OAAO,CAAC,WAC7C,aAAa,QAAQ,iBAAiB,CACtC,CAAC;GACF;EACD;CACD,EAAC;AAGF,QAAO,OAAO,OAAO,iBAAiB;EACrC,OAAO,MAAM,MAAM,OAAO;EAC1B,YAAY,MAAM,cAAc,YAAY;EAC5C,UAAU,MAAM;CAChB,EAAC;AACF"}
|
|
1
|
+
{"version":3,"file":"better-auth.mjs","names":[],"sources":["../src/better-auth.ts"],"sourcesContent":["import {\n\tcreateAdapterFactory,\n\ttype DBAdapterDebugLogOption,\n\ttype Where,\n} from 'better-auth/adapters';\n\ninterface MemoryAdapterConfig {\n\tdebugLogs?: DBAdapterDebugLogOption;\n\tusePlural?: boolean;\n\tinitialData?: Record<string, any[]>;\n}\n\nclass MemoryStore {\n\tconstructor(\n\t\tinitialData?: Record<string, any[]>,\n\t\tprivate readonly data: Map<string, any> = new Map(),\n\t) {\n\t\tif (initialData) {\n\t\t\tfor (const [model, records] of Object.entries(initialData)) {\n\t\t\t\tconst modelData = new Map();\n\t\t\t\tfor (const record of records) {\n\t\t\t\t\tmodelData.set(record.id, { ...record });\n\t\t\t\t}\n\t\t\t\tthis.data.set(model, modelData);\n\t\t\t}\n\t\t}\n\t}\n\n\tgetModel(modelName: string): Map<string, any> {\n\t\tif (!this.data.has(modelName)) {\n\t\t\tthis.data.set(modelName, new Map());\n\t\t}\n\t\treturn this.data.get(modelName)!;\n\t}\n\n\tclear() {\n\t\tthis.data.clear();\n\t}\n\n\tgetAllData() {\n\t\tconst result: Record<string, any[]> = {};\n\t\tfor (const [model, records] of this.data.entries()) {\n\t\t\tresult[model] = Array.from(records.values());\n\t\t}\n\t\treturn result;\n\t}\n}\n\nfunction matchesWhere(record: any, where?: Where[]): boolean {\n\tif (!where || where.length === 0) return true;\n\n\tlet result: boolean | null = null;\n\n\tfor (const condition of where) {\n\t\tconst { field, operator = 'eq', connector = 'AND', mode } = condition;\n\t\t// `mode: 'insensitive'` compares strings case-folded on both sides — in\n\t\t// `in`/`not_in` lists too — the way the SQL adapters use ILIKE/LOWER().\n\t\tconst fold =\n\t\t\tmode === 'insensitive'\n\t\t\t\t? (v: unknown) => (typeof v === 'string' ? v.toLowerCase() : v)\n\t\t\t\t: (v: unknown) => v;\n\t\tconst recordValue = fold(record[field]) as any;\n\t\tconst value = (\n\t\t\tArray.isArray(condition.value)\n\t\t\t\t? (condition.value as unknown[]).map(fold)\n\t\t\t\t: fold(condition.value)\n\t\t) as any;\n\n\t\tlet matches = false;\n\n\t\tswitch (operator) {\n\t\t\tcase 'eq':\n\t\t\t\tmatches = recordValue === value;\n\t\t\t\tbreak;\n\t\t\tcase 'ne':\n\t\t\t\tmatches = recordValue !== value;\n\t\t\t\tbreak;\n\t\t\tcase 'lt':\n\t\t\t\tmatches = value != null && recordValue < value;\n\t\t\t\tbreak;\n\t\t\tcase 'lte':\n\t\t\t\tmatches = value != null && recordValue <= value;\n\t\t\t\tbreak;\n\t\t\tcase 'gt':\n\t\t\t\tmatches = value != null && recordValue > value;\n\t\t\t\tbreak;\n\t\t\tcase 'gte':\n\t\t\t\tmatches = value != null && recordValue >= value;\n\t\t\t\tbreak;\n\t\t\tcase 'in':\n\t\t\t\tmatches =\n\t\t\t\t\tArray.isArray(value) && (value as unknown[]).includes(recordValue);\n\t\t\t\tbreak;\n\t\t\tcase 'not_in':\n\t\t\t\tmatches =\n\t\t\t\t\tArray.isArray(value) && !(value as unknown[]).includes(recordValue);\n\t\t\t\tbreak;\n\t\t\tcase 'contains':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.includes(value);\n\t\t\t\tbreak;\n\t\t\tcase 'starts_with':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.startsWith(value);\n\t\t\t\tbreak;\n\t\t\tcase 'ends_with':\n\t\t\t\tmatches =\n\t\t\t\t\ttypeof recordValue === 'string' &&\n\t\t\t\t\ttypeof value === 'string' &&\n\t\t\t\t\trecordValue.endsWith(value);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tmatches = recordValue === value;\n\t\t}\n\n\t\t// Apply connector logic\n\t\tif (result === null) {\n\t\t\tresult = matches;\n\t\t} else if (connector === 'OR') {\n\t\t\tresult = result || matches;\n\t\t} else {\n\t\t\t// Default: AND\n\t\t\tresult = result && matches;\n\t\t}\n\t}\n\n\treturn result ?? true;\n}\ntype SortBy = {\n\tfield: string;\n\tdirection: 'asc' | 'desc';\n};\n\nfunction applySorting(records: any[], sortBy?: SortBy): any[] {\n\tif (!sortBy) return records;\n\n\tconst { field, direction } = sortBy;\n\n\treturn records.sort((a, b) => {\n\t\tconst aVal = a[field];\n\t\tconst bVal = b[field];\n\n\t\tif (aVal === bVal) return 0;\n\t\tif (aVal == null) return direction === 'asc' ? -1 : 1;\n\t\tif (bVal == null) return direction === 'asc' ? 1 : -1;\n\n\t\tconst comparison = aVal < bVal ? -1 : 1;\n\t\treturn direction === 'asc' ? comparison : -comparison;\n\t});\n}\n\nexport const memoryAdapter = (\n\tconfig: MemoryAdapterConfig = {},\n\tstore = new Map(),\n) => {\n\tconst storeInstance = new MemoryStore(config.initialData, store);\n\n\tconst adapterInstance = createAdapterFactory({\n\t\tconfig: {\n\t\t\tadapterId: 'memory-adapter',\n\t\t\tadapterName: 'Memory Adapter',\n\t\t\tusePlural: config.usePlural ?? false,\n\t\t\tdebugLogs: config.debugLogs ?? false,\n\t\t\tsupportsJSON: true,\n\t\t\tsupportsDates: true,\n\t\t\tsupportsBooleans: true,\n\t\t\tsupportsNumericIds: false,\n\t\t},\n\n\t\t// Note: The framework automatically transforms field names in both directions:\n\t\t// - Input data is already transformed before reaching the adapter (e.g., email -> email_address)\n\t\t// - Output data is automatically transformed after the adapter returns (e.g., email_address -> email)\n\t\t// Therefore, we should NOT call transformInput or transformOutput ourselves.\n\t\tadapter: ({\n\t\t\tdebugLog,\n\t\t\tgetModelName,\n\t\t\tgetFieldName,\n\t\t\ttransformWhereClause,\n\t\t}) => ({\n\t\t\tcreate: async ({ data, model }) => {\n\t\t\t\tdebugLog('CREATE', { model, data });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\n\t\t\t\t// Data is already transformed by the framework - store directly\n\t\t\t\tconst record = data as Record<string, unknown>;\n\t\t\t\tif (!record.id) {\n\t\t\t\t\trecord.id = crypto.randomUUID();\n\t\t\t\t}\n\n\t\t\t\tconst id = record.id as string;\n\t\t\t\tmodelData.set(id, { ...record });\n\t\t\t\treturn modelData.get(id);\n\t\t\t},\n\n\t\t\tfindOne: async ({ where, model }) => {\n\t\t\t\tdebugLog('FIND_ONE', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'findOne',\n\t\t\t\t});\n\n\t\t\t\tfor (const record of modelData.values()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\treturn record;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t},\n\n\t\t\tfindMany: async ({ where, model, limit, offset, sortBy, select }) => {\n\t\t\t\tdebugLog('FIND_MANY', { model, where });\n\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'findMany',\n\t\t\t\t});\n\n\t\t\t\tlet results = Array.from(modelData.values()).filter((record) =>\n\t\t\t\t\tmatchesWhere(record, transformedWhere),\n\t\t\t\t);\n\n\t\t\t\tif (sortBy) {\n\t\t\t\t\tresults = applySorting(results, sortBy);\n\t\t\t\t}\n\n\t\t\t\tif (offset) {\n\t\t\t\t\tresults = results.slice(offset);\n\t\t\t\t}\n\t\t\t\tif (limit) {\n\t\t\t\t\tresults = results.slice(0, limit);\n\t\t\t\t}\n\n\t\t\t\t// Only the fields asked for, as a SELECT list would return — under\n\t\t\t\t// their stored names, which differ when the schema renames a field\n\t\t\t\t// (`fields: { email: 'email_address' }`).\n\t\t\t\tif (select?.length) {\n\t\t\t\t\tconst columns = select.map((field) => getFieldName({ model, field }));\n\t\t\t\t\treturn results.map((record) =>\n\t\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\t\tcolumns.map((column) => [column, record[column]]),\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn results;\n\t\t\t},\n\n\t\t\tupdate: async ({ where, update, model }) => {\n\t\t\t\tdebugLog('UPDATE', { model, where });\n\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'update',\n\t\t\t\t});\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\t// Update data is already transformed by the framework\n\t\t\t\t\t\tconst updated = { ...record, ...update };\n\t\t\t\t\t\tmodelData.set(id, updated);\n\t\t\t\t\t\treturn updated;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t},\n\n\t\t\tupdateMany: async ({ where, update, model }) => {\n\t\t\t\tdebugLog('UPDATE_MANY', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'updateMany',\n\t\t\t\t});\n\n\t\t\t\tlet count = 0;\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\t// Update data is already transformed by the framework\n\t\t\t\t\t\tmodelData.set(id, { ...record, ...update });\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn count;\n\t\t\t},\n\n\t\t\tdelete: async ({ where, model }) => {\n\t\t\t\tdebugLog('DELETE', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'delete',\n\t\t\t\t});\n\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\tmodelData.delete(id);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tdeleteMany: async ({ where, model }) => {\n\t\t\t\tdebugLog('DELETE_MANY', { model, where });\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'deleteMany',\n\t\t\t\t});\n\n\t\t\t\tconst toDelete: string[] = [];\n\t\t\t\tfor (const [id, record] of modelData.entries()) {\n\t\t\t\t\tif (matchesWhere(record, transformedWhere)) {\n\t\t\t\t\t\ttoDelete.push(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor (const id of toDelete) {\n\t\t\t\t\tmodelData.delete(id);\n\t\t\t\t}\n\t\t\t\treturn toDelete.length;\n\t\t\t},\n\n\t\t\tcount: async ({ where, model }) => {\n\t\t\t\tconst modelName = getModelName(model);\n\t\t\t\tconst modelData = storeInstance.getModel(modelName);\n\t\t\t\tconst transformedWhere = transformWhereClause({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\taction: 'count',\n\t\t\t\t});\n\n\t\t\t\treturn Array.from(modelData.values()).filter((record) =>\n\t\t\t\t\tmatchesWhere(record, transformedWhere),\n\t\t\t\t).length;\n\t\t\t},\n\t\t}),\n\t});\n\n\t// Add utility methods to the adapter\n\treturn Object.assign(adapterInstance, {\n\t\tclear: () => store.clear(),\n\t\tgetAllData: () => storeInstance.getAllData(),\n\t\tgetStore: () => store,\n\t});\n};\n"],"mappings":";;AAYA,IAAM,cAAN,MAAkB;CAGC;CAFlB,YACC,aACA,uBAA0C,IAAI,IAAI,GACjD;EADgB,KAAA,OAAA;EAEjB,IAAI,aACH,KAAK,MAAM,CAAC,OAAO,YAAY,OAAO,QAAQ,WAAW,GAAG;GAC3D,MAAM,4BAAY,IAAI,IAAI;GAC1B,KAAK,MAAM,UAAU,SACpB,UAAU,IAAI,OAAO,IAAI,EAAE,GAAG,OAAO,CAAC;GAEvC,KAAK,KAAK,IAAI,OAAO,SAAS;EAC/B;CAEF;CAEA,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AO;IAAM,CAAC;IACxC,MAAM,YAAY,aAAa,KAAK;IACpC,MAAM,YAAY,cAAc,SAAS,SAAS;IAClD,MAAM,mBAAmB,qBAAqB;KAC7C;KACA;KACA,QAAQ;IACT,CAAC;IAED,MAAM,WAAqB,CAAC;IAC5B,KAAK,MAAM,CAAC,IAAI,WAAW,UAAU,QAAQ,GAC5C,IAAI,aAAa,QAAQ,gBAAgB,GACxC,SAAS,KAAK,EAAE;IAIlB,KAAK,MAAM,MAAM,UAChB,UAAU,OAAO,EAAE;IAEpB,OAAO,SAAS;GACjB;GAEA,OAAO,OAAO,EAAE,OAAO,YAAY;IAClC,MAAM,YAAY,aAAa,KAAK;IACpC,MAAM,YAAY,cAAc,SAAS,SAAS;IAClD,MAAM,mBAAmB,qBAAqB;KAC7C;KACA;KACA,QAAQ;IACT,CAAC;IAED,OAAO,MAAM,KAAK,UAAU,OAAO,CAAC,CAAC,CAAC,QAAQ,WAC7C,aAAa,QAAQ,gBAAgB,CACtC,CAAC,CAAC;GACH;EACD;CACD,CAAC;CAGD,OAAO,OAAO,OAAO,iBAAiB;EACrC,aAAa,MAAM,MAAM;EACzB,kBAAkB,cAAc,WAAW;EAC3C,gBAAgB;CACjB,CAAC;AACF"}
|
package/dist/browser.cjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_context = require("./context.cjs");
|
|
3
|
+
let tough_cookie = require("tough-cookie");
|
|
4
|
+
//#region src/browser.ts
|
|
5
|
+
/**
|
|
6
|
+
* A browser, for tests: a `fetch` that keeps cookies the way a browser does.
|
|
7
|
+
*
|
|
8
|
+
* An app's real clients — its typed API client, better-auth's client — run on
|
|
9
|
+
* whatever `fetch` they are given. Give them this one and a test signs in the
|
|
10
|
+
* way a person does: the auth server's `Set-Cookie` lands in the jar, and every
|
|
11
|
+
* later request to a URL the cookie belongs to carries it. The jar follows the
|
|
12
|
+
* browser's rules (RFC 6265 — Domain, Path, Secure, Expires), so a cookie set
|
|
13
|
+
* for the wrong domain is not sent, exactly as a browser would not send it.
|
|
14
|
+
*
|
|
15
|
+
* Extend it once per app with that app's clients:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* export class Browser extends TestBrowser {
|
|
19
|
+
* readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
/** How many redirects `fetch` follows before giving up, as browsers do. */
|
|
24
|
+
const MAX_REDIRECTS = 20;
|
|
25
|
+
var Browser = class {
|
|
26
|
+
/** The cookies this browser holds. */
|
|
27
|
+
jar = new tough_cookie.CookieJar();
|
|
28
|
+
/**
|
|
29
|
+
* This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`
|
|
30
|
+
* into the jar on the way back, redirects followed hop by hop so each hop's
|
|
31
|
+
* cookies are kept.
|
|
32
|
+
*
|
|
33
|
+
* On the server side of a test — a handler serving a request — it is the
|
|
34
|
+
* plain `fetch`: a server forwards what it was handed and never reaches into
|
|
35
|
+
* a browser's jar.
|
|
36
|
+
*/
|
|
37
|
+
fetch;
|
|
38
|
+
send;
|
|
39
|
+
constructor(options = {}) {
|
|
40
|
+
this.send = options.fetch ?? globalThis.fetch;
|
|
41
|
+
this.fetch = (input, init) => {
|
|
42
|
+
if (require_context.currentTestContext()?.side === "server") return this.send(input, init);
|
|
43
|
+
return this.start(new Request(input, init), init?.redirect);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Open a URL the way clicking a link does: a GET, redirects followed. */
|
|
47
|
+
visit(url) {
|
|
48
|
+
return this.fetch(String(url));
|
|
49
|
+
}
|
|
50
|
+
/** The cookies this browser would send to `url`, as a header value. */
|
|
51
|
+
cookieHeader(url) {
|
|
52
|
+
return this.jar.getCookieString(String(url));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Make this browser the global `fetch`, for code that uses the global one —
|
|
56
|
+
* an app's module-level clients. Returns what puts the previous one back.
|
|
57
|
+
*/
|
|
58
|
+
install() {
|
|
59
|
+
const previous = globalThis.fetch;
|
|
60
|
+
globalThis.fetch = this.fetch;
|
|
61
|
+
return () => {
|
|
62
|
+
globalThis.fetch = previous;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Read the body once, so a redirect that keeps the method can send it again. */
|
|
66
|
+
async start(request, redirect) {
|
|
67
|
+
const body = request.method === "GET" || request.method === "HEAD" ? void 0 : await request.arrayBuffer();
|
|
68
|
+
return this.request({
|
|
69
|
+
url: request.url,
|
|
70
|
+
method: request.method,
|
|
71
|
+
headers: new Headers(request.headers),
|
|
72
|
+
body
|
|
73
|
+
}, redirect);
|
|
74
|
+
}
|
|
75
|
+
async request(request, redirect, hops = 0) {
|
|
76
|
+
const headers = new Headers(request.headers);
|
|
77
|
+
const cookie = await this.jar.getCookieString(request.url);
|
|
78
|
+
if (cookie) {
|
|
79
|
+
const existing = headers.get("cookie");
|
|
80
|
+
headers.set("cookie", existing ? `${existing}; ${cookie}` : cookie);
|
|
81
|
+
}
|
|
82
|
+
const response = await this.send(request.url, {
|
|
83
|
+
method: request.method,
|
|
84
|
+
headers,
|
|
85
|
+
body: request.body,
|
|
86
|
+
redirect: "manual"
|
|
87
|
+
});
|
|
88
|
+
for (const set of response.headers.getSetCookie()) await this.jar.setCookie(set, request.url, { ignoreError: true });
|
|
89
|
+
const location = response.headers.get("location");
|
|
90
|
+
if (redirect === "manual" || !location || response.status < 300 || response.status >= 400) return response;
|
|
91
|
+
if (hops >= MAX_REDIRECTS) throw new TooManyRedirects(request.url, MAX_REDIRECTS);
|
|
92
|
+
const keepsMethod = response.status === 307 || response.status === 308 || request.method === "GET" || request.method === "HEAD";
|
|
93
|
+
const url = new URL(location, request.url).toString();
|
|
94
|
+
const next = keepsMethod ? {
|
|
95
|
+
...request,
|
|
96
|
+
url
|
|
97
|
+
} : {
|
|
98
|
+
url,
|
|
99
|
+
method: "GET",
|
|
100
|
+
headers: withoutBody(request.headers)
|
|
101
|
+
};
|
|
102
|
+
return this.request(next, redirect, hops + 1);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
/** Headers for a request that no longer carries the body they described. */
|
|
106
|
+
function withoutBody(headers) {
|
|
107
|
+
const next = new Headers(headers);
|
|
108
|
+
next.delete("content-type");
|
|
109
|
+
next.delete("content-length");
|
|
110
|
+
return next;
|
|
111
|
+
}
|
|
112
|
+
/** A redirect chain longer than any browser follows. */
|
|
113
|
+
var TooManyRedirects = class extends Error {
|
|
114
|
+
url;
|
|
115
|
+
limit;
|
|
116
|
+
constructor(url, limit) {
|
|
117
|
+
super(`${url} redirected more than ${limit} times. A browser gives up here too — look for a redirect loop.`);
|
|
118
|
+
this.url = url;
|
|
119
|
+
this.limit = limit;
|
|
120
|
+
this.name = "TooManyRedirects";
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
//#endregion
|
|
124
|
+
exports.Browser = Browser;
|
|
125
|
+
exports.TooManyRedirects = TooManyRedirects;
|
|
126
|
+
|
|
127
|
+
//# sourceMappingURL=browser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.cjs","names":["CookieJar","currentTestContext"],"sources":["../src/browser.ts"],"sourcesContent":["/**\n * A browser, for tests: a `fetch` that keeps cookies the way a browser does.\n *\n * An app's real clients — its typed API client, better-auth's client — run on\n * whatever `fetch` they are given. Give them this one and a test signs in the\n * way a person does: the auth server's `Set-Cookie` lands in the jar, and every\n * later request to a URL the cookie belongs to carries it. The jar follows the\n * browser's rules (RFC 6265 — Domain, Path, Secure, Expires), so a cookie set\n * for the wrong domain is not sent, exactly as a browser would not send it.\n *\n * Extend it once per app with that app's clients:\n *\n * ```ts\n * export class Browser extends TestBrowser {\n * readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });\n * }\n * ```\n */\n\nimport { CookieJar } from 'tough-cookie';\nimport { currentTestContext } from './context';\n\n/** How many redirects `fetch` follows before giving up, as browsers do. */\nconst MAX_REDIRECTS = 20;\n\nexport interface BrowserOptions {\n\t/**\n\t * The `fetch` requests go out on. Defaults to the global one at the time\n\t * the browser is created — so a browser made inside a test goes through\n\t * whatever the test installed (MSW, the context stamp) and never through\n\t * itself once installed.\n\t */\n\tfetch?: typeof fetch;\n}\n\nexport class Browser {\n\t/** The cookies this browser holds. */\n\treadonly jar = new CookieJar();\n\n\t/**\n\t * This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`\n\t * into the jar on the way back, redirects followed hop by hop so each hop's\n\t * cookies are kept.\n\t *\n\t * On the server side of a test — a handler serving a request — it is the\n\t * plain `fetch`: a server forwards what it was handed and never reaches into\n\t * a browser's jar.\n\t */\n\treadonly fetch: typeof fetch;\n\n\tprivate readonly send: typeof fetch;\n\n\tconstructor(options: BrowserOptions = {}) {\n\t\tthis.send = options.fetch ?? globalThis.fetch;\n\t\tthis.fetch = (input, init) => {\n\t\t\tif (currentTestContext()?.side === 'server') {\n\t\t\t\treturn this.send(input, init);\n\t\t\t}\n\t\t\treturn this.start(new Request(input, init), init?.redirect);\n\t\t};\n\t}\n\n\t/** Open a URL the way clicking a link does: a GET, redirects followed. */\n\tvisit(url: string | URL): Promise<Response> {\n\t\treturn this.fetch(String(url));\n\t}\n\n\t/** The cookies this browser would send to `url`, as a header value. */\n\tcookieHeader(url: string | URL): Promise<string> {\n\t\treturn this.jar.getCookieString(String(url));\n\t}\n\n\t/**\n\t * Make this browser the global `fetch`, for code that uses the global one —\n\t * an app's module-level clients. Returns what puts the previous one back.\n\t */\n\tinstall(): () => void {\n\t\tconst previous = globalThis.fetch;\n\t\tglobalThis.fetch = this.fetch;\n\t\treturn () => {\n\t\t\tglobalThis.fetch = previous;\n\t\t};\n\t}\n\n\t/** Read the body once, so a redirect that keeps the method can send it again. */\n\tprivate async start(\n\t\trequest: Request,\n\t\tredirect: RequestInit['redirect'],\n\t): Promise<Response> {\n\t\tconst body =\n\t\t\trequest.method === 'GET' || request.method === 'HEAD'\n\t\t\t\t? undefined\n\t\t\t\t: await request.arrayBuffer();\n\t\treturn this.request(\n\t\t\t{\n\t\t\t\turl: request.url,\n\t\t\t\tmethod: request.method,\n\t\t\t\theaders: new Headers(request.headers),\n\t\t\t\tbody,\n\t\t\t},\n\t\t\tredirect,\n\t\t);\n\t}\n\n\tprivate async request(\n\t\trequest: Hop,\n\t\tredirect: RequestInit['redirect'],\n\t\thops = 0,\n\t): Promise<Response> {\n\t\tconst headers = new Headers(request.headers);\n\t\tconst cookie = await this.jar.getCookieString(request.url);\n\t\tif (cookie) {\n\t\t\tconst existing = headers.get('cookie');\n\t\t\theaders.set('cookie', existing ? `${existing}; ${cookie}` : cookie);\n\t\t}\n\n\t\tconst response = await this.send(request.url, {\n\t\t\tmethod: request.method,\n\t\t\theaders,\n\t\t\tbody: request.body,\n\t\t\tredirect: 'manual',\n\t\t});\n\n\t\tfor (const set of response.headers.getSetCookie()) {\n\t\t\t// A cookie a browser would refuse — the wrong domain, Secure over\n\t\t\t// plain HTTP — is dropped silently, as a browser drops it.\n\t\t\tawait this.jar.setCookie(set, request.url, { ignoreError: true });\n\t\t}\n\n\t\tconst location = response.headers.get('location');\n\t\tif (\n\t\t\tredirect === 'manual' ||\n\t\t\t!location ||\n\t\t\tresponse.status < 300 ||\n\t\t\tresponse.status >= 400\n\t\t) {\n\t\t\treturn response;\n\t\t}\n\n\t\tif (hops >= MAX_REDIRECTS) {\n\t\t\tthrow new TooManyRedirects(request.url, MAX_REDIRECTS);\n\t\t}\n\n\t\t// 303, and 301/302 after a POST, continue as a GET without a body — what\n\t\t// every browser does, whatever the spec once said about 302.\n\t\tconst keepsMethod =\n\t\t\tresponse.status === 307 ||\n\t\t\tresponse.status === 308 ||\n\t\t\trequest.method === 'GET' ||\n\t\t\trequest.method === 'HEAD';\n\n\t\tconst url = new URL(location, request.url).toString();\n\t\tconst next: Hop = keepsMethod\n\t\t\t? { ...request, url }\n\t\t\t: { url, method: 'GET', headers: withoutBody(request.headers) };\n\n\t\treturn this.request(next, redirect, hops + 1);\n\t}\n}\n\n/** One request in a redirect chain, with its body already read. */\ninterface Hop {\n\turl: string;\n\tmethod: string;\n\theaders: Headers;\n\tbody?: ArrayBuffer;\n}\n\n/** Headers for a request that no longer carries the body they described. */\nfunction withoutBody(headers: Headers): Headers {\n\tconst next = new Headers(headers);\n\tnext.delete('content-type');\n\tnext.delete('content-length');\n\treturn next;\n}\n\n/** A redirect chain longer than any browser follows. */\nexport class TooManyRedirects extends Error {\n\tconstructor(\n\t\treadonly url: string,\n\t\treadonly limit: number,\n\t) {\n\t\tsuper(\n\t\t\t`${url} redirected more than ${limit} times. A browser gives up here ` +\n\t\t\t\t`too — look for a redirect loop.`,\n\t\t);\n\t\tthis.name = 'TooManyRedirects';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAM,gBAAgB;AAYtB,IAAa,UAAb,MAAqB;;CAEpB,MAAe,IAAIA,aAAAA,UAAU;;;;;;;;;;CAW7B;CAEA;CAEA,YAAY,UAA0B,CAAC,GAAG;EACzC,KAAK,OAAO,QAAQ,SAAS,WAAW;EACxC,KAAK,SAAS,OAAO,SAAS;GAC7B,IAAIC,gBAAAA,mBAAmB,CAAC,EAAE,SAAS,UAClC,OAAO,KAAK,KAAK,OAAO,IAAI;GAE7B,OAAO,KAAK,MAAM,IAAI,QAAQ,OAAO,IAAI,GAAG,MAAM,QAAQ;EAC3D;CACD;;CAGA,MAAM,KAAsC;EAC3C,OAAO,KAAK,MAAM,OAAO,GAAG,CAAC;CAC9B;;CAGA,aAAa,KAAoC;EAChD,OAAO,KAAK,IAAI,gBAAgB,OAAO,GAAG,CAAC;CAC5C;;;;;CAMA,UAAsB;EACrB,MAAM,WAAW,WAAW;EAC5B,WAAW,QAAQ,KAAK;EACxB,aAAa;GACZ,WAAW,QAAQ;EACpB;CACD;;CAGA,MAAc,MACb,SACA,UACoB;EACpB,MAAM,OACL,QAAQ,WAA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,OAAO,IAAI,QAAQ,OAAO;CAChC,KAAK,OAAO,cAAc;CAC1B,KAAK,OAAO,gBAAgB;CAC5B,OAAO;AACR;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,KACA,OACC;EACD,MACC,GAAG,IAAI,wBAAwB,MAAM,gEAEtC;EANS,KAAA,MAAA;EACA,KAAA,QAAA;EAMT,KAAK,OAAO;CACb;AACD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CookieJar } from "tough-cookie";
|
|
2
|
+
//#region src/browser.d.ts
|
|
3
|
+
export interface BrowserOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The `fetch` requests go out on. Defaults to the global one at the time
|
|
6
|
+
* the browser is created — so a browser made inside a test goes through
|
|
7
|
+
* whatever the test installed (MSW, the context stamp) and never through
|
|
8
|
+
* itself once installed.
|
|
9
|
+
*/
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
export declare class Browser {
|
|
13
|
+
/** The cookies this browser holds. */
|
|
14
|
+
readonly jar: CookieJar;
|
|
15
|
+
/**
|
|
16
|
+
* This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`
|
|
17
|
+
* into the jar on the way back, redirects followed hop by hop so each hop's
|
|
18
|
+
* cookies are kept.
|
|
19
|
+
*
|
|
20
|
+
* On the server side of a test — a handler serving a request — it is the
|
|
21
|
+
* plain `fetch`: a server forwards what it was handed and never reaches into
|
|
22
|
+
* a browser's jar.
|
|
23
|
+
*/
|
|
24
|
+
readonly fetch: typeof fetch;
|
|
25
|
+
private readonly send;
|
|
26
|
+
constructor(options?: BrowserOptions);
|
|
27
|
+
/** Open a URL the way clicking a link does: a GET, redirects followed. */
|
|
28
|
+
visit(url: string | URL): Promise<Response>;
|
|
29
|
+
/** The cookies this browser would send to `url`, as a header value. */
|
|
30
|
+
cookieHeader(url: string | URL): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Make this browser the global `fetch`, for code that uses the global one —
|
|
33
|
+
* an app's module-level clients. Returns what puts the previous one back.
|
|
34
|
+
*/
|
|
35
|
+
install(): () => void;
|
|
36
|
+
/** Read the body once, so a redirect that keeps the method can send it again. */
|
|
37
|
+
private start;
|
|
38
|
+
private request;
|
|
39
|
+
}
|
|
40
|
+
/** A redirect chain longer than any browser follows. */
|
|
41
|
+
export declare class TooManyRedirects extends Error {
|
|
42
|
+
readonly url: string;
|
|
43
|
+
readonly limit: number;
|
|
44
|
+
constructor(url: string, limit: number);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//# sourceMappingURL=browser.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.d.cts","names":[],"sources":["../src/browser.ts"],"mappings":";;iBAyBiB;;;;;;;EAOhB,eAAe;;qBAGH;;WAEH,KAAG;;;;;;;;;;WAWH,cAAc;mBAEN;EAEjB,YAAY,UAAS;;EAWrB,MAAM,cAAc,MAAM,QAAQ;;EAKlC,aAAa,cAAc,MAAM;;;;;EAQjC;;UASc;UAmBA;;;qBAyEF,yBAAyB;WAE3B;WACA;EAFV,YACU,aACA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CookieJar } from "tough-cookie";
|
|
2
|
+
//#region src/browser.d.ts
|
|
3
|
+
export interface BrowserOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The `fetch` requests go out on. Defaults to the global one at the time
|
|
6
|
+
* the browser is created — so a browser made inside a test goes through
|
|
7
|
+
* whatever the test installed (MSW, the context stamp) and never through
|
|
8
|
+
* itself once installed.
|
|
9
|
+
*/
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
export declare class Browser {
|
|
13
|
+
/** The cookies this browser holds. */
|
|
14
|
+
readonly jar: CookieJar;
|
|
15
|
+
/**
|
|
16
|
+
* This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`
|
|
17
|
+
* into the jar on the way back, redirects followed hop by hop so each hop's
|
|
18
|
+
* cookies are kept.
|
|
19
|
+
*
|
|
20
|
+
* On the server side of a test — a handler serving a request — it is the
|
|
21
|
+
* plain `fetch`: a server forwards what it was handed and never reaches into
|
|
22
|
+
* a browser's jar.
|
|
23
|
+
*/
|
|
24
|
+
readonly fetch: typeof fetch;
|
|
25
|
+
private readonly send;
|
|
26
|
+
constructor(options?: BrowserOptions);
|
|
27
|
+
/** Open a URL the way clicking a link does: a GET, redirects followed. */
|
|
28
|
+
visit(url: string | URL): Promise<Response>;
|
|
29
|
+
/** The cookies this browser would send to `url`, as a header value. */
|
|
30
|
+
cookieHeader(url: string | URL): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Make this browser the global `fetch`, for code that uses the global one —
|
|
33
|
+
* an app's module-level clients. Returns what puts the previous one back.
|
|
34
|
+
*/
|
|
35
|
+
install(): () => void;
|
|
36
|
+
/** Read the body once, so a redirect that keeps the method can send it again. */
|
|
37
|
+
private start;
|
|
38
|
+
private request;
|
|
39
|
+
}
|
|
40
|
+
/** A redirect chain longer than any browser follows. */
|
|
41
|
+
export declare class TooManyRedirects extends Error {
|
|
42
|
+
readonly url: string;
|
|
43
|
+
readonly limit: number;
|
|
44
|
+
constructor(url: string, limit: number);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//# sourceMappingURL=browser.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.d.mts","names":[],"sources":["../src/browser.ts"],"mappings":";;iBAyBiB;;;;;;;EAOhB,eAAe;;qBAGH;;WAEH,KAAG;;;;;;;;;;WAWH,cAAc;mBAEN;EAEjB,YAAY,UAAS;;EAWrB,MAAM,cAAc,MAAM,QAAQ;;EAKlC,aAAa,cAAc,MAAM;;;;;EAQjC;;UASc;UAmBA;;;qBAyEF,yBAAyB;WAE3B;WACA;EAFV,YACU,aACA"}
|
package/dist/browser.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { currentTestContext } from "./context.mjs";
|
|
2
|
+
import { CookieJar } from "tough-cookie";
|
|
3
|
+
//#region src/browser.ts
|
|
4
|
+
/**
|
|
5
|
+
* A browser, for tests: a `fetch` that keeps cookies the way a browser does.
|
|
6
|
+
*
|
|
7
|
+
* An app's real clients — its typed API client, better-auth's client — run on
|
|
8
|
+
* whatever `fetch` they are given. Give them this one and a test signs in the
|
|
9
|
+
* way a person does: the auth server's `Set-Cookie` lands in the jar, and every
|
|
10
|
+
* later request to a URL the cookie belongs to carries it. The jar follows the
|
|
11
|
+
* browser's rules (RFC 6265 — Domain, Path, Secure, Expires), so a cookie set
|
|
12
|
+
* for the wrong domain is not sent, exactly as a browser would not send it.
|
|
13
|
+
*
|
|
14
|
+
* Extend it once per app with that app's clients:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* export class Browser extends TestBrowser {
|
|
18
|
+
* readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/** How many redirects `fetch` follows before giving up, as browsers do. */
|
|
23
|
+
const MAX_REDIRECTS = 20;
|
|
24
|
+
var Browser = class {
|
|
25
|
+
/** The cookies this browser holds. */
|
|
26
|
+
jar = new CookieJar();
|
|
27
|
+
/**
|
|
28
|
+
* This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`
|
|
29
|
+
* into the jar on the way back, redirects followed hop by hop so each hop's
|
|
30
|
+
* cookies are kept.
|
|
31
|
+
*
|
|
32
|
+
* On the server side of a test — a handler serving a request — it is the
|
|
33
|
+
* plain `fetch`: a server forwards what it was handed and never reaches into
|
|
34
|
+
* a browser's jar.
|
|
35
|
+
*/
|
|
36
|
+
fetch;
|
|
37
|
+
send;
|
|
38
|
+
constructor(options = {}) {
|
|
39
|
+
this.send = options.fetch ?? globalThis.fetch;
|
|
40
|
+
this.fetch = (input, init) => {
|
|
41
|
+
if (currentTestContext()?.side === "server") return this.send(input, init);
|
|
42
|
+
return this.start(new Request(input, init), init?.redirect);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Open a URL the way clicking a link does: a GET, redirects followed. */
|
|
46
|
+
visit(url) {
|
|
47
|
+
return this.fetch(String(url));
|
|
48
|
+
}
|
|
49
|
+
/** The cookies this browser would send to `url`, as a header value. */
|
|
50
|
+
cookieHeader(url) {
|
|
51
|
+
return this.jar.getCookieString(String(url));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Make this browser the global `fetch`, for code that uses the global one —
|
|
55
|
+
* an app's module-level clients. Returns what puts the previous one back.
|
|
56
|
+
*/
|
|
57
|
+
install() {
|
|
58
|
+
const previous = globalThis.fetch;
|
|
59
|
+
globalThis.fetch = this.fetch;
|
|
60
|
+
return () => {
|
|
61
|
+
globalThis.fetch = previous;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Read the body once, so a redirect that keeps the method can send it again. */
|
|
65
|
+
async start(request, redirect) {
|
|
66
|
+
const body = request.method === "GET" || request.method === "HEAD" ? void 0 : await request.arrayBuffer();
|
|
67
|
+
return this.request({
|
|
68
|
+
url: request.url,
|
|
69
|
+
method: request.method,
|
|
70
|
+
headers: new Headers(request.headers),
|
|
71
|
+
body
|
|
72
|
+
}, redirect);
|
|
73
|
+
}
|
|
74
|
+
async request(request, redirect, hops = 0) {
|
|
75
|
+
const headers = new Headers(request.headers);
|
|
76
|
+
const cookie = await this.jar.getCookieString(request.url);
|
|
77
|
+
if (cookie) {
|
|
78
|
+
const existing = headers.get("cookie");
|
|
79
|
+
headers.set("cookie", existing ? `${existing}; ${cookie}` : cookie);
|
|
80
|
+
}
|
|
81
|
+
const response = await this.send(request.url, {
|
|
82
|
+
method: request.method,
|
|
83
|
+
headers,
|
|
84
|
+
body: request.body,
|
|
85
|
+
redirect: "manual"
|
|
86
|
+
});
|
|
87
|
+
for (const set of response.headers.getSetCookie()) await this.jar.setCookie(set, request.url, { ignoreError: true });
|
|
88
|
+
const location = response.headers.get("location");
|
|
89
|
+
if (redirect === "manual" || !location || response.status < 300 || response.status >= 400) return response;
|
|
90
|
+
if (hops >= MAX_REDIRECTS) throw new TooManyRedirects(request.url, MAX_REDIRECTS);
|
|
91
|
+
const keepsMethod = response.status === 307 || response.status === 308 || request.method === "GET" || request.method === "HEAD";
|
|
92
|
+
const url = new URL(location, request.url).toString();
|
|
93
|
+
const next = keepsMethod ? {
|
|
94
|
+
...request,
|
|
95
|
+
url
|
|
96
|
+
} : {
|
|
97
|
+
url,
|
|
98
|
+
method: "GET",
|
|
99
|
+
headers: withoutBody(request.headers)
|
|
100
|
+
};
|
|
101
|
+
return this.request(next, redirect, hops + 1);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
/** Headers for a request that no longer carries the body they described. */
|
|
105
|
+
function withoutBody(headers) {
|
|
106
|
+
const next = new Headers(headers);
|
|
107
|
+
next.delete("content-type");
|
|
108
|
+
next.delete("content-length");
|
|
109
|
+
return next;
|
|
110
|
+
}
|
|
111
|
+
/** A redirect chain longer than any browser follows. */
|
|
112
|
+
var TooManyRedirects = class extends Error {
|
|
113
|
+
url;
|
|
114
|
+
limit;
|
|
115
|
+
constructor(url, limit) {
|
|
116
|
+
super(`${url} redirected more than ${limit} times. A browser gives up here too — look for a redirect loop.`);
|
|
117
|
+
this.url = url;
|
|
118
|
+
this.limit = limit;
|
|
119
|
+
this.name = "TooManyRedirects";
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
//#endregion
|
|
123
|
+
export { Browser, TooManyRedirects };
|
|
124
|
+
|
|
125
|
+
//# sourceMappingURL=browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.mjs","names":[],"sources":["../src/browser.ts"],"sourcesContent":["/**\n * A browser, for tests: a `fetch` that keeps cookies the way a browser does.\n *\n * An app's real clients — its typed API client, better-auth's client — run on\n * whatever `fetch` they are given. Give them this one and a test signs in the\n * way a person does: the auth server's `Set-Cookie` lands in the jar, and every\n * later request to a URL the cookie belongs to carries it. The jar follows the\n * browser's rules (RFC 6265 — Domain, Path, Secure, Expires), so a cookie set\n * for the wrong domain is not sent, exactly as a browser would not send it.\n *\n * Extend it once per app with that app's clients:\n *\n * ```ts\n * export class Browser extends TestBrowser {\n * readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });\n * }\n * ```\n */\n\nimport { CookieJar } from 'tough-cookie';\nimport { currentTestContext } from './context';\n\n/** How many redirects `fetch` follows before giving up, as browsers do. */\nconst MAX_REDIRECTS = 20;\n\nexport interface BrowserOptions {\n\t/**\n\t * The `fetch` requests go out on. Defaults to the global one at the time\n\t * the browser is created — so a browser made inside a test goes through\n\t * whatever the test installed (MSW, the context stamp) and never through\n\t * itself once installed.\n\t */\n\tfetch?: typeof fetch;\n}\n\nexport class Browser {\n\t/** The cookies this browser holds. */\n\treadonly jar = new CookieJar();\n\n\t/**\n\t * This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`\n\t * into the jar on the way back, redirects followed hop by hop so each hop's\n\t * cookies are kept.\n\t *\n\t * On the server side of a test — a handler serving a request — it is the\n\t * plain `fetch`: a server forwards what it was handed and never reaches into\n\t * a browser's jar.\n\t */\n\treadonly fetch: typeof fetch;\n\n\tprivate readonly send: typeof fetch;\n\n\tconstructor(options: BrowserOptions = {}) {\n\t\tthis.send = options.fetch ?? globalThis.fetch;\n\t\tthis.fetch = (input, init) => {\n\t\t\tif (currentTestContext()?.side === 'server') {\n\t\t\t\treturn this.send(input, init);\n\t\t\t}\n\t\t\treturn this.start(new Request(input, init), init?.redirect);\n\t\t};\n\t}\n\n\t/** Open a URL the way clicking a link does: a GET, redirects followed. */\n\tvisit(url: string | URL): Promise<Response> {\n\t\treturn this.fetch(String(url));\n\t}\n\n\t/** The cookies this browser would send to `url`, as a header value. */\n\tcookieHeader(url: string | URL): Promise<string> {\n\t\treturn this.jar.getCookieString(String(url));\n\t}\n\n\t/**\n\t * Make this browser the global `fetch`, for code that uses the global one —\n\t * an app's module-level clients. Returns what puts the previous one back.\n\t */\n\tinstall(): () => void {\n\t\tconst previous = globalThis.fetch;\n\t\tglobalThis.fetch = this.fetch;\n\t\treturn () => {\n\t\t\tglobalThis.fetch = previous;\n\t\t};\n\t}\n\n\t/** Read the body once, so a redirect that keeps the method can send it again. */\n\tprivate async start(\n\t\trequest: Request,\n\t\tredirect: RequestInit['redirect'],\n\t): Promise<Response> {\n\t\tconst body =\n\t\t\trequest.method === 'GET' || request.method === 'HEAD'\n\t\t\t\t? undefined\n\t\t\t\t: await request.arrayBuffer();\n\t\treturn this.request(\n\t\t\t{\n\t\t\t\turl: request.url,\n\t\t\t\tmethod: request.method,\n\t\t\t\theaders: new Headers(request.headers),\n\t\t\t\tbody,\n\t\t\t},\n\t\t\tredirect,\n\t\t);\n\t}\n\n\tprivate async request(\n\t\trequest: Hop,\n\t\tredirect: RequestInit['redirect'],\n\t\thops = 0,\n\t): Promise<Response> {\n\t\tconst headers = new Headers(request.headers);\n\t\tconst cookie = await this.jar.getCookieString(request.url);\n\t\tif (cookie) {\n\t\t\tconst existing = headers.get('cookie');\n\t\t\theaders.set('cookie', existing ? `${existing}; ${cookie}` : cookie);\n\t\t}\n\n\t\tconst response = await this.send(request.url, {\n\t\t\tmethod: request.method,\n\t\t\theaders,\n\t\t\tbody: request.body,\n\t\t\tredirect: 'manual',\n\t\t});\n\n\t\tfor (const set of response.headers.getSetCookie()) {\n\t\t\t// A cookie a browser would refuse — the wrong domain, Secure over\n\t\t\t// plain HTTP — is dropped silently, as a browser drops it.\n\t\t\tawait this.jar.setCookie(set, request.url, { ignoreError: true });\n\t\t}\n\n\t\tconst location = response.headers.get('location');\n\t\tif (\n\t\t\tredirect === 'manual' ||\n\t\t\t!location ||\n\t\t\tresponse.status < 300 ||\n\t\t\tresponse.status >= 400\n\t\t) {\n\t\t\treturn response;\n\t\t}\n\n\t\tif (hops >= MAX_REDIRECTS) {\n\t\t\tthrow new TooManyRedirects(request.url, MAX_REDIRECTS);\n\t\t}\n\n\t\t// 303, and 301/302 after a POST, continue as a GET without a body — what\n\t\t// every browser does, whatever the spec once said about 302.\n\t\tconst keepsMethod =\n\t\t\tresponse.status === 307 ||\n\t\t\tresponse.status === 308 ||\n\t\t\trequest.method === 'GET' ||\n\t\t\trequest.method === 'HEAD';\n\n\t\tconst url = new URL(location, request.url).toString();\n\t\tconst next: Hop = keepsMethod\n\t\t\t? { ...request, url }\n\t\t\t: { url, method: 'GET', headers: withoutBody(request.headers) };\n\n\t\treturn this.request(next, redirect, hops + 1);\n\t}\n}\n\n/** One request in a redirect chain, with its body already read. */\ninterface Hop {\n\turl: string;\n\tmethod: string;\n\theaders: Headers;\n\tbody?: ArrayBuffer;\n}\n\n/** Headers for a request that no longer carries the body they described. */\nfunction withoutBody(headers: Headers): Headers {\n\tconst next = new Headers(headers);\n\tnext.delete('content-type');\n\tnext.delete('content-length');\n\treturn next;\n}\n\n/** A redirect chain longer than any browser follows. */\nexport class TooManyRedirects extends Error {\n\tconstructor(\n\t\treadonly url: string,\n\t\treadonly limit: number,\n\t) {\n\t\tsuper(\n\t\t\t`${url} redirected more than ${limit} times. A browser gives up here ` +\n\t\t\t\t`too — look for a redirect loop.`,\n\t\t);\n\t\tthis.name = 'TooManyRedirects';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAM,gBAAgB;AAYtB,IAAa,UAAb,MAAqB;;CAEpB,MAAe,IAAI,UAAU;;;;;;;;;;CAW7B;CAEA;CAEA,YAAY,UAA0B,CAAC,GAAG;EACzC,KAAK,OAAO,QAAQ,SAAS,WAAW;EACxC,KAAK,SAAS,OAAO,SAAS;GAC7B,IAAI,mBAAmB,CAAC,EAAE,SAAS,UAClC,OAAO,KAAK,KAAK,OAAO,IAAI;GAE7B,OAAO,KAAK,MAAM,IAAI,QAAQ,OAAO,IAAI,GAAG,MAAM,QAAQ;EAC3D;CACD;;CAGA,MAAM,KAAsC;EAC3C,OAAO,KAAK,MAAM,OA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M,MAAM,IAAI,IAAI,UAAU,QAAQ,GAAG,CAAC,CAAC,SAAS;EACpD,MAAM,OAAY,cACf;GAAE,GAAG;GAAS;EAAI,IAClB;GAAE;GAAK,QAAQ;GAAO,SAAS,YAAY,QAAQ,OAAO;EAAE;EAE/D,OAAO,KAAK,QAAQ,MAAM,UAAU,OAAO,CAAC;CAC7C;AACD;;AAWA,SAAS,YAAY,SAA2B;CAC/C,MAAM,OAAO,IAAI,QAAQ,OAAO;CAChC,KAAK,OAAO,cAAc;CAC1B,KAAK,OAAO,gBAAgB;CAC5B,OAAO;AACR;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,KACA,OACC;EACD,MACC,GAAG,IAAI,wBAAwB,MAAM,gEAEtC;EANS,KAAA,MAAA;EACA,KAAA,QAAA;EAMT,KAAK,OAAO;CACb;AACD"}
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
/**
|
|
5
|
+
* The test a request belongs to.
|
|
6
|
+
*
|
|
7
|
+
* A feature test drives an app the way it runs deployed: a browser calls the
|
|
8
|
+
* API, the API calls the auth server, each over a URL. In a test those URLs are
|
|
9
|
+
* served in-process, and every request made on behalf of one test has to reach
|
|
10
|
+
* *that test's* transactions — its own call to the API, and the API's call to
|
|
11
|
+
* the auth server made while handling it.
|
|
12
|
+
*
|
|
13
|
+
* The id travels two ways. In-process, an `AsyncLocalStorage` carries it
|
|
14
|
+
* through every await a test and its handlers make. On the wire it is the
|
|
15
|
+
* `x-test-context-id` header, which a stamped `fetch` adds to every request made
|
|
16
|
+
* inside a context, so whatever serves the URL can find the context again —
|
|
17
|
+
* without any caller, including code under test, writing the header.
|
|
18
|
+
*/
|
|
19
|
+
/** The header a test context travels in between client and server. */
|
|
20
|
+
const TEST_CONTEXT_HEADER = "x-test-context-id";
|
|
21
|
+
const storage = new node_async_hooks.AsyncLocalStorage();
|
|
22
|
+
/** The context the running code belongs to, if any. */
|
|
23
|
+
function currentTestContext() {
|
|
24
|
+
return storage.getStore();
|
|
25
|
+
}
|
|
26
|
+
/** Run `fn` as the client side of the test `id`. */
|
|
27
|
+
function runInTestContext(id, fn) {
|
|
28
|
+
return storage.run({
|
|
29
|
+
id,
|
|
30
|
+
side: "client"
|
|
31
|
+
}, fn);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Run `fn` as a server handling a request for the test `id` — what whatever
|
|
35
|
+
* serves a URL in-process wraps each request in.
|
|
36
|
+
*/
|
|
37
|
+
function runAsServer(id, fn) {
|
|
38
|
+
return storage.run({
|
|
39
|
+
id,
|
|
40
|
+
side: "server"
|
|
41
|
+
}, fn);
|
|
42
|
+
}
|
|
43
|
+
/** The test a request was made for, read off its header. */
|
|
44
|
+
function testContextOf(request) {
|
|
45
|
+
return request.headers.get("x-test-context-id") ?? void 0;
|
|
46
|
+
}
|
|
47
|
+
/** `fetch`, adding the current test's id to every request made inside one. */
|
|
48
|
+
function stampedFetch(inner) {
|
|
49
|
+
return (input, init) => {
|
|
50
|
+
const context = currentTestContext();
|
|
51
|
+
if (!context) return inner(input, init);
|
|
52
|
+
const request = new Request(input, init);
|
|
53
|
+
if (!request.headers.has("x-test-context-id")) request.headers.set(TEST_CONTEXT_HEADER, context.id);
|
|
54
|
+
return inner(request);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Stamp the global `fetch` with the current test's id.
|
|
59
|
+
*
|
|
60
|
+
* Install it after anything else that patches `fetch` — MSW's interceptor in
|
|
61
|
+
* particular — so the stamp is added before the request is intercepted.
|
|
62
|
+
* Returns what puts the previous `fetch` back.
|
|
63
|
+
*/
|
|
64
|
+
function installContextFetch() {
|
|
65
|
+
const previous = globalThis.fetch;
|
|
66
|
+
globalThis.fetch = stampedFetch(previous);
|
|
67
|
+
return () => {
|
|
68
|
+
globalThis.fetch = previous;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
exports.TEST_CONTEXT_HEADER = TEST_CONTEXT_HEADER;
|
|
73
|
+
exports.currentTestContext = currentTestContext;
|
|
74
|
+
exports.installContextFetch = installContextFetch;
|
|
75
|
+
exports.runAsServer = runAsServer;
|
|
76
|
+
exports.runInTestContext = runInTestContext;
|
|
77
|
+
exports.stampedFetch = stampedFetch;
|
|
78
|
+
exports.testContextOf = testContextOf;
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.cjs","names":["AsyncLocalStorage"],"sources":["../src/context.ts"],"sourcesContent":["/**\n * The test a request belongs to.\n *\n * A feature test drives an app the way it runs deployed: a browser calls the\n * API, the API calls the auth server, each over a URL. In a test those URLs are\n * served in-process, and every request made on behalf of one test has to reach\n * *that test's* transactions — its own call to the API, and the API's call to\n * the auth server made while handling it.\n *\n * The id travels two ways. In-process, an `AsyncLocalStorage` carries it\n * through every await a test and its handlers make. On the wire it is the\n * `x-test-context-id` header, which a stamped `fetch` adds to every request made\n * inside a context, so whatever serves the URL can find the context again —\n * without any caller, including code under test, writing the header.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\n/** The header a test context travels in between client and server. */\nexport const TEST_CONTEXT_HEADER = 'x-test-context-id';\n\n/**\n * Which side of a URL the running code is on.\n *\n * `client` is the test itself and the frontend it renders — what a browser\n * does. `server` is a handler serving a request. The difference matters for\n * cookies: a browser attaches its cookies to what it sends, a server forwards\n * only what it was handed. A server call that picked up the browser's cookies\n * would hide the bug of a server that forgets to forward them.\n */\nexport type TestSide = 'client' | 'server';\n\nexport interface TestContext {\n\treadonly id: string;\n\treadonly side: TestSide;\n}\n\nconst storage = new AsyncLocalStorage<TestContext>();\n\n/** The context the running code belongs to, if any. */\nexport function currentTestContext(): TestContext | undefined {\n\treturn storage.getStore();\n}\n\n/** Run `fn` as the client side of the test `id`. */\nexport function runInTestContext<T>(id: string, fn: () => T): T {\n\treturn storage.run({ id, side: 'client' }, fn);\n}\n\n/**\n * Run `fn` as a server handling a request for the test `id` — what whatever\n * serves a URL in-process wraps each request in.\n */\nexport function runAsServer<T>(id: string, fn: () => T): T {\n\treturn storage.run({ id, side: 'server' }, fn);\n}\n\n/** The test a request was made for, read off its header. */\nexport function testContextOf(request: Request): string | undefined {\n\treturn request.headers.get(TEST_CONTEXT_HEADER) ?? undefined;\n}\n\n/** `fetch`, adding the current test's id to every request made inside one. */\nexport function stampedFetch(inner: typeof fetch): typeof fetch {\n\treturn (input, init) => {\n\t\tconst context = currentTestContext();\n\t\tif (!context) return inner(input, init);\n\n\t\tconst request = new Request(input, init);\n\t\tif (!request.headers.has(TEST_CONTEXT_HEADER)) {\n\t\t\trequest.headers.set(TEST_CONTEXT_HEADER, context.id);\n\t\t}\n\t\treturn inner(request);\n\t};\n}\n\n/**\n * Stamp the global `fetch` with the current test's id.\n *\n * Install it after anything else that patches `fetch` — MSW's interceptor in\n * particular — so the stamp is added before the request is intercepted.\n * Returns what puts the previous `fetch` back.\n */\nexport function installContextFetch(): () => void {\n\tconst previous = globalThis.fetch;\n\tglobalThis.fetch = stampedFetch(previous);\n\treturn () => {\n\t\tglobalThis.fetch = previous;\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,MAAa,sBAAsB;AAkBnC,MAAM,UAAU,IAAIA,iBAAAA,kBAA+B;;AAGnD,SAAgB,qBAA8C;CAC7D,OAAO,QAAQ,SAAS;AACzB;;AAGA,SAAgB,iBAAoB,IAAY,IAAgB;CAC/D,OAAO,QAAQ,IAAI;EAAE;EAAI,MAAM;CAAS,GAAG,EAAE;AAC9C;;;;;AAMA,SAAgB,YAAe,IAAY,IAAgB;CAC1D,OAAO,QAAQ,IAAI;EAAE;EAAI,MAAM;CAAS,GAAG,EAAE;AAC9C;;AAGA,SAAgB,cAAc,SAAsC;CACnE,OAAO,QAAQ,QAAQ,IAAA,mBAAuB,KAAK,KAAA;AACpD;;AAGA,SAAgB,aAAa,OAAmC;CAC/D,QAAQ,OAAO,SAAS;EACvB,MAAM,UAAU,mBAAmB;EACnC,IAAI,CAAC,SAAS,OAAO,MAAM,OAAO,IAAI;EAEtC,MAAM,UAAU,IAAI,QAAQ,OAAO,IAAI;EACvC,IAAI,CAAC,QAAQ,QAAQ,IAAA,mBAAuB,GAC3C,QAAQ,QAAQ,IAAI,qBAAqB,QAAQ,EAAE;EAEpD,OAAO,MAAM,OAAO;CACrB;AACD;;;;;;;;AASA,SAAgB,sBAAkC;CACjD,MAAM,WAAW,WAAW;CAC5B,WAAW,QAAQ,aAAa,QAAQ;CACxC,aAAa;EACZ,WAAW,QAAQ;CACpB;AACD"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/context.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The test a request belongs to.
|
|
4
|
+
*
|
|
5
|
+
* A feature test drives an app the way it runs deployed: a browser calls the
|
|
6
|
+
* API, the API calls the auth server, each over a URL. In a test those URLs are
|
|
7
|
+
* served in-process, and every request made on behalf of one test has to reach
|
|
8
|
+
* *that test's* transactions — its own call to the API, and the API's call to
|
|
9
|
+
* the auth server made while handling it.
|
|
10
|
+
*
|
|
11
|
+
* The id travels two ways. In-process, an `AsyncLocalStorage` carries it
|
|
12
|
+
* through every await a test and its handlers make. On the wire it is the
|
|
13
|
+
* `x-test-context-id` header, which a stamped `fetch` adds to every request made
|
|
14
|
+
* inside a context, so whatever serves the URL can find the context again —
|
|
15
|
+
* without any caller, including code under test, writing the header.
|
|
16
|
+
*/
|
|
17
|
+
/** The header a test context travels in between client and server. */
|
|
18
|
+
export declare const TEST_CONTEXT_HEADER = "x-test-context-id";
|
|
19
|
+
/**
|
|
20
|
+
* Which side of a URL the running code is on.
|
|
21
|
+
*
|
|
22
|
+
* `client` is the test itself and the frontend it renders — what a browser
|
|
23
|
+
* does. `server` is a handler serving a request. The difference matters for
|
|
24
|
+
* cookies: a browser attaches its cookies to what it sends, a server forwards
|
|
25
|
+
* only what it was handed. A server call that picked up the browser's cookies
|
|
26
|
+
* would hide the bug of a server that forgets to forward them.
|
|
27
|
+
*/
|
|
28
|
+
export type TestSide = 'client' | 'server';
|
|
29
|
+
export interface TestContext {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly side: TestSide;
|
|
32
|
+
}
|
|
33
|
+
/** The context the running code belongs to, if any. */
|
|
34
|
+
export declare function currentTestContext(): TestContext | undefined;
|
|
35
|
+
/** Run `fn` as the client side of the test `id`. */
|
|
36
|
+
export declare function runInTestContext<T>(id: string, fn: () => T): T;
|
|
37
|
+
/**
|
|
38
|
+
* Run `fn` as a server handling a request for the test `id` — what whatever
|
|
39
|
+
* serves a URL in-process wraps each request in.
|
|
40
|
+
*/
|
|
41
|
+
export declare function runAsServer<T>(id: string, fn: () => T): T;
|
|
42
|
+
/** The test a request was made for, read off its header. */
|
|
43
|
+
export declare function testContextOf(request: Request): string | undefined;
|
|
44
|
+
/** `fetch`, adding the current test's id to every request made inside one. */
|
|
45
|
+
export declare function stampedFetch(inner: typeof fetch): typeof fetch;
|
|
46
|
+
/**
|
|
47
|
+
* Stamp the global `fetch` with the current test's id.
|
|
48
|
+
*
|
|
49
|
+
* Install it after anything else that patches `fetch` — MSW's interceptor in
|
|
50
|
+
* particular — so the stamp is added before the request is intercepted.
|
|
51
|
+
* Returns what puts the previous `fetch` back.
|
|
52
|
+
*/
|
|
53
|
+
export declare function installContextFetch(): () => void;
|
|
54
|
+
//#endregion
|
|
55
|
+
//# sourceMappingURL=context.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.cts","names":[],"sources":["../src/context.ts"],"mappings":";;;;;;;;;;;;;;;;;qBAmBa;;;;;;;;;;YAWD;iBAEK;WACP;WACA,MAAM;;;wBAMA,sBAAsB;;wBAKtB,iBAAiB,GAAG,YAAY,UAAU,IAAI;;;;;wBAQ9C,YAAY,GAAG,YAAY,UAAU,IAAI;;wBAKzC,cAAc,SAAS;;wBAKvB,aAAa,cAAc,eAAe;;;;;;;;wBAoB1C"}
|