UNPKG

skywalking-backend-js-netease

Version:

The NodeJS agent for Apache SkyWalking

106 lines 5.18 kB
"use strict"; /*! * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const GrpcPlugin_1 = tslib_1.__importStar(require("./GrpcPlugin")); class GrpcJSPlugin { constructor() { this.module = '@grpc/grpc-js'; // nodejs websocket this.versions = '*'; } install(installer) { const makeClient = installer.require('@grpc/grpc-js/build/src/make-client'); const grpc = installer.require('@grpc/grpc-js'); // grpc.makeGenericClientConstructor -> client.makeClientConstructor -> function Client(address, credentials, options) this.interceptClientConstruct(grpc, makeClient, 'makeClientConstructor'); GrpcPlugin_1.default.interceptServerOperation(grpc, grpc.Server, 'addService'); } interceptClientConstruct(Mod, Cls, name) { const _original = Cls[name]; const self = this; Cls[name] = function () { let ret = _original.apply(this, arguments); const cls = arguments[0]; Object.keys(cls).forEach((name) => { if (isPrototypePolluted(name)) { return; } let methodType; const attrs = cls[name]; if (attrs.requestStream) { methodType = attrs.responseStream ? 'bidi' : 'client_stream'; } else { methodType = attrs.responseStream ? 'server_stream' : 'unary'; } const _origin = ret.prototype[name]; ret.prototype[name] = function (argOrMeta, metaOrOpts, optsOrCb, callback) { var _a, _b; let checkedArguments = {}; let args = []; switch (methodType) { case 'unary': checkedArguments = this.checkOptionalUnaryResponseArguments(metaOrOpts, optsOrCb, callback); args = [argOrMeta, checkedArguments.metadata, checkedArguments.options]; break; case 'client_stream': checkedArguments = this.checkOptionalUnaryResponseArguments(argOrMeta, metaOrOpts, optsOrCb); args = [checkedArguments.metadata, checkedArguments.options]; break; case 'server_stream': checkedArguments = this.checkMetadataAndOptions(metaOrOpts, optsOrCb); args = [argOrMeta, checkedArguments.metadata, checkedArguments.options]; break; case 'bidi': checkedArguments = this.checkMetadataAndOptions(argOrMeta, metaOrOpts); args = [checkedArguments.metadata, checkedArguments.options]; } checkedArguments.options = self.addClientIntercepter(Mod, ((_b = (_a = this.getChannel()) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.path) || this.getChannel().getTarget(), checkedArguments.options); if (checkedArguments.callback) { args.push(checkedArguments.callback); } return _origin.apply(this, args); }; if (attrs.originalName && !isPrototypePolluted(attrs.originalName)) { ret.prototype[attrs.originalName] = ret.prototype[name]; } }); return ret; }; } addClientIntercepter(Cls, address, options) { if (!options) { options = {}; } options.interceptors = options.interceptors || []; options.interceptors.push(function (opts, nextCall) { const tracer = new GrpcPlugin_1.TracingRequester(address, opts); const interceptc = new Cls.InterceptingCall(nextCall(opts), tracer); interceptc.requester._SKYWALKING_GRPC_OBJ = tracer._SKYWALKING_GRPC_OBJ; return interceptc; }); return options; } } function isPrototypePolluted(key) { return ['__proto__', 'prototype', 'constructor'].includes(key); } exports.default = new GrpcJSPlugin(); //# sourceMappingURL=GrpcJSPlugin.js.map