Skip to content

Commit

Permalink
Fix vercel encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Aug 25, 2023
1 parent f34ef3a commit a823e82
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Plugins/VercelPackager/Server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ const server = http.createServer(async (req, res) => {
const body = await readBody(req)
const _res = await invoke({ method, path, headers, body })
const _body = JSON.parse(await readBody(_res))
const encoding = _body.isBase64Encoded ? 'base64' : 'utf8'
res.writeHead(_body.statusCode, _body.headers)
res.end(Buffer.from(_body.body || '', encoding))
res.end(Buffer.from(_body.body || '', _body.encoding || 'utf8'))
} catch (err) {
console.error(err)
res.writeHead(500, {})
Expand Down
18 changes: 11 additions & 7 deletions Sources/Vercel/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
//

public struct Response: Codable, Sendable {
public enum Encoding: String, Codable, Sendable {
case base64
}

public var statusCode: HTTPResponseStatus
public var headers: HTTPHeaders?
public var body: String?
public var isBase64Encoded: Bool?
public var encoding: Encoding?
public var cookies: [String]?

public var didSend: Bool {
Expand All @@ -20,28 +24,28 @@ public struct Response: Codable, Sendable {
statusCode: HTTPResponseStatus = .ok,
headers: HTTPHeaders? = nil,
body: String? = nil,
isBase64Encoded: Bool? = nil,
encoding: Encoding? = nil,
cookies: [String]? = nil
) {
self.statusCode = statusCode
self.headers = headers
self.body = body
self.isBase64Encoded = isBase64Encoded
self.encoding = encoding
self.cookies = cookies
}

public func with(
statusCode: HTTPResponseStatus? = nil,
headers: HTTPHeaders? = nil,
body: String? = nil,
isBase64Encoded: Bool? = nil,
encoding: Encoding? = nil,
cookies: [String]? = nil
) -> Self {
return .init(
statusCode: statusCode ?? self.statusCode,
headers: headers ?? self.headers,
body: body ?? self.body,
isBase64Encoded: isBase64Encoded ?? self.isBase64Encoded,
encoding: encoding ?? self.encoding,
cookies: cookies ?? self.cookies
)
}
Expand Down Expand Up @@ -124,11 +128,11 @@ extension Response {
}

public func send(_ data: Data) -> Self {
return with(body: data.base64EncodedString(), isBase64Encoded: true)
return with(body: data.base64EncodedString(), encoding: .base64)
}

public func send(_ bytes: [UInt8]) -> Self {
return with(body: Data(bytes).base64EncodedString(), isBase64Encoded: true)
return with(body: Data(bytes).base64EncodedString(), encoding: .base64)
}

public func send<T>(
Expand Down
2 changes: 1 addition & 1 deletion Sources/VercelVapor/VaporHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ extension Vercel.Response {
statusCode: statusCode,
headers: headers,
body: bytes?.base64String(),
isBase64Encoded: true
encoding: bytes.map { _ in .base64 }
)
}

Expand Down

0 comments on commit a823e82

Please sign in to comment.