Skip to content

Commit 05ed006

Browse files
committed
Use NIOFileSystem to load dotEnv
1 parent db710ca commit 05ed006

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

Sources/Hummingbird/Environment.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import HummingbirdCore
1616
import NIOCore
17+
import _NIOFileSystem
1718

1819
#if canImport(FoundationEssentials)
1920
import FoundationEssentials
@@ -172,18 +173,10 @@ public struct Environment: Sendable, Decodable, ExpressibleByDictionaryLiteral {
172173
/// Load `.env` file into string
173174
internal static func loadDotEnv(_ dovEnvPath: String = ".env") async -> String? {
174175
do {
175-
let fileHandle = try NIOFileHandle(path: dovEnvPath)
176-
defer {
177-
try? fileHandle.close()
176+
return try await FileSystem.shared.withFileHandle(forReadingAt: .init(dovEnvPath)) { fileHandle in
177+
let buffer = try await fileHandle.readToEnd(maximumSizeAllowed: .unlimited)
178+
return String(buffer: buffer)
178179
}
179-
let fileRegion = try FileRegion(fileHandle: fileHandle)
180-
let contents = try fileHandle.withUnsafeFileDescriptor { descriptor in
181-
[UInt8](unsafeUninitializedCapacity: fileRegion.readableBytes) { bytes, size in
182-
size = fileRegion.readableBytes
183-
read(descriptor, .init(bytes.baseAddress), size)
184-
}
185-
}
186-
return String(bytes: contents, encoding: .utf8)
187180
} catch {
188181
return nil
189182
}

Tests/HummingbirdTests/FileIOTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ final class FileIOTests: XCTestCase {
104104
}
105105

106106
let contents = try await FileSystem.shared.withFileHandle(forReadingAt: .init(filename)) { read in
107-
try await read.readToEnd(fromAbsoluteOffset: 0, maximumSizeAllowed: .megabytes(1000))
107+
try await read.readToEnd(fromAbsoluteOffset: 0, maximumSizeAllowed: .unlimited)
108108
}
109109
try await FileSystem.shared.removeItem(at: .init(filename))
110110
XCTAssertEqual(String(buffer: contents), "This is a test")
@@ -127,7 +127,7 @@ final class FileIOTests: XCTestCase {
127127
}
128128

129129
let contents = try await FileSystem.shared.withFileHandle(forReadingAt: .init(filename)) { read in
130-
try await read.readToEnd(fromAbsoluteOffset: 0, maximumSizeAllowed: .megabytes(1000))
130+
try await read.readToEnd(fromAbsoluteOffset: 0, maximumSizeAllowed: .unlimited)
131131
}
132132
try await FileSystem.shared.removeItem(at: .init(filename))
133133
XCTAssertEqual(contents, buffer)

0 commit comments

Comments
 (0)