@effect/platform 0.13.12 → 0.13.14
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/Error.d.ts +1 -1
- package/Error.d.ts.map +1 -1
- package/Http/Body.d.ts +16 -0
- package/Http/Body.d.ts.map +1 -1
- package/Http/Body.js +7 -1
- package/Http/Body.js.map +1 -1
- package/Http/ClientRequest.d.ts +8 -0
- package/Http/ClientRequest.d.ts.map +1 -1
- package/Http/ClientRequest.js +7 -1
- package/Http/ClientRequest.js.map +1 -1
- package/Http/Etag.d.ts +2 -0
- package/Http/Etag.d.ts.map +1 -1
- package/Http/Etag.js.map +1 -1
- package/Http/ServerResponse.d.ts +5 -0
- package/Http/ServerResponse.d.ts.map +1 -1
- package/Http/ServerResponse.js +8 -2
- package/Http/ServerResponse.js.map +1 -1
- package/KeyValueStore.d.ts +160 -0
- package/KeyValueStore.d.ts.map +1 -0
- package/KeyValueStore.js +58 -0
- package/KeyValueStore.js.map +1 -0
- package/internal/http/body.js +6 -1
- package/internal/http/body.js.map +1 -1
- package/internal/http/clientRequest.js +4 -1
- package/internal/http/clientRequest.js.map +1 -1
- package/internal/http/serverResponse.js +12 -1
- package/internal/http/serverResponse.js.map +1 -1
- package/internal/keyValueStore.d.ts +2 -0
- package/internal/keyValueStore.d.ts.map +1 -0
- package/internal/keyValueStore.js +129 -0
- package/internal/keyValueStore.js.map +1 -0
- package/mjs/Http/Body.mjs +5 -0
- package/mjs/Http/Body.mjs.map +1 -1
- package/mjs/Http/ClientRequest.mjs +5 -0
- package/mjs/Http/ClientRequest.mjs.map +1 -1
- package/mjs/Http/Etag.mjs.map +1 -1
- package/mjs/Http/ServerResponse.mjs +5 -0
- package/mjs/Http/ServerResponse.mjs.map +1 -1
- package/mjs/KeyValueStore.mjs +42 -0
- package/mjs/KeyValueStore.mjs.map +1 -0
- package/mjs/internal/http/body.mjs +4 -0
- package/mjs/internal/http/body.mjs.map +1 -1
- package/mjs/internal/http/clientRequest.mjs +2 -0
- package/mjs/internal/http/clientRequest.mjs.map +1 -1
- package/mjs/internal/http/serverResponse.mjs +10 -0
- package/mjs/internal/http/serverResponse.mjs.map +1 -1
- package/mjs/internal/keyValueStore.mjs +113 -0
- package/mjs/internal/keyValueStore.mjs.map +1 -0
- package/package.json +1 -1
- package/src/Error.ts +1 -1
- package/src/Http/Body.ts +18 -0
- package/src/Http/ClientRequest.ts +9 -0
- package/src/Http/Etag.ts +2 -0
- package/src/Http/ServerResponse.ts +9 -0
- package/src/KeyValueStore.ts +207 -0
- package/src/internal/http/body.ts +6 -1
- package/src/internal/http/clientRequest.ts +6 -0
- package/src/internal/http/serverResponse.ts +21 -0
- package/src/internal/keyValueStore.ts +176 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import * as Context from "@effect/data/Context"
|
|
2
|
+
import { dual, pipe } from "@effect/data/Function"
|
|
3
|
+
import * as Option from "@effect/data/Option"
|
|
4
|
+
import * as Effect from "@effect/io/Effect"
|
|
5
|
+
import * as Layer from "@effect/io/Layer"
|
|
6
|
+
import * as FileSystem from "@effect/platform/FileSystem"
|
|
7
|
+
import type * as KeyValueStore from "@effect/platform/KeyValueStore"
|
|
8
|
+
import * as Path from "@effect/platform/Path"
|
|
9
|
+
import * as Schema from "@effect/schema/Schema"
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const TypeId: KeyValueStore.TypeId = Symbol.for(
|
|
13
|
+
"@effect/platform/KeyValueStore"
|
|
14
|
+
) as KeyValueStore.TypeId
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
export const keyValueStoreTag = Context.Tag<KeyValueStore.KeyValueStore>(TypeId)
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export const make: (
|
|
21
|
+
impl:
|
|
22
|
+
& Omit<KeyValueStore.KeyValueStore, KeyValueStore.TypeId | "has" | "modify" | "isEmpty" | "forSchema">
|
|
23
|
+
& Partial<KeyValueStore.KeyValueStore>
|
|
24
|
+
) => KeyValueStore.KeyValueStore = (impl) =>
|
|
25
|
+
keyValueStoreTag.of({
|
|
26
|
+
[TypeId]: TypeId,
|
|
27
|
+
has: (key) => Effect.map(impl.get(key), Option.isSome),
|
|
28
|
+
isEmpty: Effect.map(impl.size, (size) => size === 0),
|
|
29
|
+
modify: (key, f) =>
|
|
30
|
+
Effect.flatMap(
|
|
31
|
+
impl.get(key),
|
|
32
|
+
(o) => {
|
|
33
|
+
if (Option.isNone(o)) {
|
|
34
|
+
return Effect.succeedNone
|
|
35
|
+
}
|
|
36
|
+
const newValue = f(o.value)
|
|
37
|
+
return Effect.as(
|
|
38
|
+
impl.set(key, newValue),
|
|
39
|
+
Option.some(newValue)
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
),
|
|
43
|
+
forSchema(schema) {
|
|
44
|
+
return makeSchemaStore(this, schema)
|
|
45
|
+
},
|
|
46
|
+
...impl
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** @internal */
|
|
50
|
+
export const prefix = dual<
|
|
51
|
+
(prefix: string) => <S extends KeyValueStore.KeyValueStore.AnyStore>(self: S) => S,
|
|
52
|
+
<S extends KeyValueStore.KeyValueStore.AnyStore>(self: S, prefix: string) => S
|
|
53
|
+
>(
|
|
54
|
+
2,
|
|
55
|
+
((self: KeyValueStore.KeyValueStore, prefix: string): KeyValueStore.KeyValueStore => ({
|
|
56
|
+
...self,
|
|
57
|
+
get: (key) => self.get(`${prefix}${key}`),
|
|
58
|
+
set: (key, value) => self.set(`${prefix}${key}`, value),
|
|
59
|
+
remove: (key) => self.remove(`${prefix}${key}`),
|
|
60
|
+
has: (key) => self.has(`${prefix}${key}`),
|
|
61
|
+
modify: (key, f) => self.modify(`${prefix}${key}`, f)
|
|
62
|
+
})) as any
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
export const SchemaStoreTypeId: KeyValueStore.SchemaStoreTypeId = Symbol.for(
|
|
67
|
+
"@effect/platform/KeyValueStore/SchemaStore"
|
|
68
|
+
) as KeyValueStore.SchemaStoreTypeId
|
|
69
|
+
|
|
70
|
+
/** @internal */
|
|
71
|
+
const makeSchemaStore = <I, A>(
|
|
72
|
+
store: KeyValueStore.KeyValueStore,
|
|
73
|
+
schema: Schema.Schema<I, A>
|
|
74
|
+
): KeyValueStore.SchemaStore<A> => {
|
|
75
|
+
const jsonSchema = Schema.compose(Schema.ParseJson, schema)
|
|
76
|
+
const parse = Schema.parse(jsonSchema)
|
|
77
|
+
const encode = Schema.encode(jsonSchema)
|
|
78
|
+
|
|
79
|
+
const get = (key: string) =>
|
|
80
|
+
Effect.flatMap(
|
|
81
|
+
store.get(key),
|
|
82
|
+
Option.match({
|
|
83
|
+
onNone: () => Effect.succeedNone,
|
|
84
|
+
onSome: (value) => Effect.asSome(parse(value))
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const set = (key: string, value: A) => Effect.flatMap(encode(value), (json) => store.set(key, json))
|
|
89
|
+
|
|
90
|
+
const modify = (key: string, f: (value: A) => A) =>
|
|
91
|
+
Effect.flatMap(
|
|
92
|
+
get(key),
|
|
93
|
+
(o) => {
|
|
94
|
+
if (Option.isNone(o)) {
|
|
95
|
+
return Effect.succeedNone
|
|
96
|
+
}
|
|
97
|
+
const newValue = f(o.value)
|
|
98
|
+
return Effect.as(
|
|
99
|
+
set(key, newValue),
|
|
100
|
+
Option.some(newValue)
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
[SchemaStoreTypeId]: SchemaStoreTypeId,
|
|
107
|
+
get,
|
|
108
|
+
set,
|
|
109
|
+
modify,
|
|
110
|
+
remove: store.remove,
|
|
111
|
+
clear: store.clear,
|
|
112
|
+
size: store.size,
|
|
113
|
+
has: store.has,
|
|
114
|
+
isEmpty: store.isEmpty
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @internal */
|
|
119
|
+
export const layerMemory = Layer.sync(keyValueStoreTag, () => {
|
|
120
|
+
const store = new Map<string, string>()
|
|
121
|
+
|
|
122
|
+
return make({
|
|
123
|
+
get: (key: string) => Effect.sync(() => Option.fromNullable(store.get(key))),
|
|
124
|
+
set: (key: string, value: string) => Effect.sync(() => store.set(key, value)),
|
|
125
|
+
remove: (key: string) => Effect.sync(() => store.delete(key)),
|
|
126
|
+
clear: Effect.sync(() => store.clear()),
|
|
127
|
+
size: Effect.sync(() => store.size)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
/** @internal */
|
|
132
|
+
export const layerFileSystem = (directory: string) =>
|
|
133
|
+
Layer.effect(
|
|
134
|
+
keyValueStoreTag,
|
|
135
|
+
Effect.gen(function*(_) {
|
|
136
|
+
const fs = yield* _(FileSystem.FileSystem)
|
|
137
|
+
const path = yield* _(Path.Path)
|
|
138
|
+
const keyPath = (key: string) => path.join(directory, encodeURIComponent(key))
|
|
139
|
+
|
|
140
|
+
if (!(yield* _(fs.exists(directory)))) {
|
|
141
|
+
yield* _(fs.makeDirectory(directory, { recursive: true }))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return make({
|
|
145
|
+
get: (key: string) =>
|
|
146
|
+
pipe(
|
|
147
|
+
Effect.map(fs.readFileString(keyPath(key)), Option.some),
|
|
148
|
+
Effect.catchTag(
|
|
149
|
+
"SystemError",
|
|
150
|
+
(sysError) => sysError.reason === "NotFound" ? Effect.succeed(Option.none()) : Effect.fail(sysError)
|
|
151
|
+
)
|
|
152
|
+
),
|
|
153
|
+
set: (key: string, value: string) => fs.writeFileString(keyPath(key), value),
|
|
154
|
+
remove: (key: string) => fs.remove(keyPath(key)),
|
|
155
|
+
has: (key: string) => fs.exists(keyPath(key)),
|
|
156
|
+
clear: Effect.zipRight(
|
|
157
|
+
fs.remove(directory, { recursive: true }),
|
|
158
|
+
fs.makeDirectory(directory, { recursive: true })
|
|
159
|
+
),
|
|
160
|
+
size: Effect.map(
|
|
161
|
+
fs.readDirectory(directory),
|
|
162
|
+
(files) => files.length
|
|
163
|
+
)
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
/** @internal */
|
|
169
|
+
export const layerSchema = <I, A>(
|
|
170
|
+
schema: Schema.Schema<I, A>,
|
|
171
|
+
tagIdentifier?: unknown
|
|
172
|
+
) => {
|
|
173
|
+
const tag = Context.Tag<KeyValueStore.SchemaStore<A>>(tagIdentifier)
|
|
174
|
+
const layer = Layer.effect(tag, Effect.map(keyValueStoreTag, (store) => store.forSchema(schema)))
|
|
175
|
+
return { tag, layer } as const
|
|
176
|
+
}
|