@based/server 3.4.6 → 4.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/api/index.d.ts +7 -0
- package/dist/api/index.js +173 -0
- package/dist/api/index.js.map +1 -0
- package/dist/auth/dummyAuth.d.ts +2 -0
- package/dist/auth/dummyAuth.js +8 -0
- package/dist/auth/dummyAuth.js.map +1 -0
- package/dist/auth/index.d.ts +15 -0
- package/dist/auth/index.js +49 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/parseAuthState.d.ts +2 -0
- package/dist/auth/parseAuthState.js +16 -0
- package/dist/auth/parseAuthState.js.map +1 -0
- package/dist/auth/types.d.ts +10 -0
- package/dist/{handlers/rest/file → auth}/types.js +0 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/compress.d.ts +5 -0
- package/dist/compress.js +52 -0
- package/dist/compress.js.map +1 -0
- package/dist/context.d.ts +54 -0
- package/dist/context.js +18 -0
- package/dist/context.js.map +1 -0
- package/dist/error/errorTypeHandlers.d.ts +6 -0
- package/dist/error/errorTypeHandlers.js +130 -0
- package/dist/error/errorTypeHandlers.js.map +1 -0
- package/dist/error/index.d.ts +5 -0
- package/dist/error/index.js +61 -0
- package/dist/error/index.js.map +1 -0
- package/dist/error/types.d.ts +105 -0
- package/dist/error/types.js +30 -0
- package/dist/error/types.js.map +1 -0
- package/dist/functions/index.d.ts +34 -0
- package/dist/functions/index.js +210 -0
- package/dist/functions/index.js.map +1 -0
- package/dist/functions/timeout.d.ts +3 -0
- package/dist/functions/timeout.js +23 -0
- package/dist/functions/timeout.js.map +1 -0
- package/dist/functions/types.d.ts +69 -0
- package/dist/functions/types.js +15 -0
- package/dist/functions/types.js.map +1 -0
- package/dist/incoming/http/authorize.d.ts +4 -0
- package/dist/incoming/http/authorize.js +37 -0
- package/dist/incoming/http/authorize.js.map +1 -0
- package/dist/incoming/http/function.d.ts +4 -0
- package/dist/incoming/http/function.js +47 -0
- package/dist/incoming/http/function.js.map +1 -0
- package/dist/incoming/http/get.d.ts +4 -0
- package/dist/incoming/http/get.js +155 -0
- package/dist/incoming/http/get.js.map +1 -0
- package/dist/incoming/http/index.d.ts +3 -0
- package/dist/incoming/http/index.js +134 -0
- package/dist/incoming/http/index.js.map +1 -0
- package/dist/incoming/http/parseQuery.d.ts +4 -0
- package/dist/incoming/http/parseQuery.js +25 -0
- package/dist/incoming/http/parseQuery.js.map +1 -0
- package/dist/incoming/http/readBody.d.ts +4 -0
- package/dist/incoming/http/readBody.js +107 -0
- package/dist/incoming/http/readBody.js.map +1 -0
- package/dist/{handlers/rest/file → incoming/http/streamFunction}/DataStream.d.ts +0 -0
- package/dist/{handlers/rest/file → incoming/http/streamFunction}/DataStream.js +3 -1
- package/dist/incoming/http/streamFunction/DataStream.js.map +1 -0
- package/dist/{handlers/rest/file/getExtenstion.d.ts → incoming/http/streamFunction/getExtension.d.ts} +0 -0
- package/dist/{handlers/rest/file/getExtenstion.js → incoming/http/streamFunction/getExtension.js} +9 -2
- package/dist/incoming/http/streamFunction/getExtension.js.map +1 -0
- package/dist/incoming/http/streamFunction/index.d.ts +4 -0
- package/dist/incoming/http/streamFunction/index.js +106 -0
- package/dist/incoming/http/streamFunction/index.js.map +1 -0
- package/dist/incoming/http/streamFunction/multipartStream.d.ts +13 -0
- package/dist/incoming/http/streamFunction/multipartStream.js +208 -0
- package/dist/incoming/http/streamFunction/multipartStream.js.map +1 -0
- package/dist/incoming/http/streamFunction/stream.d.ts +6 -0
- package/dist/incoming/http/streamFunction/stream.js +108 -0
- package/dist/incoming/http/streamFunction/stream.js.map +1 -0
- package/dist/incoming/index.d.ts +3 -0
- package/dist/incoming/index.js +84 -0
- package/dist/incoming/index.js.map +1 -0
- package/dist/incoming/upgrade.d.ts +4 -0
- package/dist/incoming/upgrade.js +64 -0
- package/dist/incoming/upgrade.js.map +1 -0
- package/dist/incoming/ws/auth.d.ts +5 -0
- package/dist/incoming/ws/auth.js +40 -0
- package/dist/incoming/ws/auth.js.map +1 -0
- package/dist/incoming/ws/function.d.ts +3 -0
- package/dist/incoming/ws/function.js +107 -0
- package/dist/incoming/ws/function.js.map +1 -0
- package/dist/incoming/ws/get.d.ts +3 -0
- package/dist/incoming/ws/get.js +136 -0
- package/dist/incoming/ws/get.js.map +1 -0
- package/dist/incoming/ws/index.d.ts +3 -0
- package/dist/incoming/ws/index.js +61 -0
- package/dist/incoming/ws/index.js.map +1 -0
- package/dist/incoming/ws/observable.d.ts +6 -0
- package/dist/incoming/ws/observable.js +112 -0
- package/dist/incoming/ws/observable.js.map +1 -0
- package/dist/index.d.ts +10 -35
- package/dist/index.js +17 -196
- package/dist/index.js.map +1 -1
- package/dist/ip.d.ts +2 -0
- package/dist/ip.js +8 -0
- package/dist/ip.js.map +1 -0
- package/dist/observable/create.d.ts +3 -0
- package/dist/observable/create.js +30 -0
- package/dist/observable/create.js.map +1 -0
- package/dist/observable/destroy.d.ts +2 -0
- package/dist/observable/destroy.js +51 -0
- package/dist/observable/destroy.js.map +1 -0
- package/dist/observable/error.d.ts +4 -0
- package/dist/observable/error.js +17 -0
- package/dist/observable/error.js.map +1 -0
- package/dist/observable/extendCache.d.ts +2 -0
- package/dist/observable/extendCache.js +11 -0
- package/dist/observable/extendCache.js.map +1 -0
- package/dist/observable/genObservableId.d.ts +1 -0
- package/dist/observable/genObservableId.js +9 -0
- package/dist/observable/genObservableId.js.map +1 -0
- package/dist/observable/get.d.ts +4 -0
- package/dist/observable/get.js +15 -0
- package/dist/observable/get.js.map +1 -0
- package/dist/observable/index.d.ts +11 -0
- package/dist/observable/index.js +28 -0
- package/dist/observable/index.js.map +1 -0
- package/dist/observable/send.d.ts +6 -0
- package/dist/observable/send.js +32 -0
- package/dist/observable/send.js.map +1 -0
- package/dist/observable/start/error.d.ts +3 -0
- package/dist/observable/start/error.js +36 -0
- package/dist/observable/start/error.js.map +1 -0
- package/dist/observable/start/index.d.ts +2 -0
- package/dist/observable/start/index.js +46 -0
- package/dist/observable/start/index.js.map +1 -0
- package/dist/observable/start/update.d.ts +6 -0
- package/dist/observable/start/update.js +104 -0
- package/dist/observable/start/update.js.map +1 -0
- package/dist/observable/subscribe.d.ts +7 -0
- package/dist/observable/subscribe.js +54 -0
- package/dist/observable/subscribe.js.map +1 -0
- package/dist/observable/types.d.ts +26 -0
- package/dist/observable/types.js +3 -0
- package/dist/observable/types.js.map +1 -0
- package/dist/observable/unsub.d.ts +6 -0
- package/dist/observable/unsub.js +43 -0
- package/dist/observable/unsub.js.map +1 -0
- package/dist/observable/verify.d.ts +4 -0
- package/dist/observable/verify.js +18 -0
- package/dist/observable/verify.js.map +1 -0
- package/dist/protocol.d.ts +21 -0
- package/dist/protocol.js +233 -0
- package/dist/protocol.js.map +1 -0
- package/dist/security.d.ts +6 -0
- package/dist/security.js +95 -0
- package/dist/security.js.map +1 -0
- package/dist/sendError.d.ts +5 -0
- package/dist/sendError.js +42 -0
- package/dist/sendError.js.map +1 -0
- package/dist/sendHttpResponse.d.ts +4 -0
- package/dist/sendHttpResponse.js +53 -0
- package/dist/sendHttpResponse.js.map +1 -0
- package/dist/server.d.ts +69 -0
- package/dist/server.js +113 -0
- package/dist/server.js.map +1 -0
- package/package.json +16 -14
- package/dist/BasedServerClient.d.ts +0 -71
- package/dist/BasedServerClient.js +0 -390
- package/dist/BasedServerClient.js.map +0 -1
- package/dist/Client.d.ts +0 -75
- package/dist/Client.js +0 -277
- package/dist/Client.js.map +0 -1
- package/dist/Params.d.ts +0 -20
- package/dist/Params.js +0 -48
- package/dist/Params.js.map +0 -1
- package/dist/closeListener.d.ts +0 -4
- package/dist/closeListener.js +0 -13
- package/dist/closeListener.js.map +0 -1
- package/dist/findPrefix.d.ts +0 -3
- package/dist/findPrefix.js +0 -20
- package/dist/findPrefix.js.map +0 -1
- package/dist/getFromConfig.d.ts +0 -3
- package/dist/getFromConfig.js +0 -50
- package/dist/getFromConfig.js.map +0 -1
- package/dist/handlers/authorize.d.ts +0 -5
- package/dist/handlers/authorize.js +0 -196
- package/dist/handlers/authorize.js.map +0 -1
- package/dist/handlers/bulkUpdate/index.d.ts +0 -5
- package/dist/handlers/bulkUpdate/index.js +0 -26
- package/dist/handlers/bulkUpdate/index.js.map +0 -1
- package/dist/handlers/configuration/index.d.ts +0 -10
- package/dist/handlers/configuration/index.js +0 -74
- package/dist/handlers/configuration/index.js.map +0 -1
- package/dist/handlers/configuration/observable/index.d.ts +0 -46
- package/dist/handlers/configuration/observable/index.js +0 -277
- package/dist/handlers/configuration/observable/index.js.map +0 -1
- package/dist/handlers/configure/index.d.ts +0 -5
- package/dist/handlers/configure/index.js +0 -44
- package/dist/handlers/configure/index.js.map +0 -1
- package/dist/handlers/copy/index.d.ts +0 -8
- package/dist/handlers/copy/index.js +0 -135
- package/dist/handlers/copy/index.js.map +0 -1
- package/dist/handlers/delete/index.d.ts +0 -5
- package/dist/handlers/delete/index.js +0 -34
- package/dist/handlers/delete/index.js.map +0 -1
- package/dist/handlers/digest/index.d.ts +0 -5
- package/dist/handlers/digest/index.js +0 -23
- package/dist/handlers/digest/index.js.map +0 -1
- package/dist/handlers/functions/call.d.ts +0 -6
- package/dist/handlers/functions/call.js +0 -54
- package/dist/handlers/functions/call.js.map +0 -1
- package/dist/handlers/functions/observable/Observable.d.ts +0 -49
- package/dist/handlers/functions/observable/Observable.js +0 -290
- package/dist/handlers/functions/observable/Observable.js.map +0 -1
- package/dist/handlers/functions/observable/SharedObservable.d.ts +0 -45
- package/dist/handlers/functions/observable/SharedObservable.js +0 -291
- package/dist/handlers/functions/observable/SharedObservable.js.map +0 -1
- package/dist/handlers/functions/observable/index.d.ts +0 -14
- package/dist/handlers/functions/observable/index.js +0 -130
- package/dist/handlers/functions/observable/index.js.map +0 -1
- package/dist/handlers/get/index.d.ts +0 -5
- package/dist/handlers/get/index.js +0 -23
- package/dist/handlers/get/index.js.map +0 -1
- package/dist/handlers/getConfig/index.d.ts +0 -5
- package/dist/handlers/getConfig/index.js +0 -38
- package/dist/handlers/getConfig/index.js.map +0 -1
- package/dist/handlers/handleRequests.d.ts +0 -5
- package/dist/handlers/handleRequests.js +0 -78
- package/dist/handlers/handleRequests.js.map +0 -1
- package/dist/handlers/index.d.ts +0 -4
- package/dist/handlers/index.js +0 -49
- package/dist/handlers/index.js.map +0 -1
- package/dist/handlers/messageParser.d.ts +0 -5
- package/dist/handlers/messageParser.js +0 -122
- package/dist/handlers/messageParser.js.map +0 -1
- package/dist/handlers/removeField/index.d.ts +0 -5
- package/dist/handlers/removeField/index.js +0 -42
- package/dist/handlers/removeField/index.js.map +0 -1
- package/dist/handlers/removeType/index.d.ts +0 -5
- package/dist/handlers/removeType/index.js +0 -30
- package/dist/handlers/removeType/index.js.map +0 -1
- package/dist/handlers/rest/file/DataStream.js.map +0 -1
- package/dist/handlers/rest/file/formStream.d.ts +0 -4
- package/dist/handlers/rest/file/formStream.js +0 -188
- package/dist/handlers/rest/file/formStream.js.map +0 -1
- package/dist/handlers/rest/file/getExtenstion.js.map +0 -1
- package/dist/handlers/rest/file/index.d.ts +0 -4
- package/dist/handlers/rest/file/index.js +0 -53
- package/dist/handlers/rest/file/index.js.map +0 -1
- package/dist/handlers/rest/file/storeFile.d.ts +0 -6
- package/dist/handlers/rest/file/storeFile.js +0 -44
- package/dist/handlers/rest/file/storeFile.js.map +0 -1
- package/dist/handlers/rest/file/stream.d.ts +0 -5
- package/dist/handlers/rest/file/stream.js +0 -104
- package/dist/handlers/rest/file/stream.js.map +0 -1
- package/dist/handlers/rest/file/types.d.ts +0 -9
- package/dist/handlers/rest/file/types.js.map +0 -1
- package/dist/handlers/rest/getReqMessage.d.ts +0 -3
- package/dist/handlers/rest/getReqMessage.js +0 -92
- package/dist/handlers/rest/getReqMessage.js.map +0 -1
- package/dist/handlers/rest/index.d.ts +0 -4
- package/dist/handlers/rest/index.js +0 -121
- package/dist/handlers/rest/index.js.map +0 -1
- package/dist/handlers/rest/invalidReq.d.ts +0 -4
- package/dist/handlers/rest/invalidReq.js +0 -20
- package/dist/handlers/rest/invalidReq.js.map +0 -1
- package/dist/handlers/rest/parseResponse.d.ts +0 -6
- package/dist/handlers/rest/parseResponse.js +0 -230
- package/dist/handlers/rest/parseResponse.js.map +0 -1
- package/dist/handlers/rest/playground/index.d.ts +0 -5
- package/dist/handlers/rest/playground/index.js +0 -308
- package/dist/handlers/rest/playground/index.js.map +0 -1
- package/dist/handlers/rest/readBody.d.ts +0 -3
- package/dist/handlers/rest/readBody.js +0 -45
- package/dist/handlers/rest/readBody.js.map +0 -1
- package/dist/handlers/set/index.d.ts +0 -5
- package/dist/handlers/set/index.js +0 -20
- package/dist/handlers/set/index.js.map +0 -1
- package/dist/handlers/subscription/index.d.ts +0 -32
- package/dist/handlers/subscription/index.js +0 -223
- package/dist/handlers/subscription/index.js.map +0 -1
- package/dist/handlers/token.d.ts +0 -5
- package/dist/handlers/token.js +0 -122
- package/dist/handlers/token.js.map +0 -1
- package/dist/handlers/track/index.d.ts +0 -7
- package/dist/handlers/track/index.js +0 -102
- package/dist/handlers/track/index.js.map +0 -1
- package/dist/handlers/userAuth/index.d.ts +0 -5
- package/dist/handlers/userAuth/index.js +0 -78
- package/dist/handlers/userAuth/index.js.map +0 -1
- package/dist/openListener.d.ts +0 -4
- package/dist/openListener.js +0 -13
- package/dist/openListener.js.map +0 -1
- package/dist/secrets.d.ts +0 -9
- package/dist/secrets.js +0 -150
- package/dist/secrets.js.map +0 -1
- package/dist/types.d.ts +0 -129
- package/dist/types.js +0 -17
- package/dist/types.js.map +0 -1
- package/dist/upgradeListener.d.ts +0 -4
- package/dist/upgradeListener.js +0 -47
- package/dist/upgradeListener.js.map +0 -1
- package/dist/workers/index.d.ts +0 -0
- package/dist/workers/index.js +0 -20
- package/dist/workers/index.js.map +0 -1
- package/dist/workers/workerCode.d.ts +0 -12
- package/dist/workers/workerCode.js +0 -46
- package/dist/workers/workerCode.js.map +0 -1
package/dist/security.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blockIncomingRequest = exports.endRateLimitHttp = exports.rateLimitRequest = void 0;
|
|
4
|
+
const error_1 = require("./error");
|
|
5
|
+
var IsBlocked;
|
|
6
|
+
(function (IsBlocked) {
|
|
7
|
+
IsBlocked[IsBlocked["notBlocked"] = 0] = "notBlocked";
|
|
8
|
+
IsBlocked[IsBlocked["blocked"] = 1] = "blocked";
|
|
9
|
+
IsBlocked[IsBlocked["firstBlocked"] = 2] = "firstBlocked";
|
|
10
|
+
})(IsBlocked || (IsBlocked = {}));
|
|
11
|
+
const drainRequestCounter = (server) => {
|
|
12
|
+
server.requestsCounterInProgress = true;
|
|
13
|
+
server.requestsCounterTimeout = setTimeout(() => {
|
|
14
|
+
server.requestsCounterInProgress = false;
|
|
15
|
+
server.rateLimitCounter.forEach((value, ip) => {
|
|
16
|
+
if (value.requests <= 0) {
|
|
17
|
+
server.rateLimitCounter.delete(ip);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
value.requests -= server.rateLimit.drain;
|
|
21
|
+
});
|
|
22
|
+
if (server.rateLimitCounter.size) {
|
|
23
|
+
drainRequestCounter(server);
|
|
24
|
+
}
|
|
25
|
+
}, 30e3);
|
|
26
|
+
};
|
|
27
|
+
const incomingRequestCounter = (server, ip, tokens, max) => {
|
|
28
|
+
let ipReqCounter = server.rateLimitCounter.get(ip);
|
|
29
|
+
if (!ipReqCounter) {
|
|
30
|
+
ipReqCounter = {
|
|
31
|
+
requests: tokens,
|
|
32
|
+
};
|
|
33
|
+
server.rateLimitCounter.set(ip, ipReqCounter);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
ipReqCounter.requests += tokens;
|
|
37
|
+
}
|
|
38
|
+
if (ipReqCounter.requests === max) {
|
|
39
|
+
return 2;
|
|
40
|
+
}
|
|
41
|
+
if (ipReqCounter.requests > max) {
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
if (!server.requestsCounterInProgress) {
|
|
45
|
+
drainRequestCounter(server);
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
};
|
|
49
|
+
const rateLimitRequest = (server, ctx, tokens, max) => {
|
|
50
|
+
if (!ctx.session) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const ip = ctx.session.ip;
|
|
54
|
+
const code = incomingRequestCounter(server, ip, tokens, max);
|
|
55
|
+
if (code === 0) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (code === 2) {
|
|
59
|
+
server.emit('error', ctx, { code: error_1.BasedErrorCode.RateLimit });
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
};
|
|
63
|
+
exports.rateLimitRequest = rateLimitRequest;
|
|
64
|
+
const endRateLimitHttp = (res) => {
|
|
65
|
+
res.cork(() => {
|
|
66
|
+
res.writeStatus('429 Too Many Requests');
|
|
67
|
+
res.close();
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
exports.endRateLimitHttp = endRateLimitHttp;
|
|
71
|
+
const blockIncomingRequest = (server, ip, res, req, max, tokens) => {
|
|
72
|
+
if (server.allowedIps.has(ip)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (server.blockedIps.has(ip)) {
|
|
76
|
+
res.close();
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
const code = incomingRequestCounter(server, ip, tokens, max);
|
|
80
|
+
if (code === 0) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
if (code === 2) {
|
|
84
|
+
server.emit('error', {
|
|
85
|
+
session: {
|
|
86
|
+
ua: req.getHeader('user-agent'),
|
|
87
|
+
ip,
|
|
88
|
+
},
|
|
89
|
+
}, { code: error_1.BasedErrorCode.RateLimit });
|
|
90
|
+
}
|
|
91
|
+
(0, exports.endRateLimitHttp)(res);
|
|
92
|
+
return true;
|
|
93
|
+
};
|
|
94
|
+
exports.blockIncomingRequest = blockIncomingRequest;
|
|
95
|
+
//# sourceMappingURL=security.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security.js","sourceRoot":"","sources":["../src/security.ts"],"names":[],"mappings":";;;AAAA,mCAAwC;AAKxC,IAAK,SAIJ;AAJD,WAAK,SAAS;IACZ,qDAAc,CAAA;IACd,+CAAW,CAAA;IACX,yDAAgB,CAAA;AAClB,CAAC,EAJI,SAAS,KAAT,SAAS,QAIb;AAED,MAAM,mBAAmB,GAAG,CAAC,MAAmB,EAAE,EAAE;IAClD,MAAM,CAAC,yBAAyB,GAAG,IAAI,CAAA;IACvC,MAAM,CAAC,sBAAsB,GAAG,UAAU,CAAC,GAAG,EAAE;QAC9C,MAAM,CAAC,yBAAyB,GAAG,KAAK,CAAA;QACxC,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;YAC5C,IAAI,KAAK,CAAC,QAAQ,IAAI,CAAC,EAAE;gBACvB,MAAM,CAAC,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;gBAClC,OAAM;aACP;YACD,KAAK,CAAC,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAA;QAC1C,CAAC,CAAC,CAAA;QACF,IAAI,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE;YAChC,mBAAmB,CAAC,MAAM,CAAC,CAAA;SAC5B;IACH,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC,CAAA;AAED,MAAM,sBAAsB,GAAG,CAC7B,MAAmB,EACnB,EAAU,EACV,MAAc,EACd,GAAW,EACA,EAAE;IACb,IAAI,YAAY,GAAG,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAClD,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG;YACb,QAAQ,EAAE,MAAM;SACjB,CAAA;QACD,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE,EAAE,YAAY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AClC,MAAmB,EACnB,EAAU,EACV,GAAqB,EACrB,GAAoB,EACpB,GAAW,EACX,MAAc,EACL,EAAE;IACX,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;QAC7B,OAAO,KAAK,CAAA;KACb;IACD,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;QAC7B,GAAG,CAAC,KAAK,EAAE,CAAA;QACX,OAAO,IAAI,CAAA;KACZ;IACD,MAAM,IAAI,GAAG,sBAAsB,CAAC,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,CAAA;IAC5D,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,KAAK,CAAA;KACb;IAED,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,MAAM,CAAC,IAAI,CACT,OAAO,EACP;YACE,OAAO,EAAE;gBACP,EAAE,EAAE,GAAG,CAAC,SAAS,CAAC,YAAY,CAAC;gBAC/B,EAAE;aACH;SACF,EACD,EAAE,IAAI,EAAE,sBAAc,CAAC,SAAS,EAAE,CACnC,CAAA;KACF;IAED,IAAA,wBAAgB,EAAC,GAAG,CAAC,CAAA;IAErB,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AApCY,QAAA,oBAAoB,wBAoChC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BasedServer } from './server';
|
|
2
|
+
import { HttpSession, WebSocketSession, Context } from './context';
|
|
3
|
+
import { BasedErrorCode, ErrorPayload } from './error';
|
|
4
|
+
export declare function sendHttpError<T extends BasedErrorCode>(server: BasedServer, ctx: Context<HttpSession>, basedCode: T, payload: ErrorPayload[T]): void;
|
|
5
|
+
export declare function sendError<T extends BasedErrorCode>(server: BasedServer, ctx: Context<WebSocketSession | HttpSession>, basedCode: T, payload: ErrorPayload[T]): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendError = exports.sendHttpError = void 0;
|
|
4
|
+
const sendHttpResponse_1 = require("./sendHttpResponse");
|
|
5
|
+
const context_1 = require("./context");
|
|
6
|
+
const protocol_1 = require("./protocol");
|
|
7
|
+
const error_1 = require("./error");
|
|
8
|
+
const sendHttpErrorMessage = (res, error) => {
|
|
9
|
+
const { code, message, statusCode, statusMessage } = error;
|
|
10
|
+
res.writeStatus(`${statusCode} ${statusMessage}`);
|
|
11
|
+
res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
12
|
+
res.writeHeader('Access-Control-Allow-Headers', 'content-type');
|
|
13
|
+
res.writeHeader('Content-Type', 'application/json');
|
|
14
|
+
return JSON.stringify({
|
|
15
|
+
error: message,
|
|
16
|
+
code,
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
function sendHttpError(server, ctx, basedCode, payload) {
|
|
20
|
+
if (!ctx.session) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
ctx.session.res.cork(() => {
|
|
24
|
+
(0, sendHttpResponse_1.end)(ctx, sendHttpErrorMessage(ctx.session.res, (0, error_1.createError)(server, ctx, basedCode, payload)));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.sendHttpError = sendHttpError;
|
|
28
|
+
function sendError(server, ctx, basedCode, payload) {
|
|
29
|
+
if (!ctx.session) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if ((0, context_1.isHttpContext)(ctx)) {
|
|
33
|
+
return sendHttpError(server, ctx, basedCode, payload);
|
|
34
|
+
}
|
|
35
|
+
else if ((0, context_1.isWsSession)(ctx.session)) {
|
|
36
|
+
const errorData = (0, error_1.createError)(server, ctx, basedCode, payload);
|
|
37
|
+
const ws = ctx.session;
|
|
38
|
+
ws.send((0, protocol_1.encodeErrorResponse)((0, protocol_1.valueToBuffer)(errorData)), true, false);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.sendError = sendError;
|
|
42
|
+
//# sourceMappingURL=sendError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendError.js","sourceRoot":"","sources":["../src/sendError.ts"],"names":[],"mappings":";;;AACA,yDAAwC;AAExC,uCAMkB;AAClB,yCAA+D;AAC/D,mCAKgB;AAEhB,MAAM,oBAAoB,GAAG,CAC3B,GAAqB,EACrB,KAAqB,EACb,EAAE;IACV,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,GAAG,KAAK,CAAA;IAC1D,GAAG,CAAC,WAAW,CAAC,GAAG,UAAU,IAAI,aAAa,EAAE,CAAC,CAAA;IACjD,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;IACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;IAC/D,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;IACnD,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,KAAK,EAAE,OAAO;QACd,IAAI;KACL,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,SAAgB,aAAa,CAC3B,MAAmB,EACnB,GAAyB,EACzB,SAAY,EACZ,OAAwB;IAExB,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE;QAChB,OAAM;KACP;IACD,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE;QACxB,IAAA,sBAAG,EACD,GAAG,EACH,oBAAoB,CAClB,GAAG,CAAC,OAAO,CAAC,GAAG,EACf,IAAA,mBAAW,EAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,CAC7C,CACF,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAlBD,sCAkBC;AAED,SAAgB,SAAS,CACvB,MAAmB,EACnB,GAA4C,EAC5C,SAAY,EACZ,OAAwB;IAExB,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE;QAChB,OAAM;KACP;IACD,IAAI,IAAA,uBAAa,EAAC,GAAG,CAAC,EAAE;QACtB,OAAO,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;KACtD;SAAM,IAAI,IAAA,qBAAW,EAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QACnC,MAAM,SAAS,GAAG,IAAA,mBAAW,EAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;QAC9D,MAAM,EAAE,GAAqB,GAAG,CAAC,OAAO,CAAA;QACxC,EAAE,CAAC,IAAI,CAAC,IAAA,8BAAmB,EAAC,IAAA,wBAAa,EAAC,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;KACpE;AACH,CAAC;AAhBD,8BAgBC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Context, HttpSession } from './context';
|
|
3
|
+
export declare const end: (ctx: Context<HttpSession>, payload?: string | Buffer | Uint8Array) => void;
|
|
4
|
+
export declare const sendHttpResponse: (ctx: Context<HttpSession>, result: any) => void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendHttpResponse = exports.end = void 0;
|
|
4
|
+
const compress_1 = require("./compress");
|
|
5
|
+
const end = (ctx, payload) => {
|
|
6
|
+
if (!ctx.session) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
ctx.session.res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
10
|
+
// only allowed headers
|
|
11
|
+
ctx.session.res.writeHeader('Access-Control-Allow-Headers', '*');
|
|
12
|
+
if (payload === undefined) {
|
|
13
|
+
ctx.session.res.end();
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
ctx.session.res.end(payload);
|
|
17
|
+
}
|
|
18
|
+
ctx.session.res = null;
|
|
19
|
+
ctx.session.req = null;
|
|
20
|
+
ctx.session = null;
|
|
21
|
+
};
|
|
22
|
+
exports.end = end;
|
|
23
|
+
const sendHttpResponse = (ctx, result) => {
|
|
24
|
+
if (!ctx.session) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
let cType;
|
|
28
|
+
// for functions there is never cache (idea is they are used to execute - observable fns are for cache)
|
|
29
|
+
let parsed;
|
|
30
|
+
if (typeof result === 'string') {
|
|
31
|
+
cType = 'text/plain';
|
|
32
|
+
parsed = result;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
cType = 'application/json';
|
|
36
|
+
parsed = JSON.stringify(result);
|
|
37
|
+
}
|
|
38
|
+
(0, compress_1.compress)(parsed, ctx.session.headers.encoding).then(({ payload, encoding }) => {
|
|
39
|
+
if (ctx.session.res) {
|
|
40
|
+
ctx.session.res.cork(() => {
|
|
41
|
+
ctx.session.res.writeStatus('200 OK');
|
|
42
|
+
ctx.session.res.writeHeader('Cache-Control', 'max-age=0, must-revalidate');
|
|
43
|
+
ctx.session.res.writeHeader('Content-Type', cType);
|
|
44
|
+
if (encoding) {
|
|
45
|
+
ctx.session.res.writeHeader('Content-Encoding', encoding);
|
|
46
|
+
}
|
|
47
|
+
(0, exports.end)(ctx, payload);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
exports.sendHttpResponse = sendHttpResponse;
|
|
53
|
+
//# sourceMappingURL=sendHttpResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendHttpResponse.js","sourceRoot":"","sources":["../src/sendHttpResponse.ts"],"names":[],"mappings":";;;AACA,yCAAqC;AAE9B,MAAM,GAAG,GAAG,CACjB,GAAyB,EACzB,OAAsC,EACtC,EAAE;IACF,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE;QAChB,OAAM;KACP;IACD,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,6BA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AC,QAAQ,CAAC,CAAA;gBACrC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CACzB,eAAe,EACf,4BAA4B,CAC7B,CAAA;gBACD,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,KAAK,CAAC,CAAA;gBAClD,IAAI,QAAQ,EAAE;oBACZ,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAA;iBAC1D;gBACD,IAAA,WAAG,EAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YACnB,CAAC,CAAC,CAAA;SACH;IACH,CAAC,CACF,CAAA;AACH,CAAC,CAAA;AAlCY,QAAA,gBAAgB,oBAkC5B"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Context } from './context';
|
|
3
|
+
import type { ActiveObservable } from './observable';
|
|
4
|
+
import uws from '@based/uws';
|
|
5
|
+
import { BasedFunctions, FunctionConfig } from './functions';
|
|
6
|
+
import { BasedAuth, AuthConfig } from './auth';
|
|
7
|
+
import { BasedErrorCode, BasedErrorData } from './error';
|
|
8
|
+
declare type EventMap = {
|
|
9
|
+
error: BasedErrorData;
|
|
10
|
+
ratelimit: void;
|
|
11
|
+
log: any;
|
|
12
|
+
};
|
|
13
|
+
declare type Event = keyof EventMap;
|
|
14
|
+
declare type Listener<T> = (context: Context, data?: T, err?: Error) => void;
|
|
15
|
+
declare type RateLimit = {
|
|
16
|
+
ws: number;
|
|
17
|
+
http: number;
|
|
18
|
+
drain: number;
|
|
19
|
+
};
|
|
20
|
+
export declare type ServerOptions = {
|
|
21
|
+
port?: number;
|
|
22
|
+
key?: string;
|
|
23
|
+
cert?: string;
|
|
24
|
+
functions?: FunctionConfig;
|
|
25
|
+
rateLimit?: RateLimit;
|
|
26
|
+
auth?: AuthConfig;
|
|
27
|
+
workerRequest?: (type: string, payload?: any) => void | Promise<any>;
|
|
28
|
+
ws?: {
|
|
29
|
+
open: (client: Context) => void;
|
|
30
|
+
close: (client: Context) => void;
|
|
31
|
+
};
|
|
32
|
+
http?: {
|
|
33
|
+
open: (client: Context) => void;
|
|
34
|
+
close: (client: Context) => void;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export declare class BasedServer {
|
|
38
|
+
functions: BasedFunctions;
|
|
39
|
+
auth: BasedAuth;
|
|
40
|
+
port: number;
|
|
41
|
+
uwsApp: uws.TemplatedApp;
|
|
42
|
+
rateLimit: RateLimit;
|
|
43
|
+
listenSocket: any;
|
|
44
|
+
blockedIps: Set<string>;
|
|
45
|
+
allowedIps: Set<string>;
|
|
46
|
+
rateLimitCounter: Map<string, {
|
|
47
|
+
requests: number;
|
|
48
|
+
errors?: Map<BasedErrorCode, number>;
|
|
49
|
+
}>;
|
|
50
|
+
requestsCounterInProgress: boolean;
|
|
51
|
+
requestsCounterTimeout: NodeJS.Timeout;
|
|
52
|
+
activeObservables: {
|
|
53
|
+
[name: string]: Map<number, ActiveObservable>;
|
|
54
|
+
};
|
|
55
|
+
activeObservablesById: Map<number, ActiveObservable>;
|
|
56
|
+
listeners: {
|
|
57
|
+
[E in Event]?: Listener<EventMap[E]>[];
|
|
58
|
+
};
|
|
59
|
+
workerRequest: (type: string, payload?: any) => void | Promise<any>;
|
|
60
|
+
constructor(opts: ServerOptions);
|
|
61
|
+
emit(type: Event, client: Context, val: EventMap[Event], err?: Error): void;
|
|
62
|
+
on(type: Event, fn: Listener<EventMap[Event]>): void;
|
|
63
|
+
removeAllListeners(): void;
|
|
64
|
+
once(type: Event, fn: Listener<EventMap[Event]>): void;
|
|
65
|
+
off(type: Event, fn: Listener<EventMap[Event]>): void;
|
|
66
|
+
start(port?: number, sharedSocket?: boolean): Promise<BasedServer>;
|
|
67
|
+
destroy(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
export {};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BasedServer = void 0;
|
|
7
|
+
const uws_1 = __importDefault(require("@based/uws"));
|
|
8
|
+
const incoming_1 = __importDefault(require("./incoming"));
|
|
9
|
+
const functions_1 = require("./functions");
|
|
10
|
+
const auth_1 = require("./auth");
|
|
11
|
+
const utils_1 = require("@saulx/utils");
|
|
12
|
+
// extend emitter
|
|
13
|
+
class BasedServer {
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
this.rateLimit = {
|
|
16
|
+
ws: 2e3,
|
|
17
|
+
http: 1e3,
|
|
18
|
+
drain: 500,
|
|
19
|
+
};
|
|
20
|
+
this.blockedIps = new Set();
|
|
21
|
+
// opposite of blockedIps can never get blocked
|
|
22
|
+
this.allowedIps = new Set();
|
|
23
|
+
// per ip so consitent unfortanetly
|
|
24
|
+
// check how large it is and make a loop to downgrade it
|
|
25
|
+
this.rateLimitCounter = new Map();
|
|
26
|
+
this.requestsCounterInProgress = false;
|
|
27
|
+
this.activeObservables = {};
|
|
28
|
+
this.activeObservablesById = new Map();
|
|
29
|
+
this.listeners = {};
|
|
30
|
+
this.functions = new functions_1.BasedFunctions(this, opts.functions);
|
|
31
|
+
this.auth = new auth_1.BasedAuth(this, opts.auth);
|
|
32
|
+
if (opts.workerRequest) {
|
|
33
|
+
this.workerRequest = opts.workerRequest;
|
|
34
|
+
}
|
|
35
|
+
if (opts.rateLimit) {
|
|
36
|
+
this.rateLimit = opts.rateLimit;
|
|
37
|
+
}
|
|
38
|
+
(0, incoming_1.default)(this, opts);
|
|
39
|
+
}
|
|
40
|
+
emit(type, client, val, err) {
|
|
41
|
+
if (this.listeners[type]) {
|
|
42
|
+
this.listeners[type].forEach((fn) => fn(client, val, err));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
on(type, fn) {
|
|
46
|
+
if (!this.listeners[type]) {
|
|
47
|
+
this.listeners[type] = [];
|
|
48
|
+
}
|
|
49
|
+
this.listeners[type].push(fn);
|
|
50
|
+
}
|
|
51
|
+
removeAllListeners() {
|
|
52
|
+
this.listeners = {};
|
|
53
|
+
}
|
|
54
|
+
once(type, fn) {
|
|
55
|
+
this.on(type, (v) => {
|
|
56
|
+
fn(v);
|
|
57
|
+
this.off(type, fn);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
off(type, fn) {
|
|
61
|
+
const listeners = this.listeners[type];
|
|
62
|
+
if (listeners) {
|
|
63
|
+
if (!fn) {
|
|
64
|
+
delete this.listeners[type];
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
for (let i = 0, len = listeners.length; i < len; i++) {
|
|
68
|
+
if (listeners[i] === fn) {
|
|
69
|
+
listeners.splice(i, 1);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (listeners.length === 0) {
|
|
74
|
+
delete this.listeners[type];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async start(port, sharedSocket) {
|
|
80
|
+
if (!port) {
|
|
81
|
+
port = this.port;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
this.port = port;
|
|
85
|
+
}
|
|
86
|
+
await (0, utils_1.wait)(10);
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
this.uwsApp.listen(this.port, sharedSocket ? 0 : 1, (listenSocket) => {
|
|
89
|
+
if (listenSocket) {
|
|
90
|
+
console.info('💫 Based-edge-server listening on port:', this.port);
|
|
91
|
+
// do this better wrap a nice thing arround it
|
|
92
|
+
this.listenSocket = listenSocket;
|
|
93
|
+
resolve(this);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.info('🤮 Based-edge-server error on port:', this.port);
|
|
97
|
+
reject(new Error('Cannot start based-server on port: ' + this.port));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async destroy() {
|
|
103
|
+
console.info('🔥 Destroy Based-edge-server');
|
|
104
|
+
if (this.listenSocket) {
|
|
105
|
+
uws_1.default.us_listen_socket_close(this.listenSocket);
|
|
106
|
+
this.listenSocket = null;
|
|
107
|
+
}
|
|
108
|
+
this.listenSocket = null;
|
|
109
|
+
this.uwsApp = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.BasedServer = BasedServer;
|
|
113
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;AAEA,qDAA4B;AAC5B,0DAAoC;AACpC,2CAA4D;AAC5D,iCAA8C;AAE9C,wCAAmC;AAoCnC,iBAAiB;AACjB,MAAa,WAAW;IAgDtB,YAAY,IAAmB;QAvCxB,cAAS,GAAc;YAC5B,EAAE,EAAE,GAAG;YACP,IAAI,EAAE,GAAG;YACT,KAAK,EAAE,GAAG;SACX,CAAA;QAIM,eAAU,GAAgB,IAAI,GAAG,EAAE,CAAA;QAE1C,+CAA+C;QACxC,eAAU,GAAgB,IAAI,GAAG,EAAE,CAAA;QAE1C,mCAAmC;QACnC,wDAAwD;QACjD,qBAAgB,GAMnB,IAAI,GAAG,EAAE,CAAA;QAEN,8BAAyB,GAAY,KAAK,CAAA;QAI1C,sBAAiB,GAEpB,EAAE,CAAA;QAEC,0BAAqB,GAAkC,IAAI,GAAG,EAAE,CAAA;QAEhE,cAAS,GAEZ,EAAE,CAAA;QAKJ,IAAI,CAAC,SAAS,GAAG,IAAI,0BAAc,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;QACzD,IAAI,CAAC,IAAI,GAAG,IAAI,gBAAS,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;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AC,IAAI,KAAK,CAAC,qCAAqC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;iBACrE;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,KAAK,CAAC,OAAO;QACX,OAAO,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAA;QAC5C,IAAI,IAAI,CAAC,YAAY,EAAE;YACrB,aAAG,CAAC,sBAAsB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;YAC7C,IAAI,CAAC,YAAY,GAAG,IAAI,CAAA;SACzB;QACD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAA;QACxB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;IACpB,CAAC;CACF;AAtID,kCAsIC"}
|
package/package.json
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/server",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"package.json",
|
|
8
|
+
"README.md",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./dist/index.js",
|
|
13
|
+
"./worker": "./dist/worker/api.js",
|
|
14
|
+
"./dist/worker/api": "./dist/worker/api.js"
|
|
15
|
+
},
|
|
7
16
|
"scripts": {
|
|
8
17
|
"build": "npx tsc",
|
|
9
18
|
"watch": "npm run build -- --watch",
|
|
10
19
|
"clean": "rimraf {.turbo,dist,node_modules}"
|
|
11
20
|
},
|
|
12
|
-
"peerDependencies": {
|
|
13
|
-
"@saulx/selva": "10||11||12||13||14||15||16||17||18||19||20"
|
|
14
|
-
},
|
|
15
21
|
"dependencies": {
|
|
16
|
-
"@based/client": "^3.3.0",
|
|
17
|
-
"@based/graphql": "^2.4.4",
|
|
18
22
|
"@based/uws": "2.4.0",
|
|
19
23
|
"@saulx/diff": "^1.1.3",
|
|
20
24
|
"@saulx/hash": "^1.1.0",
|
|
21
|
-
"@saulx/utils": "^2.
|
|
22
|
-
"
|
|
23
|
-
"graphql": "^16.3.0",
|
|
25
|
+
"@saulx/utils": "^2.4.0",
|
|
26
|
+
"picocolors": "^1.0.0",
|
|
24
27
|
"jsonexport": "^3.2.0",
|
|
25
28
|
"jsonwebtoken": "^8.5.1",
|
|
26
|
-
"mime-types": "^2.1.31"
|
|
27
|
-
"ua-parser-js": "^0.7.28"
|
|
29
|
+
"mime-types": "^2.1.31"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
32
|
"@types/node": "^17.0.23",
|
|
31
|
-
"ts-node": "10.
|
|
33
|
+
"ts-node": "^10.1.0",
|
|
32
34
|
"typescript": "^4.3.5",
|
|
33
35
|
"rimraf": "^3.0.2"
|
|
34
36
|
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Query, SetOptions, GetOptions, GenericObject, Copy, Configuration, Based as BasedAdminClient, TrackOpts } from '@based/client';
|
|
2
|
-
import RedisSelvaClient from '@saulx/selva/dist/src/redis';
|
|
3
|
-
import { Params } from './Params';
|
|
4
|
-
import { SignOptions } from 'jsonwebtoken';
|
|
5
|
-
declare class BasedServerClient {
|
|
6
|
-
private _params;
|
|
7
|
-
private _noAuth;
|
|
8
|
-
constructor(params: Params, noAuth?: boolean);
|
|
9
|
-
opts: {
|
|
10
|
-
cluster: string;
|
|
11
|
-
org: string;
|
|
12
|
-
project: string;
|
|
13
|
-
env: string;
|
|
14
|
-
};
|
|
15
|
-
get state(): GenericObject;
|
|
16
|
-
get redis(): RedisSelvaClient;
|
|
17
|
-
destroy(): void;
|
|
18
|
-
private _authorize;
|
|
19
|
-
event(type: string, params: {
|
|
20
|
-
[key: string]: string | number | boolean;
|
|
21
|
-
}, opts?: TrackOpts): Promise<void>;
|
|
22
|
-
clearAnalytics(type: string, params?: {
|
|
23
|
-
[key: string]: number | string | boolean;
|
|
24
|
-
}): Promise<void>;
|
|
25
|
-
observeUntil(query: Query, condition: (data: GenericObject, checksum: number) => boolean, onData?: (data: any, checksum: number) => void): Promise<GenericObject>;
|
|
26
|
-
admin(key: string): Promise<BasedAdminClient>;
|
|
27
|
-
observe(query: Query, onData: (data: any, checksum: number) => void, onErr?: (err: Error) => void): Promise<() => void>;
|
|
28
|
-
observe(name: string, onData: (data: any, checksum: number) => void, onErr?: (err: Error) => void): Promise<() => void>;
|
|
29
|
-
observe(name: string, payload: any, onData: (data: any, checksum: number) => void, onErr?: (err: Error) => void): Promise<() => void>;
|
|
30
|
-
get(query: GetOptions): Promise<GenericObject>;
|
|
31
|
-
get(name: string, payload?: any): Promise<GenericObject>;
|
|
32
|
-
copy(payload: Copy): Promise<{
|
|
33
|
-
ids: string[];
|
|
34
|
-
}>;
|
|
35
|
-
id(type: string, opts?: any): Promise<string>;
|
|
36
|
-
call(name: string, payload?: any): Promise<GenericObject>;
|
|
37
|
-
delete(payload: {
|
|
38
|
-
$id: string;
|
|
39
|
-
$db?: string;
|
|
40
|
-
}): Promise<{
|
|
41
|
-
isDeleted: boolean;
|
|
42
|
-
}>;
|
|
43
|
-
set(payload: SetOptions): Promise<{
|
|
44
|
-
id: string;
|
|
45
|
-
}>;
|
|
46
|
-
schema(): Promise<Configuration>;
|
|
47
|
-
updateSchema(configuration: {
|
|
48
|
-
schema?: GenericObject;
|
|
49
|
-
db?: string;
|
|
50
|
-
}): Promise<GenericObject>;
|
|
51
|
-
encode(payload: string | object, privateKeySecret: string | {
|
|
52
|
-
secret: string;
|
|
53
|
-
} | {
|
|
54
|
-
key: string;
|
|
55
|
-
}, type?: 'jwt', signOptions?: SignOptions): Promise<string>;
|
|
56
|
-
decode(payload: string, secretOrPublicKey: string | {
|
|
57
|
-
publicKey: string;
|
|
58
|
-
}, type?: 'jwt'): Promise<any>;
|
|
59
|
-
secret(secret: string): Promise<any>;
|
|
60
|
-
digest(payload: string): Promise<string>;
|
|
61
|
-
sendEmail(payload: {
|
|
62
|
-
to: string;
|
|
63
|
-
subject: string;
|
|
64
|
-
body: string;
|
|
65
|
-
from?: string;
|
|
66
|
-
}): Promise<{
|
|
67
|
-
status: 'ok';
|
|
68
|
-
message?: string;
|
|
69
|
-
}>;
|
|
70
|
-
}
|
|
71
|
-
export default BasedServerClient;
|