jhurliman / node-rate-limiter

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
MIT License
1.5k stars 132 forks source link

Unexpected token '?' #76

Closed xeroxstar closed 3 years ago

xeroxstar commented 3 years ago

I am running this example:

import { RateLimiter } from "limiter";

// Allow 150 requests per hour (the Twitter search limit). Also understands
// 'second', 'minute', 'day', or a number of milliseconds
const limiter = new RateLimiter({ tokensPerInterval: 150, interval: "hour" });

async function sendRequest() {
  // This call will throw if we request more than the maximum number of requests
  // that were set in the constructor
  // remainingRequests tells us how many additional requests could be sent
  // right this moment
  const remainingRequests = await limiter.removeTokens(1);
  callMyRequestSendingFunction(...);
}

and i am getting this error:

this.fireImmediately = fireImmediately ?? false;
                                                ^

SyntaxError: Unexpected token '?'
jhurliman commented 3 years ago

What environment are you running this in?

Sir-Will commented 3 years ago

Same issue with NodeJS v12.3.1, Yarn 1.22.5 and Ubuntu 16.04.7.

xeroxstar commented 3 years ago

What environment are you running this in?

node v12.13.0 npm v6.12 windows 10

I was able to fix the the module by replacing the line this.fireImmediately = fireImmediately ?? false; to this.fireImmediately = fireImmediately || false; in ratelimiter.js. But as I understand the module need to be compiled with "babel-plugin-proposal-nullish-coalescing-operator" to make it work without any changes to the files.

jhurliman commented 3 years ago

Thanks. I think the issue is targeting ESNext with the TypeScript -> JS compilation, which breaks support for node v12. I'll change the compilation target to ES2019.

jhurliman commented 3 years ago

Fixed in https://github.com/jhurliman/node-rate-limiter/pull/79