diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..41583e3 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@jsr:registry=https://npm.jsr.io diff --git a/bun.lockb b/bun.lockb index 08f1f16..ba735b8 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 338b7c6..16f7cfd 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,8 @@ "start": "bun run --logLevel=debug dist/server.js", "build": "bun build src/server.ts --outdir ./dist" }, - "type": "module" + "type": "module", + "dependencies": { + "@regexplanet/common": "npm:@jsr/regexplanet__common" + } } \ No newline at end of file diff --git a/src/runTest.ts b/src/runTest.ts deleted file mode 100644 index ec0522d..0000000 --- a/src/runTest.ts +++ /dev/null @@ -1,209 +0,0 @@ -import { type TestInput, type TestOutput } from "./types"; - -function h(unsafe: string): string { - if (unsafe == null) { - return ""; - } - - return unsafe - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/"/g, """) - .replace(/'/g, "'"); -} - -export function runTest(input: TestInput): TestOutput { - if (input.regex == null || input.regex.length == 0) { - return { success: false, message: "No regex to test!" }; - } - - const options = input.option ? input.option.join("") : undefined; - - const html = []; - html.push( - '\n' - ); - - html.push("\t\n"); - html.push("\t\t\n"); - html.push("\t\t\n"); - html.push("\t\n"); - - html.push("\t\n"); - html.push("\t\t\n"); - html.push("\t\t\n"); - - html.push("\t\n"); - html.push("\t\t\n"); - html.push("\t\t\n"); - html.push("\t\n"); - html.push("
Regular Expression"); - html.push(h(input.regex)); - html.push("
Replacement"); - html.push(h(input.replacement)); - html.push("
Options"); - html.push(options ? h(options) : "(none)"); - html.push("
\n"); - - let compileTest: RegExp; - - try { - compileTest = new RegExp(input.regex, options); - } catch (err) { - const message = err instanceof Error ? err.message : `unknown error ${err}`; - return { - success: false, - message: `unable to create RegExp object: ${message}`, - html: html.join(""), - }; - } - - try { - html.push('\n'); - - html.push("\t"); - html.push("\t\t\n"); - html.push('\t\t\t\n'); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\t"); - html.push("\t\t\n"); - html.push("\t\n"); - html.push("\t\n"); - - var count = 0; - - if (input.inputs != null) { - for (var loop = 0; loop < input.inputs.length; loop++) { - var target = input.inputs[loop]; - - if (target.length == 0) { - continue; - } - html.push("\t\t\n"); - - html.push('\t\t\t\n"); - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - var regex = new RegExp(input.regex, options); - var result = regex.exec(target); - if (result == null) { - html.push('\t\t\t\n'); - } else { - var first = true; - - while (result != null) { - if (first == true) { - first = false; - } else { - html.push("\n"); - html.push('\t\t\t\n"); - } - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - html.push("\t\t\t\n"); - - result = global ? regex.exec(target) : null; - } - } - html.push("\t\t\n"); - count++; - } - } - - if (count == 0) { - html.push("\t\t\n"); - html.push('\t\t\n"); - html.push("\t\t\n"); - } - - html.push("\t\n"); - html.push("
TestInputregex.test()input.replace()input.replaceAll()input.split()[]regex.exec().indexregex.exec()[]regex.lastIndex
'); - html.push(loop + 1); - html.push(""); - html.push(h(target)); - html.push(""); - html.push( - new RegExp(input.regex, options).test(target) ? "true" : "false" - ); - html.push(""); - html.push( - h(target.replace(new RegExp(input.regex, options), input.replacement)) - ); - html.push(""); - try { - html.push( - h( - target.replaceAll( - new RegExp(input.regex, options), - input.replacement - ) - ) - ); - } catch (replaceAllErr) { - const message = - replaceAllErr instanceof Error - ? replaceAllErr.message - : `unknown error ${replaceAllErr}`; - html.push(`${message}`); - } - html.push(""); - var splits = target.split(new RegExp(input.regex, options)); - for (var split = 0; split < splits.length; split++) { - html.push("["); - html.push(split); - html.push("]: "); - html.push(splits[split] == null ? "(null)" : h(splits[split])); - html.push("
"); - } - html.push("
(null)
'); - html.push("regex.exec()"); - html.push(""); - html.push(result.index); - html.push(""); - for (var capture = 0; capture < result.length; capture++) { - html.push("["); - html.push(capture); - html.push("]: "); - html.push( - result[capture] == null ? "(null)" : h(result[capture]) - ); - html.push("
"); - } - html.push("
"); - html.push(regex.lastIndex); - html.push("
'); - html.push("(no input to test)"); - html.push("
\n"); - } catch (err) { - const message = err instanceof Error ? err.message : `unknown error ${err}`; - return { - success: false, - message: `unable to run tests: ${message}`, - html: html.join(""), - }; - } - - return { - success: true, - html: html.join(""), - }; -} diff --git a/src/server.ts b/src/server.ts index bcc3556..6af5c13 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,6 +1,6 @@ import type { Server } from "bun"; -import type { TestInput } from "./types"; -import { runTest } from "./runTest"; +import type { TestInput } from "@regexplanet/common"; +import { runTest } from "@regexplanet/common"; Bun.serve({ development: process.env.NODE_ENV !== "production", @@ -13,10 +13,9 @@ Bun.serve({ }, hostname: process.env.HOSTNAME || "0.0.0.0", idleTimeout: 15, - port: process.env.PORT || 4000, + port: process.env.PORT || 5000, static: { - //LATER: "/": Response.redirect("https://www.regexplanet.com/advanced/bun/index.html", 302), - "/": new Response("running!"), + "/": new Response(`Running Bun v${Bun.version}`), "/favicon.ico": new Response(await Bun.file("static/favicon.ico").bytes(), { headers: { diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index c892e4b..0000000 --- a/src/types.ts +++ /dev/null @@ -1,16 +0,0 @@ - - -export type TestInput = { - engine: string; - regex: string; - replacement: string; - extras?: string[]; - option: string[]; - inputs: string[]; -}; - -export type TestOutput = { - success: boolean; - html?: string; - message?: string; -}; \ No newline at end of file