effect-start 0.17.0 → 0.17.2
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/dist/Commander.d.ts +103 -0
- package/dist/Commander.js +333 -0
- package/dist/ContentNegotiation.d.ts +13 -0
- package/dist/ContentNegotiation.js +364 -0
- package/dist/Development.d.ts +34 -0
- package/dist/Development.js +52 -0
- package/dist/Entity.d.ts +47 -0
- package/dist/Entity.js +224 -0
- package/dist/FileRouter.d.ts +61 -0
- package/dist/FileRouter.js +203 -0
- package/dist/FileRouterCodegen.d.ts +19 -0
- package/dist/FileRouterCodegen.js +176 -0
- package/dist/FileRouterPattern.d.ts +9 -0
- package/dist/FileRouterPattern.js +35 -0
- package/dist/Http.d.ts +37 -0
- package/dist/Http.js +92 -0
- package/dist/HttpAppExtra.d.ts +7 -0
- package/dist/HttpAppExtra.js +320 -0
- package/dist/HttpUtils.d.ts +3 -0
- package/dist/HttpUtils.js +11 -0
- package/dist/PathPattern.d.ts +134 -0
- package/dist/PathPattern.js +415 -0
- package/dist/Random.d.ts +5 -0
- package/dist/Random.js +49 -0
- package/dist/Route.d.ts +98 -0
- package/dist/Route.js +81 -0
- package/dist/RouteBody.d.ts +53 -0
- package/dist/RouteBody.js +67 -0
- package/dist/RouteHook.d.ts +12 -0
- package/dist/RouteHook.js +45 -0
- package/dist/RouteHttp.d.ts +21 -0
- package/dist/RouteHttp.js +260 -0
- package/dist/RouteHttpTracer.d.ts +10 -0
- package/dist/RouteHttpTracer.js +62 -0
- package/dist/RouteMount.d.ts +119 -0
- package/dist/RouteMount.js +77 -0
- package/dist/RouteSchema.d.ts +65 -0
- package/dist/RouteSchema.js +155 -0
- package/dist/RouteSse.d.ts +21 -0
- package/dist/RouteSse.js +85 -0
- package/dist/RouteTree.d.ts +56 -0
- package/dist/RouteTree.js +91 -0
- package/dist/RouteTrie.d.ts +20 -0
- package/dist/RouteTrie.js +157 -0
- package/dist/RouterPattern.d.ts +118 -0
- package/dist/RouterPattern.js +269 -0
- package/dist/SchemaExtra.d.ts +7 -0
- package/dist/SchemaExtra.js +74 -0
- package/dist/Start.d.ts +19 -0
- package/dist/Start.js +23 -0
- package/dist/StartApp.d.ts +19 -0
- package/dist/StartApp.js +21 -0
- package/dist/StreamExtra.d.ts +28 -0
- package/dist/StreamExtra.js +100 -0
- package/dist/TuplePathPattern.d.ts +9 -0
- package/dist/TuplePathPattern.js +63 -0
- package/dist/Values.d.ts +26 -0
- package/dist/Values.js +30 -0
- package/dist/bun/BunBundle.d.ts +12 -0
- package/dist/bun/BunBundle.js +145 -0
- package/dist/bun/BunHttpServer.d.ts +44 -0
- package/dist/bun/BunHttpServer.js +187 -0
- package/dist/bun/BunHttpServer_web.d.ts +60 -0
- package/dist/bun/BunHttpServer_web.js +252 -0
- package/dist/bun/BunImportTrackerPlugin.d.ts +13 -0
- package/dist/bun/BunImportTrackerPlugin.js +71 -0
- package/dist/bun/BunRoute.d.ts +49 -0
- package/dist/bun/BunRoute.js +131 -0
- package/dist/bun/BunRuntime.d.ts +1 -0
- package/dist/bun/BunRuntime.js +26 -0
- package/dist/bun/BunVirtualFilesPlugin.d.ts +4 -0
- package/dist/bun/BunVirtualFilesPlugin.js +40 -0
- package/dist/bun/_BunEnhancedResolve.d.ts +45 -0
- package/dist/bun/_BunEnhancedResolve.js +104 -0
- package/dist/bun/index.d.ts +4 -0
- package/dist/bun/index.js +4 -0
- package/dist/bundler/Bundle.d.ts +60 -0
- package/dist/bundler/Bundle.js +48 -0
- package/dist/bundler/BundleFiles.d.ts +13 -0
- package/dist/bundler/BundleFiles.js +94 -0
- package/dist/bundler/BundleHttp.d.ts +45 -0
- package/dist/bundler/BundleHttp.js +176 -0
- package/dist/client/Overlay.d.ts +2 -0
- package/dist/client/Overlay.js +32 -0
- package/dist/client/ScrollState.d.ts +6 -0
- package/dist/client/ScrollState.js +98 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +81 -0
- package/dist/experimental/EncryptedCookies.d.ts +51 -0
- package/dist/experimental/EncryptedCookies.js +243 -0
- package/dist/experimental/SseHttpResponse.d.ts +7 -0
- package/dist/experimental/SseHttpResponse.js +28 -0
- package/dist/experimental/index.d.ts +2 -0
- package/dist/experimental/index.js +2 -0
- package/dist/hyper/Hyper.d.ts +32 -0
- package/dist/hyper/Hyper.js +34 -0
- package/dist/hyper/HyperHtml.d.ts +23 -0
- package/dist/hyper/HyperHtml.js +144 -0
- package/dist/hyper/HyperNode.d.ts +14 -0
- package/dist/hyper/HyperNode.js +11 -0
- package/dist/hyper/HyperRoute.d.ts +8 -0
- package/dist/hyper/HyperRoute.js +32 -0
- package/dist/hyper/HyperRoute.test.d.ts +1 -0
- package/dist/hyper/HyperRoute.test.js +72 -0
- package/dist/hyper/index.d.ts +4 -0
- package/dist/hyper/index.js +4 -0
- package/dist/hyper/jsx-runtime.d.ts +7 -0
- package/dist/hyper/jsx-runtime.js +8 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inference_check.d.ts +1 -0
- package/dist/inference_check.js +15 -0
- package/dist/middlewares/BasicAuthMiddleware.d.ts +8 -0
- package/dist/middlewares/BasicAuthMiddleware.js +22 -0
- package/dist/middlewares/index.d.ts +1 -0
- package/dist/middlewares/index.js +1 -0
- package/dist/node/FileSystem.d.ts +9 -0
- package/dist/node/FileSystem.js +440 -0
- package/dist/node/Utils.d.ts +1 -0
- package/dist/node/Utils.js +19 -0
- package/dist/repro_fail.d.ts +1 -0
- package/dist/repro_fail.js +14 -0
- package/dist/testing/TestHttpClient.d.ts +13 -0
- package/dist/testing/TestHttpClient.js +68 -0
- package/dist/testing/TestLogger.d.ts +13 -0
- package/dist/testing/TestLogger.js +29 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.js +3 -0
- package/dist/testing/utils.d.ts +9 -0
- package/dist/testing/utils.js +39 -0
- package/dist/x/cloudflare/CloudflareTunnel.d.ts +13 -0
- package/dist/x/cloudflare/CloudflareTunnel.js +43 -0
- package/dist/x/cloudflare/index.d.ts +1 -0
- package/dist/x/cloudflare/index.js +1 -0
- package/dist/x/datastar/Datastar.d.ts +6 -0
- package/dist/x/datastar/Datastar.js +46 -0
- package/dist/x/datastar/index.d.ts +2 -0
- package/dist/x/datastar/index.js +2 -0
- package/dist/x/tailwind/TailwindPlugin.d.ts +23 -0
- package/dist/x/tailwind/TailwindPlugin.js +219 -0
- package/dist/x/tailwind/compile.d.ts +19 -0
- package/dist/x/tailwind/compile.js +156 -0
- package/dist/x/tailwind/plugin.d.ts +2 -0
- package/dist/x/tailwind/plugin.js +15 -0
- package/package.json +68 -16
- package/src/RouteBody.test.ts +18 -0
- package/src/RouteBody.ts +126 -2
- package/src/x/tailwind/compile.ts +8 -2
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type * as FileSystem from "@effect/platform/FileSystem";
|
|
2
|
+
import * as Headers from "@effect/platform/Headers";
|
|
3
|
+
import * as HttpIncomingMessage from "@effect/platform/HttpIncomingMessage";
|
|
4
|
+
import type { HttpMethod } from "@effect/platform/HttpMethod";
|
|
5
|
+
import * as HttpServerError from "@effect/platform/HttpServerError";
|
|
6
|
+
import * as HttpServerRequest from "@effect/platform/HttpServerRequest";
|
|
7
|
+
import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
|
|
8
|
+
import type * as Multipart from "@effect/platform/Multipart";
|
|
9
|
+
import type * as Path from "@effect/platform/Path";
|
|
10
|
+
import * as Socket from "@effect/platform/Socket";
|
|
11
|
+
import * as UrlParams from "@effect/platform/UrlParams";
|
|
12
|
+
import type { Server as BunServerInstance, ServerWebSocket } from "bun";
|
|
13
|
+
import * as Deferred from "effect/Deferred";
|
|
14
|
+
import * as Effect from "effect/Effect";
|
|
15
|
+
import * as Inspectable from "effect/Inspectable";
|
|
16
|
+
import * as Option from "effect/Option";
|
|
17
|
+
import * as Runtime from "effect/Runtime";
|
|
18
|
+
import type * as Scope from "effect/Scope";
|
|
19
|
+
import * as Stream from "effect/Stream";
|
|
20
|
+
export interface WebSocketContext {
|
|
21
|
+
readonly deferred: Deferred.Deferred<ServerWebSocket<WebSocketContext>>;
|
|
22
|
+
readonly closeDeferred: Deferred.Deferred<void, Socket.SocketError>;
|
|
23
|
+
readonly buffer: Array<Uint8Array | string>;
|
|
24
|
+
run: (_: Uint8Array | string) => void;
|
|
25
|
+
}
|
|
26
|
+
export declare class ServerRequestImpl extends Inspectable.Class implements HttpServerRequest.HttpServerRequest {
|
|
27
|
+
readonly [HttpServerRequest.TypeId]: HttpServerRequest.TypeId;
|
|
28
|
+
readonly [HttpIncomingMessage.TypeId]: HttpIncomingMessage.TypeId;
|
|
29
|
+
readonly source: Request;
|
|
30
|
+
resolve: (response: Response) => void;
|
|
31
|
+
readonly url: string;
|
|
32
|
+
private bunServer;
|
|
33
|
+
headersOverride?: Headers.Headers;
|
|
34
|
+
private remoteAddressOverride?;
|
|
35
|
+
constructor(source: Request, resolve: (response: Response) => void, url: string, bunServer: BunServerInstance<WebSocketContext>, headersOverride?: Headers.Headers, remoteAddressOverride?: string);
|
|
36
|
+
toJSON(): unknown;
|
|
37
|
+
modify(options: {
|
|
38
|
+
readonly url?: string | undefined;
|
|
39
|
+
readonly headers?: Headers.Headers | undefined;
|
|
40
|
+
readonly remoteAddress?: string | undefined;
|
|
41
|
+
}): ServerRequestImpl;
|
|
42
|
+
get method(): HttpMethod;
|
|
43
|
+
get originalUrl(): string;
|
|
44
|
+
get remoteAddress(): Option.Option<string>;
|
|
45
|
+
get headers(): Headers.Headers;
|
|
46
|
+
private cachedCookies;
|
|
47
|
+
get cookies(): Record<string, string>;
|
|
48
|
+
get stream(): Stream.Stream<Uint8Array, HttpServerError.RequestError>;
|
|
49
|
+
private textEffect;
|
|
50
|
+
get text(): Effect.Effect<string, HttpServerError.RequestError>;
|
|
51
|
+
get json(): Effect.Effect<unknown, HttpServerError.RequestError>;
|
|
52
|
+
get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, HttpServerError.RequestError>;
|
|
53
|
+
private multipartEffect;
|
|
54
|
+
get multipart(): Effect.Effect<Multipart.Persisted, Multipart.MultipartError, Scope.Scope | FileSystem.FileSystem | Path.Path>;
|
|
55
|
+
get multipartStream(): Stream.Stream<Multipart.Part, Multipart.MultipartError>;
|
|
56
|
+
private arrayBufferEffect;
|
|
57
|
+
get arrayBuffer(): Effect.Effect<ArrayBuffer, HttpServerError.RequestError>;
|
|
58
|
+
get upgrade(): Effect.Effect<Socket.Socket, HttpServerError.RequestError>;
|
|
59
|
+
}
|
|
60
|
+
export declare function makeResponse(request: HttpServerRequest.HttpServerRequest, response: HttpServerResponse.HttpServerResponse, runtime: Runtime.Runtime<never>): Response;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import * as Cookies from "@effect/platform/Cookies";
|
|
2
|
+
import * as Headers from "@effect/platform/Headers";
|
|
3
|
+
import * as HttpApp from "@effect/platform/HttpApp";
|
|
4
|
+
import * as HttpIncomingMessage from "@effect/platform/HttpIncomingMessage";
|
|
5
|
+
import * as HttpServerError from "@effect/platform/HttpServerError";
|
|
6
|
+
import * as HttpServerRequest from "@effect/platform/HttpServerRequest";
|
|
7
|
+
import * as Socket from "@effect/platform/Socket";
|
|
8
|
+
import * as UrlParams from "@effect/platform/UrlParams";
|
|
9
|
+
import * as Deferred from "effect/Deferred";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as FiberSet from "effect/FiberSet";
|
|
12
|
+
import * as Inspectable from "effect/Inspectable";
|
|
13
|
+
import * as Option from "effect/Option";
|
|
14
|
+
import * as Stream from "effect/Stream";
|
|
15
|
+
export class ServerRequestImpl extends Inspectable.Class {
|
|
16
|
+
[HttpServerRequest.TypeId];
|
|
17
|
+
[HttpIncomingMessage.TypeId];
|
|
18
|
+
source;
|
|
19
|
+
resolve;
|
|
20
|
+
url;
|
|
21
|
+
bunServer;
|
|
22
|
+
headersOverride;
|
|
23
|
+
remoteAddressOverride;
|
|
24
|
+
constructor(source, resolve, url, bunServer, headersOverride, remoteAddressOverride) {
|
|
25
|
+
super();
|
|
26
|
+
this[HttpServerRequest.TypeId] = HttpServerRequest.TypeId;
|
|
27
|
+
this[HttpIncomingMessage.TypeId] = HttpIncomingMessage.TypeId;
|
|
28
|
+
this.source = source;
|
|
29
|
+
this.resolve = resolve;
|
|
30
|
+
this.url = url;
|
|
31
|
+
this.bunServer = bunServer;
|
|
32
|
+
this.headersOverride = headersOverride;
|
|
33
|
+
this.remoteAddressOverride = remoteAddressOverride;
|
|
34
|
+
}
|
|
35
|
+
toJSON() {
|
|
36
|
+
return HttpIncomingMessage.inspect(this, {
|
|
37
|
+
_id: "@effect/platform/HttpServerRequest",
|
|
38
|
+
method: this.method,
|
|
39
|
+
url: this.originalUrl,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
modify(options) {
|
|
43
|
+
return new ServerRequestImpl(this.source, this.resolve, options.url ?? this.url, this.bunServer, options.headers ?? this.headersOverride, options.remoteAddress ?? this.remoteAddressOverride);
|
|
44
|
+
}
|
|
45
|
+
get method() {
|
|
46
|
+
return this.source.method.toUpperCase();
|
|
47
|
+
}
|
|
48
|
+
get originalUrl() {
|
|
49
|
+
return this.source.url;
|
|
50
|
+
}
|
|
51
|
+
get remoteAddress() {
|
|
52
|
+
return this.remoteAddressOverride
|
|
53
|
+
? Option.some(this.remoteAddressOverride)
|
|
54
|
+
: Option.fromNullable(this.bunServer.requestIP(this.source)?.address);
|
|
55
|
+
}
|
|
56
|
+
get headers() {
|
|
57
|
+
this.headersOverride ??= Headers.fromInput(this.source.headers);
|
|
58
|
+
return this.headersOverride;
|
|
59
|
+
}
|
|
60
|
+
cachedCookies;
|
|
61
|
+
get cookies() {
|
|
62
|
+
if (this.cachedCookies) {
|
|
63
|
+
return this.cachedCookies;
|
|
64
|
+
}
|
|
65
|
+
return this.cachedCookies = Cookies.parseHeader(this.headers.cookie ?? "");
|
|
66
|
+
}
|
|
67
|
+
get stream() {
|
|
68
|
+
return this.source.body
|
|
69
|
+
? Stream.fromReadableStream(() => this.source.body, (cause) => new HttpServerError.RequestError({
|
|
70
|
+
request: this,
|
|
71
|
+
reason: "Decode",
|
|
72
|
+
cause,
|
|
73
|
+
}))
|
|
74
|
+
: Stream.fail(new HttpServerError.RequestError({
|
|
75
|
+
request: this,
|
|
76
|
+
reason: "Decode",
|
|
77
|
+
description: "can not create stream from empty body",
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
textEffect;
|
|
81
|
+
get text() {
|
|
82
|
+
if (this.textEffect) {
|
|
83
|
+
return this.textEffect;
|
|
84
|
+
}
|
|
85
|
+
this.textEffect = Effect.runSync(Effect.cached(Effect.tryPromise({
|
|
86
|
+
try: () => this.source.text(),
|
|
87
|
+
catch: (cause) => new HttpServerError.RequestError({
|
|
88
|
+
request: this,
|
|
89
|
+
reason: "Decode",
|
|
90
|
+
cause,
|
|
91
|
+
}),
|
|
92
|
+
})));
|
|
93
|
+
return this.textEffect;
|
|
94
|
+
}
|
|
95
|
+
get json() {
|
|
96
|
+
return Effect.tryMap(this.text, {
|
|
97
|
+
try: (_) => JSON.parse(_),
|
|
98
|
+
catch: (cause) => new HttpServerError.RequestError({
|
|
99
|
+
request: this,
|
|
100
|
+
reason: "Decode",
|
|
101
|
+
cause,
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
get urlParamsBody() {
|
|
106
|
+
return Effect.flatMap(this.text, (_) => Effect.try({
|
|
107
|
+
try: () => UrlParams.fromInput(new URLSearchParams(_)),
|
|
108
|
+
catch: (cause) => new HttpServerError.RequestError({
|
|
109
|
+
request: this,
|
|
110
|
+
reason: "Decode",
|
|
111
|
+
cause,
|
|
112
|
+
}),
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
multipartEffect;
|
|
116
|
+
get multipart() {
|
|
117
|
+
if (this.multipartEffect) {
|
|
118
|
+
return this.multipartEffect;
|
|
119
|
+
}
|
|
120
|
+
this.multipartEffect = Effect.runSync(Effect.cached(Effect.die("Multipart not implemented")));
|
|
121
|
+
return this.multipartEffect;
|
|
122
|
+
}
|
|
123
|
+
get multipartStream() {
|
|
124
|
+
return Stream.die("Multipart stream not implemented");
|
|
125
|
+
}
|
|
126
|
+
arrayBufferEffect;
|
|
127
|
+
get arrayBuffer() {
|
|
128
|
+
if (this.arrayBufferEffect) {
|
|
129
|
+
return this.arrayBufferEffect;
|
|
130
|
+
}
|
|
131
|
+
this.arrayBufferEffect = Effect.runSync(Effect.cached(Effect.tryPromise({
|
|
132
|
+
try: () => this.source.arrayBuffer(),
|
|
133
|
+
catch: (cause) => new HttpServerError.RequestError({
|
|
134
|
+
request: this,
|
|
135
|
+
reason: "Decode",
|
|
136
|
+
cause,
|
|
137
|
+
}),
|
|
138
|
+
})));
|
|
139
|
+
return this.arrayBufferEffect;
|
|
140
|
+
}
|
|
141
|
+
get upgrade() {
|
|
142
|
+
return Effect.flatMap(Effect.all([
|
|
143
|
+
Deferred.make(),
|
|
144
|
+
Deferred.make(),
|
|
145
|
+
Effect.makeSemaphore(1),
|
|
146
|
+
]), ([deferred, closeDeferred, semaphore]) => Effect.async((resume) => {
|
|
147
|
+
const success = this.bunServer.upgrade(this.source, {
|
|
148
|
+
data: {
|
|
149
|
+
deferred,
|
|
150
|
+
closeDeferred,
|
|
151
|
+
buffer: [],
|
|
152
|
+
run: wsDefaultRun,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
if (!success) {
|
|
156
|
+
resume(Effect.fail(new HttpServerError.RequestError({
|
|
157
|
+
request: this,
|
|
158
|
+
reason: "Decode",
|
|
159
|
+
description: "Not an upgradeable ServerRequest",
|
|
160
|
+
})));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
resume(Effect.map(Deferred.await(deferred), (ws) => {
|
|
164
|
+
const write = (chunk) => Effect.sync(() => {
|
|
165
|
+
if (typeof chunk === "string") {
|
|
166
|
+
ws.sendText(chunk);
|
|
167
|
+
}
|
|
168
|
+
else if (Socket.isCloseEvent(chunk)) {
|
|
169
|
+
ws.close(chunk.code, chunk.reason);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
ws.sendBinary(chunk);
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
});
|
|
176
|
+
const writer = Effect.succeed(write);
|
|
177
|
+
const runRaw = Effect.fnUntraced(function* (handler, opts) {
|
|
178
|
+
const set = yield* FiberSet.make();
|
|
179
|
+
const run = yield* FiberSet.runtime(set)();
|
|
180
|
+
function runRawInner(data) {
|
|
181
|
+
const result = handler(data);
|
|
182
|
+
if (Effect.isEffect(result)) {
|
|
183
|
+
run(result);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
ws.data.run = runRawInner;
|
|
187
|
+
ws.data.buffer.forEach(runRawInner);
|
|
188
|
+
ws.data.buffer.length = 0;
|
|
189
|
+
if (opts?.onOpen)
|
|
190
|
+
yield* opts.onOpen;
|
|
191
|
+
return yield* FiberSet.join(set);
|
|
192
|
+
}, Effect.scoped, Effect.onExit((exit) => {
|
|
193
|
+
ws.close(exit._tag === "Success" ? 1000 : 1011);
|
|
194
|
+
return Effect.void;
|
|
195
|
+
}), Effect.raceFirst(Deferred.await(closeDeferred)), semaphore.withPermits(1));
|
|
196
|
+
const encoder = new TextEncoder();
|
|
197
|
+
const run = (handler, opts) => runRaw((data) => typeof data === "string"
|
|
198
|
+
? handler(encoder.encode(data))
|
|
199
|
+
: handler(data), opts);
|
|
200
|
+
return Socket.Socket.of({
|
|
201
|
+
[Socket.TypeId]: Socket.TypeId,
|
|
202
|
+
run,
|
|
203
|
+
runRaw,
|
|
204
|
+
writer,
|
|
205
|
+
});
|
|
206
|
+
}));
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function wsDefaultRun(_) {
|
|
211
|
+
this.buffer.push(_);
|
|
212
|
+
}
|
|
213
|
+
export function makeResponse(request, response, runtime) {
|
|
214
|
+
const fields = {
|
|
215
|
+
headers: new globalThis.Headers(response.headers),
|
|
216
|
+
status: response.status,
|
|
217
|
+
};
|
|
218
|
+
if (!Cookies.isEmpty(response.cookies)) {
|
|
219
|
+
for (const header of Cookies.toSetCookieHeaders(response.cookies)) {
|
|
220
|
+
fields.headers.append("set-cookie", header);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (response.statusText !== undefined) {
|
|
224
|
+
fields.statusText = response.statusText;
|
|
225
|
+
}
|
|
226
|
+
if (request.method === "HEAD") {
|
|
227
|
+
return new Response(undefined, fields);
|
|
228
|
+
}
|
|
229
|
+
const ejectedResponse = HttpApp.unsafeEjectStreamScope(response);
|
|
230
|
+
const body = ejectedResponse.body;
|
|
231
|
+
switch (body._tag) {
|
|
232
|
+
case "Empty": {
|
|
233
|
+
return new Response(undefined, fields);
|
|
234
|
+
}
|
|
235
|
+
case "Uint8Array":
|
|
236
|
+
case "Raw": {
|
|
237
|
+
if (body.body instanceof Response) {
|
|
238
|
+
for (const [key, value] of fields.headers.entries()) {
|
|
239
|
+
body.body.headers.set(key, value);
|
|
240
|
+
}
|
|
241
|
+
return body.body;
|
|
242
|
+
}
|
|
243
|
+
return new Response(body.body, fields);
|
|
244
|
+
}
|
|
245
|
+
case "FormData": {
|
|
246
|
+
return new Response(body.formData, fields);
|
|
247
|
+
}
|
|
248
|
+
case "Stream": {
|
|
249
|
+
return new Response(Stream.toReadableStreamRuntime(body.stream, runtime), fields);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type BunPlugin, type Import } from "bun";
|
|
2
|
+
export type ImportMap = ReadonlyMap<string, Import[]>;
|
|
3
|
+
/**
|
|
4
|
+
* Tracks all imported modules.
|
|
5
|
+
* State can be accessed via 'virtual:import-tracker' module within a bundle
|
|
6
|
+
* or through `state` property returned by this function.
|
|
7
|
+
*/
|
|
8
|
+
export declare const make: (opts?: {
|
|
9
|
+
includeNodeModules?: false;
|
|
10
|
+
baseDir?: string;
|
|
11
|
+
}) => BunPlugin & {
|
|
12
|
+
state: ImportMap;
|
|
13
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as NPath from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* Tracks all imported modules.
|
|
4
|
+
* State can be accessed via 'virtual:import-tracker' module within a bundle
|
|
5
|
+
* or through `state` property returned by this function.
|
|
6
|
+
*/
|
|
7
|
+
export const make = (opts = {}) => {
|
|
8
|
+
const foundImports = new Map();
|
|
9
|
+
const baseDir = opts.baseDir ?? process.cwd();
|
|
10
|
+
return {
|
|
11
|
+
name: "import tracker",
|
|
12
|
+
setup(build) {
|
|
13
|
+
const transpiler = new Bun.Transpiler({
|
|
14
|
+
loader: "tsx",
|
|
15
|
+
});
|
|
16
|
+
// Each module that goes through this onLoad callback
|
|
17
|
+
// will record its imports in `trackedImports`
|
|
18
|
+
build.onLoad({
|
|
19
|
+
filter: /\.(ts|js)x?$/,
|
|
20
|
+
}, async (args) => {
|
|
21
|
+
if (!opts.includeNodeModules
|
|
22
|
+
&& args.path.includes("/node_modules/")) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const contents = await Bun.file(args.path).arrayBuffer();
|
|
26
|
+
try {
|
|
27
|
+
const fileImport = transpiler.scanImports(contents);
|
|
28
|
+
const resolvedImports = fileImport.map(imp => {
|
|
29
|
+
const absoluteImportPath = NPath.resolve(NPath.dirname(args.path),
|
|
30
|
+
// 'file' is a default namespace, trim it
|
|
31
|
+
imp.path.replace(/^file:/, ""));
|
|
32
|
+
return {
|
|
33
|
+
...imp,
|
|
34
|
+
// keep all module identifiers with namespace intact
|
|
35
|
+
path: /(\w+):/.test(imp.path)
|
|
36
|
+
? imp.path
|
|
37
|
+
: NPath.relative(baseDir, absoluteImportPath),
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
foundImports.set(NPath.relative(baseDir, args.path), resolvedImports);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
});
|
|
46
|
+
build.onResolve({
|
|
47
|
+
filter: /^virtual:import-tracker$/,
|
|
48
|
+
}, () => {
|
|
49
|
+
return {
|
|
50
|
+
namespace: "effect-start",
|
|
51
|
+
path: "virtual:import-tracker",
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
build.onLoad({
|
|
55
|
+
filter: /^virtual:import-tracker$/,
|
|
56
|
+
namespace: "effect-start",
|
|
57
|
+
}, async (args) => {
|
|
58
|
+
// Wait for all files to be loaded, ensuring
|
|
59
|
+
// that every file goes through the above `onLoad()` function
|
|
60
|
+
// and their imports tracked
|
|
61
|
+
await args.defer();
|
|
62
|
+
// Emit JSON containing the stats of each import
|
|
63
|
+
return {
|
|
64
|
+
contents: JSON.stringify(Object.fromEntries(foundImports.entries())),
|
|
65
|
+
loader: "json",
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
state: foundImports,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type * as Bun from "bun";
|
|
2
|
+
import * as Option from "effect/Option";
|
|
3
|
+
import * as Route from "../Route.ts";
|
|
4
|
+
import * as BunHttpServer from "./BunHttpServer.ts";
|
|
5
|
+
declare const BunRouteError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
6
|
+
readonly _tag: "BunRouteError";
|
|
7
|
+
} & Readonly<A>;
|
|
8
|
+
export declare class BunRouteError extends BunRouteError_base<{
|
|
9
|
+
reason: "ProxyError" | "UnsupportedPattern";
|
|
10
|
+
pattern: string;
|
|
11
|
+
message: string;
|
|
12
|
+
}> {
|
|
13
|
+
}
|
|
14
|
+
export type BunDescriptors = {
|
|
15
|
+
bunPrefix: string;
|
|
16
|
+
bunLoad: () => Promise<Bun.HTMLBundle>;
|
|
17
|
+
};
|
|
18
|
+
export declare function descriptors(route: Route.Route.Route): BunDescriptors | undefined;
|
|
19
|
+
export declare function htmlBundle(load: () => Promise<Bun.HTMLBundle | {
|
|
20
|
+
default: Bun.HTMLBundle;
|
|
21
|
+
}>): <D extends Route.RouteDescriptor.Any, B extends {}, I extends Route.Route.Tuple>(self: Route.RouteSet.RouteSet<D, B, I>) => Route.RouteSet.RouteSet<D, B, [...I, Route.Route.Route<BunDescriptors & {
|
|
22
|
+
format: "html";
|
|
23
|
+
}, {
|
|
24
|
+
request: Request;
|
|
25
|
+
}, string, BunRouteError, BunHttpServer.BunHttpServer>]>;
|
|
26
|
+
type BunServerFetchHandler = (request: Request, server: Bun.Server<unknown>) => Response | Promise<Response>;
|
|
27
|
+
type BunServerRouteHandler = Bun.HTMLBundle | BunServerFetchHandler | Partial<Record<Bun.Serve.HTTPMethod, BunServerFetchHandler>>;
|
|
28
|
+
export type BunRoutes = Record<string, BunServerRouteHandler>;
|
|
29
|
+
/**
|
|
30
|
+
* Validates that a route pattern can be implemented with Bun.serve routes.
|
|
31
|
+
*
|
|
32
|
+
* Supported patterns (native or via multiple routes):
|
|
33
|
+
* - /exact - Exact match
|
|
34
|
+
* - /users/:id - Full-segment named param
|
|
35
|
+
* - /path/* - Directory wildcard
|
|
36
|
+
* - /* - Catch-all
|
|
37
|
+
* - /[[id]] - Optional param (implemented via `/` and `/:id`)
|
|
38
|
+
* - /[[...rest]] - Optional rest param (implemented via `/` and `/*`)
|
|
39
|
+
*
|
|
40
|
+
* Unsupported patterns (cannot be implemented in Bun):
|
|
41
|
+
* - /pk_[id] - Prefix before param
|
|
42
|
+
* - /[id]_sfx - Suffix after param
|
|
43
|
+
* - /[id].json - Suffix with dot
|
|
44
|
+
* - /[id]~test - Suffix with tilde
|
|
45
|
+
* - /hello-* - Inline prefix wildcard
|
|
46
|
+
*/
|
|
47
|
+
export declare function validateBunPattern(pattern: string): Option.Option<BunRouteError>;
|
|
48
|
+
export declare const isHtmlBundle: (handle: any) => boolean;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import * as Array from "effect/Array";
|
|
2
|
+
import * as Data from "effect/Data";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
import * as Entity from "../Entity.js";
|
|
6
|
+
import * as Hyper from "../hyper/Hyper.js";
|
|
7
|
+
import * as HyperHtml from "../hyper/HyperHtml.js";
|
|
8
|
+
import * as Random from "../Random.js";
|
|
9
|
+
import * as Route from "../Route.js";
|
|
10
|
+
import * as RouterPattern from "../RouterPattern.js";
|
|
11
|
+
import * as BunHttpServer from "./BunHttpServer.js";
|
|
12
|
+
const INTERNAL_FETCH_HEADER = "x-effect-start-internal-fetch";
|
|
13
|
+
export class BunRouteError extends Data.TaggedError("BunRouteError") {
|
|
14
|
+
}
|
|
15
|
+
export function descriptors(route) {
|
|
16
|
+
const descriptor = Route.descriptor(route);
|
|
17
|
+
if (typeof descriptor.bunPrefix === "string"
|
|
18
|
+
&& typeof descriptor.bunLoad === "function") {
|
|
19
|
+
return descriptor;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
export function htmlBundle(load) {
|
|
24
|
+
const bunPrefix = `/.BunRoute-${Random.token(6)}`;
|
|
25
|
+
const bunLoad = () => load().then(mod => "default" in mod ? mod.default : mod);
|
|
26
|
+
const descriptors = { bunPrefix, bunLoad, format: "html" };
|
|
27
|
+
return function (self) {
|
|
28
|
+
const handler = (context, next) => Effect.gen(function* () {
|
|
29
|
+
const originalRequest = context.request;
|
|
30
|
+
if (originalRequest.headers.get(INTERNAL_FETCH_HEADER) === "true") {
|
|
31
|
+
const url = new URL(originalRequest.url);
|
|
32
|
+
return yield* Effect.fail(new BunRouteError({
|
|
33
|
+
reason: "ProxyError",
|
|
34
|
+
pattern: url.pathname,
|
|
35
|
+
message: "Request to internal Bun server was caught by BunRoute handler. This should not happen. Please report a bug.",
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
const bunServer = yield* BunHttpServer.BunHttpServer;
|
|
39
|
+
const url = new URL(originalRequest.url);
|
|
40
|
+
const internalPath = `${bunPrefix}${url.pathname}`;
|
|
41
|
+
const internalUrl = new URL(internalPath, bunServer.server.url);
|
|
42
|
+
const headers = new Headers(originalRequest.headers);
|
|
43
|
+
headers.set(INTERNAL_FETCH_HEADER, "true");
|
|
44
|
+
const proxyRequest = new Request(internalUrl, {
|
|
45
|
+
method: originalRequest.method,
|
|
46
|
+
headers,
|
|
47
|
+
});
|
|
48
|
+
const response = yield* Effect.tryPromise({
|
|
49
|
+
try: () => fetch(proxyRequest),
|
|
50
|
+
catch: (error) => new BunRouteError({
|
|
51
|
+
reason: "ProxyError",
|
|
52
|
+
pattern: internalPath,
|
|
53
|
+
message: `Failed to fetch internal HTML bundle: ${String(error)}`,
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
let html = yield* Effect.tryPromise({
|
|
57
|
+
try: () => response.text(),
|
|
58
|
+
catch: (error) => new BunRouteError({
|
|
59
|
+
reason: "ProxyError",
|
|
60
|
+
pattern: internalPath,
|
|
61
|
+
message: String(error),
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
64
|
+
const childEntity = yield* Entity.resolve(next(context));
|
|
65
|
+
const children = childEntity?.body ?? childEntity;
|
|
66
|
+
let childrenHtml = "";
|
|
67
|
+
if (children != null) {
|
|
68
|
+
if (children instanceof Response) {
|
|
69
|
+
childrenHtml = yield* Effect.promise(() => children.text());
|
|
70
|
+
}
|
|
71
|
+
else if (Hyper.isGenericJsxObject(children)) {
|
|
72
|
+
childrenHtml = HyperHtml.renderToString(children);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
childrenHtml = String(children);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
html = html.replace(/%children%/g, childrenHtml);
|
|
79
|
+
return Entity.make(html, {
|
|
80
|
+
status: response.status,
|
|
81
|
+
headers: {
|
|
82
|
+
"content-type": response.headers.get("content-type"),
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
const route = Route.make(handler, descriptors);
|
|
87
|
+
return Route.set([...Route.items(self), route], Route.descriptor(self));
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Validates that a route pattern can be implemented with Bun.serve routes.
|
|
92
|
+
*
|
|
93
|
+
* Supported patterns (native or via multiple routes):
|
|
94
|
+
* - /exact - Exact match
|
|
95
|
+
* - /users/:id - Full-segment named param
|
|
96
|
+
* - /path/* - Directory wildcard
|
|
97
|
+
* - /* - Catch-all
|
|
98
|
+
* - /[[id]] - Optional param (implemented via `/` and `/:id`)
|
|
99
|
+
* - /[[...rest]] - Optional rest param (implemented via `/` and `/*`)
|
|
100
|
+
*
|
|
101
|
+
* Unsupported patterns (cannot be implemented in Bun):
|
|
102
|
+
* - /pk_[id] - Prefix before param
|
|
103
|
+
* - /[id]_sfx - Suffix after param
|
|
104
|
+
* - /[id].json - Suffix with dot
|
|
105
|
+
* - /[id]~test - Suffix with tilde
|
|
106
|
+
* - /hello-* - Inline prefix wildcard
|
|
107
|
+
*/
|
|
108
|
+
export function validateBunPattern(pattern) {
|
|
109
|
+
const segments = RouterPattern.parse(pattern);
|
|
110
|
+
const unsupported = Array.findFirst(segments, (seg) => {
|
|
111
|
+
if (seg._tag === "ParamSegment") {
|
|
112
|
+
return seg.prefix !== undefined || seg.suffix !== undefined;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
});
|
|
116
|
+
if (Option.isSome(unsupported)) {
|
|
117
|
+
return Option.some(new BunRouteError({
|
|
118
|
+
reason: "UnsupportedPattern",
|
|
119
|
+
pattern,
|
|
120
|
+
message: `Pattern "${pattern}" uses prefixed/suffixed params (prefix_[param] or [param]_suffix) `
|
|
121
|
+
+ `which cannot be implemented in Bun.serve.`,
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
return Option.none();
|
|
125
|
+
}
|
|
126
|
+
export const isHtmlBundle = (handle) => {
|
|
127
|
+
return (typeof handle === "object"
|
|
128
|
+
&& handle !== null
|
|
129
|
+
&& (handle.toString() === "[object HTMLBundle]"
|
|
130
|
+
|| typeof handle.index === "string"));
|
|
131
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const runMain: import("@effect/platform/Runtime").RunMain;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { makeRunMain } from "@effect/platform/Runtime";
|
|
2
|
+
import { constVoid } from "effect/Function";
|
|
3
|
+
export const runMain = makeRunMain(({ fiber, teardown, }) => {
|
|
4
|
+
const keepAlive = setInterval(constVoid, 2 ** 31 - 1);
|
|
5
|
+
let receivedSignal = false;
|
|
6
|
+
fiber.addObserver((exit) => {
|
|
7
|
+
if (!receivedSignal) {
|
|
8
|
+
process.removeListener("SIGINT", onSigint);
|
|
9
|
+
process.removeListener("SIGTERM", onSigint);
|
|
10
|
+
}
|
|
11
|
+
clearInterval(keepAlive);
|
|
12
|
+
teardown(exit, (code) => {
|
|
13
|
+
if (receivedSignal || code !== 0) {
|
|
14
|
+
process.exit(code);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
function onSigint() {
|
|
19
|
+
receivedSignal = true;
|
|
20
|
+
process.removeListener("SIGINT", onSigint);
|
|
21
|
+
process.removeListener("SIGTERM", onSigint);
|
|
22
|
+
fiber.unsafeInterruptAsFork(fiber.id());
|
|
23
|
+
}
|
|
24
|
+
process.on("SIGINT", onSigint);
|
|
25
|
+
process.on("SIGTERM", onSigint);
|
|
26
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
function loaderFromPath(path) {
|
|
2
|
+
return path.slice(path.lastIndexOf(".") + 1);
|
|
3
|
+
}
|
|
4
|
+
export function make(files) {
|
|
5
|
+
return {
|
|
6
|
+
name: "virtual-fs",
|
|
7
|
+
setup(build) {
|
|
8
|
+
build.onResolve({
|
|
9
|
+
// change the filter so it only works for file namespace
|
|
10
|
+
filter: /.*/,
|
|
11
|
+
}, (args) => {
|
|
12
|
+
const resolved = resolvePath(args.path, args.resolveDir);
|
|
13
|
+
const resolvedFile = files[resolved];
|
|
14
|
+
if (resolvedFile) {
|
|
15
|
+
return {
|
|
16
|
+
path: resolved,
|
|
17
|
+
namespace: "virtual-fs",
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
});
|
|
22
|
+
build.onLoad({
|
|
23
|
+
filter: /.*/,
|
|
24
|
+
namespace: "virtual-fs",
|
|
25
|
+
}, (args) => {
|
|
26
|
+
const contents = files[args.path];
|
|
27
|
+
if (!contents) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
contents,
|
|
32
|
+
loader: loaderFromPath(args.path),
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function resolvePath(path, base = process.cwd()) {
|
|
39
|
+
return Bun.resolveSync(path, base);
|
|
40
|
+
}
|