@web3-storage/gateway-lib
TypeScript icon, indicating that this package has built-in type declarations

4.1.1 • Public • Published

gateway-lib

Shared library of components for building an IPFS gateway in Cloudflare workers.

Install

npm install @web3-storage/gateway-lib

Usage

This module provides middleware, handlers and some utilities. Handlers require certain context properties to set which the middlewares can provide. The only BYO requirement is a middleware that adds a dagula instance to the context.

Typical usage may be something like:

import {
  withContext,
  withCorsHeaders,
  withContentDispositionHeader,
  withErrorHandler,
  withHttpGet,
  withParsedIpfsUrl,
  composeMiddleware
} from '@web3-storage/gateway-lib/middleware'
import {
  handleUnixfs,
  handleBlock,
  handleCar
} from '@web3-storage/gateway-lib/handlers'

export default {
  fetch (request, env, ctx) {
    const middleware = composeMiddleware(
      withContext,
      withCorsHeaders,
      withContentDispositionHeader,
      withErrorHandler,
      withHttpGet,
      withParsedIpfsUrl,
      withDagula // Note: provided by library consumer (sets `dagula` on ctx)
    )
    return middleware(handler)(request, env, ctx)
  }
}

async function handler (request, env, ctx) {
  const { searchParams } = ctx
  if (searchParams.get('format') === 'raw') {
    return await handleBlock(request, env, ctx)
  }
  if (searchParams.get('format') === 'car') {
    return await handleCar(request, env, ctx)
  }
  return await handleUnixfs(request, env, ctx)
}

Contributing

Feel free to join in. All welcome. Open an issue!

License

Dual-licensed under MIT + Apache 2.0

Readme

Keywords

Package Sidebar

Install

npm i @web3-storage/gateway-lib

Weekly Downloads

1

Version

4.1.1

License

Apache-2.0 OR MIT

Unpacked Size

403 kB

Total Files

55

Last publish

Collaborators

  • it-dag-house
  • gozala
  • olizilla
  • vascosantos
  • alanshaw