diff --git a/.vscode/settings.json b/.vscode/settings.json index b9dece0cc..b1875e6e4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "eslint.enable": true, - "typescript.surveys.enabled": false, - "typescript.tsdk": "node_modules/typescript/lib" -} \ No newline at end of file + "typescript.surveys.enabled": false, + "typescript.tsdk": "node_modules/typescript/lib" +} diff --git a/jsconfig.json b/jsconfig.json deleted file mode 100644 index 847dbb7ed..000000000 --- a/jsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "compilerOptions": { - "checkJs": false - }, - "exclude": [ - "node_modules" - ] -} diff --git a/src/context.d.ts b/src/context.d.ts index 383303824..aebd01314 100644 --- a/src/context.d.ts +++ b/src/context.d.ts @@ -1,10 +1,13 @@ import type { BulkheadOptions } from "./middlewares"; -import Endpoint = require("./registry/endpoint"); +import ActionEndpoint = require("./registry/endpoint-action"); +import EventEndpoint = require("./registry/endpoint-event"); import type { CallingOptions, MCallDefinition, MCallCallingOptions } from "./service-broker"; import Service = require("./service"); import Span = require("./tracing/span"); import type { ActionHandler, ActionParams, ActionSchema, TracingEventOptions } from "./service"; import type ServiceBroker = require("./service-broker"); +import { Stream } from "stream"; +import util = require("util"); declare namespace Context { export interface EventSchema { @@ -27,22 +30,22 @@ declare namespace Context { } } -declare class Context> { +declare class Context, THeaders = Record> { static create( broker: ServiceBroker, - endpoint: Endpoint, + endpoint: ActionEndpoint|EventEndpoint, params: Record, opts: Record ): Context; - static create(broker: ServiceBroker, endpoint: Endpoint, params: Record): Context; - static create(broker: ServiceBroker, endpoint: Endpoint): Context; + static create(broker: ServiceBroker, endpoint: ActionEndpoint|EventEndpoint, params: Record): Context; + static create(broker: ServiceBroker, endpoint: ActionEndpoint|EventEndpoint): Context; static create(broker: ServiceBroker): Context; id: string; broker: ServiceBroker; - endpoint: Endpoint | null; + endpoint: ActionEndpoint | EventEndpoint | null; action: ActionSchema | null; @@ -80,13 +83,19 @@ declare class Context(eventName: string, data: D): Promise; broadcast(eventName: string): Promise; - copy(endpoint: Endpoint): this; + copy(endpoint: ActionEndpoint|EventEndpoint): this; copy(): this; startSpan(name: string, opts?: Record): Span; @@ -123,5 +132,11 @@ declare class Context; + + startHrTime: [number, number] | null; + + _spanStack: Span[]; + + [util.inspect.custom](depth?: number, options?: Record): string; } export = Context; diff --git a/src/context.js b/src/context.js index 1a069a8f2..82aa44a9f 100644 --- a/src/context.js +++ b/src/context.js @@ -10,13 +10,20 @@ const util = require("util"); const { pick } = require("lodash"); const { RequestSkippedError, MaxCallLevelError } = require("./errors"); +/** + * @typedef {import("./service-broker")} ServiceBroker Moleculer Service Broker instance + * @typedef {import("./service-broker").CallingOptions} CallingOptions Calling Options + * @typedef {import("./registry/endpoint")} Endpoint Registry Endpoint + * @typedef {import("./registry/endpoint-action")} ActionEndpoint Registry Action Endpoint + * @typedef {import("./registry/endpoint-event")} EventEndpoint Registry Event Endpoint + * @typedef {import("./tracing/span")} Span Tracing Span + */ + /** * Merge metadata * + * @param {Context} ctx * @param {Object} newMeta - * - * @private - * @memberof Context */ function mergeMeta(ctx, newMeta) { if (newMeta) Object.assign(ctx.meta, newMeta); @@ -26,14 +33,6 @@ function mergeMeta(ctx, newMeta) { /** * Context class for action calls * - * @property {String} id - Context ID - * @property {ServiceBroker} broker - Broker instance - * @property {Action} action - Action definition - * @property {String} [nodeID=null] - Node ID - * @property {String} parentID - Parent Context ID - * @property {Boolean} tracing - Enable tracing - * @property {Number} [level=1] - Level of context - * * @class Context */ class Context { @@ -41,7 +40,7 @@ class Context { * Creates an instance of Context. * * @param {ServiceBroker} broker - Broker instance - * @param {Endpoint} endpoint + * @param {ActionEndpoint|EventEndpoint} endpoint * * @memberof Context */ @@ -70,6 +69,7 @@ class Context { // The groups of event this.eventGroups = null; + /** @type {CallingOptions} */ this.options = { timeout: null, retries: null @@ -97,12 +97,8 @@ class Context { this.needAck = null; this.ackID = null; - //this.startTime = null; - //his.startHrTime = null; - //this.stopTime = null; - //this.duration = null; + this.startHrTime = null; - //this.error = null; this.cachedResult = false; } @@ -110,9 +106,9 @@ class Context { * Create a new Context instance * * @param {ServiceBroker} broker - * @param {Endpoint} endpoint + * @param {ActionEndpoint|EventEndpoint} endpoint * @param {Object?} params - * @param {Object} opts + * @param {CallingOptions} opts * @returns {Context} * * @static @@ -184,7 +180,7 @@ class Context { * @returns {Context} */ copy(ep) { - const newCtx = new this.constructor(this.broker); + const newCtx = this.constructor(this.broker); newCtx.nodeID = this.nodeID; newCtx.setEndpoint(ep || this.endpoint); @@ -215,7 +211,7 @@ class Context { /** * Set endpoint of context * - * @param {Endpoint} endpoint + * @param {ActionEndpoint|EventEndpoint} endpoint * @memberof Context */ setEndpoint(endpoint) { @@ -252,7 +248,7 @@ class Context { * * @param {String} actionName * @param {Object?} params - * @param {Object?} opts + * @param {Object?} _opts * @returns {Promise} * * @example Call an other service with params & options @@ -369,7 +365,7 @@ class Context { * Emit an event (grouped & balanced global event) * * @param {string} eventName - * @param {any?} payload + * @param {any?} data * @param {Object?} opts * @returns {Promise} * @@ -391,7 +387,7 @@ class Context { * Emit an event for all local & remote services * * @param {string} eventName - * @param {any?} payload + * @param {any?} data * @param {Object?} opts * @returns {Promise} * diff --git a/src/service-broker.d.ts b/src/service-broker.d.ts index ad58510ee..c055e50a9 100644 --- a/src/service-broker.d.ts +++ b/src/service-broker.d.ts @@ -217,8 +217,8 @@ declare namespace ServiceBroker { export type FallbackResponseHandler = (ctx: Context, err: MoleculerError) => Promise; export interface CallingOptions { - timeout?: number; - retries?: number; + timeout?: number | null; + retries?: number | null; fallbackResponse?: FallbackResponse | FallbackResponse[] | FallbackResponseHandler; nodeID?: string; meta?: Record; @@ -228,6 +228,7 @@ declare namespace ServiceBroker { tracking?: boolean; paramsCloning?: boolean; caller?: string; + headers?: Record; } export interface MCallCallingOptions extends CallingOptions { diff --git a/src/tracing/span.d.ts b/src/tracing/span.d.ts index c305fcc90..f30265f05 100644 --- a/src/tracing/span.d.ts +++ b/src/tracing/span.d.ts @@ -40,12 +40,13 @@ declare class Span { logs: Span.SpanLogEntry[]; tags: Record; - start(time?: number): Span; + start(time?: number | null): Span; addTags(obj: Record): Span; log(name: string, fields?: Record, time?: number): Span; setError(err: Error): Span; - finish(time?: number): Span; + finish(time?: number | null): Span; startSpan(name: string, opts?: Record): Span; + isActive(): boolean; } export = Span; diff --git a/tsconfig.json b/tsconfig.json index b0f544c3a..7f57a2311 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "outDir": "dist", "target": "ES2022", "moduleResolution": "node", - "noEmit": true + "noEmit": true, }, "include": ["**.js"], "exclude": ["node_modules", ".eslintrc.js", "prettier.config.js", "types"]