Skip to content

Commit

Permalink
fix: add CORS plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 2, 2023
1 parent a3be11b commit 5913f9d
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 27 deletions.
6 changes: 6 additions & 0 deletions packages/interop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
],
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
Expand Down
26 changes: 25 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
],
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
Expand All @@ -26,6 +42,10 @@
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./routes": {
"types": "./dist/src/routes/index.d.ts",
"import": "./dist/src/routes/index.js"
}
},
"eslintConfig": {
Expand Down Expand Up @@ -122,19 +142,23 @@
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"dep-check": "aegir dep-check -i sinon",
"build": "aegir build --bundle false",
"test": "aegir test -t node",
"test:node": "aegir test -t node --cov",
"release": "aegir release"
},
"dependencies": {
"@fastify/cors": "^8.3.0",
"@helia/interface": "^1.1.1",
"@libp2p/interfaces": "^3.3.2",
"fastify": "^4.17.0",
"multiformats": "^11.0.2"
},
"devDependencies": {
"@libp2p/interface-peer-info": "^1.0.10",
"@libp2p/peer-id-factory": "^2.0.3",
"@multiformats/multiaddr": "^12.1.3",
"@types/sinon": "^10.0.15",
"aegir": "^39.0.4",
"sinon": "^15.1.0",
Expand Down
67 changes: 54 additions & 13 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,87 @@
/**
* @packageDocumentation
*
* Create a Helia node.
* Implements HTTP routes for a Fastify server that conform to the [Routing V1 HTTP API](https://specs.ipfs.tech/routing/http-routing-v1/).
*
* @example
*
* ```typescript
* import { MemoryDatastore } from 'datastore-core'
* import { MemoryBlockstore } from 'blockstore-core'
* import { createHelia } from 'helia'
* import { unixfs } from '@helia/unixfs'
* import { CID } from 'multiformats/cid'
* import { createRoutingV1HttpApiServer } from '@helia/routing-v1-http-api-server'
*
* const node = await createHelia({
* blockstore: new MemoryBlockstore(),
* datastore: new MemoryDatastore()
* const helia = await createHelia()
* const server = await createRoutingV1HttpApiServer(helia, {
* fastify: {
* // fastify options
* },
* listen: {
* // fastify listen options
* }
* })
* const fs = unixfs(node)
* fs.cat(CID.parse('bafyFoo'))
*
* // now make http requests
* ```
*
* Alternatively if you have a Fastify instance already you can add routes to it.
*
* @example
*
* ```typescript
* import fastify from 'fastify'
* import cors from '@fastify/cors'
* import { createHelia } from 'helia'
* import routes from '@helia/routing-v1-http-api-server/routes'
*
* const server = fastify({
* // fastify options
* })
* await server.register(cors, {
* origin: '*',
* methods: ['GET', 'OPTIONS'],
* strictPreflight: false
* })
*
* const helia = await createHelia()
*
* // configure Routing V1 HTTP API routes
* routes(server, helia)
*
* await server.listen({
* // fastify listen options
* })
*
* // now make http requests
* ```
*/

import fastify, { type FastifyHttpOptions, type FastifyHttpsOptions, type FastifyInstance } from 'fastify'
import cors from '@fastify/cors'
import fastify, { type FastifyListenOptions, type FastifyHttpOptions, type FastifyHttpsOptions, type FastifyInstance } from 'fastify'
import routes from './routes/index.js'
import type { Helia } from '@helia/interface'
import type * as http from 'node:http'
import type * as https from 'node:https'

export interface ServerInit {
port?: number
fastify?: FastifyHttpOptions<http.Server> | FastifyHttpsOptions<https.Server>
listen?: FastifyListenOptions
}

/**
* Create and return a Helia node
*/
export async function createRoutingV1HttpApiServer (helia: Helia, init: ServerInit = {}): Promise<FastifyInstance> {
const server = fastify(init.fastify)
await server.register(cors, {
origin: '*',
methods: ['GET', 'OPTIONS'],
strictPreflight: false
})

routes(server, helia)

await server.listen({ port: init.port ?? 0 })
await server.listen(init.listen ?? {
port: 0
})

return server
}
42 changes: 33 additions & 9 deletions packages/server/src/routes/routing/v1/providers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,53 @@ export default function getProvidersV1 (fastify: FastifyInstance, helia: Helia):
}
},
handler: async (request, reply) => {
const { cid: cidStr } = request.params
const cid = CID.parse(cidStr)
let cid: CID

try {
const { cid: cidStr } = request.params
cid = CID.parse(cidStr)
} catch (err) {
// these are .thenables but not .catchables?
reply.code(422).type('text/html').send('Unprocessable Entity') // eslint-disable-line @typescript-eslint/no-floating-promises
return
}

if (request.headers.accept?.includes('application/x-ndjson') === true) {
const stream = new PassThrough()

// these are .thenables but not .catchables?
reply.header('Content-Type', 'application/x-ndjson') // eslint-disable-line @typescript-eslint/no-floating-promises
reply.send(stream) // eslint-disable-line @typescript-eslint/no-floating-promises

try {
let found = 0

for await (const prov of streamingHandler(cid, helia)) {
if (found === 0) {
// these are .thenables but not .catchables?
reply.header('Content-Type', 'application/x-ndjson') // eslint-disable-line @typescript-eslint/no-floating-promises
reply.send(stream) // eslint-disable-line @typescript-eslint/no-floating-promises
}

found++

stream.push(JSON.stringify(prov) + '\n')
}

if (found > 0) {
return
}
} finally {
stream.end()
}
} else {
// this is .thenable but not .catchable?
reply.header('Content-Type', 'application/json') // eslint-disable-line @typescript-eslint/no-floating-promises
const result = await nonStreamingHandler(cid, helia)

return nonStreamingHandler(cid, helia)
if (result.Providers.length > 0) {
// this is .thenable but not .catchable?
reply.header('Content-Type', 'application/json') // eslint-disable-line @typescript-eslint/no-floating-promises

return reply.send(result)
}
}

reply.callNotFound()
}
})
}
Expand Down
Loading

0 comments on commit 5913f9d

Please sign in to comment.