I'm trying to build a JS library which is able to make gRPC requests (using HTTP for transport) in browser and node (used by Server Side Rendering) in a transparent manner, not dependent of environement. There are already two different libraries providing transport for both environements:
node : @improbable-eng/grpc-web-node-http-transport (NodeHttpTransport)
browser : @improbable-eng/grpc-web (CrossBrowserHttpTransport)
For example NodeHttpTransport is using http package, not available in browser.
import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
import { grpc } from "@improbable-eng/grpc-web";
...
// contrcutor of my service
// setup client for browser
this.rpcBrowser = new GrpcWebImpl(grpcGatewayUrl, {
transport: grpc.CrossBrowserHttpTransport({ withCredentials: true }),
debug: false,
});
this.clientBrowser = new GrpcClient(this.rpcBrowser);
// setup client for node
this.rpcNode = new GrpcWebImpl(this.grpcGatewayUrl, {
transport: NodeHttpTransport(),
debug: false,
});
this.clientNode = new GrpcClient(this.rpcNode);
...
// on every gRPC requests I get the client for the current env with this function
getClient() {
const isServer = typeof window === "undefined";
if (isServer) {
return this.clientNode;
}
return this.clientBrowser;
}
...
// so for example I use it like this
public GetAll(){
return this.getClient().GetAll()
}
However, this code resuts in this error during build.
Module not found: Error: Can't resolve 'http' in '/node_modules/@improbable-eng/grpc-web-node-http-transport/lib'
Module not found: Error: Can't resolve 'https' in '/node_modules/@improbable-eng/grpc-web-node-http-transport/lib'
I understand it tries to use http package in browser environment.
I see differents solutions for those errors, like set resolve.fallback.http and resolve.fallback.https to false in webpack config but I don't like this option as browser never use http package, as it uses browser transport library.
Is there a proper way to fix it ?