@applitools/eyes-cypress 3.26.1 → 3.26.2
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 +11 -0
- package/README.md +62 -39
- package/dist/browser/spec-driver.js +10 -13
- package/package.json +8 -8
- package/src/browser/commands.js +3 -1
- package/src/browser/spec-driver.ts +9 -12
- package/src/plugin/server.js +31 -6
- package/dist/plugin/handler.js +0 -55
- package/src/plugin/handler.ts +0 -58
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,17 @@
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
|
|
12
|
+
## 3.26.2 - 2022/6/8
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
- add support for secure websocket
|
|
16
|
+
- Allowed `` values in custom properties
|
|
17
|
+
### Bug fixes
|
|
18
|
+
- Fix incorrect test results when dealing with a page that contains CORS iframes
|
|
19
|
+
- Fixed broken links to enums implementation in the README.md
|
|
20
|
+
- Fix calling `waitBeforeCapture` when failed to set required viewport size
|
|
21
|
+
|
|
11
22
|
## 3.26.1 - 2022/6/2
|
|
12
23
|
|
|
13
24
|
### Features
|
package/README.md
CHANGED
|
@@ -173,43 +173,66 @@ Applitools will take screenshots and perform the visual comparisons in the backg
|
|
|
173
173
|
<br/>
|
|
174
174
|
|
|
175
175
|
### Index
|
|
176
|
-
- [
|
|
177
|
-
- [
|
|
178
|
-
|
|
179
|
-
- [
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
- [
|
|
186
|
-
- [
|
|
187
|
-
|
|
188
|
-
- [
|
|
189
|
-
- [
|
|
190
|
-
- [
|
|
191
|
-
- [
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
- [
|
|
200
|
-
- [
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
- [
|
|
208
|
-
|
|
209
|
-
- [
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
- [
|
|
176
|
+
- [Eyes-Cypress](#eyes-cypress)
|
|
177
|
+
- [Installation](#installation)
|
|
178
|
+
- [Install npm package](#install-npm-package)
|
|
179
|
+
- [Configure plugin and commands](#configure-plugin-and-commands)
|
|
180
|
+
- [Automatic configuration](#automatic-configuration)
|
|
181
|
+
- [Manual configuration](#manual-configuration)
|
|
182
|
+
- [1. Configure Eyes-Cypress plugin](#1-configure-eyes-cypress-plugin)
|
|
183
|
+
- [2. Configure custom commands](#2-configure-custom-commands)
|
|
184
|
+
- [3. (Optional) TypeScript configuration](#3-optional-typescript-configuration)
|
|
185
|
+
- [Applitools API key](#applitools-api-key)
|
|
186
|
+
- [Eyes server URL (optional)](#eyes-server-url-optional)
|
|
187
|
+
- [Usage](#usage)
|
|
188
|
+
- [Example](#example)
|
|
189
|
+
- [Best practice for using the SDK](#best-practice-for-using-the-sdk)
|
|
190
|
+
- [Index](#index)
|
|
191
|
+
- [Commands](#commands)
|
|
192
|
+
- [Open](#open)
|
|
193
|
+
- [Check window](#check-window)
|
|
194
|
+
- [Arguments to `cy.eyesCheckWindow`](#arguments-to-cyeyescheckwindow)
|
|
195
|
+
- [`tag`](#tag)
|
|
196
|
+
- [`target`](#target)
|
|
197
|
+
- [`fully`](#fully)
|
|
198
|
+
- [`selector`](#selector)
|
|
199
|
+
- [`region`](#region)
|
|
200
|
+
- [`element`](#element)
|
|
201
|
+
- [`ignore`](#ignore)
|
|
202
|
+
- [`floating`](#floating)
|
|
203
|
+
- [`layout`](#layout)
|
|
204
|
+
- [`strict`](#strict)
|
|
205
|
+
- [`content`](#content)
|
|
206
|
+
- [`accessibility`](#accessibility)
|
|
207
|
+
- [`region in shadow DOM`](#region-in-shadow-dom)
|
|
208
|
+
- [`scriptHooks`](#scripthooks)
|
|
209
|
+
- [`layoutBreakpoints`](#layoutbreakpoints)
|
|
210
|
+
- [`sendDom`](#senddom)
|
|
211
|
+
- [`variationGroupId`](#variationgroupid)
|
|
212
|
+
- [`waitBeforeCapture`](#waitbeforecapture)
|
|
213
|
+
- [`useDom`](#usedom)
|
|
214
|
+
- [`enablePatterns`](#enablepatterns)
|
|
215
|
+
- [`matchLevel`](#matchlevel)
|
|
216
|
+
- [`visualGridOptions`](#visualgridoptions)
|
|
217
|
+
- [Close](#close)
|
|
218
|
+
- [GetAllTestResults](#getalltestresults)
|
|
219
|
+
- [deleteTestResults](#deletetestresults)
|
|
220
|
+
- [Concurrency](#concurrency)
|
|
221
|
+
- [Advanced configuration](#advanced-configuration)
|
|
222
|
+
- [Here are the available configuration properties:](#here-are-the-available-configuration-properties)
|
|
223
|
+
- [Global configuration properties:](#global-configuration-properties)
|
|
224
|
+
- [Method 1: Arguments for `cy.eyesOpen`](#method-1-arguments-for-cyeyesopen)
|
|
225
|
+
- [Method 2: Environment variables](#method-2-environment-variables)
|
|
226
|
+
- [Method 3: The `applitools.config.js` file](#method-3-the-applitoolsconfigjs-file)
|
|
227
|
+
- [Configuring the browser](#configuring-the-browser)
|
|
228
|
+
- [Previous browser versions](#previous-browser-versions)
|
|
229
|
+
- [Getting a screenshot of multiple browsers in parallel](#getting-a-screenshot-of-multiple-browsers-in-parallel)
|
|
230
|
+
- [Device emulation](#device-emulation)
|
|
231
|
+
- [iOS device](#ios-device)
|
|
232
|
+
- [Intelligent Code Completion](#intelligent-code-completion)
|
|
233
|
+
- [There are two ways you can add Eyes-Cypress intelliSense to your tests:](#there-are-two-ways-you-can-add-eyes-cypress-intellisense-to-your-tests)
|
|
234
|
+
- [1. Triple slash directives](#1-triple-slash-directives)
|
|
235
|
+
- [2. Reference type declarations via `tsconfig`](#2-reference-type-declarations-via-tsconfig)
|
|
213
236
|
|
|
214
237
|
<br/><hr/><br/>
|
|
215
238
|
|
|
@@ -827,7 +850,7 @@ cy.eyesOpen({
|
|
|
827
850
|
|
|
828
851
|
Possible values for screen orientation are `landscape` and `portrait`, and if no value is specified, the default is `portrait`.
|
|
829
852
|
|
|
830
|
-
The list of device names is available at https://github.com/applitools/eyes.sdk.javascript1/blob/master/packages/eyes-
|
|
853
|
+
The list of device names is available at https://github.com/applitools/eyes.sdk.javascript1/blob/master/packages/eyes-api/src/enums/DeviceName.ts
|
|
831
854
|
|
|
832
855
|
In addition, it's possible to use chrome's device emulation with custom viewport sizes, pixel density and mobile mode, by passing `deviceScaleFactor` and `mobile` in addition to `width` and `height`. For example:
|
|
833
856
|
|
|
@@ -859,7 +882,7 @@ cy.eyesOpen({
|
|
|
859
882
|
})
|
|
860
883
|
```
|
|
861
884
|
|
|
862
|
-
The list of devices is available at https://github.com/applitools/eyes.sdk.javascript1/blob/master/packages/eyes-
|
|
885
|
+
The list of devices is available at https://github.com/applitools/eyes.sdk.javascript1/blob/master/packages/eyes-api/src/enums/IosDeviceName.ts
|
|
863
886
|
|
|
864
887
|
Possible values for `iosVersion` are:
|
|
865
888
|
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCookies = exports.getUrl = exports.getTitle = exports.findElements = exports.findElement = exports.transformSelector = exports.setViewportSize = exports.getViewportSize = exports.childContext = exports.parentContext = exports.mainContext = exports.executeScript = void 0;
|
|
4
4
|
function executeScript(context, script, arg) {
|
|
5
|
-
context = refreshContext(context);
|
|
6
5
|
let scriptToExecute;
|
|
7
6
|
if (script.includes('dom-snapshot') ||
|
|
8
7
|
script.includes('dom-capture') ||
|
|
@@ -23,13 +22,20 @@ function mainContext() {
|
|
|
23
22
|
}
|
|
24
23
|
exports.mainContext = mainContext;
|
|
25
24
|
function parentContext(context) {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
// because Cypress doesn't support cross origin iframe, then childContext might return null, and then the input to parentContext might be null
|
|
26
|
+
if (!context) {
|
|
27
|
+
throw new Error('Context is not accessible');
|
|
28
|
+
}
|
|
29
|
+
;
|
|
28
30
|
return context === mainContext() ? context : context.defaultView.frameElement.ownerDocument;
|
|
29
31
|
}
|
|
30
32
|
exports.parentContext = parentContext;
|
|
31
33
|
function childContext(_context, element) {
|
|
32
|
-
|
|
34
|
+
if (element.contentDocument)
|
|
35
|
+
return element.contentDocument;
|
|
36
|
+
else {
|
|
37
|
+
throw new Error('Context is not accessible');
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
exports.childContext = childContext;
|
|
35
41
|
function getViewportSize() {
|
|
@@ -55,7 +61,6 @@ function transformSelector(selector) {
|
|
|
55
61
|
}
|
|
56
62
|
exports.transformSelector = transformSelector;
|
|
57
63
|
function findElement(context, selector, parent) {
|
|
58
|
-
context = refreshContext(context);
|
|
59
64
|
const eyesSelector = selector;
|
|
60
65
|
const root = parent !== null && parent !== void 0 ? parent : context;
|
|
61
66
|
const sel = typeof selector === 'string' ? selector : eyesSelector.selector;
|
|
@@ -68,7 +73,6 @@ function findElement(context, selector, parent) {
|
|
|
68
73
|
}
|
|
69
74
|
exports.findElement = findElement;
|
|
70
75
|
function findElements(context, selector, parent) {
|
|
71
|
-
context = refreshContext(context);
|
|
72
76
|
const eyesSelector = selector;
|
|
73
77
|
const root = parent !== null && parent !== void 0 ? parent : context;
|
|
74
78
|
const sel = typeof selector === 'string' ? selector : eyesSelector.selector;
|
|
@@ -86,12 +90,10 @@ function findElements(context, selector, parent) {
|
|
|
86
90
|
}
|
|
87
91
|
exports.findElements = findElements;
|
|
88
92
|
function getTitle(context) {
|
|
89
|
-
context = refreshContext(context);
|
|
90
93
|
return context.title;
|
|
91
94
|
}
|
|
92
95
|
exports.getTitle = getTitle;
|
|
93
96
|
function getUrl(context) {
|
|
94
|
-
context = refreshContext(context);
|
|
95
97
|
return context.location.href;
|
|
96
98
|
}
|
|
97
99
|
exports.getUrl = getUrl;
|
|
@@ -100,11 +102,6 @@ function getCookies() {
|
|
|
100
102
|
return Cypress.automation('get:cookies', {});
|
|
101
103
|
}
|
|
102
104
|
exports.getCookies = getCookies;
|
|
103
|
-
// we need to method to reset the context in case the user called open before visit
|
|
104
|
-
function refreshContext(context) {
|
|
105
|
-
//@ts-ignore
|
|
106
|
-
return (context && context.defaultView) ? context : cy.state('window').document;
|
|
107
|
-
}
|
|
108
105
|
// export function takeScreenshot(page: Driver): Promise<Buffer>;
|
|
109
106
|
// export function visit(page: Driver, url: string): Promise<void>; (??)
|
|
110
107
|
// export function isStaleElementError(err: any): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applitools/eyes-cypress",
|
|
3
|
-
"version": "3.26.
|
|
3
|
+
"version": "3.26.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git://github.com/applitools/eyes.sdk.javascript1.git",
|
|
@@ -50,24 +50,24 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@applitools/eyes-api": "1.5.
|
|
54
|
-
"@applitools/eyes-universal": "2.
|
|
53
|
+
"@applitools/eyes-api": "1.5.4",
|
|
54
|
+
"@applitools/eyes-universal": "2.7.1",
|
|
55
55
|
"@applitools/functional-commons": "1.6.0",
|
|
56
|
-
"@applitools/logger": "1.1.
|
|
57
|
-
"@applitools/visual-grid-client": "15.12.
|
|
56
|
+
"@applitools/logger": "1.1.10",
|
|
57
|
+
"@applitools/visual-grid-client": "15.12.41",
|
|
58
58
|
"chalk": "3.0.0",
|
|
59
59
|
"uuid": "8.3.2",
|
|
60
60
|
"ws": "8.5.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@applitools/bongo": "^2.1.
|
|
63
|
+
"@applitools/bongo": "^2.1.4",
|
|
64
64
|
"@applitools/scripts": "1.1.0",
|
|
65
65
|
"@applitools/sdk-coverage-tests": "^2.3.18",
|
|
66
66
|
"@applitools/snaptdout": "1.0.1",
|
|
67
67
|
"@applitools/test-server": "1.0.10",
|
|
68
68
|
"@applitools/test-utils": "1.3.2",
|
|
69
|
-
"@applitools/types": "^1.4.
|
|
70
|
-
"@applitools/utils": "1.3.
|
|
69
|
+
"@applitools/types": "^1.4.7",
|
|
70
|
+
"@applitools/utils": "1.3.6",
|
|
71
71
|
"@types/node": "12",
|
|
72
72
|
"@types/ws": "^8.2.2",
|
|
73
73
|
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
package/src/browser/commands.js
CHANGED
|
@@ -110,7 +110,7 @@ Cypress.Commands.add('eyesOpen', function(args = {}) {
|
|
|
110
110
|
const driver = refer.ref(cy.state('window').document);
|
|
111
111
|
|
|
112
112
|
if (!connectedToUniversal) {
|
|
113
|
-
socket.connect(`
|
|
113
|
+
socket.connect(`wss://localhost:${Cypress.config('eyesPort')}/eyes`);
|
|
114
114
|
connectedToUniversal = true;
|
|
115
115
|
socket.emit('Core.makeSDK', {
|
|
116
116
|
name: 'eyes.cypress',
|
|
@@ -152,6 +152,7 @@ Cypress.Commands.add('eyesCheckWindow', (args = {}) =>
|
|
|
152
152
|
if (isCurrentTestDisabled) return;
|
|
153
153
|
|
|
154
154
|
setRootContext();
|
|
155
|
+
const driver = refer.ref(cy.state('window').document);
|
|
155
156
|
|
|
156
157
|
Cypress.log({name: 'Eyes: check window'});
|
|
157
158
|
|
|
@@ -160,6 +161,7 @@ Cypress.Commands.add('eyesCheckWindow', (args = {}) =>
|
|
|
160
161
|
return socket.request('Eyes.check', {
|
|
161
162
|
eyes,
|
|
162
163
|
settings: checkSettings,
|
|
164
|
+
driver,
|
|
163
165
|
});
|
|
164
166
|
}),
|
|
165
167
|
);
|
|
@@ -4,7 +4,6 @@ export type Context = Document & {__applitoolsBrand?: never};
|
|
|
4
4
|
export type Element = HTMLElement & {__applitoolsBrand?: never};
|
|
5
5
|
|
|
6
6
|
export function executeScript(context: Context, script: string, arg: any): any {
|
|
7
|
-
context = refreshContext(context)
|
|
8
7
|
|
|
9
8
|
let scriptToExecute;
|
|
10
9
|
if (
|
|
@@ -28,13 +27,20 @@ export function mainContext(): Context {
|
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export function parentContext(context: Context): Context {
|
|
31
|
-
|
|
30
|
+
// because Cypress doesn't support cross origin iframe, then childContext might return null, and then the input to parentContext might be null
|
|
31
|
+
if (!context) {
|
|
32
|
+
throw new Error('Context is not accessible')
|
|
33
|
+
};
|
|
32
34
|
|
|
33
35
|
return context === mainContext() ? context : context.defaultView.frameElement.ownerDocument
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
export function childContext(_context: Context, element: HTMLIFrameElement): Context {
|
|
37
|
-
|
|
39
|
+
if(element.contentDocument)
|
|
40
|
+
return element.contentDocument
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('Context is not accessible')
|
|
43
|
+
}
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
export function getViewportSize(): Object {
|
|
@@ -60,7 +66,6 @@ export function transformSelector(selector: Selector): Selector {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
export function findElement(context: Context, selector: Selector, parent?: Element) {
|
|
63
|
-
context = refreshContext(context)
|
|
64
69
|
const eyesSelector = (selector as EyesSelector)
|
|
65
70
|
const root = parent ?? context
|
|
66
71
|
const sel = typeof selector === 'string' ? selector : eyesSelector.selector
|
|
@@ -72,7 +77,6 @@ export function findElement(context: Context, selector: Selector, parent?: Eleme
|
|
|
72
77
|
}
|
|
73
78
|
|
|
74
79
|
export function findElements(context: Context, selector: Selector, parent: Element){
|
|
75
|
-
context = refreshContext(context)
|
|
76
80
|
const eyesSelector = (selector as EyesSelector)
|
|
77
81
|
const root = parent ?? context
|
|
78
82
|
const sel = typeof selector === 'string' ? selector : eyesSelector.selector
|
|
@@ -91,12 +95,10 @@ export function findElements(context: Context, selector: Selector, parent: Eleme
|
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
export function getTitle(context: Context): string {
|
|
94
|
-
context = refreshContext(context)
|
|
95
98
|
return context.title
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
export function getUrl(context: Context): string {
|
|
99
|
-
context = refreshContext(context)
|
|
100
102
|
return context.location.href
|
|
101
103
|
}
|
|
102
104
|
|
|
@@ -105,11 +107,6 @@ export function getCookies(): Array<any> {
|
|
|
105
107
|
return Cypress.automation('get:cookies', {})
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
// we need to method to reset the context in case the user called open before visit
|
|
109
|
-
function refreshContext(context: Context) {
|
|
110
|
-
//@ts-ignore
|
|
111
|
-
return (context && context.defaultView) ? context : cy.state('window').document
|
|
112
|
-
}
|
|
113
110
|
|
|
114
111
|
// export function takeScreenshot(page: Driver): Promise<Buffer>;
|
|
115
112
|
|
package/src/plugin/server.js
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {makeHandler} = require('../../dist/plugin/handler');
|
|
3
2
|
const connectSocket = require('./webSocket');
|
|
4
3
|
const {makeServerProcess} = require('@applitools/eyes-universal');
|
|
5
4
|
const {TestResults} = require('@applitools/visual-grid-client');
|
|
6
5
|
const handleTestResults = require('./handleTestResults');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const {Server: HttpsServer} = require('https');
|
|
9
|
+
const {Server: WSServer} = require('ws');
|
|
7
10
|
|
|
8
11
|
function makeStartServer({logger}) {
|
|
9
12
|
return async function startServer() {
|
|
10
|
-
const
|
|
13
|
+
const key = fs.readFileSync(path.resolve(__dirname, '../pem/server.key'));
|
|
14
|
+
const cert = fs.readFileSync(path.resolve(__dirname, '../pem/server.cert'));
|
|
15
|
+
let port;
|
|
11
16
|
|
|
12
|
-
const
|
|
17
|
+
const https = new HttpsServer({
|
|
18
|
+
key,
|
|
19
|
+
cert,
|
|
20
|
+
});
|
|
21
|
+
await https.listen(0, err => {
|
|
22
|
+
if (err) {
|
|
23
|
+
logger.log('error starting plugin server', err);
|
|
24
|
+
} else {
|
|
25
|
+
logger.log(`plugin server running at port: ${https.address().port}`);
|
|
26
|
+
port = https.address().port;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const wss = new WSServer({server: https, path: '/eyes', maxPayload: 254 * 1024 * 1024});
|
|
31
|
+
|
|
32
|
+
wss.on('close', () => https.close());
|
|
33
|
+
|
|
34
|
+
const {port: universalPort, close: closeUniversalServer} = await makeServerProcess({
|
|
35
|
+
key: path.resolve(__dirname, '../pem/server.key'),
|
|
36
|
+
cert: path.resolve(__dirname, '../pem/server.cert'),
|
|
37
|
+
});
|
|
13
38
|
|
|
14
39
|
const managers = [];
|
|
15
40
|
let socketWithUniversal;
|
|
16
41
|
|
|
17
|
-
|
|
18
|
-
socketWithUniversal = connectSocket(`
|
|
42
|
+
wss.on('connection', socketWithClient => {
|
|
43
|
+
socketWithUniversal = connectSocket(`wss://localhost:${universalPort}/eyes`);
|
|
19
44
|
|
|
20
45
|
socketWithUniversal.setPassthroughListener(message => {
|
|
21
46
|
logger.log('<== ', message.toString().slice(0, 1000));
|
|
@@ -69,7 +94,7 @@ function makeStartServer({logger}) {
|
|
|
69
94
|
});
|
|
70
95
|
|
|
71
96
|
return {
|
|
72
|
-
server,
|
|
97
|
+
server: wss,
|
|
73
98
|
port,
|
|
74
99
|
closeManager,
|
|
75
100
|
closeBatches,
|
package/dist/plugin/handler.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeHandler = void 0;
|
|
4
|
-
const http_1 = require("http");
|
|
5
|
-
const ws_1 = require("ws");
|
|
6
|
-
const { name, version } = require('../../package.json'); // TODO is the handshake needed at all?
|
|
7
|
-
const TOKEN_HEADER = 'x-eyes-universal-token';
|
|
8
|
-
const TOKEN = `${name}@${version}`;
|
|
9
|
-
// TODO make default port 0 and return actual port
|
|
10
|
-
async function makeHandler({ port = 31077, singleton = true, lazy = false } = {}) {
|
|
11
|
-
const http = new http_1.Server();
|
|
12
|
-
http.on('request', (request, response) => {
|
|
13
|
-
if (request.url === '/handshake') {
|
|
14
|
-
if (request.headers[TOKEN_HEADER] === TOKEN) {
|
|
15
|
-
response.writeHead(200, { [TOKEN_HEADER]: TOKEN });
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
response.writeHead(400);
|
|
19
|
-
}
|
|
20
|
-
response.end();
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
http.listen(port, 'localhost');
|
|
24
|
-
return new Promise((resolve, reject) => {
|
|
25
|
-
http.on('listening', () => {
|
|
26
|
-
const ws = new ws_1.Server({ server: http, path: '/eyes' });
|
|
27
|
-
ws.on('close', () => http.close());
|
|
28
|
-
resolve({ server: ws, port });
|
|
29
|
-
});
|
|
30
|
-
http.on('error', async (err) => {
|
|
31
|
-
if (!lazy && err.code === 'EADDRINUSE') {
|
|
32
|
-
if (singleton && (await isHandshakable(port))) {
|
|
33
|
-
return resolve({ port });
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
return resolve(await makeHandler({ port: port + 1, singleton }));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
reject(err);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
exports.makeHandler = makeHandler;
|
|
44
|
-
async function isHandshakable(port) {
|
|
45
|
-
return new Promise(resolve => {
|
|
46
|
-
const handshake = (0, http_1.request)(`http://localhost:${port}/handshake`, {
|
|
47
|
-
headers: { [TOKEN_HEADER]: TOKEN },
|
|
48
|
-
});
|
|
49
|
-
handshake.on('response', ({ statusCode, headers }) => {
|
|
50
|
-
resolve(statusCode === 200 && headers[TOKEN_HEADER] === TOKEN);
|
|
51
|
-
});
|
|
52
|
-
handshake.on('error', () => resolve(false));
|
|
53
|
-
handshake.end();
|
|
54
|
-
});
|
|
55
|
-
}
|
package/src/plugin/handler.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {Server as HTTPServer, request} from 'http'
|
|
2
|
-
import {Server as WSServer} from 'ws'
|
|
3
|
-
|
|
4
|
-
const {name, version} = require('../../package.json') // TODO is the handshake needed at all?
|
|
5
|
-
const TOKEN_HEADER = 'x-eyes-universal-token'
|
|
6
|
-
const TOKEN = `${name}@${version}`
|
|
7
|
-
|
|
8
|
-
// TODO make default port 0 and return actual port
|
|
9
|
-
export async function makeHandler({port = 31077, singleton = true, lazy = false} = {}): Promise<{
|
|
10
|
-
server?: WSServer
|
|
11
|
-
port: number
|
|
12
|
-
}> {
|
|
13
|
-
const http = new HTTPServer()
|
|
14
|
-
http.on('request', (request, response) => {
|
|
15
|
-
if (request.url === '/handshake') {
|
|
16
|
-
if (request.headers[TOKEN_HEADER] === TOKEN) {
|
|
17
|
-
response.writeHead(200, {[TOKEN_HEADER]: TOKEN})
|
|
18
|
-
} else {
|
|
19
|
-
response.writeHead(400)
|
|
20
|
-
}
|
|
21
|
-
response.end()
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
http.listen(port, 'localhost')
|
|
26
|
-
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
http.on('listening', () => {
|
|
29
|
-
const ws = new WSServer({server: http, path: '/eyes'})
|
|
30
|
-
ws.on('close', () => http.close())
|
|
31
|
-
resolve({server: ws, port})
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
http.on('error', async (err: Error & {code: string}) => {
|
|
35
|
-
if (!lazy && err.code === 'EADDRINUSE') {
|
|
36
|
-
if (singleton && (await isHandshakable(port))) {
|
|
37
|
-
return resolve({port})
|
|
38
|
-
} else {
|
|
39
|
-
return resolve(await makeHandler({port: port + 1, singleton}))
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
reject(err)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function isHandshakable(port: number) {
|
|
48
|
-
return new Promise(resolve => {
|
|
49
|
-
const handshake = request(`http://localhost:${port}/handshake`, {
|
|
50
|
-
headers: {[TOKEN_HEADER]: TOKEN},
|
|
51
|
-
})
|
|
52
|
-
handshake.on('response', ({statusCode, headers}) => {
|
|
53
|
-
resolve(statusCode === 200 && headers[TOKEN_HEADER] === TOKEN)
|
|
54
|
-
})
|
|
55
|
-
handshake.on('error', () => resolve(false))
|
|
56
|
-
handshake.end()
|
|
57
|
-
})
|
|
58
|
-
}
|