@prisma/orm-mongo 8.0.0-rc.9-dev.13 → 8.0.1-dev.10
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 +48 -8
- package/dist/components-CWskH7lF-Dkc9ysZz.mjs +18 -0
- package/dist/components-CWskH7lF-Dkc9ysZz.mjs.map +1 -0
- package/dist/config.d.mts +14 -5
- package/dist/config.d.mts.map +1 -1
- package/dist/config.mjs +47 -25
- package/dist/config.mjs.map +1 -1
- package/dist/contract-builder.d.mts +33 -91
- package/dist/contract-builder.d.mts.map +1 -1
- package/dist/contract-builder.mjs +23 -7
- package/dist/contract-builder.mjs.map +1 -1
- package/dist/contract-types.d.mts +3 -0
- package/dist/contract-types.mjs +1 -0
- package/dist/control.d.mts +1 -1
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +3 -9
- package/dist/control.mjs.map +1 -1
- package/dist/init-quick-reference.md +116 -0
- package/dist/init-readme.md +35 -0
- package/dist/init.d.mts +7 -0
- package/dist/init.d.mts.map +1 -0
- package/dist/init.mjs +245 -0
- package/dist/init.mjs.map +1 -0
- package/dist/migration.d.mts +1 -0
- package/dist/migration.mjs +2 -0
- package/dist/module-base-B70DHaBE-k_c3Klu4.mjs +6 -0
- package/dist/module-base-B70DHaBE-k_c3Klu4.mjs.map +1 -0
- package/dist/mongo-static-Bti-jnlq-BGWHFTsS.mjs +74 -0
- package/dist/mongo-static-Bti-jnlq-BGWHFTsS.mjs.map +1 -0
- package/dist/{mongo-static-BdSGXYgQ-DSA-2DiK.d.mts → mongo-static-DabeBtmV-DILuhcbE.d.mts} +18 -5
- package/dist/mongo-static-DabeBtmV-DILuhcbE.d.mts.map +1 -0
- package/dist/runtime.d.mts +50 -44
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +106 -44
- package/dist/runtime.mjs.map +1 -1
- package/dist/static.d.mts +24 -2
- package/dist/static.d.mts.map +1 -0
- package/dist/static.mjs +2 -2
- package/dist/target__codec-ids.d.mts +1 -0
- package/dist/target__codec-ids.mjs +2 -0
- package/package.json +25 -70
- package/skills/prisma-8/SKILL.md +23 -18
- package/skills/prisma-8/references/build-a-target-package.md +117 -0
- package/skills/prisma-8/references/build.md +12 -16
- package/skills/prisma-8/references/contract.md +184 -78
- package/skills/prisma-8/references/debug.md +36 -25
- package/skills/prisma-8/references/feedback.md +8 -8
- package/skills/prisma-8/references/migration-model.md +23 -14
- package/skills/prisma-8/references/migration-review.md +23 -52
- package/skills/prisma-8/references/migrations.md +298 -82
- package/skills/prisma-8/references/queries-mongo.md +53 -17
- package/skills/prisma-8/references/queries-postgres.md +345 -34
- package/skills/prisma-8/references/queries.md +44 -25
- package/skills/prisma-8/references/quickstart.md +25 -22
- package/skills/prisma-8/references/runtime.md +172 -72
- package/skills/prisma-8/references/supabase.md +24 -21
- package/skills/prisma-8/references/upgrade-app.md +57 -22
- package/skills/prisma-8/references/upgrade-extension.md +72 -23
- package/skills/prisma-8/upgrading/app/upgrades/0.11-to-0.12/instructions.md +1 -1
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +639 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/multifile-psl/add-use-prisma-8-directive.mjs +60 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +332 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +489 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +1368 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +108 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +755 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +973 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/scripts/sql-expression-literals-psl/rewrite-sql-strings.mjs +271 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +1862 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +513 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +803 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +586 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/scripts/value-objects-in-storage/value-objects-in-storage.ts +1184 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +480 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/scripts/mongo-index-names/mongo-index-names.ts +317 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +336 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1561 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.9-to-8.0.0-rc.10/instructions.md +13 -0
- package/skills/prisma-8/upgrading/extension/upgrades/0.11-to-0.12/instructions.md +1 -1
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +857 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +504 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +143 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +2168 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +223 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +583 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +1008 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +2186 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +381 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +570 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +464 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +410 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +303 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1593 -0
- package/dist/adapter.d.mts +0 -1
- package/dist/adapter.mjs +0 -2
- package/dist/adapter__codec-ids.d.mts +0 -1
- package/dist/adapter__codec-ids.mjs +0 -2
- package/dist/adapter__codec-types.d.mts +0 -1
- package/dist/adapter__codec-types.mjs +0 -2
- package/dist/adapter__codecs.d.mts +0 -1
- package/dist/adapter__codecs.mjs +0 -2
- package/dist/adapter__control.d.mts +0 -3
- package/dist/adapter__control.mjs +0 -3
- package/dist/adapter__runtime.d.mts +0 -3
- package/dist/adapter__runtime.mjs +0 -3
- package/dist/components.d.mts +0 -1
- package/dist/components.mjs +0 -2
- package/dist/components__authoring.d.mts +0 -1
- package/dist/components__authoring.mjs +0 -2
- package/dist/components__codec.d.mts +0 -1
- package/dist/components__codec.mjs +0 -2
- package/dist/components__components.d.mts +0 -1
- package/dist/components__components.mjs +0 -2
- package/dist/components__control.d.mts +0 -1
- package/dist/components__control.mjs +0 -2
- package/dist/components__emission.d.mts +0 -1
- package/dist/components__emission.mjs +0 -2
- package/dist/components__execution.d.mts +0 -1
- package/dist/components__execution.mjs +0 -2
- package/dist/components__ir.d.mts +0 -1
- package/dist/components__ir.mjs +0 -2
- package/dist/components__psl-ast.d.mts +0 -1
- package/dist/components__psl-ast.mjs +0 -2
- package/dist/components__utils.d.mts +0 -1
- package/dist/components__utils.mjs +0 -2
- package/dist/contract.d.mts +0 -1
- package/dist/contract.mjs +0 -2
- package/dist/contract__apply-specifier-default-control-policy.d.mts +0 -1
- package/dist/contract__apply-specifier-default-control-policy.mjs +0 -2
- package/dist/contract__contract-validation-error.d.mts +0 -1
- package/dist/contract__contract-validation-error.mjs +0 -2
- package/dist/contract__default-namespace.d.mts +0 -1
- package/dist/contract__default-namespace.mjs +0 -2
- package/dist/contract__enum-accessor.d.mts +0 -1
- package/dist/contract__enum-accessor.mjs +0 -2
- package/dist/contract__hashing-utils.d.mts +0 -1
- package/dist/contract__hashing-utils.mjs +0 -2
- package/dist/contract__hashing.d.mts +0 -1
- package/dist/contract__hashing.mjs +0 -2
- package/dist/contract__is-plain-record.d.mts +0 -1
- package/dist/contract__is-plain-record.mjs +0 -2
- package/dist/contract__resolve-domain-model.d.mts +0 -1
- package/dist/contract__resolve-domain-model.mjs +0 -2
- package/dist/contract__validate-domain.d.mts +0 -1
- package/dist/contract__validate-domain.mjs +0 -2
- package/dist/driver.d.mts +0 -1
- package/dist/driver.mjs +0 -2
- package/dist/driver__control.d.mts +0 -3
- package/dist/driver__control.mjs +0 -3
- package/dist/family-contract__canonicalization-hooks.d.mts +0 -1
- package/dist/family-contract__canonicalization-hooks.mjs +0 -2
- package/dist/family-contract__entity-kinds.d.mts +0 -1
- package/dist/family-contract__entity-kinds.mjs +0 -2
- package/dist/family__control-adapter.d.mts +0 -1
- package/dist/family__control-adapter.mjs +0 -2
- package/dist/family__ir.d.mts +0 -1
- package/dist/family__ir.mjs +0 -2
- package/dist/family__migration.d.mts +0 -1
- package/dist/family__migration.mjs +0 -2
- package/dist/family__pack.d.mts +0 -3
- package/dist/family__pack.mjs +0 -3
- package/dist/family__runtime.d.mts +0 -1
- package/dist/family__runtime.mjs +0 -2
- package/dist/family__schema-verify.d.mts +0 -1
- package/dist/family__schema-verify.mjs +0 -2
- package/dist/migration-tools__contract-snapshot-store.d.mts +0 -1
- package/dist/migration-tools__contract-snapshot-store.mjs +0 -2
- package/dist/mongo-static-BdSGXYgQ-DSA-2DiK.d.mts.map +0 -1
- package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs +0 -38
- package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs.map +0 -1
- package/dist/query-ast.d.mts +0 -1
- package/dist/query-ast.mjs +0 -2
- package/dist/query-ast__control.d.mts +0 -1
- package/dist/query-ast__control.mjs +0 -2
- package/dist/query-builder__contract-free.d.mts +0 -1
- package/dist/query-builder__contract-free.mjs +0 -2
- package/dist/target__control.d.mts +0 -1
- package/dist/target__control.mjs +0 -2
- package/dist/target__pack.d.mts +0 -3
- package/dist/target__pack.mjs +0 -3
- package/dist/utils.d.mts +0 -1
- package/dist/utils.mjs +0 -2
- package/dist/utils__abortable.d.mts +0 -1
- package/dist/utils__abortable.mjs +0 -2
- package/dist/utils__array-equal.d.mts +0 -1
- package/dist/utils__array-equal.mjs +0 -2
- package/dist/utils__assertions.d.mts +0 -1
- package/dist/utils__assertions.mjs +0 -2
- package/dist/utils__canonical-stringify.d.mts +0 -1
- package/dist/utils__canonical-stringify.mjs +0 -2
- package/dist/utils__casts.d.mts +0 -1
- package/dist/utils__casts.mjs +0 -2
- package/dist/utils__defined.d.mts +0 -1
- package/dist/utils__defined.mjs +0 -2
- package/dist/utils__hash-content.d.mts +0 -1
- package/dist/utils__hash-content.mjs +0 -2
- package/dist/utils__internal-error.d.mts +0 -1
- package/dist/utils__internal-error.mjs +0 -2
- package/dist/utils__json.d.mts +0 -1
- package/dist/utils__json.mjs +0 -2
- package/dist/utils__promise.d.mts +0 -1
- package/dist/utils__promise.mjs +0 -2
- package/dist/utils__redact-db-url.d.mts +0 -1
- package/dist/utils__redact-db-url.mjs +0 -2
- package/dist/utils__result.d.mts +0 -1
- package/dist/utils__result.mjs +0 -2
- package/dist/utils__simplify-deep.d.mts +0 -1
- package/dist/utils__simplify-deep.mjs +0 -2
- package/dist/utils__structured-error.d.mts +0 -1
- package/dist/utils__structured-error.mjs +0 -2
- package/dist/utils__suppress-idle-connection-errors.d.mts +0 -1
- package/dist/utils__suppress-idle-connection-errors.mjs +0 -2
- package/dist/utils__types.d.mts +0 -1
- package/dist/utils__types.mjs +0 -2
- package/dist/value__mongodb-types.d.mts +0 -1
- package/dist/value__mongodb-types.mjs +0 -2
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites raw SQL written as a quoted string in PSL into a `sql` literal with the same text:
|
|
4
|
+
* the `where:` and `expression:` arguments of `@@index`, `@@fullTextIndex` and `@@check`, and
|
|
5
|
+
* `using =` and `withCheck =` in `policy_*` blocks.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* node scripts/codemods/rewrite-sql-strings.mjs <file-or-glob> [...more]
|
|
9
|
+
*
|
|
10
|
+
* Globs never descend into `node_modules` or `dist`. Files are rewritten in place, and each
|
|
11
|
+
* changed file is printed with its number of rewrites. Running it twice is a no-op.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
15
|
+
import { glob } from 'node:fs/promises';
|
|
16
|
+
import { argv, exit, stderr, stdout } from 'node:process';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
18
|
+
|
|
19
|
+
const ATTRIBUTE_START = /@@(index|fullTextIndex|check)\s*\(/g;
|
|
20
|
+
const ATTRIBUTE_ARGUMENT = /^(where|expression)\s*:\s*$/;
|
|
21
|
+
const POLICY_START = /^[ \t]*policy_[A-Za-z]+\s+[A-Za-z_][A-Za-z0-9_]*\s*\{/gm;
|
|
22
|
+
const POLICY_ENTRY = /(^|\n)([ \t]*(using|withCheck)\s*=\s*)$/;
|
|
23
|
+
const TAG = 'sql';
|
|
24
|
+
|
|
25
|
+
/** The end (exclusive) of the quoted string that starts at `start`, or -1 when it is not closed. */
|
|
26
|
+
function stringEnd(source, start) {
|
|
27
|
+
const quote = source[start];
|
|
28
|
+
for (let i = start + 1; i < source.length; i += 1) {
|
|
29
|
+
if (source[i] === '\\') {
|
|
30
|
+
i += 1;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (source[i] === quote) return i + 1;
|
|
34
|
+
if (source[i] === '\n') return -1;
|
|
35
|
+
}
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The index of the line break that ends the `//` comment starting at `start`, or the source length. */
|
|
40
|
+
function commentEnd(source, start) {
|
|
41
|
+
const newline = source.indexOf('\n', start);
|
|
42
|
+
return newline === -1 ? source.length : newline;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The `[start, end)` ranges of `//` and `///` comments, quoted strings and backtick literals: text in which nothing declares an attribute or a policy. */
|
|
46
|
+
function inertRanges(source) {
|
|
47
|
+
const ranges = [];
|
|
48
|
+
for (let i = 0; i < source.length; i += 1) {
|
|
49
|
+
const ch = source[i];
|
|
50
|
+
if (ch === '"' || ch === "'") {
|
|
51
|
+
const end = stringEnd(source, i);
|
|
52
|
+
if (end !== -1) ranges.push([i, end]);
|
|
53
|
+
i = end === -1 ? commentEnd(source, i) : end - 1;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (ch === '`') {
|
|
57
|
+
const end = source.indexOf('`', i + 1);
|
|
58
|
+
if (end === -1) continue;
|
|
59
|
+
ranges.push([i, end + 1]);
|
|
60
|
+
i = end;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === '/' && source[i + 1] === '/') {
|
|
64
|
+
const end = commentEnd(source, i);
|
|
65
|
+
ranges.push([i, end]);
|
|
66
|
+
i = end;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return ranges;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The end (exclusive) of the bracketed region opening at `open`, skipping strings and tagged literals. */
|
|
73
|
+
function closingEnd(source, open, openChar, closeChar) {
|
|
74
|
+
let depth = 0;
|
|
75
|
+
for (let i = open; i < source.length; i += 1) {
|
|
76
|
+
const ch = source[i];
|
|
77
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
78
|
+
const end = ch === '`' ? source.indexOf('`', i + 1) + 1 : stringEnd(source, i);
|
|
79
|
+
if (end <= 0) return -1;
|
|
80
|
+
i = end - 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (ch === '/' && source[i + 1] === '/') {
|
|
84
|
+
const newline = source.indexOf('\n', i);
|
|
85
|
+
if (newline === -1) return -1;
|
|
86
|
+
i = newline;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (ch === openChar) depth += 1;
|
|
90
|
+
if (ch === closeChar) {
|
|
91
|
+
depth -= 1;
|
|
92
|
+
if (depth === 0) return i + 1;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function hexAt(raw, start, length) {
|
|
99
|
+
const digits = raw.slice(start, start + length);
|
|
100
|
+
return digits.length === length && /^[0-9A-Fa-f]+$/.test(digits)
|
|
101
|
+
? String.fromCharCode(Number.parseInt(digits, 16))
|
|
102
|
+
: undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const SIMPLE_ESCAPES = {
|
|
106
|
+
'/': '/',
|
|
107
|
+
b: '\b',
|
|
108
|
+
f: '\f',
|
|
109
|
+
n: '\n',
|
|
110
|
+
r: '\r',
|
|
111
|
+
t: '\t',
|
|
112
|
+
'"': '"',
|
|
113
|
+
"'": "'",
|
|
114
|
+
'\\': '\\',
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** The text of a PSL string literal body, decoded as the PSL parser decodes it. */
|
|
118
|
+
export function decodePslString(raw) {
|
|
119
|
+
let out = '';
|
|
120
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
121
|
+
const ch = raw[i];
|
|
122
|
+
if (ch !== '\\' || i + 1 >= raw.length) {
|
|
123
|
+
out += ch;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const next = raw[i + 1];
|
|
127
|
+
if (Object.hasOwn(SIMPLE_ESCAPES, next)) {
|
|
128
|
+
out += SIMPLE_ESCAPES[next];
|
|
129
|
+
i += 1;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const length = next === 'x' ? 2 : next === 'u' ? 4 : 0;
|
|
133
|
+
const decoded = length === 0 ? undefined : hexAt(raw, i + 2, length);
|
|
134
|
+
if (decoded === undefined) {
|
|
135
|
+
out += `\\${next}`;
|
|
136
|
+
i += 1;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
out += decoded;
|
|
140
|
+
i += 1 + length;
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A `sql` literal holding `text`: the backtick form, or the double-quote form when the text holds a backtick. A backtick body doubles a backslash only before another backslash or at its end, where it would be read as an escape. */
|
|
146
|
+
export function printSqlLiteral(text) {
|
|
147
|
+
if (text.includes('`')) {
|
|
148
|
+
const escaped = text
|
|
149
|
+
.replace(/\\/g, '\\\\')
|
|
150
|
+
.replace(/"/g, '\\"')
|
|
151
|
+
.replace(/\n/g, '\\n')
|
|
152
|
+
.replace(/\r/g, '\\r');
|
|
153
|
+
return `${TAG}"${escaped}"`;
|
|
154
|
+
}
|
|
155
|
+
const fenced = text.replace(/\\(?=\\|$)/g, '\\\\');
|
|
156
|
+
return text.includes('\n') ? `${TAG}\`\n${fenced}\n\`` : `${TAG}\`${fenced}\``;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The quoted strings to rewrite, as `[start, end)` ranges of `source`. */
|
|
160
|
+
function stringRanges(source) {
|
|
161
|
+
const inert = inertRanges(source);
|
|
162
|
+
const isInert = (index) => inert.some(([start, end]) => index >= start && index < end);
|
|
163
|
+
const ranges = [];
|
|
164
|
+
for (const match of source.matchAll(ATTRIBUTE_START)) {
|
|
165
|
+
if (isInert(match.index)) continue;
|
|
166
|
+
const open = match.index + match[0].length - 1;
|
|
167
|
+
const close = closingEnd(source, open, '(', ')');
|
|
168
|
+
if (close === -1) continue;
|
|
169
|
+
let depth = 0;
|
|
170
|
+
let argumentStart = open + 1;
|
|
171
|
+
for (let i = open + 1; i < close - 1; i += 1) {
|
|
172
|
+
const ch = source[i];
|
|
173
|
+
if (ch === '/' && source[i + 1] === '/') {
|
|
174
|
+
i = commentEnd(source, i);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (ch === '"' || ch === "'") {
|
|
178
|
+
const end = stringEnd(source, i);
|
|
179
|
+
if (end === -1) break;
|
|
180
|
+
const before = source.slice(argumentStart, i).trim();
|
|
181
|
+
if (depth === 0 && ATTRIBUTE_ARGUMENT.test(before)) ranges.push([i, end]);
|
|
182
|
+
i = end - 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (ch === '`') {
|
|
186
|
+
i = source.indexOf('`', i + 1);
|
|
187
|
+
if (i === -1) break;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (ch === '(' || ch === '[' || ch === '{') depth += 1;
|
|
191
|
+
if (ch === ')' || ch === ']' || ch === '}') depth -= 1;
|
|
192
|
+
if (ch === ',' && depth === 0) argumentStart = i + 1;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const match of source.matchAll(POLICY_START)) {
|
|
196
|
+
if (isInert(match.index + match[0].search(/\S/))) continue;
|
|
197
|
+
const open = match.index + match[0].length - 1;
|
|
198
|
+
const close = closingEnd(source, open, '{', '}');
|
|
199
|
+
if (close === -1) continue;
|
|
200
|
+
for (let i = open + 1; i < close - 1; i += 1) {
|
|
201
|
+
const ch = source[i];
|
|
202
|
+
if (ch === '/' && source[i + 1] === '/') {
|
|
203
|
+
i = commentEnd(source, i);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (ch === '"' || ch === "'") {
|
|
207
|
+
const end = stringEnd(source, i);
|
|
208
|
+
if (end === -1) break;
|
|
209
|
+
if (POLICY_ENTRY.test(source.slice(open + 1, i))) ranges.push([i, end]);
|
|
210
|
+
i = end - 1;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (ch === '`') {
|
|
214
|
+
i = source.indexOf('`', i + 1);
|
|
215
|
+
if (i === -1) break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return ranges.sort((a, b) => a[0] - b[0]);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Rewrites `source` and returns it with the number of strings rewritten. */
|
|
223
|
+
export function rewriteSqlStringsWithCount(source) {
|
|
224
|
+
const ranges = stringRanges(source);
|
|
225
|
+
let out = '';
|
|
226
|
+
let position = 0;
|
|
227
|
+
for (const [start, end] of ranges) {
|
|
228
|
+
out += source.slice(position, start);
|
|
229
|
+
out += printSqlLiteral(decodePslString(source.slice(start + 1, end - 1)));
|
|
230
|
+
position = end;
|
|
231
|
+
}
|
|
232
|
+
out += source.slice(position);
|
|
233
|
+
return { source: out, count: ranges.length };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function rewriteSqlStrings(source) {
|
|
237
|
+
return rewriteSqlStringsWithCount(source).source;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const SKIPPED_DIRECTORIES = /(^|\/)(node_modules|dist)\//;
|
|
241
|
+
|
|
242
|
+
async function filesMatching(patterns) {
|
|
243
|
+
const files = new Set();
|
|
244
|
+
for (const pattern of patterns) {
|
|
245
|
+
for await (const match of glob(pattern, {
|
|
246
|
+
exclude: (path) => /(^|\/)(node_modules|dist)$/.test(path),
|
|
247
|
+
})) {
|
|
248
|
+
if (!SKIPPED_DIRECTORIES.test(match)) files.add(match);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return [...files].sort();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function main() {
|
|
255
|
+
const patterns = argv.slice(2);
|
|
256
|
+
if (patterns.length === 0) {
|
|
257
|
+
stderr.write('usage: node rewrite-sql-strings.mjs <file-or-glob> [...more]\n');
|
|
258
|
+
exit(2);
|
|
259
|
+
}
|
|
260
|
+
for (const file of await filesMatching(patterns)) {
|
|
261
|
+
const before = readFileSync(file, 'utf8');
|
|
262
|
+
const { source, count } = rewriteSqlStringsWithCount(before);
|
|
263
|
+
if (count === 0) continue;
|
|
264
|
+
writeFileSync(file, source);
|
|
265
|
+
stdout.write(`${file}: ${count} rewritten\n`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (argv[1] && fileURLToPath(import.meta.url) === argv[1]) {
|
|
270
|
+
await main();
|
|
271
|
+
}
|