Registry Mirror
Skip to content

File tree

‎packages/node/src/static-file-handler-plugin.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Buffer } from 'node:buffer'
22
import {
3+
appendFileSync,
34
mkdirSync,
45
mkdtempSync,
56
rmSync,
@@ -1045,6 +1046,31 @@ describe('staticFileHandlerPlugin', () => {
10451046
expect(freshRes.status).toBe(304)
10461047
})
10471048

1049+
it('never sends more bytes than content-length when the file grows after stat', async ({
1050+
onTestFinished,
1051+
}) => {
1052+
const growingPath = path.join(rootDir, 'growing.txt')
1053+
const growingEmptyPath = path.join(rootDir, 'growing-empty.txt')
1054+
writeFileSync(growingPath, 'hello world')
1055+
writeFileSync(growingEmptyPath, '')
1056+
onTestFinished(() => {
1057+
rmSync(growingPath, { force: true })
1058+
rmSync(growingEmptyPath, { force: true })
1059+
})
1060+
1061+
const handler = new FetchRPCHandler({}, { plugins: [new StaticFileHandlerPlugin({ rootDir })] })
1062+
1063+
const served = await handler.handle(new Request('https://example.com/growing.txt'))
1064+
appendFileSync(growingPath, ' + appended')
1065+
expect(served.response!.headers.get('content-length')).toBe('11')
1066+
expect(await served.response!.text()).toBe('hello world')
1067+
1068+
const servedEmpty = await handler.handle(new Request('https://example.com/growing-empty.txt'))
1069+
appendFileSync(growingEmptyPath, 'appended')
1070+
expect(servedEmpty.response!.headers.get('content-length')).toBe('0')
1071+
expect(await servedEmpty.response!.text()).toBe('')
1072+
})
1073+
10481074
describe('tracing', () => {
10491075
it('renames the active span to the mounted base path', async ({ onTestFinished }) => {
10501076
const span = {

‎packages/node/src/static-file-handler-plugin.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,9 @@ export class StaticFileHandlerPlugin<T extends Context> implements StandardHandl
292292
headers['content-length'] = `${end - start + 1}`
293293

294294
const body: ReadableStream<Uint8Array<ArrayBuffer>> =
295-
request.method === 'HEAD'
295+
request.method === 'HEAD' || end < start
296296
? new ReadableStream({ start: (controller) => controller.close() })
297-
: toWebReadableStream(
298-
createReadStream(filePath, status === 206 ? { start, end } : undefined),
299-
)
297+
: toWebReadableStream(createReadStream(filePath, { start, end }))
300298

301299
return { status, headers, body }
302300
}

0 commit comments

Comments
 (0)