Skip to content

Commit

Permalink
actually streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Jan 10, 2024
1 parent d241dc3 commit 94b7b80
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ console.log(text);
</table>
🔎 Works great for writing isomorphic `fetch(import.meta.resolve())` code \
✅ Uses streaming `openAsBlob()` if available \
🧰 Supports bringing your own `fetch()` function \
🌊 Uses `fs.createReadStream()` for streaming reads \
🦕 Mirrors Deno's implementation of `fetch()` for `file:` URLs
## Installation
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-with-file-support",
"version": "2.0.0",
"version": "2.0.1",
"description": "📂 fetch() but with support for file:///my/file.txt URLs",
"keywords": [
"nodejs",
Expand Down
9 changes: 5 additions & 4 deletions src/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import openAsBlob from "./lib/node-fs-openAsBlob.js";
import { createReadStream } from "node:fs";
import { Readable } from "node:stream";

const { fetch: globalThisFetch, Request, Response, Headers } = globalThis

Expand All @@ -10,13 +11,13 @@ export async function fetch(
input instanceof Request && !init ? input : new Request(input, init);
if (request.url.startsWith("file:")) {
if (request.method === "GET") {
let blob: Blob;
let readable: ReadableStream<Uint8Array>
try {
blob = await openAsBlob(new URL(request.url));
readable = Readable.toWeb(createReadStream(new URL(request.url)))
} catch (error) {
throw new TypeError("NetworkError when attempting to fetch resource");
}
return new Response(blob);
return new Response(readable)
} else {
throw new TypeError(
`Fetching files only supports the GET method. ` +
Expand Down
9 changes: 0 additions & 9 deletions src/lib/node-fs-openAsBlob.ts

This file was deleted.

0 comments on commit 94b7b80

Please sign in to comment.