-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (133 loc) · 3.71 KB
/
index.js
File metadata and controls
144 lines (133 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* eslint-env worker */
import {
withContext,
withCorsHeaders,
withContentDispositionHeader,
withErrorHandler,
createWithHttpMethod,
withCdnCache,
withParsedIpfsUrl,
withFixedLengthStream,
composeMiddleware
} from '@web3-storage/gateway-lib/middleware'
import {
handleUnixfs,
handleBlock,
handleCar
} from '@web3-storage/gateway-lib/handlers'
import {
withContentClaimsDagula,
withVersionHeader,
withAuthToken,
withCarBlockHandler,
withRateLimit,
withEgressTracker,
withAuthorizedSpace,
withLocator,
withDelegationStubs,
withMemoryBudget,
withRangeCache
} from './middleware/index.js'
import { instrument } from '@microlabs/otel-cf-workers'
import { NoopSpanProcessor } from '@opentelemetry/sdk-trace-base'
/**
* @import {
* Handler,
* Middleware,
* Context,
* IpfsUrlContext,
* BlockContext,
* DagContext,
* UnixfsContext
* } from '@web3-storage/gateway-lib'
* @import { Environment } from './bindings.js'
*/
const handler = {
/** @type {Handler<Context, Environment>} */
fetch (request, env, ctx) {
console.log(request.method, request.url)
const middleware = composeMiddleware(
// Basic context and error handling
withContext,
withErrorHandler,
withCorsHeaders,
withVersionHeader,
// Security and resource management
withMemoryBudget, // Add memory budgeting early
withRateLimit, // Rate limiting before expensive operations
withAuthToken, // Authentication
withAuthorizedSpace, // Authorization
// Request parsing and validation
withParsedIpfsUrl,
createWithHttpMethod('GET', 'HEAD'),
// Caching and performance
withCdnCache,
withRangeCache, // Range request handling
// Data fetching and processing
withLocator,
withDelegationStubs,
withCarBlockHandler,
withContentClaimsDagula,
withEgressTracker,
// Response preparation
withContentDispositionHeader,
withFixedLengthStream
)
return middleware(handleUnixfs)(request, env, ctx)
}
}
/**
*
* @param {Environment} env
* @param {*} _trigger
*/
function config (env, _trigger) {
if (env.HONEYCOMB_API_KEY) {
return {
exporter: {
url: 'https://api.honeycomb.io/v1/traces',
headers: { 'x-honeycomb-team': env.HONEYCOMB_API_KEY }
},
service: { name: 'freeway' }
}
}
return {
spanProcessors: new NoopSpanProcessor(),
service: { name: 'freeway' }
}
}
export default instrument(handler, config)
/**
* @type {Middleware<BlockContext & UnixfsContext & IpfsUrlContext, BlockContext & UnixfsContext & IpfsUrlContext, Environment>}
*/
export function withFormatRawHandler (handler) {
return async (request, env, ctx) => {
const { headers } = request
const { searchParams } = ctx
if (!searchParams) throw new Error('missing URL search params')
if (
searchParams.get('format') === 'raw' ||
headers.get('Accept')?.includes('application/vnd.ipld.raw')
) {
return await handleBlock(request, env, ctx)
}
return handler(request, env, ctx) // pass to other handlers
}
}
/**
* @type {Middleware<DagContext & IpfsUrlContext, DagContext & IpfsUrlContext, Environment>}
*/
export function withFormatCarHandler (handler) {
return async (request, env, ctx) => {
const { headers } = request
const { searchParams } = ctx
if (!searchParams) throw new Error('missing URL search params')
if (
searchParams.get('format') === 'car' ||
headers.get('Accept')?.includes('application/vnd.ipld.car')
) {
return await handleCar(request, env, ctx)
}
return handler(request, env, ctx) // pass to other handlers
}
}