Skip to content

Commit

Permalink
Generate grpc/pb with commonjs_strict (#5336)
Browse files Browse the repository at this point in the history
This enables applications to load more than one version of the JS API as ES6
modules, without the implementations interfering with each other.
Unfortunately, the JS protoc plugin doesn't support this case well, so
workarounds are required to correctly reference proto files that use packages.

Fixes #5318
  • Loading branch information
niloc132 committed May 17, 2024
1 parent d498a8e commit cd3af54
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 52 deletions.
2 changes: 1 addition & 1 deletion proto/proto-backplane-grpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN set -eux; \
/includes/deephaven/proto/storage.proto; \
/opt/protoc/bin/protoc \
--plugin=protoc-gen-ts=/usr/src/app/node_modules/.bin/protoc-gen-ts \
--js_out=import_style=commonjs:/generated/js \
--js_out=import_style=commonjs_strict:/generated/js \
--ts_out=service=grpc-web:/generated/js \
-I/dependencies \
-I/includes \
Expand Down
100 changes: 50 additions & 50 deletions proto/raw-js-openapi/src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
require("deephaven/proto/session_pb");
require("deephaven/proto/table_pb");
require("deephaven/proto/console_pb");
require("deephaven/proto/ticket_pb");
require("deephaven/proto/application_pb");
require("deephaven/proto/inputtable_pb");
require("deephaven/proto/object_pb");
require("deephaven/proto/partitionedtable_pb");
require("deephaven/proto/storage_pb");
require("deephaven/proto/config_pb");
require("deephaven/proto/hierarchicaltable_pb");
require("Flight_pb")
require("BrowserFlight_pb")
var sessionService = require("deephaven/proto/session_pb_service");
var tableService = require("deephaven/proto/table_pb_service");
var consoleService = require("deephaven/proto/console_pb_service");
var applicationService = require("deephaven/proto/application_pb_service");
var inputTableService = require("deephaven/proto/inputtable_pb_service");
var objectService = require("deephaven/proto/object_pb_service");
var partitionedTableService = require("deephaven/proto/partitionedtable_pb_service");
var storageService = require("deephaven/proto/storage_pb_service");
var configService = require("deephaven/proto/config_pb_service");
var hierarchicalTableService = require("deephaven/proto/hierarchicaltable_pb_service");
var browserFlightService = require("BrowserFlight_pb_service");
var flightService = require("Flight_pb_service");
var session_pb = require("deephaven/proto/session_pb");
var table_pb = require("deephaven/proto/table_pb");
var console_pb = require("deephaven/proto/console_pb");
var ticket_pb = require("deephaven/proto/ticket_pb");
var application_pb = require("deephaven/proto/application_pb");
var inputtable_pb = require("deephaven/proto/inputtable_pb");
var object_pb = require("deephaven/proto/object_pb");
var partitionedtable_pb = require("deephaven/proto/partitionedtable_pb");
var storage_pb = require("deephaven/proto/storage_pb");
var config_pb = require("deephaven/proto/config_pb");
var hierarchicaltable_pb = require("deephaven/proto/hierarchicaltable_pb");
var Flight_pb = require("Flight_pb")
var BrowserFlight_pb = require("BrowserFlight_pb")
var session_pb_service = require("deephaven/proto/session_pb_service");
var table_pb_service = require("deephaven/proto/table_pb_service");
var console_pb_service = require("deephaven/proto/console_pb_service");
var application_pb_service = require("deephaven/proto/application_pb_service");
var inputtable_pb_service = require("deephaven/proto/inputtable_pb_service");
var object_pb_service = require("deephaven/proto/object_pb_service");
var partitionedtable_pb_service = require("deephaven/proto/partitionedtable_pb_service");
var storage_pb_service = require("deephaven/proto/storage_pb_service");
var config_pb_service = require("deephaven/proto/config_pb_service");
var hierarchicaltable_pb_service = require("deephaven/proto/hierarchicaltable_pb_service");
var BrowserFlight_pb_service = require("BrowserFlight_pb_service");
var Flight_pb_service = require("Flight_pb_service");

var browserHeaders = require("browser-headers");

Expand All @@ -36,27 +36,27 @@ var schema = require('./arrow/flight/flatbuf/Schema_generated');

var io = { deephaven: {
proto: {
session_pb: proto.io.deephaven.proto.backplane.grpc,
session_pb_service: sessionService,
table_pb: proto.io.deephaven.proto.backplane.grpc,
table_pb_service: tableService,
console_pb: proto.io.deephaven.proto.backplane.script.grpc,
console_pb_service: consoleService,
ticket_pb: proto.io.deephaven.proto.backplane.grpc,
application_pb: proto.io.deephaven.proto.backplane.grpc,
application_pb_service: applicationService,
inputtable_pb: proto.io.deephaven.proto.backplane.grpc,
inputtable_pb_service: inputTableService,
object_pb: proto.io.deephaven.proto.backplane.grpc,
object_pb_service: objectService,
partitionedtable_pb: proto.io.deephaven.proto.backplane.grpc,
partitionedtable_pb_service: partitionedTableService,
storage_pb: proto.io.deephaven.proto.backplane.grpc,
storage_pb_service: storageService,
config_pb: proto.io.deephaven.proto.backplane.grpc,
config_pb_service: configService,
hierarchicaltable_pb: proto.io.deephaven.proto.backplane.grpc,
hierarchicaltable_pb_service: hierarchicalTableService
session_pb,
session_pb_service,
table_pb,
table_pb_service,
console_pb,
console_pb_service,
ticket_pb,
application_pb,
application_pb_service,
inputtable_pb,
inputtable_pb_service,
object_pb,
object_pb_service,
partitionedtable_pb,
partitionedtable_pb_service,
storage_pb,
storage_pb_service,
config_pb,
config_pb_service,
hierarchicaltable_pb,
hierarchicaltable_pb_service
},
barrage: {
"flatbuf": {
Expand All @@ -70,10 +70,10 @@ var arrow = { flight: {
Schema_generated: schema,
},
protocol: {
Flight_pb: proto.arrow.flight.protocol,
Flight_pb_service: flightService,
BrowserFlight_pb: proto.arrow.flight.protocol,
BrowserFlight_pb_service: browserFlightService
Flight_pb,
Flight_pb_service,
BrowserFlight_pb,
BrowserFlight_pb_service
}
}};
var dhinternal = {
Expand Down
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/BrowserFlight_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/BrowserFlight_pb').io.deephaven.flightjs.protocol)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/Flight_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/Flight_pb').arrow.flight.protocol)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/application_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/application_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/config_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/config_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/console_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/console_pb').io.deephaven.proto.backplane.script.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/hierarchicaltable_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/hierarchicaltable_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/inputtable_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/inputtable_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/object_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/object_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/partitionedtable_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/partitionedtable_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/session_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/session_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/storage_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/storage_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/table_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/table_pb').io.deephaven.proto.backplane.grpc)
1 change: 1 addition & 0 deletions proto/raw-js-openapi/src/shim/ticket_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(exports, require('real/ticket_pb').io.deephaven.proto.backplane.grpc)
31 changes: 30 additions & 1 deletion proto/raw-js-openapi/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
const path = require('path');

// Workaround for broken codegen from protoc-gen-js using import_style=commonjs_strict, both in
// the grpc-web protoc-gen-ts plugin, and in protoc-gen-js itself:
const aliases = {};
for (const proto of ['application', 'config', 'console', 'hierarchicaltable', 'inputtable', 'object', 'partitionedtable', 'session', 'storage', 'table', 'ticket']) {
// Allows a reference to the real proto files, to be made from the shim
aliases[`real/${proto}_pb`] = `${__dirname}/build/js-src/deephaven/proto/${proto}_pb`;

const shimPath = `${__dirname}/src/shim/${proto}_pb`;
// Three aliases which would normally point at the real proto file, now directed to the shim:
// * First, an unsuffixed, relative reference from any service files
aliases[`../../deephaven/proto/${proto}_pb`] = shimPath;
// * Next, a ".js"-suffixed, relative reference from other proto files (see https://github.com/protocolbuffers/protobuf-javascript/issues/40)
aliases[`../../deephaven/proto/${proto}_pb.js`] = shimPath;
// * Last, an absolute reference from the index.js
aliases[`deephaven/proto/${proto}_pb`] = shimPath;
}
for (const proto of ['Flight', 'BrowserFlight']) {
// Allows a reference to the real proto files, to be made from the shim
aliases[`real/${proto}_pb`] = `${__dirname}/build/js-src/${proto}_pb`;

const shimPath = `${__dirname}/src/shim/${proto}_pb`;
// Two aliases which would normally point to the real proto file, now directed to the shim:
// * First, a relative reference from any service file
aliases[`./${proto}_pb`] = shimPath;
// * Second, an absolute reference from the index.js
aliases[`${proto}_pb`] = shimPath;
}

module.exports = {
mode: "production",
module: {
Expand All @@ -16,6 +44,7 @@ module.exports = {
},
resolve : {
modules: ['node_modules', __dirname + '/build/js-src'],
extensions: ['.ts', '.js']
extensions: ['.ts', '.js'],
alias: aliases,
},
};

0 comments on commit cd3af54

Please sign in to comment.