@based/server 0.0.1
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/BasedServerClient.d.ts +48 -0
- package/dist/BasedServerClient.js +313 -0
- package/dist/BasedServerClient.js.map +1 -0
- package/dist/Client.d.ts +69 -0
- package/dist/Client.js +253 -0
- package/dist/Client.js.map +1 -0
- package/dist/Params.d.ts +19 -0
- package/dist/Params.js +45 -0
- package/dist/Params.js.map +1 -0
- package/dist/closeListener.d.ts +4 -0
- package/dist/closeListener.js +13 -0
- package/dist/closeListener.js.map +1 -0
- package/dist/findPrefix.d.ts +3 -0
- package/dist/findPrefix.js +20 -0
- package/dist/findPrefix.js.map +1 -0
- package/dist/getFromConfig.d.ts +3 -0
- package/dist/getFromConfig.js +49 -0
- package/dist/getFromConfig.js.map +1 -0
- package/dist/handlers/authorize.d.ts +5 -0
- package/dist/handlers/authorize.js +173 -0
- package/dist/handlers/authorize.js.map +1 -0
- package/dist/handlers/configure/index.d.ts +5 -0
- package/dist/handlers/configure/index.js +44 -0
- package/dist/handlers/configure/index.js.map +1 -0
- package/dist/handlers/copy/index.d.ts +8 -0
- package/dist/handlers/copy/index.js +135 -0
- package/dist/handlers/copy/index.js.map +1 -0
- package/dist/handlers/delete/index.d.ts +5 -0
- package/dist/handlers/delete/index.js +34 -0
- package/dist/handlers/delete/index.js.map +1 -0
- package/dist/handlers/digest/index.d.ts +5 -0
- package/dist/handlers/digest/index.js +23 -0
- package/dist/handlers/digest/index.js.map +1 -0
- package/dist/handlers/functions/call.d.ts +6 -0
- package/dist/handlers/functions/call.js +47 -0
- package/dist/handlers/functions/call.js.map +1 -0
- package/dist/handlers/functions/observable/Observable.d.ts +49 -0
- package/dist/handlers/functions/observable/Observable.js +290 -0
- package/dist/handlers/functions/observable/Observable.js.map +1 -0
- package/dist/handlers/functions/observable/SharedObservable.d.ts +43 -0
- package/dist/handlers/functions/observable/SharedObservable.js +312 -0
- package/dist/handlers/functions/observable/SharedObservable.js.map +1 -0
- package/dist/handlers/functions/observable/index.d.ts +13 -0
- package/dist/handlers/functions/observable/index.js +130 -0
- package/dist/handlers/functions/observable/index.js.map +1 -0
- package/dist/handlers/get/index.d.ts +5 -0
- package/dist/handlers/get/index.js +23 -0
- package/dist/handlers/get/index.js.map +1 -0
- package/dist/handlers/getConfig/index.d.ts +5 -0
- package/dist/handlers/getConfig/index.js +37 -0
- package/dist/handlers/getConfig/index.js.map +1 -0
- package/dist/handlers/handleRequests.d.ts +5 -0
- package/dist/handlers/handleRequests.js +62 -0
- package/dist/handlers/handleRequests.js.map +1 -0
- package/dist/handlers/index.d.ts +4 -0
- package/dist/handlers/index.js +49 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/messageParser.d.ts +5 -0
- package/dist/handlers/messageParser.js +76 -0
- package/dist/handlers/messageParser.js.map +1 -0
- package/dist/handlers/rest/file/DataStream.d.ts +7 -0
- package/dist/handlers/rest/file/DataStream.js +17 -0
- package/dist/handlers/rest/file/DataStream.js.map +1 -0
- package/dist/handlers/rest/file/formStream.d.ts +4 -0
- package/dist/handlers/rest/file/formStream.js +184 -0
- package/dist/handlers/rest/file/formStream.js.map +1 -0
- package/dist/handlers/rest/file/getExtenstion.d.ts +2 -0
- package/dist/handlers/rest/file/getExtenstion.js +22 -0
- package/dist/handlers/rest/file/getExtenstion.js.map +1 -0
- package/dist/handlers/rest/file/index.d.ts +4 -0
- package/dist/handlers/rest/file/index.js +53 -0
- package/dist/handlers/rest/file/index.js.map +1 -0
- package/dist/handlers/rest/file/storeFile.d.ts +5 -0
- package/dist/handlers/rest/file/storeFile.js +24 -0
- package/dist/handlers/rest/file/storeFile.js.map +1 -0
- package/dist/handlers/rest/file/stream.d.ts +5 -0
- package/dist/handlers/rest/file/stream.js +98 -0
- package/dist/handlers/rest/file/stream.js.map +1 -0
- package/dist/handlers/rest/file/types.d.ts +9 -0
- package/dist/handlers/rest/file/types.js +3 -0
- package/dist/handlers/rest/file/types.js.map +1 -0
- package/dist/handlers/rest/getReqMessage.d.ts +3 -0
- package/dist/handlers/rest/getReqMessage.js +88 -0
- package/dist/handlers/rest/getReqMessage.js.map +1 -0
- package/dist/handlers/rest/index.d.ts +4 -0
- package/dist/handlers/rest/index.js +111 -0
- package/dist/handlers/rest/index.js.map +1 -0
- package/dist/handlers/rest/invalidReq.d.ts +4 -0
- package/dist/handlers/rest/invalidReq.js +20 -0
- package/dist/handlers/rest/invalidReq.js.map +1 -0
- package/dist/handlers/rest/parseResponse.d.ts +4 -0
- package/dist/handlers/rest/parseResponse.js +176 -0
- package/dist/handlers/rest/parseResponse.js.map +1 -0
- package/dist/handlers/rest/readBody.d.ts +3 -0
- package/dist/handlers/rest/readBody.js +44 -0
- package/dist/handlers/rest/readBody.js.map +1 -0
- package/dist/handlers/set/index.d.ts +5 -0
- package/dist/handlers/set/index.js +20 -0
- package/dist/handlers/set/index.js.map +1 -0
- package/dist/handlers/subscription/index.d.ts +34 -0
- package/dist/handlers/subscription/index.js +206 -0
- package/dist/handlers/subscription/index.js.map +1 -0
- package/dist/handlers/token.d.ts +5 -0
- package/dist/handlers/token.js +64 -0
- package/dist/handlers/token.js.map +1 -0
- package/dist/handlers/track/index.d.ts +7 -0
- package/dist/handlers/track/index.js +102 -0
- package/dist/handlers/track/index.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +211 -0
- package/dist/index.js.map +1 -0
- package/dist/openListener.d.ts +4 -0
- package/dist/openListener.js +13 -0
- package/dist/openListener.js.map +1 -0
- package/dist/secrets.d.ts +6 -0
- package/dist/secrets.js +103 -0
- package/dist/secrets.js.map +1 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/upgradeListener.d.ts +4 -0
- package/dist/upgradeListener.js +47 -0
- package/dist/upgradeListener.js.map +1 -0
- package/dist/workers/index.d.ts +0 -0
- package/dist/workers/index.js +20 -0
- package/dist/workers/index.js.map +1 -0
- package/dist/workers/workerCode.d.ts +12 -0
- package/dist/workers/workerCode.js +46 -0
- package/dist/workers/workerCode.js.map +1 -0
- package/package.json +29 -0
- package/readme.md +1 -0
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
const readBody_1 = __importDefault(require("./readBody"));
|
|
7
|
+
const Client_1 = require("../../Client");
|
|
8
|
+
const getReqMessage_1 = __importDefault(require("./getReqMessage"));
|
|
9
|
+
const handleRequests_1 = __importDefault(require("../handleRequests"));
|
|
10
|
+
const querystring_1 = __importDefault(require("querystring"));
|
|
11
|
+
const file_1 = __importDefault(require("./file"));
|
|
12
|
+
const invalidReq_1 = __importDefault(require("./invalidReq"));
|
|
13
|
+
const execMessages = (server, res, message, token, format = 0) => {
|
|
14
|
+
const client = new Client_1.Client(server, undefined, res, format);
|
|
15
|
+
const messages = [message];
|
|
16
|
+
if (token) {
|
|
17
|
+
client.setToken(token);
|
|
18
|
+
}
|
|
19
|
+
res.client = client;
|
|
20
|
+
(0, handleRequests_1.default)(server, client, messages);
|
|
21
|
+
};
|
|
22
|
+
exports.default = async (server, req, res) => {
|
|
23
|
+
const url = req.getUrl();
|
|
24
|
+
res.onAborted(() => {
|
|
25
|
+
res.aborted = true;
|
|
26
|
+
if (res.client) {
|
|
27
|
+
res.client.destroy();
|
|
28
|
+
res.client = null;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
if (!url) {
|
|
32
|
+
(0, invalidReq_1.default)(res);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const fn = url.split('/');
|
|
36
|
+
const handler = fn[1];
|
|
37
|
+
if (!(handler &&
|
|
38
|
+
(handler === 'call' ||
|
|
39
|
+
handler === 'file' ||
|
|
40
|
+
handler === 'get' ||
|
|
41
|
+
handler === 'delete' ||
|
|
42
|
+
handler === 'copy' ||
|
|
43
|
+
handler === 'set' ||
|
|
44
|
+
handler === 'configure' ||
|
|
45
|
+
handler === 'update-schema' ||
|
|
46
|
+
handler === 'updateSchema' ||
|
|
47
|
+
handler === 'configuration' ||
|
|
48
|
+
handler === 'track' ||
|
|
49
|
+
handler === 'schema' ||
|
|
50
|
+
handler === 'digest'))) {
|
|
51
|
+
(0, invalidReq_1.default)(res);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (handler === 'file') {
|
|
55
|
+
return (0, file_1.default)(server, req, res, url);
|
|
56
|
+
}
|
|
57
|
+
let token = req.getHeader('authorization');
|
|
58
|
+
const acceptEncoding = req.getHeader('accept-encoding');
|
|
59
|
+
const method = req.getMethod();
|
|
60
|
+
const contentType = req.getHeader('content-type') || 'application/json';
|
|
61
|
+
// have to make this cleaner...
|
|
62
|
+
if (acceptEncoding) {
|
|
63
|
+
res.acceptEncoding = acceptEncoding;
|
|
64
|
+
}
|
|
65
|
+
let format = 0;
|
|
66
|
+
const last = fn[fn.length - 1];
|
|
67
|
+
if (last.endsWith('.csv')) {
|
|
68
|
+
format = 1;
|
|
69
|
+
fn[fn.length - 1] = last.slice(0, -4);
|
|
70
|
+
}
|
|
71
|
+
res.ua = req.getHeader('user-agent');
|
|
72
|
+
if (method === 'post') {
|
|
73
|
+
(0, readBody_1.default)(res, handler === 'digest' ? 'text/plain' : contentType, (d) => {
|
|
74
|
+
const message = (0, getReqMessage_1.default)(fn, d);
|
|
75
|
+
if (!message) {
|
|
76
|
+
(0, invalidReq_1.default)(res);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
execMessages(server, res, message, token, format);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (method === 'get') {
|
|
85
|
+
const query = req.getQuery();
|
|
86
|
+
let payload;
|
|
87
|
+
if (query) {
|
|
88
|
+
const x = querystring_1.default.parse(query);
|
|
89
|
+
const p = x.q || x.payload;
|
|
90
|
+
if (x.token && !token) {
|
|
91
|
+
if (typeof x.token === 'string') {
|
|
92
|
+
token = x.token;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (p) {
|
|
96
|
+
try {
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
payload = JSON.parse(p);
|
|
99
|
+
}
|
|
100
|
+
catch (err) { }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const message = (0, getReqMessage_1.default)(fn, payload);
|
|
104
|
+
if (message) {
|
|
105
|
+
execMessages(server, res, message, token, format);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
(0, invalidReq_1.default)(res);
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/handlers/rest/index.ts"],"names":[],"mappings":";;;;;AAEA,0DAAiC;AACjC,yCAAqC;AACrC,oEAA2C;AAE3C,uEAA8C;AAC9C,8DAA4B;AAC5B,kDAAyB;AACzB,8DAAqC;AAErC,MAAM,YAAY,GAAG,CACnB,MAAmB,EACnB,GAAqB,EACrB,OAA+B,EAC/B,KAAc,EACd,SAAgB,CAAC,EACjB,EAAE;IACF,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;IACzD,MAAM,QAAQ,GAAG,CAAC,OAAO,CAAC,CAAA;IAC1B,IAAI,KAAK,EAAE;QACT,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;KACvB;IACD,GAAG,CAAC,MAAM,GAAG,MAAM,CAAA;IACnB,IAAA,wBAAc,EAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;AAC1C,CAAC,CAAA;AAED,kBAAe,KAAK,EAClB,MAAmB,EACnB,GAAoB,EACpB,GAA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CAAC,KAAK,CAAC,CAAA;YACzB,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAA;YAC1B,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,KAAK,EAAE;gBACrB,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,EAAE;oBAC/B,KAAK,GAAG,CAAC,CAAC,KAAK,CAAA;iBAChB;aACF;YACD,IAAI,CAAC,EAAE;gBACL,IAAI;oBACF,aAAa;oBACb,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;iBACxB;gBAAC,OAAO,GAAG,EAAE,GAAE;aACjB;SACF;QACD,MAAM,OAAO,GAAG,IAAA,uBAAa,EAAC,EAAE,EAAE,OAAO,CAAC,CAAA;QAC1C,IAAI,OAAO,EAAE;YACX,YAAY,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;YACjD,OAAM;SACP;KACF;IAED,IAAA,oBAAU,EAAC,GAAG,CAAC,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.invalidReqNoCors = void 0;
|
|
4
|
+
const invalidReq = (res) => {
|
|
5
|
+
res.aborted = true;
|
|
6
|
+
res
|
|
7
|
+
.writeStatus('400 Invalid Request')
|
|
8
|
+
.writeHeader('Access-Control-Allow-Origin', '*')
|
|
9
|
+
.writeHeader('Access-Control-Allow-Headers', 'content-type')
|
|
10
|
+
.end(`{"code":400,"error":"Invalid Request"}`);
|
|
11
|
+
};
|
|
12
|
+
const invalidReqNoCors = (res) => {
|
|
13
|
+
res.aborted = true;
|
|
14
|
+
res
|
|
15
|
+
.writeStatus('400 Invalid Request')
|
|
16
|
+
.end(`{"code":400,"error":"Invalid Request"}`);
|
|
17
|
+
};
|
|
18
|
+
exports.invalidReqNoCors = invalidReqNoCors;
|
|
19
|
+
exports.default = invalidReq;
|
|
20
|
+
//# sourceMappingURL=invalidReq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invalidReq.js","sourceRoot":"","sources":["../../../src/handlers/rest/invalidReq.ts"],"names":[],"mappings":";;;AAEA,MAAM,UAAU,GAAG,CAAC,GAAqB,EAAE,EAAE;IAC3C,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;IAClB,GAAG;SACA,WAAW,CAAC,qBAAqB,CAAC;SAClC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC;SAC/C,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC;SAC3D,GAAG,CAAC,wCAAwC,CAAC,CAAA;AAClD,CAAC,CAAA;AAEM,MAAM,gBAAgB,GAAG,CAAC,GAAqB,EAAE,EAAE;IACxD,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;IAClB,GAAG;SACA,WAAW,CAAC,qBAAqB,CAAC;SAClC,GAAG,CAAC,wCAAwC,CAAC,CAAA;AAClD,CAAC,CAAA;AALY,QAAA,gBAAgB,oBAK5B;AAED,kBAAe,UAAU,CAAA"}
|
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
const client_1 = require("@based/client");
|
|
7
|
+
const hash_1 = require("@saulx/hash");
|
|
8
|
+
const zlib_1 = __importDefault(require("zlib"));
|
|
9
|
+
const jsonexport_1 = __importDefault(require("jsonexport"));
|
|
10
|
+
const invalid = (res, error, statusMsg = 'Invald request') => {
|
|
11
|
+
res.aborted = true;
|
|
12
|
+
res.writeStatus('400 ' + statusMsg);
|
|
13
|
+
res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
14
|
+
res.writeHeader('Access-Control-Allow-Headers', 'content-type');
|
|
15
|
+
res.writeHeader('Content-Type', 'application/json');
|
|
16
|
+
res.end(JSON.stringify({ error, code: 400 }));
|
|
17
|
+
};
|
|
18
|
+
const ok = (res) => {
|
|
19
|
+
res.writeStatus('200 OK');
|
|
20
|
+
res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
21
|
+
res.writeHeader('Access-Control-Allow-Headers', 'content-type');
|
|
22
|
+
};
|
|
23
|
+
exports.default = (res, payload, type) => {
|
|
24
|
+
if (res.aborted) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
let result;
|
|
28
|
+
const reqType = payload[0];
|
|
29
|
+
if (reqType === client_1.RequestTypes.Get) {
|
|
30
|
+
if (payload[3]) {
|
|
31
|
+
invalid(res, payload[3], 'Invalid Query');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const r = payload[2];
|
|
36
|
+
const checksum = (0, hash_1.hashObjectIgnoreKeyOrder)(r);
|
|
37
|
+
ok(res);
|
|
38
|
+
res.writeHeader('ETag', String(checksum));
|
|
39
|
+
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate');
|
|
40
|
+
result = r;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (reqType === client_1.RequestTypes.Set ||
|
|
44
|
+
reqType === client_1.RequestTypes.Copy ||
|
|
45
|
+
reqType === client_1.RequestTypes.Configuration ||
|
|
46
|
+
reqType === client_1.RequestTypes.Digest ||
|
|
47
|
+
reqType === client_1.RequestTypes.GetConfiguration ||
|
|
48
|
+
reqType === client_1.RequestTypes.Delete) {
|
|
49
|
+
if (payload[3]) {
|
|
50
|
+
invalid(res, payload[3]);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
ok(res);
|
|
55
|
+
result = payload[2];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (reqType === client_1.RequestTypes.Call) {
|
|
59
|
+
if (payload[3]) {
|
|
60
|
+
invalid(res, payload[3]);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
ok(res);
|
|
65
|
+
result = payload[2];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (reqType === client_1.RequestTypes.Subscription) {
|
|
69
|
+
if (payload[4]) {
|
|
70
|
+
invalid(res, payload[4]);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
ok(res);
|
|
75
|
+
res.writeHeader('ETag', String(payload[3]));
|
|
76
|
+
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate');
|
|
77
|
+
result = payload[2];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
invalid(res, 'Invalid Request');
|
|
82
|
+
}
|
|
83
|
+
if (reqType === client_1.RequestTypes.Digest) {
|
|
84
|
+
res.writeHeader('Content-Type', 'text/plain');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
if (type === 1) {
|
|
88
|
+
res.writeHeader('Content-Type', 'text/csv');
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
res.writeHeader('Content-Type', 'application/json');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (result !== undefined) {
|
|
95
|
+
let parsed;
|
|
96
|
+
let csvParser;
|
|
97
|
+
if (type === 0) {
|
|
98
|
+
parsed = JSON.stringify(result);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
csvParser = (0, jsonexport_1.default)();
|
|
102
|
+
}
|
|
103
|
+
if (res.acceptEncoding) {
|
|
104
|
+
const acceptEncoding = res.acceptEncoding;
|
|
105
|
+
let compressor;
|
|
106
|
+
if (acceptEncoding.includes('deflate')) {
|
|
107
|
+
res.writeHeader('Content-Encoding', 'deflate');
|
|
108
|
+
compressor = zlib_1.default.createDeflate();
|
|
109
|
+
}
|
|
110
|
+
else if (acceptEncoding.includes('gzip')) {
|
|
111
|
+
res.writeHeader('Content-Encoding', 'gzip');
|
|
112
|
+
compressor = zlib_1.default.createGzip();
|
|
113
|
+
}
|
|
114
|
+
else if (acceptEncoding.includes('br')) {
|
|
115
|
+
res.writeHeader('Content-Encoding', 'br');
|
|
116
|
+
compressor = zlib_1.default.createBrotliCompress();
|
|
117
|
+
}
|
|
118
|
+
if (compressor) {
|
|
119
|
+
compressor.on('data', (buffer) => {
|
|
120
|
+
if (!res.aborted) {
|
|
121
|
+
res.write(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
compressor.on('end', () => {
|
|
125
|
+
if (!res.aborted) {
|
|
126
|
+
res.end();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
if (csvParser) {
|
|
130
|
+
if (!res.aborted) {
|
|
131
|
+
csvParser.pipe(compressor);
|
|
132
|
+
csvParser.write(Buffer.from(JSON.stringify(result)));
|
|
133
|
+
csvParser.end();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
if (!res.aborted) {
|
|
138
|
+
compressor.write(Buffer.from(parsed));
|
|
139
|
+
compressor.end();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (csvParser) {
|
|
146
|
+
csvParser.on('end', () => {
|
|
147
|
+
console.info('end');
|
|
148
|
+
if (!res.aborted) {
|
|
149
|
+
res.end();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
csvParser.on('data', (buffer) => {
|
|
153
|
+
if (!res.aborted) {
|
|
154
|
+
res.write(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
if (!res.aborted) {
|
|
158
|
+
csvParser.write(Buffer.from(JSON.stringify(result)));
|
|
159
|
+
csvParser.end();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
if (!res.aborted) {
|
|
164
|
+
res.end(parsed);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
if (!res.aborted) {
|
|
170
|
+
res.end();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// fix
|
|
174
|
+
res.client = null;
|
|
175
|
+
};
|
|
176
|
+
//# sourceMappingURL=parseResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseResponse.js","sourceRoot":"","sources":["../../../src/handlers/rest/parseResponse.ts"],"names":[],"mappings":";;;;;AACA,0CAA0D;AAC1D,sCAAsD;AACtD,gDAAuB;AAEvB,4DAAmC;AAEnC,MAAM,OAAO,GAAG,CACd,GAAqB,EACrB,KAAU,EACV,YAAoB,gBAAgB,EACpC,EAAE;IACF,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;IAClB,GAAG,CAAC,WAAW,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IACnC,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,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;AAC/C,CAAC,CAAA;AAED,MAAM,EAAE,GAAG,CAAC,GAAqB,EAAE,EAAE;IACnC,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAA;IACzB,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;IACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;AACjE,CAAC,CAAA;AAED,kBAAe,CAAC,GAAqB,EAAE,OAAqB,E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AAG,CAAC,GAAG,EAAE,CAAA;SACV;KACF;IACD,MAAM;IACN,GAAG,CAAC,MAAM,GAAG,IAAI,CAAA;AACnB,CAAC,CAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MAX_BODY_SIZE = 4 ** 20; // 2MB
|
|
4
|
+
exports.default = (res, contentType,
|
|
5
|
+
// add more later...
|
|
6
|
+
onData) => {
|
|
7
|
+
let data = Buffer.from([]);
|
|
8
|
+
res.onData(async (chunk, isLast) => {
|
|
9
|
+
if (res.aborted) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
data = Buffer.concat([data, Buffer.from(chunk)]);
|
|
13
|
+
// data += Buffer.from(chunk).toString('utf8')
|
|
14
|
+
if (data.length > MAX_BODY_SIZE) {
|
|
15
|
+
res.writeStatus('413 Payload Too Large');
|
|
16
|
+
res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
17
|
+
res.writeHeader('Access-Control-Allow-Headers', 'content-type');
|
|
18
|
+
res.end(`{"code":413,"error":"Payload Too Large"}`);
|
|
19
|
+
res.aborted = true;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (isLast) {
|
|
23
|
+
let params;
|
|
24
|
+
const str = data.toString();
|
|
25
|
+
if (contentType === 'application/json') {
|
|
26
|
+
try {
|
|
27
|
+
params = data.length ? JSON.parse(str) : undefined;
|
|
28
|
+
onData(params);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
res.aborted = true;
|
|
32
|
+
res.writeStatus('400 Invalid Request');
|
|
33
|
+
res.writeHeader('Access-Control-Allow-Origin', '*');
|
|
34
|
+
res.writeHeader('Access-Control-Allow-Headers', 'content-type');
|
|
35
|
+
res.end(`{"code":400,"error":"Invalid payload"}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
onData(str);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=readBody.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readBody.js","sourceRoot":"","sources":["../../../src/handlers/rest/readBody.ts"],"names":[],"mappings":";;AAEA,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,CAAA,CAAC,MAAM;AAEpC,kBAAe,CACb,GAAqB,EACrB,WAAmB;AACnB,oBAAoB;AACpB,MAAkC,EAClC,EAAE;IACF,IAAI,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAC1B,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QACjC,IAAI,GAAG,CAAC,OAAO,EAAE;YACf,OAAM;SACP;QACD,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAChD,8CAA8C;QAC9C,IAAI,IAAI,CAAC,MAAM,GAAG,aAAa,EAAE;YAC/B,GAAG,CAAC,WAAW,CAAC,uBAAuB,CAAC,CAAA;YACxC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;YACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;YAC/D,GAAG,CAAC,GAAG,CAAC,0CAA0C,CAAC,CAAA;YACnD,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;YAClB,OAAM;SACP;QACD,IAAI,MAAM,EAAE;YACV,IAAI,MAAM,CAAA;YACV,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAA;YAC3B,IAAI,WAAW,KAAK,kBAAkB,EAAE;gBACtC,IAAI;oBACF,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;oBAClD,MAAM,CAAC,MAAM,CAAC,CAAA;iBACf;gBAAC,OAAO,CAAC,EAAE;oBACV,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;oBAClB,GAAG,CAAC,WAAW,CAAC,qBAAqB,CAAC,CAAA;oBACtC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;oBACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;oBAC/D,GAAG,CAAC,GAAG,CAAC,wCAAwC,CAAC,CAAA;iBAClD;aACF;iBAAM;gBACL,MAAM,CAAC,GAAG,CAAC,CAAA;aACZ;SACF;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BasedServer } from '../..';
|
|
2
|
+
import Client from '../../Client';
|
|
3
|
+
import { RequestMessage } from '@based/client';
|
|
4
|
+
declare const _default: (server: BasedServer, client: Client, [, reqId, payload]: RequestMessage<import("@based/client").GenericObject>) => Promise<void>;
|
|
5
|
+
export default _default;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const client_1 = require("@based/client");
|
|
4
|
+
exports.default = async (server, client, [, reqId, payload]) => {
|
|
5
|
+
if (typeof payload === 'string')
|
|
6
|
+
throw new Error('payload cannot be a string');
|
|
7
|
+
try {
|
|
8
|
+
const id = await server.db.set(payload);
|
|
9
|
+
client.send([client_1.RequestTypes.Set, reqId, { id }]);
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
client.send([
|
|
13
|
+
client_1.RequestTypes.Set,
|
|
14
|
+
reqId,
|
|
15
|
+
0,
|
|
16
|
+
{ type: 'ValidationError', name: 'set', message: err.message, payload },
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/handlers/set/index.ts"],"names":[],"mappings":";;AAEA,0CAA4D;AAC5D,kBAAe,KAAK,EAClB,MAAmB,EACnB,MAAc,EACd,CAAC,EAAE,KAAK,EAAE,OAAO,CAAiB,EAClC,EAAE;IACF,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;IAC9E,IAAI;QACF,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACvC,MAAM,CAAC,IAAI,CAAC,CAAC,qBAAY,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;KAC/C;IAAC,OAAO,GAAG,EAAE;QACZ,MAAM,CAAC,IAAI,CAAC;YACV,qBAAY,CAAC,GAAG;YAChB,KAAK;YACL,CAAC;YACD,EAAE,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE;SACxE,CAAC,CAAA;KACH;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BasedServer } from '../..';
|
|
3
|
+
import { Observable } from '@saulx/selva';
|
|
4
|
+
import Client from '../../Client';
|
|
5
|
+
import { SendSubscriptionDataMessage, SubscribeMessage, UnsubscribeMessage, ErrorObject } from '@based/client';
|
|
6
|
+
import { DataListener } from '../../types';
|
|
7
|
+
declare type GenericObject = {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
export declare class Subscription {
|
|
11
|
+
lastDiff: [GenericObject, number];
|
|
12
|
+
checksum: number;
|
|
13
|
+
server: BasedServer;
|
|
14
|
+
removeTimer: NodeJS.Timeout;
|
|
15
|
+
observable: Observable;
|
|
16
|
+
id: number;
|
|
17
|
+
clients: {
|
|
18
|
+
[id: string]: [Client, number];
|
|
19
|
+
};
|
|
20
|
+
clientsCnt: number;
|
|
21
|
+
errorState: ErrorObject;
|
|
22
|
+
retryTimer: NodeJS.Timeout;
|
|
23
|
+
initSubscription(query: any, isSchemaSubscription: any): void;
|
|
24
|
+
constructor(server: BasedServer, id: number, query: any);
|
|
25
|
+
unsubscribeDataListener(client: Client, fn: DataListener, id?: string): void;
|
|
26
|
+
subscribeDataListener(client: Client, fn: DataListener, id?: string): void;
|
|
27
|
+
unsubscribe(client: Client): void;
|
|
28
|
+
subscribe(client: Client, checksum?: number): void;
|
|
29
|
+
sendData(client: Client): void;
|
|
30
|
+
}
|
|
31
|
+
export declare const subscribe: (server: BasedServer, client: Client, [, id, query, checksum, allwaysSend]: SubscribeMessage) => void;
|
|
32
|
+
export declare const sendSubscriptionData: (server: BasedServer, client: Client, [, subscriptionId]: SendSubscriptionDataMessage) => void;
|
|
33
|
+
export declare const unsubscribe: (server: BasedServer, client: Client, [, subscriptionId]: UnsubscribeMessage) => void;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unsubscribe = exports.sendSubscriptionData = exports.subscribe = exports.Subscription = void 0;
|
|
4
|
+
const client_1 = require("@based/client");
|
|
5
|
+
class Subscription {
|
|
6
|
+
constructor(server, id, query) {
|
|
7
|
+
this.clients = {};
|
|
8
|
+
this.clientsCnt = 0;
|
|
9
|
+
this.id = id;
|
|
10
|
+
server.subscriptions[id] = this;
|
|
11
|
+
const isSchemaSubscription = query.$subscribe_schema;
|
|
12
|
+
this.observable = isSchemaSubscription
|
|
13
|
+
? server.db.subscribeSchema(query.$subscribe_schema)
|
|
14
|
+
: server.db.observe(query);
|
|
15
|
+
this.server = server;
|
|
16
|
+
this.initSubscription(query, isSchemaSubscription);
|
|
17
|
+
}
|
|
18
|
+
initSubscription(query, isSchemaSubscription) {
|
|
19
|
+
this.observable.subscribe((data, checksum, diff) => {
|
|
20
|
+
if (isSchemaSubscription) {
|
|
21
|
+
// TODO: investigage why this happens
|
|
22
|
+
if (!this.observable) {
|
|
23
|
+
console.error('! Observable does not exists on schema - sub this should never happen...');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
checksum = data.sha;
|
|
27
|
+
this.observable.version = checksum;
|
|
28
|
+
this.observable.cache = data;
|
|
29
|
+
}
|
|
30
|
+
if (this.errorState) {
|
|
31
|
+
delete this.errorState;
|
|
32
|
+
}
|
|
33
|
+
let payload;
|
|
34
|
+
if (diff) {
|
|
35
|
+
this.lastDiff = [diff, this.checksum];
|
|
36
|
+
payload = [
|
|
37
|
+
client_1.RequestTypes.SubscriptionDiff,
|
|
38
|
+
this.id,
|
|
39
|
+
diff,
|
|
40
|
+
[this.checksum, checksum],
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
if (this.lastDiff) {
|
|
45
|
+
delete this.lastDiff;
|
|
46
|
+
}
|
|
47
|
+
payload = [client_1.RequestTypes.Subscription, this.id, data, checksum];
|
|
48
|
+
}
|
|
49
|
+
for (const id in this.clients) {
|
|
50
|
+
const c = this.clients[id];
|
|
51
|
+
if (checksum !== c[1]) {
|
|
52
|
+
c[1] = checksum;
|
|
53
|
+
c[0].send(payload);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
this.checksum = checksum;
|
|
57
|
+
}, (err) => {
|
|
58
|
+
if (err.message.includes('ERR_SUBSCRIPTIONS ENOENT')) {
|
|
59
|
+
console.error('WRONG INIT SUB RETRY IN 500ms');
|
|
60
|
+
this.retryTimer = setTimeout(() => {
|
|
61
|
+
this.initSubscription(query, isSchemaSubscription);
|
|
62
|
+
}, 500);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
let validationErr = false;
|
|
66
|
+
if (!this.checksum) {
|
|
67
|
+
// need to get type
|
|
68
|
+
validationErr = true;
|
|
69
|
+
// initial error - then remove this sub ?
|
|
70
|
+
}
|
|
71
|
+
this.errorState = {
|
|
72
|
+
type: validationErr ? 'ValidationError' : 'ObserveError',
|
|
73
|
+
message: err.message,
|
|
74
|
+
name: 'subscription',
|
|
75
|
+
query,
|
|
76
|
+
};
|
|
77
|
+
const payload = [
|
|
78
|
+
client_1.RequestTypes.Subscription,
|
|
79
|
+
this.id,
|
|
80
|
+
{},
|
|
81
|
+
0,
|
|
82
|
+
this.errorState,
|
|
83
|
+
];
|
|
84
|
+
for (const id in this.clients) {
|
|
85
|
+
const c = this.clients[id];
|
|
86
|
+
c[0].send(payload);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
unsubscribeDataListener(client, fn, id) { }
|
|
92
|
+
subscribeDataListener(client, fn, id) { }
|
|
93
|
+
unsubscribe(client) {
|
|
94
|
+
if (this.clients[client.id]) {
|
|
95
|
+
delete this.clients[client.id];
|
|
96
|
+
delete client.subscriptions[this.id];
|
|
97
|
+
this.clientsCnt--;
|
|
98
|
+
if (this.clientsCnt === 0) {
|
|
99
|
+
this.removeTimer = setTimeout(() => {
|
|
100
|
+
clearTimeout(this.retryTimer);
|
|
101
|
+
this.observable.unsubscribe();
|
|
102
|
+
delete this.observable;
|
|
103
|
+
delete this.server.subscriptions[this.id];
|
|
104
|
+
}, 1e3);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
subscribe(client, checksum) {
|
|
109
|
+
if (!client.subscriptions) {
|
|
110
|
+
client.subscriptions = {};
|
|
111
|
+
}
|
|
112
|
+
if (this.removeTimer) {
|
|
113
|
+
clearTimeout(this.removeTimer);
|
|
114
|
+
}
|
|
115
|
+
this.clients[client.id] = [client, checksum];
|
|
116
|
+
client.subscriptions[this.id] = this;
|
|
117
|
+
this.clientsCnt++;
|
|
118
|
+
if (this.errorState) {
|
|
119
|
+
const payload = [
|
|
120
|
+
client_1.RequestTypes.Subscription,
|
|
121
|
+
this.id,
|
|
122
|
+
{},
|
|
123
|
+
0,
|
|
124
|
+
this.errorState,
|
|
125
|
+
];
|
|
126
|
+
client.send(payload);
|
|
127
|
+
}
|
|
128
|
+
else if (this.observable.cache) {
|
|
129
|
+
if (checksum === this.observable.version) {
|
|
130
|
+
// console.info(
|
|
131
|
+
// 'got version dont re-send',
|
|
132
|
+
// x,
|
|
133
|
+
// checksum,
|
|
134
|
+
// this.observable.version
|
|
135
|
+
// )
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
this.clients[client.id][1] = this.observable.version;
|
|
139
|
+
// this send has to be checked dont want to resend if it immediatly updates from the sub
|
|
140
|
+
if (this.lastDiff && this.lastDiff[1] === checksum) {
|
|
141
|
+
const payload = [
|
|
142
|
+
client_1.RequestTypes.SubscriptionDiff,
|
|
143
|
+
this.id,
|
|
144
|
+
this.lastDiff[0],
|
|
145
|
+
[this.lastDiff[1], this.observable.version],
|
|
146
|
+
];
|
|
147
|
+
client.send(payload);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
const payload = [
|
|
151
|
+
client_1.RequestTypes.Subscription,
|
|
152
|
+
this.id,
|
|
153
|
+
this.observable.cache,
|
|
154
|
+
this.observable.version,
|
|
155
|
+
];
|
|
156
|
+
client.send(payload);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
// console.info(x, 'kein informazaion')
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
sendData(client) {
|
|
165
|
+
if (this.observable?.version && !this.errorState) {
|
|
166
|
+
const store = this.clients[client.id];
|
|
167
|
+
if (store) {
|
|
168
|
+
store[1] = this.observable.version;
|
|
169
|
+
const payload = [
|
|
170
|
+
client_1.RequestTypes.Subscription,
|
|
171
|
+
this.id,
|
|
172
|
+
this.observable.cache,
|
|
173
|
+
this.observable.version,
|
|
174
|
+
];
|
|
175
|
+
client.send(payload);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.Subscription = Subscription;
|
|
181
|
+
const subscribe = (server, client, [, id, query, checksum, allwaysSend]) => {
|
|
182
|
+
let subscription = server.subscriptions[id];
|
|
183
|
+
if (!subscription) {
|
|
184
|
+
subscription = new Subscription(server, id, query);
|
|
185
|
+
}
|
|
186
|
+
// pass
|
|
187
|
+
subscription.subscribe(client, checksum);
|
|
188
|
+
};
|
|
189
|
+
exports.subscribe = subscribe;
|
|
190
|
+
const sendSubscriptionData = (server, client, [, subscriptionId]) => {
|
|
191
|
+
const subscription = client.subscriptions?.[subscriptionId];
|
|
192
|
+
if (subscription) {
|
|
193
|
+
subscription.sendData(client);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
console.warn('Client does not have subscription for sendSubscriptionData');
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
exports.sendSubscriptionData = sendSubscriptionData;
|
|
200
|
+
const unsubscribe = (server, client, [, subscriptionId]) => {
|
|
201
|
+
if (client.subscriptions && client.subscriptions[subscriptionId]) {
|
|
202
|
+
client.subscriptions[subscriptionId].unsubscribe(client);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
exports.unsubscribe = unsubscribe;
|
|
206
|
+
//# sourceMappingURL=index.js.map
|