@helia/verified-fetch 2.6.5 → 2.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/index.min.js +45 -45
  2. package/dist/src/plugins/plugin-handle-byte-range-context.d.ts +13 -0
  3. package/dist/src/plugins/plugin-handle-byte-range-context.d.ts.map +1 -0
  4. package/dist/src/plugins/plugin-handle-byte-range-context.js +19 -0
  5. package/dist/src/plugins/plugin-handle-byte-range-context.js.map +1 -0
  6. package/dist/src/plugins/plugin-handle-car.d.ts +1 -1
  7. package/dist/src/plugins/plugin-handle-car.d.ts.map +1 -1
  8. package/dist/src/plugins/plugin-handle-car.js +9 -2
  9. package/dist/src/plugins/plugin-handle-car.js.map +1 -1
  10. package/dist/src/plugins/plugin-handle-dag-cbor.d.ts +2 -2
  11. package/dist/src/plugins/plugin-handle-dag-cbor.d.ts.map +1 -1
  12. package/dist/src/plugins/plugin-handle-dag-cbor.js +7 -6
  13. package/dist/src/plugins/plugin-handle-dag-cbor.js.map +1 -1
  14. package/dist/src/plugins/plugin-handle-dag-pb.d.ts +2 -2
  15. package/dist/src/plugins/plugin-handle-dag-pb.d.ts.map +1 -1
  16. package/dist/src/plugins/plugin-handle-dag-pb.js +5 -6
  17. package/dist/src/plugins/plugin-handle-dag-pb.js.map +1 -1
  18. package/dist/src/plugins/plugin-handle-dir-index-html.d.ts +1 -1
  19. package/dist/src/plugins/plugin-handle-dir-index-html.d.ts.map +1 -1
  20. package/dist/src/plugins/plugin-handle-dir-index-html.js +7 -15
  21. package/dist/src/plugins/plugin-handle-dir-index-html.js.map +1 -1
  22. package/dist/src/plugins/plugin-handle-ipns-record.d.ts +2 -2
  23. package/dist/src/plugins/plugin-handle-ipns-record.d.ts.map +1 -1
  24. package/dist/src/plugins/plugin-handle-ipns-record.js +7 -3
  25. package/dist/src/plugins/plugin-handle-ipns-record.js.map +1 -1
  26. package/dist/src/plugins/plugin-handle-json.d.ts +2 -2
  27. package/dist/src/plugins/plugin-handle-json.d.ts.map +1 -1
  28. package/dist/src/plugins/plugin-handle-json.js +7 -3
  29. package/dist/src/plugins/plugin-handle-json.js.map +1 -1
  30. package/dist/src/plugins/plugin-handle-raw.d.ts +2 -2
  31. package/dist/src/plugins/plugin-handle-raw.d.ts.map +1 -1
  32. package/dist/src/plugins/plugin-handle-raw.js +6 -5
  33. package/dist/src/plugins/plugin-handle-raw.js.map +1 -1
  34. package/dist/src/plugins/plugin-handle-tar.d.ts +2 -2
  35. package/dist/src/plugins/plugin-handle-tar.d.ts.map +1 -1
  36. package/dist/src/plugins/plugin-handle-tar.js +7 -3
  37. package/dist/src/plugins/plugin-handle-tar.js.map +1 -1
  38. package/dist/src/plugins/types.d.ts +10 -1
  39. package/dist/src/plugins/types.d.ts.map +1 -1
  40. package/dist/src/utils/byte-range-context.d.ts.map +1 -1
  41. package/dist/src/utils/byte-range-context.js +1 -0
  42. package/dist/src/utils/byte-range-context.js.map +1 -1
  43. package/dist/src/verified-fetch.d.ts.map +1 -1
  44. package/dist/src/verified-fetch.js +13 -1
  45. package/dist/src/verified-fetch.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/plugins/plugin-handle-byte-range-context.ts +22 -0
  48. package/src/plugins/plugin-handle-car.ts +10 -3
  49. package/src/plugins/plugin-handle-dag-cbor.ts +10 -7
  50. package/src/plugins/plugin-handle-dag-pb.ts +6 -7
  51. package/src/plugins/plugin-handle-dir-index-html.ts +11 -16
  52. package/src/plugins/plugin-handle-ipns-record.ts +9 -4
  53. package/src/plugins/plugin-handle-json.ts +9 -4
  54. package/src/plugins/plugin-handle-raw.ts +7 -6
  55. package/src/plugins/plugin-handle-tar.ts +9 -4
  56. package/src/plugins/types.ts +10 -1
  57. package/src/utils/byte-range-context.ts +1 -0
  58. package/src/verified-fetch.ts +14 -1
@@ -107,6 +107,7 @@ export class ByteRangeContext {
107
107
  this.log.trace('returning body with byteStart=%o, byteEnd=%o, byteSize=%o', byteStart, byteEnd, byteSize)
108
108
  if (body instanceof ReadableStream) {
109
109
  // stream should already be spliced by `unixfs.cat`
110
+ // TODO: if the content is not unixfs and unixfs.cat was not called, we need to slice the body here.
110
111
  return body
111
112
  }
112
113
  return this.getSlicedBody(body)
@@ -4,6 +4,7 @@ import { prefixLogger } from '@libp2p/logger'
4
4
  import { LRUCache } from 'lru-cache'
5
5
  import { type CID } from 'multiformats/cid'
6
6
  import { CustomProgressEvent } from 'progress-events'
7
+ import { ByteRangeContextPlugin } from './plugins/plugin-handle-byte-range-context.js'
7
8
  import { CarPlugin } from './plugins/plugin-handle-car.js'
8
9
  import { DagCborPlugin } from './plugins/plugin-handle-dag-cbor.js'
9
10
  import { DagPbPlugin } from './plugins/plugin-handle-dag-pb.js'
@@ -90,6 +91,7 @@ export class VerifiedFetch {
90
91
 
91
92
  const defaultPlugins = [
92
93
  new DagWalkPlugin(pluginOptions),
94
+ new ByteRangeContextPlugin(pluginOptions),
93
95
  new IpnsRecordPlugin(pluginOptions),
94
96
  new CarPlugin(pluginOptions),
95
97
  new RawPlugin(pluginOptions),
@@ -151,13 +153,24 @@ export class VerifiedFetch {
151
153
  * Server-Timing header to the response if it has been collected. It should be used for any final processing of the
152
154
  * response before it is returned to the user.
153
155
  */
154
- private handleFinalResponse (response: Response, { query, cid, reqFormat, ttl, protocol, ipfsPath, pathDetails }: Partial<PluginContext> = {}): Response {
156
+ private handleFinalResponse (response: Response, { query, cid, reqFormat, ttl, protocol, ipfsPath, pathDetails, byteRangeContext }: Partial<PluginContext> = {}): Response {
155
157
  if (this.serverTimingHeaders.length > 0) {
156
158
  const headerString = this.serverTimingHeaders.join(', ')
157
159
  response.headers.set('Server-Timing', headerString)
158
160
  this.serverTimingHeaders = []
159
161
  }
160
162
 
163
+ // if there are multiple ranges, we should omit the content-length header. see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding
164
+ if (response.headers.get('Transfer-Encoding') !== 'chunked') {
165
+ if (byteRangeContext != null) {
166
+ const contentLength = byteRangeContext.length
167
+ if (contentLength != null) {
168
+ this.log.trace('Setting Content-Length from byteRangeContext: %d', contentLength)
169
+ response.headers.set('Content-Length', contentLength.toString())
170
+ }
171
+ }
172
+ }
173
+
161
174
  // set Content-Disposition header
162
175
  let contentDisposition: string | undefined
163
176