@tgrospic/rnode-grpc-js
Version:
RNode gRPC helpers
266 lines • 15.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
var path_1 = __importDefault(require("path"));
var child_process_1 = require("child_process");
var fs_extra_1 = __importDefault(require("fs-extra"));
var R = __importStar(require("ramda"));
var args_1 = require("./args");
var download_1 = require("./download");
var typings_1 = require("./typings");
var lib_1 = require("../lib");
// Relative location of directories where protobuf files are located
// in RChain main repository https://github.com/rchain/rchain
var rchainProtoDirs = ['models/src/main/protobuf', 'node/src/main/protobuf'];
var log = console.log, error = console.error;
var blue = function (txt) { return "\u001B[34m".concat(txt, "\u001B[0m"); };
var ext = process.platform === 'win32' ? '.cmd' : '';
/**
* Generate JS bindings with native `grpc-tools` library.
*/
var generateJsPb = function (_a) {
var jsPath = _a.jsPath, protoPath = _a.protoPath, protoFiles = _a.protoFiles, npmBin = _a.npmBin;
return __awaiter(void 0, void 0, void 0, function () {
var protoc, args, protocExe;
return __generator(this, function (_b) {
protoc = path_1.default.resolve(npmBin, "grpc_tools_node_protoc".concat(ext));
args = __spreadArray([
"--js_out=import_style=commonjs:".concat(jsPath),
"-I".concat(protoPath)
], __read(protoFiles), false);
protocExe = (0, child_process_1.spawn)(protoc, args, { stdio: 'inherit' });
return [2 /*return*/, (0, lib_1.waitExit)(protocExe, null, "Failed to generate JS files with grpc-tools.")];
});
});
};
/**
* Generate JSON representation of protobuf types with `protobufjs` library.
*/
var generateJsonPb = function (_a) {
var jsPath = _a.jsPath, protoFiles = _a.protoFiles, npmBin = _a.npmBin;
return __awaiter(void 0, void 0, void 0, function () {
var pbjs, jsonPath, args, pbExe;
return __generator(this, function (_b) {
pbjs = path_1.default.resolve(npmBin, "pbjs".concat(ext));
jsonPath = "".concat(jsPath, "/pbjs_generated.json");
args = __spreadArray([
"-t",
"json",
"-o",
jsonPath,
'--keep-case'
], __read(protoFiles), false);
pbExe = (0, child_process_1.spawn)(pbjs, args, { stdio: 'inherit' });
return [2 /*return*/, (0, lib_1.waitExit)(pbExe, jsonPath, "Failed to generate JSON schema with pbjs.")];
});
});
};
/**
* Run RChain client gRPC/protobuf API generator.
*
* - download protobuf definition files from GitHub rchain/rchain
* - generate JS bindings (grpc-tools)
* - generate JSON schema (protobufjs)
* - generate TypeScript definitions
*/
var run = function (_a) {
var args = _a.args, cwd = _a.cwd, npmBin = _a.npmBin;
return __awaiter(void 0, void 0, void 0, function () {
var options, _b, version, _c, genDirRel, _d, includeReflection, dirPath, protoPath, scalapbPath, jsPath, githubListFilesUrl, fetchProtoListFromGithub, protoDownload, scalapbDownload, reflectionDownload, protoDownloads, newDirs, protoFiles, jsonPath, protoSchema;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
options = (0, args_1.parseArgs)(args);
_b = options["rnode-version"], version = _b === void 0 ? 'v0.12.4' : _b, _c = options["gen-dir"], genDirRel = _c === void 0 ? 'rnode-grpc-gen' : _c, _d = options["include-reflection"], includeReflection = _d === void 0 ? false : _d;
log(blue('RNode gRPC versions'), { 'rnode-version': version });
dirPath = path_1.default.resolve(cwd, genDirRel);
protoPath = path_1.default.resolve(dirPath, 'proto');
scalapbPath = path_1.default.resolve(protoPath, 'scalapb');
jsPath = path_1.default.resolve(dirPath, 'js');
githubListFilesUrl = function (dir) {
return "https://api.github.com/repos/rchain/rchain/contents/".concat(dir, "?ref=").concat(version);
};
fetchProtoListFromGithub = function (dir) { return (0, download_1.fetch)({
url: githubListFilesUrl(dir),
headers: { 'User-Agent': 'rnode-grpc-js generator for RChain RNode API' },
json: true,
}); };
protoDownload = function (_a) {
var name = _a.name, download_url = _a.download_url;
return ({ downloadUrl: download_url, filePath: path_1.default.resolve(protoPath, name) });
};
scalapbDownload = {
downloadUrl: 'https://raw.githubusercontent.com/scalapb/ScalaPB/master/protobuf/scalapb/scalapb.proto',
filePath: path_1.default.resolve(scalapbPath, 'scalapb.proto'),
};
reflectionDownload = {
downloadUrl: 'https://raw.githubusercontent.com/grpc/grpc/v1.24.1/src/proto/grpc/reflection/v1alpha/reflection.proto',
filePath: path_1.default.resolve(protoPath, 'reflection.proto'),
};
return [4 /*yield*/, R.pipe((0, lib_1.chainAsync)(fetchProtoListFromGithub), (0, lib_1.filterAsync)(function (_a) {
var name = _a.name;
return name !== 'routing.proto';
}), (0, lib_1.mapAsync)(protoDownload), (0, lib_1.then)(R.append(scalapbDownload)), (0, lib_1.then)(includeReflection ? R.append(reflectionDownload) : R.identity))(rchainProtoDirs)
// Cleanup existing files and ensure directory structure
];
case 1:
protoDownloads = _e.sent();
newDirs = [protoPath, scalapbPath, jsPath];
return [4 /*yield*/, fs_extra_1.default.remove(dirPath)];
case 2:
_e.sent();
return [4 /*yield*/, (0, lib_1.mapAsync)(fs_extra_1.default.ensureDir, newDirs)];
case 3:
_e.sent();
log(blue('Startinmg downloads...'));
// Download all files
return [4 /*yield*/, (0, download_1.downloadAll)(protoDownloads)];
case 4:
// Download all files
_e.sent();
log(blue('Generating JS files...'));
protoFiles = R.map(R.prop('filePath'), protoDownloads);
// Generate JS code from proto files (with grpc-tools)
return [4 /*yield*/, generateJsPb({ jsPath: jsPath, protoPath: protoPath, protoFiles: protoFiles, npmBin: npmBin })
// Generate JSON definition from proto files (with protobufjs)
];
case 5:
// Generate JS code from proto files (with grpc-tools)
_e.sent();
return [4 /*yield*/, generateJsonPb({ jsPath: jsPath, protoFiles: protoFiles, npmBin: npmBin })
// Append fix to Expr type primitive fields
];
case 6:
jsonPath = _e.sent();
// Append fix to Expr type primitive fields
return [4 /*yield*/, addFixExprPrimitiveFields(jsPath)
// Load generated pbjs JSON schema
];
case 7:
// Append fix to Expr type primitive fields
_e.sent();
protoSchema = require(jsonPath);
log(blue('Generating TypeScript definitions...'));
// Generate TypeScript definitions
return [4 /*yield*/, (0, typings_1.generateTs)({ jsPath: jsPath, protoPath: protoPath, protoSchema: protoSchema, version: version })];
case 8:
// Generate TypeScript definitions
_e.sent();
return [2 /*return*/];
}
});
});
};
exports.run = run;
/**
* Fix for primitive fields in Expr type.
* https://github.com/rchain/rchain/issues/3566
*
* RNode uses protobuf v3 format which doesn't serialize default values for primitive types.
* RNode overrides this behavior for Rholang types, so fields with primitive values are
* always serialized, even containing default values.
* JS generated code with grpc-tools will set default values for not serialized fields which
* makes it impossible to detect in Expr type which field is really set.
* This fix overrides `toObject` generated function with detection of non-serialized fields
* of Expr type and sets them to undefined.
*/
function addFixExprPrimitiveFields(jsPath) {
return __awaiter(this, void 0, void 0, function () {
var rhoTypesJs, patch;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
rhoTypesJs = path_1.default.resolve(jsPath, "RhoTypes_pb.js");
patch = "\n\n/**\n * Fix for primitive fields in Expr type.\n * https://github.com/rchain/rchain/issues/3566\n *\n * RNode uses protobuf v3 format which doesn't serialize default values for primitive types.\n * RNode overrides this behavior for Rholang types, so fields with primitive values are\n * always serialized, even containing default values.\n * JS generated code with grpc-tools will set default values for not serialized fields which\n * makes it impossible to detect in Expr type which field is really set.\n * This fix detects non-serialized fields of Expr type and sets them to undefined.\n */\nconst originExprToObject = proto.Expr.toObject\n\nconst patchFields = [\n ['gBool' , 1],\n ['gInt' , 2],\n ['gString' , 3],\n ['gUri' , 4],\n ['gByteArray', 25],\n]\n\nfunction deleteFieldIfEmpty(msg, exprObj, name, fieldNr) {\n const v = jspb.Message.getField(msg, fieldNr)\n if (v === void 666 || v === null) exprObj[name] = void 666\n}\n\nproto.Expr.toObject = function(includeInstance, msg) {\n const expr = originExprToObject.call(includeInstance, includeInstance, msg)\n patchFields.forEach(([name, pos]) => deleteFieldIfEmpty(msg, expr, name, pos))\n return expr\n}\n";
return [4 /*yield*/, fs_extra_1.default.appendFile(rhoTypesJs, patch, 'utf8')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
//# sourceMappingURL=index.js.map