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

[WIP] fix(serve-static): support Windows #3477

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ jobs:
with:
name: coverage-bun
path: coverage/

bun-windows:
name: 'Bun - Windows'
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: '1.1.16'
- run: bun run test:bun

fastly:
name: 'Fastly Compute'
runs-on: ubuntu-latest
Expand Down Expand Up @@ -204,7 +215,6 @@ jobs:
- run: echo "$COMPARISON"
name: display comparison


perf-measures-type-check-on-main:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
Expand Down
4 changes: 2 additions & 2 deletions runtime-tests/bun/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ describe('Serve Static Middleware', () => {
it('Should return 200 response - /static/helloworld', async () => {
const res = await app.request('http://localhost/static/helloworld')
expect(res.status).toBe(200)
expect(await res.text()).toBe('Hi\n')
expect(await res.text()).toMatch(/Hi\r?\n/)
})

it('Should return 200 response - /static/hello.world', async () => {
const res = await app.request('http://localhost/static/hello.world')
expect(res.status).toBe(200)
expect(await res.text()).toBe('Hi\n')
expect(await res.text()).toMatch(/Hi\r?\n/)
})

it('Should return 200 response - /static-absolute-root/plain.txt', async () => {
Expand Down
16 changes: 14 additions & 2 deletions src/middleware/serve-static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS) as (keyof typeof ENCODINGS
const DEFAULT_DOCUMENT = 'index.html'
const defaultPathResolve = (path: string) => path

const isAbsolutePath = (path: string) => {
const isUnixAbsolutePath = path.startsWith('/')
const hasDriveLetter = /^[a-zA-Z]:\\/.test(path)
const isUncPath = /^\\\\[^\\]+\\[^\\]+/.test(path)
return isUnixAbsolutePath || hasDriveLetter || isUncPath
}

const windowsPathToUnixPath = (path: string) => {
return path.replace(/^[a-zA-Z]:/, '').replace(/\\/g, '/')
}

/**
* This middleware is not directly used by the user. Create a wrapper specifying `getContent()` by the environment such as Deno or Bun.
*/
Expand All @@ -43,9 +54,10 @@ export const serveStatic = <E extends Env = Env>(
let root: string

if (options.root) {
if (options.root.startsWith('/')) {
if (isAbsolutePath(options.root)) {
isAbsoluteRoot = true
root = new URL(`file://${options.root}`).pathname
root = windowsPathToUnixPath(options.root)
root = new URL(`file://${root}`).pathname
} else {
root = options.root
}
Expand Down