From 6f03fbd130fce615134e2624e9334ee34c5df622 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 1 Oct 2024 16:52:50 +0900 Subject: [PATCH 1/4] wip --- .github/workflows/ci.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5fd29d8df..26ab135e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,6 +97,18 @@ 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 install + - run: bun run test + fastly: name: 'Fastly Compute' runs-on: ubuntu-latest @@ -204,7 +216,6 @@ jobs: - run: echo "$COMPARISON" name: display comparison - perf-measures-type-check-on-main: runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' From 04e4fb1af1a65cb1d6e1c065ae6106f7081bdf51 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 1 Oct 2024 16:59:06 +0900 Subject: [PATCH 2/4] fixed the CI command --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 26ab135e5..f40e3a80f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -106,8 +106,7 @@ jobs: - uses: oven-sh/setup-bun@v1 with: bun-version: '1.1.16' - - run: bun install - - run: bun run test + - run: bun run test:bun fastly: name: 'Fastly Compute' From 5b8d81a892e83b0ec9a156a1a7819a971c006671 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 1 Oct 2024 17:05:24 +0900 Subject: [PATCH 3/4] fixed the tests for passing on Windows --- runtime-tests/bun/index.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime-tests/bun/index.test.tsx b/runtime-tests/bun/index.test.tsx index 6dda78aba..7b303f839 100644 --- a/runtime-tests/bun/index.test.tsx +++ b/runtime-tests/bun/index.test.tsx @@ -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 () => { From 1e66b38b7c70245d76488213103164911e3e6da3 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 1 Oct 2024 17:17:17 +0900 Subject: [PATCH 4/4] support Windows path when using `option.root` --- src/middleware/serve-static/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 0f1837f22..9e44a40aa 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -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. */ @@ -43,9 +54,10 @@ export const serveStatic = ( 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 }