@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
@@ -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,CAA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,4BAA4B,CAAC,CAAA;gBACrE,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,cAAc,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,M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oBAAa,EAAC,MAAM,EAAE,MAAM,EAAE,sBAAc,CAAC,gBAAgB,EAAE,KAAK,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;
@@ -0,0 +1,208 @@
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 DataStream_1 = require("./DataStream");
7
+ const error_1 = require("../../../error");
8
+ const send_1 = require("../send");
9
+ const getExtension_1 = __importDefault(require("./getExtension"));
10
+ const MAX_CHUNK_SIZE = 1024 * 1024 * 5;
11
+ const streamProgress = (stream, size) => {
12
+ stream.emit('progress', 0);
13
+ if (size < 200000) {
14
+ stream.on('end', () => {
15
+ stream.emit('progress', 1);
16
+ });
17
+ }
18
+ else {
19
+ let progress = 0;
20
+ let total = 0;
21
+ let setInProgress = false;
22
+ const updateProgress = () => {
23
+ if (!setInProgress) {
24
+ setInProgress = true;
25
+ setTimeout(() => {
26
+ stream.emit('progress', progress);
27
+ setInProgress = false;
28
+ }, 250);
29
+ }
30
+ };
31
+ stream.on('end', () => {
32
+ progress = 1;
33
+ updateProgress();
34
+ });
35
+ stream.on('data', (chunk) => {
36
+ total += chunk.byteLength;
37
+ progress = total / size;
38
+ updateProgress();
39
+ });
40
+ }
41
+ };
42
+ // only use this if you have individual file else its just all
43
+ const setHeader = (file) => {
44
+ file.headersSet++;
45
+ if (file.headersSet === 2) {
46
+ return true;
47
+ }
48
+ return false;
49
+ };
50
+ const toBuffer = (str, firstWritten) => {
51
+ return Buffer.from(firstWritten ? str + '\r\n' : str, 'binary');
52
+ };
53
+ exports.default = async (client, server, payload, route, spec) => {
54
+ if (!payload || (!payload && typeof payload !== 'object')) {
55
+ payload = {};
56
+ }
57
+ const fn = require(spec.functionPath);
58
+ const files = [];
59
+ const contentLength = client.context.headers['content-length'];
60
+ const promiseQ = [];
61
+ let setInProgress = false;
62
+ let boundary = null;
63
+ let prevLine;
64
+ let isWriting = false;
65
+ let total = 0;
66
+ let progress = 0;
67
+ client.res.onData((chunk, isLast) => {
68
+ // see if this goes ok... (clearing mem etc)
69
+ if (chunk.byteLength > MAX_CHUNK_SIZE) {
70
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.ChunkTooLarge, route);
71
+ for (const file of files) {
72
+ file.stream.destroy();
73
+ }
74
+ return;
75
+ }
76
+ let firstWritten = false;
77
+ const blocks = Buffer.from(chunk).toString('binary').split('\r\n');
78
+ total += chunk.byteLength;
79
+ progress = total / contentLength;
80
+ for (const file of files) {
81
+ if (file.headersSet > 1 && !file.isDone && !file.opts.size) {
82
+ if (contentLength > 200000) {
83
+ if (!setInProgress) {
84
+ setInProgress = true;
85
+ setTimeout(() => {
86
+ for (const file of files) {
87
+ if (file.headersSet > 1 && !file.isDone && !file.opts.size) {
88
+ if (progress === 1) {
89
+ file.isDone = true;
90
+ }
91
+ file.stream.emit('progress', progress);
92
+ }
93
+ }
94
+ setInProgress = false;
95
+ }, 250);
96
+ }
97
+ break;
98
+ }
99
+ }
100
+ }
101
+ if (!boundary) {
102
+ boundary = blocks[0];
103
+ }
104
+ for (let i = 0; i < blocks.length; i++) {
105
+ const line = blocks[i];
106
+ if (!boundary) {
107
+ continue;
108
+ }
109
+ if (isWriting && (line === boundary || line === boundary + '--')) {
110
+ isWriting = false;
111
+ const file = files[files.length - 1];
112
+ if (prevLine) {
113
+ file.stream.end(toBuffer(prevLine, firstWritten));
114
+ }
115
+ else {
116
+ file.stream.end();
117
+ }
118
+ if (!file.opts.size && !file.isDone) {
119
+ file.stream.emit('progress', 1);
120
+ }
121
+ file.isDone = true;
122
+ prevLine = null;
123
+ if (line === boundary + '--') {
124
+ continue;
125
+ }
126
+ }
127
+ if (line === boundary && !isWriting) {
128
+ const file = {
129
+ stream: new DataStream_1.DataStream(),
130
+ headersSet: 0,
131
+ opts: {},
132
+ isDone: false,
133
+ };
134
+ files.push(file);
135
+ continue;
136
+ }
137
+ const file = files[files.length - 1];
138
+ if (!file) {
139
+ // TODO: invalid file
140
+ return (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.InvalidPayload, route);
141
+ }
142
+ if (!isWriting && line.includes('Content-Disposition')) {
143
+ const meta = line.match(/name="(.*?)"/)?.[1];
144
+ if (!meta) {
145
+ // TODO: invalid file
146
+ return (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.InvalidPayload, route);
147
+ }
148
+ const opts = file.opts;
149
+ opts.name = line.match(/filename="(.*?)"/)?.[1] || 'untitled';
150
+ const disposition = meta.split('|');
151
+ for (const seg of disposition) {
152
+ if (/=/.test(seg)) {
153
+ const [k, v] = seg.split('=');
154
+ if (k === 'size') {
155
+ opts[k] = Number(v);
156
+ }
157
+ else {
158
+ opts[k] = v;
159
+ }
160
+ }
161
+ }
162
+ if (opts.size) {
163
+ streamProgress(file.stream, opts.size);
164
+ }
165
+ isWriting = setHeader(file);
166
+ if (isWriting) {
167
+ promiseQ.push(fn({ payload: { ...payload, ...file.opts }, stream: file.stream }, client.context));
168
+ }
169
+ continue;
170
+ }
171
+ if (!isWriting && line.includes('Content-Type')) {
172
+ const mimeType = line.match(/Content-Type: ([a-zA-Z0-9].+\/[a-zA-Z0-9].+)/)?.[1];
173
+ if (!mimeType) {
174
+ // TODO: invalid file (can speficy in route potentialy...)
175
+ return (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.InvalidPayload, route);
176
+ }
177
+ file.opts.type = mimeType;
178
+ file.opts.extension = (0, getExtension_1.default)(mimeType);
179
+ isWriting = setHeader(file);
180
+ if (isWriting) {
181
+ promiseQ.push(fn({ payload: { ...payload, ...file.opts }, stream: file.stream }, client.context));
182
+ }
183
+ continue;
184
+ }
185
+ if (isWriting) {
186
+ if (prevLine) {
187
+ file.stream.write(toBuffer(prevLine, firstWritten));
188
+ }
189
+ prevLine = line;
190
+ firstWritten = true;
191
+ }
192
+ }
193
+ if (isLast) {
194
+ Promise.allSettled(promiseQ).then((results) => {
195
+ const r = results.map((v) => {
196
+ if (v.status === 'rejected') {
197
+ return { err: v.reason };
198
+ }
199
+ else {
200
+ return v.value;
201
+ }
202
+ });
203
+ (0, send_1.sendHttpResponse)(client, r);
204
+ });
205
+ }
206
+ });
207
+ };
208
+ //# sourceMappingURL=multipartStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multipartStream.js","sourceRoot":"","sources":["../../../../src/network/http/streamFunction/multipartStream.ts"],"names":[],"mappings":";;;;;AAAA,6CAAyC;AAMzC,0CAA+C;AAC/C,kCAAyD;AACzD,kEAAyC;AAGzC,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC,CAAA;AAgBtC,MAAM,cAAc,GAAG,CAAC,MAAkB,EAAE,IAAY,EAAE,EAAE;IAC1D,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;IAC1B,IAAI,IAAI,GAAG,MAAM,EAAE;QACjB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YACpB,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;QAC5B,CAAC,CAAC,CAAA;KACH;SAAM;QACL,IAAI,QAAQ,GAAG,CAAC,CAAA;QAChB,IAAI,KAAK,GAAG,CAAC,CAAA;QACb,IAAI,aAAa,GAAG,KAAK,CAAA;QACzB,MAAM,cAAc,GAAG,GAAG,EA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ute } from '../../../types';
3
+ import { BasedServer } from '../../../server';
4
+ declare const _default: (server: BasedServer, route: BasedFunctionRoute, client: HttpClient, size: number) => DataStream;
5
+ export default _default;
@@ -0,0 +1,103 @@
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 DataStream_1 = require("./DataStream");
7
+ const send_1 = require("../send");
8
+ const node_zlib_1 = __importDefault(require("node:zlib"));
9
+ const error_1 = require("../../../error");
10
+ const MAX_CHUNK_SIZE = 1024 * 1024 * 5;
11
+ const UNCOMPRESS_OPTS = {
12
+ // can be endless scince we limit by incoming
13
+ chunkSize: 1024 * 1024 * 100,
14
+ };
15
+ exports.default = (server, route, client, size) => {
16
+ const stream = new DataStream_1.DataStream();
17
+ let total = 0;
18
+ let progress = 0;
19
+ let setInProgress = false;
20
+ stream.emit('progress', progress);
21
+ const emitProgress = size > 200000;
22
+ const contentEncoding = client.context.headers['content-encoding'];
23
+ if (contentEncoding) {
24
+ let uncompressStream;
25
+ if (contentEncoding === 'deflate') {
26
+ uncompressStream = node_zlib_1.default.createInflate(UNCOMPRESS_OPTS);
27
+ }
28
+ else if (contentEncoding === 'gzip') {
29
+ uncompressStream = node_zlib_1.default.createGunzip(UNCOMPRESS_OPTS);
30
+ }
31
+ else if (contentEncoding === 'br') {
32
+ uncompressStream = node_zlib_1.default.createBrotliDecompress(UNCOMPRESS_OPTS);
33
+ }
34
+ if (uncompressStream) {
35
+ client.res.onData((c, isLast) => {
36
+ total += c.byteLength;
37
+ if (c.byteLength > MAX_CHUNK_SIZE) {
38
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.ChunkTooLarge, route);
39
+ uncompressStream.destroy();
40
+ stream.destroy();
41
+ return;
42
+ }
43
+ const buf = Buffer.alloc(c.byteLength, Buffer.from(c));
44
+ if (emitProgress) {
45
+ progress = total / size;
46
+ if (!setInProgress) {
47
+ setInProgress = true;
48
+ setTimeout(() => {
49
+ stream.emit('progress', progress);
50
+ setInProgress = false;
51
+ }, 250);
52
+ }
53
+ }
54
+ if (isLast) {
55
+ if (!emitProgress) {
56
+ stream.emit('progress', 1);
57
+ }
58
+ uncompressStream.end(buf);
59
+ }
60
+ else {
61
+ if (!uncompressStream.write(buf)) {
62
+ // handle backpressure
63
+ }
64
+ }
65
+ });
66
+ uncompressStream.pipe(stream);
67
+ }
68
+ else {
69
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.UnsupportedContentEncoding, route);
70
+ }
71
+ }
72
+ else {
73
+ client.res.onData((c, isLast) => {
74
+ total += c.byteLength;
75
+ if (c.byteLength > MAX_CHUNK_SIZE) {
76
+ (0, send_1.sendHttpError)(server, client, error_1.BasedErrorCode.ChunkTooLarge, route);
77
+ stream.destroy();
78
+ return;
79
+ }
80
+ if (emitProgress) {
81
+ progress = total / size;
82
+ if (!setInProgress) {
83
+ setInProgress = true;
84
+ setTimeout(() => {
85
+ stream.emit('progress', progress);
86
+ setInProgress = false;
87
+ }, 250);
88
+ }
89
+ }
90
+ if (isLast) {
91
+ if (!emitProgress) {
92
+ stream.emit('progress', 1);
93
+ }
94
+ stream.end(Buffer.from(c));
95
+ }
96
+ else {
97
+ stream.write(Buffer.from(c));
98
+ }
99
+ });
100
+ }
101
+ return stream;
102
+ };
103
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../../src/network/http/streamFunction/stream.ts"],"names":[],"mappings":";;;;;AAAA,6CAAyC;AAEzC,kCAAuC;AACvC,0DAA4B;AAC5B,0CAA+C;AAG/C,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC,CAAA;AAEtC,MAAM,eAAe,GAAG;IACtB,6CAA6C;IAC7C,SAAS,EAAE,IAAI,GAAG,IAAI,GAAG,GAAG;CAC7B,CAAA;AAED,kBAAe,CACb,MAAmB,E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AAE;oBAClB,aAAa,GAAG,IAAI,CAAA;oBACpB,UAAU,CAAC,GAAG,EAAE;wBACd,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAA;wBACjC,aAAa,GAAG,KAAK,CAAA;oBACvB,CAAC,EAAE,GAAG,CAAC,CAAA;iBACR;aACF;YACD,IAAI,MAAM,EAAE;gBACV,IAAI,CAAC,YAAY,EAAE;oBACjB,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;iBAC3B;gBACD,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;aAC3B;iBAAM;gBACL,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;aAC7B;QACH,CAAC,CAAC,CAAA;KACH;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ import type { ServerOptions } from '../types';
2
+ import type { BasedServer } from '../server';
3
+ declare const _default: (server: BasedServer, { key, cert, port }: ServerOptions) => void;
4
+ export default _default;