Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ let package = Package(
.package(url: "https://github.com/apple/swift-system", from: "1.2.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.56.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.17.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.2"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -45,5 +50,28 @@ let package = Package(
.target(name: "SwiftSDKGenerator"),
]
),
.target(
name: "AsyncProcess",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
.product(name: "Logging", package: "swift-log"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOExtras", package: "swift-nio-extras"),
.product(name: "DequeModule", package: "swift-collections"),
.product(name: "SystemPackage", package: "swift-system"),
]
),
.testTarget(
name: "AsyncProcessTests",
dependencies: [
"AsyncProcess",
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "Logging", package: "swift-log"),
]
),
]
)
63 changes: 63 additions & 0 deletions Sources/AsyncProcess/ChunkSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import NIO

#if os(Linux) || os(Android) || os(Windows)
@preconcurrency import Foundation
#else
import Foundation
#endif

public struct IllegalStreamConsumptionError: Error {
var description: String
}

public struct ChunkSequence: AsyncSequence & Sendable {
private let fileHandle: FileHandle?
private let group: EventLoopGroup

public init(takingOwnershipOfFileHandle fileHandle: FileHandle?, group: EventLoopGroup) {
self.group = group
self.fileHandle = fileHandle
}

public func makeAsyncIterator() -> AsyncIterator {
// This will close the file handle.
AsyncIterator(try! self.fileHandle?.fileContentStream(eventLoop: self.group.any()))
}

public typealias Element = ByteBuffer
public struct AsyncIterator: AsyncIteratorProtocol {
public typealias Element = ByteBuffer
internal typealias UnderlyingSequence = FileContentStream

private var underlyingIterator: UnderlyingSequence.AsyncIterator?

internal init(_ underlyingSequence: UnderlyingSequence?) {
self.underlyingIterator = underlyingSequence?.makeAsyncIterator()
}

public mutating func next() async throws -> Element? {
if self.underlyingIterator != nil {
return try await self.underlyingIterator!.next()
} else {
throw IllegalStreamConsumptionError(
description: """
Either `.discard`ed, `.inherit`ed or redirected this stream to a `.fileHandle`,
cannot also consume it. To consume, please `.stream` it.
"""
)
}
}
}
}
27 changes: 27 additions & 0 deletions Sources/AsyncProcess/EOFSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

public struct EOFSequence<Element>: AsyncSequence & Sendable {
public typealias Element = Element

public struct AsyncIterator: AsyncIteratorProtocol {
public mutating func next() async throws -> Element? {
nil
}
}

public init(of type: Element.Type = Element.self) {}

public func makeAsyncIterator() -> AsyncIterator {
AsyncIterator()
}
}
Loading