@utoo/pack 0.0.1-alpha.18 → 0.0.1-alpha.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/cjs/binding.d.ts +9 -11
- package/cjs/build.d.ts +2 -4
- package/cjs/build.js +11 -18
- package/cjs/{hmr.d.ts → hotReloader.d.ts} +11 -4
- package/cjs/{hmr.js → hotReloader.js} +61 -14
- package/cjs/index.d.ts +14 -13
- package/cjs/index.js +4 -6
- package/cjs/project.d.ts +4 -4
- package/cjs/project.js +2 -0
- package/cjs/sourceMap.d.ts +21 -0
- package/cjs/sourceMap.js +2 -0
- package/cjs/types.d.ts +9 -22
- package/cjs/util.d.ts +1 -1
- package/cjs/util.js +1 -1
- package/cjs/watch.d.ts +2 -0
- package/cjs/watch.js +14 -0
- package/esm/binding.d.ts +9 -11
- package/esm/build.d.ts +2 -4
- package/esm/build.js +11 -18
- package/esm/{hmr.d.ts → hotReloader.d.ts} +11 -4
- package/esm/{hmr.js → hotReloader.js} +60 -14
- package/esm/index.d.ts +14 -13
- package/esm/index.js +2 -6
- package/esm/project.d.ts +4 -4
- package/esm/project.js +2 -0
- package/esm/sourceMap.d.ts +21 -0
- package/esm/sourceMap.js +1 -0
- package/esm/types.d.ts +9 -22
- package/esm/util.d.ts +1 -1
- package/esm/util.js +1 -1
- package/esm/watch.d.ts +2 -0
- package/esm/watch.js +11 -0
- package/package.json +10 -15
- package/cjs/dev.d.ts +0 -44
- package/cjs/dev.js +0 -362
- package/cjs/mkcert.d.ts +0 -7
- package/cjs/mkcert.js +0 -183
- package/cjs/webpackCompat.d.ts +0 -6
- package/cjs/webpackCompat.js +0 -341
- package/esm/dev.d.ts +0 -44
- package/esm/dev.js +0 -346
- package/esm/mkcert.d.ts +0 -7
- package/esm/mkcert.js +0 -176
- package/esm/webpackCompat.d.ts +0 -6
- package/esm/webpackCompat.js +0 -338
package/cjs/dev.js
DELETED
|
@@ -1,362 +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 hmr_1 = require("./hmr");
|
|
23
|
-
const mkcert_1 = require("./mkcert");
|
|
24
|
-
const util_1 = require("./util");
|
|
25
|
-
const webpackCompat_1 = require("./webpackCompat");
|
|
26
|
-
const xcodeProfile_1 = require("./xcodeProfile");
|
|
27
|
-
function serve(options, projectPath, rootPath, serverOptions) {
|
|
28
|
-
const bundleOptions = options.compatMode
|
|
29
|
-
? (0, webpackCompat_1.compatOptionsFromWebpack)(options)
|
|
30
|
-
: options;
|
|
31
|
-
return serveInternal(bundleOptions, projectPath, rootPath, serverOptions);
|
|
32
|
-
}
|
|
33
|
-
async function serveInternal(options, projectPath, rootPath, serverOptions) {
|
|
34
|
-
(0, util_1.blockStdout)();
|
|
35
|
-
if (process.env.XCODE_PROFILE) {
|
|
36
|
-
await (0, xcodeProfile_1.xcodeProfilingReady)();
|
|
37
|
-
}
|
|
38
|
-
startServer({
|
|
39
|
-
hostname: (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.hostname) || "localhost",
|
|
40
|
-
port: (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.port) || 3000,
|
|
41
|
-
https: serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.https,
|
|
42
|
-
selfSignedCertificate: (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.https)
|
|
43
|
-
? await (0, mkcert_1.createSelfSignedCertificate)((serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.hostname) || "localhost")
|
|
44
|
-
: undefined,
|
|
45
|
-
}, options, projectPath || process.cwd(), rootPath);
|
|
46
|
-
}
|
|
47
|
-
async function startServer(serverOptions, bundleOptions, projectPath, rootPath) {
|
|
48
|
-
let { port, hostname, selfSignedCertificate } = serverOptions;
|
|
49
|
-
process.title = "utoo-pack-dev-server";
|
|
50
|
-
let handlersReady = () => { };
|
|
51
|
-
let handlersError = () => { };
|
|
52
|
-
let handlersPromise = new Promise((resolve, reject) => {
|
|
53
|
-
handlersReady = resolve;
|
|
54
|
-
handlersError = reject;
|
|
55
|
-
});
|
|
56
|
-
let requestHandler = async (req, res) => {
|
|
57
|
-
if (handlersPromise) {
|
|
58
|
-
await handlersPromise;
|
|
59
|
-
return requestHandler(req, res);
|
|
60
|
-
}
|
|
61
|
-
throw new Error("Invariant request handler was not setup");
|
|
62
|
-
};
|
|
63
|
-
let upgradeHandler = async (req, socket, head) => {
|
|
64
|
-
if (handlersPromise) {
|
|
65
|
-
await handlersPromise;
|
|
66
|
-
return upgradeHandler(req, socket, head);
|
|
67
|
-
}
|
|
68
|
-
throw new Error("Invariant upgrade handler was not setup");
|
|
69
|
-
};
|
|
70
|
-
async function requestListener(req, res) {
|
|
71
|
-
try {
|
|
72
|
-
if (handlersPromise) {
|
|
73
|
-
await handlersPromise;
|
|
74
|
-
handlersPromise = undefined;
|
|
75
|
-
}
|
|
76
|
-
await requestHandler(req, res);
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
res.statusCode = 500;
|
|
80
|
-
res.end("Internal Server Error");
|
|
81
|
-
console.error(`Failed to handle request for ${req.url}`);
|
|
82
|
-
console.error(err);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const server = selfSignedCertificate
|
|
86
|
-
? https_1.default.createServer({
|
|
87
|
-
key: fs_1.default.readFileSync(selfSignedCertificate.key),
|
|
88
|
-
cert: fs_1.default.readFileSync(selfSignedCertificate.cert),
|
|
89
|
-
}, requestListener)
|
|
90
|
-
: http_1.default.createServer(requestListener);
|
|
91
|
-
server.on("upgrade", async (req, socket, head) => {
|
|
92
|
-
try {
|
|
93
|
-
await upgradeHandler(req, socket, head);
|
|
94
|
-
}
|
|
95
|
-
catch (err) {
|
|
96
|
-
socket.destroy();
|
|
97
|
-
console.error(`Failed to handle request for ${req.url}`);
|
|
98
|
-
console.error(err);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
let portRetryCount = 0;
|
|
102
|
-
const originalPort = port;
|
|
103
|
-
server.on("error", (err) => {
|
|
104
|
-
if (port && err.code === "EADDRINUSE" && portRetryCount < 10) {
|
|
105
|
-
port += 1;
|
|
106
|
-
portRetryCount += 1;
|
|
107
|
-
server.listen(port, hostname);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
console.error(`Failed to start server`);
|
|
111
|
-
console.error(err);
|
|
112
|
-
process.exit(1);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
await new Promise((resolve) => {
|
|
116
|
-
server.on("listening", async () => {
|
|
117
|
-
const addr = server.address();
|
|
118
|
-
const actualHostname = formatHostname(typeof addr === "object"
|
|
119
|
-
? (addr === null || addr === void 0 ? void 0 : addr.address) || hostname || "localhost"
|
|
120
|
-
: addr);
|
|
121
|
-
const formattedHostname = !hostname || actualHostname === "0.0.0.0"
|
|
122
|
-
? "localhost"
|
|
123
|
-
: actualHostname === "[::]"
|
|
124
|
-
? "[::1]"
|
|
125
|
-
: formatHostname(hostname);
|
|
126
|
-
port = typeof addr === "object" ? (addr === null || addr === void 0 ? void 0 : addr.port) || port : port;
|
|
127
|
-
if (portRetryCount) {
|
|
128
|
-
console.warn(`Port ${originalPort} is in use, using available port ${port} instead.`);
|
|
129
|
-
}
|
|
130
|
-
console.log(`Listening on ${serverOptions.https ? "https" : "http"}://${formattedHostname}:${port} ...`);
|
|
131
|
-
try {
|
|
132
|
-
let cleanupStarted = false;
|
|
133
|
-
let closeUpgraded = null;
|
|
134
|
-
const cleanup = () => {
|
|
135
|
-
if (cleanupStarted) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
cleanupStarted = true;
|
|
139
|
-
(async () => {
|
|
140
|
-
console.debug("start-server process cleanup");
|
|
141
|
-
await new Promise((res) => {
|
|
142
|
-
server.close((err) => {
|
|
143
|
-
if (err)
|
|
144
|
-
console.error(err);
|
|
145
|
-
res();
|
|
146
|
-
});
|
|
147
|
-
server.closeAllConnections();
|
|
148
|
-
closeUpgraded === null || closeUpgraded === void 0 ? void 0 : closeUpgraded();
|
|
149
|
-
});
|
|
150
|
-
console.debug("start-server process cleanup finished");
|
|
151
|
-
process.exit(0);
|
|
152
|
-
})();
|
|
153
|
-
};
|
|
154
|
-
const exception = (err) => {
|
|
155
|
-
console.error(err);
|
|
156
|
-
};
|
|
157
|
-
process.on("SIGINT", cleanup);
|
|
158
|
-
process.on("SIGTERM", cleanup);
|
|
159
|
-
process.on("rejectionHandled", () => { });
|
|
160
|
-
process.on("uncaughtException", exception);
|
|
161
|
-
process.on("unhandledRejection", exception);
|
|
162
|
-
const initResult = await initialize(bundleOptions, projectPath, rootPath);
|
|
163
|
-
requestHandler = initResult.requestHandler;
|
|
164
|
-
upgradeHandler = initResult.upgradeHandler;
|
|
165
|
-
closeUpgraded = initResult.closeUpgraded;
|
|
166
|
-
handlersReady();
|
|
167
|
-
}
|
|
168
|
-
catch (err) {
|
|
169
|
-
handlersError();
|
|
170
|
-
console.error(err);
|
|
171
|
-
process.exit(1);
|
|
172
|
-
}
|
|
173
|
-
resolve();
|
|
174
|
-
});
|
|
175
|
-
server.listen(port, hostname);
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
async function initialize(bundleOptions, projectPath, rootPath) {
|
|
179
|
-
process.env.NODE_ENV = "development";
|
|
180
|
-
const hotReloader = await (0, hmr_1.createHotReloader)(bundleOptions, projectPath, rootPath);
|
|
181
|
-
await hotReloader.start();
|
|
182
|
-
const requestHandlerImpl = async (req, res) => {
|
|
183
|
-
req.on("error", console.error);
|
|
184
|
-
res.on("error", console.error);
|
|
185
|
-
const handleRequest = async () => {
|
|
186
|
-
var _a;
|
|
187
|
-
if (!(req.method === "GET" || req.method === "HEAD")) {
|
|
188
|
-
res.setHeader("Allow", ["GET", "HEAD"]);
|
|
189
|
-
res.statusCode = 405;
|
|
190
|
-
res.end();
|
|
191
|
-
}
|
|
192
|
-
const distRoot = path_1.default.resolve(projectPath, ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || "./dist");
|
|
193
|
-
try {
|
|
194
|
-
const reqUrl = req.url || "";
|
|
195
|
-
const path = url_1.default.parse(reqUrl).pathname || "";
|
|
196
|
-
return await serveStatic(req, res, path, { root: distRoot });
|
|
197
|
-
}
|
|
198
|
-
catch (err) {
|
|
199
|
-
res.setHeader("Cache-Control", "private, no-cache, no-store, max-age=0, must-revalidate");
|
|
200
|
-
res.statusCode = 404;
|
|
201
|
-
res.end();
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
try {
|
|
205
|
-
await handleRequest();
|
|
206
|
-
}
|
|
207
|
-
catch (err) {
|
|
208
|
-
res.statusCode = 500;
|
|
209
|
-
res.end("Internal Server Error");
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
let requestHandler = requestHandlerImpl;
|
|
213
|
-
const logError = async (type, err) => {
|
|
214
|
-
if (type === "unhandledRejection") {
|
|
215
|
-
console.error("unhandledRejection: ", err);
|
|
216
|
-
}
|
|
217
|
-
else if (type === "uncaughtException") {
|
|
218
|
-
console.error("uncaughtException: ", err);
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
process.on("uncaughtException", logError.bind(null, "uncaughtException"));
|
|
222
|
-
process.on("unhandledRejection", logError.bind(null, "unhandledRejection"));
|
|
223
|
-
const upgradeHandler = async (req, socket, head) => {
|
|
224
|
-
var _a;
|
|
225
|
-
try {
|
|
226
|
-
const isHMRRequest = (_a = req.url) === null || _a === void 0 ? void 0 : _a.includes("turbopack-hmr");
|
|
227
|
-
if (isHMRRequest) {
|
|
228
|
-
hotReloader.onHMR(req, socket, head);
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
socket.end();
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
catch (err) {
|
|
235
|
-
console.error("Error handling upgrade request", err);
|
|
236
|
-
socket.end();
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
return {
|
|
240
|
-
requestHandler,
|
|
241
|
-
upgradeHandler,
|
|
242
|
-
closeUpgraded() {
|
|
243
|
-
hotReloader.close();
|
|
244
|
-
},
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
async function pipeToNodeResponse(readable, res, waitUntilForEnd) {
|
|
248
|
-
try {
|
|
249
|
-
const { errored, destroyed } = res;
|
|
250
|
-
if (errored || destroyed)
|
|
251
|
-
return;
|
|
252
|
-
const controller = createAbortController(res);
|
|
253
|
-
const writer = createWriterFromResponse(res, waitUntilForEnd);
|
|
254
|
-
await readable.pipeTo(writer, { signal: controller.signal });
|
|
255
|
-
}
|
|
256
|
-
catch (err) {
|
|
257
|
-
if (isAbortError(err))
|
|
258
|
-
return;
|
|
259
|
-
throw new Error("failed to pipe response", { cause: err });
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
function createAbortController(response) {
|
|
263
|
-
const controller = new AbortController();
|
|
264
|
-
response.once("close", () => {
|
|
265
|
-
if (response.writableFinished)
|
|
266
|
-
return;
|
|
267
|
-
controller.abort(new ResponseAborted());
|
|
268
|
-
});
|
|
269
|
-
return controller;
|
|
270
|
-
}
|
|
271
|
-
function isAbortError(e) {
|
|
272
|
-
return (e === null || e === void 0 ? void 0 : e.name) === "AbortError" || (e === null || e === void 0 ? void 0 : e.name) === exports.ResponseAbortedName;
|
|
273
|
-
}
|
|
274
|
-
exports.ResponseAbortedName = "ResponseAborted";
|
|
275
|
-
class ResponseAborted extends Error {
|
|
276
|
-
constructor() {
|
|
277
|
-
super(...arguments);
|
|
278
|
-
this.name = exports.ResponseAbortedName;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
exports.ResponseAborted = ResponseAborted;
|
|
282
|
-
function createWriterFromResponse(res, waitUntilForEnd) {
|
|
283
|
-
let started = false;
|
|
284
|
-
let drained = new DetachedPromise();
|
|
285
|
-
function onDrain() {
|
|
286
|
-
drained.resolve();
|
|
287
|
-
}
|
|
288
|
-
res.on("drain", onDrain);
|
|
289
|
-
res.once("close", () => {
|
|
290
|
-
res.off("drain", onDrain);
|
|
291
|
-
drained.resolve();
|
|
292
|
-
});
|
|
293
|
-
const finished = new DetachedPromise();
|
|
294
|
-
res.once("finish", () => {
|
|
295
|
-
finished.resolve();
|
|
296
|
-
});
|
|
297
|
-
return new WritableStream({
|
|
298
|
-
write: async (chunk) => {
|
|
299
|
-
if (!started) {
|
|
300
|
-
started = true;
|
|
301
|
-
res.flushHeaders();
|
|
302
|
-
}
|
|
303
|
-
try {
|
|
304
|
-
const ok = res.write(chunk);
|
|
305
|
-
if ("flush" in res && typeof res.flush === "function") {
|
|
306
|
-
res.flush();
|
|
307
|
-
}
|
|
308
|
-
if (!ok) {
|
|
309
|
-
await drained.promise;
|
|
310
|
-
drained = new DetachedPromise();
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
catch (err) {
|
|
314
|
-
res.end();
|
|
315
|
-
throw new Error("failed to write chunk to response", { cause: err });
|
|
316
|
-
}
|
|
317
|
-
},
|
|
318
|
-
abort: (err) => {
|
|
319
|
-
if (res.writableFinished)
|
|
320
|
-
return;
|
|
321
|
-
res.destroy(err);
|
|
322
|
-
},
|
|
323
|
-
close: async () => {
|
|
324
|
-
if (waitUntilForEnd) {
|
|
325
|
-
await waitUntilForEnd;
|
|
326
|
-
}
|
|
327
|
-
if (res.writableFinished)
|
|
328
|
-
return;
|
|
329
|
-
res.end();
|
|
330
|
-
return finished.promise;
|
|
331
|
-
},
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
class DetachedPromise {
|
|
335
|
-
constructor() {
|
|
336
|
-
let resolve;
|
|
337
|
-
let reject;
|
|
338
|
-
this.promise = new Promise((res, rej) => {
|
|
339
|
-
resolve = res;
|
|
340
|
-
reject = rej;
|
|
341
|
-
});
|
|
342
|
-
this.resolve = resolve;
|
|
343
|
-
this.reject = reject;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
exports.DetachedPromise = DetachedPromise;
|
|
347
|
-
function serveStatic(req, res, path, opts) {
|
|
348
|
-
return new Promise((resolve, reject) => {
|
|
349
|
-
(0, send_1.default)(req, path, opts)
|
|
350
|
-
.on("directory", () => {
|
|
351
|
-
const err = new Error("No directory access");
|
|
352
|
-
err.code = "ENOENT";
|
|
353
|
-
reject(err);
|
|
354
|
-
})
|
|
355
|
-
.on("error", reject)
|
|
356
|
-
.pipe(res)
|
|
357
|
-
.on("finish", resolve);
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
function formatHostname(hostname) {
|
|
361
|
-
return (0, net_1.isIPv6)(hostname) ? `[${hostname}]` : hostname;
|
|
362
|
-
}
|
package/cjs/mkcert.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export interface SelfSignedCertificate {
|
|
2
|
-
key: string;
|
|
3
|
-
cert: string;
|
|
4
|
-
rootCA?: string;
|
|
5
|
-
}
|
|
6
|
-
export declare function createSelfSignedCertificate(host?: string, certDir?: string): Promise<SelfSignedCertificate | undefined>;
|
|
7
|
-
export declare function getCacheDirectory(fileDirectory: string, envPath?: string): string;
|
package/cjs/mkcert.js
DELETED
|
@@ -1,183 +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.createSelfSignedCertificate = createSelfSignedCertificate;
|
|
7
|
-
exports.getCacheDirectory = getCacheDirectory;
|
|
8
|
-
const node_child_process_1 = require("node:child_process");
|
|
9
|
-
const node_crypto_1 = require("node:crypto");
|
|
10
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
-
const os_1 = __importDefault(require("os"));
|
|
13
|
-
const { WritableStream } = require("node:stream/web");
|
|
14
|
-
const MKCERT_VERSION = "v1.4.4";
|
|
15
|
-
function getBinaryName() {
|
|
16
|
-
const platform = process.platform;
|
|
17
|
-
const arch = process.arch === "x64" ? "amd64" : process.arch;
|
|
18
|
-
if (platform === "win32") {
|
|
19
|
-
return `mkcert-${MKCERT_VERSION}-windows-${arch}.exe`;
|
|
20
|
-
}
|
|
21
|
-
if (platform === "darwin") {
|
|
22
|
-
return `mkcert-${MKCERT_VERSION}-darwin-${arch}`;
|
|
23
|
-
}
|
|
24
|
-
if (platform === "linux") {
|
|
25
|
-
return `mkcert-${MKCERT_VERSION}-linux-${arch}`;
|
|
26
|
-
}
|
|
27
|
-
throw new Error(`Unsupported platform: ${platform}`);
|
|
28
|
-
}
|
|
29
|
-
async function downloadBinary() {
|
|
30
|
-
try {
|
|
31
|
-
const binaryName = getBinaryName();
|
|
32
|
-
const cacheDirectory = getCacheDirectory("mkcert");
|
|
33
|
-
const binaryPath = node_path_1.default.join(cacheDirectory, binaryName);
|
|
34
|
-
if (node_fs_1.default.existsSync(binaryPath)) {
|
|
35
|
-
return binaryPath;
|
|
36
|
-
}
|
|
37
|
-
const downloadUrl = `https://github.com/FiloSottile/mkcert/releases/download/${MKCERT_VERSION}/${binaryName}`;
|
|
38
|
-
await node_fs_1.default.promises.mkdir(cacheDirectory, { recursive: true });
|
|
39
|
-
console.log(`Downloading mkcert package...`);
|
|
40
|
-
const response = await fetch(downloadUrl);
|
|
41
|
-
if (!response.ok || !response.body) {
|
|
42
|
-
throw new Error(`request failed with status ${response.status}`);
|
|
43
|
-
}
|
|
44
|
-
console.log(`Download response was successful, writing to disk`);
|
|
45
|
-
const binaryWriteStream = node_fs_1.default.createWriteStream(binaryPath);
|
|
46
|
-
await response.body.pipeTo(new WritableStream({
|
|
47
|
-
write(chunk) {
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
binaryWriteStream.write(chunk, (error) => {
|
|
50
|
-
if (error) {
|
|
51
|
-
reject(error);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
resolve();
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
close() {
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
binaryWriteStream.close((error) => {
|
|
61
|
-
if (error) {
|
|
62
|
-
reject(error);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
resolve();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
}));
|
|
70
|
-
await node_fs_1.default.promises.chmod(binaryPath, 0o755);
|
|
71
|
-
return binaryPath;
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
console.error("Error downloading mkcert:", err);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async function createSelfSignedCertificate(host, certDir = "certificates") {
|
|
78
|
-
try {
|
|
79
|
-
const binaryPath = await downloadBinary();
|
|
80
|
-
if (!binaryPath)
|
|
81
|
-
throw new Error("missing mkcert binary");
|
|
82
|
-
const resolvedCertDir = node_path_1.default.resolve(process.cwd(), `./${certDir}`);
|
|
83
|
-
await node_fs_1.default.promises.mkdir(resolvedCertDir, {
|
|
84
|
-
recursive: true,
|
|
85
|
-
});
|
|
86
|
-
const keyPath = node_path_1.default.resolve(resolvedCertDir, "localhost-key.pem");
|
|
87
|
-
const certPath = node_path_1.default.resolve(resolvedCertDir, "localhost.pem");
|
|
88
|
-
if (node_fs_1.default.existsSync(keyPath) && node_fs_1.default.existsSync(certPath)) {
|
|
89
|
-
const cert = new node_crypto_1.X509Certificate(node_fs_1.default.readFileSync(certPath));
|
|
90
|
-
const key = node_fs_1.default.readFileSync(keyPath);
|
|
91
|
-
if (cert.checkHost(host !== null && host !== void 0 ? host : "localhost") &&
|
|
92
|
-
cert.checkPrivateKey((0, node_crypto_1.createPrivateKey)(key))) {
|
|
93
|
-
console.log("Using already generated self signed certificate");
|
|
94
|
-
const caLocation = (0, node_child_process_1.execSync)(`"${binaryPath}" -CAROOT`)
|
|
95
|
-
.toString()
|
|
96
|
-
.trim();
|
|
97
|
-
return {
|
|
98
|
-
key: keyPath,
|
|
99
|
-
cert: certPath,
|
|
100
|
-
rootCA: `${caLocation}/rootCA.pem`,
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
console.log("Attempting to generate self signed certificate. This may prompt for your password");
|
|
105
|
-
const defaultHosts = ["localhost", "127.0.0.1", "::1"];
|
|
106
|
-
const hosts = host && !defaultHosts.includes(host)
|
|
107
|
-
? [...defaultHosts, host]
|
|
108
|
-
: defaultHosts;
|
|
109
|
-
(0, node_child_process_1.execSync)(`"${binaryPath}" -install -key-file "${keyPath}" -cert-file "${certPath}" ${hosts.join(" ")}`, { stdio: "ignore" });
|
|
110
|
-
const caLocation = (0, node_child_process_1.execSync)(`"${binaryPath}" -CAROOT`).toString().trim();
|
|
111
|
-
if (!node_fs_1.default.existsSync(keyPath) || !node_fs_1.default.existsSync(certPath)) {
|
|
112
|
-
throw new Error("Certificate files not found");
|
|
113
|
-
}
|
|
114
|
-
console.log(`CA Root certificate created in ${caLocation}`);
|
|
115
|
-
console.log(`Certificates created in ${resolvedCertDir}`);
|
|
116
|
-
const gitignorePath = node_path_1.default.resolve(process.cwd(), "./.gitignore");
|
|
117
|
-
if (node_fs_1.default.existsSync(gitignorePath)) {
|
|
118
|
-
const gitignore = await node_fs_1.default.promises.readFile(gitignorePath, "utf8");
|
|
119
|
-
if (!gitignore.includes(certDir)) {
|
|
120
|
-
console.log("Adding certificates to .gitignore");
|
|
121
|
-
await node_fs_1.default.promises.appendFile(gitignorePath, `\n${certDir}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return {
|
|
125
|
-
key: keyPath,
|
|
126
|
-
cert: certPath,
|
|
127
|
-
rootCA: `${caLocation}/rootCA.pem`,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
catch (err) {
|
|
131
|
-
console.error("Failed to generate self-signed certificate. Falling back to http.", err);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
// get platform specific cache directory adapted from playwright's handling
|
|
135
|
-
// https://github.com/microsoft/playwright/blob/7d924470d397975a74a19184c136b3573a974e13/packages/playwright-core/src/utils/registry.ts#L141
|
|
136
|
-
function getCacheDirectory(fileDirectory, envPath) {
|
|
137
|
-
let result;
|
|
138
|
-
if (envPath) {
|
|
139
|
-
result = envPath;
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
let systemCacheDirectory;
|
|
143
|
-
if (process.platform === "linux") {
|
|
144
|
-
systemCacheDirectory =
|
|
145
|
-
process.env.XDG_CACHE_HOME || node_path_1.default.join(os_1.default.homedir(), ".cache");
|
|
146
|
-
}
|
|
147
|
-
else if (process.platform === "darwin") {
|
|
148
|
-
systemCacheDirectory = node_path_1.default.join(os_1.default.homedir(), "Library", "Caches");
|
|
149
|
-
}
|
|
150
|
-
else if (process.platform === "win32") {
|
|
151
|
-
systemCacheDirectory =
|
|
152
|
-
process.env.LOCALAPPDATA || node_path_1.default.join(os_1.default.homedir(), "AppData", "Local");
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
/// Attempt to use generic tmp location for un-handled platform
|
|
156
|
-
if (!systemCacheDirectory) {
|
|
157
|
-
for (const dir of [
|
|
158
|
-
node_path_1.default.join(os_1.default.homedir(), ".cache"),
|
|
159
|
-
node_path_1.default.join(os_1.default.tmpdir()),
|
|
160
|
-
]) {
|
|
161
|
-
if (node_fs_1.default.existsSync(dir)) {
|
|
162
|
-
systemCacheDirectory = dir;
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (!systemCacheDirectory) {
|
|
168
|
-
console.error(new Error("Unsupported platform: " + process.platform));
|
|
169
|
-
process.exit(0);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
result = node_path_1.default.join(systemCacheDirectory, fileDirectory);
|
|
173
|
-
}
|
|
174
|
-
if (!node_path_1.default.isAbsolute(result)) {
|
|
175
|
-
// It is important to resolve to the absolute path:
|
|
176
|
-
// - for unzipping to work correctly;
|
|
177
|
-
// - so that registry directory matches between installation and execution.
|
|
178
|
-
// INIT_CWD points to the root of `npm/yarn install` and is probably what
|
|
179
|
-
// the user meant when typing the relative path.
|
|
180
|
-
result = node_path_1.default.resolve(process.env["INIT_CWD"] || process.cwd(), result);
|
|
181
|
-
}
|
|
182
|
-
return result;
|
|
183
|
-
}
|
package/cjs/webpackCompat.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type webpack from "webpack";
|
|
2
|
-
import { BundleOptions } from "./types";
|
|
3
|
-
export type WebpackConfig = Pick<webpack.Configuration, "name" | "entry" | "mode" | "module" | "resolve" | "externals" | "output" | "target" | "devtool" | "optimization" | "plugins" | "stats"> & {
|
|
4
|
-
compatMode: true;
|
|
5
|
-
};
|
|
6
|
-
export declare function compatOptionsFromWebpack(webpackConfig: WebpackConfig): BundleOptions;
|