Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Shaw <[email protected]>
  • Loading branch information
vasco-santos and Alan Shaw committed Jan 14, 2022
1 parent c8a4a82 commit b13a6ae
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 62 deletions.
4 changes: 2 additions & 2 deletions packages/gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ One time set up of your cloudflare worker subdomain for dev:
```

- `npm run publish` - Publish the worker under your env. An alias for `wrangler publish --env $(whoami)`
- `npm start` - Run the worker in dev mode. An alias for `wrangler dev --env $(whoami)
- `npm start` - Run the worker in dev mode. An alias for `wrangler dev --env $(whoami)`

You only need to `npm start` for subsequent runs. PR your env config to the wrangler.toml, to celebrate 🎉
You only need to `npm start` for subsequent runs. PR your env config to the `wrangler.toml` to celebrate 🎉

## API

Expand Down
4 changes: 2 additions & 2 deletions packages/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "node scripts/cli.js build",
"dev": "miniflare --watch --debug",
"deploy": "wrangler publish --env production",
"pretest": "npm run build ",
"pretest": "npm run build",
"test": "npm-run-all -p -r mock:ipfs.io test:worker",
"test:worker": "ava --verbose test/*.spec.js",
"mock:ipfs.io": "smoke -p 9081 test/mocks/ipfs.io"
Expand All @@ -27,5 +27,5 @@
"smoke": "^3.1.1"
},
"author": "Vasco Santos <[email protected]>",
"license": "(Apache-2.0 AND MIT)"
"license": "Apache-2.0 OR MIT"
}
35 changes: 15 additions & 20 deletions packages/gateway/scripts/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@ prog
.describe('Build the worker.')
.option('--env', 'Environment', 'dev')
.action(async (opts) => {
try {
const version = `${pkg.name}@${pkg.version}-${opts.env}+${git.short(
__dirname
)}`
const version = `${pkg.name}@${pkg.version}-${opts.env}+${git.short(
__dirname
)}`

await build({
entryPoints: [path.join(__dirname, '../src/index.js')],
bundle: true,
format: 'esm',
outfile: 'dist/index.mjs',
legalComments: 'external',
define: {
global: 'globalThis',
},
minify: opts.env === 'dev' ? false : true,
sourcemap: true,
})
} catch (err) {
console.error(err)
process.exit(1)
}
await build({
entryPoints: [path.join(__dirname, '../src/index.js')],
bundle: true,
format: 'esm',
outfile: 'dist/index.mjs',
legalComments: 'external',
define: {
global: 'globalThis',
},
minify: opts.env === 'dev' ? false : true,
sourcemap: true,
})
})

prog.parse(process.argv)
8 changes: 1 addition & 7 deletions packages/gateway/src/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { JSONResponse } from './utils/json-response.js'

/**
* @param {Error & {status?: number;code?: string;}} err
*/
export function errorHandler(err) {
// TODO: setup sentry
console.error(err.stack)

let error = {
code: err.code || 'HTTP_ERROR',
message: err.message || 'Server Error',
}
let status = err.status || 500

return new JSONResponse(error, { status })
return new Response(err.message || 'Server Error', { status })
}
4 changes: 2 additions & 2 deletions packages/gateway/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class InvalidIpfsPathError extends Error {
*/
constructor(cid) {
super(`invalid ipfs path: invalid path "/ipfs/${cid}/"`)
this.name = 'InvalidIpfsPath'
this.name = 'InvalidIpfsPathError'
this.status = 400
this.code = InvalidIpfsPathError.CODE
}
Expand All @@ -17,7 +17,7 @@ export class InvalidUrlError extends Error {
*/
constructor(url) {
super(`invalid url: ${url}`)
this.name = 'InvalidUrl'
this.name = 'InvalidUrlError'
this.status = 400
this.code = InvalidUrlError.CODE
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gateway/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { getCidFromSubdomainUrl } from './utils/cid.js'
async function handleRequest(request, env) {
const publicGatewayUrl = new URL('ipfs', env.IPFS_GATEWAY)
const url = new URL(request.url)
const cid = getCidFromSubdomainUrl(url.hostname)
const cid = getCidFromSubdomainUrl(url)
const response = await fetch(
`${publicGatewayUrl.toString()}/${cid}${url.pathname || ''}`
`${publicGatewayUrl}/${cid}${url.pathname || ''}`
)

// forward gateway response
Expand Down
6 changes: 3 additions & 3 deletions packages/gateway/src/utils/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { InvalidIpfsPathError, InvalidUrlError } from '../errors.js'
/**
* Parse subdomain URL and return cid
*
* @param {string} url
* @param {URL} url
*/
export function getCidFromSubdomainUrl(url) {
// Replace "ipfs-staging" by "ipfs" if needed
const nUrl = url.replace('ipfs-staging', 'ipfs')
const nUrl = url.hostname.replace('ipfs-staging', 'ipfs')
const splitUrl = nUrl.split('.ipfs.')

if (!splitUrl.length) {
throw new InvalidUrlError(url)
throw new InvalidUrlError(url.hostname)
}

return normalizeCid(splitUrl[0])
Expand Down
16 changes: 0 additions & 16 deletions packages/gateway/src/utils/json-response.js

This file was deleted.

10 changes: 2 additions & 8 deletions packages/gateway/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import test from 'ava'
import { Miniflare } from 'miniflare'

import { InvalidIpfsPathError } from '../src/errors.js'

test.beforeEach((t) => {
// Create a new Miniflare environment for each test
const mf = new Miniflare({
Expand Down Expand Up @@ -31,12 +29,8 @@ test('Fails when invalid cid is provided', async (t) => {
)
t.is(response.status, 400)

const jsonResponse = await response.json()
t.is(jsonResponse.code, InvalidIpfsPathError.CODE)
t.is(
jsonResponse.message,
`invalid ipfs path: invalid path "/ipfs/${invalidCid}/"`
)
const textResponse = await response.text()
t.is(textResponse, `invalid ipfs path: invalid path "/ipfs/${invalidCid}/"`)
})

test('Gets content', async (t) => {
Expand Down

0 comments on commit b13a6ae

Please sign in to comment.