Skip to content

Commit

Permalink
feat: superhot support for w3s.link read (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Aug 8, 2022
1 parent 618e8c9 commit 533e7e1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DBClient } from './utils/db-client.js'
/**
* @typedef {Object} EnvInput
* @property {string} ENV
* @property {string} GATEWAY_DOMAIN
* @property {string[]} GATEWAY_DOMAINS
* @property {string} NFT_STORAGE_API
* @property {string} DATABASE_URL
* @property {string} DATABASE_TOKEN
Expand Down
14 changes: 11 additions & 3 deletions packages/api/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { InvalidUrlError } from '../errors.js'

import { normalizeCid } from './cid.js'

/**
* @typedef {import('../env').Env} Env
*/

/**
* Parses provided URL and verifes if is a valid nftstorage.link URL
*
Expand All @@ -25,9 +29,12 @@ export function getSourceUrl(request, env) {
`invalid URL provided: ${request.params.url}: maximum allowed length or URL is ${MAX_ALLOWED_URL_LENGTH}`
)
}
if (!urlString.includes(env.GATEWAY_DOMAIN)) {
if (
!env.GATEWAY_DOMAINS.filter((gwDomain) => urlString.includes(gwDomain))
.length
) {
throw new InvalidUrlError(
`invalid URL provided: ${urlString}: not nftstorage.link URL`
`invalid URL provided: ${urlString}: not ${env.GATEWAY_DOMAINS.join(' or ')} URL`
)
}

Expand All @@ -54,8 +61,9 @@ export function getNormalizedUrl(candidateUrl, env) {
? `?${queryParamsCandidate}`
: ''

// Always set normalized url as first URL in supported gateway domains (w3s.link)
return new URL(
`${candidateUrl.protocol}//${cid}.ipfs.${env.GATEWAY_DOMAIN}${path}${queryParams}`
`${candidateUrl.protocol}//${cid}.ipfs.${env.GATEWAY_DOMAINS[0]}${path}${queryParams}`
)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/api/test/perma-cache-get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,27 @@ test('Gets 404 response from perma cache by URL when url not perma cached', asyn
)
t.is(responseGet.status, 404)
})

test('Gets 400 response from perma cache by URL when url not valid', async (t) => {
const { mf } = t.context
const url =
'http://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:9083/'

// GET URL content from perma cache
const responseGet = await mf.dispatchFetch(getPermaCachePutUrl(url), {
method: 'GET',
})
t.is(responseGet.status, 400)
})

test('Gets 404 response from perma cache by URL when url from secondary supported gateway not perma cached', async (t) => {
const { mf } = t.context
const url =
'http://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:9082/'

// GET URL content from perma cache
const responseGet = await mf.dispatchFetch(getPermaCachePutUrl(url), {
method: 'GET',
})
t.is(responseGet.status, 404)
})
4 changes: 2 additions & 2 deletions packages/api/test/perma-cache-post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('Fails when invalid url is provided', async (t) => {
t.is(body, 'invalid URL provided: test.png: Invalid URL')
})

test('Fails when non nftstorage.link url is provided', async (t) => {
test('Fails when non localhost:9081 or localhost:9082 url is provided', async (t) => {
const { mf, user } = t.context

const response = await mf.dispatchFetch(
Expand All @@ -46,7 +46,7 @@ test('Fails when non nftstorage.link url is provided', async (t) => {
const body = await response.json()
t.is(
body,
'invalid URL provided: https://example.com/test.png: not nftstorage.link URL'
'invalid URL provided: https://example.com/test.png: not localhost:9081 or localhost:9082 URL'
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/scripts/worker-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const globals = {
NFT_STORAGE_API: 'http://localhost:9096',
DATABASE_TOKEN:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJyb2xlIjoic2VydmljZV9yb2xlIn0.necIJaiP7X2T2QjGeV-FhpkizcNTX8HjDDBAxpgQTEI',
GATEWAY_DOMAIN: 'localhost:9081',
GATEWAY_DOMAINS: ['localhost:9081', 'localhost:9082'],
}
2 changes: 1 addition & 1 deletion packages/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getParsedUrl(url) {
const queryParams = queryParamsString.length ? `?${queryParamsString}` : ''

normalizedUrl = new URL(
`${normalizedUrl.protocol}//${cid}.ipfs.${globals.GATEWAY_DOMAIN}${path}${queryParams}`
`${normalizedUrl.protocol}//${cid}.ipfs.${globals.GATEWAY_DOMAINS[0]}${path}${queryParams}`
)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/api/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DATABASE_URL = "https://nft-link-prod.herokuapp.com"
NFT_STORAGE_API = "https://api.nft.storage"
DEBUG = "false"
ENV = "production"
GATEWAY_DOMAIN = "nftstorage.link"
GATEWAY_DOMAINS = "[\"w3s.link\", \"nftstorage.link\"]"

[[env.production.r2_buckets]]
bucket_name = "super-hot"
Expand All @@ -46,7 +46,7 @@ DATABASE_URL = "https://nft-link-staging.herokuapp.com"
NFT_STORAGE_API = "https://api-staging.nft.storage"
DEBUG = "true"
ENV = "staging"
GATEWAY_DOMAIN = "nftstorage.link"
GATEWAY_DOMAINS = "[\"w3s.link\", \"nftstorage.link\"]"

[[env.staging.r2_buckets]]
bucket_name = "super-hot-staging"
Expand All @@ -65,7 +65,7 @@ workers_dev = true
[env.test.vars]
DEBUG = "true"
ENV = "test"
GATEWAY_DOMAIN = "localhost:9081"
GATEWAY_DOMAINS = "[\"localhost:9081\", \"localhost:9082\"]"

# Dev!
[env.vsantos]
Expand All @@ -75,4 +75,4 @@ account_id = "7ec0b7cf2ec201b2580374e53ba5f37b"
[env.vsantos.vars]
DEBUG = "true"
ENV = "test"
GATEWAY_DOMAIN = "nftstorage.link"
GATEWAY_DOMAINS = "[\"w3s.link\", \"nftstorage.link\"]"

0 comments on commit 533e7e1

Please sign in to comment.