Skip to content

Commit cb8045d

Browse files
committed
refactor: extracting limited captures implementation to a function for more reliable error handling
1 parent ab98326 commit cb8045d

File tree

2 files changed

+95
-70
lines changed

2 files changed

+95
-70
lines changed

src/core/captureLimits.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Err, Ok } from 'ts-results'
2+
import { CaptureAmountLimit, CapturedCall, CapturedFunction } from './types'
3+
import {
4+
parseCaptureAmountLimit,
5+
parseFilepathFromFunctionId,
6+
sortCapturesByTimestamp
7+
} from './util'
8+
import { getCaptureSize } from './stringify'
9+
10+
export function getLimitedCaptures(
11+
calls: CapturedCall[],
12+
functions: CapturedFunction[],
13+
limit: CaptureAmountLimit
14+
) {
15+
const parsedCaptureAmountLimit = parseCaptureAmountLimit(limit)
16+
17+
// Return the Error result
18+
if (parsedCaptureAmountLimit.err) {
19+
return parsedCaptureAmountLimit
20+
}
21+
22+
// Sort functions and calls
23+
functions = sortCapturesByTimestamp(functions)
24+
calls = sortCapturesByTimestamp(calls)
25+
26+
const combinedFunctionsAndCalls = [...functions, ...calls]
27+
const sortedCombined = sortCapturesByTimestamp(combinedFunctionsAndCalls)
28+
29+
// Handle file limits
30+
if (parsedCaptureAmountLimit.val.type === 'files') {
31+
const capturedFilenames = new Set<string>()
32+
for (let i = 0; i < sortedCombined.length; i++) {
33+
const currentFunctionOrCall = sortedCombined.at(-(i + 1))
34+
if (!currentFunctionOrCall) {
35+
return Err(`Current function or call not found.`)
36+
}
37+
38+
const filepath = parseFilepathFromFunctionId(currentFunctionOrCall.id).unwrap()
39+
capturedFilenames.add(filepath)
40+
if (capturedFilenames.size >= parsedCaptureAmountLimit.val.fileLimit) {
41+
break
42+
}
43+
}
44+
45+
// Use the filepaths of N latest to filter calls and functions
46+
calls = calls.filter((call) => {
47+
const parsedFilepath = parseFilepathFromFunctionId(call.id).unwrap()
48+
if (capturedFilenames.has(parsedFilepath)) {
49+
return true
50+
}
51+
return false
52+
})
53+
54+
functions = functions.filter((func) => {
55+
const parsedFilepath = parseFilepathFromFunctionId(func.id).unwrap()
56+
if (capturedFilenames.has(parsedFilepath)) {
57+
return true
58+
}
59+
return false
60+
})
61+
62+
return Ok({ calls, functions })
63+
}
64+
65+
// Handle byte limits
66+
const duplicateCalls: typeof calls = []
67+
const duplicateFunctions: typeof functions = []
68+
69+
let sizeCounter = 0
70+
71+
for (let i = 0; i < Math.max(calls.length, functions.length); i++) {
72+
if (sizeCounter >= parsedCaptureAmountLimit.val.sizeLimit) {
73+
break
74+
}
75+
const callEntry = calls.at(-(i + 1))
76+
const functionEntry = functions.at(-(i + 1))
77+
if (callEntry) {
78+
duplicateCalls.push(callEntry)
79+
sizeCounter += getCaptureSize(callEntry)
80+
}
81+
if (functionEntry) {
82+
duplicateFunctions.push(functionEntry)
83+
sizeCounter += getCaptureSize(functionEntry)
84+
}
85+
}
86+
87+
return Ok({ calls: duplicateCalls, functions: duplicateFunctions })
88+
}

src/core/storage.ts

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
import { shouldIgnoreCapture } from './captureIgnores'
3939
import { filePersistence, getPersistence } from './persistence/isomorphic'
4040
import { createHumanLog } from './errors'
41+
import { getLimitedCaptures } from './captureLimits'
4142

4243
const loadedCaptures = new Map<string, CaptureDecryptedAndRevived | undefined>()
4344

@@ -215,77 +216,13 @@ export const liveFlytrapStorage: FlytrapStorage = {
215216

216217
// Handle capture amount limits
217218
if (captureAmountLimit) {
218-
const parsedCaptureAmountLimit = parseCaptureAmountLimit(captureAmountLimit)
219-
if (parsedCaptureAmountLimit.err) {
220-
// @todo: human-friendly error
221-
console.error(parsedCaptureAmountLimit.val)
219+
const limitedCapturesResult = getLimitedCaptures(calls, functions, captureAmountLimit)
220+
221+
if (limitedCapturesResult.err) {
222+
console.error(limitedCapturesResult.val)
222223
} else {
223-
functions = sortCapturesByTimestamp(functions)
224-
calls = sortCapturesByTimestamp(calls)
225-
226-
const combinedFunctionsAndCalls = [...functions, ...calls]
227-
const sortedCombined = sortCapturesByTimestamp(combinedFunctionsAndCalls)
228-
229-
if (parsedCaptureAmountLimit.val.type === 'files') {
230-
const fileNamesSet = new Set<string>()
231-
232-
for (let i = 0; i < sortedCombined.length; i++) {
233-
const currentFunctionOrCall = sortedCombined.at(-(i + 1))
234-
if (!currentFunctionOrCall) {
235-
return
236-
}
237-
238-
const filepath = parseFilepathFromFunctionId(currentFunctionOrCall.id).unwrap()
239-
fileNamesSet.add(filepath)
240-
if (fileNamesSet.size >= parsedCaptureAmountLimit.val.fileLimit) {
241-
break
242-
}
243-
}
244-
245-
// Use the filepaths of N latest to filter calls and functions
246-
calls = calls.filter((call) => {
247-
const parsedFilepath = parseFilepathFromFunctionId(call.id).unwrap()
248-
if (fileNamesSet.has(parsedFilepath)) {
249-
return true
250-
}
251-
return false
252-
})
253-
254-
functions = functions.filter((func) => {
255-
const parsedFilepath = parseFilepathFromFunctionId(func.id).unwrap()
256-
if (fileNamesSet.has(parsedFilepath)) {
257-
return true
258-
}
259-
return false
260-
})
261-
} else {
262-
const duplicateCalls: typeof calls = []
263-
const duplicateFunctions: typeof functions = []
264-
265-
let sizeCounter = 0
266-
267-
for (let i = 0; i < Math.max(calls.length, functions.length); i++) {
268-
if (sizeCounter >= parsedCaptureAmountLimit.val.sizeLimit) {
269-
break
270-
}
271-
272-
const callEntry = calls.at(-(i + 1))
273-
const functionEntry = functions.at(-(i + 1))
274-
275-
if (callEntry) {
276-
duplicateCalls.push(callEntry)
277-
sizeCounter += getCaptureSize(callEntry)
278-
}
279-
280-
if (functionEntry) {
281-
duplicateFunctions.push(functionEntry)
282-
sizeCounter += getCaptureSize(functionEntry)
283-
}
284-
}
285-
286-
calls = duplicateCalls
287-
functions = duplicateFunctions
288-
}
224+
calls = limitedCapturesResult.val.calls
225+
functions = limitedCapturesResult.val.functions
289226
}
290227
}
291228

0 commit comments

Comments
 (0)