From e24c15bf2d9073946b68792af51d107590b40290 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Tue, 15 Mar 2022 22:55:42 +0200 Subject: [PATCH] Upgrade formidable to v2 v1 is deprecated. --- package-lock.json | 113 ++++++++++++++++++++++++++++++++++++++++------ package.json | 3 +- src/utils.ts | 60 ++++++++++++------------ 3 files changed, 129 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index eef8f9b45..7d439ab19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,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", @@ -23,6 +23,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", @@ -252,6 +253,15 @@ "@types/range-parser": "*" } }, + "node_modules/@types/formidable": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/formidable/-/formidable-2.0.4.tgz", + "integrity": "sha512-6HYcnmBCeby/nNGgX9kq1DxUpK2UcB3yoHCr3GzFjjqkpivOdcBSbsXP9NbxLcPEi11Fl/L41rbFCIsteF9sbg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/lodash": { "version": "4.14.172", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.172.tgz", @@ -452,6 +462,11 @@ "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=", "dev": true }, + "node_modules/asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" + }, "node_modules/asn1": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", @@ -1092,6 +1107,15 @@ "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=", "dev": true }, + "node_modules/dezalgo": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz", + "integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=", + "dependencies": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "node_modules/diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", @@ -1692,14 +1716,30 @@ } }, "node_modules/formidable": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.2.tgz", - "integrity": "sha512-V8gLm+41I/8kguQ4/o1D3RIHRmhYFG4pnNyonvua+40rqcEmT4+V71yaZ3B457xbbgCsCfjSPi65u/W6vK1U5Q==", - "deprecated": "Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz", + "integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==", + "dependencies": { + "dezalgo": "1.0.3", + "hexoid": "1.0.0", + "once": "1.4.0", + "qs": "6.9.3" + }, "funding": { "url": "https://ko-fi.com/tunnckoCore/commissions" } }, + "node_modules/formidable/node_modules/qs": { + "version": "6.9.3", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz", + "integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==", + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", @@ -1948,6 +1988,14 @@ "he": "bin/he" } }, + "node_modules/hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==", + "engines": { + "node": ">=8" + } + }, "node_modules/hosted-git-info": { "version": "2.8.9", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", @@ -3174,7 +3222,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "dependencies": { "wrappy": "1" } @@ -4746,8 +4793,7 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "node_modules/write-file-atomic": { "version": "2.4.3", @@ -5079,6 +5125,15 @@ "@types/range-parser": "*" } }, + "@types/formidable": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/formidable/-/formidable-2.0.4.tgz", + "integrity": "sha512-6HYcnmBCeby/nNGgX9kq1DxUpK2UcB3yoHCr3GzFjjqkpivOdcBSbsXP9NbxLcPEi11Fl/L41rbFCIsteF9sbg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/lodash": { "version": "4.14.172", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.172.tgz", @@ -5257,6 +5312,11 @@ "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=", "dev": true }, + "asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" + }, "asn1": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", @@ -5785,6 +5845,15 @@ "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=", "dev": true }, + "dezalgo": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz", + "integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=", + "requires": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", @@ -6248,9 +6317,22 @@ } }, "formidable": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.2.tgz", - "integrity": "sha512-V8gLm+41I/8kguQ4/o1D3RIHRmhYFG4pnNyonvua+40rqcEmT4+V71yaZ3B457xbbgCsCfjSPi65u/W6vK1U5Q==" + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz", + "integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==", + "requires": { + "dezalgo": "1.0.3", + "hexoid": "1.0.0", + "once": "1.4.0", + "qs": "6.9.3" + }, + "dependencies": { + "qs": { + "version": "6.9.3", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz", + "integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==" + } + } }, "forwarded": { "version": "0.1.2", @@ -6430,6 +6512,11 @@ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, + "hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==" + }, "hosted-git-info": { "version": "2.8.9", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", @@ -7381,7 +7468,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -8653,8 +8739,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write-file-atomic": { "version": "2.4.3", diff --git a/package.json b/package.json index b2cb2f890..7e10b0c75 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/utils.ts b/src/utils.ts index 67dd49a4f..fae15bb60 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { @@ -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;