This commit is contained in:
Manav Rathi 2024-05-01 18:21:45 +05:30
parent de4aa3a6ca
commit 4eb51061cb
No known key found for this signature in database
2 changed files with 12 additions and 16 deletions

View file

@ -37,25 +37,18 @@ export const registerStreamProtocol = () => {
protocol.handle("stream", async (request: Request) => {
const url = request.url;
// The request URL contains the command to run as the host, and the
// pathname of the file as the path. An additional path can be specified
// as the URL hash.
//
// For example,
//
// stream://write/path/to/file#/path/to/another/file
// host[pathname----] [pathname-2---------]
//
// pathname of the file(s) as the search params.
const { host, searchParams } = new URL(url);
const path = ensure(searchParams.get("path"));
const path2 = searchParams.get("path2") ?? undefined;
log.debug(() => `[stream] ${host} ${path}${path2 ? "::" + path2 : ""}`);
switch (host) {
case "read":
return handleRead(path);
return handleRead(ensure(searchParams.get("path")));
case "read-zip":
return handleReadZip(path, ensure(path2));
return handleReadZip(
ensure(searchParams.get("zipPath")),
ensure(searchParams.get("entryName")),
);
case "write":
return handleWrite(path, request);
return handleWrite(ensure(searchParams.get("path")), request);
default:
return new Response("", { status: 404 });
}

View file

@ -43,7 +43,7 @@ export const readStream = async (
url = new URL(`stream://read?${params.toString()}`);
} else {
const [zipPath, entryName] = pathOrZipItem;
const params = new URLSearchParams({ path: zipPath, path2: entryName });
const params = new URLSearchParams({ zipPath, entryName });
url = new URL(`stream://read-zip?${params.toString()}`);
}
@ -90,6 +90,9 @@ export const writeStream = async (
path: string,
stream: ReadableStream,
) => {
const params = new URLSearchParams({ path });
const url = new URL(`stream://write?${params.toString()}`);
// TODO(MR): This doesn't currently work.
//
// Not sure what I'm doing wrong here; I've opened an issue upstream
@ -120,7 +123,7 @@ export const writeStream = async (
});
*/
const req = new Request(`stream://write${path}`, {
const req = new Request(url, {
method: "POST",
body: await new Response(stream).blob(),
});