uppy/packages/@uppy/url/vitest.setup.ts
Kevin van Zonneveld e99a17f1fe
companion: port to TypeScript (#6179)
Supersedes #6178.

This branch keeps the exact same final content as #6178, but splits
history for reviewability:

1. `chore(companion): rename source and test files from .js to .ts`
(pure `git mv`, no content changes)
2. `feat(companion): port Companion to TypeScript` (actual
content/type/schema/build updates)

Validation note:
- Final tree is byte-for-byte identical to `ts-companion`
(`7b5b16a298690b0492de4cc4fab744f998b45570`).

---------

Co-authored-by: Mikael Finstad <finstaden@gmail.com>
2026-05-04 20:50:43 +08:00

75 lines
1.9 KiB
TypeScript

import { spawn } from 'node:child_process'
import { createServer } from 'node:http'
import { dirname, join } from 'node:path'
import { setTimeout } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'
import type { TestProject } from 'vitest/node'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const mockServerPort = 62450
export default async function setup(project: TestProject) {
const mockServer = createServer((req, res) => {
const fileName = `DALL·E IMG_9078 - 学中文 🤑`
if (req.url === '/file-with-content-disposition') {
res.writeHead(200, {
'content-disposition': `attachment; filename="ASCII-name.zip"; filename*=UTF-8''${encodeURIComponent(
fileName,
)}`,
'content-type': 'image/jpeg',
'content-length': '86500',
})
if (req.method === 'HEAD') {
res.end()
} else {
res.end('mock image data')
}
} else if (req.url === '/file-no-headers') {
// Explicitly remove any default content-type
res.removeHeader('content-type')
res.writeHead(200, {})
if (req.method === 'HEAD') {
res.end()
} else {
res.end('mock file content')
}
} else {
res.writeHead(404)
res.end()
}
})
await new Promise<void>((resolve) => {
mockServer.listen(mockServerPort, 'localhost', resolve)
})
const companionProcess = spawn(
'node',
[
join(
__dirname,
'../../@uppy/companion/dist/scripts/with-load-balancer.js',
),
],
{
stdio: 'inherit',
cwd: join(__dirname, '../../../..'),
env: {
...process.env,
// Pass the mock server URL to companion if needed
MOCK_SERVER_URL: `http://localhost:${mockServerPort}`,
},
},
)
await setTimeout(1000)
return () => {
companionProcess.kill()
mockServer.close()
}
}