@effect/platform-deno 4.0.0-beta.104
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/DenoChildProcessSpawner.d.ts +39 -0
- package/dist/DenoChildProcessSpawner.d.ts.map +1 -0
- package/dist/DenoChildProcessSpawner.js +311 -0
- package/dist/DenoChildProcessSpawner.js.map +1 -0
- package/dist/DenoClusterHttp.d.ts +60 -0
- package/dist/DenoClusterHttp.d.ts.map +1 -0
- package/dist/DenoClusterHttp.js +78 -0
- package/dist/DenoClusterHttp.js.map +1 -0
- package/dist/DenoClusterSocket.d.ts +65 -0
- package/dist/DenoClusterSocket.d.ts.map +1 -0
- package/dist/DenoClusterSocket.js +101 -0
- package/dist/DenoClusterSocket.js.map +1 -0
- package/dist/DenoCrypto.d.ts +25 -0
- package/dist/DenoCrypto.d.ts.map +1 -0
- package/dist/DenoCrypto.js +52 -0
- package/dist/DenoCrypto.js.map +1 -0
- package/dist/DenoFileSystem.d.ts +10 -0
- package/dist/DenoFileSystem.d.ts.map +1 -0
- package/dist/DenoFileSystem.js +331 -0
- package/dist/DenoFileSystem.js.map +1 -0
- package/dist/DenoHttpClient.d.ts +9 -0
- package/dist/DenoHttpClient.d.ts.map +1 -0
- package/dist/DenoHttpClient.js +9 -0
- package/dist/DenoHttpClient.js.map +1 -0
- package/dist/DenoHttpPlatform.d.ts +34 -0
- package/dist/DenoHttpPlatform.d.ts.map +1 -0
- package/dist/DenoHttpPlatform.js +88 -0
- package/dist/DenoHttpPlatform.js.map +1 -0
- package/dist/DenoHttpServer.d.ts +99 -0
- package/dist/DenoHttpServer.d.ts.map +1 -0
- package/dist/DenoHttpServer.js +353 -0
- package/dist/DenoHttpServer.js.map +1 -0
- package/dist/DenoHttpServerRequest.d.ts +18 -0
- package/dist/DenoHttpServerRequest.d.ts.map +1 -0
- package/dist/DenoHttpServerRequest.js +8 -0
- package/dist/DenoHttpServerRequest.js.map +1 -0
- package/dist/DenoKeyValueStore.d.ts +22 -0
- package/dist/DenoKeyValueStore.d.ts.map +1 -0
- package/dist/DenoKeyValueStore.js +21 -0
- package/dist/DenoKeyValueStore.js.map +1 -0
- package/dist/DenoMultipart.d.ts +32 -0
- package/dist/DenoMultipart.d.ts.map +1 -0
- package/dist/DenoMultipart.js +25 -0
- package/dist/DenoMultipart.js.map +1 -0
- package/dist/DenoPath.d.ts +43 -0
- package/dist/DenoPath.d.ts.map +1 -0
- package/dist/DenoPath.js +81 -0
- package/dist/DenoPath.js.map +1 -0
- package/dist/DenoRedis.d.ts +60 -0
- package/dist/DenoRedis.d.ts.map +1 -0
- package/dist/DenoRedis.js +76 -0
- package/dist/DenoRedis.js.map +1 -0
- package/dist/DenoRuntime.d.ts +36 -0
- package/dist/DenoRuntime.d.ts.map +1 -0
- package/dist/DenoRuntime.js +35 -0
- package/dist/DenoRuntime.js.map +1 -0
- package/dist/DenoServices.d.ts +34 -0
- package/dist/DenoServices.d.ts.map +1 -0
- package/dist/DenoServices.js +16 -0
- package/dist/DenoServices.js.map +1 -0
- package/dist/DenoSocket.d.ts +99 -0
- package/dist/DenoSocket.d.ts.map +1 -0
- package/dist/DenoSocket.js +192 -0
- package/dist/DenoSocket.js.map +1 -0
- package/dist/DenoSocketServer.d.ts +70 -0
- package/dist/DenoSocketServer.d.ts.map +1 -0
- package/dist/DenoSocketServer.js +68 -0
- package/dist/DenoSocketServer.js.map +1 -0
- package/dist/DenoStdio.d.ts +11 -0
- package/dist/DenoStdio.d.ts.map +1 -0
- package/dist/DenoStdio.js +39 -0
- package/dist/DenoStdio.js.map +1 -0
- package/dist/DenoTerminal.d.ts +21 -0
- package/dist/DenoTerminal.d.ts.map +1 -0
- package/dist/DenoTerminal.js +27 -0
- package/dist/DenoTerminal.js.map +1 -0
- package/dist/DenoWorker.d.ts +17 -0
- package/dist/DenoWorker.d.ts.map +1 -0
- package/dist/DenoWorker.js +54 -0
- package/dist/DenoWorker.js.map +1 -0
- package/dist/DenoWorkerRunner.d.ts +24 -0
- package/dist/DenoWorkerRunner.d.ts.map +1 -0
- package/dist/DenoWorkerRunner.js +150 -0
- package/dist/DenoWorkerRunner.js.map +1 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/denoSocketServer.d.ts +7 -0
- package/dist/internal/denoSocketServer.d.ts.map +1 -0
- package/dist/internal/denoSocketServer.js +55 -0
- package/dist/internal/denoSocketServer.js.map +1 -0
- package/dist/internal/error.d.ts +4 -0
- package/dist/internal/error.d.ts.map +1 -0
- package/dist/internal/error.js +46 -0
- package/dist/internal/error.js.map +1 -0
- package/package.json +72 -0
- package/src/DenoChildProcessSpawner.ts +439 -0
- package/src/DenoClusterHttp.ts +156 -0
- package/src/DenoClusterSocket.ts +197 -0
- package/src/DenoCrypto.ts +63 -0
- package/src/DenoFileSystem.ts +497 -0
- package/src/DenoHttpClient.ts +9 -0
- package/src/DenoHttpPlatform.ts +93 -0
- package/src/DenoHttpServer.ts +535 -0
- package/src/DenoHttpServerRequest.ts +18 -0
- package/src/DenoKeyValueStore.ts +28 -0
- package/src/DenoMultipart.ts +53 -0
- package/src/DenoPath.ts +102 -0
- package/src/DenoRedis.ts +113 -0
- package/src/DenoRuntime.ts +73 -0
- package/src/DenoServices.ts +49 -0
- package/src/DenoSocket.ts +318 -0
- package/src/DenoSocketServer.ts +120 -0
- package/src/DenoStdio.ts +51 -0
- package/src/DenoTerminal.ts +32 -0
- package/src/DenoWorker.ts +75 -0
- package/src/DenoWorkerRunner.ts +175 -0
- package/src/index.ts +110 -0
- package/src/internal/denoSocketServer.ts +89 -0
- package/src/internal/error.ts +63 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deno implementation of Effect's `FileSystem` service.
|
|
3
|
+
*
|
|
4
|
+
* @since 4.0.0
|
|
5
|
+
*/
|
|
6
|
+
import { copy as denoCopy, expandGlob, walk } from "@std/fs"
|
|
7
|
+
import { relative } from "@std/path"
|
|
8
|
+
import * as Effect from "effect/Effect"
|
|
9
|
+
import * as FileSystem from "effect/FileSystem"
|
|
10
|
+
import * as Layer from "effect/Layer"
|
|
11
|
+
import * as Option from "effect/Option"
|
|
12
|
+
import * as PlatformError from "effect/PlatformError"
|
|
13
|
+
import * as Stream from "effect/Stream"
|
|
14
|
+
import { handleError } from "./internal/error.ts"
|
|
15
|
+
|
|
16
|
+
const tryPromise = <A>(
|
|
17
|
+
method: string,
|
|
18
|
+
pathOrDescriptor: string | number | undefined,
|
|
19
|
+
evaluate: (signal: AbortSignal) => PromiseLike<A>
|
|
20
|
+
) =>
|
|
21
|
+
Effect.tryPromise({
|
|
22
|
+
try: evaluate,
|
|
23
|
+
catch: handleError("FileSystem", method, pathOrDescriptor)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const close = (file: Deno.FsFile, method: string, pathOrDescriptor: string | number) =>
|
|
27
|
+
Effect.orDie(Effect.try({
|
|
28
|
+
try: () => file.close(),
|
|
29
|
+
catch: handleError("FileSystem", method, pathOrDescriptor)
|
|
30
|
+
}))
|
|
31
|
+
|
|
32
|
+
const collectAsyncIterable = <A>(
|
|
33
|
+
method: string,
|
|
34
|
+
pathOrDescriptor: string | number | undefined,
|
|
35
|
+
evaluate: () => AsyncIterable<A>
|
|
36
|
+
) =>
|
|
37
|
+
Effect.tryPromise({
|
|
38
|
+
try: async () => {
|
|
39
|
+
const values = new Array<A>()
|
|
40
|
+
for await (const value of evaluate()) {
|
|
41
|
+
values.push(value)
|
|
42
|
+
}
|
|
43
|
+
return values
|
|
44
|
+
},
|
|
45
|
+
catch: handleError("FileSystem", method, pathOrDescriptor)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const access: FileSystem.FileSystem["access"] = (path, options) => {
|
|
49
|
+
if (!options?.readable && !options?.writable) {
|
|
50
|
+
return Effect.asVoid(tryPromise("access", path, () => Deno.stat(path)))
|
|
51
|
+
}
|
|
52
|
+
return Effect.acquireUseRelease(
|
|
53
|
+
tryPromise("access", path, () =>
|
|
54
|
+
Deno.open(path, {
|
|
55
|
+
...(options.readable ? { read: true } : {}),
|
|
56
|
+
...(options.writable ? { write: true } : {})
|
|
57
|
+
})),
|
|
58
|
+
() => Effect.void,
|
|
59
|
+
(file) => close(file, "access", path)
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const copy: FileSystem.FileSystem["copy"] = (fromPath, toPath, options) =>
|
|
64
|
+
tryPromise("copy", fromPath, () =>
|
|
65
|
+
denoCopy(fromPath, toPath, {
|
|
66
|
+
overwrite: options?.overwrite ?? false,
|
|
67
|
+
preserveTimestamps: options?.preserveTimestamps ?? false
|
|
68
|
+
}))
|
|
69
|
+
|
|
70
|
+
const copyFile: FileSystem.FileSystem["copyFile"] = (fromPath, toPath) =>
|
|
71
|
+
tryPromise("copyFile", fromPath, () => Deno.copyFile(fromPath, toPath))
|
|
72
|
+
|
|
73
|
+
const chmod: FileSystem.FileSystem["chmod"] = (path, mode) => tryPromise("chmod", path, () => Deno.chmod(path, mode))
|
|
74
|
+
|
|
75
|
+
const chown: FileSystem.FileSystem["chown"] = (path, uid, gid) =>
|
|
76
|
+
tryPromise("chown", path, () => Deno.chown(path, uid, gid))
|
|
77
|
+
|
|
78
|
+
const glob: FileSystem.FileSystem["glob"] = (pattern, options) =>
|
|
79
|
+
Effect.map(
|
|
80
|
+
collectAsyncIterable("glob", pattern, () =>
|
|
81
|
+
expandGlob(pattern, {
|
|
82
|
+
root: options?.root ?? Deno.cwd(),
|
|
83
|
+
exclude: options?.exclude ? [...options.exclude] : []
|
|
84
|
+
})),
|
|
85
|
+
(entries) => entries.map((entry) => entry.path)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const link: FileSystem.FileSystem["link"] = (existingPath, newPath) =>
|
|
89
|
+
tryPromise("link", existingPath, () => Deno.link(existingPath, newPath))
|
|
90
|
+
|
|
91
|
+
const makeDirectory: FileSystem.FileSystem["makeDirectory"] = (path, options) =>
|
|
92
|
+
tryPromise("makeDirectory", path, () =>
|
|
93
|
+
Deno.mkdir(path, {
|
|
94
|
+
recursive: options?.recursive ?? false,
|
|
95
|
+
...(options?.mode === undefined ? {} : { mode: options.mode })
|
|
96
|
+
}))
|
|
97
|
+
|
|
98
|
+
const makeTempDirectoryFactory = (method: string): FileSystem.FileSystem["makeTempDirectory"] => (options) =>
|
|
99
|
+
tryPromise(method, options?.directory, () =>
|
|
100
|
+
Deno.makeTempDir({
|
|
101
|
+
...(options?.directory === undefined ? {} : { dir: options.directory }),
|
|
102
|
+
...(options?.prefix === undefined ? {} : { prefix: options.prefix })
|
|
103
|
+
}))
|
|
104
|
+
|
|
105
|
+
const makeTempDirectory = makeTempDirectoryFactory("makeTempDirectory")
|
|
106
|
+
|
|
107
|
+
const removeFactory = (method: string): FileSystem.FileSystem["remove"] => (path, options) => {
|
|
108
|
+
const effect = tryPromise(method, path, () => Deno.remove(path, { recursive: options?.recursive ?? false }))
|
|
109
|
+
return options?.force
|
|
110
|
+
? Effect.catchTag(
|
|
111
|
+
effect,
|
|
112
|
+
"PlatformError",
|
|
113
|
+
(error) => error.reason._tag === "NotFound" ? Effect.void : Effect.fail(error)
|
|
114
|
+
)
|
|
115
|
+
: effect
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const remove = removeFactory("remove")
|
|
119
|
+
|
|
120
|
+
const makeTempDirectoryScoped: FileSystem.FileSystem["makeTempDirectoryScoped"] = (options) =>
|
|
121
|
+
Effect.acquireRelease(
|
|
122
|
+
makeTempDirectoryFactory("makeTempDirectoryScoped")(options),
|
|
123
|
+
(directory) => Effect.orDie(removeFactory("makeTempDirectoryScoped")(directory, { recursive: true }))
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
const openOptions = (flag: FileSystem.OpenFlag = "r", mode?: number): Deno.OpenOptions => {
|
|
127
|
+
const modeOption = mode === undefined ? {} : { mode }
|
|
128
|
+
switch (flag) {
|
|
129
|
+
case "r":
|
|
130
|
+
return { read: true, ...modeOption }
|
|
131
|
+
case "r+":
|
|
132
|
+
return { read: true, write: true, ...modeOption }
|
|
133
|
+
case "w":
|
|
134
|
+
return { write: true, create: true, truncate: true, ...modeOption }
|
|
135
|
+
case "wx":
|
|
136
|
+
return { write: true, createNew: true, ...modeOption }
|
|
137
|
+
case "w+":
|
|
138
|
+
return { read: true, write: true, create: true, truncate: true, ...modeOption }
|
|
139
|
+
case "wx+":
|
|
140
|
+
return { read: true, write: true, createNew: true, ...modeOption }
|
|
141
|
+
case "a":
|
|
142
|
+
return { append: true, create: true, ...modeOption }
|
|
143
|
+
case "ax":
|
|
144
|
+
return { append: true, createNew: true, ...modeOption }
|
|
145
|
+
case "a+":
|
|
146
|
+
return { read: true, append: true, create: true, ...modeOption }
|
|
147
|
+
case "ax+":
|
|
148
|
+
return { read: true, append: true, createNew: true, ...modeOption }
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const makeFileInfo = (info: Deno.FileInfo): FileSystem.File.Info => ({
|
|
153
|
+
type: info.isFile ?
|
|
154
|
+
"File" :
|
|
155
|
+
info.isDirectory ?
|
|
156
|
+
"Directory" :
|
|
157
|
+
info.isSymlink ?
|
|
158
|
+
"SymbolicLink" :
|
|
159
|
+
info.isBlockDevice ?
|
|
160
|
+
"BlockDevice" :
|
|
161
|
+
info.isCharDevice ?
|
|
162
|
+
"CharacterDevice" :
|
|
163
|
+
info.isFifo ?
|
|
164
|
+
"FIFO" :
|
|
165
|
+
info.isSocket ?
|
|
166
|
+
"Socket" :
|
|
167
|
+
"Unknown",
|
|
168
|
+
mtime: Option.fromNullishOr(info.mtime),
|
|
169
|
+
atime: Option.fromNullishOr(info.atime),
|
|
170
|
+
birthtime: Option.fromNullishOr(info.birthtime),
|
|
171
|
+
dev: info.dev,
|
|
172
|
+
rdev: Option.fromNullishOr(info.rdev),
|
|
173
|
+
ino: Option.fromNullishOr(info.ino),
|
|
174
|
+
mode: info.mode ?? 0,
|
|
175
|
+
nlink: Option.fromNullishOr(info.nlink),
|
|
176
|
+
uid: Option.fromNullishOr(info.uid),
|
|
177
|
+
gid: Option.fromNullishOr(info.gid),
|
|
178
|
+
size: FileSystem.Size(info.size),
|
|
179
|
+
blksize: Option.map(Option.fromNullishOr(info.blksize), FileSystem.Size),
|
|
180
|
+
blocks: Option.fromNullishOr(info.blocks)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
/** A file handle is stateful and must not be used concurrently. */
|
|
184
|
+
class FileImpl implements FileSystem.File {
|
|
185
|
+
readonly [FileSystem.FileTypeId]: typeof FileSystem.FileTypeId = FileSystem.FileTypeId
|
|
186
|
+
private readonly file: Deno.FsFile
|
|
187
|
+
private readonly append: boolean
|
|
188
|
+
private position = BigInt(0)
|
|
189
|
+
private nativePosition: bigint | undefined = undefined
|
|
190
|
+
|
|
191
|
+
constructor(
|
|
192
|
+
file: Deno.FsFile,
|
|
193
|
+
append: boolean
|
|
194
|
+
) {
|
|
195
|
+
this.file = file
|
|
196
|
+
this.append = append
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
get stat() {
|
|
200
|
+
return Effect.map(
|
|
201
|
+
tryPromise("stat", undefined, () => this.file.stat()),
|
|
202
|
+
makeFileInfo
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
get sync() {
|
|
207
|
+
return tryPromise("sync", undefined, () => this.file.sync())
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
seek(offset: FileSystem.SizeInput, from: FileSystem.SeekMode) {
|
|
211
|
+
const size = FileSystem.Size(offset)
|
|
212
|
+
return Effect.sync(() => {
|
|
213
|
+
if (from === "start") {
|
|
214
|
+
this.position = size
|
|
215
|
+
} else {
|
|
216
|
+
this.position += size
|
|
217
|
+
}
|
|
218
|
+
return FileSystem.Size(this.position)
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private readChunk(method: string, buffer: Uint8Array) {
|
|
223
|
+
return Effect.suspend(() => {
|
|
224
|
+
const position = this.position
|
|
225
|
+
return Effect.map(
|
|
226
|
+
tryPromise(
|
|
227
|
+
method,
|
|
228
|
+
undefined,
|
|
229
|
+
async () => {
|
|
230
|
+
if (this.nativePosition !== position) {
|
|
231
|
+
this.file.seekSync(position, Deno.SeekMode.Start)
|
|
232
|
+
}
|
|
233
|
+
this.nativePosition = undefined
|
|
234
|
+
return await this.file.read(buffer)
|
|
235
|
+
}
|
|
236
|
+
),
|
|
237
|
+
(bytesRead) => {
|
|
238
|
+
const sizeRead = FileSystem.Size(bytesRead ?? 0)
|
|
239
|
+
this.position = this.nativePosition = position + sizeRead
|
|
240
|
+
return sizeRead
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
read(buffer: Uint8Array) {
|
|
247
|
+
return this.readChunk("read", buffer)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
readAlloc(size: FileSystem.SizeInput) {
|
|
251
|
+
const sizeNumber = Number(size)
|
|
252
|
+
return Effect.suspend(() => {
|
|
253
|
+
const buffer = new Uint8Array(sizeNumber)
|
|
254
|
+
return Effect.map(this.readChunk("readAlloc", buffer), (bytesRead) => {
|
|
255
|
+
if (bytesRead === BigInt(0)) {
|
|
256
|
+
return Option.none()
|
|
257
|
+
}
|
|
258
|
+
return Option.some(bytesRead === BigInt(sizeNumber) ? buffer : buffer.subarray(0, Number(bytesRead)))
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
truncate(length?: FileSystem.SizeInput) {
|
|
264
|
+
const size = FileSystem.Size(length ?? 0)
|
|
265
|
+
return Effect.map(
|
|
266
|
+
tryPromise("truncate", undefined, () => this.file.truncate(Number(size))),
|
|
267
|
+
() => {
|
|
268
|
+
if (!this.append && this.position > size) {
|
|
269
|
+
this.position = size
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private writeChunk(method: string, buffer: Uint8Array) {
|
|
276
|
+
return Effect.suspend(() => {
|
|
277
|
+
const position = this.position
|
|
278
|
+
return Effect.map(
|
|
279
|
+
tryPromise(
|
|
280
|
+
method,
|
|
281
|
+
undefined,
|
|
282
|
+
async () => {
|
|
283
|
+
if (!this.append && this.nativePosition !== position) {
|
|
284
|
+
this.file.seekSync(position, Deno.SeekMode.Start)
|
|
285
|
+
}
|
|
286
|
+
this.nativePosition = undefined
|
|
287
|
+
return await this.file.write(buffer)
|
|
288
|
+
}
|
|
289
|
+
),
|
|
290
|
+
(bytesWritten) => {
|
|
291
|
+
const sizeWritten = FileSystem.Size(bytesWritten)
|
|
292
|
+
if (this.append) {
|
|
293
|
+
this.nativePosition = undefined
|
|
294
|
+
} else {
|
|
295
|
+
this.position = this.nativePosition = position + sizeWritten
|
|
296
|
+
}
|
|
297
|
+
return sizeWritten
|
|
298
|
+
}
|
|
299
|
+
)
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
write(buffer: Uint8Array) {
|
|
304
|
+
return this.writeChunk("write", buffer)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private writeAllChunk(buffer: Uint8Array): Effect.Effect<void, PlatformError.PlatformError> {
|
|
308
|
+
return Effect.flatMap(this.writeChunk("writeAll", buffer), (bytesWritten) => {
|
|
309
|
+
if (bytesWritten === BigInt(0)) {
|
|
310
|
+
return Effect.fail(PlatformError.systemError({
|
|
311
|
+
module: "FileSystem",
|
|
312
|
+
method: "writeAll",
|
|
313
|
+
_tag: "WriteZero",
|
|
314
|
+
description: "write returned 0 bytes written"
|
|
315
|
+
}))
|
|
316
|
+
}
|
|
317
|
+
return bytesWritten < buffer.length
|
|
318
|
+
? this.writeAllChunk(buffer.subarray(Number(bytesWritten)))
|
|
319
|
+
: Effect.void
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
writeAll(buffer: Uint8Array) {
|
|
324
|
+
return buffer.length === 0 ? Effect.void : this.writeAllChunk(buffer)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const open: FileSystem.FileSystem["open"] = (path, options) => {
|
|
329
|
+
const append = options?.flag?.startsWith("a") ?? false
|
|
330
|
+
return Effect.map(
|
|
331
|
+
Effect.acquireRelease(
|
|
332
|
+
tryPromise("open", path, () => Deno.open(path, openOptions(options?.flag, options?.mode))),
|
|
333
|
+
(file) => close(file, "open", path)
|
|
334
|
+
),
|
|
335
|
+
(file) => new FileImpl(file, append)
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const makeTempFileFactory = (method: string): FileSystem.FileSystem["makeTempFile"] => (options) =>
|
|
340
|
+
tryPromise(method, options?.directory, () =>
|
|
341
|
+
Deno.makeTempFile({
|
|
342
|
+
...(options?.directory === undefined ? {} : { dir: options.directory }),
|
|
343
|
+
...(options?.prefix === undefined ? {} : { prefix: options.prefix }),
|
|
344
|
+
...(options?.suffix === undefined ? {} : { suffix: options.suffix })
|
|
345
|
+
}))
|
|
346
|
+
|
|
347
|
+
const makeTempFile = makeTempFileFactory("makeTempFile")
|
|
348
|
+
|
|
349
|
+
const makeTempFileScoped: FileSystem.FileSystem["makeTempFileScoped"] = (options) =>
|
|
350
|
+
Effect.acquireRelease(
|
|
351
|
+
makeTempFileFactory("makeTempFileScoped")(options),
|
|
352
|
+
(file) => Effect.orDie(removeFactory("makeTempFileScoped")(file, { force: true }))
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
const readDirectory: FileSystem.FileSystem["readDirectory"] = (path, options) => {
|
|
356
|
+
if (options?.recursive) {
|
|
357
|
+
return Effect.map(
|
|
358
|
+
collectAsyncIterable("readDirectory", path, () => walk(path)),
|
|
359
|
+
(entries) => entries.map((entry) => relative(path, entry.path)).filter(Boolean)
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
return Effect.map(
|
|
363
|
+
collectAsyncIterable("readDirectory", path, () => Deno.readDir(path)),
|
|
364
|
+
(entries) => entries.map((entry) => entry.name)
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const readFile: FileSystem.FileSystem["readFile"] = (path) =>
|
|
369
|
+
tryPromise("readFile", path, (signal) => Deno.readFile(path, { signal }))
|
|
370
|
+
|
|
371
|
+
const readLink: FileSystem.FileSystem["readLink"] = (path) => tryPromise("readLink", path, () => Deno.readLink(path))
|
|
372
|
+
|
|
373
|
+
const realPath: FileSystem.FileSystem["realPath"] = (path) => tryPromise("realPath", path, () => Deno.realPath(path))
|
|
374
|
+
|
|
375
|
+
const rename: FileSystem.FileSystem["rename"] = (oldPath, newPath) =>
|
|
376
|
+
tryPromise("rename", oldPath, () => Deno.rename(oldPath, newPath))
|
|
377
|
+
|
|
378
|
+
const stat: FileSystem.FileSystem["stat"] = (path) =>
|
|
379
|
+
Effect.map(
|
|
380
|
+
tryPromise("stat", path, () => Deno.stat(path)),
|
|
381
|
+
makeFileInfo
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
const symlink: FileSystem.FileSystem["symlink"] = (target, path) =>
|
|
385
|
+
tryPromise("symlink", target, () => Deno.symlink(target, path))
|
|
386
|
+
|
|
387
|
+
const truncate: FileSystem.FileSystem["truncate"] = (path, length) =>
|
|
388
|
+
tryPromise("truncate", path, () => Deno.truncate(path, length === undefined ? undefined : Number(length)))
|
|
389
|
+
|
|
390
|
+
const utimes: FileSystem.FileSystem["utimes"] = (path, atime, mtime) =>
|
|
391
|
+
tryPromise("utimes", path, () => Deno.utime(path, atime, mtime))
|
|
392
|
+
|
|
393
|
+
const watchNative = (
|
|
394
|
+
path: string,
|
|
395
|
+
options?: FileSystem.WatchOptions
|
|
396
|
+
): Stream.Stream<FileSystem.WatchEvent, PlatformError.PlatformError> =>
|
|
397
|
+
Stream.unwrap(
|
|
398
|
+
Effect.map(
|
|
399
|
+
Effect.try({
|
|
400
|
+
try: () => Deno.watchFs(path, { recursive: options?.recursive ?? false }),
|
|
401
|
+
catch: handleError("FileSystem", "watch", path)
|
|
402
|
+
}),
|
|
403
|
+
(watcher) =>
|
|
404
|
+
Stream.fromAsyncIterable(watcher, handleError("FileSystem", "watch", path)).pipe(
|
|
405
|
+
Stream.flatMap((event): Stream.Stream<FileSystem.WatchEvent, PlatformError.PlatformError> => {
|
|
406
|
+
switch (event.kind) {
|
|
407
|
+
case "create":
|
|
408
|
+
return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Create" as const, path }))
|
|
409
|
+
case "modify":
|
|
410
|
+
return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Update" as const, path }))
|
|
411
|
+
case "remove":
|
|
412
|
+
return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Remove" as const, path }))
|
|
413
|
+
case "rename":
|
|
414
|
+
return Stream.mapEffect(Stream.fromIterable(event.paths), (path) =>
|
|
415
|
+
Effect.match(stat(path), {
|
|
416
|
+
onFailure: () => ({ _tag: "Remove" as const, path }),
|
|
417
|
+
onSuccess: () => ({ _tag: "Create" as const, path })
|
|
418
|
+
}))
|
|
419
|
+
default:
|
|
420
|
+
return Stream.empty
|
|
421
|
+
}
|
|
422
|
+
})
|
|
423
|
+
)
|
|
424
|
+
)
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
const watch = (
|
|
428
|
+
backend: Option.Option<FileSystem.WatchBackend["Service"]>,
|
|
429
|
+
path: string,
|
|
430
|
+
options?: FileSystem.WatchOptions
|
|
431
|
+
) =>
|
|
432
|
+
stat(path).pipe(
|
|
433
|
+
Effect.map((info) =>
|
|
434
|
+
backend.pipe(
|
|
435
|
+
Option.flatMap((backend) => backend.register(path, info, options)),
|
|
436
|
+
Option.getOrElse(() => watchNative(path, options))
|
|
437
|
+
)
|
|
438
|
+
),
|
|
439
|
+
Stream.unwrap
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) => {
|
|
443
|
+
const flag = options?.flag ?? "w"
|
|
444
|
+
if (flag === "w" || flag === "wx" || flag === "a" || flag === "ax") {
|
|
445
|
+
return tryPromise("writeFile", path, (signal) =>
|
|
446
|
+
Deno.writeFile(path, data, {
|
|
447
|
+
append: flag.startsWith("a"),
|
|
448
|
+
createNew: flag.includes("x"),
|
|
449
|
+
...(options?.mode === undefined ? {} : { mode: options.mode }),
|
|
450
|
+
signal
|
|
451
|
+
}))
|
|
452
|
+
}
|
|
453
|
+
return Effect.acquireUseRelease(
|
|
454
|
+
tryPromise("writeFile", path, () => Deno.open(path, openOptions(flag, options?.mode))),
|
|
455
|
+
(file) => new FileImpl(file, flag.startsWith("a")).writeAll(data),
|
|
456
|
+
(file) => close(file, "writeFile", path)
|
|
457
|
+
)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend), (backend) =>
|
|
461
|
+
FileSystem.make({
|
|
462
|
+
access,
|
|
463
|
+
chmod,
|
|
464
|
+
chown,
|
|
465
|
+
copy,
|
|
466
|
+
copyFile,
|
|
467
|
+
glob,
|
|
468
|
+
link,
|
|
469
|
+
makeDirectory,
|
|
470
|
+
makeTempDirectory,
|
|
471
|
+
makeTempDirectoryScoped,
|
|
472
|
+
makeTempFile,
|
|
473
|
+
makeTempFileScoped,
|
|
474
|
+
open,
|
|
475
|
+
readDirectory,
|
|
476
|
+
readFile,
|
|
477
|
+
readLink,
|
|
478
|
+
realPath,
|
|
479
|
+
remove,
|
|
480
|
+
rename,
|
|
481
|
+
stat,
|
|
482
|
+
symlink,
|
|
483
|
+
truncate,
|
|
484
|
+
utimes,
|
|
485
|
+
watch(path, options) {
|
|
486
|
+
return watch(backend, path, options)
|
|
487
|
+
},
|
|
488
|
+
writeFile
|
|
489
|
+
}))
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Provides the `FileSystem` service backed by Deno filesystem APIs.
|
|
493
|
+
*
|
|
494
|
+
* @category layers
|
|
495
|
+
* @since 4.0.0
|
|
496
|
+
*/
|
|
497
|
+
export const layer: Layer.Layer<FileSystem.FileSystem> = Layer.effect(FileSystem.FileSystem)(makeFileSystem)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deno implementation of the Effect HTTP platform service.
|
|
3
|
+
*
|
|
4
|
+
* File responses use `Deno.FsFile.readable` directly so full-file responses
|
|
5
|
+
* retain Deno's resource-backed fast path. Deno closes the file after the body
|
|
6
|
+
* is sent, but server adapters must cancel a raw body when they do not send it,
|
|
7
|
+
* including when handling `HEAD` requests.
|
|
8
|
+
*
|
|
9
|
+
* The provided layer uses strong ETags, unlike the portable `HttpPlatform`
|
|
10
|
+
* layer, which uses weak ETags.
|
|
11
|
+
*
|
|
12
|
+
* @since 4.0.0
|
|
13
|
+
*/
|
|
14
|
+
import * as NodeHttpCompression from "@effect/platform-node-shared/NodeHttpCompression"
|
|
15
|
+
import { contentType } from "@std/media-types"
|
|
16
|
+
import { extname } from "@std/path"
|
|
17
|
+
import { ByteSliceStream } from "@std/streams"
|
|
18
|
+
import * as Layer from "effect/Layer"
|
|
19
|
+
import * as Etag from "effect/unstable/http/Etag"
|
|
20
|
+
import * as Platform from "effect/unstable/http/HttpPlatform"
|
|
21
|
+
import * as Response from "effect/unstable/http/HttpServerResponse"
|
|
22
|
+
import * as DenoFileSystem from "./DenoFileSystem.ts"
|
|
23
|
+
|
|
24
|
+
// gzip and deflate use the native CompressionStream, which does not expose a
|
|
25
|
+
// per-chunk flush control. br and zstd go through node:zlib compatibility
|
|
26
|
+
// streams and flush each input chunk.
|
|
27
|
+
const compression = NodeHttpCompression.make(Platform.makeCompressionWeb({
|
|
28
|
+
algorithms: NodeHttpCompression.algorithms,
|
|
29
|
+
transform: (algorithm, options) =>
|
|
30
|
+
algorithm === "gzip" || algorithm === "deflate"
|
|
31
|
+
? Platform.compressionTransformWeb(algorithm)
|
|
32
|
+
: NodeHttpCompression.compressTransformWeb(algorithm, options)
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates the Deno `HttpPlatform`, serving file responses from resource-backed
|
|
37
|
+
* readable streams and adding content type and content length headers.
|
|
38
|
+
*
|
|
39
|
+
* @category constructors
|
|
40
|
+
* @since 4.0.0
|
|
41
|
+
*/
|
|
42
|
+
export const make = Platform.make({
|
|
43
|
+
platform: "deno",
|
|
44
|
+
compression,
|
|
45
|
+
fileResponse(path, status, statusText, headers, start, end, contentLength) {
|
|
46
|
+
let body: ReadableStream<Uint8Array>
|
|
47
|
+
if (contentLength === 0) {
|
|
48
|
+
body = new ReadableStream<Uint8Array>({
|
|
49
|
+
start(controller) {
|
|
50
|
+
controller.close()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
} else {
|
|
54
|
+
const file = Deno.openSync(path)
|
|
55
|
+
file.seekSync(start, Deno.SeekMode.Start)
|
|
56
|
+
body = end === undefined
|
|
57
|
+
? file.readable
|
|
58
|
+
: file.readable.pipeThrough(new ByteSliceStream(0, contentLength - 1))
|
|
59
|
+
}
|
|
60
|
+
return Response.raw(body, {
|
|
61
|
+
headers: {
|
|
62
|
+
...headers,
|
|
63
|
+
"content-type": headers["content-type"] ?? contentType(extname(path)) ?? "application/octet-stream",
|
|
64
|
+
"content-length": contentLength.toString()
|
|
65
|
+
},
|
|
66
|
+
status,
|
|
67
|
+
statusText
|
|
68
|
+
})
|
|
69
|
+
},
|
|
70
|
+
fileWebResponse(file, status, statusText, headers, _options) {
|
|
71
|
+
return Response.raw(file, {
|
|
72
|
+
headers: {
|
|
73
|
+
...headers,
|
|
74
|
+
"content-type": file.type,
|
|
75
|
+
"content-length": file.size.toString()
|
|
76
|
+
},
|
|
77
|
+
status,
|
|
78
|
+
statusText
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Provides the Deno `HttpPlatform` together with its filesystem and strong ETag
|
|
85
|
+
* services.
|
|
86
|
+
*
|
|
87
|
+
* @category layers
|
|
88
|
+
* @since 4.0.0
|
|
89
|
+
*/
|
|
90
|
+
export const layer: Layer.Layer<Platform.HttpPlatform> = Layer.effect(Platform.HttpPlatform)(make).pipe(
|
|
91
|
+
Layer.provide(DenoFileSystem.layer),
|
|
92
|
+
Layer.provide(Etag.layer)
|
|
93
|
+
)
|