Skip to content

Commit 19d17c7

Browse files
authored
feat: implement file: protocol support for fetch (#55)
* feat: export native fetch on the web * chore: add file protocol support * fix: cjs adapter * feat: allow fetching file:// URLs
1 parent af03280 commit 19d17c7

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

packages/fetch/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"@web-std/blob": "^3.0.3",
101101
"@web-std/form-data": "^3.0.2",
102102
"data-uri-to-buffer": "^3.0.1",
103+
"mrmime": "^1.0.0",
103104
"@web3-storage/multipart-parser": "^1.0.0"
104105
},
105106
"esm": {

packages/fetch/src/fetch.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import http from 'http';
1010
import https from 'https';
1111
import zlib from 'zlib';
12+
import fs from 'fs';
13+
import * as mime from 'mrmime'
1214
import dataUriToBuffer from 'data-uri-to-buffer';
1315

1416
import {writeToStream, fromAsyncIterable} from './body.js';
@@ -25,7 +27,7 @@ import { ReadableStream, Blob, FormData } from './package.js';
2527

2628
export {Headers, Request, Response, ReadableStream, Blob, FormData};
2729

28-
const supportedSchemas = new Set(['data:', 'http:', 'https:']);
30+
const supportedSchemas = new Set(['data:', 'http:', 'https:', 'file:']);
2931

3032
/**
3133
* Fetch function
@@ -50,6 +52,14 @@ async function fetch(url, options_ = {}) {
5052
return;
5153
}
5254

55+
if (options.protocol === 'file:') {
56+
const stream = fs.createReadStream(new URL(request.url))
57+
const type = mime.lookup(request.url) || 'application/octet-stream'
58+
const response = new Response(stream, {headers: {'Content-Type': type }});
59+
resolve(response);
60+
return;
61+
}
62+
5363
// Wrap http.request into fetch
5464
const send = (options.protocol === 'https:' ? https : http).request;
5565
const {signal} = request;

packages/fetch/test/file.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fetch from '@web-std/fetch'
2+
import { assert } from "chai"
3+
describe("can fetch local files", () => {
4+
it("can fetch local file", async () => {
5+
const response = await fetch(import.meta.url)
6+
assert.equal(response.headers.get('content-type'), "application/javascript")
7+
const code = await response.text()
8+
9+
assert.ok(code.includes('it("can fetch local file"'))
10+
})
11+
})
12+
13+

yarn.lock

+20-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,21 @@
324324
dependencies:
325325
defer-to-connect "^1.0.1"
326326

327-
"@types/chai@^4.3.0":
327+
"@types/chai-as-promised@^7.1.5":
328+
version "7.1.5"
329+
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.5.tgz#6e016811f6c7a64f2eed823191c3a6955094e255"
330+
integrity sha512-jStwss93SITGBwt/niYrkf2C+/1KTeZCZl1LaeezTlqppAKeoQC7jxyqYuP72sxBGKCIbw7oHgbYssIRzT5FCQ==
331+
dependencies:
332+
"@types/chai" "*"
333+
334+
"@types/chai-string@^1.4.2":
335+
version "1.4.2"
336+
resolved "https://registry.yarnpkg.com/@types/chai-string/-/chai-string-1.4.2.tgz#0f116504a666b6c6a3c42becf86634316c9a19ac"
337+
integrity sha512-ld/1hV5qcPRGuwlPdvRfvM3Ka/iofOk2pH4VkasK4b1JJP1LjNmWWn0LsISf6RRzyhVOvs93rb9tM09e+UuF8Q==
338+
dependencies:
339+
"@types/chai" "*"
340+
341+
"@types/chai@*", "@types/chai@^4.3.0":
328342
version "4.3.0"
329343
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.0.tgz#23509ebc1fa32f1b4d50d6a66c4032d5b8eaabdc"
330344
integrity sha512-/ceqdqeRraGolFTcfoXNiqjyQhZzbINDngeoAq9GoHa8PPK1yNzTaxWjA6BFWp5Ua9JpXEMSS4s5i9tS0hOJtw==
@@ -4207,6 +4221,11 @@ mri@^1.1.0:
42074221
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
42084222
integrity sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==
42094223

4224+
mrmime@^1.0.0:
4225+
version "1.0.0"
4226+
resolved "https://registry.yarnpkg.com/mrmime/-/mrmime-1.0.0.tgz#14d387f0585a5233d291baba339b063752a2398b"
4227+
integrity sha512-a70zx7zFfVO7XpnQ2IX1Myh9yY4UYvfld/dikWRnsXxbyvMcfz+u6UfgNAtH+k2QqtJuzVpv6eLTx1G2+WKZbQ==
4228+
42104229
42114230
version "2.0.0"
42124231
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

0 commit comments

Comments
 (0)