5.1.2 • Published 9 months ago

@web3-storage/gateway-lib v5.1.2

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
9 months ago

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

5.1.2

9 months ago

5.1.1

9 months ago

5.0.1

1 year ago

5.0.0

1 year ago

4.1.1

1 year ago

4.1.0

2 years ago

4.0.0

2 years ago

3.4.0

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.4.3

2 years ago

3.5.1

2 years ago

3.4.2

2 years ago

3.3.3

2 years ago

3.2.4

2 years ago

3.5.0

2 years ago

3.4.1

2 years ago

3.3.2

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.2.3

2 years ago

3.0.0

2 years ago

2.0.3

2 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago