From 4a87d55988d2c0dc99778352f5356deb132d71ba Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 31 Jan 2022 17:01:42 +0300 Subject: [PATCH 1/3] implement stringify service --- src/__test__/unit/builtInHandler.spec.ts | 6 ++++-- src/internal/builtInServices.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index a8fe5ce13..cd3a6ea3d 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -65,7 +65,9 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[200, ['test']]} | ${0} | ${['test']}} ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - + + ${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${"{\"a\":10,\"b\":20}"}} + ${'sig'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} ${'sig'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} ${'sig'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} @@ -249,7 +251,7 @@ describe('Tests for default handler', () => { // assert expect(res).toMatchObject({ retCode: 1, - result: expect.stringContaining("Only data from the following services is allowed to be signed:"), + result: expect.stringContaining('Only data from the following services is allowed to be signed:'), }); }); diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 6422d1e95..17a8df092 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -129,6 +129,16 @@ export function builtInServices(context: BuiltInServiceContext): { }, }, + debug: { + stringify: (req) => { + let arg = req.args[0]; + if (arg === undefined) { + arg = ''; + } + return success(JSON.stringify(arg)); + }, + }, + sig: { sign: async (req) => { if (req.args.length !== 1) { From 85e4af6315f195d0a0171c9507d8b7d97abc0f1a Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 31 Jan 2022 21:01:03 +0300 Subject: [PATCH 2/3] return '' for empty args list, just value for 1 arg, list of values for any other --- src/__test__/unit/builtInHandler.spec.ts | 2 ++ src/internal/builtInServices.ts | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index cd3a6ea3d..40b4d3893 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -66,7 +66,9 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} + ${'debug'} | ${'stringify'} | ${[]} | ${0} | ${""}} ${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${"{\"a\":10,\"b\":20}"}} + ${'debug'} | ${'stringify'} | ${[1, 2, 3, 4]} | ${0} | ${"[1,2,3,4]"}} ${'sig'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} ${'sig'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 17a8df092..9a712cdc7 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -131,11 +131,15 @@ export function builtInServices(context: BuiltInServiceContext): { debug: { stringify: (req) => { - let arg = req.args[0]; - if (arg === undefined) { - arg = ''; + if(req.args.length === 0) { + return success('') } - return success(JSON.stringify(arg)); + + if(req.args.length === 1) { + return success(JSON.stringify(req.args[0])) + } + + return success(JSON.stringify(req.args)) }, }, From ce44848d0961286c309f54d030a9d9e59041695e Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 1 Feb 2022 15:13:39 +0300 Subject: [PATCH 3/3] pretty-printing and valid sting for zero-argument call --- src/__test__/unit/builtInHandler.spec.ts | 18 +++++++++++++++--- src/internal/builtInServices.ts | 15 +++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 40b4d3893..f7b5ae4bc 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -32,6 +32,18 @@ const testDataWrongSig = [ 157, 159, 25, 109, 95, 160, 181, 65, 254, 238, 47, 156, 240, 151, 58, 14, ]; +const a10b20 = `{ + "a": 10, + "b": 20 +}`; + +const oneTwoThreeFour = `[ + 1, + 2, + 3, + 4 +]`; + describe('Tests for default handler', () => { // prettier-ignore each` @@ -66,9 +78,9 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - ${'debug'} | ${'stringify'} | ${[]} | ${0} | ${""}} - ${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${"{\"a\":10,\"b\":20}"}} - ${'debug'} | ${'stringify'} | ${[1, 2, 3, 4]} | ${0} | ${"[1,2,3,4]"}} + ${'debug'} | ${'stringify'} | ${[]} | ${0} | ${'""'}} + ${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${a10b20}} + ${'debug'} | ${'stringify'} | ${[1, 2, 3, 4]} | ${0} | ${oneTwoThreeFour}} ${'sig'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} ${'sig'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 9a712cdc7..dcdac78fa 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -19,6 +19,7 @@ import { encode, decode } from 'bs58'; import { PeerIdB58 } from 'src'; import { GenericCallServiceHandler, ResultCodes } from './commonTypes'; import { KeyPair } from './KeyPair'; +import { jsonify } from './utils'; const success = (result: any): CallServiceResult => { return { @@ -131,15 +132,17 @@ export function builtInServices(context: BuiltInServiceContext): { debug: { stringify: (req) => { - if(req.args.length === 0) { - return success('') - } + let out; - if(req.args.length === 1) { - return success(JSON.stringify(req.args[0])) + if (req.args.length === 0) { + out = ''; + } else if (req.args.length === 1) { + out = req.args[0]; + } else { + out = req.args; } - return success(JSON.stringify(req.args)) + return success(jsonify(out)); }, },