Skip to content

Commit

Permalink
fix: url parsing in firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jan 19, 2024
1 parent 22cadfe commit 16a2629
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions packages/verified-fetch/src/utils/parse-url-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,17 @@ export interface ParseUrlStringOptions {
ipns: IPNS
}

const URL_REGEX = /^(?<protocol>ip[fn]s):\/\/(?<cidOrPeerIdOrDnsLink>[^/$]+)\/?(?<path>[^$^?]*)/

/**
* A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
*/
export async function parseUrlString ({ urlString, ipns }: ParseUrlStringOptions): Promise<ParsedUrlStringResults> {
const url = new URL(urlString)
const protocol = url.protocol.slice(0, -1)
let hostnameRecognized = true
if (url.pathname.slice(0, 2) === '//') {
// Browser and NodeJS URL parser handles `ipfs://` URL hostnames differently.
hostnameRecognized = false
const match = urlString.match(URL_REGEX)
if (match == null || match.groups == null) {
throw new TypeError(`Invalid URL: ${urlString}`)
}

Check warning on line 28 in packages/verified-fetch/src/utils/parse-url-string.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/parse-url-string.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
const urlPathParts = hostnameRecognized ? url.pathname.slice(1).split('/') : url.pathname.slice(2).split('/')
const cidOrPeerIdOrDnsLink = hostnameRecognized ? url.hostname : urlPathParts.shift() as string

const remainderPath = urlPathParts.map(decodeURIComponent).join('/')
const path = remainderPath.length > 0 ? remainderPath : ''
const { protocol, cidOrPeerIdOrDnsLink, path } = match.groups

let cid: CID | null = null
if (protocol === 'ipfs') {
Expand Down

0 comments on commit 16a2629

Please sign in to comment.