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.
- package/.vscode/settings.json +2 -1
- package/package.json +1 -1
- package/src/Util.js +46 -38
- package/src/WebServer.js +3 -1
- package/test/gateway.js +4 -12
- package/test/upload.js +23 -0
- package/test/big_upload.js +0 -0
package/.vscode/settings.json
CHANGED
package/package.json
CHANGED
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
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
break;
|
|
311
|
-
}
|
|
322
|
+
if (boundaryIndex > 0) {
|
|
323
|
+
write(combined.subarray(0, boundaryIndex));
|
|
324
|
+
}
|
|
312
325
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
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
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
|
-
|
|
12
|
-
const parts = await parseMultipart(req, { useMemory: false });
|
|
11
|
+
const parts = await parseMultipart(req, { useMemory: false });
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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 } });
|
package/test/big_upload.js
DELETED
|
File without changes
|