mirror of
https://github.com/denoland/deno.git
synced 2025-09-29 13:44:47 +00:00
feat(unstable): Support data: urls (#5157)
This commit is contained in:
parent
a3282aa9ed
commit
e3319f34a6
18 changed files with 170 additions and 10 deletions
1
cli/tests/data_import_invalid.js
Normal file
1
cli/tests/data_import_invalid.js
Normal file
|
@ -0,0 +1 @@
|
|||
import _invalid from "data:";
|
2
cli/tests/data_import_invalid.out
Normal file
2
cli/tests/data_import_invalid.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
error: Malformed data url, missing comma
|
||||
Imported from [WILDCARD]
|
2
cli/tests/data_import_origin_upgrade.js
Normal file
2
cli/tests/data_import_origin_upgrade.js
Normal file
|
@ -0,0 +1,2 @@
|
|||
// export default from "https://deno.land/std/version.ts";
|
||||
import _upgrade from "data:application/javascript;base64,ZXhwb3J0IGRlZmF1bHQgZnJvbSAiaHR0cHM6Ly9kZW5vLmxhbmQvc3RkL3ZlcnNpb24udHMiOw==";
|
2
cli/tests/data_import_origin_upgrade.out
Normal file
2
cli/tests/data_import_origin_upgrade.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
error: Modules loaded using data URL are not allowed to import other modules
|
||||
Imported from [WILDCARD]
|
59
cli/tests/data_import_test.js
Normal file
59
cli/tests/data_import_test.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import { assertEquals } from "../../std/testing/asserts.ts";
|
||||
|
||||
// export const value = 'Successful import'; export default value;
|
||||
import data1 from "data:application/javascript;base64,ZXhwb3J0IGNvbnN0IHZhbHVlID0gJ1N1Y2Nlc3NmdWwgaW1wb3J0JzsgZXhwb3J0IGRlZmF1bHQgdmFsdWU7";
|
||||
|
||||
Deno.test("static base64 data url import", () => {
|
||||
assertEquals(data1, "Successful import");
|
||||
});
|
||||
|
||||
Deno.test("dynamic base64 data url import", async () => {
|
||||
const data2 = await import(
|
||||
// export const leet = 1337
|
||||
"data:application/javascript;base64,ZXhwb3J0IGNvbnN0IGxlZXQgPSAxMzM3"
|
||||
);
|
||||
assertEquals(data2.leet, 1337);
|
||||
});
|
||||
|
||||
Deno.test("dynamic percent-encoding data url import", async () => {
|
||||
const data3 = await import(
|
||||
// export const value = 42;
|
||||
"data:application/javascript,export%20const%20value%20%3D%2042%3B"
|
||||
);
|
||||
assertEquals(data3.value, 42);
|
||||
});
|
||||
|
||||
Deno.test("dynamic base64 typescript data url import", async () => {
|
||||
const data2 = await import(
|
||||
// export const leet: number = 1337;
|
||||
"data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGxlZXQ6IG51bWJlciA9IDEzMzc7"
|
||||
);
|
||||
assertEquals(data2.leet, 1337);
|
||||
});
|
||||
|
||||
Deno.test("spawn worker with data url", async () => {
|
||||
let resolve, timeout;
|
||||
const promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
timeout = setTimeout(() => rej("Worker timed out"), 2000);
|
||||
});
|
||||
|
||||
const worker = new Worker(
|
||||
"data:application/javascript," +
|
||||
encodeURIComponent("self.onmessage = () => self.postMessage('Worker');"),
|
||||
{ type: "module" },
|
||||
);
|
||||
|
||||
worker.onmessage = (m) => {
|
||||
if (m.data === "Worker") {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
worker.postMessage();
|
||||
|
||||
await promise;
|
||||
|
||||
clearTimeout(timeout);
|
||||
worker.terminate();
|
||||
});
|
3
cli/tests/data_import_test.out
Normal file
3
cli/tests/data_import_test.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
[WILDCARD]
|
||||
test result: ok. 5 passed; [WILDCARD]
|
||||
|
|
@ -2394,6 +2394,23 @@ itest!(info_type_import {
|
|||
output: "info_type_import.out",
|
||||
});
|
||||
|
||||
itest!(data_import {
|
||||
args: "test --reload --unstable data_import_test.js",
|
||||
output: "data_import_test.out",
|
||||
});
|
||||
|
||||
itest!(data_import_invalid {
|
||||
args: "test --reload --unstable data_import_invalid.js",
|
||||
output: "data_import_invalid.out",
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(data_import_origin_upgrade {
|
||||
args: "test --reload --unstable data_import_origin_upgrade.js",
|
||||
output: "data_import_origin_upgrade.out",
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn cafile_env_fetch() {
|
||||
use url::Url;
|
||||
|
|
|
@ -2,4 +2,5 @@ error: Uncaught TypeError: Unsupported scheme "xxx" for module "xxx:". Supported
|
|||
"http",
|
||||
"https",
|
||||
"file",
|
||||
"data",
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue