@melio-eng/web-sdk 1.1.0-pr.92.a38482a → 1.1.0-pr.93.ae7fb3c
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/README.md +21 -0
- package/dist/flows/Flow.d.ts +18 -0
- package/dist/flows/Flow.js +212 -63
- package/dist/flows/InitFlow.d.ts +20 -0
- package/dist/flows/InitFlow.js +213 -36
- package/dist/flows/logger.d.ts +61 -0
- package/dist/flows/logger.js +153 -0
- package/dist/flows/utils.js +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +57 -34
- package/dist/types.d.ts +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ const init = melioSDK.init("AUTH_CODE_FROM_YOUR_BACKEND", {
|
|
|
28
28
|
partnerName: "your-partner-name", // required
|
|
29
29
|
environment: "production", // optional — defaults to 'production'
|
|
30
30
|
keepAlive: true, // optional — keeps the session warm
|
|
31
|
+
debug: true, // optional — verbose console logging
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
init.on("authenticationSucceeded", () => {
|
|
@@ -65,9 +66,29 @@ melioSDK.init(authenticationCode: string, options: InitOptions): InitFlowInstanc
|
|
|
65
66
|
- `keepAlive` (boolean): If true, the session will be kept alive in the background via an invisible iframe
|
|
66
67
|
- `environment` (Environment): The environment to use for API endpoints (defaults to `'production'`)
|
|
67
68
|
- `branchOverride` (string): Advanced/testing only
|
|
69
|
+
- `debug` (boolean): If true, prints debug-level logs to the console on top of the warnings and errors the SDK always prints (defaults to `false`)
|
|
68
70
|
|
|
69
71
|
`init` returns an `InitFlowInstance` you can listen on for `authenticationSucceeded` / `authenticationFailed`.
|
|
70
72
|
|
|
73
|
+
### Debug Logging
|
|
74
|
+
|
|
75
|
+
The SDK always prints warnings and errors to the browser console, prefixed with `[melio-sdk]`. Pass `debug: true` to `init()` to add debug-level output:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
melioSDK.init("auth_code", { partnerName: "your-partner-name", debug: true });
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Every line carries `msSinceInit`, the elapsed time since `init()` was called — which is what makes a flow that *hangs* (rather than fails) visible in a console capture.
|
|
82
|
+
|
|
83
|
+
Debug output covers:
|
|
84
|
+
|
|
85
|
+
- host environment at init (user agent, `readyState`, whether the SDK is running inside a frame)
|
|
86
|
+
- iframe lifecycle for the hidden auth iframe and every visible flow, with `loadMs` / `authMs` / `readyMs` timings
|
|
87
|
+
- every `postMessage` exchanged with the embedded app, including message types the SDK does not handle
|
|
88
|
+
- a keep-alive heartbeat (when `keepAlive: true`), which doubles as proof the page is still executing
|
|
89
|
+
|
|
90
|
+
Regardless of the flag, a flow that neither loads nor becomes interactive is reported as an error every 15 seconds, up to 8 times. Auth codes and customer details are redacted from all output.
|
|
91
|
+
|
|
71
92
|
### Environment Configuration
|
|
72
93
|
|
|
73
94
|
The SDK supports multiple environments to facilitate development and testing:
|
package/dist/flows/Flow.d.ts
CHANGED
|
@@ -13,11 +13,28 @@ export declare class Flow implements FlowInstance {
|
|
|
13
13
|
private eventListeners;
|
|
14
14
|
protected keepAliveInterval: number | null;
|
|
15
15
|
private messageHandler;
|
|
16
|
+
private loadWatchdog;
|
|
17
|
+
private watchdogReports;
|
|
18
|
+
private iframeLoaded;
|
|
19
|
+
private readyForInteraction;
|
|
20
|
+
private injectedAt;
|
|
16
21
|
constructor(containerId: string, config: BaseFlowConfig, partnerName: string, environment: Environment, branchOverride?: string | undefined);
|
|
17
22
|
/**
|
|
18
23
|
* Initialize the flow by creating and injecting the iframe
|
|
19
24
|
*/
|
|
20
25
|
initialize(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Report a flow that has neither loaded nor become interactive within the budget.
|
|
28
|
+
*
|
|
29
|
+
* Diagnostic only: this logs and does nothing else. It separates "the iframe
|
|
30
|
+
* never loaded" from "it loaded but never reported READY_FOR_INTERACTION",
|
|
31
|
+
* which is the difference between a transport problem and the embedded app
|
|
32
|
+
* stalling after its document arrived.
|
|
33
|
+
*/
|
|
34
|
+
private startLoadWatchdog;
|
|
35
|
+
/** Milliseconds since the iframe was injected, for load/ready timings. */
|
|
36
|
+
private sinceInject;
|
|
37
|
+
private clearLoadWatchdog;
|
|
21
38
|
/**
|
|
22
39
|
* Construct the specific flow URL - can be overridden by subclasses
|
|
23
40
|
*/
|
|
@@ -27,6 +44,7 @@ export declare class Flow implements FlowInstance {
|
|
|
27
44
|
*/
|
|
28
45
|
protected createFlowUrl(): string;
|
|
29
46
|
private setupEventListeners;
|
|
47
|
+
private handleMessage;
|
|
30
48
|
/**
|
|
31
49
|
* Emit events to registered listeners
|
|
32
50
|
*/
|
package/dist/flows/Flow.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getBaseUrl } from './utils.js';
|
|
2
|
+
import { logger, redactUrl, describeError, describeConfig, LOAD_WATCHDOG_MS, WATCHDOG_MAX_REPORTS, } from './logger.js';
|
|
2
3
|
/**
|
|
3
4
|
* Flow class implementation for handling iframe flows and events
|
|
4
5
|
*/
|
|
@@ -14,6 +15,11 @@ export class Flow {
|
|
|
14
15
|
this.eventListeners = new Map();
|
|
15
16
|
this.keepAliveInterval = null;
|
|
16
17
|
this.messageHandler = null;
|
|
18
|
+
this.loadWatchdog = null;
|
|
19
|
+
this.watchdogReports = 0;
|
|
20
|
+
this.iframeLoaded = false;
|
|
21
|
+
this.readyForInteraction = false;
|
|
22
|
+
this.injectedAt = null;
|
|
17
23
|
this.setupEventListeners();
|
|
18
24
|
}
|
|
19
25
|
/**
|
|
@@ -22,15 +28,95 @@ export class Flow {
|
|
|
22
28
|
async initialize() {
|
|
23
29
|
this.container = document.getElementById(this.containerId);
|
|
24
30
|
if (!this.container) {
|
|
31
|
+
logger.error('Flow container not found in the DOM', {
|
|
32
|
+
containerId: this.containerId,
|
|
33
|
+
partnerName: this.partnerName,
|
|
34
|
+
});
|
|
25
35
|
throw new Error(`Container with ID "${this.containerId}" not found`);
|
|
26
36
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
let safeUrl = '<url not yet built>';
|
|
38
|
+
try {
|
|
39
|
+
const url = this.createFlowUrl();
|
|
40
|
+
safeUrl = redactUrl(url);
|
|
41
|
+
this.iframe = document.createElement('iframe');
|
|
42
|
+
this.iframe.src = url;
|
|
43
|
+
this.iframe.style.width = '100%';
|
|
44
|
+
this.iframe.style.height = '1000px';
|
|
45
|
+
this.iframe.style.border = 'none';
|
|
46
|
+
this.iframe.style.display = 'block';
|
|
47
|
+
this.iframe.addEventListener('load', () => {
|
|
48
|
+
this.iframeLoaded = true;
|
|
49
|
+
logger.debug('Flow iframe fired `load`', {
|
|
50
|
+
url: safeUrl,
|
|
51
|
+
loadMs: this.sinceInject(),
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
// Fires for network-level failures. A document that loads but then stalls
|
|
55
|
+
// internally will NOT fire this — that case is covered by the watchdog.
|
|
56
|
+
this.iframe.addEventListener('error', () => {
|
|
57
|
+
this.clearLoadWatchdog();
|
|
58
|
+
logger.error('Flow iframe failed to load', { url: safeUrl });
|
|
59
|
+
});
|
|
60
|
+
this.startLoadWatchdog(safeUrl);
|
|
61
|
+
logger.debug('Injecting flow iframe', {
|
|
62
|
+
url: safeUrl,
|
|
63
|
+
containerId: this.containerId,
|
|
64
|
+
});
|
|
65
|
+
this.injectedAt = Date.now();
|
|
66
|
+
this.container.appendChild(this.iframe);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
this.clearLoadWatchdog();
|
|
70
|
+
logger.error('Failed to inject flow iframe', {
|
|
71
|
+
url: safeUrl,
|
|
72
|
+
...describeError(error),
|
|
73
|
+
});
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Report a flow that has neither loaded nor become interactive within the budget.
|
|
79
|
+
*
|
|
80
|
+
* Diagnostic only: this logs and does nothing else. It separates "the iframe
|
|
81
|
+
* never loaded" from "it loaded but never reported READY_FOR_INTERACTION",
|
|
82
|
+
* which is the difference between a transport problem and the embedded app
|
|
83
|
+
* stalling after its document arrived.
|
|
84
|
+
*/
|
|
85
|
+
startLoadWatchdog(safeUrl) {
|
|
86
|
+
this.loadWatchdog = window.setInterval(() => {
|
|
87
|
+
if (this.iframeLoaded && this.readyForInteraction) {
|
|
88
|
+
this.clearLoadWatchdog();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.watchdogReports += 1;
|
|
92
|
+
const context = {
|
|
93
|
+
url: safeUrl,
|
|
94
|
+
waitedMs: this.watchdogReports * LOAD_WATCHDOG_MS,
|
|
95
|
+
report: `${this.watchdogReports}/${WATCHDOG_MAX_REPORTS}`,
|
|
96
|
+
containerId: this.containerId,
|
|
97
|
+
environment: this.environment,
|
|
98
|
+
};
|
|
99
|
+
if (!this.iframeLoaded) {
|
|
100
|
+
logger.error('Flow iframe has not fired `load` — the flow document appears to be stuck', context);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
logger.error('Flow iframe loaded but never sent READY_FOR_INTERACTION — the embedded app is stuck after its document loaded', context);
|
|
104
|
+
}
|
|
105
|
+
if (this.watchdogReports >= WATCHDOG_MAX_REPORTS) {
|
|
106
|
+
logger.error('Giving up on stuck-flow reporting — the flow never became interactive. If this is the last SDK line in the console, the page stopped executing here', context);
|
|
107
|
+
this.clearLoadWatchdog();
|
|
108
|
+
}
|
|
109
|
+
}, LOAD_WATCHDOG_MS);
|
|
110
|
+
}
|
|
111
|
+
/** Milliseconds since the iframe was injected, for load/ready timings. */
|
|
112
|
+
sinceInject() {
|
|
113
|
+
return this.injectedAt === null ? null : Date.now() - this.injectedAt;
|
|
114
|
+
}
|
|
115
|
+
clearLoadWatchdog() {
|
|
116
|
+
if (this.loadWatchdog !== null) {
|
|
117
|
+
clearInterval(this.loadWatchdog);
|
|
118
|
+
this.loadWatchdog = null;
|
|
119
|
+
}
|
|
34
120
|
}
|
|
35
121
|
/**
|
|
36
122
|
* Construct the specific flow URL - can be overridden by subclasses
|
|
@@ -42,11 +128,6 @@ export class Flow {
|
|
|
42
128
|
* Create flow URL using partner name and environment
|
|
43
129
|
*/
|
|
44
130
|
createFlowUrl() {
|
|
45
|
-
console.log('🔧 Creating flow URL...');
|
|
46
|
-
console.log('📝 Config:', this.config);
|
|
47
|
-
console.log('🏢 Partner:', this.partnerName);
|
|
48
|
-
console.log('🌍 Environment:', this.environment);
|
|
49
|
-
console.log('🔝 Branch Override:', this.branchOverride);
|
|
50
131
|
const baseUrl = getBaseUrl(this.environment);
|
|
51
132
|
let finalUrl = this.constructFlowUrl(baseUrl);
|
|
52
133
|
// Add cdn_branch_override parameter for non-production environments
|
|
@@ -54,70 +135,125 @@ export class Flow {
|
|
|
54
135
|
const separator = finalUrl.includes('?') ? '&' : '?';
|
|
55
136
|
finalUrl += `${separator}cdn_branch_override=${this.branchOverride}`;
|
|
56
137
|
}
|
|
57
|
-
|
|
138
|
+
logger.debug('Created flow URL', {
|
|
139
|
+
url: redactUrl(finalUrl),
|
|
140
|
+
partnerName: this.partnerName,
|
|
141
|
+
environment: this.environment,
|
|
142
|
+
branchOverride: this.branchOverride,
|
|
143
|
+
config: describeConfig(this.config),
|
|
144
|
+
});
|
|
58
145
|
return finalUrl;
|
|
59
146
|
}
|
|
60
147
|
setupEventListeners() {
|
|
61
148
|
// Add post message handlers for internal events - setHeight, scroll etc
|
|
62
149
|
// Also need to implement callbacks for flow completed exit etc in the platform-app
|
|
63
150
|
this.messageHandler = (event) => {
|
|
64
|
-
|
|
65
|
-
|
|
151
|
+
try {
|
|
152
|
+
this.handleMessage(event);
|
|
66
153
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
case 'FLOW_COMPLETED':
|
|
75
|
-
this.emit('completed', data);
|
|
76
|
-
break;
|
|
77
|
-
case 'FLOW_EXIT':
|
|
78
|
-
this.emit('exit');
|
|
79
|
-
break;
|
|
80
|
-
case 'NAVIGATED_TO_TARGET':
|
|
81
|
-
this.emit('navigated', data);
|
|
82
|
-
break;
|
|
83
|
-
case 'ONBOARDING_FORM_COMPLETED':
|
|
84
|
-
this.emit('onboardingCompleted');
|
|
85
|
-
break;
|
|
86
|
-
case 'ONBOARDING_REQUIRED':
|
|
87
|
-
this.emit('onboardingRequired');
|
|
88
|
-
break;
|
|
89
|
-
case 'READY_FOR_INTERACTION':
|
|
90
|
-
this.emit('loaded');
|
|
91
|
-
break;
|
|
92
|
-
case 'PAYMENT_SCHEDULED':
|
|
93
|
-
this.emit('completed', { flowName: 'payment', ...data });
|
|
94
|
-
break;
|
|
95
|
-
case 'MELIO_ERROR':
|
|
96
|
-
if (data.code === 'failed_to_sync_bills') {
|
|
97
|
-
this.emit('error', { errorCode: 'billsSyncFailed' });
|
|
98
|
-
}
|
|
99
|
-
break;
|
|
100
|
-
case 'HEIGHT_CHANGE':
|
|
101
|
-
if (this.iframe) {
|
|
102
|
-
this.iframe.style.height = `${data.height}px`;
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
154
|
+
catch (error) {
|
|
155
|
+
// A throw here would escape into the host page as an uncaught listener
|
|
156
|
+
// error with no indication it came from the SDK.
|
|
157
|
+
logger.error('Failed to handle message from flow iframe', {
|
|
158
|
+
origin: event.origin,
|
|
159
|
+
...describeError(error),
|
|
160
|
+
});
|
|
105
161
|
}
|
|
106
162
|
};
|
|
107
163
|
window.addEventListener('message', this.messageHandler);
|
|
108
164
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
165
|
+
handleMessage(event) {
|
|
166
|
+
if (!/melio\.com|melioservices\.com/.test(event.origin)) {
|
|
167
|
+
// Only surface near-misses. Host pages receive plenty of unrelated
|
|
168
|
+
// postMessage traffic and logging all of it would bury the signal.
|
|
169
|
+
if (/melio/i.test(event.origin)) {
|
|
170
|
+
logger.warn('Ignored message from an unrecognised Melio origin — check the origin allowlist', { origin: event.origin });
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
// Destructuring a null or primitive payload throws, and this handler runs
|
|
175
|
+
// for every message that clears the origin check.
|
|
176
|
+
if (event.data === null || typeof event.data !== 'object') {
|
|
177
|
+
logger.warn('Ignored non-object message from a Melio origin', {
|
|
178
|
+
origin: event.origin,
|
|
179
|
+
dataType: event.data === null ? 'null' : typeof event.data,
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const { type, ...data } = event.data;
|
|
184
|
+
logger.debug('Received message from flow iframe', {
|
|
185
|
+
type,
|
|
186
|
+
data,
|
|
187
|
+
origin: event.origin,
|
|
188
|
+
});
|
|
189
|
+
switch (type) {
|
|
190
|
+
case 'FLOW_COMPLETED':
|
|
191
|
+
this.emit('completed', data);
|
|
192
|
+
break;
|
|
193
|
+
case 'FLOW_EXIT':
|
|
194
|
+
this.emit('exit');
|
|
195
|
+
break;
|
|
196
|
+
case 'NAVIGATED_TO_TARGET':
|
|
197
|
+
this.emit('navigated', data);
|
|
198
|
+
break;
|
|
199
|
+
case 'ONBOARDING_FORM_COMPLETED':
|
|
200
|
+
this.emit('onboardingCompleted');
|
|
201
|
+
break;
|
|
202
|
+
case 'ONBOARDING_REQUIRED':
|
|
203
|
+
this.emit('onboardingRequired');
|
|
204
|
+
break;
|
|
205
|
+
case 'READY_FOR_INTERACTION':
|
|
206
|
+
this.readyForInteraction = true;
|
|
207
|
+
this.clearLoadWatchdog();
|
|
208
|
+
logger.debug('Flow reported READY_FOR_INTERACTION', {
|
|
209
|
+
readyMs: this.sinceInject(),
|
|
210
|
+
});
|
|
211
|
+
this.emit('loaded');
|
|
212
|
+
break;
|
|
213
|
+
case 'PAYMENT_SCHEDULED':
|
|
214
|
+
this.emit('completed', { flowName: 'payment', ...data });
|
|
215
|
+
break;
|
|
216
|
+
case 'MELIO_ERROR':
|
|
217
|
+
logger.error('Embedded app reported MELIO_ERROR', { ...data });
|
|
218
|
+
if (data.code === 'failed_to_sync_bills') {
|
|
219
|
+
this.emit('error', { errorCode: 'billsSyncFailed' });
|
|
115
220
|
}
|
|
116
|
-
|
|
117
|
-
|
|
221
|
+
else {
|
|
222
|
+
// Only failed_to_sync_bills maps to a public ErrorData code, so any
|
|
223
|
+
// other code is not forwarded to the host at all. Log it loudly
|
|
224
|
+
// rather than dropping it silently.
|
|
225
|
+
logger.error('MELIO_ERROR code has no mapping and was not forwarded to the host', { code: data.code });
|
|
118
226
|
}
|
|
119
|
-
|
|
227
|
+
break;
|
|
228
|
+
case 'HEIGHT_CHANGE':
|
|
229
|
+
if (this.iframe) {
|
|
230
|
+
this.iframe.style.height = `${data.height}px`;
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
logger.warn('Received HEIGHT_CHANGE but the flow iframe no longer exists', { height: data.height });
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
default:
|
|
237
|
+
logger.debug('Unhandled message type from flow iframe', { type });
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
emit(event, data) {
|
|
242
|
+
const listeners = this.eventListeners.get(event);
|
|
243
|
+
if (!listeners || listeners.size === 0) {
|
|
244
|
+
if (event === 'error' || event === 'authenticationFailed') {
|
|
245
|
+
logger.warn(`Emitted '${event}' but no listener is registered — the host will not learn about it`, { data });
|
|
246
|
+
}
|
|
247
|
+
return;
|
|
120
248
|
}
|
|
249
|
+
listeners.forEach((callback) => {
|
|
250
|
+
try {
|
|
251
|
+
callback(data);
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
logger.error(`Listener for '${event}' threw`, describeError(error));
|
|
255
|
+
}
|
|
256
|
+
});
|
|
121
257
|
}
|
|
122
258
|
on(event, callback) {
|
|
123
259
|
if (!this.eventListeners.has(event)) {
|
|
@@ -139,12 +275,25 @@ export class Flow {
|
|
|
139
275
|
* Close the flow and clean up resources
|
|
140
276
|
*/
|
|
141
277
|
close() {
|
|
278
|
+
logger.debug('Closing flow', {
|
|
279
|
+
containerId: this.containerId,
|
|
280
|
+
iframeLoaded: this.iframeLoaded,
|
|
281
|
+
readyForInteraction: this.readyForInteraction,
|
|
282
|
+
});
|
|
283
|
+
this.clearLoadWatchdog();
|
|
142
284
|
if (this.keepAliveInterval) {
|
|
143
285
|
clearInterval(this.keepAliveInterval);
|
|
144
286
|
this.keepAliveInterval = null;
|
|
145
287
|
}
|
|
146
288
|
if (this.iframe && this.container) {
|
|
147
|
-
|
|
289
|
+
try {
|
|
290
|
+
this.container.removeChild(this.iframe);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
// Throwing here would abandon the rest of teardown, leaving the message
|
|
294
|
+
// listener attached and the keep-alive running.
|
|
295
|
+
logger.error('Failed to remove the flow iframe during close — continuing teardown', describeError(error));
|
|
296
|
+
}
|
|
148
297
|
this.iframe = null;
|
|
149
298
|
}
|
|
150
299
|
if (this.messageHandler) {
|
package/dist/flows/InitFlow.d.ts
CHANGED
|
@@ -13,11 +13,30 @@ export declare class InitFlow implements InitFlowInstance {
|
|
|
13
13
|
private partnerName;
|
|
14
14
|
private environment;
|
|
15
15
|
private branchOverride?;
|
|
16
|
+
private loadWatchdog;
|
|
17
|
+
private watchdogReports;
|
|
18
|
+
private iframeLoaded;
|
|
19
|
+
private authResolved;
|
|
20
|
+
private keepAliveTargetLost;
|
|
21
|
+
private injectedAt;
|
|
22
|
+
private keepAlivePings;
|
|
16
23
|
constructor(config: InitConfig, partnerName: string, environment: Environment, branchOverride?: string);
|
|
17
24
|
/**
|
|
18
25
|
* Initialize the init flow by creating and injecting a hidden iframe
|
|
19
26
|
*/
|
|
20
27
|
initialize(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Report a flow that has neither loaded nor authenticated within the budget.
|
|
30
|
+
*
|
|
31
|
+
* Diagnostic only: this logs and does nothing else. It deliberately separates
|
|
32
|
+
* "the auth document never loaded" from "it loaded but never authenticated",
|
|
33
|
+
* because those point at different layers and the distinction is invisible
|
|
34
|
+
* from the host page otherwise.
|
|
35
|
+
*/
|
|
36
|
+
private startLoadWatchdog;
|
|
37
|
+
/** Milliseconds since the iframe was injected, for load/auth timings. */
|
|
38
|
+
private sinceInject;
|
|
39
|
+
private clearLoadWatchdog;
|
|
21
40
|
/**
|
|
22
41
|
* Construct the specific flow URL for initialization
|
|
23
42
|
*/
|
|
@@ -30,6 +49,7 @@ export declare class InitFlow implements InitFlowInstance {
|
|
|
30
49
|
* Setup event listeners to only handle authentication events
|
|
31
50
|
*/
|
|
32
51
|
private setupEventListeners;
|
|
52
|
+
private handleMessage;
|
|
33
53
|
/**
|
|
34
54
|
* Emit events to registered listeners
|
|
35
55
|
*/
|
package/dist/flows/InitFlow.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isEmptyString, getBaseUrl } from './utils.js';
|
|
2
|
+
import { logger, redactUrl, describeError, describeConfig, LOAD_WATCHDOG_MS, WATCHDOG_MAX_REPORTS, KEEP_ALIVE_INTERVAL_MS, } from './logger.js';
|
|
2
3
|
/**
|
|
3
4
|
* Standalone InitFlow class for handling initialization with callbacks
|
|
4
5
|
*/
|
|
@@ -9,7 +10,18 @@ export class InitFlow {
|
|
|
9
10
|
this.eventListeners = new Map();
|
|
10
11
|
this.keepAliveInterval = null;
|
|
11
12
|
this.messageHandler = null;
|
|
13
|
+
this.loadWatchdog = null;
|
|
14
|
+
this.watchdogReports = 0;
|
|
15
|
+
this.iframeLoaded = false;
|
|
16
|
+
this.authResolved = false;
|
|
17
|
+
this.keepAliveTargetLost = false;
|
|
18
|
+
this.injectedAt = null;
|
|
19
|
+
this.keepAlivePings = 0;
|
|
12
20
|
if (isEmptyString(config.authCode)) {
|
|
21
|
+
logger.error('Cannot create init flow: authorization code is missing', {
|
|
22
|
+
partnerName,
|
|
23
|
+
environment,
|
|
24
|
+
});
|
|
13
25
|
throw new Error('Authorization code is required for init flow');
|
|
14
26
|
}
|
|
15
27
|
this.authorizationCode = config.authCode;
|
|
@@ -23,15 +35,101 @@ export class InitFlow {
|
|
|
23
35
|
* Initialize the init flow by creating and injecting a hidden iframe
|
|
24
36
|
*/
|
|
25
37
|
async initialize() {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
let safeUrl = '<url not yet built>';
|
|
39
|
+
try {
|
|
40
|
+
const url = this.createFlowUrl();
|
|
41
|
+
safeUrl = redactUrl(url);
|
|
42
|
+
// The SDK is preloaded, so it can run before the host document has a
|
|
43
|
+
// body. Without this the failure surfaces as an opaque TypeError from
|
|
44
|
+
// appendChild.
|
|
45
|
+
if (!document.body) {
|
|
46
|
+
logger.error('Cannot inject auth iframe: document.body does not exist yet — the SDK was initialized before the host document was ready', { url: safeUrl, readyState: document.readyState });
|
|
47
|
+
throw new Error('document.body is not available for iframe injection');
|
|
48
|
+
}
|
|
49
|
+
this.container = document.body;
|
|
50
|
+
this.iframe = document.createElement('iframe');
|
|
51
|
+
this.iframe.src = url;
|
|
52
|
+
this.iframe.style.width = '1px';
|
|
53
|
+
this.iframe.style.height = '1px';
|
|
54
|
+
this.iframe.style.position = 'absolute';
|
|
55
|
+
this.iframe.style.left = '-9999px';
|
|
56
|
+
this.iframe.style.top = '-9999px';
|
|
57
|
+
this.iframe.addEventListener('load', () => {
|
|
58
|
+
this.iframeLoaded = true;
|
|
59
|
+
logger.debug('Auth iframe fired `load`', {
|
|
60
|
+
url: safeUrl,
|
|
61
|
+
loadMs: this.sinceInject(),
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
// Fires for network-level failures. A document that loads but then stalls
|
|
65
|
+
// internally will NOT fire this — that case is covered by the watchdog.
|
|
66
|
+
this.iframe.addEventListener('error', () => {
|
|
67
|
+
this.clearLoadWatchdog();
|
|
68
|
+
logger.error('Auth iframe failed to load', { url: safeUrl });
|
|
69
|
+
});
|
|
70
|
+
this.startLoadWatchdog(safeUrl);
|
|
71
|
+
logger.debug('Injecting hidden auth iframe', {
|
|
72
|
+
url: safeUrl,
|
|
73
|
+
partnerName: this.partnerName,
|
|
74
|
+
environment: this.environment,
|
|
75
|
+
config: describeConfig(this.config),
|
|
76
|
+
});
|
|
77
|
+
this.injectedAt = Date.now();
|
|
78
|
+
this.container.appendChild(this.iframe);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
this.clearLoadWatchdog();
|
|
82
|
+
logger.error('Failed to inject auth iframe', {
|
|
83
|
+
url: safeUrl,
|
|
84
|
+
...describeError(error),
|
|
85
|
+
});
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Report a flow that has neither loaded nor authenticated within the budget.
|
|
91
|
+
*
|
|
92
|
+
* Diagnostic only: this logs and does nothing else. It deliberately separates
|
|
93
|
+
* "the auth document never loaded" from "it loaded but never authenticated",
|
|
94
|
+
* because those point at different layers and the distinction is invisible
|
|
95
|
+
* from the host page otherwise.
|
|
96
|
+
*/
|
|
97
|
+
startLoadWatchdog(safeUrl) {
|
|
98
|
+
this.loadWatchdog = window.setInterval(() => {
|
|
99
|
+
if (this.iframeLoaded && this.authResolved) {
|
|
100
|
+
this.clearLoadWatchdog();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.watchdogReports += 1;
|
|
104
|
+
const waitedMs = this.watchdogReports * LOAD_WATCHDOG_MS;
|
|
105
|
+
const context = {
|
|
106
|
+
url: safeUrl,
|
|
107
|
+
waitedMs,
|
|
108
|
+
report: `${this.watchdogReports}/${WATCHDOG_MAX_REPORTS}`,
|
|
109
|
+
partnerName: this.partnerName,
|
|
110
|
+
environment: this.environment,
|
|
111
|
+
};
|
|
112
|
+
if (!this.iframeLoaded) {
|
|
113
|
+
logger.error('Auth iframe has not fired `load` — the auth document appears to be stuck', context);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
logger.error('Auth iframe loaded but never reported AUTHENTICATION_SUCCESS or AUTHENTICATION_ERROR — auth is stuck inside the iframe', context);
|
|
117
|
+
}
|
|
118
|
+
if (this.watchdogReports >= WATCHDOG_MAX_REPORTS) {
|
|
119
|
+
logger.error('Giving up on stuck-flow reporting — the auth flow never resolved. If this is the last SDK line in the console, the page stopped executing here', context);
|
|
120
|
+
this.clearLoadWatchdog();
|
|
121
|
+
}
|
|
122
|
+
}, LOAD_WATCHDOG_MS);
|
|
123
|
+
}
|
|
124
|
+
/** Milliseconds since the iframe was injected, for load/auth timings. */
|
|
125
|
+
sinceInject() {
|
|
126
|
+
return this.injectedAt === null ? null : Date.now() - this.injectedAt;
|
|
127
|
+
}
|
|
128
|
+
clearLoadWatchdog() {
|
|
129
|
+
if (this.loadWatchdog !== null) {
|
|
130
|
+
clearInterval(this.loadWatchdog);
|
|
131
|
+
this.loadWatchdog = null;
|
|
132
|
+
}
|
|
35
133
|
}
|
|
36
134
|
/**
|
|
37
135
|
* Construct the specific flow URL for initialization
|
|
@@ -61,42 +159,80 @@ export class InitFlow {
|
|
|
61
159
|
*/
|
|
62
160
|
setupEventListeners() {
|
|
63
161
|
this.messageHandler = (event) => {
|
|
64
|
-
|
|
65
|
-
|
|
162
|
+
try {
|
|
163
|
+
this.handleMessage(event);
|
|
66
164
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
case 'AUTHENTICATION_SUCCESS':
|
|
75
|
-
this.emit('authenticationSucceeded');
|
|
76
|
-
break;
|
|
77
|
-
case 'AUTHENTICATION_ERROR':
|
|
78
|
-
this.emit('authenticationFailed');
|
|
79
|
-
break;
|
|
165
|
+
catch (error) {
|
|
166
|
+
// A throw here would escape into the host page as an uncaught listener
|
|
167
|
+
// error with no indication it came from the SDK.
|
|
168
|
+
logger.error('Failed to handle message from auth iframe', {
|
|
169
|
+
origin: event.origin,
|
|
170
|
+
...describeError(error),
|
|
171
|
+
});
|
|
80
172
|
}
|
|
81
173
|
};
|
|
82
174
|
window.addEventListener('message', this.messageHandler);
|
|
83
175
|
}
|
|
176
|
+
handleMessage(event) {
|
|
177
|
+
if (!/melio\.com|melioservices\.com/.test(event.origin)) {
|
|
178
|
+
// Only surface near-misses. Host pages receive plenty of unrelated
|
|
179
|
+
// postMessage traffic and logging all of it would bury the signal.
|
|
180
|
+
if (/melio/i.test(event.origin)) {
|
|
181
|
+
logger.warn('Ignored message from an unrecognised Melio origin — check the origin allowlist', { origin: event.origin });
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// Destructuring a null or primitive payload throws, and this handler runs
|
|
186
|
+
// for every message that clears the origin check.
|
|
187
|
+
if (event.data === null || typeof event.data !== 'object') {
|
|
188
|
+
logger.warn('Ignored non-object message from a Melio origin', {
|
|
189
|
+
origin: event.origin,
|
|
190
|
+
dataType: event.data === null ? 'null' : typeof event.data,
|
|
191
|
+
});
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const { type, ...data } = event.data;
|
|
195
|
+
logger.debug('Received message from auth iframe', {
|
|
196
|
+
type,
|
|
197
|
+
data,
|
|
198
|
+
origin: event.origin,
|
|
199
|
+
});
|
|
200
|
+
switch (type) {
|
|
201
|
+
case 'AUTHENTICATION_SUCCESS':
|
|
202
|
+
this.authResolved = true;
|
|
203
|
+
this.clearLoadWatchdog();
|
|
204
|
+
logger.debug('Authentication succeeded');
|
|
205
|
+
this.emit('authenticationSucceeded');
|
|
206
|
+
break;
|
|
207
|
+
case 'AUTHENTICATION_ERROR':
|
|
208
|
+
this.authResolved = true;
|
|
209
|
+
this.clearLoadWatchdog();
|
|
210
|
+
logger.error('Authentication failed', { ...data });
|
|
211
|
+
this.emit('authenticationFailed', data);
|
|
212
|
+
break;
|
|
213
|
+
default:
|
|
214
|
+
logger.debug('Unhandled message type from auth iframe', { type });
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
84
218
|
/**
|
|
85
219
|
* Emit events to registered listeners
|
|
86
220
|
*/
|
|
87
221
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
88
222
|
emit(event, data) {
|
|
89
223
|
const listeners = this.eventListeners.get(event);
|
|
90
|
-
if (listeners) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
callback(data);
|
|
94
|
-
}
|
|
95
|
-
catch (error) {
|
|
96
|
-
console.error(`Error in ${event} event listener:`, error);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
224
|
+
if (!listeners || listeners.size === 0) {
|
|
225
|
+
logger.warn(`Emitted '${event}' but no listener is registered — the host will not learn about it`, { data });
|
|
226
|
+
return;
|
|
99
227
|
}
|
|
228
|
+
listeners.forEach((callback) => {
|
|
229
|
+
try {
|
|
230
|
+
callback(data);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
logger.error(`Listener for '${event}' threw`, describeError(error));
|
|
234
|
+
}
|
|
235
|
+
});
|
|
100
236
|
}
|
|
101
237
|
/**
|
|
102
238
|
* Register an event listener
|
|
@@ -121,12 +257,24 @@ export class InitFlow {
|
|
|
121
257
|
* Close the flow and clean up resources
|
|
122
258
|
*/
|
|
123
259
|
close() {
|
|
260
|
+
logger.debug('Closing init flow', {
|
|
261
|
+
iframeLoaded: this.iframeLoaded,
|
|
262
|
+
authResolved: this.authResolved,
|
|
263
|
+
});
|
|
264
|
+
this.clearLoadWatchdog();
|
|
124
265
|
if (this.keepAliveInterval) {
|
|
125
266
|
clearInterval(this.keepAliveInterval);
|
|
126
267
|
this.keepAliveInterval = null;
|
|
127
268
|
}
|
|
128
269
|
if (this.iframe && this.container) {
|
|
129
|
-
|
|
270
|
+
try {
|
|
271
|
+
this.container.removeChild(this.iframe);
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
// Throwing here would abandon the rest of teardown, leaving the message
|
|
275
|
+
// listener attached and the keep-alive running.
|
|
276
|
+
logger.error('Failed to remove the auth iframe during close — continuing teardown', describeError(error));
|
|
277
|
+
}
|
|
130
278
|
this.iframe = null;
|
|
131
279
|
}
|
|
132
280
|
if (this.messageHandler) {
|
|
@@ -136,10 +284,39 @@ export class InitFlow {
|
|
|
136
284
|
this.eventListeners.clear();
|
|
137
285
|
}
|
|
138
286
|
setupKeepAlive() {
|
|
287
|
+
if (this.keepAliveInterval !== null) {
|
|
288
|
+
logger.warn('setupKeepAlive called while a keep-alive is already running — clearing the previous interval to avoid a leak');
|
|
289
|
+
clearInterval(this.keepAliveInterval);
|
|
290
|
+
this.keepAliveInterval = null;
|
|
291
|
+
}
|
|
139
292
|
this.keepAliveInterval = window.setInterval(() => {
|
|
140
|
-
if (this.iframe
|
|
293
|
+
if (!this.iframe || !this.iframe.contentWindow) {
|
|
294
|
+
// Logged once: the interval keeps running, so an unguarded log here
|
|
295
|
+
// would repeat every 30s for the life of the page.
|
|
296
|
+
if (!this.keepAliveTargetLost) {
|
|
297
|
+
this.keepAliveTargetLost = true;
|
|
298
|
+
logger.error('Keep-alive ping has no target — the auth iframe or its content window is gone, every subsequent ping is a no-op', {
|
|
299
|
+
hasIframe: Boolean(this.iframe),
|
|
300
|
+
iframeLoaded: this.iframeLoaded,
|
|
301
|
+
authResolved: this.authResolved,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (this.keepAliveTargetLost) {
|
|
307
|
+
this.keepAliveTargetLost = false;
|
|
308
|
+
logger.warn('Keep-alive target recovered');
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
141
311
|
this.iframe.contentWindow.postMessage({ type: 'USER_ACTIVE_PING' }, '*');
|
|
312
|
+
this.keepAlivePings += 1;
|
|
313
|
+
// Doubles as a liveness heartbeat: if these stop while the flow is
|
|
314
|
+
// still open, the page stopped executing rather than merely stalling.
|
|
315
|
+
logger.debug('Keep-alive ping sent', { ping: this.keepAlivePings });
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
logger.error('Keep-alive ping threw', describeError(error));
|
|
142
319
|
}
|
|
143
|
-
},
|
|
320
|
+
}, KEEP_ALIVE_INTERVAL_MS);
|
|
144
321
|
}
|
|
145
322
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight console logger for the SDK.
|
|
3
|
+
*
|
|
4
|
+
* Every line is prefixed with `[melio-sdk]` so partners can filter SDK output out
|
|
5
|
+
* of a busy host page, and carries `msSinceInit` — elapsed time since `init()` was
|
|
6
|
+
* called. The elapsed time is what makes a *hang* legible in a console capture: a
|
|
7
|
+
* hard failure shows up as an error either way, but a flow that simply never
|
|
8
|
+
* finishes is only visible as a gap on the clock.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Severity levels, lowest first. `debug` is opt-in via `init({ debug: true })`;
|
|
12
|
+
* `warn` and `error` always print, because a partner debugging a broken flow
|
|
13
|
+
* should not have to enable anything to see that something failed.
|
|
14
|
+
*/
|
|
15
|
+
export type LogLevel = 'debug' | 'warn' | 'error';
|
|
16
|
+
/** Enable or disable debug-level output. Called from `init({ debug })`. */
|
|
17
|
+
export declare const setDebugEnabled: (enabled: boolean) => void;
|
|
18
|
+
/** Whether debug-level output is currently enabled. */
|
|
19
|
+
export declare const isDebugEnabled: () => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* How long a flow may stay unloaded before the SDK reports it as stuck, and how
|
|
22
|
+
* often it keeps repeating that report afterwards.
|
|
23
|
+
*
|
|
24
|
+
* Diagnostic only — reaching it logs, it does not tear the flow down.
|
|
25
|
+
*
|
|
26
|
+
* The repeat matters for diagnosing a page that dies rather than recovers: a
|
|
27
|
+
* stuck flow keeps announcing itself every 15s, so the timestamp of the *last*
|
|
28
|
+
* report marks the moment the tab stopped executing. A one-shot log cannot
|
|
29
|
+
* distinguish "still hanging" from "the renderer was killed".
|
|
30
|
+
*/
|
|
31
|
+
export declare const LOAD_WATCHDOG_MS = 15000;
|
|
32
|
+
/** Stop repeating the stuck-flow report after this many rounds (~2 minutes). */
|
|
33
|
+
export declare const WATCHDOG_MAX_REPORTS = 8;
|
|
34
|
+
/** Interval between keep-alive pings to an initialized flow. */
|
|
35
|
+
export declare const KEEP_ALIVE_INTERVAL_MS = 30000;
|
|
36
|
+
/** Start the SDK clock. Called once from `init()`. */
|
|
37
|
+
export declare const markInitStart: () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Strip credential-bearing query params so a flow URL can be logged safely.
|
|
40
|
+
* On a URL we cannot parse we fall back to the path, never the raw string.
|
|
41
|
+
*/
|
|
42
|
+
export declare const redactUrl: (url: string) => string;
|
|
43
|
+
/** Flatten an unknown throwable into something that survives a console capture. */
|
|
44
|
+
export declare const describeError: (error: unknown) => Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Shallow-copy a flow config with PII redacted, so a config can be logged
|
|
47
|
+
* without printing customer details into the partner's console.
|
|
48
|
+
*/
|
|
49
|
+
export declare const describeConfig: (config: object | undefined) => Record<string, unknown>;
|
|
50
|
+
/**
|
|
51
|
+
* Describe the host environment once at init. The Safari 26.5 regression on
|
|
52
|
+
* Xero's Bills page was only ever reproducible on specific browser builds, and
|
|
53
|
+
* whether we are running inside a frame decides which failure modes are even
|
|
54
|
+
* possible — so both belong in any console capture we are asked to read.
|
|
55
|
+
*/
|
|
56
|
+
export declare const describeHost: () => Record<string, unknown>;
|
|
57
|
+
export declare const logger: {
|
|
58
|
+
debug(message: string, context?: Record<string, unknown>): void;
|
|
59
|
+
warn(message: string, context?: Record<string, unknown>): void;
|
|
60
|
+
error(message: string, context?: Record<string, unknown>): void;
|
|
61
|
+
};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight console logger for the SDK.
|
|
3
|
+
*
|
|
4
|
+
* Every line is prefixed with `[melio-sdk]` so partners can filter SDK output out
|
|
5
|
+
* of a busy host page, and carries `msSinceInit` — elapsed time since `init()` was
|
|
6
|
+
* called. The elapsed time is what makes a *hang* legible in a console capture: a
|
|
7
|
+
* hard failure shows up as an error either way, but a flow that simply never
|
|
8
|
+
* finishes is only visible as a gap on the clock.
|
|
9
|
+
*/
|
|
10
|
+
const PREFIX = '[melio-sdk]';
|
|
11
|
+
/** Query params that may carry a credential and must never reach the console. */
|
|
12
|
+
const REDACTED_PARAMS = ['token', 'authCode', 'code'];
|
|
13
|
+
/**
|
|
14
|
+
* Flow-config keys holding a credential or customer PII. Their presence is worth
|
|
15
|
+
* logging, their contents are not — this SDK runs inside a partner's page.
|
|
16
|
+
*
|
|
17
|
+
* `authCode` matters most: it is on both `InitConfig` and `BaseFlowConfig`, so
|
|
18
|
+
* logging a config verbatim prints a live auth token to the console.
|
|
19
|
+
*/
|
|
20
|
+
const REDACTED_CONFIG_KEYS = [
|
|
21
|
+
'authCode',
|
|
22
|
+
'token',
|
|
23
|
+
'code',
|
|
24
|
+
'userDetails',
|
|
25
|
+
'organizationDetails',
|
|
26
|
+
];
|
|
27
|
+
const LEVEL_WEIGHT = {
|
|
28
|
+
debug: 10,
|
|
29
|
+
warn: 20,
|
|
30
|
+
error: 30,
|
|
31
|
+
};
|
|
32
|
+
let minimumWeight = LEVEL_WEIGHT.warn;
|
|
33
|
+
/** Enable or disable debug-level output. Called from `init({ debug })`. */
|
|
34
|
+
export const setDebugEnabled = (enabled) => {
|
|
35
|
+
minimumWeight = enabled ? LEVEL_WEIGHT.debug : LEVEL_WEIGHT.warn;
|
|
36
|
+
};
|
|
37
|
+
/** Whether debug-level output is currently enabled. */
|
|
38
|
+
export const isDebugEnabled = () => minimumWeight <= LEVEL_WEIGHT.debug;
|
|
39
|
+
const shouldLog = (level) => LEVEL_WEIGHT[level] >= minimumWeight;
|
|
40
|
+
/**
|
|
41
|
+
* How long a flow may stay unloaded before the SDK reports it as stuck, and how
|
|
42
|
+
* often it keeps repeating that report afterwards.
|
|
43
|
+
*
|
|
44
|
+
* Diagnostic only — reaching it logs, it does not tear the flow down.
|
|
45
|
+
*
|
|
46
|
+
* The repeat matters for diagnosing a page that dies rather than recovers: a
|
|
47
|
+
* stuck flow keeps announcing itself every 15s, so the timestamp of the *last*
|
|
48
|
+
* report marks the moment the tab stopped executing. A one-shot log cannot
|
|
49
|
+
* distinguish "still hanging" from "the renderer was killed".
|
|
50
|
+
*/
|
|
51
|
+
export const LOAD_WATCHDOG_MS = 15000;
|
|
52
|
+
/** Stop repeating the stuck-flow report after this many rounds (~2 minutes). */
|
|
53
|
+
export const WATCHDOG_MAX_REPORTS = 8;
|
|
54
|
+
/** Interval between keep-alive pings to an initialized flow. */
|
|
55
|
+
export const KEEP_ALIVE_INTERVAL_MS = 30000;
|
|
56
|
+
let initTimestamp = null;
|
|
57
|
+
/** Start the SDK clock. Called once from `init()`. */
|
|
58
|
+
export const markInitStart = () => {
|
|
59
|
+
initTimestamp = Date.now();
|
|
60
|
+
};
|
|
61
|
+
const buildContext = (context) => ({
|
|
62
|
+
...(initTimestamp === null
|
|
63
|
+
? {}
|
|
64
|
+
: { msSinceInit: Date.now() - initTimestamp }),
|
|
65
|
+
...context,
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* Strip credential-bearing query params so a flow URL can be logged safely.
|
|
69
|
+
* On a URL we cannot parse we fall back to the path, never the raw string.
|
|
70
|
+
*/
|
|
71
|
+
export const redactUrl = (url) => {
|
|
72
|
+
try {
|
|
73
|
+
const parsed = new URL(url);
|
|
74
|
+
REDACTED_PARAMS.forEach((param) => {
|
|
75
|
+
if (parsed.searchParams.has(param)) {
|
|
76
|
+
parsed.searchParams.set(param, 'redacted');
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return parsed.toString();
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return `${url.split('?')[0]}?[unparsed-query-redacted]`;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
/** Flatten an unknown throwable into something that survives a console capture. */
|
|
86
|
+
export const describeError = (error) => {
|
|
87
|
+
if (error instanceof Error) {
|
|
88
|
+
return { name: error.name, message: error.message, stack: error.stack };
|
|
89
|
+
}
|
|
90
|
+
return { thrown: String(error) };
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Shallow-copy a flow config with PII redacted, so a config can be logged
|
|
94
|
+
* without printing customer details into the partner's console.
|
|
95
|
+
*/
|
|
96
|
+
export const describeConfig = (config) => {
|
|
97
|
+
if (!config) {
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
return Object.fromEntries(Object.entries(config).map(([key, value]) => [
|
|
101
|
+
key,
|
|
102
|
+
REDACTED_CONFIG_KEYS.includes(key) && value !== undefined
|
|
103
|
+
? '[redacted]'
|
|
104
|
+
: value,
|
|
105
|
+
]));
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Describe the host environment once at init. The Safari 26.5 regression on
|
|
109
|
+
* Xero's Bills page was only ever reproducible on specific browser builds, and
|
|
110
|
+
* whether we are running inside a frame decides which failure modes are even
|
|
111
|
+
* possible — so both belong in any console capture we are asked to read.
|
|
112
|
+
*/
|
|
113
|
+
export const describeHost = () => {
|
|
114
|
+
const host = {};
|
|
115
|
+
if (typeof navigator !== 'undefined') {
|
|
116
|
+
host.userAgent = navigator.userAgent;
|
|
117
|
+
}
|
|
118
|
+
if (typeof document !== 'undefined') {
|
|
119
|
+
host.readyState = document.readyState;
|
|
120
|
+
host.visibilityState = document.visibilityState;
|
|
121
|
+
}
|
|
122
|
+
if (typeof window !== 'undefined') {
|
|
123
|
+
try {
|
|
124
|
+
host.isFramed = window.top !== window.self;
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// Cross-origin parents throw on access, which itself proves we are framed.
|
|
128
|
+
host.isFramed = true;
|
|
129
|
+
}
|
|
130
|
+
host.origin = window.location?.origin;
|
|
131
|
+
}
|
|
132
|
+
return host;
|
|
133
|
+
};
|
|
134
|
+
export const logger = {
|
|
135
|
+
debug(message, context) {
|
|
136
|
+
if (!shouldLog('debug')) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
console.log(`${PREFIX} ${message}`, buildContext(context));
|
|
140
|
+
},
|
|
141
|
+
warn(message, context) {
|
|
142
|
+
if (!shouldLog('warn')) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
console.warn(`${PREFIX} ${message}`, buildContext(context));
|
|
146
|
+
},
|
|
147
|
+
error(message, context) {
|
|
148
|
+
if (!shouldLog('error')) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
console.error(`${PREFIX} ${message}`, buildContext(context));
|
|
152
|
+
},
|
|
153
|
+
};
|
package/dist/flows/utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { logger } from './logger.js';
|
|
1
2
|
/**
|
|
2
3
|
* Utility function to get the base URL for a given environment
|
|
3
4
|
*/
|
|
@@ -16,6 +17,7 @@ export function getBaseUrl(environment) {
|
|
|
16
17
|
case 'localhost':
|
|
17
18
|
return 'http://localhost:3005';
|
|
18
19
|
default:
|
|
20
|
+
logger.warn('Unknown environment — falling back to the default base URL, which may not serve this partner', { environment, fallback: 'https://partnerships.melioservices.com' });
|
|
19
21
|
return 'https://partnerships.melioservices.com';
|
|
20
22
|
}
|
|
21
23
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ export declare class MelioSDK implements MelioSDK {
|
|
|
14
14
|
* Initialize the SDK - triggers auth event without creating iframe
|
|
15
15
|
*/
|
|
16
16
|
init(authenticationCode: string, options: InitOptions): InitFlowInstance;
|
|
17
|
+
/**
|
|
18
|
+
* Shared launch path for every open* flow: log the attempt, report a
|
|
19
|
+
* constructor failure, and make sure a rejected initialize() surfaces rather
|
|
20
|
+
* than becoming an unhandled promise.
|
|
21
|
+
*/
|
|
22
|
+
private startFlow;
|
|
17
23
|
/**
|
|
18
24
|
* Launch the onboarding flow
|
|
19
25
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InitFlow, OnboardingFlow, PayFlow, JustPayFlow, SettingsFlow, PaymentsDashboardFlow, isEmptyString, } from './flows/index.js';
|
|
2
|
+
import { logger, markInitStart, describeError, describeConfig, describeHost, setDebugEnabled, } from './flows/logger.js';
|
|
2
3
|
/**
|
|
3
4
|
* Main SDK implementation - now partner agnostic
|
|
4
5
|
*/
|
|
@@ -16,79 +17,101 @@ export class MelioSDK {
|
|
|
16
17
|
*/
|
|
17
18
|
init(authenticationCode, options) {
|
|
18
19
|
if (this.initFlow) {
|
|
19
|
-
|
|
20
|
+
logger.warn('SDK already initialized. Returning existing init flow instance.');
|
|
20
21
|
return this.initFlow;
|
|
21
22
|
}
|
|
23
|
+
// Set before the first log so the init banner honours the flag.
|
|
24
|
+
setDebugEnabled(Boolean(options.debug));
|
|
25
|
+
markInitStart();
|
|
22
26
|
this.partnerName = options.partnerName;
|
|
23
27
|
this.environment = options.environment || 'production';
|
|
24
28
|
this.branchOverride = options.branchOverride;
|
|
25
29
|
this.authCode = authenticationCode;
|
|
26
|
-
|
|
30
|
+
logger.debug('Starting init flow', {
|
|
27
31
|
partnerName: this.partnerName,
|
|
28
32
|
environment: this.environment,
|
|
33
|
+
keepAlive: Boolean(options.keepAlive),
|
|
34
|
+
branchOverride: this.branchOverride,
|
|
35
|
+
...describeHost(),
|
|
36
|
+
});
|
|
37
|
+
let initFlow;
|
|
38
|
+
try {
|
|
39
|
+
initFlow = new InitFlow({ authCode: authenticationCode, containerId: '' }, this.partnerName, this.environment, this.branchOverride);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
logger.error('Failed to create the init flow', describeError(error));
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
// `initialize()` is intentionally not awaited — `init()` is synchronous by
|
|
46
|
+
// contract — but its rejection must not become an unhandled promise, which
|
|
47
|
+
// is invisible to partners debugging from the console.
|
|
48
|
+
initFlow.initialize().catch((error) => {
|
|
49
|
+
logger.error('Init flow failed to initialize', describeError(error));
|
|
29
50
|
});
|
|
30
|
-
const initFlow = new InitFlow({ authCode: authenticationCode, containerId: '' }, this.partnerName, this.environment, this.branchOverride);
|
|
31
|
-
initFlow.initialize();
|
|
32
|
-
console.log('InitFlow initialized successfully');
|
|
33
51
|
if (options.keepAlive)
|
|
34
52
|
initFlow.setupKeepAlive();
|
|
35
53
|
this.initFlow = initFlow;
|
|
36
54
|
return this.initFlow;
|
|
37
55
|
}
|
|
38
56
|
/**
|
|
39
|
-
*
|
|
57
|
+
* Shared launch path for every open* flow: log the attempt, report a
|
|
58
|
+
* constructor failure, and make sure a rejected initialize() surfaces rather
|
|
59
|
+
* than becoming an unhandled promise.
|
|
40
60
|
*/
|
|
41
|
-
|
|
61
|
+
startFlow(flowName, config, create) {
|
|
42
62
|
if (isEmptyString(this.authCode)) {
|
|
43
|
-
|
|
63
|
+
const message = `SDK not initialized. Please call init() before opening ${flowName}.`;
|
|
64
|
+
logger.error(message);
|
|
65
|
+
throw new Error(message);
|
|
66
|
+
}
|
|
67
|
+
logger.debug(`Opening ${flowName}`, {
|
|
68
|
+
partnerName: this.partnerName,
|
|
69
|
+
environment: this.environment,
|
|
70
|
+
config: describeConfig(config),
|
|
71
|
+
});
|
|
72
|
+
let flow;
|
|
73
|
+
try {
|
|
74
|
+
flow = create();
|
|
44
75
|
}
|
|
45
|
-
|
|
46
|
-
|
|
76
|
+
catch (error) {
|
|
77
|
+
logger.error(`Failed to create ${flowName}`, describeError(error));
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
// Not awaited by contract, but a rejection must not vanish.
|
|
81
|
+
flow.initialize().catch((error) => {
|
|
82
|
+
logger.error(`${flowName} failed to initialize`, describeError(error));
|
|
83
|
+
});
|
|
47
84
|
return flow;
|
|
48
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Launch the onboarding flow
|
|
88
|
+
*/
|
|
89
|
+
openOnboarding(config) {
|
|
90
|
+
return this.startFlow('onboarding flow', config, () => new OnboardingFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride));
|
|
91
|
+
}
|
|
49
92
|
/**
|
|
50
93
|
* Launch the pay flow
|
|
51
94
|
*/
|
|
52
95
|
openPayFlow(config) {
|
|
53
|
-
|
|
54
|
-
throw new Error('SDK not initialized. Please call init() before opening pay flow.');
|
|
55
|
-
}
|
|
56
|
-
const flow = new PayFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride);
|
|
57
|
-
flow.initialize();
|
|
58
|
-
return flow;
|
|
96
|
+
return this.startFlow('pay flow', config, () => new PayFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride));
|
|
59
97
|
}
|
|
60
98
|
/**
|
|
61
99
|
* Launch the just pay flow (vendor-based payment)
|
|
62
100
|
*/
|
|
63
101
|
openJustPayFlow(config) {
|
|
64
|
-
|
|
65
|
-
throw new Error('SDK not initialized. Please call init() before opening just pay flow.');
|
|
66
|
-
}
|
|
67
|
-
const flow = new JustPayFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride);
|
|
68
|
-
flow.initialize();
|
|
69
|
-
return flow;
|
|
102
|
+
return this.startFlow('just pay flow', config, () => new JustPayFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride));
|
|
70
103
|
}
|
|
71
104
|
/**
|
|
72
105
|
* Launch the settings flow
|
|
73
106
|
*/
|
|
74
107
|
openSettings(config) {
|
|
75
|
-
|
|
76
|
-
throw new Error('SDK not initialized. Please call init() before opening settings flow.');
|
|
77
|
-
}
|
|
78
|
-
const flow = new SettingsFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride);
|
|
79
|
-
flow.initialize();
|
|
80
|
-
return flow;
|
|
108
|
+
return this.startFlow('settings flow', config, () => new SettingsFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride));
|
|
81
109
|
}
|
|
82
110
|
/**
|
|
83
111
|
* Launch the payments dashboard flow
|
|
84
112
|
*/
|
|
85
113
|
openPaymentsDashboard(config) {
|
|
86
|
-
|
|
87
|
-
throw new Error('SDK not initialized. Please call init() before opening payments dashboard flow.');
|
|
88
|
-
}
|
|
89
|
-
const flow = new PaymentsDashboardFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride);
|
|
90
|
-
flow.initialize();
|
|
91
|
-
return flow;
|
|
114
|
+
return this.startFlow('payments dashboard flow', config, () => new PaymentsDashboardFlow(config.containerId, config, this.partnerName, this.environment, this.authCode, this.branchOverride));
|
|
92
115
|
}
|
|
93
116
|
}
|
|
94
117
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -14,6 +14,16 @@ export interface InitOptions {
|
|
|
14
14
|
environment?: Environment;
|
|
15
15
|
/** The branch to use for the melio platform. Defaults to 'main' */
|
|
16
16
|
branchOverride?: string;
|
|
17
|
+
/**
|
|
18
|
+
* If true, the SDK prints debug-level logs to the console on top of the
|
|
19
|
+
* warnings and errors it always prints. Defaults to false.
|
|
20
|
+
*
|
|
21
|
+
* Turn this on to trace a flow that loads but never becomes usable: debug
|
|
22
|
+
* output covers iframe lifecycle, load and auth timings, every postMessage
|
|
23
|
+
* exchanged with the embedded app, and a keep-alive heartbeat proving the
|
|
24
|
+
* page is still executing.
|
|
25
|
+
*/
|
|
26
|
+
debug?: boolean;
|
|
17
27
|
}
|
|
18
28
|
/**
|
|
19
29
|
* Base configuration for all flows
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@melio-eng/web-sdk",
|
|
3
|
-
"version": "1.1.0-pr.
|
|
3
|
+
"version": "1.1.0-pr.93.ae7fb3c",
|
|
4
4
|
"description": "Melio Web SDK - Embed core Melio workflows directly into partner UI with minimal effort",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|