@based/server 3.4.6 → 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 (269) hide show
  1. package/dist/api/index.d.ts +7 -0
  2. package/dist/api/index.js +173 -0
  3. package/dist/api/index.js.map +1 -0
  4. package/dist/auth/authorize.d.ts +2 -0
  5. package/dist/auth/authorize.js +8 -0
  6. package/dist/auth/authorize.js.map +1 -0
  7. package/dist/auth/dummyAuth.d.ts +2 -0
  8. package/dist/auth/dummyAuth.js +8 -0
  9. package/dist/auth/dummyAuth.js.map +1 -0
  10. package/dist/auth/index.d.ts +15 -0
  11. package/dist/auth/index.js +49 -0
  12. package/dist/auth/index.js.map +1 -0
  13. package/dist/auth/parseAuthState.d.ts +2 -0
  14. package/dist/auth/parseAuthState.js +16 -0
  15. package/dist/auth/parseAuthState.js.map +1 -0
  16. package/dist/auth/types.d.ts +10 -0
  17. package/dist/auth/types.js +3 -0
  18. package/dist/auth/types.js.map +1 -0
  19. package/dist/clientContext.d.ts +0 -0
  20. package/dist/clientContext.js +83 -0
  21. package/dist/clientContext.js.map +1 -0
  22. package/dist/compress.d.ts +5 -0
  23. package/dist/compress.js +52 -0
  24. package/dist/compress.js.map +1 -0
  25. package/dist/context.d.ts +54 -0
  26. package/dist/context.js +18 -0
  27. package/dist/context.js.map +1 -0
  28. package/dist/createSimpleServer.d.ts +22 -0
  29. package/dist/createSimpleServer.js +120 -0
  30. package/dist/createSimpleServer.js.map +1 -0
  31. package/dist/error/errorTypeHandlers.d.ts +6 -0
  32. package/dist/error/errorTypeHandlers.js +130 -0
  33. package/dist/error/errorTypeHandlers.js.map +1 -0
  34. package/dist/error/index.d.ts +5 -0
  35. package/dist/error/index.js +61 -0
  36. package/dist/error/index.js.map +1 -0
  37. package/dist/error/types.d.ts +105 -0
  38. package/dist/error/types.js +30 -0
  39. package/dist/error/types.js.map +1 -0
  40. package/dist/error.d.ts +66 -0
  41. package/dist/error.js +149 -0
  42. package/dist/error.js.map +1 -0
  43. package/dist/functions/index.d.ts +34 -0
  44. package/dist/functions/index.js +210 -0
  45. package/dist/functions/index.js.map +1 -0
  46. package/dist/functions/timeout.d.ts +3 -0
  47. package/dist/functions/timeout.js +23 -0
  48. package/dist/functions/timeout.js.map +1 -0
  49. package/dist/functions/types.d.ts +67 -0
  50. package/dist/functions/types.js +15 -0
  51. package/dist/functions/types.js.map +1 -0
  52. package/dist/functions/workerPool.d.ts +0 -0
  53. package/dist/functions/workerPool.js +5 -0
  54. package/dist/functions/workerPool.js.map +1 -0
  55. package/dist/handlers/configuration/index.d.ts +1 -1
  56. package/dist/incoming/http/authorize.d.ts +4 -0
  57. package/dist/incoming/http/authorize.js +37 -0
  58. package/dist/incoming/http/authorize.js.map +1 -0
  59. package/dist/incoming/http/function.d.ts +4 -0
  60. package/dist/incoming/http/function.js +47 -0
  61. package/dist/incoming/http/function.js.map +1 -0
  62. package/dist/incoming/http/get.d.ts +4 -0
  63. package/dist/incoming/http/get.js +155 -0
  64. package/dist/incoming/http/get.js.map +1 -0
  65. package/dist/incoming/http/index.d.ts +3 -0
  66. package/dist/incoming/http/index.js +134 -0
  67. package/dist/incoming/http/index.js.map +1 -0
  68. package/dist/incoming/http/parseQuery.d.ts +4 -0
  69. package/dist/incoming/http/parseQuery.js +25 -0
  70. package/dist/incoming/http/parseQuery.js.map +1 -0
  71. package/dist/incoming/http/readBody.d.ts +4 -0
  72. package/dist/incoming/http/readBody.js +107 -0
  73. package/dist/incoming/http/readBody.js.map +1 -0
  74. package/dist/incoming/http/streamFunction/DataStream.d.ts +7 -0
  75. package/dist/incoming/http/streamFunction/DataStream.js +19 -0
  76. package/dist/incoming/http/streamFunction/DataStream.js.map +1 -0
  77. package/dist/incoming/http/streamFunction/getExtension.d.ts +2 -0
  78. package/dist/incoming/http/streamFunction/getExtension.js +29 -0
  79. package/dist/incoming/http/streamFunction/getExtension.js.map +1 -0
  80. package/dist/incoming/http/streamFunction/index.d.ts +4 -0
  81. package/dist/incoming/http/streamFunction/index.js +106 -0
  82. package/dist/incoming/http/streamFunction/index.js.map +1 -0
  83. package/dist/incoming/http/streamFunction/multipartStream.d.ts +13 -0
  84. package/dist/incoming/http/streamFunction/multipartStream.js +208 -0
  85. package/dist/incoming/http/streamFunction/multipartStream.js.map +1 -0
  86. package/dist/incoming/http/streamFunction/stream.d.ts +6 -0
  87. package/dist/incoming/http/streamFunction/stream.js +108 -0
  88. package/dist/incoming/http/streamFunction/stream.js.map +1 -0
  89. package/dist/incoming/index.d.ts +3 -0
  90. package/dist/incoming/index.js +84 -0
  91. package/dist/incoming/index.js.map +1 -0
  92. package/dist/incoming/upgrade.d.ts +4 -0
  93. package/dist/incoming/upgrade.js +64 -0
  94. package/dist/incoming/upgrade.js.map +1 -0
  95. package/dist/incoming/ws/auth.d.ts +5 -0
  96. package/dist/incoming/ws/auth.js +40 -0
  97. package/dist/incoming/ws/auth.js.map +1 -0
  98. package/dist/incoming/ws/function.d.ts +3 -0
  99. package/dist/incoming/ws/function.js +107 -0
  100. package/dist/incoming/ws/function.js.map +1 -0
  101. package/dist/incoming/ws/get.d.ts +3 -0
  102. package/dist/incoming/ws/get.js +136 -0
  103. package/dist/incoming/ws/get.js.map +1 -0
  104. package/dist/incoming/ws/index.d.ts +3 -0
  105. package/dist/incoming/ws/index.js +61 -0
  106. package/dist/incoming/ws/index.js.map +1 -0
  107. package/dist/incoming/ws/observable.d.ts +6 -0
  108. package/dist/incoming/ws/observable.js +112 -0
  109. package/dist/incoming/ws/observable.js.map +1 -0
  110. package/dist/index.d.ts +11 -35
  111. package/dist/index.js +19 -196
  112. package/dist/index.js.map +1 -1
  113. package/dist/ip.d.ts +2 -0
  114. package/dist/ip.js +8 -0
  115. package/dist/ip.js.map +1 -0
  116. package/dist/network/http/authorize.d.ts +3 -0
  117. package/dist/network/http/authorize.js +28 -0
  118. package/dist/network/http/authorize.js.map +1 -0
  119. package/dist/network/http/compress.d.ts +6 -0
  120. package/dist/network/http/compress.js +56 -0
  121. package/dist/network/http/compress.js.map +1 -0
  122. package/dist/network/http/end.d.ts +4 -0
  123. package/dist/network/http/end.js +19 -0
  124. package/dist/network/http/end.js.map +1 -0
  125. package/dist/network/http/function.d.ts +3 -0
  126. package/dist/network/http/function.js +47 -0
  127. package/dist/network/http/function.js.map +1 -0
  128. package/dist/network/http/get.d.ts +3 -0
  129. package/dist/network/http/get.js +129 -0
  130. package/dist/network/http/get.js.map +1 -0
  131. package/dist/network/http/index.d.ts +3 -0
  132. package/dist/network/http/index.js +122 -0
  133. package/dist/network/http/index.js.map +1 -0
  134. package/dist/network/http/readBody.d.ts +3 -0
  135. package/dist/network/http/readBody.js +112 -0
  136. package/dist/network/http/readBody.js.map +1 -0
  137. package/dist/network/http/send.d.ts +5 -0
  138. package/dist/network/http/send.js +60 -0
  139. package/dist/network/http/send.js.map +1 -0
  140. package/dist/network/http/streamFunction/DataStream.d.ts +7 -0
  141. package/dist/network/http/streamFunction/DataStream.js +19 -0
  142. package/dist/network/http/streamFunction/DataStream.js.map +1 -0
  143. package/dist/network/http/streamFunction/getExtension.d.ts +2 -0
  144. package/dist/network/http/streamFunction/getExtension.js +29 -0
  145. package/dist/network/http/streamFunction/getExtension.js.map +1 -0
  146. package/dist/network/http/streamFunction/index.d.ts +3 -0
  147. package/dist/network/http/streamFunction/index.js +81 -0
  148. package/dist/network/http/streamFunction/index.js.map +1 -0
  149. package/dist/network/http/streamFunction/multipartStream.d.ts +12 -0
  150. package/dist/network/http/streamFunction/multipartStream.js +208 -0
  151. package/dist/network/http/streamFunction/multipartStream.js.map +1 -0
  152. package/dist/network/http/streamFunction/stream.d.ts +5 -0
  153. package/dist/network/http/streamFunction/stream.js +103 -0
  154. package/dist/network/http/streamFunction/stream.js.map +1 -0
  155. package/dist/network/index.d.ts +4 -0
  156. package/dist/network/index.js +84 -0
  157. package/dist/network/index.js.map +1 -0
  158. package/dist/network/message/auth.d.ts +4 -0
  159. package/dist/network/message/auth.js +32 -0
  160. package/dist/network/message/auth.js.map +1 -0
  161. package/dist/network/message/function.d.ts +3 -0
  162. package/dist/network/message/function.js +67 -0
  163. package/dist/network/message/function.js.map +1 -0
  164. package/dist/network/message/get.d.ts +3 -0
  165. package/dist/network/message/get.js +109 -0
  166. package/dist/network/message/get.js.map +1 -0
  167. package/dist/network/message/index.d.ts +3 -0
  168. package/dist/network/message/index.js +62 -0
  169. package/dist/network/message/index.js.map +1 -0
  170. package/dist/network/message/observable.d.ts +5 -0
  171. package/dist/network/message/observable.js +103 -0
  172. package/dist/network/message/observable.js.map +1 -0
  173. package/dist/network/message/send.d.ts +4 -0
  174. package/dist/network/message/send.js +11 -0
  175. package/dist/network/message/send.js.map +1 -0
  176. package/dist/network/upgrade.d.ts +4 -0
  177. package/dist/network/upgrade.js +49 -0
  178. package/dist/network/upgrade.js.map +1 -0
  179. package/dist/network/worker/get.d.ts +0 -0
  180. package/dist/network/worker/get.js +122 -0
  181. package/dist/network/worker/get.js.map +1 -0
  182. package/dist/network/worker/observable.d.ts +0 -0
  183. package/dist/network/worker/observable.js +120 -0
  184. package/dist/network/worker/observable.js.map +1 -0
  185. package/dist/observable/create.d.ts +3 -0
  186. package/dist/observable/create.js +30 -0
  187. package/dist/observable/create.js.map +1 -0
  188. package/dist/observable/destroy.d.ts +2 -0
  189. package/dist/observable/destroy.js +51 -0
  190. package/dist/observable/destroy.js.map +1 -0
  191. package/dist/observable/error.d.ts +4 -0
  192. package/dist/observable/error.js +17 -0
  193. package/dist/observable/error.js.map +1 -0
  194. package/dist/observable/extendCache.d.ts +2 -0
  195. package/dist/observable/extendCache.js +11 -0
  196. package/dist/observable/extendCache.js.map +1 -0
  197. package/dist/observable/genObservableId.d.ts +1 -0
  198. package/dist/observable/genObservableId.js +9 -0
  199. package/dist/observable/genObservableId.js.map +1 -0
  200. package/dist/observable/get.d.ts +4 -0
  201. package/dist/observable/get.js +15 -0
  202. package/dist/observable/get.js.map +1 -0
  203. package/dist/observable/index.d.ts +11 -0
  204. package/dist/observable/index.js +28 -0
  205. package/dist/observable/index.js.map +1 -0
  206. package/dist/observable/send.d.ts +6 -0
  207. package/dist/observable/send.js +32 -0
  208. package/dist/observable/send.js.map +1 -0
  209. package/dist/observable/start/error.d.ts +3 -0
  210. package/dist/observable/start/error.js +36 -0
  211. package/dist/observable/start/error.js.map +1 -0
  212. package/dist/observable/start/index.d.ts +2 -0
  213. package/dist/observable/start/index.js +46 -0
  214. package/dist/observable/start/index.js.map +1 -0
  215. package/dist/observable/start/update.d.ts +6 -0
  216. package/dist/observable/start/update.js +104 -0
  217. package/dist/observable/start/update.js.map +1 -0
  218. package/dist/observable/subscribe.d.ts +7 -0
  219. package/dist/observable/subscribe.js +54 -0
  220. package/dist/observable/subscribe.js.map +1 -0
  221. package/dist/observable/types.d.ts +26 -0
  222. package/dist/observable/types.js +3 -0
  223. package/dist/observable/types.js.map +1 -0
  224. package/dist/observable/unsub.d.ts +6 -0
  225. package/dist/observable/unsub.js +43 -0
  226. package/dist/observable/unsub.js.map +1 -0
  227. package/dist/observable/verify.d.ts +4 -0
  228. package/dist/observable/verify.js +18 -0
  229. package/dist/observable/verify.js.map +1 -0
  230. package/dist/protocol.d.ts +21 -0
  231. package/dist/protocol.js +233 -0
  232. package/dist/protocol.js.map +1 -0
  233. package/dist/security/index.d.ts +3 -0
  234. package/dist/security/index.js +66 -0
  235. package/dist/security/index.js.map +1 -0
  236. package/dist/security.d.ts +6 -0
  237. package/dist/security.js +95 -0
  238. package/dist/security.js.map +1 -0
  239. package/dist/sendError.d.ts +5 -0
  240. package/dist/sendError.js +42 -0
  241. package/dist/sendError.js.map +1 -0
  242. package/dist/sendHttpResponse.d.ts +4 -0
  243. package/dist/sendHttpResponse.js +53 -0
  244. package/dist/sendHttpResponse.js.map +1 -0
  245. package/dist/server.d.ts +69 -0
  246. package/dist/server.js +113 -0
  247. package/dist/server.js.map +1 -0
  248. package/dist/worker/authorize.d.ts +5 -0
  249. package/dist/worker/authorize.js +15 -0
  250. package/dist/worker/authorize.js.map +1 -0
  251. package/dist/worker/fnMap.d.ts +2 -0
  252. package/dist/worker/fnMap.js +5 -0
  253. package/dist/worker/fnMap.js.map +1 -0
  254. package/dist/worker/functions.d.ts +3 -0
  255. package/dist/worker/functions.js +8 -0
  256. package/dist/worker/functions.js.map +1 -0
  257. package/dist/worker/http/function.d.ts +4 -0
  258. package/dist/worker/http/function.js +75 -0
  259. package/dist/worker/http/function.js.map +1 -0
  260. package/dist/worker/index.d.ts +1 -0
  261. package/dist/worker/index.js +101 -0
  262. package/dist/worker/index.js.map +1 -0
  263. package/dist/worker/observable.d.ts +15 -0
  264. package/dist/worker/observable.js +110 -0
  265. package/dist/worker/observable.js.map +1 -0
  266. package/dist/worker/ws/function.d.ts +3 -0
  267. package/dist/worker/ws/function.js +42 -0
  268. package/dist/worker/ws/function.js.map +1 -0
  269. package/package.json +16 -14
package/dist/error.js ADDED
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createError = exports.BasedErrorCode = void 0;
4
+ var BasedErrorCode;
5
+ (function (BasedErrorCode) {
6
+ BasedErrorCode[BasedErrorCode["FunctionError"] = 50001] = "FunctionError";
7
+ BasedErrorCode[BasedErrorCode["AuthorizeFunctionError"] = 50002] = "AuthorizeFunctionError";
8
+ BasedErrorCode[BasedErrorCode["NoOservableCacheAvailable"] = 50003] = "NoOservableCacheAvailable";
9
+ BasedErrorCode[BasedErrorCode["ObservableFunctionError"] = 50004] = "ObservableFunctionError";
10
+ BasedErrorCode[BasedErrorCode["FunctionNotFound"] = 40401] = "FunctionNotFound";
11
+ BasedErrorCode[BasedErrorCode["FunctionIsNotObservable"] = 40402] = "FunctionIsNotObservable";
12
+ BasedErrorCode[BasedErrorCode["FunctionIsObservable"] = 40403] = "FunctionIsObservable";
13
+ BasedErrorCode[BasedErrorCode["FunctionIsStream"] = 40404] = "FunctionIsStream";
14
+ BasedErrorCode[BasedErrorCode["CannotStreamToObservableFunction"] = 40402] = "CannotStreamToObservableFunction";
15
+ BasedErrorCode[BasedErrorCode["AuthorizeRejectedError"] = 40301] = "AuthorizeRejectedError";
16
+ BasedErrorCode[BasedErrorCode["InvalidPayload"] = 40001] = "InvalidPayload";
17
+ BasedErrorCode[BasedErrorCode["PayloadTooLarge"] = 40002] = "PayloadTooLarge";
18
+ BasedErrorCode[BasedErrorCode["ChunkTooLarge"] = 40003] = "ChunkTooLarge";
19
+ BasedErrorCode[BasedErrorCode["UnsupportedContentEncoding"] = 40004] = "UnsupportedContentEncoding";
20
+ BasedErrorCode[BasedErrorCode["NoBinaryProtocol"] = 40005] = "NoBinaryProtocol";
21
+ BasedErrorCode[BasedErrorCode["LengthRequired"] = 41101] = "LengthRequired";
22
+ BasedErrorCode[BasedErrorCode["MethodNotAllowed"] = 40501] = "MethodNotAllowed";
23
+ })(BasedErrorCode = exports.BasedErrorCode || (exports.BasedErrorCode = {}));
24
+ /*
25
+ for functione errors
26
+
27
+ // errorData.basedMessage =
28
+ // typeof errorDefaults[basedCode]?.message === 'function'
29
+ // ? errorDefaults[basedCode]?.message(err)
30
+ // : errorDefaults[basedCode]?.message ||
31
+ // errorDefaults[basedCode]?.status ||
32
+ // 'Oops something went wrong'
33
+ // if (payload && 'err' in payload && payload instanceof Error) {
34
+ // Object.getOwnPropertyNames(payload.err).forEach((key: string) => {
35
+ // errorData[key] = payload.err[key]
36
+ // })
37
+ // } else {
38
+ // errorData.message = errorData.basedMessage
39
+ // const captureTarget = { stack: null }
40
+ // Error.captureStackTrace(captureTarget, createError)
41
+ // errorData.stack = captureTarget.stack
42
+ // }
43
+ // }
44
+ */
45
+ const errorTypes = {
46
+ [BasedErrorCode.FunctionError]: {
47
+ statusCode: 500,
48
+ statusMessage: 'Internal Server Error',
49
+ message: (payload) => {
50
+ // do it nice
51
+ return `Error in function ${payload.route.name} [${payload.err.name}] ${payload.err.message}`;
52
+ },
53
+ },
54
+ [BasedErrorCode.FunctionNotFound]: {
55
+ statusCode: 404,
56
+ statusMessage: 'Not Found',
57
+ message: (payload) => `Function not found${payload.name ? ` '${payload.name}'` : ''}${payload.path ? ` path '${payload.path}'` : ''}`,
58
+ },
59
+ [BasedErrorCode.FunctionIsStream]: {
60
+ statusCode: 400,
61
+ statusMessage: 'Incorrect protocol',
62
+ message: () => 'Cannot use stream functions over websockets',
63
+ },
64
+ [BasedErrorCode.CannotStreamToObservableFunction]: {
65
+ statusCode: 404,
66
+ statusMessage: 'Not Found',
67
+ message: 'Cannot stream to observable function.',
68
+ },
69
+ [BasedErrorCode.AuthorizeFunctionError]: {
70
+ statusCode: 403,
71
+ statusMessage: 'Forbidden',
72
+ message: 'Error in authorize function',
73
+ },
74
+ [BasedErrorCode.AuthorizeRejectedError]: {
75
+ statusCode: 403,
76
+ statusMessage: 'Forbidden',
77
+ message: (payload) => `Authorize rejected access to ${payload.name}`,
78
+ },
79
+ [BasedErrorCode.InvalidPayload]: {
80
+ statusCode: 400,
81
+ statusMessage: 'Bad Request',
82
+ message: (payload) => 'Invalid payload ' + payload.name,
83
+ },
84
+ [BasedErrorCode.NoBinaryProtocol]: {
85
+ statusCode: 400,
86
+ statusMessage: 'Protocol mismatch',
87
+ message: () => 'Please upgrade to the latest based client',
88
+ },
89
+ [BasedErrorCode.PayloadTooLarge]: {
90
+ statusCode: 413,
91
+ status: 'Payload Too Large',
92
+ message: (payload) => 'PayloadTooLarge ' + payload.name,
93
+ },
94
+ [BasedErrorCode.ChunkTooLarge]: {
95
+ statusCode: 413,
96
+ status: 'Payload Too Large',
97
+ message: (payload) => 'ChunkTooLarge ' + payload.name,
98
+ },
99
+ [BasedErrorCode.UnsupportedContentEncoding]: {
100
+ statusCode: 400,
101
+ statusMessage: 'Incorrect content encoding',
102
+ },
103
+ [BasedErrorCode.LengthRequired]: { code: 411, status: 'Length Required' },
104
+ [BasedErrorCode.MethodNotAllowed]: {
105
+ statusCode: 405,
106
+ statusMessage: 'Method Not Allowed',
107
+ },
108
+ [BasedErrorCode.NoOservableCacheAvailable]: {
109
+ statusCode: 500,
110
+ statusMessage: 'Internal Server Error',
111
+ message: (payload) => `No observable cache available${payload.route.name} - ${payload.observableId}`,
112
+ },
113
+ };
114
+ const isBasedFunctionRoute = (route) => {
115
+ if (route && typeof route === 'object' && 'name' in route) {
116
+ return true;
117
+ }
118
+ return false;
119
+ };
120
+ const EMPTY = {
121
+ route: {
122
+ name: 'no-route',
123
+ },
124
+ };
125
+ const createError = (server, client, code, payload) => {
126
+ const type = errorTypes[code];
127
+ const route = !payload
128
+ ? EMPTY.route
129
+ : isBasedFunctionRoute(payload)
130
+ ? payload
131
+ : payload.route;
132
+ const errorData = {
133
+ code,
134
+ statusCode: type.statusCode,
135
+ statusMessage: type.statusMessage,
136
+ message: typeof type.message === 'function' ? type.message(payload) : type.message,
137
+ route,
138
+ };
139
+ if ('requestId' in payload) {
140
+ errorData.requestId = payload.requestId;
141
+ }
142
+ if ('observableId' in payload) {
143
+ errorData.observableId = payload.observableId;
144
+ }
145
+ server.emit('error', client, errorData);
146
+ return errorData;
147
+ };
148
+ exports.createError = createError;
149
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAGA,IAAY,cAkBX;AAlBD,WAAY,cAAc;IACxB,yEAAqB,CAAA;IACrB,2FAA8B,CAAA;IAC9B,iGAAiC,CAAA;IACjC,6FAA+B,CAAA;IAC/B,+EAAwB,CAAA;IACxB,6FAA+B,CAAA;IAC/B,uFAA4B,CAAA;IAC5B,+EAAwB,CAAA;IACxB,+GAAwC,CAAA;IACxC,2FAA8B,CAAA;IAC9B,2EAAsB,CAAA;IACtB,6EAAuB,CAAA;IACvB,yEAAqB,CAAA;IACrB,mGAAkC,CAAA;IAClC,+EAAwB,CAAA;IACxB,2EAAsB,CAAA;IACtB,+EAAwB,CAAA;AAC1B,CAAC,EAlBW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAkBzB;AA0CD;;;;;;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vB"}
@@ -0,0 +1,34 @@
1
+ /// <reference types="node" />
2
+ import type { BasedServer } from '../server';
3
+ import { BasedFunctionRoute, BasedFunctionSpec, BasedObservableFunctionSpec, FunctionConfig } from './types';
4
+ export * from './types';
5
+ export declare class BasedFunctions {
6
+ server: BasedServer;
7
+ reqId: number;
8
+ config: FunctionConfig;
9
+ unregisterTimeout: NodeJS.Timeout;
10
+ installsInProgress: {
11
+ [name: string]: Promise<any>;
12
+ };
13
+ paths: {
14
+ [path: string]: string;
15
+ };
16
+ specs: {
17
+ [name: string]: BasedFunctionSpec | BasedObservableFunctionSpec;
18
+ };
19
+ beingUninstalled: {
20
+ [name: string]: boolean;
21
+ };
22
+ constructor(server: BasedServer, config?: FunctionConfig);
23
+ uninstallLoop(): void;
24
+ updateConfig(config: FunctionConfig): void;
25
+ updateFunction(spec: BasedObservableFunctionSpec | BasedFunctionSpec): Promise<void>;
26
+ private installGaurdedFromConfig;
27
+ install(name: string): Promise<BasedObservableFunctionSpec | BasedFunctionSpec | false>;
28
+ getNameFromPath(path: string): string;
29
+ route(name?: string, path?: string): BasedFunctionRoute | false;
30
+ getFromStore(name: string): BasedObservableFunctionSpec | BasedFunctionSpec | false;
31
+ update(spec: BasedObservableFunctionSpec | BasedFunctionSpec): boolean;
32
+ remove(name: string): boolean;
33
+ uninstall(name: string, spec?: BasedObservableFunctionSpec | BasedFunctionSpec | false): Promise<boolean>;
34
+ }
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.BasedFunctions = void 0;
18
+ const types_1 = require("./types");
19
+ const utils_1 = require("@saulx/utils");
20
+ const timeout_1 = require("./timeout");
21
+ const observable_1 = require("../observable");
22
+ __exportStar(require("./types"), exports);
23
+ class BasedFunctions {
24
+ constructor(server, config) {
25
+ this.reqId = 0;
26
+ this.installsInProgress = {};
27
+ this.paths = {};
28
+ this.specs = {};
29
+ this.beingUninstalled = {};
30
+ this.server = server;
31
+ if (config) {
32
+ this.updateConfig(config);
33
+ }
34
+ }
35
+ uninstallLoop() {
36
+ this.unregisterTimeout = setTimeout(async () => {
37
+ const q = [];
38
+ for (const name in this.specs) {
39
+ const spec = this.specs[name];
40
+ if (spec.observable && this.server.activeObservables[name]) {
41
+ (0, timeout_1.updateTimeoutCounter)(spec);
42
+ }
43
+ else if ((0, timeout_1.fnIsTimedOut)(spec)) {
44
+ q.push(this.uninstall(name, spec));
45
+ }
46
+ }
47
+ await Promise.all(q);
48
+ this.uninstallLoop();
49
+ }, 3e3);
50
+ }
51
+ updateConfig(config) {
52
+ if (this.config) {
53
+ (0, utils_1.deepMerge)(this.config, config);
54
+ }
55
+ else {
56
+ this.config = config;
57
+ }
58
+ if (this.config.idleTimeout === undefined) {
59
+ this.config.idleTimeout = 60e3; // 1 min
60
+ }
61
+ if (this.config.memCacheTimeout === undefined) {
62
+ this.config.memCacheTimeout = 3e3;
63
+ }
64
+ if (this.config.maxWorkers === undefined) {
65
+ this.config.maxWorkers = 1;
66
+ }
67
+ if (this.unregisterTimeout) {
68
+ clearTimeout(this.unregisterTimeout);
69
+ }
70
+ this.uninstallLoop();
71
+ }
72
+ async updateFunction(spec) {
73
+ const { name } = spec;
74
+ const prevSpec = this.specs[name];
75
+ if (prevSpec) {
76
+ if (prevSpec.function !== spec.function) {
77
+ if (this.beingUninstalled[name]) {
78
+ delete this.beingUninstalled[name];
79
+ }
80
+ (0, timeout_1.updateTimeoutCounter)(spec);
81
+ await this.installGaurdedFromConfig(name);
82
+ await this.config.uninstall({
83
+ server: this.server,
84
+ function: prevSpec,
85
+ name,
86
+ });
87
+ this.update(spec);
88
+ }
89
+ else {
90
+ this.update(spec);
91
+ }
92
+ }
93
+ }
94
+ async installGaurdedFromConfig(name) {
95
+ if (this.installsInProgress[name]) {
96
+ return this.installsInProgress[name];
97
+ }
98
+ this.installsInProgress[name] = this.config.install({
99
+ server: this.server,
100
+ name,
101
+ });
102
+ const s = await this.installsInProgress[name];
103
+ delete this.installsInProgress[name];
104
+ return s;
105
+ }
106
+ async install(name) {
107
+ let spec = this.getFromStore(name);
108
+ if (spec) {
109
+ return spec;
110
+ }
111
+ spec = await this.installGaurdedFromConfig(name);
112
+ if (spec) {
113
+ this.update(spec);
114
+ return this.getFromStore(name);
115
+ }
116
+ return false;
117
+ }
118
+ getNameFromPath(path) {
119
+ return this.paths[path];
120
+ }
121
+ route(name, path) {
122
+ return this.config.route({ server: this.server, name, path });
123
+ }
124
+ getFromStore(name) {
125
+ const spec = this.specs[name];
126
+ if (spec) {
127
+ if (this.beingUninstalled[name]) {
128
+ delete this.beingUninstalled[name];
129
+ }
130
+ (0, timeout_1.updateTimeoutCounter)(spec);
131
+ return spec;
132
+ }
133
+ return false;
134
+ }
135
+ update(spec) {
136
+ if (!spec) {
137
+ return false;
138
+ }
139
+ if (!spec.idleTimeout) {
140
+ spec.idleTimeout = this.config.idleTimeout;
141
+ }
142
+ if (spec.timeoutCounter === undefined) {
143
+ spec.timeoutCounter =
144
+ spec.idleTimeout === 0 ? -1 : Math.ceil(spec.idleTimeout / 1e3);
145
+ }
146
+ if (spec.path) {
147
+ this.paths[spec.path] = spec.name;
148
+ }
149
+ if (this.specs[spec.name] && this.server.activeObservables[spec.name]) {
150
+ if (!(0, types_1.isObservableFunctionSpec)(spec)) {
151
+ for (const [id] of this.server.activeObservables[spec.name]) {
152
+ (0, observable_1.destroyObs)(this.server, id);
153
+ }
154
+ }
155
+ else {
156
+ for (const [id] of this.server.activeObservables[spec.name]) {
157
+ (0, observable_1.start)(this.server, id);
158
+ }
159
+ }
160
+ }
161
+ this.specs[spec.name] = spec;
162
+ return false;
163
+ }
164
+ remove(name) {
165
+ // Does not call unregister!
166
+ const spec = this.specs[name];
167
+ if (!spec) {
168
+ return false;
169
+ }
170
+ if ((0, types_1.isObservableFunctionSpec)(spec)) {
171
+ const activeObs = this.server.activeObservables[name];
172
+ if (activeObs) {
173
+ for (const [id] of activeObs) {
174
+ (0, observable_1.destroyObs)(this.server, id);
175
+ }
176
+ delete this.server.activeObservables[name];
177
+ }
178
+ }
179
+ delete this.specs[name];
180
+ return true;
181
+ }
182
+ async uninstall(name, spec) {
183
+ if (this.beingUninstalled[name]) {
184
+ console.error('Allready being unregistered...', name);
185
+ }
186
+ if (!spec && spec !== false) {
187
+ spec = this.specs[name];
188
+ }
189
+ if (!spec) {
190
+ return false;
191
+ }
192
+ this.beingUninstalled[name] = true;
193
+ if (await this.config.uninstall({
194
+ server: this.server,
195
+ function: spec,
196
+ name,
197
+ })) {
198
+ if (this.beingUninstalled[name]) {
199
+ delete this.beingUninstalled[name];
200
+ return this.remove(name);
201
+ }
202
+ else {
203
+ console.info('got requested while being unregistered', name);
204
+ }
205
+ }
206
+ return false;
207
+ }
208
+ }
209
+ exports.BasedFunctions = BasedFunctions;
210
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,mCAMgB;AAChB,wCAAwC;AACxC,uCAA8D;AAC9D,8CAAiD;AACjD,0CAAuB;AAEvB,MAAa,cAAc;IAuBzB,YAAY,MAAmB,EAAE,MAAuB;QApBxD,UAAK,GAAW,CAAC,CAAA;QAMjB,uBAAkB,GAAqC,EAAE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1B,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,QAAQ,EAAE,IAAI;YACd,IAAI;SACL,CAAC,EACF;YACA,IAAI,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE;gBAC/B,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;gBAClC,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;aACzB;iBAAM;gBACL,OAAO,CAAC,IAAI,CAAC,wCAAwC,EAAE,IAAI,CAAC,CAAA;aAC7D;SACF;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AA9OD,wCA8OC"}
@@ -0,0 +1,3 @@
1
+ import type { BasedFunctionSpec, BasedObservableFunctionSpec } from './types';
2
+ export declare const fnIsTimedOut: (spec: BasedObservableFunctionSpec | BasedFunctionSpec) => boolean;
3
+ export declare const updateTimeoutCounter: (spec: BasedObservableFunctionSpec | BasedFunctionSpec) => void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateTimeoutCounter = exports.fnIsTimedOut = void 0;
4
+ const fnIsTimedOut = (spec) => {
5
+ if (spec.timeoutCounter !== -1) {
6
+ if (spec.timeoutCounter === 0) {
7
+ return true;
8
+ }
9
+ if (spec.idleTimeout > 0 && spec.timeoutCounter > 0) {
10
+ spec.timeoutCounter--;
11
+ }
12
+ }
13
+ return false;
14
+ };
15
+ exports.fnIsTimedOut = fnIsTimedOut;
16
+ const updateTimeoutCounter = (spec) => {
17
+ if (spec.timeoutCounter !== -1) {
18
+ spec.timeoutCounter =
19
+ spec.idleTimeout === -1 ? -1 : Math.ceil(spec.idleTimeout / 1e3 / 3);
20
+ }
21
+ };
22
+ exports.updateTimeoutCounter = updateTimeoutCounter;
23
+ //# sourceMappingURL=timeout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeout.js","sourceRoot":"","sources":["../../src/functions/timeout.ts"],"names":[],"mappings":";;;AAEO,MAAM,YAAY,GAAG,CAC1B,IAAqD,EAC5C,EAAE;IACX,IAAI,IAAI,CAAC,cAAc,KAAK,CAAC,CAAC,EAAE;QAC9B,IAAI,IAAI,CAAC,cAAc,KAAK,CAAC,EAAE;YAC7B,OAAO,IAAI,CAAA;SACZ;QACD,IAAI,IAAI,CAAC,WAAW,GAAG,CAAC,IAAI,IAAI,CAAC,cAAc,GAAG,CAAC,EAAE;YACnD,IAAI,CAAC,cAAc,EAAE,CAAA;SACtB;KACF;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAZY,QAAA,YAAY,gBAYxB;AAEM,MAAM,oBAAoB,GAAG,CAClC,IAAqD,EACrD,EAAE;IACF,IAAI,IAAI,CAAC,cAAc,KAAK,CAAC,CAAC,EAAE;QAC9B,IAAI,CAAC,cAAc;YACjB,IAAI,CAAC,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,GAAG,GAAG,CAAC,CAAC,CAAA;KACvE;AACH,CAAC,CAAA;AAPY,QAAA,oBAAoB,wBAOhC"}
@@ -0,0 +1,67 @@
1
+ import { Context, HttpSession } from '../context';
2
+ import { ObservableUpdateFunction } from '../observable';
3
+ import { BasedServer } from '../server';
4
+ export declare type CustomHttpResponse = (result: any, payload: any, client: Context<HttpSession>) => Promise<boolean>;
5
+ export declare type BasedFunctionRoute = {
6
+ name: string;
7
+ observable?: boolean;
8
+ headers?: string[];
9
+ path?: string;
10
+ stream?: boolean;
11
+ maxPayloadSize?: number;
12
+ rateLimitTokens?: number;
13
+ public?: boolean;
14
+ };
15
+ export declare type BasedObservableFunction = (payload: any, update: ObservableUpdateFunction) => Promise<() => void>;
16
+ export declare type BasedObservableFunctionSpec = BasedFunctionRoute & {
17
+ name: string;
18
+ checksum: number;
19
+ observable: true;
20
+ function: BasedObservableFunction;
21
+ customHttpResponse?: CustomHttpResponse;
22
+ memCacheTimeout?: number;
23
+ idleTimeout?: number;
24
+ timeoutCounter?: number;
25
+ };
26
+ export declare type BasedFunction = (payload: any, client: Context) => Promise<any>;
27
+ export declare type BasedFunctionSpec = BasedFunctionRoute & {
28
+ name: string;
29
+ observable?: false;
30
+ customHttpResponse?: CustomHttpResponse;
31
+ checksum: number;
32
+ function: BasedFunction;
33
+ maxExecTime?: number;
34
+ idleTimeout?: number;
35
+ timeoutCounter?: number;
36
+ };
37
+ export declare type FunctionConfig = {
38
+ memCacheTimeout?: number;
39
+ idleTimeout?: number;
40
+ maxWorkers?: number;
41
+ importWrapperPath?: string;
42
+ route: (opts: {
43
+ server: BasedServer;
44
+ name?: string;
45
+ path?: string;
46
+ }) => false | (BasedFunctionRoute & {
47
+ maxPayloadSize: number;
48
+ rateLimitTokens: number;
49
+ });
50
+ install: (opts: {
51
+ server: BasedServer;
52
+ name: string;
53
+ function?: BasedFunctionSpec | BasedObservableFunctionSpec;
54
+ }) => Promise<false | BasedObservableFunctionSpec | BasedFunctionSpec>;
55
+ uninstall: (opts: {
56
+ server: BasedServer;
57
+ name: string;
58
+ function: BasedObservableFunctionSpec | BasedFunctionSpec;
59
+ }) => Promise<boolean>;
60
+ };
61
+ export declare enum FunctionType {
62
+ authorize = 0,
63
+ observe = 1,
64
+ function = 2,
65
+ streamFunction = 3
66
+ }
67
+ export declare function isObservableFunctionSpec(fn: BasedObservableFunctionSpec | BasedFunctionSpec): fn is BasedObservableFunctionSpec;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isObservableFunctionSpec = exports.FunctionType = void 0;
4
+ var FunctionType;
5
+ (function (FunctionType) {
6
+ FunctionType[FunctionType["authorize"] = 0] = "authorize";
7
+ FunctionType[FunctionType["observe"] = 1] = "observe";
8
+ FunctionType[FunctionType["function"] = 2] = "function";
9
+ FunctionType[FunctionType["streamFunction"] = 3] = "streamFunction";
10
+ })(FunctionType = exports.FunctionType || (exports.FunctionType = {}));
11
+ function isObservableFunctionSpec(fn) {
12
+ return fn.observable;
13
+ }
14
+ exports.isObservableFunctionSpec = isObservableFunctionSpec;
15
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/functions/types.ts"],"names":[],"mappings":";;;AA2EA,IAAY,YAKX;AALD,WAAY,YAAY;IACtB,yDAAS,CAAA;IACT,qDAAO,CAAA;IACP,uDAAQ,CAAA;IACR,mEAAc,CAAA;AAChB,CAAC,EALW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAKvB;AAED,SAAgB,wBAAwB,CACtC,EAAmD;IAEnD,OAAQ,EAAkC,CAAC,UAAU,CAAA;AACvD,CAAC;AAJD,4DAIC"}
File without changes
@@ -0,0 +1,5 @@
1
+ // add a few worker also handle errors of observables
2
+ // think about making a dedicated worker for based client
3
+ // also make a thing 'sticky worker'
4
+ // cant get the server in a function like this , also not the client just PAYLOAD / OUTGOING PAYLOAD - outgoing
5
+ //# sourceMappingURL=workerPool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerPool.js","sourceRoot":"","sources":["../../src/functions/workerPool.ts"],"names":[],"mappings":"AAAA,qDAAqD;AAErD,yDAAyD;AACzD,oCAAoC;AAEpC,+GAA+G"}
@@ -5,6 +5,6 @@ import Client from '../../Client';
5
5
  import { SendSubscriptionGetDataMessage, SubscribeMessage } from '@based/client';
6
6
  import { Params } from '../../Params';
7
7
  import { Subscription } from '../subscription';
8
- export declare const getObservable: (server: BasedServer, client: Client, [, id, payload, checksum, name]: SendSubscriptionGetDataMessage) => Promise<Subscription | SharedConfigurationObservable | import("../functions/observable").SharedFunctionObservable | import("../functions/observable").FunctionObservable>;
8
+ export declare const getObservable: (server: BasedServer, client: Client, [, id, payload, checksum, name]: SendSubscriptionGetDataMessage) => Promise<SharedConfigurationObservable | import("../functions/observable").SharedFunctionObservable | import("../functions/observable").FunctionObservable | Subscription>;
9
9
  export declare const subscribeObservable: (server: BasedServer, client: Client, [, id, _payload, checksum, allwaysSend, _name]: SubscribeMessage) => Promise<void>;
10
10
  export declare const subscribeConfiguration: (params: Params, dataListener: DataListener) => Promise<SharedConfigurationObservable>;
@@ -0,0 +1,4 @@
1
+ import { BasedServer } from '../../server';
2
+ import { BasedFunctionRoute } from '../../functions';
3
+ import { HttpSession, Context } from '../../context';
4
+ export declare const authorizeRequest: (server: BasedServer, ctx: Context<HttpSession>, payload: any, route: BasedFunctionRoute, authorized: (payload: any) => void, notAuth?: () => void) => void;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.authorizeRequest = void 0;
4
+ const sendError_1 = require("../../sendError");
5
+ const error_1 = require("../../error");
6
+ const authorizeRequest = (server, ctx, payload, route, authorized, notAuth = () => undefined) => {
7
+ if (route.public === true) {
8
+ authorized(payload);
9
+ return;
10
+ }
11
+ server.auth
12
+ .authorize(ctx, route.name, payload)
13
+ .then((ok) => {
14
+ if (!ctx.session) {
15
+ notAuth();
16
+ return;
17
+ }
18
+ if (!ok) {
19
+ notAuth();
20
+ (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.AuthorizeRejectedError, {
21
+ route,
22
+ });
23
+ }
24
+ else {
25
+ authorized(payload);
26
+ }
27
+ })
28
+ .catch((err) => {
29
+ notAuth();
30
+ (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.AuthorizeFunctionError, {
31
+ route,
32
+ err,
33
+ });
34
+ });
35
+ };
36
+ exports.authorizeRequest = authorizeRequest;
37
+ //# sourceMappingURL=authorize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authorize.js","sourceRoot":"","sources":["../../../src/incoming/http/authorize.ts"],"names":[],"mappings":";;;AAEA,+CAA2C;AAC3C,uCAA4C;AAGrC,MAAM,gBAAgB,GAAG,CAC9B,MAAmB,EACnB,GAAyB,EACzB,OAAY,EACZ,KAAyB,EACzB,UAAkC,EAClC,UAAsB,GAAG,EAAE,CAAC,SAAS,EACrC,EAAE;IACF,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,EAAE;QACzB,UAAU,CAAC,OAAO,CAAC,CAAA;QACnB,OAAM;KACP;IAED,MAAM,CAAC,IAAI;SACR,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC;SACnC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE;QACX,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE;YAChB,OAAO,EAAE,CAAA;YACT,OAAM;SACP;QACD,IAAI,CAAC,EAAE,EAAE;YACP,OAAO,EAAE,CAAA;YACT,IAAA,qBAAS,EAAC,MAAM,EAAE,GAAG,EAAE,sBAAc,CAAC,sBAAsB,EAAE;gBAC5D,KAAK;aACN,CAAC,CAAA;SACH;aAAM;YACL,UAAU,CAAC,OAAO,CAAC,CAAA;SACpB;IACH,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACb,OAAO,EAAE,CAAA;QACT,IAAA,qBAAS,EAAC,MAAM,EAAE,GAAG,EAAE,sBAAc,CAAC,sBAAsB,EAAE;YAC5D,KAAK;YACL,GAAG;SACJ,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACN,CAAC,CAAA;AApCY,QAAA,gBAAgB,oBAoC5B"}
@@ -0,0 +1,4 @@
1
+ import { BasedServer } from '../../server';
2
+ import { BasedFunctionRoute } from '../../functions';
3
+ import { HttpSession, Context } from '../../context';
4
+ export declare const httpFunction: (route: BasedFunctionRoute, ctx: Context<HttpSession>, server: BasedServer, payload?: Uint8Array) => void;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.httpFunction = void 0;
4
+ const functions_1 = require("../../functions");
5
+ const sendHttpResponse_1 = require("../../sendHttpResponse");
6
+ const error_1 = require("../../error");
7
+ const sendError_1 = require("../../sendError");
8
+ const httpFunction = (route, ctx, server, payload) => {
9
+ if (!ctx.session) {
10
+ return;
11
+ }
12
+ const name = route.name;
13
+ server.functions
14
+ .install(name)
15
+ .then((spec) => {
16
+ if (!ctx.session) {
17
+ return;
18
+ }
19
+ if (spec && !(0, functions_1.isObservableFunctionSpec)(spec)) {
20
+ spec
21
+ .function(payload, ctx)
22
+ .then(async (result) => {
23
+ if (!ctx.session) {
24
+ return;
25
+ }
26
+ if (spec.customHttpResponse) {
27
+ if (await spec.customHttpResponse(result, payload, ctx)) {
28
+ return;
29
+ }
30
+ (0, sendHttpResponse_1.sendHttpResponse)(ctx, result);
31
+ }
32
+ else {
33
+ (0, sendHttpResponse_1.sendHttpResponse)(ctx, result);
34
+ }
35
+ })
36
+ .catch((err) => {
37
+ (0, sendError_1.sendError)(server, ctx, err.code, {
38
+ err,
39
+ route,
40
+ });
41
+ });
42
+ }
43
+ })
44
+ .catch(() => (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, route));
45
+ };
46
+ exports.httpFunction = httpFunction;
47
+ //# sourceMappingURL=function.js.map