1
1
import { join } from 'path'
2
2
import { tmpdir } from 'os'
3
- import { createIPX , handleRequest , IPXOptions } from 'ipx'
3
+ import { createIPX , createIPXPlainServer , ipxFSStorage , ipxHttpStorage , IPXOptions } from 'ipx'
4
4
import { builder , Handler } from '@netlify/functions'
5
5
import { parseURL } from 'ufo'
6
6
import etag from 'etag'
@@ -30,6 +30,10 @@ export interface IPXHandlerOptions extends Partial<IPXOptions> {
30
30
* Restrict local image access to a specific prefix
31
31
*/
32
32
localPrefix ?: string
33
+ /**
34
+ * List of domains to allow for remote images
35
+ */
36
+ domains ?: string [ ]
33
37
/**
34
38
* Patterns used to verify remote image URLs
35
39
*/
@@ -54,9 +58,21 @@ export function createIPXHandler ({
54
58
remotePatterns,
55
59
responseHeaders,
56
60
localPrefix,
61
+ domains = [ ] ,
57
62
...opts
58
63
} : IPXHandlerOptions = { } , loadSourceImage = defaultLoadSourceImage ) {
59
- const ipx = createIPX ( { ...opts , dir : join ( cacheDir , 'cache' ) } )
64
+ const ipx = createIPX (
65
+ {
66
+ storage : ipxFSStorage ( {
67
+ dir : join ( cacheDir , 'cache' )
68
+ } ) ,
69
+ httpStorage : ipxHttpStorage ( {
70
+ ...opts
71
+ } )
72
+ } )
73
+
74
+ const handleRequest = createIPXPlainServer ( ipx )
75
+
60
76
if ( ! basePath . endsWith ( '/' ) ) {
61
77
basePath = `${ basePath } /`
62
78
}
@@ -73,7 +89,7 @@ export function createIPXHandler ({
73
89
headers : plainText
74
90
}
75
91
}
76
- let domains = ( opts as IPXOptions ) . domains || [ ]
92
+
77
93
const remoteURLPatterns = remotePatterns || [ ]
78
94
const requestEtag = event . headers [ 'if-none-match' ]
79
95
const eventPath = event . path . replace ( basePath , '' )
@@ -195,19 +211,21 @@ export function createIPXHandler ({
195
211
196
212
const res = await handleRequest (
197
213
{
198
- url : `/${ modifiers } /${ cacheKey } ` ,
199
- headers : event . headers
200
- } ,
201
- ipx
214
+ path : `/${ modifiers } /${ cacheKey } ` ,
215
+ headers : event . headers ,
216
+ method : 'GET'
217
+ }
202
218
)
203
219
220
+ const headers = Object . fromEntries ( res . headers )
221
+
204
222
const body =
205
- typeof res . body === 'string' ? res . body : res . body . toString ( 'base64' )
223
+ typeof res . body === 'string' ? res . body : ( res . body as Buffer ) . toString ( 'base64' )
206
224
207
- res . headers . etag = responseEtag || JSON . parse ( etag ( body ) )
208
- delete res . headers [ 'Last-Modified' ]
225
+ headers . etag = responseEtag || JSON . parse ( etag ( body ) )
226
+ delete headers [ 'Last-Modified' ]
209
227
210
- if ( requestEtag && requestEtag === res . headers . etag ) {
228
+ if ( requestEtag && requestEtag === headers . etag ) {
211
229
return {
212
230
statusCode : 304 ,
213
231
message : 'Not Modified'
@@ -216,14 +234,14 @@ export function createIPXHandler ({
216
234
217
235
if ( responseHeaders ) {
218
236
for ( const [ header , value ] of Object . entries ( responseHeaders ) ) {
219
- res . headers [ header ] = value
237
+ headers [ header ] = value
220
238
}
221
239
}
222
240
223
241
return {
224
- statusCode : res . statusCode ,
225
- message : res . statusMessage ,
226
- headers : res . headers ,
242
+ statusCode : res . status ,
243
+ statusText : res . statusText ,
244
+ headers,
227
245
isBase64Encoded : typeof res . body !== 'string' ,
228
246
body
229
247
}
0 commit comments