2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-10-26 03:17:57 +00:00
|
|
|
import {
|
|
|
|
assert,
|
2023-11-09 00:01:46 +00:00
|
|
|
assertAlmostEquals,
|
2023-10-26 03:17:57 +00:00
|
|
|
assertEquals,
|
|
|
|
assertFalse,
|
2023-11-09 00:01:46 +00:00
|
|
|
assertMatch,
|
2023-10-26 03:17:57 +00:00
|
|
|
assertStringIncludes,
|
2024-04-29 02:57:30 +00:00
|
|
|
} from "@std/assert";
|
|
|
|
import { stub } from "@std/testing/mock";
|
2024-02-27 21:57:25 +00:00
|
|
|
import { serveDir, type ServeDirOptions, serveFile } from "./file_server.ts";
|
2024-07-10 06:27:34 +00:00
|
|
|
import { eTag } from "./etag.ts";
|
2023-06-14 07:37:53 +00:00
|
|
|
import {
|
|
|
|
basename,
|
|
|
|
dirname,
|
|
|
|
fromFileUrl,
|
|
|
|
join,
|
|
|
|
resolve,
|
|
|
|
toFileUrl,
|
2024-04-29 02:57:30 +00:00
|
|
|
} from "@std/path";
|
|
|
|
import denoConfig from "./deno.json" with { type: "json" };
|
|
|
|
import { MINUTE } from "@std/datetime/constants";
|
|
|
|
import { getAvailablePort } from "@std/net/get-available-port";
|
|
|
|
import { concat } from "@std/bytes/concat";
|
2020-05-21 17:55:18 +00:00
|
|
|
|
2020-09-14 10:58:43 +00:00
|
|
|
const moduleDir = dirname(fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = resolve(moduleDir, "testdata");
|
2023-11-09 00:01:46 +00:00
|
|
|
const serveDirOptions: ServeDirOptions = {
|
|
|
|
quiet: true,
|
|
|
|
fsRoot: testdataDir,
|
|
|
|
showDirListing: true,
|
|
|
|
showDotfiles: true,
|
|
|
|
enableCors: true,
|
|
|
|
};
|
|
|
|
|
2023-12-01 00:02:18 +00:00
|
|
|
const TEST_FILE_PATH = join(testdataDir, "test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const TEST_FILE_STAT = await Deno.stat(TEST_FILE_PATH);
|
|
|
|
const TEST_FILE_SIZE = TEST_FILE_STAT.size;
|
2024-07-10 06:27:34 +00:00
|
|
|
const TEST_FILE_ETAG = await eTag(TEST_FILE_STAT) as string;
|
2023-11-09 00:01:46 +00:00
|
|
|
const TEST_FILE_LAST_MODIFIED = TEST_FILE_STAT.mtime instanceof Date
|
|
|
|
? new Date(TEST_FILE_STAT.mtime).toUTCString()
|
|
|
|
: "";
|
|
|
|
const TEST_FILE_TEXT = await Deno.readTextFile(TEST_FILE_PATH);
|
2024-07-29 03:59:32 +00:00
|
|
|
const LOCALHOST = Deno.build.os === "windows" ? "localhost" : "0.0.0.0";
|
2018-12-11 22:56:32 +00:00
|
|
|
|
2020-11-26 21:31:19 +00:00
|
|
|
/* HTTP GET request allowing arbitrary paths */
|
|
|
|
async function fetchExactPath(
|
|
|
|
hostname: string,
|
|
|
|
port: number,
|
|
|
|
path: string,
|
2021-08-31 14:36:30 +00:00
|
|
|
): Promise<Response> {
|
2020-11-26 21:31:19 +00:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
2023-10-28 02:34:08 +00:00
|
|
|
const conn = await Deno.connect({ hostname, port });
|
2023-11-09 00:01:46 +00:00
|
|
|
await conn.write(encoder.encode("GET " + path + " HTTP/1.1\r\n\r\n"));
|
2023-10-28 02:34:08 +00:00
|
|
|
let currentResult = "";
|
|
|
|
let contentLength = -1;
|
|
|
|
let startOfBody = -1;
|
|
|
|
for await (const chunk of conn.readable) {
|
|
|
|
currentResult += decoder.decode(chunk);
|
|
|
|
if (contentLength === -1) {
|
|
|
|
const match = /^content-length: (.*)$/m.exec(currentResult);
|
|
|
|
if (match && match[1]) {
|
|
|
|
contentLength = Number(match[1]);
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-28 02:34:08 +00:00
|
|
|
if (startOfBody === -1) {
|
|
|
|
const ind = currentResult.indexOf("\r\n\r\n");
|
|
|
|
if (ind !== -1) {
|
|
|
|
startOfBody = ind + 4;
|
|
|
|
}
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
2023-10-28 02:34:08 +00:00
|
|
|
if (startOfBody !== -1 && contentLength !== -1) {
|
|
|
|
const byteLen = encoder.encode(currentResult).length;
|
|
|
|
if (byteLen >= contentLength + startOfBody) {
|
|
|
|
break;
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-28 02:34:08 +00:00
|
|
|
}
|
|
|
|
const status = /^HTTP\/1.1 (...)/.exec(currentResult);
|
|
|
|
let statusCode = 0;
|
|
|
|
if (status && status[1]) {
|
|
|
|
statusCode = Number(status[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = currentResult.slice(startOfBody);
|
|
|
|
const headersStr = currentResult.slice(0, startOfBody);
|
|
|
|
const headersReg = /^(.*): (.*)$/mg;
|
|
|
|
const headersObj: { [i: string]: string } = {};
|
|
|
|
let match = headersReg.exec(headersStr);
|
|
|
|
while (match !== null) {
|
|
|
|
if (match[1] && match[2]) {
|
|
|
|
headersObj[match[1]] = match[2];
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
2023-10-28 02:34:08 +00:00
|
|
|
match = headersReg.exec(headersStr);
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
2023-10-28 02:34:08 +00:00
|
|
|
return new Response(body, {
|
|
|
|
status: statusCode,
|
|
|
|
headers: new Headers(headersObj),
|
|
|
|
});
|
2020-11-26 21:31:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() sets last-modified header", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
const lastModifiedHeader = res.headers.get("last-modified") as string;
|
|
|
|
const lastModifiedTime = Date.parse(lastModifiedHeader);
|
|
|
|
const expectedTime = TEST_FILE_STAT.mtime instanceof Date
|
|
|
|
? TEST_FILE_STAT.mtime.getTime()
|
|
|
|
: Number.NaN;
|
2018-12-11 22:56:32 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertAlmostEquals(lastModifiedTime, expectedTime, 5 * MINUTE);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2020-05-21 17:55:18 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() sets date header", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
const dateHeader = res.headers.get("date") as string;
|
|
|
|
const date = Date.parse(dateHeader);
|
|
|
|
const expectedTime =
|
|
|
|
TEST_FILE_STAT.atime && TEST_FILE_STAT.atime instanceof Date
|
|
|
|
? TEST_FILE_STAT.atime.getTime()
|
|
|
|
: Number.NaN;
|
2022-09-29 15:02:43 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertAlmostEquals(date, expectedTime, 5 * MINUTE);
|
2019-01-17 18:08:59 +00:00
|
|
|
});
|
2022-09-29 15:02:43 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir()", async () => {
|
|
|
|
const req = new Request("http://localhost/hello.html");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
const localFile = await Deno.readTextFile(join(testdataDir, "hello.html"));
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
|
|
|
assertEquals(res.headers.get("content-type"), "text/html; charset=UTF-8");
|
2021-10-22 06:30:16 +00:00
|
|
|
});
|
2018-12-11 22:56:32 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() with hash symbol in filename", async () => {
|
2023-11-30 01:50:07 +00:00
|
|
|
const filePath = join(testdataDir, "file#2.txt");
|
|
|
|
const text = "Plain text";
|
|
|
|
await Deno.writeTextFile(filePath, text);
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/file%232.txt");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-type"),
|
|
|
|
"text/plain; charset=UTF-8",
|
|
|
|
);
|
2023-11-30 01:50:07 +00:00
|
|
|
assertEquals(downloadedFile, text);
|
|
|
|
|
|
|
|
await Deno.remove(filePath);
|
2023-11-09 00:01:46 +00:00
|
|
|
});
|
|
|
|
|
2023-12-04 06:00:44 +00:00
|
|
|
Deno.test("serveDir() with space in filename", async () => {
|
|
|
|
const filePath = join(testdataDir, "test file.txt");
|
|
|
|
const text = "Plain text";
|
|
|
|
await Deno.writeTextFile(filePath, text);
|
|
|
|
|
|
|
|
const req = new Request("http://localhost/test%20file.txt");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-type"),
|
|
|
|
"text/plain; charset=UTF-8",
|
|
|
|
);
|
|
|
|
assertEquals(downloadedFile, text);
|
|
|
|
|
|
|
|
await Deno.remove(filePath);
|
|
|
|
});
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() serves directory index", async () => {
|
2023-11-30 01:50:07 +00:00
|
|
|
const filePath = join(testdataDir, "%25A.txt");
|
|
|
|
await Deno.writeTextFile(filePath, "25A");
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const page = await res.text();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(page, '<a href="/hello.html">hello.html</a>');
|
|
|
|
assertStringIncludes(page, '<a href="/tls/">tls/</a>');
|
|
|
|
assertStringIncludes(page, "%2525A.txt");
|
|
|
|
// `Deno.FileInfo` is not completely compatible with Windows yet
|
|
|
|
// TODO(bartlomieju): `mode` should work correctly in the future.
|
|
|
|
// Correct this test case accordingly.
|
|
|
|
if (Deno.build.os === "windows") {
|
|
|
|
assertMatch(page, /<td class="mode">(\s)*\(unknown mode\)(\s)*<\/td>/);
|
2023-12-04 06:00:44 +00:00
|
|
|
} else {
|
|
|
|
assertMatch(page, /<td class="mode">(\s)*[a-zA-Z- ]{14}(\s)*<\/td>/);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Deno.remove(filePath);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("serveDir() serves directory index with file containing space in the filename", async () => {
|
|
|
|
const filePath = join(testdataDir, "test file.txt");
|
|
|
|
await Deno.writeTextFile(filePath, "25A");
|
|
|
|
|
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const page = await res.text();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(page, '<a href="/hello.html">hello.html</a>');
|
|
|
|
assertStringIncludes(page, '<a href="/tls/">tls/</a>');
|
|
|
|
assertStringIncludes(page, "test%20file.txt");
|
|
|
|
// `Deno.FileInfo` is not completely compatible with Windows yet
|
|
|
|
// TODO(bartlomieju): `mode` should work correctly in the future.
|
|
|
|
// Correct this test case accordingly.
|
|
|
|
if (Deno.build.os === "windows") {
|
|
|
|
assertMatch(page, /<td class="mode">(\s)*\(unknown mode\)(\s)*<\/td>/);
|
2023-11-09 00:01:46 +00:00
|
|
|
} else {
|
|
|
|
assertMatch(page, /<td class="mode">(\s)*[a-zA-Z- ]{14}(\s)*<\/td>/);
|
2022-09-29 15:02:43 +00:00
|
|
|
}
|
2023-11-30 01:50:07 +00:00
|
|
|
|
|
|
|
await Deno.remove(filePath);
|
2022-09-29 15:02:43 +00:00
|
|
|
});
|
|
|
|
|
2024-06-25 06:16:28 +00:00
|
|
|
Deno.test("serveDir() serves directory index with file's mode is 0", async () => {
|
|
|
|
const stat = Deno.stat;
|
|
|
|
using _stub = stub(
|
|
|
|
Deno,
|
|
|
|
"stat",
|
|
|
|
async (path: string | URL): Promise<Deno.FileInfo> => ({
|
|
|
|
...(await stat(path)),
|
|
|
|
mode: 0,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
const res = await serveDir(new Request("http://localhost/"), serveDirOptions);
|
|
|
|
const page = await res.text();
|
|
|
|
assertMatch(page, /<td class="mode">(\s)*- --- --- ---(\s)*<\/td>/);
|
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() returns a response even if fileinfo is inaccessible", async () => {
|
2023-05-10 06:15:03 +00:00
|
|
|
// Note: Deno.stat for windows system files may be rejected with os error 32.
|
|
|
|
// Mock Deno.stat to test that the dirlisting page can be generated
|
|
|
|
// even if the fileInfo for a particular file cannot be obtained.
|
|
|
|
|
2023-12-01 00:02:18 +00:00
|
|
|
// Assuming that fileInfo of `test_file.txt` cannot be accessible
|
2024-02-16 04:26:33 +00:00
|
|
|
using denoStatStub = stub(Deno, "stat", (path): Promise<Deno.FileInfo> => {
|
2023-12-01 00:02:18 +00:00
|
|
|
if (path.toString().includes("test_file.txt")) {
|
2023-05-10 06:15:03 +00:00
|
|
|
return Promise.reject(new Error("__stubed_error__"));
|
|
|
|
}
|
|
|
|
return denoStatStub.original.call(Deno, path);
|
|
|
|
});
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const page = await res.text();
|
2023-05-10 06:15:03 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.status, 200);
|
2023-12-01 00:02:18 +00:00
|
|
|
assertStringIncludes(page, "/test_file.txt");
|
2023-05-10 06:15:03 +00:00
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles not found files", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/badfile.txt");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 404);
|
2020-10-27 10:48:45 +00:00
|
|
|
});
|
|
|
|
|
2024-08-07 12:35:30 +00:00
|
|
|
Deno.test("serveDir() handles incorrect method", async () => {
|
|
|
|
const req = new Request("http://localhost/", { method: "POST" });
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 405);
|
|
|
|
assertEquals(res.statusText, "Method Not Allowed");
|
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() traverses path correctly", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/../../../../../../../..");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const page = await res.text();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(page, "hello.html");
|
2020-10-27 10:48:45 +00:00
|
|
|
});
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() traverses path", async () => {
|
|
|
|
const controller = new AbortController();
|
|
|
|
const port = 4507;
|
2023-11-15 08:37:19 +00:00
|
|
|
const server = Deno.serve(
|
2023-11-09 00:01:46 +00:00
|
|
|
{ port, signal: controller.signal },
|
|
|
|
async (req) => await serveDir(req, serveDirOptions),
|
|
|
|
);
|
2020-11-26 21:31:19 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
const res1 = await fetchExactPath("127.0.0.1", port, "../../../..");
|
|
|
|
await res1.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res1.status, 400);
|
|
|
|
|
|
|
|
const res2 = await fetchExactPath(
|
|
|
|
"127.0.0.1",
|
|
|
|
port,
|
|
|
|
"http://localhost/../../../..",
|
|
|
|
);
|
|
|
|
const page = await res2.text();
|
|
|
|
|
|
|
|
assertEquals(res2.status, 200);
|
|
|
|
assertStringIncludes(page, "hello.html");
|
|
|
|
|
|
|
|
controller.abort();
|
2023-11-15 08:37:19 +00:00
|
|
|
await server.finished;
|
2020-11-26 21:31:19 +00:00
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() traverses encoded URI path", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request(
|
|
|
|
"http://localhost/%2F..%2F..%2F..%2F..%2F..%2F..%2F..%2F..",
|
|
|
|
);
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.status, 301);
|
|
|
|
assertEquals(res.headers.get("location"), "http://localhost/");
|
2019-01-17 18:08:59 +00:00
|
|
|
});
|
2019-12-10 12:11:55 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() serves unusual filename", async () => {
|
2023-11-30 01:50:07 +00:00
|
|
|
const filePath = join(testdataDir, "%");
|
2024-01-14 21:35:50 +00:00
|
|
|
using _file = await Deno.create(filePath);
|
2023-11-30 01:50:07 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
const req1 = new Request("http://localhost/%25");
|
|
|
|
const res1 = await serveDir(req1, serveDirOptions);
|
|
|
|
await res1.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res1.status, 200);
|
|
|
|
assert(res1.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res1.headers.has("access-control-allow-headers"));
|
|
|
|
|
2023-12-01 00:02:18 +00:00
|
|
|
const req2 = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res2 = await serveDir(req2, serveDirOptions);
|
|
|
|
await res2.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res2.status, 200);
|
|
|
|
assert(res2.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res2.headers.has("access-control-allow-headers"));
|
2023-11-30 01:50:07 +00:00
|
|
|
|
|
|
|
await Deno.remove(filePath);
|
2021-07-22 06:12:40 +00:00
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() supports CORS", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req1 = new Request("http://localhost/");
|
|
|
|
const res1 = await serveDir(req1, serveDirOptions);
|
|
|
|
await res1.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res1.status, 200);
|
|
|
|
assert(res1.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res1.headers.has("access-control-allow-headers"));
|
|
|
|
|
|
|
|
const req2 = new Request("http://localhost/hello.html");
|
|
|
|
const res2 = await serveDir(req2, serveDirOptions);
|
|
|
|
await res2.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res2.status, 200);
|
|
|
|
assert(res2.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res2.headers.has("access-control-allow-headers"));
|
2019-12-10 12:11:55 +00:00
|
|
|
});
|
2019-12-12 05:05:26 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() script prints help", async () => {
|
2022-11-15 06:00:59 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2022-05-25 09:08:27 +00:00
|
|
|
args: [
|
2020-04-30 15:23:40 +00:00
|
|
|
"run",
|
2021-11-03 08:11:19 +00:00
|
|
|
"--no-check",
|
2020-11-20 17:01:58 +00:00
|
|
|
"--quiet",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2024-02-22 04:40:55 +00:00
|
|
|
"http/file_server.ts",
|
2020-04-30 15:23:40 +00:00
|
|
|
"--help",
|
|
|
|
],
|
2019-12-14 02:01:32 +00:00
|
|
|
});
|
2022-11-15 06:00:59 +00:00
|
|
|
const { stdout } = await command.output();
|
|
|
|
const output = new TextDecoder().decode(stdout);
|
2024-04-29 02:57:30 +00:00
|
|
|
assert(output.includes(`Deno File Server ${denoConfig.version}`));
|
2022-11-11 05:08:35 +00:00
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() script prints version", async () => {
|
2022-11-15 06:00:59 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2022-11-11 05:08:35 +00:00
|
|
|
args: [
|
|
|
|
"run",
|
|
|
|
"--no-check",
|
|
|
|
"--quiet",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2024-02-22 04:40:55 +00:00
|
|
|
"http/file_server.ts",
|
2022-11-11 05:08:35 +00:00
|
|
|
"--version",
|
|
|
|
],
|
|
|
|
});
|
2022-11-15 06:00:59 +00:00
|
|
|
const { stdout } = await command.output();
|
|
|
|
const output = new TextDecoder().decode(stdout);
|
2024-04-29 02:57:30 +00:00
|
|
|
assert(output.includes(`Deno File Server ${denoConfig.version}`));
|
2019-12-14 02:01:32 +00:00
|
|
|
});
|
2020-04-01 16:51:01 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() ignores query params", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/hello.html?key=value");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
const localFile = await Deno.readTextFile(join(testdataDir, "hello.html"));
|
2020-08-12 10:55:38 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2020-08-12 10:55:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() script fails with partial TLS args", async () => {
|
2023-10-26 03:17:57 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2022-05-25 09:08:27 +00:00
|
|
|
args: [
|
2020-08-12 10:55:38 +00:00
|
|
|
"run",
|
2021-11-03 08:11:19 +00:00
|
|
|
"--no-check",
|
2020-11-20 17:01:58 +00:00
|
|
|
"--quiet",
|
2020-08-12 10:55:38 +00:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2024-02-22 04:40:55 +00:00
|
|
|
"http/file_server.ts",
|
2020-08-12 10:55:38 +00:00
|
|
|
".",
|
|
|
|
"--host",
|
|
|
|
"localhost",
|
|
|
|
"--cert",
|
2020-09-14 10:58:43 +00:00
|
|
|
"./testdata/tls/localhost.crt",
|
2020-08-12 10:55:38 +00:00
|
|
|
"-p",
|
|
|
|
`4578`,
|
|
|
|
],
|
|
|
|
stderr: "null",
|
|
|
|
});
|
2023-10-26 03:17:57 +00:00
|
|
|
const { stdout, success } = await command.output();
|
|
|
|
assertFalse(success);
|
|
|
|
assertStringIncludes(
|
|
|
|
new TextDecoder().decode(stdout),
|
|
|
|
"--key and --cert are required for TLS",
|
|
|
|
);
|
2020-08-12 10:55:38 +00:00
|
|
|
});
|
2020-08-12 15:38:25 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() doesn't show directory listings", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, {
|
|
|
|
...serveDirOptions,
|
|
|
|
showDirListing: false,
|
|
|
|
});
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 404);
|
2020-08-12 15:38:25 +00:00
|
|
|
});
|
2021-02-02 11:01:21 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() doesn't show dotfiles", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req1 = new Request("http://localhost/");
|
|
|
|
const res1 = await serveDir(req1, {
|
|
|
|
...serveDirOptions,
|
|
|
|
showDotfiles: false,
|
|
|
|
});
|
|
|
|
const page1 = await res1.text();
|
|
|
|
|
|
|
|
assert(!page1.includes(".dotfile"));
|
|
|
|
|
|
|
|
const req2 = new Request("http://localhost/.dotfile");
|
|
|
|
const res2 = await serveDir(req2, {
|
|
|
|
...serveDirOptions,
|
|
|
|
showDotfiles: false,
|
|
|
|
});
|
|
|
|
const body = await res2.text();
|
|
|
|
|
|
|
|
assertEquals(body, "dotfile");
|
2021-02-02 19:36:52 +00:00
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() shows .. if it makes sense", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req1 = new Request("http://localhost/");
|
|
|
|
const res1 = await serveDir(req1, serveDirOptions);
|
|
|
|
const page1 = await res1.text();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assert(!page1.includes("../"));
|
|
|
|
assertStringIncludes(page1, "tls/");
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
const req2 = new Request("http://localhost/tls/");
|
|
|
|
const res2 = await serveDir(req2, serveDirOptions);
|
|
|
|
const page2 = await res2.text();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertStringIncludes(page2, "../");
|
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() handles range request (bytes=0-0)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=0-0" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(text, "L");
|
|
|
|
});
|
2023-11-08 03:31:52 +00:00
|
|
|
|
|
|
|
Deno.test("serveDir() handles range request (bytes=0-100)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=0-100" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
2021-08-03 06:58:31 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-range"),
|
|
|
|
`bytes 0-100/${TEST_FILE_SIZE}`,
|
|
|
|
);
|
|
|
|
assertEquals(res.status, 206);
|
|
|
|
assertEquals((await res.arrayBuffer()).byteLength, 101);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2021-08-03 06:58:31 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles range request (bytes=300-)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=300-" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-range"),
|
|
|
|
`bytes 300-${TEST_FILE_SIZE - 1}/${TEST_FILE_SIZE}`,
|
|
|
|
);
|
|
|
|
assertEquals(text, TEST_FILE_TEXT.substring(300));
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles range request (bytes=-200)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=-200" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(await res.text(), TEST_FILE_TEXT.slice(-200));
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-range"),
|
|
|
|
`bytes ${TEST_FILE_SIZE - 200}-${TEST_FILE_SIZE - 1}/${TEST_FILE_SIZE}`,
|
|
|
|
);
|
|
|
|
assertEquals(res.status, 206);
|
|
|
|
assertEquals(res.statusText, "Partial Content");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() clamps ranges that are too large (bytes=0-999999999)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=0-999999999" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(await res.text(), TEST_FILE_TEXT);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-range"),
|
|
|
|
`bytes 0-${TEST_FILE_SIZE - 1}/${TEST_FILE_SIZE}`,
|
|
|
|
);
|
|
|
|
assertEquals(res.status, 206);
|
|
|
|
assertEquals(res.statusText, "Partial Content");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() clamps ranges that are too large (bytes=-999999999)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
// This means the last 999999999 bytes. It is too big and should be clamped.
|
|
|
|
headers: { range: "bytes=-999999999" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(await res.text(), TEST_FILE_TEXT);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-range"),
|
|
|
|
`bytes 0-${TEST_FILE_SIZE - 1}/${TEST_FILE_SIZE}`,
|
|
|
|
);
|
|
|
|
assertEquals(res.status, 206);
|
|
|
|
assertEquals(res.statusText, "Partial Content");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles bad range request (bytes=500-200)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=500-200" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("content-range"), `bytes */${TEST_FILE_SIZE}`);
|
|
|
|
assertEquals(res.status, 416);
|
2023-11-17 02:26:11 +00:00
|
|
|
assertEquals(res.statusText, "Range Not Satisfiable");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles bad range request (bytes=99999-999999)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=99999-999999" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("content-range"), `bytes */${TEST_FILE_SIZE}`);
|
|
|
|
assertEquals(res.status, 416);
|
2023-11-17 02:26:11 +00:00
|
|
|
assertEquals(res.statusText, "Range Not Satisfiable");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() handles bad range request (bytes=99999)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=99999-" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("content-range"), `bytes */${TEST_FILE_SIZE}`);
|
|
|
|
assertEquals(res.status, 416);
|
2023-11-17 02:26:11 +00:00
|
|
|
assertEquals(res.statusText, "Range Not Satisfiable");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() ignores bad range request (bytes=100)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=100" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
|
|
|
|
|
|
|
assertEquals(text, TEST_FILE_TEXT);
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() ignores bad range request (bytes=a-b)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=a-b" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
|
|
|
|
|
|
|
assertEquals(text, TEST_FILE_TEXT);
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() ignores bad multi-range request (bytes=0-10, 20-30)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=0-10, 20-30" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
|
|
|
|
|
|
|
assertEquals(text, TEST_FILE_TEXT);
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-05-04 04:58:13 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() serves ok response for empty file range request", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/test_empty_file.txt", {
|
|
|
|
headers: { range: "bytes=0-10, 20-30" },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
const text = await res.text();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(text, "");
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() sets accept-ranges header to bytes for directory listing", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("accept-ranges"), "bytes");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() sets accept-ranges header to bytes for file response", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("accept-ranges"), "bytes");
|
2021-07-23 13:16:52 +00:00
|
|
|
});
|
2021-07-22 06:12:40 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() sets headers if provided as arguments", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveDir(req, {
|
|
|
|
...serveDirOptions,
|
2023-11-08 03:31:52 +00:00
|
|
|
headers: ["cache-control:max-age=100", "x-custom-header:hi"],
|
|
|
|
});
|
2023-11-09 00:01:46 +00:00
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.headers.get("cache-control"), "max-age=100");
|
|
|
|
assertEquals(res.headers.get("x-custom-header"), "hi");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("serveDir() sets etag header", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
2023-11-08 03:31:52 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(res.headers.get("etag"), TEST_FILE_ETAG);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
Deno.test("serveDir() serves empty HTTP 304 response for if-none-match request of unmodified file", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { "if-none-match": TEST_FILE_ETAG },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
|
|
|
|
assertEquals(await res.text(), "");
|
|
|
|
assertEquals(res.status, 304);
|
|
|
|
assertEquals(res.statusText, "Not Modified");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("serveDir() serves HTTP 304 response for if-modified-since request of unmodified file", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { "if-modified-since": TEST_FILE_LAST_MODIFIED },
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 304);
|
|
|
|
assertEquals(res.statusText, "Not Modified");
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
/**
|
|
|
|
* When used in combination with If-None-Match, If-Modified-Since is ignored.
|
|
|
|
* If etag doesn't match, don't return 304 even if if-modified-since is a valid
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since}
|
|
|
|
*/
|
2022-12-27 04:43:04 +00:00
|
|
|
Deno.test(
|
2023-11-08 03:31:52 +00:00
|
|
|
"serveDir() only uses if-none-match header if if-non-match and if-modified-since headers are provided",
|
2023-02-14 07:47:38 +00:00
|
|
|
async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: {
|
|
|
|
"if-none-match": "not match etag",
|
|
|
|
"if-modified-since": TEST_FILE_LAST_MODIFIED,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
2023-02-14 07:47:38 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() serves test file", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt");
|
2023-11-09 00:01:46 +00:00
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 200);
|
2023-11-09 00:01:46 +00:00
|
|
|
assertEquals(await res.text(), TEST_FILE_TEXT);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2022-08-16 05:49:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() handles file not found", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/testdata/non_existent.txt");
|
2023-11-08 03:31:52 +00:00
|
|
|
const testdataPath = join(testdataDir, "non_existent.txt");
|
|
|
|
const res = await serveFile(req, testdataPath);
|
2023-11-09 00:01:46 +00:00
|
|
|
await res.body?.cancel();
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 404);
|
|
|
|
assertEquals(res.statusText, "Not Found");
|
|
|
|
});
|
2022-08-16 05:49:38 +00:00
|
|
|
|
2024-08-07 12:35:30 +00:00
|
|
|
Deno.test("serveFile() handles method not allowed", async () => {
|
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt", {
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 405);
|
|
|
|
assertEquals(res.statusText, "Method Not Allowed");
|
|
|
|
});
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() serves HTTP 404 when the path is a directory", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/testdata/");
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveFile(req, testdataDir);
|
2023-11-09 00:01:46 +00:00
|
|
|
await res.body?.cancel();
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 404);
|
|
|
|
assertEquals(res.statusText, "Not Found");
|
|
|
|
});
|
2022-09-15 14:34:22 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() handles bad range request (bytes=200-500)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=200-500" },
|
|
|
|
});
|
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 206);
|
|
|
|
assertEquals((await res.arrayBuffer()).byteLength, 301);
|
|
|
|
});
|
2023-04-18 07:34:51 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() handles bad range request (bytes=500-200)", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { range: "bytes=500-200" },
|
|
|
|
});
|
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 416);
|
|
|
|
});
|
2022-08-16 05:49:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() serves HTTP 304 response for if-modified-since request of unmodified file", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: { "if-none-match": TEST_FILE_ETAG },
|
|
|
|
});
|
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 304);
|
|
|
|
assertEquals(res.statusText, "Not Modified");
|
|
|
|
});
|
2022-02-22 02:06:26 +00:00
|
|
|
|
2023-11-09 00:01:46 +00:00
|
|
|
/**
|
|
|
|
* When used in combination with If-None-Match, If-Modified-Since is ignored.
|
|
|
|
* If etag doesn't match, don't return 304 even if if-modified-since is a valid
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since}
|
|
|
|
*/
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() only uses if-none-match header if if-non-match and if-modified-since headers are provided", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt", {
|
2023-11-09 00:01:46 +00:00
|
|
|
headers: {
|
|
|
|
"if-none-match": "not match etag",
|
|
|
|
"if-modified-since": TEST_FILE_LAST_MODIFIED,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const res = await serveFile(req, TEST_FILE_PATH);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.statusText, "OK");
|
|
|
|
});
|
2023-02-14 07:47:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveFile() etag value falls back to DENO_DEPLOYMENT_ID if fileInfo.mtime is not available", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const DENO_DEPLOYMENT_ID = "__THIS_IS_DENO_DEPLOYMENT_ID__";
|
2024-07-10 06:27:34 +00:00
|
|
|
const hashedDenoDeploymentId = await eTag(DENO_DEPLOYMENT_ID, {
|
2023-11-08 03:31:52 +00:00
|
|
|
weak: true,
|
|
|
|
});
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const code = `
|
|
|
|
import { serveFile } from "${import.meta.resolve("./file_server.ts")}";
|
|
|
|
import { fromFileUrl } from "${import.meta.resolve("../path/mod.ts")}";
|
2024-07-02 03:42:40 +00:00
|
|
|
import { assertEquals } from "${import.meta.resolve("../assert/equals.ts")}";
|
2023-12-01 00:02:18 +00:00
|
|
|
const testdataPath = "${toFileUrl(join(testdataDir, "test_file.txt"))}";
|
2023-11-08 03:31:52 +00:00
|
|
|
const fileInfo = await Deno.stat(new URL(testdataPath));
|
|
|
|
fileInfo.mtime = null;
|
2023-12-01 00:02:18 +00:00
|
|
|
const req = new Request("http://localhost/testdata/test_file.txt");
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveFile(req, fromFileUrl(testdataPath), { fileInfo });
|
|
|
|
assertEquals(res.headers.get("etag"), \`${hashedDenoDeploymentId}\`);
|
|
|
|
`;
|
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2024-01-12 04:38:41 +00:00
|
|
|
args: ["eval", "--no-lock", code],
|
2023-11-09 00:01:46 +00:00
|
|
|
stdout: "null",
|
|
|
|
stderr: "null",
|
|
|
|
env: { DENO_DEPLOYMENT_ID },
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
|
|
|
const { success } = await command.output();
|
|
|
|
assert(success);
|
|
|
|
});
|
2023-02-14 07:47:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() without options serves files in current directory", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/http/testdata/hello.html");
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveDir(req);
|
2023-02-14 07:47:38 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(await res.text(), "Hello World");
|
|
|
|
});
|
2022-02-22 02:06:26 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() with fsRoot and urlRoot option serves files in given directory", async () => {
|
|
|
|
const req = new Request(
|
2023-11-09 00:01:46 +00:00
|
|
|
"http://localhost/my-static-root/testdata/hello.html",
|
2023-11-08 03:31:52 +00:00
|
|
|
);
|
|
|
|
const res = await serveDir(req, {
|
|
|
|
fsRoot: "http",
|
|
|
|
urlRoot: "my-static-root",
|
|
|
|
});
|
2023-11-09 00:01:46 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(await res.text(), "Hello World");
|
|
|
|
});
|
2022-02-22 02:06:26 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() serves index.html when showIndex is true", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const url = "http://localhost/http/testdata/subdir-with-index/";
|
2023-11-08 03:31:52 +00:00
|
|
|
const expectedText = "This is subdir-with-index/index.html";
|
|
|
|
{
|
|
|
|
const res = await serveDir(new Request(url), { showIndex: true });
|
2022-02-22 02:06:26 +00:00
|
|
|
assertEquals(res.status, 200);
|
2023-11-08 03:31:52 +00:00
|
|
|
assertStringIncludes(await res.text(), expectedText);
|
|
|
|
}
|
2022-10-03 11:09:09 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
{
|
|
|
|
// showIndex is true by default
|
|
|
|
const res = await serveDir(new Request(url));
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(await res.text(), expectedText);
|
|
|
|
}
|
|
|
|
});
|
2022-10-03 11:09:09 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() doesn't serve index.html when showIndex is false", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request(
|
|
|
|
"http://localhost/http/testdata/subdir-with-index/",
|
|
|
|
);
|
|
|
|
const res = await serveDir(req, { showIndex: false });
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 404);
|
|
|
|
});
|
2022-10-03 11:09:09 +00:00
|
|
|
|
2023-03-01 14:50:58 +00:00
|
|
|
Deno.test(
|
2023-11-08 03:31:52 +00:00
|
|
|
"serveDir() redirects a directory URL not ending with a slash if it has an index",
|
2023-03-01 14:50:58 +00:00
|
|
|
async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const url = "http://localhost/http/testdata/subdir-with-index";
|
2023-03-01 14:50:58 +00:00
|
|
|
const res = await serveDir(new Request(url), { showIndex: true });
|
2023-11-09 00:01:46 +00:00
|
|
|
|
2023-03-01 14:50:58 +00:00
|
|
|
assertEquals(res.status, 301);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("Location"),
|
2023-11-09 00:01:46 +00:00
|
|
|
"http://localhost/http/testdata/subdir-with-index/",
|
2023-03-01 14:50:58 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() redirects a directory URL not ending with a slash correctly even with a query string", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const url = "http://localhost/http/testdata/subdir-with-index?test";
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveDir(new Request(url), { showIndex: true });
|
2023-11-09 00:01:46 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 301);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("Location"),
|
2023-11-09 00:01:46 +00:00
|
|
|
"http://localhost/http/testdata/subdir-with-index/?test",
|
2023-11-08 03:31:52 +00:00
|
|
|
);
|
|
|
|
});
|
2023-03-01 14:50:58 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() redirects a file URL ending with a slash correctly even with a query string", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const url = "http://localhost/http/testdata/test_file.txt/?test";
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveDir(new Request(url), { showIndex: true });
|
2023-11-09 00:01:46 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 301);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("Location"),
|
2023-12-01 00:02:18 +00:00
|
|
|
"http://localhost/http/testdata/test_file.txt?test",
|
2023-11-08 03:31:52 +00:00
|
|
|
);
|
|
|
|
});
|
2023-05-06 07:28:27 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() redirects non-canonical URLs", async () => {
|
2023-12-01 00:02:18 +00:00
|
|
|
const url = "http://localhost/http/testdata//////test_file.txt/////?test";
|
2023-11-08 03:31:52 +00:00
|
|
|
const res = await serveDir(new Request(url), { showIndex: true });
|
2023-11-09 00:01:46 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
assertEquals(res.status, 301);
|
|
|
|
assertEquals(
|
|
|
|
res.headers.get("Location"),
|
2023-12-01 00:02:18 +00:00
|
|
|
"http://localhost/http/testdata/test_file.txt/?test",
|
2023-11-08 03:31:52 +00:00
|
|
|
);
|
|
|
|
});
|
2023-05-06 07:28:27 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() serves HTTP 304 for if-none-match requests with W/-prefixed etag", async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const testurl = "http://localhost/desktop.ini";
|
|
|
|
const fileurl = new URL("./testdata/desktop.ini", import.meta.url);
|
|
|
|
const req1 = new Request(testurl, {
|
|
|
|
headers: { "accept-encoding": "gzip, deflate, br" },
|
|
|
|
});
|
|
|
|
const res1 = await serveDir(req1, serveDirOptions);
|
|
|
|
const etag = res1.headers.get("etag");
|
|
|
|
|
|
|
|
assertEquals(res1.status, 200);
|
|
|
|
assertEquals(res1.statusText, "OK");
|
|
|
|
assertEquals(await Deno.readTextFile(fileurl), await res1.text());
|
|
|
|
assert(typeof etag === "string");
|
|
|
|
assert(etag.length > 0);
|
|
|
|
assert(etag.startsWith("W/"));
|
|
|
|
|
|
|
|
const req2 = new Request(testurl, {
|
|
|
|
headers: { "if-none-match": etag },
|
|
|
|
});
|
|
|
|
const res2 = await serveDir(req2, serveDirOptions);
|
|
|
|
|
|
|
|
assertEquals(res2.status, 304);
|
|
|
|
assertEquals(res2.statusText, "Not Modified");
|
|
|
|
assertEquals("", await res2.text());
|
|
|
|
assert(
|
|
|
|
etag === res2.headers.get("etag") ||
|
|
|
|
etag === "W/" + res2.headers.get("etag"),
|
|
|
|
);
|
2023-11-08 03:31:52 +00:00
|
|
|
});
|
2023-06-06 05:00:05 +00:00
|
|
|
|
2023-11-08 03:31:52 +00:00
|
|
|
Deno.test("serveDir() resolves path correctly on Windows", {
|
2023-06-06 05:00:05 +00:00
|
|
|
ignore: Deno.build.os !== "windows",
|
|
|
|
}, async () => {
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request("http://localhost/");
|
|
|
|
const res = await serveDir(req, { ...serveDirOptions, fsRoot: "c:/" });
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
2023-06-06 05:00:05 +00:00
|
|
|
});
|
2023-06-14 07:37:53 +00:00
|
|
|
|
|
|
|
Deno.test(
|
2023-11-08 03:31:52 +00:00
|
|
|
"serveDir() resolves empty sub-directory without asking for current directory read permissions on Windows",
|
2023-06-14 07:37:53 +00:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os !== "windows",
|
2023-11-09 00:01:46 +00:00
|
|
|
permissions: {
|
|
|
|
read: [`${moduleDir}/testdata`],
|
|
|
|
write: true,
|
|
|
|
},
|
2023-06-14 07:37:53 +00:00
|
|
|
},
|
|
|
|
async () => {
|
2024-03-12 00:36:10 +00:00
|
|
|
const tempDir = await Deno.makeTempDir({
|
|
|
|
dir: `${moduleDir}/testdata`,
|
|
|
|
});
|
2023-11-09 00:01:46 +00:00
|
|
|
const req = new Request(`http://localhost/${basename(tempDir)}/`);
|
|
|
|
const res = await serveDir(req, serveDirOptions);
|
|
|
|
await res.body?.cancel();
|
|
|
|
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
|
2024-03-12 00:36:10 +00:00
|
|
|
await Deno.remove(tempDir);
|
2023-06-14 07:37:53 +00:00
|
|
|
},
|
|
|
|
);
|
2024-04-22 05:25:14 +00:00
|
|
|
|
|
|
|
Deno.test("file_server prints local and network urls", async () => {
|
|
|
|
const port = await getAvailablePort();
|
|
|
|
const process = spawnDeno([
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
|
|
|
"--allow-sys=networkInterfaces",
|
|
|
|
"http/file_server.ts",
|
|
|
|
"--port",
|
|
|
|
`${port}`,
|
|
|
|
]);
|
|
|
|
const output = await readUntilMatch(process.stdout, "Network:");
|
|
|
|
const networkAdress = Deno.networkInterfaces().find((i) =>
|
|
|
|
i.family === "IPv4" && !i.address.startsWith("127")
|
|
|
|
)?.address;
|
|
|
|
assertEquals(
|
|
|
|
output,
|
2024-07-29 03:59:32 +00:00
|
|
|
`Listening on:\n- Local: http://${LOCALHOST}:${port}\n- Network: http://${networkAdress}:${port}\n`,
|
2024-04-22 05:25:14 +00:00
|
|
|
);
|
|
|
|
process.stdout.cancel();
|
|
|
|
process.stderr.cancel();
|
|
|
|
process.kill();
|
|
|
|
await process.status;
|
|
|
|
});
|
|
|
|
|
2024-07-25 10:13:16 +00:00
|
|
|
Deno.test("file_server doesn't print local network url without --allow-sys", async () => {
|
|
|
|
const port = await getAvailablePort();
|
|
|
|
const process = spawnDeno([
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
|
|
|
"http/file_server.ts",
|
|
|
|
"--port",
|
|
|
|
`${port}`,
|
|
|
|
]);
|
|
|
|
const output = await readUntilMatch(process.stdout, "Local:");
|
|
|
|
assertEquals(
|
|
|
|
output,
|
2024-07-29 03:59:32 +00:00
|
|
|
`Listening on:\n- Local: http://${LOCALHOST}:${port}\n`,
|
2024-07-25 10:13:16 +00:00
|
|
|
);
|
|
|
|
process.stdout.cancel();
|
|
|
|
process.stderr.cancel();
|
|
|
|
process.kill();
|
|
|
|
await process.status;
|
|
|
|
});
|
|
|
|
|
2024-04-22 05:25:14 +00:00
|
|
|
Deno.test("file_server prints only local address on Deploy", async () => {
|
|
|
|
const port = await getAvailablePort();
|
|
|
|
const process = spawnDeno([
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
|
|
|
"--allow-sys=networkInterfaces",
|
|
|
|
"--allow-env=DENO_DEPLOYMENT_ID",
|
|
|
|
"http/file_server.ts",
|
|
|
|
"--port",
|
|
|
|
`${port}`,
|
|
|
|
], {
|
|
|
|
env: {
|
|
|
|
DENO_DEPLOYMENT_ID: "abcdef",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const output = await readUntilMatch(process.stdout, "Local:");
|
|
|
|
assertEquals(
|
|
|
|
output,
|
2024-07-29 03:59:32 +00:00
|
|
|
`Listening on:\n- Local: http://${LOCALHOST}:${port}\n`,
|
2024-04-22 05:25:14 +00:00
|
|
|
);
|
|
|
|
process.stdout.cancel();
|
|
|
|
process.stderr.cancel();
|
|
|
|
process.kill();
|
|
|
|
await process.status;
|
|
|
|
});
|
|
|
|
|
|
|
|
/** Spawn deno child process with the options convenient for testing */
|
|
|
|
function spawnDeno(args: string[], opts?: Deno.CommandOptions) {
|
|
|
|
const cmd = new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"run",
|
|
|
|
"--no-lock",
|
|
|
|
"--quiet",
|
|
|
|
...args,
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped",
|
|
|
|
...opts,
|
|
|
|
});
|
|
|
|
return cmd.spawn();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readUntilMatch(
|
|
|
|
source: ReadableStream,
|
|
|
|
match: string,
|
|
|
|
) {
|
|
|
|
const reader = source.getReader();
|
|
|
|
let buf = new Uint8Array(0);
|
|
|
|
const dec = new TextDecoder();
|
|
|
|
while (!dec.decode(buf).includes(match)) {
|
|
|
|
const { value } = await reader.read();
|
|
|
|
if (!value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = concat([buf, value]);
|
|
|
|
}
|
|
|
|
reader.releaseLock();
|
|
|
|
return dec.decode(buf);
|
|
|
|
}
|