From 28339fe5952b852734985ac32e38e09747074b65 Mon Sep 17 00:00:00 2001 From: Andrew Barba Date: Mon, 28 Aug 2023 12:03:09 -0400 Subject: [PATCH] Support arm64 Lambdas (#17) * Build on native arch * Support custom architecture * Output architecture * Strip * Force build lhr * Try args * Try pre * Pass debug * Fix region name * Another * Another * Cleanup * Beef up trailing slash regex * Remove build region --- Plugins/VercelPackager/Utils.swift | 2 +- Plugins/VercelPackager/VercelOutput.swift | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Plugins/VercelPackager/Utils.swift b/Plugins/VercelPackager/Utils.swift index aab3c78..d2f0bc5 100644 --- a/Plugins/VercelPackager/Utils.swift +++ b/Plugins/VercelPackager/Utils.swift @@ -8,7 +8,7 @@ import Foundation import PackagePlugin -public enum Architecture: String { +public enum Architecture: String, Codable { case arm64 = "arm64" case x86 = "x86_64" } diff --git a/Plugins/VercelPackager/VercelOutput.swift b/Plugins/VercelPackager/VercelOutput.swift index c68d48a..dee0e1f 100644 --- a/Plugins/VercelPackager/VercelOutput.swift +++ b/Plugins/VercelPackager/VercelOutput.swift @@ -160,6 +160,13 @@ extension VercelOutput { argument("port") ?? "7676" } + public var architecture: Architecture { + if let value = argument("arch"), let arch = Architecture(rawValue: value) { + return arch + } + return Utils.currentArchitecture ?? .x86 + } + public func argument(_ key: String) -> String? { guard let index = arguments.firstIndex(of: "--\(key)") else { return nil @@ -368,11 +375,11 @@ extension VercelOutput { let vercel = vercelConfiguration() let routes: [OutputConfiguration.Route] = [ // Remove trailing slash - .init(src: "^/(.*)/$", headers: ["Location": "/$1"], status: 308), + .init(src: "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$", headers: ["Location": "/$1"], status: 308), // Handle filesystem .init(handle: "filesystem"), // Proxy all other routes - .init(src: "^(?:/(.*))$", dest: product.name, check: true) + .init(src: "^.*$", dest: product.name, check: true) ] let config = OutputConfiguration( routes: routes, @@ -415,6 +422,7 @@ extension VercelOutput { public struct FunctionConfiguration: Codable { public var runtime: String = "provided.al2" public var handler: String = "bootstrap" + public var architecture: Architecture? = nil public var memory: Int? = nil public var maxDuration: Int? = nil public var regions: [String]? = nil @@ -427,6 +435,7 @@ extension VercelOutput { public func writeFunctionConfigurations() throws { let config = FunctionConfiguration( + architecture: architecture, memory: .init(functionMemory), maxDuration: .init(functionDuration), regions: functionRegions?.components(separatedBy: ",").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } @@ -511,7 +520,7 @@ extension VercelOutput { executable: dockerToolPath, arguments: [ "run", - "--platform", "linux/x86_64", + "--platform", "linux/\(architecture.rawValue)", "--rm", "-v", "\(context.package.directory.string):/workspace", "-w", "/workspace", @@ -529,7 +538,7 @@ extension VercelOutput { executable: dockerToolPath, arguments: [ "run", - "--platform", "linux/x86_64", + "--platform", "linux/\(architecture.rawValue)", "--rm", "-v", "\(context.package.directory.string):/workspace", "-w", "/workspace",