@sdkgen/node-runtime 1.5.5 → 2.0.0
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/spec/rest/rest.spec.js +11 -11
- package/dist/spec/runtime/errors.spec.js +5 -5
- package/dist/spec/runtime/middleware.spec.js +4 -22
- package/dist/spec/simple/legacyNodeClient.js +2 -2
- package/dist/spec/simple/simple.spec.js +6 -6
- package/dist/spec/types.d.ts +1 -0
- package/dist/spec/types.js +60 -0
- package/dist/spec/types.spec.js +63 -47
- package/dist/src/api-config.d.ts +0 -12
- package/dist/src/api-config.js +1 -12
- package/dist/src/encode-decode.d.ts +14 -2
- package/dist/src/encode-decode.js +68 -6
- package/dist/src/execute.js +4 -4
- package/dist/src/http-client.d.ts +2 -1
- package/dist/src/http-client.js +18 -10
- package/dist/src/http-server.d.ts +1 -1
- package/dist/src/http-server.js +92 -70
- package/dist/src/index.d.ts +7 -7
- package/dist/src/index.js +16 -17
- package/dist/src/swagger.js +24 -14
- package/dist/src/test-wrapper.js +5 -5
- package/dist/tsconfig.tsbuildinfo +1 -2081
- package/package.json +28 -27
package/dist/src/execute.js
CHANGED
|
@@ -14,9 +14,9 @@ async function executeRequest(ctx, apiConfig) {
|
|
|
14
14
|
if (!functionDescription || !functionImplementation) {
|
|
15
15
|
throw new error_1.Fatal(`Function does not exist: ${ctx.request.name}`);
|
|
16
16
|
}
|
|
17
|
-
const args = encode_decode_1.decode(apiConfig.astJson.typeTable, `${ctx.request.name}.args`, functionDescription.args, ctx.request.args);
|
|
18
|
-
const ret = await functionImplementation(ctx, args);
|
|
19
|
-
const encodedRet = encode_decode_1.encode(apiConfig.astJson.typeTable, `${ctx.request.name}.ret`, functionDescription.ret, ret);
|
|
17
|
+
const args = (0, encode_decode_1.decode)(apiConfig.astJson.typeTable, `${ctx.request.name}.args`, functionDescription.args, ctx.request.args);
|
|
18
|
+
const ret = (await functionImplementation(ctx, args));
|
|
19
|
+
const encodedRet = (0, encode_decode_1.encode)(apiConfig.astJson.typeTable, `${ctx.request.name}.ret`, functionDescription.ret, ret);
|
|
20
20
|
return { result: encodedRet };
|
|
21
21
|
}
|
|
22
22
|
catch (error) {
|
|
@@ -43,7 +43,7 @@ async function executeRequest(ctx, apiConfig) {
|
|
|
43
43
|
const allowedErrors = functionAst.annotations.filter(ann => ann instanceof parser_1.ThrowsAnnotation).map(ann => ann.error);
|
|
44
44
|
if (typeof reply.error !== "object" ||
|
|
45
45
|
reply.error === null ||
|
|
46
|
-
!utils_1.has(reply.error, "type") ||
|
|
46
|
+
!(0, utils_1.has)(reply.error, "type") ||
|
|
47
47
|
typeof reply.error.type !== "string" ||
|
|
48
48
|
(allowedErrors.length > 0 && !allowedErrors.includes(reply.error.type)) ||
|
|
49
49
|
!apiConfig.astJson.errors.map(error => (typeof error === "string" ? error : error[0])).includes(reply.error.type)) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AstJson } from "@sdkgen/parser";
|
|
2
|
+
import type { PartialDeep } from "type-fest";
|
|
2
3
|
import type { Context } from "./context";
|
|
3
4
|
import type { SdkgenError, SdkgenErrorWithData } from "./error";
|
|
4
5
|
import type { DeepReadonly } from "./utils";
|
|
@@ -11,6 +12,6 @@ export declare class SdkgenHttpClient {
|
|
|
11
12
|
private baseUrl;
|
|
12
13
|
extra: Map<string, unknown>;
|
|
13
14
|
constructor(baseUrl: string, astJson: DeepReadonly<AstJson>, errClasses: ErrClasses);
|
|
14
|
-
makeRequest(ctx: Context | null, functionName: string, args: unknown): Promise<any>;
|
|
15
|
+
makeRequest(ctx: PartialDeep<Context> | null, functionName: string, args: unknown): Promise<any>;
|
|
15
16
|
}
|
|
16
17
|
export {};
|
package/dist/src/http-client.js
CHANGED
|
@@ -17,16 +17,21 @@ class SdkgenHttpClient {
|
|
|
17
17
|
this.baseUrl = new url_1.URL(baseUrl);
|
|
18
18
|
}
|
|
19
19
|
async makeRequest(ctx, functionName, args) {
|
|
20
|
+
var _a, _b, _c, _d;
|
|
20
21
|
const func = this.astJson.functionTable[functionName];
|
|
21
22
|
if (!func) {
|
|
22
23
|
throw new Error(`Unknown function ${functionName}`);
|
|
23
24
|
}
|
|
25
|
+
const extra = {};
|
|
26
|
+
for (const [key, value] of this.extra) {
|
|
27
|
+
extra[key] = value;
|
|
28
|
+
}
|
|
24
29
|
const requestBody = JSON.stringify({
|
|
25
|
-
args: encode_decode_1.encode(this.astJson.typeTable, `${functionName}.args`, func.args, args),
|
|
26
|
-
deviceInfo: (ctx === null || ctx === void 0 ? void 0 : ctx.request) ?
|
|
27
|
-
extra: Object.assign(Object.assign({},
|
|
30
|
+
args: (0, encode_decode_1.encode)(this.astJson.typeTable, `${functionName}.args`, func.args, args),
|
|
31
|
+
deviceInfo: (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _a === void 0 ? void 0 : _a.deviceInfo) !== null && _b !== void 0 ? _b : { id: (0, os_1.hostname)(), type: "node" },
|
|
32
|
+
extra: Object.assign(Object.assign({}, extra), (_c = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _c === void 0 ? void 0 : _c.extra),
|
|
28
33
|
name: functionName,
|
|
29
|
-
requestId: (ctx === null || ctx === void 0 ? void 0 : ctx.request) ? ctx.request.id + crypto_1.randomBytes(6).toString("hex") : crypto_1.randomBytes(16).toString("hex"),
|
|
34
|
+
requestId: ((_d = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _d === void 0 ? void 0 : _d.id) ? ctx.request.id + (0, crypto_1.randomBytes)(6).toString("hex") : (0, crypto_1.randomBytes)(16).toString("hex"),
|
|
30
35
|
version: 3,
|
|
31
36
|
});
|
|
32
37
|
const options = {
|
|
@@ -34,6 +39,9 @@ class SdkgenHttpClient {
|
|
|
34
39
|
method: "POST",
|
|
35
40
|
path: this.baseUrl.pathname,
|
|
36
41
|
port: this.baseUrl.port,
|
|
42
|
+
headers: {
|
|
43
|
+
"content-type": "application/sdkgen",
|
|
44
|
+
},
|
|
37
45
|
};
|
|
38
46
|
const encodedRet = await new Promise((resolve, reject) => {
|
|
39
47
|
const req = (this.baseUrl.protocol === "http:" ? http_1.request : https_1.request)(options, res => {
|
|
@@ -44,11 +52,11 @@ class SdkgenHttpClient {
|
|
|
44
52
|
res.on("end", () => {
|
|
45
53
|
try {
|
|
46
54
|
const response = JSON.parse(data);
|
|
47
|
-
if (utils_1.has(response, "error") && response.error) {
|
|
55
|
+
if ((0, utils_1.has)(response, "error") && response.error) {
|
|
48
56
|
reject(response.error);
|
|
49
57
|
}
|
|
50
58
|
else {
|
|
51
|
-
resolve(utils_1.has(response, "result") ? response.result : null);
|
|
59
|
+
resolve((0, utils_1.has)(response, "result") ? response.result : null);
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
62
|
catch (error) {
|
|
@@ -68,13 +76,13 @@ class SdkgenHttpClient {
|
|
|
68
76
|
req.write(requestBody);
|
|
69
77
|
req.end();
|
|
70
78
|
}).catch(error => {
|
|
71
|
-
if (utils_1.has(error, "type") && utils_1.has(error, "message") && typeof error.type === "string" && typeof error.message === "string") {
|
|
79
|
+
if ((0, utils_1.has)(error, "type") && (0, utils_1.has)(error, "message") && typeof error.type === "string" && typeof error.message === "string") {
|
|
72
80
|
const errClass = this.errClasses[error.type];
|
|
73
81
|
if (errClass) {
|
|
74
82
|
const errorJson = this.astJson.errors.find(err => (Array.isArray(err) ? err[0] === error.type : err === error.type));
|
|
75
83
|
if (errorJson) {
|
|
76
|
-
if (Array.isArray(errorJson) && utils_1.has(error, "data")) {
|
|
77
|
-
throw new errClass(error.message, encode_decode_1.decode(this.astJson.typeTable, `${errClass.name}.data`, errorJson[1], error.data));
|
|
84
|
+
if (Array.isArray(errorJson) && (0, utils_1.has)(error, "data")) {
|
|
85
|
+
throw new errClass(error.message, (0, encode_decode_1.decode)(this.astJson.typeTable, `${errClass.name}.data`, errorJson[1], error.data));
|
|
78
86
|
}
|
|
79
87
|
else {
|
|
80
88
|
throw new errClass(error.message, undefined);
|
|
@@ -88,7 +96,7 @@ class SdkgenHttpClient {
|
|
|
88
96
|
}
|
|
89
97
|
});
|
|
90
98
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
91
|
-
return encode_decode_1.decode(this.astJson.typeTable, `${functionName}.ret`, func.ret, encodedRet);
|
|
99
|
+
return (0, encode_decode_1.decode)(this.astJson.typeTable, `${functionName}.ret`, func.ret, encodedRet);
|
|
92
100
|
}
|
|
93
101
|
}
|
|
94
102
|
exports.SdkgenHttpClient = SdkgenHttpClient;
|
|
@@ -10,6 +10,7 @@ export declare class SdkgenHttpServer<ExtraContextT = unknown> {
|
|
|
10
10
|
private handlers;
|
|
11
11
|
dynamicCorsOrigin: boolean;
|
|
12
12
|
introspection: boolean;
|
|
13
|
+
log: (message: string) => void;
|
|
13
14
|
private hasSwagger;
|
|
14
15
|
private ignoredUrlPrefix;
|
|
15
16
|
constructor(apiConfig: BaseApiConfig<ExtraContextT>, extraContext: ExtraContextT);
|
|
@@ -23,7 +24,6 @@ export declare class SdkgenHttpServer<ExtraContextT = unknown> {
|
|
|
23
24
|
private findBestHandler;
|
|
24
25
|
private attachRestHandlers;
|
|
25
26
|
private handleRequest;
|
|
26
|
-
private log;
|
|
27
27
|
private handleRequestWithBody;
|
|
28
28
|
private executeRequest;
|
|
29
29
|
private parseRequest;
|
package/dist/src/http-server.js
CHANGED
|
@@ -11,9 +11,13 @@ const os_1 = require("os");
|
|
|
11
11
|
const querystring_1 = require("querystring");
|
|
12
12
|
const url_1 = require("url");
|
|
13
13
|
const util_1 = require("util");
|
|
14
|
+
const csharp_generator_1 = require("@sdkgen/csharp-generator");
|
|
14
15
|
const dart_generator_1 = require("@sdkgen/dart-generator");
|
|
16
|
+
const fsharp_generator_1 = require("@sdkgen/fsharp-generator");
|
|
17
|
+
const kotlin_generator_1 = require("@sdkgen/kotlin-generator");
|
|
15
18
|
const parser_1 = require("@sdkgen/parser");
|
|
16
19
|
const playground_1 = require("@sdkgen/playground");
|
|
20
|
+
const swift_generator_1 = require("@sdkgen/swift-generator");
|
|
17
21
|
const typescript_generator_1 = require("@sdkgen/typescript-generator");
|
|
18
22
|
const busboy_1 = __importDefault(require("busboy"));
|
|
19
23
|
const file_type_1 = __importDefault(require("file-type"));
|
|
@@ -33,16 +37,25 @@ class SdkgenHttpServer {
|
|
|
33
37
|
this.handlers = [];
|
|
34
38
|
this.dynamicCorsOrigin = true;
|
|
35
39
|
this.introspection = true;
|
|
40
|
+
this.log = (message) => {
|
|
41
|
+
console.log(`${new Date().toISOString()} ${message}`);
|
|
42
|
+
};
|
|
36
43
|
this.hasSwagger = false;
|
|
37
44
|
this.ignoredUrlPrefix = "";
|
|
38
|
-
this.httpServer = http_1.createServer(this.handleRequest.bind(this));
|
|
45
|
+
this.httpServer = (0, http_1.createServer)(this.handleRequest.bind(this));
|
|
39
46
|
this.enableCors();
|
|
40
47
|
this.attachRestHandlers();
|
|
41
48
|
const targetTable = [
|
|
49
|
+
["/targets/android/client.kt", (ast) => (0, kotlin_generator_1.generateAndroidClientSource)(ast, true)],
|
|
50
|
+
["/targets/android/client_without_callbacks.kt", (ast) => (0, kotlin_generator_1.generateAndroidClientSource)(ast, false)],
|
|
51
|
+
["/targets/dotnet/api.cs", csharp_generator_1.generateCSharpServerSource],
|
|
52
|
+
["/targets/dotnet/api.fs", fsharp_generator_1.generateFSharpServerSource],
|
|
53
|
+
["/targets/flutter/client.dart", dart_generator_1.generateDartClientSource],
|
|
54
|
+
["/targets/ios/client.swift", (ast) => (0, swift_generator_1.generateSwiftClientSource)(ast, false)],
|
|
55
|
+
["/targets/ios/client-rx.swift", (ast) => (0, swift_generator_1.generateSwiftClientSource)(ast, true)],
|
|
42
56
|
["/targets/node/api.ts", typescript_generator_1.generateNodeServerSource],
|
|
43
57
|
["/targets/node/client.ts", typescript_generator_1.generateNodeClientSource],
|
|
44
58
|
["/targets/web/client.ts", typescript_generator_1.generateBrowserClientSource],
|
|
45
|
-
["/targets/flutter/client.dart", dart_generator_1.generateDartClientSource],
|
|
46
59
|
];
|
|
47
60
|
for (const [path, generateFn] of targetTable) {
|
|
48
61
|
this.addHttpHandler("GET", path, (_req, res) => {
|
|
@@ -82,7 +95,7 @@ class SdkgenHttpServer {
|
|
|
82
95
|
if (req.url) {
|
|
83
96
|
req.url = req.url.endsWith("/playground") ? req.url.replace(/\/playground/u, "/index.html") : req.url.replace(/\/playground/u, "");
|
|
84
97
|
}
|
|
85
|
-
serve_handler_1.default(req, res, {
|
|
98
|
+
(0, serve_handler_1.default)(req, res, {
|
|
86
99
|
cleanUrls: false,
|
|
87
100
|
directoryListing: false,
|
|
88
101
|
etag: true,
|
|
@@ -132,7 +145,7 @@ class SdkgenHttpServer {
|
|
|
132
145
|
});
|
|
133
146
|
}
|
|
134
147
|
async close() {
|
|
135
|
-
return util_1.promisify(this.httpServer.close.bind(this.httpServer))();
|
|
148
|
+
return (0, util_1.promisify)(this.httpServer.close.bind(this.httpServer))();
|
|
136
149
|
}
|
|
137
150
|
enableCors() {
|
|
138
151
|
this.addHeader("Access-Control-Allow-Methods", "DELETE, HEAD, PUT, POST, PATCH, GET, OPTIONS");
|
|
@@ -201,7 +214,7 @@ class SdkgenHttpServer {
|
|
|
201
214
|
continue;
|
|
202
215
|
}
|
|
203
216
|
if (!this.hasSwagger) {
|
|
204
|
-
swagger_1.setupSwagger(this);
|
|
217
|
+
(0, swagger_1.setupSwagger)(this);
|
|
205
218
|
this.hasSwagger = true;
|
|
206
219
|
}
|
|
207
220
|
const pathFragments = ann.path.split(/\{\w+\}/u);
|
|
@@ -214,15 +227,15 @@ class SdkgenHttpServer {
|
|
|
214
227
|
}
|
|
215
228
|
pathRegex += "/?$";
|
|
216
229
|
for (const header of ann.headers.keys()) {
|
|
217
|
-
this.addHeader("Access-Control-Allow-Headers", header);
|
|
230
|
+
this.addHeader("Access-Control-Allow-Headers", header.toLowerCase());
|
|
218
231
|
}
|
|
219
232
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
220
233
|
this.addHttpHandler(ann.method, new RegExp(pathRegex, "u"), async (req, res, body) => {
|
|
221
|
-
var _a, _b, _c, _d, _e, _f;
|
|
234
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
222
235
|
try {
|
|
223
236
|
const args = {};
|
|
224
237
|
const files = [];
|
|
225
|
-
const { pathname, query } = url_1.parse((_a = req.url) !== null && _a !== void 0 ? _a : "");
|
|
238
|
+
const { pathname, query } = (0, url_1.parse)((_a = req.url) !== null && _a !== void 0 ? _a : "");
|
|
226
239
|
const match = pathname === null || pathname === void 0 ? void 0 : pathname.match(pathRegex);
|
|
227
240
|
if (!match) {
|
|
228
241
|
res.statusCode = 404;
|
|
@@ -234,40 +247,49 @@ class SdkgenHttpServer {
|
|
|
234
247
|
const argValue = match[i + 1];
|
|
235
248
|
simpleArgs.set(argName, argValue);
|
|
236
249
|
}
|
|
237
|
-
const parsedQuery = query ? querystring_1.parse(query) : {};
|
|
250
|
+
const parsedQuery = query ? (0, querystring_1.parse)(query) : {};
|
|
238
251
|
for (const argName of ann.queryVariables) {
|
|
239
252
|
const argValue = (_b = parsedQuery[argName]) !== null && _b !== void 0 ? _b : null;
|
|
253
|
+
if (argValue === null) {
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
240
256
|
simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
|
|
241
257
|
}
|
|
242
258
|
for (const [headerName, argName] of ann.headers) {
|
|
243
|
-
const argValue = (_c = req.headers[headerName]) !== null && _c !== void 0 ? _c : null;
|
|
259
|
+
const argValue = (_c = req.headers[headerName.toLowerCase()]) !== null && _c !== void 0 ? _c : null;
|
|
260
|
+
if (argValue === null) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
244
263
|
simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
|
|
245
264
|
}
|
|
246
265
|
if (!ann.bodyVariable && ((_d = req.headers["content-type"]) === null || _d === void 0 ? void 0 : _d.match(/^application\/x-www-form-urlencoded/iu))) {
|
|
247
|
-
const parsedBody = querystring_1.parse(body.toString());
|
|
266
|
+
const parsedBody = (0, querystring_1.parse)(body.toString());
|
|
248
267
|
for (const argName of ann.queryVariables) {
|
|
249
268
|
const argValue = (_e = parsedBody[argName]) !== null && _e !== void 0 ? _e : null;
|
|
269
|
+
if (argValue === null) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
250
272
|
simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
|
|
251
273
|
}
|
|
252
274
|
}
|
|
253
275
|
else if (!ann.bodyVariable && ((_f = req.headers["content-type"]) === null || _f === void 0 ? void 0 : _f.match(/^multipart\/form-data/iu))) {
|
|
254
|
-
const busboy =
|
|
276
|
+
const busboy = (0, busboy_1.default)({ headers: req.headers });
|
|
255
277
|
const filePromises = [];
|
|
256
278
|
busboy.on("field", (field, value) => {
|
|
257
279
|
if (ann.queryVariables.includes(field)) {
|
|
258
280
|
simpleArgs.set(field, `${value}`);
|
|
259
281
|
}
|
|
260
282
|
});
|
|
261
|
-
busboy.on("file", (_field,
|
|
262
|
-
const tempName = crypto_1.randomBytes(32).toString("hex");
|
|
263
|
-
const writeStream = fs_1.createWriteStream(tempName);
|
|
283
|
+
busboy.on("file", (_field, stream, info) => {
|
|
284
|
+
const tempName = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
285
|
+
const writeStream = (0, fs_1.createWriteStream)(tempName);
|
|
264
286
|
filePromises.push(new Promise((resolve, reject) => {
|
|
265
287
|
writeStream.on("error", reject);
|
|
266
288
|
writeStream.on("close", () => {
|
|
267
|
-
const contents = fs_1.createReadStream(tempName);
|
|
268
|
-
files.push({ contents, name });
|
|
289
|
+
const contents = (0, fs_1.createReadStream)(tempName);
|
|
290
|
+
files.push({ contents, name: info.filename });
|
|
269
291
|
contents.on("open", () => {
|
|
270
|
-
fs_1.unlink(tempName, err => {
|
|
292
|
+
(0, fs_1.unlink)(tempName, err => {
|
|
271
293
|
if (err) {
|
|
272
294
|
reject(err);
|
|
273
295
|
}
|
|
@@ -278,7 +300,7 @@ class SdkgenHttpServer {
|
|
|
278
300
|
});
|
|
279
301
|
});
|
|
280
302
|
writeStream.on("open", () => {
|
|
281
|
-
|
|
303
|
+
stream.pipe(writeStream);
|
|
282
304
|
});
|
|
283
305
|
}));
|
|
284
306
|
});
|
|
@@ -292,7 +314,7 @@ class SdkgenHttpServer {
|
|
|
292
314
|
else if (ann.bodyVariable) {
|
|
293
315
|
const argName = ann.bodyVariable;
|
|
294
316
|
const arg = op.args.find(x => x.name === argName);
|
|
295
|
-
if (req.headers["content-type"]
|
|
317
|
+
if (/application\/json/iu.test((_g = req.headers["content-type"]) !== null && _g !== void 0 ? _g : "")) {
|
|
296
318
|
args[argName] = JSON.parse(body.toString());
|
|
297
319
|
}
|
|
298
320
|
else if (arg) {
|
|
@@ -373,7 +395,7 @@ class SdkgenHttpServer {
|
|
|
373
395
|
args[argName] = argValue;
|
|
374
396
|
}
|
|
375
397
|
}
|
|
376
|
-
const ip = request_ip_1.getClientIp(req);
|
|
398
|
+
const ip = (0, request_ip_1.getClientIp)(req);
|
|
377
399
|
if (!ip) {
|
|
378
400
|
throw new Error("Couldn't determine client IP");
|
|
379
401
|
}
|
|
@@ -381,7 +403,7 @@ class SdkgenHttpServer {
|
|
|
381
403
|
args,
|
|
382
404
|
deviceInfo: {
|
|
383
405
|
fingerprint: null,
|
|
384
|
-
id: crypto_1.randomBytes(16).toString("hex"),
|
|
406
|
+
id: (0, crypto_1.randomBytes)(16).toString("hex"),
|
|
385
407
|
language: null,
|
|
386
408
|
platform: null,
|
|
387
409
|
timezone: null,
|
|
@@ -391,7 +413,7 @@ class SdkgenHttpServer {
|
|
|
391
413
|
extra: {},
|
|
392
414
|
files,
|
|
393
415
|
headers: req.headers,
|
|
394
|
-
id: crypto_1.randomBytes(16).toString("hex"),
|
|
416
|
+
id: (0, crypto_1.randomBytes)(16).toString("hex"),
|
|
395
417
|
ip,
|
|
396
418
|
name: op.name,
|
|
397
419
|
version: 3,
|
|
@@ -464,7 +486,8 @@ class SdkgenHttpServer {
|
|
|
464
486
|
.then(() => {
|
|
465
487
|
res.write(buffer);
|
|
466
488
|
res.end();
|
|
467
|
-
})
|
|
489
|
+
})
|
|
490
|
+
.catch(() => { });
|
|
468
491
|
}
|
|
469
492
|
else {
|
|
470
493
|
res.setHeader("content-type", "application/json");
|
|
@@ -520,26 +543,24 @@ class SdkgenHttpServer {
|
|
|
520
543
|
}
|
|
521
544
|
const body = [];
|
|
522
545
|
req.on("data", chunk => body.push(chunk));
|
|
523
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
524
546
|
req.on("end", () => {
|
|
525
|
-
this.handleRequestWithBody(req, res, Buffer.concat(body), hrStart).catch(e => this.writeReply(res, null, { error: e }, hrStart));
|
|
547
|
+
this.handleRequestWithBody(req, res, Buffer.concat(body), hrStart).catch((e) => this.writeReply(res, null, { error: e }, hrStart));
|
|
526
548
|
});
|
|
527
549
|
}
|
|
528
|
-
log(message) {
|
|
529
|
-
console.log(`${new Date().toISOString()} ${message}`);
|
|
530
|
-
}
|
|
531
550
|
async handleRequestWithBody(req, res, body, hrStart) {
|
|
532
|
-
var _a;
|
|
533
|
-
const { pathname, query } = url_1.parse((_a = req.url) !== null && _a !== void 0 ? _a : "");
|
|
551
|
+
var _a, _b;
|
|
552
|
+
const { pathname, query } = (0, url_1.parse)((_a = req.url) !== null && _a !== void 0 ? _a : "");
|
|
534
553
|
let path = pathname !== null && pathname !== void 0 ? pathname : "";
|
|
535
554
|
if (path.startsWith(this.ignoredUrlPrefix)) {
|
|
536
555
|
path = path.slice(this.ignoredUrlPrefix.length);
|
|
537
556
|
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
557
|
+
if (!((_b = req.headers["content-type"]) === null || _b === void 0 ? void 0 : _b.match(/application\/sdkgen/iu))) {
|
|
558
|
+
const externalHandler = this.findBestHandler(path, req);
|
|
559
|
+
if (externalHandler) {
|
|
560
|
+
this.log(`HTTP ${req.method} ${path}${query ? `?${query}` : ""}`);
|
|
561
|
+
externalHandler.handler(req, res, body);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
543
564
|
}
|
|
544
565
|
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
545
566
|
if (req.method === "HEAD") {
|
|
@@ -553,9 +574,8 @@ class SdkgenHttpServer {
|
|
|
553
574
|
res.end();
|
|
554
575
|
return;
|
|
555
576
|
}
|
|
556
|
-
let ok;
|
|
577
|
+
let ok = true;
|
|
557
578
|
try {
|
|
558
|
-
ok = await this.apiConfig.hook.onHealthCheck();
|
|
559
579
|
for (const healthCheck of this.healthChecks) {
|
|
560
580
|
if (!ok) {
|
|
561
581
|
break;
|
|
@@ -576,7 +596,7 @@ class SdkgenHttpServer {
|
|
|
576
596
|
res.end();
|
|
577
597
|
return;
|
|
578
598
|
}
|
|
579
|
-
const clientIp = request_ip_1.getClientIp(req);
|
|
599
|
+
const clientIp = (0, request_ip_1.getClientIp)(req);
|
|
580
600
|
if (!clientIp) {
|
|
581
601
|
this.writeReply(res, null, {
|
|
582
602
|
error: new error_1.Fatal("Couldn't determine client IP"),
|
|
@@ -594,7 +614,7 @@ class SdkgenHttpServer {
|
|
|
594
614
|
}
|
|
595
615
|
async executeRequest(request, writeReply) {
|
|
596
616
|
const ctx = Object.assign(Object.assign({}, this.extraContext), { request });
|
|
597
|
-
writeReply(ctx, await execute_1.executeRequest(ctx, this.apiConfig));
|
|
617
|
+
writeReply(ctx, await (0, execute_1.executeRequest)(ctx, this.apiConfig));
|
|
598
618
|
}
|
|
599
619
|
parseRequest(req, body, ip) {
|
|
600
620
|
switch (this.identifyRequestVersion(req, body)) {
|
|
@@ -610,13 +630,13 @@ class SdkgenHttpServer {
|
|
|
610
630
|
}
|
|
611
631
|
identifyRequestVersion(_req, body) {
|
|
612
632
|
const parsed = JSON.parse(body);
|
|
613
|
-
if (typeof parsed === "object" && parsed && utils_1.has(parsed, "version") && typeof parsed.version === "number") {
|
|
633
|
+
if (typeof parsed === "object" && parsed && (0, utils_1.has)(parsed, "version") && typeof parsed.version === "number") {
|
|
614
634
|
return parsed.version;
|
|
615
635
|
}
|
|
616
|
-
else if (typeof parsed === "object" && parsed && utils_1.has(parsed, "requestId")) {
|
|
636
|
+
else if (typeof parsed === "object" && parsed && (0, utils_1.has)(parsed, "requestId")) {
|
|
617
637
|
return 2;
|
|
618
638
|
}
|
|
619
|
-
else if (typeof parsed === "object" && parsed && utils_1.has(parsed, "device")) {
|
|
639
|
+
else if (typeof parsed === "object" && parsed && (0, utils_1.has)(parsed, "device")) {
|
|
620
640
|
return 1;
|
|
621
641
|
}
|
|
622
642
|
return 3;
|
|
@@ -624,23 +644,24 @@ class SdkgenHttpServer {
|
|
|
624
644
|
// Old Sdkgen format
|
|
625
645
|
parseRequestV1(req, body, ip) {
|
|
626
646
|
var _a, _b;
|
|
627
|
-
const parsed = encode_decode_1.decode({
|
|
647
|
+
const parsed = (0, encode_decode_1.decode)({
|
|
628
648
|
Request: {
|
|
629
649
|
args: "json",
|
|
630
|
-
device:
|
|
631
|
-
fingerprint: "string?",
|
|
632
|
-
id: "string?",
|
|
633
|
-
language: "string?",
|
|
634
|
-
platform: "json?",
|
|
635
|
-
timezone: "string?",
|
|
636
|
-
type: "string?",
|
|
637
|
-
version: "string?",
|
|
638
|
-
},
|
|
650
|
+
device: "RequestDevice",
|
|
639
651
|
id: "string",
|
|
640
652
|
name: "string",
|
|
641
653
|
},
|
|
654
|
+
RequestDevice: {
|
|
655
|
+
fingerprint: "string?",
|
|
656
|
+
id: "string?",
|
|
657
|
+
language: "string?",
|
|
658
|
+
platform: "json?",
|
|
659
|
+
timezone: "string?",
|
|
660
|
+
type: "string?",
|
|
661
|
+
version: "string?",
|
|
662
|
+
},
|
|
642
663
|
}, "root", "Request", JSON.parse(body));
|
|
643
|
-
const deviceId = (_a = parsed.device.id) !== null && _a !== void 0 ? _a : crypto_1.randomBytes(20).toString("hex");
|
|
664
|
+
const deviceId = (_a = parsed.device.id) !== null && _a !== void 0 ? _a : (0, crypto_1.randomBytes)(20).toString("hex");
|
|
644
665
|
if (!parsed.args || Array.isArray(parsed.args) || typeof parsed.args !== "object") {
|
|
645
666
|
throw new Error("Expected 'args' to be an object");
|
|
646
667
|
}
|
|
@@ -667,21 +688,22 @@ class SdkgenHttpServer {
|
|
|
667
688
|
// Maxima sdkgen format
|
|
668
689
|
parseRequestV2(req, body, ip) {
|
|
669
690
|
var _a, _b;
|
|
670
|
-
const parsed = encode_decode_1.decode({
|
|
691
|
+
const parsed = (0, encode_decode_1.decode)({
|
|
671
692
|
Request: {
|
|
672
693
|
args: "json",
|
|
673
694
|
deviceFingerprint: "string?",
|
|
674
695
|
deviceId: "string",
|
|
675
|
-
info:
|
|
676
|
-
browserUserAgent: "string?",
|
|
677
|
-
language: "string",
|
|
678
|
-
type: "string",
|
|
679
|
-
},
|
|
696
|
+
info: "RequestInfo",
|
|
680
697
|
name: "string",
|
|
681
698
|
partnerId: "string?",
|
|
682
699
|
requestId: "string?",
|
|
683
700
|
sessionId: "string?",
|
|
684
701
|
},
|
|
702
|
+
RequestInfo: {
|
|
703
|
+
browserUserAgent: "string?",
|
|
704
|
+
language: "string",
|
|
705
|
+
type: "string",
|
|
706
|
+
},
|
|
685
707
|
}, "root", "Request", JSON.parse(body));
|
|
686
708
|
if (!parsed.args || Array.isArray(parsed.args) || typeof parsed.args !== "object") {
|
|
687
709
|
throw new Error("Expected 'args' to be an object");
|
|
@@ -705,7 +727,7 @@ class SdkgenHttpServer {
|
|
|
705
727
|
},
|
|
706
728
|
files: [],
|
|
707
729
|
headers: req.headers,
|
|
708
|
-
id: `${parsed.deviceId}-${(_b = parsed.requestId) !== null && _b !== void 0 ? _b : crypto_1.randomBytes(16).toString("hex")}`,
|
|
730
|
+
id: `${parsed.deviceId}-${(_b = parsed.requestId) !== null && _b !== void 0 ? _b : (0, crypto_1.randomBytes)(16).toString("hex")}`,
|
|
709
731
|
ip,
|
|
710
732
|
name: parsed.name,
|
|
711
733
|
version: 2,
|
|
@@ -714,7 +736,7 @@ class SdkgenHttpServer {
|
|
|
714
736
|
// New sdkgen format
|
|
715
737
|
parseRequestV3(req, body, ip) {
|
|
716
738
|
var _a, _b, _c, _d;
|
|
717
|
-
const parsed = encode_decode_1.decode({
|
|
739
|
+
const parsed = (0, encode_decode_1.decode)({
|
|
718
740
|
DeviceInfo: {
|
|
719
741
|
fingerprint: "string?",
|
|
720
742
|
id: "string?",
|
|
@@ -741,7 +763,7 @@ class SdkgenHttpServer {
|
|
|
741
763
|
type: null,
|
|
742
764
|
version: null,
|
|
743
765
|
};
|
|
744
|
-
const deviceId = (_b = deviceInfo.id) !== null && _b !== void 0 ? _b : crypto_1.randomBytes(16).toString("hex");
|
|
766
|
+
const deviceId = (_b = deviceInfo.id) !== null && _b !== void 0 ? _b : (0, crypto_1.randomBytes)(16).toString("hex");
|
|
745
767
|
if (!parsed.args || Array.isArray(parsed.args) || typeof parsed.args !== "object") {
|
|
746
768
|
throw new Error("Expected 'args' to be an object");
|
|
747
769
|
}
|
|
@@ -759,7 +781,7 @@ class SdkgenHttpServer {
|
|
|
759
781
|
extra: typeof parsed.extra === "object" ? Object.assign({}, parsed.extra) : {},
|
|
760
782
|
files: [],
|
|
761
783
|
headers: req.headers,
|
|
762
|
-
id: `${deviceId}-${(_d = parsed.requestId) !== null && _d !== void 0 ? _d : crypto_1.randomBytes(16).toString("hex")}`,
|
|
784
|
+
id: `${deviceId}-${(_d = parsed.requestId) !== null && _d !== void 0 ? _d : (0, crypto_1.randomBytes)(16).toString("hex")}`,
|
|
763
785
|
ip,
|
|
764
786
|
name: parsed.name,
|
|
765
787
|
version: 3,
|
|
@@ -767,11 +789,11 @@ class SdkgenHttpServer {
|
|
|
767
789
|
}
|
|
768
790
|
makeResponseError(err) {
|
|
769
791
|
let type = "Fatal";
|
|
770
|
-
if (typeof err === "object" && err !== null && utils_1.has(err, "type") && typeof err.type === "string") {
|
|
792
|
+
if (typeof err === "object" && err !== null && (0, utils_1.has)(err, "type") && typeof err.type === "string") {
|
|
771
793
|
({ type } = err);
|
|
772
794
|
}
|
|
773
795
|
let message;
|
|
774
|
-
if (typeof err === "object" && err !== null && utils_1.has(err, "message") && typeof err.message === "string") {
|
|
796
|
+
if (typeof err === "object" && err !== null && (0, utils_1.has)(err, "message") && typeof err.message === "string") {
|
|
775
797
|
({ message } = err);
|
|
776
798
|
}
|
|
777
799
|
else if (err instanceof Error) {
|
|
@@ -784,14 +806,14 @@ class SdkgenHttpServer {
|
|
|
784
806
|
message = `${err}`;
|
|
785
807
|
}
|
|
786
808
|
let data;
|
|
787
|
-
if (typeof err === "object" && err !== null && utils_1.has(err, "data")) {
|
|
809
|
+
if (typeof err === "object" && err !== null && (0, utils_1.has)(err, "data")) {
|
|
788
810
|
({ data } = err);
|
|
789
811
|
}
|
|
790
812
|
const error = this.apiConfig.ast.errors.find(x => x.name === type);
|
|
791
813
|
if (error) {
|
|
792
814
|
if (!(error.dataType instanceof parser_1.VoidPrimitiveType)) {
|
|
793
815
|
try {
|
|
794
|
-
data = encode_decode_1.encode(this.apiConfig.astJson.typeTable, `error.${type}`, error.dataType.name, data);
|
|
816
|
+
data = (0, encode_decode_1.encode)(this.apiConfig.astJson.typeTable, `error.${type}`, error.dataType.name, data);
|
|
795
817
|
}
|
|
796
818
|
catch (encodeError) {
|
|
797
819
|
message = `Failed to encode error ${type} because: ${encodeError}. Original message: ${message}`;
|
|
@@ -826,7 +848,7 @@ class SdkgenHttpServer {
|
|
|
826
848
|
deviceId: ctx.request.deviceInfo.id,
|
|
827
849
|
duration,
|
|
828
850
|
error: reply.error ? this.makeResponseError(reply.error) : null,
|
|
829
|
-
host: os_1.hostname(),
|
|
851
|
+
host: (0, os_1.hostname)(),
|
|
830
852
|
id: ctx.request.id,
|
|
831
853
|
ok: !reply.error,
|
|
832
854
|
result: reply.error ? null : reply.result,
|
|
@@ -858,7 +880,7 @@ class SdkgenHttpServer {
|
|
|
858
880
|
const response = {
|
|
859
881
|
duration,
|
|
860
882
|
error: reply.error ? this.makeResponseError(reply.error) : null,
|
|
861
|
-
host: os_1.hostname(),
|
|
883
|
+
host: (0, os_1.hostname)(),
|
|
862
884
|
result: reply.error ? null : reply.result,
|
|
863
885
|
};
|
|
864
886
|
if (response.error) {
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
1
|
+
export { BaseApiConfig } from "./api-config";
|
|
2
|
+
export { Context, ContextReply, ContextRequest } from "./context";
|
|
3
|
+
export { decode, encode } from "./encode-decode";
|
|
4
|
+
export { Fatal, SdkgenError, SdkgenErrorWithData } from "./error";
|
|
5
|
+
export { SdkgenHttpClient } from "./http-client";
|
|
6
|
+
export { SdkgenHttpServer } from "./http-server";
|
|
7
|
+
export { apiTestWrapper } from "./test-wrapper";
|
package/dist/src/index.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
3
|
+
exports.apiTestWrapper = exports.SdkgenHttpServer = exports.SdkgenHttpClient = exports.SdkgenErrorWithData = exports.SdkgenError = exports.Fatal = exports.encode = exports.decode = exports.BaseApiConfig = void 0;
|
|
4
|
+
var api_config_1 = require("./api-config");
|
|
5
|
+
Object.defineProperty(exports, "BaseApiConfig", { enumerable: true, get: function () { return api_config_1.BaseApiConfig; } });
|
|
6
|
+
var encode_decode_1 = require("./encode-decode");
|
|
7
|
+
Object.defineProperty(exports, "decode", { enumerable: true, get: function () { return encode_decode_1.decode; } });
|
|
8
|
+
Object.defineProperty(exports, "encode", { enumerable: true, get: function () { return encode_decode_1.encode; } });
|
|
9
|
+
var error_1 = require("./error");
|
|
10
|
+
Object.defineProperty(exports, "Fatal", { enumerable: true, get: function () { return error_1.Fatal; } });
|
|
11
|
+
Object.defineProperty(exports, "SdkgenError", { enumerable: true, get: function () { return error_1.SdkgenError; } });
|
|
12
|
+
Object.defineProperty(exports, "SdkgenErrorWithData", { enumerable: true, get: function () { return error_1.SdkgenErrorWithData; } });
|
|
13
|
+
var http_client_1 = require("./http-client");
|
|
14
|
+
Object.defineProperty(exports, "SdkgenHttpClient", { enumerable: true, get: function () { return http_client_1.SdkgenHttpClient; } });
|
|
15
|
+
var http_server_1 = require("./http-server");
|
|
16
|
+
Object.defineProperty(exports, "SdkgenHttpServer", { enumerable: true, get: function () { return http_server_1.SdkgenHttpServer; } });
|
|
17
|
+
var test_wrapper_1 = require("./test-wrapper");
|
|
18
|
+
Object.defineProperty(exports, "apiTestWrapper", { enumerable: true, get: function () { return test_wrapper_1.apiTestWrapper; } });
|