@@ -8,7 +8,6 @@ import https from "node:https";
8
8
import path from "node:path" ;
9
9
import { Writable } from "node:stream" ;
10
10
11
- import { GetObjectCommand , S3Client } from "@aws-sdk/client-s3" ;
12
11
import { loadBuildId , loadConfig } from "config/util.js" ;
13
12
import { OpenNextNodeResponse , StreamCreator } from "http/openNextResponse.js" ;
14
13
// @ts -ignore
@@ -19,16 +18,14 @@ import {
19
18
} from "next/dist/server/image-optimizer" ;
20
19
// @ts -ignore
21
20
import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta" ;
22
- import { ImageLoader , InternalEvent , InternalResult } from "types/open-next.js" ;
21
+ import { InternalEvent , InternalResult } from "types/open-next.js" ;
23
22
24
23
import { createGenericHandler } from "../core/createGenericHandler.js" ;
25
- import { awsLogger , debug , error } from "./logger.js" ;
24
+ import { resolveImageLoader } from "../core/resolve.js" ;
25
+ import { debug , error } from "./logger.js" ;
26
26
import { optimizeImage } from "./plugins/image-optimization/image-optimization.js" ;
27
27
import { setNodeEnv } from "./util.js" ;
28
28
29
- // Expected environment variables
30
- const { BUCKET_NAME , BUCKET_KEY_PREFIX } = process . env ;
31
-
32
29
setNodeEnv ( ) ;
33
30
const nextDir = path . join ( __dirname , ".next" ) ;
34
31
const config = loadConfig ( nextDir ) ;
@@ -42,7 +39,6 @@ const nextConfig = {
42
39
} ;
43
40
debug ( "Init config" , {
44
41
nextDir,
45
- BUCKET_NAME ,
46
42
nextConfig,
47
43
} ) ;
48
44
@@ -64,7 +60,14 @@ export async function defaultHandler(
64
60
const { headers, query : queryString } = event ;
65
61
66
62
try {
67
- // const headers = normalizeHeaderKeysToLowercase(rawHeaders);
63
+ // Set the HOST environment variable to the host header if it is not set
64
+ // If it is set it is assumed to be set by the user and should be used instead
65
+ // It might be useful for cases where the user wants to use a different host than the one in the request
66
+ // It could even allow to have multiple hosts for the image optimization by setting the HOST environment variable in the wrapper for example
67
+ if ( ! process . env . HOST ) {
68
+ const headersHost = headers [ "x-forwarded-host" ] || headers [ "host" ] ;
69
+ process . env . HOST = headersHost ;
70
+ }
68
71
69
72
const imageParams = validateImageParams (
70
73
headers ,
@@ -101,20 +104,6 @@ export async function defaultHandler(
101
104
// Helper functions //
102
105
//////////////////////
103
106
104
- // function normalizeHeaderKeysToLowercase(headers: APIGatewayProxyEventHeaders) {
105
- // // Make header keys lowercase to ensure integrity
106
- // return Object.entries(headers).reduce(
107
- // (acc, [key, value]) => ({ ...acc, [key.toLowerCase()]: value }),
108
- // {} as APIGatewayProxyEventHeaders,
109
- // );
110
- // }
111
-
112
- function ensureBucketExists ( ) {
113
- if ( ! BUCKET_NAME ) {
114
- throw new Error ( "Bucket name must be defined!" ) ;
115
- }
116
- }
117
-
118
107
function validateImageParams (
119
108
headers : OutgoingHttpHeaders ,
120
109
query ?: InternalEvent [ "query" ] ,
@@ -218,36 +207,9 @@ function buildFailureResponse(
218
207
} ;
219
208
}
220
209
221
- const resolveLoader = ( ) => {
222
- const openNextParams = globalThis . openNextConfig . imageOptimization ;
223
- if ( typeof openNextParams ?. loader === "function" ) {
224
- return openNextParams . loader ( ) ;
225
- } else {
226
- const s3Client = new S3Client ( { logger : awsLogger } ) ;
227
- return Promise . resolve < ImageLoader > ( {
228
- name : "s3" ,
229
- // @ts -ignore
230
- load : async ( key : string ) => {
231
- ensureBucketExists ( ) ;
232
- const keyPrefix = BUCKET_KEY_PREFIX ?. replace ( / ^ \/ | \/ $ / g, "" ) ;
233
- const response = await s3Client . send (
234
- new GetObjectCommand ( {
235
- Bucket : BUCKET_NAME ,
236
- Key : keyPrefix
237
- ? keyPrefix + "/" + key . replace ( / ^ \/ / , "" )
238
- : key . replace ( / ^ \/ / , "" ) ,
239
- } ) ,
240
- ) ;
241
- return {
242
- body : response . Body ,
243
- contentType : response . ContentType ,
244
- cacheControl : response . CacheControl ,
245
- } ;
246
- } ,
247
- } ) ;
248
- }
249
- } ;
250
- const loader = await resolveLoader ( ) ;
210
+ const loader = await resolveImageLoader (
211
+ globalThis . openNextConfig . imageOptimization ?. loader ?? "s3" ,
212
+ ) ;
251
213
252
214
async function downloadHandler (
253
215
_req : IncomingMessage ,
0 commit comments