4.1.1 • Published 4 months ago

@web3-storage/gateway-lib v4.1.1

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
4 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

4.1.1

4 months ago

4.1.0

5 months ago

4.0.0

5 months ago

3.4.0

9 months ago

3.3.1

10 months ago

3.3.0

10 months ago

3.4.3

9 months ago

3.5.1

8 months ago

3.4.2

9 months ago

3.3.3

9 months ago

3.2.4

10 months ago

3.5.0

9 months ago

3.4.1

9 months ago

3.3.2

10 months ago

3.2.2

11 months ago

3.2.1

11 months ago

3.2.0

11 months ago

3.1.1

12 months ago

3.1.0

12 months ago

3.2.3

11 months ago

3.0.0

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago