Skip to content

Commit f6c88f3

Browse files
committed
fix: replace json-string-splitter library with local function
Signed-off-by: Maksim Sukharev <[email protected]>
1 parent d9e2207 commit f6c88f3

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/utils/logfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { ILogEntry, IRawLogEntry } from '../interfaces'
77

88
import { parseException } from './exception'
99
import { logger } from './logger'
10+
import { splitter } from './splitter'
1011

1112
/**
1213
* Parse a given log file
@@ -30,7 +31,6 @@ export async function parseLogString(raw: string): Promise<ILogEntry[]> {
3031
} catch (e) {
3132
logger.debug('falling back to json splitter')
3233

33-
const splitter = (await import('json-string-splitter')).default
3434
// the input might have had its data reformatted, breaking the original newline separated json
3535
const lines = splitter(raw).jsons
3636
entries = lines.map(tryParseJSON)

src/utils/splitter.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud Gmbh and Nextcloud contributors
3+
* SPDX-License-Identifier: LGPL-3.0-or-later
4+
*/
5+
6+
/**
7+
* Copied from Densaugeo/JSON-String-Splitter
8+
*
9+
* Split concatenated JSON strings
10+
* Accepts a string consisting of one or more valid JSON substrings and splits it. Any remaining string after the end of the last complete JSON substring is returned in the 'remainder' field.
11+
* Passing in invalid JSON can result in garbage output
12+
*
13+
* @param {string} string The string to look for JSON in
14+
* @returns {{ jsons: string[], remainder: '' }}
15+
*
16+
* @example
17+
* var splitter = require('json-string-splitter');
18+
*
19+
* var pieces = splitter('{"foo":"bar"}{"more":"json"}{"partial":"json"');
20+
*
21+
* console.log(pieces.jsons[0]); // '{"foo":"bar"}'
22+
* console.log(pieces.jsons[1]); // '{"more":"json"}'
23+
* console.log(pieces.remainder); // '{"partial":"json"'
24+
*/
25+
export function splitter(string) {
26+
var START = 0, JSON = 1, STRING = 2, ESCAPE = 3;
27+
28+
var state = START;
29+
var nesting_level = 0;
30+
var json_start = null;
31+
var bounds = [];
32+
33+
for(var i = 0 || 0; i < string.length; ++i) {
34+
switch(state) {
35+
case START:
36+
switch(string[i]) {
37+
case '{':
38+
++nesting_level;
39+
state = JSON;
40+
json_start = i;
41+
break;
42+
}
43+
break;
44+
case JSON:
45+
switch(string[i]) {
46+
case '{': ++nesting_level; break;
47+
case '}':
48+
--nesting_level;
49+
if(nesting_level === 0) {
50+
state = START;
51+
bounds.push({ start: json_start, end: i + 1 });
52+
}
53+
break;
54+
case '"': state = STRING; break;
55+
}
56+
break;
57+
case STRING:
58+
switch(string[i]) {
59+
case '"': state = JSON; break;
60+
case '\\': state = ESCAPE; break;
61+
}
62+
break;
63+
case ESCAPE:
64+
state = STRING;
65+
break;
66+
}
67+
}
68+
69+
var result = {
70+
jsons: [],
71+
remainder: string.substring(bounds[bounds.length - 1].end),
72+
}
73+
74+
bounds.forEach(function(v) {
75+
result.jsons.push(string.substring(v.start, v.end));
76+
});
77+
78+
return result;
79+
}

0 commit comments

Comments
 (0)