-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure for cross-runtime file server
- Loading branch information
Showing
36 changed files
with
481 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Filesystem functions for HTTP servers | ||
|
||
Cross-runtime functions for interacting with the Filesystem from a HTTP server. | ||
|
||
This package doesn't aim to cover all possible filesystem operations or file | ||
data, just enough to aid in sending files to/from a HTTP server. | ||
|
||
## `fileBody()` | ||
|
||
Is a cross-runtime function to construct the body for a Response from a file | ||
given its path and optionally start/end offsets. The actual return value of this | ||
is a `BodyInit` type, ie. something suitable for passing into a `Response`. The | ||
actual object may vary depending on the runtime. | ||
|
||
## `serveDir()` & `serveFile()` | ||
|
||
These functions have been copied from `@std/http/file-server` and adapted to fit | ||
better with other `@http` functions, and to provide the basis for `staticRoute`. | ||
|
||
They have some features stripped out that were present in | ||
`@std/http/file-server`: | ||
|
||
- No directory listing renderer | ||
- No built-in support for Deno Deploy (see `denoDeployEtag()`) | ||
- No CORS support (can use `cors()` interceptor instead) | ||
- No custom headers added to response (can use an interceptor and | ||
`appendHeaders()` instead) | ||
- No standalone server |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@http/fs", | ||
"version": "0.19.0", | ||
"exports": { | ||
"./file-body": "./file_body.ts", | ||
"./file-body-bun": "./file_body_bun.ts", | ||
"./file-body-deno": "./file_body_deno.ts", | ||
"./file-desc": "./file_desc.ts", | ||
"./file-not-found": "./file_not_found.ts", | ||
"./serve-dir": "./serve_dir.ts", | ||
"./serve-file": "./serve_file.ts", | ||
"./stat": "./stat.ts", | ||
"./types": "./types.ts" | ||
}, | ||
"publish": { | ||
"exclude": [ | ||
"_test/**", | ||
"_testdata/**", | ||
"*.test.ts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { FileBodyFn } from "./types.ts"; | ||
|
||
/** | ||
* Create a Response body for a given file | ||
*/ | ||
export const fileBody: FileBodyFn = "Deno" in globalThis | ||
? (await import("./file_body_deno.ts")).default | ||
: "Bun" in globalThis | ||
? (await import("./file_body_bun.ts")).default | ||
: () => { | ||
throw new Error("fileBody not supported on this runtime"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// <reference types="npm:bun-types@^1.1.6" /> | ||
|
||
import type { FileBodyOptions } from "./types.ts"; | ||
|
||
/** | ||
* Create a Response body for a given file | ||
*/ | ||
export function fileBodyBun( | ||
filePath: string, | ||
opts?: FileBodyOptions, | ||
): Promise<BodyInit> { | ||
const { start = 0, end } = opts ?? {}; | ||
|
||
let file = Bun.file(filePath); | ||
|
||
if (start > 0 || end !== undefined) { | ||
file = file.slice(start, end); | ||
} | ||
|
||
return Promise.resolve(file.stream() as ReadableStream<Uint8Array>); | ||
} | ||
|
||
export default fileBodyBun; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ByteSliceStream } from "@std/streams/byte-slice-stream"; | ||
import type { FileBodyOptions } from "./types.ts"; | ||
|
||
/** | ||
* Create a Response body for a given file | ||
*/ | ||
export async function fileBodyDeno( | ||
filePath: string, | ||
opts?: FileBodyOptions, | ||
): Promise<BodyInit> { | ||
const { start = 0, end } = opts ?? {}; | ||
const file = await Deno.open(filePath); | ||
|
||
if (start > 0) { | ||
await file.seek(start, Deno.SeekMode.Start); | ||
} | ||
|
||
if (end !== undefined) { | ||
return file.readable.pipeThrough(new ByteSliceStream(0, end - start)); | ||
} | ||
|
||
return file.readable; | ||
} | ||
|
||
export default fileBodyDeno; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { FileDesc } from "./types.ts"; | ||
|
||
export type { FileDesc }; | ||
|
||
/** | ||
* Does the given file descriptor represent a directory? | ||
* | ||
* @example | ||
* ```ts | ||
* import { stat } from "jsr:@http/fs/stat"; | ||
* import { isDirectory } from "jsr:@http/fs/file-desc"; | ||
* | ||
* const fileInfo = await stat("./foo"); | ||
* | ||
* if (isDirectory(fileInfo)) { | ||
* ... | ||
* } | ||
* ``` | ||
* | ||
* @param entry may be a Deno `FileInfo`, or Node `Stats` object | ||
*/ | ||
export function isDirectory(entry: FileDesc): boolean { | ||
return typeof entry.isDirectory === "function" | ||
? entry.isDirectory() | ||
: !!entry.isDirectory; | ||
} | ||
|
||
/** | ||
* Does the given file descriptor represent a regular file? | ||
* | ||
* @example | ||
* ```ts | ||
* import { stat } from "jsr:@http/fs/stat"; | ||
* import { isFile } from "jsr:@http/fs/file-desc"; | ||
* | ||
* const fileInfo = await stat("./foo"); | ||
* | ||
* if (isFile(fileInfo)) { | ||
* ... | ||
* } | ||
* ``` | ||
* | ||
* @param entry may be a Deno `FileInfo`, or Node `Stats` object | ||
*/ | ||
export function isFile(entry: FileDesc): boolean { | ||
return typeof entry.isFile === "function" ? entry.isFile() : !!entry.isFile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Check whether an error is a file not found | ||
*/ | ||
export function fileNotFound(error: unknown): boolean { | ||
return error instanceof Error && "code" in error && error.code === "ENOENT"; | ||
} |
Oops, something went wrong.