@helia/verified-fetch 0.0.0-a04e041 → 0.0.0-dc2e7a6
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/README.md +0 -33
- package/dist/index.min.js +4 -4
- package/dist/src/index.d.ts +0 -36
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +0 -33
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils/parse-url-string.d.ts +0 -2
- package/dist/src/utils/parse-url-string.d.ts.map +1 -1
- package/dist/src/utils/parse-url-string.js +0 -6
- package/dist/src/utils/parse-url-string.js.map +1 -1
- package/dist/src/verified-fetch.d.ts +15 -17
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +129 -211
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +12 -18
- package/src/index.ts +0 -37
- package/src/utils/parse-url-string.ts +1 -11
- package/src/verified-fetch.ts +134 -237
- package/dist/src/utils/get-content-disposition-filename.d.ts +0 -6
- package/dist/src/utils/get-content-disposition-filename.d.ts.map +0 -1
- package/dist/src/utils/get-content-disposition-filename.js +0 -16
- package/dist/src/utils/get-content-disposition-filename.js.map +0 -1
- package/dist/src/utils/responses.d.ts +0 -4
- package/dist/src/utils/responses.d.ts.map +0 -1
- package/dist/src/utils/responses.js +0 -21
- package/dist/src/utils/responses.js.map +0 -1
- package/dist/src/utils/select-output-type.d.ts +0 -12
- package/dist/src/utils/select-output-type.d.ts.map +0 -1
- package/dist/src/utils/select-output-type.js +0 -147
- package/dist/src/utils/select-output-type.js.map +0 -1
- package/src/utils/get-content-disposition-filename.ts +0 -18
- package/src/utils/responses.ts +0 -22
- package/src/utils/select-output-type.ts +0 -166
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { code as dagCborCode } from '@ipld/dag-cbor';
|
|
2
|
-
import { code as dagJsonCode } from '@ipld/dag-json';
|
|
3
|
-
import { code as dagPbCode } from '@ipld/dag-pb';
|
|
4
|
-
import { code as jsonCode } from 'multiformats/codecs/json';
|
|
5
|
-
import { code as rawCode } from 'multiformats/codecs/raw';
|
|
6
|
-
/**
|
|
7
|
-
* This maps supported response types for each codec supported by verified-fetch
|
|
8
|
-
*/
|
|
9
|
-
const CID_TYPE_MAP = {
|
|
10
|
-
[dagCborCode]: [
|
|
11
|
-
'application/json',
|
|
12
|
-
'application/vnd.ipld.dag-cbor',
|
|
13
|
-
'application/cbor',
|
|
14
|
-
'application/vnd.ipld.dag-json',
|
|
15
|
-
'application/octet-stream',
|
|
16
|
-
'application/vnd.ipld.raw',
|
|
17
|
-
'application/vnd.ipfs.ipns-record',
|
|
18
|
-
'application/vnd.ipld.car'
|
|
19
|
-
],
|
|
20
|
-
[dagJsonCode]: [
|
|
21
|
-
'application/json',
|
|
22
|
-
'application/vnd.ipld.dag-cbor',
|
|
23
|
-
'application/cbor',
|
|
24
|
-
'application/vnd.ipld.dag-json',
|
|
25
|
-
'application/octet-stream',
|
|
26
|
-
'application/vnd.ipld.raw',
|
|
27
|
-
'application/vnd.ipfs.ipns-record',
|
|
28
|
-
'application/vnd.ipld.car'
|
|
29
|
-
],
|
|
30
|
-
[jsonCode]: [
|
|
31
|
-
'application/json',
|
|
32
|
-
'application/vnd.ipld.dag-cbor',
|
|
33
|
-
'application/cbor',
|
|
34
|
-
'application/vnd.ipld.dag-json',
|
|
35
|
-
'application/octet-stream',
|
|
36
|
-
'application/vnd.ipld.raw',
|
|
37
|
-
'application/vnd.ipfs.ipns-record',
|
|
38
|
-
'application/vnd.ipld.car'
|
|
39
|
-
],
|
|
40
|
-
[dagPbCode]: [
|
|
41
|
-
'application/octet-stream',
|
|
42
|
-
'application/json',
|
|
43
|
-
'application/vnd.ipld.dag-cbor',
|
|
44
|
-
'application/cbor',
|
|
45
|
-
'application/vnd.ipld.dag-json',
|
|
46
|
-
'application/vnd.ipld.raw',
|
|
47
|
-
'application/vnd.ipfs.ipns-record',
|
|
48
|
-
'application/vnd.ipld.car',
|
|
49
|
-
'application/x-tar'
|
|
50
|
-
],
|
|
51
|
-
[rawCode]: [
|
|
52
|
-
'application/octet-stream',
|
|
53
|
-
'application/vnd.ipld.raw',
|
|
54
|
-
'application/vnd.ipfs.ipns-record',
|
|
55
|
-
'application/vnd.ipld.car'
|
|
56
|
-
]
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Selects an output mime-type based on the CID and a passed `Accept` header
|
|
60
|
-
*/
|
|
61
|
-
export function selectOutputType(cid, accept) {
|
|
62
|
-
const cidMimeTypes = CID_TYPE_MAP[cid.code];
|
|
63
|
-
if (accept != null) {
|
|
64
|
-
return chooseMimeType(accept, cidMimeTypes);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function chooseMimeType(accept, validMimeTypes) {
|
|
68
|
-
const requestedMimeTypes = accept
|
|
69
|
-
.split(',')
|
|
70
|
-
.map(s => {
|
|
71
|
-
const parts = s.trim().split(';');
|
|
72
|
-
return {
|
|
73
|
-
mimeType: `${parts[0]}`.trim(),
|
|
74
|
-
weight: parseQFactor(parts[1])
|
|
75
|
-
};
|
|
76
|
-
})
|
|
77
|
-
.sort((a, b) => {
|
|
78
|
-
if (a.weight === b.weight) {
|
|
79
|
-
return 0;
|
|
80
|
-
}
|
|
81
|
-
if (a.weight > b.weight) {
|
|
82
|
-
return -1;
|
|
83
|
-
}
|
|
84
|
-
return 1;
|
|
85
|
-
})
|
|
86
|
-
.map(s => s.mimeType);
|
|
87
|
-
for (const headerFormat of requestedMimeTypes) {
|
|
88
|
-
for (const mimeType of validMimeTypes) {
|
|
89
|
-
if (headerFormat.includes(mimeType)) {
|
|
90
|
-
return mimeType;
|
|
91
|
-
}
|
|
92
|
-
if (headerFormat === '*/*') {
|
|
93
|
-
return mimeType;
|
|
94
|
-
}
|
|
95
|
-
if (headerFormat.startsWith('*/') && mimeType.split('/')[1] === headerFormat.split('/')[1]) {
|
|
96
|
-
return mimeType;
|
|
97
|
-
}
|
|
98
|
-
if (headerFormat.endsWith('/*') && mimeType.split('/')[0] === headerFormat.split('/')[0]) {
|
|
99
|
-
return mimeType;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Parses q-factor weighting from the accept header to allow letting some mime
|
|
106
|
-
* types take precedence over others.
|
|
107
|
-
*
|
|
108
|
-
* If the q-factor for an acceptable mime representation is omitted it defaults
|
|
109
|
-
* to `1`.
|
|
110
|
-
*
|
|
111
|
-
* All specified values should be in the range 0-1.
|
|
112
|
-
*
|
|
113
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept#q
|
|
114
|
-
*/
|
|
115
|
-
function parseQFactor(str) {
|
|
116
|
-
if (str != null) {
|
|
117
|
-
str = str.trim();
|
|
118
|
-
}
|
|
119
|
-
if (str == null || !str.startsWith('q=')) {
|
|
120
|
-
return 1;
|
|
121
|
-
}
|
|
122
|
-
const factor = parseFloat(str.replace('q=', ''));
|
|
123
|
-
if (isNaN(factor)) {
|
|
124
|
-
return 0;
|
|
125
|
-
}
|
|
126
|
-
return factor;
|
|
127
|
-
}
|
|
128
|
-
const FORMAT_TO_MIME_TYPE = {
|
|
129
|
-
raw: 'application/vnd.ipld.raw',
|
|
130
|
-
car: 'application/vnd.ipld.car',
|
|
131
|
-
'dag-json': 'application/vnd.ipld.dag-json',
|
|
132
|
-
'dag-cbor': 'application/vnd.ipld.dag-cbor',
|
|
133
|
-
json: 'application/json',
|
|
134
|
-
cbor: 'application/cbor',
|
|
135
|
-
'ipns-record': 'application/vnd.ipfs.ipns-record',
|
|
136
|
-
tar: 'application/x-tar'
|
|
137
|
-
};
|
|
138
|
-
/**
|
|
139
|
-
* Converts a `format=...` query param to a mime type as would be found in the
|
|
140
|
-
* `Accept` header, if a valid mapping is available
|
|
141
|
-
*/
|
|
142
|
-
export function queryFormatToAcceptHeader(format) {
|
|
143
|
-
if (format != null) {
|
|
144
|
-
return FORMAT_TO_MIME_TYPE[format];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
//# sourceMappingURL=select-output-type.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"select-output-type.js","sourceRoot":"","sources":["../../../src/utils/select-output-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAIzD;;GAEG;AACH,MAAM,YAAY,GAA6B;IAC7C,CAAC,WAAW,CAAC,EAAE;QACb,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;QAClC,0BAA0B;KAC3B;IACD,CAAC,WAAW,CAAC,EAAE;QACb,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;QAClC,0BAA0B;KAC3B;IACD,CAAC,QAAQ,CAAC,EAAE;QACV,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;QAClC,0BAA0B;KAC3B;IACD,CAAC,SAAS,CAAC,EAAE;QACX,0BAA0B;QAC1B,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,kCAAkC;QAClC,0BAA0B;QAC1B,mBAAmB;KACpB;IACD,CAAC,OAAO,CAAC,EAAE;QACT,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;Q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;IACxB,IAAI,EAAE,kBAAkB;IACxB,aAAa,EAAE,kCAAkC;IACjD,GAAG,EAAE,mBAAmB;CACzB,CAAA;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAE,MAA+B;IACxE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QACnB,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAA;IACpC,CAAC;AACH,CAAC"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Takes a filename URL param and returns a string for use in a
|
|
3
|
-
* `Content-Disposition` header
|
|
4
|
-
*/
|
|
5
|
-
export function getContentDispositionFilename (filename: string): string {
|
|
6
|
-
const asciiOnly = replaceNonAsciiCharacters(filename)
|
|
7
|
-
|
|
8
|
-
if (asciiOnly === filename) {
|
|
9
|
-
return `filename="${filename}"`
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return `filename="${asciiOnly}"; filename*=UTF-8''${encodeURIComponent(filename)}`
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function replaceNonAsciiCharacters (filename: string): string {
|
|
16
|
-
// eslint-disable-next-line no-control-regex
|
|
17
|
-
return filename.replace(/[^\x00-\x7F]/g, '_')
|
|
18
|
-
}
|
package/src/utils/responses.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export function okResponse (body?: BodyInit | null): Response {
|
|
2
|
-
return new Response(body, {
|
|
3
|
-
status: 200,
|
|
4
|
-
statusText: 'OK'
|
|
5
|
-
})
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function notSupportedResponse (body?: BodyInit | null): Response {
|
|
9
|
-
const response = new Response(body, {
|
|
10
|
-
status: 501,
|
|
11
|
-
statusText: 'Not Implemented'
|
|
12
|
-
})
|
|
13
|
-
response.headers.set('X-Content-Type-Options', 'nosniff') // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
|
|
14
|
-
return response
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function notAcceptableResponse (body?: BodyInit | null): Response {
|
|
18
|
-
return new Response(body, {
|
|
19
|
-
status: 406,
|
|
20
|
-
statusText: 'Not Acceptable'
|
|
21
|
-
})
|
|
22
|
-
}
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { code as dagCborCode } from '@ipld/dag-cbor'
|
|
2
|
-
import { code as dagJsonCode } from '@ipld/dag-json'
|
|
3
|
-
import { code as dagPbCode } from '@ipld/dag-pb'
|
|
4
|
-
import { code as jsonCode } from 'multiformats/codecs/json'
|
|
5
|
-
import { code as rawCode } from 'multiformats/codecs/raw'
|
|
6
|
-
import type { RequestFormatShorthand } from '../types.js'
|
|
7
|
-
import type { CID } from 'multiformats/cid'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* This maps supported response types for each codec supported by verified-fetch
|
|
11
|
-
*/
|
|
12
|
-
const CID_TYPE_MAP: Record<number, string[]> = {
|
|
13
|
-
[dagCborCode]: [
|
|
14
|
-
'application/json',
|
|
15
|
-
'application/vnd.ipld.dag-cbor',
|
|
16
|
-
'application/cbor',
|
|
17
|
-
'application/vnd.ipld.dag-json',
|
|
18
|
-
'application/octet-stream',
|
|
19
|
-
'application/vnd.ipld.raw',
|
|
20
|
-
'application/vnd.ipfs.ipns-record',
|
|
21
|
-
'application/vnd.ipld.car'
|
|
22
|
-
],
|
|
23
|
-
[dagJsonCode]: [
|
|
24
|
-
'application/json',
|
|
25
|
-
'application/vnd.ipld.dag-cbor',
|
|
26
|
-
'application/cbor',
|
|
27
|
-
'application/vnd.ipld.dag-json',
|
|
28
|
-
'application/octet-stream',
|
|
29
|
-
'application/vnd.ipld.raw',
|
|
30
|
-
'application/vnd.ipfs.ipns-record',
|
|
31
|
-
'application/vnd.ipld.car'
|
|
32
|
-
],
|
|
33
|
-
[jsonCode]: [
|
|
34
|
-
'application/json',
|
|
35
|
-
'application/vnd.ipld.dag-cbor',
|
|
36
|
-
'application/cbor',
|
|
37
|
-
'application/vnd.ipld.dag-json',
|
|
38
|
-
'application/octet-stream',
|
|
39
|
-
'application/vnd.ipld.raw',
|
|
40
|
-
'application/vnd.ipfs.ipns-record',
|
|
41
|
-
'application/vnd.ipld.car'
|
|
42
|
-
],
|
|
43
|
-
[dagPbCode]: [
|
|
44
|
-
'application/octet-stream',
|
|
45
|
-
'application/json',
|
|
46
|
-
'application/vnd.ipld.dag-cbor',
|
|
47
|
-
'application/cbor',
|
|
48
|
-
'application/vnd.ipld.dag-json',
|
|
49
|
-
'application/vnd.ipld.raw',
|
|
50
|
-
'application/vnd.ipfs.ipns-record',
|
|
51
|
-
'application/vnd.ipld.car',
|
|
52
|
-
'application/x-tar'
|
|
53
|
-
],
|
|
54
|
-
[rawCode]: [
|
|
55
|
-
'application/octet-stream',
|
|
56
|
-
'application/vnd.ipld.raw',
|
|
57
|
-
'application/vnd.ipfs.ipns-record',
|
|
58
|
-
'application/vnd.ipld.car'
|
|
59
|
-
]
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Selects an output mime-type based on the CID and a passed `Accept` header
|
|
64
|
-
*/
|
|
65
|
-
export function selectOutputType (cid: CID, accept?: string): string | undefined {
|
|
66
|
-
const cidMimeTypes = CID_TYPE_MAP[cid.code]
|
|
67
|
-
|
|
68
|
-
if (accept != null) {
|
|
69
|
-
return chooseMimeType(accept, cidMimeTypes)
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function chooseMimeType (accept: string, validMimeTypes: string[]): string | undefined {
|
|
74
|
-
const requestedMimeTypes = accept
|
|
75
|
-
.split(',')
|
|
76
|
-
.map(s => {
|
|
77
|
-
const parts = s.trim().split(';')
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
mimeType: `${parts[0]}`.trim(),
|
|
81
|
-
weight: parseQFactor(parts[1])
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
.sort((a, b) => {
|
|
85
|
-
if (a.weight === b.weight) {
|
|
86
|
-
return 0
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (a.weight > b.weight) {
|
|
90
|
-
return -1
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return 1
|
|
94
|
-
})
|
|
95
|
-
.map(s => s.mimeType)
|
|
96
|
-
|
|
97
|
-
for (const headerFormat of requestedMimeTypes) {
|
|
98
|
-
for (const mimeType of validMimeTypes) {
|
|
99
|
-
if (headerFormat.includes(mimeType)) {
|
|
100
|
-
return mimeType
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (headerFormat === '*/*') {
|
|
104
|
-
return mimeType
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (headerFormat.startsWith('*/') && mimeType.split('/')[1] === headerFormat.split('/')[1]) {
|
|
108
|
-
return mimeType
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (headerFormat.endsWith('/*') && mimeType.split('/')[0] === headerFormat.split('/')[0]) {
|
|
112
|
-
return mimeType
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Parses q-factor weighting from the accept header to allow letting some mime
|
|
120
|
-
* types take precedence over others.
|
|
121
|
-
*
|
|
122
|
-
* If the q-factor for an acceptable mime representation is omitted it defaults
|
|
123
|
-
* to `1`.
|
|
124
|
-
*
|
|
125
|
-
* All specified values should be in the range 0-1.
|
|
126
|
-
*
|
|
127
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept#q
|
|
128
|
-
*/
|
|
129
|
-
function parseQFactor (str?: string): number {
|
|
130
|
-
if (str != null) {
|
|
131
|
-
str = str.trim()
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (str == null || !str.startsWith('q=')) {
|
|
135
|
-
return 1
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const factor = parseFloat(str.replace('q=', ''))
|
|
139
|
-
|
|
140
|
-
if (isNaN(factor)) {
|
|
141
|
-
return 0
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return factor
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const FORMAT_TO_MIME_TYPE: Record<RequestFormatShorthand, string> = {
|
|
148
|
-
raw: 'application/vnd.ipld.raw',
|
|
149
|
-
car: 'application/vnd.ipld.car',
|
|
150
|
-
'dag-json': 'application/vnd.ipld.dag-json',
|
|
151
|
-
'dag-cbor': 'application/vnd.ipld.dag-cbor',
|
|
152
|
-
json: 'application/json',
|
|
153
|
-
cbor: 'application/cbor',
|
|
154
|
-
'ipns-record': 'application/vnd.ipfs.ipns-record',
|
|
155
|
-
tar: 'application/x-tar'
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Converts a `format=...` query param to a mime type as would be found in the
|
|
160
|
-
* `Accept` header, if a valid mapping is available
|
|
161
|
-
*/
|
|
162
|
-
export function queryFormatToAcceptHeader (format?: RequestFormatShorthand): string | undefined {
|
|
163
|
-
if (format != null) {
|
|
164
|
-
return FORMAT_TO_MIME_TYPE[format]
|
|
165
|
-
}
|
|
166
|
-
}
|