Socket
Socket
Sign inDemoInstall

@snapshot-labs/keycard

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@snapshot-labs/keycard - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

6

dist/index.d.ts

@@ -9,2 +9,5 @@ type KeycardParams = {

restricted_monthly: string[];
monthly_counts: Record<string, number>;
limits: Record<string, number>;
reset: number;
};

@@ -23,2 +26,5 @@ export declare class Keycard {

rateLimited?: boolean;
remaining?: number;
reset?: number;
limit?: number;
};

@@ -25,0 +31,0 @@ private callAPI;

50

dist/index.js

@@ -17,4 +17,8 @@ "use strict";

keys = {
// TODO: active and restricted_monthly will be deprecated in the future.
active: [],
restricted_monthly: []
restricted_monthly: [],
monthly_counts: {},
limits: {},
reset: 0
};

@@ -29,2 +33,3 @@ constructor(params) {

}
console.log('[keycard] Initializing keycard...');
this.run();

@@ -47,9 +52,2 @@ }

if (result?.[app]) {
// Useful to debug, Uncomment this to see the keys in the console.
// console.log(
// '[keycard] getKeys: Success! Active keys:',
// result[app].active.length,
// 'Restricted monthly keys:',
// result[app].restricted_monthly.length
// );
this.keys = result[app];

@@ -60,13 +58,27 @@ }

logReq(key) {
if (key && this.configured) {
if (!this.keys.active.includes(key))
return { valid: false };
if (this.keys.restricted_monthly.includes(key))
return { valid: true, rateLimited: true };
// Increase the total count for this key, but don't wait for it to finish.
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.callAPI('log_req', { key }).catch(() => { });
}
// If the keycard doesn't receive any keys (incase of a restart), we don't want to block the request.
return { valid: true, rateLimited: false };
if (!key)
return { valid: false };
const { monthly_counts: activeKeys, limits, reset } = this.keys;
const { secret } = this;
const limit = limits.monthly;
// Unlimited requests to snapshot APIs (example: if hub is sending requests to hub itself or to score-api)
const unlimitedRequests = key === secret;
// If key is not in active keys, it's not valid.
if (!unlimitedRequests && activeKeys[key] === undefined)
return { valid: false };
activeKeys[key]++;
let keyCount = activeKeys[key];
if (unlimitedRequests)
keyCount = 0;
// Increase the total count for this key, but don't wait for it to finish.
if (!unlimitedRequests)
this.callAPI('log_req', { key }).catch();
const rateLimited = keyCount > limit;
return {
valid: true,
rateLimited,
remaining: Math.max(0, limit - keyCount),
reset,
limit
};
}

@@ -73,0 +85,0 @@ async callAPI(method, params = {}) {

{
"name": "@snapshot-labs/keycard",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",

@@ -8,9 +8,10 @@ "main": "dist/index.js",

"scripts": {
"lint": "eslint ./src ./test --ext .ts",
"lint:fix": "yarn lint --fix",
"typecheck": "tsc --noEmit",
"build": "tsc",
"lint": "eslint ./src ./test --ext .ts --fix",
"prepare": "yarn build",
"prepublishOnly": "yarn run lint",
"test": "vitest",
"test:local": "ts-node test/keycard.ts",
"coverage": "vitest run --coverage"
"test": "vitest run --coverage",
"test:local": "ts-node test/keycard.ts"
},

@@ -17,0 +18,0 @@ "dependencies": {

@@ -14,4 +14,8 @@ import fetch from 'cross-fetch';

type AppKeys = {
// TODO: active and restricted_monthly will be deprecated in the future.
active: string[];
restricted_monthly: string[];
monthly_counts: Record<string, number>;
limits: Record<string, number>;
reset: number;
};

@@ -25,4 +29,8 @@

private keys: AppKeys = {
// TODO: active and restricted_monthly will be deprecated in the future.
active: [],
restricted_monthly: []
restricted_monthly: [],
monthly_counts: {},
limits: {},
reset: 0
};

@@ -39,2 +47,3 @@

}
console.log('[keycard] Initializing keycard...');
this.run();

@@ -58,9 +67,2 @@ }

if (result?.[app]) {
// Useful to debug, Uncomment this to see the keys in the console.
// console.log(
// '[keycard] getKeys: Success! Active keys:',
// result[app].active.length,
// 'Restricted monthly keys:',
// result[app].restricted_monthly.length
// );
this.keys = result[app];

@@ -71,13 +73,37 @@ }

logReq(key: string): { valid: boolean; rateLimited?: boolean } {
if (key && this.configured) {
if (!this.keys.active.includes(key)) return { valid: false };
if (this.keys.restricted_monthly.includes(key)) return { valid: true, rateLimited: true };
logReq(key: string): {
valid: boolean;
rateLimited?: boolean;
remaining?: number;
reset?: number;
limit?: number;
} {
if (!key) return { valid: false };
// Increase the total count for this key, but don't wait for it to finish.
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.callAPI('log_req', { key }).catch(() => {});
}
// If the keycard doesn't receive any keys (incase of a restart), we don't want to block the request.
return { valid: true, rateLimited: false };
const { monthly_counts: activeKeys, limits, reset } = this.keys;
const { secret } = this;
const limit = limits.monthly;
// Unlimited requests to snapshot APIs (example: if hub is sending requests to hub itself or to score-api)
const unlimitedRequests = key === secret;
// If key is not in active keys, it's not valid.
if (!unlimitedRequests && activeKeys[key] === undefined) return { valid: false };
activeKeys[key]++;
let keyCount = activeKeys[key];
if (unlimitedRequests) keyCount = 0;
// Increase the total count for this key, but don't wait for it to finish.
if (!unlimitedRequests) this.callAPI('log_req', { key }).catch();
const rateLimited = keyCount > limit;
return {
valid: true,
rateLimited,
remaining: Math.max(0, limit - keyCount),
reset,
limit
};
}

@@ -84,0 +110,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc