Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use fix-esm package #55

Open
arboledavargas opened this issue Mar 1, 2024 · 4 comments
Open

don't use fix-esm package #55

arboledavargas opened this issue Mar 1, 2024 · 4 comments

Comments

@arboledavargas
Copy link

fix-esm package uses @babel/[email protected]: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained.

@johnleider
Copy link

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
Copy link

lisonge commented May 23, 2024

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
Copy link

zheung commented Jun 22, 2024

Agreed. Time to clean up the code.

@resteinbock
Copy link

+1 -- need this fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants