@based/server 4.0.0 → 4.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.
Files changed (261) hide show
  1. package/dist/BasedServerClient.d.ts +71 -0
  2. package/dist/BasedServerClient.js +390 -0
  3. package/dist/BasedServerClient.js.map +1 -0
  4. package/dist/Client.d.ts +75 -0
  5. package/dist/Client.js +277 -0
  6. package/dist/Client.js.map +1 -0
  7. package/dist/Params.d.ts +20 -0
  8. package/dist/Params.js +48 -0
  9. package/dist/Params.js.map +1 -0
  10. package/dist/auth/authorize.d.ts +2 -0
  11. package/dist/auth/authorize.js +8 -0
  12. package/dist/auth/authorize.js.map +1 -0
  13. package/dist/clientContext.d.ts +0 -0
  14. package/dist/clientContext.js +83 -0
  15. package/dist/clientContext.js.map +1 -0
  16. package/dist/closeListener.d.ts +4 -0
  17. package/dist/closeListener.js +13 -0
  18. package/dist/closeListener.js.map +1 -0
  19. package/dist/createSimpleServer.d.ts +22 -0
  20. package/dist/createSimpleServer.js +120 -0
  21. package/dist/createSimpleServer.js.map +1 -0
  22. package/dist/error.d.ts +66 -0
  23. package/dist/error.js +149 -0
  24. package/dist/error.js.map +1 -0
  25. package/dist/findPrefix.d.ts +3 -0
  26. package/dist/findPrefix.js +20 -0
  27. package/dist/findPrefix.js.map +1 -0
  28. package/dist/functions/types.d.ts +0 -2
  29. package/dist/functions/types.js.map +1 -1
  30. package/dist/functions/workerPool.d.ts +0 -0
  31. package/dist/functions/workerPool.js +5 -0
  32. package/dist/functions/workerPool.js.map +1 -0
  33. package/dist/getFromConfig.d.ts +3 -0
  34. package/dist/getFromConfig.js +50 -0
  35. package/dist/getFromConfig.js.map +1 -0
  36. package/dist/handlers/authorize.d.ts +5 -0
  37. package/dist/handlers/authorize.js +196 -0
  38. package/dist/handlers/authorize.js.map +1 -0
  39. package/dist/handlers/bulkUpdate/index.d.ts +5 -0
  40. package/dist/handlers/bulkUpdate/index.js +26 -0
  41. package/dist/handlers/bulkUpdate/index.js.map +1 -0
  42. package/dist/handlers/configuration/index.d.ts +10 -0
  43. package/dist/handlers/configuration/index.js +74 -0
  44. package/dist/handlers/configuration/index.js.map +1 -0
  45. package/dist/handlers/configuration/observable/index.d.ts +46 -0
  46. package/dist/handlers/configuration/observable/index.js +277 -0
  47. package/dist/handlers/configuration/observable/index.js.map +1 -0
  48. package/dist/handlers/configure/index.d.ts +5 -0
  49. package/dist/handlers/configure/index.js +44 -0
  50. package/dist/handlers/configure/index.js.map +1 -0
  51. package/dist/handlers/copy/index.d.ts +8 -0
  52. package/dist/handlers/copy/index.js +135 -0
  53. package/dist/handlers/copy/index.js.map +1 -0
  54. package/dist/handlers/delete/index.d.ts +5 -0
  55. package/dist/handlers/delete/index.js +34 -0
  56. package/dist/handlers/delete/index.js.map +1 -0
  57. package/dist/handlers/digest/index.d.ts +5 -0
  58. package/dist/handlers/digest/index.js +23 -0
  59. package/dist/handlers/digest/index.js.map +1 -0
  60. package/dist/handlers/functions/call.d.ts +6 -0
  61. package/dist/handlers/functions/call.js +54 -0
  62. package/dist/handlers/functions/call.js.map +1 -0
  63. package/dist/handlers/functions/observable/Observable.d.ts +49 -0
  64. package/dist/handlers/functions/observable/Observable.js +290 -0
  65. package/dist/handlers/functions/observable/Observable.js.map +1 -0
  66. package/dist/handlers/functions/observable/SharedObservable.d.ts +45 -0
  67. package/dist/handlers/functions/observable/SharedObservable.js +291 -0
  68. package/dist/handlers/functions/observable/SharedObservable.js.map +1 -0
  69. package/dist/handlers/functions/observable/index.d.ts +14 -0
  70. package/dist/handlers/functions/observable/index.js +130 -0
  71. package/dist/handlers/functions/observable/index.js.map +1 -0
  72. package/dist/handlers/get/index.d.ts +5 -0
  73. package/dist/handlers/get/index.js +23 -0
  74. package/dist/handlers/get/index.js.map +1 -0
  75. package/dist/handlers/getConfig/index.d.ts +5 -0
  76. package/dist/handlers/getConfig/index.js +38 -0
  77. package/dist/handlers/getConfig/index.js.map +1 -0
  78. package/dist/handlers/handleRequests.d.ts +5 -0
  79. package/dist/handlers/handleRequests.js +78 -0
  80. package/dist/handlers/handleRequests.js.map +1 -0
  81. package/dist/handlers/index.d.ts +4 -0
  82. package/dist/handlers/index.js +49 -0
  83. package/dist/handlers/index.js.map +1 -0
  84. package/dist/handlers/messageParser.d.ts +5 -0
  85. package/dist/handlers/messageParser.js +122 -0
  86. package/dist/handlers/messageParser.js.map +1 -0
  87. package/dist/handlers/removeField/index.d.ts +5 -0
  88. package/dist/handlers/removeField/index.js +42 -0
  89. package/dist/handlers/removeField/index.js.map +1 -0
  90. package/dist/handlers/removeType/index.d.ts +5 -0
  91. package/dist/handlers/removeType/index.js +30 -0
  92. package/dist/handlers/removeType/index.js.map +1 -0
  93. package/dist/handlers/rest/file/DataStream.d.ts +7 -0
  94. package/dist/handlers/rest/file/DataStream.js +17 -0
  95. package/dist/handlers/rest/file/DataStream.js.map +1 -0
  96. package/dist/handlers/rest/file/formStream.d.ts +4 -0
  97. package/dist/handlers/rest/file/formStream.js +188 -0
  98. package/dist/handlers/rest/file/formStream.js.map +1 -0
  99. package/dist/handlers/rest/file/getExtenstion.d.ts +2 -0
  100. package/dist/handlers/rest/file/getExtenstion.js +22 -0
  101. package/dist/handlers/rest/file/getExtenstion.js.map +1 -0
  102. package/dist/handlers/rest/file/index.d.ts +4 -0
  103. package/dist/handlers/rest/file/index.js +53 -0
  104. package/dist/handlers/rest/file/index.js.map +1 -0
  105. package/dist/handlers/rest/file/storeFile.d.ts +6 -0
  106. package/dist/handlers/rest/file/storeFile.js +44 -0
  107. package/dist/handlers/rest/file/storeFile.js.map +1 -0
  108. package/dist/handlers/rest/file/stream.d.ts +5 -0
  109. package/dist/handlers/rest/file/stream.js +104 -0
  110. package/dist/handlers/rest/file/stream.js.map +1 -0
  111. package/dist/handlers/rest/file/types.d.ts +9 -0
  112. package/dist/handlers/rest/file/types.js +3 -0
  113. package/dist/handlers/rest/file/types.js.map +1 -0
  114. package/dist/handlers/rest/getReqMessage.d.ts +3 -0
  115. package/dist/handlers/rest/getReqMessage.js +92 -0
  116. package/dist/handlers/rest/getReqMessage.js.map +1 -0
  117. package/dist/handlers/rest/index.d.ts +4 -0
  118. package/dist/handlers/rest/index.js +121 -0
  119. package/dist/handlers/rest/index.js.map +1 -0
  120. package/dist/handlers/rest/invalidReq.d.ts +4 -0
  121. package/dist/handlers/rest/invalidReq.js +20 -0
  122. package/dist/handlers/rest/invalidReq.js.map +1 -0
  123. package/dist/handlers/rest/parseResponse.d.ts +6 -0
  124. package/dist/handlers/rest/parseResponse.js +230 -0
  125. package/dist/handlers/rest/parseResponse.js.map +1 -0
  126. package/dist/handlers/rest/playground/index.d.ts +5 -0
  127. package/dist/handlers/rest/playground/index.js +308 -0
  128. package/dist/handlers/rest/playground/index.js.map +1 -0
  129. package/dist/handlers/rest/readBody.d.ts +3 -0
  130. package/dist/handlers/rest/readBody.js +45 -0
  131. package/dist/handlers/rest/readBody.js.map +1 -0
  132. package/dist/handlers/set/index.d.ts +5 -0
  133. package/dist/handlers/set/index.js +20 -0
  134. package/dist/handlers/set/index.js.map +1 -0
  135. package/dist/handlers/subscription/index.d.ts +32 -0
  136. package/dist/handlers/subscription/index.js +223 -0
  137. package/dist/handlers/subscription/index.js.map +1 -0
  138. package/dist/handlers/token.d.ts +5 -0
  139. package/dist/handlers/token.js +122 -0
  140. package/dist/handlers/token.js.map +1 -0
  141. package/dist/handlers/track/index.d.ts +7 -0
  142. package/dist/handlers/track/index.js +102 -0
  143. package/dist/handlers/track/index.js.map +1 -0
  144. package/dist/handlers/userAuth/index.d.ts +5 -0
  145. package/dist/handlers/userAuth/index.js +78 -0
  146. package/dist/handlers/userAuth/index.js.map +1 -0
  147. package/dist/index.d.ts +1 -0
  148. package/dist/index.js +3 -1
  149. package/dist/index.js.map +1 -1
  150. package/dist/network/http/authorize.d.ts +3 -0
  151. package/dist/network/http/authorize.js +28 -0
  152. package/dist/network/http/authorize.js.map +1 -0
  153. package/dist/network/http/compress.d.ts +6 -0
  154. package/dist/network/http/compress.js +56 -0
  155. package/dist/network/http/compress.js.map +1 -0
  156. package/dist/network/http/end.d.ts +4 -0
  157. package/dist/network/http/end.js +19 -0
  158. package/dist/network/http/end.js.map +1 -0
  159. package/dist/network/http/function.d.ts +3 -0
  160. package/dist/network/http/function.js +47 -0
  161. package/dist/network/http/function.js.map +1 -0
  162. package/dist/network/http/get.d.ts +3 -0
  163. package/dist/network/http/get.js +129 -0
  164. package/dist/network/http/get.js.map +1 -0
  165. package/dist/network/http/index.d.ts +3 -0
  166. package/dist/network/http/index.js +122 -0
  167. package/dist/network/http/index.js.map +1 -0
  168. package/dist/network/http/readBody.d.ts +3 -0
  169. package/dist/network/http/readBody.js +112 -0
  170. package/dist/network/http/readBody.js.map +1 -0
  171. package/dist/network/http/send.d.ts +5 -0
  172. package/dist/network/http/send.js +60 -0
  173. package/dist/network/http/send.js.map +1 -0
  174. package/dist/network/http/streamFunction/DataStream.d.ts +7 -0
  175. package/dist/network/http/streamFunction/DataStream.js +19 -0
  176. package/dist/network/http/streamFunction/DataStream.js.map +1 -0
  177. package/dist/network/http/streamFunction/getExtension.d.ts +2 -0
  178. package/dist/network/http/streamFunction/getExtension.js +29 -0
  179. package/dist/network/http/streamFunction/getExtension.js.map +1 -0
  180. package/dist/network/http/streamFunction/index.d.ts +3 -0
  181. package/dist/network/http/streamFunction/index.js +81 -0
  182. package/dist/network/http/streamFunction/index.js.map +1 -0
  183. package/dist/network/http/streamFunction/multipartStream.d.ts +12 -0
  184. package/dist/network/http/streamFunction/multipartStream.js +208 -0
  185. package/dist/network/http/streamFunction/multipartStream.js.map +1 -0
  186. package/dist/network/http/streamFunction/stream.d.ts +5 -0
  187. package/dist/network/http/streamFunction/stream.js +103 -0
  188. package/dist/network/http/streamFunction/stream.js.map +1 -0
  189. package/dist/network/index.d.ts +4 -0
  190. package/dist/network/index.js +84 -0
  191. package/dist/network/index.js.map +1 -0
  192. package/dist/network/message/auth.d.ts +4 -0
  193. package/dist/network/message/auth.js +32 -0
  194. package/dist/network/message/auth.js.map +1 -0
  195. package/dist/network/message/function.d.ts +3 -0
  196. package/dist/network/message/function.js +67 -0
  197. package/dist/network/message/function.js.map +1 -0
  198. package/dist/network/message/get.d.ts +3 -0
  199. package/dist/network/message/get.js +109 -0
  200. package/dist/network/message/get.js.map +1 -0
  201. package/dist/network/message/index.d.ts +3 -0
  202. package/dist/network/message/index.js +62 -0
  203. package/dist/network/message/index.js.map +1 -0
  204. package/dist/network/message/observable.d.ts +5 -0
  205. package/dist/network/message/observable.js +103 -0
  206. package/dist/network/message/observable.js.map +1 -0
  207. package/dist/network/message/send.d.ts +4 -0
  208. package/dist/network/message/send.js +11 -0
  209. package/dist/network/message/send.js.map +1 -0
  210. package/dist/network/upgrade.d.ts +4 -0
  211. package/dist/network/upgrade.js +49 -0
  212. package/dist/network/upgrade.js.map +1 -0
  213. package/dist/network/worker/get.d.ts +0 -0
  214. package/dist/network/worker/get.js +122 -0
  215. package/dist/network/worker/get.js.map +1 -0
  216. package/dist/network/worker/observable.d.ts +0 -0
  217. package/dist/network/worker/observable.js +120 -0
  218. package/dist/network/worker/observable.js.map +1 -0
  219. package/dist/openListener.d.ts +4 -0
  220. package/dist/openListener.js +13 -0
  221. package/dist/openListener.js.map +1 -0
  222. package/dist/secrets.d.ts +9 -0
  223. package/dist/secrets.js +150 -0
  224. package/dist/secrets.js.map +1 -0
  225. package/dist/security/index.d.ts +3 -0
  226. package/dist/security/index.js +66 -0
  227. package/dist/security/index.js.map +1 -0
  228. package/dist/types.d.ts +129 -0
  229. package/dist/types.js +17 -0
  230. package/dist/types.js.map +1 -0
  231. package/dist/upgradeListener.d.ts +4 -0
  232. package/dist/upgradeListener.js +47 -0
  233. package/dist/upgradeListener.js.map +1 -0
  234. package/dist/worker/authorize.d.ts +5 -0
  235. package/dist/worker/authorize.js +15 -0
  236. package/dist/worker/authorize.js.map +1 -0
  237. package/dist/worker/fnMap.d.ts +2 -0
  238. package/dist/worker/fnMap.js +5 -0
  239. package/dist/worker/fnMap.js.map +1 -0
  240. package/dist/worker/functions.d.ts +3 -0
  241. package/dist/worker/functions.js +8 -0
  242. package/dist/worker/functions.js.map +1 -0
  243. package/dist/worker/http/function.d.ts +4 -0
  244. package/dist/worker/http/function.js +75 -0
  245. package/dist/worker/http/function.js.map +1 -0
  246. package/dist/worker/index.d.ts +1 -0
  247. package/dist/worker/index.js +101 -0
  248. package/dist/worker/index.js.map +1 -0
  249. package/dist/worker/observable.d.ts +15 -0
  250. package/dist/worker/observable.js +110 -0
  251. package/dist/worker/observable.js.map +1 -0
  252. package/dist/worker/ws/function.d.ts +3 -0
  253. package/dist/worker/ws/function.js +42 -0
  254. package/dist/worker/ws/function.js.map +1 -0
  255. package/dist/workers/index.d.ts +0 -0
  256. package/dist/workers/index.js +20 -0
  257. package/dist/workers/index.js.map +1 -0
  258. package/dist/workers/workerCode.d.ts +12 -0
  259. package/dist/workers/workerCode.js +46 -0
  260. package/dist/workers/workerCode.js.map +1 -0
  261. package/package.json +1 -1
package/dist/types.js ADDED
@@ -0,0 +1,17 @@
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.isObservableFunction = exports.isCallFunction = exports.Client = void 0;
7
+ const Client_1 = __importDefault(require("./Client"));
8
+ exports.Client = Client_1.default;
9
+ const isCallFunction = (fn) => {
10
+ return fn.observable === false;
11
+ };
12
+ exports.isCallFunction = isCallFunction;
13
+ const isObservableFunction = (fn) => {
14
+ return fn.observable === true;
15
+ };
16
+ exports.isObservableFunction = isObservableFunction;
17
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA6B;AAapB,iBAbF,gBAAM,CAaE;AAmCR,MAAM,cAAc,GAAG,CAC5B,EAAqC,EACjB,EAAE;IACtB,OAAO,EAAE,CAAC,UAAU,KAAK,KAAK,CAAA;AAChC,CAAC,CAAA;AAJY,QAAA,cAAc,kBAI1B;AAEM,MAAM,oBAAoB,GAAG,CAClC,EAAqC,EACX,EAAE;IAC5B,OAAO,EAAE,CAAC,UAAU,KAAK,IAAI,CAAA;AAC/B,CAAC,CAAA;AAJY,QAAA,oBAAoB,wBAIhC"}
@@ -0,0 +1,4 @@
1
+ import uws from '@based/uws';
2
+ import { BasedServer } from '.';
3
+ declare const _default: (server: BasedServer, res: uws.HttpResponse, req: uws.HttpRequest, ctx: uws.us_socket_context_t) => void;
4
+ export default _default;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (server, res, req,
4
+ // eslint-disable-next-line
5
+ ctx) => {
6
+ let aborted;
7
+ const onAborted = () => {
8
+ aborted = true;
9
+ };
10
+ // res.writeStatus('101 Switching Protocols')
11
+ const onAuth = (authorized) => {
12
+ if (aborted) {
13
+ return;
14
+ }
15
+ if (authorized) {
16
+ const query = req.getQuery();
17
+ const url = req.getUrl();
18
+ const ua = req.getHeader('user-agent');
19
+ const ip = req.getHeader('x-forwarded-for') ||
20
+ Buffer.from(res.getRemoteAddressAsText()).toString();
21
+ const origin = req.getHeader('origin');
22
+ const secWebSocketKey = req.getHeader('sec-websocket-key');
23
+ const secWebSocketProtocol = req.getHeader('sec-websocket-protocol');
24
+ const secWebSocketExtensions = req.getHeader('sec-websocket-extensions');
25
+ res.writeStatus('101 Switching Protocols');
26
+ res.upgrade({
27
+ query,
28
+ origin,
29
+ url,
30
+ ua,
31
+ ip,
32
+ }, secWebSocketKey, secWebSocketProtocol, secWebSocketExtensions, ctx);
33
+ }
34
+ else {
35
+ res.writeStatus('401 Unauthorized');
36
+ res.end();
37
+ }
38
+ };
39
+ res.onAborted(onAborted);
40
+ if (server.config && server.config.authorizeConnection) {
41
+ server.config.authorizeConnection(req, ctx).then(onAuth);
42
+ }
43
+ else {
44
+ onAuth(true);
45
+ }
46
+ };
47
+ //# sourceMappingURL=upgradeListener.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upgradeListener.js","sourceRoot":"","sources":["../src/upgradeListener.ts"],"names":[],"mappings":";;AAGA,kBAAe,CACb,MAAmB,EACnB,GAAqB,EACrB,GAAoB;AACpB,2BAA2B;AAC3B,GAA4B,EAC5B,EAAE;IACF,IAAI,OAAO,CAAA;IACX,MAAM,SAAS,GAAG,GAAG,EAAE;QACrB,OAAO,GAAG,IAAI,CAAA;IAChB,CAAC,CAAA;IACD,6CAA6C;IAE7C,MAAM,MAAM,GAAG,CAAC,UAAU,EAAE,EAAE;QAC5B,IAAI,OAAO,EAAE;YACX,OAAM;SACP;QACD,IAAI,UAAU,EAAE;YACd,MAAM,KAAK,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAA;YAC5B,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,EAAE,CAAA;YACxB,MAAM,EAAE,GAAG,GAAG,CAAC,SAAS,CAAC,YAAY,CAAC,CAAA;YACtC,MAAM,EAAE,GACN,GAAG,CAAC,SAAS,CAAC,iBAAiB,CAAC;gBAChC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,sBAAsB,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAA;YACtD,MAAM,MAAM,GAAG,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;YACtC,MAAM,eAAe,GAAG,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAA;YAC1D,MAAM,oBAAoB,GAAG,GAAG,CAAC,SAAS,CAAC,wBAAwB,CAAC,CAAA;YACpE,MAAM,sBAAsB,GAAG,GAAG,CAAC,SAAS,CAAC,0BAA0B,CAAC,CAAA;YAExE,GAAG,CAAC,WAAW,CAAC,yBAAyB,CAAC,CAAA;YAE1C,GAAG,CAAC,OAAO,CACT;gBACE,KAAK;gBACL,MAAM;gBACN,GAAG;gBACH,EAAE;gBACF,EAAE;aACH,EACD,eAAe,EACf,oBAAoB,EACpB,sBAAsB,EACtB,GAAG,CACJ,CAAA;SACF;aAAM;YACL,GAAG,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAA;YACnC,GAAG,CAAC,GAAG,EAAE,CAAA;SACV;IACH,CAAC,CAAA;IAED,GAAG,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;IAExB,IAAI,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,mBAAmB,EAAE;QACtD,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;KACzD;SAAM;QACL,MAAM,CAAC,IAAI,CAAC,CAAA;KACb;AACH,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ import { Authorize } from '../types';
2
+ export declare const state: {
3
+ authorize?: Authorize;
4
+ };
5
+ export declare const authorize: Authorize;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.authorize = exports.state = void 0;
4
+ const worker_threads_1 = require("worker_threads");
5
+ exports.state = {};
6
+ const authorize = async (client, name, payload) => {
7
+ if (!exports.state.authorize) {
8
+ console.warn('No authorize installed in worker...', worker_threads_1.threadId);
9
+ return true;
10
+ }
11
+ // needs callstack...
12
+ return exports.state.authorize(client, name, payload);
13
+ };
14
+ exports.authorize = authorize;
15
+ //# sourceMappingURL=authorize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authorize.js","sourceRoot":"","sources":["../../src/worker/authorize.ts"],"names":[],"mappings":";;;AAAA,mDAAyC;AAG5B,QAAA,KAAK,GAEd,EAAE,CAAA;AAEC,MAAM,SAAS,GAAc,KAAK,EACvC,MAAqB,EACrB,IAAY,EACZ,OAAa,EACb,EAAE;IACF,IAAI,CAAC,aAAK,CAAC,SAAS,EAAE;QACpB,OAAO,CAAC,IAAI,CAAC,qCAAqC,EAAE,yBAAQ,CAAC,CAAA;QAC7D,OAAO,IAAI,CAAA;KACZ;IACD,qBAAqB;IACrB,OAAO,aAAK,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AAC/C,CAAC,CAAA;AAXY,QAAA,SAAS,aAWrB"}
@@ -0,0 +1,2 @@
1
+ declare const fnMap: Map<string, string>;
2
+ export default fnMap;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fnMap = new Map();
4
+ exports.default = fnMap;
5
+ //# sourceMappingURL=fnMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fnMap.js","sourceRoot":"","sources":["../../src/worker/fnMap.ts"],"names":[],"mappings":";;AAAA,MAAM,KAAK,GAAwB,IAAI,GAAG,EAAE,CAAA;AAE5C,kBAAe,KAAK,CAAA"}
@@ -0,0 +1,3 @@
1
+ declare const fnPathMap: Map<string, string>;
2
+ declare const fnInstallListeners: Map<string, ((fn: Function, err?: Error) => void)[]>;
3
+ export { fnPathMap, fnInstallListeners };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fnInstallListeners = exports.fnPathMap = void 0;
4
+ const fnPathMap = new Map();
5
+ exports.fnPathMap = fnPathMap;
6
+ const fnInstallListeners = new Map();
7
+ exports.fnInstallListeners = fnInstallListeners;
8
+ //# sourceMappingURL=functions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/worker/functions.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAwB,IAAI,GAAG,EAAE,CAAA;AAKvC,8BAAS;AAHlB,MAAM,kBAAkB,GACtB,IAAI,GAAG,EAAE,CAAA;AAES,gDAAkB"}
@@ -0,0 +1,4 @@
1
+ import { ClientContext } from '../../types';
2
+ export declare const parsePayload: (id: number, context: ClientContext, data: Uint8Array) => any;
3
+ declare const _default: (name: string, type: number, path: string, id: number, context: ClientContext, payload?: Uint8Array) => void;
4
+ export default _default;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parsePayload = void 0;
4
+ const worker_threads_1 = require("worker_threads");
5
+ const utils_1 = require("@saulx/utils");
6
+ const authorize_1 = require("../authorize");
7
+ const decoder = new TextDecoder('utf-8');
8
+ const parsePayload = (id, context, data) => {
9
+ const contentType = context.headers['content-type'];
10
+ if (contentType === 'application/json' || !contentType) {
11
+ const str = decoder.decode(data);
12
+ let parsedData;
13
+ try {
14
+ parsedData = data.byteLength ? JSON.parse(str) : undefined;
15
+ return parsedData;
16
+ }
17
+ catch (err) {
18
+ worker_threads_1.parentPort.postMessage({
19
+ id,
20
+ err,
21
+ });
22
+ // sendHttpError(server, client, BasedErrorCode.InvalidPayload, route)
23
+ }
24
+ }
25
+ else if (contentType.startsWith('text') ||
26
+ contentType === 'application/xml') {
27
+ return decoder.decode(data);
28
+ }
29
+ else {
30
+ return data;
31
+ }
32
+ };
33
+ exports.parsePayload = parsePayload;
34
+ exports.default = (name, type, path, id, context, payload) => {
35
+ const fn = require(path);
36
+ let parsedPayload;
37
+ if (payload) {
38
+ parsedPayload = (0, exports.parsePayload)(id, context, payload);
39
+ }
40
+ else if (type === 4) {
41
+ parsedPayload = (0, utils_1.parseQuery)(context.query);
42
+ }
43
+ (0, authorize_1.authorize)(context, name, payload)
44
+ .then((ok) => {
45
+ if (!ok) {
46
+ console.error('autn wrong');
47
+ // err will become based error
48
+ worker_threads_1.parentPort.postMessage({
49
+ id,
50
+ err: new Error('AITH WRONG'),
51
+ });
52
+ return;
53
+ }
54
+ fn(parsedPayload, {})
55
+ .then((v) => {
56
+ worker_threads_1.parentPort.postMessage({
57
+ id,
58
+ payload: v,
59
+ });
60
+ })
61
+ .catch((err) => {
62
+ worker_threads_1.parentPort.postMessage({
63
+ id,
64
+ err,
65
+ });
66
+ });
67
+ })
68
+ .catch((err) => {
69
+ worker_threads_1.parentPort.postMessage({
70
+ id,
71
+ err,
72
+ });
73
+ });
74
+ };
75
+ //# sourceMappingURL=function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function.js","sourceRoot":"","sources":["../../../src/worker/http/function.ts"],"names":[],"mappings":";;;AACA,mDAA2C;AAC3C,wCAAyC;AACzC,4CAAwC;AAExC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;AAEjC,MAAM,YAAY,GAAG,CAC1B,EAAU,EACV,OAAsB,EACtB,IAAgB,EACX,EAAE;IACP,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;IACnD,IAAI,WAAW,KAAK,kBAAkB,IAAI,CAAC,WAAW,EAAE;QACtD,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,UAAe,CAAA;QACnB,IAAI;YACF,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;YAC1D,OAAO,UAAU,CAAA;SAClB;QAAC,OAAO,GAAG,EAAE;YACZ,2BAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,GAAG;aACJ,CAAC,CAAA;YACF,sEAAsE;SACvE;KACF;SAAM,IACL,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC;QAC9B,WAAW,KAAK,iBAAiB,EACjC;QACA,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;KAC5B;SAAM;QACL,OAAO,IAAI,CAAA;KACZ;AACH,CAAC,CAAA;AA3BY,QAAA,YAAY,gBA2BxB;AAED,kBAAe,CACb,IAAY,EACZ,IAAY,EACZ,IAAY,EACZ,EAAU,EACV,OAAsB,EACtB,OAAoB,EACpB,EAAE;IACF,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACxB,IAAI,aAAkB,CAAA;IACtB,IAAI,OAAO,EAAE;QACX,aAAa,GAAG,IAAA,oBAAY,EAAC,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;KACnD;SAAM,IAAI,IAAI,KAAK,CAAC,EAAE;QACrB,aAAa,GAAG,IAAA,kBAAU,EAAC,OAAO,CAAC,KAAK,CAAC,CAAA;KAC1C;IACD,IAAA,qBAAS,EAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;SAC9B,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE;QACX,IAAI,CAAC,EAAE,EAAE;YACP,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAA;YAC3B,8BAA8B;YAC9B,2BAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,GAAG,EAAE,IAAI,KAAK,CAAC,YAAY,CAAC;aAC7B,CAAC,CAAA;YACF,OAAM;SACP;QACD,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC;aAClB,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;YACV,2BAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,OAAO,EAAE,CAAC;aACX,CAAC,CAAA;QACJ,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,2BAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,GAAG;aACJ,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACN,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACb,2BAAU,CAAC,WAAW,CAAC;YACrB,EAAE;YACF,GAAG;SACJ,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACN,CAAC,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,101 @@
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 node_worker_threads_1 = require("node:worker_threads");
7
+ const observable_1 = require("./observable");
8
+ const function_1 = __importDefault(require("./ws/function"));
9
+ const function_2 = __importDefault(require("./http/function"));
10
+ const functions_1 = require("./functions");
11
+ const authorize_1 = require("./authorize");
12
+ console.info('Start worker', node_worker_threads_1.threadId);
13
+ // outgoing type: 0 => install function
14
+ // outgoing type: 1 => GET
15
+ // outgoing type: 2 => OBSERVE
16
+ node_worker_threads_1.parentPort.on('message', (d) => {
17
+ // d.type === 3 // HTTP POST FN
18
+ // d.type === 4 // HTTP GET FN
19
+ // d.type === 5 // FN INSTALLED (can be observable as well)
20
+ // d.type === 6 // UNINSTALL FN
21
+ // d.type === 7 // CANNOT INSTALL FN
22
+ if (d.type === 5) {
23
+ // maybe if you install like this it gets marked for a bit longer
24
+ // nested functions have to be kept in mem a bit longer...
25
+ const x = functions_1.fnInstallListeners.get(d.name);
26
+ const prevPath = functions_1.fnPathMap.get(d.name);
27
+ if (prevPath) {
28
+ delete require.cache[require.resolve(prevPath)];
29
+ }
30
+ functions_1.fnPathMap.set(d.name, d.path);
31
+ if (x) {
32
+ const installedFn = require(d.path);
33
+ x.forEach((r) => {
34
+ r(installedFn);
35
+ });
36
+ }
37
+ if (d.name === 'authorize') {
38
+ authorize_1.state.authorize = require(d.path);
39
+ }
40
+ }
41
+ else if (d.type === 7) {
42
+ const x = functions_1.fnInstallListeners.get(d.name);
43
+ const prevPath = functions_1.fnPathMap.get(d.name);
44
+ if (prevPath) {
45
+ delete require.cache[require.resolve(prevPath)];
46
+ functions_1.fnPathMap.delete(d.path);
47
+ }
48
+ if (x) {
49
+ const err = new Error(`Cannot install function ${d.name}`);
50
+ x.forEach((r) => {
51
+ r(undefined, err);
52
+ });
53
+ }
54
+ }
55
+ else if (d.type === 6) {
56
+ const path = functions_1.fnPathMap.get(d.name);
57
+ if (!path) {
58
+ // console.info('Cannot find path to uninstall', d.name)
59
+ return;
60
+ }
61
+ console.info('Uninstall', d.name);
62
+ functions_1.fnPathMap.delete(path);
63
+ delete require.cache[require.resolve(path)];
64
+ }
65
+ else if (d.type === 3 || d.type === 4) {
66
+ const prevPath = functions_1.fnPathMap.get(d.name);
67
+ if (!prevPath) {
68
+ functions_1.fnPathMap.set(d.name, d.path);
69
+ }
70
+ else if (prevPath !== d.path) {
71
+ delete require.cache[require.resolve(prevPath)];
72
+ }
73
+ console.info('Http function...');
74
+ (0, function_2.default)(d.name, d.type, d.path, d.id, d.context, d.payload);
75
+ }
76
+ else if (d.type === 0) {
77
+ const prevPath = functions_1.fnPathMap.get(d.name);
78
+ if (!prevPath) {
79
+ functions_1.fnPathMap.set(d.name, d.path);
80
+ }
81
+ else if (prevPath !== d.path) {
82
+ delete require.cache[require.resolve(prevPath)];
83
+ }
84
+ (0, function_1.default)(d.name, d.path, d.id, d.context.reqId, d.context, d.context.isDeflate, d.payload);
85
+ }
86
+ else if (d.type === 1) {
87
+ const prevPath = functions_1.fnPathMap.get(d.name);
88
+ if (!prevPath) {
89
+ functions_1.fnPathMap.set(d.name, d.path);
90
+ }
91
+ else if (prevPath !== d.path) {
92
+ delete require.cache[require.resolve(prevPath)];
93
+ }
94
+ // payload is parsed for this
95
+ (0, observable_1.createObs)(d.id, d.path, d.payload);
96
+ }
97
+ else if (d.type === 2) {
98
+ (0, observable_1.closeObs)(d.id);
99
+ }
100
+ });
101
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/worker/index.ts"],"names":[],"mappings":";;;;;AAAA,6DAA0D;AAC1D,6CAAkD;AAClD,6DAAsC;AACtC,+DAA0C;AAC1C,2CAA2D;AAC3D,2CAAmC;AAEnC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,8BAAQ,CAAC,CAAA;AAEtC,uCAAuC;AACvC,0BAA0B;AAC1B,8BAA8B;AAE9B,gCAAU,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE;IAC7B,+BAA+B;IAC/B,8BAA8B;IAC9B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;QACvB,IAAA,qBAAQ,EAAC,CAAC,CAAC,EAAE,CAAC,CAAA;KACf;AACH,CAAC,CAAC,CAAA"}
@@ -0,0 +1,15 @@
1
+ export declare type WorkerObs = {
2
+ id: number;
3
+ isDestroyed: boolean;
4
+ rawData?: any;
5
+ rawDataSize?: number;
6
+ diffCache?: Uint8Array;
7
+ previousChecksum?: number;
8
+ cache?: Uint8Array;
9
+ isDeflate?: boolean;
10
+ checksum?: number;
11
+ closeFunction?: () => void;
12
+ };
13
+ export declare const activeObs: Map<number, WorkerObs>;
14
+ export declare const createObs: (id: number, functionPath: string, payload?: any) => void;
15
+ export declare const closeObs: (id: number) => void;
@@ -0,0 +1,110 @@
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.closeObs = exports.createObs = exports.activeObs = void 0;
7
+ const protocol_1 = require("../protocol");
8
+ const hash_1 = require("@saulx/hash");
9
+ const utils_1 = require("@saulx/utils");
10
+ const diff_1 = __importDefault(require("@saulx/diff"));
11
+ const node_worker_threads_1 = require("node:worker_threads");
12
+ exports.activeObs = new Map();
13
+ const createObs = (id, functionPath, payload) => {
14
+ if (exports.activeObs.has(id)) {
15
+ console.warn('trying to creater an obs that allready exists...');
16
+ return;
17
+ }
18
+ const obs = {
19
+ id,
20
+ isDestroyed: false,
21
+ };
22
+ exports.activeObs.set(id, obs);
23
+ const fn = require(functionPath);
24
+ const update = (data, checksum, diff, previousChecksum) => {
25
+ if (checksum === undefined) {
26
+ if (data === undefined) {
27
+ checksum = 0;
28
+ }
29
+ else {
30
+ // do something
31
+ if (typeof data === 'object' && data !== null) {
32
+ checksum = (0, hash_1.hashObjectIgnoreKeyOrder)(data);
33
+ }
34
+ else {
35
+ checksum = (0, hash_1.hash)(data);
36
+ }
37
+ }
38
+ }
39
+ if (checksum !== obs.checksum) {
40
+ const buff = (0, protocol_1.valueToBuffer)(data);
41
+ if (previousChecksum === undefined) {
42
+ if (typeof data === 'object' && data !== null) {
43
+ if (obs.rawData) {
44
+ diff = (0, diff_1.default)(obs.rawData, data);
45
+ obs.previousChecksum = obs.checksum;
46
+ }
47
+ obs.rawData = (0, utils_1.deepCopy)(data);
48
+ }
49
+ else if (obs.rawData) {
50
+ delete obs.rawData;
51
+ delete obs.rawDataSize;
52
+ }
53
+ }
54
+ // TODO: this will all become SHAREDBUFFERS
55
+ // keep track globally of total mem usage
56
+ const [encodedData, isDeflate] = (0, protocol_1.encodeObservableResponse)(id, checksum, buff);
57
+ // add deflate info
58
+ obs.isDeflate = isDeflate;
59
+ obs.cache = encodedData;
60
+ obs.checksum = checksum;
61
+ if (diff) {
62
+ const diffBuff = (0, protocol_1.valueToBuffer)(diff);
63
+ const encodedDiffData = (0, protocol_1.encodeObservableDiffResponse)(id, checksum, obs.previousChecksum, diffBuff);
64
+ obs.diffCache = encodedDiffData;
65
+ // add to cache size
66
+ }
67
+ node_worker_threads_1.parentPort.postMessage({
68
+ id,
69
+ payload: {
70
+ diff: obs.diffCache,
71
+ data: encodedData,
72
+ checksum: checksum,
73
+ isDeflate: isDeflate,
74
+ },
75
+ });
76
+ }
77
+ };
78
+ fn(payload, update)
79
+ .then((close) => {
80
+ if (obs.isDestroyed) {
81
+ close();
82
+ }
83
+ else {
84
+ obs.closeFunction = close;
85
+ }
86
+ })
87
+ .catch((err) => {
88
+ node_worker_threads_1.parentPort.postMessage({
89
+ id,
90
+ err,
91
+ });
92
+ // TODO: maybe clear instantly?
93
+ });
94
+ };
95
+ exports.createObs = createObs;
96
+ const closeObs = (id) => {
97
+ const obs = exports.activeObs.get(id);
98
+ if (!obs) {
99
+ console.warn('trying to close an obs that does not exist');
100
+ return;
101
+ }
102
+ obs.isDestroyed = true;
103
+ if (obs.closeFunction) {
104
+ obs.closeFunction();
105
+ }
106
+ exports.activeObs.delete(id);
107
+ };
108
+ exports.closeObs = closeObs;
109
+ // make a map
110
+ //# sourceMappingURL=observable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"observable.js","sourceRoot":"","sources":["../../src/worker/observable.ts"],"names":[],"mappings":";;;;;;AAAA,0CAIoB;AAEpB,sCAA4D;AAC5D,wCAAuC;AACvC,uDAAqC;AACrC,6DAAgD;AAenC,QAAA,SAAS,GAA2B,IAAI,GAAG,EAAE,CAAA;AAEnD,MAAM,SAAS,GAAG,CAAC,EAAU,EAAE,YAAoB,EAAE,OAAa,EAAE,EAAE;IAC3E,IAAI,iBAAS,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;QACrB,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAA;QAChE,OAAM;KACP;IAED,MAAM,GAAG,GAAc;QACrB,EAAE;QACF,WAAW,EAAE,KAAK;KACnB,CAAA;IAED,iBAAS,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;IAEtB,MAAM,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IAEhC,MAAM,MAAM,GAA6B,CACvC,IAAS,EACT,QAAiB,EACjB,IAAU,EACV,gBAAyB,EACzB,EAAE;QACF,IAAI,QAAQ,KAAK,SAAS,EAAE;YAC1B,IAAI,IAAI,KAAK,SAAS,EAAE;gBACtB,QAAQ,GAAG,CAAC,CAAA;aACb;iBAAM;gBACL,eAAe;gBACf,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;oBAC7C,QAAQ,GAAG,IAAA,+BAAwB,EAAC,IAAI,CAAC,CAAA;iBAC1C;qBAAM;oBACL,QAAQ,GAAG,IAAA,WAAI,EAAC,IAAI,CAAC,CAAA;iBACtB;aACF;SACF;QAED,IAAI,QAAQ,KAAK,GAAG,CAAC,QAAQ,EAAE;YAC7B,MAAM,IAAI,GAAG,IAAA,wBAAa,EAAC,IAAI,CAAC,CAAA;YAEhC,IAAI,gBAAgB,KAAK,SAAS,EAAE;gBAClC,IAAI,OAAO,IAA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AAC;SAChB,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE;QACd,IAAI,GAAG,CAAC,WAAW,EAAE;YACnB,KAAK,EAAE,CAAA;SACR;aAAM;YACL,GAAG,CAAC,aAAa,GAAG,KAAK,CAAA;SAC1B;IACH,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACb,gCAAU,CAAC,WAAW,CAAC;YACrB,EAAE;YACF,GAAG;SACJ,CAAC,CAAA;QACF,+BAA+B;IACjC,CAAC,CAAC,CAAA;AACN,CAAC,CAAA;AAtGY,QAAA,SAAS,aAsGrB;AAEM,MAAM,QAAQ,GAAG,CAAC,EAAU,EAAE,EAAE;IACrC,MAAM,GAAG,GAAG,iBAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAE7B,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAA;QAC1D,OAAM;KACP;IAED,GAAG,CAAC,WAAW,GAAG,IAAI,CAAA;IACtB,IAAI,GAAG,CAAC,aAAa,EAAE;QACrB,GAAG,CAAC,aAAa,EAAE,CAAA;KACpB;IACD,iBAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AACtB,CAAC,CAAA;AAbY,QAAA,QAAQ,YAapB;AAED,aAAa"}
@@ -0,0 +1,3 @@
1
+ import { ClientContext } from '../../types';
2
+ declare const _default: (name: string, path: string, id: number, reqId: number, context: ClientContext, isDeflate: boolean, payload?: Uint8Array) => void;
3
+ export default _default;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const protocol_1 = require("../../protocol");
4
+ const node_worker_threads_1 = require("node:worker_threads");
5
+ const authorize_1 = require("../authorize");
6
+ exports.default = (name, path, id, reqId, context, isDeflate, payload) => {
7
+ const fn = require(path);
8
+ let parsedPayload;
9
+ if (payload) {
10
+ parsedPayload = (0, protocol_1.decodePayload)(payload, isDeflate);
11
+ }
12
+ (0, authorize_1.authorize)(context, name, payload)
13
+ .then((ok) => {
14
+ if (!ok) {
15
+ node_worker_threads_1.parentPort.postMessage({
16
+ id,
17
+ err: new Error('AITH WRONG'),
18
+ });
19
+ return false;
20
+ }
21
+ fn(parsedPayload, {})
22
+ .then((v) => {
23
+ node_worker_threads_1.parentPort.postMessage({
24
+ id,
25
+ payload: (0, protocol_1.encodeFunctionResponse)(reqId, (0, protocol_1.valueToBuffer)(v)),
26
+ });
27
+ })
28
+ .catch((err) => {
29
+ node_worker_threads_1.parentPort.postMessage({
30
+ id,
31
+ err,
32
+ });
33
+ });
34
+ })
35
+ .catch((err) => {
36
+ node_worker_threads_1.parentPort.postMessage({
37
+ id,
38
+ err,
39
+ });
40
+ });
41
+ };
42
+ //# sourceMappingURL=function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function.js","sourceRoot":"","sources":["../../../src/worker/ws/function.ts"],"names":[],"mappings":";;AAAA,6CAIuB;AACvB,6DAAgD;AAEhD,4CAAwC;AAExC,kBAAe,CACb,IAAY,EACZ,IAAY,EACZ,EAAU,EACV,KAAa,EACb,OAAsB,EACtB,SAAkB,EAClB,OAAoB,EACpB,EAAE;IACF,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAExB,IAAI,aAAkB,CAAA;IAEtB,IAAI,OAAO,EAAE;QACX,aAAa,GAAG,IAAA,wBAAa,EAAC,OAAO,EAAE,SAAS,CAAC,CAAA;KAClD;IAED,IAAA,qBAAS,EAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;SAC9B,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE;QACX,IAAI,CAAC,EAAE,EAAE;YACP,gCAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,GAAG,EAAE,IAAI,KAAK,CAAC,YAAY,CAAC;aAC7B,CAAC,CAAA;YACF,OAAO,KAAK,CAAA;SACb;QAED,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC;aAClB,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;YACV,gCAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,OAAO,EAAE,IAAA,iCAAsB,EAAC,KAAK,EAAE,IAAA,wBAAa,EAAC,CAAC,CAAC,CAAC;aACzD,CAAC,CAAA;QACJ,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,gCAAU,CAAC,WAAW,CAAC;gBACrB,EAAE;gBACF,GAAG;aACJ,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACN,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACb,gCAAU,CAAC,WAAW,CAAC;YACrB,EAAE;YACF,GAAG;SACJ,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACN,CAAC,CAAA"}
File without changes
@@ -0,0 +1,20 @@
1
+ // make a based server thing useFunctionWorker
2
+ // workerConfig = {
3
+ // concurrency: 5
4
+ // maxCrashRate: [5, 1000] // 20 per second
5
+ // logs in a special way
6
+ // }
7
+ // import workerify from './workerify'
8
+ /*
9
+ import { CallFunction, ObservableFunction } from '@based/server/src/types'
10
+
11
+ export default (
12
+ fn: ObservableFunction | CallFunction
13
+ ): ObservableFunction | CallFunction => {
14
+ console.info('GO GO GO', fn)
15
+
16
+ return fn
17
+ }
18
+
19
+ */
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/workers/index.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,mBAAmB;AACnB,kBAAkB;AAClB,4CAA4C;AAC5C,wBAAwB;AACxB,IAAI;AACJ,sCAAsC;AAEtC;;;;;;;;;;;EAWE"}
@@ -0,0 +1,12 @@
1
+ declare class WorkerClient {
2
+ constructor(id: number);
3
+ }
4
+ declare class WorkerBasedClient {
5
+ }
6
+ declare class WorkerParams {
7
+ private _clientId;
8
+ payload: any;
9
+ callStack: any[];
10
+ get user(): WorkerClient;
11
+ get based(): WorkerBasedClient;
12
+ }
@@ -0,0 +1,46 @@
1
+ // import { Client } from '@based/server/src/types'
2
+ // import { BasedServerClient } from '@based/server'
3
+ // export type CallParams = {
4
+ // payload: any
5
+ // based: BasedServerClient
6
+ // user: Client
7
+ // callStack: string[]
8
+ // }
9
+ // export type ObservableParams = {
10
+ // payload: any
11
+ // based: BasedServerClient
12
+ // user?: Client
13
+ // callStack: string[]
14
+ // // error later...
15
+ // update: (value: any, checksum?: number) => void
16
+ // }
17
+ // workers
18
+ // in the worker we are going to run the code with getters
19
+ // e.g.
20
+ // fn({ user, payload, update, based }) // we add error later
21
+ // fn({ user, payload, based })
22
+ // WorkerParams object - mimic normal params object
23
+ // this all has to be part of based server
24
+ // mimic normal client
25
+ class WorkerClient {
26
+ constructor(id) {
27
+ // yes number
28
+ console.info(id);
29
+ }
30
+ }
31
+ // has to wrap every method
32
+ class WorkerBasedClient {
33
+ }
34
+ class WorkerParams {
35
+ constructor() {
36
+ this.callStack = [];
37
+ }
38
+ get user() {
39
+ // also pair it with other functions potentialy
40
+ return new WorkerClient(this._clientId);
41
+ }
42
+ get based() {
43
+ return new WorkerBasedClient();
44
+ }
45
+ }
46
+ //# sourceMappingURL=workerCode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerCode.js","sourceRoot":"","sources":["../../src/workers/workerCode.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,oDAAoD;AAEpD,6BAA6B;AAC7B,iBAAiB;AACjB,6BAA6B;AAC7B,iBAAiB;AACjB,wBAAwB;AACxB,IAAI;AAEJ,mCAAmC;AACnC,iBAAiB;AACjB,6BAA6B;AAC7B,kBAAkB;AAClB,wBAAwB;AACxB,sBAAsB;AACtB,oDAAoD;AACpD,IAAI;AAEJ,UAAU;AAEV,0DAA0D;AAE1D,OAAO;AAEP,6DAA6D;AAE7D,+BAA+B;AAE/B,mDAAmD;AAEnD,0CAA0C;AAE1C,sBAAsB;AACtB,MAAM,YAAY;IAChB,YAAY,EAAU;QACpB,aAAa;QACb,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAClB,CAAC;CAEF;AAED,2BAA2B;AAC3B,MAAM,iBAAiB;CAEtB;AAED,MAAM,YAAY;IAAlB;QAKS,cAAS,GAAG,EAAE,CAAA;IAUvB,CAAC;IARC,IAAI,IAAI;QACN,+CAA+C;QAC/C,OAAO,IAAI,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IACzC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,iBAAiB,EAAE,CAAA;IAChC,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/server",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index.js",
6
6
  "files": [