如何在客戶端排除導入 (How to exclude an import on client side)


問題描述

如何在客戶端排除導入 (How to exclude an import on client side)

我正在使用 protoc‑gen‑validate,這是一個代碼生成器用於在服務器端驗證 protobufs 的插件。服務器是用 Go 編寫的,但客戶端是用 Javascript 編寫的。我遇到的問題是 protoc‑gen‑validate 庫似乎包含在使用 ts‑protoc‑gen,這是一種 protoc‑gen‑validate 不受支持的語言。結果,使用生成的 javascript 代碼時出現 MODULE_NOT_FOUND 錯誤,此處失敗:驗證錯誤

internal/modules/cjs/loader.js:1033
  throw err;
  ^

Error: Cannot find module '../../validate/validate_pb.js'
Require stack:
‑ /home/amadeus/HarmonyProject/harmony‑grpc‑test/gen/core/v1/core_pb.js
‑ /home/amadeus/HarmonyProject/harmony‑grpc‑test/index.js

我願意想知道,如何自動從生成的代碼中排除這種導入?此外,如果解決方案保持 proto 文件不可變,那就太好了,因為它們位於 git 子模塊中。


參考解法

方法 1:

Messy hack to fix this, looking for more elegant ways to make this work:

IN_DIR="./protocol"
PROTOCOL_BUILD_TMP="./protocol‑build‑tmp"

mkdir ‑p $PROTOCOL_BUILD_TMP
cp ‑r $IN_DIR $PROTOCOL_BUILD_TMP

for dir in $(find "${PROTOCOL_BUILD_TMP}" ‑name '*.proto' ‑print0 | xargs ‑0 ‑n1 dirname | sort | uniq); do
  sed ‑i ‑E "s/ \[ \(validate.rules(.*)\];/;/g" $(find "${dir}" ‑name '*.proto')
  sed ‑i ‑E "/validate.proto/d" $(find "${dir}" ‑name '*.proto')
done
rm ‑r $PROTOCOL_BUILD_TMP

方法 2:

This is also a messy hack but it works.

Create a validate_pb.js file along with the generated files and paste the following code in it.

var jspb = require('google‑protobuf');
var goog = jspb;
var global = (function() {
    if (this) { return this; }
    if (typeof window !== 'undefined') { return window; }
    if (typeof global !== 'undefined') { return global; }
    if (typeof self !== 'undefined') { return self; }
    return Function('return this')();
}.call(null));

goog.exportSymbol('proto.tagger', null, global);

This solved the issue for me.

(by BluskriptBluskriptChandraaditya)

參考文件

  1. How to exclude an import on client side (CC BY‑SA 2.5/3.0/4.0)

#protocol-buffers #javascript #GO #gRPC






相關問題

通過 Method.invoke() 調用靜態方法給了我 NPE (Static method invocation via Method.invoke() gave me NPE)

python中的protobuf到json (Protobuf to json in python)

如何序列化/反序列化使用 ScalaPB 的“oneof”的 protobuf 消息? (How to serialize/deserialize a protobuf message that uses 'oneof' with ScalaPB?)

序列化和反序列化未知的繼承類型 (Serializing and deserializing unknown inherited types)

Thrift 與協議緩衝區 (Thrift vs Protocol buffers)

如何在客戶端排除導入 (How to exclude an import on client side)

為什麼當我安裝了 Tensorflow 的所有庫後,會出現無目錄錯誤? (Why do I get a no directory error, when I have installed all the libraries for Tensorflow?)

如何為 C# <proto/> 定義傳遞experimental_allow_proto3_optional 以在proto3 中啟用可選? (How to pass experimental_allow_proto3_optional for C# <proto/> definitions to enable optional in proto3?)

CentOS7 的 libprotobuf-lite.so 文件在 CentOS8 機器上工作嗎? (is libprotobuf-lite.so file from CentOS7 working in CentOS8 machine?)

protogen - 支持 <Property> 指定的語法 (protogen - support the <Property>Specified syntax)

.Net 字典類型在 protobuff (.Net dictionary type in protobuff)

為什麼 protobuf 更喜歡代碼生成器而不是運行時動態加載 (why protobuf prefer code-generator other than dynamic loading at runtime)







留言討論