Skip to content

Commit 25eb6e1

Browse files
authored
Explicitly only use FoundationEssentials where possible (#436)
We want that the runtime only depends on `FoundationEssentials` where available (ie. on linux) to ensure small binary size. ### Motivation: Smaller binary size is good for lambda deployment and cold-start times. The runtime should only depend on `FoundationEssentials`. ### Modifications: - replace `import Foundation` with `import FoundationEssentials` if `FoundationEssentials` is available. - I also applied the same treatment to tests to ensure that catch error where tests run on linux and we use API that is only available in `Foundation` which easily happens when you develop on macOS (where always full `Foundation` is available). ### Result: This should allow builds without linking full `Foundation`.
1 parent b687a09 commit 25eb6e1

File tree

12 files changed

+68
-8
lines changed

12 files changed

+68
-8
lines changed

Examples/BackgroundTasks/Sources/main.swift

+5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaRuntime
16+
17+
#if canImport(FoundationEssentials)
18+
import FoundationEssentials
19+
#else
1620
import Foundation
21+
#endif
1722

1823
struct BackgroundProcessingHandler: LambdaWithBackgroundProcessingHandler {
1924
struct Input: Decodable {

Sources/AWSLambdaRuntime/Context+Foundation.swift

+4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
import AWSLambdaRuntimeCore
1616

17+
#if canImport(FoundationEssentials)
18+
import FoundationEssentials
19+
#else
1720
import struct Foundation.Date
21+
#endif
1822

1923
extension LambdaContext {
2024
var deadlineDate: Date {

Sources/AWSLambdaRuntime/Vendored/ByteBuffer-foundation.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29-
import Foundation
3029
import NIOCore
3130

31+
#if canImport(FoundationEssentials)
32+
import FoundationEssentials
33+
#else
34+
import Foundation
35+
#endif
36+
3237
// This is NIO's `NIOFoundationCompat` module which at the moment only adds `ByteBuffer` utility methods
3338
// for Foundation's `Data` type.
3439
//

Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import Logging
1716
import NIOConcurrencyHelpers
1817
import NIOCore
1918

19+
#if canImport(FoundationEssentials)
20+
import FoundationEssentials
21+
#else
22+
import Foundation
23+
#endif
24+
2025
// We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
2126
// We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
2227
// sadly crashes the compiler today.

Sources/MockServer/main.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
15+
import Dispatch
1616
import NIOCore
1717
import NIOHTTP1
1818
import NIOPosix
1919

20+
#if canImport(FoundationEssentials)
21+
import FoundationEssentials
22+
#else
23+
import Foundation
24+
#endif
25+
2026
struct MockServer {
2127
private let group: EventLoopGroup
2228
private let host: String

Tests/AWSLambdaRuntimeCoreTests/InvocationTests.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import NIOHTTP1
1716
import Testing
1817

1918
@testable import AWSLambdaRuntimeCore
2019

20+
#if canImport(FoundationEssentials)
21+
import FoundationEssentials
22+
#else
23+
import Foundation
24+
#endif
25+
2126
@Suite
2227
struct InvocationTest {
2328
@Test

Tests/AWSLambdaRuntimeCoreTests/LambdaMockClient.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaRuntimeCore
16-
import Foundation
1716
import Logging
1817
import NIOCore
1918

19+
#if canImport(FoundationEssentials)
20+
import FoundationEssentials
21+
#else
22+
import Foundation
23+
#endif
24+
2025
struct LambdaMockWriter: LambdaRuntimeClientResponseStreamWriter {
2126
var underlying: LambdaMockClient
2227

Tests/AWSLambdaRuntimeCoreTests/LambdaRequestIDTests.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import NIOCore
1716
import Testing
1817

1918
@testable import AWSLambdaRuntimeCore
2019

20+
#if canImport(FoundationEssentials)
21+
import FoundationEssentials
22+
#else
23+
import Foundation
24+
#endif
25+
2126
@Suite("LambdaRequestID tests")
2227
struct LambdaRequestIDTest {
2328
@Test
@@ -100,6 +105,7 @@ struct LambdaRequestIDTest {
100105
#expect(buffer.readableBytes == readableBeforeRead)
101106
}
102107

108+
#if os(macOS)
103109
@Test
104110
func testInitFromNSStringSuccess() {
105111
let nsString = NSMutableString(capacity: 16)
@@ -121,6 +127,7 @@ struct LambdaRequestIDTest {
121127
#expect(requestID?.uuidString == LambdaRequestID(uuidString: nsString as String)?.uuidString)
122128
#expect(requestID?.uppercased == nsString as String)
123129
}
130+
#endif
124131

125132
@Test
126133
func testUnparse() {

Tests/AWSLambdaRuntimeCoreTests/LambdaRunLoopTests.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import Logging
1716
import NIOCore
1817
import Testing
1918

2019
@testable import AWSLambdaRuntimeCore
2120

21+
#if canImport(FoundationEssentials)
22+
import FoundationEssentials
23+
#else
24+
import Foundation
25+
#endif
26+
2227
@Suite
2328
struct LambdaRunLoopTests {
2429
struct MockEchoHandler: StreamingLambdaHandler {

Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation // for JSON
1615
import Logging
1716
import NIOCore
1817
import NIOHTTP1
1918
import NIOPosix
2019

2120
@testable import AWSLambdaRuntimeCore
2221

22+
#if canImport(FoundationEssentials)
23+
import FoundationEssentials
24+
#else
25+
import Foundation
26+
#endif
27+
2328
func withMockServer<Result>(
2429
behaviour: some LambdaServerBehavior,
2530
port: Int = 0,

Tests/AWSLambdaRuntimeCoreTests/Utils.swift

+4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#if canImport(FoundationEssentials)
16+
import FoundationEssentials
17+
#else
1518
import Foundation
19+
#endif
1620

1721
extension Date {
1822
var millisSinceEpoch: Int64 {

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ Background tasks allow code to execute asynchronously after the main response ha
253253
Here is an example of a minimal function that waits 10 seconds after it returned a response but before the handler returns.
254254
```swift
255255
import AWSLambdaRuntime
256+
#if canImport(FoundationEssentials)
257+
import FoundationEssentials
258+
#else
256259
import Foundation
260+
#endif
257261

258262
struct BackgroundProcessingHandler: LambdaWithBackgroundProcessingHandler {
259263
struct Input: Decodable {

0 commit comments

Comments
 (0)