Skip to content

Commit 26b6d3d

Browse files
authored
refactor: use useshouldbase64encode from @netlify/dev-utils (#7445)
* Refactor useShouldBase64Encode util from dev-utils * Moved import to correct grouping
1 parent ed3fa72 commit 26b6d3d

File tree

2 files changed

+2
-37
lines changed

2 files changed

+2
-37
lines changed

src/lib/functions/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { promises as fs } from 'fs'
33
import type { IncomingHttpHeaders } from 'http'
44
import path from 'path'
55

6+
import { shouldBase64Encode } from '@netlify/dev-utils'
67
import express, { type Request, type RequestHandler } from 'express'
78
import expressLogging from 'express-logging'
89
import { jwtDecode } from 'jwt-decode'
@@ -29,7 +30,6 @@ import { createFormSubmissionHandler } from './form-submissions-handler.js'
2930
import { FunctionsRegistry } from './registry.js'
3031
import { handleScheduledFunction } from './scheduled.js'
3132
import { handleSynchronousFunction } from './synchronous.js'
32-
import { shouldBase64Encode } from './utils.js'
3333

3434
type FunctionsSettings = Pick<ServerSettings, 'functions' | 'functionsPort'>
3535

@@ -122,7 +122,7 @@ export const createHandler = function (options: GetFunctionsServerOptions): Requ
122122
return
123123
}
124124

125-
const isBase64Encoded = shouldBase64Encode(request.header('content-type'))
125+
const isBase64Encoded = shouldBase64Encode(request.header('content-type') ?? '')
126126
let body
127127
if (hasBody(request)) {
128128
body = request.body.toString(isBase64Encoded ? 'base64' : 'utf8')

src/lib/functions/utils.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,4 @@ export const formatLambdaError = (err: Error | InvocationError): string =>
2020
`${'errorType' in err ? err.errorType : 'Error'}: ${'errorMessage' in err ? err.errorMessage : err.message}`,
2121
)
2222

23-
// should be equivalent to https://github.com/netlify/proxy/blob/main/pkg/functions/request.go#L105
24-
const exceptionsList = new Set([
25-
'application/csp-report',
26-
'application/graphql',
27-
'application/json',
28-
'application/javascript',
29-
'application/x-www-form-urlencoded',
30-
'application/x-ndjson',
31-
'application/xml',
32-
])
33-
34-
export const shouldBase64Encode = function (contentType?: string): boolean {
35-
if (!contentType) {
36-
return true
37-
}
38-
39-
const [contentTypeSegment] = contentType.split(';')
40-
contentType = contentTypeSegment
41-
contentType = contentType.toLowerCase()
42-
43-
if (contentType.startsWith('text/')) {
44-
return false
45-
}
46-
47-
if (contentType.endsWith('+json') || contentType.endsWith('+xml')) {
48-
return false
49-
}
50-
51-
if (exceptionsList.has(contentType)) {
52-
return false
53-
}
54-
55-
return true
56-
}
57-
5823
export const styleFunctionName = (name: string): string => chalk.magenta(name)

0 commit comments

Comments
 (0)