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

fix: decodeURIComponent on path parts #40

Merged
merged 2 commits into from
Apr 2, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/verified-fetch/src/utils/parse-url-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,5 @@ function joinPaths (resolvedPath: string | undefined, urlPath: string): string {
path = path.substring(1)
}

return path
return path.split('/').map(decodeURIComponent).join('/')
}
14 changes: 14 additions & 0 deletions packages/verified-fetch/test/utils/parse-url-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ describe('parseUrlString', () => {
}
)
})

// tests for https://github.com/ipfs-shipyard/service-worker-gateway/issues/83 issue
it('can parse an IPFS path with encodedURIComponents', async () => {
const rawPathLabel = "Plan_d'exécution_du_second_étage_de_l'hôtel_de_Brionne_(dessin)_De_Cotte_2503c_–_Gallica_2011_(adjusted).jpg.webp"
await assertMatchUrl(
`/ipfs/QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr/I/${encodeURIComponent(rawPathLabel)}`, {
protocol: 'ipfs',
cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr',
// path with decoded component
path: `I/${rawPathLabel}`,
query: {}
}
)
})
})

describe('http://example.com/ipfs/<CID> URLs', () => {
Expand Down
38 changes: 38 additions & 0 deletions packages/verified-fetch/test/verified-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,42 @@
expect(output).to.deep.equal(obj)
})
})

describe('encoded URI paths', () => {
let verifiedFetch: VerifiedFetch

beforeEach(async () => {
verifiedFetch = new VerifiedFetch({
helia
})
})

afterEach(async () => {
await verifiedFetch.stop()
})

// tests for https://github.com/ipfs-shipyard/service-worker-gateway/issues/83 issue
it('should decode encoded URI paths', async () => {
const finalRootFileContent = new Uint8Array([0x01, 0x02, 0x03])

const fs = unixfs(helia)
const res = await last(fs.addAll([{
path: "Plan_d'exécution_du_second_étage_de_l'hôtel_de_Brionne_(dessin)_De_Cotte_2503c_–_Gallica_2011_(adjusted).jpg.webp",
content: finalRootFileContent
}], {
wrapWithDirectory: true
}))

if (res == null) {
throw new Error('Import failed')
}

Check warning on line 741 in packages/verified-fetch/test/verified-fetch.spec.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/test/verified-fetch.spec.ts#L740-L741

Added lines #L740 - L741 were not covered by tests
const { cid } = res
const resp = await verifiedFetch.fetch(`ipfs://${cid}/${encodeURIComponent("Plan_d'exécution_du_second_étage_de_l'hôtel_de_Brionne_(dessin)_De_Cotte_2503c_–_Gallica_2011_(adjusted).jpg.webp")}`)
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.statusText).to.equal('OK')
const data = await resp.arrayBuffer()
expect(new Uint8Array(data)).to.equalBytes(finalRootFileContent)
})
})
})
Loading