gongxiangp2/lib/__tests__/file-stream-cleanup.test.ts

31 lines
1.2 KiB
TypeScript

/**
* 大文件下载流门禁:路由必须使用统一的背压转换器,不能通过 data 事件
* 无限制地把源文件推入 Web Stream 队列。
*/
describe('大文件下载流清理与背压', () => {
const fs = require('fs')
const path = require('path')
const downloadRoutes = [
['app/api/materials/download/route.ts', 'nodeReadableToWebStream'],
['app/api/materials/[id]/skus/[skuId]/download/route.ts', 'createArchiveDownloadStream'],
['app/api/materials/[id]/skus/batch-download/route.ts', 'createArchiveDownloadStream'],
['app/api/external/download/route.ts', 'createArchiveDownloadStream'],
]
test.each(downloadRoutes)('%s 应使用统一的背压/取消流转换器', (routePath, helperName) => {
const filePath = path.join(__dirname, '../..', routePath)
const content = fs.readFileSync(filePath, 'utf-8')
expect(content).toContain(helperName)
expect(content).not.toMatch(/(?:fileStream|nodeStream|archive)\.on\(['"]data['"]/)
})
test('转换器取消时必须销毁 Node 源流', () => {
const helperPath = path.join(__dirname, '..', 'node-readable-stream.ts')
const content = fs.readFileSync(helperPath, 'utf-8')
expect(content).toMatch(/cancel\([^)]*\)[\s\S]*?source\.destroy\(\)/)
})
})