Skip to content

Commit

Permalink
Merge pull request cloudflare#142 from xiaolanglanglang/fix-decode-ur…
Browse files Browse the repository at this point in the history
…l-path

bugfix: Fix decode path issue.
  • Loading branch information
kristianfreeman authored May 18, 2021
2 parents 2b3e308 + 360c465 commit e96e520
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@ const getAssetFromKV = async (event: FetchEvent, options?: Partial<Options>): Pr
pathIsEncoded = true;
requestKey = request
} else {
// use default mapRequestToAsset
requestKey = mapRequestToAsset(request)
const mappedRequest = mapRequestToAsset(request)
const mappedRawPathKey = new URL(mappedRequest.url).pathname.replace(/^\/+/, '')
if (ASSET_MANIFEST[decodeURIComponent(mappedRawPathKey)]) {
pathIsEncoded = true;
requestKey = mappedRequest
} else {
// use default mapRequestToAsset
requestKey = mapRequestToAsset(request)
}
}

const SUPPORTED_METHODS = ['GET', 'HEAD']
Expand Down
2 changes: 2 additions & 0 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const store: any = {
'sub/index.123HASHBROWN.html': 'picturedis',
'client.123HASHBROWN': 'important file',
'client.123HASHBROWN/index.html': 'Im here but serve my big bro above',
'你好/index.123HASHBROWN.html': 'My path is non-ascii',
}
export const mockKV = (store: any) => {
return {
Expand All @@ -48,6 +49,7 @@ export const mockManifest = () => {
'sub/index.html': `sub/index.${HASH}.html`,
'client': `client.${HASH}`,
'client/index.html': `client.${HASH}`,
'你好/index.html': `你好/index.${HASH}.html`
})
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/getAssetFromKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ test('getAssetFromKV only decode URL when necessary', async t => {
}
})

test('getAssetFromKV Support for user decode url path', async t => {
mockGlobal()
const event1 = getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD/'))
const event2 = getEvent(new Request('https://blah.com/你好/'))
const res1 = await getAssetFromKV(event1)
const res2 = await getAssetFromKV(event2)

if (res1 && res2) {
t.is(await res1.text(), 'My path is non-ascii')
t.is(await res2.text(), 'My path is non-ascii')
} else {
t.fail('Response was undefined')
}
})

test('getAssetFromKV custom key modifier', async t => {
mockGlobal()
const event = getEvent(new Request('https://blah.com/docs/sub/blah.png'))
Expand Down

0 comments on commit e96e520

Please sign in to comment.