diff --git a/packages/effect/src/logging/Logger.ts b/packages/effect/src/logging/Logger.ts index 2fd252e521..798d5d50f7 100644 --- a/packages/effect/src/logging/Logger.ts +++ b/packages/effect/src/logging/Logger.ts @@ -1280,22 +1280,22 @@ export const layer = < export const toFile = dual< ( path: string, - options?: - | FileSystem.OpenFileOptions & { - readonly batchWindow?: Duration.DurationInput | undefined - } - | undefined + options?: { + readonly flag?: FileSystem.OpenFlag | undefined + readonly mode?: number | undefined + readonly batchWindow?: Duration.DurationInput | undefined + } | undefined ) => ( self: Logger ) => Effect.Effect, PlatformError, Scope.Scope | FileSystem.FileSystem>, ( self: Logger, path: string, - options?: - | FileSystem.OpenFileOptions & { - readonly batchWindow?: Duration.DurationInput | undefined - } - | undefined + options?: { + readonly flag?: FileSystem.OpenFlag | undefined + readonly mode?: number | undefined + readonly batchWindow?: Duration.DurationInput | undefined + } | undefined ) => Effect.Effect, PlatformError, Scope.Scope | FileSystem.FileSystem> >( (args) => isLogger(args[0]), diff --git a/packages/effect/src/platform/FileSystem.ts b/packages/effect/src/platform/FileSystem.ts index ce42f5e968..3eb54b462b 100644 --- a/packages/effect/src/platform/FileSystem.ts +++ b/packages/effect/src/platform/FileSystem.ts @@ -101,7 +101,11 @@ export interface FileSystem { */ readonly access: ( path: string, - options?: AccessFileOptions + options?: { + readonly ok?: boolean | undefined + readonly readable?: boolean | undefined + readonly writable?: boolean | undefined + } ) => Effect.Effect /** * Copy a file or directory from `fromPath` to `toPath`. @@ -111,7 +115,10 @@ export interface FileSystem { readonly copy: ( fromPath: string, toPath: string, - options?: CopyOptions + options?: { + readonly overwrite?: boolean | undefined + readonly preserveTimestamps?: boolean | undefined + } ) => Effect.Effect /** * Copy a file from `fromPath` to `toPath`. @@ -154,7 +161,10 @@ export interface FileSystem { */ readonly makeDirectory: ( path: string, - options?: MakeDirectoryOptions + options?: { + readonly recursive?: boolean | undefined + readonly mode?: number | undefined + } ) => Effect.Effect /** * Create a temporary directory. @@ -166,35 +176,41 @@ export interface FileSystem { * You can also specify a prefix for the directory name by setting the * `prefix` option. */ - readonly makeTempDirectory: ( - options?: MakeTempDirectoryOptions - ) => Effect.Effect + readonly makeTempDirectory: (options?: { + readonly directory?: string | undefined + readonly prefix?: string | undefined + }) => Effect.Effect /** * Create a temporary directory inside a scope. * * Functionally equivalent to `makeTempDirectory`, but the directory will be * automatically deleted when the scope is closed. */ - readonly makeTempDirectoryScoped: ( - options?: MakeTempDirectoryOptions - ) => Effect.Effect + readonly makeTempDirectoryScoped: (options?: { + readonly directory?: string | undefined + readonly prefix?: string | undefined + }) => Effect.Effect /** * Create a temporary file. * The directory creation is functionally equivalent to `makeTempDirectory`. * The file name will be a randomly generated string. */ - readonly makeTempFile: ( - options?: MakeTempFileOptions - ) => Effect.Effect + readonly makeTempFile: (options?: { + readonly directory?: string | undefined + readonly prefix?: string | undefined + readonly suffix?: string | undefined + }) => Effect.Effect /** * Create a temporary file inside a scope. * * Functionally equivalent to `makeTempFile`, but the file will be * automatically deleted when the scope is closed. */ - readonly makeTempFileScoped: ( - options?: MakeTempFileOptions - ) => Effect.Effect + readonly makeTempFileScoped: (options?: { + readonly directory?: string | undefined + readonly prefix?: string | undefined + readonly suffix?: string | undefined + }) => Effect.Effect /** * Open a file at `path` with the specified `options`. * @@ -202,7 +218,10 @@ export interface FileSystem { */ readonly open: ( path: string, - options?: OpenFileOptions + options?: { + readonly flag?: OpenFlag | undefined + readonly mode?: number | undefined + } ) => Effect.Effect /** * List the contents of a directory. @@ -212,7 +231,9 @@ export interface FileSystem { */ readonly readDirectory: ( path: string, - options?: ReadDirectoryOptions + options?: { + readonly recursive?: boolean | undefined + } ) => Effect.Effect, PlatformError> /** * Read the contents of a file. @@ -244,7 +265,16 @@ export interface FileSystem { */ readonly remove: ( path: string, - options?: RemoveOptions + options?: { + /** + * When `true`, you can recursively remove nested directories. + */ + readonly recursive?: boolean | undefined + /** + * When `true`, exceptions will be ignored if `path` does not exist. + */ + readonly force?: boolean | undefined + } ) => Effect.Effect /** * Rename a file or directory. @@ -258,7 +288,10 @@ export interface FileSystem { */ readonly sink: ( path: string, - options?: SinkOptions + options?: { + readonly flag?: OpenFlag | undefined + readonly mode?: number | undefined + } ) => Sink.Sink /** * Get information about a file at `path`. @@ -280,7 +313,11 @@ export interface FileSystem { */ readonly stream: ( path: string, - options?: StreamOptions + options?: { + readonly bytesToRead?: SizeInput | undefined + readonly chunkSize?: SizeInput | undefined + readonly offset?: SizeInput | undefined + } ) => Stream.Stream /** * Create a symbolic link from `fromPath` to `toPath`. @@ -315,7 +352,10 @@ export interface FileSystem { readonly writeFile: ( path: string, data: Uint8Array, - options?: WriteFileOptions + options?: { + readonly flag?: OpenFlag | undefined + readonly mode?: number | undefined + } ) => Effect.Effect /** * Write a string to a file at `path`. @@ -323,7 +363,10 @@ export interface FileSystem { readonly writeFileString: ( path: string, data: string, - options?: WriteFileStringOptions + options?: { + readonly flag?: OpenFlag | undefined + readonly mode?: number | undefined + } ) => Effect.Effect } @@ -646,175 +689,6 @@ export type OpenFlag = | "a+" | "ax+" -/** - * Options for checking file accessibility. - * - * These options control what level of access to test when using the `access` method. - * By default, the method tests if the file exists and is readable. - * - * @example - * ```ts - * import { Effect } from "effect" - * import { FileSystem } from "effect/platform" - * - * const program = Effect.gen(function* () { - * const fs = yield* FileSystem.FileSystem - * - * // Check if file exists - * yield* fs.access("./config.json") - * - * // Check if file is readable - * yield* fs.access("./data.txt", { readable: true }) - * - * // Check if file is writable - * yield* fs.access("./output.log", { writable: true }) - * - * // Check multiple permissions - * yield* fs.access("./script.sh", { - * readable: true, - * writable: true - * }) - * }) - * ``` - * - * @since 4.0.0 - * @category options - */ -export interface AccessFileOptions { - readonly ok?: boolean - readonly readable?: boolean - readonly writable?: boolean -} - -/** - * Options for creating directories. - * - * Controls the behavior when creating new directories, including permission modes - * and whether to create parent directories if they don't exist. - * - * @example - * ```ts - * import { Effect } from "effect" - * import { FileSystem } from "effect/platform" - * - * const program = Effect.gen(function* () { - * const fs = yield* FileSystem.FileSystem - * - * // Create a single directory - * yield* fs.makeDirectory("./temp") - * - * // Create nested directories recursively - * yield* fs.makeDirectory("./data/logs/app", { recursive: true }) - * - * // Create directory with specific permissions - * yield* fs.makeDirectory("./secure", { - * mode: 0o700, // Owner read/write/execute only - * recursive: false - * }) - * }) - * ``` - * - * @since 4.0.0 - * @category options - */ -export interface MakeDirectoryOptions { - readonly recursive?: boolean - readonly mode?: number -} - -/** - * @since 4.0.0 - * @category options - */ -export interface CopyOptions { - readonly overwrite?: boolean - readonly preserveTimestamps?: boolean -} - -/** - * @since 4.0.0 - * @category options - */ -export interface MakeTempDirectoryOptions { - readonly directory?: string - readonly prefix?: string -} - -/** - * @since 4.0.0 - * @category options - */ -export interface MakeTempFileOptions { - readonly directory?: string - readonly prefix?: string -} - -/** - * @since 4.0.0 - * @category options - */ -export interface OpenFileOptions { - readonly flag?: OpenFlag - readonly mode?: number -} - -/** - * @since 4.0.0 - * @category options - */ -export interface ReadDirectoryOptions { - readonly recursive?: boolean -} - -/** - * @since 4.0.0 - * @category options - */ -export interface RemoveOptions { - /** - * When `true`, you can recursively remove nested directories. - */ - readonly recursive?: boolean - /** - * When `true`, exceptions will be ignored if `path` does not exist. - */ - readonly force?: boolean -} - -/** - * @since 4.0.0 - * @category options - */ -export interface SinkOptions extends OpenFileOptions {} - -/** - * @since 4.0.0 - * @category options - */ -export interface StreamOptions { - readonly bytesToRead?: SizeInput | undefined - readonly chunkSize?: SizeInput | undefined - readonly offset?: SizeInput | undefined -} - -/** - * @since 4.0.0 - * @category options - */ -export interface WriteFileOptions { - readonly flag?: OpenFlag - readonly mode?: number -} - -/** - * @since 4.0.0 - * @category options - */ -export interface WriteFileStringOptions { - readonly flag?: OpenFlag - readonly mode?: number -} - /** * The service identifier for the FileSystem service. * diff --git a/packages/effect/src/unstable/http/HttpBody.ts b/packages/effect/src/unstable/http/HttpBody.ts index ee9a0da7d5..5484af0c51 100644 --- a/packages/effect/src/unstable/http/HttpBody.ts +++ b/packages/effect/src/unstable/http/HttpBody.ts @@ -326,7 +326,12 @@ export const stream = ( */ export const file = ( path: string, - options?: FileSystem.StreamOptions & { readonly contentType?: string } + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + readonly contentType?: string | undefined + } ): Effect.Effect => Effect.flatMap( FileSystem.FileSystem.asEffect(), @@ -346,7 +351,12 @@ export const file = ( export const fileFromInfo = ( path: string, info: FileSystem.File.Info, - options?: FileSystem.StreamOptions & { readonly contentType?: string } + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + readonly contentType?: string | undefined + } ): Effect.Effect => Effect.map( FileSystem.FileSystem.asEffect(), diff --git a/packages/effect/src/unstable/http/HttpClientRequest.ts b/packages/effect/src/unstable/http/HttpClientRequest.ts index aaad9f534e..7c1f3bc151 100644 --- a/packages/effect/src/unstable/http/HttpClientRequest.ts +++ b/packages/effect/src/unstable/http/HttpClientRequest.ts @@ -667,19 +667,34 @@ export const bodyStream: { export const bodyFile: { ( path: string, - options?: FileSystem.StreamOptions & { readonly contentType?: string } + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + readonly contentType?: string + } ): (self: HttpClientRequest) => Effect.Effect ( self: HttpClientRequest, path: string, - options?: FileSystem.StreamOptions & { readonly contentType?: string } + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + readonly contentType?: string + } ): Effect.Effect } = dual( (args) => isHttpClientRequest(args[0]), ( self: HttpClientRequest, path: string, - options?: FileSystem.StreamOptions & { readonly contentType?: string } + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + readonly contentType?: string + } ): Effect.Effect => Effect.map( HttpBody.file(path, options), diff --git a/packages/effect/src/unstable/http/HttpPlatform.ts b/packages/effect/src/unstable/http/HttpPlatform.ts index a25876b2bb..766a64488b 100644 --- a/packages/effect/src/unstable/http/HttpPlatform.ts +++ b/packages/effect/src/unstable/http/HttpPlatform.ts @@ -20,11 +20,19 @@ import * as Response from "./HttpServerResponse.ts" export class HttpPlatform extends ServiceMap.Key Effect.Effect readonly fileWebResponse: ( file: Body.HttpBody.FileLike, - options?: Response.Options.WithContent & FileSystem.StreamOptions + options?: Response.Options.WithContent & { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + } ) => Effect.Effect }>()("effect/http/HttpPlatform") {} @@ -47,7 +55,11 @@ export const make: (impl: { status: number, statusText: string | undefined, headers: Headers.Headers, - options?: FileSystem.StreamOptions + options?: { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + } ) => Response.HttpServerResponse }) => Effect.Effect< HttpPlatform["Service"], diff --git a/packages/effect/src/unstable/http/HttpServerResponse.ts b/packages/effect/src/unstable/http/HttpServerResponse.ts index 13d38069d7..6ab310caf3 100644 --- a/packages/effect/src/unstable/http/HttpServerResponse.ts +++ b/packages/effect/src/unstable/http/HttpServerResponse.ts @@ -328,7 +328,13 @@ const HttpPlatformKey = ServiceMap.Key( */ export const file = ( path: string, - options?: (Options & FileSystem.StreamOptions) | undefined + options?: + | (Options & { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + }) + | undefined ): Effect.Effect => Effect.flatMap( HttpPlatformKey.asEffect(), @@ -341,7 +347,13 @@ export const file = ( */ export const fileWeb = ( file: Body.HttpBody.FileLike, - options?: (Options.WithContent & FileSystem.StreamOptions) | undefined + options?: + | (Options.WithContent & { + readonly bytesToRead?: FileSystem.SizeInput | undefined + readonly chunkSize?: FileSystem.SizeInput | undefined + readonly offset?: FileSystem.SizeInput | undefined + }) + | undefined ): Effect.Effect => Effect.flatMap( HttpPlatformKey.asEffect(), diff --git a/packages/platform-node-shared/src/NodeFileSystem.ts b/packages/platform-node-shared/src/NodeFileSystem.ts index 53d8e69821..8138a89ab7 100644 --- a/packages/platform-node-shared/src/NodeFileSystem.ts +++ b/packages/platform-node-shared/src/NodeFileSystem.ts @@ -26,13 +26,13 @@ const handleBadArgument = (method: string) => (err: unknown) => // == access -const access = (() => { +const access = ((): FileSystem.FileSystem["access"] => { const nodeAccess = effectify( NFS.access, handleErrnoException("FileSystem", "access"), handleBadArgument("access") ) - return (path: string, options?: FileSystem.AccessFileOptions) => { + return (path, options) => { let mode = NFS.constants.F_OK if (options?.readable) { mode |= NFS.constants.R_OK @@ -46,13 +46,13 @@ const access = (() => { // == copy -const copy = (() => { +const copy = ((): FileSystem.FileSystem["copy"] => { const nodeCp = effectify( NFS.cp, handleErrnoException("FileSystem", "copy"), handleBadArgument("copy") ) - return (fromPath: string, toPath: string, options?: FileSystem.CopyOptions) => + return (fromPath, toPath, options) => nodeCp(fromPath, toPath, { force: options?.overwrite ?? false, preserveTimestamps: options?.preserveTimestamps ?? false, @@ -106,13 +106,13 @@ const link = (() => { // == makeDirectory -const makeDirectory = (() => { +const makeDirectory = ((): FileSystem.FileSystem["makeDirectory"] => { const nodeMkdir = effectify( NFS.mkdir, handleErrnoException("FileSystem", "makeDirectory"), handleBadArgument("makeDirectory") ) - return (path: string, options?: FileSystem.MakeDirectoryOptions) => + return (path, options) => nodeMkdir(path, { recursive: options?.recursive ?? false, mode: options?.mode @@ -121,13 +121,13 @@ const makeDirectory = (() => { // == makeTempDirectory -const makeTempDirectoryFactory = (method: string) => { +const makeTempDirectoryFactory = (method: string): FileSystem.FileSystem["makeTempDirectory"] => { const nodeMkdtemp = effectify( NFS.mkdtemp, handleErrnoException("FileSystem", method), handleBadArgument(method) ) - return (options?: FileSystem.MakeTempDirectoryOptions) => + return (options) => Effect.suspend(() => { const prefix = options?.prefix ?? "" const directory = typeof options?.directory === "string" @@ -141,13 +141,13 @@ const makeTempDirectory = makeTempDirectoryFactory("makeTempDirectory") // == remove -const removeFactory = (method: string) => { +const removeFactory = (method: string): FileSystem.FileSystem["remove"] => { const nodeRm = effectify( NFS.rm, handleErrnoException("FileSystem", method), handleBadArgument(method) ) - return (path: string, options?: FileSystem.RemoveOptions) => + return (path, options) => nodeRm( path, { recursive: options?.recursive ?? false, force: options?.force ?? false } @@ -157,12 +157,10 @@ const remove = removeFactory("remove") // == makeTempDirectoryScoped -const makeTempDirectoryScoped = (() => { +const makeTempDirectoryScoped = ((): FileSystem.FileSystem["makeTempDirectoryScoped"] => { const makeDirectory = makeTempDirectoryFactory("makeTempDirectoryScoped") const removeDirectory = removeFactory("makeTempDirectoryScoped") - return ( - options?: FileSystem.MakeTempDirectoryOptions - ) => + return (options) => Effect.acquireRelease( makeDirectory(options), (directory) => Effect.orDie(removeDirectory(directory, { recursive: true })) @@ -171,7 +169,7 @@ const makeTempDirectoryScoped = (() => { // == open -const openFactory = (method: string) => { +const openFactory = (method: string): FileSystem.FileSystem["open"] => { const nodeOpen = effectify( NFS.open, handleErrnoException("FileSystem", method), @@ -183,7 +181,7 @@ const openFactory = (method: string) => { handleBadArgument(method) ) - return (path: string, options?: FileSystem.OpenFileOptions) => + return (path, options) => pipe( Effect.acquireRelease( nodeOpen(path, options?.flag ?? "r", options?.mode), @@ -370,25 +368,24 @@ const makeFile = (() => { // == makeTempFile -const makeTempFileFactory = (method: string) => { +const makeTempFileFactory = (method: string): FileSystem.FileSystem["makeTempFile"] => { const makeDirectory = makeTempDirectoryFactory(method) - const open = openFactory(method) - const randomHexString = (bytes: number) => Effect.sync(() => Crypto.randomBytes(bytes).toString("hex")) - return (options?: FileSystem.MakeTempFileOptions) => - pipe( - Effect.zip(makeDirectory(options), randomHexString(6)), - Effect.map(([directory, random]) => Path.join(directory, random)), - Effect.tap((path) => Effect.scoped(open(path, { flag: "w+" }))) - ) + return Effect.fnUntraced(function*(options) { + const directory = yield* makeDirectory(options) + const random = Crypto.randomBytes(6).toString("hex") + const name = Path.join(directory, options?.suffix ? `${random}${options.suffix}` : random) + yield* writeFile(name, new Uint8Array(0)) + return name + }) } const makeTempFile = makeTempFileFactory("makeTempFile") // == makeTempFileScoped -const makeTempFileScoped = (() => { +const makeTempFileScoped = ((): FileSystem.FileSystem["makeTempFileScoped"] => { const makeFile = makeTempFileFactory("makeTempFileScoped") const removeDirectory = removeFactory("makeTempFileScoped") - return (options?: FileSystem.MakeTempFileOptions) => + return (options) => Effect.acquireRelease( makeFile(options), (file) => Effect.orDie(removeDirectory(Path.dirname(file), { recursive: true })) @@ -397,7 +394,7 @@ const makeTempFileScoped = (() => { // == readDirectory -const readDirectory = (path: string, options?: FileSystem.ReadDirectoryOptions) => +const readDirectory: FileSystem.FileSystem["readDirectory"] = (path, options) => Effect.tryPromise({ try: () => NFS.promises.readdir(path, options), catch: (err) => handleErrnoException("FileSystem", "readDirectory")(err as any, [path]) @@ -587,7 +584,7 @@ const watch = (backend: FileSystem.WatchBackend["Service"] | undefined, path: st // == writeFile -const writeFile = (path: string, data: Uint8Array, options?: FileSystem.WriteFileOptions) => +const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) => Effect.callback((resume, signal) => { try { NFS.writeFile(path, data, { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9756eb8d2f..e8a8243834 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7502,7 +7502,9 @@ snapshots: metro-runtime: 0.83.1 transitivePeerDependencies: - '@babel/core' + - bufferutil - supports-color + - utf-8-validate '@react-native/normalize-colors@0.81.0': {}