Skip to content

Commit

Permalink
Upgrade formidable to v2 (vpulim#1183)
Browse files Browse the repository at this point in the history
v1 is deprecated.
  • Loading branch information
orgads authored and sandrozbinden-axa committed Oct 6, 2022
1 parent a934e89 commit 20b3fad
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 47 deletions.
113 changes: 99 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"axios-ntlm": "^1.2.0",
"debug": "^4.3.2",
"formidable": "^1.2.2",
"formidable": "^2.0.1",
"get-stream": "^6.0.1",
"lodash": "^4.17.21",
"sax": ">=0.6",
Expand Down Expand Up @@ -50,6 +50,7 @@
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/express": "^4.17.13",
"@types/formidable": "^2.0.4",
"@types/lodash": "^4.14.172",
"@types/node": "^11.15.54",
"@types/request": "^2.48.7",
Expand Down
60 changes: 28 additions & 32 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import * as crypto from 'crypto';
import { MultipartParser } from 'formidable/lib/multipart_parser.js';
import { MultipartParser } from 'formidable';
import { IMTOMAttachments } from './types';

export function passwordDigest(nonce: string, created: string, password: string): string {
Expand Down Expand Up @@ -78,38 +78,34 @@ export function parseMTOMResp(payload: Buffer, boundary: string): IMTOMAttachmen
const parser = new MultipartParser();

parser.initWithBoundary(boundary);
parser.onPartBegin = () => {
resp.parts[partIndex] = {
body: null,
headers: {},
};
data = Buffer.from('');
};

parser.onHeaderField = (b: Buffer, start: number, end: number) => {
headerName = b.slice(start, end).toString();
};

parser.onHeaderValue = (b: Buffer, start: number, end: number) => {
headerValue = b.slice(start, end).toString();
};

parser.onHeaderEnd = () => {
resp.parts[partIndex].headers[headerName.toLowerCase()] = headerValue;
};

parser.onHeadersEnd = () => {};

parser.onPartData = (b: Buffer, start: number, end: number) => {
data = Buffer.concat([data, b.slice(start, end)]);
};

parser.onPartEnd = () => {
resp.parts[partIndex].body = data;
partIndex++;
};
parser.on('data', ({ name, buffer, start, end }) => {
switch (name) {
case 'partBegin':
resp.parts[partIndex] = {
body: null,
headers: {},
};
data = Buffer.from('');
break;
case 'headerField':
headerName = buffer.slice(start, end).toString();
break;
case 'headerValue':
headerValue = buffer.slice(start, end).toString();
break;
case 'headerEnd':
resp.parts[partIndex].headers[headerName.toLowerCase()] = headerValue;
break;
case 'partData':
data = Buffer.concat([data, buffer.slice(start, end)]);
break;
case 'partEnd':
resp.parts[partIndex].body = data;
partIndex++;
break;
}
});

parser.onEnd = () => {};
parser.write(payload);

return resp;
Expand Down

0 comments on commit 20b3fad

Please sign in to comment.