This commit is contained in:
Manav Rathi 2024-04-30 14:19:25 +05:30
parent 0c312c0ea1
commit 612d8682b5
No known key found for this signature in database
2 changed files with 13 additions and 13 deletions

View file

@ -47,16 +47,15 @@ const eventData = (path: string): [string, FolderWatch] => {
return [path, watch];
};
export const watchGet = (watcher: FSWatcher) => {
const [valid, deleted] = folderWatches().reduce(
([valid, deleted], watch) => {
(fsIsDir(watch.folderPath) ? valid : deleted).push(watch);
return [valid, deleted];
},
[[], []],
);
if (deleted.length) {
for (const watch of deleted) watchRemove(watcher, watch.folderPath);
export const watchGet = async (watcher: FSWatcher): Promise<FolderWatch[]> => {
const valid: FolderWatch[] = [];
const deletedPaths: string[] = [];
for (const watch of folderWatches()) {
if (await fsIsDir(watch.folderPath)) valid.push(watch);
else deletedPaths.push(watch.folderPath);
}
if (deletedPaths.length) {
await Promise.all(deletedPaths.map((p) => watchRemove(watcher, p)));
setFolderWatches(valid);
}
return valid;

View file

@ -8,6 +8,7 @@ import fs from "node:fs/promises";
import { Readable } from "node:stream";
import { pathToFileURL } from "node:url";
import log from "./log";
import { ensure } from "./utils/common";
/**
* Register a protocol handler that we use for streaming large files between the
@ -89,7 +90,7 @@ const handleRead = async (path: string) => {
return res;
} catch (e) {
log.error(`Failed to read stream at ${path}`, e);
return new Response(`Failed to read stream: ${e.message}`, {
return new Response(`Failed to read stream: ${String(e)}`, {
status: 500,
});
}
@ -133,11 +134,11 @@ const handleReadZip = async (zipPath: string, entryName: string) => {
const handleWrite = async (path: string, request: Request) => {
try {
await writeStream(path, request.body);
await writeStream(path, ensure(request.body));
return new Response("", { status: 200 });
} catch (e) {
log.error(`Failed to write stream to ${path}`, e);
return new Response(`Failed to write stream: ${e.message}`, {
return new Response(`Failed to write stream: ${String(e)}`, {
status: 500,
});
}