@riotprompt/riotprompt 0.0.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.
Files changed (129) hide show
  1. package/.gitcarve/config.yaml +10 -0
  2. package/.gitcarve/context/content.md +11 -0
  3. package/.markdown-doctest-setup.mjs +23 -0
  4. package/.nvmrc +1 -0
  5. package/LICENSE +190 -0
  6. package/README.md +513 -0
  7. package/dist/builder.cjs +152 -0
  8. package/dist/builder.cjs.map +1 -0
  9. package/dist/builder.d.ts +37 -0
  10. package/dist/builder.js +148 -0
  11. package/dist/builder.js.map +1 -0
  12. package/dist/chat.cjs +26 -0
  13. package/dist/chat.cjs.map +1 -0
  14. package/dist/chat.d.ts +14 -0
  15. package/dist/chat.js +21 -0
  16. package/dist/chat.js.map +1 -0
  17. package/dist/constants.cjs +34 -0
  18. package/dist/constants.cjs.map +1 -0
  19. package/dist/constants.d.ts +13 -0
  20. package/dist/constants.js +23 -0
  21. package/dist/constants.js.map +1 -0
  22. package/dist/formatter.cjs +139 -0
  23. package/dist/formatter.cjs.map +1 -0
  24. package/dist/formatter.d.ts +88 -0
  25. package/dist/formatter.js +131 -0
  26. package/dist/formatter.js.map +1 -0
  27. package/dist/items/content.cjs +14 -0
  28. package/dist/items/content.cjs.map +1 -0
  29. package/dist/items/content.d.ts +3 -0
  30. package/dist/items/content.js +10 -0
  31. package/dist/items/content.js.map +1 -0
  32. package/dist/items/context.cjs +13 -0
  33. package/dist/items/context.cjs.map +1 -0
  34. package/dist/items/context.d.ts +3 -0
  35. package/dist/items/context.js +9 -0
  36. package/dist/items/context.js.map +1 -0
  37. package/dist/items/instruction.cjs +13 -0
  38. package/dist/items/instruction.cjs.map +1 -0
  39. package/dist/items/instruction.d.ts +3 -0
  40. package/dist/items/instruction.js +9 -0
  41. package/dist/items/instruction.js.map +1 -0
  42. package/dist/items/parameters.cjs +53 -0
  43. package/dist/items/parameters.cjs.map +1 -0
  44. package/dist/items/parameters.d.ts +5 -0
  45. package/dist/items/parameters.js +47 -0
  46. package/dist/items/parameters.js.map +1 -0
  47. package/dist/items/section.cjs +120 -0
  48. package/dist/items/section.cjs.map +1 -0
  49. package/dist/items/section.d.ts +33 -0
  50. package/dist/items/section.js +115 -0
  51. package/dist/items/section.js.map +1 -0
  52. package/dist/items/trait.cjs +13 -0
  53. package/dist/items/trait.cjs.map +1 -0
  54. package/dist/items/trait.d.ts +3 -0
  55. package/dist/items/trait.js +9 -0
  56. package/dist/items/trait.js.map +1 -0
  57. package/dist/items/weighted.cjs +27 -0
  58. package/dist/items/weighted.cjs.map +1 -0
  59. package/dist/items/weighted.d.ts +24 -0
  60. package/dist/items/weighted.js +22 -0
  61. package/dist/items/weighted.js.map +1 -0
  62. package/dist/loader.cjs +167 -0
  63. package/dist/loader.cjs.map +1 -0
  64. package/dist/loader.d.ts +35 -0
  65. package/dist/loader.js +161 -0
  66. package/dist/loader.js.map +1 -0
  67. package/dist/logger.cjs +51 -0
  68. package/dist/logger.cjs.map +1 -0
  69. package/dist/logger.d.ts +11 -0
  70. package/dist/logger.js +46 -0
  71. package/dist/logger.js.map +1 -0
  72. package/dist/override.cjs +109 -0
  73. package/dist/override.cjs.map +1 -0
  74. package/dist/override.d.ts +31 -0
  75. package/dist/override.js +105 -0
  76. package/dist/override.js.map +1 -0
  77. package/dist/parse/markdown.cjs +114 -0
  78. package/dist/parse/markdown.cjs.map +1 -0
  79. package/dist/parse/markdown.d.ts +3 -0
  80. package/dist/parse/markdown.js +110 -0
  81. package/dist/parse/markdown.js.map +1 -0
  82. package/dist/parse/text.cjs +33 -0
  83. package/dist/parse/text.cjs.map +1 -0
  84. package/dist/parse/text.d.ts +3 -0
  85. package/dist/parse/text.js +29 -0
  86. package/dist/parse/text.js.map +1 -0
  87. package/dist/parser.cjs +99 -0
  88. package/dist/parser.cjs.map +1 -0
  89. package/dist/parser.d.ts +21 -0
  90. package/dist/parser.js +75 -0
  91. package/dist/parser.js.map +1 -0
  92. package/dist/prompt.cjs +15 -0
  93. package/dist/prompt.cjs.map +1 -0
  94. package/dist/prompt.d.ts +16 -0
  95. package/dist/prompt.js +11 -0
  96. package/dist/prompt.js.map +1 -0
  97. package/dist/riotprompt.cjs +1359 -0
  98. package/dist/riotprompt.cjs.map +1 -0
  99. package/dist/riotprompt.d.ts +25 -0
  100. package/dist/riotprompt.js +21 -0
  101. package/dist/riotprompt.js.map +1 -0
  102. package/dist/util/general.cjs +52 -0
  103. package/dist/util/general.cjs.map +1 -0
  104. package/dist/util/general.d.ts +4 -0
  105. package/dist/util/general.js +47 -0
  106. package/dist/util/general.js.map +1 -0
  107. package/dist/util/markdown.cjs +115 -0
  108. package/dist/util/markdown.cjs.map +1 -0
  109. package/dist/util/markdown.d.ts +7 -0
  110. package/dist/util/markdown.js +111 -0
  111. package/dist/util/markdown.js.map +1 -0
  112. package/dist/util/storage.cjs +155 -0
  113. package/dist/util/storage.cjs.map +1 -0
  114. package/dist/util/storage.d.ts +32 -0
  115. package/dist/util/storage.js +132 -0
  116. package/dist/util/storage.js.map +1 -0
  117. package/dist/util/text.cjs +42 -0
  118. package/dist/util/text.cjs.map +1 -0
  119. package/dist/util/text.d.ts +1 -0
  120. package/dist/util/text.js +38 -0
  121. package/dist/util/text.js.map +1 -0
  122. package/docs/loader.md +237 -0
  123. package/docs/override.md +323 -0
  124. package/docs/parser.md +130 -0
  125. package/eslint.config.mjs +82 -0
  126. package/nodemon.json +14 -0
  127. package/package.json +72 -0
  128. package/vite.config.ts +114 -0
  129. package/vitest.config.ts +25 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown.cjs","sources":["../../src/util/markdown.ts"],"sourcesContent":["import { DEFAULT_CHARACTER_ENCODING } from \"../constants\";\n\n// Heuristic to check for Markdown syntax. This is not a full parser.\n// It looks for common Markdown patterns.\nconst markdownRegex = /^(#+\\s|\\*\\s|-\\s|\\+\\s|>\\s|\\[.*\\]\\(.*\\)|```|~~~|---\\\\s*$)/m;\n\n/**\n * Inspects a string to see if it likely contains Markdown syntax.\n *\n * @param input The string or Buffer content to inspect.\n * @returns True if Markdown syntax is suspected, false otherwise.\n */\nexport function isMarkdown(input: string | Buffer): boolean {\n if (input == null) {\n return false;\n }\n // Convert Buffer to string if necessary\n const content = typeof input === 'string' ? input : input.toString(DEFAULT_CHARACTER_ENCODING);\n if (!content || content.trim() === '') {\n return false; // Empty string is not considered Markdown\n }\n\n // Check for common Markdown patterns in the entire content\n if (markdownRegex.test(content)) {\n return true;\n }\n\n // Fallback: Check for a high prevalence of Markdown-like list/header starters\n // or thematic breaks, or code blocks.\n // We'll consider up to the first ~2000 characters, roughly equivalent to the byte check.\n const effectiveContent = content.length > 2000 ? content.substring(0, 2000) : content;\n const lines = effectiveContent.split('\\n');\n let markdownFeatureCount = 0;\n const featurePatterns = [\n /^#+\\s+.+/, // Headers (e.g., # Heading)\n /^\\s*[*+-]\\s+.+/, // List items (e.g., * item, - item, + item)\n /^\\s*>\\s+.+/, // Blockquotes (e.g., > quote)\n /\\[.+\\]\\(.+\\)/, // Links (e.g., [text](url))\n /!\\[.+\\]\\(.+\\)/, // Images (e.g., ![alt](src))\n /`{1,3}[^`]+`{1,3}/, // Inline code (e.g., `code`) or code blocks (```code```)\n /^\\s*_{3,}\\s*$/, // Thematic breaks (e.g., ---, ***, ___)\n /^\\s*-{3,}\\s*$/,\n /^\\s*\\*{3,}\\s*$/\n ];\n\n for (const line of lines) {\n // Stop checking if we have already found enough features to be confident.\n // This is a small optimization for very long inputs that are clearly markdown early on.\n if (markdownFeatureCount >= 2 && lines.length > 10) { // Heuristic threshold\n const significantLineCountEarly = Math.min(lines.indexOf(line) + 1, 20);\n if (significantLineCountEarly > 0 && markdownFeatureCount / significantLineCountEarly > 0.1) {\n return true;\n }\n }\n\n for (const pattern of featurePatterns) {\n if (pattern.test(line.trim())) {\n markdownFeatureCount++;\n break; // Count each line only once\n }\n }\n }\n\n // If more than 5% of the first few lines (up to 20 lines or all lines if fewer)\n // show markdown features, or if there are at least 2 distinct features in short texts,\n // consider it Markdown.\n const significantLineCount = Math.min(lines.length, 20);\n if (significantLineCount > 0) {\n // Calculate the exact threshold percentage\n const thresholdPercentage = markdownFeatureCount / significantLineCount;\n\n // Check against the 5% threshold (0.05)\n // Using >= 0.05 exactly matches 5%, > 0.05 requires more than 5%\n if (thresholdPercentage >= 0.05 + 0.0001) { // Adding a small epsilon to ensure exactly 5% passes but just below fails\n return true;\n }\n\n // Other conditions for returning true\n if ((markdownFeatureCount >= 1 && significantLineCount <= 5) || markdownFeatureCount >= 2) {\n return true;\n }\n }\n\n return false;\n}\n\n// Example usage (optional, for testing):\n// function testIsMarkdownString() {\n// console.log('--- Testing isMarkdownString ---');\n// const markdown1 = '# Hello World\\\\nThis is a test.';\n// console.log(`Test 1 (Header): \"${markdown1.substring(0,10)}...\" -> ${isMarkdownString(markdown1)}`); // true\n\n// const markdown2 = '* Item 1\\\\n* Item 2';\n// console.log(`Test 2 (List): \"${markdown2.substring(0,10)}...\" -> ${isMarkdownString(markdown2)}`); // true\n\n// const markdown3 = '[Google](https://google.com)';\n// console.log(`Test 3 (Link): \"${markdown3.substring(0,15)}...\" -> ${isMarkdownString(markdown3)}`); // true\n\n// const markdown4 = '> This is a quote.';\n// console.log(`Test 4 (Blockquote): \"${markdown4.substring(0,10)}...\" -> ${isMarkdownString(markdown4)}`); // true\n\n// const markdown5 = '```javascript\\\\nconsole.log(\"hello\");\\\\n```';\n// console.log(`Test 5 (Code block): \"${markdown5.substring(0,15)}...\" -> ${isMarkdownString(markdown5)}`); // true\n\n// const text1 = 'This is a plain text string.';\n// console.log(`Test 6 (Plain text): \"${text1.substring(0,10)}...\" -> ${isMarkdownString(text1)}`); // false\n\n// const text2 = 'hello_world.this_is_a_test_string_with_underscores_but_not_markdown_thematic_break';\n// console.log(`Test 7 (Long non-markdown): \"${text2.substring(0,10)}...\" -> ${isMarkdownString(text2)}`); // false\n\n// const text3 = '<xml><tag>value</tag></xml>';\n// console.log(`Test 8 (XML): \"${text3.substring(0,10)}...\" -> ${isMarkdownString(text3)}`); // false\n\n// const shortMarkdown = '# H';\n// console.log(`Test 9 (Short Markdown): \"${shortMarkdown}\" -> ${isMarkdownString(shortMarkdown)}`); // true\n\n// const shortNonMarkdown = 'Hello';\n// console.log(`Test 10 (Short Non-Markdown): \"${shortNonMarkdown}\" -> ${isMarkdownString(shortNonMarkdown)}`); // false\n\n// const emptyString = '';\n// console.log(`Test 11 (Empty string): \"\" -> ${isMarkdownString(emptyString)}`); // false\n\n// const whitespaceString = ' \\t \\n ';\n// console.log(`Test 12 (Whitespace string): \"${whitespaceString.substring(0,5)}...\" -> ${isMarkdownString(whitespaceString)}`); // false\n\n// const markdownWithManyFeatures = `# Title\\\\n\\\\n* list\\\\n* list2\\\\n\\\\n> quote here\\\\n\\\\n\\`\\`\\`\\\\ncode\\\\n\\`\\`\\`\\\\n\\\\nnormal text paragraph with a [link](url).\\n---\\nAnother paragraph.\\nThis is just a test string to see how it performs with multiple markdown features present.\\nHello world this is a very long line that does not contain any markdown syntax at all, it is just plain text that goes on and on.\\n* Another list item\\n* And another one\\n# Another Header\\n## Subheader\\nThis is fun.\\nOkay I think this is enough.\\nFinal line.\\nAnother final line.\\nOne more for good measure.\\nOkay that should be enough lines to test the early exit.\\n`;\n// console.log(`Test 13 (Many Features): \"${markdownWithManyFeatures.substring(0,10)}...\" -> ${isMarkdownString(markdownWithManyFeatures)}`); // true\n\n// const htmlLike = '<div><p>Hello</p><ul><li>item</li></ul></div>';\n// console.log(`Test 14 (HTML-like): \"${htmlLike.substring(0,10)}...\" -> ${isMarkdownString(htmlLike)}`); // false\n\n// console.log('--- End Testing ---');\n// }\n\n// testIsMarkdownString();\n"],"names":["markdownRegex","isMarkdown","input","content","toString","DEFAULT_CHARACTER_ENCODING","trim","test","effectiveContent","length","substring","lines","split","markdownFeatureCount","featurePatterns","line","significantLineCountEarly","Math","min","indexOf","pattern","significantLineCount","thresholdPercentage"],"mappings":";;;;;;AAEA;AACA;AACA,MAAMA,a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N,eAAiB,CAAA;AACnC,YAAA,IAAIM,OAAQb,CAAAA,IAAI,CAACQ,IAAAA,CAAKT,IAAI,EAAK,CAAA,EAAA;AAC3BO,gBAAAA,oBAAAA,EAAAA;AACA,gBAAA,MAAA;AACJ;AACJ;AACJ;;;;AAKA,IAAA,MAAMQ,uBAAuBJ,IAAKC,CAAAA,GAAG,CAACP,KAAAA,CAAMF,MAAM,EAAE,EAAA,CAAA;AACpD,IAAA,IAAIY,uBAAuB,CAAG,EAAA;;AAE1B,QAAA,MAAMC,sBAAsBT,oBAAuBQ,GAAAA,oBAAAA;;;QAInD,IAAIC,mBAAAA,IAAuB,OAAO,MAAQ,EAAA;YACtC,OAAO,IAAA;AACX;;AAGA,QAAA,IAAI,oBAAyB,IAAA,CAAA,IAAKD,oBAAwB,IAAA,CAAA,IAAMR,wBAAwB,CAAG,EAAA;YACvF,OAAO,IAAA;AACX;AACJ;IAEA,OAAO,KAAA;AACX,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Inspects a string to see if it likely contains Markdown syntax.
3
+ *
4
+ * @param input The string or Buffer content to inspect.
5
+ * @returns True if Markdown syntax is suspected, false otherwise.
6
+ */
7
+ export declare function isMarkdown(input: string | Buffer): boolean;
@@ -0,0 +1,111 @@
1
+ import { DEFAULT_CHARACTER_ENCODING } from '../constants.js';
2
+
3
+ // Heuristic to check for Markdown syntax. This is not a full parser.
4
+ // It looks for common Markdown patterns.
5
+ const markdownRegex = /^(#+\s|\*\s|-\s|\+\s|>\s|\[.*\]\(.*\)|```|~~~|---\\s*$)/m;
6
+ /**
7
+ * Inspects a string to see if it likely contains Markdown syntax.
8
+ *
9
+ * @param input The string or Buffer content to inspect.
10
+ * @returns True if Markdown syntax is suspected, false otherwise.
11
+ */ function isMarkdown(input) {
12
+ if (input == null) {
13
+ return false;
14
+ }
15
+ // Convert Buffer to string if necessary
16
+ const content = typeof input === 'string' ? input : input.toString(DEFAULT_CHARACTER_ENCODING);
17
+ if (!content || content.trim() === '') {
18
+ return false; // Empty string is not considered Markdown
19
+ }
20
+ // Check for common Markdown patterns in the entire content
21
+ if (markdownRegex.test(content)) {
22
+ return true;
23
+ }
24
+ // Fallback: Check for a high prevalence of Markdown-like list/header starters
25
+ // or thematic breaks, or code blocks.
26
+ // We'll consider up to the first ~2000 characters, roughly equivalent to the byte check.
27
+ const effectiveContent = content.length > 2000 ? content.substring(0, 2000) : content;
28
+ const lines = effectiveContent.split('\n');
29
+ let markdownFeatureCount = 0;
30
+ const featurePatterns = [
31
+ /^#+\s+.+/,
32
+ /^\s*[*+-]\s+.+/,
33
+ /^\s*>\s+.+/,
34
+ /\[.+\]\(.+\)/,
35
+ /!\[.+\]\(.+\)/,
36
+ /`{1,3}[^`]+`{1,3}/,
37
+ /^\s*_{3,}\s*$/,
38
+ /^\s*-{3,}\s*$/,
39
+ /^\s*\*{3,}\s*$/
40
+ ];
41
+ for (const line of lines){
42
+ // Stop checking if we have already found enough features to be confident.
43
+ // This is a small optimization for very long inputs that are clearly markdown early on.
44
+ if (markdownFeatureCount >= 2 && lines.length > 10) {
45
+ const significantLineCountEarly = Math.min(lines.indexOf(line) + 1, 20);
46
+ if (significantLineCountEarly > 0 && markdownFeatureCount / significantLineCountEarly > 0.1) {
47
+ return true;
48
+ }
49
+ }
50
+ for (const pattern of featurePatterns){
51
+ if (pattern.test(line.trim())) {
52
+ markdownFeatureCount++;
53
+ break; // Count each line only once
54
+ }
55
+ }
56
+ }
57
+ // If more than 5% of the first few lines (up to 20 lines or all lines if fewer)
58
+ // show markdown features, or if there are at least 2 distinct features in short texts,
59
+ // consider it Markdown.
60
+ const significantLineCount = Math.min(lines.length, 20);
61
+ if (significantLineCount > 0) {
62
+ // Calculate the exact threshold percentage
63
+ const thresholdPercentage = markdownFeatureCount / significantLineCount;
64
+ // Check against the 5% threshold (0.05)
65
+ // Using >= 0.05 exactly matches 5%, > 0.05 requires more than 5%
66
+ if (thresholdPercentage >= 0.05 + 0.0001) {
67
+ return true;
68
+ }
69
+ // Other conditions for returning true
70
+ if (markdownFeatureCount >= 1 && significantLineCount <= 5 || markdownFeatureCount >= 2) {
71
+ return true;
72
+ }
73
+ }
74
+ return false;
75
+ } // Example usage (optional, for testing):
76
+ // function testIsMarkdownString() {
77
+ // console.log('--- Testing isMarkdownString ---');
78
+ // const markdown1 = '# Hello World\\nThis is a test.';
79
+ // console.log(`Test 1 (Header): "${markdown1.substring(0,10)}..." -> ${isMarkdownString(markdown1)}`); // true
80
+ // const markdown2 = '* Item 1\\n* Item 2';
81
+ // console.log(`Test 2 (List): "${markdown2.substring(0,10)}..." -> ${isMarkdownString(markdown2)}`); // true
82
+ // const markdown3 = '[Google](https://google.com)';
83
+ // console.log(`Test 3 (Link): "${markdown3.substring(0,15)}..." -> ${isMarkdownString(markdown3)}`); // true
84
+ // const markdown4 = '> This is a quote.';
85
+ // console.log(`Test 4 (Blockquote): "${markdown4.substring(0,10)}..." -> ${isMarkdownString(markdown4)}`); // true
86
+ // const markdown5 = '```javascript\\nconsole.log("hello");\\n```';
87
+ // console.log(`Test 5 (Code block): "${markdown5.substring(0,15)}..." -> ${isMarkdownString(markdown5)}`); // true
88
+ // const text1 = 'This is a plain text string.';
89
+ // console.log(`Test 6 (Plain text): "${text1.substring(0,10)}..." -> ${isMarkdownString(text1)}`); // false
90
+ // const text2 = 'hello_world.this_is_a_test_string_with_underscores_but_not_markdown_thematic_break';
91
+ // console.log(`Test 7 (Long non-markdown): "${text2.substring(0,10)}..." -> ${isMarkdownString(text2)}`); // false
92
+ // const text3 = '<xml><tag>value</tag></xml>';
93
+ // console.log(`Test 8 (XML): "${text3.substring(0,10)}..." -> ${isMarkdownString(text3)}`); // false
94
+ // const shortMarkdown = '# H';
95
+ // console.log(`Test 9 (Short Markdown): "${shortMarkdown}" -> ${isMarkdownString(shortMarkdown)}`); // true
96
+ // const shortNonMarkdown = 'Hello';
97
+ // console.log(`Test 10 (Short Non-Markdown): "${shortNonMarkdown}" -> ${isMarkdownString(shortNonMarkdown)}`); // false
98
+ // const emptyString = '';
99
+ // console.log(`Test 11 (Empty string): "" -> ${isMarkdownString(emptyString)}`); // false
100
+ // const whitespaceString = ' \t \n ';
101
+ // console.log(`Test 12 (Whitespace string): "${whitespaceString.substring(0,5)}..." -> ${isMarkdownString(whitespaceString)}`); // false
102
+ // const markdownWithManyFeatures = `# Title\\n\\n* list\\n* list2\\n\\n> quote here\\n\\n\`\`\`\\ncode\\n\`\`\`\\n\\nnormal text paragraph with a [link](url).\n---\nAnother paragraph.\nThis is just a test string to see how it performs with multiple markdown features present.\nHello world this is a very long line that does not contain any markdown syntax at all, it is just plain text that goes on and on.\n* Another list item\n* And another one\n# Another Header\n## Subheader\nThis is fun.\nOkay I think this is enough.\nFinal line.\nAnother final line.\nOne more for good measure.\nOkay that should be enough lines to test the early exit.\n`;
103
+ // console.log(`Test 13 (Many Features): "${markdownWithManyFeatures.substring(0,10)}..." -> ${isMarkdownString(markdownWithManyFeatures)}`); // true
104
+ // const htmlLike = '<div><p>Hello</p><ul><li>item</li></ul></div>';
105
+ // console.log(`Test 14 (HTML-like): "${htmlLike.substring(0,10)}..." -> ${isMarkdownString(htmlLike)}`); // false
106
+ // console.log('--- End Testing ---');
107
+ // }
108
+ // testIsMarkdownString();
109
+
110
+ export { isMarkdown };
111
+ //# sourceMappingURL=markdown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown.js","sources":["../../src/util/markdown.ts"],"sourcesContent":["import { DEFAULT_CHARACTER_ENCODING } from \"../constants\";\n\n// Heuristic to check for Markdown syntax. This is not a full parser.\n// It looks for common Markdown patterns.\nconst markdownRegex = /^(#+\\s|\\*\\s|-\\s|\\+\\s|>\\s|\\[.*\\]\\(.*\\)|```|~~~|---\\\\s*$)/m;\n\n/**\n * Inspects a string to see if it likely contains Markdown syntax.\n *\n * @param input The string or Buffer content to inspect.\n * @returns True if Markdown syntax is suspected, false otherwise.\n */\nexport function isMarkdown(input: string | Buffer): boolean {\n if (input == null) {\n return false;\n }\n // Convert Buffer to string if necessary\n const content = typeof input === 'string' ? input : input.toString(DEFAULT_CHARACTER_ENCODING);\n if (!content || content.trim() === '') {\n return false; // Empty string is not considered Markdown\n }\n\n // Check for common Markdown patterns in the entire content\n if (markdownRegex.test(content)) {\n return true;\n }\n\n // Fallback: Check for a high prevalence of Markdown-like list/header starters\n // or thematic breaks, or code blocks.\n // We'll consider up to the first ~2000 characters, roughly equivalent to the byte check.\n const effectiveContent = content.length > 2000 ? content.substring(0, 2000) : content;\n const lines = effectiveContent.split('\\n');\n let markdownFeatureCount = 0;\n const featurePatterns = [\n /^#+\\s+.+/, // Headers (e.g., # Heading)\n /^\\s*[*+-]\\s+.+/, // List items (e.g., * item, - item, + item)\n /^\\s*>\\s+.+/, // Blockquotes (e.g., > quote)\n /\\[.+\\]\\(.+\\)/, // Links (e.g., [text](url))\n /!\\[.+\\]\\(.+\\)/, // Images (e.g., ![alt](src))\n /`{1,3}[^`]+`{1,3}/, // Inline code (e.g., `code`) or code blocks (```code```)\n /^\\s*_{3,}\\s*$/, // Thematic breaks (e.g., ---, ***, ___)\n /^\\s*-{3,}\\s*$/,\n /^\\s*\\*{3,}\\s*$/\n ];\n\n for (const line of lines) {\n // Stop checking if we have already found enough features to be confident.\n // This is a small optimization for very long inputs that are clearly markdown early on.\n if (markdownFeatureCount >= 2 && lines.length > 10) { // Heuristic threshold\n const significantLineCountEarly = Math.min(lines.indexOf(line) + 1, 20);\n if (significantLineCountEarly > 0 && markdownFeatureCount / significantLineCountEarly > 0.1) {\n return true;\n }\n }\n\n for (const pattern of featurePatterns) {\n if (pattern.test(line.trim())) {\n markdownFeatureCount++;\n break; // Count each line only once\n }\n }\n }\n\n // If more than 5% of the first few lines (up to 20 lines or all lines if fewer)\n // show markdown features, or if there are at least 2 distinct features in short texts,\n // consider it Markdown.\n const significantLineCount = Math.min(lines.length, 20);\n if (significantLineCount > 0) {\n // Calculate the exact threshold percentage\n const thresholdPercentage = markdownFeatureCount / significantLineCount;\n\n // Check against the 5% threshold (0.05)\n // Using >= 0.05 exactly matches 5%, > 0.05 requires more than 5%\n if (thresholdPercentage >= 0.05 + 0.0001) { // Adding a small epsilon to ensure exactly 5% passes but just below fails\n return true;\n }\n\n // Other conditions for returning true\n if ((markdownFeatureCount >= 1 && significantLineCount <= 5) || markdownFeatureCount >= 2) {\n return true;\n }\n }\n\n return false;\n}\n\n// Example usage (optional, for testing):\n// function testIsMarkdownString() {\n// console.log('--- Testing isMarkdownString ---');\n// const markdown1 = '# Hello World\\\\nThis is a test.';\n// console.log(`Test 1 (Header): \"${markdown1.substring(0,10)}...\" -> ${isMarkdownString(markdown1)}`); // true\n\n// const markdown2 = '* Item 1\\\\n* Item 2';\n// console.log(`Test 2 (List): \"${markdown2.substring(0,10)}...\" -> ${isMarkdownString(markdown2)}`); // true\n\n// const markdown3 = '[Google](https://google.com)';\n// console.log(`Test 3 (Link): \"${markdown3.substring(0,15)}...\" -> ${isMarkdownString(markdown3)}`); // true\n\n// const markdown4 = '> This is a quote.';\n// console.log(`Test 4 (Blockquote): \"${markdown4.substring(0,10)}...\" -> ${isMarkdownString(markdown4)}`); // true\n\n// const markdown5 = '```javascript\\\\nconsole.log(\"hello\");\\\\n```';\n// console.log(`Test 5 (Code block): \"${markdown5.substring(0,15)}...\" -> ${isMarkdownString(markdown5)}`); // true\n\n// const text1 = 'This is a plain text string.';\n// console.log(`Test 6 (Plain text): \"${text1.substring(0,10)}...\" -> ${isMarkdownString(text1)}`); // false\n\n// const text2 = 'hello_world.this_is_a_test_string_with_underscores_but_not_markdown_thematic_break';\n// console.log(`Test 7 (Long non-markdown): \"${text2.substring(0,10)}...\" -> ${isMarkdownString(text2)}`); // false\n\n// const text3 = '<xml><tag>value</tag></xml>';\n// console.log(`Test 8 (XML): \"${text3.substring(0,10)}...\" -> ${isMarkdownString(text3)}`); // false\n\n// const shortMarkdown = '# H';\n// console.log(`Test 9 (Short Markdown): \"${shortMarkdown}\" -> ${isMarkdownString(shortMarkdown)}`); // true\n\n// const shortNonMarkdown = 'Hello';\n// console.log(`Test 10 (Short Non-Markdown): \"${shortNonMarkdown}\" -> ${isMarkdownString(shortNonMarkdown)}`); // false\n\n// const emptyString = '';\n// console.log(`Test 11 (Empty string): \"\" -> ${isMarkdownString(emptyString)}`); // false\n\n// const whitespaceString = ' \\t \\n ';\n// console.log(`Test 12 (Whitespace string): \"${whitespaceString.substring(0,5)}...\" -> ${isMarkdownString(whitespaceString)}`); // false\n\n// const markdownWithManyFeatures = `# Title\\\\n\\\\n* list\\\\n* list2\\\\n\\\\n> quote here\\\\n\\\\n\\`\\`\\`\\\\ncode\\\\n\\`\\`\\`\\\\n\\\\nnormal text paragraph with a [link](url).\\n---\\nAnother paragraph.\\nThis is just a test string to see how it performs with multiple markdown features present.\\nHello world this is a very long line that does not contain any markdown syntax at all, it is just plain text that goes on and on.\\n* Another list item\\n* And another one\\n# Another Header\\n## Subheader\\nThis is fun.\\nOkay I think this is enough.\\nFinal line.\\nAnother final line.\\nOne more for good measure.\\nOkay that should be enough lines to test the early exit.\\n`;\n// console.log(`Test 13 (Many Features): \"${markdownWithManyFeatures.substring(0,10)}...\" -> ${isMarkdownString(markdownWithManyFeatures)}`); // true\n\n// const htmlLike = '<div><p>Hello</p><ul><li>item</li></ul></div>';\n// console.log(`Test 14 (HTML-like): \"${htmlLike.substring(0,10)}...\" -> ${isMarkdownString(htmlLike)}`); // false\n\n// console.log('--- End Testing ---');\n// }\n\n// testIsMarkdownString();\n"],"names":["markdownRegex","isMarkdown","input","content","toString","DEFAULT_CHARACTER_ENCODING","trim","test","effectiveContent","length","substring","lines","split","markdownFeatureCount","featurePatterns","line","significantLineCountEarly","Math","min","indexOf","pattern","significantLineCount","thresholdPercentage"],"mappings":";;AAEA;AACA;AACA,MAAMA,aAAgB,GAAA,0DAAA;AAEtB;;;;;IAMO,SAASC,UAAAA,CAAWC,KAAsB,EAAA;AAC7C,IAAA,IAAI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CAAA,EAAA;AAC3BO,gBAAAA,oBAAAA,EAAAA;AACA,gBAAA,MAAA;AACJ;AACJ;AACJ;;;;AAKA,IAAA,MAAMQ,uBAAuBJ,IAAKC,CAAAA,GAAG,CAACP,KAAAA,CAAMF,MAAM,EAAE,EAAA,CAAA;AACpD,IAAA,IAAIY,uBAAuB,CAAG,EAAA;;AAE1B,QAAA,MAAMC,sBAAsBT,oBAAuBQ,GAAAA,oBAAAA;;;QAInD,IAAIC,mBAAAA,IAAuB,OAAO,MAAQ,EAAA;YACtC,OAAO,IAAA;AACX;;AAGA,QAAA,IAAI,oBAAyB,IAAA,CAAA,IAAKD,oBAAwB,IAAA,CAAA,IAAMR,wBAAwB,CAAG,EAAA;YACvF,OAAO,IAAA;AACX;AACJ;IAEA,OAAO,KAAA;AACX,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,155 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const fs = require('fs');
6
+ const glob = require('glob');
7
+ const path = require('path');
8
+ const crypto = require('crypto');
9
+
10
+ function _interopNamespaceDefault(e) {
11
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
12
+ if (e) {
13
+ for (const k in e) {
14
+ if (k !== 'default') {
15
+ const d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: () => e[k]
19
+ });
20
+ }
21
+ }
22
+ }
23
+ n.default = e;
24
+ return Object.freeze(n);
25
+ }
26
+
27
+ const fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
28
+
29
+ // eslint-disable-next-line no-restricted-imports
30
+ const create = (params)=>{
31
+ // eslint-disable-next-line no-console
32
+ const log = params.log || console.log;
33
+ const exists = async (path)=>{
34
+ try {
35
+ await fs__namespace.promises.stat(path);
36
+ return true;
37
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
38
+ } catch (error) {
39
+ return false;
40
+ }
41
+ };
42
+ const isDirectory = async (path)=>{
43
+ const stats = await fs__namespace.promises.stat(path);
44
+ if (!stats.isDirectory()) {
45
+ log(`${path} is not a directory`);
46
+ return false;
47
+ }
48
+ return true;
49
+ };
50
+ const isFile = async (path)=>{
51
+ const stats = await fs__namespace.promises.stat(path);
52
+ if (!stats.isFile()) {
53
+ log(`${path} is not a file`);
54
+ return false;
55
+ }
56
+ return true;
57
+ };
58
+ const isReadable = async (path)=>{
59
+ try {
60
+ await fs__namespace.promises.access(path, fs__namespace.constants.R_OK);
61
+ } catch (error) {
62
+ log(`${path} is not readable: %s %s`, error.message, error.stack);
63
+ return false;
64
+ }
65
+ return true;
66
+ };
67
+ const isWritable = async (path)=>{
68
+ try {
69
+ await fs__namespace.promises.access(path, fs__namespace.constants.W_OK);
70
+ } catch (error) {
71
+ log(`${path} is not writable: %s %s`, error.message, error.stack);
72
+ return false;
73
+ }
74
+ return true;
75
+ };
76
+ const isFileReadable = async (path)=>{
77
+ return await exists(path) && await isFile(path) && await isReadable(path);
78
+ };
79
+ const isDirectoryWritable = async (path)=>{
80
+ return await exists(path) && await isDirectory(path) && await isWritable(path);
81
+ };
82
+ const isDirectoryReadable = async (path)=>{
83
+ return await exists(path) && await isDirectory(path) && await isReadable(path);
84
+ };
85
+ const createDirectory = async (path)=>{
86
+ try {
87
+ await fs__namespace.promises.mkdir(path, {
88
+ recursive: true
89
+ });
90
+ } catch (mkdirError) {
91
+ throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);
92
+ }
93
+ };
94
+ const readFile = async (path, encoding)=>{
95
+ return await fs__namespace.promises.readFile(path, {
96
+ encoding: encoding
97
+ });
98
+ };
99
+ const writeFile = async (path, data, encoding)=>{
100
+ await fs__namespace.promises.writeFile(path, data, {
101
+ encoding: encoding
102
+ });
103
+ };
104
+ const forEachFileIn = async (directory, callback, options = {
105
+ pattern: '*.*'
106
+ })=>{
107
+ try {
108
+ let filesProcessed = 0;
109
+ const files = await glob.glob(options.pattern, {
110
+ cwd: directory,
111
+ nodir: true
112
+ });
113
+ for (const file of files){
114
+ await callback(path.join(directory, file));
115
+ filesProcessed++;
116
+ if (options.limit && filesProcessed >= options.limit) {
117
+ log(`Reached limit of ${options.limit} files, stopping`);
118
+ break;
119
+ }
120
+ }
121
+ } catch (err) {
122
+ throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);
123
+ }
124
+ };
125
+ const readStream = async (path)=>{
126
+ return fs__namespace.createReadStream(path);
127
+ };
128
+ const hashFile = async (path, length)=>{
129
+ const file = await readFile(path, 'utf8');
130
+ return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);
131
+ };
132
+ const listFiles = async (directory)=>{
133
+ return await fs__namespace.promises.readdir(directory);
134
+ };
135
+ return {
136
+ exists,
137
+ isDirectory,
138
+ isFile,
139
+ isReadable,
140
+ isWritable,
141
+ isFileReadable,
142
+ isDirectoryWritable,
143
+ isDirectoryReadable,
144
+ createDirectory,
145
+ readFile,
146
+ readStream,
147
+ writeFile,
148
+ forEachFileIn,
149
+ hashFile,
150
+ listFiles
151
+ };
152
+ };
153
+
154
+ exports.create = create;
155
+ //# sourceMappingURL=storage.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.cjs","sources":["../../src/util/storage.ts"],"sourcesContent":["// eslint-disable-next-line no-restricted-imports\nimport * as fs from 'fs';\nimport { glob } from 'glob';\nimport path from 'path';\nimport crypto from 'crypto';\n/**\n * This module exists to isolate filesystem operations from the rest of the codebase.\n * This makes testing easier by avoiding direct fs mocking in jest configuration.\n * \n * Additionally, abstracting storage operations allows for future flexibility - \n * this export utility may need to work with storage systems other than the local filesystem\n * (e.g. S3, Google Cloud Storage, etc).\n */\n\nexport interface Utility {\n exists: (path: string) => Promise<boolean>;\n isDirectory: (path: string) => Promise<boolean>;\n isFile: (path: string) => Promise<boolean>;\n isReadable: (path: string) => Promise<boolean>;\n isWritable: (path: string) => Promise<boolean>;\n isFileReadable: (path: string) => Promise<boolean>;\n isDirectoryWritable: (path: string) => Promise<boolean>;\n isDirectoryReadable: (path: string) => Promise<boolean>;\n createDirectory: (path: string) => Promise<void>;\n readFile: (path: string, encoding: string) => Promise<string>;\n readStream: (path: string) => Promise<fs.ReadStream>;\n writeFile: (path: string, data: string | Buffer, encoding: string) => Promise<void>;\n forEachFileIn: (directory: string, callback: (path: string) => Promise<void>, options?: { pattern: string, limit?: number }) => Promise<void>;\n hashFile: (path: string, length: number) => Promise<string>;\n listFiles: (directory: string) => Promise<string[]>;\n}\n\nexport const create = (params: { log?: (message: string, ...args: any[]) => void }): Utility => {\n\n // eslint-disable-next-line no-console\n const log = params.log || console.log;\n\n const exists = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.stat(path);\n return true;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error: any) {\n return false;\n }\n }\n\n const isDirectory = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isDirectory()) {\n log(`${path} is not a directory`);\n return false;\n }\n return true;\n }\n\n const isFile = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isFile()) {\n log(`${path} is not a file`);\n return false;\n }\n return true;\n }\n\n const isReadable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.R_OK);\n } catch (error: any) {\n log(`${path} is not readable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isWritable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.W_OK);\n } catch (error: any) {\n log(`${path} is not writable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isFileReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isFile(path) && await isReadable(path);\n }\n\n const isDirectoryWritable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isWritable(path);\n }\n\n const isDirectoryReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isReadable(path);\n }\n\n const createDirectory = async (path: string): Promise<void> => {\n try {\n await fs.promises.mkdir(path, { recursive: true });\n } catch (mkdirError: any) {\n throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);\n }\n }\n\n const readFile = async (path: string, encoding: string): Promise<string> => {\n return await fs.promises.readFile(path, { encoding: encoding as BufferEncoding });\n }\n\n const writeFile = async (path: string, data: string | Buffer, encoding: string): Promise<void> => {\n await fs.promises.writeFile(path, data, { encoding: encoding as BufferEncoding });\n }\n\n const forEachFileIn = async (\n directory: string,\n callback: (file: string) => Promise<void>,\n options: { pattern: string | string[], limit?: number } = { pattern: '*.*' },\n ): Promise<void> => {\n try {\n let filesProcessed = 0;\n const files = await glob(options.pattern, { cwd: directory, nodir: true });\n for (const file of files) {\n await callback(path.join(directory, file));\n filesProcessed++;\n if (options.limit && filesProcessed >= options.limit) {\n log(`Reached limit of ${options.limit} files, stopping`);\n break;\n }\n }\n } catch (err: any) {\n throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);\n }\n }\n\n const readStream = async (path: string): Promise<fs.ReadStream> => {\n return fs.createReadStream(path);\n }\n\n const hashFile = async (path: string, length: number): Promise<string> => {\n const file = await readFile(path, 'utf8');\n return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);\n }\n\n const listFiles = async (directory: string): Promise<string[]> => {\n return await fs.promises.readdir(directory);\n }\n\n return {\n exists,\n isDirectory,\n isFile,\n isReadable,\n isWritable,\n isFileReadable,\n isDirectoryWritable,\n isDirectoryReadable,\n createDirectory,\n readFile,\n readStream,\n writeFile,\n forEachFileIn,\n hashFile,\n listFiles,\n };\n}"],"names":["create","params","log","console","exists","path","fs","promises","stat","error","isDirectory","stats","isFile","isReadable","access","constants","R_OK","message","stack","isWritable","W_OK","isFileReadable","isDirectoryWritable","isDirectoryReadable","createDirectory","mkdir","recursive","mkdirError","Error","readFile","encoding","writeFile","data","forEachFileIn","directory","callback","options","pattern","filesProcessed","files","glob","cwd","nodir","file","join","limit","err","readStream","createReadStream","hashFile","length","crypto","createHash","update","digest","slice","listFiles","readdir"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAgCO,MAAMA,SAAS,CAACC,MAAAA,GAAAA;;AAGnB,IAAA,MAAMC,GAAMD,GAAAA,MAAAA,CAAOC,GAAG,IAAIC,QAAQD,GAAG;AAErC,IAAA,MAAME,SAAS,OAAOC,IAAAA,GAAAA;QAClB,IAAI;AACA,YAAA,MAAMC,aAAGC,CAAAA,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;YACvB,OAAO,IAAA;;AAEX,SAAA,CAAE,OAAOI,KAAY,EAAA;YACjB,OAAO,KAAA;AACX;AACJ,KAAA;AAEA,IAAA,MAAMC,cAAc,OAAOL,IAAAA,GAAAA;AACvB,QAAA,MAAMM,QAAQ,MAAML,aAAAA,CAAGC,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;QACrC,IAAI,CAACM,KAAMD,CAAAA,WAAW,EAAI,EAAA;YACtBR,GAAI,CAAA,CAAA,EAAGG,IAAK,CAAA,mBAAmB,CAAC,CAAA;YAChC,OAAO,KAAA;AACX;QACA,OAAO,IAAA;AACX,KAAA;AAEA,IAAA,MAAMO,SAAS,OAAOP,IAAAA,GAAAA;AAClB,QAAA,MAAMM,QAAQ,MAAML,aAAAA,CAAGC,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;QACrC,IAAI,CAACM,KAAMC,CAAAA,MAAM,EAAI,EAAA;YACjBV,GAAI,CAAA,CAAA,EAAGG,IAAK,CAAA,cAAc,CAAC,CAAA;YAC3B,OAAO,KAAA;AACX;QACA,OAAO,IAAA;AACX,KAAA;AAEA,IAAA,MAAMQ,aAAa,OAAOR,IAAAA,GAAAA;QACtB,IAAI;YACA,MAAMC,aAAAA,CAAGC,QAAQ,CAACO,MAAM,CAACT,IAAMC,EAAAA,aAAAA,CAAGS,SAAS,CAACC,IA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AA;AACAK,QAAAA,QAAAA;AACAkB,QAAAA,UAAAA;AACAhB,QAAAA,SAAAA;AACAE,QAAAA,aAAAA;AACAgB,QAAAA,QAAAA;AACAO,QAAAA;AACJ,KAAA;AACJ;;;;"}
@@ -0,0 +1,32 @@
1
+ import * as fs from 'fs';
2
+ /**
3
+ * This module exists to isolate filesystem operations from the rest of the codebase.
4
+ * This makes testing easier by avoiding direct fs mocking in jest configuration.
5
+ *
6
+ * Additionally, abstracting storage operations allows for future flexibility -
7
+ * this export utility may need to work with storage systems other than the local filesystem
8
+ * (e.g. S3, Google Cloud Storage, etc).
9
+ */
10
+ export interface Utility {
11
+ exists: (path: string) => Promise<boolean>;
12
+ isDirectory: (path: string) => Promise<boolean>;
13
+ isFile: (path: string) => Promise<boolean>;
14
+ isReadable: (path: string) => Promise<boolean>;
15
+ isWritable: (path: string) => Promise<boolean>;
16
+ isFileReadable: (path: string) => Promise<boolean>;
17
+ isDirectoryWritable: (path: string) => Promise<boolean>;
18
+ isDirectoryReadable: (path: string) => Promise<boolean>;
19
+ createDirectory: (path: string) => Promise<void>;
20
+ readFile: (path: string, encoding: string) => Promise<string>;
21
+ readStream: (path: string) => Promise<fs.ReadStream>;
22
+ writeFile: (path: string, data: string | Buffer, encoding: string) => Promise<void>;
23
+ forEachFileIn: (directory: string, callback: (path: string) => Promise<void>, options?: {
24
+ pattern: string;
25
+ limit?: number;
26
+ }) => Promise<void>;
27
+ hashFile: (path: string, length: number) => Promise<string>;
28
+ listFiles: (directory: string) => Promise<string[]>;
29
+ }
30
+ export declare const create: (params: {
31
+ log?: (message: string, ...args: any[]) => void;
32
+ }) => Utility;
@@ -0,0 +1,132 @@
1
+ import * as fs from 'fs';
2
+ import { glob } from 'glob';
3
+ import path__default from 'path';
4
+ import crypto from 'crypto';
5
+
6
+ // eslint-disable-next-line no-restricted-imports
7
+ const create = (params)=>{
8
+ // eslint-disable-next-line no-console
9
+ const log = params.log || console.log;
10
+ const exists = async (path)=>{
11
+ try {
12
+ await fs.promises.stat(path);
13
+ return true;
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
+ } catch (error) {
16
+ return false;
17
+ }
18
+ };
19
+ const isDirectory = async (path)=>{
20
+ const stats = await fs.promises.stat(path);
21
+ if (!stats.isDirectory()) {
22
+ log(`${path} is not a directory`);
23
+ return false;
24
+ }
25
+ return true;
26
+ };
27
+ const isFile = async (path)=>{
28
+ const stats = await fs.promises.stat(path);
29
+ if (!stats.isFile()) {
30
+ log(`${path} is not a file`);
31
+ return false;
32
+ }
33
+ return true;
34
+ };
35
+ const isReadable = async (path)=>{
36
+ try {
37
+ await fs.promises.access(path, fs.constants.R_OK);
38
+ } catch (error) {
39
+ log(`${path} is not readable: %s %s`, error.message, error.stack);
40
+ return false;
41
+ }
42
+ return true;
43
+ };
44
+ const isWritable = async (path)=>{
45
+ try {
46
+ await fs.promises.access(path, fs.constants.W_OK);
47
+ } catch (error) {
48
+ log(`${path} is not writable: %s %s`, error.message, error.stack);
49
+ return false;
50
+ }
51
+ return true;
52
+ };
53
+ const isFileReadable = async (path)=>{
54
+ return await exists(path) && await isFile(path) && await isReadable(path);
55
+ };
56
+ const isDirectoryWritable = async (path)=>{
57
+ return await exists(path) && await isDirectory(path) && await isWritable(path);
58
+ };
59
+ const isDirectoryReadable = async (path)=>{
60
+ return await exists(path) && await isDirectory(path) && await isReadable(path);
61
+ };
62
+ const createDirectory = async (path)=>{
63
+ try {
64
+ await fs.promises.mkdir(path, {
65
+ recursive: true
66
+ });
67
+ } catch (mkdirError) {
68
+ throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);
69
+ }
70
+ };
71
+ const readFile = async (path, encoding)=>{
72
+ return await fs.promises.readFile(path, {
73
+ encoding: encoding
74
+ });
75
+ };
76
+ const writeFile = async (path, data, encoding)=>{
77
+ await fs.promises.writeFile(path, data, {
78
+ encoding: encoding
79
+ });
80
+ };
81
+ const forEachFileIn = async (directory, callback, options = {
82
+ pattern: '*.*'
83
+ })=>{
84
+ try {
85
+ let filesProcessed = 0;
86
+ const files = await glob(options.pattern, {
87
+ cwd: directory,
88
+ nodir: true
89
+ });
90
+ for (const file of files){
91
+ await callback(path__default.join(directory, file));
92
+ filesProcessed++;
93
+ if (options.limit && filesProcessed >= options.limit) {
94
+ log(`Reached limit of ${options.limit} files, stopping`);
95
+ break;
96
+ }
97
+ }
98
+ } catch (err) {
99
+ throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);
100
+ }
101
+ };
102
+ const readStream = async (path)=>{
103
+ return fs.createReadStream(path);
104
+ };
105
+ const hashFile = async (path, length)=>{
106
+ const file = await readFile(path, 'utf8');
107
+ return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);
108
+ };
109
+ const listFiles = async (directory)=>{
110
+ return await fs.promises.readdir(directory);
111
+ };
112
+ return {
113
+ exists,
114
+ isDirectory,
115
+ isFile,
116
+ isReadable,
117
+ isWritable,
118
+ isFileReadable,
119
+ isDirectoryWritable,
120
+ isDirectoryReadable,
121
+ createDirectory,
122
+ readFile,
123
+ readStream,
124
+ writeFile,
125
+ forEachFileIn,
126
+ hashFile,
127
+ listFiles
128
+ };
129
+ };
130
+
131
+ export { create };
132
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sources":["../../src/util/storage.ts"],"sourcesContent":["// eslint-disable-next-line no-restricted-imports\nimport * as fs from 'fs';\nimport { glob } from 'glob';\nimport path from 'path';\nimport crypto from 'crypto';\n/**\n * This module exists to isolate filesystem operations from the rest of the codebase.\n * This makes testing easier by avoiding direct fs mocking in jest configuration.\n * \n * Additionally, abstracting storage operations allows for future flexibility - \n * this export utility may need to work with storage systems other than the local filesystem\n * (e.g. S3, Google Cloud Storage, etc).\n */\n\nexport interface Utility {\n exists: (path: string) => Promise<boolean>;\n isDirectory: (path: string) => Promise<boolean>;\n isFile: (path: string) => Promise<boolean>;\n isReadable: (path: string) => Promise<boolean>;\n isWritable: (path: string) => Promise<boolean>;\n isFileReadable: (path: string) => Promise<boolean>;\n isDirectoryWritable: (path: string) => Promise<boolean>;\n isDirectoryReadable: (path: string) => Promise<boolean>;\n createDirectory: (path: string) => Promise<void>;\n readFile: (path: string, encoding: string) => Promise<string>;\n readStream: (path: string) => Promise<fs.ReadStream>;\n writeFile: (path: string, data: string | Buffer, encoding: string) => Promise<void>;\n forEachFileIn: (directory: string, callback: (path: string) => Promise<void>, options?: { pattern: string, limit?: number }) => Promise<void>;\n hashFile: (path: string, length: number) => Promise<string>;\n listFiles: (directory: string) => Promise<string[]>;\n}\n\nexport const create = (params: { log?: (message: string, ...args: any[]) => void }): Utility => {\n\n // eslint-disable-next-line no-console\n const log = params.log || console.log;\n\n const exists = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.stat(path);\n return true;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error: any) {\n return false;\n }\n }\n\n const isDirectory = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isDirectory()) {\n log(`${path} is not a directory`);\n return false;\n }\n return true;\n }\n\n const isFile = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isFile()) {\n log(`${path} is not a file`);\n return false;\n }\n return true;\n }\n\n const isReadable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.R_OK);\n } catch (error: any) {\n log(`${path} is not readable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isWritable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.W_OK);\n } catch (error: any) {\n log(`${path} is not writable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isFileReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isFile(path) && await isReadable(path);\n }\n\n const isDirectoryWritable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isWritable(path);\n }\n\n const isDirectoryReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isReadable(path);\n }\n\n const createDirectory = async (path: string): Promise<void> => {\n try {\n await fs.promises.mkdir(path, { recursive: true });\n } catch (mkdirError: any) {\n throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);\n }\n }\n\n const readFile = async (path: string, encoding: string): Promise<string> => {\n return await fs.promises.readFile(path, { encoding: encoding as BufferEncoding });\n }\n\n const writeFile = async (path: string, data: string | Buffer, encoding: string): Promise<void> => {\n await fs.promises.writeFile(path, data, { encoding: encoding as BufferEncoding });\n }\n\n const forEachFileIn = async (\n directory: string,\n callback: (file: string) => Promise<void>,\n options: { pattern: string | string[], limit?: number } = { pattern: '*.*' },\n ): Promise<void> => {\n try {\n let filesProcessed = 0;\n const files = await glob(options.pattern, { cwd: directory, nodir: true });\n for (const file of files) {\n await callback(path.join(directory, file));\n filesProcessed++;\n if (options.limit && filesProcessed >= options.limit) {\n log(`Reached limit of ${options.limit} files, stopping`);\n break;\n }\n }\n } catch (err: any) {\n throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);\n }\n }\n\n const readStream = async (path: string): Promise<fs.ReadStream> => {\n return fs.createReadStream(path);\n }\n\n const hashFile = async (path: string, length: number): Promise<string> => {\n const file = await readFile(path, 'utf8');\n return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);\n }\n\n const listFiles = async (directory: string): Promise<string[]> => {\n return await fs.promises.readdir(directory);\n }\n\n return {\n exists,\n isDirectory,\n isFile,\n isReadable,\n isWritable,\n isFileReadable,\n isDirectoryWritable,\n isDirectoryReadable,\n createDirectory,\n readFile,\n readStream,\n writeFile,\n forEachFileIn,\n hashFile,\n listFiles,\n };\n}"],"names":["create","params","log","console","exists","path","fs","promises","stat","error","isDirectory","stats","isFile","isReadable","access","constants","R_OK","message","stack","isWritable","W_OK","isFileReadable","isDirectoryWritable","isDirectoryReadable","createDirectory","mkdir","recursive","mkdirError","Error","readFile","encoding","writeFile","data","forEachFileIn","directory","callback","options","pattern","filesProcessed","files","glob","cwd","nodir","file","join","limit","err","readStream","createReadStream","hashFile","length","crypto","createHash","update","digest","slice","listFiles","readdir"],"mappings":";;;;;AAAA;AAgCO,MAAMA,SAAS,CAACC,MAAAA,GAAAA;;AAGnB,IAAA,MAAMC,GAAMD,GAAAA,MAAAA,CAAOC,GAAG,IAAIC,QAAQD,GAAG;AAErC,IAAA,MAAME,SAAS,OAAOC,IAAAA,GAAAA;QAClB,IAAI;AACA,YAAA,MAAMC,EAAGC,CAAAA,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;YACvB,OAAO,IAAA;;AAEX,SAAA,CAAE,OAAOI,KAAY,EAAA;YACjB,OAAO,KAAA;AACX;AACJ,KAAA;AAEA,IAAA,MAAMC,cAAc,OAAOL,IAAAA,GAAAA;AACvB,QAAA,MAAMM,QAAQ,MAAML,EAAAA,CAAGC,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;QACrC,IAAI,CAACM,KAAMD,CAAAA,WAAW,EAAI,EAAA;YACtBR,GAAI,CAAA,CAAA,EAAGG,IAAK,CAAA,mBAAmB,CAAC,CAAA;YAChC,OAAO,KAAA;AACX;QACA,OAAO,IAAA;AACX,KAAA;AAEA,IAAA,MAAMO,SAAS,OAAOP,IAAAA,GAAAA;AAClB,QAAA,MAAMM,QAAQ,MAAML,EAAAA,CAAGC,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;QACrC,IAAI,CAACM,KAAMC,CAAAA,MAAM,EAAI,EAAA;YACjBV,GAAI,CAAA,CAAA,EAAGG,IAAK,CAAA,cAAc,CAAC,CAAA;YAC3B,OAAO,KAAA;AACX;QACA,OAAO,IAAA;AACX,KAAA;AAEA,IAAA,MAAMQ,aAAa,OAAOR,IAAAA,GAAAA;QACtB,IAAI;YACA,MAAMC,EAAAA,CAAGC,QAAQ,CAACO,MAAM,CAACT,IAAMC,EAAAA,EAAAA,CAAGS,SAAS,CAACC,IAAI,CAAA;AACpD,SAAA,CAAE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AkB,QAAAA,UAAAA;AACAhB,QAAAA,SAAAA;AACAE,QAAAA,aAAAA;AACAgB,QAAAA,QAAAA;AACAO,QAAAA;AACJ,KAAA;AACJ;;;;"}
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('../constants.cjs');
6
+
7
+ // Returns true if the input is likely text, false if likely binary
8
+ function isText(input) {
9
+ let buf;
10
+ if (typeof input === 'string') {
11
+ buf = Buffer.from(input, constants.DEFAULT_CHARACTER_ENCODING);
12
+ } else {
13
+ buf = input;
14
+ }
15
+ // Empty buffers are considered text
16
+ if (buf.length === 0) {
17
+ return true;
18
+ }
19
+ // If the buffer contains null bytes, it's likely binary
20
+ if (buf.includes(0)) {
21
+ return false;
22
+ }
23
+ // For UTF-8 encoded text (including emoji and international characters),
24
+ // convert to string first and check if there are non-printable characters
25
+ const str = buf.toString(constants.DEFAULT_CHARACTER_ENCODING);
26
+ // Count the number of non-printable ASCII characters (excluding common whitespace)
27
+ let nonPrintable = 0;
28
+ const len = Math.min(str.length, 512); // Only check the first 512 characters for performance
29
+ for(let i = 0; i < len; i++){
30
+ const charCode = str.charCodeAt(i);
31
+ // Allow: tab (9), line feed (10), carriage return (13), printable ASCII (32-126)
32
+ // Also allow all non-ASCII Unicode characters (charCode > 127)
33
+ if (charCode !== 9 && charCode !== 10 && charCode !== 13 && (charCode < 32 || charCode > 126 && charCode < 128)) {
34
+ nonPrintable++;
35
+ }
36
+ }
37
+ // If more than 10% of the checked characters are non-printable, consider it binary
38
+ return nonPrintable / len < 0.1;
39
+ }
40
+
41
+ exports.isText = isText;
42
+ //# sourceMappingURL=text.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.cjs","sources":["../../src/util/text.ts"],"sourcesContent":["import { DEFAULT_CHARACTER_ENCODING } from \"../constants\";\n\n// Returns true if the input is likely text, false if likely binary\nexport function isText(input: string | Buffer): boolean {\n let buf: Buffer;\n if (typeof input === 'string') {\n buf = Buffer.from(input, DEFAULT_CHARACTER_ENCODING);\n } else {\n buf = input;\n }\n\n // Empty buffers are considered text\n if (buf.length === 0) {\n return true;\n }\n\n // If the buffer contains null bytes, it's likely binary\n if (buf.includes(0)) {\n return false;\n }\n\n // For UTF-8 encoded text (including emoji and international characters),\n // convert to string first and check if there are non-printable characters\n const str = buf.toString(DEFAULT_CHARACTER_ENCODING);\n\n // Count the number of non-printable ASCII characters (excluding common whitespace)\n let nonPrintable = 0;\n const len = Math.min(str.length, 512); // Only check the first 512 characters for performance\n\n for (let i = 0; i < len; i++) {\n const charCode = str.charCodeAt(i);\n // Allow: tab (9), line feed (10), carriage return (13), printable ASCII (32-126)\n // Also allow all non-ASCII Unicode characters (charCode > 127)\n if (\n charCode !== 9 && charCode !== 10 && charCode !== 13 &&\n (charCode < 32 || (charCode > 126 && charCode < 128))\n ) {\n nonPrintable++;\n }\n }\n\n // If more than 10% of the checked characters are non-printable, consider it binary\n return nonPrintable / len < 0.1;\n}\n"],"names":["isText","input","buf","Buffer","from","DEFAULT_CHARACTER_ENCODING","length","includes","str","toString","nonPrintable","len","Math","min","i","charCode","charCodeAt"],"mappings":";;;;;;AAEA;AACO,SAASA,OAAOC,KAAsB,EAAA;IACzC,IAAIC,GAAAA;IACJ,IAAI,OAAOD,UAAU,QAAU,EAAA;QAC3BC,GAAMC,GAAAA,MAAAA,CAAOC,IAAI,CAACH,KAAOI,EAAAA,oCAAAA,CAAAA;KACtB,MAAA;QACHH,GAAMD,GAAAA,KAAAA;AACV;;IAGA,IAAIC,GAAAA,CAAII,MAAM,KAAK,CAAG,EAAA;QAClB,OAAO,IAAA;AACX;;IAGA,IAAIJ,GAAAA,CAAIK,QAAQ,CAAC,CAAI,CAAA,EAAA;QACjB,OAAO,KAAA;AACX;;;IAIA,MAAMC,GAAAA,GAAMN,GAAIO,CAAAA,QAAQ,CAACJ,oCAAAA,CAAAA;;AAGzB,IAAA,IAAIK,YAAe,GAAA,CAAA;IACnB,MAAMC,GAAAA,GAAMC,KAAKC,GAAG,CAACL,IAAIF,MAAM,EAAE;AAEjC,IAAA,IAAK,IAAIQ,CAAAA,GAAI,CAAGA,EAAAA,CAAAA,GAAIH,KAAKG,CAAK,EAAA,CAAA;QAC1B,MAAMC,QAAAA,GAAWP,GAAIQ,CAAAA,UAAU,CAACF,CAAAA,CAAAA;;;AAGhC,QAAA,IACIC,QAAa,KAAA,CAAA,IAAKA,QAAa,KAAA,EAAA,IAAMA,QAAa,KAAA,EAAA,KACjDA,QAAAA,GAAW,EAAOA,IAAAA,QAAAA,GAAW,GAAOA,IAAAA,QAAAA,GAAW,GAAG,CACrD,EAAA;AACEL,YAAAA,YAAAA,EAAAA;AACJ;AACJ;;AAGA,IAAA,OAAOA,eAAeC,GAAM,GAAA,GAAA;AAChC;;;;"}
@@ -0,0 +1 @@
1
+ export declare function isText(input: string | Buffer): boolean;
@@ -0,0 +1,38 @@
1
+ import { DEFAULT_CHARACTER_ENCODING } from '../constants.js';
2
+
3
+ // Returns true if the input is likely text, false if likely binary
4
+ function isText(input) {
5
+ let buf;
6
+ if (typeof input === 'string') {
7
+ buf = Buffer.from(input, DEFAULT_CHARACTER_ENCODING);
8
+ } else {
9
+ buf = input;
10
+ }
11
+ // Empty buffers are considered text
12
+ if (buf.length === 0) {
13
+ return true;
14
+ }
15
+ // If the buffer contains null bytes, it's likely binary
16
+ if (buf.includes(0)) {
17
+ return false;
18
+ }
19
+ // For UTF-8 encoded text (including emoji and international characters),
20
+ // convert to string first and check if there are non-printable characters
21
+ const str = buf.toString(DEFAULT_CHARACTER_ENCODING);
22
+ // Count the number of non-printable ASCII characters (excluding common whitespace)
23
+ let nonPrintable = 0;
24
+ const len = Math.min(str.length, 512); // Only check the first 512 characters for performance
25
+ for(let i = 0; i < len; i++){
26
+ const charCode = str.charCodeAt(i);
27
+ // Allow: tab (9), line feed (10), carriage return (13), printable ASCII (32-126)
28
+ // Also allow all non-ASCII Unicode characters (charCode > 127)
29
+ if (charCode !== 9 && charCode !== 10 && charCode !== 13 && (charCode < 32 || charCode > 126 && charCode < 128)) {
30
+ nonPrintable++;
31
+ }
32
+ }
33
+ // If more than 10% of the checked characters are non-printable, consider it binary
34
+ return nonPrintable / len < 0.1;
35
+ }
36
+
37
+ export { isText };
38
+ //# sourceMappingURL=text.js.map