samchon / tgrid

TypeScript RPC (Remote Procedure Call) for WebSocket and Worker protocols
https://tgrid.com/
MIT License
141 stars 19 forks source link

Parametric value undefined be null in RFC (Remote Function Call) #15

Closed samchon closed 5 years ago

samchon commented 5 years ago

Summary

Call remote function (1, 2, undefined) -> be (1, 2, null)

Code occuring the bug

import { WebServer } from "tgrid/protocols/web/WebServer";
import { WebConnector } from "tgrid/protocols/web/WebConnector";
import { Driver } from "tgrid/basic/Driver";

class Something
{
    public trace(...args: any[]): void
    {
        console.log(...args);
    }
}

async function main(): Promise<void>
{
    let server: WebServer = new WebServer();
    await server.open(10000, async acceptor =>
    {
        await acceptor.accept(new Something());
    });

    let connector: WebConnector = new WebConnector();
    await connector.connect("ws://127.0.0.1:10000");

    let something: Driver<Something> = connector.getDriver<Something>();
    await something.trace(1, { obj: {} }, undefined, "text");

    await connector.close();
    await server.close();
}
main();
1 { obj: {} } null 'text'

Solution

console.log(JSON.stringify([1, 2, undefined, 'text']));
'[1, 2, null, "text"]'

When insert an undefined value in an Array and stringify the Array, the undefined value is printed as a null value. That's the reason why such error has been occurred. Thus, the only way to avoid such error is to changing data structure the Invoke, message structure for RFC.

https://github.com/samchon/tgrid/blob/db768071dafe371b2076fa8837e80fd67c223f5b/src/basic/Invoke.ts#L17-L29

If the Invoke.IFunction represents parametric values as not atomic values, but being capsuled into objects, the bug must be fixed.

export namespace Invoke
{
    export interface IFunction
    {
        uid: number;
        listener: string;
        parameters: IParameter[];
    }
    export interface IParameter
    {
        type: string;
        value: any;
    }
}