From f926eb17c063764586c78b94f93eb84eace9216d Mon Sep 17 00:00:00 2001 From: Marco Eidinger Date: Fri, 21 Jul 2023 14:19:56 -0700 Subject: [PATCH] bring "public func fetchLatestPSL() async throws" to Linux --- .github/workflows/swift.yml | 15 +++++++++++++-- Sources/TLDExtract/TLDExtract.swift | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 9b02142..4979c30 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -16,7 +16,7 @@ on: - "**/*.dat" jobs: - build: + build-mac: runs-on: macos-latest @@ -31,4 +31,15 @@ jobs: - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + + build-linux: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v \ No newline at end of file diff --git a/Sources/TLDExtract/TLDExtract.swift b/Sources/TLDExtract/TLDExtract.swift index 420f77e..2e090ab 100644 --- a/Sources/TLDExtract/TLDExtract.swift +++ b/Sources/TLDExtract/TLDExtract.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /** Extract root domain, top level domain (TLD), second level domain or subdomain from a `URL` or hostname `String` @@ -43,16 +46,22 @@ public class TLDExtract { self.tldParser = TLDParser(dataSet: dataSet) } -#if !os(Linux) /// invoke network request to fetch latest Public Suffix List (PSL) from a remote server ensuring that extractor operates most accurate @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public func fetchLatestPSL() async throws { let url: URL = URL(string: "https://publicsuffix.org/list/public_suffix_list.dat")! - let (data, _) = try await URLSession.shared.data(from: url) + let data: Data = try await withCheckedThrowingContinuation{ continuation in + URLSession.shared.dataTask(with: URLRequest(url: url)) { data, response, error in + if let data = data { + continuation.resume(returning: data) + } else if let error = error { + continuation.resume(throwing: error) + } + }.resume() + } let dataSet = try PSLParser().parse(data: data) self.tldParser = TLDParser(dataSet: dataSet) } -#endif /// Parameters: /// - host: Hostname to be extracted