Skip to content

Commit 30488e5

Browse files
authored
Merge pull request #914 from IntersectMBO/qualify-js-from-proto
Bundle generated JS files qualified
2 parents 1563033 + 635558f commit 30488e5

6 files changed

Lines changed: 49 additions & 34 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ cardano-wasm/grpc-example/cardano-wasm.wasm
7878
cardano-wasm/grpc-example/cardano-wasm.js
7979
cardano-wasm/grpc-example/cardano-api.d.ts
8080
cardano-wasm/grpc-example/cardano-api.js
81-
cardano-wasm/grpc-example/node_grpc_web_pb.js
81+
cardano-wasm/grpc-example/cardano_node_grpc_web_pb.js
8282
cardano-wasm/example/cardano-wasm.wasm
8383
cardano-wasm/example/cardano-wasm.js
8484
cardano-wasm/example/cardano-api.d.ts

cardano-wasm/README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,8 @@ To run the example in the `example` subfolder:
239239
To run the example in the `grpc-example` subfolder:
240240

241241
1. Run an instance of the `cardano-node` with the GRPC server enabled and put the socket file for the GRPC server in the root folder of this repo with the name `rpc.socket`. (You can put it somewhere else, but you will have to update the `envoy-conf.yaml` function later.)
242-
2. Generate the JS GRPC client bundle `node_grpc_web_pb.js` from the GRPC server proto files by either:
243-
- Running `nix build .#proto-js-bundle`. (This will generate it under the `result` folder.)
244-
- Or using `protoc`, `npm` and `browserify`:
245-
```
246-
protoc -I../../cardano-rpc/proto --js_out=import_style=commonjs,binary:./ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:. ../../cardano-rpc/proto/cardano/rpc/node.proto
247-
248-
npm install grpc-web
249-
250-
npm install google-protobuf
251-
252-
browserify --standalone grpc cardano/rpc/node_grpc_web_pb.js > node_grpc_web_pb.js
253-
```
254-
3. Copy the generated `node_grpc_web_pb.js` file to the `grpc-example` subfolder.
242+
2. Generate the JS GRPC client bundle `cardano_node_grpc_web_pb.js` from the GRPC server proto files by running `nix build .#proto-js-bundle`. (This will generate it under the `result` folder.)
243+
3. Copy the generated `cardano_node_grpc_web_pb.js` file to the `grpc-example` subfolder.
255244
4. Copy the generated `cardano-wasm.wasm` file to the `grpc-example` subfolder. You can find its location using:
256245
```console
257246
echo "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)"

cardano-wasm/grpc-example/envoy-conf.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ static_resources:
9393
body:
9494
filename: "./example.js"
9595
- match:
96-
path: "/node_grpc_web_pb.js"
96+
path: "/cardano_node_grpc_web_pb.js"
9797
response_headers_to_add:
9898
- header:
9999
key: "Content-Type"
100100
value: "text/javascript"
101101
direct_response:
102102
status: 200
103103
body:
104-
filename: "./node_grpc_web_pb.js"
104+
filename: "./cardano_node_grpc_web_pb.js"
105105
http_filters:
106106
- name: envoy.filters.http.grpc_web
107107
typed_config:

cardano-wasm/grpc-example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<title>cardano-wasm test</title>
44
</head>
55
<body>
6-
<script type="module" src="./node_grpc_web_pb.js"></script>
6+
<script type="module" src="./cardano_node_grpc_web_pb.js"></script>
77
<script type="module" src="./example.js"></script>
88
<h1>Test output</h1>
99
</body>

cardano-wasm/src/Cardano/Wasm/Internal/JavaScript/GRPC.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ module Cardano.Wasm.Internal.JavaScript.GRPC (js_newWebGrpcClient, js_getEra) wh
99
import GHC.Wasm.Prim
1010

1111
-- | Create a GRPC-web client for the Cardano API.
12-
foreign import javascript safe "new grpc.NodePromiseClient($1, null, null)"
12+
foreign import javascript safe "{ node: new cardano_node.node.NodePromiseClient($1, null, null), \
13+
query: new cardano_node.query.QueryServicePromiseClient($1, null, null) \
14+
}"
1315
js_newWebGrpcClientImpl :: JSString -> IO JSVal
1416

1517
js_newWebGrpcClient :: String -> IO JSVal
1618
js_newWebGrpcClient = js_newWebGrpcClientImpl . toJSString
1719

1820
-- | Get the era from the Cardano API using a GRPC-web client.
19-
foreign import javascript safe "($1).getEra(new proto.Empty(), {})"
21+
foreign import javascript safe "($1).node.getEra(new proto.Empty(), {})"
2022
js_getEra :: JSVal -> IO Int
2123

2224
#endif

nix/proto-to-js.nix

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ in pkgs.stdenv.mkDerivation {
4343
mkdir -p "$GEN_JS_PATH"
4444
mkdir -p "$BUNDLE_PATH"
4545
46-
echo "--- Compiling .proto file: $PROTO_FILE ---"
46+
echo "--- Compiling .proto files in $PROTO_INCLUDE_PATH ---"
4747
48+
# Find all .proto files and compile them.
4849
for PROTO_FILE in `find "$PROTO_INCLUDE_PATH" -type f -name "*.proto"`
4950
do
5051
protoc \
@@ -57,32 +58,55 @@ in pkgs.stdenv.mkDerivation {
5758
echo "--- Compilation finished. Generated files are in $GEN_JS_PATH ---"
5859
ls -R "$GEN_JS_PATH"
5960
60-
# Check if there are any files in the top-level generated directory
61-
if [ ! "$(ls -1 "$GEN_JS_PATH" | head -n 1)" ]; then
62-
echo "Error: protoc did not generate any gRPC-Web files!"
63-
exit 1
64-
fi
61+
# Generate JS file that imports the generated files for Browserify
62+
ENTRYPOINT_FILE=$GEN_JS_PATH/index.js
63+
echo "--- Creating browserify entrypoint: $ENTRYPOINT_FILE ---"
64+
65+
# Ensure the entrypoint file is empty before we start.
66+
rm -f $ENTRYPOINT_FILE
67+
touch "$ENTRYPOINT_FILE"
68+
69+
# Find all *_grpc_web_pb.js files and build the entrypoint content.
70+
for JS_FULLPATH in `find "$GEN_JS_PATH" -type f -name "*_grpc_web_pb.js"`
71+
do
72+
# Get the filename, e.g., "node_grpc_web_pb.js"
73+
JS_FILENAME=$(basename "$JS_FULLPATH")
74+
75+
# Extract the module name by removing the suffix
76+
MODULE_NAME=''${JS_FILENAME%_grpc_web_pb.js}
77+
78+
# Get the path relative to GEN_JS_PATH for the require() statement.
79+
RELATIVE_PATH=''${JS_FULLPATH#$GEN_JS_PATH/}
80+
81+
echo "Adding module '$MODULE_NAME' from './$RELATIVE_PATH' to bundle."
82+
# Append the export line to our entrypoint file.
83+
# This creates the desired submodule structure.
84+
echo "exports.$MODULE_NAME = require('./$RELATIVE_PATH');" >> "$ENTRYPOINT_FILE"
85+
done
86+
87+
echo "--- Generated entrypoint content: ---"
88+
cat "$ENTRYPOINT_FILE"
6589
6690
echo "--- Setting up node_modules for browserify ---"
6791
ln -s ${node-deps}/node_modules ./node_modules
6892
69-
echo "--- Bundling generated JS with browserify ---"
93+
echo "--- Bundling all generated JS gRPC modules with browserify ---"
7094
71-
for GENERATED_GRPC_FILE in `find "$GEN_JS_PATH" -type f -name "*.js"`
72-
do
73-
browserify --standalone grpc "$GENERATED_GRPC_FILE" > "$BUNDLE_PATH/$(basename $GENERATED_GRPC_FILE)"
74-
done
95+
# Bundle the entrypoint file into a single standalone module.
96+
# The --standalone flag exposes the exports under the 'cardano_node' global variable.
97+
browserify "$ENTRYPOINT_FILE" --standalone cardano_node > "$BUNDLE_PATH/cardano_node_grpc_web_pb.js"
7598
76-
echo "--- Bundling complete. Final files are in $BUNDLE_PATH ---"
77-
ls "$BUNDLE_PATH"
99+
echo "--- Bundling complete. Final file is in $BUNDLE_PATH ---"
100+
ls -l "$BUNDLE_PATH"
78101
79102
runHook postBuild
80103
'';
81104

82105
installPhase = ''
83106
runHook preInstall
84-
mkdir -p "$out"
85-
cp ./bundled-js/*_grpc_web_pb.js "$out/"
107+
mkdir -p $out
108+
# Copy the final, correctly named bundle to the output directory.
109+
cp ./bundled-js/cardano_node_grpc_web_pb.js $out/
86110
runHook postInstall
87111
'';
88112

0 commit comments

Comments
 (0)