Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-fetch): improve file access #2009

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tough-moles-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@whatwg-node/node-fetch': patch
---

When `fetch('file:///...')` is used to read files;
- 404 is returned if the file is missing
- 403 is returned if the file is not accessible
36 changes: 31 additions & 5 deletions packages/node-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Buffer } from 'node:buffer';
import { createReadStream } from 'node:fs';
import { createReadStream, promises as fsPromises } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { fetchCurl } from './fetchCurl.js';
import { fetchNodeHttp } from './fetchNodeHttp.js';
Expand All @@ -10,10 +10,36 @@ import { fakePromise } from './utils.js';

const BASE64_SUFFIX = ';base64';

function getResponseForFile(url: string) {
async function getResponseForFile(url: string) {
const path = fileURLToPath(url);
const readable = createReadStream(path);
return new PonyfillResponse(readable);
try {
await fsPromises.access(path, fsPromises.constants.R_OK);
const stats = await fsPromises.stat(path, {
bigint: true,
});
const readable = createReadStream(path);
return new PonyfillResponse(readable, {
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/octet-stream',
'last-modified': stats.mtime.toUTCString(),
},
});
} catch (err: any) {
if (err.code === 'ENOENT') {
return new PonyfillResponse(null, {
status: 404,
statusText: 'Not Found',
});
} else if (err.code === 'EACCES') {
return new PonyfillResponse(null, {
status: 403,
statusText: 'Forbidden',
});
}
throw err;
}
}

function getResponseForDataUri(url: string) {
Expand Down Expand Up @@ -73,7 +99,7 @@ export function fetchPonyfill<TResponseJSON = any, TRequestJSON = any>(

if (fetchRequest.url.startsWith('file:')) {
const response = getResponseForFile(fetchRequest.url);
return fakePromise(response);
return response;
}
if (fetchRequest.url.startsWith('blob:')) {
const response = getResponseForBlob(fetchRequest.url);
Expand Down
26 changes: 19 additions & 7 deletions packages/node-fetch/tests/non-http-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import { pathToFileURL } from 'node:url';
import { describe, expect, it } from '@jest/globals';
import { fetchPonyfill } from '../src/fetch.js';

it('should respect file protocol', async () => {
const response = await fetchPonyfill(
pathToFileURL(join(process.cwd(), './packages/node-fetch/tests/fixtures/test.json')),
);
expect(response.status).toBe(200);
const body = await response.json();
expect(body.foo).toBe('bar');
describe('File protocol', () => {
it('reads', async () => {
const response = await fetchPonyfill(
pathToFileURL(join(process.cwd(), './packages/node-fetch/tests/fixtures/test.json')),
);
expect(response.status).toBe(200);
const body = await response.json();
expect(body.foo).toBe('bar');
});
it('returns 404 if file does not exist', async () => {
const response = await fetchPonyfill(
pathToFileURL(join(process.cwd(), './packages/node-fetch/tests/fixtures/missing.json')),
);
expect(response.status).toBe(404);
});
it('returns 403 if file is not accessible', async () => {
const response = await fetchPonyfill(pathToFileURL('/root/private_data.txt'));
expect(response.status).toBe(403);
});
});

describe('data uris', () => {
Expand Down
Loading