From aadaf4ed09467a0c86b918593f12af3e0ac95d89 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Fri, 16 Jul 2021 09:36:12 -0400 Subject: [PATCH] addressing PR comments --- .../server/utils/string_from_buffer_rt.test.ts | 11 +---------- .../apm/server/utils/string_from_buffer_rt.ts | 15 ++++----------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/x-pack/plugins/apm/server/utils/string_from_buffer_rt.test.ts b/x-pack/plugins/apm/server/utils/string_from_buffer_rt.test.ts index 221970aeb5adda..4e21215cac8b5d 100644 --- a/x-pack/plugins/apm/server/utils/string_from_buffer_rt.test.ts +++ b/x-pack/plugins/apm/server/utils/string_from_buffer_rt.test.ts @@ -32,7 +32,7 @@ const sourceMap = { describe('stringFromBufferRt', () => { describe('decode', () => { - it('converts from buffer to SourceMap', () => { + it('converts from buffer to string', () => { const sourceMapBuffer = Buffer.from(JSON.stringify(sourceMap)); const decoded = stringFromBufferRt.decode(sourceMapBuffer); if (isRight(decoded)) { @@ -41,15 +41,6 @@ describe('stringFromBufferRt', () => { expect(true).toBeFalsy(); } }); - it('converts from string to SourceMap', () => { - const sourceMapString = JSON.stringify(sourceMap); - const decoded = stringFromBufferRt.decode(sourceMapString); - if (isRight(decoded)) { - expect(decoded.right).toEqual(JSON.stringify(sourceMap)); - } else { - expect(true).toBeFalsy(); - } - }); }); describe('encode', () => { it('converts from string to buffer', () => { diff --git a/x-pack/plugins/apm/server/utils/string_from_buffer_rt.ts b/x-pack/plugins/apm/server/utils/string_from_buffer_rt.ts index f1d8d26101f3f4..3e9304361c8636 100644 --- a/x-pack/plugins/apm/server/utils/string_from_buffer_rt.ts +++ b/x-pack/plugins/apm/server/utils/string_from_buffer_rt.ts @@ -4,22 +4,15 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { either } from 'fp-ts/lib/Either'; import * as t from 'io-ts'; export const stringFromBufferRt = new t.Type( - 'toSourceMapRt', + 'stringFromBufferRt', t.string.is, (input, context) => { - return either.chain( - t.unknown.validate(input, context), - (inputAsUnknown) => { - const inputAsString = (inputAsUnknown as Buffer).toString(); - return inputAsString - ? t.success(inputAsString) - : t.failure(input, context, 'could not parse buffer to string'); - } - ); + return Buffer.isBuffer(input) + ? t.success(input.toString('utf-8')) + : t.failure(input, context, 'Input is not a Buffer'); }, (str) => { return Buffer.from(str);