-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3be11b
commit 5913f9d
Showing
5 changed files
with
286 additions
and
27 deletions.
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
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 | ||
} |
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
Oops, something went wrong.