@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
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.httpHandler = void 0;
4
+ const function_1 = require("./function");
5
+ const streamFunction_1 = require("./streamFunction");
6
+ const get_1 = require("./get");
7
+ const utils_1 = require("@saulx/utils");
8
+ const readBody_1 = require("./readBody");
9
+ const send_1 = require("./send");
10
+ const authorize_1 = require("./authorize");
11
+ const error_1 = require("../../error");
12
+ const security_1 = require("../../security");
13
+ let clientId = 0;
14
+ // TODO: re-add authorize
15
+ const handleRequest = (server, method, client, route, ready) => {
16
+ // send shared array buffer
17
+ if (method === 'post') {
18
+ (0, readBody_1.readBody)(server, client, ready, route);
19
+ }
20
+ else {
21
+ ready();
22
+ }
23
+ };
24
+ const httpHandler = (server, req, res) => {
25
+ res.onAborted(() => {
26
+ client.context = null;
27
+ client.res = null;
28
+ client.req = null;
29
+ });
30
+ const ip = req.getHeader('x-forwarded-for') ||
31
+ Buffer.from(res.getRemoteAddressAsText()).toString();
32
+ if ((0, security_1.incomingCounter)(server, ip)) {
33
+ res.writeStatus('429 Too Many Requests');
34
+ res.end();
35
+ return;
36
+ }
37
+ const url = req.getUrl();
38
+ const path = url.split('/');
39
+ const route = server.functions.route(path[1], url);
40
+ if (route === false) {
41
+ (0, send_1.sendHttpError)(server, {
42
+ res,
43
+ req,
44
+ // @ts-ignore (ignore because we dont need a lot here)
45
+ context: { ip, id: ++clientId, headers: {} },
46
+ }, error_1.BasedErrorCode.FunctionNotFound, path[1] ? { name: path[1] } : { path: url });
47
+ return;
48
+ }
49
+ const method = req.getMethod();
50
+ //
51
+ // const valid = simdjson.isValid(jsonString); // true
52
+ // read only...
53
+ const client = {
54
+ res,
55
+ req,
56
+ context: {
57
+ method,
58
+ query: req.getQuery(),
59
+ ua: req.getHeader('user-agent'),
60
+ ip,
61
+ id: ++clientId,
62
+ headers: {
63
+ authorization: req.getHeader('authorization'),
64
+ 'content-type': req.getHeader('content-type'),
65
+ 'content-encoding': req.getHeader('content-encoding'),
66
+ encoding: req.getHeader('accept-encoding'),
67
+ },
68
+ },
69
+ };
70
+ const len = req.getHeader('content-length');
71
+ // @ts-ignore
72
+ if (len && !isNaN(len)) {
73
+ client.context.headers['content-length'] = Number(len);
74
+ }
75
+ if (method === 'post' &&
76
+ client.context.headers['content-length'] === undefined) {
77
+ // zero allowed, but not for streams
78
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.LengthRequired, route);
79
+ return;
80
+ }
81
+ if (route.headers) {
82
+ for (const header of route.headers) {
83
+ const v = req.getHeader(header);
84
+ if (v) {
85
+ client.context[header] = v;
86
+ }
87
+ }
88
+ }
89
+ if (route.observable === true) {
90
+ if (route.stream) {
91
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.CannotStreamToObservableFunction, route);
92
+ return;
93
+ }
94
+ const checksumRaw = req.getHeader('if-none-match');
95
+ // @ts-ignore use isNaN to cast string to number
96
+ const checksum = !isNaN(checksumRaw) ? Number(checksumRaw) : 0;
97
+ handleRequest(server, method, client, route, (payload) => {
98
+ (0, authorize_1.authorizeRequest)(server, client, payload, route, () => {
99
+ (0, get_1.httpGet)(route, payload, client, server, checksum);
100
+ });
101
+ });
102
+ }
103
+ else {
104
+ if (route.stream === true) {
105
+ if (method !== 'post') {
106
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.MethodNotAllowed, route);
107
+ return;
108
+ }
109
+ if (client.context.headers['content-length'] === 0) {
110
+ // zero is also not allowed for streams
111
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.LengthRequired, route);
112
+ return;
113
+ }
114
+ (0, streamFunction_1.httpStreamFunction)(server, client, (0, utils_1.parseQuery)(client.context.query), route);
115
+ }
116
+ else {
117
+ handleRequest(server, method, client, route, (payload) => (0, function_1.httpFunction)(method, route, client, server, payload));
118
+ }
119
+ }
120
+ };
121
+ exports.httpHandler = httpHandler;
122
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/network/http/index.ts"],"names":[],"mappings":";;;AAGA,yCAAyC;AACzC,qDAAqD;AACrD,+BAA+B;AAC/B,wCAAyC;AACzC,yCAAqC;AACrC,iCAAsC;AACtC,2CAA8C;AAC9C,uCAA4C;AAC5C,6CAAgD;AAEhD,IAAI,QAAQ,GAAG,CAAC,CAAA;AAEhB,yBAAyB;AACzB,MAAM,aAAa,GAAG,CACpB,MAAmB,EACnB,MAAc,EACd,MAAkB,EAClB,KAAyB,EACzB,KAA8B,EAC9B,EAAE;IACF,2BAA2B;IAC3B,IAAI,MAAM,KAAK,MAAM,EAAE;QACrB,IAAA,mBAAQ,EAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;KACvC;SAAM;QACL,KAAK,EAAE,CAAA;KACR;AACH,CAAC,CAAA;AAEM,MAAM,WAAW,GAAG,CACzB,MAAmB,EACnB,GAAoB,EACpB,GAAqB,EACrB,EAAE;IACF,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE;QACjB,MAAM,CAAC,OAAO,GAAG,IAAI,CAAA;QACrB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EAAE,EAAE,CACvD,IAAA,uBAAY,EAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CACrD,CAAA;SACF;KACF;AACH,CAAC,CAAA;AApIY,QAAA,WAAW,eAoIvB"}
@@ -0,0 +1,3 @@
1
+ import { BasedFunctionRoute, HttpClient } from '../../types';
2
+ import { BasedServer } from '../../server';
3
+ export declare const readBody: (server: BasedServer, client: HttpClient, onData: (data: any | void) => void, route: BasedFunctionRoute) => void;
@@ -0,0 +1,112 @@
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.readBody = void 0;
7
+ const node_zlib_1 = __importDefault(require("node:zlib"));
8
+ const send_1 = require("./send");
9
+ const error_1 = require("../../error");
10
+ const MAX_CHUNK_SIZE = 1024 * 1024;
11
+ const UNCOMPRESS_OPTS = {
12
+ // can be endless scince we limit by incoming
13
+ chunkSize: 1024 * 1024 * 1000,
14
+ };
15
+ const readBody = (server, client, onData, route) => {
16
+ if (!client.res) {
17
+ return;
18
+ }
19
+ const contentLen = client.context.headers['content-length'];
20
+ if (contentLen > route.maxPayloadSize) {
21
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.PayloadTooLarge, route);
22
+ return;
23
+ }
24
+ const contentEncoding = client.context.headers['content-encoding'];
25
+ let size = 0;
26
+ if (contentEncoding) {
27
+ let uncompressStream;
28
+ if (contentEncoding === 'deflate') {
29
+ uncompressStream = node_zlib_1.default.createInflate(UNCOMPRESS_OPTS);
30
+ }
31
+ else if (contentEncoding === 'gzip') {
32
+ uncompressStream = node_zlib_1.default.createGunzip(UNCOMPRESS_OPTS);
33
+ }
34
+ else if (contentEncoding === 'br') {
35
+ uncompressStream = node_zlib_1.default.createBrotliDecompress(UNCOMPRESS_OPTS);
36
+ }
37
+ if (uncompressStream) {
38
+ client.res.onData((c, isLast) => {
39
+ size += c.byteLength;
40
+ if (size > route.maxPayloadSize) {
41
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.PayloadTooLarge, route);
42
+ // sendHttpError(client, 'Payload Too Large', 413)
43
+ uncompressStream.destroy();
44
+ return;
45
+ }
46
+ if (c.byteLength > MAX_CHUNK_SIZE) {
47
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.ChunkTooLarge, route);
48
+ uncompressStream.destroy();
49
+ return;
50
+ }
51
+ const buf = Buffer.alloc(c.byteLength, Buffer.from(c));
52
+ if (isLast) {
53
+ uncompressStream.end(buf);
54
+ }
55
+ else {
56
+ if (!uncompressStream.write(buf)) {
57
+ // handle backpressure
58
+ }
59
+ }
60
+ });
61
+ // TODO: NEED TO READ CONTENT LEN OF UNCOMPRESSED....
62
+ const data = new SharedArrayBuffer(contentLen);
63
+ const buf = new Uint8Array(data);
64
+ // let index = 0
65
+ uncompressStream.on('data', (c) => {
66
+ console.info('bytes written???', uncompressStream.bytesWritten, c);
67
+ // FIX LATER
68
+ // const len = c.byteLength
69
+ // for (let i = 0; i < len; i++) {
70
+ // console.log(c[i])
71
+ // worng ofc..
72
+ // maybe unpack in worker? - will not work like this...
73
+ // Atomics.store(buf, index, c[i])
74
+ // }
75
+ // index += len
76
+ });
77
+ uncompressStream.on('end', () => {
78
+ uncompressStream.destroy();
79
+ // parseData(server, client, contentType, data, false, route)
80
+ onData(buf);
81
+ });
82
+ }
83
+ else {
84
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.InvalidPayload, route);
85
+ }
86
+ }
87
+ else {
88
+ const data = new SharedArrayBuffer(contentLen);
89
+ const buf = new Uint8Array(data);
90
+ let index = 0;
91
+ client.res.onData((c, isLast) => {
92
+ const len = c.byteLength;
93
+ size += len;
94
+ if (size > route.maxPayloadSize) {
95
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.PayloadTooLarge, route);
96
+ return;
97
+ }
98
+ if (c.byteLength > MAX_CHUNK_SIZE) {
99
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.ChunkTooLarge, route);
100
+ return;
101
+ }
102
+ buf.set(new Uint8Array(c), index);
103
+ index += len;
104
+ if (isLast) {
105
+ console.info('shared buf time', buf);
106
+ onData(buf);
107
+ }
108
+ });
109
+ }
110
+ };
111
+ exports.readBody = readBody;
112
+ //# sourceMappingURL=readBody.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readBody.js","sourceRoot":"","sources":["../../../src/network/http/readBody.ts"],"names":[],"mappings":";;;;;;AACA,0DAA4B;AAC5B,iCAAsC;AACtC,uCAA4C;AAG5C,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,CAAA;AAElC,MAAM,eAAe,GAAG;IACtB,6CAA6C;IAC7C,SAAS,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI;CAC9B,CAAA;AAEM,MAAM,QAAQ,GAAG,CACtB,MAAmB,EACnB,MAAkB,EAClB,MAAkC,EAClC,KAAyB,EACzB,EAAE;IACF,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;QACf,OAAM;KACP;IAED,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAE3D,IAAI,UAAU,GAAG,KAAK,CAAC,cAAc,EAAE;QACrC,IAAA,oBAAa,EAAC,MAAM,EAAE,MAAM,EAAE,sBAAc,CAAC,eAAe,EAAE,KAAK,CAAC,CAAA;QACpE,OAAM;KACP;IAED,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAA;IAClE,IAAI,IAAI,GAAG,CAAC,CAAA;IAEZ,IAAI,eAAe,EAAE;QACnB,IAAI,gBAA4C,CAAA;QAChD,IAAI,eAAe,KAAK,SAAS,EAAE;YACjC,gBAAgB,GAAG,mBAAI,CAAC,aAAa,CAAC,eAAe,CAAC,CAAA;SACvD;aAAM,IAAI,eAAe,KAAK,MAA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AClE,OAAM;aACP;YAED,GAAG,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;YACjC,KAAK,IAAI,GAAG,CAAA;YAEZ,IAAI,MAAM,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAA;gBACpC,MAAM,CAAC,GAAG,CAAC,CAAA;aACZ;QACH,CAAC,CAAC,CAAA;KACH;AACH,CAAC,CAAA;AA1GY,QAAA,QAAQ,YA0GpB"}
@@ -0,0 +1,5 @@
1
+ import { HttpClient } from '../../types';
2
+ import { BasedErrorCode, ErrorPayload } from '../../error';
3
+ import { BasedServer } from '../../server';
4
+ export declare const sendHttpError: (server: BasedServer, client: HttpClient, basedCode: BasedErrorCode, err: ErrorPayload[BasedErrorCode]) => void;
5
+ export declare const sendHttpResponse: (client: HttpClient, result: any) => void;
@@ -0,0 +1,60 @@
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.sendHttpResponse = exports.sendHttpError = void 0;
7
+ const end_1 = __importDefault(require("./end"));
8
+ const compress_1 = require("./compress");
9
+ const error_1 = require("../../error");
10
+ const sendHttpErrorMessage = (res, error) => {
11
+ const { code, message, statusCode, statusMessage } = error;
12
+ res.writeStatus(`${statusCode} ${statusMessage}`);
13
+ res.writeHeader('Access-Control-Allow-Origin', '*');
14
+ res.writeHeader('Access-Control-Allow-Headers', 'content-type');
15
+ res.writeHeader('Content-Type', 'application/json');
16
+ return JSON.stringify({
17
+ error: message,
18
+ code,
19
+ });
20
+ };
21
+ const sendHttpError = (server, client, basedCode, err) => {
22
+ if (!client.res) {
23
+ return;
24
+ }
25
+ client.res.cork(() => {
26
+ (0, end_1.default)(client, sendHttpErrorMessage(client.res, (0, error_1.createError)(server, client, basedCode, err)));
27
+ });
28
+ };
29
+ exports.sendHttpError = sendHttpError;
30
+ const sendHttpResponse = (client, result) => {
31
+ if (!client.res) {
32
+ return;
33
+ }
34
+ let cType;
35
+ // for functions there is never cache (idea is they are used to execute - observable fns are for cache)
36
+ let parsed;
37
+ if (typeof result === 'string') {
38
+ cType = 'text/plain';
39
+ parsed = result;
40
+ }
41
+ else {
42
+ cType = 'application/json';
43
+ parsed = JSON.stringify(result);
44
+ }
45
+ (0, compress_1.compress)(client, parsed).then(({ payload, encoding }) => {
46
+ if (client.res) {
47
+ client.res.cork(() => {
48
+ client.res.writeStatus('200 OK');
49
+ client.res.writeHeader('Cache-Control', 'max-age=0, must-revalidate');
50
+ client.res.writeHeader('Content-Type', cType);
51
+ if (encoding) {
52
+ client.res.writeHeader('Content-Encoding', encoding);
53
+ }
54
+ (0, end_1.default)(client, payload);
55
+ });
56
+ }
57
+ });
58
+ };
59
+ exports.sendHttpResponse = sendHttpResponse;
60
+ //# sourceMappingURL=send.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.js","sourceRoot":"","sources":["../../../src/network/http/send.ts"],"names":[],"mappings":";;;;;;AAEA,gDAAuB;AACvB,yCAAqC;AACrC,uCAKoB;AAGpB,MAAM,oBAAoB,GAAG,CAC3B,GAAqB,EACrB,KAAqB,EACb,EAAE;IACV,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,GAAG,KAAK,CAAA;IAC1D,GAAG,CAAC,WAAW,CAAC,GAAG,UAAU,IAAI,aAAa,EAAE,CAAC,CAAA;IACjD,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;IACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;IAC/D,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;IACnD,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,KAAK,EAAE,OAAO;QACd,IAAI;KACL,CAAC,CAAA;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AAc,EAAE,KAAK,CAAC,CAAA;gBAC7C,IAAI,QAAQ,EAAE;oBACZ,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAA;iBACrD;gBACD,IAAA,aAAG,EAAC,MAAM,EAAE,OAAO,CAAC,CAAA;YACtB,CAAC,CAAC,CAAA;SACH;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AA7BY,QAAA,gBAAgB,oBA6B5B"}
@@ -0,0 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { Duplex } from 'stream';
3
+ export declare class DataStream extends Duplex {
4
+ _read(): void;
5
+ _write(chunk: any, encoding: any, callback: any): void;
6
+ _final(): void;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataStream = void 0;
4
+ const stream_1 = require("stream");
5
+ // copy / ;/ or shall we just run stream functions on the main thread
6
+ // how to send to worker...
7
+ class DataStream extends stream_1.Duplex {
8
+ _read() { }
9
+ // writableHighWaterMark:
10
+ _write(chunk, encoding, callback) {
11
+ this.push(Buffer.from(chunk, encoding));
12
+ callback();
13
+ }
14
+ _final() {
15
+ this.push(null);
16
+ }
17
+ }
18
+ exports.DataStream = DataStream;
19
+ //# sourceMappingURL=DataStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DataStream.js","sourceRoot":"","sources":["../../../../src/network/http/streamFunction/DataStream.ts"],"names":[],"mappings":";;;AAAA,mCAA+B;AAE/B,qEAAqE;AAErE,2BAA2B;AAC3B,MAAa,UAAW,SAAQ,eAAM;IACpC,KAAK,KAAI,CAAC;IAEV,yBAAyB;IACzB,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ;QAC9B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAA;QACvC,QAAQ,EAAE,CAAA;IACZ,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACjB,CAAC;CACF;AAZD,gCAYC"}
@@ -0,0 +1,2 @@
1
+ declare const _default: (mimeType: string) => any;
2
+ export default _default;
@@ -0,0 +1,29 @@
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 mime_types_1 = __importDefault(require("mime-types"));
7
+ exports.default = (mimeType) => {
8
+ const [mime, type] = mimeType.split('/');
9
+ if (mime === 'font') {
10
+ return 'woff';
11
+ }
12
+ else if (type === 'mp3') {
13
+ return 'mp3';
14
+ }
15
+ else if (type === 'woff2') {
16
+ return 'woff2';
17
+ }
18
+ else {
19
+ const t = mime_types_1.default.extension(mimeType);
20
+ if (t === 'markdown') {
21
+ return 'md';
22
+ }
23
+ if (t === 'qt') {
24
+ return 'mov';
25
+ }
26
+ return t;
27
+ }
28
+ };
29
+ //# sourceMappingURL=getExtension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getExtension.js","sourceRoot":"","sources":["../../../../src/network/http/streamFunction/getExtension.ts"],"names":[],"mappings":";;;;;AAAA,4DAAkC;AAElC,kBAAe,CAAC,QAAgB,EAAE,EAAE;IAClC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACxC,IAAI,IAAI,KAAK,MAAM,EAAE;QACnB,OAAO,MAAM,CAAA;KACd;SAAM,IAAI,IAAI,KAAK,KAAK,EAAE;QACzB,OAAO,KAAK,CAAA;KACb;SAAM,IAAI,IAAI,KAAK,OAAO,EAAE;QAC3B,OAAO,OAAO,CAAA;KACf;SAAM;QACL,MAAM,CAAC,GAAG,oBAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;QACvC,IAAI,CAAC,KAAK,UAAU,EAAE;YACpB,OAAO,IAAI,CAAA;SACZ;QACD,IAAI,CAAC,KAAK,IAAI,EAAE;YACd,OAAO,KAAK,CAAA;SACb;QACD,OAAO,CAAC,CAAA;KACT;AACH,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { BasedServer } from '../../../server';
2
+ import { BasedFunctionRoute, HttpClient } from '../../../types';
3
+ export declare const httpStreamFunction: (server: BasedServer, client: HttpClient, payload: any, route: BasedFunctionRoute) => void;
@@ -0,0 +1,81 @@
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.httpStreamFunction = void 0;
7
+ const stream_1 = __importDefault(require("./stream"));
8
+ const send_1 = require("../send");
9
+ const types_1 = require("../../../types");
10
+ const authorize_1 = require("../authorize");
11
+ const error_1 = require("../../../error");
12
+ const multipartStream_1 = __importDefault(require("./multipartStream"));
13
+ // TODO: move to workers....
14
+ const httpStreamFunction = (server, client, payload, route) => {
15
+ if (!client.res) {
16
+ return;
17
+ }
18
+ const size = client.context.headers['content-length'];
19
+ if (route.maxPayloadSize > -1 && route.maxPayloadSize < size) {
20
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.PayloadTooLarge, route);
21
+ return;
22
+ }
23
+ const type = client.context.headers['content-type'];
24
+ // replace this with transder encoding 'chunked'
25
+ if (type && type.startsWith('multipart/form-data')) {
26
+ (0, authorize_1.authorizeRequest)(server, client, payload, route, (payload) => {
27
+ server.functions
28
+ .install(route.name)
29
+ .then((spec) => {
30
+ if (spec && !(0, types_1.isObservableFunctionSpec)(spec) && spec.stream) {
31
+ (0, multipartStream_1.default)(client, server, payload, route, spec);
32
+ }
33
+ else {
34
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.FunctionNotFound, route);
35
+ }
36
+ })
37
+ .catch(() => {
38
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.FunctionNotFound, route);
39
+ });
40
+ });
41
+ return;
42
+ }
43
+ // destroy stream from context
44
+ (0, authorize_1.authorizeRequest)(server, client, payload, route, (payload) => {
45
+ server.functions
46
+ .install(route.name)
47
+ .then((spec) => {
48
+ if (spec && !(0, types_1.isObservableFunctionSpec)(spec) && spec.stream) {
49
+ const stream = (0, stream_1.default)(server, route, client, size);
50
+ const streamPayload = { payload, stream };
51
+ const fn = require(spec.functionPath);
52
+ fn(streamPayload, client.context)
53
+ .catch((err) => {
54
+ stream.destroy();
55
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.FunctionError, {
56
+ err,
57
+ name: route.name,
58
+ });
59
+ })
60
+ .then((r) => {
61
+ if (stream.readableEnded) {
62
+ (0, send_1.sendHttpResponse)(client, r);
63
+ }
64
+ else {
65
+ stream.once('end', () => {
66
+ (0, send_1.sendHttpResponse)(client, r);
67
+ });
68
+ }
69
+ });
70
+ }
71
+ else {
72
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.FunctionNotFound, route);
73
+ }
74
+ })
75
+ .catch(() => {
76
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.FunctionNotFound, route);
77
+ });
78
+ });
79
+ };
80
+ exports.httpStreamFunction = httpStreamFunction;
81
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/network/http/streamFunction/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAuC;AAEvC,kCAAyD;AACzD,0CAIuB;AACvB,4CAA+C;AAC/C,0CAA+C;AAC/C,wEAA+C;AAE/C,4BAA4B;AAErB,MAAM,kBAAkB,GAAG,CAChC,MAAmB,EACnB,MAAkB,EAClB,OAAY,EACZ,KAAyB,EACzB,EAAE;IACF,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;QACf,OAAM;KACP;IAED,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAErD,IAAI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AAK,CAAC,CAAA;QACvE,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AA7EY,QAAA,kBAAkB,sBA6E9B"}
@@ -0,0 +1,12 @@
1
+ import { HttpClient, BasedFunctionSpec, BasedFunctionRoute } from '../../../types';
2
+ import { BasedServer } from '../../../server';
3
+ export declare type FileOptions = {
4
+ name?: string;
5
+ size?: number;
6
+ type: string;
7
+ extension: string;
8
+ } & {
9
+ [key: string]: string;
10
+ };
11
+ declare const _default: (client: HttpClient, server: BasedServer, payload: any, route: BasedFunctionRoute, spec: BasedFunctionSpec) => Promise<void>;
12
+ export default _default;