Skip to content
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ app.use(async (ctx) => {

### setHeaders

The function is called as `fn(res, path, stats)`, where the arguments are:
The function is called as `fn(response, path, stats)`, where the arguments are:

- `res`: the response object.
- `response`: the Koa response object.
- `path`: the resolved file path that is being sent.
- `stats`: the stats object of the file that is being sent.

Expand Down
2 changes: 1 addition & 1 deletion src/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function send(
}

// inject headers
setHeaders?.(ctx.res, filePath, stats);
setHeaders?.(ctx.response, filePath, stats);

// stream
ctx.set('Content-Length', stats.size.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/send.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {Stats} from 'node:fs';
import type {ParameterizedContext} from 'koa';

type SetHeaders = (
res: ParameterizedContext['res'],
response: ParameterizedContext['response'],
path: string,
stats: Stats,
) => void;
Expand Down
13 changes: 8 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,18 +765,21 @@ describe('send(ctx, file)', () => {

app.use(async (ctx) => {
await send(ctx, testFilePath, {
setHeaders: function (res, path, stats) {
setHeaders: function (response, path, stats) {
expect(path.slice(-normalizedTestFilePath.length)).toBe(
normalizedTestFilePath
);
expect(stats.size).toBe(18);
expect(res).toBeTruthy();
expect(response).toBe(ctx.response);

// these can be set
res.setHeader('Cache-Control', 'max-age=0,must-revalidate');
res.setHeader('Last-Modified', 'foo');
response.set('Cache-Control', 'max-age=0,must-revalidate');
response.set('Last-Modified', 'foo');
expect(response.get('Cache-Control')).toBe(
'max-age=0,must-revalidate'
);
// this one can not
res.setHeader('Content-Length', 9000);
response.set('Content-Length', '9000');
},
});
});
Expand Down
Loading