Skip to content

Commit 0ab551e

Browse files
authored
Merge pull request #324 from ulixee/fixes
Fixes
2 parents fb624a5 + da27e1d commit 0ab551e

File tree

7 files changed

+42
-8
lines changed

7 files changed

+42
-8
lines changed

commons/ShutdownHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type ShutdownSignal = NodeJS.Signals | 'exit';
66
const { log } = logger(module);
77

88
export default class ShutdownHandler {
9-
public static exitOnSignal = true;
9+
public static exitOnSignal = false;
1010

1111
private static isRegistered = false;
1212
private static hasRunHandlers = false;

core/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import DefaultHumanEmulator from '@secret-agent/default-human-emulator';
1313
import extractPlugins from '@secret-agent/plugin-utils/lib/utils/extractPlugins';
1414
import requirePlugins from '@secret-agent/plugin-utils/lib/utils/requirePlugins';
1515
import { IPluginClass } from '@secret-agent/interfaces/IPlugin';
16+
import ShutdownHandler from '@secret-agent/commons/ShutdownHandler';
1617
import ConnectionToClient from './server/ConnectionToClient';
1718
import CoreServer from './server';
1819
import CoreProcess from './lib/CoreProcess';
@@ -118,7 +119,10 @@ export default class Core {
118119
sessionsDir: GlobalPool.sessionsDir,
119120
});
120121
// if started as a subprocess, send back the host
121-
if (process.send) process.send(host);
122+
if (process.send && process.connected) {
123+
ShutdownHandler.exitOnSignal = true;
124+
process.send(host);
125+
}
122126
}
123127

124128
public static async shutdown(force = false): Promise<void> {

core/lib/CoreProcess.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ export default class CoreProcess {
3737
public static kill(signal?: NodeJS.Signals) {
3838
const child = this.child;
3939
if (child) {
40+
child.unref();
4041
const closed = new Promise<void>(resolve => child.once('exit', () => setImmediate(resolve)));
41-
if (!child.killed) child.kill(signal);
42+
if (!child.killed) {
43+
child.kill(signal);
44+
}
4245
return closed;
4346
}
4447
}

core/lib/FrameNavigations.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import { IBoundLog } from '@secret-agent/interfaces/ILog';
1010
import Log from '@secret-agent/commons/Logger';
1111
import SessionState from './SessionState';
1212

13-
interface IFrameNavigationEvents {
13+
export interface IFrameNavigationEvents {
1414
'navigation-requested': INavigation;
1515
'status-change': {
16+
id: number;
1617
url: string;
1718
stateChanges: { [state: string]: Date };
1819
newStatus: NavigationState;
@@ -77,6 +78,7 @@ export default class FrameNavigations extends TypedEventEmitter<IFrameNavigation
7778
this.checkStoredNavigationReason(nextTop, url);
7879

7980
const currentTop = this.top;
81+
let shouldPublishLocationChange = false;
8082
// if in-page, set the state to match current top
8183
if (reason === 'inPage') {
8284
if (currentTop) {
@@ -93,12 +95,22 @@ export default class FrameNavigations extends TypedEventEmitter<IFrameNavigation
9395
nextTop.stateChanges.set(LoadStatus.ContentPaint, nextTop.initiatedTime);
9496
nextTop.resourceId.resolve(-1);
9597
}
98+
shouldPublishLocationChange = true;
9699
nextTop.finalUrl = url;
97100
}
98101
this.history.push(nextTop);
99102

100103
this.emit('navigation-requested', nextTop);
101104
this.captureNavigationUpdate(nextTop);
105+
if (shouldPublishLocationChange) {
106+
this.emit('status-change', {
107+
id: nextTop.id,
108+
newStatus: LoadStatus.ContentPaint,
109+
url,
110+
// @ts-ignore
111+
stateChanges: Object.fromEntries(nextTop.stateChanges),
112+
});
113+
}
102114
return nextTop;
103115
}
104116

@@ -295,6 +307,7 @@ export default class FrameNavigations extends TypedEventEmitter<IFrameNavigation
295307
navigation.stateChanges.set(newStatus, statusChangeDate ?? new Date());
296308

297309
this.emit('status-change', {
310+
id: navigation.id,
298311
url: navigation.finalUrl ?? navigation.requestedUrl,
299312
// @ts-ignore - Typescript refuses to recognize this function
300313
stateChanges: Object.fromEntries(navigation.stateChanges),

core/lib/FrameNavigationsObserver.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ export default class FrameNavigationsObserver {
4646
last = command;
4747
}
4848
// handle cases like waitForLocation two times in a row
49-
if (newCommand.name === 'waitForLocation' && last && last.name.startsWith('waitFor')) {
49+
if (
50+
newCommand.name === 'waitForLocation' &&
51+
last &&
52+
last.name.startsWith('waitFor') &&
53+
last.name !== 'waitForMillis'
54+
) {
5055
this.defaultWaitForLocationCommandId = newCommand.id;
5156
}
5257
}
@@ -222,7 +227,12 @@ export default class FrameNavigationsObserver {
222227
previousLoadedUrl &&
223228
previousLoadedUrl !== history.finalUrl
224229
) {
225-
isLocationChange = true;
230+
// Don't accept adding a slash as a page change
231+
const isInPageUrlAdjust =
232+
history.navigationReason === 'inPage' &&
233+
history.finalUrl.replace(previousLoadedUrl, '').length <= 1;
234+
235+
if (!isInPageUrlAdjust) isLocationChange = true;
226236
}
227237

228238
if (isLocationChange) {
@@ -236,7 +246,8 @@ export default class FrameNavigationsObserver {
236246
}
237247

238248
if (
239-
history.stateChanges.has(LoadStatus.HttpResponded) &&
249+
(history.stateChanges.has(LoadStatus.HttpResponded) ||
250+
history.stateChanges.has(LoadStatus.DomContentLoaded)) &&
240251
!history.stateChanges.has(LoadStatus.HttpRedirected)
241252
) {
242253
previousLoadedUrl = history.finalUrl;

core/lib/Tab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {
422422
}
423423

424424
public takeScreenshot(options: IScreenshotOptions = {}): Promise<Buffer> {
425-
options.rectangle.scale ??= 1;
425+
if (options.rectangle) options.rectangle.scale ??= 1;
426426
return this.puppetPage.screenshot(options.format, options.rectangle, options.jpegQuality);
427427
}
428428

puppet/lib/launchProcess.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Log from '@secret-agent/commons/Logger';
2222
import ILaunchedProcess from '@secret-agent/interfaces/ILaunchedProcess';
2323
import Resolvable from '@secret-agent/commons/Resolvable';
2424
import * as Fs from 'fs';
25+
import ShutdownHandler from '@secret-agent/commons/ShutdownHandler';
2526
import { WebSocketTransport } from './WebSocketTransport';
2627

2728
const { log } = Log(module);
@@ -106,6 +107,8 @@ export default async function launchProcess(
106107
runOnClose();
107108
if (dataDir) cleanDataDir(dataDir);
108109
});
110+
process.on('uncaughtExceptionMonitor', () => close());
111+
ShutdownHandler.register(close);
109112

110113
const wsEndpoint = await websocketEndpointResolvable.promise;
111114
transport = new WebSocketTransport(wsEndpoint);

0 commit comments

Comments
 (0)