Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grpc-code-gen

Package Overview
Dependencies
Maintainers
1
Versions
130
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grpc-code-gen - npm Package Compare versions

Comparing version 4.3.11 to 4.4.0

2

build/base.d.ts

@@ -7,5 +7,5 @@ import { IOption } from 'load-proto';

serviceCode?: boolean;
configFilePath: string;
configFilePath?: string;
grpcNpmName?: string;
}
export declare function gen(opt: Options): Promise<string>;

@@ -283,2 +283,3 @@ "use strict";

const grpcObjPath = getAbsPath(`grpcObj.${moduleSuffix}`, baseDir);
const grpcClientPath = getAbsPath(`getGrpcClient.${moduleSuffix}`, baseDir);
if (typescript) {

@@ -356,2 +357,74 @@ yield fs.writeFile(grpcObjPath, [

].join('\n'));
const grpcCodeGenPath = path.join(process.cwd(), '.grpc-code-gen');
yield fs.writeFile(grpcClientPath, [
fileTip,
`
import * as grpc from "@grpc/grpc-js/";
import { ChannelCredentials } from "@grpc/grpc-js//build/src/channel-credentials";
import * as fs from 'fs';
import * as path from 'path';
export interface IService<S> {
$FILE_NAME: string;
new(address: string, credentials: ChannelCredentials, options?: object): S;
}
let grpcServiceConfig: {
[key: string]: {
server_name: string;
server_port: number;
cert_pem_path: string | undefined;
}
};
const globalConfigPath = path.resolve(__dirname, '${getImportPath(grpcClientPath, path.join(grpcCodeGenPath, 'config.json'))}');
if (!fs.existsSync(globalConfigPath)) {
console.error('Please run: "yarn grpc-gen" first');
process.exit(-1);
}
const grpcServiceConfigPath = path.resolve(__dirname, '${getImportPath(grpcClientPath, path.join(process.cwd(), 'grpc-service.config.js'))}.js');
grpcServiceConfig = require(globalConfigPath);
let grpcServiceConfigLocal: any = {};
const serviceConfigFileExist = fs.existsSync(grpcServiceConfigPath);
if (serviceConfigFileExist) {
grpcServiceConfigLocal = require(grpcServiceConfigPath);
console.info('---------------------------');
console.info('Use local service config: ');
console.info(JSON.stringify(grpcServiceConfigLocal, (key, value) => value, 2));
console.info('---------------------------');
}
export default function getGrpcClient<S>(service: IService<S>): S {
const exec = /\\/([^/]+)-proto\\//.exec(service.$FILE_NAME);
if (exec) {
const serverName = exec[1];
const configLocal = grpcServiceConfigLocal[serverName];
if (serviceConfigFileExist && !configLocal) {
console.warn(\`Service: \$\{serverName\} not setting local, use global config, please ensure have set hosts\`)
}
const config = configLocal || grpcServiceConfig[serverName];
if (config) {
let credentials;
if (config.cert_pem_path) {
credentials = grpc.credentials.createSsl(
fs.readFileSync(path.join(__dirname, '${getImportPath(grpcClientPath, path.join(grpcCodeGenPath, 'ca.pem'))}')),
);
} else {
credentials = grpc.credentials.createInsecure();
}
return new service(\`\$\{config.server_name\}:\$\{config.server_port\}\`, credentials, {
'grpc.ssl_target_name_override': serverName,
'grpc.keepalive_time_ms': 3000,
'grpc.keepalive_timeout_ms': 2000,
});
}
}
throw new Error(\`\$\{service.$FILE_NAME\} config not exists!\`);
}
`,
].join('\n'));
}

@@ -407,7 +480,8 @@ else {

const responseType = `types.${getTsType(method.responseType, packageName, config).tsType}`;
return ` ${method.name}(
return ` /** @deprecated 请使用: ${method.name}V2 */
${method.name}(
request: ${requestType},
options?: { timeout?: number; flags?: number; host?: string; }
): Promise<${responseType}>;
/** @deprecated 请使用V2版本 */
/** @deprecated 请使用: ${method.name}V2 */
${method.name}(

@@ -436,2 +510,3 @@ request: ${requestType},

`import * as types from '${getImportPath(servicePath, typesPath)}';\n`,
`import getGrpcClient from '${getImportPath(servicePath, grpcClientPath)}';\n`,
`const config = require('${getImportPath(servicePath, configFilePath)}');\n`,

@@ -545,3 +620,4 @@ `const logOptions = config.logOptions ? { ...config.logOptions } : { enable: true, attributes: ['request'] } \n`,

`export const ${service.name}: ${typeName} = Service;`,
`export default ${service.name};\n`,
`export default ${service.name};`,
`export const ${service.name[0].toLowerCase()}${service.name.slice(1)} = getGrpcClient<${typeName}>(${service.name});\n`,
].join('\n'));

@@ -548,0 +624,0 @@ }

@@ -22,15 +22,18 @@ "use strict";

const opt = {};
if (!config) {
console.error('Must set config file: grpc-code-gen.config.js');
process.exit(1);
}
let configFilePath = undefined;
if (fs.existsSync(config)) {
if (path.isAbsolute(config)) {
configFilePath = config;
Object.assign(opt, require(config));
}
else {
configFilePath = path.join(process.cwd(), config);
Object.assign(opt, require(configFile));
}
if (path.isAbsolute(config)) {
configFilePath = config;
}
if (!configFilePath) {
console.error('Must set config file: grpc-code-gen.config.js');
else {
configFilePath = path.join(process.cwd(), config);
}
if (fs.existsSync(configFilePath)) {
Object.assign(opt, require(configFilePath));
}
else {
console.error(`Config file not exits: ${configFilePath}`);
process.exit(1);

@@ -37,0 +40,0 @@ }

{
"name": "grpc-code-gen",
"version": "4.3.11",
"version": "4.4.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "repository": "https://github.com/yunkeCN/grpc-code-gen.git",

@@ -252,3 +252,3 @@ import * as fs from 'fs-extra';

serviceCode?: boolean;
configFilePath: string;
configFilePath?: string;
grpcNpmName?: string;

@@ -355,2 +355,3 @@ }

const grpcObjPath = getAbsPath(`grpcObj.${moduleSuffix}`, baseDir);
const grpcClientPath = getAbsPath(`getGrpcClient.${moduleSuffix}`, baseDir);
if (typescript) {

@@ -366,4 +367,4 @@ await fs.writeFile(grpcObjPath, [

`let config;`,
`if (fs.existsSync(require.resolve('${getImportPath(grpcObjPath, configFilePath)}'))) {
config = require('${getImportPath(grpcObjPath, configFilePath)}');
`if (fs.existsSync(require.resolve('${getImportPath(grpcObjPath, configFilePath as string)}'))) {
config = require('${getImportPath(grpcObjPath, configFilePath as string)}');
}`,

@@ -429,2 +430,75 @@ `const grpcObject = grpc.loadPackageDefinition(loadFromJson(`,

].join('\n'));
const grpcCodeGenPath = path.join(process.cwd(), '.grpc-code-gen');
await fs.writeFile(grpcClientPath, [
fileTip,
`
import * as grpc from "@grpc/grpc-js/";
import { ChannelCredentials } from "@grpc/grpc-js//build/src/channel-credentials";
import * as fs from 'fs';
import * as path from 'path';
export interface IService<S> {
$FILE_NAME: string;
new(address: string, credentials: ChannelCredentials, options?: object): S;
}
let grpcServiceConfig: {
[key: string]: {
server_name: string;
server_port: number;
cert_pem_path: string | undefined;
}
};
const globalConfigPath = path.resolve(__dirname, '${getImportPath(grpcClientPath, path.join(grpcCodeGenPath, 'config.json'))}');
if (!fs.existsSync(globalConfigPath)) {
console.error('Please run: "yarn grpc-gen" first');
process.exit(-1);
}
const grpcServiceConfigPath = path.resolve(__dirname, '${getImportPath(grpcClientPath, path.join(process.cwd(), 'grpc-service.config.js'))}.js');
grpcServiceConfig = require(globalConfigPath);
let grpcServiceConfigLocal: any = {};
const serviceConfigFileExist = fs.existsSync(grpcServiceConfigPath);
if (serviceConfigFileExist) {
grpcServiceConfigLocal = require(grpcServiceConfigPath);
console.info('---------------------------');
console.info('Use local service config: ');
console.info(JSON.stringify(grpcServiceConfigLocal, (key, value) => value, 2));
console.info('---------------------------');
}
export default function getGrpcClient<S>(service: IService<S>): S {
const exec = /\\/([^/]+)-proto\\//.exec(service.$FILE_NAME);
if (exec) {
const serverName = exec[1];
const configLocal = grpcServiceConfigLocal[serverName];
if (serviceConfigFileExist && !configLocal) {
console.warn(\`Service: \$\{serverName\} not setting local, use global config, please ensure have set hosts\`)
}
const config = configLocal || grpcServiceConfig[serverName];
if (config) {
let credentials;
if (config.cert_pem_path) {
credentials = grpc.credentials.createSsl(
fs.readFileSync(path.join(__dirname, '${getImportPath(grpcClientPath, path.join(grpcCodeGenPath, 'ca.pem'))}')),
);
} else {
credentials = grpc.credentials.createInsecure();
}
return new service(\`\$\{config.server_name\}:\$\{config.server_port\}\`, credentials, {
'grpc.ssl_target_name_override': serverName,
'grpc.keepalive_time_ms': 3000,
'grpc.keepalive_timeout_ms': 2000,
});
}
}
throw new Error(\`\$\{service.$FILE_NAME\} config not exists!\`);
}
`,
].join('\n'));
} else {

@@ -487,7 +561,8 @@ await fs.writeFile(grpcObjPath, [

const responseType = `types.${getTsType(method.responseType, packageName, config).tsType}`;
return ` ${method.name}(
return ` /** @deprecated 请使用: ${method.name}V2 */
${method.name}(
request: ${requestType},
options?: { timeout?: number; flags?: number; host?: string; }
): Promise<${responseType}>;
/** @deprecated 请使用V2版本 */
/** @deprecated 请使用: ${method.name}V2 */
${method.name}(

@@ -517,3 +592,4 @@ request: ${requestType},

`import * as types from '${getImportPath(servicePath, typesPath)}';\n`,
`const config = require('${getImportPath(servicePath, configFilePath)}');\n`,
`import getGrpcClient from '${getImportPath(servicePath, grpcClientPath)}';\n`,
`const config = require('${getImportPath(servicePath, configFilePath as string)}');\n`,
`const logOptions = config.logOptions ? { ...config.logOptions } : { enable: true, attributes: ['request'] } \n`,

@@ -626,3 +702,4 @@ `const callOptions = config.callOptions ? { ...config.callOptions } : {} \n`,

`export const ${service.name}: ${typeName} = Service;`,
`export default ${service.name};\n`,
`export default ${service.name};`,
`export const ${service.name[0].toLowerCase()}${service.name.slice(1)} = getGrpcClient<${typeName}>(${service.name});\n`,
].join('\n'));

@@ -636,3 +713,3 @@ } else {

`const grpcObject = require('${getImportPath(servicePath, grpcObjPath)}');\n`,
`const config = require('${getImportPath(servicePath, configFilePath)}');\n`,
`const config = require('${getImportPath(servicePath, configFilePath as string)}');\n`,
`const logOptions = config.logOptions ? { ...config.logOptions } : { disable: false, attributes: ['request'] } \n`,

@@ -639,0 +716,0 @@ `const callOptions = config.callOptions ? { ...config.callOptions } : {} \n`,

@@ -32,15 +32,17 @@ import * as program from 'commander';

if (!config) {
console.error('Must set config file: grpc-code-gen.config.js');
process.exit(1);
}
let configFilePath = undefined;
if (fs.existsSync(config)) {
if (path.isAbsolute(config)) {
configFilePath = config;
Object.assign(opt, require(config));
} else {
configFilePath = path.join(process.cwd(), config);
Object.assign(opt, require(configFile));
}
if (path.isAbsolute(config)) {
configFilePath = config;
} else {
configFilePath = path.join(process.cwd(), config);
}
if (!configFilePath) {
console.error('Must set config file: grpc-code-gen.config.js');
if (fs.existsSync(configFilePath)) {
Object.assign(opt, require(configFilePath));
} else {
console.error(`Config file not exits: ${configFilePath}`);
process.exit(1);

@@ -47,0 +49,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc