Skip to content

Commit 274154b

Browse files
timfishAbhiPrasad
authored andcommitted
feat(node): Remove lru_map dependency (#9300)
1 parent 595ee3c commit 274154b

File tree

13 files changed

+113
-44
lines changed

13 files changed

+113
-44
lines changed

packages/deno/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
"@sentry/browser": "7.74.1",
2222
"@sentry/core": "7.74.1",
2323
"@sentry/types": "7.74.1",
24-
"@sentry/utils": "7.74.1",
25-
"lru_map": "^0.3.3"
24+
"@sentry/utils": "7.74.1"
2625
},
2726
"devDependencies": {
28-
"@rollup/plugin-commonjs": "^25.0.5",
2927
"@rollup/plugin-typescript": "^11.1.5",
3028
"@types/node": "20.8.2",
3129
"rollup-plugin-dts": "^6.1.0"

packages/deno/rollup.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @ts-check
22
import nodeResolve from '@rollup/plugin-node-resolve';
3-
import commonjs from '@rollup/plugin-commonjs';
43
import sucrase from '@rollup/plugin-sucrase';
54
import { defineConfig } from 'rollup';
65

@@ -21,7 +20,6 @@ export default defineConfig({
2120
nodeResolve({
2221
extensions: ['.mjs', '.js', '.json', '.node', '.ts', '.tsx'],
2322
}),
24-
commonjs(),
2523
sucrase({ transforms: ['typescript'] }),
2624
],
2725
});

packages/deno/src/integrations/contextlines.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Event, EventProcessor, Integration, StackFrame } from '@sentry/types';
2-
import { addContextToFrame } from '@sentry/utils';
3-
import { LRUMap } from 'lru_map';
2+
import { addContextToFrame, LRUMap } from '@sentry/utils';
43

54
const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100);
65
const DEFAULT_LINES_OF_CONTEXT = 7;

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@sentry/utils": "7.74.1",
3030
"cookie": "^0.5.0",
3131
"https-proxy-agent": "^5.0.0",
32-
"lru_map": "^0.3.3",
3332
"tslib": "^2.4.1 || ^1.9.3"
3433
},
3534
"devDependencies": {

packages/node/src/integrations/contextlines.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
2-
import { addContextToFrame } from '@sentry/utils';
2+
import { addContextToFrame, LRUMap } from '@sentry/utils';
33
import { readFile } from 'fs';
4-
import { LRUMap } from 'lru_map';
54

65
const FILE_CONTENT_CACHE = new LRUMap<string, string[] | null>(100);
76
const DEFAULT_LINES_OF_CONTEXT = 7;

packages/node/src/integrations/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
fill,
1313
generateSentryTraceHeader,
1414
logger,
15+
LRUMap,
1516
stringMatchesSomePattern,
1617
} from '@sentry/utils';
1718
import type * as http from 'http';
1819
import type * as https from 'https';
19-
import { LRUMap } from 'lru_map';
2020

2121
import type { NodeClient } from '../client';
2222
import { NODE_VERSION } from '../nodeVersion';

packages/node/src/integrations/localvariables.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-disable max-lines */
22
import type { Event, EventProcessor, Exception, Hub, Integration, StackFrame, StackParser } from '@sentry/types';
3-
import { logger } from '@sentry/utils';
3+
import { logger, LRUMap } from '@sentry/utils';
44
import type { Debugger, InspectorNotification, Runtime, Session } from 'inspector';
5-
import { LRUMap } from 'lru_map';
65

76
import { NODE_VERSION } from '../nodeVersion';
87
import type { NodeClientOptions } from '../types';
@@ -470,8 +469,8 @@ export class LocalVariables implements Integration {
470469
}
471470

472471
// Check if we have local variables for an exception that matches the hash
473-
// delete is identical to get but also removes the entry from the cache
474-
const cachedFrames = this._cachedFrames.delete(hash);
472+
// remove is identical to get but also removes the entry from the cache
473+
const cachedFrames = this._cachedFrames.remove(hash);
475474

476475
if (cachedFrames === undefined) {
477476
return;

packages/node/src/integrations/undici/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
dynamicSamplingContextToSentryBaggageHeader,
66
generateSentryTraceHeader,
77
getSanitizedUrlString,
8+
LRUMap,
89
parseUrl,
910
stringMatchesSomePattern,
1011
} from '@sentry/utils';
11-
import { LRUMap } from 'lru_map';
1212

1313
import type { NodeClient } from '../../client';
1414
import { NODE_VERSION } from '../../nodeVersion';

packages/node/test/integrations/localvariables.test.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ClientOptions, EventProcessor } from '@sentry/types';
2+
import type { LRUMap } from '@sentry/utils';
23
import type { Debugger, InspectorNotification } from 'inspector';
3-
import type { LRUMap } from 'lru_map';
44

55
import { defaultStackParser } from '../../src';
66
import type { DebugSession, FrameVariables } from '../../src/integrations/localvariables';
@@ -178,11 +178,7 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
178178

179179
expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(1);
180180

181-
let frames: FrameVariables[] | undefined;
182-
183-
(localVariables as unknown as LocalVariablesPrivate)._cachedFrames.forEach(f => {
184-
frames = f;
185-
});
181+
const frames: FrameVariables[] = (localVariables as unknown as LocalVariablesPrivate)._cachedFrames.values()[0];
186182

187183
expect(frames).toBeDefined();
188184

@@ -274,11 +270,7 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
274270

275271
expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(1);
276272

277-
let frames: FrameVariables[] | undefined;
278-
279-
(localVariables as unknown as LocalVariablesPrivate)._cachedFrames.forEach(f => {
280-
frames = f;
281-
});
273+
const frames: FrameVariables[] = (localVariables as unknown as LocalVariablesPrivate)._cachedFrames.values()[0];
282274

283275
expect(frames).toBeDefined();
284276

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export * from './userIntegrations';
3232
export * from './cache';
3333
export * from './eventbuilder';
3434
export * from './anr';
35+
export * from './lru';

packages/utils/src/lru.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/** A simple Least Recently Used map */
2+
export class LRUMap<K, V> {
3+
private readonly _cache: Map<K, V>;
4+
5+
public constructor(private readonly _maxSize: number) {
6+
this._cache = new Map<K, V>();
7+
}
8+
9+
/** Get the current size of the cache */
10+
public get size(): number {
11+
return this._cache.size;
12+
}
13+
14+
/** Get an entry or undefined if it was not in the cache. Re-inserts to update the recently used order */
15+
public get(key: K): V | undefined {
16+
const value = this._cache.get(key);
17+
if (value === undefined) {
18+
return undefined;
19+
}
20+
// Remove and re-insert to update the order
21+
this._cache.delete(key);
22+
this._cache.set(key, value);
23+
return value;
24+
}
25+
26+
/** Insert an entry and evict an older entry if we've reached maxSize */
27+
public set(key: K, value: V): void {
28+
if (this._cache.size >= this._maxSize) {
29+
// keys() returns an iterator in insertion order so keys().next() gives us the oldest key
30+
this._cache.delete(this._cache.keys().next().value);
31+
}
32+
this._cache.set(key, value);
33+
}
34+
35+
/** Remove an entry and return the entry if it was in the cache */
36+
public remove(key: K): V | undefined {
37+
const value = this._cache.get(key);
38+
if (value) {
39+
this._cache.delete(key);
40+
}
41+
return value;
42+
}
43+
44+
/** Clear all entries */
45+
public clear(): void {
46+
this._cache.clear();
47+
}
48+
49+
/** Get all the keys */
50+
public keys(): Array<K> {
51+
return Array.from(this._cache.keys());
52+
}
53+
54+
/** Get all the values */
55+
public values(): Array<V> {
56+
const values: V[] = [];
57+
this._cache.forEach(value => values.push(value));
58+
return values;
59+
}
60+
}

packages/utils/test/lru.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { LRUMap } from '../src/lru';
2+
3+
describe('LRUMap', () => {
4+
test('evicts older entries when reaching max size', () => {
5+
const map = new LRUMap<string, string>(3);
6+
map.set('a', '1');
7+
map.set('b', '2');
8+
map.set('c', '3');
9+
map.set('d', '4');
10+
map.set('e', '5');
11+
12+
expect(map.keys()).toEqual(['c', 'd', 'e']);
13+
});
14+
15+
test('updates last used when calling get', () => {
16+
const map = new LRUMap<string, string>(3);
17+
map.set('a', '1');
18+
map.set('b', '2');
19+
map.set('c', '3');
20+
21+
map.get('a');
22+
23+
map.set('d', '4');
24+
map.set('e', '5');
25+
26+
expect(map.keys()).toEqual(['a', 'd', 'e']);
27+
});
28+
29+
test('removes and returns entry', () => {
30+
const map = new LRUMap<string, string>(3);
31+
map.set('a', '1');
32+
map.set('b', '2');
33+
map.set('c', '3');
34+
map.set('d', '4');
35+
map.set('e', '5');
36+
37+
expect(map.remove('c')).toEqual('3');
38+
39+
expect(map.keys()).toEqual(['d', 'e']);
40+
});
41+
});

yarn.lock

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4800,18 +4800,6 @@
48004800
magic-string "^0.25.7"
48014801
resolve "^1.17.0"
48024802

4803-
"@rollup/plugin-commonjs@^25.0.5":
4804-
version "25.0.5"
4805-
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-25.0.5.tgz#0bac8f985a5de151b4b09338847f8c7f20a28a29"
4806-
integrity sha512-xY8r/A9oisSeSuLCTfhssyDjo9Vp/eDiRLXkg1MXCcEEgEjPmLU+ZyDB20OOD0NlyDa/8SGbK5uIggF5XTx77w==
4807-
dependencies:
4808-
"@rollup/pluginutils" "^5.0.1"
4809-
commondir "^1.0.1"
4810-
estree-walker "^2.0.2"
4811-
glob "^8.0.3"
4812-
is-reference "1.2.1"
4813-
magic-string "^0.27.0"
4814-
48154803
"@rollup/plugin-json@^4.0.0", "@rollup/plugin-json@^4.1.0":
48164804
version "4.1.0"
48174805
resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.1.0.tgz#54e09867ae6963c593844d8bd7a9c718294496f3"
@@ -19997,11 +19985,6 @@ lru-cache@^9.0.0:
1999719985
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.0.0.tgz#b9e2a6a72a129d81ab317202d93c7691df727e61"
1999819986
integrity sha512-svTf/fzsKHffP42sujkO/Rjs37BCIsQVRCeNYIm9WN8rgT7ffoUnRtZCqU+6BqcSBdv8gwJeTz8knJpgACeQMw==
1999919987

20000-
lru_map@^0.3.3:
20001-
version "0.3.3"
20002-
resolved "https://registry.yarnpkg.com/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd"
20003-
integrity sha1-tcg1G5Rky9dQM1p5ZQoOwOVhGN0=
20004-
2000519988
lunr@^2.3.8:
2000619989
version "2.3.9"
2000719990
resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1"

0 commit comments

Comments
 (0)