@prismatic-io/spectral 10.3.8 → 10.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ import { ActionContext as ServerActionContext } from ".";
2
+ import { ActionContext, DebugContext } from "../types";
3
+ export declare function createDebugContext(context: ServerActionContext): DebugContext;
4
+ export declare function logDebugResults(context: ActionContext): void;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDebugContext = createDebugContext;
4
+ exports.logDebugResults = logDebugResults;
5
+ const node_process_1 = require("node:process");
6
+ const node_perf_hooks_1 = require("node:perf_hooks");
7
+ function createDebugContext(context) {
8
+ const globalDebug = Boolean(context.globalDebug);
9
+ return {
10
+ enabled: globalDebug,
11
+ timeElapsed: {
12
+ mark: (actionContext, label) => {
13
+ if (globalDebug) {
14
+ actionContext.debug.results.timeElapsed.marks[label] = node_perf_hooks_1.performance.now();
15
+ }
16
+ },
17
+ measure: (actionContext, label, marks) => {
18
+ if (globalDebug) {
19
+ actionContext.debug.results.timeElapsed.measurements[label] = {
20
+ marks,
21
+ duration: actionContext.debug.results.timeElapsed.marks[marks.end] -
22
+ actionContext.debug.results.timeElapsed.marks[marks.start],
23
+ };
24
+ }
25
+ },
26
+ },
27
+ memoryUsage: (actionContext, label, showDetail) => {
28
+ if (globalDebug) {
29
+ // @ts-expect-error: memoryUsage.rss() is documented but not typed
30
+ const usage = showDetail ? (0, node_process_1.memoryUsage)() : node_process_1.memoryUsage.rss();
31
+ actionContext.debug.results.memoryUsage.push({
32
+ mark: label,
33
+ rss: typeof usage === "number" ? usage / 1000000 : usage.rss / 1000000,
34
+ detail: typeof usage === "number" ? undefined : usage,
35
+ });
36
+ }
37
+ },
38
+ results: {
39
+ timeElapsed: { marks: {}, measurements: {} },
40
+ memoryUsage: [],
41
+ allowedMemory: Number(context.runnerAllocatedMemoryMb),
42
+ },
43
+ };
44
+ }
45
+ function logDebugResults(context) {
46
+ if (context.debug.enabled) {
47
+ context.logger.metric(context.debug.results);
48
+ }
49
+ }
@@ -23,6 +23,7 @@ const convertComponent_1 = require("./convertComponent");
23
23
  const integration_1 = require("./integration");
24
24
  const merge_1 = __importDefault(require("lodash/merge"));
25
25
  const perform_1 = require("./perform");
26
+ const context_1 = require("./context");
26
27
  const convertIntegration = (definition) => {
27
28
  var _a, _b, _c;
28
29
  // Generate a unique reference key that will be used to reference the
@@ -193,7 +194,9 @@ const convertComponentReference = (componentReference, componentRegistry, refere
193
194
  type: "value",
194
195
  value: JSON.stringify(v),
195
196
  }))
196
- : value.value;
197
+ : manifestEntryInput.collection === "valuelist" && Array.isArray(value.value)
198
+ ? value.value.map((v) => ({ type: "value", value: v }))
199
+ : value.value;
197
200
  const formattedValue = type === "complex" || typeof valueExpr === "string"
198
201
  ? valueExpr
199
202
  : JSON.stringify(valueExpr);
@@ -516,7 +519,7 @@ const generateOnInstanceWrapperFn = (componentRef, onTrigger, eventName, customF
516
519
  : customFn;
517
520
  return onInstanceFn;
518
521
  };
519
- const convertOnExecution = (onExecution, componentRegistry) => (context, params) => {
522
+ const convertOnExecution = (onExecution, componentRegistry) => (context, params) => __awaiter(void 0, void 0, void 0, function* () {
520
523
  // @ts-expect-error _components isn't part of the public API
521
524
  const { _components } = context;
522
525
  const invoke = _components.invoke;
@@ -549,8 +552,11 @@ const convertOnExecution = (onExecution, componentRegistry) => (context, params)
549
552
  }, {});
550
553
  return Object.assign(Object.assign({}, accumulator), { [registryComponentKey]: componentActions });
551
554
  }, {});
552
- return onExecution(Object.assign(Object.assign({}, context), { components: componentMethods, invokeFlow: (0, perform_1.createInvokeFlow)(context, { isCNI: true }) }), params);
553
- };
555
+ const actionContext = Object.assign(Object.assign({}, context), { debug: (0, context_1.createDebugContext)(context), components: componentMethods, invokeFlow: (0, perform_1.createInvokeFlow)(context, { isCNI: true }) });
556
+ const result = yield onExecution(actionContext, params);
557
+ (0, context_1.logDebugResults)(actionContext);
558
+ return result;
559
+ });
554
560
  /** Creates the structure necessary to import a Component as part of a
555
561
  * Code Native integration. */
556
562
  const codeNativeIntegrationComponent = ({ name, iconPath, description, flows = [], componentRegistry = {} }, referenceKey, configVars) => {
@@ -1,4 +1,4 @@
1
- import { InstanceAttributes, CustomerAttributes, DataSourceType, DataSourceResultType, UserAttributes, TriggerEventFunctionReturn, IntegrationAttributes, FlowAttributes, ConfigVarResultCollection, ComponentManifest, FlowInvoker, ExecutionFrame } from "../types";
1
+ import { InstanceAttributes, CustomerAttributes, DataSourceType, DataSourceResultType, UserAttributes, TriggerEventFunctionReturn, IntegrationAttributes, FlowAttributes, ConfigVarResultCollection, ComponentManifest, FlowInvoker, ExecutionFrame, DebugContext } from "../types";
2
2
  interface DisplayDefinition {
3
3
  label: string;
4
4
  description: string;
@@ -54,11 +54,6 @@ export type ActionContext<TConfigVars extends ConfigVarResultCollection = Config
54
54
  executionState: Record<string, unknown>;
55
55
  integrationState: Record<string, unknown>;
56
56
  configVars: TConfigVars;
57
- components: {
58
- [K in keyof TComponentActions]: {
59
- [A in keyof TComponentActions[K]]: TComponentActions[K][A]["perform"];
60
- };
61
- };
62
57
  stepId: string;
63
58
  executionId: string;
64
59
  webhookUrls: Record<string, string>;
@@ -70,9 +65,16 @@ export type ActionContext<TConfigVars extends ConfigVarResultCollection = Config
70
65
  integration: IntegrationAttributes;
71
66
  flow: FlowAttributes;
72
67
  startedAt: string;
73
- invokeFlow: FlowInvoker<TFlows>;
74
68
  executionFrame: ExecutionFrame;
75
- globalDebug: boolean;
69
+ globalDebug?: boolean;
70
+ runnerAllocatedMemoryMb?: number;
71
+ components: {
72
+ [K in keyof TComponentActions]: {
73
+ [A in keyof TComponentActions[K]]: TComponentActions[K][A]["perform"];
74
+ };
75
+ };
76
+ invokeFlow: FlowInvoker<TFlows>;
77
+ debug: DebugContext;
76
78
  };
77
79
  type TriggerOptionChoice = "invalid" | "valid" | "required";
78
80
  export interface TriggerPayload {
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.createPollingPerform = exports.createPerform = exports.createInvokeFlow = exports.cleanParams = void 0;
27
27
  const axios_1 = __importDefault(require("axios"));
28
28
  const uniq_1 = __importDefault(require("lodash/uniq"));
29
+ const context_1 = require("./context");
29
30
  const cleanParams = (params, cleaners) => {
30
31
  const keys = (0, uniq_1.default)([...Object.keys(params), ...Object.keys(cleaners)]);
31
32
  return keys.reduce((result, key) => {
@@ -63,10 +64,16 @@ const createPerform = (performFn, { inputCleaners, errorHandler }) => {
63
64
  }
64
65
  if (args.length === 2) {
65
66
  const [context, params] = args;
66
- return yield performFn(Object.assign(Object.assign({}, context), { invokeFlow: (0, exports.createInvokeFlow)(context) }), (0, exports.cleanParams)(params, inputCleaners));
67
+ const actionContext = Object.assign(Object.assign({}, context), { debug: (0, context_1.createDebugContext)(context), invokeFlow: (0, exports.createInvokeFlow)(context) });
68
+ const result = yield performFn(actionContext, (0, exports.cleanParams)(params, inputCleaners));
69
+ (0, context_1.logDebugResults)(actionContext);
70
+ return result;
67
71
  }
68
72
  const [context, payload, params] = args;
69
- return yield performFn(Object.assign(Object.assign({}, context), { invokeFlow: (0, exports.createInvokeFlow)(context) }), payload, (0, exports.cleanParams)(params, inputCleaners));
73
+ const actionContext = Object.assign(Object.assign({}, context), { debug: (0, context_1.createDebugContext)(context), invokeFlow: (0, exports.createInvokeFlow)(context) });
74
+ const result = yield performFn(actionContext, payload, (0, exports.cleanParams)(params, inputCleaners));
75
+ (0, context_1.logDebugResults)(actionContext);
76
+ return result;
70
77
  }
71
78
  catch (error) {
72
79
  throw errorHandler ? errorHandler(error) : error;
package/dist/testing.js CHANGED
@@ -113,7 +113,19 @@ const createActionContext = (context) => {
113
113
  executionStartedAt: "",
114
114
  stepName: "some-step",
115
115
  loopPath: "",
116
- }, globalDebug: false }, context);
116
+ }, debug: {
117
+ enabled: false,
118
+ timeElapsed: {
119
+ mark: (context, label) => { },
120
+ measure: (context, label, marks) => { },
121
+ },
122
+ memoryUsage: (context, label, showDetail) => { },
123
+ results: {
124
+ timeElapsed: { marks: {}, measurements: {} },
125
+ memoryUsage: [],
126
+ allowedMemory: 1024,
127
+ },
128
+ } }, context);
117
129
  };
118
130
  const createDataSourceContext = (context) => {
119
131
  return Object.assign({ logger: (0, exports.loggerMock)(), configVars: {}, customer: {
@@ -10,6 +10,54 @@ interface StandardLineage {
10
10
  interface CustomLineage {
11
11
  customSource: string;
12
12
  }
13
+ export interface DebugContext {
14
+ /** READ-ONLY: Denotes whether code should be executed in debug mode. */
15
+ enabled: boolean;
16
+ /** Helper methods for measuring how long a process takes between marks. Only runs if debug.enabled is true. */
17
+ timeElapsed: {
18
+ /** Creates a mark for measuring time in your process. */
19
+ mark: (context: ActionContext, label: string) => void;
20
+ /** Measures the time spent between two marks. The label should be a unique descriptor of this duration. */
21
+ measure: (context: ActionContext, label: string, marks: {
22
+ start: string;
23
+ end: string;
24
+ }) => void;
25
+ };
26
+ /** Measures memory usage up until that point in the process. showDetail will run slower but show the full output of process.memoryUsage().
27
+ * Only runs if debug.enabled is true. */
28
+ memoryUsage: (context: ActionContext, label: string, showDetail?: boolean) => void;
29
+ /** Resulting debug measurements that can be logged or saved. */
30
+ results: DebugResult;
31
+ }
32
+ interface DebugResult {
33
+ /** Resulting data about time measurements. */
34
+ timeElapsed: {
35
+ /** The set of recorded marks and their measured start times. */
36
+ marks: Record<string, number>;
37
+ /** The set of measured durations based on the difference in marked times. */
38
+ measurements: Record<string, {
39
+ marks: {
40
+ start: string;
41
+ end: string;
42
+ };
43
+ duration: number;
44
+ }>;
45
+ };
46
+ /** Memory limit in MB */
47
+ allowedMemory: number;
48
+ /** Resulting data bout memory usage. */
49
+ memoryUsage: Array<{
50
+ mark: string;
51
+ rss: number;
52
+ detail?: {
53
+ rss: number;
54
+ heapTotal: number;
55
+ heapUsed: number;
56
+ external: number;
57
+ arrayBuffers: number;
58
+ };
59
+ }>;
60
+ }
13
61
  export type ExecutionFrame = ({
14
62
  invokedByExecutionJWT: string;
15
63
  invokedByExecutionStartedAt: string;
@@ -19,7 +67,7 @@ export type ExecutionFrame = ({
19
67
  } & CustomLineage);
20
68
  export type FlowInvoker<TFlows extends Readonly<string[]> | undefined> = (flowName: TFlows extends Readonly<string[]> ? TFlows[number] : string, data?: Record<string, unknown>, config?: AxiosRequestConfig<any>, source?: string) => Promise<AxiosResponse<any, any>>;
21
69
  /** Definition of the function to perform when an Action is invoked. */
22
- export type ActionPerformFunction<TInputs extends Inputs, TConfigVars extends ConfigVarResultCollection, TComponentActions extends Record<string, ComponentManifest["actions"]>, TAllowsBranching extends boolean | undefined, TReturn extends ActionPerformReturn<TAllowsBranching, unknown>> = (context: ActionContext<TConfigVars, TComponentActions>, params: ActionInputParameters<TInputs>) => Promise<TReturn>;
70
+ export type ActionPerformFunction<TInputs extends Inputs = Inputs, TConfigVars extends ConfigVarResultCollection = ConfigVarResultCollection, TComponentActions extends Record<string, ComponentManifest["actions"]> = Record<string, ComponentManifest["actions"]>, TAllowsBranching extends boolean | undefined = undefined, TReturn extends ActionPerformReturn<TAllowsBranching, unknown> = ActionPerformReturn<TAllowsBranching, unknown>> = (context: ActionContext<TConfigVars, TComponentActions>, params: ActionInputParameters<TInputs>) => Promise<TReturn>;
23
71
  /** Context provided to perform method containing helpers and contextual data */
24
72
  export type ActionContext<TConfigVars extends ConfigVarResultCollection = ConfigVarResultCollection, TComponentActions extends Record<string, ComponentManifest["actions"]> = Record<string, ComponentManifest["actions"]>, TFlows extends string[] = string[]> = {
25
73
  /** Logger for permanent logging; console calls are also captured */
@@ -62,11 +110,11 @@ export type ActionContext<TConfigVars extends ConfigVarResultCollection = Config
62
110
  flow: FlowAttributes;
63
111
  /** The time in UTC that execution started. */
64
112
  startedAt: string;
65
- /** Function to invoke an execution of another flow.. */
113
+ /** Function to invoke an execution of another flow. */
66
114
  invokeFlow: FlowInvoker<TFlows>;
67
115
  /** Reference to the current execution and, when applicable, the current step. */
68
116
  executionFrame: ExecutionFrame;
69
- /** Denotes whether code should be executed in debug mode. */
70
- globalDebug: boolean;
117
+ /** Contains methods, flags, and data to support debug modes. */
118
+ debug: DebugContext;
71
119
  };
72
120
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismatic-io/spectral",
3
- "version": "10.3.8",
3
+ "version": "10.3.10",
4
4
  "description": "Utility library for building Prismatic connectors and code-native integrations",
5
5
  "keywords": ["prismatic"],
6
6
  "main": "dist/index.js",