Skip to content

Commit

Permalink
bring "public func fetchLatestPSL() async throws" to Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEidinger committed Jul 21, 2023
1 parent 7ca1ee5 commit f926eb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
- "**/*.dat"

jobs:
build:
build-mac:

runs-on: macos-latest

Expand All @@ -31,4 +31,15 @@ jobs:
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
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
15 changes: 12 additions & 3 deletions Sources/TLDExtract/TLDExtract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f926eb1

Please sign in to comment.