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