@helia/verified-fetch 7.2.12 → 7.2.14
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.
- package/dist/index.min.js +32 -32
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +5 -5
- package/dist/src/index.js +6 -6
- package/dist/src/plugins/index.d.ts +1 -1
- package/dist/src/plugins/index.js +1 -1
- package/dist/src/plugins/plugin-base.d.ts +1 -1
- package/dist/src/plugins/plugin-handle-car.d.ts +2 -2
- package/dist/src/plugins/plugin-handle-car.js +2 -2
- package/dist/src/plugins/plugin-handle-ipld.d.ts +1 -1
- package/dist/src/plugins/plugin-handle-ipld.js +2 -2
- package/dist/src/plugins/plugin-handle-ipns-record.d.ts +2 -2
- package/dist/src/plugins/plugin-handle-ipns-record.js +2 -2
- package/dist/src/plugins/plugin-handle-raw.d.ts +1 -1
- package/dist/src/plugins/plugin-handle-raw.js +2 -2
- package/dist/src/plugins/plugin-handle-tar.d.ts +2 -2
- package/dist/src/plugins/plugin-handle-tar.js +3 -3
- package/dist/src/plugins/plugin-handle-unixfs.d.ts +2 -2
- package/dist/src/plugins/plugin-handle-unixfs.js +3 -3
- package/dist/src/plugins/plugin-handle-unixfs.js.map +1 -1
- package/dist/src/singleton.d.ts +1 -1
- package/dist/src/singleton.js +1 -1
- package/dist/src/utils/content-type-parser.d.ts.map +1 -1
- package/dist/src/utils/content-type-parser.js +3 -1
- package/dist/src/utils/content-type-parser.js.map +1 -1
- package/dist/src/utils/error-to-response.d.ts +1 -1
- package/dist/src/utils/error-to-response.js +1 -1
- package/dist/src/utils/get-content-type.d.ts +1 -1
- package/dist/src/utils/get-content-type.js +2 -2
- package/dist/src/utils/response-headers.js +1 -1
- package/dist/src/utils/responses.d.ts +1 -1
- package/dist/src/utils/responses.d.ts.map +1 -1
- package/dist/src/utils/responses.js +39 -5
- package/dist/src/utils/responses.js.map +1 -1
- package/dist/src/verified-fetch.d.ts +1 -1
- package/dist/src/verified-fetch.js +10 -10
- package/package.json +2 -2
- package/src/index.ts +7 -7
- package/src/plugins/index.ts +1 -1
- package/src/plugins/plugin-base.ts +1 -1
- package/src/plugins/plugin-handle-car.ts +3 -3
- package/src/plugins/plugin-handle-ipld.ts +2 -2
- package/src/plugins/plugin-handle-ipns-record.ts +3 -3
- package/src/plugins/plugin-handle-raw.ts +2 -2
- package/src/plugins/plugin-handle-tar.ts +4 -4
- package/src/plugins/plugin-handle-unixfs.ts +4 -4
- package/src/singleton.ts +2 -2
- package/src/utils/content-type-parser.ts +3 -1
- package/src/utils/error-to-response.ts +2 -2
- package/src/utils/get-content-type.ts +3 -3
- package/src/utils/libp2p-defaults.browser.ts +1 -1
- package/src/utils/libp2p-defaults.ts +1 -1
- package/src/utils/response-headers.ts +1 -1
- package/src/utils/responses.ts +50 -6
- package/src/verified-fetch.ts +11 -11
|
@@ -6,9 +6,9 @@ import toBrowserReadableStream from 'it-to-browser-readablestream'
|
|
|
6
6
|
import { CONTENT_TYPE_CAR, MEDIA_TYPE_CAR } from '../utils/content-types.ts'
|
|
7
7
|
import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
|
|
8
8
|
import { entityBytesToOffsetAndLength } from '../utils/get-offset-and-length.ts'
|
|
9
|
-
import { badRequestResponse, notAcceptableResponse, okResponse } from '../utils/responses.
|
|
10
|
-
import { BasePlugin } from './plugin-base.
|
|
11
|
-
import type { PluginContext } from '../index.
|
|
9
|
+
import { badRequestResponse, notAcceptableResponse, okResponse } from '../utils/responses.ts'
|
|
10
|
+
import { BasePlugin } from './plugin-base.ts'
|
|
11
|
+
import type { PluginContext } from '../index.ts'
|
|
12
12
|
import type { ExportCarOptions, UnixFSExporterOptions } from '@helia/car'
|
|
13
13
|
|
|
14
14
|
function getFilename (ipfsPath: string): string {
|
|
@@ -9,8 +9,8 @@ import { CODEC_CBOR } from '../constants.ts'
|
|
|
9
9
|
import { getContentTypesForCid, MEDIA_TYPE_CBOR, MEDIA_TYPE_DAG_CBOR, MEDIA_TYPE_DAG_JSON, MEDIA_TYPE_JSON } from '../utils/content-types.ts'
|
|
10
10
|
import { convertOutput } from '../utils/convert-output.ts'
|
|
11
11
|
import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
|
|
12
|
-
import { notAcceptableResponse, okResponse, partialContentResponse } from '../utils/responses.
|
|
13
|
-
import { BasePlugin } from './plugin-base.
|
|
12
|
+
import { notAcceptableResponse, okResponse, partialContentResponse } from '../utils/responses.ts'
|
|
13
|
+
import { BasePlugin } from './plugin-base.ts'
|
|
14
14
|
import type { ContentType, PluginContext } from '../index.ts'
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -2,9 +2,9 @@ import { peerIdFromString } from '@libp2p/peer-id'
|
|
|
2
2
|
import { marshalIPNSRecord } from 'ipns'
|
|
3
3
|
import { CONTENT_TYPE_IPNS, MEDIA_TYPE_IPNS_RECORD } from '../utils/content-types.ts'
|
|
4
4
|
import { getContentDispositionFilename } from '../utils/get-content-disposition-filename.ts'
|
|
5
|
-
import { badRequestResponse, okResponse } from '../utils/responses.
|
|
6
|
-
import { BasePlugin } from './plugin-base.
|
|
7
|
-
import type { PluginContext } from '../index.
|
|
5
|
+
import { badRequestResponse, okResponse } from '../utils/responses.ts'
|
|
6
|
+
import { BasePlugin } from './plugin-base.ts'
|
|
7
|
+
import type { PluginContext } from '../index.ts'
|
|
8
8
|
import type { PeerId } from '@libp2p/interface'
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -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.
|
|
6
|
-
import { BasePlugin } from './plugin-base.
|
|
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.
|
|
8
|
-
import { badRequestResponse, okResponse } from '../utils/responses.
|
|
9
|
-
import { BasePlugin } from './plugin-base.
|
|
10
|
-
import type { PluginContext } from '../index.
|
|
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.
|
|
12
|
-
import { BasePlugin } from './plugin-base.
|
|
13
|
-
import type { PluginContext, Resource } from '../index.
|
|
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
|
|
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.
|
|
2
|
-
import type { Resource, VerifiedFetch, VerifiedFetchInit } from './index.
|
|
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
|
|
|
@@ -67,7 +67,9 @@ export async function contentTypeParser (bytes: Uint8Array, fileName?: string):
|
|
|
67
67
|
case 'html':
|
|
68
68
|
return 'text/html; charset=utf-8'
|
|
69
69
|
case 'js':
|
|
70
|
-
|
|
70
|
+
case 'mjs':
|
|
71
|
+
case 'cjs':
|
|
72
|
+
return 'text/javascript'
|
|
71
73
|
case 'json':
|
|
72
74
|
return 'application/json'
|
|
73
75
|
case 'txt':
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { badGatewayResponse, badRequestResponse, gatewayTimeoutResponse, internalServerErrorResponse, notFoundResponse, preconditionFailedResponse } from './responses.
|
|
2
|
-
import type { Resource } from '../index.
|
|
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.
|
|
2
|
-
import { isPromise } from './type-guards.
|
|
3
|
-
import type { ContentTypeParser } from '../index.
|
|
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.
|
|
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.
|
|
5
|
+
import type { ServiceFactoryMap } from './libp2p-types.ts'
|
|
6
6
|
import type { DefaultLibp2pServices } from 'helia'
|
|
7
7
|
import type { Libp2pOptions } from 'libp2p'
|
|
8
8
|
|
package/src/utils/responses.ts
CHANGED
|
@@ -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.
|
|
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'
|
package/src/verified-fetch.ts
CHANGED
|
@@ -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.
|
|
9
|
-
import { IpldPlugin } from './plugins/plugin-handle-ipld.
|
|
10
|
-
import { IpnsRecordPlugin } from './plugins/plugin-handle-ipns-record.
|
|
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.
|
|
13
|
-
import { UnixFSPlugin } from './plugins/plugin-handle-unixfs.
|
|
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.
|
|
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.
|
|
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.
|
|
24
|
-
import { notAcceptableResponse, notImplementedResponse, notModifiedResponse } from './utils/responses.
|
|
25
|
-
import { ServerTiming } from './utils/server-timing.
|
|
26
|
-
import type { AcceptHeader, CIDDetail, ContentTypeParser, CreateVerifiedFetchOptions, Resource, ResourceDetail, VerifiedFetchInit as VerifiedFetchOptions, VerifiedFetchPlugin, PluginContext, PluginOptions } from './index.
|
|
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'
|