koajs / multer

Middleware for handling `multipart/form-data` for koa, based on Express's multer.
MIT License
170 stars 26 forks source link

don't use fix-esm package #55

Open arboledavargas opened 8 months ago

arboledavargas commented 8 months ago

fix-esm package uses @babel/plugin-proposal-export-namespace-from@7.18.9: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained.

johnleider commented 8 months ago

I just ran into this issue. Had to downgrade to v3.0.0

/home/john/sites/api/node_modules/fix-esm/index.js:40
                Module._extensions[".mjs"] = newLoader;
                           ^
TypeError: Cannot assign to read only property '.mjs' of object '[object Object]'
    at Object.register (/home/john/sites/api/node_modules/fix-esm/index.js:40:28)
    at Object.require (/home/john/sites/api/node_modules/fix-esm/index.js:47:8)
    at /home/john/sites/api/node_modules/@koa/multer/index.js:14:41
    at Object.<anonymous> (/home/john/sites/api/node_modules/@koa/multer/index.js:8:3)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Object.j (/home/john/sites/api/node_modules/tsx/dist/cjs/index.cjs:1:1197)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1021:12)
    at cjsLoader (node:internal/modules/esm/translators:366:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:315:7)
error Command failed with exit code 1.
lisonge commented 5 months ago

If you use it directly, you only need a simple replacement

your code

import multer from '@koa/multer'

// -------- your other code --------

use https://github.com/koajs/multer/blob/master/index.js replace your code

import originalMulter from 'multer';

function multer(options) {
  const m = originalMulter(options);

  makePromise(m, 'any');
  makePromise(m, 'array');
  makePromise(m, 'fields');
  makePromise(m, 'none');
  makePromise(m, 'single');

  return m;
}

function makePromise(multer, name) {
  if (!multer[name]) return;

  const fn = multer[name];

  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) => {
          if (err) return reject(err);
          if ('request' in ctx) {
            if (ctx.req.body) {
              ctx.request.body = ctx.req.body;
              delete ctx.req.body;
            }

            if (ctx.req.file) {
              ctx.request.file = ctx.req.file;
              ctx.file = ctx.req.file;
              delete ctx.req.file;
            }

            if (ctx.req.files) {
              ctx.request.files = ctx.req.files;
              ctx.files = ctx.req.files;
              delete ctx.req.files;
            }
          }

          resolve(ctx);
        });
      });

      return next();
    };
  };
}

multer.diskStorage = originalMulter.diskStorage;
multer.memoryStorage = originalMulter.memoryStorage;

// -------- your other code --------
zheung commented 4 months ago

Agreed. Time to clean up the code.

resteinbock commented 1 month ago

+1 -- need this fix