UNPKG

@helia/verified-fetch

Version:

A fetch-like API for obtaining verified & trustless IPFS content on the web

31 lines (25 loc) 1.01 kB
import { ByteRangeContext } from '../utils/byte-range-context.js' import { badRangeResponse } from '../utils/responses.js' import { BasePlugin } from './plugin-base.js' import type { PluginContext } from './types.js' /** * This plugin simply adds the ByteRangeContext to the PluginContext. */ export class ByteRangeContextPlugin extends BasePlugin { readonly id = 'byte-range-context-plugin' /** * Return false if the ByteRangeContext has already been set, otherwise return true. */ canHandle (context: PluginContext): boolean { return context.byteRangeContext == null } async handle (context: PluginContext): Promise<Response | null> { context.byteRangeContext = new ByteRangeContext(this.pluginOptions.logger, context.options?.headers) context.modified++ if (context.byteRangeContext.isRangeRequest && !context.byteRangeContext.isValidRangeRequest) { // invalid range request.. fail return badRangeResponse(context.resource) } return null } }