@@ -3,41 +3,65 @@ import { ImageConfig } from 'next/dist/server/image-config';
3
3
import { NextConfig } from 'next/dist/server/config' ;
4
4
import { imageOptimizer as nextImageOptimizer } from 'next/dist/server/image-optimizer' ;
5
5
import Server from 'next/dist/server/next-server' ;
6
- import nodeFetch , { RequestInfo , RequestInit } from 'node-fetch' ;
6
+ import nodeFetch from 'node-fetch' ;
7
7
import { UrlWithParsedQuery } from 'url' ;
8
8
import S3 from 'aws-sdk/clients/s3' ;
9
9
10
+ /* -----------------------------------------------------------------------------
11
+ * Types
12
+ * ---------------------------------------------------------------------------*/
13
+
14
+ type NodeFetch = typeof nodeFetch ;
15
+
16
+ type OriginCacheControl = string | null ;
17
+
18
+ interface S3Config {
19
+ s3 : S3 ;
20
+ bucket : string ;
21
+ }
22
+
23
+ type ImageOptimizerResult = {
24
+ finished : boolean ;
25
+ originCacheControl : OriginCacheControl ;
26
+ } ;
27
+
28
+ /* -----------------------------------------------------------------------------
29
+ * globals
30
+ * ---------------------------------------------------------------------------*/
31
+
10
32
// Sets working dir of Next.js to /tmp (Lambda tmp dir)
11
33
const distDir = '/tmp' ;
12
34
13
- let originCacheControl : string | null ;
35
+ let originCacheControl : OriginCacheControl ;
14
36
15
37
/**
16
38
* fetch polyfill to intercept the request to the external resource
17
39
* to get the Cache-Control header from the origin
18
40
*/
19
- function fetchPolyfill ( url : RequestInfo , init ?: RequestInit ) {
41
+ const fetchPolyfill : NodeFetch = ( url , init ) => {
20
42
return nodeFetch ( url , init ) . then ( ( result ) => {
21
43
originCacheControl = result . headers . get ( 'Cache-Control' ) ;
22
44
return result ;
23
45
} ) ;
24
- }
46
+ } ;
25
47
26
- // Polyfill fetch used by nextImageOptimizer
48
+ fetchPolyfill . isRedirect = nodeFetch . isRedirect ;
49
+
50
+ // Polyfill fetch is used by nextImageOptimizer
51
+ // @ts -ignore
27
52
global . fetch = fetchPolyfill ;
28
53
29
- interface S3Config {
30
- s3 : S3 ;
31
- bucket : string ;
32
- }
54
+ /* -----------------------------------------------------------------------------
55
+ * imageOptimizer
56
+ * ---------------------------------------------------------------------------*/
33
57
34
58
async function imageOptimizer (
35
59
imageConfig : ImageConfig ,
36
60
req : IncomingMessage ,
37
61
res : ServerResponse ,
38
62
parsedUrl : UrlWithParsedQuery ,
39
63
s3Config ?: S3Config
40
- ) {
64
+ ) : Promise < ImageOptimizerResult > {
41
65
// Create next config mock
42
66
const nextConfig = ( {
43
67
images : imageConfig ,
@@ -79,9 +103,6 @@ async function imageOptimizer(
79
103
80
104
res . end ( object . Body ) ;
81
105
} else if ( headers . referer ) {
82
- let upstreamBuffer : Buffer ;
83
- let upstreamType : string | null ;
84
-
85
106
const { referer } = headers ;
86
107
const trimmedReferer = referer . endsWith ( '/' )
87
108
? referer . substring ( 0 , referer . length - 1 )
@@ -94,14 +115,14 @@ async function imageOptimizer(
94
115
}
95
116
96
117
res . statusCode = upstreamRes . status ;
97
- upstreamBuffer = Buffer . from ( await upstreamRes . arrayBuffer ( ) ) ;
98
- upstreamType = upstreamRes . headers . get ( 'Content-Type' ) ;
118
+ const upstreamType = upstreamRes . headers . get ( 'Content-Type' ) ;
99
119
originCacheControl = upstreamRes . headers . get ( 'Cache-Control' ) ;
100
120
101
121
if ( upstreamType ) {
102
122
res . setHeader ( 'Content-Type' , upstreamType ) ;
103
123
}
104
124
125
+ const upstreamBuffer = Buffer . from ( await upstreamRes . arrayBuffer ( ) ) ;
105
126
res . end ( upstreamBuffer ) ;
106
127
}
107
128
} ,
@@ -121,4 +142,5 @@ async function imageOptimizer(
121
142
} ;
122
143
}
123
144
124
- export { S3Config , imageOptimizer } ;
145
+ export type { S3Config } ;
146
+ export { imageOptimizer } ;
0 commit comments