-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use wasm for regex validation
- Loading branch information
1 parent
b04c67b
commit 10b071d
Showing
5 changed files
with
164 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import initWasm, { extractSubstr, extractSubstrIdxes } from "@zk-email/relayer-utils"; | ||
import fs from "fs"; | ||
|
||
let wasmLoaded: Promise<boolean>; | ||
const code = fs.readFileSync("node_modules/@zk-email/relayer-utils/relayer_utils_bg.wasm"); | ||
wasmLoaded = initWasm({ module_or_path: code }).then(() => { | ||
return true; | ||
}) | ||
|
||
export interface Values { | ||
name: string, | ||
location: "header" | "body", | ||
parts: { | ||
regex_def: string, | ||
is_public: boolean, | ||
}[], | ||
} | ||
|
||
export function extractMatches(headerString: string, bodyString: string | undefined, values: Values[]) { | ||
const regexes = values.map((v: any) => { | ||
let publicGroups: number[] = []; | ||
let index = 1; | ||
const regex = v.parts.reduce((acc: any, part: any) => { | ||
const regexWithoutGroup = part.regex_def.replace("(", "(?:") | ||
if (part.is_public) { | ||
publicGroups.push(index); | ||
index++; | ||
return acc + "(" + regexWithoutGroup + ")" | ||
} | ||
return acc + regexWithoutGroup | ||
}, ""); | ||
return { | ||
regex, | ||
publicGroups, | ||
name: v.name, | ||
location: v.location, | ||
} | ||
}) | ||
|
||
let matches = [] | ||
|
||
for (const regex of regexes) { | ||
if (regex.location == "body" && bodyString) { | ||
const match = bodyString.match(regex.regex) | ||
if (match) { | ||
for (const group of regex.publicGroups) { | ||
matches.push({ | ||
name: regex.name, | ||
match: match[group], | ||
}) | ||
} | ||
} | ||
} else { | ||
const match = headerString.match(regex.regex) | ||
if (match) { | ||
for (const group of regex.publicGroups) { | ||
matches.push({ | ||
name: regex.name, | ||
match: match[group], | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
return matches; | ||
} | ||
|
||
export async function extractMatchesWasm(headerString: string, bodyString: string | undefined, values: Values[]) { | ||
let ready = await wasmLoaded; | ||
let matches: {name: string, match: string}[] = []; | ||
for (const value of values) { | ||
if (value.location == "body" && bodyString) { | ||
try { | ||
let match = extractSubstr(bodyString, value, false) | ||
if (match.length > 0) { | ||
matches = matches.concat(match.map( (m:string) => { | ||
return { | ||
name: value.name, | ||
match: m, | ||
} | ||
})); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} else { | ||
try { | ||
let match = extractSubstr(headerString, value, false) | ||
if (match.length > 0) { | ||
matches = matches.concat(match.map( (m:string) => { | ||
return { | ||
name: value.name, | ||
match: m, | ||
} | ||
})); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
return matches; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// import initWasm, { extractSubstr, extractSubstrIdxes } from "@zk-email/relayer-utils"; | ||
// import fs from "fs"; | ||
|
||
import { extractMatches, extractMatchesWasm } from "@/lib/regex" | ||
|
||
// (async () => { | ||
// const code = fs.readFileSync("node_modules/@zk-email/relayer-utils/relayer_utils_bg.wasm"); | ||
// await initWasm({module_or_path: code}); | ||
// const email = "subject:Congratulations mr x\r\n"; | ||
// const matches = extractSubstr(email, { | ||
// parts: [ | ||
// { | ||
// "is_public": false, | ||
// "regex_def": "(\r\n|^)subject:" | ||
// }, | ||
// { | ||
// "is_public": true, | ||
// "regex_def": "Congratulations" | ||
// }, | ||
// { | ||
// "is_public": false, | ||
// "regex_def": "[^\r]+\r\n" | ||
// } | ||
// ] | ||
// }, false) | ||
// console.log(matches) | ||
// })() | ||
|
||
const matches = extractMatchesWasm("\r\nsubject:Congratulations mr x\r\n", "body", [{ | ||
name: "subject", | ||
location: "header", | ||
parts: [ | ||
{ | ||
"is_public": false, | ||
"regex_def": "(\r\n|^)subject:" | ||
}, | ||
{ | ||
"is_public": true, | ||
"regex_def": "Congratulations" | ||
}, | ||
{ | ||
"is_public": false, | ||
"regex_def": "[^\r]+\r\n" | ||
} | ||
], | ||
}, {name: "body", location: "body", parts: [ | ||
{ | ||
"is_public": true, | ||
"regex_def": "body" | ||
} | ||
]}]).then((matches) => { | ||
console.log(matches) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters