Open History to see the last commit
1/**
2 * Node-backed provider for Effect's `Path` service.
3 *
4 * This module turns Node's `node:path` and `node:url` APIs into `Path` layers.
5 * `layer` uses the host platform path implementation, while `layerPosix` and
6 * `layerWin32` provide fixed POSIX and Windows variants. All three layers also
7 * include helpers for converting between file paths and file URLs.
8 *
9 * @since 4.0.0
10 */
11import * as Effect from "effect/Effect"
12import * as Layer from "effect/Layer"
13import { Path, TypeId } from "effect/Path"
14import { BadArgument } from "effect/PlatformError"
15import * as NodePath from "node:path"
16import * as NodeUrl from "node:url"
17
18const fromFileUrl = (url: URL): Effect.Effect<string, BadArgument> =>
19 Effect.try({
20 try: () => NodeUrl.fileURLToPath(url),
21 catch: (cause) =>
22 new BadArgument({
23 module: "Path",
24 method: "fromFileUrl",
25 cause
26 })
27 })
28
29const toFileUrl = (path: string): Effect.Effect<URL, BadArgument> =>
30 Effect.try({
31 try: () => NodeUrl.pathToFileURL(path),
32 catch: (cause) =>
33 new BadArgument({
34 module: "Path",
35 method: "toFileUrl",
36 cause
37 })
38 })
39
40/**
41 * Provides the `Path` service using Node's POSIX path implementation plus
42 * file URL conversion helpers.
43 *
44 * @category layers
45 * @since 4.0.0
46 */
47export const layerPosix: Layer.Layer<Path> = Layer.succeed(Path)({
48 [TypeId]: TypeId,
49 ...NodePath.posix,
50 fromFileUrl,
51 toFileUrl
52})
53
54/**
55 * Provides the `Path` service using Node's Windows path implementation plus
56 * file URL conversion helpers.
57 *
58 * @category layers
59 * @since 4.0.0
60 */
61export const layerWin32: Layer.Layer<Path> = Layer.succeed(Path)({
62 [TypeId]: TypeId,
63 ...NodePath.win32,
64 fromFileUrl,
65 toFileUrl
66})
67
68/**
69 * Provides the default `Path` service using the host platform's Node path
70 * implementation plus file URL conversion helpers.
71 *
72 * @category layers
73 * @since 4.0.0
74 */
75export const layer: Layer.Layer<Path> = Layer.succeed(Path)({
76 [TypeId]: TypeId,
77 ...NodePath,
78 fromFileUrl,
79 toFileUrl
80})