@chidchanun/bcp 0.1.6 → 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.
- package/CHANGELOG.md +31 -1
- package/README.md +77 -1
- package/docs/server-request-apis.md +404 -0
- package/package.json +6 -1
- package/packages/bundler/src/client-boundary.ts +8 -6
- package/packages/bundler/src/server-production.ts +17 -0
- package/packages/client/src/server.ts +23 -0
- package/packages/server/src/index.ts +32 -13
- package/packages/server/src/request-context.ts +1043 -0
- package/packages/server/src/server-response.ts +82 -0
- package/packages/server/src/standalone-production-runtime-v2.ts +44 -13
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
requestUrl,
|
|
3
|
+
} from "./request-context.js";
|
|
4
|
+
|
|
5
|
+
export type RedirectStatus =
|
|
6
|
+
| 301
|
|
7
|
+
| 302
|
|
8
|
+
| 303
|
|
9
|
+
| 307
|
|
10
|
+
| 308;
|
|
11
|
+
|
|
12
|
+
export function json<T>(
|
|
13
|
+
data: T,
|
|
14
|
+
init?: ResponseInit
|
|
15
|
+
): Response {
|
|
16
|
+
return Response.json(
|
|
17
|
+
data,
|
|
18
|
+
init
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function redirect(
|
|
23
|
+
destination:
|
|
24
|
+
string |
|
|
25
|
+
URL,
|
|
26
|
+
status:
|
|
27
|
+
RedirectStatus =
|
|
28
|
+
307
|
|
29
|
+
): Promise<Response> {
|
|
30
|
+
assertRedirectStatus(
|
|
31
|
+
status
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const base =
|
|
35
|
+
await requestUrl();
|
|
36
|
+
|
|
37
|
+
const target =
|
|
38
|
+
destination instanceof URL
|
|
39
|
+
? new URL(
|
|
40
|
+
destination.href
|
|
41
|
+
)
|
|
42
|
+
: new URL(
|
|
43
|
+
destination,
|
|
44
|
+
base
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
if (
|
|
48
|
+
target.protocol !== "http:" &&
|
|
49
|
+
target.protocol !== "https:"
|
|
50
|
+
) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`BCP Framework: redirect only supports http and https URLs, received "${target.protocol}".`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return new Response(
|
|
57
|
+
null,
|
|
58
|
+
{
|
|
59
|
+
status,
|
|
60
|
+
headers: {
|
|
61
|
+
Location:
|
|
62
|
+
target.href,
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function assertRedirectStatus(
|
|
69
|
+
status: number
|
|
70
|
+
): asserts status is RedirectStatus {
|
|
71
|
+
if (
|
|
72
|
+
status !== 301 &&
|
|
73
|
+
status !== 302 &&
|
|
74
|
+
status !== 303 &&
|
|
75
|
+
status !== 307 &&
|
|
76
|
+
status !== 308
|
|
77
|
+
) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`BCP Framework: invalid redirect status ${status}. Use 301, 302, 303, 307 or 308.`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -49,6 +49,11 @@ import {
|
|
|
49
49
|
type MetadataSource,
|
|
50
50
|
} from "./metadata.js";
|
|
51
51
|
|
|
52
|
+
import {
|
|
53
|
+
applyResponseCookies,
|
|
54
|
+
runWithRequestContext,
|
|
55
|
+
} from "./request-context.ts";
|
|
56
|
+
|
|
52
57
|
import {
|
|
53
58
|
getContentType,
|
|
54
59
|
readPublicAsset,
|
|
@@ -1405,7 +1410,8 @@ async function handleApiRoute(
|
|
|
1405
1410
|
|
|
1406
1411
|
if (
|
|
1407
1412
|
!handler &&
|
|
1408
|
-
method ===
|
|
1413
|
+
method ===
|
|
1414
|
+
"HEAD"
|
|
1409
1415
|
) {
|
|
1410
1416
|
handler =
|
|
1411
1417
|
getApiHandler(
|
|
@@ -1434,23 +1440,48 @@ async function handleApiRoute(
|
|
|
1434
1440
|
return;
|
|
1435
1441
|
}
|
|
1436
1442
|
|
|
1443
|
+
const request =
|
|
1444
|
+
await createWebRequest(
|
|
1445
|
+
req,
|
|
1446
|
+
requestUrl
|
|
1447
|
+
);
|
|
1448
|
+
|
|
1437
1449
|
const response =
|
|
1438
|
-
await
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1450
|
+
await runWithRequestContext(
|
|
1451
|
+
request,
|
|
1452
|
+
|
|
1453
|
+
async () => {
|
|
1454
|
+
const result =
|
|
1455
|
+
await handler(
|
|
1456
|
+
request,
|
|
1457
|
+
{
|
|
1458
|
+
params,
|
|
1459
|
+
}
|
|
1460
|
+
);
|
|
1461
|
+
|
|
1462
|
+
if (
|
|
1463
|
+
!(
|
|
1464
|
+
result instanceof
|
|
1465
|
+
Response
|
|
1466
|
+
)
|
|
1467
|
+
) {
|
|
1468
|
+
throw new Error(
|
|
1469
|
+
`API handler "${method} ${definition.pathname}" must return a Response object.`
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
return applyResponseCookies(
|
|
1474
|
+
result
|
|
1475
|
+
);
|
|
1476
|
+
},
|
|
1443
1477
|
{
|
|
1444
|
-
|
|
1478
|
+
remoteAddress:
|
|
1479
|
+
req.socket
|
|
1480
|
+
.remoteAddress ??
|
|
1481
|
+
null,
|
|
1445
1482
|
}
|
|
1446
1483
|
);
|
|
1447
1484
|
|
|
1448
|
-
if (!(response instanceof Response)) {
|
|
1449
|
-
throw new Error(
|
|
1450
|
-
`API handler "${method} ${definition.pathname}" must return a Response object.`
|
|
1451
|
-
);
|
|
1452
|
-
}
|
|
1453
|
-
|
|
1454
1485
|
await sendWebResponse(
|
|
1455
1486
|
req,
|
|
1456
1487
|
res,
|