Skip to content

Commit 6f9c69c

Browse files
authored
[Console] Fix windows bug with line endings (elastic#257367)
1 parent ce1bd82 commit 6f9c69c

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/platform/packages/shared/kbn-monaco/src/languages/console/parser.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,27 @@ describe('console parser', () => {
195195
{ startOffset: 22, endOffset: 32 },
196196
]);
197197
});
198+
199+
describe('CRLF line endings (Windows)', () => {
200+
it('parses a single request with CRLF line endings without errors', () => {
201+
const input = 'GET _search\r\n';
202+
const { requests, errors } = parser(input)!;
203+
expect(errors).toEqual([]);
204+
expect(requests.length).toBe(1);
205+
});
206+
207+
it('parses a bulk request with CRLF line endings without errors', () => {
208+
const input = 'POST /_bulk\r\n{"create": {}}\r\n{"field": "value"}\r\n';
209+
const { requests, errors } = parser(input)!;
210+
expect(errors).toEqual([]);
211+
expect(requests.length).toBe(1);
212+
});
213+
214+
it('parses multiple requests separated by CRLF without errors', () => {
215+
const input = 'GET _search\r\nPOST _test_index\r\n';
216+
const { requests, errors } = parser(input)!;
217+
expect(errors).toEqual([]);
218+
expect(requests.length).toBe(2);
219+
});
220+
});
198221
});

src/platform/packages/shared/kbn-monaco/src/languages/console/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export const createParser = (): ConsoleParser => {
207207
}
208208
};
209209
const newLine = function () {
210+
if (ch === '\r') ch = next(); // handle CRLF: skip \r before \n
210211
if (ch === '\n') ch = next();
211212
};
212213
const word = function () {
@@ -355,7 +356,7 @@ export const createParser = (): ConsoleParser => {
355356

356357
const url = function () {
357358
let parsedUrl = '';
358-
while (ch && ch !== '\n') {
359+
while (ch && ch !== '\n' && ch !== '\r') {
359360
parsedUrl += ch;
360361
next();
361362
}

src/platform/plugins/shared/console/public/application/hooks/use_send_current_request/send_request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
101101
// If the request data contains multiple data objects (e.g. bulk request)
102102
// ES only accepts it if each object is on a single line
103103
// Therefore, we need to remove all new line characters from each data object
104-
const unformattedData = req.data.map((body) => body.replaceAll('\n', ''));
104+
const unformattedData = req.data.map((body) => body.replace(/[\r\n]/g, ''));
105105

106106
let data = collapseLiteralStrings(unformattedData.join('\n'));
107107
if (data) {

0 commit comments

Comments
 (0)