-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: redirect to dweb link when ipns
- Loading branch information
1 parent
df21a37
commit d423086
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DNS_LABEL_MAX_LENGTH } from './constants.js' | ||
import { InvalidUrlError } from './errors.js' | ||
|
||
/** | ||
* Handle gateway request | ||
* | ||
* @param {Request} request | ||
* @param {import('./env').Env} env | ||
*/ | ||
export async function ipnsGet(request, env) { | ||
const name = request.params.name | ||
const reqUrl = new URL(request.url) | ||
const reqQueryString = reqUrl.searchParams.toString() | ||
|
||
// Get pathname to query from URL pathname avoiding potential name appear in the domain | ||
const redirectPath = reqUrl.pathname.split(name).slice(1).join(name) | ||
const redirectQueryString = reqQueryString ? `?${reqQueryString}` : '' | ||
const dnsLabel = toDNSLinkDNSLabel(name) | ||
|
||
const url = new URL( | ||
`https://${dnsLabel}.${env.IPNS_GATEWAY_HOSTNAME}${redirectPath}${redirectQueryString}` | ||
) | ||
|
||
return Response.redirect(url, 302) | ||
} | ||
|
||
/** | ||
* Converts a FQDN to DNS-safe representation that fits in 63 characters. | ||
* Example: my.v-long.example.com → my-v--long-example-com | ||
* @param {string} fqdn | ||
*/ | ||
function toDNSLinkDNSLabel(fqdn) { | ||
const dnsLabel = fqdn.replaceAll('-', '--').replaceAll('.', '-') | ||
|
||
if (dnsLabel.length > DNS_LABEL_MAX_LENGTH) { | ||
throw new InvalidUrlError( | ||
`invalid FQDN: ${fqdn}: longer than max length: ${DNS_LABEL_MAX_LENGTH}` | ||
) | ||
} | ||
|
||
return dnsLabel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import test from 'ava' | ||
import { createErrorHtmlContent } from '../src/errors.js' | ||
|
||
import { getMiniflare } from './utils.js' | ||
|
||
test.beforeEach((t) => { | ||
// Create a new Miniflare environment for each test | ||
t.context = { | ||
mf: getMiniflare(), | ||
} | ||
}) | ||
|
||
test('Fails when invalid name with IPNS canonical resolution', async (t) => { | ||
const { mf } = t.context | ||
|
||
const response = await mf.dispatchFetch( | ||
'https://localhost:8787/ipns/en.super-long-name-on-ipfs-exceeding-limit-from-ietf-rfc1034.org' | ||
) | ||
t.is(response.status, 400) | ||
|
||
const textResponse = await response.text() | ||
t.is( | ||
textResponse, | ||
createErrorHtmlContent( | ||
400, | ||
'invalid FQDN: en.super-long-name-on-ipfs-exceeding-limit-from-ietf-rfc1034.org: longer than max length: 63' | ||
) | ||
) | ||
}) | ||
|
||
test('should redirect to subdomain with IPNS canonical resolution', async (t) => { | ||
const { mf } = t.context | ||
|
||
const response = await mf.dispatchFetch( | ||
'https://localhost:8787/ipns/en.wikipedia-on-ipfs.org' | ||
) | ||
await response.waitUntil() | ||
t.is(response.status, 302) | ||
t.is( | ||
response.headers.get('location'), | ||
'https://en-wikipedia--on--ipfs-org.ipns.localhost:8787/' | ||
) | ||
}) | ||
|
||
test('should redirect to subdomain with IPNS canonical resolution keeping path and query params', async (t) => { | ||
const { mf } = t.context | ||
|
||
const response = await mf.dispatchFetch( | ||
'https://localhost:8787/ipns/en.wikipedia-on-ipfs.org/Energy?key=value' | ||
) | ||
await response.waitUntil() | ||
t.is(response.status, 302) | ||
t.is( | ||
response.headers.get('location'), | ||
'https://en-wikipedia--on--ipfs-org.ipns.localhost:8787/Energy?key=value' | ||
) | ||
}) | ||
|
||
test('should redirect to dweb.link with IPNS subdomain resolution', async (t) => { | ||
const { mf } = t.context | ||
|
||
const response = await mf.dispatchFetch( | ||
'https://en-wikipedia--on--ipfs-org.ipns.localhost:8787/Energy?key=value' | ||
) | ||
await response.waitUntil() | ||
t.is(response.status, 302) | ||
t.is( | ||
response.headers.get('location'), | ||
'https://en-wikipedia--on--ipfs-org.ipns.dweb.link/Energy?key=value' | ||
) | ||
}) |