@faasjs/vite 7.0.2 → 7.0.4
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/dist/index.cjs +44 -32
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +44 -32
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var child_process = require('child_process');
|
|
4
4
|
var http = require('http');
|
|
5
|
+
var logger = require('@faasjs/logger');
|
|
5
6
|
|
|
6
7
|
// src/index.ts
|
|
7
8
|
function viteFaasJsServer(options = {}) {
|
|
8
9
|
let config;
|
|
9
10
|
let childProcess = null;
|
|
11
|
+
const logger$1 = new logger.Logger("FaasJs:Vite");
|
|
10
12
|
return {
|
|
11
13
|
name: "vite:faasjs",
|
|
12
14
|
enforce: "pre",
|
|
@@ -31,18 +33,12 @@ function viteFaasJsServer(options = {}) {
|
|
|
31
33
|
stdio: "pipe",
|
|
32
34
|
shell: true
|
|
33
35
|
});
|
|
34
|
-
childProcess.stdout.on(
|
|
35
|
-
|
|
36
|
-
(data) => console.log(data.toString().trim())
|
|
37
|
-
);
|
|
38
|
-
childProcess.stderr.on(
|
|
39
|
-
"data",
|
|
40
|
-
(data) => console.error(data.toString().trim())
|
|
41
|
-
);
|
|
36
|
+
childProcess.stdout.on("data", (data) => logger$1.raw(data.toString().trim()));
|
|
37
|
+
childProcess.stderr.on("data", (data) => logger$1.raw(data.toString().trim()));
|
|
42
38
|
middlewares.use(async (req, res, next) => {
|
|
43
39
|
if (!req.url || req.method !== "POST") return next();
|
|
44
40
|
try {
|
|
45
|
-
const targetUrl = `http://localhost:${config.port}${req.url.replace(config.base, "")}`;
|
|
41
|
+
const targetUrl = `http://localhost:${config.port}${req.url.replace(config.base, "/")}`;
|
|
46
42
|
let body = null;
|
|
47
43
|
const chunks = [];
|
|
48
44
|
for await (const chunk of req) {
|
|
@@ -50,41 +46,57 @@ function viteFaasJsServer(options = {}) {
|
|
|
50
46
|
}
|
|
51
47
|
body = Buffer.concat(chunks).toString();
|
|
52
48
|
try {
|
|
53
|
-
body = JSON.parse(body);
|
|
49
|
+
if (body) body = JSON.parse(body);
|
|
54
50
|
} catch (e) {
|
|
55
|
-
|
|
51
|
+
logger$1.error("Failed to parse JSON:", e);
|
|
56
52
|
}
|
|
57
53
|
const headers = {};
|
|
58
54
|
for (const [key, value] of Object.entries(req.headers))
|
|
59
55
|
if (!["host", "connection"].includes(key)) headers[key] = value;
|
|
60
56
|
return new Promise((resolve) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
57
|
+
logger$1.debug(`Request ${targetUrl}`);
|
|
58
|
+
try {
|
|
59
|
+
const proxyReq = http.request(
|
|
60
|
+
targetUrl,
|
|
61
|
+
{
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers
|
|
64
|
+
},
|
|
65
|
+
(proxyRes) => {
|
|
66
|
+
res.statusCode = proxyRes.statusCode || 200;
|
|
67
|
+
for (const key of Object.keys(proxyRes.headers)) {
|
|
68
|
+
const value = proxyRes.headers[key];
|
|
69
|
+
if (value) res.setHeader(key, value);
|
|
70
|
+
}
|
|
71
|
+
proxyRes.pipe(res);
|
|
72
72
|
}
|
|
73
|
-
|
|
73
|
+
);
|
|
74
|
+
if (body) {
|
|
75
|
+
proxyReq.write(JSON.stringify(body));
|
|
76
|
+
}
|
|
77
|
+
proxyReq.on("error", (err) => {
|
|
78
|
+
logger$1.error(err);
|
|
79
|
+
next();
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
proxyReq.end();
|
|
83
|
+
} catch (err) {
|
|
84
|
+
logger$1.error(err);
|
|
85
|
+
if (!res.headersSent) {
|
|
86
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
87
|
+
res.write(
|
|
88
|
+
JSON.stringify({
|
|
89
|
+
error: { message: "Internal Server Error" }
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
res.end();
|
|
74
93
|
}
|
|
75
|
-
);
|
|
76
|
-
if (body) {
|
|
77
|
-
proxyReq.write(JSON.stringify(body));
|
|
78
|
-
}
|
|
79
|
-
proxyReq.on("error", (err) => {
|
|
80
|
-
console.error(`\x1B[031m${err.toString()}\x1B[39m`);
|
|
81
94
|
next();
|
|
82
95
|
resolve();
|
|
83
|
-
}
|
|
84
|
-
proxyReq.end();
|
|
96
|
+
}
|
|
85
97
|
});
|
|
86
98
|
} catch (error) {
|
|
87
|
-
|
|
99
|
+
logger$1.error(error);
|
|
88
100
|
return next();
|
|
89
101
|
}
|
|
90
102
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Plugin } from 'vite';
|
|
|
3
3
|
type ViteFaasJsServerOptions = {
|
|
4
4
|
/** faas server root path, default is vite's root */
|
|
5
5
|
root: string;
|
|
6
|
-
/** faas server base path, default is
|
|
6
|
+
/** faas server base path, default is vite's base */
|
|
7
7
|
base: string;
|
|
8
8
|
/** faas server port, 3000 as default */
|
|
9
9
|
port: number;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
2
|
import { request } from 'http';
|
|
3
|
+
import { Logger } from '@faasjs/logger';
|
|
3
4
|
|
|
4
5
|
// src/index.ts
|
|
5
6
|
function viteFaasJsServer(options = {}) {
|
|
6
7
|
let config;
|
|
7
8
|
let childProcess = null;
|
|
9
|
+
const logger = new Logger("FaasJs:Vite");
|
|
8
10
|
return {
|
|
9
11
|
name: "vite:faasjs",
|
|
10
12
|
enforce: "pre",
|
|
@@ -29,18 +31,12 @@ function viteFaasJsServer(options = {}) {
|
|
|
29
31
|
stdio: "pipe",
|
|
30
32
|
shell: true
|
|
31
33
|
});
|
|
32
|
-
childProcess.stdout.on(
|
|
33
|
-
|
|
34
|
-
(data) => console.log(data.toString().trim())
|
|
35
|
-
);
|
|
36
|
-
childProcess.stderr.on(
|
|
37
|
-
"data",
|
|
38
|
-
(data) => console.error(data.toString().trim())
|
|
39
|
-
);
|
|
34
|
+
childProcess.stdout.on("data", (data) => logger.raw(data.toString().trim()));
|
|
35
|
+
childProcess.stderr.on("data", (data) => logger.raw(data.toString().trim()));
|
|
40
36
|
middlewares.use(async (req, res, next) => {
|
|
41
37
|
if (!req.url || req.method !== "POST") return next();
|
|
42
38
|
try {
|
|
43
|
-
const targetUrl = `http://localhost:${config.port}${req.url.replace(config.base, "")}`;
|
|
39
|
+
const targetUrl = `http://localhost:${config.port}${req.url.replace(config.base, "/")}`;
|
|
44
40
|
let body = null;
|
|
45
41
|
const chunks = [];
|
|
46
42
|
for await (const chunk of req) {
|
|
@@ -48,41 +44,57 @@ function viteFaasJsServer(options = {}) {
|
|
|
48
44
|
}
|
|
49
45
|
body = Buffer.concat(chunks).toString();
|
|
50
46
|
try {
|
|
51
|
-
body = JSON.parse(body);
|
|
47
|
+
if (body) body = JSON.parse(body);
|
|
52
48
|
} catch (e) {
|
|
53
|
-
|
|
49
|
+
logger.error("Failed to parse JSON:", e);
|
|
54
50
|
}
|
|
55
51
|
const headers = {};
|
|
56
52
|
for (const [key, value] of Object.entries(req.headers))
|
|
57
53
|
if (!["host", "connection"].includes(key)) headers[key] = value;
|
|
58
54
|
return new Promise((resolve) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
55
|
+
logger.debug(`Request ${targetUrl}`);
|
|
56
|
+
try {
|
|
57
|
+
const proxyReq = request(
|
|
58
|
+
targetUrl,
|
|
59
|
+
{
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers
|
|
62
|
+
},
|
|
63
|
+
(proxyRes) => {
|
|
64
|
+
res.statusCode = proxyRes.statusCode || 200;
|
|
65
|
+
for (const key of Object.keys(proxyRes.headers)) {
|
|
66
|
+
const value = proxyRes.headers[key];
|
|
67
|
+
if (value) res.setHeader(key, value);
|
|
68
|
+
}
|
|
69
|
+
proxyRes.pipe(res);
|
|
70
70
|
}
|
|
71
|
-
|
|
71
|
+
);
|
|
72
|
+
if (body) {
|
|
73
|
+
proxyReq.write(JSON.stringify(body));
|
|
74
|
+
}
|
|
75
|
+
proxyReq.on("error", (err) => {
|
|
76
|
+
logger.error(err);
|
|
77
|
+
next();
|
|
78
|
+
resolve();
|
|
79
|
+
});
|
|
80
|
+
proxyReq.end();
|
|
81
|
+
} catch (err) {
|
|
82
|
+
logger.error(err);
|
|
83
|
+
if (!res.headersSent) {
|
|
84
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
85
|
+
res.write(
|
|
86
|
+
JSON.stringify({
|
|
87
|
+
error: { message: "Internal Server Error" }
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
res.end();
|
|
72
91
|
}
|
|
73
|
-
);
|
|
74
|
-
if (body) {
|
|
75
|
-
proxyReq.write(JSON.stringify(body));
|
|
76
|
-
}
|
|
77
|
-
proxyReq.on("error", (err) => {
|
|
78
|
-
console.error(`\x1B[031m${err.toString()}\x1B[39m`);
|
|
79
92
|
next();
|
|
80
93
|
resolve();
|
|
81
|
-
}
|
|
82
|
-
proxyReq.end();
|
|
94
|
+
}
|
|
83
95
|
});
|
|
84
96
|
} catch (error) {
|
|
85
|
-
|
|
97
|
+
logger.error(error);
|
|
86
98
|
return next();
|
|
87
99
|
}
|
|
88
100
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/vite",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@faasjs/cli": ">=7.0.
|
|
33
|
+
"@faasjs/cli": ">=7.0.4",
|
|
34
34
|
"vite": "*"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@faasjs/cli": ">=7.0.
|
|
37
|
+
"@faasjs/cli": ">=7.0.4",
|
|
38
38
|
"vite": "*"
|
|
39
39
|
},
|
|
40
40
|
"engines": {
|