@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,184 @@
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 storeFile_1 = __importDefault(require("./storeFile"));
8
+ const getExtenstion_1 = __importDefault(require("./getExtenstion"));
9
+ const isCompleteOpts = (options) => {
10
+ return !!(options.id && options.size && options.type && options.extension);
11
+ };
12
+ const setHeader = (server, file, res) => {
13
+ file.headersSet++;
14
+ if (file.headersSet === 2) {
15
+ const opts = file.opts;
16
+ if (isCompleteOpts(opts)) {
17
+ if (opts.size < 200000) {
18
+ file.stream.on('end', () => {
19
+ const payload = {
20
+ $id: opts.id,
21
+ progress: 1,
22
+ size: opts.size,
23
+ };
24
+ if (opts.name) {
25
+ payload.name = opts.name;
26
+ }
27
+ server.based.set(payload);
28
+ });
29
+ }
30
+ else {
31
+ let progress = 0;
32
+ let total = 0;
33
+ let setInProgress = false;
34
+ const payload = {
35
+ $id: opts.id,
36
+ progress: 0,
37
+ size: opts.size,
38
+ };
39
+ if (opts.name) {
40
+ payload.name = opts.name;
41
+ }
42
+ server.based.set(payload);
43
+ const updateProgress = () => {
44
+ if (!setInProgress) {
45
+ setInProgress = true;
46
+ setTimeout(() => {
47
+ const payload = {
48
+ $id: opts.id,
49
+ progress,
50
+ };
51
+ server.based.set(payload);
52
+ setInProgress = false;
53
+ }, 250);
54
+ }
55
+ };
56
+ file.stream.on('end', () => {
57
+ progress = 1;
58
+ updateProgress();
59
+ });
60
+ file.stream.on('data', (chunk) => {
61
+ total += chunk.byteLength;
62
+ progress = total / opts.size;
63
+ updateProgress();
64
+ });
65
+ }
66
+ (0, storeFile_1.default)(server, file.stream, opts);
67
+ }
68
+ else {
69
+ invalidFile(res);
70
+ return null;
71
+ }
72
+ return true;
73
+ }
74
+ return false;
75
+ };
76
+ const invalidFile = (res) => {
77
+ console.info('Invalid file');
78
+ res.aborted = true;
79
+ res.writeStatus('400 Invalid Request').end('{"error":"invalid file}');
80
+ };
81
+ const toBuffer = (str, firstWritten) => {
82
+ return Buffer.from(firstWritten ? str + '\r\n' : str, 'binary');
83
+ };
84
+ exports.default = async (server, res) => {
85
+ const files = [];
86
+ let boundary = null;
87
+ let prevLine;
88
+ let isWriting = false;
89
+ res.onData((chunk, isLast) => {
90
+ let firstWritten = false;
91
+ const blocks = Buffer.from(chunk).toString('binary').split('\r\n');
92
+ if (!boundary) {
93
+ boundary = blocks[0];
94
+ }
95
+ for (let i = 0; i < blocks.length; i++) {
96
+ const line = blocks[i];
97
+ if (!boundary) {
98
+ continue;
99
+ }
100
+ if (isWriting && (line === boundary || line === boundary + '--')) {
101
+ isWriting = false;
102
+ const file = files[files.length - 1];
103
+ if (prevLine) {
104
+ file.stream.end(toBuffer(prevLine, firstWritten));
105
+ }
106
+ else {
107
+ file.stream.end();
108
+ }
109
+ prevLine = null;
110
+ if (line === boundary + '--') {
111
+ continue;
112
+ }
113
+ }
114
+ if (line === boundary && !isWriting) {
115
+ const file = {
116
+ stream: new DataStream_1.DataStream(),
117
+ headersSet: 0,
118
+ opts: {},
119
+ };
120
+ files.push(file);
121
+ // try {
122
+ // fs.unlinkSync('/Users/jim/Desktop/yes.png')
123
+ // } catch (err) {}
124
+ // file.stream.pipe(fs.createWriteStream('/Users/jim/Desktop/yes.png'))
125
+ continue;
126
+ }
127
+ const file = files[files.length - 1];
128
+ if (!file) {
129
+ return invalidFile(res);
130
+ }
131
+ if (!isWriting && line.includes('Content-Disposition')) {
132
+ const meta = line.match(/name="(.*?)"/)?.[1];
133
+ if (!meta) {
134
+ return invalidFile(res);
135
+ }
136
+ const [functionName, raw, id, size, ...name] = meta.split('|');
137
+ const opts = file.opts;
138
+ opts.id = id;
139
+ if (raw) {
140
+ opts.raw = true;
141
+ }
142
+ if (name && name.length) {
143
+ opts.name = name.join('|');
144
+ }
145
+ else {
146
+ opts.name = line.match(/filename="(.*?)"/)?.[1] || 'untitled';
147
+ }
148
+ if (functionName) {
149
+ opts.functionName = functionName;
150
+ }
151
+ opts.size = Number(size);
152
+ isWriting = setHeader(server, file, res);
153
+ if (isWriting === null) {
154
+ return;
155
+ }
156
+ continue;
157
+ }
158
+ if (!isWriting && line.includes('Content-Type')) {
159
+ const mimeType = line.match(/Content-Type: ([a-zA-Z0-9].+\/[a-zA-Z0-9].+)/)?.[1];
160
+ if (!mimeType) {
161
+ return invalidFile(res);
162
+ }
163
+ file.opts.type = mimeType;
164
+ file.opts.extension = (0, getExtenstion_1.default)(mimeType);
165
+ isWriting = setHeader(server, file, res);
166
+ if (isWriting === null) {
167
+ return;
168
+ }
169
+ continue;
170
+ }
171
+ if (isWriting) {
172
+ if (prevLine) {
173
+ file.stream.write(toBuffer(prevLine, firstWritten));
174
+ }
175
+ prevLine = line;
176
+ firstWritten = true;
177
+ }
178
+ }
179
+ if (isLast) {
180
+ res.end('{}');
181
+ }
182
+ });
183
+ };
184
+ //# sourceMappingURL=formStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formStream.js","sourceRoot":"","sources":["../../../../src/network/http/stream/formStream.ts"],"names":[],"mappings":";;;;;AAAA,6CAAyC;AACzC,4DAAmC;AAInC,oEAA2C;AAS3C,MAAM,cAAc,GAAG,CACrB,OAA6B,EACL,EAAE;IAC1B,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,SAAS,CAAC,CAAA;AAC5E,CAAC,CAAA;AAED,MAAM,SAAS,GAAG,CAChB,MAAmB,EACnB,IAAoB,EACpB,GAAqB,EACZ,EAAE;IACX,IAAI,CAAC,UAAU,EAAE,CAAA;IACjB,IAAI,IAAI,CAAC,UAAU,KAAK,CAAC,EAAE;QACzB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACtB,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE;YACxB,IAAI,IAAI,CAAC,IAAI,GAAG,MAAM,EAAE;gBACtB,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;oBACzB,MAAM,OAAO,GAAQ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AAC,SAAS,IAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE;gBAC/C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CACzB,8CAA8C,CAC/C,EAAE,CAAC,CAAC,CAAC,CAAA;gBACN,IAAI,CAAC,QAAQ,EAAE;oBACb,OAAO,WAAW,CAAC,GAAG,CAAC,CAAA;iBACxB;gBACD,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAA;gBACzB,IAAI,CAAC,IAAI,CAAC,SAAS,GAAG,IAAA,uBAAa,EAAC,QAAQ,CAAC,CAAA;gBAC7C,SAAS,GAAG,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,CAAA;gBACxC,IAAI,SAAS,KAAK,IAAI,EAAE;oBACtB,OAAM;iBACP;gBACD,SAAQ;aACT;YAED,IAAI,SAAS,EAAE;gBACb,IAAI,QAAQ,EAAE;oBACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAA;iBACpD;gBACD,QAAQ,GAAG,IAAI,CAAA;gBACf,YAAY,GAAG,IAAI,CAAA;aACpB;SACF;QAED,IAAI,MAAM,EAAE;YACV,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;SACd;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ declare const _default: (mimeType: string) => any;
2
+ export default _default;
@@ -0,0 +1,22 @@
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
+ return mime_types_1.default.extension(mimeType);
20
+ }
21
+ };
22
+ //# sourceMappingURL=getExtenstion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getExtenstion.js","sourceRoot":"","sources":["../../../../src/network/http/stream/getExtenstion.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,OAAO,oBAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;KACrC;AACH,CAAC,CAAA"}
@@ -0,0 +1,9 @@
1
+ import uws from '@based/uws';
2
+ import { BasedServer } from '../../..';
3
+ export declare const invalidReqNoCors: (res: uws.HttpResponse) => void;
4
+ export declare type StreamOptions = {
5
+ type: string;
6
+ size: number;
7
+ };
8
+ declare const _default: (server: BasedServer, name: string, req: uws.HttpRequest, res: uws.HttpResponse) => Promise<void>;
9
+ export default _default;
@@ -0,0 +1,42 @@
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.invalidReqNoCors = void 0;
7
+ const stream_1 = __importDefault(require("./stream"));
8
+ const invalidReqNoCors = (res) => {
9
+ res.aborted = true;
10
+ res
11
+ .writeStatus('400 Invalid Request')
12
+ .end(`{"code":400,"error":"Invalid Request"}`);
13
+ };
14
+ exports.invalidReqNoCors = invalidReqNoCors;
15
+ exports.default = async (server, name, req, res) => {
16
+ const method = req.getMethod();
17
+ if (method !== 'post') {
18
+ console.info('options or get');
19
+ res.end('');
20
+ return;
21
+ }
22
+ const type = req.getHeader('content-type');
23
+ if (type === 'multipart/form-data') {
24
+ // formStream(server, res)
25
+ return;
26
+ }
27
+ const size = Number(req.getHeader('content-length'));
28
+ if (!type) {
29
+ (0, exports.invalidReqNoCors)(res);
30
+ return;
31
+ }
32
+ const opts = {
33
+ size,
34
+ type,
35
+ };
36
+ if (!opts.size) {
37
+ (0, exports.invalidReqNoCors)(res);
38
+ return;
39
+ }
40
+ (0, stream_1.default)(res, size);
41
+ };
42
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/network/http/stream/index.ts"],"names":[],"mappings":";;;;;;AAEA,sDAA6B;AAEtB,MAAM,gBAAgB,GAAG,CAAC,GAAqB,EAAE,EAAE;IACxD,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;IAClB,GAAG;SACA,WAAW,CAAC,qBAAqB,CAAC;SAClC,GAAG,CAAC,wCAAwC,CAAC,CAAA;AAClD,CAAC,CAAA;AALY,QAAA,gBAAgB,oBAK5B;AAOD,kBAAe,KAAK,EAClB,MAAmB,EACnB,IAAY,EACZ,GAAoB,EACpB,GAAqB,EACrB,EAAE;IACF,MAAM,MAAM,GAAG,GAAG,CAAC,SAAS,EAAE,CAAA;IAE9B,IAAI,MAAM,KAAK,MAAM,EAAE;QACrB,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAA;QAC9B,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QACX,OAAM;KACP;IAED,MAAM,IAAI,GAAG,GAAG,CAAC,SAAS,CAAC,cAAc,CAAC,CAAA;IAE1C,IAAI,IAAI,KAAK,qBAAqB,EAAE;QAClC,0BAA0B;QAC1B,OAAM;KACP;IAED,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAA;IAEpD,IAAI,CAAC,IAAI,EAAE;QACT,IAAA,wBAAgB,EAAC,GAAG,CAAC,CAAA;QACrB,OAAM;KACP;IAED,MAAM,IAAI,GAAkB;QAC1B,IAAI;QACJ,IAAI;KACL,CAAA;IAED,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;QACd,IAAA,wBAAgB,EAAC,GAAG,CAAC,CAAA;QACrB,OAAM;KACP;IAED,IAAA,gBAAM,EAAC,GAAG,EAAE,IAAI,CAAC,CAAA;AACnB,CAAC,CAAA"}
@@ -0,0 +1,194 @@
1
+ // import { DataStream } from './DataStream'
2
+ // import uws from '@based/uws'
3
+ // import { BasedServer } from '../../..'
4
+ // import getExtenstion from './getExtenstion'
5
+ // // import fs from 'fs'
6
+ // type FileDescriptor = {
7
+ // opts: Partial<FileOptions>
8
+ // stream: DataStream
9
+ // headersSet: number
10
+ // }
11
+ // const isCompleteOpts = (
12
+ // options: Partial<FileOptions>
13
+ // ): options is FileOptions => {
14
+ // return !!(options.id && options.size && options.type && options.extension)
15
+ // }
16
+ // // handler for updates
17
+ // const setHeader = (
18
+ // server: BasedServer,
19
+ // file: FileDescriptor,
20
+ // res: uws.HttpResponse
21
+ // ): boolean => {
22
+ // file.headersSet++
23
+ // if (file.headersSet === 2) {
24
+ // const opts = file.opts
25
+ // if (isCompleteOpts(opts)) {
26
+ // if (opts.size < 200000) {
27
+ // file.stream.on('end', () => {
28
+ // const payload: any = {
29
+ // $id: opts.id,
30
+ // progress: 1,
31
+ // size: opts.size,
32
+ // }
33
+ // if (opts.name) {
34
+ // payload.name = opts.name
35
+ // }
36
+ // server.based.set(payload)
37
+ // })
38
+ // } else {
39
+ // let progress = 0
40
+ // let total = 0
41
+ // let setInProgress = false
42
+ // const payload: any = {
43
+ // $id: opts.id,
44
+ // progress: 0,
45
+ // size: opts.size,
46
+ // }
47
+ // if (opts.name) {
48
+ // payload.name = opts.name
49
+ // }
50
+ // server.based.set(payload)
51
+ // const updateProgress = () => {
52
+ // if (!setInProgress) {
53
+ // setInProgress = true
54
+ // setTimeout(() => {
55
+ // const payload: any = {
56
+ // $id: opts.id,
57
+ // progress,
58
+ // }
59
+ // server.based.set(payload)
60
+ // setInProgress = false
61
+ // }, 250)
62
+ // }
63
+ // }
64
+ // file.stream.on('end', () => {
65
+ // progress = 1
66
+ // updateProgress()
67
+ // })
68
+ // file.stream.on('data', (chunk) => {
69
+ // total += chunk.byteLength
70
+ // progress = total / opts.size
71
+ // updateProgress()
72
+ // })
73
+ // }
74
+ // storeFile(server, file.stream, opts)
75
+ // } else {
76
+ // invalidFile(res)
77
+ // return null
78
+ // }
79
+ // return true
80
+ // }
81
+ // return false
82
+ // }
83
+ // const invalidFile = (res: uws.HttpResponse) => {
84
+ // console.info('Invalid file')
85
+ // res.aborted = true
86
+ // res.writeStatus('400 Invalid Request').end('{"error":"invalid file}')
87
+ // }
88
+ // const toBuffer = (str: string, firstWritten: boolean): Buffer => {
89
+ // return Buffer.from(firstWritten ? str + '\r\n' : str, 'binary')
90
+ // }
91
+ // export default async (
92
+ // server: BasedServer,
93
+ // res: uws.HttpResponse
94
+ // ): Promise<void> => {
95
+ // const files: FileDescriptor[] = []
96
+ // let boundary = null
97
+ // let prevLine: string
98
+ // let isWriting = false
99
+ // res.onData((chunk, isLast) => {
100
+ // let firstWritten = false
101
+ // const blocks = Buffer.from(chunk).toString('binary').split('\r\n')
102
+ // if (!boundary) {
103
+ // boundary = blocks[0]
104
+ // }
105
+ // for (let i = 0; i < blocks.length; i++) {
106
+ // const line = blocks[i]
107
+ // if (!boundary) {
108
+ // continue
109
+ // }
110
+ // if (isWriting && (line === boundary || line === boundary + '--')) {
111
+ // isWriting = false
112
+ // const file = files[files.length - 1]
113
+ // if (prevLine) {
114
+ // file.stream.end(toBuffer(prevLine, firstWritten))
115
+ // } else {
116
+ // file.stream.end()
117
+ // }
118
+ // prevLine = null
119
+ // if (line === boundary + '--') {
120
+ // continue
121
+ // }
122
+ // }
123
+ // if (line === boundary && !isWriting) {
124
+ // const file = {
125
+ // stream: new DataStream(),
126
+ // headersSet: 0,
127
+ // opts: {},
128
+ // }
129
+ // files.push(file)
130
+ // // try {
131
+ // // fs.unlinkSync('/Users/jim/Desktop/yes.png')
132
+ // // } catch (err) {}
133
+ // // file.stream.pipe(fs.createWriteStream('/Users/jim/Desktop/yes.png'))
134
+ // continue
135
+ // }
136
+ // const file = files[files.length - 1]
137
+ // if (!file) {
138
+ // return invalidFile(res)
139
+ // }
140
+ // if (!isWriting && line.includes('Content-Disposition')) {
141
+ // const meta = line.match(/name="(.*?)"/)?.[1]
142
+ // if (!meta) {
143
+ // return invalidFile(res)
144
+ // }
145
+ // const [functionName, raw, id, size, ...name] = meta.split('|')
146
+ // const opts = file.opts
147
+ // opts.id = id
148
+ // if (raw) {
149
+ // opts.raw = true
150
+ // }
151
+ // if (name && name.length) {
152
+ // opts.name = name.join('|')
153
+ // } else {
154
+ // opts.name = line.match(/filename="(.*?)"/)?.[1] || 'untitled'
155
+ // }
156
+ // if (functionName) {
157
+ // opts.functionName = functionName
158
+ // }
159
+ // opts.size = Number(size)
160
+ // isWriting = setHeader(server, file, res)
161
+ // if (isWriting === null) {
162
+ // return
163
+ // }
164
+ // continue
165
+ // }
166
+ // if (!isWriting && line.includes('Content-Type')) {
167
+ // const mimeType = line.match(
168
+ // /Content-Type: ([a-zA-Z0-9].+\/[a-zA-Z0-9].+)/
169
+ // )?.[1]
170
+ // if (!mimeType) {
171
+ // return invalidFile(res)
172
+ // }
173
+ // file.opts.type = mimeType
174
+ // file.opts.extension = getExtenstion(mimeType)
175
+ // isWriting = setHeader(server, file, res)
176
+ // if (isWriting === null) {
177
+ // return
178
+ // }
179
+ // continue
180
+ // }
181
+ // if (isWriting) {
182
+ // if (prevLine) {
183
+ // file.stream.write(toBuffer(prevLine, firstWritten))
184
+ // }
185
+ // prevLine = line
186
+ // firstWritten = true
187
+ // }
188
+ // }
189
+ // if (isLast) {
190
+ // res.end('{}')
191
+ // }
192
+ // })
193
+ // }
194
+ //# sourceMappingURL=multipartFormStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multipartFormStream.js","sourceRoot":"","sources":["../../../../src/network/http/stream/multipartFormStream.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,+BAA+B;AAC/B,yCAAyC;AACzC,8CAA8C;AAC9C,yBAAyB;AAEzB,0BAA0B;AAC1B,+BAA+B;AAC/B,uBAAuB;AACvB,uBAAuB;AACvB,IAAI;AAEJ,2BAA2B;AAC3B,kCAAkC;AAClC,iCAAiC;AACjC,+EAA+E;AAC/E,IAAI;AAEJ,yBAAyB;AAEzB,sBAAsB;AACtB,yBAAyB;AACzB,0BAA0B;AAC1B,0BAA0B;AAC1B,kBAAkB;AAClB,sBAAsB;AACtB,iCAAiC;AACjC,6BAA6B;AAC7B,kCAAkC;AAClC,kCAAkC;AAClC,wCAAwC;AACxC,mCAAmC;AACnC,4BAA4B;AAC5B,2BAA2B;AAC3B,+BAA+B;AAC/B,cAAc;AACd,6BAA6B;AAC7B,uCAAuC;AACvC,cAAc;AACd,sCAAsC;AACtC,aAAa;AACb,iBAAiB;AACjB,2BAA2B;AAC3B,wBAAwB;AACxB,oCAAoC;AACpC,iCAAiC;AACjC,0BAA0B;AAC1B,yBAAyB;AACzB,6BAA6B;AAC7B,YAAY;AACZ,2BAA2B;AAC3B,qCAAqC;AACrC,YAAY;AACZ,oCAAoC;AACpC,yCAAyC;AACzC,kCAAkC;AAClC,mCAAmC;AACnC,iCAAiC;AACjC,uCAAuC;AACvC,gCAAgC;AAChC,4BAA4B;AAC5B,kBAAkB;AAClB,0CAA0C;AAC1C,sCAAsC;AACtC,sBAAsB;AACtB,cAAc;AACd,YAAY;AACZ,wCAAwC;AACxC,yBAAyB;AACzB,6BAA6B;AAC7B,aAAa;AACb,8CAA8C;AAC9C,sCAAsC;AACtC,yCAAyC;AACzC,6BAA6B;AAC7B,aAAa;AACb,UAAU;AAC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C,mDAAmD;AACnD,oCAAoC;AACpC,mBAAmB;AACnB,YAAY;AACZ,mBAAmB;AACnB,UAAU;AAEV,2DAA2D;AAC3D,uCAAuC;AACvC,2DAA2D;AAC3D,iBAAiB;AACjB,2BAA2B;AAC3B,oCAAoC;AACpC,YAAY;AACZ,oCAAoC;AACpC,wDAAwD;AACxD,mDAAmD;AACnD,oCAAoC;AACpC,mBAAmB;AACnB,YAAY;AACZ,mBAAmB;AACnB,UAAU;AAEV,yBAAyB;AACzB,0BAA0B;AAC1B,gEAAgE;AAChE,YAAY;AACZ,0BAA0B;AAC1B,8BAA8B;AAC9B,UAAU;AACV,QAAQ;AAER,oBAAoB;AACpB,sBAAsB;AACtB,QAAQ;AACR,OAAO;AACP,IAAI"}
@@ -0,0 +1,5 @@
1
+ import { Readable } from 'stream';
2
+ import { BasedServer } from '../../..';
3
+ import { FileOptions } from './types';
4
+ declare const _default: (server: BasedServer, stream: Readable, opts: FileOptions) => Promise<void>;
5
+ export default _default;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = async (server, stream, opts) => {
4
+ const storeFile = server.config.storeFile;
5
+ if (!storeFile) {
6
+ throw new Error('No file upload handler available!');
7
+ }
8
+ const result = await storeFile({
9
+ based: server.based,
10
+ stream,
11
+ id: opts.id,
12
+ mimeType: opts.type,
13
+ extension: opts.extension,
14
+ size: opts.size,
15
+ });
16
+ // thumb
17
+ const setObj = {
18
+ $id: opts.id,
19
+ src: result.src,
20
+ origin: result.origin,
21
+ version: result.version,
22
+ mimeType: result.mimeType || opts.type,
23
+ };
24
+ if (result.thumb) {
25
+ setObj.thumb = result.thumb;
26
+ }
27
+ // add version
28
+ server.based.set(setObj);
29
+ };
30
+ //# sourceMappingURL=storeFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storeFile.js","sourceRoot":"","sources":["../../../../src/network/http/stream/storeFile.ts"],"names":[],"mappings":";;AAIA,kBAAe,KAAK,EAClB,MAAmB,EACnB,MAAgB,EAChB,IAAiB,EACF,EAAE;IACjB,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAA;IAEzC,IAAI,CAAC,SAAS,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;KACrD;IAED,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC;QAC7B,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM;QACN,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,QAAQ,EAAE,IAAI,CAAC,IAAI;QACnB,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,IAAI,EAAE,IAAI,CAAC,IAAI;KAChB,CAAC,CAAA;IAEF,QAAQ;IAER,MAAM,MAAM,GAAQ;QAClB,GAAG,EAAE,IAAI,CAAC,EAAE;QACZ,GAAG,EAAE,MAAM,CAAC,GAAG;QACf,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI;KACvC,CAAA;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;KAC5B;IAED,cAAc;IACd,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAC1B,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ import { DataStream } from './DataStream';
2
+ import uws from '@based/uws';
3
+ declare const _default: (res: uws.HttpResponse, size: number) => Promise<DataStream>;
4
+ export default _default;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const DataStream_1 = require("./DataStream");
4
+ const smallFile = (res, stream) => {
5
+ stream.emit('progress', 0);
6
+ res.onData(async (chunk, isLast) => {
7
+ const buf = Buffer.from(chunk);
8
+ stream.write(buf);
9
+ if (isLast) {
10
+ stream.emit('progress', 1);
11
+ stream.end();
12
+ res.end(`{}`);
13
+ }
14
+ });
15
+ };
16
+ const largeFile = (res, stream, size) => {
17
+ let progress = 0;
18
+ let total = 0;
19
+ let setInProgress = false;
20
+ stream.emit('progress', progress);
21
+ res.onData(async (chunk, isLast) => {
22
+ const buf = Buffer.from(chunk);
23
+ total += buf.byteLength;
24
+ progress = total / size;
25
+ if (!setInProgress) {
26
+ setInProgress = true;
27
+ setTimeout(() => {
28
+ stream.emit('progress', progress);
29
+ setInProgress = false;
30
+ }, 250);
31
+ }
32
+ stream.write(buf);
33
+ if (isLast) {
34
+ stream.end();
35
+ res.end(`{}`);
36
+ }
37
+ });
38
+ };
39
+ exports.default = async (res, size) => {
40
+ const stream = new DataStream_1.DataStream();
41
+ if (size > 200000) {
42
+ largeFile(res, stream, size);
43
+ }
44
+ else {
45
+ smallFile(res, stream);
46
+ }
47
+ return stream;
48
+ };
49
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../../src/network/http/stream/stream.ts"],"names":[],"mappings":";;AAAA,6CAAyC;AAGzC,MAAM,SAAS,GAAG,CAAC,GAAqB,EAAE,MAAkB,EAAE,EAAE;IAC9D,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;IAC1B,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC9B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACjB,IAAI,MAAM,EAAE;YACV,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;YAC1B,MAAM,CAAC,GAAG,EAAE,CAAA;YACZ,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;SACd;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CA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 export declare type FileOptions = {
2
+ name?: string;
3
+ raw?: boolean;
4
+ size: number;
5
+ type: string;
6
+ id: string;
7
+ extension: string;
8
+ functionName?: string;
9
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/network/http/stream/types.ts"],"names":[],"mappings":""}
@@ -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, encoding));
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/streamFunction/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,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,3 @@
1
+ import { HttpClient } from '../../../types';
2
+ declare const _default: (client: HttpClient) => void;
3
+ export default _default;