nomagick commited on
Commit
f2fcacd
·
unverified ·
1 Parent(s): db27b38

fix: a small lru for invalid tokens

Browse files
package-lock.json CHANGED
@@ -28,6 +28,7 @@
28
  "jose": "^5.1.0",
29
  "langdetect": "^0.2.1",
30
  "linkedom": "^0.18.4",
 
31
  "maxmind": "^4.3.18",
32
  "minio": "^7.1.3",
33
  "node-libcurl": "^4.1.0",
@@ -1946,6 +1947,12 @@
1946
  "node": ">= 14"
1947
  }
1948
  },
 
 
 
 
 
 
1949
  "node_modules/@npmcli/fs": {
1950
  "version": "3.1.1",
1951
  "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-3.1.1.tgz",
@@ -3834,6 +3841,12 @@
3834
  "url": "https://github.com/sponsors/isaacs"
3835
  }
3836
  },
 
 
 
 
 
 
3837
  "node_modules/cacache/node_modules/minimatch": {
3838
  "version": "9.0.5",
3839
  "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
@@ -8561,9 +8574,13 @@
8561
  "optional": true
8562
  },
8563
  "node_modules/lru-cache": {
8564
- "version": "10.4.3",
8565
- "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz",
8566
- "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="
 
 
 
 
8567
  },
8568
  "node_modules/lru-memoizer": {
8569
  "version": "2.2.0",
@@ -9970,6 +9987,12 @@
9970
  "url": "https://github.com/sponsors/isaacs"
9971
  }
9972
  },
 
 
 
 
 
 
9973
  "node_modules/path-to-regexp": {
9974
  "version": "6.3.0",
9975
  "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz",
 
28
  "jose": "^5.1.0",
29
  "langdetect": "^0.2.1",
30
  "linkedom": "^0.18.4",
31
+ "lru-cache": "^11.0.2",
32
  "maxmind": "^4.3.18",
33
  "minio": "^7.1.3",
34
  "node-libcurl": "^4.1.0",
 
1947
  "node": ">= 14"
1948
  }
1949
  },
1950
+ "node_modules/@npmcli/agent/node_modules/lru-cache": {
1951
+ "version": "10.4.3",
1952
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz",
1953
+ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==",
1954
+ "license": "ISC"
1955
+ },
1956
  "node_modules/@npmcli/fs": {
1957
  "version": "3.1.1",
1958
  "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-3.1.1.tgz",
 
3841
  "url": "https://github.com/sponsors/isaacs"
3842
  }
3843
  },
3844
+ "node_modules/cacache/node_modules/lru-cache": {
3845
+ "version": "10.4.3",
3846
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz",
3847
+ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==",
3848
+ "license": "ISC"
3849
+ },
3850
  "node_modules/cacache/node_modules/minimatch": {
3851
  "version": "9.0.5",
3852
  "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
 
8574
  "optional": true
8575
  },
8576
  "node_modules/lru-cache": {
8577
+ "version": "11.0.2",
8578
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.0.2.tgz",
8579
+ "integrity": "sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA==",
8580
+ "license": "ISC",
8581
+ "engines": {
8582
+ "node": "20 || >=22"
8583
+ }
8584
  },
8585
  "node_modules/lru-memoizer": {
8586
  "version": "2.2.0",
 
9987
  "url": "https://github.com/sponsors/isaacs"
9988
  }
9989
  },
9990
+ "node_modules/path-scurry/node_modules/lru-cache": {
9991
+ "version": "10.4.3",
9992
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz",
9993
+ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==",
9994
+ "license": "ISC"
9995
+ },
9996
  "node_modules/path-to-regexp": {
9997
  "version": "6.3.0",
9998
  "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz",
package.json CHANGED
@@ -37,6 +37,7 @@
37
  "jose": "^5.1.0",
38
  "langdetect": "^0.2.1",
39
  "linkedom": "^0.18.4",
 
40
  "maxmind": "^4.3.18",
41
  "minio": "^7.1.3",
42
  "node-libcurl": "^4.1.0",
 
37
  "jose": "^5.1.0",
38
  "langdetect": "^0.2.1",
39
  "linkedom": "^0.18.4",
40
+ "lru-cache": "^11.0.2",
41
  "maxmind": "^4.3.18",
42
  "minio": "^7.1.3",
43
  "node-libcurl": "^4.1.0",
src/dto/jina-embeddings-auth.ts CHANGED
@@ -17,9 +17,18 @@ import envConfig from '../shared/services/secrets';
17
  import { JinaEmbeddingsDashboardHTTP } from '../shared/3rd-party/jina-embeddings';
18
  import { JinaEmbeddingsTokenAccount } from '../shared/db/jina-embeddings-token-account';
19
 
 
20
 
21
  const authDtoLogger = logger.child({ service: 'JinaAuthDTO' });
22
 
 
 
 
 
 
 
 
 
23
  const THE_VERY_SAME_JINA_EMBEDDINGS_CLIENT = new JinaEmbeddingsDashboardHTTP(envConfig.JINA_EMBEDDINGS_DASHBOARD_API_KEY);
24
 
25
  @Also({
@@ -81,6 +90,12 @@ export class JinaEmbeddingsAuthDTO extends AutoCastable {
81
  });
82
  }
83
 
 
 
 
 
 
 
84
  let account;
85
  try {
86
  account = await JinaEmbeddingsTokenAccount.fromFirestore(this.bearerToken);
@@ -118,6 +133,7 @@ export class JinaEmbeddingsAuthDTO extends AutoCastable {
118
  authDtoLogger.warn(`Failed to get user brief: ${err}`, { err: marshalErrorLike(err) });
119
 
120
  if (err?.status === 401) {
 
121
  throw new AuthenticationFailedError({
122
  message: 'Invalid API key, please get a new one from https://jina.ai'
123
  });
 
17
  import { JinaEmbeddingsDashboardHTTP } from '../shared/3rd-party/jina-embeddings';
18
  import { JinaEmbeddingsTokenAccount } from '../shared/db/jina-embeddings-token-account';
19
 
20
+ import { LRUCache } from 'lru-cache';
21
 
22
  const authDtoLogger = logger.child({ service: 'JinaAuthDTO' });
23
 
24
+ const invalidTokenLRU = new LRUCache({
25
+ max: 256,
26
+ ttl: 60 * 60 * 1000,
27
+ updateAgeOnGet: false,
28
+ updateAgeOnHas: false,
29
+ });
30
+
31
+
32
  const THE_VERY_SAME_JINA_EMBEDDINGS_CLIENT = new JinaEmbeddingsDashboardHTTP(envConfig.JINA_EMBEDDINGS_DASHBOARD_API_KEY);
33
 
34
  @Also({
 
90
  });
91
  }
92
 
93
+ if (invalidTokenLRU.get(this.bearerToken)) {
94
+ throw new AuthenticationFailedError({
95
+ message: 'Invalid API key, please get a new one from https://jina.ai'
96
+ });
97
+ }
98
+
99
  let account;
100
  try {
101
  account = await JinaEmbeddingsTokenAccount.fromFirestore(this.bearerToken);
 
133
  authDtoLogger.warn(`Failed to get user brief: ${err}`, { err: marshalErrorLike(err) });
134
 
135
  if (err?.status === 401) {
136
+ invalidTokenLRU.set(this.bearerToken, true);
137
  throw new AuthenticationFailedError({
138
  message: 'Invalid API key, please get a new one from https://jina.ai'
139
  });