@helia/verified-fetch 7.2.12 → 7.2.13

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 (51) hide show
  1. package/dist/index.min.js +32 -32
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/index.d.ts +5 -5
  4. package/dist/src/index.js +6 -6
  5. package/dist/src/plugins/index.d.ts +1 -1
  6. package/dist/src/plugins/index.js +1 -1
  7. package/dist/src/plugins/plugin-base.d.ts +1 -1
  8. package/dist/src/plugins/plugin-handle-car.d.ts +2 -2
  9. package/dist/src/plugins/plugin-handle-car.js +2 -2
  10. package/dist/src/plugins/plugin-handle-ipld.d.ts +1 -1
  11. package/dist/src/plugins/plugin-handle-ipld.js +2 -2
  12. package/dist/src/plugins/plugin-handle-ipns-record.d.ts +2 -2
  13. package/dist/src/plugins/plugin-handle-ipns-record.js +2 -2
  14. package/dist/src/plugins/plugin-handle-raw.d.ts +1 -1
  15. package/dist/src/plugins/plugin-handle-raw.js +2 -2
  16. package/dist/src/plugins/plugin-handle-tar.d.ts +2 -2
  17. package/dist/src/plugins/plugin-handle-tar.js +3 -3
  18. package/dist/src/plugins/plugin-handle-unixfs.d.ts +2 -2
  19. package/dist/src/plugins/plugin-handle-unixfs.js +3 -3
  20. package/dist/src/plugins/plugin-handle-unixfs.js.map +1 -1
  21. package/dist/src/singleton.d.ts +1 -1
  22. package/dist/src/singleton.js +1 -1
  23. package/dist/src/utils/error-to-response.d.ts +1 -1
  24. package/dist/src/utils/error-to-response.js +1 -1
  25. package/dist/src/utils/get-content-type.d.ts +1 -1
  26. package/dist/src/utils/get-content-type.js +2 -2
  27. package/dist/src/utils/response-headers.js +1 -1
  28. package/dist/src/utils/responses.d.ts +1 -1
  29. package/dist/src/utils/responses.d.ts.map +1 -1
  30. package/dist/src/utils/responses.js +39 -5
  31. package/dist/src/utils/responses.js.map +1 -1
  32. package/dist/src/verified-fetch.d.ts +1 -1
  33. package/dist/src/verified-fetch.js +10 -10
  34. package/package.json +2 -2
  35. package/src/index.ts +7 -7
  36. package/src/plugins/index.ts +1 -1
  37. package/src/plugins/plugin-base.ts +1 -1
  38. package/src/plugins/plugin-handle-car.ts +3 -3
  39. package/src/plugins/plugin-handle-ipld.ts +2 -2
  40. package/src/plugins/plugin-handle-ipns-record.ts +3 -3
  41. package/src/plugins/plugin-handle-raw.ts +2 -2
  42. package/src/plugins/plugin-handle-tar.ts +4 -4
  43. package/src/plugins/plugin-handle-unixfs.ts +4 -4
  44. package/src/singleton.ts +2 -2
  45. package/src/utils/error-to-response.ts +2 -2
  46. package/src/utils/get-content-type.ts +3 -3
  47. package/src/utils/libp2p-defaults.browser.ts +1 -1
  48. package/src/utils/libp2p-defaults.ts +1 -1
  49. package/src/utils/response-headers.ts +1 -1
  50. package/src/utils/responses.ts +50 -6
  51. package/src/verified-fetch.ts +11 -11
@@ -2,8 +2,8 @@ import { isPromise } from '@libp2p/utils'
2
2
  import toBuffer from 'it-to-buffer'
3
3
  import { MEDIA_TYPE_OCTET_STREAM, MEDIA_TYPE_RAW } from '../utils/content-types.ts'
4
4
  import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
5
- import { okResponse, partialContentResponse } from '../utils/responses.js'
6
- import { BasePlugin } from './plugin-base.js'
5
+ import { okResponse, partialContentResponse } from '../utils/responses.ts'
6
+ import { BasePlugin } from './plugin-base.ts'
7
7
  import type { PluginContext } from '../index.ts'
8
8
 
9
9
  /**
@@ -4,10 +4,10 @@ import toBrowserReadableStream from 'it-to-browser-readablestream'
4
4
  import { code as rawCode } from 'multiformats/codecs/raw'
5
5
  import { MEDIA_TYPE_TAR } from '../utils/content-types.ts'
6
6
  import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
7
- import { tarStream } from '../utils/get-tar-stream.js'
8
- import { badRequestResponse, okResponse } from '../utils/responses.js'
9
- import { BasePlugin } from './plugin-base.js'
10
- import type { PluginContext } from '../index.js'
7
+ import { tarStream } from '../utils/get-tar-stream.ts'
8
+ import { badRequestResponse, okResponse } from '../utils/responses.ts'
9
+ import { BasePlugin } from './plugin-base.ts'
10
+ import type { PluginContext } from '../index.ts'
11
11
 
12
12
  /**
13
13
  * Accepts a UnixFS `CID` and returns a `.tar` file containing the file or
@@ -8,9 +8,9 @@ import toBuffer from 'it-to-buffer'
8
8
  import * as raw from 'multiformats/codecs/raw'
9
9
  import { MEDIA_TYPE_OCTET_STREAM, MEDIA_TYPE_DAG_PB } from '../utils/content-types.ts'
10
10
  import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
11
- import { badGatewayResponse, movedPermanentlyResponse, partialContentResponse, okResponse } from '../utils/responses.js'
12
- import { BasePlugin } from './plugin-base.js'
13
- import type { PluginContext, Resource } from '../index.js'
11
+ import { badGatewayResponse, movedPermanentlyResponse, partialContentResponse, okResponse } from '../utils/responses.ts'
12
+ import { BasePlugin } from './plugin-base.ts'
13
+ import type { PluginContext, Resource } from '../index.ts'
14
14
  import type { RangeHeader } from '../utils/get-range-header.ts'
15
15
  import type { AbortOptions } from '@libp2p/interface'
16
16
  import type { IdentityNode, RawNode, UnixFSEntry, UnixFSFile } from 'ipfs-unixfs-exporter'
@@ -62,7 +62,7 @@ export class UnixFSPlugin extends BasePlugin {
62
62
  let entry: UnixFSEntry
63
63
 
64
64
  try {
65
- entry = await exporter(terminalElement.cid, blockstore, context)
65
+ entry = await exporter(`/ipfs/${terminalElement.cid}`, blockstore, context)
66
66
  } catch (err: any) {
67
67
  // throw abort error if signal was aborted
68
68
  context?.signal?.throwIfAborted()
package/src/singleton.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { createVerifiedFetch } from './index.js'
2
- import type { Resource, VerifiedFetch, VerifiedFetchInit } from './index.js'
1
+ import { createVerifiedFetch } from './index.ts'
2
+ import type { Resource, VerifiedFetch, VerifiedFetchInit } from './index.ts'
3
3
 
4
4
  let impl: VerifiedFetch | undefined
5
5
 
@@ -1,5 +1,5 @@
1
- import { badGatewayResponse, badRequestResponse, gatewayTimeoutResponse, internalServerErrorResponse, notFoundResponse, preconditionFailedResponse } from './responses.js'
2
- import type { Resource } from '../index.js'
1
+ import { badGatewayResponse, badRequestResponse, gatewayTimeoutResponse, internalServerErrorResponse, notFoundResponse, preconditionFailedResponse } from './responses.ts'
2
+ import type { Resource } from '../index.ts'
3
3
 
4
4
  export function errorToResponse (resource: Resource | string, err: any, init?: RequestInit): Response {
5
5
  // throw an AbortError if the passed signal has aborted
@@ -1,6 +1,6 @@
1
- import { defaultMimeType } from './content-type-parser.js'
2
- import { isPromise } from './type-guards.js'
3
- import type { ContentTypeParser } from '../index.js'
1
+ import { defaultMimeType } from './content-type-parser.ts'
2
+ import { isPromise } from './type-guards.ts'
3
+ import type { ContentTypeParser } from '../index.ts'
4
4
  import type { Logger } from '@libp2p/interface'
5
5
 
6
6
  export interface GetContentTypeOptions {
@@ -1,7 +1,7 @@
1
1
  import { webRTCDirect } from '@libp2p/webrtc'
2
2
  import { webSockets } from '@libp2p/websockets'
3
3
  import { libp2pDefaults } from 'helia'
4
- import type { ServiceFactoryMap } from './libp2p-types.js'
4
+ import type { ServiceFactoryMap } from './libp2p-types.ts'
5
5
  import type { DefaultLibp2pServices } from 'helia'
6
6
  import type { Libp2pOptions } from 'libp2p'
7
7
 
@@ -2,7 +2,7 @@ import { kadDHT } from '@libp2p/kad-dht'
2
2
  import { libp2pDefaults } from 'helia'
3
3
  import { ipnsSelector } from 'ipns/selector'
4
4
  import { ipnsValidator } from 'ipns/validator'
5
- import type { ServiceFactoryMap } from './libp2p-types.js'
5
+ import type { ServiceFactoryMap } from './libp2p-types.ts'
6
6
  import type { DefaultLibp2pServices } from 'helia'
7
7
  import type { Libp2pOptions } from 'libp2p'
8
8
 
@@ -1,4 +1,4 @@
1
- import { InvalidRangeError } from '../errors.js'
1
+ import { InvalidRangeError } from '../errors.ts'
2
2
 
3
3
  interface CacheControlHeaderOptions {
4
4
  /**
@@ -3,7 +3,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
3
  import { errorToObject } from './error-to-object.ts'
4
4
  import { rangeToOffsetAndLength } from './get-offset-and-length.ts'
5
5
  import { getContentRangeHeader } from './response-headers.ts'
6
- import type { SupportedBodyTypes, ContentType, Resource } from '../index.js'
6
+ import type { SupportedBodyTypes, ContentType, Resource } from '../index.ts'
7
7
  import type { Range, RangeHeader } from './get-range-header.ts'
8
8
 
9
9
  function setField (response: Response, name: string, value: string | boolean): void {
@@ -38,12 +38,56 @@ function setRedirected (response: Response): void {
38
38
  setField(response, 'redirected', true)
39
39
  }
40
40
 
41
+ function isArrayBufferBacked (buf: Uint8Array): buf is Uint8Array<ArrayBuffer> {
42
+ return buf.buffer instanceof ArrayBuffer
43
+ }
44
+
45
+ function withArrayBuffer (buf: Uint8Array): Uint8Array<ArrayBuffer> {
46
+ if (isArrayBufferBacked(buf)) {
47
+ return buf
48
+ }
49
+
50
+ const arr = new ArrayBuffer(buf.byteLength)
51
+ const u8a = new Uint8Array(arr)
52
+ u8a.set(buf)
53
+
54
+ return u8a
55
+ }
56
+
57
+ function isReadableStream <T> (obj?: any): obj is ReadableStream<T> {
58
+ if (obj == null) {
59
+ return false
60
+ }
61
+
62
+ return typeof obj.pipeThrough === 'function' && typeof obj.getReader === 'function'
63
+ }
64
+
65
+ function normalizeBody (body?: SupportedBodyTypes): string | Uint8Array<ArrayBuffer> | ArrayBuffer | Blob | ReadableStream<Uint8Array<ArrayBuffer>> | null | undefined {
66
+ if (body == null) {
67
+ return body
68
+ }
69
+
70
+ if (body instanceof Uint8Array) {
71
+ return withArrayBuffer(body)
72
+ }
73
+
74
+ if (isReadableStream(body)) {
75
+ return body.pipeThrough(new TransformStream({
76
+ transform (chunk, controller) {
77
+ controller.enqueue(withArrayBuffer(chunk))
78
+ }
79
+ }))
80
+ }
81
+
82
+ return body
83
+ }
84
+
41
85
  export interface ResponseOptions extends ResponseInit {
42
86
  redirected?: boolean
43
87
  }
44
88
 
45
89
  export function okResponse (url: Resource, body?: SupportedBodyTypes, init?: ResponseOptions): Response {
46
- const response = new Response(body, {
90
+ const response = new Response(normalizeBody(body), {
47
91
  ...(init ?? {}),
48
92
  status: 200,
49
93
  statusText: 'OK'
@@ -81,7 +125,7 @@ export function internalServerErrorResponse (url: Resource, err: Error, init?: R
81
125
  * A 504 Gateway Timeout for when a request made to an upstream server timed out
82
126
  */
83
127
  export function gatewayTimeoutResponse (url: Resource, body?: SupportedBodyTypes, init?: ResponseInit): Response {
84
- const response = new Response(body, {
128
+ const response = new Response(normalizeBody(body), {
85
129
  ...(init ?? {}),
86
130
  status: 504,
87
131
  statusText: 'Gateway Timeout'
@@ -98,7 +142,7 @@ export function gatewayTimeoutResponse (url: Resource, body?: SupportedBodyTypes
98
142
  * upstream server.
99
143
  */
100
144
  export function badGatewayResponse (url: Resource, body?: SupportedBodyTypes, init?: ResponseInit): Response {
101
- const response = new Response(body, {
145
+ const response = new Response(normalizeBody(body), {
102
146
  ...(init ?? {}),
103
147
  status: 502,
104
148
  statusText: 'Bad Gateway'
@@ -111,7 +155,7 @@ export function badGatewayResponse (url: Resource, body?: SupportedBodyTypes, in
111
155
  }
112
156
 
113
157
  export function notImplementedResponse (url: Resource, body?: SupportedBodyTypes, init?: ResponseInit): Response {
114
- const response = new Response(body, {
158
+ const response = new Response(normalizeBody(body), {
115
159
  ...(init ?? {}),
116
160
  status: 501,
117
161
  statusText: 'Not Implemented'
@@ -145,7 +189,7 @@ export function notAcceptableResponse (url: Resource, requested: Array<Pick<Cont
145
189
  }
146
190
 
147
191
  export function notFoundResponse (url: Resource, body?: SupportedBodyTypes, init?: ResponseInit): Response {
148
- const response = new Response(body, {
192
+ const response = new Response(normalizeBody(body), {
149
193
  ...(init ?? {}),
150
194
  status: 404,
151
195
  statusText: 'Not Found'
@@ -5,25 +5,25 @@ import { CID } from 'multiformats/cid'
5
5
  import { CustomProgressEvent } from 'progress-events'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
7
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
- import { CarPlugin } from './plugins/plugin-handle-car.js'
9
- import { IpldPlugin } from './plugins/plugin-handle-ipld.js'
10
- import { IpnsRecordPlugin } from './plugins/plugin-handle-ipns-record.js'
8
+ import { CarPlugin } from './plugins/plugin-handle-car.ts'
9
+ import { IpldPlugin } from './plugins/plugin-handle-ipld.ts'
10
+ import { IpnsRecordPlugin } from './plugins/plugin-handle-ipns-record.ts'
11
11
  import { RawPlugin } from './plugins/plugin-handle-raw.ts'
12
- import { TarPlugin } from './plugins/plugin-handle-tar.js'
13
- import { UnixFSPlugin } from './plugins/plugin-handle-unixfs.js'
12
+ import { TarPlugin } from './plugins/plugin-handle-tar.ts'
13
+ import { UnixFSPlugin } from './plugins/plugin-handle-unixfs.ts'
14
14
  import { URLResolver } from './url-resolver.ts'
15
15
  import { abbreviate, abbreviateAddress } from './utils/abbreviate.ts'
16
- import { contentTypeParser } from './utils/content-type-parser.js'
16
+ import { contentTypeParser } from './utils/content-type-parser.ts'
17
17
  import { getContentType, getSupportedContentTypes, CONTENT_TYPE_OCTET_STREAM, MEDIA_TYPE_IPNS_RECORD, CONTENT_TYPE_IPNS } from './utils/content-types.ts'
18
18
  import { errorToObject } from './utils/error-to-object.ts'
19
19
  import { errorToResponse } from './utils/error-to-response.ts'
20
- import { getETag, ifNoneMatches } from './utils/get-e-tag.js'
20
+ import { getETag, ifNoneMatches } from './utils/get-e-tag.ts'
21
21
  import { getRangeHeader } from './utils/get-range-header.ts'
22
22
  import { stringToIpfsUrl } from './utils/parse-resource.ts'
23
- import { setCacheControlHeader } from './utils/response-headers.js'
24
- import { notAcceptableResponse, notImplementedResponse, notModifiedResponse } from './utils/responses.js'
25
- import { ServerTiming } from './utils/server-timing.js'
26
- import type { AcceptHeader, CIDDetail, ContentTypeParser, CreateVerifiedFetchOptions, Resource, ResourceDetail, VerifiedFetchInit as VerifiedFetchOptions, VerifiedFetchPlugin, PluginContext, PluginOptions } from './index.js'
23
+ import { setCacheControlHeader } from './utils/response-headers.ts'
24
+ import { notAcceptableResponse, notImplementedResponse, notModifiedResponse } from './utils/responses.ts'
25
+ import { ServerTiming } from './utils/server-timing.ts'
26
+ import type { AcceptHeader, CIDDetail, ContentTypeParser, CreateVerifiedFetchOptions, Resource, ResourceDetail, VerifiedFetchInit as VerifiedFetchOptions, VerifiedFetchPlugin, PluginContext, PluginOptions } from './index.ts'
27
27
  import type { DNSLink } from '@helia/dnslink'
28
28
  import type { Helia } from '@helia/interface'
29
29
  import type { IPNSResolver } from '@helia/ipns'