@effect/platform-deno 4.0.7 → 4.1.0

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/src/DenoCrypto.ts CHANGED
@@ -1,66 +1,178 @@
1
1
  /**
2
- * Deno-backed implementation of Effect's Crypto service.
2
+ * Deno layers for the `effect/crypto` modules.
3
3
  *
4
- * This module uses Deno's global Web Crypto API.
4
+ * Every layer here runs the core WebCrypto layer of its family, so that Deno
5
+ * programs take every crypto layer from `DenoCrypto`. They use the
6
+ * `WebCrypto` reference, which defaults to Deno's `globalThis.crypto`. Layers
7
+ * that need `SecureRandom` get it from {@link layerSecureRandom}, so no layer
8
+ * here has requirements. {@link layer} provides every family at once; provide
9
+ * a family's own layer to pick services selectively.
5
10
  *
6
11
  * @stability unstable
7
12
  * @since 4.0.0
8
13
  */
9
- import * as Context from "effect/Context"
10
- import * as EffectCrypto from "effect/Crypto"
11
- import * as Effect from "effect/Effect"
14
+ import * as Aead from "effect/crypto/Aead"
15
+ import * as Cipher from "effect/crypto/Cipher"
16
+ import * as Digest from "effect/crypto/Digest"
17
+ import * as Kdf from "effect/crypto/Kdf"
18
+ import * as KeyAgreement from "effect/crypto/KeyAgreement"
19
+ import * as KeyWrap from "effect/crypto/KeyWrap"
20
+ import * as Mac from "effect/crypto/Mac"
21
+ import * as SecureRandom from "effect/crypto/SecureRandom"
22
+ import * as Signature from "effect/crypto/Signature"
12
23
  import * as Layer from "effect/Layer"
13
- import * as PlatformError from "effect/PlatformError"
14
24
 
15
25
  /**
16
- * Provides the Web Crypto API used by the Crypto service implementation.
26
+ * Layer that provides `SecureRandom` on the `WebCrypto` reference.
27
+ *
28
+ * **Details**
29
+ *
30
+ * This is `SecureRandom.layerWebCrypto`, re-exported for Deno programs.
17
31
  *
18
32
  * @stability unstable
19
- * @category services
33
+ * @category layers
20
34
  * @since 4.0.0
21
35
  */
22
- export const WebCrypto = Context.Reference<Crypto>("@effect/platform-deno/Crypto/WebCrypto", {
23
- defaultValue: () => globalThis.crypto
24
- })
36
+ export const layerSecureRandom: Layer.Layer<SecureRandom.SecureRandom> = SecureRandom.layerWebCrypto
25
37
 
26
38
  /**
27
- * A layer that provides Effect's Crypto service using Deno's Web Crypto API.
39
+ * Layer that provides `Digest` on the `WebCrypto` reference.
40
+ *
41
+ * **Details**
42
+ *
43
+ * This is `Digest.layerWebCrypto`, re-exported for Deno programs.
28
44
  *
29
45
  * @stability unstable
30
46
  * @category layers
31
47
  * @since 4.0.0
32
48
  */
33
- export const layer: Layer.Layer<EffectCrypto.Crypto> = Layer.effect(
34
- EffectCrypto.Crypto,
35
- Effect.gen(function*() {
36
- const crypto = yield* WebCrypto
37
- const randomBytes = (size: number): Uint8Array => {
38
- const bytes = new Uint8Array(size)
39
- for (let offset = 0; offset < bytes.length; offset += 65_536) {
40
- crypto.getRandomValues(bytes.subarray(offset, offset + 65_536))
41
- }
42
- return bytes
43
- }
49
+ export const layerDigest: Layer.Layer<Digest.Digest> = Digest.layerWebCrypto
44
50
 
45
- const digest: EffectCrypto.Crypto["digest"] = (algorithm, data) =>
46
- Effect.map(
47
- Effect.tryPromise({
48
- try: () => crypto.subtle.digest(algorithm, new Uint8Array(data)),
49
- catch: (cause) =>
50
- PlatformError.systemError({
51
- module: "Crypto",
52
- method: "digest",
53
- _tag: "Unknown",
54
- description: "Could not compute digest",
55
- cause
56
- })
57
- }),
58
- (buffer) => new Uint8Array(buffer)
59
- )
51
+ /**
52
+ * Layer that provides `Mac` on the `WebCrypto` reference.
53
+ *
54
+ * **Details**
55
+ *
56
+ * This is `Mac.layerWebCrypto` with {@link layerSecureRandom}.
57
+ *
58
+ * @stability unstable
59
+ * @category layers
60
+ * @since 4.0.0
61
+ */
62
+ export const layerMac: Layer.Layer<Mac.Mac> = Mac.layerWebCrypto.pipe(Layer.provide(layerSecureRandom))
60
63
 
61
- return EffectCrypto.make({
62
- randomBytes,
63
- digest
64
- })
65
- })
64
+ /**
65
+ * Layer that provides `Kdf` on the `WebCrypto` reference.
66
+ *
67
+ * **Details**
68
+ *
69
+ * This is `Kdf.layerWebCrypto`, re-exported for Deno programs.
70
+ *
71
+ * @stability unstable
72
+ * @category layers
73
+ * @since 4.0.0
74
+ */
75
+ export const layerKdf: Layer.Layer<Kdf.Kdf> = Kdf.layerWebCrypto
76
+
77
+ /**
78
+ * Layer that provides `Aead` on the `WebCrypto` reference.
79
+ *
80
+ * **Details**
81
+ *
82
+ * This is `Aead.layerWebCrypto` with {@link layerSecureRandom}.
83
+ *
84
+ * @stability unstable
85
+ * @category layers
86
+ * @since 4.0.0
87
+ */
88
+ export const layerAead: Layer.Layer<Aead.Aead> = Aead.layerWebCrypto.pipe(Layer.provide(layerSecureRandom))
89
+
90
+ /**
91
+ * Layer that provides `Cipher` on the `WebCrypto` reference.
92
+ *
93
+ * **Details**
94
+ *
95
+ * This is `Cipher.layerWebCrypto` with {@link layerSecureRandom}.
96
+ *
97
+ * @stability unstable
98
+ * @category layers
99
+ * @since 4.0.0
100
+ */
101
+ export const layerCipher: Layer.Layer<Cipher.Cipher> = Cipher.layerWebCrypto.pipe(Layer.provide(layerSecureRandom))
102
+
103
+ /**
104
+ * Layer that provides `Signature` on the `WebCrypto` reference.
105
+ *
106
+ * **Details**
107
+ *
108
+ * This is `Signature.layerWebCrypto` with {@link layerSecureRandom}.
109
+ *
110
+ * @stability unstable
111
+ * @category layers
112
+ * @since 4.0.0
113
+ */
114
+ export const layerSignature: Layer.Layer<Signature.Signature> = Signature.layerWebCrypto.pipe(
115
+ Layer.provide(layerSecureRandom)
116
+ )
117
+
118
+ /**
119
+ * Layer that provides `KeyAgreement` on the `WebCrypto` reference.
120
+ *
121
+ * **Details**
122
+ *
123
+ * This is `KeyAgreement.layerWebCrypto` with {@link layerSecureRandom}.
124
+ *
125
+ * @stability unstable
126
+ * @category layers
127
+ * @since 4.0.0
128
+ */
129
+ export const layerKeyAgreement: Layer.Layer<KeyAgreement.KeyAgreement> = KeyAgreement.layerWebCrypto.pipe(
130
+ Layer.provide(layerSecureRandom)
131
+ )
132
+
133
+ /**
134
+ * Layer that provides `KeyWrap` on the `WebCrypto` reference.
135
+ *
136
+ * **Details**
137
+ *
138
+ * This is `KeyWrap.layerWebCrypto` with {@link layerSecureRandom}.
139
+ *
140
+ * @stability unstable
141
+ * @category layers
142
+ * @since 4.0.0
143
+ */
144
+ export const layerKeyWrap: Layer.Layer<KeyWrap.KeyWrap> = KeyWrap.layerWebCrypto.pipe(Layer.provide(layerSecureRandom))
145
+
146
+ /**
147
+ * Layer that provides every `effect/crypto` service on the `WebCrypto`
148
+ * reference.
149
+ *
150
+ * **Details**
151
+ *
152
+ * This merges the family layers of this module, so it requires nothing.
153
+ *
154
+ * @stability unstable
155
+ * @category layers
156
+ * @since 4.0.0
157
+ */
158
+ export const layer: Layer.Layer<
159
+ | SecureRandom.SecureRandom
160
+ | Digest.Digest
161
+ | Mac.Mac
162
+ | Kdf.Kdf
163
+ | Aead.Aead
164
+ | Cipher.Cipher
165
+ | Signature.Signature
166
+ | KeyAgreement.KeyAgreement
167
+ | KeyWrap.KeyWrap
168
+ > = Layer.mergeAll(
169
+ layerSecureRandom,
170
+ layerDigest,
171
+ layerMac,
172
+ layerKdf,
173
+ layerAead,
174
+ layerCipher,
175
+ layerSignature,
176
+ layerKeyAgreement,
177
+ layerKeyWrap
66
178
  )
@@ -4,7 +4,7 @@
4
4
  * @stability unstable
5
5
  * @since 4.0.0
6
6
  */
7
- import { copy as denoCopy, expandGlob, walk } from "@std/fs"
7
+ import { copy as denoCopy, walk } from "@std/fs"
8
8
  import { relative } from "@std/path"
9
9
  import * as ByteSize from "effect/ByteSize"
10
10
  import * as Effect from "effect/Effect"
@@ -77,16 +77,6 @@ const chmod: FileSystem.FileSystem["chmod"] = (path, mode) => tryPromise("chmod"
77
77
  const chown: FileSystem.FileSystem["chown"] = (path, uid, gid) =>
78
78
  tryPromise("chown", path, () => Deno.chown(path, uid, gid))
79
79
 
80
- const glob: FileSystem.FileSystem["glob"] = (pattern, options) =>
81
- Effect.map(
82
- collectAsyncIterable("glob", pattern, () =>
83
- expandGlob(pattern, {
84
- root: options?.root ?? Deno.cwd(),
85
- exclude: options?.exclude ? [...options.exclude] : []
86
- })),
87
- (entries) => entries.map((entry) => entry.path)
88
- )
89
-
90
80
  const link: FileSystem.FileSystem["link"] = (existingPath, newPath) =>
91
81
  tryPromise("link", existingPath, () => Deno.link(existingPath, newPath))
92
82
 
@@ -151,22 +141,28 @@ const openOptions = (flag: FileSystem.OpenFlag = "r", mode?: number): Deno.OpenO
151
141
  }
152
142
  }
153
143
 
154
- const makeFileInfo = (info: Deno.FileInfo): FileSystem.File.Info => ({
155
- type: info.isFile ?
144
+ // `Deno.DirEntry` only reports the first three flags
145
+ const fileType = (info: Deno.DirEntry | Deno.FileInfo): FileSystem.File.Type => {
146
+ const flags: Partial<Deno.FileInfo> = info
147
+ return info.isFile ?
156
148
  "File" :
157
149
  info.isDirectory ?
158
150
  "Directory" :
159
151
  info.isSymlink ?
160
152
  "SymbolicLink" :
161
- info.isBlockDevice ?
153
+ flags.isBlockDevice ?
162
154
  "BlockDevice" :
163
- info.isCharDevice ?
155
+ flags.isCharDevice ?
164
156
  "CharacterDevice" :
165
- info.isFifo ?
157
+ flags.isFifo ?
166
158
  "FIFO" :
167
- info.isSocket ?
159
+ flags.isSocket ?
168
160
  "Socket" :
169
- "Unknown",
161
+ "Unknown"
162
+ }
163
+
164
+ const makeFileInfo = (info: Deno.FileInfo): FileSystem.File.Info => ({
165
+ type: fileType(info),
170
166
  mtime: Option.fromNullishOr(info.mtime),
171
167
  atime: Option.fromNullishOr(info.atime),
172
168
  birthtime: Option.fromNullishOr(info.birthtime),
@@ -412,6 +408,12 @@ const readDirectory: FileSystem.FileSystem["readDirectory"] = (path, options) =>
412
408
  )
413
409
  }
414
410
 
411
+ const readDirectoryEntries = (path: string) =>
412
+ Effect.map(
413
+ collectAsyncIterable("readDirectoryEntries", path, () => Deno.readDir(path)),
414
+ (entries) => entries.map((entry): FileSystem.DirectoryEntry => ({ name: entry.name, type: fileType(entry) }))
415
+ )
416
+
415
417
  const readFile: FileSystem.FileSystem["readFile"] = (path) =>
416
418
  tryPromise("readFile", path, (signal) => Deno.readFile(path, { signal }))
417
419
 
@@ -525,7 +527,6 @@ const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend),
525
527
  chown,
526
528
  copy,
527
529
  copyFile,
528
- glob,
529
530
  link,
530
531
  lstat,
531
532
  makeDirectory,
@@ -535,6 +536,7 @@ const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend),
535
536
  makeTempFileScoped,
536
537
  open,
537
538
  readDirectory,
539
+ readDirectoryEntries,
538
540
  readFile,
539
541
  readLink,
540
542
  realPath,
@@ -327,6 +327,8 @@ export const layerConfig = (
327
327
  layerHttpServices
328
328
  )
329
329
 
330
+ const formDecoder = new TextDecoder("utf-8", { ignoreBOM: true })
331
+
330
332
  class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpServerRequest {
331
333
  readonly [ServerRequest.TypeId]: typeof ServerRequest.TypeId
332
334
  readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId
@@ -441,15 +443,7 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
441
443
 
442
444
  private textEffect: Effect.Effect<string, Error.HttpServerError> | undefined
443
445
  get text(): Effect.Effect<string, Error.HttpServerError> {
444
- return this.textEffect ??= Effect.runSync(Effect.cached(
445
- Effect.tryPromise({
446
- try: () => this.source.text(),
447
- catch: (cause) =>
448
- new Error.HttpServerError({
449
- reason: new Error.RequestParseError({ request: this, cause })
450
- })
451
- })
452
- ))
446
+ return this.textEffect ??= Effect.map(this.arrayBuffer, (buffer) => new TextDecoder().decode(buffer))
453
447
  }
454
448
  get json(): Effect.Effect<Schema.Json, Error.HttpServerError> {
455
449
  return Effect.flatMap(this.text, (_) =>
@@ -462,14 +456,15 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
462
456
  }))
463
457
  }
464
458
  get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, Error.HttpServerError> {
465
- return Effect.flatMap(this.text, (_) =>
459
+ return Effect.flatMap(this.arrayBuffer, (bytes) =>
466
460
  Effect.try({
467
- try: () => UrlParams.fromInput(new URLSearchParams(_)),
461
+ // A leading "?" is form data; URLSearchParams would drop it as a query prefix.
462
+ try: () => UrlParams.fromInput(new URLSearchParams(formDecoder.decode(bytes).replace(/^\?/, "%3F"))),
468
463
  catch: (cause) =>
469
464
  new Error.HttpServerError({
470
465
  reason: new Error.RequestParseError({ request: this, cause })
471
466
  })
472
- }))
467
+ }));
473
468
  }
474
469
 
475
470
  private multipartEffect:
@@ -498,7 +493,6 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
498
493
  })
499
494
  })
500
495
  ))
501
- this.textEffect = Effect.map(this.arrayBufferEffect, (_) => new TextDecoder().decode(_))
502
496
  return this.arrayBufferEffect
503
497
  }
504
498
 
@@ -2,14 +2,13 @@
2
2
  * Aggregate Deno platform services layer.
3
3
  *
4
4
  * This module defines the `DenoServices` union and a single `layer` that
5
- * provides Deno-backed child process spawning, crypto, filesystem, path, stdio,
5
+ * provides Deno-backed child process spawning, filesystem, path, stdio,
6
6
  * and terminal services. Use the layer when a Deno program wants the standard
7
7
  * platform services from one place.
8
8
  *
9
9
  * @stability unstable
10
10
  * @since 4.0.0
11
11
  */
12
- import type { Crypto } from "effect/Crypto"
13
12
  import type { FileSystem } from "effect/FileSystem"
14
13
  import * as Layer from "effect/Layer"
15
14
  import type { Path } from "effect/Path"
@@ -17,7 +16,6 @@ import type { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"
17
16
  import type { Stdio } from "effect/Stdio"
18
17
  import type { Terminal } from "effect/Terminal"
19
18
  import * as DenoChildProcessSpawner from "./DenoChildProcessSpawner.ts"
20
- import * as DenoCrypto from "./DenoCrypto.ts"
21
19
  import * as DenoFileSystem from "./DenoFileSystem.ts"
22
20
  import * as DenoPath from "./DenoPath.ts"
23
21
  import * as DenoStdio from "./DenoStdio.ts"
@@ -25,13 +23,13 @@ import * as DenoTerminal from "./DenoTerminal.ts"
25
23
 
26
24
  /**
27
25
  * The union of core services provided by the Deno platform layer, including
28
- * child process spawning, crypto, filesystem, path, stdio, and terminal services.
26
+ * child process spawning, filesystem, path, stdio, and terminal services.
29
27
  *
30
28
  * @stability unstable
31
29
  * @category models
32
30
  * @since 4.0.0
33
31
  */
34
- export type DenoServices = ChildProcessSpawner | Crypto | FileSystem | Path | Terminal | Stdio
32
+ export type DenoServices = ChildProcessSpawner | FileSystem | Path | Terminal | Stdio
35
33
 
36
34
  /**
37
35
  * Provides the default Deno implementations for child process spawning,
@@ -44,7 +42,6 @@ export type DenoServices = ChildProcessSpawner | Crypto | FileSystem | Path | Te
44
42
  export const layer: Layer.Layer<DenoServices> = DenoChildProcessSpawner.layer.pipe(
45
43
  Layer.provideMerge(Layer.mergeAll(
46
44
  DenoFileSystem.layer,
47
- DenoCrypto.layer,
48
45
  DenoPath.layer,
49
46
  DenoStdio.layer,
50
47
  DenoTerminal.layer