Repository: honojs/hono
File: src/adapter/aws-lambda/handler.ts (TypeScript)

Code before:
    result.headers[key] = value
  })

  return result
}

function createRequest(event: APIGatewayProxyEvent | APIGatewayProxyEventV2) {
  const queryString = extractQueryString(event)
  const urlPath = isProxyEventV2(event)
    ? `https://${event.requestContext.domainName}${event.rawPath}`
    : `https://${event.requestContext.domainName}${event.path}`

  const url = queryString ? `${urlPath}?${queryString}` : urlPath

  const headers = new Headers()
  for (const [k, v] of Object.entries(event.headers)) {
    if (v) headers.set(k, v)
  }

  const requestInit: RequestInit = {
    headers,
    method: event.httpMethod,
  }

  if (event.body) {
    requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body
  }

  return new Request(url, requestInit)
}

function extractQueryString(event: APIGatewayProxyEvent | APIGatewayProxyEventV2) {
  if (isProxyEventV2(event)) {
    return event.rawQueryString
  }

  return Object.entries(event.queryStringParameters)
    .filter(([, value]) => value)
    .map(([key, value]) => `${key}=${value}`)
    .join('&')
}

function isProxyEventV2(
  event: APIGatewayProxyEvent | APIGatewayProxyEventV2
): event is APIGatewayProxyEventV2 {
  return Object.prototype.hasOwnProperty.call(event, 'rawPath')
}

async function fromBufferTostring(res: Response) {
  const arrayBuffer = await res.arrayBuffer()
  return String.fromCharCode(...new Uint8Array(arrayBuffer))

Review comment (suggestion):
Use `atob`/`btoa` instead of `Buffer`. 

We can use `Buffer` on AWS Lambda with Node.js, but the Hono core consistently uses `btoa`/`atob` instead of `Buffer`, so use that.

Code after:
  return result
}

const createRequest = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2) => {
  const queryString = extractQueryString(event)
  const urlPath = isProxyEventV2(event)
    ? `https://${event.requestContext.domainName}${event.rawPath}`
    : `https://${event.requestContext.domainName}${event.path}`

  const url = queryString ? `${urlPath}?${queryString}` : urlPath

  const headers = new Headers()
  for (const [k, v] of Object.entries(event.headers)) {
    if (v) headers.set(k, v)
  }

  const requestInit: RequestInit = {
    headers,
    method: event.httpMethod,
  }

  if (event.body) {
    requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body
  }

  return new Request(url, requestInit)
}

const extractQueryString = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2) => {
  if (isProxyEventV2(event)) {
    return event.rawQueryString
  }

  return Object.entries(event.queryStringParameters || {})
    .filter(([, value]) => value)
    .map(([key, value]) => `${key}=${value}`)
    .join('&')
}

const isProxyEventV2 = (
  event: APIGatewayProxyEvent | APIGatewayProxyEventV2
): event is APIGatewayProxyEventV2 => {
  return Object.prototype.hasOwnProperty.call(event, 'rawPath')
}

const fromReadableToString = async (res: Response) => {
  const stream = res.body || new ReadableStream()
  const decoder = new TextDecoder()
  let string = ''

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment