@agent-native/core 0.44.0 → 0.44.1
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/dist/cli/pr-visual-recap-workflow.d.ts +1 -1
- package/dist/cli/pr-visual-recap-workflow.d.ts.map +1 -1
- package/dist/cli/pr-visual-recap-workflow.js +1 -1
- package/dist/cli/pr-visual-recap-workflow.js.map +1 -1
- package/dist/cli/recap.d.ts +23 -0
- package/dist/cli/recap.d.ts.map +1 -1
- package/dist/cli/recap.js +175 -0
- package/dist/cli/recap.js.map +1 -1
- package/dist/cli/skills.d.ts +3 -3
- package/dist/cli/skills.d.ts.map +1 -1
- package/dist/cli/skills.js +54 -7
- package/dist/cli/skills.js.map +1 -1
- package/dist/client/blocks/library/AnnotatedCodeBlock.d.ts.map +1 -1
- package/dist/client/blocks/library/AnnotatedCodeBlock.js +21 -8
- package/dist/client/blocks/library/AnnotatedCodeBlock.js.map +1 -1
- package/dist/client/blocks/library/ApiEndpointBlock.d.ts.map +1 -1
- package/dist/client/blocks/library/ApiEndpointBlock.js +112 -12
- package/dist/client/blocks/library/ApiEndpointBlock.js.map +1 -1
- package/dist/client/blocks/library/DiffBlock.d.ts.map +1 -1
- package/dist/client/blocks/library/DiffBlock.js +59 -75
- package/dist/client/blocks/library/DiffBlock.js.map +1 -1
- package/dist/client/blocks/library/JsonExplorerBlock.d.ts.map +1 -1
- package/dist/client/blocks/library/JsonExplorerBlock.js +1 -1
- package/dist/client/blocks/library/JsonExplorerBlock.js.map +1 -1
- package/dist/client/blocks/library/MermaidBlock.d.ts.map +1 -1
- package/dist/client/blocks/library/MermaidBlock.js +22 -3
- package/dist/client/blocks/library/MermaidBlock.js.map +1 -1
- package/dist/client/blocks/library/annotation-rail.d.ts +85 -0
- package/dist/client/blocks/library/annotation-rail.d.ts.map +1 -1
- package/dist/client/blocks/library/annotation-rail.js +149 -8
- package/dist/client/blocks/library/annotation-rail.js.map +1 -1
- package/dist/client/blocks/library/diagram.d.ts +17 -0
- package/dist/client/blocks/library/diagram.d.ts.map +1 -1
- package/dist/client/blocks/library/diagram.js +47 -2
- package/dist/client/blocks/library/diagram.js.map +1 -1
- package/dist/client/composer/TiptapComposer.d.ts.map +1 -1
- package/dist/client/composer/TiptapComposer.js +13 -8
- package/dist/client/composer/TiptapComposer.js.map +1 -1
- package/dist/client/composer/pasted-text.d.ts +25 -0
- package/dist/client/composer/pasted-text.d.ts.map +1 -1
- package/dist/client/composer/pasted-text.js +86 -4
- package/dist/client/composer/pasted-text.js.map +1 -1
- package/dist/db/migrations.d.ts +10 -0
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +32 -0
- package/dist/db/migrations.js.map +1 -1
- package/dist/server/og-fonts-data.d.ts +3 -0
- package/dist/server/og-fonts-data.d.ts.map +1 -0
- package/dist/server/og-fonts-data.js +9 -0
- package/dist/server/og-fonts-data.js.map +1 -0
- package/dist/server/og-fonts.d.ts +10 -0
- package/dist/server/og-fonts.d.ts.map +1 -0
- package/dist/server/og-fonts.js +58 -0
- package/dist/server/og-fonts.js.map +1 -0
- package/dist/server/social-og-image.d.ts.map +1 -1
- package/dist/server/social-og-image.js +16 -5
- package/dist/server/social-og-image.js.map +1 -1
- package/dist/styles/blocks.css +111 -0
- package/dist/usage/store.d.ts +12 -0
- package/dist/usage/store.d.ts.map +1 -1
- package/dist/usage/store.js +35 -5
- package/dist/usage/store.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,29 @@
|
|
|
1
|
+
/** The clipboard flavors we care about for a paste. */
|
|
2
|
+
export interface ClipboardPaste {
|
|
3
|
+
/** `text/plain` flavor — used for size heuristics and as the default body. */
|
|
4
|
+
text: string;
|
|
5
|
+
/** `text/html` flavor when the source provided one. */
|
|
6
|
+
html?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Read the relevant clipboard flavors from a paste/drop DataTransfer. */
|
|
9
|
+
export declare function readClipboardPaste(data: {
|
|
10
|
+
getData(type: string): string;
|
|
11
|
+
} | null | undefined): ClipboardPaste;
|
|
1
12
|
export declare function shouldConvertPasteToAttachment(text: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a clipboard paste is large enough to become a `Pasted text`
|
|
15
|
+
* attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the
|
|
16
|
+
* representation we'd actually store, so an HTML-only paste (empty text/plain)
|
|
17
|
+
* still converts on the strength of its markup.
|
|
18
|
+
*/
|
|
19
|
+
export declare function shouldConvertClipboardToAttachment(paste: ClipboardPaste): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Build the attachment File for a page-sized paste, preserving HTML markup when
|
|
22
|
+
* the pasted content is an HTML document so it travels the same rail as an
|
|
23
|
+
* uploaded .html file.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createPastedAttachmentFile(paste: ClipboardPaste): File;
|
|
26
|
+
/** Back-compat helper for callers that only have plain text. */
|
|
2
27
|
export declare function createPastedTextFile(text: string): File;
|
|
3
28
|
export declare function isPastedTextAttachmentName(name: string | undefined): boolean;
|
|
4
29
|
export declare function unwrapAttachmentEnvelope(text: string): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pasted-text.d.ts","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pasted-text.d.ts","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AA4CA,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC7B,8EAA8E;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,0EAA0E;AAC1E,wBAAgB,kBAAkB,CAChC,IAAI,EAAE;IAAE,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GACzD,cAAc,CAIhB;AAmCD,wBAAgB,8BAA8B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAWpE;AAED;;;;;GAKG;AACH,wBAAgB,kCAAkC,CAChD,KAAK,EAAE,cAAc,GACpB,OAAO,CAET;AAQD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAGtE;AAED,gEAAgE;AAChE,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEvD;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAE5E;AAKD,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG7D;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO/C"}
|
|
@@ -4,6 +4,67 @@
|
|
|
4
4
|
const PASTED_TEXT_MIN_CHARS = 3200;
|
|
5
5
|
const PASTED_TEXT_MIN_LINES = 24;
|
|
6
6
|
const PASTED_TEXT_FILENAME_PREFIX = "pasted-text-";
|
|
7
|
+
// A copied HTML document/source is recognizable from its markup: a closing tag
|
|
8
|
+
// (`</div>`), a doctype, or a common structural/element tag. We key off the
|
|
9
|
+
// *content* the user actually pasted, not the clipboard's `text/html` flavor —
|
|
10
|
+
// editors (VS Code) and rich-text apps (Google Docs) populate `text/html` with
|
|
11
|
+
// syntax-highlight spans or formatting wrappers even when the real content is
|
|
12
|
+
// plain code/prose, so trusting `text/html` blindly would mangle those pastes.
|
|
13
|
+
const HTML_SOURCE_SIGNAL = /<!doctype\s+html|<html[\s>]|<\/[a-z][a-z0-9-]*\s*>|<(?:body|head|div|span|section|main|header|footer|nav|article|aside|ul|ol|li|table|thead|tbody|tr|td|th|h[1-6]|p|a|img|button|input|textarea|select|form|label|script|style|link|meta|svg|canvas|template)\b/i;
|
|
14
|
+
// A real HTML *document* announces itself with a doctype / html / head / body.
|
|
15
|
+
// When one of these is present we keep the HTML classification even if an inline
|
|
16
|
+
// <script> contains JS keywords — it's a genuine page.
|
|
17
|
+
const HTML_DOCUMENT_SIGNAL = /<!doctype\s+html|<html[\s>]|<head[\s>]|<body[\s>]/i;
|
|
18
|
+
// JSX/TSX source contains the same `</div>`/`<span>` tags as an HTML document,
|
|
19
|
+
// so the HTML tag signal alone misfiles a pasted React/TS component (even a bare
|
|
20
|
+
// function component) as an `.html` artifact — the agent then mishandles it as a
|
|
21
|
+
// hostable document instead of source. These markers appear in JS/TS/JSX source
|
|
22
|
+
// but not in a plain HTML *fragment*: `className=` (JSX uses it; HTML uses
|
|
23
|
+
// `class=`), ES module import/export, arrow functions, TS type/React annotations,
|
|
24
|
+
// React hooks, and the basic JS declaration/return keywords that make up a
|
|
25
|
+
// component body.
|
|
26
|
+
const CODE_SOURCE_SIGNAL = /\bclassName=|\bimport\b|\bexport\b|=>|:\s*React\.|\buse[A-Z]\w*\(|\b(?:function|const|let|var|return|class|interface|type|enum)\b/;
|
|
27
|
+
function looksLikeHtml(value) {
|
|
28
|
+
if (!value || !HTML_SOURCE_SIGNAL.test(value))
|
|
29
|
+
return false;
|
|
30
|
+
// A self-announcing HTML document stays HTML even if it embeds a <script>.
|
|
31
|
+
if (HTML_DOCUMENT_SIGNAL.test(value))
|
|
32
|
+
return true;
|
|
33
|
+
// Otherwise it's a bare fragment — if it carries JS/TS/JSX code signals,
|
|
34
|
+
// treat it as source code, not an HTML attachment.
|
|
35
|
+
if (CODE_SOURCE_SIGNAL.test(value))
|
|
36
|
+
return false;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
/** Read the relevant clipboard flavors from a paste/drop DataTransfer. */
|
|
40
|
+
export function readClipboardPaste(data) {
|
|
41
|
+
const text = data?.getData("text/plain") ?? "";
|
|
42
|
+
const html = data?.getData("text/html") ?? "";
|
|
43
|
+
return { text, html: html.trim() ? html : undefined };
|
|
44
|
+
}
|
|
45
|
+
// Decide what to actually store for a paste. Preserving HTML markup means a
|
|
46
|
+
// pasted HTML document behaves exactly like uploading that .html file: the agent
|
|
47
|
+
// recognizes it as a hostable artifact and reads it verbatim via
|
|
48
|
+
// `contentFromAttachment` instead of retyping it inline (which cuts off
|
|
49
|
+
// mid-stream on large files and triggers a continuation loop / "spin").
|
|
50
|
+
function selectPasteBody(paste) {
|
|
51
|
+
const plain = paste.text ?? "";
|
|
52
|
+
const html = paste.html ?? "";
|
|
53
|
+
// 1) The pasted text is itself HTML source (copied from an editor, a file, or
|
|
54
|
+
// view-source). The plain text *is* the markup, so keep it as .html.
|
|
55
|
+
if (plain.trim() && looksLikeHtml(plain)) {
|
|
56
|
+
return { body: plain, ext: "html", type: "text/html" };
|
|
57
|
+
}
|
|
58
|
+
// 2) No usable plain text, but a real HTML flavor exists (some apps only
|
|
59
|
+
// expose text/html). Fall back to the markup so nothing is dropped.
|
|
60
|
+
if (!plain.trim() && looksLikeHtml(html)) {
|
|
61
|
+
return { body: html, ext: "html", type: "text/html" };
|
|
62
|
+
}
|
|
63
|
+
// 3) Default: keep the clean plain text. Avoids the syntax-highlight /
|
|
64
|
+
// rich-text noise that lives in text/html when the plain text is already
|
|
65
|
+
// the real content (code from an editor, prose from a doc).
|
|
66
|
+
return { body: plain, ext: "txt", type: "text/plain" };
|
|
67
|
+
}
|
|
7
68
|
export function shouldConvertPasteToAttachment(text) {
|
|
8
69
|
if (!text)
|
|
9
70
|
return false;
|
|
@@ -19,11 +80,32 @@ export function shouldConvertPasteToAttachment(text) {
|
|
|
19
80
|
}
|
|
20
81
|
return false;
|
|
21
82
|
}
|
|
22
|
-
|
|
23
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Whether a clipboard paste is large enough to become a `Pasted text`
|
|
85
|
+
* attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the
|
|
86
|
+
* representation we'd actually store, so an HTML-only paste (empty text/plain)
|
|
87
|
+
* still converts on the strength of its markup.
|
|
88
|
+
*/
|
|
89
|
+
export function shouldConvertClipboardToAttachment(paste) {
|
|
90
|
+
return shouldConvertPasteToAttachment(selectPasteBody(paste).body);
|
|
91
|
+
}
|
|
92
|
+
function pastedAttachmentName(ext) {
|
|
93
|
+
return `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()
|
|
24
94
|
.toString(36)
|
|
25
|
-
.slice(2, 8)}
|
|
26
|
-
|
|
95
|
+
.slice(2, 8)}.${ext}`;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Build the attachment File for a page-sized paste, preserving HTML markup when
|
|
99
|
+
* the pasted content is an HTML document so it travels the same rail as an
|
|
100
|
+
* uploaded .html file.
|
|
101
|
+
*/
|
|
102
|
+
export function createPastedAttachmentFile(paste) {
|
|
103
|
+
const { body, ext, type } = selectPasteBody(paste);
|
|
104
|
+
return new File([body], pastedAttachmentName(ext), { type });
|
|
105
|
+
}
|
|
106
|
+
/** Back-compat helper for callers that only have plain text. */
|
|
107
|
+
export function createPastedTextFile(text) {
|
|
108
|
+
return createPastedAttachmentFile({ text });
|
|
27
109
|
}
|
|
28
110
|
export function isPastedTextAttachmentName(name) {
|
|
29
111
|
return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pasted-text.js","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,0EAA0E;AAC1E,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,MAAM,2BAA2B,GAAG,cAAc,CAAC;AAEnD,MAAM,UAAU,8BAA8B,CAAC,IAAY;IACzD,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IACxB,IAAI,IAAI,CAAC,MAAM,IAAI,qBAAqB;QAAE,OAAO,IAAI,CAAC;IACtD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC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,EAAE;YAAE,KAAK,EAAE,CAAC;IACzC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// Page-sized pastes turn into a `Pasted text` attachment chip instead of being\n// dumped into the editor. Short paragraphs and everyday lists should stay\n// inline so the composer still feels like a normal text field.\nconst PASTED_TEXT_MIN_CHARS = 3200;\nconst PASTED_TEXT_MIN_LINES = 24;\n\nconst PASTED_TEXT_FILENAME_PREFIX = \"pasted-text-\";\n\nexport function shouldConvertPasteToAttachment(text: string): boolean {\n if (!text) return false;\n if (text.length >= PASTED_TEXT_MIN_CHARS) return true;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) {\n lines++;\n if (lines >= PASTED_TEXT_MIN_LINES) return true;\n }\n }\n return false;\n}\n\nexport function createPastedTextFile(text: string): File {\n const name = `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()\n .toString(36)\n .slice(2, 8)}.txt`;\n return new File([text], name, { type: \"text/plain\" });\n}\n\nexport function isPastedTextAttachmentName(name: string | undefined): boolean {\n return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);\n}\n\n// Strips the `<attachment name=...>\\n` / `\\n</attachment>` envelope that\n// SimpleTextAttachmentAdapter wraps the file body in when sending. Returns the\n// raw body for previewing.\nexport function unwrapAttachmentEnvelope(text: string): string {\n const match = text.match(/^<attachment\\b[^>]*>\\n([\\s\\S]*)\\n<\\/attachment>$/);\n return match ? match[1] : text;\n}\n\nexport function countLines(text: string): number {\n if (!text) return 0;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) lines++;\n }\n return lines;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pasted-text.js","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,0EAA0E;AAC1E,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,qBAAqB,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OAAO,CAAC,CAAC;IACpB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE;YAAE,KAAK,EAAE,CAAC;IACzC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// Page-sized pastes turn into a `Pasted text` attachment chip instead of being\n// dumped into the editor. Short paragraphs and everyday lists should stay\n// inline so the composer still feels like a normal text field.\nconst PASTED_TEXT_MIN_CHARS = 3200;\nconst PASTED_TEXT_MIN_LINES = 24;\n\nconst PASTED_TEXT_FILENAME_PREFIX = \"pasted-text-\";\n\n// A copied HTML document/source is recognizable from its markup: a closing tag\n// (`</div>`), a doctype, or a common structural/element tag. We key off the\n// *content* the user actually pasted, not the clipboard's `text/html` flavor —\n// editors (VS Code) and rich-text apps (Google Docs) populate `text/html` with\n// syntax-highlight spans or formatting wrappers even when the real content is\n// plain code/prose, so trusting `text/html` blindly would mangle those pastes.\nconst HTML_SOURCE_SIGNAL =\n /<!doctype\\s+html|<html[\\s>]|<\\/[a-z][a-z0-9-]*\\s*>|<(?:body|head|div|span|section|main|header|footer|nav|article|aside|ul|ol|li|table|thead|tbody|tr|td|th|h[1-6]|p|a|img|button|input|textarea|select|form|label|script|style|link|meta|svg|canvas|template)\\b/i;\n\n// A real HTML *document* announces itself with a doctype / html / head / body.\n// When one of these is present we keep the HTML classification even if an inline\n// <script> contains JS keywords — it's a genuine page.\nconst HTML_DOCUMENT_SIGNAL =\n /<!doctype\\s+html|<html[\\s>]|<head[\\s>]|<body[\\s>]/i;\n\n// JSX/TSX source contains the same `</div>`/`<span>` tags as an HTML document,\n// so the HTML tag signal alone misfiles a pasted React/TS component (even a bare\n// function component) as an `.html` artifact — the agent then mishandles it as a\n// hostable document instead of source. These markers appear in JS/TS/JSX source\n// but not in a plain HTML *fragment*: `className=` (JSX uses it; HTML uses\n// `class=`), ES module import/export, arrow functions, TS type/React annotations,\n// React hooks, and the basic JS declaration/return keywords that make up a\n// component body.\nconst CODE_SOURCE_SIGNAL =\n /\\bclassName=|\\bimport\\b|\\bexport\\b|=>|:\\s*React\\.|\\buse[A-Z]\\w*\\(|\\b(?:function|const|let|var|return|class|interface|type|enum)\\b/;\n\nfunction looksLikeHtml(value: string): boolean {\n if (!value || !HTML_SOURCE_SIGNAL.test(value)) return false;\n // A self-announcing HTML document stays HTML even if it embeds a <script>.\n if (HTML_DOCUMENT_SIGNAL.test(value)) return true;\n // Otherwise it's a bare fragment — if it carries JS/TS/JSX code signals,\n // treat it as source code, not an HTML attachment.\n if (CODE_SOURCE_SIGNAL.test(value)) return false;\n return true;\n}\n\n/** The clipboard flavors we care about for a paste. */\nexport interface ClipboardPaste {\n /** `text/plain` flavor — used for size heuristics and as the default body. */\n text: string;\n /** `text/html` flavor when the source provided one. */\n html?: string;\n}\n\n/** Read the relevant clipboard flavors from a paste/drop DataTransfer. */\nexport function readClipboardPaste(\n data: { getData(type: string): string } | null | undefined,\n): ClipboardPaste {\n const text = data?.getData(\"text/plain\") ?? \"\";\n const html = data?.getData(\"text/html\") ?? \"\";\n return { text, html: html.trim() ? html : undefined };\n}\n\ninterface SelectedPasteBody {\n body: string;\n ext: \"html\" | \"txt\";\n type: \"text/html\" | \"text/plain\";\n}\n\n// Decide what to actually store for a paste. Preserving HTML markup means a\n// pasted HTML document behaves exactly like uploading that .html file: the agent\n// recognizes it as a hostable artifact and reads it verbatim via\n// `contentFromAttachment` instead of retyping it inline (which cuts off\n// mid-stream on large files and triggers a continuation loop / \"spin\").\nfunction selectPasteBody(paste: ClipboardPaste): SelectedPasteBody {\n const plain = paste.text ?? \"\";\n const html = paste.html ?? \"\";\n\n // 1) The pasted text is itself HTML source (copied from an editor, a file, or\n // view-source). The plain text *is* the markup, so keep it as .html.\n if (plain.trim() && looksLikeHtml(plain)) {\n return { body: plain, ext: \"html\", type: \"text/html\" };\n }\n\n // 2) No usable plain text, but a real HTML flavor exists (some apps only\n // expose text/html). Fall back to the markup so nothing is dropped.\n if (!plain.trim() && looksLikeHtml(html)) {\n return { body: html, ext: \"html\", type: \"text/html\" };\n }\n\n // 3) Default: keep the clean plain text. Avoids the syntax-highlight /\n // rich-text noise that lives in text/html when the plain text is already\n // the real content (code from an editor, prose from a doc).\n return { body: plain, ext: \"txt\", type: \"text/plain\" };\n}\n\nexport function shouldConvertPasteToAttachment(text: string): boolean {\n if (!text) return false;\n if (text.length >= PASTED_TEXT_MIN_CHARS) return true;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) {\n lines++;\n if (lines >= PASTED_TEXT_MIN_LINES) return true;\n }\n }\n return false;\n}\n\n/**\n * Whether a clipboard paste is large enough to become a `Pasted text`\n * attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the\n * representation we'd actually store, so an HTML-only paste (empty text/plain)\n * still converts on the strength of its markup.\n */\nexport function shouldConvertClipboardToAttachment(\n paste: ClipboardPaste,\n): boolean {\n return shouldConvertPasteToAttachment(selectPasteBody(paste).body);\n}\n\nfunction pastedAttachmentName(ext: \"html\" | \"txt\"): string {\n return `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()\n .toString(36)\n .slice(2, 8)}.${ext}`;\n}\n\n/**\n * Build the attachment File for a page-sized paste, preserving HTML markup when\n * the pasted content is an HTML document so it travels the same rail as an\n * uploaded .html file.\n */\nexport function createPastedAttachmentFile(paste: ClipboardPaste): File {\n const { body, ext, type } = selectPasteBody(paste);\n return new File([body], pastedAttachmentName(ext), { type });\n}\n\n/** Back-compat helper for callers that only have plain text. */\nexport function createPastedTextFile(text: string): File {\n return createPastedAttachmentFile({ text });\n}\n\nexport function isPastedTextAttachmentName(name: string | undefined): boolean {\n return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);\n}\n\n// Strips the `<attachment name=...>\\n` / `\\n</attachment>` envelope that\n// SimpleTextAttachmentAdapter wraps the file body in when sending. Returns the\n// raw body for previewing.\nexport function unwrapAttachmentEnvelope(text: string): string {\n const match = text.match(/^<attachment\\b[^>]*>\\n([\\s\\S]*)\\n<\\/attachment>$/);\n return match ? match[1] : text;\n}\n\nexport function countLines(text: string): number {\n if (!text) return 0;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) lines++;\n }\n return lines;\n}\n"]}
|
package/dist/db/migrations.d.ts
CHANGED
|
@@ -8,6 +8,16 @@ type NitroPluginDef = (nitroApp: any) => void | Promise<void>;
|
|
|
8
8
|
* regex with subtly different shapes.
|
|
9
9
|
*/
|
|
10
10
|
export declare function isDuplicateColumnError(err: unknown): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* True when a migration statement failed because the connected DB ROLE lacks
|
|
13
|
+
* privilege — e.g. a permission-limited dev/replica role that doesn't own the
|
|
14
|
+
* table. Postgres raises SQLSTATE 42501 ("insufficient_privilege", routine
|
|
15
|
+
* aclcheck_error, message "must be owner of table …"). We treat these as
|
|
16
|
+
* NON-FATAL so a perms-limited database can't crash-loop the whole server: the
|
|
17
|
+
* migration is skipped (left unrecorded) and a properly-privileged role applies
|
|
18
|
+
* it later. Production, where the role owns its tables, never hits this path.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isPermissionError(err: unknown): boolean;
|
|
11
21
|
export interface RunMigrationsOptions {
|
|
12
22
|
/**
|
|
13
23
|
* Name of the migrations bookkeeping table. REQUIRED — there is intentionally
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAOA,KAAK,cAAc,GAAG,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AA4B9D;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,GA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
|
package/dist/db/migrations.js
CHANGED
|
@@ -34,6 +34,24 @@ export function isDuplicateColumnError(err) {
|
|
|
34
34
|
const msg = err?.message ?? "";
|
|
35
35
|
return (/duplicate column name/i.test(msg) || /column .* already exists/i.test(msg));
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* True when a migration statement failed because the connected DB ROLE lacks
|
|
39
|
+
* privilege — e.g. a permission-limited dev/replica role that doesn't own the
|
|
40
|
+
* table. Postgres raises SQLSTATE 42501 ("insufficient_privilege", routine
|
|
41
|
+
* aclcheck_error, message "must be owner of table …"). We treat these as
|
|
42
|
+
* NON-FATAL so a perms-limited database can't crash-loop the whole server: the
|
|
43
|
+
* migration is skipped (left unrecorded) and a properly-privileged role applies
|
|
44
|
+
* it later. Production, where the role owns its tables, never hits this path.
|
|
45
|
+
*/
|
|
46
|
+
export function isPermissionError(err) {
|
|
47
|
+
const e = err;
|
|
48
|
+
if (e?.code === "42501")
|
|
49
|
+
return true;
|
|
50
|
+
const msg = e?.message ?? "";
|
|
51
|
+
return (/must be owner of/i.test(msg) ||
|
|
52
|
+
/permission denied/i.test(msg) ||
|
|
53
|
+
/insufficient privilege/i.test(msg));
|
|
54
|
+
}
|
|
37
55
|
/**
|
|
38
56
|
* Split a multi-statement SQL blob into individual statements.
|
|
39
57
|
*
|
|
@@ -221,6 +239,20 @@ export function runMigrations(migrations, options) {
|
|
|
221
239
|
console.log(`[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? "" : "s"})`);
|
|
222
240
|
}
|
|
223
241
|
catch (err) {
|
|
242
|
+
if (pg && isPermissionError(err)) {
|
|
243
|
+
// The connected role lacks privilege for this migration (e.g. a
|
|
244
|
+
// permission-limited dev/replica role that doesn't own the table).
|
|
245
|
+
// Don't crash-loop the whole server over it — warn and STOP here.
|
|
246
|
+
// We must NOT continue to later migrations: pending work is computed
|
|
247
|
+
// as `version > MAX(recorded version)`, so applying a later migration
|
|
248
|
+
// would advance MAX past this unrecorded one and orphan it forever.
|
|
249
|
+
// Stopping leaves MAX at the last recorded version, so a properly-
|
|
250
|
+
// privileged role resumes from this exact migration, in order.
|
|
251
|
+
console.warn(`[db] Migration v${m.version} skipped — insufficient privilege: ${err.message}. ` +
|
|
252
|
+
`Apply it with a DB role that owns the table. ` +
|
|
253
|
+
`Halting further migrations so this one isn't orphaned.`, "\nStatement:", currentStmt);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
224
256
|
console.error(`[db] Migration v${m.version} FAILED:`, err.message, "\nStatement:", currentStmt);
|
|
225
257
|
throw err;
|
|
226
258
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,eAAe,GAChB,MAAM,aAAa,CAAC;AAIrB;;;GAGG;AACH,SAAS,mBAAmB,CAAC,GAAW;IACtC,OAAO,GAAG;SACP,OAAO,CAAC,8BAA8B,EAAE,mBAAmB,CAAC;SAC5D,OAAO,CAAC,qBAAqB,EAAE,EAAE,CAAC;SAClC,OAAO,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,2BAA2B,GAAG,mCAAmC,CAAC;AAExE;;;;;;;;GAQG;AACH,SAAS,iBAAiB,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,OAAO,CAAC,oCAAoC,EAAE,YAAY,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,GAAY;IACjD,MAAM,GAAG,GAAI,GAAyB,EAAE,OAAO,IAAI,EAAE,CAAC;IACtD,OAAO,CACL,wBAAwB,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC,CAC5E,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,kBAAkB,CAAC,GAAW;IACrC,MAAM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a,CAAC,OAAO,CAAC,CAAC;YAChE,uEAAuE;YACvE,oEAAoE;YACpE,oEAAoE;YACpE,sEAAsE;YACtE,kEAAkE;YAClE,qDAAqD;YACrD,MAAM,YAAY,GAChB,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO;gBAClC,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,wBAAwB;gBACnD,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM;gBACjC,UAAU,IAAI,UAAU,CAAC;YAC3B,IAAI,OAAO,UAAU,CAAC,OAAO,EAAE,IAAI,KAAK,UAAU,IAAI,CAAC,YAAY,EAAE,CAAC;gBACpE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {\n getDbExec,\n isPostgres,\n getDialect,\n retrySqliteBusy,\n} from \"./client.js\";\n\ntype NitroPluginDef = (nitroApp: any) => void | Promise<void>;\n\n/**\n * Rewrite SQLite-specific SQL to Postgres-compatible equivalents.\n * Handles: datetime('now') → CURRENT_TIMESTAMP, AUTOINCREMENT → GENERATED, etc.\n */\nfunction adaptSqlForPostgres(sql: string): string {\n return sql\n .replace(/datetime\\s*\\(\\s*'now'\\s*\\)/gi, \"CURRENT_TIMESTAMP\")\n .replace(/\\bAUTOINCREMENT\\b/gi, \"\")\n .replace(/\\bINTEGER\\b/gi, \"BIGINT\");\n}\n\nconst IF_NOT_EXISTS_ADD_COLUMN_RE = /ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/i;\n\n/**\n * Strip Postgres-only syntax that SQLite doesn't support.\n * Handles: ALTER TABLE ... ADD COLUMN IF NOT EXISTS → ADD COLUMN\n *\n * Note: SQLite does not have a native equivalent, so the idempotent\n * semantic is emulated at the executor level by swallowing the\n * \"duplicate column name\" error for statements that originally carried\n * the clause. See `hadIfNotExists` tracking in the run loop.\n */\nfunction adaptSqlForSqlite(sql: string): string {\n return sql.replace(/ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/gi, \"ADD COLUMN\");\n}\n\n/**\n * True when an error from `ALTER TABLE ... ADD COLUMN` indicates the\n * column already existed. Recognizes both SQLite (\"duplicate column\n * name\") and Postgres (\"column ... already exists\" — exact text varies\n * by error code 42701, but the substring is stable). Exported so other\n * idempotent column-upgrade loops in the codebase don't reinvent this\n * regex with subtly different shapes.\n */\nexport function isDuplicateColumnError(err: unknown): boolean {\n const msg = (err as Error | undefined)?.message ?? \"\";\n return (\n /duplicate column name/i.test(msg) || /column .* already exists/i.test(msg)\n );\n}\n\n/**\n * Split a multi-statement SQL blob into individual statements.\n *\n * libsql's `execute(sql)` only runs the first statement in a multi-statement\n * string. This splitter is intentionally simple: it respects single-quoted\n * string literals (with `''` escaping) and `--` line comments, and splits on\n * top-level `;`. It does NOT attempt to parse `$$`-quoted Postgres function\n * bodies — migrations that define functions/triggers with `;` inside bodies\n * should pass a single-statement migration per entry instead.\n */\nfunction splitSqlStatements(sql: string): string[] {\n const out: string[] = [];\n let buf = \"\";\n let i = 0;\n let inSingle = false;\n while (i < sql.length) {\n const ch = sql[i];\n const next = sql[i + 1];\n if (!inSingle && ch === \"-\" && next === \"-\") {\n // Skip to end of line\n while (i < sql.length && sql[i] !== \"\\n\") i++;\n continue;\n }\n if (ch === \"'\") {\n buf += ch;\n if (inSingle && next === \"'\") {\n buf += next;\n i += 2;\n continue;\n }\n inSingle = !inSingle;\n i++;\n continue;\n }\n if (ch === \";\" && !inSingle) {\n const trimmed = buf.trim();\n if (trimmed) out.push(trimmed);\n buf = \"\";\n i++;\n continue;\n }\n buf += ch;\n i++;\n }\n const tail = buf.trim();\n if (tail) out.push(tail);\n return out;\n}\n\nexport interface RunMigrationsOptions {\n /**\n * Name of the migrations bookkeeping table. REQUIRED — there is intentionally\n * no default. Two templates that share a database (e.g. via the same Neon URL)\n * each have their own version space starting at v1, and a single shared\n * `_migrations` table will silently skip the second template's migrations if\n * the first has already advanced past those version numbers. This caused the\n * design template's migrations to be skipped entirely on a Neon DB that\n * slides had already populated up to v15 (PR #320 era).\n *\n * Use one bookkeeping table per template, e.g. `slides_migrations`. Core\n * feature plugins (e.g. the org module) follow the same convention with\n * their own prefix, e.g. `_org_migrations`.\n */\n table: string;\n}\n\n/**\n * A single migration entry.\n *\n * `sql` can be a string (runs on every dialect) or an object with dialect\n * keys for dialect-gated SQL. Useful when Postgres needs an ALTER that\n * SQLite can't parse.\n *\n * { version: 14, sql: { postgres: \"ALTER TABLE …\" } } // no-op on sqlite\n * { version: 15, sql: { sqlite: \"…\", postgres: \"…\" } } // both dialects\n */\nexport type MigrationSql = string | { postgres?: string; sqlite?: string };\n\nexport interface MigrationEntry {\n version: number;\n sql: MigrationSql;\n}\n\nfunction resolveMigrationSql(sql: MigrationSql, pg: boolean): string | null {\n if (typeof sql === \"string\") return sql;\n const raw = pg ? sql.postgres : sql.sqlite;\n return raw ?? null;\n}\n\nexport function runMigrations(\n migrations: Array<MigrationEntry>,\n options: RunMigrationsOptions,\n): NitroPluginDef {\n const table = options?.table;\n if (\n !table ||\n typeof table !== \"string\" ||\n !/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)\n ) {\n throw new Error(\n \"runMigrations: `table` option is required and must be a valid SQL identifier \" +\n '(e.g. `{ table: \"slides_migrations\" }`). See packages/core/src/db/migrations.ts ' +\n \"for why this is required (shared-DB version-collision bug).\",\n );\n }\n return async () => {\n try {\n // Check for Cloudflare D1 binding (only if DATABASE_URL not set)\n const d1 = getDialect() === \"d1\" ? globalThis.__cf_env?.DB : null;\n if (d1) {\n await d1\n .prepare(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n )\n .run();\n const firstRow = await d1\n .prepare(`SELECT MAX(version) as v FROM ${table}`)\n .first<{ v?: number }>();\n const current = (firstRow?.v as number) ?? 0;\n\n for (const m of migrations.filter((m) => m.version > current)) {\n try {\n // D1 is SQLite-compatible\n const raw = resolveMigrationSql(m.sql, false);\n if (raw == null) {\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n continue;\n }\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n const hasIfNotExists = statements.some((s) => s.hadIfNotExists);\n if (hasIfNotExists) {\n // Per-statement path: we need to swallow \"duplicate column\"\n // errors for statements that originally carried\n // `ADD COLUMN IF NOT EXISTS`, which a batch() can't express.\n // Loses atomicity, but the idempotent-ADD-COLUMN semantic\n // means a partial re-run resolves cleanly on retry.\n for (const { sql: stmt, hadIfNotExists } of statements) {\n try {\n await d1.prepare(stmt).run();\n } catch (err) {\n if (hadIfNotExists && isDuplicateColumnError(err)) continue;\n throw err;\n }\n }\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n } else {\n // Atomic batch: all statements + version-row insert land in\n // the same transaction. A failing statement rolls the whole\n // migration back, so we never record a half-applied version.\n await d1.batch([\n ...statements.map((s) => d1.prepare(s.sql)),\n d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version),\n ]);\n }\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nSQL:\",\n JSON.stringify(m.sql),\n );\n throw err;\n }\n }\n return;\n }\n\n // Generic path — works for libsql and Postgres\n const exec = getDbExec();\n const pg = isPostgres();\n\n // Retry initial table creation — SQLITE_BUSY_RECOVERY can occur on HMR\n // restarts when WAL files from the previous process haven't been released yet.\n await retrySqliteBusy(\n () =>\n exec.execute(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n ),\n { maxAttempts: 6, baseDelayMs: 1000, rethrow: true },\n );\n\n const { rows } = await exec.execute(\n `SELECT MAX(version) as v FROM ${table}`,\n );\n const current = (rows[0]?.v as number) ?? 0;\n\n const insertSql = pg\n ? `INSERT INTO ${table} VALUES (?) ON CONFLICT DO NOTHING`\n : `INSERT OR IGNORE INTO ${table} VALUES (?)`;\n\n const pending = migrations.filter((m) => m.version > current);\n if (pending.length > 0) {\n console.log(\n `[db] Applying ${pending.length} migration(s) on ${pg ? \"Postgres\" : \"SQLite/libsql\"}…`,\n );\n }\n\n for (const m of pending) {\n const raw = resolveMigrationSql(m.sql, pg);\n if (raw == null) {\n // Dialect-gated migration with no SQL for this dialect; still mark\n // as applied so we don't retry forever.\n await exec.execute({ sql: insertSql, args: [m.version] });\n continue;\n }\n // Split BEFORE adapting so we can remember which original statements\n // carried `ADD COLUMN IF NOT EXISTS` — SQLite drops the clause, so we\n // emulate the idempotent semantic by swallowing duplicate-column\n // errors only for those statements.\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: pg ? adaptSqlForPostgres(orig) : adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n let currentStmt = \"\";\n try {\n for (const { sql: stmt, hadIfNotExists } of statements) {\n currentStmt = stmt;\n try {\n await exec.execute(stmt);\n } catch (err) {\n if (!pg && hadIfNotExists && isDuplicateColumnError(err)) {\n // IF NOT EXISTS semantic: column already present, skip.\n continue;\n }\n throw err;\n }\n }\n await exec.execute({ sql: insertSql, args: [m.version] });\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nStatement:\",\n currentStmt,\n );\n throw err;\n }\n }\n } catch (err) {\n console.error(\"[db] Migration failed:\", (err as Error).message);\n // In local dev, hard-fail so the developer catches errors immediately.\n // On serverless runtimes (Netlify Functions, Vercel, CF Workers) we\n // keep the process alive — the app will return 500s for routes that\n // depend on the missing tables, but at least other routes still work.\n // Note: Node.js 21+ defines globalThis.navigator, so we check for\n // serverless env vars instead of navigator presence.\n const isServerless =\n !!globalThis.process?.env?.NETLIFY ||\n !!globalThis.process?.env?.AWS_LAMBDA_FUNCTION_NAME ||\n !!globalThis.process?.env?.VERCEL ||\n \"__cf_env\" in globalThis;\n if (typeof globalThis.process?.exit === \"function\" && !isServerless) {\n process.exit(1);\n }\n }\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,eAAe,GAChB,MAAM,aAAa,CAAC;AAIrB;;;GAGG;AACH,SAAS,mBAAmB,CAAC,GAAW;IACtC,OAAO,GAAG;SACP,OAAO,CAAC,8BAA8B,EAAE,mBAAmB,CAAC;SAC5D,OAAO,CAAC,qBAAqB,EAAE,EAAE,CAAC;SAClC,OAAO,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,2BAA2B,GAAG,mCAAmC,CAAC;AAExE;;;;;;;;GAQG;AACH,SAAS,iBAAiB,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,OAAO,CAAC,oCAAoC,EAAE,YAAY,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,GAAY;IACjD,MAAM,GAAG,GAAI,GAAyB,EAAE,OAAO,IAAI,EAAE,CAAC;IACtD,OAAO,CACL,wBAAwB,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC,CAC5E,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAY;IAC5C,MAAM,CAAC,GAAG,GAAsD,CAAC;IACjE,IAA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;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {\n getDbExec,\n isPostgres,\n getDialect,\n retrySqliteBusy,\n} from \"./client.js\";\n\ntype NitroPluginDef = (nitroApp: any) => void | Promise<void>;\n\n/**\n * Rewrite SQLite-specific SQL to Postgres-compatible equivalents.\n * Handles: datetime('now') → CURRENT_TIMESTAMP, AUTOINCREMENT → GENERATED, etc.\n */\nfunction adaptSqlForPostgres(sql: string): string {\n return sql\n .replace(/datetime\\s*\\(\\s*'now'\\s*\\)/gi, \"CURRENT_TIMESTAMP\")\n .replace(/\\bAUTOINCREMENT\\b/gi, \"\")\n .replace(/\\bINTEGER\\b/gi, \"BIGINT\");\n}\n\nconst IF_NOT_EXISTS_ADD_COLUMN_RE = /ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/i;\n\n/**\n * Strip Postgres-only syntax that SQLite doesn't support.\n * Handles: ALTER TABLE ... ADD COLUMN IF NOT EXISTS → ADD COLUMN\n *\n * Note: SQLite does not have a native equivalent, so the idempotent\n * semantic is emulated at the executor level by swallowing the\n * \"duplicate column name\" error for statements that originally carried\n * the clause. See `hadIfNotExists` tracking in the run loop.\n */\nfunction adaptSqlForSqlite(sql: string): string {\n return sql.replace(/ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/gi, \"ADD COLUMN\");\n}\n\n/**\n * True when an error from `ALTER TABLE ... ADD COLUMN` indicates the\n * column already existed. Recognizes both SQLite (\"duplicate column\n * name\") and Postgres (\"column ... already exists\" — exact text varies\n * by error code 42701, but the substring is stable). Exported so other\n * idempotent column-upgrade loops in the codebase don't reinvent this\n * regex with subtly different shapes.\n */\nexport function isDuplicateColumnError(err: unknown): boolean {\n const msg = (err as Error | undefined)?.message ?? \"\";\n return (\n /duplicate column name/i.test(msg) || /column .* already exists/i.test(msg)\n );\n}\n\n/**\n * True when a migration statement failed because the connected DB ROLE lacks\n * privilege — e.g. a permission-limited dev/replica role that doesn't own the\n * table. Postgres raises SQLSTATE 42501 (\"insufficient_privilege\", routine\n * aclcheck_error, message \"must be owner of table …\"). We treat these as\n * NON-FATAL so a perms-limited database can't crash-loop the whole server: the\n * migration is skipped (left unrecorded) and a properly-privileged role applies\n * it later. Production, where the role owns its tables, never hits this path.\n */\nexport function isPermissionError(err: unknown): boolean {\n const e = err as { code?: string; message?: string } | undefined;\n if (e?.code === \"42501\") return true;\n const msg = e?.message ?? \"\";\n return (\n /must be owner of/i.test(msg) ||\n /permission denied/i.test(msg) ||\n /insufficient privilege/i.test(msg)\n );\n}\n\n/**\n * Split a multi-statement SQL blob into individual statements.\n *\n * libsql's `execute(sql)` only runs the first statement in a multi-statement\n * string. This splitter is intentionally simple: it respects single-quoted\n * string literals (with `''` escaping) and `--` line comments, and splits on\n * top-level `;`. It does NOT attempt to parse `$$`-quoted Postgres function\n * bodies — migrations that define functions/triggers with `;` inside bodies\n * should pass a single-statement migration per entry instead.\n */\nfunction splitSqlStatements(sql: string): string[] {\n const out: string[] = [];\n let buf = \"\";\n let i = 0;\n let inSingle = false;\n while (i < sql.length) {\n const ch = sql[i];\n const next = sql[i + 1];\n if (!inSingle && ch === \"-\" && next === \"-\") {\n // Skip to end of line\n while (i < sql.length && sql[i] !== \"\\n\") i++;\n continue;\n }\n if (ch === \"'\") {\n buf += ch;\n if (inSingle && next === \"'\") {\n buf += next;\n i += 2;\n continue;\n }\n inSingle = !inSingle;\n i++;\n continue;\n }\n if (ch === \";\" && !inSingle) {\n const trimmed = buf.trim();\n if (trimmed) out.push(trimmed);\n buf = \"\";\n i++;\n continue;\n }\n buf += ch;\n i++;\n }\n const tail = buf.trim();\n if (tail) out.push(tail);\n return out;\n}\n\nexport interface RunMigrationsOptions {\n /**\n * Name of the migrations bookkeeping table. REQUIRED — there is intentionally\n * no default. Two templates that share a database (e.g. via the same Neon URL)\n * each have their own version space starting at v1, and a single shared\n * `_migrations` table will silently skip the second template's migrations if\n * the first has already advanced past those version numbers. This caused the\n * design template's migrations to be skipped entirely on a Neon DB that\n * slides had already populated up to v15 (PR #320 era).\n *\n * Use one bookkeeping table per template, e.g. `slides_migrations`. Core\n * feature plugins (e.g. the org module) follow the same convention with\n * their own prefix, e.g. `_org_migrations`.\n */\n table: string;\n}\n\n/**\n * A single migration entry.\n *\n * `sql` can be a string (runs on every dialect) or an object with dialect\n * keys for dialect-gated SQL. Useful when Postgres needs an ALTER that\n * SQLite can't parse.\n *\n * { version: 14, sql: { postgres: \"ALTER TABLE …\" } } // no-op on sqlite\n * { version: 15, sql: { sqlite: \"…\", postgres: \"…\" } } // both dialects\n */\nexport type MigrationSql = string | { postgres?: string; sqlite?: string };\n\nexport interface MigrationEntry {\n version: number;\n sql: MigrationSql;\n}\n\nfunction resolveMigrationSql(sql: MigrationSql, pg: boolean): string | null {\n if (typeof sql === \"string\") return sql;\n const raw = pg ? sql.postgres : sql.sqlite;\n return raw ?? null;\n}\n\nexport function runMigrations(\n migrations: Array<MigrationEntry>,\n options: RunMigrationsOptions,\n): NitroPluginDef {\n const table = options?.table;\n if (\n !table ||\n typeof table !== \"string\" ||\n !/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)\n ) {\n throw new Error(\n \"runMigrations: `table` option is required and must be a valid SQL identifier \" +\n '(e.g. `{ table: \"slides_migrations\" }`). See packages/core/src/db/migrations.ts ' +\n \"for why this is required (shared-DB version-collision bug).\",\n );\n }\n return async () => {\n try {\n // Check for Cloudflare D1 binding (only if DATABASE_URL not set)\n const d1 = getDialect() === \"d1\" ? globalThis.__cf_env?.DB : null;\n if (d1) {\n await d1\n .prepare(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n )\n .run();\n const firstRow = await d1\n .prepare(`SELECT MAX(version) as v FROM ${table}`)\n .first<{ v?: number }>();\n const current = (firstRow?.v as number) ?? 0;\n\n for (const m of migrations.filter((m) => m.version > current)) {\n try {\n // D1 is SQLite-compatible\n const raw = resolveMigrationSql(m.sql, false);\n if (raw == null) {\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n continue;\n }\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n const hasIfNotExists = statements.some((s) => s.hadIfNotExists);\n if (hasIfNotExists) {\n // Per-statement path: we need to swallow \"duplicate column\"\n // errors for statements that originally carried\n // `ADD COLUMN IF NOT EXISTS`, which a batch() can't express.\n // Loses atomicity, but the idempotent-ADD-COLUMN semantic\n // means a partial re-run resolves cleanly on retry.\n for (const { sql: stmt, hadIfNotExists } of statements) {\n try {\n await d1.prepare(stmt).run();\n } catch (err) {\n if (hadIfNotExists && isDuplicateColumnError(err)) continue;\n throw err;\n }\n }\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n } else {\n // Atomic batch: all statements + version-row insert land in\n // the same transaction. A failing statement rolls the whole\n // migration back, so we never record a half-applied version.\n await d1.batch([\n ...statements.map((s) => d1.prepare(s.sql)),\n d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version),\n ]);\n }\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nSQL:\",\n JSON.stringify(m.sql),\n );\n throw err;\n }\n }\n return;\n }\n\n // Generic path — works for libsql and Postgres\n const exec = getDbExec();\n const pg = isPostgres();\n\n // Retry initial table creation — SQLITE_BUSY_RECOVERY can occur on HMR\n // restarts when WAL files from the previous process haven't been released yet.\n await retrySqliteBusy(\n () =>\n exec.execute(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n ),\n { maxAttempts: 6, baseDelayMs: 1000, rethrow: true },\n );\n\n const { rows } = await exec.execute(\n `SELECT MAX(version) as v FROM ${table}`,\n );\n const current = (rows[0]?.v as number) ?? 0;\n\n const insertSql = pg\n ? `INSERT INTO ${table} VALUES (?) ON CONFLICT DO NOTHING`\n : `INSERT OR IGNORE INTO ${table} VALUES (?)`;\n\n const pending = migrations.filter((m) => m.version > current);\n if (pending.length > 0) {\n console.log(\n `[db] Applying ${pending.length} migration(s) on ${pg ? \"Postgres\" : \"SQLite/libsql\"}…`,\n );\n }\n\n for (const m of pending) {\n const raw = resolveMigrationSql(m.sql, pg);\n if (raw == null) {\n // Dialect-gated migration with no SQL for this dialect; still mark\n // as applied so we don't retry forever.\n await exec.execute({ sql: insertSql, args: [m.version] });\n continue;\n }\n // Split BEFORE adapting so we can remember which original statements\n // carried `ADD COLUMN IF NOT EXISTS` — SQLite drops the clause, so we\n // emulate the idempotent semantic by swallowing duplicate-column\n // errors only for those statements.\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: pg ? adaptSqlForPostgres(orig) : adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n let currentStmt = \"\";\n try {\n for (const { sql: stmt, hadIfNotExists } of statements) {\n currentStmt = stmt;\n try {\n await exec.execute(stmt);\n } catch (err) {\n if (!pg && hadIfNotExists && isDuplicateColumnError(err)) {\n // IF NOT EXISTS semantic: column already present, skip.\n continue;\n }\n throw err;\n }\n }\n await exec.execute({ sql: insertSql, args: [m.version] });\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n if (pg && isPermissionError(err)) {\n // The connected role lacks privilege for this migration (e.g. a\n // permission-limited dev/replica role that doesn't own the table).\n // Don't crash-loop the whole server over it — warn and STOP here.\n // We must NOT continue to later migrations: pending work is computed\n // as `version > MAX(recorded version)`, so applying a later migration\n // would advance MAX past this unrecorded one and orphan it forever.\n // Stopping leaves MAX at the last recorded version, so a properly-\n // privileged role resumes from this exact migration, in order.\n console.warn(\n `[db] Migration v${m.version} skipped — insufficient privilege: ${(err as Error).message}. ` +\n `Apply it with a DB role that owns the table. ` +\n `Halting further migrations so this one isn't orphaned.`,\n \"\\nStatement:\",\n currentStmt,\n );\n break;\n }\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nStatement:\",\n currentStmt,\n );\n throw err;\n }\n }\n } catch (err) {\n console.error(\"[db] Migration failed:\", (err as Error).message);\n // In local dev, hard-fail so the developer catches errors immediately.\n // On serverless runtimes (Netlify Functions, Vercel, CF Workers) we\n // keep the process alive — the app will return 500s for routes that\n // depend on the missing tables, but at least other routes still work.\n // Note: Node.js 21+ defines globalThis.navigator, so we check for\n // serverless env vars instead of navigator presence.\n const isServerless =\n !!globalThis.process?.env?.NETLIFY ||\n !!globalThis.process?.env?.AWS_LAMBDA_FUNCTION_NAME ||\n !!globalThis.process?.env?.VERCEL ||\n \"__cf_env\" in globalThis;\n if (typeof globalThis.process?.exit === \"function\" && !isServerless) {\n process.exit(1);\n }\n }\n };\n}\n"]}
|