protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Where to access `Method` or `rpc.ServiceMethod` for creating an `rpcImpl` ?

Open pbower opened this issue 8 months ago • 0 comments

Hi all,

Wondering if someone can help ? I must be missing something basic here - where do we access the Method or rpc.ServiceMethod for a given endpoint in the generated protobufjs protobuf?

In ProtobufJS we have:

/**
 * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
 * @param method Reflected or static method being called
 * @param requestData Request data
 * @param callback Callback function
 */
type RPCImpl = (method: (Method|rpc.ServiceMethod<Message<{}>, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void;

and:

/** Reflected service method. */
export class Method extends ReflectionObject {

    /**
     * Constructs a new service method instance.
     * @param name Method name
     * @param type Method type, usually `"rpc"`
     * @param requestType Request message type
     * @param responseType Response message type
     * @param [requestStream] Whether the request is streamed
     * @param [responseStream] Whether the response is streamed
     * @param [options] Declared options
     * @param [comment] The comment for this method
     * @param [parsedOptions] Declared options, properly parsed into an object
     */
    constructor(name: string, type: (string|undefined), requestType: string, responseType: string, requestStream?: (boolean|{ [k: string]: any }), responseStream?: (boolean|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string, parsedOptions?: { [k: string]: any });

    /** Method type. */
    public type: string;

    /** Request type. */
    public requestType: string;

    /** Whether requests are streamed or not. */
    public requestStream?: boolean;

    /** Response type. */
    public responseType: string;

    /** Whether responses are streamed or not. */
    public responseStream?: boolean;

    /** Resolved request type. */
    public resolvedRequestType: (Type|null);

    /** Resolved response type. */
    public resolvedResponseType: (Type|null);

    /** Comment for this method */
    public comment: (string|null);

    /** Options properly parsed into an object */
    public parsedOptions: any;

    /**
     * Constructs a method from a method descriptor.
     * @param name Method name
     * @param json Method descriptor
     * @returns Created method
     * @throws {TypeError} If arguments are invalid
     */
    public static fromJSON(name: string, json: IMethod): Method;

    /**
     * Converts this method to a method descriptor.
     * @param [toJSONOptions] JSON conversion options
     * @returns Method descriptor
     */
    public toJSON(toJSONOptions?: IToJSONOptions): IMethod;
}

So, we need to create an rpcImpl in order to send/receive the protobuf messages.

But let's say I go "

const endpointMethod = $root.myProtobufFileName.myProtobufRpcService.prototype
      .myProtobufRpcEndpoint as unknown as Method

" it's not actually a method. Where do we access the Method?

or , for an example here:

    const endpointMethod = $root.SpriteService.SpriteServiceRpc.prototype
      .getSpriteFromServer as unknown as Method

Accesses this from the proto bundle file:


        /**
         * Calls GetSpriteFromServer.
         * @param request SpritePb message or plain object
         * @returns Promise
         */
        public getSpriteFromServer(request: SpriteService.ISpritePb): Promise<SpriteService.SpritePb>;

This has a completely different signature to Method , but it's the only thing that looks like it's generated in the protobuf - so where do we get it, as one of the inputs to create a valid rpcImpl (i.e., disregarding th actual transport side of things)?

Thanks for your help!

pbower avatar Mar 02 '25 10:03 pbower