Skip to content

Commit

Permalink
feat: support multer esm versions via fix-esm package (https://gist.g…
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Nov 22, 2022
1 parent 17e00a8 commit 1e13736
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 135 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ app.listen(3000);
[MIT](LICENSE) © Fangdun Cai


##
##

[npm-img]: https://img.shields.io/npm/v/@koa/multer.svg?style=flat-square

Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* Module dependencies.
*/

const originalMulter = require('multer');
let originalMulter = require('fix-esm').require('multer');

if (originalMulter.default) originalMulter = originalMulter.default;

function multer(options) {
const m = originalMulter(options);
Expand All @@ -30,12 +32,12 @@ function makePromise(multer, name) {

const fn = multer[name];

multer[name] = function() {
multer[name] = function () {
const middleware = Reflect.apply(fn, this, arguments);

return async (ctx, next) => {
await new Promise((resolve, reject) => {
middleware(ctx.req, ctx.res, err => {
middleware(ctx.req, ctx.res, (err) => {
if (err) return reject(err);
if ('request' in ctx) {
if (ctx.req.body) {
Expand Down
32 changes: 17 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,27 @@
"url": "https://www.3imed-jaberi.com/"
}
],
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@koa/router": "^8",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
"@koa/router": "^12",
"concat-stream": "^2",
"eslint-config-xo-lass": "^1.0.3",
"fixpack": "^2.3.1",
"form-data": "^3",
"fs-temp": "^1",
"husky": "^3.1.0",
"eslint-config-xo-lass": "^2.0.1",
"fixpack": "^4.0.0",
"form-data": "^4",
"fs-temp": "^2",
"husky": "^8.0.2",
"koa": "^2",
"lint-staged": "^9.5.0",
"lint-staged": "^13.0.3",
"mocha": "3.x",
"multer": "^1.3.0",
"nyc": "^15.0.0",
"multer": "1",
"nyc": "^15.1.0",
"on-finished": "^2",
"remark-cli": "^7.0.1",
"remark-preset-github": "^0.0.16",
"remark-cli": "^11.0.0",
"remark-preset-github": "^4.0.4",
"rimraf": "^3",
"testdata-w3c-json-form": "^1",
"xo": "^0.25.3"
"xo": "^0.53.1"
},
"engines": {
"node": ">= 8"
Expand Down Expand Up @@ -129,5 +128,8 @@
"no-prototype-builtins": "off",
"prefer-rest-params": "off"
}
},
"dependencies": {
"fix-esm": "^1.0.1"
}
}
13 changes: 6 additions & 7 deletions test/_util.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const fs = require('node:fs');
const path = require('node:path');
const stream = require('node:stream');
const onFinished = require('on-finished');

exports.file = name => {
exports.file = (name) => {
return fs.createReadStream(path.join(__dirname, 'files', name));
};

exports.fileSize = path => {
exports.fileSize = (path) => {
return fs.statSync(path).size;
};

Expand All @@ -31,13 +31,12 @@ exports.submitForm = (multer, form, cb) => {
const res = null;
const ctx = { req, res };
multer(ctx, () => {})
// eslint-disable-next-line promise/prefer-await-to-then
.then(() => {
onFinished(req, () => {
cb(null, req);
});
})
.catch(err_ => {
.catch((err_) => {
onFinished(req, () => {
cb(err_, req);
});
Expand Down
24 changes: 12 additions & 12 deletions test/disk-storage.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-env mocha */

const assert = require('assert');
const assert = require('node:assert');

const fs = require('fs');
const path = require('path');
const temp = require('fs-temp');
const fs = require('node:fs');
const path = require('node:path');
const temp = require('fix-esm').require('fs-temp').default;
const rimraf = require('rimraf');
const FormData = require('form-data');
const util = require('./_util');
const multer = require('..');
const util = require('./_util');

describe('Disk Storage', () => {
let uploadDir;
let upload;

beforeEach(done => {
beforeEach((done) => {
temp.mkdir((err, path) => {
if (err) return done(err);

Expand All @@ -24,11 +24,11 @@ describe('Disk Storage', () => {
});
});

afterEach(done => {
afterEach((done) => {
rimraf(uploadDir, done);
});

it('should process parser/form-data POST request', done => {
it('should process parser/form-data POST request', (done) => {
const form = new FormData();
const parser = upload.single('small0');

Expand All @@ -49,7 +49,7 @@ describe('Disk Storage', () => {
});
});

it('should process empty fields and an empty file', done => {
it('should process empty fields and an empty file', (done) => {
const form = new FormData();
const parser = upload.single('empty');

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Disk Storage', () => {
});
});

it('should process multiple files', done => {
it('should process multiple files', (done) => {
const form = new FormData();
const parser = upload.fields([
{ name: 'empty', maxCount: 1 },
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Disk Storage', () => {
});
});

it('should remove uploaded files on error', done => {
it('should remove uploaded files on error', (done) => {
const form = new FormData();
const parser = upload.single('tiny0');

Expand All @@ -167,7 +167,7 @@ describe('Disk Storage', () => {
});
});

it("should report error when directory doesn't exist", done => {
it("should report error when directory doesn't exist", (done) => {
const directory = path.join(temp.mkdirSync(), 'ghost');
function dest($0, $1, cb) {
cb(null, directory);
Expand Down
34 changes: 17 additions & 17 deletions test/error-handling.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* eslint-env mocha */

const assert = require('assert');
const assert = require('node:assert');

const stream = require('stream');
const stream = require('node:stream');
const FormData = require('form-data');
const util = require('./_util');
const multer = require('..');
const util = require('./_util');

function withLimits(limits, fields) {
const storage = multer.memoryStorage();
return multer({ storage, limits }).fields(fields);
}

describe('Error Handling', () => {
it('should respect parts limit', done => {
it('should respect parts limit', (done) => {
const form = new FormData();
const parser = withLimits({ parts: 1 }, [{ name: 'small0', maxCount: 1 }]);

Expand All @@ -26,7 +26,7 @@ describe('Error Handling', () => {
});
});

it('should respect file size limit', done => {
it('should respect file size limit', (done) => {
const form = new FormData();
const parser = withLimits({ fileSize: 1500 }, [
{ name: 'tiny0', maxCount: 1 },
Expand All @@ -43,7 +43,7 @@ describe('Error Handling', () => {
});
});

it('should respect file count limit', done => {
it('should respect file count limit', (done) => {
const form = new FormData();
const parser = withLimits({ files: 1 }, [
{ name: 'small0', maxCount: 1 },
Expand All @@ -59,7 +59,7 @@ describe('Error Handling', () => {
});
});

it('should respect file key limit', done => {
it('should respect file key limit', (done) => {
const form = new FormData();
const parser = withLimits({ fieldNameSize: 4 }, [
{ name: 'small0', maxCount: 1 }
Expand All @@ -73,7 +73,7 @@ describe('Error Handling', () => {
});
});

it('should respect field key limit', done => {
it('should respect field key limit', (done) => {
const form = new FormData();
const parser = withLimits({ fieldNameSize: 4 }, []);

Expand All @@ -86,7 +86,7 @@ describe('Error Handling', () => {
});
});

it('should respect field value limit', done => {
it('should respect field value limit', (done) => {
const form = new FormData();
const parser = withLimits({ fieldSize: 16 }, []);

Expand All @@ -100,7 +100,7 @@ describe('Error Handling', () => {
});
});

it('should respect field count limit', done => {
it('should respect field count limit', (done) => {
const form = new FormData();
const parser = withLimits({ fields: 1 }, []);

Expand All @@ -113,7 +113,7 @@ describe('Error Handling', () => {
});
});

it('should respect fields given', done => {
it('should respect fields given', (done) => {
const form = new FormData();
const parser = withLimits(undefined, [{ name: 'wrongname', maxCount: 1 }]);

Expand All @@ -126,10 +126,10 @@ describe('Error Handling', () => {
});
});

it('should report errors from storage engines', done => {
it('should report errors from storage engines', (done) => {
const storage = multer.memoryStorage();

storage._removeFile = function(req, file, cb) {
storage._removeFile = function (req, file, cb) {
const err = new Error('Test error');
err.code = 'TEST';
cb(err);
Expand All @@ -155,7 +155,7 @@ describe('Error Handling', () => {
});
});

it('should report errors from busboy constructor', done => {
it('should report errors from busboy constructor', (done) => {
const req = new stream.PassThrough();
const storage = multer.memoryStorage();
const upload = multer({ storage }).single('tiny0');
Expand All @@ -168,13 +168,13 @@ describe('Error Handling', () => {

req.end(body);

upload({ req }, () => {}).catch(err => {
upload({ req }, () => {}).catch((err) => {
assert.equal(err.message, 'Multipart: Boundary not found');
done();
});
});

it('should report errors from busboy parsing', done => {
it('should report errors from busboy parsing', (done) => {
const req = new stream.PassThrough();
const storage = multer.memoryStorage();
const upload = multer({ storage }).single('tiny0');
Expand All @@ -194,7 +194,7 @@ describe('Error Handling', () => {

req.end(body);

upload({ req }, () => {}).catch(err => {
upload({ req }, () => {}).catch((err) => {
assert.equal(err.message, 'Unexpected end of multipart data');
done();
});
Expand Down

0 comments on commit 1e13736

Please sign in to comment.