mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
Add CORS option to file_server (denoland/deno_std#45)
Original: 731f03829c
This commit is contained in:
parent
7143f7d860
commit
a5ad386887
3 changed files with 33 additions and 3 deletions
|
@ -40,12 +40,22 @@ const dirViewerTemplate = `
|
||||||
</html>
|
</html>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const serverArgs = args.slice();
|
||||||
|
let CORSEnabled = false;
|
||||||
|
// TODO: switch to flags if we later want to add more options
|
||||||
|
for (let i = 0; i < serverArgs.length; i++) {
|
||||||
|
if (serverArgs[i] === "--cors") {
|
||||||
|
CORSEnabled = true;
|
||||||
|
serverArgs.splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
let currentDir = cwd();
|
let currentDir = cwd();
|
||||||
const target = args[1];
|
const target = serverArgs[1];
|
||||||
if (target) {
|
if (target) {
|
||||||
currentDir = `${currentDir}/${target}`;
|
currentDir = `${currentDir}/${target}`;
|
||||||
}
|
}
|
||||||
const addr = `0.0.0.0:${args[2] || 4500}`;
|
const addr = `0.0.0.0:${serverArgs[2] || 4500}`;
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
function modeToString(isDir: boolean, maybeMode: number | null) {
|
function modeToString(isDir: boolean, maybeMode: number | null) {
|
||||||
|
@ -188,6 +198,17 @@ function serverLog(req: ServerRequest, res: Response) {
|
||||||
console.log(s);
|
console.log(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setCORS(res: Response) {
|
||||||
|
if (!res.headers) {
|
||||||
|
res.headers = new Headers();
|
||||||
|
}
|
||||||
|
res.headers!.append("access-control-allow-origin", "*");
|
||||||
|
res.headers!.append(
|
||||||
|
"access-control-allow-headers",
|
||||||
|
"Origin, X-Requested-With, Content-Type, Accept, Range"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
listenAndServe(addr, async req => {
|
listenAndServe(addr, async req => {
|
||||||
const fileName = req.url.replace(/\/$/, "");
|
const fileName = req.url.replace(/\/$/, "");
|
||||||
const filePath = currentDir + fileName;
|
const filePath = currentDir + fileName;
|
||||||
|
@ -206,6 +227,9 @@ listenAndServe(addr, async req => {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
response = await serveFallback(req, e);
|
response = await serveFallback(req, e);
|
||||||
} finally {
|
} finally {
|
||||||
|
if (CORSEnabled) {
|
||||||
|
setCORS(response);
|
||||||
|
}
|
||||||
serverLog(req, response);
|
serverLog(req, response);
|
||||||
req.respond(response);
|
req.respond(response);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,8 @@ export function runTests(serverReadyPromise: Promise<any>) {
|
||||||
test(async function serveFile() {
|
test(async function serveFile() {
|
||||||
await serverReadyPromise;
|
await serverReadyPromise;
|
||||||
const res = await fetch("http://localhost:4500/.travis.yml");
|
const res = await fetch("http://localhost:4500/.travis.yml");
|
||||||
|
assert(res.headers.has("access-control-allow-origin"));
|
||||||
|
assert(res.headers.has("access-control-allow-headers"));
|
||||||
const downloadedFile = await res.text();
|
const downloadedFile = await res.text();
|
||||||
const localFile = new TextDecoder().decode(await readFile("./.travis.yml"));
|
const localFile = new TextDecoder().decode(await readFile("./.travis.yml"));
|
||||||
assertEqual(downloadedFile, localFile);
|
assertEqual(downloadedFile, localFile);
|
||||||
|
@ -32,6 +34,8 @@ export function runTests(serverReadyPromise: Promise<any>) {
|
||||||
test(async function serveDirectory() {
|
test(async function serveDirectory() {
|
||||||
await serverReadyPromise;
|
await serverReadyPromise;
|
||||||
const res = await fetch("http://localhost:4500/");
|
const res = await fetch("http://localhost:4500/");
|
||||||
|
assert(res.headers.has("access-control-allow-origin"));
|
||||||
|
assert(res.headers.has("access-control-allow-headers"));
|
||||||
const page = await res.text();
|
const page = await res.text();
|
||||||
assert(page.includes(".travis.yml"));
|
assert(page.includes(".travis.yml"));
|
||||||
maybeCompleteTests();
|
maybeCompleteTests();
|
||||||
|
@ -40,6 +44,8 @@ export function runTests(serverReadyPromise: Promise<any>) {
|
||||||
test(async function serveFallback() {
|
test(async function serveFallback() {
|
||||||
await serverReadyPromise;
|
await serverReadyPromise;
|
||||||
const res = await fetch("http://localhost:4500/badfile.txt");
|
const res = await fetch("http://localhost:4500/badfile.txt");
|
||||||
|
assert(res.headers.has("access-control-allow-origin"));
|
||||||
|
assert(res.headers.has("access-control-allow-headers"));
|
||||||
assertEqual(res.status, 404);
|
assertEqual(res.status, 404);
|
||||||
maybeCompleteTests();
|
maybeCompleteTests();
|
||||||
});
|
});
|
||||||
|
|
2
test.ts
2
test.ts
|
@ -19,7 +19,7 @@ import "logging/test.ts";
|
||||||
|
|
||||||
// file server test
|
// file server test
|
||||||
const fileServer = run({
|
const fileServer = run({
|
||||||
args: ["deno", "--allow-net", "net/file_server.ts", "."]
|
args: ["deno", "--allow-net", "net/file_server.ts", ".", "--cors"]
|
||||||
});
|
});
|
||||||
// path test
|
// path test
|
||||||
import "path/basename_test.ts";
|
import "path/basename_test.ts";
|
||||||
|
|
Loading…
Reference in a new issue