scttcper / koa-simple-ratelimit

Simple rate limiter for Koa.js v2 web framework
MIT License
17 stars 5 forks source link

Run distribution .js file Error after typescript compiled #148

Closed ShenQingchuan closed 5 years ago

ShenQingchuan commented 5 years ago

My Enviorment:

$ node -v
v10.16.0

$ npm -v
6.9.0

app.use(koa_simple_ratelimit_1.default({ ^ TypeError: koa_simple_ratelimit_1.default is not a function at Object. (/Users/macbook/Documents/Repository/DevLand/api.sicnurpz.online/dist/apiserver.js:29:40) at Module._compile (internal/modules/cjs/loader.js:776:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:829:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Issue May be caused by?

import ratelimt from 'koa-simple-ratelimit';          

// I use the 'redis' , just the same as your README.md documentation...
// but I believe that this way is correct ...
import * as redis from 'redis';                 

// I call the ratelimit() function in this way...
let app = new Koa();
app.use(ratelimt({      //  <-  seems like this function compiled by typescript error ?? 
    db: redis.createClient(),
    duration: 30000,
    max: 10
}));

Due to this middleware installation failed, my koa app ran in crash...

ShenQingchuan commented 5 years ago

Checked in Your dist index.d.ts

/**
 * Expose `ratelimit()`
 */
export default ratelimit;

so I turned my views into dist/index.js

// line 30:34

/**
 * Expose `ratelimit()`
 */
exports.default = ratelimit;
module.exports = ratelimit;

exports.default <= it seems like not a correct way for commonjs ??? ( I'm not quite sure about that ...)

Tricky way to solve ( Very Strange ... evenv funny )

I changed my dist/app.js :

//kapp.use(koa_simple_ratelimit_1.default({
//                           ^ with no .default call
   kapp.use(koa_simple_ratelimit_1({
    db: redis.createClient(),
    duration: 30000,
    max: 5
}));

Then my app runs correctly but I didn't check it this rate limiter works...

scttcper commented 5 years ago

it was time to finally remove the baggage of the old export. Thanks for making this issue. This should be fixed in v4 https://github.com/scttcper/koa-simple-ratelimit/releases/tag/v4.0.0

ShenQingchuan commented 5 years ago

Thanks for Reply

And I also found something useful 👍 I checked those conversations in https://github.com/microsoft/TypeScript/issues/5565 and I'm now pretty sure about that the export should be like that:

// if we use import like this:
import * as ratelimit from 'koa-simple-ratelimit'

// export shoud be :
export = ratelimit

those conversations in the typescript repository is very helpful and I really read them carefully in last few minutes...

ShenQingchuan commented 5 years ago

it was time to finally remove the baggage of the old export. Thanks for making this issue. This should be fixed in v4 https://github.com/scttcper/koa-simple-ratelimit/releases/tag/v4.0.0

Would it be supported in npm ( I mean I can install v4.0.0 by npm/yarn ) soon?

I'm under China time now so it's already night now, it's time to sleep, I wish I can fix this in my app's runtime next morning LOL🤣 it's really a typescript's magical tour for me...