Skip to content

Commit

Permalink
refactor(cmd/lsjson): use GET for file
Browse files Browse the repository at this point in the history
But adjust the `location` to be of the parent folder.

The listing is then filtered by filename.

This allows us to use the single `GET` instead of depending on `HEAD`.
  • Loading branch information
sntran committed Jul 6, 2024
1 parent 31502eb commit bd36a4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 64 deletions.
51 changes: 19 additions & 32 deletions cmd/lsjson/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,48 +54,26 @@ const decoder = new TextDecoder();
*/
export async function lsjson(location, flags = {}) {
const isDirectory = location.endsWith("/");
const init = { method: isDirectory ? "GET" : "HEAD" };
const url = `${location}?${new URLSearchParams(flags)}`;
const init = { method: "GET" };
const segments = location.split("/");
let filename;
// If file, remove the last segment to query the parent directory.
if (!isDirectory) {
filename = segments.pop();
}
const params = new URLSearchParams(flags);
const url = `${segments.join("/")}?${params}`;
let response = await fetch(url, init);

if (!response.ok) {
return response;
}

let maxDepth = Number(flags.max_depth || (flags.recursive ? Infinity : 1));
if (!flags.recursive) {
if (!isDirectory || !flags.recursive) {
maxDepth = 1;
}

if (!isDirectory) {
const headers = response.headers;
const { pathname } = new URL(location);
const Path = pathname.slice(1);
const Name = pathname.split("/").pop();
const item = {
Path,
Name,
Size: Number(headers.get("Content-Length")),
MimeType: headers.get("Content-Type"),
ModTime: headers.get("Last-Modified"),
IsDir: false,
};

const body = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode("[\n"));
controller.enqueue(encoder.encode(JSON.stringify(item) + "\n"));
controller.enqueue(encoder.encode("]\n"));
},
});

return new Response(body, {
headers: {
"Content-Type": "application/json",
},
});
}

/**
* A stream of JSON-encoded file objects, one per chunk/line.
* Each JSON stringified line is followed by a comma and a newline.
Expand Down Expand Up @@ -158,6 +136,13 @@ export async function lsjson(location, flags = {}) {
}

const name = basename(pathname);

// If listing a file, skip other files.
if (filename && name !== filename) {
item = null;
return;
}

item.Name = name;

/**
Expand Down Expand Up @@ -220,12 +205,14 @@ export async function lsjson(location, flags = {}) {
});
rewriter.on(`tr data[value]`, {
element(element) {
if (!item) return;
const value = element.getAttribute("value");
item.Size = Number(value);
},
});
rewriter.on(`tr time[datetime]`, {
element(element) {
if (!item) return;
const value = element.getAttribute("datetime");
if (value) {
item.ModTime = toLocaleISOString(value);
Expand Down
64 changes: 32 additions & 32 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd36a4f

Please sign in to comment.