@effect/platform
Advanced tools
Comparing version 0.10.3 to 0.10.4
@@ -162,2 +162,6 @@ /** | ||
readonly writeFile: (path: string, data: Uint8Array, options?: WriteFileOptions) => Effect.Effect<never, PlatformError, void>; | ||
/** | ||
* Write a string to a file at `path`. | ||
*/ | ||
readonly writeFileString: (path: string, data: string, options?: WriteFileStringOptions) => Effect.Effect<never, PlatformError, void>; | ||
} | ||
@@ -270,2 +274,10 @@ /** | ||
* @since 1.0.0 | ||
* @category options | ||
*/ | ||
export interface WriteFileStringOptions { | ||
readonly flag?: OpenFlag; | ||
readonly mode?: number; | ||
} | ||
/** | ||
* @since 1.0.0 | ||
* @category tag | ||
@@ -278,3 +290,3 @@ */ | ||
*/ | ||
export declare const make: (impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink">) => FileSystem; | ||
export declare const make: (impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink" | "writeFileString">) => FileSystem; | ||
/** | ||
@@ -303,4 +315,5 @@ * @since 1.0.0 | ||
readonly stat: Effect.Effect<never, PlatformError, File.Info>; | ||
readonly read: (buffer: Uint8Array, options?: FileReadOptions) => Effect.Effect<never, PlatformError, Size>; | ||
readonly readAlloc: (size: Size, options?: FileReadOptions) => Effect.Effect<never, PlatformError, Option<Uint8Array>>; | ||
readonly seek: (offset: Size, from: SeekMode) => Effect.Effect<never, never, void>; | ||
readonly read: (buffer: Uint8Array) => Effect.Effect<never, PlatformError, Size>; | ||
readonly readAlloc: (size: Size) => Effect.Effect<never, PlatformError, Option<Uint8Array>>; | ||
readonly truncate: (length?: Size) => Effect.Effect<never, PlatformError, void>; | ||
@@ -354,6 +367,3 @@ readonly write: (buffer: Uint8Array) => Effect.Effect<never, PlatformError, Size>; | ||
*/ | ||
export interface FileReadOptions { | ||
readonly offset?: Size; | ||
readonly length?: Size; | ||
} | ||
export type SeekMode = "start" | "current"; | ||
//# sourceMappingURL=FileSystem.d.ts.map |
@@ -25,3 +25,3 @@ "use strict"; | ||
exports.ProcessId = ProcessId; | ||
const CommandExecutor = /*#__PURE__*/(0, _Context.Tag)(); | ||
const CommandExecutor = /*#__PURE__*/(0, _Context.Tag)("@effect/platform/CommandExecutor"); | ||
/** @internal */ | ||
@@ -28,0 +28,0 @@ exports.CommandExecutor = CommandExecutor; |
@@ -16,3 +16,3 @@ "use strict"; | ||
*/ | ||
const Console = /*#__PURE__*/(0, _Context.Tag)(); | ||
const Console = /*#__PURE__*/(0, _Context.Tag)("@effect/platform/Console"); | ||
/** @internal */ | ||
@@ -19,0 +19,0 @@ exports.Console = Console; |
@@ -8,2 +8,3 @@ "use strict"; | ||
var _Context = /*#__PURE__*/require("@effect/data/Context"); | ||
var _Function = /*#__PURE__*/require("@effect/data/Function"); | ||
var Option = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Option")); | ||
@@ -17,3 +18,3 @@ var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/io/Effect")); | ||
/** @internal */ | ||
const tag = /*#__PURE__*/(0, _Context.Tag)(); | ||
const tag = /*#__PURE__*/(0, _Context.Tag)("@effect/platform/FileSystem"); | ||
/** @internal */ | ||
@@ -36,9 +37,17 @@ exports.tag = tag; | ||
}), | ||
stream: (path, options) => Stream.unwrapScoped(Effect.map(file => stream(file, options))(impl.open(path, { | ||
stream: (path, options) => Stream.unwrapScoped(Effect.map(file => stream(file, options))((options?.offset ? Effect.tap(file => file.seek(options.offset, "start")) : _Function.identity)(impl.open(path, { | ||
flag: "r" | ||
}))), | ||
})))), | ||
sink: (path, options) => Sink.unwrapScoped(Effect.map(file => Sink.forEach(_ => file.writeAll(_)))(impl.open(path, { | ||
flag: "w", | ||
...options | ||
}))) | ||
}))), | ||
writeFileString: (path, data, options) => Effect.flatMap(Effect.try({ | ||
try: () => new TextEncoder().encode(data), | ||
catch: () => Error.BadArgument({ | ||
module: "FileSystem", | ||
method: "writeFileString", | ||
message: "could not encode string" | ||
}) | ||
}), _ => impl.writeFile(path, _, options)) | ||
}); | ||
@@ -51,12 +60,9 @@ }; | ||
bytesToRead, | ||
chunkSize = Size(64 * 1024), | ||
offset = Size(0) | ||
} = {}) => Stream.bufferChunks(Stream.unfoldEffect(offset, position => { | ||
if (bytesToRead !== undefined && bytesToRead <= position - offset) { | ||
chunkSize = Size(64 * 1024) | ||
} = {}) => Stream.bufferChunks(Stream.unfoldEffect(BigInt(0), totalBytesRead => { | ||
if (bytesToRead !== undefined && bytesToRead <= totalBytesRead) { | ||
return Effect.succeed(Option.none()); | ||
} | ||
const toRead = bytesToRead !== undefined && bytesToRead - (position - offset) < chunkSize ? bytesToRead - (position - offset) : chunkSize; | ||
return Effect.map(Option.map(buf => [buf, Size(position + BigInt(buf.length))]))(file.readAlloc(toRead, { | ||
offset: position | ||
})); | ||
const toRead = bytesToRead !== undefined && bytesToRead - totalBytesRead < chunkSize ? bytesToRead - totalBytesRead : chunkSize; | ||
return Effect.map(file.readAlloc(toRead), Option.map(buf => [buf, Size(totalBytesRead + BigInt(buf.length))])); | ||
}), { | ||
@@ -63,0 +69,0 @@ capacity: bufferSize |
@@ -16,3 +16,3 @@ "use strict"; | ||
/** @internal */ | ||
const Path = /*#__PURE__*/(0, _Context.Tag)(); | ||
const Path = /*#__PURE__*/(0, _Context.Tag)("@effect/platform/Path"); | ||
/** @internal */ | ||
@@ -19,0 +19,0 @@ exports.Path = Path; |
{ | ||
"name": "@effect/platform", | ||
"version": "0.10.3", | ||
"version": "0.10.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -240,2 +240,10 @@ /** | ||
) => Effect.Effect<never, PlatformError, void> | ||
/** | ||
* Write a string to a file at `path`. | ||
*/ | ||
readonly writeFileString: ( | ||
path: string, | ||
data: string, | ||
options?: WriteFileStringOptions | ||
) => Effect.Effect<never, PlatformError, void> | ||
} | ||
@@ -372,2 +380,11 @@ | ||
* @since 1.0.0 | ||
* @category options | ||
*/ | ||
export interface WriteFileStringOptions { | ||
readonly flag?: OpenFlag | ||
readonly mode?: number | ||
} | ||
/** | ||
* @since 1.0.0 | ||
* @category tag | ||
@@ -381,4 +398,5 @@ */ | ||
*/ | ||
export const make: (impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink">) => FileSystem = | ||
internal.make | ||
export const make: ( | ||
impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink" | "writeFileString"> | ||
) => FileSystem = internal.make | ||
@@ -413,9 +431,11 @@ /** | ||
readonly stat: Effect.Effect<never, PlatformError, File.Info> | ||
readonly seek: ( | ||
offset: Size, | ||
from: SeekMode | ||
) => Effect.Effect<never, never, void> | ||
readonly read: ( | ||
buffer: Uint8Array, | ||
options?: FileReadOptions | ||
buffer: Uint8Array | ||
) => Effect.Effect<never, PlatformError, Size> | ||
readonly readAlloc: ( | ||
size: Size, | ||
options?: FileReadOptions | ||
size: Size | ||
) => Effect.Effect<never, PlatformError, Option<Uint8Array>> | ||
@@ -489,5 +509,2 @@ readonly truncate: ( | ||
*/ | ||
export interface FileReadOptions { | ||
readonly offset?: Size | ||
readonly length?: Size | ||
} | ||
export type SeekMode = "start" | "current" |
@@ -22,3 +22,3 @@ import * as Brand from "@effect/data/Brand" | ||
/** @internal */ | ||
export const CommandExecutor = Tag<_CommandExecutor.CommandExecutor>() | ||
export const CommandExecutor = Tag<_CommandExecutor.CommandExecutor>("@effect/platform/CommandExecutor") | ||
@@ -25,0 +25,0 @@ /** @internal */ |
@@ -10,3 +10,3 @@ import { Tag } from "@effect/data/Context" | ||
*/ | ||
export const Console = Tag<Console_>() | ||
export const Console = Tag<Console_>("@effect/platform/Console") | ||
@@ -13,0 +13,0 @@ /** @internal */ |
import { Tag } from "@effect/data/Context" | ||
import { pipe } from "@effect/data/Function" | ||
import { identity, pipe } from "@effect/data/Function" | ||
import * as Option from "@effect/data/Option" | ||
@@ -11,3 +11,3 @@ import * as Effect from "@effect/io/Effect" | ||
/** @internal */ | ||
export const tag = Tag<FileSystem>() | ||
export const tag = Tag<FileSystem>("@effect/platform/FileSystem") | ||
@@ -18,3 +18,5 @@ /** @internal */ | ||
/** @internal */ | ||
export const make = (impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink">): FileSystem => { | ||
export const make = ( | ||
impl: Omit<FileSystem, "exists" | "readFileString" | "stream" | "sink" | "writeFileString"> | ||
): FileSystem => { | ||
return tag.of({ | ||
@@ -41,2 +43,5 @@ ...impl, | ||
impl.open(path, { flag: "r" }), | ||
options?.offset ? | ||
Effect.tap((file) => file.seek(options.offset!, "start")) : | ||
identity, | ||
Effect.map((file) => stream(file, options)), | ||
@@ -50,2 +55,15 @@ Stream.unwrapScoped | ||
Sink.unwrapScoped | ||
), | ||
writeFileString: (path, data, options) => | ||
Effect.flatMap( | ||
Effect.try({ | ||
try: () => new TextEncoder().encode(data), | ||
catch: () => | ||
Error.BadArgument({ | ||
module: "FileSystem", | ||
method: "writeFileString", | ||
message: "could not encode string" | ||
}) | ||
}), | ||
(_) => impl.writeFile(path, _, options) | ||
) | ||
@@ -59,20 +77,17 @@ }) | ||
bytesToRead, | ||
chunkSize = Size(64 * 1024), | ||
offset = Size(0) | ||
chunkSize = Size(64 * 1024) | ||
}: StreamOptions = {}) => | ||
Stream.bufferChunks( | ||
Stream.unfoldEffect(offset, (position) => { | ||
if (bytesToRead !== undefined && bytesToRead <= position - offset) { | ||
Stream.unfoldEffect(BigInt(0), (totalBytesRead) => { | ||
if (bytesToRead !== undefined && bytesToRead <= totalBytesRead) { | ||
return Effect.succeed(Option.none()) | ||
} | ||
const toRead = bytesToRead !== undefined && bytesToRead - (position - offset) < chunkSize | ||
? bytesToRead - (position - offset) | ||
const toRead = bytesToRead !== undefined && (bytesToRead - totalBytesRead) < chunkSize | ||
? bytesToRead - totalBytesRead | ||
: chunkSize | ||
return pipe( | ||
file.readAlloc(toRead as Size_, { offset: position }), | ||
Effect.map( | ||
Option.map((buf) => [buf, Size(position + BigInt(buf.length))] as const) | ||
) | ||
return Effect.map( | ||
file.readAlloc(toRead as Size_), | ||
Option.map((buf) => [buf, Size(totalBytesRead + BigInt(buf.length))] as const) | ||
) | ||
@@ -79,0 +94,0 @@ }), |
@@ -10,3 +10,3 @@ import { Tag } from "@effect/data/Context" | ||
/** @internal */ | ||
export const Path = Tag<_Path>() | ||
export const Path = Tag<_Path>("@effect/platform/Path") | ||
@@ -13,0 +13,0 @@ /** @internal */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
316292
5416