Skip to content

Commit 2729847

Browse files
authored
Merge pull request #911 from IntersectMBO/proto-to-js
Add nix output for web-grpc client bundled as a single JS file
2 parents 53297f1 + 13e01cd commit 2729847

File tree

4 files changed

+145
-10
lines changed

4 files changed

+145
-10
lines changed

flake.nix

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
};
7575
inherit (nixpkgs) lib;
7676

77+
proto-js-bundle-drv = import ./nix/proto-to-js.nix {pkgs = nixpkgs;};
78+
7779
# We use cabalProject' to ensure we don't build the plan for
7880
# all systems.
7981
cabalProject = nixpkgs.haskell-nix.cabalProject' ({config, ...}: {
@@ -150,7 +152,11 @@
150152
};
151153
};
152154
})
153-
({pkgs, config, ...}: let
155+
({
156+
pkgs,
157+
config,
158+
...
159+
}: let
154160
generatedExampleFiles = ["cardano-wasm/lib-wrapper/cardano-api.d.ts"];
155161
exportWasmPath = "export CARDANO_WASM=${config.hsPkgs.cardano-wasm.components.exes.cardano-wasm}/bin/cardano-wasm${pkgs.stdenv.hostPlatform.extensions.executable}";
156162
in {
@@ -208,16 +214,16 @@
208214
};
209215
};
210216
playwrightShell = let
211-
playwright-pkgs = inputs.nixpkgs.legacyPackages.${system};
212-
in {
213-
playwright = playwright-pkgs.mkShell {
214-
packages = [
215-
playwright-pkgs.playwright-test
216-
playwright-pkgs.python313Packages.docopt
217-
playwright-pkgs.python313Packages.httpserver
218-
];
219-
};
217+
playwright-pkgs = inputs.nixpkgs.legacyPackages.${system};
218+
in {
219+
playwright = playwright-pkgs.mkShell {
220+
packages = [
221+
playwright-pkgs.playwright-test
222+
playwright-pkgs.python313Packages.docopt
223+
playwright-pkgs.python313Packages.httpserver
224+
];
220225
};
226+
};
221227
flakeWithWasmShell = nixpkgs.lib.recursiveUpdate flake {
222228
devShells = wasmShell;
223229
hydraJobs = {devShells = wasmShell;};
@@ -238,13 +244,23 @@
238244
// {
239245
# This ensure hydra send a status for the required job (even if no change other than commit hash)
240246
revision = nixpkgs.writeText "revision" (inputs.self.rev or "dirty");
247+
proto-js-bundle = proto-js-bundle-drv;
241248
};
249+
}
250+
// lib.optionalAttrs (system != "aarch64-darwin")
251+
{
252+
packages = {
253+
proto-js-bundle = proto-js-bundle-drv;
254+
};
242255
};
243256
legacyPackages = {
244257
inherit cabalProject nixpkgs;
245258
# also provide hydraJobs through legacyPackages to allow building without system prefix:
246259
inherit hydraJobs;
247260
};
261+
packages = lib.optionalAttrs (system != "aarch64-darwin") {
262+
proto-js-bundle = proto-js-bundle-drv;
263+
};
248264
devShells = let
249265
# profiling shell
250266
profilingShell = p: {

nix/npm-deps/package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/npm-deps/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"google-protobuf": "^3.21.4",
4+
"grpc-web": "^1.5.0"
5+
}
6+
}

nix/proto-to-js.nix

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# proto-to-js.nix
2+
3+
{ pkgs ? import <nixpkgs> {} }:
4+
5+
let
6+
7+
# Node dependencies
8+
node-deps = pkgs.buildNpmPackage {
9+
version = "1.0.0";
10+
name = "proto-js-dependencies";
11+
src = ../nix/npm-deps;
12+
npmDepsHash = "sha256-b8x9xZ0dCu1cvILF0HPVVLfkCGHOWCcPUKyC2x1gQ+c=";
13+
dontNpmBuild = true;
14+
dontNpmInstall = true;
15+
installPhase = ''
16+
mkdir -p $out
17+
cp -r node_modules $out/
18+
'';
19+
};
20+
21+
cardano-rpc-src = ../cardano-rpc;
22+
23+
in pkgs.stdenv.mkDerivation {
24+
pname = "cardano-rpc-proto-js-bundle";
25+
version = "0.1.0";
26+
27+
src = cardano-rpc-src;
28+
29+
nativeBuildInputs = [
30+
pkgs.protobuf
31+
pkgs.protoc-gen-js
32+
pkgs.protoc-gen-grpc-web
33+
pkgs.nodePackages.browserify
34+
];
35+
36+
buildPhase = ''
37+
runHook preBuild
38+
39+
PROTO_INCLUDE_PATH=$src/proto
40+
GEN_JS_PATH=./generated-js
41+
BUNDLE_PATH=./bundled-js
42+
43+
mkdir -p $GEN_JS_PATH
44+
mkdir -p $BUNDLE_PATH
45+
46+
PROTO_FILE=$PROTO_INCLUDE_PATH/cardano/rpc/node.proto
47+
48+
echo "--- Compiling .proto file: $PROTO_FILE ---"
49+
50+
protoc \
51+
-I=$PROTO_INCLUDE_PATH \
52+
--js_out=import_style=commonjs,binary:$GEN_JS_PATH \
53+
--grpc-web_out=import_style=commonjs,mode=grpcwebtext:$GEN_JS_PATH \
54+
$PROTO_FILE
55+
56+
echo "--- Compilation finished. Generated files are in $GEN_JS_PATH ---"
57+
ls -R $GEN_JS_PATH
58+
59+
GENERATED_GRPC_FILE=$GEN_JS_PATH/cardano/rpc/node_grpc_web_pb.js
60+
61+
if [ ! -f "$GENERATED_GRPC_FILE" ]; then
62+
echo "Error: Protoc did not generate the expected gRPC-Web file!"
63+
exit 1
64+
fi
65+
66+
echo "--- Setting up node_modules for browserify ---"
67+
ln -s ${node-deps}/node_modules ./node_modules
68+
69+
echo "--- Bundling generated JS with browserify ---"
70+
71+
browserify --standalone grpc $GENERATED_GRPC_FILE > $BUNDLE_PATH/node_grpc_web_pb.js
72+
73+
echo "--- Bundling complete. Final file is in $BUNDLE_PATH ---"
74+
ls $BUNDLE_PATH
75+
76+
runHook postBuild
77+
'';
78+
79+
installPhase = ''
80+
runHook preInstall
81+
mkdir -p $out
82+
cp ./bundled-js/node_grpc_web_pb.js $out/
83+
runHook postInstall
84+
'';
85+
86+
dontConfigure = true;
87+
}
88+

0 commit comments

Comments
 (0)