@santi020k/auth-migrations 0.4.0
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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/cli-lib.d.ts +9 -0
- package/dist/cli-lib.d.ts.map +1 -0
- package/dist/cli-lib.js +141 -0
- package/dist/cli-lib.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +7 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +232 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Santiago Molina
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# `@santi020k/auth-migrations`
|
|
2
|
+
|
|
3
|
+
Generates the reviewed SQLite/D1 schema used by the auth packages. Migrations remain application-owned: generate the SQL,
|
|
4
|
+
review it, save it in the consumer repository, and apply it through that repository's migration workflow.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { createAuthD1Migration } from "@santi020k/auth-migrations";
|
|
8
|
+
|
|
9
|
+
const sql = createAuthD1Migration({ tablePrefix: "planner_v2" });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The optional prefix changes every table, foreign-key reference, and index. Use the identical prefix in
|
|
13
|
+
`@santi020k/auth-cloudflare`; changing it after deployment starts a different identity store rather than migrating data.
|
|
14
|
+
|
|
15
|
+
## Schema versioning and upgrade planning
|
|
16
|
+
|
|
17
|
+
`AUTH_SCHEMA_MIGRATIONS` is the ordered, additive history of the schema; `AUTH_SCHEMA_VERSION` is the latest known
|
|
18
|
+
version (`1` today, since only the base schema exists). `planAuthSchemaUpgrade` concatenates the additive SQL between
|
|
19
|
+
two versions without connecting to a database:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { planAuthSchemaUpgrade } from "@santi020k/auth-migrations";
|
|
23
|
+
|
|
24
|
+
const plan = planAuthSchemaUpgrade(0, undefined, { tablePrefix: "planner_v2" });
|
|
25
|
+
// plan.sql: statements introduced by every version after 0 up to the latest
|
|
26
|
+
// plan.steps: the AuthSchemaMigrationStep entries that contributed those statements
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Requesting the same version on both ends (for example, an application already at the latest version) returns an empty
|
|
30
|
+
plan (`steps: []`, `sql: ""`) instead of an error, so a caller can always apply `plan.sql` unconditionally.
|
|
31
|
+
|
|
32
|
+
`checkAuthSchemaCompatibility` compares an already-introspected table list (the caller's own trusted read of
|
|
33
|
+
`sqlite_master` or equivalent) and the consumer's app-owned applied migration version against this package, without
|
|
34
|
+
ever opening a database connection itself:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { checkAuthSchemaCompatibility } from "@santi020k/auth-migrations";
|
|
38
|
+
|
|
39
|
+
const existingTables = (await db.prepare("SELECT name FROM sqlite_master WHERE type = 'table'").all()).results.map(
|
|
40
|
+
(row) => row.name,
|
|
41
|
+
);
|
|
42
|
+
const report = checkAuthSchemaCompatibility(existingTables, {
|
|
43
|
+
appliedVersion: 1, // read from the consumer's migration journal
|
|
44
|
+
tablePrefix: "planner_v2",
|
|
45
|
+
});
|
|
46
|
+
// report.status: "up-to-date" | "upgrade-required" | "not-initialized" |
|
|
47
|
+
// "version-unknown" | "incompatible"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
When `appliedVersion` is behind, the `"upgrade-required"` result includes the additive `plan` to review and copy into
|
|
51
|
+
the consumer's next migration. Omitting `appliedVersion` can validate table presence but deliberately reports
|
|
52
|
+
`"version-unknown"` instead of claiming the schema is current. `unexpectedTables` (present on an `"incompatible"`
|
|
53
|
+
result) is only populated when a table prefix is set; without a prefix there is no safe way to distinguish a leftover
|
|
54
|
+
auth table from the consumer's own application tables.
|
|
55
|
+
|
|
56
|
+
## CLI
|
|
57
|
+
|
|
58
|
+
`auth-migrations` is a safe, non-mutating command line tool: it only prints SQL or a JSON report to stdout (or a file
|
|
59
|
+
with `--out`) and never opens a network or database connection itself.
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
auth-migrations generate --prefix planner_v2 --out migrations/0001_auth.sql
|
|
63
|
+
auth-migrations plan --from 0 --to 1 --prefix planner_v2
|
|
64
|
+
auth-migrations check --tables "$(cat table-names.txt)" --prefix planner_v2 --version 1
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`check` reads table names from stdin (comma- or newline-separated) when `--tables` is omitted, so it can sit at the end
|
|
68
|
+
of a pipeline built from the consumer's own trusted database tooling, for example:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
wrangler d1 execute planner_db --command "SELECT name FROM sqlite_master WHERE type='table'" --json \
|
|
72
|
+
| jq -r '.[0].results[].name' \
|
|
73
|
+
| auth-migrations check --prefix planner_v2 --version 1
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Supply `--version` from the consuming application's migration journal, not from untrusted request data. `check` exits
|
|
77
|
+
`0` only when both the schema shape and version are `"up-to-date"`, so it can gate a deploy step without the CLI ever
|
|
78
|
+
touching the database directly.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface CliIO {
|
|
2
|
+
readStdin(): Promise<string>;
|
|
3
|
+
stderr(text: string): void;
|
|
4
|
+
stdout(text: string): void;
|
|
5
|
+
writeFile(path: string, contents: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function runAuthMigrationsCli(argv: readonly string[], io: CliIO): Promise<number>;
|
|
8
|
+
export declare const nodeCliIO: CliIO;
|
|
9
|
+
//# sourceMappingURL=cli-lib.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli-lib.d.ts","sourceRoot":"","sources":["../src/cli-lib.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,KAAK;IACpB,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAsGD,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,EAAE,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAuB9F;AAED,eAAO,MAAM,SAAS,EAAE,KAiBvB,CAAC"}
|
package/dist/cli-lib.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { AUTH_SCHEMA_VERSION, checkAuthSchemaCompatibility, planAuthSchemaUpgrade, } from "./index.js";
|
|
4
|
+
const USAGE = `Usage: auth-migrations <command> [options]
|
|
5
|
+
|
|
6
|
+
Commands:
|
|
7
|
+
generate [--prefix <prefix>] [--version <n>] [--out <file>]
|
|
8
|
+
Print the schema as of the given version (default: latest, ${String(AUTH_SCHEMA_VERSION)}) as D1 SQL.
|
|
9
|
+
|
|
10
|
+
plan --from <version> [--to <version>] [--prefix <prefix>] [--out <file>]
|
|
11
|
+
Print the additive SQL needed to upgrade between two schema versions.
|
|
12
|
+
|
|
13
|
+
check [--tables <comma-separated-table-names>] [--prefix <prefix>] [--version <n>]
|
|
14
|
+
Compare an already-introspected table list against the expected schema
|
|
15
|
+
and the app-owned applied migration version. Reads table names from stdin
|
|
16
|
+
(comma- or newline-separated) when --tables is omitted. Without --version,
|
|
17
|
+
a complete table set reports "version-unknown" and cannot pass the gate.
|
|
18
|
+
|
|
19
|
+
This command never connects to a database. It only prints SQL and reports; it
|
|
20
|
+
never applies, mutates, or inspects a live database itself.
|
|
21
|
+
`;
|
|
22
|
+
function parseTableList(value) {
|
|
23
|
+
return value
|
|
24
|
+
.split(/[,\n]/u)
|
|
25
|
+
.map((table) => table.trim())
|
|
26
|
+
.filter(Boolean);
|
|
27
|
+
}
|
|
28
|
+
function parseVersion(value, code) {
|
|
29
|
+
if (!/^\d+$/u.test(value))
|
|
30
|
+
throw new Error(code);
|
|
31
|
+
return Number.parseInt(value, 10);
|
|
32
|
+
}
|
|
33
|
+
async function emit(io, sql, out) {
|
|
34
|
+
const text = sql.endsWith("\n") ? sql : `${sql}\n`;
|
|
35
|
+
if (out) {
|
|
36
|
+
await io.writeFile(out, text);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
io.stdout(text);
|
|
40
|
+
}
|
|
41
|
+
async function runGenerate(argv, io) {
|
|
42
|
+
const { values } = parseArgs({
|
|
43
|
+
args: [...argv],
|
|
44
|
+
options: { out: { type: "string" }, prefix: { type: "string" }, version: { type: "string" } },
|
|
45
|
+
strict: true,
|
|
46
|
+
});
|
|
47
|
+
const toVersion = values.version === undefined
|
|
48
|
+
? AUTH_SCHEMA_VERSION
|
|
49
|
+
: parseVersion(values.version, "auth_migrations_cli_version_invalid");
|
|
50
|
+
const plan = planAuthSchemaUpgrade(0, toVersion, { tablePrefix: values.prefix ?? "" });
|
|
51
|
+
await emit(io, plan.sql, values.out);
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
async function runPlan(argv, io) {
|
|
55
|
+
const { values } = parseArgs({
|
|
56
|
+
args: [...argv],
|
|
57
|
+
options: {
|
|
58
|
+
from: { type: "string" },
|
|
59
|
+
out: { type: "string" },
|
|
60
|
+
prefix: { type: "string" },
|
|
61
|
+
to: { type: "string" },
|
|
62
|
+
},
|
|
63
|
+
strict: true,
|
|
64
|
+
});
|
|
65
|
+
if (values.from === undefined)
|
|
66
|
+
throw new Error("auth_migrations_cli_from_required");
|
|
67
|
+
const fromVersion = parseVersion(values.from, "auth_migrations_cli_from_invalid");
|
|
68
|
+
const toVersion = values.to === undefined ? undefined : parseVersion(values.to, "auth_migrations_cli_to_invalid");
|
|
69
|
+
const plan = planAuthSchemaUpgrade(fromVersion, toVersion, { tablePrefix: values.prefix ?? "" });
|
|
70
|
+
if (plan.steps.length === 0) {
|
|
71
|
+
io.stderr(`Already at version ${String(plan.toVersion)}; nothing to apply.\n`);
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
await emit(io, plan.sql, values.out);
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
async function runCheck(argv, io) {
|
|
78
|
+
const { values } = parseArgs({
|
|
79
|
+
args: [...argv],
|
|
80
|
+
options: {
|
|
81
|
+
prefix: { type: "string" },
|
|
82
|
+
tables: { type: "string" },
|
|
83
|
+
version: { type: "string" },
|
|
84
|
+
},
|
|
85
|
+
strict: true,
|
|
86
|
+
});
|
|
87
|
+
const raw = values.tables ?? (await io.readStdin());
|
|
88
|
+
const options = {
|
|
89
|
+
...(values.version === undefined
|
|
90
|
+
? {}
|
|
91
|
+
: { appliedVersion: parseVersion(values.version, "auth_migrations_cli_version_invalid") }),
|
|
92
|
+
tablePrefix: values.prefix ?? "",
|
|
93
|
+
};
|
|
94
|
+
const report = checkAuthSchemaCompatibility(parseTableList(raw), options);
|
|
95
|
+
io.stdout(`${JSON.stringify(report, null, 2)}\n`);
|
|
96
|
+
return report.status === "up-to-date" ? 0 : 1;
|
|
97
|
+
}
|
|
98
|
+
export async function runAuthMigrationsCli(argv, io) {
|
|
99
|
+
const [command, ...rest] = argv;
|
|
100
|
+
try {
|
|
101
|
+
switch (command) {
|
|
102
|
+
case "generate":
|
|
103
|
+
return await runGenerate(rest, io);
|
|
104
|
+
case "plan":
|
|
105
|
+
return await runPlan(rest, io);
|
|
106
|
+
case "check":
|
|
107
|
+
return await runCheck(rest, io);
|
|
108
|
+
case undefined:
|
|
109
|
+
case "--help":
|
|
110
|
+
case "-h":
|
|
111
|
+
io.stdout(USAGE);
|
|
112
|
+
return 0;
|
|
113
|
+
default:
|
|
114
|
+
io.stderr(USAGE);
|
|
115
|
+
return 1;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
io.stderr(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
120
|
+
return 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export const nodeCliIO = {
|
|
124
|
+
readStdin: async () => {
|
|
125
|
+
const chunks = [];
|
|
126
|
+
for await (const chunk of process.stdin) {
|
|
127
|
+
chunks.push(chunk);
|
|
128
|
+
}
|
|
129
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
130
|
+
},
|
|
131
|
+
stderr: (text) => {
|
|
132
|
+
process.stderr.write(text);
|
|
133
|
+
},
|
|
134
|
+
stdout: (text) => {
|
|
135
|
+
process.stdout.write(text);
|
|
136
|
+
},
|
|
137
|
+
writeFile: async (path, contents) => {
|
|
138
|
+
await writeFile(path, contents, "utf8");
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
//# sourceMappingURL=cli-lib.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli-lib.js","sourceRoot":"","sources":["../src/cli-lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EACL,mBAAmB,EAEnB,4BAA4B,EAC5B,qBAAqB,GACtB,MAAM,YAAY,CAAC;AASpB,MAAM,KAAK,GAAG;;;;mEAIqD,MAAM,CAAC,mBAAmB,CAAC;;;;;;;;;;;;;CAa7F,CAAC;AAEF,SAAS,cAAc,CAAC,KAAa;IACnC,OAAO,KAAK;SACT,KAAK,CAAC,QAAQ,CAAC;SACf,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;SAC5B,MAAM,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,YAAY,CAAC,KAAa,EAAE,IAAY;IAC/C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IACjD,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC;AAED,KAAK,UAAU,IAAI,CAAC,EAAS,EAAE,GAAW,EAAE,GAAuB;IACjE,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC;IACnD,IAAI,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,MAAM,CAAC,KAAK,CAAC,CAAC;gBACjB,OAAO,CAAC,CAAC;QACb,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,EAAE,CAAC,MAAM,CAAC,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACzE,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAU;IAC9B,SAAS,EAAE,KAAK,IAAI,EAAE;QACpB,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YACxC,MAAM,CAAC,IAAI,CAAC,KAAe,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;IACD,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;QACf,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;QACf,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;QAClC,MAAM,SAAS,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC1C,CAAC;CACF,CAAC"}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAE/D,KAAK,UAAU,IAAI;IACjB,OAAO,CAAC,QAAQ,GAAG,MAAM,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AAClF,CAAC;AAED,KAAK,IAAI,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export interface AuthTableNames {
|
|
2
|
+
account: string;
|
|
3
|
+
passkey: string;
|
|
4
|
+
rateLimit: string;
|
|
5
|
+
session: string;
|
|
6
|
+
user: string;
|
|
7
|
+
verification: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AuthMigrationOptions {
|
|
10
|
+
tablePrefix?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AuthSchemaCompatibilityOptions extends AuthMigrationOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Latest auth migration version recorded by the consuming application.
|
|
15
|
+
* Consumers own this value and its migration journal; this package never
|
|
16
|
+
* creates or mutates a version table.
|
|
17
|
+
*/
|
|
18
|
+
appliedVersion?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function normalizeAuthTablePrefix(value: string | undefined): string;
|
|
21
|
+
export declare function resolveAuthTableNames(tablePrefix?: string): AuthTableNames;
|
|
22
|
+
export declare function createAuthD1Migration(options?: AuthMigrationOptions): string;
|
|
23
|
+
export declare const AUTH_D1_MIGRATION: string;
|
|
24
|
+
export interface AuthSchemaMigrationStep {
|
|
25
|
+
readonly description: string;
|
|
26
|
+
sql(options?: AuthMigrationOptions): string;
|
|
27
|
+
readonly version: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Ordered, additive schema history. Each entry's `sql` returns only the
|
|
31
|
+
* statements introduced at that version, so upgrade planning can concatenate
|
|
32
|
+
* a contiguous range without replaying already-applied statements. Version 1
|
|
33
|
+
* is the full base schema because no earlier version exists yet.
|
|
34
|
+
*/
|
|
35
|
+
export declare const AUTH_SCHEMA_MIGRATIONS: readonly AuthSchemaMigrationStep[];
|
|
36
|
+
export declare const AUTH_SCHEMA_VERSION: number;
|
|
37
|
+
export interface AuthSchemaUpgradePlan {
|
|
38
|
+
readonly fromVersion: number;
|
|
39
|
+
readonly sql: string;
|
|
40
|
+
readonly steps: readonly AuthSchemaMigrationStep[];
|
|
41
|
+
readonly toVersion: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Plans the additive statements needed to bring a database from
|
|
45
|
+
* `fromVersion` to `toVersion` (defaults to the latest known version). This
|
|
46
|
+
* never inspects or mutates a real database; the caller applies the returned
|
|
47
|
+
* SQL through its own app-owned migration process.
|
|
48
|
+
*/
|
|
49
|
+
export declare function planAuthSchemaUpgrade(fromVersion: number, toVersion?: number, options?: AuthMigrationOptions): AuthSchemaUpgradePlan;
|
|
50
|
+
export interface AuthSchemaIncompatibility {
|
|
51
|
+
status: "incompatible";
|
|
52
|
+
appliedVersion?: number;
|
|
53
|
+
latestVersion: number;
|
|
54
|
+
missingTables: readonly string[];
|
|
55
|
+
reason: "partial-schema" | "schema-newer-than-library" | "version-without-schema" | "version-zero-with-schema";
|
|
56
|
+
tables: AuthTableNames;
|
|
57
|
+
unexpectedTables: readonly string[];
|
|
58
|
+
}
|
|
59
|
+
export interface AuthSchemaNotInitialized {
|
|
60
|
+
status: "not-initialized";
|
|
61
|
+
latestVersion: number;
|
|
62
|
+
tables: AuthTableNames;
|
|
63
|
+
}
|
|
64
|
+
export interface AuthSchemaUpgradeRequired {
|
|
65
|
+
status: "upgrade-required";
|
|
66
|
+
appliedVersion: number;
|
|
67
|
+
latestVersion: number;
|
|
68
|
+
plan: AuthSchemaUpgradePlan;
|
|
69
|
+
tables: AuthTableNames;
|
|
70
|
+
}
|
|
71
|
+
export interface AuthSchemaUpToDate {
|
|
72
|
+
status: "up-to-date";
|
|
73
|
+
appliedVersion: number;
|
|
74
|
+
latestVersion: number;
|
|
75
|
+
tables: AuthTableNames;
|
|
76
|
+
}
|
|
77
|
+
export interface AuthSchemaVersionUnknown {
|
|
78
|
+
status: "version-unknown";
|
|
79
|
+
latestVersion: number;
|
|
80
|
+
tables: AuthTableNames;
|
|
81
|
+
}
|
|
82
|
+
export type AuthSchemaCompatibility = AuthSchemaIncompatibility | AuthSchemaNotInitialized | AuthSchemaUpgradeRequired | AuthSchemaUpToDate | AuthSchemaVersionUnknown;
|
|
83
|
+
/**
|
|
84
|
+
* Compares an already-introspected list of table names (the caller's own,
|
|
85
|
+
* trusted read of `sqlite_master` or equivalent) and optional app-owned
|
|
86
|
+
* migration version against this package. It never connects to a database.
|
|
87
|
+
* A complete table set without `appliedVersion` is deliberately reported as
|
|
88
|
+
* `version-unknown`, because table names cannot prove that later additive
|
|
89
|
+
* column or index migrations were applied.
|
|
90
|
+
*/
|
|
91
|
+
export declare function checkAuthSchemaCompatibility(existingTables: readonly string[], options?: AuthSchemaCompatibilityOptions): AuthSchemaCompatibility;
|
|
92
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,oBAAoB;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,8BAA+B,SAAQ,oBAAoB;IAC1E;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,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E,iBAAiB,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,qBAAqB,CAAC;IAC5B,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,YAAY,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,MAAM,uBAAuB,GAC/B,yBAAyB,GACzB,wBAAwB,GACxB,yBAAyB,GACzB,kBAAkB,GAClB,wBAAwB,CAAC;AA2D7B;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,cAAc,EAAE,SAAS,MAAM,EAAE,EACjC,OAAO,GAAE,8BAAmC,GAC3C,uBAAuB,CAoCzB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
const PREFIX_PATTERN = /^[a-z][a-z0-9_]{0,30}$/u;
|
|
2
|
+
export function normalizeAuthTablePrefix(value) {
|
|
3
|
+
if (value === undefined || value === "")
|
|
4
|
+
return "";
|
|
5
|
+
if (!PREFIX_PATTERN.test(value))
|
|
6
|
+
throw new Error("auth_table_prefix_invalid");
|
|
7
|
+
return `${value}_`;
|
|
8
|
+
}
|
|
9
|
+
export function resolveAuthTableNames(tablePrefix) {
|
|
10
|
+
const prefix = normalizeAuthTablePrefix(tablePrefix);
|
|
11
|
+
return {
|
|
12
|
+
account: `${prefix}account`,
|
|
13
|
+
passkey: `${prefix}passkey`,
|
|
14
|
+
rateLimit: `${prefix}rateLimit`,
|
|
15
|
+
session: `${prefix}session`,
|
|
16
|
+
user: `${prefix}user`,
|
|
17
|
+
verification: `${prefix}verification`,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function quote(identifier) {
|
|
21
|
+
return `"${identifier}"`;
|
|
22
|
+
}
|
|
23
|
+
export function createAuthD1Migration(options = {}) {
|
|
24
|
+
const table = resolveAuthTableNames(options.tablePrefix);
|
|
25
|
+
const user = quote(table.user);
|
|
26
|
+
const session = quote(table.session);
|
|
27
|
+
const account = quote(table.account);
|
|
28
|
+
const verification = quote(table.verification);
|
|
29
|
+
const passkey = quote(table.passkey);
|
|
30
|
+
const rateLimit = quote(table.rateLimit);
|
|
31
|
+
return `CREATE TABLE ${user} (
|
|
32
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
33
|
+
"name" TEXT NOT NULL,
|
|
34
|
+
"email" TEXT NOT NULL UNIQUE,
|
|
35
|
+
"emailVerified" INTEGER NOT NULL DEFAULT 0,
|
|
36
|
+
"image" TEXT,
|
|
37
|
+
"createdAt" INTEGER NOT NULL,
|
|
38
|
+
"updatedAt" INTEGER NOT NULL
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
CREATE TABLE ${session} (
|
|
42
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
43
|
+
"userId" TEXT NOT NULL REFERENCES ${user}("id") ON DELETE CASCADE,
|
|
44
|
+
"token" TEXT NOT NULL UNIQUE,
|
|
45
|
+
"expiresAt" INTEGER NOT NULL,
|
|
46
|
+
"ipAddress" TEXT,
|
|
47
|
+
"userAgent" TEXT,
|
|
48
|
+
"createdAt" INTEGER NOT NULL,
|
|
49
|
+
"updatedAt" INTEGER NOT NULL
|
|
50
|
+
);
|
|
51
|
+
CREATE INDEX "${table.session}_userId_idx" ON ${session} ("userId");
|
|
52
|
+
|
|
53
|
+
CREATE TABLE ${account} (
|
|
54
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
55
|
+
"userId" TEXT NOT NULL REFERENCES ${user}("id") ON DELETE CASCADE,
|
|
56
|
+
"accountId" TEXT NOT NULL,
|
|
57
|
+
"providerId" TEXT NOT NULL,
|
|
58
|
+
"accessToken" TEXT,
|
|
59
|
+
"refreshToken" TEXT,
|
|
60
|
+
"accessTokenExpiresAt" INTEGER,
|
|
61
|
+
"refreshTokenExpiresAt" INTEGER,
|
|
62
|
+
"scope" TEXT,
|
|
63
|
+
"idToken" TEXT,
|
|
64
|
+
"password" TEXT,
|
|
65
|
+
"createdAt" INTEGER NOT NULL,
|
|
66
|
+
"updatedAt" INTEGER NOT NULL
|
|
67
|
+
);
|
|
68
|
+
CREATE INDEX "${table.account}_userId_idx" ON ${account} ("userId");
|
|
69
|
+
|
|
70
|
+
CREATE TABLE ${verification} (
|
|
71
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
72
|
+
"identifier" TEXT NOT NULL,
|
|
73
|
+
"value" TEXT NOT NULL,
|
|
74
|
+
"expiresAt" INTEGER NOT NULL,
|
|
75
|
+
"createdAt" INTEGER NOT NULL,
|
|
76
|
+
"updatedAt" INTEGER NOT NULL
|
|
77
|
+
);
|
|
78
|
+
CREATE INDEX "${table.verification}_identifier_idx" ON ${verification} ("identifier");
|
|
79
|
+
|
|
80
|
+
CREATE TABLE ${passkey} (
|
|
81
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
82
|
+
"name" TEXT,
|
|
83
|
+
"publicKey" TEXT NOT NULL,
|
|
84
|
+
"userId" TEXT NOT NULL REFERENCES ${user}("id") ON DELETE CASCADE,
|
|
85
|
+
"credentialID" TEXT NOT NULL UNIQUE,
|
|
86
|
+
"counter" INTEGER NOT NULL,
|
|
87
|
+
"deviceType" TEXT NOT NULL,
|
|
88
|
+
"backedUp" INTEGER NOT NULL,
|
|
89
|
+
"transports" TEXT,
|
|
90
|
+
"createdAt" INTEGER,
|
|
91
|
+
"aaguid" TEXT
|
|
92
|
+
);
|
|
93
|
+
CREATE INDEX "${table.passkey}_userId_idx" ON ${passkey} ("userId");
|
|
94
|
+
|
|
95
|
+
CREATE TABLE ${rateLimit} (
|
|
96
|
+
"id" TEXT PRIMARY KEY NOT NULL,
|
|
97
|
+
"key" TEXT NOT NULL UNIQUE,
|
|
98
|
+
"count" INTEGER NOT NULL,
|
|
99
|
+
"lastRequest" INTEGER NOT NULL
|
|
100
|
+
);
|
|
101
|
+
`;
|
|
102
|
+
}
|
|
103
|
+
export const AUTH_D1_MIGRATION = createAuthD1Migration();
|
|
104
|
+
/**
|
|
105
|
+
* Ordered, additive schema history. Each entry's `sql` returns only the
|
|
106
|
+
* statements introduced at that version, so upgrade planning can concatenate
|
|
107
|
+
* a contiguous range without replaying already-applied statements. Version 1
|
|
108
|
+
* is the full base schema because no earlier version exists yet.
|
|
109
|
+
*/
|
|
110
|
+
export const AUTH_SCHEMA_MIGRATIONS = [
|
|
111
|
+
{
|
|
112
|
+
description: "Create the base Better Auth schema (user, session, account, verification, passkey, rateLimit).",
|
|
113
|
+
sql: createAuthD1Migration,
|
|
114
|
+
version: 1,
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
export const AUTH_SCHEMA_VERSION = AUTH_SCHEMA_MIGRATIONS.length;
|
|
118
|
+
function requireSchemaVersion(value, code) {
|
|
119
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > AUTH_SCHEMA_VERSION) {
|
|
120
|
+
throw new Error(code);
|
|
121
|
+
}
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Plans the additive statements needed to bring a database from
|
|
126
|
+
* `fromVersion` to `toVersion` (defaults to the latest known version). This
|
|
127
|
+
* never inspects or mutates a real database; the caller applies the returned
|
|
128
|
+
* SQL through its own app-owned migration process.
|
|
129
|
+
*/
|
|
130
|
+
export function planAuthSchemaUpgrade(fromVersion, toVersion = AUTH_SCHEMA_VERSION, options = {}) {
|
|
131
|
+
const from = requireSchemaVersion(fromVersion, "auth_migration_from_version_invalid");
|
|
132
|
+
const to = requireSchemaVersion(toVersion, "auth_migration_to_version_invalid");
|
|
133
|
+
if (from > to)
|
|
134
|
+
throw new Error("auth_migration_version_range_invalid");
|
|
135
|
+
const steps = AUTH_SCHEMA_MIGRATIONS.filter((step) => step.version > from && step.version <= to);
|
|
136
|
+
return {
|
|
137
|
+
fromVersion: from,
|
|
138
|
+
sql: steps.map((step) => step.sql(options)).join("\n"),
|
|
139
|
+
steps,
|
|
140
|
+
toVersion: to,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function requireAppliedVersion(value) {
|
|
144
|
+
if (value === undefined)
|
|
145
|
+
return undefined;
|
|
146
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
147
|
+
throw new Error("auth_migration_applied_version_invalid");
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function checkAbsentSchema(appliedVersion, missingTables, tables, unexpectedTables) {
|
|
151
|
+
if (appliedVersion === undefined || appliedVersion === 0) {
|
|
152
|
+
return { status: "not-initialized", latestVersion: AUTH_SCHEMA_VERSION, tables };
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
status: "incompatible",
|
|
156
|
+
appliedVersion,
|
|
157
|
+
latestVersion: AUTH_SCHEMA_VERSION,
|
|
158
|
+
missingTables,
|
|
159
|
+
reason: "version-without-schema",
|
|
160
|
+
tables,
|
|
161
|
+
unexpectedTables,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function checkCompleteSchema(appliedVersion, options, tables) {
|
|
165
|
+
if (appliedVersion === undefined) {
|
|
166
|
+
return { status: "version-unknown", latestVersion: AUTH_SCHEMA_VERSION, tables };
|
|
167
|
+
}
|
|
168
|
+
if (appliedVersion === 0 || appliedVersion > AUTH_SCHEMA_VERSION) {
|
|
169
|
+
return {
|
|
170
|
+
status: "incompatible",
|
|
171
|
+
appliedVersion,
|
|
172
|
+
latestVersion: AUTH_SCHEMA_VERSION,
|
|
173
|
+
missingTables: [],
|
|
174
|
+
reason: appliedVersion === 0 ? "version-zero-with-schema" : "schema-newer-than-library",
|
|
175
|
+
tables,
|
|
176
|
+
unexpectedTables: [],
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (appliedVersion < AUTH_SCHEMA_VERSION) {
|
|
180
|
+
return {
|
|
181
|
+
status: "upgrade-required",
|
|
182
|
+
appliedVersion,
|
|
183
|
+
latestVersion: AUTH_SCHEMA_VERSION,
|
|
184
|
+
plan: planAuthSchemaUpgrade(appliedVersion, AUTH_SCHEMA_VERSION, options),
|
|
185
|
+
tables,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return { status: "up-to-date", appliedVersion, latestVersion: AUTH_SCHEMA_VERSION, tables };
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Compares an already-introspected list of table names (the caller's own,
|
|
192
|
+
* trusted read of `sqlite_master` or equivalent) and optional app-owned
|
|
193
|
+
* migration version against this package. It never connects to a database.
|
|
194
|
+
* A complete table set without `appliedVersion` is deliberately reported as
|
|
195
|
+
* `version-unknown`, because table names cannot prove that later additive
|
|
196
|
+
* column or index migrations were applied.
|
|
197
|
+
*/
|
|
198
|
+
export function checkAuthSchemaCompatibility(existingTables, options = {}) {
|
|
199
|
+
const appliedVersion = requireAppliedVersion(options.appliedVersion);
|
|
200
|
+
const tables = resolveAuthTableNames(options.tablePrefix);
|
|
201
|
+
const expected = [
|
|
202
|
+
tables.account,
|
|
203
|
+
tables.passkey,
|
|
204
|
+
tables.rateLimit,
|
|
205
|
+
tables.session,
|
|
206
|
+
tables.user,
|
|
207
|
+
tables.verification,
|
|
208
|
+
];
|
|
209
|
+
const expectedSet = new Set(expected);
|
|
210
|
+
const existingSet = new Set(existingTables);
|
|
211
|
+
const missingTables = expected.filter((table) => !existingSet.has(table));
|
|
212
|
+
const prefix = normalizeAuthTablePrefix(options.tablePrefix);
|
|
213
|
+
const unexpectedTables = prefix
|
|
214
|
+
? existingTables.filter((table) => table.startsWith(prefix) && !expectedSet.has(table))
|
|
215
|
+
: [];
|
|
216
|
+
if (missingTables.length === expected.length) {
|
|
217
|
+
return checkAbsentSchema(appliedVersion, missingTables, tables, unexpectedTables);
|
|
218
|
+
}
|
|
219
|
+
if (missingTables.length > 0 || unexpectedTables.length > 0) {
|
|
220
|
+
return {
|
|
221
|
+
status: "incompatible",
|
|
222
|
+
...(appliedVersion === undefined ? {} : { appliedVersion }),
|
|
223
|
+
latestVersion: AUTH_SCHEMA_VERSION,
|
|
224
|
+
missingTables,
|
|
225
|
+
reason: "partial-schema",
|
|
226
|
+
tables,
|
|
227
|
+
unexpectedTables,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return checkCompleteSchema(appliedVersion, options, tables);
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAwBjD,MAAM,UAAU,wBAAwB,CAAC,KAAyB;IAChE,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IACnD,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC9E,OAAO,GAAG,KAAK,GAAG,CAAC;AACrB,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,WAAoB;IACxD,MAAM,MAAM,GAAG,wBAAwB,CAAC,WAAW,CAAC,CAAC;IACrD,OAAO;QACL,OAAO,EAAE,GAAG,MAAM,SAAS;QAC3B,OAAO,EAAE,GAAG,MAAM,SAAS;QAC3B,SAAS,EAAE,GAAG,MAAM,WAAW;QAC/B,OAAO,EAAE,GAAG,MAAM,SAAS;QAC3B,IAAI,EAAE,GAAG,MAAM,MAAM;QACrB,YAAY,EAAE,GAAG,MAAM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gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5D,OAAO;YACL,MAAM,EAAE,cAAc;YACtB,GAAG,CAAC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,CAAC;YAC3D,aAAa,EAAE,mBAAmB;YAClC,aAAa;YACb,MAAM,EAAE,gBAAgB;YACxB,MAAM;YACN,gBAAgB;SACjB,CAAC;IACJ,CAAC;IAED,OAAO,mBAAmB,CAAC,cAAc,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AAC9D,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@santi020k/auth-migrations",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Reviewed D1 migration generation for the Santiago-owned Better Auth schema.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Santiago Molina",
|
|
8
|
+
"url": "https://santi020k.com"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"bin": {
|
|
15
|
+
"auth-migrations": "./dist/cli.js"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
28
|
+
"@types/node": "26.6.2",
|
|
29
|
+
"publint": "0.3.24",
|
|
30
|
+
"typescript": "5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.22.3"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/santi020k/auth.git",
|
|
38
|
+
"directory": "packages/auth-migrations"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://auth.santi020k.com",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/santi020k/auth/issues"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"provenance": false
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.build.json",
|
|
50
|
+
"check:package": "publint . --strict && attw --pack . --profile esm-only",
|
|
51
|
+
"clean": "rm -rf dist dist-test",
|
|
52
|
+
"lint": "eslint src tests --max-warnings=0",
|
|
53
|
+
"test": "tsc -p tsconfig.test.json --outDir dist-test && node --test dist-test/tests/*.test.js",
|
|
54
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
55
|
+
}
|
|
56
|
+
}
|