geonix 1.23.1 → 1.23.3

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.
@@ -6,5 +6,6 @@
6
6
  "javascript"
7
7
  ],
8
8
  "editor.tabSize": 4,
9
- "editor.detectIndentation": false
9
+ "editor.detectIndentation": false,
10
+ "editor.formatOnSave": true
10
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geonix",
3
- "version": "1.23.1",
3
+ "version": "1.23.3",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "bin": {
package/src/Util.js CHANGED
@@ -280,6 +280,25 @@ export async function parseMultipart(req, _options) {
280
280
  let activePart;
281
281
  let done = false;
282
282
 
283
+ const write = (chunk) => {
284
+ if (options.useMemory) {
285
+ activePart.body.push(chunk);
286
+ } else {
287
+ activePart.body.write(chunk);
288
+ }
289
+ };
290
+
291
+ const newPart = () => {
292
+ // create new part
293
+ const bodyFile = tempFilename();
294
+ activePart = {
295
+ headers: {},
296
+ bodyFile: options.useMemory ? undefined : bodyFile,
297
+ body: options.useMemory ? [] : createWriteStream(bodyFile, { flags: "wx" })
298
+ };
299
+ parts.push(activePart);
300
+ };
301
+
283
302
  while (stream.readable) {
284
303
  // next next chunk
285
304
  let chunk = stream.read(BUFFER_SIZE);
@@ -295,53 +314,42 @@ export async function parseMultipart(req, _options) {
295
314
  const boundaryIndex = combined.indexOf(boundary);
296
315
  const isLastBoundary = combined[boundaryIndex + boundary.length] === 45 && combined[boundaryIndex + boundary.length + 1] === 45;
297
316
 
298
- if (boundaryIndex > -1) {
299
- if (boundaryIndex > 0) {
300
- if (options.useMemory) {
301
- activePart.body.push(combined.slice(0, boundaryIndex));
302
- } else {
303
- activePart.body.write(combined.slice(0, boundaryIndex));
304
- }
305
- }
317
+ if (boundaryIndex === -1) {
318
+ lastChunk = combined;
319
+ break;
320
+ }
306
321
 
307
- if (isLastBoundary) {
308
- combined = combined.slice(boundaryIndex + boundary.length + 2);
309
- done = true;
310
- break;
311
- }
322
+ if (boundaryIndex > 0) {
323
+ write(combined.subarray(0, boundaryIndex));
324
+ }
312
325
 
313
- // create new part
314
- const bodyFile = tempFilename();
315
- activePart = {
316
- headers: {},
317
- bodyFile: options.useMemory ? undefined : bodyFile,
318
- body: options.useMemory ? [] : createWriteStream(bodyFile, { flags: "wx" })
319
- };
320
- parts.push(activePart);
321
-
322
- const endOfHeaders = combined.indexOf(END_OF_HEADERS, boundaryIndex);
323
- activePart.headers = combined
324
- .slice(boundaryIndex + boundary.length + 2, endOfHeaders).toString()
325
- .split("\r\n")
326
- .reduce((acc, val) => {
327
- const [header, value] = val.split(": ");
328
- acc[header.toLowerCase()] = value;
329
- return acc;
330
- }, {});
331
-
332
- combined = combined.slice(endOfHeaders + END_OF_HEADERS.length);
326
+ if (isLastBoundary) {
327
+ combined = combined.subarray(boundaryIndex + boundary.length + 2);
328
+ done = true;
329
+ break;
333
330
  }
334
331
 
332
+ newPart();
333
+
334
+ const endOfHeaders = combined.indexOf(END_OF_HEADERS, boundaryIndex);
335
+
336
+ activePart.headers = combined
337
+ .subarray(boundaryIndex + boundary.length + 2, endOfHeaders).toString()
338
+ .split("\r\n")
339
+ .reduce((acc, val) => {
340
+ const [header, value] = val.split(": ");
341
+ acc[header.toLowerCase()] = value;
342
+ return acc;
343
+ }, {});
344
+
345
+ combined = combined.subarray(endOfHeaders + END_OF_HEADERS.length);
346
+
335
347
  lastChunk = combined;
336
348
  }
337
349
 
338
350
  // there's no boundary in the chunk, add it to active part
339
351
  if (activePart && lastChunk.length > 0 && !done) {
340
- if (options.useMemory) {
341
- activePart.body.push(lastChunk);
342
- } else {
343
- activePart.body.write(lastChunk);
344
- }
352
+ write(lastChunk);
345
353
  lastChunk = Buffer.alloc(0);
346
354
  }
347
355
  }
package/src/WebServer.js CHANGED
@@ -98,7 +98,9 @@ class WebServer {
98
98
 
99
99
  res.on("finish", () => {
100
100
  handled = true;
101
- currentResolve();
101
+ if (currentResolve) {
102
+ currentResolve();
103
+ }
102
104
  });
103
105
 
104
106
  let router = routers.shift();
package/test/gateway.js CHANGED
@@ -8,21 +8,13 @@ class TestService extends Service {
8
8
  }
9
9
 
10
10
  async "POST /upload"(req, res) {
11
- try {
12
- const parts = await parseMultipart(req, { useMemory: false });
11
+ const parts = await parseMultipart(req, { useMemory: false });
13
12
 
14
- for (const part of parts) {
15
- console.debug(part?.body);
16
- }
17
-
18
- res.send("OK");
19
- } catch (e) {
20
- console.error(e);
13
+ for (const part of parts) {
14
+ console.log(part.body);
21
15
  }
22
- }
23
16
 
24
- "GET /test"(req, res) {
25
- res.send("Hello World - /test");
17
+ res.send("OK");
26
18
  }
27
19
 
28
20
  }
package/test/upload.js ADDED
@@ -0,0 +1,23 @@
1
+ import { write } from "fs";
2
+ import { Service, streamToBuffer } from "../exports.js";
3
+ import { parseMultipart } from "../src/Util.js";
4
+ import { writeFile } from "fs/promises";
5
+
6
+ class UploadService extends Service {
7
+
8
+ async "POST /upload"(req, res) {
9
+ const files = await parseMultipart(req, { useMemory: false });
10
+
11
+ for (const file of files) {
12
+ file.body = await streamToBuffer(file.body);
13
+ console.log(`${file.filename} ${file.headers["content-type"]} size=${file.body.length}`);
14
+
15
+ await writeFile("/tmp/temp.pdf", file.body);
16
+ }
17
+
18
+ res.send("ok");
19
+ }
20
+
21
+ }
22
+
23
+ UploadService.start({ middleware: { raw: false } });
File without changes