effect 3.2.8 → 3.3.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.
Files changed (155) hide show
  1. package/Redacted/package.json +6 -0
  2. package/dist/cjs/Config.js +9 -1
  3. package/dist/cjs/Config.js.map +1 -1
  4. package/dist/cjs/Either.js +2 -1
  5. package/dist/cjs/Either.js.map +1 -1
  6. package/dist/cjs/Iterable.js +15 -2
  7. package/dist/cjs/Iterable.js.map +1 -1
  8. package/dist/cjs/Layer.js +11 -1
  9. package/dist/cjs/Layer.js.map +1 -1
  10. package/dist/cjs/Option.js +7 -1
  11. package/dist/cjs/Option.js.map +1 -1
  12. package/dist/cjs/Pool.js +26 -0
  13. package/dist/cjs/Pool.js.map +1 -1
  14. package/dist/cjs/Predicate.js +51 -1
  15. package/dist/cjs/Predicate.js.map +1 -1
  16. package/dist/cjs/Redacted.js +65 -0
  17. package/dist/cjs/Redacted.js.map +1 -0
  18. package/dist/cjs/STM.js.map +1 -1
  19. package/dist/cjs/Secret.js +7 -0
  20. package/dist/cjs/Secret.js.map +1 -1
  21. package/dist/cjs/Stream.js +32 -1
  22. package/dist/cjs/Stream.js.map +1 -1
  23. package/dist/cjs/Tuple.js +15 -1
  24. package/dist/cjs/Tuple.js.map +1 -1
  25. package/dist/cjs/Utils.js.map +1 -1
  26. package/dist/cjs/index.js +4 -2
  27. package/dist/cjs/index.js.map +1 -1
  28. package/dist/cjs/internal/cause.js +14 -18
  29. package/dist/cjs/internal/cause.js.map +1 -1
  30. package/dist/cjs/internal/clock.js +1 -1
  31. package/dist/cjs/internal/clock.js.map +1 -1
  32. package/dist/cjs/internal/config.js +8 -1
  33. package/dist/cjs/internal/config.js.map +1 -1
  34. package/dist/cjs/internal/layer.js +14 -2
  35. package/dist/cjs/internal/layer.js.map +1 -1
  36. package/dist/cjs/internal/pool.js +206 -235
  37. package/dist/cjs/internal/pool.js.map +1 -1
  38. package/dist/cjs/internal/redacted.js +87 -0
  39. package/dist/cjs/internal/redacted.js.map +1 -0
  40. package/dist/cjs/internal/secret.js +40 -23
  41. package/dist/cjs/internal/secret.js.map +1 -1
  42. package/dist/cjs/internal/stm/stm.js +2 -1
  43. package/dist/cjs/internal/stm/stm.js.map +1 -1
  44. package/dist/cjs/internal/stream.js +15 -7
  45. package/dist/cjs/internal/stream.js.map +1 -1
  46. package/dist/cjs/internal/version.js +1 -1
  47. package/dist/dts/Config.d.ts +13 -6
  48. package/dist/dts/Config.d.ts.map +1 -1
  49. package/dist/dts/Context.d.ts +1 -1
  50. package/dist/dts/Context.d.ts.map +1 -1
  51. package/dist/dts/Either.d.ts.map +1 -1
  52. package/dist/dts/Iterable.d.ts +7 -0
  53. package/dist/dts/Iterable.d.ts.map +1 -1
  54. package/dist/dts/Layer.d.ts +20 -0
  55. package/dist/dts/Layer.d.ts.map +1 -1
  56. package/dist/dts/Option.d.ts.map +1 -1
  57. package/dist/dts/Pool.d.ts +31 -0
  58. package/dist/dts/Pool.d.ts.map +1 -1
  59. package/dist/dts/Predicate.d.ts +57 -0
  60. package/dist/dts/Predicate.d.ts.map +1 -1
  61. package/dist/dts/Redacted.d.ts +68 -0
  62. package/dist/dts/Redacted.d.ts.map +1 -0
  63. package/dist/dts/STM.d.ts +4 -1
  64. package/dist/dts/STM.d.ts.map +1 -1
  65. package/dist/dts/Secret.d.ts +14 -1
  66. package/dist/dts/Secret.d.ts.map +1 -1
  67. package/dist/dts/Stream.d.ts +61 -4
  68. package/dist/dts/Stream.d.ts.map +1 -1
  69. package/dist/dts/Tuple.d.ts +51 -0
  70. package/dist/dts/Tuple.d.ts.map +1 -1
  71. package/dist/dts/Types.d.ts +46 -0
  72. package/dist/dts/Types.d.ts.map +1 -1
  73. package/dist/dts/Utils.d.ts +6 -5
  74. package/dist/dts/Utils.d.ts.map +1 -1
  75. package/dist/dts/index.d.ts +5 -0
  76. package/dist/dts/index.d.ts.map +1 -1
  77. package/dist/dts/internal/layer.d.ts +15 -1
  78. package/dist/dts/internal/layer.d.ts.map +1 -1
  79. package/dist/dts/internal/redacted.d.ts +2 -0
  80. package/dist/dts/internal/redacted.d.ts.map +1 -0
  81. package/dist/dts/internal/stm/stm.d.ts.map +1 -1
  82. package/dist/dts/internal/stream.d.ts +1 -0
  83. package/dist/dts/internal/stream.d.ts.map +1 -1
  84. package/dist/esm/Config.js +8 -0
  85. package/dist/esm/Config.js.map +1 -1
  86. package/dist/esm/Either.js +2 -1
  87. package/dist/esm/Either.js.map +1 -1
  88. package/dist/esm/Iterable.js +12 -0
  89. package/dist/esm/Iterable.js.map +1 -1
  90. package/dist/esm/Layer.js +10 -0
  91. package/dist/esm/Layer.js.map +1 -1
  92. package/dist/esm/Option.js +7 -1
  93. package/dist/esm/Option.js.map +1 -1
  94. package/dist/esm/Pool.js +26 -0
  95. package/dist/esm/Pool.js.map +1 -1
  96. package/dist/esm/Predicate.js +50 -0
  97. package/dist/esm/Predicate.js.map +1 -1
  98. package/dist/esm/Redacted.js +33 -0
  99. package/dist/esm/Redacted.js.map +1 -0
  100. package/dist/esm/STM.js.map +1 -1
  101. package/dist/esm/Secret.js +7 -0
  102. package/dist/esm/Secret.js.map +1 -1
  103. package/dist/esm/Stream.js +31 -0
  104. package/dist/esm/Stream.js.map +1 -1
  105. package/dist/esm/Tuple.js +51 -0
  106. package/dist/esm/Tuple.js.map +1 -1
  107. package/dist/esm/Utils.js.map +1 -1
  108. package/dist/esm/index.js +5 -0
  109. package/dist/esm/index.js.map +1 -1
  110. package/dist/esm/internal/cause.js +14 -18
  111. package/dist/esm/internal/cause.js.map +1 -1
  112. package/dist/esm/internal/clock.js +1 -1
  113. package/dist/esm/internal/clock.js.map +1 -1
  114. package/dist/esm/internal/config.js +6 -0
  115. package/dist/esm/internal/config.js.map +1 -1
  116. package/dist/esm/internal/layer.js +12 -0
  117. package/dist/esm/internal/layer.js.map +1 -1
  118. package/dist/esm/internal/pool.js +205 -235
  119. package/dist/esm/internal/pool.js.map +1 -1
  120. package/dist/esm/internal/redacted.js +52 -0
  121. package/dist/esm/internal/redacted.js.map +1 -0
  122. package/dist/esm/internal/secret.js +39 -22
  123. package/dist/esm/internal/secret.js.map +1 -1
  124. package/dist/esm/internal/stm/stm.js +2 -1
  125. package/dist/esm/internal/stm/stm.js.map +1 -1
  126. package/dist/esm/internal/stream.js +13 -4
  127. package/dist/esm/internal/stream.js.map +1 -1
  128. package/dist/esm/internal/version.js +1 -1
  129. package/package.json +9 -1
  130. package/src/Config.ts +15 -7
  131. package/src/Context.ts +1 -1
  132. package/src/Either.ts +4 -1
  133. package/src/Iterable.ts +13 -0
  134. package/src/Layer.ts +22 -0
  135. package/src/Option.ts +7 -1
  136. package/src/Pool.ts +39 -6
  137. package/src/Predicate.ts +59 -0
  138. package/src/Redacted.ts +79 -0
  139. package/src/STM.ts +7 -2
  140. package/src/Secret.ts +14 -1
  141. package/src/Stream.ts +67 -7
  142. package/src/Tuple.ts +53 -0
  143. package/src/Types.ts +48 -0
  144. package/src/Utils.ts +9 -6
  145. package/src/index.ts +6 -0
  146. package/src/internal/cause.ts +14 -17
  147. package/src/internal/clock.ts +1 -6
  148. package/src/internal/config.ts +11 -0
  149. package/src/internal/layer.ts +63 -0
  150. package/src/internal/pool.ts +320 -447
  151. package/src/internal/redacted.ts +69 -0
  152. package/src/internal/secret.ts +39 -28
  153. package/src/internal/stm/stm.ts +4 -1
  154. package/src/internal/stream.ts +111 -50
  155. package/src/internal/version.ts +1 -1
package/src/Types.ts CHANGED
@@ -4,6 +4,54 @@
4
4
  * @since 2.0.0
5
5
  */
6
6
 
7
+ type _TupleOf<T, N extends number, R extends Array<unknown>> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>
8
+
9
+ /**
10
+ * Represents a tuple with a fixed number of elements of type `T`.
11
+ *
12
+ * This type constructs a tuple that has exactly `N` elements of type `T`.
13
+ *
14
+ * @typeParam N - The number of elements in the tuple.
15
+ * @typeParam T - The type of elements in the tuple.
16
+ *
17
+ * @example
18
+ * import { TupleOf } from "effect/Types"
19
+ *
20
+ * // A tuple with exactly 3 numbers
21
+ * const example1: TupleOf<3, number> = [1, 2, 3]; // valid
22
+ * // @ts-expect-error
23
+ * const example2: TupleOf<3, number> = [1, 2]; // invalid
24
+ * // @ts-expect-error
25
+ * const example3: TupleOf<3, number> = [1, 2, 3, 4]; // invalid
26
+ *
27
+ * @category tuples
28
+ * @since 3.3.0
29
+ */
30
+ export type TupleOf<N extends number, T> = N extends N ? number extends N ? Array<T> : _TupleOf<T, N, []> : never
31
+
32
+ /**
33
+ * Represents a tuple with at least `N` elements of type `T`.
34
+ *
35
+ * This type constructs a tuple that has a fixed number of elements `N` of type `T` at the start,
36
+ * followed by any number (including zero) of additional elements of the same type `T`.
37
+ *
38
+ * @typeParam N - The minimum number of elements in the tuple.
39
+ * @typeParam T - The type of elements in the tuple.
40
+ *
41
+ * @example
42
+ * import { TupleOfAtLeast } from "effect/Types"
43
+ *
44
+ * // A tuple with at least 3 numbers
45
+ * const example1: TupleOfAtLeast<3, number> = [1, 2, 3]; // valid
46
+ * const example2: TupleOfAtLeast<3, number> = [1, 2, 3, 4, 5]; // valid
47
+ * // @ts-expect-error
48
+ * const example3: TupleOfAtLeast<3, number> = [1, 2]; // invalid
49
+ *
50
+ * @category tuples
51
+ * @since 3.3.0
52
+ */
53
+ export type TupleOfAtLeast<N extends number, T> = [...TupleOf<N, T>, ...Array<T>]
54
+
7
55
  /**
8
56
  * Returns the tags in a type.
9
57
  * @example
package/src/Utils.ts CHANGED
@@ -172,17 +172,20 @@ export interface Variance<in out F extends TypeLambda, in R, out O, out E> {
172
172
  readonly _E: Types.Covariant<E>
173
173
  }
174
174
 
175
- /**
176
- * @category models
177
- * @since 2.0.0
178
- */
179
175
  /**
180
176
  * @category models
181
177
  * @since 2.0.0
182
178
  */
183
179
  export interface Gen<F extends TypeLambda, Z> {
184
- <K extends Variance<F, any, any, any> | YieldWrap<Kind<F, any, any, any, any>>, A>(
185
- body: (resume: Z) => Generator<K, A, never>
180
+ <Self, K extends Variance<F, any, any, any> | YieldWrap<Kind<F, any, any, any, any>>, A>(
181
+ ...args:
182
+ | [
183
+ self: Self,
184
+ body: (this: Self, resume: Z) => Generator<K, A, never>
185
+ ]
186
+ | [
187
+ body: (resume: Z) => Generator<K, A, never>
188
+ ]
186
189
  ): Kind<
187
190
  F,
188
191
  [K] extends [Variance<F, infer R, any, any>] ? R
package/src/index.ts CHANGED
@@ -614,6 +614,11 @@ export * as Record from "./Record.js"
614
614
  */
615
615
  export * as RedBlackTree from "./RedBlackTree.js"
616
616
 
617
+ /**
618
+ * @since 3.3.0
619
+ */
620
+ export * as Redacted from "./Redacted.js"
621
+
617
622
  /**
618
623
  * @since 2.0.0
619
624
  */
@@ -713,6 +718,7 @@ export * as ScopedRef from "./ScopedRef.js"
713
718
 
714
719
  /**
715
720
  * @since 2.0.0
721
+ * @deprecated
716
722
  */
717
723
  export * as Secret from "./Secret.js"
718
724
 
@@ -983,8 +983,10 @@ class PrettyError extends globalThis.Error implements Cause.PrettyError {
983
983
  const prevLimit = Error.stackTraceLimit
984
984
  Error.stackTraceLimit = 0
985
985
  super(prettyErrorMessage(originalError))
986
+ if (this.message === "") {
987
+ this.message = "An error has occurred"
988
+ }
986
989
  Error.stackTraceLimit = prevLimit
987
-
988
990
  this.name = originalError instanceof Error ? originalError.name : "Error"
989
991
  if (typeof originalError === "object" && originalError !== null) {
990
992
  if (spanSymbol in originalError) {
@@ -998,21 +1000,13 @@ class PrettyError extends globalThis.Error implements Cause.PrettyError {
998
1000
  })
999
1001
  }
1000
1002
  this.stack = prettyErrorStack(
1001
- this.message,
1003
+ `${this.name}: ${this.message}`,
1002
1004
  originalError instanceof Error && originalError.stack
1003
1005
  ? originalError.stack
1004
1006
  : "",
1005
1007
  this.span
1006
1008
  )
1007
1009
  }
1008
-
1009
- toJSON() {
1010
- const out: any = { message: this.message, stack: this.stack }
1011
- if (this.span) {
1012
- out.span = this.span
1013
- }
1014
- return out
1015
- }
1016
1010
  }
1017
1011
 
1018
1012
  /**
@@ -1020,10 +1014,9 @@ class PrettyError extends globalThis.Error implements Cause.PrettyError {
1020
1014
  *
1021
1015
  * Rules:
1022
1016
  *
1023
- * 1) If the input `u` is already a string, it's considered a message, and "Error" is added as a prefix.
1024
- * 2) If `u` has a user-defined `toString()` method, it uses that method and adds "Error" as a prefix.
1025
- * 3) If `u` is an object and its only (optional) properties are "name", "message", or "_tag", it constructs
1026
- * an error message based on those properties.
1017
+ * 1) If the input `u` is already a string, it's considered a message.
1018
+ * 2) If `u` is an Error instance with a message defined, it uses the message.
1019
+ * 3) If `u` has a user-defined `toString()` method, it uses that method.
1027
1020
  * 4) Otherwise, it uses `JSON.stringify` to produce a string representation and uses it as the error message,
1028
1021
  * with "Error" added as a prefix.
1029
1022
  *
@@ -1032,9 +1025,13 @@ class PrettyError extends globalThis.Error implements Cause.PrettyError {
1032
1025
  export const prettyErrorMessage = (u: unknown): string => {
1033
1026
  // 1)
1034
1027
  if (typeof u === "string") {
1035
- return `Error: ${u}`
1028
+ return u
1036
1029
  }
1037
1030
  // 2)
1031
+ if (typeof u === "object" && u !== null && u instanceof Error) {
1032
+ return u.message
1033
+ }
1034
+ // 3)
1038
1035
  try {
1039
1036
  if (
1040
1037
  hasProperty(u, "toString") &&
@@ -1047,8 +1044,8 @@ export const prettyErrorMessage = (u: unknown): string => {
1047
1044
  } catch {
1048
1045
  // something's off, rollback to json
1049
1046
  }
1050
- // 3)
1051
- return `Error: ${JSON.stringify(u)}`
1047
+ // 4)
1048
+ return JSON.stringify(u)
1052
1049
  }
1053
1050
 
1054
1051
  const locationRegex = /\((.*)\)/
@@ -40,15 +40,10 @@ export const globalClockScheduler: Clock.ClockScheduler = {
40
40
 
41
41
  const performanceNowNanos = (function() {
42
42
  const bigint1e6 = BigInt(1_000_000)
43
-
44
43
  if (typeof performance === "undefined") {
45
44
  return () => BigInt(Date.now()) * bigint1e6
46
45
  }
47
-
48
- const origin = "timeOrigin" in performance && typeof performance.timeOrigin === "number" ?
49
- BigInt(Math.round(performance.timeOrigin * 1_000_000)) :
50
- (BigInt(Date.now()) * bigint1e6) - BigInt(Math.round(performance.now() * 1_000_000))
51
-
46
+ const origin = (BigInt(Date.now()) * bigint1e6) - BigInt(Math.round(performance.now() * 1_000_000))
52
47
  return () => origin + BigInt(Math.round(performance.now() * 1_000_000))
53
48
  })()
54
49
  const processOrPerformanceNow = (function() {
@@ -10,12 +10,14 @@ import * as HashSet from "../HashSet.js"
10
10
  import type * as LogLevel from "../LogLevel.js"
11
11
  import * as Option from "../Option.js"
12
12
  import { hasProperty, type Predicate, type Refinement } from "../Predicate.js"
13
+ import type * as Redacted from "../Redacted.js"
13
14
  import type * as Secret from "../Secret.js"
14
15
  import * as configError from "./configError.js"
15
16
  import * as core from "./core.js"
16
17
  import * as defaultServices from "./defaultServices.js"
17
18
  import * as effectable from "./effectable.js"
18
19
  import * as OpCodes from "./opCodes/config.js"
20
+ import * as redacted_ from "./redacted.js"
19
21
  import * as InternalSecret from "./secret.js"
20
22
 
21
23
  const ConfigSymbolKey = "effect/Config"
@@ -421,6 +423,15 @@ export const secret = (name?: string): Config.Config<Secret.Secret> => {
421
423
  return name === undefined ? config : nested(config, name)
422
424
  }
423
425
 
426
+ /** @internal */
427
+ export const redacted = (name?: string): Config.Config<Redacted.Redacted> => {
428
+ const config = primitive(
429
+ "a redacted property",
430
+ (text) => Either.right(redacted_.make(text))
431
+ )
432
+ return name === undefined ? config : nested(config, name)
433
+ }
434
+
424
435
  /** @internal */
425
436
  export const hashSet = <A>(config: Config.Config<A>, name?: string): Config.Config<HashSet.HashSet<A>> => {
426
437
  const newConfig = map(chunk(config), HashSet.fromIterable)
@@ -8,6 +8,7 @@ import type { FiberRef } from "../FiberRef.js"
8
8
  import * as FiberRefsPatch from "../FiberRefsPatch.js"
9
9
  import type { LazyArg } from "../Function.js"
10
10
  import { dual, pipe } from "../Function.js"
11
+ import * as HashMap from "../HashMap.js"
11
12
  import type * as Layer from "../Layer.js"
12
13
  import { pipeArguments } from "../Pipeable.js"
13
14
  import { hasProperty } from "../Predicate.js"
@@ -1108,10 +1109,72 @@ export const unwrapScoped = <A, E1, R1, E, R>(
1108
1109
  return flatMap(scoped(tag, self), (context) => Context.get(context, tag))
1109
1110
  }
1110
1111
 
1112
+ // -----------------------------------------------------------------------------
1113
+ // logging
1114
+ // -----------------------------------------------------------------------------
1115
+
1116
+ export const annotateLogs = dual<
1117
+ {
1118
+ (key: string, value: unknown): <A, E, R>(self: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
1119
+ (
1120
+ values: Record<string, unknown>
1121
+ ): <A, E, R>(self: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
1122
+ },
1123
+ {
1124
+ <A, E, R>(self: Layer.Layer<A, E, R>, key: string, value: unknown): Layer.Layer<A, E, R>
1125
+ <A, E, R>(self: Layer.Layer<A, E, R>, values: Record<string, unknown>): Layer.Layer<A, E, R>
1126
+ }
1127
+ >(
1128
+ (args) => isLayer(args[0]),
1129
+ function<A, E, R>() {
1130
+ const args = arguments
1131
+ return fiberRefLocallyWith(
1132
+ args[0] as Layer.Layer<A, E, R>,
1133
+ core.currentLogAnnotations,
1134
+ typeof args[1] === "string"
1135
+ ? HashMap.set(args[1], args[2])
1136
+ : (annotations) =>
1137
+ Object.entries(args[1] as Record<string, unknown>).reduce(
1138
+ (acc, [key, value]) => HashMap.set(acc, key, value),
1139
+ annotations
1140
+ )
1141
+ )
1142
+ }
1143
+ )
1144
+
1111
1145
  // -----------------------------------------------------------------------------
1112
1146
  // tracing
1113
1147
  // -----------------------------------------------------------------------------
1114
1148
 
1149
+ export const annotateSpans = dual<
1150
+ {
1151
+ (key: string, value: unknown): <A, E, R>(self: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
1152
+ (
1153
+ values: Record<string, unknown>
1154
+ ): <A, E, R>(self: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
1155
+ },
1156
+ {
1157
+ <A, E, R>(self: Layer.Layer<A, E, R>, key: string, value: unknown): Layer.Layer<A, E, R>
1158
+ <A, E, R>(self: Layer.Layer<A, E, R>, values: Record<string, unknown>): Layer.Layer<A, E, R>
1159
+ }
1160
+ >(
1161
+ (args) => isLayer(args[0]),
1162
+ function<A, E, R>() {
1163
+ const args = arguments
1164
+ return fiberRefLocallyWith(
1165
+ args[0] as Layer.Layer<A, E, R>,
1166
+ core.currentTracerSpanAnnotations,
1167
+ typeof args[1] === "string"
1168
+ ? HashMap.set(args[1], args[2])
1169
+ : (annotations) =>
1170
+ Object.entries(args[1] as Record<string, unknown>).reduce(
1171
+ (acc, [key, value]) => HashMap.set(acc, key, value),
1172
+ annotations
1173
+ )
1174
+ )
1175
+ }
1176
+ )
1177
+
1115
1178
  /** @internal */
1116
1179
  export const withSpan: {
1117
1180
  (