Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

> It's working for grpc-web + protobuf.js. I hope it helps somebody. #1410

Open
rmv9490 opened this issue Mar 10, 2024 · 1 comment
Open

Comments

@rmv9490
Copy link

rmv9490 commented Mar 10, 2024

          > It's working for grpc-web + protobuf.js. I hope it helps somebody.
import { ChunkParser, ChunkType } from "grpc-web-client/dist/ChunkParser"
import { RPCImpl } from "protobufjs";

function rpcImpl(serviceName: string): RPCImpl {
    return async (method, requestData, callback) => {
        const request = await fetch(`${BASE_URL}/${serviceName}/${method.name}`, {
            method: "POST",
            headers: {
                "content-type": "application/grpc-web+proto",
                "x-grpc-web": "1",
            },
            body: frameRequest(requestData),
        });
        const buffer = await request.arrayBuffer();
        const chunk = parseChunk(buffer);

        callback(null, chunk && chunk.data ? new Uint8Array(chunk.data) : null);
    };
}

function parseChunk(buffer: ArrayBuffer) {
    return new ChunkParser()
        .parse(new Uint8Array(buffer))
        .find(chunk => chunk.chunkType === ChunkType.MESSAGE);
}

function frameRequest(bytes: Uint8Array) {
    const frame = new ArrayBuffer(bytes.byteLength + 5);
    new DataView(frame, 1, 4).setUint32(0, bytes.length, false);
    new Uint8Array(frame, 5).set(bytes);
    return new Uint8Array(frame);
}

Getting Failed to parse URL, Browser is not able to parse the URL

Fetch error: TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from http://10.81.200.09:8000/loginService_package.LoginService/LoginMethod
at zone.js:1505:1
at proto. (zone.js:973:1)
at LoginService. (app.component.ts:41:31)
at Generator.next ()
at asyncGeneratorStep (asyncToGenerator.js:3:1)
at _next (asyncToGenerator.js:25:1)
at asyncToGenerator.js:32:1
at new ZoneAwarePromise (zone.js:1427:1)
at LoginService. (asyncToGenerator.js:21:1)
at LoginService.rpcImpl (app.component.ts:36:5)

Can someone please help in resolving this?
Many Thanks in advance

Originally posted by @rmv9490 in #80 (comment)

@sampajano
Copy link
Collaborator

Thanks for re-posting the question!

FYI, murgatroid99@ provided a reply at:
#80 (comment)

Is that helpful to you?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants