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