Skip to content

Commit a5635c6

Browse files
committed
chore: move utils into helpers file
1 parent 39b2968 commit a5635c6

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/templates/getHandler.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path')
55

66
const { Bridge } = require('@vercel/node/dist/bridge')
77

8-
const { downloadFile } = require('./handlerUtils')
8+
const { downloadFile, getMaxAge, getMultiValueHeaders } = require('./handlerUtils')
99

1010
const makeHandler =
1111
() =>
@@ -109,22 +109,6 @@ const makeHandler =
109109
const bridge = new Bridge(server)
110110
bridge.listen()
111111

112-
const getMaxAge = (header) => {
113-
const parts = header.split(',')
114-
let maxAge
115-
for (const part of parts) {
116-
const [key, value] = part.split('=')
117-
if (key?.trim() === 's-maxage') {
118-
maxAge = value?.trim()
119-
}
120-
}
121-
if (maxAge) {
122-
const result = Number.parseInt(maxAge)
123-
return Number.isNaN(result) ? 0 : result
124-
}
125-
return 0
126-
}
127-
128112
return async (event, context) => {
129113
// Ensure that paths are encoded - but don't double-encode them
130114
event.path = new URL(event.path, event.rawUrl).pathname
@@ -141,14 +125,8 @@ const makeHandler =
141125
/** @type import("@netlify/functions").HandlerResponse */
142126

143127
// Convert all headers to multiValueHeaders
144-
const multiValueHeaders = {}
145-
for (const key of Object.keys(headers)) {
146-
if (Array.isArray(headers[key])) {
147-
multiValueHeaders[key] = headers[key]
148-
} else {
149-
multiValueHeaders[key] = [headers[key]]
150-
}
151-
}
128+
129+
const multiValueHeaders = getMultiValueHeaders(headers)
152130

153131
if (multiValueHeaders['set-cookie']?.[0]?.includes('__prerender_bypass')) {
154132
delete multiValueHeaders.etag
@@ -163,12 +141,9 @@ const makeHandler =
163141
if (mode === 'odb' && process.env.EXPERIMENTAL_ODB_TTL) {
164142
mode = 'isr'
165143
const ttl = getMaxAge(cacheHeader)
166-
multiValueHeaders['x-raw-ttl'] = [ttl]
167-
168144
// Long-expiry TTL is basically no TTL
169145
if (ttl > 0 && ttl < ONE_YEAR_IN_SECONDS) {
170146
result.ttl = ttl
171-
multiValueHeaders['x-ttl-result'] = [result.ttl]
172147
}
173148
}
174149
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
@@ -189,7 +164,7 @@ const { tmpdir } = require('os')
189164
const { promises, existsSync } = require("fs");
190165
// We copy the file here rather than requiring from the node module
191166
const { Bridge } = require("./bridge");
192-
const { downloadFile } = require('./handlerUtils')
167+
const { downloadFile, getMaxAge, getMultiValueHeaders } = require('./handlerUtils')
193168
194169
const { builder } = require("@netlify/functions");
195170
const { config } = require("${publishDir}/required-server-files.json")

src/templates/handlerUtils.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { promisify } from 'util'
66

77
const streamPipeline = promisify(pipeline)
88

9-
export const downloadFile = async (url, destination) => {
9+
export const downloadFile = async (url: string, destination: string): Promise<void> => {
1010
console.log(`Downloading ${url} to ${destination}`)
1111

1212
const httpx = url.startsWith('https') ? https : http
@@ -31,3 +31,33 @@ export const downloadFile = async (url, destination) => {
3131
})
3232
})
3333
}
34+
35+
export const getMaxAge = (header: string): number => {
36+
const parts = header.split(',')
37+
let maxAge
38+
for (const part of parts) {
39+
const [key, value] = part.split('=')
40+
if (key?.trim() === 's-maxage') {
41+
maxAge = value?.trim()
42+
}
43+
}
44+
if (maxAge) {
45+
const result = Number.parseInt(maxAge)
46+
return Number.isNaN(result) ? 0 : result
47+
}
48+
return 0
49+
}
50+
51+
export const getMultiValueHeaders = (
52+
headers: Record<string, string | Array<string>>,
53+
): Record<string, Array<string>> => {
54+
const multiValueHeaders: Record<string, Array<string>> = {}
55+
for (const key of Object.keys(headers)) {
56+
if (Array.isArray(headers[key])) {
57+
multiValueHeaders[key] = headers[key] as Array<string>
58+
} else {
59+
multiValueHeaders[key] = [headers[key] as string]
60+
}
61+
}
62+
return multiValueHeaders
63+
}

0 commit comments

Comments
 (0)