effect-start 0.15.0 → 0.17.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 (46) hide show
  1. package/package.json +2 -1
  2. package/src/ContentNegotiation.test.ts +103 -0
  3. package/src/ContentNegotiation.ts +10 -3
  4. package/src/Development.test.ts +119 -0
  5. package/src/Development.ts +137 -0
  6. package/src/Entity.test.ts +592 -0
  7. package/src/Entity.ts +359 -0
  8. package/src/FileRouter.ts +2 -2
  9. package/src/Http.test.ts +315 -20
  10. package/src/Http.ts +153 -11
  11. package/src/PathPattern.ts +3 -1
  12. package/src/Route.ts +26 -10
  13. package/src/RouteBody.test.ts +98 -66
  14. package/src/RouteBody.ts +125 -35
  15. package/src/RouteHook.ts +15 -14
  16. package/src/RouteHttp.test.ts +2549 -83
  17. package/src/RouteHttp.ts +337 -113
  18. package/src/RouteHttpTracer.ts +92 -0
  19. package/src/RouteMount.test.ts +23 -10
  20. package/src/RouteMount.ts +161 -4
  21. package/src/RouteSchema.test.ts +346 -0
  22. package/src/RouteSchema.ts +386 -7
  23. package/src/RouteSse.test.ts +249 -0
  24. package/src/RouteSse.ts +195 -0
  25. package/src/RouteTree.test.ts +233 -85
  26. package/src/RouteTree.ts +98 -44
  27. package/src/StreamExtra.ts +21 -1
  28. package/src/Values.test.ts +263 -0
  29. package/src/Values.ts +68 -6
  30. package/src/bun/BunBundle.ts +0 -73
  31. package/src/bun/BunHttpServer.ts +23 -7
  32. package/src/bun/BunRoute.test.ts +162 -0
  33. package/src/bun/BunRoute.ts +144 -105
  34. package/src/hyper/HyperHtml.test.ts +119 -0
  35. package/src/hyper/HyperHtml.ts +10 -2
  36. package/src/hyper/HyperNode.ts +2 -0
  37. package/src/hyper/HyperRoute.test.tsx +197 -0
  38. package/src/hyper/HyperRoute.ts +61 -0
  39. package/src/hyper/index.ts +4 -0
  40. package/src/hyper/jsx.d.ts +15 -0
  41. package/src/index.ts +2 -0
  42. package/src/node/FileSystem.ts +8 -0
  43. package/src/testing/TestLogger.test.ts +0 -3
  44. package/src/testing/TestLogger.ts +15 -9
  45. package/src/FileSystemExtra.test.ts +0 -242
  46. package/src/FileSystemExtra.ts +0 -66
package/src/Entity.ts ADDED
@@ -0,0 +1,359 @@
1
+ import * as Effect from "effect/Effect"
2
+ import * as ParseResult from "effect/ParseResult"
3
+ import * as Pipeable from "effect/Pipeable"
4
+ import * as Predicate from "effect/Predicate"
5
+ import * as Schema from "effect/Schema"
6
+ import * as Stream from "effect/Stream"
7
+ import * as StreamExtra from "./StreamExtra.ts"
8
+ import * as Values from "./Values.ts"
9
+
10
+ export const TypeId: unique symbol = Symbol.for("effect-start/Entity")
11
+ export type TypeId = typeof TypeId
12
+
13
+ const textDecoder = new TextDecoder()
14
+ const textEncoder = new TextEncoder()
15
+
16
+ function isBinary(v: unknown): v is Uint8Array | ArrayBuffer {
17
+ return v instanceof Uint8Array || v instanceof ArrayBuffer
18
+ }
19
+
20
+ /**
21
+ * Header keys are guaranteed to be lowercase.
22
+ */
23
+ export type Headers = {
24
+ [header: string]: string | null | undefined
25
+ }
26
+
27
+ export interface Entity<
28
+ T = unknown,
29
+ E = never,
30
+ > extends Pipeable.Pipeable {
31
+ readonly [TypeId]: TypeId
32
+ readonly body: T
33
+ readonly headers: Headers
34
+ /**
35
+ * Accepts any valid URI (Uniform Resource Identifier), including URLs
36
+ * (http://, https://, file://), URNs (urn:isbn:...), S3 URIs (s3://bucket/key),
37
+ * data URIs, and other schemes. While commonly called "URL" in many APIs,
38
+ * this property handles URIs as the correct superset term per RFC 3986.
39
+ */
40
+ readonly url: string | undefined
41
+ readonly status: number | undefined
42
+ readonly text: T extends string ? Effect.Effect<T, ParseResult.ParseError | E>
43
+ : Effect.Effect<string, ParseResult.ParseError | E>
44
+ readonly json: [T] extends [Effect.Effect<infer A, any, any>] ? Effect.Effect<
45
+ A extends string | Uint8Array | ArrayBuffer ? unknown : A,
46
+ ParseResult.ParseError | E
47
+ >
48
+ : [T] extends [Stream.Stream<any, any, any>]
49
+ ? Effect.Effect<unknown, ParseResult.ParseError | E>
50
+ : [T] extends [string | Uint8Array | ArrayBuffer]
51
+ ? Effect.Effect<unknown, ParseResult.ParseError | E>
52
+ : [T] extends [Values.Json] ? Effect.Effect<T, ParseResult.ParseError | E>
53
+ : Effect.Effect<unknown, ParseResult.ParseError | E>
54
+ readonly bytes: Effect.Effect<Uint8Array, ParseResult.ParseError | E>
55
+ readonly stream: T extends Stream.Stream<infer A, infer E1, any>
56
+ ? Stream.Stream<A, ParseResult.ParseError | E | E1>
57
+ : Stream.Stream<Uint8Array, ParseResult.ParseError | E>
58
+ }
59
+
60
+ export interface Proto extends Pipeable.Pipeable {
61
+ readonly [TypeId]: TypeId
62
+ }
63
+
64
+ function parseJson(s: string): Effect.Effect<unknown, ParseResult.ParseError> {
65
+ try {
66
+ return Effect.succeed(JSON.parse(s))
67
+ } catch (e) {
68
+ return Effect.fail(
69
+ new ParseResult.ParseError({
70
+ issue: new ParseResult.Type(
71
+ Schema.Unknown.ast,
72
+ s,
73
+ e instanceof Error ? e.message : "Failed to parse JSON",
74
+ ),
75
+ }),
76
+ )
77
+ }
78
+ }
79
+
80
+ function getText(
81
+ self: Entity<unknown, unknown>,
82
+ ): Effect.Effect<string, ParseResult.ParseError | unknown> {
83
+ const v = self.body
84
+ if (StreamExtra.isStream(v)) {
85
+ return Stream.mkString(
86
+ Stream.decodeText(v as Stream.Stream<Uint8Array, unknown, never>),
87
+ )
88
+ }
89
+ if (Effect.isEffect(v)) {
90
+ return Effect.flatMap(
91
+ v as Effect.Effect<unknown, unknown, never>,
92
+ (inner): Effect.Effect<string, ParseResult.ParseError | unknown> => {
93
+ if (isEntity(inner)) {
94
+ return inner.text
95
+ }
96
+ if (typeof inner === "string") {
97
+ return Effect.succeed(inner)
98
+ }
99
+ if (isBinary(inner)) {
100
+ return Effect.succeed(textDecoder.decode(inner))
101
+ }
102
+ return Effect.fail(mismatch(Schema.String, inner))
103
+ },
104
+ )
105
+ }
106
+ if (typeof v === "string") {
107
+ return Effect.succeed(v)
108
+ }
109
+ if (isBinary(v)) {
110
+ return Effect.succeed(textDecoder.decode(v))
111
+ }
112
+ return Effect.fail(mismatch(Schema.String, v))
113
+ }
114
+
115
+ function getJson(
116
+ self: Entity<unknown, unknown>,
117
+ ): Effect.Effect<unknown, ParseResult.ParseError | unknown> {
118
+ const v = self.body
119
+ if (StreamExtra.isStream(v)) {
120
+ return Effect.flatMap(getText(self), parseJson)
121
+ }
122
+ if (Effect.isEffect(v)) {
123
+ return Effect.flatMap(
124
+ v as Effect.Effect<unknown, unknown, never>,
125
+ (inner): Effect.Effect<unknown, ParseResult.ParseError | unknown> => {
126
+ if (isEntity(inner)) {
127
+ return inner.json
128
+ }
129
+ if (typeof inner === "object" && inner !== null && !isBinary(inner)) {
130
+ return Effect.succeed(inner)
131
+ }
132
+ if (typeof inner === "string") {
133
+ return parseJson(inner)
134
+ }
135
+ if (isBinary(inner)) {
136
+ return parseJson(textDecoder.decode(inner))
137
+ }
138
+ return Effect.fail(mismatch(Schema.Unknown, inner))
139
+ },
140
+ )
141
+ }
142
+ if (typeof v === "object" && v !== null && !isBinary(v)) {
143
+ return Effect.succeed(v)
144
+ }
145
+ if (typeof v === "string") {
146
+ return parseJson(v)
147
+ }
148
+ if (isBinary(v)) {
149
+ return parseJson(textDecoder.decode(v))
150
+ }
151
+ return Effect.fail(mismatch(Schema.Unknown, v))
152
+ }
153
+
154
+ function getBytes(
155
+ self: Entity<unknown, unknown>,
156
+ ): Effect.Effect<Uint8Array, ParseResult.ParseError | unknown> {
157
+ const v = self.body
158
+ if (StreamExtra.isStream(v)) {
159
+ return Stream.runFold(
160
+ v as Stream.Stream<Uint8Array, unknown, never>,
161
+ new Uint8Array(0),
162
+ Values.concatBytes,
163
+ )
164
+ }
165
+ if (Effect.isEffect(v)) {
166
+ return Effect.flatMap(
167
+ v as Effect.Effect<unknown, unknown, never>,
168
+ (inner): Effect.Effect<Uint8Array, ParseResult.ParseError | unknown> => {
169
+ if (isEntity(inner)) {
170
+ return inner.bytes
171
+ }
172
+ if (inner instanceof Uint8Array) {
173
+ return Effect.succeed(inner)
174
+ }
175
+ if (inner instanceof ArrayBuffer) {
176
+ return Effect.succeed(new Uint8Array(inner))
177
+ }
178
+ if (typeof inner === "string") {
179
+ return Effect.succeed(textEncoder.encode(inner))
180
+ }
181
+ return Effect.fail(mismatch(Schema.Uint8ArrayFromSelf, inner))
182
+ },
183
+ )
184
+ }
185
+ if (v instanceof Uint8Array) {
186
+ return Effect.succeed(v)
187
+ }
188
+ if (v instanceof ArrayBuffer) {
189
+ return Effect.succeed(new Uint8Array(v))
190
+ }
191
+ if (typeof v === "string") {
192
+ return Effect.succeed(textEncoder.encode(v))
193
+ }
194
+ // Allows entity.stream to work when body is a JSON object
195
+ if (typeof v === "object" && v !== null && !isBinary(v)) {
196
+ return Effect.succeed(textEncoder.encode(JSON.stringify(v)))
197
+ }
198
+ return Effect.fail(mismatch(Schema.Uint8ArrayFromSelf, v))
199
+ }
200
+
201
+ function getStream<A, E1, E2>(
202
+ self: Entity<Stream.Stream<A, E1, never>, E2>,
203
+ ): Stream.Stream<A, ParseResult.ParseError | E1 | E2>
204
+ function getStream<T, E>(
205
+ self: Entity<T, E>,
206
+ ): Stream.Stream<Uint8Array, ParseResult.ParseError | E>
207
+ function getStream(
208
+ self: Entity<unknown, unknown>,
209
+ ): Stream.Stream<unknown, unknown> {
210
+ const v = self.body
211
+ if (StreamExtra.isStream(v)) {
212
+ return v as Stream.Stream<unknown, unknown, never>
213
+ }
214
+ if (Effect.isEffect(v)) {
215
+ return Stream.unwrap(
216
+ Effect.map(
217
+ v as Effect.Effect<unknown, unknown, never>,
218
+ (inner) => {
219
+ if (isEntity(inner)) {
220
+ return inner.stream
221
+ }
222
+ return Stream.fromEffect(getBytes(make(inner)))
223
+ },
224
+ ),
225
+ )
226
+ }
227
+ return Stream.fromEffect(getBytes(self))
228
+ }
229
+
230
+ const Proto: Proto = Object.defineProperties(
231
+ Object.create(null),
232
+ {
233
+ [TypeId]: { value: TypeId },
234
+ pipe: {
235
+ value: function(this: Entity) {
236
+ return Pipeable.pipeArguments(this, arguments)
237
+ },
238
+ },
239
+ text: {
240
+ get(this: Entity<unknown, unknown>) {
241
+ return getText(this)
242
+ },
243
+ },
244
+ json: {
245
+ get(this: Entity<unknown, unknown>) {
246
+ return getJson(this)
247
+ },
248
+ },
249
+ bytes: {
250
+ get(this: Entity<unknown, unknown>) {
251
+ return getBytes(this)
252
+ },
253
+ },
254
+ stream: {
255
+ get(this: Entity<unknown, unknown>) {
256
+ return getStream(this)
257
+ },
258
+ },
259
+ },
260
+ )
261
+
262
+ export function isEntity(input: unknown): input is Entity {
263
+ return Predicate.hasProperty(input, TypeId)
264
+ }
265
+
266
+ interface Options {
267
+ readonly headers?: Headers
268
+ readonly url?: string
269
+ readonly status?: number
270
+ }
271
+
272
+ export function make<A, E>(
273
+ body: Effect.Effect<A, E, never>,
274
+ options?: Options,
275
+ ): Entity<Effect.Effect<A, E, never>, E>
276
+ export function make<A extends Uint8Array | string, E>(
277
+ body: Stream.Stream<A, E, never>,
278
+ options?: Options,
279
+ ): Entity<Stream.Stream<A, E, never>, E>
280
+ export function make<T>(body: T, options?: Options): Entity<T, never>
281
+ export function make(
282
+ body: unknown,
283
+ options?: Options,
284
+ ): Entity<unknown, unknown> {
285
+ return Object.assign(
286
+ Object.create(Proto),
287
+ {
288
+ body,
289
+ headers: options?.headers ?? {},
290
+ url: options?.url,
291
+ status: options?.status,
292
+ },
293
+ )
294
+ }
295
+
296
+ export function effect<A, E, R>(
297
+ body: Effect.Effect<Entity<A> | A, E, R>,
298
+ ): Entity<A, E> {
299
+ return make(body) as unknown as Entity<A, E>
300
+ }
301
+
302
+ export function resolve<A, E>(
303
+ entity: Entity<A, E>,
304
+ ): Effect.Effect<Entity<A, E>, E, never> {
305
+ const body = entity.body
306
+ if (Effect.isEffect(body)) {
307
+ return Effect.map(
308
+ body as Effect.Effect<Entity<A> | A, E, never>,
309
+ (inner) =>
310
+ isEntity(inner)
311
+ ? inner as Entity<A, E>
312
+ : make(inner as A, {
313
+ status: entity.status,
314
+ headers: entity.headers,
315
+ url: entity.url,
316
+ }) as Entity<A, E>,
317
+ )
318
+ }
319
+ return Effect.succeed(entity)
320
+ }
321
+
322
+ export function type(self: Entity): string {
323
+ const h = self.headers
324
+ if (h["content-type"]) {
325
+ return h["content-type"]
326
+ }
327
+ const v = self.body
328
+ if (typeof v === "string") {
329
+ return "text/plain"
330
+ }
331
+ if (typeof v === "object" && v !== null && !isBinary(v)) {
332
+ return "application/json"
333
+ }
334
+ return "application/octet-stream"
335
+ }
336
+
337
+ export function length(self: Entity): number | undefined {
338
+ const h = self.headers
339
+ if (h["content-length"]) {
340
+ return parseInt(h["content-length"], 10)
341
+ }
342
+ const v = self.body
343
+ if (typeof v === "string") {
344
+ return textEncoder.encode(v).byteLength
345
+ }
346
+ if (isBinary(v)) {
347
+ return v.byteLength
348
+ }
349
+ return undefined
350
+ }
351
+
352
+ function mismatch(
353
+ expected: Schema.Schema.Any,
354
+ actual: unknown,
355
+ ): ParseResult.ParseError {
356
+ return new ParseResult.ParseError({
357
+ issue: new ParseResult.Type(expected.ast, actual),
358
+ })
359
+ }
package/src/FileRouter.ts CHANGED
@@ -10,9 +10,9 @@ import * as Record from "effect/Record"
10
10
  import * as Stream from "effect/Stream"
11
11
  import * as NPath from "node:path"
12
12
  import * as NUrl from "node:url"
13
+ import * as Development from "./Development.ts"
13
14
  import * as FileRouterCodegen from "./FileRouterCodegen.ts"
14
15
  import * as FileRouterPattern from "./FileRouterPattern.ts"
15
- import * as FileSystemExtra from "./FileSystemExtra.ts"
16
16
 
17
17
  export type RouteModule = {
18
18
  default: RouteSet.RouteSet.Default
@@ -161,7 +161,7 @@ export function layer(options: {
161
161
  yield* FileRouterCodegen.update(routesPath, manifestFilename)
162
162
 
163
163
  const stream = Function.pipe(
164
- FileSystemExtra.watchSource({
164
+ Development.watchSource({
165
165
  path: routesPath,
166
166
  filter: (e) => !e.path.includes("node_modules"),
167
167
  }),