@cogitator-ai/wasm-tools 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -6
- package/dist/index.d.ts +227 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/compression.d.ts +2 -0
- package/dist/plugins/compression.d.ts.map +1 -0
- package/dist/plugins/compression.js +530 -0
- package/dist/plugins/compression.js.map +1 -0
- package/dist/plugins/csv.d.ts +2 -0
- package/dist/plugins/csv.d.ts.map +1 -0
- package/dist/plugins/csv.js +138 -0
- package/dist/plugins/csv.js.map +1 -0
- package/dist/plugins/datetime.d.ts +2 -0
- package/dist/plugins/datetime.d.ts.map +1 -0
- package/dist/plugins/datetime.js +184 -0
- package/dist/plugins/datetime.js.map +1 -0
- package/dist/plugins/diff.d.ts +2 -0
- package/dist/plugins/diff.d.ts.map +1 -0
- package/dist/plugins/diff.js +187 -0
- package/dist/plugins/diff.js.map +1 -0
- package/dist/plugins/markdown.d.ts +2 -0
- package/dist/plugins/markdown.d.ts.map +1 -0
- package/dist/plugins/markdown.js +203 -0
- package/dist/plugins/markdown.js.map +1 -0
- package/dist/plugins/regex.d.ts +2 -0
- package/dist/plugins/regex.d.ts.map +1 -0
- package/dist/plugins/regex.js +119 -0
- package/dist/plugins/regex.js.map +1 -0
- package/dist/plugins/signing.d.ts +2 -0
- package/dist/plugins/signing.d.ts.map +1 -0
- package/dist/plugins/signing.js +565 -0
- package/dist/plugins/signing.js.map +1 -0
- package/dist/plugins/slug.d.ts +2 -0
- package/dist/plugins/slug.d.ts.map +1 -0
- package/dist/plugins/slug.js +180 -0
- package/dist/plugins/slug.js.map +1 -0
- package/dist/plugins/validation.d.ts +2 -0
- package/dist/plugins/validation.d.ts.map +1 -0
- package/dist/plugins/validation.js +105 -0
- package/dist/plugins/validation.js.map +1 -0
- package/dist/plugins/xml.d.ts +2 -0
- package/dist/plugins/xml.d.ts.map +1 -0
- package/dist/plugins/xml.js +293 -0
- package/dist/plugins/xml.js.map +1 -0
- package/dist/temp/compression.js +614 -0
- package/dist/temp/csv.js +154 -0
- package/dist/temp/datetime.js +196 -0
- package/dist/temp/diff.js +198 -0
- package/dist/temp/markdown.js +220 -0
- package/dist/temp/regex.js +140 -0
- package/dist/temp/signing.js +569 -0
- package/dist/temp/slug.js +198 -0
- package/dist/temp/validation.js +125 -0
- package/dist/temp/xml.js +289 -0
- package/dist/wasm/base64.wasm +0 -0
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/compression.wasm +0 -0
- package/dist/wasm/csv.wasm +0 -0
- package/dist/wasm/datetime.wasm +0 -0
- package/dist/wasm/diff.wasm +0 -0
- package/dist/wasm/hash.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/dist/wasm/markdown.wasm +0 -0
- package/dist/wasm/regex.wasm +0 -0
- package/dist/wasm/signing.wasm +0 -0
- package/dist/wasm/slug.wasm +0 -0
- package/dist/wasm/validation.wasm +0 -0
- package/dist/wasm/xml.wasm +0 -0
- package/package.json +2 -2
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/markdown.ts
|
|
21
|
+
var markdown_exports = {};
|
|
22
|
+
__export(markdown_exports, {
|
|
23
|
+
markdown: () => markdown
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(markdown_exports);
|
|
26
|
+
function escapeHtml(text) {
|
|
27
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
28
|
+
}
|
|
29
|
+
function parseInline(text, sanitize) {
|
|
30
|
+
let result = sanitize ? escapeHtml(text) : text;
|
|
31
|
+
result = result.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">');
|
|
32
|
+
result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
|
|
33
|
+
result = result.replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
34
|
+
result = result.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
|
35
|
+
result = result.replace(/__([^_]+)__/g, "<strong>$1</strong>");
|
|
36
|
+
result = result.replace(/\*([^*]+)\*/g, "<em>$1</em>");
|
|
37
|
+
result = result.replace(/_([^_]+)_/g, "<em>$1</em>");
|
|
38
|
+
result = result.replace(/~~([^~]+)~~/g, "<del>$1</del>");
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
function parseMarkdown(md, sanitize, gfm) {
|
|
42
|
+
const lines = md.split("\n");
|
|
43
|
+
const html = [];
|
|
44
|
+
let inCodeBlock = false;
|
|
45
|
+
let codeBlockContent = [];
|
|
46
|
+
let codeBlockLang = "";
|
|
47
|
+
let inList = false;
|
|
48
|
+
let listType = "ul";
|
|
49
|
+
let inBlockquote = false;
|
|
50
|
+
let blockquoteContent = [];
|
|
51
|
+
let inParagraph = false;
|
|
52
|
+
let paragraphContent = [];
|
|
53
|
+
const flushParagraph = () => {
|
|
54
|
+
if (inParagraph && paragraphContent.length > 0) {
|
|
55
|
+
html.push(`<p>${parseInline(paragraphContent.join(" "), sanitize)}</p>`);
|
|
56
|
+
paragraphContent = [];
|
|
57
|
+
inParagraph = false;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const flushBlockquote = () => {
|
|
61
|
+
if (inBlockquote && blockquoteContent.length > 0) {
|
|
62
|
+
html.push(`<blockquote><p>${parseInline(blockquoteContent.join(" "), sanitize)}</p></blockquote>`);
|
|
63
|
+
blockquoteContent = [];
|
|
64
|
+
inBlockquote = false;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const flushList = () => {
|
|
68
|
+
if (inList) {
|
|
69
|
+
html.push(listType === "ul" ? "</ul>" : "</ol>");
|
|
70
|
+
inList = false;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
for (let i = 0; i < lines.length; i++) {
|
|
74
|
+
const line = lines[i];
|
|
75
|
+
if (line.startsWith("```")) {
|
|
76
|
+
if (inCodeBlock) {
|
|
77
|
+
const content = sanitize ? escapeHtml(codeBlockContent.join("\n")) : codeBlockContent.join("\n");
|
|
78
|
+
html.push(`<pre><code${codeBlockLang ? ` class="language-${codeBlockLang}"` : ""}>${content}</code></pre>`);
|
|
79
|
+
codeBlockContent = [];
|
|
80
|
+
codeBlockLang = "";
|
|
81
|
+
inCodeBlock = false;
|
|
82
|
+
} else {
|
|
83
|
+
flushParagraph();
|
|
84
|
+
flushBlockquote();
|
|
85
|
+
flushList();
|
|
86
|
+
codeBlockLang = line.slice(3).trim();
|
|
87
|
+
inCodeBlock = true;
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (inCodeBlock) {
|
|
92
|
+
codeBlockContent.push(line);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (line.trim() === "") {
|
|
96
|
+
flushParagraph();
|
|
97
|
+
flushBlockquote();
|
|
98
|
+
flushList();
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const headerMatch = line.match(/^(#{1,6})\s+(.+)$/);
|
|
102
|
+
if (headerMatch) {
|
|
103
|
+
flushParagraph();
|
|
104
|
+
flushBlockquote();
|
|
105
|
+
flushList();
|
|
106
|
+
const level = headerMatch[1].length;
|
|
107
|
+
const content = parseInline(headerMatch[2], sanitize);
|
|
108
|
+
html.push(`<h${level}>${content}</h${level}>`);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (/^(-{3,}|\*{3,}|_{3,})$/.test(line.trim())) {
|
|
112
|
+
flushParagraph();
|
|
113
|
+
flushBlockquote();
|
|
114
|
+
flushList();
|
|
115
|
+
html.push("<hr>");
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const blockquoteMatch = line.match(/^>\s?(.*)$/);
|
|
119
|
+
if (blockquoteMatch) {
|
|
120
|
+
flushParagraph();
|
|
121
|
+
flushList();
|
|
122
|
+
inBlockquote = true;
|
|
123
|
+
blockquoteContent.push(blockquoteMatch[1]);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const ulMatch = line.match(/^[-*+]\s+(.+)$/);
|
|
127
|
+
if (ulMatch) {
|
|
128
|
+
flushParagraph();
|
|
129
|
+
flushBlockquote();
|
|
130
|
+
if (!inList || listType !== "ul") {
|
|
131
|
+
flushList();
|
|
132
|
+
html.push("<ul>");
|
|
133
|
+
inList = true;
|
|
134
|
+
listType = "ul";
|
|
135
|
+
}
|
|
136
|
+
html.push(`<li>${parseInline(ulMatch[1], sanitize)}</li>`);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const olMatch = line.match(/^\d+\.\s+(.+)$/);
|
|
140
|
+
if (olMatch) {
|
|
141
|
+
flushParagraph();
|
|
142
|
+
flushBlockquote();
|
|
143
|
+
if (!inList || listType !== "ol") {
|
|
144
|
+
flushList();
|
|
145
|
+
html.push("<ol>");
|
|
146
|
+
inList = true;
|
|
147
|
+
listType = "ol";
|
|
148
|
+
}
|
|
149
|
+
html.push(`<li>${parseInline(olMatch[1], sanitize)}</li>`);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (gfm && line.includes("|")) {
|
|
153
|
+
flushParagraph();
|
|
154
|
+
flushBlockquote();
|
|
155
|
+
flushList();
|
|
156
|
+
const tableLines = [line];
|
|
157
|
+
while (i + 1 < lines.length && lines[i + 1].includes("|")) {
|
|
158
|
+
i++;
|
|
159
|
+
tableLines.push(lines[i]);
|
|
160
|
+
}
|
|
161
|
+
if (tableLines.length >= 2) {
|
|
162
|
+
const headerCells = tableLines[0].split("|").map((c) => c.trim()).filter((c) => c);
|
|
163
|
+
const rows = tableLines.slice(2);
|
|
164
|
+
html.push("<table>");
|
|
165
|
+
html.push("<thead><tr>");
|
|
166
|
+
for (const cell of headerCells) {
|
|
167
|
+
html.push(`<th>${parseInline(cell, sanitize)}</th>`);
|
|
168
|
+
}
|
|
169
|
+
html.push("</tr></thead>");
|
|
170
|
+
if (rows.length > 0) {
|
|
171
|
+
html.push("<tbody>");
|
|
172
|
+
for (const row of rows) {
|
|
173
|
+
const cells = row.split("|").map((c) => c.trim()).filter((c) => c);
|
|
174
|
+
html.push("<tr>");
|
|
175
|
+
for (const cell of cells) {
|
|
176
|
+
html.push(`<td>${parseInline(cell, sanitize)}</td>`);
|
|
177
|
+
}
|
|
178
|
+
html.push("</tr>");
|
|
179
|
+
}
|
|
180
|
+
html.push("</tbody>");
|
|
181
|
+
}
|
|
182
|
+
html.push("</table>");
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
flushBlockquote();
|
|
187
|
+
flushList();
|
|
188
|
+
inParagraph = true;
|
|
189
|
+
paragraphContent.push(line.trim());
|
|
190
|
+
}
|
|
191
|
+
flushParagraph();
|
|
192
|
+
flushBlockquote();
|
|
193
|
+
flushList();
|
|
194
|
+
if (inCodeBlock) {
|
|
195
|
+
const content = sanitize ? escapeHtml(codeBlockContent.join("\n")) : codeBlockContent.join("\n");
|
|
196
|
+
html.push(`<pre><code>${content}</code></pre>`);
|
|
197
|
+
}
|
|
198
|
+
return html.join("\n");
|
|
199
|
+
}
|
|
200
|
+
function markdown() {
|
|
201
|
+
try {
|
|
202
|
+
const inputStr = Host.inputString();
|
|
203
|
+
const input = JSON.parse(inputStr);
|
|
204
|
+
const sanitize = input.options?.sanitize ?? true;
|
|
205
|
+
const gfm = input.options?.gfm ?? true;
|
|
206
|
+
const htmlResult = parseMarkdown(input.markdown, sanitize, gfm);
|
|
207
|
+
const output = {
|
|
208
|
+
html: htmlResult
|
|
209
|
+
};
|
|
210
|
+
Host.outputString(JSON.stringify(output));
|
|
211
|
+
return 0;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
const output = {
|
|
214
|
+
html: "",
|
|
215
|
+
error: error instanceof Error ? error.message : String(error)
|
|
216
|
+
};
|
|
217
|
+
Host.outputString(JSON.stringify(output));
|
|
218
|
+
return 1;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/regex.ts
|
|
21
|
+
var regex_exports = {};
|
|
22
|
+
__export(regex_exports, {
|
|
23
|
+
regex: () => regex
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(regex_exports);
|
|
26
|
+
var MAX_ITERATIONS = 1e5;
|
|
27
|
+
var DANGEROUS_PATTERNS = [
|
|
28
|
+
/\(\?[^)]*\)\+\+/,
|
|
29
|
+
/\(\.\*\)\+/,
|
|
30
|
+
/\(\.\+\)\+/,
|
|
31
|
+
/\([^)]+\+\)\+/,
|
|
32
|
+
/\([^)]+\*\)\*/
|
|
33
|
+
];
|
|
34
|
+
function isDangerousPattern(pattern) {
|
|
35
|
+
for (const dangerous of DANGEROUS_PATTERNS) {
|
|
36
|
+
if (dangerous.test(pattern)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let depth = 0;
|
|
41
|
+
let quantifiers = 0;
|
|
42
|
+
for (const char of pattern) {
|
|
43
|
+
if (char === "(") depth++;
|
|
44
|
+
if (char === ")") depth--;
|
|
45
|
+
if ((char === "+" || char === "*") && depth > 0) quantifiers++;
|
|
46
|
+
}
|
|
47
|
+
return quantifiers > 3;
|
|
48
|
+
}
|
|
49
|
+
function safeMatch(text, regex2) {
|
|
50
|
+
const match = text.match(regex2);
|
|
51
|
+
if (!match) return null;
|
|
52
|
+
return {
|
|
53
|
+
match: match[0],
|
|
54
|
+
index: match.index ?? 0,
|
|
55
|
+
groups: match.groups
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function safeMatchAll(text, regex2, limit) {
|
|
59
|
+
const results = [];
|
|
60
|
+
let iterations = 0;
|
|
61
|
+
const globalRegex = new RegExp(
|
|
62
|
+
regex2.source,
|
|
63
|
+
regex2.flags.includes("g") ? regex2.flags : regex2.flags + "g"
|
|
64
|
+
);
|
|
65
|
+
let match;
|
|
66
|
+
while ((match = globalRegex.exec(text)) !== null) {
|
|
67
|
+
results.push({
|
|
68
|
+
match: match[0],
|
|
69
|
+
index: match.index,
|
|
70
|
+
groups: match.groups
|
|
71
|
+
});
|
|
72
|
+
iterations++;
|
|
73
|
+
if (iterations >= limit || iterations >= MAX_ITERATIONS) break;
|
|
74
|
+
if (match[0].length === 0) {
|
|
75
|
+
globalRegex.lastIndex++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return results;
|
|
79
|
+
}
|
|
80
|
+
function safeReplace(text, regex2, replacement) {
|
|
81
|
+
return text.replace(regex2, replacement);
|
|
82
|
+
}
|
|
83
|
+
function safeSplit(text, regex2, limit) {
|
|
84
|
+
return text.split(regex2, limit);
|
|
85
|
+
}
|
|
86
|
+
function safeTest(text, regex2) {
|
|
87
|
+
return regex2.test(text);
|
|
88
|
+
}
|
|
89
|
+
function regex() {
|
|
90
|
+
try {
|
|
91
|
+
const inputStr = Host.inputString();
|
|
92
|
+
const input = JSON.parse(inputStr);
|
|
93
|
+
if (isDangerousPattern(input.pattern)) {
|
|
94
|
+
throw new Error("Pattern may cause ReDoS - nested quantifiers detected");
|
|
95
|
+
}
|
|
96
|
+
const flags = input.flags ?? "";
|
|
97
|
+
const regex2 = new RegExp(input.pattern, flags);
|
|
98
|
+
const limit = input.limit ?? 1e3;
|
|
99
|
+
let result;
|
|
100
|
+
let matchCount;
|
|
101
|
+
switch (input.operation) {
|
|
102
|
+
case "test":
|
|
103
|
+
result = safeTest(input.text, regex2);
|
|
104
|
+
break;
|
|
105
|
+
case "match":
|
|
106
|
+
result = safeMatch(input.text, regex2);
|
|
107
|
+
matchCount = result ? 1 : 0;
|
|
108
|
+
break;
|
|
109
|
+
case "matchAll":
|
|
110
|
+
const matches = safeMatchAll(input.text, regex2, limit);
|
|
111
|
+
result = matches;
|
|
112
|
+
matchCount = matches.length;
|
|
113
|
+
break;
|
|
114
|
+
case "replace":
|
|
115
|
+
if (input.replacement === void 0) {
|
|
116
|
+
throw new Error("replacement is required for replace operation");
|
|
117
|
+
}
|
|
118
|
+
result = safeReplace(input.text, regex2, input.replacement);
|
|
119
|
+
break;
|
|
120
|
+
case "split":
|
|
121
|
+
result = safeSplit(input.text, regex2, input.limit);
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
throw new Error(`Unknown operation: ${input.operation}`);
|
|
125
|
+
}
|
|
126
|
+
const output = {
|
|
127
|
+
result,
|
|
128
|
+
matchCount
|
|
129
|
+
};
|
|
130
|
+
Host.outputString(JSON.stringify(output));
|
|
131
|
+
return 0;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
const output = {
|
|
134
|
+
result: null,
|
|
135
|
+
error: error instanceof Error ? error.message : String(error)
|
|
136
|
+
};
|
|
137
|
+
Host.outputString(JSON.stringify(output));
|
|
138
|
+
return 1;
|
|
139
|
+
}
|
|
140
|
+
}
|