From a75bd8192b6c98673187630ceff8098610f5dff1 Mon Sep 17 00:00:00 2001 From: dojyorin Date: Tue, 25 Jul 2023 22:20:01 +0900 Subject: [PATCH 1/2] add safe eval. --- mod.test.ts | 1 + mod.ts | 1 + mod.universal.ts | 1 + src/evaluate.ts | 11 +++++++++++ test/evaluate.test.ts | 11 +++++++++++ 5 files changed, 25 insertions(+) create mode 100644 src/evaluate.ts create mode 100644 test/evaluate.test.ts diff --git a/mod.test.ts b/mod.test.ts index dcd88a4..3c30570 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -2,6 +2,7 @@ import "./test/base64.test.ts"; import "./test/crypto.test.ts"; import "./test/deep.test.ts"; import "./test/deflate.test.ts"; +import "./test/evaluate.test.ts"; import "./test/fetch.test.ts"; import "./test/import.test.ts"; import "./test/json.deno.test.ts"; diff --git a/mod.ts b/mod.ts index 2945fda..adbfa6b 100644 --- a/mod.ts +++ b/mod.ts @@ -2,6 +2,7 @@ export * from "./src/base64.ts"; export * from "./src/crypto.ts"; export * from "./src/deep.ts"; export * from "./src/deflate.ts"; +export * from "./src/evaluate.ts"; export * from "./src/fetch.ts"; export * from "./src/import.ts"; export * from "./src/minipack.ts"; diff --git a/mod.universal.ts b/mod.universal.ts index b884879..38db0fe 100644 --- a/mod.universal.ts +++ b/mod.universal.ts @@ -2,6 +2,7 @@ export * from "./src/base64.ts"; export * from "./src/crypto.ts"; export * from "./src/deep.ts"; export * from "./src/deflate.ts"; +export * from "./src/evaluate.ts"; export * from "./src/fetch.ts"; export * from "./src/import.ts"; export * from "./src/minipack.ts"; diff --git a/src/evaluate.ts b/src/evaluate.ts new file mode 100644 index 0000000..b9e9608 --- /dev/null +++ b/src/evaluate.ts @@ -0,0 +1,11 @@ +import {type Opt} from "./deep.ts"; +import {base64Encode} from "./base64.ts"; +import {utfEncode} from "./text.ts"; + +export interface DefExp{ + default: T; +} + +export async function evaluateESM>(code:string):Promise{ + return await import(`data:text/javascript;base64,${base64Encode(utfEncode(code))}`); +} \ No newline at end of file diff --git a/test/evaluate.test.ts b/test/evaluate.test.ts new file mode 100644 index 0000000..c5a262c --- /dev/null +++ b/test/evaluate.test.ts @@ -0,0 +1,11 @@ +import {assertEquals} from "../deps.test.ts"; +import {type DefExp, evaluateESM} from "../src/evaluate.ts"; + +Deno.test({ + name: "Evaluate: ESM", + async fn(){ + const {default: esm} = await evaluateESM>("export default 'hello!';"); + + assertEquals(esm, "hello!"); + } +}); \ No newline at end of file From b32bca730954200aaf2fc84a0ffd7a40ccb05422 Mon Sep 17 00:00:00 2001 From: dojyorin Date: Tue, 25 Jul 2023 22:25:53 +0900 Subject: [PATCH 2/2] add jsdoc. --- src/evaluate.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/evaluate.ts b/src/evaluate.ts index b9e9608..f901f9f 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -2,10 +2,20 @@ import {type Opt} from "./deep.ts"; import {base64Encode} from "./base64.ts"; import {utfEncode} from "./text.ts"; +/** +* Auxiliary type for default export. +*/ export interface DefExp{ default: T; } +/** +* Evaluate ESM JavaScript code in module scope. +* @example +* ```ts +* const {default: data} = await evaluateESM>("export default 'hello!';"); +* ``` +*/ export async function evaluateESM>(code:string):Promise{ return await import(`data:text/javascript;base64,${base64Encode(utfEncode(code))}`); } \ No newline at end of file