Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only FoundationEssentials when possible #81

Merged
merged 15 commits into from
Dec 19, 2024

Conversation

t089
Copy link
Contributor

@t089 t089 commented Dec 14, 2024

Linking against Foundation on linux adds a hefty binary size penalty. Mostly due to the ICU data from FoundationInternationalization. To address this we conditionally import FoundationEssentials instead of Foundation if possible and replace Foundation-only API with a different implementation.

Motivation:

Smaller binary sizes are better.

Modifications:

  • Replace import Foundation with import FoundationEssentials.
  • Update date parsing logic to use newer API if available.
  • Implement a custom parser for RFC5322 dates

Result:

Ability to build the package without linking Foundation.

@t089 t089 force-pushed the foundation-essentials branch from 40c5714 to 904af3d Compare December 14, 2024 16:47
Copy link
Contributor

@sebsto sebsto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sebsto sebsto added the semver/none No version bump required. label Dec 19, 2024
@sebsto
Copy link
Contributor

sebsto commented Dec 19, 2024

Thank you @t089 !
There is an issue with the license headers on some files and a couple of formatting checks that fails to validate
See https://github.com/swift-server/swift-aws-lambda-events/actions/runs/12408957260 for the details

To check the formatting, I usually download the script from https://github.com/swiftlang/github-workflows/blob/main/.github/workflows/scripts/check-swift-format.sh and run it locally.

}

private static func parseISO8601(dateString: String) throws -> Date {
if #available(macOS 12.0, *) {
return try Date(dateString, strategy: .iso8601)
Copy link
Contributor Author

@t089 t089 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I don't think this will actually compile on older swift versions on Linux where this api is not yet available in Foundation, need to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah there are a bunch of build errors with swift 5.10 on linux. will look into it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next version will support Swift 6 and onwards, aligned on the Swift Lambda Runtime

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you want to fix this or not. I'm happy to merge with a Swift 6 only change. If there is an easy fix, let's be nice with our existing users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the if #available(macOS 12, *) check was redundant as the conditional compile already excludes macOS altogether. Now it should compile for both on linux and macOS.

@sebsto
Copy link
Contributor

sebsto commented Dec 19, 2024

@t089 looks like your last commit breaks the Swift 6 unit tests
https://github.com/swift-server/swift-aws-lambda-events/actions/runs/12413722400/job/34657011160?pr=81

@t089
Copy link
Contributor Author

t089 commented Dec 19, 2024

@sebsto should be good now! got a bit confused with all the platform specific checks...

@sebsto sebsto merged commit 7bcecc5 into swift-server:main Dec 19, 2024
15 checks passed
@sebsto
Copy link
Contributor

sebsto commented Dec 19, 2024

Merged - thank you !

@adam-fowler
Copy link
Member

adam-fowler commented Dec 19, 2024

Given this is being used with more than HTTP headers I think we need to extend the number of timezone identifiers. The list is missing standard ones listed in RFC PST, EST, MST, CST (funnily RFC only lists US zones). Also does the parser deal with additional characters at the en. It is common to see email dates written as follows Sun, 22 Sep 2024 18:46:31 +0100 (BST) with the three character timezone at the end.

@sebsto
Copy link
Contributor

sebsto commented Dec 19, 2024

@adam-fowler these are valid concerns indeed. I created #85 to follow up

@t089
Copy link
Contributor Author

t089 commented Dec 19, 2024

Given this is being used with more than HTTP headers I think we need to extend the number of timezone identifiers. The list is missing standard ones listed in RFC PST, EST, MST, CST (funnily RFC only lists US zones).

This is fair, I was assuming this is only used with AWS APIs where UTC/GMT should suffice.

Also does the parser deal with additional characters at the en. It is common to see email dates written as follows Sun, 22 Sep 2024 18:46:31 +0100 (BST) with the three character timezone at the end.

Yes I think this should be covered:

    func testWithLeadingDayName() throws {
        let input = "Fri, 26 Jun 2020 03:04:03 -0500 (CDT)"
        let date = try strategy.parse(input)
        XCTAssertEqual("2020-06-26 08:04:03 +0000", date.description)
    }

The Tz in brackets is ignored.

@adam-fowler
Copy link
Member

This is fair, I was assuming this is only used with AWS APIs where UTC/GMT should suffice

For SES you are decoding the email header which could come from any email provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants