@zapier/zapier-sdk 0.13.6 → 0.13.7
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.
- package/CHANGELOG.md +7 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +7 -2
- package/src/api/auth.ts +0 -28
- package/src/api/client.ts +0 -491
- package/src/api/debug.test.ts +0 -76
- package/src/api/debug.ts +0 -154
- package/src/api/index.ts +0 -90
- package/src/api/polling.test.ts +0 -405
- package/src/api/polling.ts +0 -253
- package/src/api/schemas.ts +0 -465
- package/src/api/types.ts +0 -152
- package/src/auth.ts +0 -72
- package/src/constants.ts +0 -16
- package/src/index.ts +0 -111
- package/src/plugins/api/index.ts +0 -43
- package/src/plugins/apps/index.ts +0 -203
- package/src/plugins/apps/schemas.ts +0 -64
- package/src/plugins/eventEmission/builders.ts +0 -115
- package/src/plugins/eventEmission/index.test.ts +0 -169
- package/src/plugins/eventEmission/index.ts +0 -294
- package/src/plugins/eventEmission/transport.test.ts +0 -214
- package/src/plugins/eventEmission/transport.ts +0 -135
- package/src/plugins/eventEmission/types.ts +0 -58
- package/src/plugins/eventEmission/utils.ts +0 -121
- package/src/plugins/fetch/index.ts +0 -83
- package/src/plugins/fetch/schemas.ts +0 -37
- package/src/plugins/findFirstAuthentication/index.test.ts +0 -209
- package/src/plugins/findFirstAuthentication/index.ts +0 -68
- package/src/plugins/findFirstAuthentication/schemas.ts +0 -47
- package/src/plugins/findUniqueAuthentication/index.test.ts +0 -197
- package/src/plugins/findUniqueAuthentication/index.ts +0 -77
- package/src/plugins/findUniqueAuthentication/schemas.ts +0 -49
- package/src/plugins/getAction/index.test.ts +0 -239
- package/src/plugins/getAction/index.ts +0 -75
- package/src/plugins/getAction/schemas.ts +0 -41
- package/src/plugins/getApp/index.test.ts +0 -181
- package/src/plugins/getApp/index.ts +0 -60
- package/src/plugins/getApp/schemas.ts +0 -33
- package/src/plugins/getAuthentication/index.test.ts +0 -294
- package/src/plugins/getAuthentication/index.ts +0 -95
- package/src/plugins/getAuthentication/schemas.ts +0 -38
- package/src/plugins/getProfile/index.ts +0 -60
- package/src/plugins/getProfile/schemas.ts +0 -24
- package/src/plugins/listActions/index.test.ts +0 -526
- package/src/plugins/listActions/index.ts +0 -132
- package/src/plugins/listActions/schemas.ts +0 -55
- package/src/plugins/listApps/index.test.ts +0 -378
- package/src/plugins/listApps/index.ts +0 -159
- package/src/plugins/listApps/schemas.ts +0 -41
- package/src/plugins/listAuthentications/index.test.ts +0 -739
- package/src/plugins/listAuthentications/index.ts +0 -152
- package/src/plugins/listAuthentications/schemas.ts +0 -77
- package/src/plugins/listInputFieldChoices/index.test.ts +0 -653
- package/src/plugins/listInputFieldChoices/index.ts +0 -173
- package/src/plugins/listInputFieldChoices/schemas.ts +0 -125
- package/src/plugins/listInputFields/index.test.ts +0 -439
- package/src/plugins/listInputFields/index.ts +0 -294
- package/src/plugins/listInputFields/schemas.ts +0 -68
- package/src/plugins/manifest/index.test.ts +0 -776
- package/src/plugins/manifest/index.ts +0 -461
- package/src/plugins/manifest/schemas.ts +0 -60
- package/src/plugins/registry/index.ts +0 -160
- package/src/plugins/request/index.test.ts +0 -333
- package/src/plugins/request/index.ts +0 -105
- package/src/plugins/request/schemas.ts +0 -69
- package/src/plugins/runAction/index.test.ts +0 -388
- package/src/plugins/runAction/index.ts +0 -215
- package/src/plugins/runAction/schemas.ts +0 -60
- package/src/resolvers/actionKey.ts +0 -37
- package/src/resolvers/actionType.ts +0 -34
- package/src/resolvers/appKey.ts +0 -7
- package/src/resolvers/authenticationId.ts +0 -54
- package/src/resolvers/index.ts +0 -11
- package/src/resolvers/inputFieldKey.ts +0 -70
- package/src/resolvers/inputs.ts +0 -69
- package/src/schemas/Action.ts +0 -52
- package/src/schemas/App.ts +0 -45
- package/src/schemas/Auth.ts +0 -59
- package/src/schemas/Field.ts +0 -169
- package/src/schemas/Run.ts +0 -40
- package/src/schemas/UserProfile.ts +0 -60
- package/src/sdk.test.ts +0 -212
- package/src/sdk.ts +0 -178
- package/src/types/domain.test.ts +0 -50
- package/src/types/domain.ts +0 -66
- package/src/types/errors.ts +0 -278
- package/src/types/events.ts +0 -43
- package/src/types/functions.ts +0 -28
- package/src/types/optional-zapier-sdk-cli-login.d.ts +0 -37
- package/src/types/plugin.ts +0 -125
- package/src/types/properties.ts +0 -80
- package/src/types/sdk.ts +0 -111
- package/src/types/telemetry-events.ts +0 -85
- package/src/utils/array-utils.test.ts +0 -131
- package/src/utils/array-utils.ts +0 -41
- package/src/utils/domain-utils.test.ts +0 -433
- package/src/utils/domain-utils.ts +0 -267
- package/src/utils/file-utils.test.ts +0 -73
- package/src/utils/file-utils.ts +0 -94
- package/src/utils/function-utils.test.ts +0 -141
- package/src/utils/function-utils.ts +0 -245
- package/src/utils/pagination-utils.test.ts +0 -620
- package/src/utils/pagination-utils.ts +0 -242
- package/src/utils/schema-utils.ts +0 -207
- package/src/utils/string-utils.test.ts +0 -45
- package/src/utils/string-utils.ts +0 -54
- package/src/utils/validation.test.ts +0 -51
- package/src/utils/validation.ts +0 -44
- package/tsconfig.build.json +0 -18
- package/tsconfig.json +0 -20
- package/tsconfig.tsbuildinfo +0 -1
- package/tsup.config.ts +0 -23
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Event builder utilities for creating telemetry events
|
|
3
|
-
*
|
|
4
|
-
* Provides builder functions that auto-populate common fields and ensure
|
|
5
|
-
* schema compliance for all event types.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type {
|
|
9
|
-
ErrorOccurredEvent,
|
|
10
|
-
ApplicationLifecycleEvent,
|
|
11
|
-
BaseEvent,
|
|
12
|
-
} from "../../types/telemetry-events";
|
|
13
|
-
import type {
|
|
14
|
-
ApplicationLifecycleEventData,
|
|
15
|
-
EnhancedErrorEventData,
|
|
16
|
-
ErrorEventData,
|
|
17
|
-
EventContext,
|
|
18
|
-
} from "./types";
|
|
19
|
-
import {
|
|
20
|
-
generateEventId,
|
|
21
|
-
getCurrentTimestamp,
|
|
22
|
-
getReleaseId,
|
|
23
|
-
getOsInfo,
|
|
24
|
-
getPlatformVersions,
|
|
25
|
-
getMemoryUsage,
|
|
26
|
-
getCpuTime,
|
|
27
|
-
isCi,
|
|
28
|
-
getCiPlatform,
|
|
29
|
-
} from "./utils";
|
|
30
|
-
import sdkPackageJson from "../../../package.json";
|
|
31
|
-
|
|
32
|
-
// Create base event with auto-populated common fields
|
|
33
|
-
// Kept for backward compatibility but can be replaced with direct construction
|
|
34
|
-
export function createBaseEvent(context: EventContext = {}): BaseEvent {
|
|
35
|
-
return {
|
|
36
|
-
event_id: generateEventId(),
|
|
37
|
-
timestamp_ms: getCurrentTimestamp(),
|
|
38
|
-
release_id: getReleaseId(),
|
|
39
|
-
customuser_id: context.customuser_id,
|
|
40
|
-
account_id: context.account_id,
|
|
41
|
-
identity_id: context.identity_id,
|
|
42
|
-
visitor_id: context.visitor_id,
|
|
43
|
-
correlation_id: context.correlation_id,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function buildErrorEvent(
|
|
48
|
-
data: ErrorEventData,
|
|
49
|
-
context: EventContext = {},
|
|
50
|
-
): ErrorOccurredEvent {
|
|
51
|
-
return {
|
|
52
|
-
...createBaseEvent(context),
|
|
53
|
-
zap_id: context.zap_id,
|
|
54
|
-
node_id: context.node_id,
|
|
55
|
-
selected_api: context.selected_api,
|
|
56
|
-
app_id: context.app_id,
|
|
57
|
-
app_version_id: context.app_version_id,
|
|
58
|
-
environment: context.environment,
|
|
59
|
-
sdk_version: sdkPackageJson.version,
|
|
60
|
-
...data,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function buildApplicationLifecycleEvent(
|
|
65
|
-
data: ApplicationLifecycleEventData,
|
|
66
|
-
context: EventContext = {},
|
|
67
|
-
): ApplicationLifecycleEvent {
|
|
68
|
-
const osInfo = getOsInfo();
|
|
69
|
-
const platformVersions = getPlatformVersions();
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
...createBaseEvent(context),
|
|
73
|
-
selected_api: context.selected_api,
|
|
74
|
-
app_id: context.app_id,
|
|
75
|
-
app_version_id: context.app_version_id,
|
|
76
|
-
sdk_version: sdkPackageJson.version,
|
|
77
|
-
cli_version: null,
|
|
78
|
-
memory_usage_bytes: data.memory_usage_bytes ?? getMemoryUsage(),
|
|
79
|
-
peak_memory_usage_bytes: data.peak_memory_usage_bytes ?? getMemoryUsage(),
|
|
80
|
-
cpu_time_ms: data.cpu_time_ms ?? getCpuTime(),
|
|
81
|
-
os_platform: osInfo.platform,
|
|
82
|
-
os_release: osInfo.release,
|
|
83
|
-
os_architecture: osInfo.architecture,
|
|
84
|
-
platform_versions: platformVersions,
|
|
85
|
-
environment: context.environment ?? (process.env.NODE_ENV || null),
|
|
86
|
-
is_ci_environment: isCi(),
|
|
87
|
-
ci_platform: getCiPlatform(),
|
|
88
|
-
session_id: null,
|
|
89
|
-
metadata: null,
|
|
90
|
-
process_argv: process.argv || null,
|
|
91
|
-
...data,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function buildErrorEventWithContext(
|
|
96
|
-
data: EnhancedErrorEventData,
|
|
97
|
-
context: EventContext = {},
|
|
98
|
-
): ErrorOccurredEvent {
|
|
99
|
-
const executionTime = data.execution_start_time
|
|
100
|
-
? Date.now() - data.execution_start_time
|
|
101
|
-
: null;
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
...createBaseEvent(context),
|
|
105
|
-
zap_id: context.zap_id,
|
|
106
|
-
node_id: context.node_id,
|
|
107
|
-
selected_api: context.selected_api,
|
|
108
|
-
app_id: context.app_id,
|
|
109
|
-
app_version_id: context.app_version_id,
|
|
110
|
-
environment: context.environment ?? (process.env.NODE_ENV || null),
|
|
111
|
-
sdk_version: sdkPackageJson.version,
|
|
112
|
-
execution_time_before_error_ms: executionTime,
|
|
113
|
-
...data,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for Event Emission Plugin
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
6
|
-
import { eventEmissionPlugin } from "./index";
|
|
7
|
-
import { createTransport } from "./transport";
|
|
8
|
-
import type { EventEmissionConfig } from "./index";
|
|
9
|
-
|
|
10
|
-
// Mock transport for testing
|
|
11
|
-
const mockTransport = {
|
|
12
|
-
emit: vi.fn().mockResolvedValue(undefined),
|
|
13
|
-
close: vi.fn().mockResolvedValue(undefined),
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
vi.mock("./transport", () => ({
|
|
17
|
-
createTransport: vi.fn(() => mockTransport),
|
|
18
|
-
}));
|
|
19
|
-
|
|
20
|
-
describe("eventEmissionPlugin", () => {
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
vi.clearAllMocks();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("should create plugin with default configuration", () => {
|
|
26
|
-
const plugin = eventEmissionPlugin({
|
|
27
|
-
sdk: {},
|
|
28
|
-
context: {
|
|
29
|
-
meta: {},
|
|
30
|
-
options: {},
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
expect(plugin.context.eventEmission).toBeDefined();
|
|
35
|
-
expect(plugin.context.eventEmission.emit).toBeDefined();
|
|
36
|
-
expect(plugin.context.eventEmission.transport).toBeDefined();
|
|
37
|
-
expect(plugin.context.eventEmission.config).toBeDefined();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("should create noop implementations when disabled", () => {
|
|
41
|
-
const config: EventEmissionConfig = { enabled: false };
|
|
42
|
-
const plugin = eventEmissionPlugin({
|
|
43
|
-
sdk: {},
|
|
44
|
-
context: {
|
|
45
|
-
meta: {},
|
|
46
|
-
options: { eventEmission: config },
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// Should not emit any events when disabled
|
|
51
|
-
plugin.context.eventEmission.emit("platform.sdk.TestEvent", {
|
|
52
|
-
test_event: "data",
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
expect(mockTransport.emit).not.toHaveBeenCalled();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("should emit events using generic emit method", async () => {
|
|
59
|
-
const plugin = eventEmissionPlugin({
|
|
60
|
-
sdk: {},
|
|
61
|
-
context: {
|
|
62
|
-
meta: {},
|
|
63
|
-
options: {
|
|
64
|
-
eventEmission: {
|
|
65
|
-
enabled: true,
|
|
66
|
-
transport: { type: "console" as const },
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const testEvent = {
|
|
73
|
-
test_data: "example",
|
|
74
|
-
value: 123,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const testSubject = "test.event.TestEvent";
|
|
78
|
-
|
|
79
|
-
plugin.context.eventEmission.emit(testSubject, testEvent);
|
|
80
|
-
|
|
81
|
-
// Give async emission time to complete
|
|
82
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
83
|
-
|
|
84
|
-
expect(mockTransport.emit).toHaveBeenCalledWith(testSubject, testEvent);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("should handle transport creation failures silently", () => {
|
|
88
|
-
// Mock createTransport to throw an error
|
|
89
|
-
vi.mocked(createTransport).mockImplementationOnce(() => {
|
|
90
|
-
throw new Error("Transport creation failed");
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
expect(() => {
|
|
94
|
-
eventEmissionPlugin({
|
|
95
|
-
sdk: {},
|
|
96
|
-
context: {
|
|
97
|
-
meta: {},
|
|
98
|
-
options: {
|
|
99
|
-
eventEmission: {
|
|
100
|
-
enabled: true,
|
|
101
|
-
transport: { type: "http" as const, endpoint: "invalid-url" },
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
}).not.toThrow();
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("should handle event emission failures silently", async () => {
|
|
110
|
-
// Mock transport to throw error
|
|
111
|
-
const failingTransport = {
|
|
112
|
-
emit: vi.fn().mockRejectedValue(new Error("Network error")),
|
|
113
|
-
close: vi.fn().mockResolvedValue(undefined),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
vi.mocked(createTransport).mockReturnValueOnce(failingTransport);
|
|
117
|
-
|
|
118
|
-
const plugin = eventEmissionPlugin({
|
|
119
|
-
sdk: {},
|
|
120
|
-
context: {
|
|
121
|
-
meta: {},
|
|
122
|
-
options: {
|
|
123
|
-
eventEmission: {
|
|
124
|
-
enabled: true,
|
|
125
|
-
transport: {
|
|
126
|
-
type: "http" as const,
|
|
127
|
-
endpoint: "https://example.com",
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Should not throw even if transport fails
|
|
135
|
-
expect(() => {
|
|
136
|
-
plugin.context.eventEmission.emit("test.event.TestEvent", {
|
|
137
|
-
test_event: "data",
|
|
138
|
-
});
|
|
139
|
-
}).not.toThrow();
|
|
140
|
-
|
|
141
|
-
// Give async emission time to complete
|
|
142
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
143
|
-
|
|
144
|
-
expect(failingTransport.emit).toHaveBeenCalled();
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("should merge options with defaults", () => {
|
|
148
|
-
const plugin = eventEmissionPlugin({
|
|
149
|
-
sdk: {},
|
|
150
|
-
context: {
|
|
151
|
-
meta: {},
|
|
152
|
-
options: {
|
|
153
|
-
eventEmission: {
|
|
154
|
-
transport: {
|
|
155
|
-
type: "http" as const,
|
|
156
|
-
endpoint: "https://example.com",
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
expect(plugin.context.eventEmission.config.enabled).toBe(true);
|
|
164
|
-
expect(plugin.context.eventEmission.config.transport).toEqual({
|
|
165
|
-
type: "http",
|
|
166
|
-
endpoint: "https://example.com",
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
});
|
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Event Emission Plugin for Zapier SDK
|
|
3
|
-
*
|
|
4
|
-
* Provides silent telemetry event emission capability to the SDK.
|
|
5
|
-
* All events are emitted asynchronously and failures are silently handled.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Plugin } from "../../types/plugin";
|
|
9
|
-
import type { BaseEvent } from "../../types/telemetry-events";
|
|
10
|
-
import type { EventTransport, TransportConfig } from "./transport";
|
|
11
|
-
import { createTransport } from "./transport";
|
|
12
|
-
import { generateEventId, getCurrentTimestamp, getReleaseId } from "./utils";
|
|
13
|
-
import {
|
|
14
|
-
buildApplicationLifecycleEvent,
|
|
15
|
-
buildErrorEventWithContext,
|
|
16
|
-
} from "./builders";
|
|
17
|
-
import { TRACKING_API_ENDPOINT } from "../../constants";
|
|
18
|
-
|
|
19
|
-
// Plugin configuration
|
|
20
|
-
export interface EventEmissionConfig {
|
|
21
|
-
enabled?: boolean;
|
|
22
|
-
transport?: TransportConfig;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Context provided by this plugin
|
|
26
|
-
export interface EventEmissionContext {
|
|
27
|
-
eventEmission: {
|
|
28
|
-
transport: EventTransport;
|
|
29
|
-
config: EventEmissionConfig;
|
|
30
|
-
// Generic event emission method
|
|
31
|
-
emit<T extends any>(subject: string, event: T): void;
|
|
32
|
-
// Builder for base event properties
|
|
33
|
-
createBaseEvent(): BaseEvent;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface EventEmissionProvides {
|
|
38
|
-
context: EventEmissionContext;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const APPLICATION_LIFECYCLE_EVENT_SUBJECT =
|
|
42
|
-
"platform.sdk.ApplicationLifecycleEvent";
|
|
43
|
-
const ERROR_OCCURRED_EVENT_SUBJECT = "platform.sdk.ErrorOccurredEvent";
|
|
44
|
-
|
|
45
|
-
// Silent emission wrapper - ensures events never disrupt SDK operation
|
|
46
|
-
async function silentEmit<T extends any>(
|
|
47
|
-
transport: EventTransport,
|
|
48
|
-
subject: string,
|
|
49
|
-
event: T,
|
|
50
|
-
): Promise<void> {
|
|
51
|
-
try {
|
|
52
|
-
// Fire and forget - don't await the transport
|
|
53
|
-
transport.emit(subject, event).catch(() => {
|
|
54
|
-
// Silently ignore transport failures
|
|
55
|
-
});
|
|
56
|
-
} catch {
|
|
57
|
-
// Silently ignore all errors
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Helper to get transport config from environment or defaults
|
|
62
|
-
function getTransportConfig(): TransportConfig {
|
|
63
|
-
const envTransport = process?.env?.ZAPIER_SDK_TELEMETRY_TRANSPORT;
|
|
64
|
-
|
|
65
|
-
if (envTransport === "noop" || envTransport === "disabled") {
|
|
66
|
-
return { type: "noop" };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (envTransport === "console") {
|
|
70
|
-
return { type: "console" };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Default to HTTP transport
|
|
74
|
-
const endpoint =
|
|
75
|
-
process?.env?.ZAPIER_SDK_TELEMETRY_ENDPOINT || TRACKING_API_ENDPOINT;
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
type: "http",
|
|
79
|
-
endpoint,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export const eventEmissionPlugin: Plugin<
|
|
84
|
-
{},
|
|
85
|
-
{ options: { eventEmission?: EventEmissionConfig } },
|
|
86
|
-
EventEmissionProvides
|
|
87
|
-
> = ({ context }) => {
|
|
88
|
-
const defaultTransport = getTransportConfig();
|
|
89
|
-
|
|
90
|
-
// Merge config: env var takes precedence over options, options take precedence over defaults
|
|
91
|
-
const config: EventEmissionConfig = {
|
|
92
|
-
enabled: context.options.eventEmission?.enabled ?? true,
|
|
93
|
-
transport:
|
|
94
|
-
// If env var is set, use it (defaultTransport will be from env)
|
|
95
|
-
process?.env?.ZAPIER_SDK_TELEMETRY_TRANSPORT
|
|
96
|
-
? defaultTransport
|
|
97
|
-
: // Otherwise, use option transport or default
|
|
98
|
-
(context.options.eventEmission?.transport ?? defaultTransport),
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const startupTime = Date.now();
|
|
102
|
-
let shutdownStartTime: number | null = null;
|
|
103
|
-
|
|
104
|
-
// If disabled, return noop implementations
|
|
105
|
-
if (!config.enabled) {
|
|
106
|
-
return {
|
|
107
|
-
context: {
|
|
108
|
-
eventEmission: {
|
|
109
|
-
transport: createTransport({ type: "noop" }),
|
|
110
|
-
config,
|
|
111
|
-
emit: () => {},
|
|
112
|
-
createBaseEvent: (): BaseEvent => ({
|
|
113
|
-
event_id: generateEventId(),
|
|
114
|
-
timestamp_ms: getCurrentTimestamp(),
|
|
115
|
-
release_id: getReleaseId(),
|
|
116
|
-
customuser_id: null,
|
|
117
|
-
account_id: null,
|
|
118
|
-
identity_id: null,
|
|
119
|
-
visitor_id: null,
|
|
120
|
-
correlation_id: null,
|
|
121
|
-
}),
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
let transport: EventTransport;
|
|
128
|
-
try {
|
|
129
|
-
transport = createTransport(config.transport || { type: "noop" });
|
|
130
|
-
} catch {
|
|
131
|
-
// If transport creation fails, fallback to noop to maintain silent operation
|
|
132
|
-
transport = createTransport({ type: "noop" });
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Helper to create base event
|
|
136
|
-
const createBaseEventHelper = (): BaseEvent => ({
|
|
137
|
-
event_id: generateEventId(),
|
|
138
|
-
timestamp_ms: getCurrentTimestamp(),
|
|
139
|
-
release_id: getReleaseId(),
|
|
140
|
-
customuser_id: null,
|
|
141
|
-
account_id: null,
|
|
142
|
-
identity_id: null,
|
|
143
|
-
visitor_id: null,
|
|
144
|
-
correlation_id: null,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
// Register lifecycle event handlers if enabled
|
|
148
|
-
if (config.enabled) {
|
|
149
|
-
// Emit startup event
|
|
150
|
-
const startupEvent = buildApplicationLifecycleEvent({
|
|
151
|
-
lifecycle_event_type: "startup",
|
|
152
|
-
});
|
|
153
|
-
silentEmit(transport, APPLICATION_LIFECYCLE_EVENT_SUBJECT, startupEvent);
|
|
154
|
-
|
|
155
|
-
// Register process event handlers (Node.js only)
|
|
156
|
-
if (typeof process?.on === "function") {
|
|
157
|
-
// Handle normal process exit
|
|
158
|
-
process.on("exit", (code: number) => {
|
|
159
|
-
const uptime = Date.now() - startupTime;
|
|
160
|
-
const shutdownDuration = shutdownStartTime
|
|
161
|
-
? Date.now() - shutdownStartTime
|
|
162
|
-
: null;
|
|
163
|
-
|
|
164
|
-
const exitEvent = buildApplicationLifecycleEvent({
|
|
165
|
-
lifecycle_event_type: "exit",
|
|
166
|
-
exit_code: code,
|
|
167
|
-
uptime_ms: uptime,
|
|
168
|
-
is_graceful_shutdown: code === 0,
|
|
169
|
-
shutdown_duration_ms: shutdownDuration,
|
|
170
|
-
});
|
|
171
|
-
silentEmit(transport, APPLICATION_LIFECYCLE_EVENT_SUBJECT, exitEvent);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
// Handle uncaught exceptions
|
|
175
|
-
process.on("uncaughtException", async (error: Error) => {
|
|
176
|
-
const errorEvent = buildErrorEventWithContext({
|
|
177
|
-
error_message: error.message || "Unknown error",
|
|
178
|
-
error_type: "UncaughtException",
|
|
179
|
-
error_stack_trace: error.stack || null,
|
|
180
|
-
error_severity: "critical",
|
|
181
|
-
is_user_facing: false,
|
|
182
|
-
is_recoverable: false,
|
|
183
|
-
execution_start_time: startupTime,
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
// Wait up to 300ms for telemetry to send before allowing process to exit
|
|
187
|
-
try {
|
|
188
|
-
await Promise.race([
|
|
189
|
-
transport.emit(ERROR_OCCURRED_EVENT_SUBJECT, errorEvent),
|
|
190
|
-
new Promise((resolve) => setTimeout(resolve, 300)),
|
|
191
|
-
]);
|
|
192
|
-
} catch {
|
|
193
|
-
// Silently ignore telemetry failures
|
|
194
|
-
}
|
|
195
|
-
// Let Node.js exit naturally after all handlers complete
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// Handle unhandled promise rejections
|
|
199
|
-
process.on(
|
|
200
|
-
"unhandledRejection",
|
|
201
|
-
async (reason: any, promise: Promise<any>) => {
|
|
202
|
-
const errorMessage =
|
|
203
|
-
reason instanceof Error
|
|
204
|
-
? reason.message
|
|
205
|
-
: typeof reason === "string"
|
|
206
|
-
? reason
|
|
207
|
-
: "Unhandled promise rejection";
|
|
208
|
-
|
|
209
|
-
const errorStack = reason instanceof Error ? reason.stack : null;
|
|
210
|
-
|
|
211
|
-
const errorEvent = buildErrorEventWithContext({
|
|
212
|
-
error_message: errorMessage,
|
|
213
|
-
error_type: "UnhandledRejection",
|
|
214
|
-
error_stack_trace: errorStack,
|
|
215
|
-
error_severity: "critical",
|
|
216
|
-
is_user_facing: false,
|
|
217
|
-
is_recoverable: false,
|
|
218
|
-
execution_start_time: startupTime,
|
|
219
|
-
error_metadata: {
|
|
220
|
-
promise: String(promise),
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
// Wait up to 300ms for telemetry to send
|
|
225
|
-
try {
|
|
226
|
-
await Promise.race([
|
|
227
|
-
transport.emit(ERROR_OCCURRED_EVENT_SUBJECT, errorEvent),
|
|
228
|
-
new Promise((resolve) => setTimeout(resolve, 300)),
|
|
229
|
-
]);
|
|
230
|
-
} catch {
|
|
231
|
-
// Silently ignore telemetry failures
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
// Handle termination signals
|
|
237
|
-
const handleSignal = async (signal: string) => {
|
|
238
|
-
shutdownStartTime = Date.now();
|
|
239
|
-
const uptime = Date.now() - startupTime;
|
|
240
|
-
|
|
241
|
-
const signalEvent = buildApplicationLifecycleEvent({
|
|
242
|
-
lifecycle_event_type: "signal_termination",
|
|
243
|
-
signal_name: signal,
|
|
244
|
-
uptime_ms: uptime,
|
|
245
|
-
is_graceful_shutdown: true,
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
// Wait up to 300ms for telemetry to send
|
|
249
|
-
try {
|
|
250
|
-
await Promise.race([
|
|
251
|
-
transport.emit(APPLICATION_LIFECYCLE_EVENT_SUBJECT, signalEvent),
|
|
252
|
-
new Promise((resolve) => setTimeout(resolve, 300)),
|
|
253
|
-
]);
|
|
254
|
-
} catch {
|
|
255
|
-
// Silently ignore telemetry failures
|
|
256
|
-
}
|
|
257
|
-
// Let other signal handlers run and decide when to exit
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
// Register signal handlers
|
|
261
|
-
["SIGINT", "SIGTERM"].forEach((signal) => {
|
|
262
|
-
process.on(signal as any, () => handleSignal(signal));
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return {
|
|
268
|
-
context: {
|
|
269
|
-
eventEmission: {
|
|
270
|
-
transport,
|
|
271
|
-
config,
|
|
272
|
-
emit: <T extends any>(subject: string, event: T) => {
|
|
273
|
-
silentEmit(transport, subject, event);
|
|
274
|
-
},
|
|
275
|
-
createBaseEvent: createBaseEventHelper,
|
|
276
|
-
},
|
|
277
|
-
},
|
|
278
|
-
};
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
// Export types and utilities for other plugins
|
|
282
|
-
export type {
|
|
283
|
-
EventContext,
|
|
284
|
-
ApplicationLifecycleEventData,
|
|
285
|
-
EnhancedErrorEventData,
|
|
286
|
-
} from "./types";
|
|
287
|
-
export {
|
|
288
|
-
buildApplicationLifecycleEvent,
|
|
289
|
-
buildErrorEventWithContext,
|
|
290
|
-
buildErrorEvent,
|
|
291
|
-
createBaseEvent,
|
|
292
|
-
} from "./builders";
|
|
293
|
-
export type { BaseEvent } from "../../types/telemetry-events";
|
|
294
|
-
export * from "./utils";
|