Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/__test__/unit/builtInHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { KeyPair } from '../../internal/KeyPair';
import { Sig, defaultSigGuard, allowServiceFn } from '../../internal/builtins/Sig';
import { toUint8Array } from 'js-base64';

const a10b20 = `{
"a": 10,
"b": 20
}`;

const oneTwoThreeFour = `[
1,
2,
3,
4
]`;

describe('Tests for default handler', () => {
// prettier-ignore
each`
Expand Down Expand Up @@ -48,6 +60,10 @@ 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'} | ${[]} | ${0} | ${'"<empty argument list>"'}}
${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${a10b20}}
${'debug'} | ${'stringify'} | ${[1, 2, 3, 4]} | ${0} | ${oneTwoThreeFour}}

`.test(
//
Expand Down
17 changes: 17 additions & 0 deletions src/internal/builtins/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CallServiceResult } from '@fluencelabs/avm-runner-interface';
import { encode, decode } from 'bs58';
import { sha256 } from 'multiformats/hashes/sha2';
import { ResultCodes } from '../commonTypes';
import { jsonify } from '../utils'
import Buffer from '../Buffer';

const success = (result: any): CallServiceResult => {
Expand Down Expand Up @@ -128,6 +129,22 @@ export const builtInServices = {
},
},

debug: {
stringify: (req) => {
let out;

if (req.args.length === 0) {
out = '<empty argument list>';
} else if (req.args.length === 1) {
out = req.args[0];
} else {
out = req.args;
}

return success(jsonify(out));
},
},

peer: {
timeout: (req) => {
if (req.args.length !== 2) {
Expand Down