Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
]
},
"parserOptions": {
"ecmaVersion": 12,
"ecmaVersion": 2022,
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x, 17.x]
node-version: [14.x, 16.x, 17.x, 18.x]
os: [ ubuntu-latest, windows-latest ]
exclude:
- os: windows-latest
node-version: 12.x
runs-on: ${{ matrix.os }}
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
"access": "public"
},
"engines": {
"node": ">=12.0.0 <=17.0.0"
"node": ">=14.0.0 <=18.0.0"
}
}
40 changes: 20 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Switcher {
this.options.retryDurationIn = DEFAULT_RETRY_TIME.charAt(1);
}

this.#initTimedMatch(options);
this._initTimedMatch(options);
}
}

Expand All @@ -89,7 +89,7 @@ class Switcher {
return false;

if (!Switcher.context.exp || Date.now() > (Switcher.context.exp*1000))
await Switcher.#auth();
await Switcher._auth();

const result = await validateSnapshot(
Switcher.context,
Expand Down Expand Up @@ -144,13 +144,13 @@ class Switcher {
if (Switcher.options.offline) {
checkSwitchers(Switcher.snapshot, switcherKeys);
} else {
await Switcher.#auth();
await Switcher._auth();
await services.checkSwitchers(
Switcher.context.url, Switcher.context.token, switcherKeys);
}
}

static #initTimedMatch(options) {
static _initTimedMatch(options) {
if ('regexMaxBlackList' in options) {
TimedMatch.setMaxBlackListed(options.regexMaxBlackList);
}
Expand All @@ -161,13 +161,13 @@ class Switcher {
}


static async #auth() {
static async _auth() {
const response = await services.auth(Switcher.context);
Switcher.context.token = response.token;
Switcher.context.exp = response.exp;
}

static async #checkHealth() {
static async _checkHealth() {
// checks if silent mode is still activated
if (Switcher.context.token === 'SILENT') {
if (!Switcher.context.exp || Date.now() < (Switcher.context.exp*1000)) {
Expand Down Expand Up @@ -224,7 +224,7 @@ class Switcher {
if (input) { this._input = input; }

if (!Switcher.options.offline) {
await Switcher.#auth();
await Switcher._auth();
}
}

Expand All @@ -247,7 +247,7 @@ class Switcher {
errors.push('Missing key field');
}

await this.#executeApiValidation();
await this._executeApiValidation();
if (!Switcher.context.token) {
errors.push('Missing token field');
}
Expand All @@ -259,7 +259,7 @@ class Switcher {

async isItOn(key, input, showReason = false) {
let result;
this.#validateArgs(key, input);
this._validateArgs(key, input);

// verify if query from Bypasser
const bypassKey = Bypasser.searchBypassed(this._key);
Expand All @@ -269,13 +269,13 @@ class Switcher {

// verify if query from snapshot
if (Switcher.options.offline) {
result = await this.#executeOfflineCriteria();
result = await this._executeOfflineCriteria();
} else {
await this.validate();
if (Switcher.context.token === 'SILENT')
result = await this.#executeOfflineCriteria();
result = await this._executeOfflineCriteria();
else
result = await this.#executeOnlineCriteria(showReason);
result = await this._executeOnlineCriteria(showReason);
}

return result;
Expand All @@ -290,8 +290,8 @@ class Switcher {
return this;
}

async #executeOnlineCriteria(showReason) {
if (!this.#useSync())
async _executeOnlineCriteria(showReason) {
if (!this._useSync())
return this._executeAsyncOnlineCriteria(showReason);

const responseCriteria = await services.checkCriteria(
Expand All @@ -313,16 +313,16 @@ class Switcher {
return ExecutionLogger.getExecution(this._key, this._input).response.result;
}

async #executeApiValidation() {
if (this.#useSync()) {
if (await Switcher.#checkHealth() &&
async _executeApiValidation() {
if (this._useSync()) {
if (await Switcher._checkHealth() &&
(!Switcher.context.exp || Date.now() > (Switcher.context.exp * 1000))) {
await this.prepare(this._key, this._input);
}
}
}

async #executeOfflineCriteria() {
async _executeOfflineCriteria() {
const response = await checkCriteriaOffline(
this._key, this._input, Switcher.snapshot);

Expand All @@ -332,12 +332,12 @@ class Switcher {
return response.result;
}

#validateArgs(key, input) {
_validateArgs(key, input) {
if (key) { this._key = key; }
if (input) { this._input = input; }
}

#useSync() {
_useSync() {
return this._delay == 0 || !ExecutionLogger.getExecution(this._key, this._input);
}

Expand Down
46 changes: 23 additions & 23 deletions src/lib/utils/timed-match/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ const cp = require('child_process');

class TimedMatch {

static #worker = TimedMatch.#createChildProcess();
static #blacklisted = [];
static #maxBlackListed = 50;
static #maxTimeLimit = 3000;
static _worker = this._createChildProcess();
static _blacklisted = [];
static _maxBlackListed = 50;
static _maxTimeLimit = 3000;

static async tryMatch(values, input) {
let result = false;
let timer, resolveListener;

if (TimedMatch.#isBlackListed({ values, input }))
if (this._isBlackListed({ values, input }))
return false;

const matchPromise = new Promise((resolve) => {
resolveListener = resolve;
TimedMatch.#worker.on('message', resolveListener);
TimedMatch.#worker.send({ values, input });
this._worker.on('message', resolveListener);
this._worker.send({ values, input });
});

const matchTimer = new Promise((resolve) => {
timer = setTimeout(() => {
TimedMatch.#resetWorker({ values, input });
this._resetWorker({ values, input });
resolve(false);
}, TimedMatch.#maxTimeLimit);
}, this._maxTimeLimit);
});

await Promise.any([matchPromise, matchTimer]).then((value) => {
TimedMatch.#worker.off('message', resolveListener);
await Promise.race([matchPromise, matchTimer]).then((value) => {
this._worker.off('message', resolveListener);
clearTimeout(timer);
result = value;
});
Expand All @@ -37,36 +37,36 @@ class TimedMatch {
}

static clearBlackList() {
TimedMatch.#blacklisted = [];
this._blacklisted = [];
}

static setMaxBlackListed(value) {
TimedMatch.#maxBlackListed = value;
this._maxBlackListed = value;
}

static setMaxTimeLimit(value) {
TimedMatch.#maxTimeLimit = value;
this._maxTimeLimit = value;
}

static #isBlackListed({ values, input }) {
const bls = TimedMatch.#blacklisted.filter(bl => bl.input == input && bl.res == values);
static _isBlackListed({ values, input }) {
const bls = this._blacklisted.filter(bl => bl.input == input && bl.res == values);
return bls.length;
}

static #resetWorker({ values, input }) {
TimedMatch.#worker.kill();
TimedMatch.#worker = TimedMatch.#createChildProcess();
static _resetWorker({ values, input }) {
this._worker.kill();
this._worker = this._createChildProcess();

if (TimedMatch.#blacklisted.length == TimedMatch.#maxBlackListed)
TimedMatch.#blacklisted.splice(0, 1);
if (this._blacklisted.length == this._maxBlackListed)
this._blacklisted.splice(0, 1);

TimedMatch.#blacklisted.push({
this._blacklisted.push({
res: values,
input
});
}

static #createChildProcess() {
static _createChildProcess() {
const match_proc = cp.fork(`${__dirname}/match-proc.js`, {
stdio: 'ignore'
});
Expand Down
2 changes: 1 addition & 1 deletion test/timed-match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const nokRE = ['^(([a-z])+.)+[A-Z]([a-z])+$'];
const nokInput = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

const COLD_TIME = 500;
const WARM_TIME = 10;
const WARM_TIME = 50;
const TIMEOUT = 1000;

const getTimer = (timer) => (timer - Date.now()) * -1;
Expand Down