@chidchanun/bcp 0.1.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.
- package/CHANGELOG.md +49 -0
- package/LICENSE +21 -0
- package/README.md +241 -0
- package/docs/caching.md +76 -0
- package/docs/configuration.md +97 -0
- package/docs/deployment.md +74 -0
- package/docs/getting-started.md +82 -0
- package/docs/middleware.md +58 -0
- package/docs/releasing.md +299 -0
- package/docs/routing.md +103 -0
- package/docs/security.md +57 -0
- package/package.json +68 -0
- package/packages/bundler/src/client-islands.ts +1457 -0
- package/packages/bundler/src/incremental-context.ts +206 -0
- package/packages/bundler/src/index.ts +1991 -0
- package/packages/bundler/src/module-graph.ts +317 -0
- package/packages/bundler/src/partial-hydration.ts +414 -0
- package/packages/bundler/src/production.ts +974 -0
- package/packages/bundler/src/server-production-middleware.ts +447 -0
- package/packages/bundler/src/server-production.ts +1193 -0
- package/packages/bundler/src/special-files.ts +131 -0
- package/packages/cache/src/index.ts +761 -0
- package/packages/cli/bin/bcp.mjs +93 -0
- package/packages/cli/src/args.ts +305 -0
- package/packages/cli/src/bootstrap.ts +514 -0
- package/packages/cli/src/index.ts +504 -0
- package/packages/cli/src/version.ts +45 -0
- package/packages/client/src/cache.ts +11 -0
- package/packages/client/src/config.ts +18 -0
- package/packages/client/src/error-boundary.tsx +149 -0
- package/packages/client/src/hydration.ts +3 -0
- package/packages/client/src/index.tsx +57 -0
- package/packages/client/src/islands.tsx +315 -0
- package/packages/client/src/metadata.ts +281 -0
- package/packages/client/src/navigation-loading.ts +52 -0
- package/packages/client/src/navigation-state.ts +80 -0
- package/packages/client/src/not-found.ts +34 -0
- package/packages/client/src/persistent-layout-runtime.ts +273 -0
- package/packages/client/src/router-v2.tsx +969 -0
- package/packages/client/src/router.tsx +1 -0
- package/packages/config/src/index.ts +1038 -0
- package/packages/env/src/index.ts +593 -0
- package/packages/router/src/advanced-router.ts +1032 -0
- package/packages/router/src/index.ts +1 -0
- package/packages/server/src/compression.ts +249 -0
- package/packages/server/src/dev-document-metadata.ts +154 -0
- package/packages/server/src/dev-hmr.ts +225 -0
- package/packages/server/src/index.ts +2265 -0
- package/packages/server/src/metadata.ts +478 -0
- package/packages/server/src/middleware-dev-server.ts +260 -0
- package/packages/server/src/middleware-loader.ts +140 -0
- package/packages/server/src/middleware-proxy.ts +516 -0
- package/packages/server/src/middleware.ts +704 -0
- package/packages/server/src/navigation-payload.ts +471 -0
- package/packages/server/src/production-server.ts +1746 -0
- package/packages/server/src/response-cache-proxy.ts +828 -0
- package/packages/server/src/security-proxy.ts +406 -0
- package/packages/server/src/security.ts +451 -0
- package/packages/server/src/standalone-production-runtime-v2.ts +2047 -0
- package/packages/server/src/standalone-production-runtime-v3.ts +250 -0
- package/packages/server/src/standalone-production-runtime-v4.ts +289 -0
- package/packages/server/src/standalone-production-runtime-v5.ts +289 -0
- package/packages/server/src/standalone-production-runtime.ts +1951 -0
- package/packages/server/src/standalone-production-server.ts +6 -0
- package/packages/server/src/static-assets.ts +262 -0
- package/packages/server/src/static-dev-server.ts +854 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export interface StaticAsset {
|
|
6
|
+
filePath: string;
|
|
7
|
+
|
|
8
|
+
content: Buffer;
|
|
9
|
+
|
|
10
|
+
contentType: string;
|
|
11
|
+
|
|
12
|
+
etag: string;
|
|
13
|
+
|
|
14
|
+
lastModified: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Read a file from the application's `public/` directory.
|
|
19
|
+
*
|
|
20
|
+
* The resolver intentionally works from the filesystem root
|
|
21
|
+
* instead of trusting the URL path. It also resolves symlinks
|
|
22
|
+
* and verifies that the final file remains inside `public/`.
|
|
23
|
+
*/
|
|
24
|
+
export async function readPublicAsset(
|
|
25
|
+
rootDirectory: string,
|
|
26
|
+
pathname: string
|
|
27
|
+
): Promise<StaticAsset | null> {
|
|
28
|
+
if (
|
|
29
|
+
!pathname.startsWith("/") ||
|
|
30
|
+
pathname === "/" ||
|
|
31
|
+
pathname.startsWith("/_bcp/") ||
|
|
32
|
+
pathname === "/_bcp"
|
|
33
|
+
) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let decodedPath: string;
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
decodedPath =
|
|
41
|
+
decodeURIComponent(pathname);
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (
|
|
47
|
+
decodedPath.includes("\0")
|
|
48
|
+
) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const publicDirectory =
|
|
53
|
+
path.resolve(
|
|
54
|
+
rootDirectory,
|
|
55
|
+
"public"
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const relativePath =
|
|
59
|
+
decodedPath
|
|
60
|
+
.replace(/^\/+/, "")
|
|
61
|
+
.replaceAll("/", path.sep);
|
|
62
|
+
|
|
63
|
+
if (!relativePath) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const candidatePath =
|
|
68
|
+
path.resolve(
|
|
69
|
+
publicDirectory,
|
|
70
|
+
relativePath
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
!isPathInside(
|
|
75
|
+
publicDirectory,
|
|
76
|
+
candidatePath
|
|
77
|
+
)
|
|
78
|
+
) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let realPublicDirectory: string;
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
realPublicDirectory =
|
|
86
|
+
await fs.realpath(
|
|
87
|
+
publicDirectory
|
|
88
|
+
);
|
|
89
|
+
} catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let realFilePath: string;
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
realFilePath =
|
|
97
|
+
await fs.realpath(
|
|
98
|
+
candidatePath
|
|
99
|
+
);
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (
|
|
105
|
+
!isPathInside(
|
|
106
|
+
realPublicDirectory,
|
|
107
|
+
realFilePath
|
|
108
|
+
)
|
|
109
|
+
) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let stat: Awaited<
|
|
114
|
+
ReturnType<typeof fs.stat>
|
|
115
|
+
>;
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
stat =
|
|
119
|
+
await fs.stat(
|
|
120
|
+
realFilePath
|
|
121
|
+
);
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!stat.isFile()) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const content =
|
|
131
|
+
await fs.readFile(
|
|
132
|
+
realFilePath
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const etag =
|
|
136
|
+
crypto
|
|
137
|
+
.createHash("sha1")
|
|
138
|
+
.update(content)
|
|
139
|
+
.digest("hex");
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
filePath: realFilePath,
|
|
143
|
+
|
|
144
|
+
content,
|
|
145
|
+
|
|
146
|
+
contentType:
|
|
147
|
+
getContentType(
|
|
148
|
+
realFilePath
|
|
149
|
+
),
|
|
150
|
+
|
|
151
|
+
etag: `"${etag}"`,
|
|
152
|
+
|
|
153
|
+
lastModified:
|
|
154
|
+
stat.mtime.toUTCString(),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function getContentType(
|
|
159
|
+
filePath: string
|
|
160
|
+
): string {
|
|
161
|
+
const extension =
|
|
162
|
+
path.extname(
|
|
163
|
+
filePath
|
|
164
|
+
).toLowerCase();
|
|
165
|
+
|
|
166
|
+
const contentTypes: Record<
|
|
167
|
+
string,
|
|
168
|
+
string
|
|
169
|
+
> = {
|
|
170
|
+
".html":
|
|
171
|
+
"text/html; charset=utf-8",
|
|
172
|
+
|
|
173
|
+
".htm":
|
|
174
|
+
"text/html; charset=utf-8",
|
|
175
|
+
|
|
176
|
+
".css":
|
|
177
|
+
"text/css; charset=utf-8",
|
|
178
|
+
|
|
179
|
+
".js":
|
|
180
|
+
"text/javascript; charset=utf-8",
|
|
181
|
+
|
|
182
|
+
".mjs":
|
|
183
|
+
"text/javascript; charset=utf-8",
|
|
184
|
+
|
|
185
|
+
".json":
|
|
186
|
+
"application/json; charset=utf-8",
|
|
187
|
+
|
|
188
|
+
".txt":
|
|
189
|
+
"text/plain; charset=utf-8",
|
|
190
|
+
|
|
191
|
+
".xml":
|
|
192
|
+
"application/xml; charset=utf-8",
|
|
193
|
+
|
|
194
|
+
".csv":
|
|
195
|
+
"text/csv; charset=utf-8",
|
|
196
|
+
|
|
197
|
+
".svg":
|
|
198
|
+
"image/svg+xml",
|
|
199
|
+
|
|
200
|
+
".ico":
|
|
201
|
+
"image/x-icon",
|
|
202
|
+
|
|
203
|
+
".png":
|
|
204
|
+
"image/png",
|
|
205
|
+
|
|
206
|
+
".jpg":
|
|
207
|
+
"image/jpeg",
|
|
208
|
+
|
|
209
|
+
".jpeg":
|
|
210
|
+
"image/jpeg",
|
|
211
|
+
|
|
212
|
+
".gif":
|
|
213
|
+
"image/gif",
|
|
214
|
+
|
|
215
|
+
".webp":
|
|
216
|
+
"image/webp",
|
|
217
|
+
|
|
218
|
+
".avif":
|
|
219
|
+
"image/avif",
|
|
220
|
+
|
|
221
|
+
".bmp":
|
|
222
|
+
"image/bmp",
|
|
223
|
+
|
|
224
|
+
".wasm":
|
|
225
|
+
"application/wasm",
|
|
226
|
+
|
|
227
|
+
".pdf":
|
|
228
|
+
"application/pdf",
|
|
229
|
+
|
|
230
|
+
".webmanifest":
|
|
231
|
+
"application/manifest+json",
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
contentTypes[extension] ??
|
|
236
|
+
"application/octet-stream"
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function isPathInside(
|
|
241
|
+
parent: string,
|
|
242
|
+
child: string
|
|
243
|
+
): boolean {
|
|
244
|
+
const relative =
|
|
245
|
+
path.relative(
|
|
246
|
+
parent,
|
|
247
|
+
child
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
relative === "" ||
|
|
252
|
+
(
|
|
253
|
+
relative !== ".." &&
|
|
254
|
+
!relative.startsWith(
|
|
255
|
+
`..${path.sep}`
|
|
256
|
+
) &&
|
|
257
|
+
!path.isAbsolute(
|
|
258
|
+
relative
|
|
259
|
+
)
|
|
260
|
+
)
|
|
261
|
+
);
|
|
262
|
+
}
|