Skip to content

Commit

Permalink
fix: remove deprecated API, work around Deno bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jan 25, 2024
1 parent bbae544 commit 71a1d92
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
15 changes: 7 additions & 8 deletions etag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ export {
ifNoneMatch,
} from "./deps.ts";

function fstat(file: Deno.FsFile): Promise<Deno.FileInfo | undefined> {
if ("fstat" in Deno) {
// deno-lint-ignore no-explicit-any
return (Deno as any).fstat(file.rid);
}
return Promise.resolve(undefined);
// This is to work around issue introduced in Deno 1.40
// See: https://github.com/denoland/deno/issues/22115
function isFsFile(value: unknown): value is Deno.FsFile {
return !!(value && typeof value === "object" && "stat" in value &&
typeof value.stat === "function");
}

/** For a given Context, try to determine the response body entity that an ETag
Expand All @@ -36,8 +35,8 @@ export function getEntity<S extends State = Record<string, any>>(
context: Context<S>,
): Promise<string | Uint8Array | Deno.FileInfo | undefined> {
const { body } = context.response;
if (body instanceof Deno.FsFile) {
return fstat(body);
if (isFsFile(body)) {
return body.stat();
}
if (body instanceof Uint8Array) {
return Promise.resolve(body);
Expand Down
16 changes: 2 additions & 14 deletions etag_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,8 @@ test({

const mw = factory();
await mw(context, next);
// Deno.fstat is currently an unstable API in Deno, but the code is written
// to fail gracefully, so we sniff if the API is unavailable and change
// the assertions accordingly.
if ("fstat" in Deno) {
const actual = context.response.headers.get("etag");
// mtime will vary from system to system which makes up part of the hash
// we we only look at the part that is consistent.
assert(actual && actual.startsWith(`W/"4a3b7-`));
} else {
assertEquals(
context.response.headers.get("etag"),
null,
);
}
const actual = context.response.headers.get("etag");
assert(actual && actual.startsWith(`W/"4a3b7-`));

file!.close();
},
Expand Down
10 changes: 5 additions & 5 deletions response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Response {
#headers = new Headers();
#jsonBodyReplacer?: (key: string, value: unknown) => unknown;
#request: Request;
#resources: number[] = [];
#resources: { close(): void }[] = [];
#status?: Status;
#type?: string;
#writable = true;
Expand Down Expand Up @@ -223,8 +223,8 @@ export class Response {

/** Add a resource to the list of resources that will be closed when the
* request is destroyed. */
addResource(rid: number): void {
this.#resources.push(rid);
addResource(resource: { close(): void }): void {
this.#resources.push(resource);
}

/** Release any resources that are being tracked by the response.
Expand All @@ -236,9 +236,9 @@ export class Response {
this.#body = undefined;
this.#domResponse = undefined;
if (closeResources) {
for (const rid of this.#resources) {
for (const resource of this.#resources) {
try {
Deno.close(rid);
resource.close();
} catch {
// we don't care about errors here
}
Expand Down
2 changes: 1 addition & 1 deletion send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function getEntity(
file.close();
body = entity = buffer;
} else {
response.addResource(file.rid);
response.addResource(file);
body = file;
entity = {
mtime: new Date(mtime!),
Expand Down

0 comments on commit 71a1d92

Please sign in to comment.