unplugin-https-reverse-proxy 0.0.1

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.
@@ -0,0 +1,381 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/webpack.ts
31
+ var webpack_exports = {};
32
+ __export(webpack_exports, {
33
+ default: () => webpack_default
34
+ });
35
+ module.exports = __toCommonJS(webpack_exports);
36
+ var import_unplugin2 = require("unplugin");
37
+
38
+ // src/index.ts
39
+ var import_node_process3 = __toESM(require("process"), 1);
40
+ var import_unplugin = require("unplugin");
41
+ var import_picocolors = __toESM(require("picocolors"), 1);
42
+
43
+ // src/caddy/index.ts
44
+ var import_node_os = require("os");
45
+ var import_node_process2 = __toESM(require("process"), 1);
46
+ var import_node_fs = require("fs");
47
+ var import_node_child_process = require("child_process");
48
+ var import_got = __toESM(require("got"), 1);
49
+ var hostile = __toESM(require("hostile"), 1);
50
+ var import_kill_port = __toESM(require("kill-port"), 1);
51
+
52
+ // src/caddy/constants.ts
53
+ var import_node_path = require("path");
54
+ var import_node_process = __toESM(require("process"), 1);
55
+ var TEMP_DIR = import_node_process.default.env.TEMP || import_node_process.default.env.TMPDIR || "/tmp";
56
+ var caddyPath = (0, import_node_path.join)(TEMP_DIR, "caddy");
57
+ var caddyFilePath = (0, import_node_path.join)(TEMP_DIR, "CADDYFILE");
58
+ var supportList = [
59
+ {
60
+ platform: "dragonfly",
61
+ arch: "amd64"
62
+ },
63
+ {
64
+ platform: "freebsd",
65
+ arch: "amd64"
66
+ },
67
+ {
68
+ platform: "freebsd",
69
+ arch: "arm-6"
70
+ },
71
+ {
72
+ platform: "freebsd",
73
+ arch: "arm-7"
74
+ },
75
+ {
76
+ platform: "freebsd",
77
+ arch: "arm64"
78
+ },
79
+ {
80
+ platform: "linux",
81
+ arch: "amd64"
82
+ },
83
+ {
84
+ platform: "linux",
85
+ arch: "arm-5"
86
+ },
87
+ {
88
+ platform: "linux",
89
+ arch: "arm-6"
90
+ },
91
+ {
92
+ platform: "linux",
93
+ arch: "arm-7"
94
+ },
95
+ {
96
+ platform: "linux",
97
+ arch: "arm64"
98
+ },
99
+ {
100
+ platform: "linux",
101
+ arch: "mips"
102
+ },
103
+ {
104
+ platform: "linux",
105
+ arch: "mips64"
106
+ },
107
+ {
108
+ platform: "linux",
109
+ arch: "mips64le"
110
+ },
111
+ {
112
+ platform: "linux",
113
+ arch: "mipsle"
114
+ },
115
+ {
116
+ platform: "linux",
117
+ arch: "ppc64"
118
+ },
119
+ {
120
+ platform: "linux",
121
+ arch: "ppc64le"
122
+ },
123
+ {
124
+ platform: "linux",
125
+ arch: "s390x"
126
+ },
127
+ {
128
+ platform: "darwin",
129
+ arch: "amd64"
130
+ },
131
+ {
132
+ platform: "darwin",
133
+ arch: "arm64"
134
+ },
135
+ {
136
+ platform: "openbsd",
137
+ arch: "amd64"
138
+ },
139
+ {
140
+ platform: "openbsd",
141
+ arch: "arm-6"
142
+ },
143
+ {
144
+ platform: "openbsd",
145
+ arch: "arm-7"
146
+ },
147
+ {
148
+ platform: "openbsd",
149
+ arch: "arm64"
150
+ },
151
+ {
152
+ platform: "windows",
153
+ arch: "amd64"
154
+ },
155
+ {
156
+ platform: "windows",
157
+ arch: "arm-6"
158
+ },
159
+ {
160
+ platform: "windows",
161
+ arch: "arm-7"
162
+ },
163
+ {
164
+ platform: "windows",
165
+ arch: "arm64"
166
+ }
167
+ ];
168
+
169
+ // src/caddy/index.ts
170
+ async function download() {
171
+ if (await testCaddy())
172
+ return caddyPath;
173
+ return new Promise((resolve, reject) => {
174
+ const file = (0, import_node_fs.createWriteStream)(caddyPath);
175
+ let _platform = (0, import_node_os.platform)().toString();
176
+ _platform === "win32" && (_platform = "windows");
177
+ let _arch = import_node_process2.default.arch.toString();
178
+ _arch === "x64" && (_arch = "amd64");
179
+ const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
180
+ if (!support)
181
+ return reject(new Error("not support"));
182
+ const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
183
+ (0, import_node_fs.existsSync)(caddyPath) && (0, import_node_fs.unlinkSync)(caddyPath);
184
+ import_got.default.stream(dowmloadLink).pipe(file).on("finish", () => {
185
+ if (import_node_process2.default.platform === "win32")
186
+ return resolve(caddyPath);
187
+ (0, import_node_fs.chmodSync)(caddyPath, 493);
188
+ resolve(caddyPath);
189
+ }).on("error", (err) => {
190
+ reject(err);
191
+ });
192
+ });
193
+ }
194
+ function testCaddy() {
195
+ return new Promise((resolve, reject) => {
196
+ if (!(0, import_node_fs.existsSync)(caddyPath))
197
+ return resolve(false);
198
+ (0, import_node_fs.chmodSync)(caddyPath, 493);
199
+ const child = (0, import_node_child_process.spawn)("sudo", [caddyPath]);
200
+ child.on("error", (err) => {
201
+ reject(err);
202
+ });
203
+ child.stdout.on("data", (_data) => {
204
+ resolve(true);
205
+ });
206
+ });
207
+ }
208
+ var CaddyInstant = class {
209
+ constructor() {
210
+ this.inited = false;
211
+ testCaddy().then(() => {
212
+ this.inited = true;
213
+ });
214
+ }
215
+ async init() {
216
+ await download();
217
+ this.inited = true;
218
+ }
219
+ async updateHost(ip, host) {
220
+ ["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
221
+ return new Promise((resolve, reject) => {
222
+ hostile.set(ip, host, (err) => {
223
+ if (err) {
224
+ return reject(err);
225
+ } else {
226
+ if (import_node_process2.default.platform === "win32") {
227
+ (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err2) => {
228
+ return reject(err2);
229
+ }).on("close", () => {
230
+ resolve(true);
231
+ });
232
+ } else {
233
+ (0, import_node_child_process.spawn)("sudo", ["killall", "-HUP", "mDNSResponder"]).on("error", (err2) => {
234
+ return reject(err2);
235
+ }).on("close", () => {
236
+ resolve(true);
237
+ });
238
+ }
239
+ }
240
+ });
241
+ });
242
+ }
243
+ async restoreHost(ip, host) {
244
+ ["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
245
+ return new Promise((resolve, reject) => {
246
+ hostile.remove(ip, host, (err) => {
247
+ if (err) {
248
+ return reject(err);
249
+ } else {
250
+ if (import_node_process2.default.platform === "win32") {
251
+ (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err2) => {
252
+ return reject(err2);
253
+ }).on("close", () => {
254
+ resolve(true);
255
+ });
256
+ } else {
257
+ (0, import_node_child_process.spawn)("sudo", ["killall", "-HUP", "mDNSResponder"]).on("error", (err2) => {
258
+ return reject(err2);
259
+ }).on("close", () => {
260
+ resolve(true);
261
+ });
262
+ }
263
+ }
264
+ });
265
+ });
266
+ }
267
+ /**
268
+ * @param source like `127.0.0.1:8080`
269
+ * @param target like `test.abc.com`. And if you provide a port ,it will be ignore.
270
+ * @returns
271
+ */
272
+ async run(source, target, options) {
273
+ const {
274
+ restore = false,
275
+ base = "/"
276
+ } = options || {};
277
+ if (!this.inited)
278
+ await this.init();
279
+ if (!await this.updateHost(source.split(":")[0], target.split(":")[0]))
280
+ throw new Error("update host failed");
281
+ await (0, import_kill_port.default)(443, "tcp");
282
+ return new Promise((resolve, reject) => {
283
+ const child = (0, import_node_child_process.spawn)("sudo", [caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
284
+ child.on("error", (err) => {
285
+ return reject(err);
286
+ });
287
+ child.stderr.on("data", (data) => {
288
+ if (data.toString().includes("Error")) {
289
+ child.kill();
290
+ return reject(data.toString());
291
+ }
292
+ });
293
+ child.stdout.on("data", (_data) => {
294
+ resolve(async () => {
295
+ if (!restore)
296
+ return;
297
+ if (await this.restoreHost(source.split(":")[0], target.split(":")[0])) {
298
+ console.log("restore host success\n");
299
+ } else {
300
+ console.error("restore host failed\n");
301
+ }
302
+ child.kill();
303
+ });
304
+ });
305
+ });
306
+ }
307
+ };
308
+
309
+ // src/index.ts
310
+ var config;
311
+ var unpluginFactory = (options) => ({
312
+ name: "unplugin-https-reverse-proxy",
313
+ vite: {
314
+ configResolved(_config) {
315
+ config = _config;
316
+ },
317
+ configureServer(server) {
318
+ const {
319
+ target = ""
320
+ } = options;
321
+ if (!target) {
322
+ console.log("please provide target");
323
+ return;
324
+ }
325
+ server.printUrls = () => {
326
+ var _a;
327
+ let source = `localhost:${config.server.port}`;
328
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
329
+ if (url) {
330
+ const u = new URL(url);
331
+ source = u.host;
332
+ }
333
+ const base = server.config.base || "/";
334
+ try {
335
+ const caddy = new CaddyInstant();
336
+ caddy.run(source, target, {
337
+ base
338
+ }).then((stop) => {
339
+ const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
340
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}${base}`)}`);
341
+ import_node_process3.default.on("SIGINT", async () => {
342
+ await stop();
343
+ server.close();
344
+ import_node_process3.default.exit();
345
+ });
346
+ });
347
+ } catch (e) {
348
+ console.error(e);
349
+ }
350
+ };
351
+ }
352
+ },
353
+ webpack(compiler) {
354
+ var _a, _b;
355
+ const {
356
+ target = ""
357
+ } = options;
358
+ if (!target) {
359
+ console.log("please provide target");
360
+ return;
361
+ }
362
+ const source = `${((_a = compiler.options.devServer) == null ? void 0 : _a.host) || "127.0.0.1"}:${((_b = compiler.options.devServer) == null ? void 0 : _b.port) || "8080"}`;
363
+ try {
364
+ const caddy = new CaddyInstant();
365
+ caddy.run(source, target).then((stop) => {
366
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
367
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}`)}`);
368
+ import_node_process3.default.on("SIGINT", async () => {
369
+ await stop();
370
+ import_node_process3.default.exit();
371
+ });
372
+ });
373
+ } catch (e) {
374
+ console.error(e);
375
+ }
376
+ }
377
+ });
378
+
379
+ // src/webpack.ts
380
+ var webpack_default = (0, import_unplugin2.createWebpackPlugin)(unpluginFactory);
381
+ exports.default = module.exports;
@@ -0,0 +1,6 @@
1
+ import * as webpack from 'webpack';
2
+ import { Options } from './types.cjs';
3
+
4
+ declare const _default: (options: Options) => webpack.WebpackPluginInstance;
5
+
6
+ export { _default as default };
@@ -0,0 +1,6 @@
1
+ import * as webpack from 'webpack';
2
+ import { Options } from './types.js';
3
+
4
+ declare const _default: (options: Options) => webpack.WebpackPluginInstance;
5
+
6
+ export { _default as default };
@@ -0,0 +1,7 @@
1
+ import {
2
+ webpack_default
3
+ } from "./chunk-TIR4PYKX.js";
4
+ import "./chunk-QYHKWM3K.js";
5
+ export {
6
+ webpack_default as default
7
+ };
package/package.json ADDED
@@ -0,0 +1,143 @@
1
+ {
2
+ "name": "unplugin-https-reverse-proxy",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "packageManager": "pnpm@8.6.12",
6
+ "description": "",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/zcf0508/unplugin-https-reverse-proxy#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/zcf0508/unplugin-https-reverse-proxy.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/zcf0508/unplugin-https-reverse-proxy/issues"
15
+ },
16
+ "keywords": [
17
+ "unplugin",
18
+ "vite",
19
+ "webpack",
20
+ "rollup",
21
+ "transform"
22
+ ],
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js",
27
+ "require": "./dist/index.cjs"
28
+ },
29
+ "./vite": {
30
+ "types": "./dist/vite.d.ts",
31
+ "import": "./dist/vite.js",
32
+ "require": "./dist/vite.cjs"
33
+ },
34
+ "./webpack": {
35
+ "types": "./dist/webpack.d.ts",
36
+ "import": "./dist/webpack.js",
37
+ "require": "./dist/webpack.cjs"
38
+ },
39
+ "./rollup": {
40
+ "types": "./dist/rollup.d.ts",
41
+ "import": "./dist/rollup.js",
42
+ "require": "./dist/rollup.cjs"
43
+ },
44
+ "./esbuild": {
45
+ "types": "./dist/esbuild.d.ts",
46
+ "import": "./dist/esbuild.js",
47
+ "require": "./dist/esbuild.cjs"
48
+ },
49
+ "./nuxt": {
50
+ "types": "./dist/nuxt.d.ts",
51
+ "import": "./dist/nuxt.js",
52
+ "require": "./dist/nuxt.cjs"
53
+ },
54
+ "./types": {
55
+ "types": "./dist/types.d.ts",
56
+ "import": "./dist/types.js",
57
+ "require": "./dist/types.cjs"
58
+ },
59
+ "./*": "./*"
60
+ },
61
+ "main": "dist/index.cjs",
62
+ "module": "dist/index.js",
63
+ "types": "dist/index.d.ts",
64
+ "typesVersions": {
65
+ "*": {
66
+ "*": [
67
+ "./dist/*",
68
+ "./*"
69
+ ]
70
+ }
71
+ },
72
+ "files": [
73
+ "dist"
74
+ ],
75
+ "peerDependencies": {
76
+ "@nuxt/kit": "^3",
77
+ "@nuxt/schema": "^3",
78
+ "esbuild": "*",
79
+ "rollup": "^3",
80
+ "vite": ">=3",
81
+ "webpack": "^4 || ^5"
82
+ },
83
+ "peerDependenciesMeta": {
84
+ "webpack": {
85
+ "optional": true
86
+ },
87
+ "rollup": {
88
+ "optional": true
89
+ },
90
+ "vite": {
91
+ "optional": true
92
+ },
93
+ "esbuild": {
94
+ "optional": true
95
+ },
96
+ "@nuxt/kit": {
97
+ "optional": true
98
+ },
99
+ "@nuxt/schema": {
100
+ "optional": true
101
+ }
102
+ },
103
+ "dependencies": {
104
+ "got": "^13.0.0",
105
+ "hostile": "^1.3.3",
106
+ "kill-port": "^2.0.1",
107
+ "picocolors": "^1.0.0",
108
+ "unplugin": "^1.4.0"
109
+ },
110
+ "devDependencies": {
111
+ "@antfu/eslint-config": "^0.40.2",
112
+ "@nuxt/kit": "^3.6.5",
113
+ "@nuxt/schema": "^3.6.5",
114
+ "@types/got": "^9.6.12",
115
+ "@types/hostile": "^1.3.3",
116
+ "@types/kill-port": "^2.0.3",
117
+ "@types/node": "^20.5.2",
118
+ "bumpp": "^9.2.0",
119
+ "chalk": "^5.3.0",
120
+ "eslint": "^8.47.0",
121
+ "esno": "^0.17.0",
122
+ "fast-glob": "^3.3.1",
123
+ "nodemon": "^3.0.1",
124
+ "rimraf": "^5.0.1",
125
+ "rollup": "^3.28.1",
126
+ "tsup": "^7.2.0",
127
+ "typescript": "^5.1.6",
128
+ "vite": "^4.4.9",
129
+ "vitest": "^0.34.2",
130
+ "webpack": "^5.88.2"
131
+ },
132
+ "scripts": {
133
+ "postinstall": "esno scripts/postinstall.ts",
134
+ "build": "tsup",
135
+ "dev": "tsup --watch src",
136
+ "build:fix": "esno scripts/postbuild.ts",
137
+ "lint": "eslint .",
138
+ "play": "npm -C playground run dev",
139
+ "release": "bumpp",
140
+ "start": "esno src/index.ts",
141
+ "test": "vitest"
142
+ }
143
+ }