@utoo/pack 1.3.2-alpha.0 → 1.3.3-alpha.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/cjs/commands/build.js +7 -5
- package/cjs/commands/dev.js +1 -6
- package/cjs/core/hmr.js +4 -2
- package/cjs/utils/runtimePluginStratety.d.ts +1 -0
- package/cjs/utils/runtimePluginStratety.js +13 -0
- package/config_schema.json +280 -225
- package/esm/commands/build.js +7 -5
- package/esm/commands/dev.js +1 -6
- package/esm/core/hmr.js +4 -2
- package/esm/utils/runtimePluginStratety.d.ts +1 -0
- package/esm/utils/runtimePluginStratety.js +7 -0
- package/package.json +13 -12
- package/cjs/commands/dev-legacy.d.ts +0 -44
- package/cjs/commands/dev-legacy.js +0 -458
- package/cjs/core/proxy-hono.d.ts +0 -29
- package/cjs/core/proxy-hono.js +0 -96
- package/esm/commands/dev-legacy.d.ts +0 -44
- package/esm/commands/dev-legacy.js +0 -442
- package/esm/core/proxy-hono.d.ts +0 -29
- package/esm/core/proxy-hono.js +0 -89
|
@@ -1,458 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DetachedPromise = exports.ResponseAborted = exports.ResponseAbortedName = void 0;
|
|
7
|
-
exports.serve = serve;
|
|
8
|
-
exports.startServer = startServer;
|
|
9
|
-
exports.initialize = initialize;
|
|
10
|
-
exports.pipeToNodeResponse = pipeToNodeResponse;
|
|
11
|
-
exports.createAbortController = createAbortController;
|
|
12
|
-
exports.isAbortError = isAbortError;
|
|
13
|
-
exports.serveStatic = serveStatic;
|
|
14
|
-
exports.formatHostname = formatHostname;
|
|
15
|
-
const fs_1 = __importDefault(require("fs"));
|
|
16
|
-
const http_1 = __importDefault(require("http"));
|
|
17
|
-
const https_1 = __importDefault(require("https"));
|
|
18
|
-
const net_1 = require("net");
|
|
19
|
-
const path_1 = __importDefault(require("path"));
|
|
20
|
-
const send_1 = __importDefault(require("send"));
|
|
21
|
-
const url_1 = __importDefault(require("url"));
|
|
22
|
-
const webpackCompat_1 = require("../config/webpackCompat");
|
|
23
|
-
const hmr_1 = require("../core/hmr");
|
|
24
|
-
const common_1 = require("../utils/common");
|
|
25
|
-
const findRoot_1 = require("../utils/findRoot");
|
|
26
|
-
const mkcert_1 = require("../utils/mkcert");
|
|
27
|
-
const printServerInfo_1 = require("../utils/printServerInfo");
|
|
28
|
-
const xcodeProfile_1 = require("../utils/xcodeProfile");
|
|
29
|
-
function parsePath(pathStr) {
|
|
30
|
-
const hashIndex = pathStr.indexOf("#");
|
|
31
|
-
const queryIndex = pathStr.indexOf("?");
|
|
32
|
-
const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex);
|
|
33
|
-
if (hasQuery || hashIndex > -1) {
|
|
34
|
-
return {
|
|
35
|
-
pathname: pathStr.substring(0, hasQuery ? queryIndex : hashIndex),
|
|
36
|
-
query: hasQuery
|
|
37
|
-
? pathStr.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined)
|
|
38
|
-
: "",
|
|
39
|
-
hash: hashIndex > -1 ? pathStr.slice(hashIndex) : "",
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return { pathname: pathStr, query: "", hash: "" };
|
|
43
|
-
}
|
|
44
|
-
function pathHasPrefix(pathStr, prefix) {
|
|
45
|
-
if (typeof pathStr !== "string") {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
const { pathname } = parsePath(pathStr);
|
|
49
|
-
return pathname === prefix || pathname.startsWith(prefix + "/");
|
|
50
|
-
}
|
|
51
|
-
function removePathPrefix(pathStr, prefix) {
|
|
52
|
-
// If the path doesn't start with the prefix we can return it as is.
|
|
53
|
-
if (!pathHasPrefix(pathStr, prefix)) {
|
|
54
|
-
return pathStr;
|
|
55
|
-
}
|
|
56
|
-
// Remove the prefix from the path via slicing.
|
|
57
|
-
const withoutPrefix = pathStr.slice(prefix.length);
|
|
58
|
-
// If the path without the prefix starts with a `/` we can return it as is.
|
|
59
|
-
if (withoutPrefix.startsWith("/")) {
|
|
60
|
-
return withoutPrefix;
|
|
61
|
-
}
|
|
62
|
-
// If the path without the prefix doesn't start with a `/` we need to add it
|
|
63
|
-
// back to the path to make sure it's a valid path.
|
|
64
|
-
return `/${withoutPrefix}`;
|
|
65
|
-
}
|
|
66
|
-
function normalizedPublicPath(publicPath) {
|
|
67
|
-
const escapedPublicPath = (publicPath === null || publicPath === void 0 ? void 0 : publicPath.replace(/^\/+|\/+$/g, "")) || false;
|
|
68
|
-
if (!escapedPublicPath) {
|
|
69
|
-
return "";
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
if (URL.canParse(escapedPublicPath)) {
|
|
73
|
-
const url = new URL(escapedPublicPath).toString();
|
|
74
|
-
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch (_a) { }
|
|
78
|
-
return `/${escapedPublicPath}`;
|
|
79
|
-
}
|
|
80
|
-
function serve(options, projectPath, rootPath, serverOptions) {
|
|
81
|
-
const bundleOptions = (0, webpackCompat_1.resolveBundleOptions)(options, projectPath, rootPath);
|
|
82
|
-
if (!rootPath) {
|
|
83
|
-
rootPath = (0, findRoot_1.findRootDir)(projectPath || process.cwd());
|
|
84
|
-
}
|
|
85
|
-
return serveInternal(bundleOptions, projectPath, rootPath, serverOptions);
|
|
86
|
-
}
|
|
87
|
-
async function serveInternal(options, projectPath, rootPath, serverOptions) {
|
|
88
|
-
var _a;
|
|
89
|
-
(0, common_1.blockStdout)();
|
|
90
|
-
if (process.env.XCODE_PROFILE) {
|
|
91
|
-
await (0, xcodeProfile_1.xcodeProfilingReady)();
|
|
92
|
-
}
|
|
93
|
-
// FIXME: fix any type
|
|
94
|
-
const cfgDevServer = (((_a = options.config) === null || _a === void 0 ? void 0 : _a.devServer) || {});
|
|
95
|
-
const serverOpts = {
|
|
96
|
-
hostname: (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.hostname) || cfgDevServer.host || "localhost",
|
|
97
|
-
port: typeof (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.port) !== "undefined"
|
|
98
|
-
? serverOptions.port
|
|
99
|
-
: cfgDevServer.port || 3000,
|
|
100
|
-
https: typeof (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.https) !== "undefined"
|
|
101
|
-
? serverOptions.https
|
|
102
|
-
: cfgDevServer.https,
|
|
103
|
-
logServerInfo: serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.logServerInfo,
|
|
104
|
-
selfSignedCertificate: serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.selfSignedCertificate,
|
|
105
|
-
};
|
|
106
|
-
// If HTTPS is requested and no certificate provided, attempt to generate one.
|
|
107
|
-
if (serverOpts.https && !serverOpts.selfSignedCertificate) {
|
|
108
|
-
try {
|
|
109
|
-
// createSelfSignedCertificate may return undefined on failure
|
|
110
|
-
const cert = await (0, mkcert_1.createSelfSignedCertificate)(serverOpts.hostname);
|
|
111
|
-
if (cert)
|
|
112
|
-
serverOpts.selfSignedCertificate = cert;
|
|
113
|
-
}
|
|
114
|
-
catch (e) {
|
|
115
|
-
// ignore and fall back to http if certificate generation fails
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
await startServer(serverOpts, {
|
|
119
|
-
...options,
|
|
120
|
-
config: {
|
|
121
|
-
...options.config,
|
|
122
|
-
devServer: {
|
|
123
|
-
hot: true,
|
|
124
|
-
...(options.config.devServer || {}),
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
packPath: (0, common_1.getPackPath)(),
|
|
128
|
-
}, projectPath || process.cwd(), rootPath);
|
|
129
|
-
}
|
|
130
|
-
async function startServer(serverOptions, bundleOptions, projectPath, rootPath) {
|
|
131
|
-
let { port, hostname, selfSignedCertificate } = serverOptions;
|
|
132
|
-
process.title = "utoopack-dev-server";
|
|
133
|
-
let handlersReady = () => { };
|
|
134
|
-
let handlersError = () => { };
|
|
135
|
-
let handlersPromise = new Promise((resolve, reject) => {
|
|
136
|
-
handlersReady = resolve;
|
|
137
|
-
handlersError = reject;
|
|
138
|
-
});
|
|
139
|
-
let requestHandler = async (req, res) => {
|
|
140
|
-
if (handlersPromise) {
|
|
141
|
-
await handlersPromise;
|
|
142
|
-
return requestHandler(req, res);
|
|
143
|
-
}
|
|
144
|
-
throw new Error("Invariant request handler was not setup");
|
|
145
|
-
};
|
|
146
|
-
let upgradeHandler = async (req, socket, head) => {
|
|
147
|
-
if (handlersPromise) {
|
|
148
|
-
await handlersPromise;
|
|
149
|
-
return upgradeHandler(req, socket, head);
|
|
150
|
-
}
|
|
151
|
-
throw new Error("Invariant upgrade handler was not setup");
|
|
152
|
-
};
|
|
153
|
-
async function requestListener(req, res) {
|
|
154
|
-
try {
|
|
155
|
-
if (handlersPromise) {
|
|
156
|
-
await handlersPromise;
|
|
157
|
-
handlersPromise = undefined;
|
|
158
|
-
}
|
|
159
|
-
await requestHandler(req, res);
|
|
160
|
-
}
|
|
161
|
-
catch (err) {
|
|
162
|
-
res.statusCode = 500;
|
|
163
|
-
res.end("Internal Server Error");
|
|
164
|
-
console.error(`Failed to handle request for ${req.url}`);
|
|
165
|
-
console.error(err);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
const server = selfSignedCertificate
|
|
169
|
-
? https_1.default.createServer({
|
|
170
|
-
key: fs_1.default.readFileSync(selfSignedCertificate.key),
|
|
171
|
-
cert: fs_1.default.readFileSync(selfSignedCertificate.cert),
|
|
172
|
-
}, requestListener)
|
|
173
|
-
: http_1.default.createServer(requestListener);
|
|
174
|
-
server.on("upgrade", async (req, socket, head) => {
|
|
175
|
-
try {
|
|
176
|
-
await upgradeHandler(req, socket, head);
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
socket.destroy();
|
|
180
|
-
console.error(`Failed to handle request for ${req.url}`);
|
|
181
|
-
console.error(err);
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
let portRetryCount = 0;
|
|
185
|
-
const originalPort = port;
|
|
186
|
-
server.on("error", (err) => {
|
|
187
|
-
if (port && err.code === "EADDRINUSE" && portRetryCount < 10) {
|
|
188
|
-
port += 1;
|
|
189
|
-
portRetryCount += 1;
|
|
190
|
-
server.listen(port, hostname);
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
console.error(`Failed to start server`);
|
|
194
|
-
console.error(err);
|
|
195
|
-
process.exit(1);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
await new Promise((resolve) => {
|
|
199
|
-
server.on("listening", async () => {
|
|
200
|
-
const addr = server.address();
|
|
201
|
-
const actualHostname = formatHostname(typeof addr === "object"
|
|
202
|
-
? (addr === null || addr === void 0 ? void 0 : addr.address) || hostname || "localhost"
|
|
203
|
-
: addr);
|
|
204
|
-
const formattedHostname = !hostname || actualHostname === "0.0.0.0"
|
|
205
|
-
? "localhost"
|
|
206
|
-
: actualHostname === "[::]"
|
|
207
|
-
? "[::1]"
|
|
208
|
-
: formatHostname(hostname);
|
|
209
|
-
port = typeof addr === "object" ? (addr === null || addr === void 0 ? void 0 : addr.port) || port : port;
|
|
210
|
-
if (portRetryCount) {
|
|
211
|
-
console.warn(`Port ${originalPort} is in use, using available port ${port} instead.`);
|
|
212
|
-
}
|
|
213
|
-
if (serverOptions.logServerInfo !== false) {
|
|
214
|
-
(0, printServerInfo_1.printServerInfo)(serverOptions.https ? "https" : "http", formattedHostname, port);
|
|
215
|
-
}
|
|
216
|
-
try {
|
|
217
|
-
let cleanupStarted = false;
|
|
218
|
-
let closeUpgraded = null;
|
|
219
|
-
const cleanup = () => {
|
|
220
|
-
if (cleanupStarted) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
cleanupStarted = true;
|
|
224
|
-
(async () => {
|
|
225
|
-
console.debug("start-server process cleanup");
|
|
226
|
-
await new Promise((res) => {
|
|
227
|
-
server.close((err) => {
|
|
228
|
-
if (err)
|
|
229
|
-
console.error(err);
|
|
230
|
-
res();
|
|
231
|
-
});
|
|
232
|
-
server.closeAllConnections();
|
|
233
|
-
closeUpgraded === null || closeUpgraded === void 0 ? void 0 : closeUpgraded();
|
|
234
|
-
});
|
|
235
|
-
console.debug("start-server process cleanup finished");
|
|
236
|
-
process.exit(0);
|
|
237
|
-
})();
|
|
238
|
-
};
|
|
239
|
-
const exception = (err) => {
|
|
240
|
-
console.error(err);
|
|
241
|
-
};
|
|
242
|
-
process.on("SIGINT", cleanup);
|
|
243
|
-
process.on("SIGTERM", cleanup);
|
|
244
|
-
process.on("rejectionHandled", () => { });
|
|
245
|
-
process.on("uncaughtException", exception);
|
|
246
|
-
process.on("unhandledRejection", exception);
|
|
247
|
-
const initResult = await initialize(bundleOptions, projectPath, rootPath);
|
|
248
|
-
requestHandler = initResult.requestHandler;
|
|
249
|
-
upgradeHandler = initResult.upgradeHandler;
|
|
250
|
-
closeUpgraded = initResult.closeUpgraded;
|
|
251
|
-
handlersReady();
|
|
252
|
-
}
|
|
253
|
-
catch (err) {
|
|
254
|
-
handlersError();
|
|
255
|
-
console.error(err);
|
|
256
|
-
process.exit(1);
|
|
257
|
-
}
|
|
258
|
-
resolve();
|
|
259
|
-
});
|
|
260
|
-
server.listen(port, hostname);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
async function initialize(bundleOptions, projectPath, rootPath) {
|
|
264
|
-
process.env.NODE_ENV = "development";
|
|
265
|
-
const hotReloader = await (0, hmr_1.createHotReloader)(bundleOptions, projectPath, rootPath);
|
|
266
|
-
await hotReloader.start();
|
|
267
|
-
const requestHandlerImpl = async (req, res) => {
|
|
268
|
-
req.on("error", console.error);
|
|
269
|
-
res.on("error", console.error);
|
|
270
|
-
const handleRequest = async () => {
|
|
271
|
-
var _a, _b;
|
|
272
|
-
if (!(req.method === "GET" || req.method === "HEAD")) {
|
|
273
|
-
res.setHeader("Allow", ["GET", "HEAD"]);
|
|
274
|
-
res.statusCode = 405;
|
|
275
|
-
res.end();
|
|
276
|
-
}
|
|
277
|
-
const distRoot = path_1.default.resolve(projectPath, ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || "./dist");
|
|
278
|
-
const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
|
|
279
|
-
try {
|
|
280
|
-
const reqUrl = req.url || "";
|
|
281
|
-
let requestPath = url_1.default.parse(reqUrl).pathname || "";
|
|
282
|
-
if (publicPath && publicPath !== "runtime") {
|
|
283
|
-
const normalizedPrefix = normalizedPublicPath(publicPath);
|
|
284
|
-
const isAbsoluteUrl = normalizedPrefix.startsWith("http://") ||
|
|
285
|
-
normalizedPrefix.startsWith("https://");
|
|
286
|
-
if (!isAbsoluteUrl && normalizedPrefix) {
|
|
287
|
-
if (pathHasPrefix(requestPath, normalizedPrefix)) {
|
|
288
|
-
requestPath = removePathPrefix(requestPath, normalizedPrefix);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return await serveStatic(req, res, requestPath, { root: distRoot });
|
|
293
|
-
}
|
|
294
|
-
catch (err) {
|
|
295
|
-
res.setHeader("Cache-Control", "private, no-cache, no-store, max-age=0, must-revalidate");
|
|
296
|
-
res.statusCode = 404;
|
|
297
|
-
res.end();
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
try {
|
|
301
|
-
await handleRequest();
|
|
302
|
-
}
|
|
303
|
-
catch (err) {
|
|
304
|
-
res.statusCode = 500;
|
|
305
|
-
res.end("Internal Server Error");
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
let requestHandler = requestHandlerImpl;
|
|
309
|
-
const logError = async (type, err) => {
|
|
310
|
-
if (type === "unhandledRejection") {
|
|
311
|
-
console.error("unhandledRejection: ", err);
|
|
312
|
-
}
|
|
313
|
-
else if (type === "uncaughtException") {
|
|
314
|
-
console.error("uncaughtException: ", err);
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
process.on("uncaughtException", logError.bind(null, "uncaughtException"));
|
|
318
|
-
process.on("unhandledRejection", logError.bind(null, "unhandledRejection"));
|
|
319
|
-
const upgradeHandler = async (req, socket, head) => {
|
|
320
|
-
var _a;
|
|
321
|
-
try {
|
|
322
|
-
const isHMRRequest = (_a = req.url) === null || _a === void 0 ? void 0 : _a.includes("turbopack-hmr");
|
|
323
|
-
if (isHMRRequest) {
|
|
324
|
-
hotReloader.onHMR(req, socket, head);
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
socket.end();
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
catch (err) {
|
|
331
|
-
console.error("Error handling upgrade request", err);
|
|
332
|
-
socket.end();
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
return {
|
|
336
|
-
requestHandler,
|
|
337
|
-
upgradeHandler,
|
|
338
|
-
closeUpgraded() {
|
|
339
|
-
hotReloader.close();
|
|
340
|
-
},
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
async function pipeToNodeResponse(readable, res, waitUntilForEnd) {
|
|
344
|
-
try {
|
|
345
|
-
const { errored, destroyed } = res;
|
|
346
|
-
if (errored || destroyed)
|
|
347
|
-
return;
|
|
348
|
-
const controller = createAbortController(res);
|
|
349
|
-
const writer = createWriterFromResponse(res, waitUntilForEnd);
|
|
350
|
-
await readable.pipeTo(writer, { signal: controller.signal });
|
|
351
|
-
}
|
|
352
|
-
catch (err) {
|
|
353
|
-
if (isAbortError(err))
|
|
354
|
-
return;
|
|
355
|
-
throw new Error("failed to pipe response", { cause: err });
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
function createAbortController(response) {
|
|
359
|
-
const controller = new AbortController();
|
|
360
|
-
response.once("close", () => {
|
|
361
|
-
if (response.writableFinished)
|
|
362
|
-
return;
|
|
363
|
-
controller.abort(new ResponseAborted());
|
|
364
|
-
});
|
|
365
|
-
return controller;
|
|
366
|
-
}
|
|
367
|
-
function isAbortError(e) {
|
|
368
|
-
return (e === null || e === void 0 ? void 0 : e.name) === "AbortError" || (e === null || e === void 0 ? void 0 : e.name) === exports.ResponseAbortedName;
|
|
369
|
-
}
|
|
370
|
-
exports.ResponseAbortedName = "ResponseAborted";
|
|
371
|
-
class ResponseAborted extends Error {
|
|
372
|
-
constructor() {
|
|
373
|
-
super(...arguments);
|
|
374
|
-
this.name = exports.ResponseAbortedName;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
exports.ResponseAborted = ResponseAborted;
|
|
378
|
-
function createWriterFromResponse(res, waitUntilForEnd) {
|
|
379
|
-
let started = false;
|
|
380
|
-
let drained = new DetachedPromise();
|
|
381
|
-
function onDrain() {
|
|
382
|
-
drained.resolve();
|
|
383
|
-
}
|
|
384
|
-
res.on("drain", onDrain);
|
|
385
|
-
res.once("close", () => {
|
|
386
|
-
res.off("drain", onDrain);
|
|
387
|
-
drained.resolve();
|
|
388
|
-
});
|
|
389
|
-
const finished = new DetachedPromise();
|
|
390
|
-
res.once("finish", () => {
|
|
391
|
-
finished.resolve();
|
|
392
|
-
});
|
|
393
|
-
return new WritableStream({
|
|
394
|
-
write: async (chunk) => {
|
|
395
|
-
if (!started) {
|
|
396
|
-
started = true;
|
|
397
|
-
res.flushHeaders();
|
|
398
|
-
}
|
|
399
|
-
try {
|
|
400
|
-
const ok = res.write(chunk);
|
|
401
|
-
if ("flush" in res && typeof res.flush === "function") {
|
|
402
|
-
res.flush();
|
|
403
|
-
}
|
|
404
|
-
if (!ok) {
|
|
405
|
-
await drained.promise;
|
|
406
|
-
drained = new DetachedPromise();
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
catch (err) {
|
|
410
|
-
res.end();
|
|
411
|
-
throw new Error("failed to write chunk to response", { cause: err });
|
|
412
|
-
}
|
|
413
|
-
},
|
|
414
|
-
abort: (err) => {
|
|
415
|
-
if (res.writableFinished)
|
|
416
|
-
return;
|
|
417
|
-
res.destroy(err);
|
|
418
|
-
},
|
|
419
|
-
close: async () => {
|
|
420
|
-
if (waitUntilForEnd) {
|
|
421
|
-
await waitUntilForEnd;
|
|
422
|
-
}
|
|
423
|
-
if (res.writableFinished)
|
|
424
|
-
return;
|
|
425
|
-
res.end();
|
|
426
|
-
return finished.promise;
|
|
427
|
-
},
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
class DetachedPromise {
|
|
431
|
-
constructor() {
|
|
432
|
-
let resolve;
|
|
433
|
-
let reject;
|
|
434
|
-
this.promise = new Promise((res, rej) => {
|
|
435
|
-
resolve = res;
|
|
436
|
-
reject = rej;
|
|
437
|
-
});
|
|
438
|
-
this.resolve = resolve;
|
|
439
|
-
this.reject = reject;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
exports.DetachedPromise = DetachedPromise;
|
|
443
|
-
function serveStatic(req, res, path, opts) {
|
|
444
|
-
return new Promise((resolve, reject) => {
|
|
445
|
-
(0, send_1.default)(req, path, opts)
|
|
446
|
-
.on("directory", () => {
|
|
447
|
-
const err = new Error("No directory access");
|
|
448
|
-
err.code = "ENOENT";
|
|
449
|
-
reject(err);
|
|
450
|
-
})
|
|
451
|
-
.on("error", reject)
|
|
452
|
-
.pipe(res)
|
|
453
|
-
.on("finish", resolve);
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
function formatHostname(hostname) {
|
|
457
|
-
return (0, net_1.isIPv6)(hostname) ? `[${hostname}]` : hostname;
|
|
458
|
-
}
|
package/cjs/core/proxy-hono.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP proxy middleware for Hono dev server.
|
|
3
|
-
* Matches ProxyRule by path, applies pathRewrite, and forwards via Hono proxy().
|
|
4
|
-
* No generic WS proxying in this module.
|
|
5
|
-
*/
|
|
6
|
-
import type { DevServerProxy, PathRewrite, ProxyRule } from "@utoo/pack-shared";
|
|
7
|
-
import type { Context, Next } from "hono";
|
|
8
|
-
/** Path prefix or ^-prefixed regex match (regex tests full path). */
|
|
9
|
-
export declare function doesContextMatchUrl(context: string, pathOrUrl: string): boolean;
|
|
10
|
-
export declare function ruleMatchesUrl(rule: ProxyRule, pathOrUrl: string): boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Apply pathRewrite (http-proxy-middleware style).
|
|
13
|
-
* Object: first matching regex → replace, then stop. Function: (path) => new path.
|
|
14
|
-
*/
|
|
15
|
-
export declare function applyPathRewrite(path: string, pathRewrite?: PathRewrite): string;
|
|
16
|
-
export interface PreparedProxyRequest {
|
|
17
|
-
url: URL;
|
|
18
|
-
headers: Headers;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Build target URL and headers for the proxy request from current context and rule.
|
|
22
|
-
*/
|
|
23
|
-
export declare function prepareProxyRequest(c: Context, rule: ProxyRule): PreparedProxyRequest;
|
|
24
|
-
/**
|
|
25
|
-
* Hono middleware: on match, proxy to rule.target with pathRewrite and changeOrigin;
|
|
26
|
-
* on error return 502; otherwise next().
|
|
27
|
-
* Caller (e.g. dev.ts) must pass a non-empty array; empty array means no rule matches.
|
|
28
|
-
*/
|
|
29
|
-
export declare function createHttpProxyMiddleware(rules: DevServerProxy): (c: Context, next: Next) => Promise<void | Response>;
|
package/cjs/core/proxy-hono.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP proxy middleware for Hono dev server.
|
|
4
|
-
* Matches ProxyRule by path, applies pathRewrite, and forwards via Hono proxy().
|
|
5
|
-
* No generic WS proxying in this module.
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.doesContextMatchUrl = doesContextMatchUrl;
|
|
9
|
-
exports.ruleMatchesUrl = ruleMatchesUrl;
|
|
10
|
-
exports.applyPathRewrite = applyPathRewrite;
|
|
11
|
-
exports.prepareProxyRequest = prepareProxyRequest;
|
|
12
|
-
exports.createHttpProxyMiddleware = createHttpProxyMiddleware;
|
|
13
|
-
const proxy_1 = require("hono/proxy");
|
|
14
|
-
/** Path prefix or ^-prefixed regex match (regex tests full path). */
|
|
15
|
-
function doesContextMatchUrl(context, pathOrUrl) {
|
|
16
|
-
if (context[0] === "^") {
|
|
17
|
-
try {
|
|
18
|
-
return new RegExp(context).test(pathOrUrl);
|
|
19
|
-
}
|
|
20
|
-
catch (_a) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return pathOrUrl === context || pathOrUrl.startsWith(context + "/");
|
|
25
|
-
}
|
|
26
|
-
function ruleMatchesUrl(rule, pathOrUrl) {
|
|
27
|
-
const contexts = Array.isArray(rule.context) ? rule.context : [rule.context];
|
|
28
|
-
return contexts.some((ctx) => doesContextMatchUrl(ctx, pathOrUrl));
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Apply pathRewrite (http-proxy-middleware style).
|
|
32
|
-
* Object: first matching regex → replace, then stop. Function: (path) => new path.
|
|
33
|
-
*/
|
|
34
|
-
function applyPathRewrite(path, pathRewrite) {
|
|
35
|
-
if (!pathRewrite)
|
|
36
|
-
return path;
|
|
37
|
-
if (typeof pathRewrite === "function") {
|
|
38
|
-
return pathRewrite(path);
|
|
39
|
-
}
|
|
40
|
-
for (const [pattern, value] of Object.entries(pathRewrite)) {
|
|
41
|
-
try {
|
|
42
|
-
const regex = new RegExp(pattern);
|
|
43
|
-
if (regex.test(path)) {
|
|
44
|
-
return path.replace(regex, value);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch (_a) {
|
|
48
|
-
// invalid regex, skip
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return path;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Build target URL and headers for the proxy request from current context and rule.
|
|
55
|
-
*/
|
|
56
|
-
function prepareProxyRequest(c, rule) {
|
|
57
|
-
const origUrl = new URL(c.req.url);
|
|
58
|
-
const targetUrl = new URL(rule.target);
|
|
59
|
-
const origPath = c.req.path;
|
|
60
|
-
const rewrittenPath = applyPathRewrite(origPath, rule.pathRewrite);
|
|
61
|
-
targetUrl.pathname = rewrittenPath;
|
|
62
|
-
targetUrl.search = origUrl.search;
|
|
63
|
-
const headers = new Headers(c.req.header());
|
|
64
|
-
if (rule.changeOrigin !== false) {
|
|
65
|
-
headers.set("host", targetUrl.host);
|
|
66
|
-
}
|
|
67
|
-
return { url: targetUrl, headers };
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Hono middleware: on match, proxy to rule.target with pathRewrite and changeOrigin;
|
|
71
|
-
* on error return 502; otherwise next().
|
|
72
|
-
* Caller (e.g. dev.ts) must pass a non-empty array; empty array means no rule matches.
|
|
73
|
-
*/
|
|
74
|
-
function createHttpProxyMiddleware(rules) {
|
|
75
|
-
return async (c, next) => {
|
|
76
|
-
const rule = rules.find((r) => ruleMatchesUrl(r, c.req.path));
|
|
77
|
-
if (!rule) {
|
|
78
|
-
return next();
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
const prepared = prepareProxyRequest(c, rule);
|
|
82
|
-
const init = {
|
|
83
|
-
method: c.req.method,
|
|
84
|
-
headers: prepared.headers,
|
|
85
|
-
};
|
|
86
|
-
if (c.req.method !== "GET" && c.req.method !== "HEAD") {
|
|
87
|
-
init.body = c.req.raw.body;
|
|
88
|
-
}
|
|
89
|
-
return (0, proxy_1.proxy)(prepared.url.toString(), init);
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
console.error("[dev proxy] error", c.req.method, c.req.url, err);
|
|
93
|
-
return c.text("Bad Gateway", 502);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, ServerResponse } from "http";
|
|
2
|
-
import send from "send";
|
|
3
|
-
import { Duplex, Writable } from "stream";
|
|
4
|
-
import { BundleOptions } from "../config/types";
|
|
5
|
-
import { WebpackConfig } from "../config/webpackCompat";
|
|
6
|
-
export declare function serve(options: BundleOptions | WebpackConfig, projectPath?: string, rootPath?: string, serverOptions?: StartServerOptions): Promise<void>;
|
|
7
|
-
export interface SelfSignedCertificate {
|
|
8
|
-
key: string;
|
|
9
|
-
cert: string;
|
|
10
|
-
rootCA?: string;
|
|
11
|
-
}
|
|
12
|
-
export interface StartServerOptions {
|
|
13
|
-
port: number;
|
|
14
|
-
https?: boolean;
|
|
15
|
-
hostname?: string;
|
|
16
|
-
logServerInfo?: boolean;
|
|
17
|
-
selfSignedCertificate?: SelfSignedCertificate;
|
|
18
|
-
}
|
|
19
|
-
export type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
20
|
-
export type UpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => Promise<void>;
|
|
21
|
-
export type ServerInitResult = {
|
|
22
|
-
requestHandler: RequestHandler;
|
|
23
|
-
upgradeHandler: UpgradeHandler;
|
|
24
|
-
closeUpgraded: () => void;
|
|
25
|
-
};
|
|
26
|
-
export declare function startServer(serverOptions: StartServerOptions, bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<void>;
|
|
27
|
-
export declare function initialize(bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<ServerInitResult>;
|
|
28
|
-
export declare function pipeToNodeResponse(readable: ReadableStream<Uint8Array>, res: ServerResponse, waitUntilForEnd?: Promise<unknown>): Promise<void>;
|
|
29
|
-
export declare function createAbortController(response: Writable): AbortController;
|
|
30
|
-
export declare function isAbortError(e: any): e is Error & {
|
|
31
|
-
name: "AbortError";
|
|
32
|
-
};
|
|
33
|
-
export declare const ResponseAbortedName = "ResponseAborted";
|
|
34
|
-
export declare class ResponseAborted extends Error {
|
|
35
|
-
readonly name = "ResponseAborted";
|
|
36
|
-
}
|
|
37
|
-
export declare class DetachedPromise<T = any> {
|
|
38
|
-
readonly resolve: (value: T | PromiseLike<T>) => void;
|
|
39
|
-
readonly reject: (reason: any) => void;
|
|
40
|
-
readonly promise: Promise<T>;
|
|
41
|
-
constructor();
|
|
42
|
-
}
|
|
43
|
-
export declare function serveStatic(req: IncomingMessage, res: ServerResponse, path: string, opts?: Parameters<typeof send>[2]): Promise<void>;
|
|
44
|
-
export declare function formatHostname(hostname: string): string;
|