@faasjs/dev 8.0.0-beta.5 → 8.0.0-beta.7
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/README.md +21 -31
- package/dist/chunk-CtajNgzt.mjs +36 -0
- package/dist/cli.cjs +67 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.mjs +66 -0
- package/dist/index.cjs +311 -333
- package/dist/index.d.ts +72 -97
- package/dist/index.mjs +274 -310
- package/dist/typegen-C6t9LIyi.cjs +183 -0
- package/dist/typegen-D5s91_xL.mjs +166 -0
- package/faas-types.mjs +7 -0
- package/package.json +23 -19
package/dist/index.cjs
CHANGED
|
@@ -1,356 +1,334 @@
|
|
|
1
|
-
'
|
|
2
|
-
|
|
3
|
-
var fs = require('fs');
|
|
4
|
-
var knex = require('knex');
|
|
5
|
-
var PgliteDialect = require('knex-pglite');
|
|
6
|
-
var zlib = require('zlib');
|
|
7
|
-
var deep_merge = require('@faasjs/deep_merge');
|
|
8
|
-
var http = require('@faasjs/http');
|
|
9
|
-
var load = require('@faasjs/load');
|
|
10
|
-
var logger = require('@faasjs/logger');
|
|
11
|
-
var func_star = require('@faasjs/func');
|
|
12
|
-
var path = require('path');
|
|
13
|
-
var server = require('@faasjs/server');
|
|
14
|
-
|
|
15
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
-
|
|
17
|
-
function _interopNamespace(e) {
|
|
18
|
-
if (e && e.__esModule) return e;
|
|
19
|
-
var n = Object.create(null);
|
|
20
|
-
if (e) {
|
|
21
|
-
Object.keys(e).forEach(function (k) {
|
|
22
|
-
if (k !== 'default') {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return e[k]; }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
n.default = e;
|
|
32
|
-
return Object.freeze(n);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var PgliteDialect__default = /*#__PURE__*/_interopDefault(PgliteDialect);
|
|
36
|
-
var func_star__namespace = /*#__PURE__*/_interopNamespace(func_star);
|
|
37
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
38
3
|
var __defProp = Object.defineProperty;
|
|
39
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
40
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
41
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
7
|
+
var __exportAll = (all, no_symbols) => {
|
|
8
|
+
let target = {};
|
|
9
|
+
for (var name in all) {
|
|
10
|
+
__defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: true
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
if (!no_symbols) {
|
|
16
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
45
19
|
};
|
|
46
20
|
var __copyProps = (to, from, except, desc) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
23
|
+
key = keys[i];
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: ((k) => from[k]).bind(null, key),
|
|
27
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
53
33
|
};
|
|
54
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget);
|
|
34
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
55
35
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
streamToString: () => streamToString,
|
|
65
|
-
test: () => test,
|
|
66
|
-
unmountFaasKnex: () => unmountFaasKnex,
|
|
67
|
-
viteFaasJsServer: () => viteFaasJsServer
|
|
68
|
-
});
|
|
69
|
-
function createPgliteKnex(config = {}, connection = {}) {
|
|
70
|
-
return knex.knex({
|
|
71
|
-
...config,
|
|
72
|
-
client: PgliteDialect__default.default,
|
|
73
|
-
connection
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
function mountFaasKnex(db, options = {}) {
|
|
77
|
-
const globalWithFaasKnex = globalThis;
|
|
78
|
-
const name = options.name || "knex";
|
|
79
|
-
if (!globalWithFaasKnex.FaasJS_Knex) globalWithFaasKnex.FaasJS_Knex = {};
|
|
80
|
-
globalWithFaasKnex.FaasJS_Knex[name] = {
|
|
81
|
-
adapter: db,
|
|
82
|
-
query: db,
|
|
83
|
-
config: options.config || {}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function unmountFaasKnex(name = "knex") {
|
|
87
|
-
const globalWithFaasKnex = globalThis;
|
|
88
|
-
if (!globalWithFaasKnex.FaasJS_Knex) return;
|
|
89
|
-
delete globalWithFaasKnex.FaasJS_Knex[name];
|
|
90
|
-
}
|
|
91
|
-
async function runPgliteSql(db, sql) {
|
|
92
|
-
if (!sql.trim()) return;
|
|
93
|
-
await db.raw(sql);
|
|
94
|
-
}
|
|
95
|
-
async function runPgliteSqlFile(db, filePath, options = {}) {
|
|
96
|
-
const stripUuidOsspExtension = options.stripUuidOsspExtension !== false;
|
|
97
|
-
let sql = fs.readFileSync(filePath, "utf8");
|
|
98
|
-
if (stripUuidOsspExtension)
|
|
99
|
-
sql = sql.replace(/CREATE EXTENSION IF NOT EXISTS "uuid-ossp";\s*/gi, "");
|
|
100
|
-
await runPgliteSql(db, sql);
|
|
101
|
-
}
|
|
36
|
+
//#endregion
|
|
37
|
+
const require_typegen = require('./typegen-C6t9LIyi.cjs');
|
|
38
|
+
let node_zlib = require("node:zlib");
|
|
39
|
+
let _faasjs_http = require("@faasjs/http");
|
|
40
|
+
let _faasjs_logger = require("@faasjs/logger");
|
|
41
|
+
let _faasjs_node_utils = require("@faasjs/node-utils");
|
|
42
|
+
let node_path = require("node:path");
|
|
43
|
+
let _faasjs_server = require("@faasjs/server");
|
|
102
44
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
while (true) {
|
|
118
|
-
const { done, value } = await reader.read();
|
|
119
|
-
if (done) break;
|
|
120
|
-
if (value) chunks.push(value);
|
|
121
|
-
}
|
|
122
|
-
} finally {
|
|
123
|
-
reader.releaseLock();
|
|
124
|
-
}
|
|
125
|
-
const decoder = new TextDecoder();
|
|
126
|
-
return decoder.decode(Buffer.concat(chunks.map((c) => Buffer.from(c))));
|
|
127
|
-
}
|
|
45
|
+
//#region src/test.ts
|
|
46
|
+
/**
|
|
47
|
+
* Test wrapper for a function.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { FuncWarper } from '@faasjs/dev'
|
|
51
|
+
* import Func from '../demo.func.ts'
|
|
52
|
+
*
|
|
53
|
+
* const func = new FuncWarper(Func)
|
|
54
|
+
*
|
|
55
|
+
* expect(await func.handler()).toEqual('Hello, world')
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
128
58
|
var FuncWarper = class {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
});
|
|
254
|
-
response.error = { message: error.message };
|
|
255
|
-
response.statusCode = 500;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (response?.headers && response.body && response.headers["content-type"]?.includes("json")) {
|
|
259
|
-
const parsedBody = JSON.parse(response.body);
|
|
260
|
-
response.data = parsedBody.data;
|
|
261
|
-
response.error = parsedBody.error;
|
|
262
|
-
}
|
|
263
|
-
if (this.http) {
|
|
264
|
-
response.cookie = this.http.cookie.content;
|
|
265
|
-
response.session = this.http.session.content;
|
|
266
|
-
}
|
|
267
|
-
this.logger.debug("response: %j", response);
|
|
268
|
-
return response;
|
|
269
|
-
}
|
|
59
|
+
file;
|
|
60
|
+
staging;
|
|
61
|
+
logger;
|
|
62
|
+
func;
|
|
63
|
+
config;
|
|
64
|
+
plugins;
|
|
65
|
+
_handler;
|
|
66
|
+
/**
|
|
67
|
+
* @param initBy {Func} A FaasJS function
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { FuncWarper } from '@faasjs/dev'
|
|
70
|
+
*
|
|
71
|
+
* new FuncWarper(__dirname + '/../demo.func.ts')
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
constructor(initBy) {
|
|
75
|
+
this.staging = process.env.FaasEnv ?? "default";
|
|
76
|
+
this.logger = new _faasjs_logger.Logger("TestCase");
|
|
77
|
+
this.func = initBy.default ? initBy.default : initBy;
|
|
78
|
+
if (this.func.filename) this.func.config = (0, _faasjs_node_utils.deepMerge)((0, _faasjs_node_utils.loadConfig)(process.cwd(), this.func.filename, this.staging, this.logger), this.func.config);
|
|
79
|
+
this.file = this.func.filename || "";
|
|
80
|
+
this.config = this.func.config;
|
|
81
|
+
this.plugins = this.func.plugins || [];
|
|
82
|
+
for (const plugin of this.plugins) {
|
|
83
|
+
if ([
|
|
84
|
+
"handler",
|
|
85
|
+
"config",
|
|
86
|
+
"plugins",
|
|
87
|
+
"logger",
|
|
88
|
+
"mount"
|
|
89
|
+
].includes(plugin.type)) continue;
|
|
90
|
+
this[plugin.type] = plugin;
|
|
91
|
+
}
|
|
92
|
+
this._handler = this.func.export().handler;
|
|
93
|
+
}
|
|
94
|
+
async mount(handler) {
|
|
95
|
+
if (!this.func.mounted) await this.func.mount();
|
|
96
|
+
if (handler) await handler(this);
|
|
97
|
+
}
|
|
98
|
+
async handler(event = Object.create(null), context = Object.create(null)) {
|
|
99
|
+
await this.mount();
|
|
100
|
+
const response = await this._handler(event, context);
|
|
101
|
+
this.logger.debug("response: %j", response);
|
|
102
|
+
return response;
|
|
103
|
+
}
|
|
104
|
+
async JSONhandler(body, options = Object.create(null)) {
|
|
105
|
+
await this.mount();
|
|
106
|
+
const headers = options.headers || Object.create(null);
|
|
107
|
+
if (this.http && this.http instanceof _faasjs_http.Http) {
|
|
108
|
+
if (options.cookie) for (const key in options.cookie) this.http.cookie.write(key, options.cookie[key]);
|
|
109
|
+
if (options.session) {
|
|
110
|
+
for (const key in options.session) this.http.session.write(key, options.session[key]);
|
|
111
|
+
this.http.session.update();
|
|
112
|
+
}
|
|
113
|
+
const cookie = this.http.cookie.headers()["Set-Cookie"]?.map((c) => c.split(";")[0]).join(";");
|
|
114
|
+
if (cookie) if (headers.cookie) headers.cookie += `;${cookie}`;
|
|
115
|
+
else headers.cookie = cookie;
|
|
116
|
+
}
|
|
117
|
+
const response = await this._handler({
|
|
118
|
+
httpMethod: "POST",
|
|
119
|
+
headers: Object.assign({ "content-type": "application/json" }, headers),
|
|
120
|
+
body: typeof body === "string" ? body : JSON.stringify(body)
|
|
121
|
+
});
|
|
122
|
+
if (response?.body instanceof ReadableStream) {
|
|
123
|
+
let stream = response.body;
|
|
124
|
+
const encoding = response.headers?.["Content-Encoding"] || response.headers?.["content-encoding"];
|
|
125
|
+
if (encoding) {
|
|
126
|
+
const chunks = [];
|
|
127
|
+
const reader = stream.getReader();
|
|
128
|
+
try {
|
|
129
|
+
while (true) {
|
|
130
|
+
const { done, value } = await reader.read();
|
|
131
|
+
if (done) break;
|
|
132
|
+
if (value) chunks.push(value);
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
this.logger.error("Failed to read ReadableStream: %s", error);
|
|
136
|
+
response.body = JSON.stringify({ error: { message: error.message } });
|
|
137
|
+
response.error = { message: error.message };
|
|
138
|
+
response.statusCode = 500;
|
|
139
|
+
reader.releaseLock();
|
|
140
|
+
return response;
|
|
141
|
+
}
|
|
142
|
+
reader.releaseLock();
|
|
143
|
+
const compressedBuffer = Buffer.concat(chunks);
|
|
144
|
+
try {
|
|
145
|
+
let decompressed;
|
|
146
|
+
if (encoding === "br") decompressed = (0, node_zlib.brotliDecompressSync)(compressedBuffer);
|
|
147
|
+
else if (encoding === "gzip") decompressed = (0, node_zlib.gunzipSync)(compressedBuffer);
|
|
148
|
+
else if (encoding === "deflate") decompressed = (0, node_zlib.inflateSync)(compressedBuffer);
|
|
149
|
+
else throw new Error(`Unsupported encoding: ${encoding}`);
|
|
150
|
+
stream = new ReadableStream({ start(controller) {
|
|
151
|
+
controller.enqueue(new Uint8Array(decompressed));
|
|
152
|
+
controller.close();
|
|
153
|
+
} });
|
|
154
|
+
} catch (error) {
|
|
155
|
+
this.logger.error("Failed to decompress: %s", error);
|
|
156
|
+
response.body = JSON.stringify({ error: { message: error.message } });
|
|
157
|
+
response.error = { message: error.message };
|
|
158
|
+
response.statusCode = 500;
|
|
159
|
+
return response;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
response.body = await (0, _faasjs_node_utils.streamToText)(stream);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
this.logger.error("Failed to decode ReadableStream: %s", error);
|
|
166
|
+
response.body = JSON.stringify({ error: { message: error.message } });
|
|
167
|
+
response.error = { message: error.message };
|
|
168
|
+
response.statusCode = 500;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (response?.headers && response.body && response.headers["content-type"]?.includes("json")) {
|
|
172
|
+
const parsedBody = JSON.parse(response.body);
|
|
173
|
+
response.data = parsedBody.data;
|
|
174
|
+
response.error = parsedBody.error;
|
|
175
|
+
}
|
|
176
|
+
if (this.http) {
|
|
177
|
+
response.cookie = this.http.cookie.content;
|
|
178
|
+
response.session = this.http.session.content;
|
|
179
|
+
}
|
|
180
|
+
this.logger.debug("response: %j", response);
|
|
181
|
+
return response;
|
|
182
|
+
}
|
|
270
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* A simple way to wrap a FaasJS function.
|
|
186
|
+
* @param initBy {Func} Full file path or a FaasJs function
|
|
187
|
+
*
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { test } from '@faasjs/dev'
|
|
190
|
+
* import Func from '../demo.func.ts'
|
|
191
|
+
*
|
|
192
|
+
* const func = test(Func)
|
|
193
|
+
*
|
|
194
|
+
* expect(await func.handler()).toEqual('Hello, world')
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
271
197
|
function test(initBy) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
198
|
+
const warper = new FuncWarper(initBy);
|
|
199
|
+
warper.mount = warper.mount.bind(warper);
|
|
200
|
+
warper.handler = warper.handler.bind(warper);
|
|
201
|
+
warper.JSONhandler = warper.JSONhandler.bind(warper);
|
|
202
|
+
return warper;
|
|
277
203
|
}
|
|
278
204
|
|
|
279
|
-
|
|
280
|
-
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/vite.ts
|
|
207
|
+
const TYPEGEN_DEBOUNCE = 120;
|
|
281
208
|
function normalizeBase(base) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
209
|
+
const normalized = base.startsWith("/") ? base : `/${base}`;
|
|
210
|
+
if (normalized === "/") return "/";
|
|
211
|
+
return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
285
212
|
}
|
|
286
213
|
function stripBase(url, base) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return url;
|
|
214
|
+
if (base === "/") return url;
|
|
215
|
+
const queryIndex = url.indexOf("?");
|
|
216
|
+
const pathname = queryIndex >= 0 ? url.slice(0, queryIndex) : url;
|
|
217
|
+
const search = queryIndex >= 0 ? url.slice(queryIndex) : "";
|
|
218
|
+
if (pathname === base) return `/${search}`;
|
|
219
|
+
if (pathname.startsWith(`${base}/`)) return `${pathname.slice(base.length)}${search}`;
|
|
220
|
+
return url;
|
|
295
221
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Create a Vite plugin that proxies POST requests to an in-process FaasJS server.
|
|
224
|
+
*
|
|
225
|
+
* It resolves server root/base from `src/faas.yaml` and strips `base` from
|
|
226
|
+
* request URL before forwarding to `@faasjs/server`.
|
|
227
|
+
*/
|
|
228
|
+
function viteFaasJsServer() {
|
|
229
|
+
let config;
|
|
230
|
+
let server = null;
|
|
231
|
+
const logger = new _faasjs_logger.Logger("FaasJs:Vite");
|
|
232
|
+
return {
|
|
233
|
+
name: "vite:faasjs",
|
|
234
|
+
enforce: "pre",
|
|
235
|
+
configResolved(resolvedConfig) {
|
|
236
|
+
const serverConfig = require_typegen.resolveServerConfig(resolvedConfig.root, logger, resolvedConfig.base);
|
|
237
|
+
config = {
|
|
238
|
+
root: serverConfig.root,
|
|
239
|
+
base: normalizeBase(serverConfig.base)
|
|
240
|
+
};
|
|
241
|
+
},
|
|
242
|
+
configureServer: async ({ middlewares, watcher }) => {
|
|
243
|
+
if (process.env.VITEST) {
|
|
244
|
+
logger.debug("Skipping faas server in vitest environment");
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (!config) throw new Error("viteFaasJsServer: config is not resolved");
|
|
248
|
+
server = new _faasjs_server.Server((0, node_path.join)(config.root, "src"));
|
|
249
|
+
const runTypegen = async () => {
|
|
250
|
+
try {
|
|
251
|
+
const result = await require_typegen.generateFaasTypes({ root: config.root });
|
|
252
|
+
logger.debug("[faas-types] %s %s (%i routes)", result.changed ? "generated" : "up-to-date", result.output, result.routeCount);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
logger.error("[faas-types] %s", error.message);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
let timer = null;
|
|
258
|
+
let runningTypegen = false;
|
|
259
|
+
let pendingTypegen = false;
|
|
260
|
+
const flushTypegen = async () => {
|
|
261
|
+
if (runningTypegen || !pendingTypegen) return;
|
|
262
|
+
pendingTypegen = false;
|
|
263
|
+
runningTypegen = true;
|
|
264
|
+
try {
|
|
265
|
+
await runTypegen();
|
|
266
|
+
} finally {
|
|
267
|
+
runningTypegen = false;
|
|
268
|
+
if (pendingTypegen) flushTypegen();
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const scheduleTypegen = () => {
|
|
272
|
+
pendingTypegen = true;
|
|
273
|
+
if (timer) clearTimeout(timer);
|
|
274
|
+
timer = setTimeout(() => {
|
|
275
|
+
flushTypegen();
|
|
276
|
+
}, TYPEGEN_DEBOUNCE);
|
|
277
|
+
};
|
|
278
|
+
await runTypegen();
|
|
279
|
+
watcher.on("all", (_eventName, filePath) => {
|
|
280
|
+
if (!require_typegen.isTypegenSourceFile(filePath)) return;
|
|
281
|
+
scheduleTypegen();
|
|
282
|
+
});
|
|
283
|
+
middlewares.use(async (req, res, next) => {
|
|
284
|
+
if (!req.url || req.method !== "POST" || !server) return next();
|
|
285
|
+
const originalUrl = req.url;
|
|
286
|
+
req.url = stripBase(req.url, config.base);
|
|
287
|
+
try {
|
|
288
|
+
logger.debug(`Request ${req.url}`);
|
|
289
|
+
await server.handle(req, res, { requestedAt: Date.now() });
|
|
290
|
+
} catch (error) {
|
|
291
|
+
logger.error(error);
|
|
292
|
+
if (!res.headersSent && !res.writableEnded) {
|
|
293
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
294
|
+
res.write(JSON.stringify({ error: { message: "Internal Server Error" } }));
|
|
295
|
+
res.end();
|
|
296
|
+
}
|
|
297
|
+
} finally {
|
|
298
|
+
req.url = originalUrl;
|
|
299
|
+
}
|
|
300
|
+
if (!res.writableEnded) next();
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
};
|
|
346
304
|
}
|
|
347
305
|
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/index.ts
|
|
308
|
+
var src_exports = /* @__PURE__ */ __exportAll({
|
|
309
|
+
FuncWarper: () => FuncWarper,
|
|
310
|
+
generateFaasTypes: () => require_typegen.generateFaasTypes,
|
|
311
|
+
isTypegenSourceFile: () => require_typegen.isTypegenSourceFile,
|
|
312
|
+
streamToObject: () => _faasjs_node_utils.streamToObject,
|
|
313
|
+
streamToString: () => _faasjs_node_utils.streamToString,
|
|
314
|
+
streamToText: () => _faasjs_node_utils.streamToText,
|
|
315
|
+
test: () => test,
|
|
316
|
+
viteFaasJsServer: () => viteFaasJsServer
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
//#endregion
|
|
348
320
|
exports.FuncWarper = FuncWarper;
|
|
349
|
-
exports.
|
|
350
|
-
exports.
|
|
351
|
-
exports.
|
|
352
|
-
exports.
|
|
353
|
-
exports.
|
|
321
|
+
exports.generateFaasTypes = require_typegen.generateFaasTypes;
|
|
322
|
+
exports.isTypegenSourceFile = require_typegen.isTypegenSourceFile;
|
|
323
|
+
exports.streamToObject = _faasjs_node_utils.streamToObject;
|
|
324
|
+
exports.streamToString = _faasjs_node_utils.streamToString;
|
|
325
|
+
exports.streamToText = _faasjs_node_utils.streamToText;
|
|
354
326
|
exports.test = test;
|
|
355
|
-
exports.unmountFaasKnex = unmountFaasKnex;
|
|
356
327
|
exports.viteFaasJsServer = viteFaasJsServer;
|
|
328
|
+
var _faasjs_func = require("@faasjs/func");
|
|
329
|
+
Object.keys(_faasjs_func).forEach(function (k) {
|
|
330
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
331
|
+
enumerable: true,
|
|
332
|
+
get: function () { return _faasjs_func[k]; }
|
|
333
|
+
});
|
|
334
|
+
});
|