Skip to content

Commit

Permalink
Fallback to localhost for hostname correctly and respect base64 encod…
Browse files Browse the repository at this point in the history
…ed event bodies (redwoodjs#4891)
  • Loading branch information
ardatan authored Mar 23, 2022
1 parent 8394173 commit 4cf5772
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/graphql-server/src/functions/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ export const formatError: FormatErrorHandler = (err: any, message: string) => {
return err
}

// Converts base64 string to ArrayBuffer for Request body
function base64ToArrayBuffer(base64: string): ArrayBuffer {
const binary_string = atob(base64)
const len = binary_string.length
const bytes = new Uint8Array(len)
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i)
}
return bytes.buffer
}

/**
* Creates an Enveloped GraphQL Server, configured with default Redwood plugins
*
Expand Down Expand Up @@ -268,7 +279,7 @@ export const createGraphQLHandler = ({

const requestUrl = new URL(
event.path,
protocol + '://' + event.requestContext?.domainName || 'localhost'
protocol + '://' + (event.requestContext?.domainName || 'localhost')
)

if (event.multiValueQueryStringParameters) {
Expand All @@ -290,16 +301,23 @@ export const createGraphQLHandler = ({
}
}

if (event.httpMethod === 'GET' || event.httpMethod === 'HEAD') {
if (
event.httpMethod === 'GET' ||
event.httpMethod === 'HEAD' ||
event.body == null
) {
return new Request(requestUrl.toString(), {
method: event.httpMethod,
headers: requestHeaders,
})
} else {
const body = event.isBase64Encoded
? base64ToArrayBuffer(event.body)
: event.body
return new Request(requestUrl.toString(), {
method: event.httpMethod,
headers: requestHeaders,
body: event.body,
body,
})
}
}
Expand Down

0 comments on commit 4cf5772

Please sign in to comment.