Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jul 16, 2021
1 parent 53e4f89 commit aadaf4e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
11 changes: 1 addition & 10 deletions x-pack/plugins/apm/server/utils/string_from_buffer_rt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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', () => {
Expand Down
15 changes: 4 additions & 11 deletions x-pack/plugins/apm/server/utils/string_from_buffer_rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Buffer, unknown>(
'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);
Expand Down

0 comments on commit aadaf4e

Please sign in to comment.