@chidchanun/bcp 0.1.5 → 0.1.7

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.
@@ -41,6 +41,8 @@ import {
41
41
  DevHmrServer,
42
42
  } from "./dev-hmr.js";
43
43
 
44
+ import { runWithRequestContext, applyResponseCookies } from "./request-context.ts";
45
+
44
46
  /*
45
47
  * =====================================
46
48
  * Constants
@@ -1441,24 +1443,41 @@ async function handleApiRoute(
1441
1443
  );
1442
1444
 
1443
1445
  const response =
1444
- await handler(
1446
+ await runWithRequestContext(
1445
1447
  request,
1448
+
1449
+ async () => {
1450
+ const result =
1451
+ await handler(
1452
+ request,
1453
+ {
1454
+ params,
1455
+ }
1456
+ );
1457
+
1458
+ if (
1459
+ !(
1460
+ result instanceof
1461
+ Response
1462
+ )
1463
+ ) {
1464
+ throw new Error(
1465
+ `API handler "${method} ${route.pathname}" must return a Response object.`
1466
+ );
1467
+ }
1468
+
1469
+ return applyResponseCookies(
1470
+ result
1471
+ );
1472
+ },
1446
1473
  {
1447
- params,
1474
+ remoteAddress:
1475
+ req.socket
1476
+ .remoteAddress ??
1477
+ null,
1448
1478
  }
1449
1479
  );
1450
1480
 
1451
- if (
1452
- !(
1453
- response instanceof
1454
- Response
1455
- )
1456
- ) {
1457
- throw new Error(
1458
- `API handler "${method} ${route.pathname}" must return a Response object.`
1459
- );
1460
- }
1461
-
1462
1481
  await sendWebResponse(
1463
1482
  res,
1464
1483
  response,
@@ -5,6 +5,10 @@ import type {
5
5
  DevServerOptions,
6
6
  } from "./index.js";
7
7
 
8
+ import {
9
+ createCriticalCssProxy,
10
+ } from "./critical-css-proxy.js";
11
+
8
12
  import {
9
13
  loadProjectMiddleware,
10
14
  } from "./middleware-loader.js";
@@ -56,6 +60,12 @@ export function createMiddlewareDevServer(
56
60
  > | null =
57
61
  null;
58
62
 
63
+ let criticalCssGateway:
64
+ ReturnType<
65
+ typeof createCriticalCssProxy
66
+ > | null =
67
+ null;
68
+
59
69
  let started =
60
70
  false;
61
71
 
@@ -68,6 +78,8 @@ export function createMiddlewareDevServer(
68
78
  await findFreePort();
69
79
  const middlewarePort =
70
80
  await findFreePort();
81
+ const securityPort =
82
+ await findFreePort();
71
83
 
72
84
  internalServer =
73
85
  createStaticDevServer({
@@ -111,8 +123,10 @@ export function createMiddlewareDevServer(
111
123
 
112
124
  securityGateway =
113
125
  createSecurityProxy({
114
- port,
115
- hostname,
126
+ port:
127
+ securityPort,
128
+ hostname:
129
+ "127.0.0.1",
116
130
  upstreamPort:
117
131
  middlewarePort,
118
132
  upstreamHostname:
@@ -133,12 +147,48 @@ export function createMiddlewareDevServer(
133
147
  throw error;
134
148
  }
135
149
 
150
+ criticalCssGateway =
151
+ createCriticalCssProxy({
152
+ port,
153
+ hostname,
154
+ upstreamPort:
155
+ securityPort,
156
+ upstreamHostname:
157
+ "127.0.0.1",
158
+ cssFile:
159
+ path.join(
160
+ rootDirectory,
161
+ "public",
162
+ "bcp.css"
163
+ ),
164
+ });
165
+
166
+ try {
167
+ await criticalCssGateway.start();
168
+ } catch (error) {
169
+ await securityGateway.stop();
170
+ await middlewareGateway.stop();
171
+ await internalServer.stop();
172
+ internalServer =
173
+ null;
174
+ middlewareGateway =
175
+ null;
176
+ securityGateway =
177
+ null;
178
+ criticalCssGateway =
179
+ null;
180
+ throw error;
181
+ }
182
+
136
183
  started =
137
184
  true;
138
185
 
139
186
  console.log(
140
187
  `[BCP Security] Dev gateway: http://${hostname}:${port}`
141
188
  );
189
+ console.log(
190
+ `[BCP CSS] Inline /bcp.css when <= 8 KiB and allowed by CSP.`
191
+ );
142
192
  console.log(
143
193
  `[BCP Middleware] File: ${path.join(rootDirectory, "middleware.ts")} (optional; .tsx/.js/.jsx also supported)`
144
194
  );
@@ -146,6 +196,12 @@ export function createMiddlewareDevServer(
146
196
  }
147
197
 
148
198
  async function stop(): Promise<void> {
199
+ if (criticalCssGateway) {
200
+ await criticalCssGateway.stop();
201
+ criticalCssGateway =
202
+ null;
203
+ }
204
+
149
205
  if (securityGateway) {
150
206
  await securityGateway.stop();
151
207
  securityGateway =
@@ -257,4 +313,4 @@ async function findFreePort(): Promise<number> {
257
313
  );
258
314
  }
259
315
  }
260
- }
316
+ }