@based/server 3.3.6 → 3.4.0

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 (308) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/dist/BasedServerClient.d.ts +0 -0
  3. package/dist/BasedServerClient.js +0 -0
  4. package/dist/BasedServerClient.js.map +0 -0
  5. package/dist/Client.d.ts +0 -0
  6. package/dist/Client.js +0 -0
  7. package/dist/Client.js.map +0 -0
  8. package/dist/Params.d.ts +0 -0
  9. package/dist/Params.js +0 -0
  10. package/dist/Params.js.map +0 -0
  11. package/dist/auth/authorize.d.ts +2 -0
  12. package/dist/auth/authorize.js +8 -0
  13. package/dist/auth/authorize.js.map +1 -0
  14. package/dist/auth/authorizeAdvanced.d.ts +2 -0
  15. package/dist/auth/authorizeAdvanced.js +10 -0
  16. package/dist/auth/authorizeAdvanced.js.map +1 -0
  17. package/dist/auth/defaults/authorize.d.ts +2 -0
  18. package/dist/auth/defaults/authorize.js +8 -0
  19. package/dist/auth/defaults/authorize.js.map +1 -0
  20. package/dist/auth/defaults/authorizeAdvanced.d.ts +2 -0
  21. package/dist/auth/defaults/authorizeAdvanced.js +8 -0
  22. package/dist/auth/defaults/authorizeAdvanced.js.map +1 -0
  23. package/dist/auth/defaults/index.d.ts +2 -0
  24. package/dist/auth/defaults/index.js +19 -0
  25. package/dist/auth/defaults/index.js.map +1 -0
  26. package/dist/auth/index.d.ts +10 -0
  27. package/dist/auth/index.js +23 -0
  28. package/dist/auth/index.js.map +1 -0
  29. package/dist/closeListener.d.ts +0 -0
  30. package/dist/closeListener.js +0 -0
  31. package/dist/closeListener.js.map +0 -0
  32. package/dist/error.d.ts +34 -0
  33. package/dist/error.js +107 -0
  34. package/dist/error.js.map +1 -0
  35. package/dist/findPrefix.d.ts +0 -0
  36. package/dist/findPrefix.js +0 -0
  37. package/dist/findPrefix.js.map +0 -0
  38. package/dist/functions/index.d.ts +31 -0
  39. package/dist/functions/index.js +200 -0
  40. package/dist/functions/index.js.map +1 -0
  41. package/dist/functions/timeout.d.ts +3 -0
  42. package/dist/functions/timeout.js +23 -0
  43. package/dist/functions/timeout.js.map +1 -0
  44. package/dist/getFromConfig.d.ts +0 -0
  45. package/dist/getFromConfig.js +0 -0
  46. package/dist/getFromConfig.js.map +0 -0
  47. package/dist/handlers/authorize.d.ts +0 -0
  48. package/dist/handlers/authorize.js +0 -0
  49. package/dist/handlers/authorize.js.map +0 -0
  50. package/dist/handlers/bulkUpdate/index.d.ts +0 -0
  51. package/dist/handlers/bulkUpdate/index.js +0 -0
  52. package/dist/handlers/bulkUpdate/index.js.map +0 -0
  53. package/dist/handlers/configuration/index.d.ts +0 -0
  54. package/dist/handlers/configuration/index.js +0 -0
  55. package/dist/handlers/configuration/index.js.map +0 -0
  56. package/dist/handlers/configuration/observable/index.d.ts +0 -0
  57. package/dist/handlers/configuration/observable/index.js +0 -0
  58. package/dist/handlers/configuration/observable/index.js.map +0 -0
  59. package/dist/handlers/configure/index.d.ts +0 -0
  60. package/dist/handlers/configure/index.js +0 -0
  61. package/dist/handlers/configure/index.js.map +0 -0
  62. package/dist/handlers/copy/index.d.ts +0 -0
  63. package/dist/handlers/copy/index.js +0 -0
  64. package/dist/handlers/copy/index.js.map +0 -0
  65. package/dist/handlers/delete/index.d.ts +0 -0
  66. package/dist/handlers/delete/index.js +0 -0
  67. package/dist/handlers/delete/index.js.map +0 -0
  68. package/dist/handlers/digest/index.d.ts +0 -0
  69. package/dist/handlers/digest/index.js +0 -0
  70. package/dist/handlers/digest/index.js.map +0 -0
  71. package/dist/handlers/functions/call.d.ts +0 -0
  72. package/dist/handlers/functions/call.js +0 -0
  73. package/dist/handlers/functions/call.js.map +0 -0
  74. package/dist/handlers/functions/observable/Observable.d.ts +0 -0
  75. package/dist/handlers/functions/observable/Observable.js +0 -0
  76. package/dist/handlers/functions/observable/Observable.js.map +0 -0
  77. package/dist/handlers/functions/observable/SharedObservable.d.ts +0 -0
  78. package/dist/handlers/functions/observable/SharedObservable.js +0 -0
  79. package/dist/handlers/functions/observable/SharedObservable.js.map +0 -0
  80. package/dist/handlers/functions/observable/index.d.ts +0 -0
  81. package/dist/handlers/functions/observable/index.js +0 -0
  82. package/dist/handlers/functions/observable/index.js.map +0 -0
  83. package/dist/handlers/get/index.d.ts +0 -0
  84. package/dist/handlers/get/index.js +0 -0
  85. package/dist/handlers/get/index.js.map +0 -0
  86. package/dist/handlers/getConfig/index.d.ts +0 -0
  87. package/dist/handlers/getConfig/index.js +0 -0
  88. package/dist/handlers/getConfig/index.js.map +0 -0
  89. package/dist/handlers/handleRequests.d.ts +0 -0
  90. package/dist/handlers/handleRequests.js +0 -0
  91. package/dist/handlers/handleRequests.js.map +0 -0
  92. package/dist/handlers/index.d.ts +0 -0
  93. package/dist/handlers/index.js +0 -0
  94. package/dist/handlers/index.js.map +0 -0
  95. package/dist/handlers/messageParser.d.ts +0 -0
  96. package/dist/handlers/messageParser.js +0 -0
  97. package/dist/handlers/messageParser.js.map +0 -0
  98. package/dist/handlers/removeField/index.d.ts +0 -0
  99. package/dist/handlers/removeField/index.js +0 -0
  100. package/dist/handlers/removeField/index.js.map +0 -0
  101. package/dist/handlers/removeType/index.d.ts +0 -0
  102. package/dist/handlers/removeType/index.js +0 -0
  103. package/dist/handlers/removeType/index.js.map +0 -0
  104. package/dist/handlers/rest/file/DataStream.d.ts +0 -0
  105. package/dist/handlers/rest/file/DataStream.js +0 -0
  106. package/dist/handlers/rest/file/DataStream.js.map +0 -0
  107. package/dist/handlers/rest/file/formStream.d.ts +0 -0
  108. package/dist/handlers/rest/file/formStream.js +0 -0
  109. package/dist/handlers/rest/file/formStream.js.map +0 -0
  110. package/dist/handlers/rest/file/getExtenstion.d.ts +0 -0
  111. package/dist/handlers/rest/file/getExtenstion.js +0 -0
  112. package/dist/handlers/rest/file/getExtenstion.js.map +0 -0
  113. package/dist/handlers/rest/file/index.d.ts +0 -0
  114. package/dist/handlers/rest/file/index.js +0 -0
  115. package/dist/handlers/rest/file/index.js.map +0 -0
  116. package/dist/handlers/rest/file/storeFile.d.ts +0 -0
  117. package/dist/handlers/rest/file/storeFile.js +0 -0
  118. package/dist/handlers/rest/file/storeFile.js.map +0 -0
  119. package/dist/handlers/rest/file/stream.d.ts +0 -0
  120. package/dist/handlers/rest/file/stream.js +0 -0
  121. package/dist/handlers/rest/file/stream.js.map +0 -0
  122. package/dist/handlers/rest/file/types.d.ts +0 -0
  123. package/dist/handlers/rest/file/types.js +0 -0
  124. package/dist/handlers/rest/file/types.js.map +0 -0
  125. package/dist/handlers/rest/getReqMessage.d.ts +0 -0
  126. package/dist/handlers/rest/getReqMessage.js +0 -0
  127. package/dist/handlers/rest/getReqMessage.js.map +0 -0
  128. package/dist/handlers/rest/index.d.ts +0 -0
  129. package/dist/handlers/rest/index.js +0 -0
  130. package/dist/handlers/rest/index.js.map +0 -0
  131. package/dist/handlers/rest/invalidReq.d.ts +0 -0
  132. package/dist/handlers/rest/invalidReq.js +0 -0
  133. package/dist/handlers/rest/invalidReq.js.map +0 -0
  134. package/dist/handlers/rest/parseResponse.d.ts +0 -0
  135. package/dist/handlers/rest/parseResponse.js +0 -0
  136. package/dist/handlers/rest/parseResponse.js.map +0 -0
  137. package/dist/handlers/rest/playground/index.d.ts +0 -0
  138. package/dist/handlers/rest/playground/index.js +0 -0
  139. package/dist/handlers/rest/playground/index.js.map +0 -0
  140. package/dist/handlers/rest/readBody.d.ts +0 -0
  141. package/dist/handlers/rest/readBody.js +0 -0
  142. package/dist/handlers/rest/readBody.js.map +0 -0
  143. package/dist/handlers/set/index.d.ts +0 -0
  144. package/dist/handlers/set/index.js +0 -0
  145. package/dist/handlers/set/index.js.map +0 -0
  146. package/dist/handlers/subscription/index.d.ts +0 -0
  147. package/dist/handlers/subscription/index.js +0 -0
  148. package/dist/handlers/subscription/index.js.map +0 -0
  149. package/dist/handlers/token.d.ts +0 -0
  150. package/dist/handlers/token.js +0 -0
  151. package/dist/handlers/token.js.map +0 -0
  152. package/dist/handlers/track/index.d.ts +0 -0
  153. package/dist/handlers/track/index.js +0 -0
  154. package/dist/handlers/track/index.js.map +0 -0
  155. package/dist/handlers/userAuth/index.d.ts +0 -0
  156. package/dist/handlers/userAuth/index.js +0 -0
  157. package/dist/handlers/userAuth/index.js.map +0 -0
  158. package/dist/index.d.ts +0 -0
  159. package/dist/index.js +0 -0
  160. package/dist/index.js.map +0 -0
  161. package/dist/network/http/authorize.d.ts +3 -0
  162. package/dist/network/http/authorize.js +30 -0
  163. package/dist/network/http/authorize.js.map +1 -0
  164. package/dist/network/http/compress.d.ts +6 -0
  165. package/dist/network/http/compress.js +47 -0
  166. package/dist/network/http/compress.js.map +1 -0
  167. package/dist/network/http/end.d.ts +4 -0
  168. package/dist/network/http/end.js +19 -0
  169. package/dist/network/http/end.js.map +1 -0
  170. package/dist/network/http/function/index.d.ts +3 -0
  171. package/dist/network/http/function/index.js +82 -0
  172. package/dist/network/http/function/index.js.map +1 -0
  173. package/dist/network/http/function/stream/DataStream.d.ts +7 -0
  174. package/dist/network/http/function/stream/DataStream.js +17 -0
  175. package/dist/network/http/function/stream/DataStream.js.map +1 -0
  176. package/dist/network/http/function/stream/index.d.ts +8 -0
  177. package/dist/network/http/function/stream/index.js +93 -0
  178. package/dist/network/http/function/stream/index.js.map +1 -0
  179. package/dist/network/http/function/stream/multipartFormStream.d.ts +0 -0
  180. package/dist/network/http/function/stream/multipartFormStream.js +194 -0
  181. package/dist/network/http/function/stream/multipartFormStream.js.map +1 -0
  182. package/dist/network/http/function/stream/stream.d.ts +4 -0
  183. package/dist/network/http/function/stream/stream.js +49 -0
  184. package/dist/network/http/function/stream/stream.js.map +1 -0
  185. package/dist/network/http/function.d.ts +3 -0
  186. package/dist/network/http/function.js +46 -0
  187. package/dist/network/http/function.js.map +1 -0
  188. package/dist/network/http/get.d.ts +3 -0
  189. package/dist/network/http/get.js +143 -0
  190. package/dist/network/http/get.js.map +1 -0
  191. package/dist/network/http/index.d.ts +3 -0
  192. package/dist/network/http/index.js +125 -0
  193. package/dist/network/http/index.js.map +1 -0
  194. package/dist/network/http/readBody.d.ts +2 -0
  195. package/dist/network/http/readBody.js +134 -0
  196. package/dist/network/http/readBody.js.map +1 -0
  197. package/dist/network/http/readPostData.d.ts +3 -0
  198. package/dist/network/http/readPostData.js +50 -0
  199. package/dist/network/http/readPostData.js.map +1 -0
  200. package/dist/network/http/send.d.ts +6 -0
  201. package/dist/network/http/send.js +65 -0
  202. package/dist/network/http/send.js.map +1 -0
  203. package/dist/network/http/sendError.d.ts +4 -0
  204. package/dist/network/http/sendError.js +27 -0
  205. package/dist/network/http/sendError.js.map +1 -0
  206. package/dist/network/http/stream/DataStream.d.ts +7 -0
  207. package/dist/network/http/stream/DataStream.js +17 -0
  208. package/dist/network/http/stream/DataStream.js.map +1 -0
  209. package/dist/network/http/stream/formStream.d.ts +4 -0
  210. package/dist/network/http/stream/formStream.js +184 -0
  211. package/dist/network/http/stream/formStream.js.map +1 -0
  212. package/dist/network/http/stream/getExtenstion.d.ts +2 -0
  213. package/dist/network/http/stream/getExtenstion.js +22 -0
  214. package/dist/network/http/stream/getExtenstion.js.map +1 -0
  215. package/dist/network/http/stream/index.d.ts +9 -0
  216. package/dist/network/http/stream/index.js +42 -0
  217. package/dist/network/http/stream/index.js.map +1 -0
  218. package/dist/network/http/stream/multipartFormStream.d.ts +0 -0
  219. package/dist/network/http/stream/multipartFormStream.js +194 -0
  220. package/dist/network/http/stream/multipartFormStream.js.map +1 -0
  221. package/dist/network/http/stream/storeFile.d.ts +5 -0
  222. package/dist/network/http/stream/storeFile.js +30 -0
  223. package/dist/network/http/stream/storeFile.js.map +1 -0
  224. package/dist/network/http/stream/stream.d.ts +4 -0
  225. package/dist/network/http/stream/stream.js +49 -0
  226. package/dist/network/http/stream/stream.js.map +1 -0
  227. package/dist/network/http/stream/types.d.ts +9 -0
  228. package/dist/network/http/stream/types.js +3 -0
  229. package/dist/network/http/stream/types.js.map +1 -0
  230. package/dist/network/http/streamFunction/DataStream.d.ts +7 -0
  231. package/dist/network/http/streamFunction/DataStream.js +17 -0
  232. package/dist/network/http/streamFunction/DataStream.js.map +1 -0
  233. package/dist/network/http/streamFunction/endStreamRequest.d.ts +3 -0
  234. package/dist/network/http/streamFunction/endStreamRequest.js +20 -0
  235. package/dist/network/http/streamFunction/endStreamRequest.js.map +1 -0
  236. package/dist/network/http/streamFunction/getExtension.d.ts +2 -0
  237. package/dist/network/http/streamFunction/getExtension.js +22 -0
  238. package/dist/network/http/streamFunction/getExtension.js.map +1 -0
  239. package/dist/network/http/streamFunction/index.d.ts +3 -0
  240. package/dist/network/http/streamFunction/index.js +71 -0
  241. package/dist/network/http/streamFunction/index.js.map +1 -0
  242. package/dist/network/http/streamFunction/multipartFormStream.d.ts +5 -0
  243. package/dist/network/http/streamFunction/multipartFormStream.js +189 -0
  244. package/dist/network/http/streamFunction/multipartFormStream.js.map +1 -0
  245. package/dist/network/http/streamFunction/multipartStream.d.ts +13 -0
  246. package/dist/network/http/streamFunction/multipartStream.js +166 -0
  247. package/dist/network/http/streamFunction/multipartStream.js.map +1 -0
  248. package/dist/network/http/streamFunction/stream.d.ts +4 -0
  249. package/dist/network/http/streamFunction/stream.js +106 -0
  250. package/dist/network/http/streamFunction/stream.js.map +1 -0
  251. package/dist/network/index.d.ts +4 -0
  252. package/dist/network/index.js +84 -0
  253. package/dist/network/index.js.map +1 -0
  254. package/dist/network/message/auth.d.ts +4 -0
  255. package/dist/network/message/auth.js +35 -0
  256. package/dist/network/message/auth.js.map +1 -0
  257. package/dist/network/message/function.d.ts +3 -0
  258. package/dist/network/message/function.js +70 -0
  259. package/dist/network/message/function.js.map +1 -0
  260. package/dist/network/message/get.d.ts +3 -0
  261. package/dist/network/message/get.js +111 -0
  262. package/dist/network/message/get.js.map +1 -0
  263. package/dist/network/message/index.d.ts +3 -0
  264. package/dist/network/message/index.js +61 -0
  265. package/dist/network/message/index.js.map +1 -0
  266. package/dist/network/message/observable.d.ts +5 -0
  267. package/dist/network/message/observable.js +105 -0
  268. package/dist/network/message/observable.js.map +1 -0
  269. package/dist/network/message/send.d.ts +3 -0
  270. package/dist/network/message/send.js +11 -0
  271. package/dist/network/message/send.js.map +1 -0
  272. package/dist/network/rest/function.d.ts +3 -0
  273. package/dist/network/rest/function.js +85 -0
  274. package/dist/network/rest/function.js.map +1 -0
  275. package/dist/network/rest/index.d.ts +3 -0
  276. package/dist/network/rest/index.js +65 -0
  277. package/dist/network/rest/index.js.map +1 -0
  278. package/dist/network/upgrade.d.ts +4 -0
  279. package/dist/network/upgrade.js +48 -0
  280. package/dist/network/upgrade.js.map +1 -0
  281. package/dist/observable/index.d.ts +8 -0
  282. package/dist/observable/index.js +224 -0
  283. package/dist/observable/index.js.map +1 -0
  284. package/dist/openListener.d.ts +0 -0
  285. package/dist/openListener.js +0 -0
  286. package/dist/openListener.js.map +0 -0
  287. package/dist/protocol.d.ts +18 -0
  288. package/dist/protocol.js +206 -0
  289. package/dist/protocol.js.map +1 -0
  290. package/dist/secrets.d.ts +0 -0
  291. package/dist/secrets.js +0 -0
  292. package/dist/secrets.js.map +0 -0
  293. package/dist/server.d.ts +28 -0
  294. package/dist/server.js +96 -0
  295. package/dist/server.js.map +1 -0
  296. package/dist/types.d.ts +0 -0
  297. package/dist/types.js +0 -0
  298. package/dist/types.js.map +0 -0
  299. package/dist/upgradeListener.d.ts +0 -0
  300. package/dist/upgradeListener.js +0 -0
  301. package/dist/upgradeListener.js.map +0 -0
  302. package/dist/workers/index.d.ts +0 -0
  303. package/dist/workers/index.js +0 -0
  304. package/dist/workers/index.js.map +0 -0
  305. package/dist/workers/workerCode.d.ts +0 -0
  306. package/dist/workers/workerCode.js +0 -0
  307. package/dist/workers/workerCode.js.map +0 -0
  308. package/package.json +2 -2
@@ -0,0 +1,125 @@
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
+ let clientId = 0;
13
+ // these can be stored on context
14
+ // const allowedHeaders = [
15
+ // 'x-forwarded-for',
16
+ // 'user-agent',
17
+ // 'authorization',
18
+ // 'accept',
19
+ // 'accept-language',
20
+ // 'accept-encoding',
21
+ // 'referer',
22
+ // 'connection',
23
+ // 'upgrade-insecure-requests',
24
+ // 'if-modified-since',
25
+ // 'if-none-match',
26
+ // 'cache-control',
27
+ // 'host',
28
+ // 'origin',
29
+ // 'pragma',
30
+ // ]
31
+ const handleRequest = (server, method, client, route, authorized) => {
32
+ if (method === 'post') {
33
+ (0, readBody_1.readBody)(client, (payload) => (0, authorize_1.authorizeRequest)(server, client, payload, route, authorized), route.maxPayloadSize);
34
+ }
35
+ else {
36
+ const payload = (0, utils_1.parseQuery)(client.context.query);
37
+ (0, authorize_1.authorizeRequest)(server, client, payload, route, authorized);
38
+ }
39
+ };
40
+ const httpHandler = (server, req, res) => {
41
+ res.onAborted(() => {
42
+ client.context = null;
43
+ client.res = null;
44
+ client.req = null;
45
+ });
46
+ const ip = req.getHeader('x-forwarded-for') ||
47
+ Buffer.from(res.getRemoteAddressAsText()).toString();
48
+ if (server.blocked.has(ip)) {
49
+ (0, send_1.sendErrorRaw)(res, 'Too Many Requests', 429);
50
+ return;
51
+ }
52
+ const url = req.getUrl();
53
+ const path = url.split('/');
54
+ const route = server.functions.route(path[1], url);
55
+ if (route === false) {
56
+ (0, send_1.sendErrorRaw)(res, 'Not found', 404);
57
+ return;
58
+ }
59
+ // add all headers in context that are specialy defined for the route
60
+ const method = req.getMethod();
61
+ const client = {
62
+ res,
63
+ req,
64
+ context: {
65
+ query: req.getQuery(),
66
+ ua: req.getHeader('user-agent'),
67
+ ip,
68
+ id: ++clientId,
69
+ headers: {
70
+ authorization: req.getHeader('authorization'),
71
+ 'content-type': req.getHeader('content-type'),
72
+ 'content-encoding': req.getHeader('content-encoding'),
73
+ encoding: req.getHeader('accept-encoding'),
74
+ },
75
+ },
76
+ };
77
+ const len = req.getHeader('content-length');
78
+ // @ts-ignore
79
+ if (len && !isNaN(len)) {
80
+ client.context.headers['content-length'] = Number(len);
81
+ }
82
+ if (method === 'post' &&
83
+ client.context.headers['content-length'] === undefined) {
84
+ // zero is also not allowed
85
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.LengthRequired);
86
+ return;
87
+ }
88
+ if (route.headers) {
89
+ for (const header of route.headers) {
90
+ const v = req.getHeader(header);
91
+ if (v) {
92
+ client.context[header] = v;
93
+ }
94
+ }
95
+ }
96
+ if (route.observable === true) {
97
+ if (route.stream) {
98
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.CannotStreamToObservableFunction);
99
+ return;
100
+ }
101
+ const checksumRaw = req.getHeader('if-none-match');
102
+ // @ts-ignore use isNaN to cast string to number
103
+ const checksum = !isNaN(checksumRaw) ? Number(checksumRaw) : 0;
104
+ handleRequest(server, method, client, route, (payload) => (0, get_1.httpGet)(route, payload, client, server, checksum));
105
+ }
106
+ else {
107
+ if (route.stream === true) {
108
+ if (method !== 'post') {
109
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.MethodNotAllowed);
110
+ return;
111
+ }
112
+ if (client.context.headers['content-length'] === 0) {
113
+ // zero is also not allowed for streams
114
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.LengthRequired);
115
+ return;
116
+ }
117
+ (0, streamFunction_1.httpStreamFunction)(server, client, (0, utils_1.parseQuery)(client.context.query), route);
118
+ }
119
+ else {
120
+ handleRequest(server, method, client, route, (payload) => (0, function_1.httpFunction)(route, payload, client, server));
121
+ }
122
+ }
123
+ };
124
+ exports.httpHandler = httpHandler;
125
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/network/http/index.ts"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I,KAAK,CAAC,MAAM,KAAK,IAAI,EAAE;YACzB,IAAI,MAAM,KAAK,MAAM,EAAE;gBACrB,IAAA,oBAAa,EAAC,MAAM,EAAE,sBAAc,CAAC,gBAAgB,CAAC,CAAA;gBACtD,OAAM;aACP;YAED,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE;gBAClD,uCAAuC;gBACvC,IAAA,oBAAa,EAAC,MAAM,EAAE,sBAAc,CAAC,cAAc,CAAC,CAAA;gBACpD,OAAM;aACP;YAED,IAAA,mCAAkB,EAChB,MAAM,EACN,MAAM,EACN,IAAA,kBAAU,EAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAChC,KAAK,CACN,CAAA;SACF;aAAM;YACL,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE,CACvD,IAAA,uBAAY,EAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAC7C,CAAA;SACF;KACF;AACH,CAAC,CAAA;AA9GY,QAAA,WAAW,eA8GvB"}
@@ -0,0 +1,2 @@
1
+ import { HttpClient } from '../../types';
2
+ export declare const readBody: (client: HttpClient, onData: (data: any | void) => void, maxSize: number) => void;
@@ -0,0 +1,134 @@
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 parseData = (client, contentType, data, rawBuffer) => {
16
+ if (contentType === 'application/json' || !contentType) {
17
+ const str = data.toString();
18
+ let params;
19
+ try {
20
+ params = data.length ? JSON.parse(str) : undefined;
21
+ return params;
22
+ }
23
+ catch (e) {
24
+ // make this an event
25
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.InvalidPayload);
26
+ }
27
+ }
28
+ else if (contentType.startsWith('text') ||
29
+ contentType === 'application/xml') {
30
+ return data.toString();
31
+ }
32
+ else {
33
+ if (rawBuffer) {
34
+ return Buffer.alloc(data.byteLength, data);
35
+ }
36
+ return data;
37
+ }
38
+ };
39
+ const readBody = (client, onData, maxSize) => {
40
+ if (!client.res) {
41
+ return;
42
+ }
43
+ const contentLen = client.context.headers['content-length'];
44
+ if (contentLen > maxSize) {
45
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.PayloadTooLarge);
46
+ return;
47
+ }
48
+ const contentType = client.context.headers['content-type'];
49
+ const contentEncoding = client.context.headers['content-encoding'];
50
+ let size = 0;
51
+ if (contentEncoding) {
52
+ let uncompressStream;
53
+ if (contentEncoding === 'deflate') {
54
+ uncompressStream = node_zlib_1.default.createInflate(UNCOMPRESS_OPTS);
55
+ }
56
+ else if (contentEncoding === 'gzip') {
57
+ uncompressStream = node_zlib_1.default.createGunzip(UNCOMPRESS_OPTS);
58
+ }
59
+ else if (contentEncoding === 'br') {
60
+ uncompressStream = node_zlib_1.default.createBrotliDecompress(UNCOMPRESS_OPTS);
61
+ }
62
+ if (uncompressStream) {
63
+ client.res.onData((c, isLast) => {
64
+ size += c.byteLength;
65
+ if (size > maxSize) {
66
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.PayloadTooLarge);
67
+ // sendHttpError(client, 'Payload Too Large', 413)
68
+ uncompressStream.destroy();
69
+ return;
70
+ }
71
+ if (c.byteLength > MAX_CHUNK_SIZE) {
72
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.ChunkTooLarge);
73
+ uncompressStream.destroy();
74
+ return;
75
+ }
76
+ const buf = Buffer.alloc(c.byteLength, Buffer.from(c));
77
+ if (isLast) {
78
+ uncompressStream.end(buf);
79
+ }
80
+ else {
81
+ if (!uncompressStream.write(buf)) {
82
+ // handle backpressure
83
+ }
84
+ }
85
+ });
86
+ let data;
87
+ uncompressStream.on('data', (c) => {
88
+ if (!data) {
89
+ data = c;
90
+ }
91
+ else {
92
+ data = Buffer.concat([data, c]);
93
+ }
94
+ });
95
+ uncompressStream.on('end', () => {
96
+ uncompressStream.destroy();
97
+ onData(parseData(client, contentType, data, false));
98
+ });
99
+ }
100
+ else {
101
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.InvalidPayload, 'Unsupported Content-Encoding');
102
+ }
103
+ }
104
+ else {
105
+ let data;
106
+ client.res.onData((c, isLast) => {
107
+ size += c.byteLength;
108
+ if (size > maxSize) {
109
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.PayloadTooLarge);
110
+ return;
111
+ }
112
+ if (c.byteLength > MAX_CHUNK_SIZE) {
113
+ (0, send_1.sendHttpError)(client, error_1.BasedErrorCode.ChunkTooLarge);
114
+ return;
115
+ }
116
+ if (!data && isLast) {
117
+ data = Buffer.from(c);
118
+ onData(parseData(client, contentType, data, true));
119
+ return;
120
+ }
121
+ else if (!data) {
122
+ data = Buffer.alloc(c.byteLength, Buffer.from(c));
123
+ }
124
+ else {
125
+ data = Buffer.concat([data, Buffer.from(c)]);
126
+ }
127
+ if (isLast) {
128
+ onData(parseData(client, contentType, data, false));
129
+ }
130
+ });
131
+ }
132
+ };
133
+ exports.readBody = readBody;
134
+ //# 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;AAE5C,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,CAAA;AAElC,MAAM,eAAe,GAAG;IACtB,6CAA6C;IAC7C,SAAS,EAAE,IAAI,GAAG,IAAI,GA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AW,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;gBAClD,OAAM;aACP;iBAAM,IAAI,CAAC,IAAI,EAAE;gBAChB,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;aAClD;iBAAM;gBACL,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;aAC7C;YACD,IAAI,MAAM,EAAE;gBACV,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAA;aACpD;QACH,CAAC,CAAC,CAAA;KACH;AACH,CAAC,CAAA;AAlGY,QAAA,QAAQ,YAkGpB"}
@@ -0,0 +1,3 @@
1
+ import { HttpClient } from '../../types';
2
+ declare const _default: (client: HttpClient, contentType: string, onData: (data: any | void) => void) => void;
3
+ export default _default;
@@ -0,0 +1,50 @@
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 end_1 = __importDefault(require("./end"));
7
+ const MAX_BODY_SIZE = 4 ** 20; // 2MB
8
+ // make this and add max_body_size as config
9
+ // and fn content-size
10
+ exports.default = (client, contentType,
11
+ // add more later...
12
+ onData) => {
13
+ let data = Buffer.from([]);
14
+ client.res.onData(async (chunk, isLast) => {
15
+ if (!client.res) {
16
+ return;
17
+ }
18
+ data = Buffer.concat([data, Buffer.from(chunk)]);
19
+ if (data.length > MAX_BODY_SIZE) {
20
+ client.res.writeStatus('413 Payload Too Large');
21
+ client.res.writeHeader('Access-Control-Allow-Origin', '*');
22
+ client.res.writeHeader('Access-Control-Allow-Headers', 'content-type');
23
+ (0, end_1.default)(client, `{"code":413,"error":"Payload Too Large"}`);
24
+ return;
25
+ }
26
+ if (isLast) {
27
+ let params;
28
+ const str = data.toString();
29
+ // also check for buffer / binary daya
30
+ // may want to do this differently....
31
+ if (contentType === 'application/json' || !contentType) {
32
+ try {
33
+ params = data.length ? JSON.parse(str) : undefined;
34
+ onData(params);
35
+ }
36
+ catch (e) {
37
+ console.error(e, str);
38
+ client.res.writeStatus('400 Invalid Request');
39
+ client.res.writeHeader('Access-Control-Allow-Origin', '*');
40
+ client.res.writeHeader('Access-Control-Allow-Headers', 'content-type');
41
+ (0, end_1.default)(client, `{"code":400,"error":"Invalid payload"}`);
42
+ }
43
+ }
44
+ else {
45
+ onData(str);
46
+ }
47
+ }
48
+ });
49
+ };
50
+ //# sourceMappingURL=readPostData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readPostData.js","sourceRoot":"","sources":["../../../src/network/http/readPostData.ts"],"names":[],"mappings":";;;;;AACA,gDAAuB;AAEvB,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,CAAA,CAAC,MAAM;AAEpC,4CAA4C;AAC5C,sBAAsB;AAEtB,kBAAe,CACb,MAAkB,EAClB,WAAmB;AACnB,oBAAoB;AACpB,MAAkC,EAClC,EAAE;IACF,IAAI,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAC1B,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QACxC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAM;SACP;QACD,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAChD,IAAI,IAAI,CAAC,MAAM,GAAG,aAAa,EAAE;YAC/B,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,uBAAuB,CAAC,CAAA;YAC/C,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;YAC1D,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;YACtE,IAAA,aAAG,EAAC,MAAM,EAAE,0CAA0C,CAAC,CAAA;YACvD,OAAM;SACP;QACD,IAAI,MAAM,EAAE;YACV,IAAI,MAAM,CAAA;YACV,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAA;YAC3B,sCAAsC;YACtC,sCAAsC;YACtC,IAAI,WAAW,KAAK,kBAAkB,IAAI,CAAC,WAAW,EAAE;gBACtD,IAAI;oBACF,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;oBAClD,MAAM,CAAC,MAAM,CAAC,CAAA;iBACf;gBAAC,OAAO,CAAC,EAAE;oBACV,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;oBACrB,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,qBAAqB,CAAC,CAAA;oBAC7C,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;oBAC1D,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;oBACtE,IAAA,aAAG,EAAC,MAAM,EAAE,wCAAwC,CAAC,CAAA;iBACtD;aACF;iBAAM;gBACL,MAAM,CAAC,GAAG,CAAC,CAAA;aACZ;SACF;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { HttpClient } from '../../types';
2
+ import uws from '@based/uws';
3
+ import { BasedErrorCode, CreateErrorProps } from '../../error';
4
+ export declare const sendHttpError: (client: HttpClient, basedCode: BasedErrorCode, err?: CreateErrorProps, overrides?: any) => void;
5
+ export declare const sendErrorRaw: (res: uws.HttpResponse, error: any, code?: number, status?: string) => void;
6
+ export declare const sendHttpResponse: (client: HttpClient, result: any) => void;
@@ -0,0 +1,65 @@
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.sendErrorRaw = 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 sendHttpError = (client, basedCode, err, overrides) => {
11
+ if (!client.res) {
12
+ return;
13
+ }
14
+ client.res.cork(() => {
15
+ const errorData = (0, error_1.createError)(basedCode, err);
16
+ const { code, status, message, basedMessage } = errorData;
17
+ // fix them status
18
+ client.res.writeStatus(`${code} ${status}`);
19
+ client.res.writeHeader('Access-Control-Allow-Origin', '*');
20
+ client.res.writeHeader('Access-Control-Allow-Headers', 'content-type');
21
+ client.res.writeHeader('Content-Type', 'application/json');
22
+ (0, end_1.default)(client, JSON.stringify({ error: err || message, code, basedCode, basedMessage }));
23
+ });
24
+ };
25
+ exports.sendHttpError = sendHttpError;
26
+ const sendErrorRaw = (res, error, code = 400, status = 'Bad Request') => {
27
+ res.cork(() => {
28
+ res.writeStatus(`${code} ${status}`);
29
+ res.writeHeader('Content-Type', 'application/json');
30
+ res.end(JSON.stringify({ error, code }));
31
+ });
32
+ };
33
+ exports.sendErrorRaw = sendErrorRaw;
34
+ const sendHttpResponse = (client, result) => {
35
+ if (!client.res) {
36
+ return;
37
+ }
38
+ const encoding = client.context.encoding;
39
+ let cType;
40
+ // for functions there is never cache (idea is they are used to execute - observable fns are for cache)
41
+ let parsed;
42
+ if (typeof result === 'string') {
43
+ cType = 'text/plain';
44
+ parsed = result;
45
+ }
46
+ else {
47
+ cType = 'application/json';
48
+ parsed = JSON.stringify(result);
49
+ }
50
+ (0, compress_1.compress)(client, parsed, encoding).then(({ payload, encoding }) => {
51
+ if (client.res) {
52
+ client.res.cork(() => {
53
+ client.res.writeStatus('200 OK');
54
+ client.res.writeHeader('Cache-Control', 'max-age=0, must-revalidate');
55
+ client.res.writeHeader('Content-Type', cType);
56
+ if (encoding) {
57
+ client.res.writeHeader('Content-Encoding', encoding);
58
+ }
59
+ (0, end_1.default)(client, payload);
60
+ });
61
+ }
62
+ });
63
+ };
64
+ exports.sendHttpResponse = sendHttpResponse;
65
+ //# 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,uCAA2E;AAEpE,MAAM,aAAa,GAAG,CAC3B,MAAkB,EAClB,SAAyB,EACzB,GAAsB,EACtB,SAAe,EACf,EAAE;IACF,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;QACf,OAAM;KACP;IACD,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE;QACnB,MAAM,SAAS,GAAG,IAAA,mBAAW,EAAC,SAAS,EAAE,GAAG,CAAC,CAAA;QAC7C,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,SAAS,CAAA;QAEzD,kBAAkB;QAElB,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,IAAI,IAAI,MAAM,EAAE,CAAC,CAAA;QAC3C,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;QAC1D,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;QACtE,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;QAC1D,IAAA,aAAG,EACD,MAAM,EACN,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,GAAG,IAAI,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC,C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G,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAA;gBAChC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,eAAe,EAAE,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;AA/BY,QAAA,gBAAgB,oBA+B5B"}
@@ -0,0 +1,4 @@
1
+ import { HttpClient } from '../../types';
2
+ import uws from '@based/uws';
3
+ export declare const sendError: (client: HttpClient, error: any, code?: number, status?: string) => void;
4
+ export declare const sendErrorRaw: (res: uws.HttpResponse, error: any, code?: number, status?: string) => void;
@@ -0,0 +1,27 @@
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.sendErrorRaw = exports.sendError = void 0;
7
+ const end_1 = __importDefault(require("./end"));
8
+ const sendError = (client, error, code = 400, status = 'Bad Request') => {
9
+ if (!client.res) {
10
+ return;
11
+ }
12
+ client.res.writeStatus(`${code} ${status}`);
13
+ client.res.writeHeader('Access-Control-Allow-Origin', '*');
14
+ client.res.writeHeader('Access-Control-Allow-Headers', 'content-type');
15
+ client.res.writeHeader('Content-Type', 'application/json');
16
+ (0, end_1.default)(client, JSON.stringify({ error, code }));
17
+ };
18
+ exports.sendError = sendError;
19
+ const sendErrorRaw = (res, error, code = 400, status = 'Bad Request') => {
20
+ res.writeStatus(`${code} ${status}`);
21
+ res.writeHeader('Access-Control-Allow-Origin', '*');
22
+ res.writeHeader('Access-Control-Allow-Headers', 'content-type');
23
+ res.writeHeader('Content-Type', 'application/json');
24
+ res.end(JSON.stringify({ error, code }));
25
+ };
26
+ exports.sendErrorRaw = sendErrorRaw;
27
+ //# sourceMappingURL=sendError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sendError.js","sourceRoot":"","sources":["../../../src/network/http/sendError.ts"],"names":[],"mappings":";;;;;;AAEA,gDAAuB;AAEhB,MAAM,SAAS,GAAG,CACvB,MAAkB,EAClB,KAAU,EACV,OAAe,GAAG,EAClB,SAAiB,aAAa,EAC9B,EAAE;IACF,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;QACf,OAAM;KACP;IACD,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,IAAI,IAAI,MAAM,EAAE,CAAC,CAAA;IAC3C,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;IAC1D,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;IACtE,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;IAC1D,IAAA,aAAG,EAAC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AAC9C,CAAC,CAAA;AAdY,QAAA,SAAS,aAcrB;AAEM,MAAM,YAAY,GAAG,CAC1B,GAAqB,EACrB,KAAU,EACV,OAAe,GAAG,EAClB,SAAiB,aAAa,EAC9B,EAAE;IACF,GAAG,CAAC,WAAW,CAAC,GAAG,IAAI,IAAI,MAAM,EAAE,CAAC,CAAA;IACpC,GAAG,CAAC,WAAW,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;IACnD,GAAG,CAAC,WAAW,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;IAC/D,GAAG,CAAC,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;IACnD,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AAC1C,CAAC,CAAA;AAXY,QAAA,YAAY,gBAWxB"}
@@ -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,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataStream = void 0;
4
+ const stream_1 = require("stream");
5
+ class DataStream extends stream_1.Duplex {
6
+ _read() { }
7
+ // writableHighWaterMark:
8
+ _write(chunk, encoding, callback) {
9
+ this.push(Buffer.from(chunk));
10
+ callback();
11
+ }
12
+ _final() {
13
+ this.push(null);
14
+ }
15
+ }
16
+ exports.DataStream = DataStream;
17
+ //# sourceMappingURL=DataStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DataStream.js","sourceRoot":"","sources":["../../../../src/network/http/stream/DataStream.ts"],"names":[],"mappings":";;;AAAA,mCAA+B;AAE/B,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,CAAC,CAAC,CAAA;QAC7B,QAAQ,EAAE,CAAA;IACZ,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACjB,CAAC;CACF;AAZD,gCAYC"}
@@ -0,0 +1,4 @@
1
+ import uws from '@based/uws';
2
+ import { BasedServer } from '../../..';
3
+ declare const _default: (server: BasedServer, res: uws.HttpResponse) => Promise<void>;
4
+ export default _default;