samchon / tgrid

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

[RFC] Prohibit access to edge-underscored members #25

Closed samchon closed 3 years ago

samchon commented 4 years ago

Prohibit Remote Function Calls on members starting or ending with the underscore (_).

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

interface ICalculator
{
    // Possible to access
    plus(x: number, y: number): number;
    minus(x: number, y: number): number;
    statistics: IStatistics;

    // Impossible to access
    _Divides(x: number, y: number): number; // impossible
    scientific_: IScientific; // impossible
}

async main(): Promise<void>
{
    //----
    // PREPARATION
    //----
    let connector: WebConnector = new WebConnector();
    await connector.connect("ws://127.0.0.1:8091");

    let calc: Driver<ICalculator> = connector.getDriver();

    //----
    // REMOTE FUNCTION CALLS
    //----
    // POSSIBLE
    await calc.plus(5, 6);
    await calc.minus(7, 2);
    await calc.statistics.mean(1, 2, 3, 4);

    // IMPOSSIBLE - be RuntimeError, Also should be compile error
    await calc._Divides(72, 9);
    await calc.scientific_.pow(3, 4);

    await connector.close();
}
main();
samchon commented 4 years ago

It's possible to block Remote Function Calls on the edge-underscored members in the runtime level. However, prohibiting them in the compile level is not possible yet because TypeScript does not support handling sub-position of literal type yet.

Hoping the TypeScript support it, I've published two issues:

If those issues are adopted, the Driver would be like such below: