browser-webdriver-downloader 1.5.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 CrowdStrike
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # browser-webdriver-downloader
2
+
3
+ [![npm version](https://badge.fury.io/js/browser-webdriver-downloader.svg)](https://badge.fury.io/js/browser-webdriver-downloader)
4
+
5
+ Install and wrap msedgedriver in Node.js
6
+
7
+ Setting `EDGEDRIVER_VERSION` will prevent it from downloading latest, for example `EDGEDRIVER_VERSION=102.0.1245.33 npm install`.
8
+
9
+ Setting `DETECT_EDGEDRIVER_VERSION=true` will match your installed Edge version.
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ require('../src/utils/throw-up');
4
+
5
+ const { install } = require('../src');
6
+
7
+ install();
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ require('../src/utils/throw-up');
5
+
6
+ const execa = require('execa');
7
+ const { getDriverPath } = require('../src');
8
+
9
+ (async () => {
10
+ await execa(getDriverPath(), process.argv.slice(2), {
11
+ stdio: 'inherit',
12
+ });
13
+ })();
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "browser-webdriver-downloader",
3
+ "version": "1.5.1",
4
+ "description": "Install and wrap msedgedriver in Node.js",
5
+ "bin": {
6
+ "msedgedriver": "bin/msedgedriver.js"
7
+ },
8
+ "main": "src",
9
+ "files": [
10
+ "bin",
11
+ "src"
12
+ ],
13
+ "keywords": [
14
+ "msedgedriver",
15
+ "webdriver"
16
+ ],
17
+ "scripts": {
18
+ "lint:git": "commitlint --default-branch main",
19
+ "lint:js": "eslint . --ext js,json",
20
+ "lint:md": "remark -f README.md",
21
+ "install": "node bin/install-msedgedriver",
22
+ "start": "node bin/msedgedriver.js",
23
+ "release": "standard-version",
24
+ "test": "mocha"
25
+ },
26
+ "standard-version": {
27
+ "scripts": {
28
+ "posttag": "git push --follow-tags --atomic"
29
+ }
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+ssh://git@github.com/CrowdStrike/browser-webdriver-downloader.git"
34
+ },
35
+ "author": "Kelly Selden",
36
+ "license": "MIT",
37
+ "bugs": {
38
+ "url": "https://github.com/CrowdStrike/browser-webdriver-downloader/issues"
39
+ },
40
+ "homepage": "https://github.com/CrowdStrike/browser-webdriver-downloader#readme",
41
+ "engines": {
42
+ "node": ">=14.15"
43
+ },
44
+ "dependencies": {
45
+ "execa": "^5.0.0",
46
+ "extract-zip": "^2.0.1",
47
+ "find-edge-version": "0.1.1",
48
+ "fs-extra": "^10.1.0",
49
+ "got": "^11.8.5",
50
+ "tmp": "0.2.1",
51
+ "yn": "^4.0.0"
52
+ },
53
+ "devDependencies": {
54
+ "@crowdstrike/commitlint": "^5.0.0",
55
+ "chai": "^4.2.0",
56
+ "chai-as-promised": "^7.1.1",
57
+ "chai-fs": "^2.0.0",
58
+ "eslint": "^8.0.0",
59
+ "eslint-config-crowdstrike": "^8.0.0",
60
+ "eslint-config-crowdstrike-node": "^3.0.0",
61
+ "eslint-plugin-json-files": "^1.0.0",
62
+ "eslint-plugin-mocha": "^10.0.0",
63
+ "eslint-plugin-node": "^11.0.0",
64
+ "mocha": "^10.0.0",
65
+ "mocha-helpers": "^6.2.1",
66
+ "remark-cli": "^11.0.0",
67
+ "remark-preset-lint-crowdstrike": "^2.0.0",
68
+ "renovate-config-standard": "^2.0.0",
69
+ "standard-node-template": "3.0.0",
70
+ "standard-version": "9.5.0"
71
+ }
72
+ }
package/src/fs.js ADDED
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const fs = { ...require('fs'), ...require('fs').promises };
4
+
5
+ async function exists(path) {
6
+ try {
7
+ await fs.access(path, fs.constants.F_OK);
8
+
9
+ return true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
14
+
15
+ async function rename(from, to) {
16
+ // system tmp may be mounted on a different drive
17
+ // use `fs-extra` to prevent `Error: EXDEV: cross-device link not permitted, rename ...`
18
+ await require('fs-extra').move(from, to);
19
+ }
20
+
21
+ module.exports = {
22
+ exists,
23
+ rename,
24
+ };
package/src/index.js ADDED
@@ -0,0 +1,241 @@
1
+ 'use strict';
2
+
3
+ const got = require('got');
4
+ const { promisify } = require('util');
5
+ const fs = { ...require('fs'), ...require('fs').promises, ...require('../src/fs') };
6
+ const path = require('path');
7
+ const extractZip = require('extract-zip');
8
+ const pipeline = promisify(require('stream').pipeline);
9
+ const os = require('os');
10
+ const { createTmpDir } = require('../src/tmp');
11
+ const execa = require('execa');
12
+ const yn = require('yn');
13
+
14
+ const platform = os.platform();
15
+ const arch = os.arch();
16
+
17
+ const downloadHost = 'https://msedgedriver.azureedge.net';
18
+
19
+ const driversRoot = path.join(__dirname, '../bin');
20
+
21
+ function getDownloadName() {
22
+ let firstPart;
23
+ let secondPart;
24
+
25
+ if (platform === 'linux' && arch === 'x64') {
26
+ firstPart = 'linux';
27
+ secondPart = '64';
28
+ } else if (platform === 'darwin' && arch === 'x64') {
29
+ firstPart = 'mac';
30
+ secondPart = '64';
31
+ } else if (platform === 'darwin' && arch === 'arm64') {
32
+ firstPart = 'mac';
33
+ secondPart = '64';
34
+ } else if (platform === 'win32' && arch === 'x64') {
35
+ firstPart = 'win';
36
+ secondPart = '64';
37
+ } else if (platform === 'win32' && arch === 'x32') {
38
+ firstPart = 'win';
39
+ secondPart = '32';
40
+ } else if (platform === 'win32' && arch === 'arm64') {
41
+ firstPart = 'arm';
42
+ secondPart = '64';
43
+ } else {
44
+ throw new Error(`${platform} ${arch} not supported`);
45
+ }
46
+
47
+ return `edgedriver_${firstPart}${secondPart}.zip`;
48
+ }
49
+
50
+ async function getDriverVersion() {
51
+ let version;
52
+
53
+ if (process.env.EDGEDRIVER_VERSION) {
54
+ version = process.env.EDGEDRIVER_VERSION;
55
+ } else if (yn(process.env.DETECT_EDGEDRIVER_VERSION)) {
56
+ version = await getDetectedDriverVersion();
57
+ }
58
+
59
+ if (!version) {
60
+ version = await getLatestDriverVersion();
61
+ }
62
+
63
+ return version;
64
+ }
65
+
66
+ async function getDetectedDriverVersion() {
67
+ let version;
68
+
69
+ if (platform === 'win32') {
70
+ const findEdgeVersion = require('find-edge-version');
71
+
72
+ let result;
73
+
74
+ try {
75
+ result = await findEdgeVersion();
76
+ } catch (err) {
77
+ if (err.message !== 'MS Edge browser is not found') {
78
+ throw err;
79
+ }
80
+ }
81
+
82
+ if (result) {
83
+ version = result.version;
84
+ }
85
+ } else {
86
+ let browserCmd = (() => {
87
+ switch (platform) {
88
+ case 'linux': return 'microsoft-edge';
89
+ case 'darwin': return '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge';
90
+ default: throw new Error(`Platform "${platform}" not supported`);
91
+ }
92
+ })();
93
+
94
+ let ps;
95
+
96
+ try {
97
+ ps = await execa(browserCmd, ['--version']);
98
+ } catch (err) {
99
+ if (err.code !== 'ENOENT') {
100
+ throw err;
101
+ }
102
+ }
103
+
104
+ if (ps) {
105
+ // "Microsoft Edge 105.0.1343.53 "
106
+ version = ps.stdout.match(/(?:\d|\.)+/)[0];
107
+ }
108
+ }
109
+
110
+ if (version) {
111
+ console.log(`DETECT_EDGEDRIVER_VERSION=${process.env.DETECT_EDGEDRIVER_VERSION}, detected version ${version}`);
112
+ } else {
113
+ console.log(`DETECT_EDGEDRIVER_VERSION=${process.env.DETECT_EDGEDRIVER_VERSION}, but Microsoft Edge install not found`);
114
+ }
115
+
116
+ return version;
117
+ }
118
+
119
+ async function getLatestDriverVersion() {
120
+ let { body } = await got.get(`${downloadHost}/LATEST_STABLE`);
121
+
122
+ // For example: '��102.0.1245.33\r\n'
123
+ let version = body.replace(/[^\d.]/g, '');
124
+
125
+ return version;
126
+ }
127
+
128
+ function getDriverName() {
129
+ if (platform === 'win32') {
130
+ return 'msedgedriver.exe';
131
+ } else {
132
+ return 'msedgedriver';
133
+ }
134
+ }
135
+
136
+ function getDriverPath(driverName = getDriverName()) {
137
+ return path.resolve(driversRoot, driverName);
138
+ }
139
+
140
+ async function install() {
141
+ let version = await getDriverVersion();
142
+
143
+ let driverName = getDriverName();
144
+
145
+ let driverPath = getDriverPath(driverName);
146
+
147
+ let shouldDownload = true;
148
+
149
+ if (await fs.exists(driverPath)) {
150
+ let ps = await execa(driverPath, ['--version']);
151
+
152
+ // "Microsoft Edge WebDriver 105.0.1343.53 (3a47f00402d579c8ba1fad7e143f9d73831b6765)"
153
+ let existingVersion = ps.stdout.match(/(?:\d|\.)+/)[0];
154
+
155
+ if (existingVersion === version) {
156
+ console.log(`Found ${driverPath} at version ${existingVersion}, not downloading`);
157
+
158
+ shouldDownload = false;
159
+ } else {
160
+ console.log(`Found ${driverPath} at different version ${existingVersion}, redownloading`);
161
+
162
+ await fs.unlink(driverPath);
163
+ }
164
+ }
165
+
166
+ if (shouldDownload) {
167
+ await downloadAndExtract({ version, driverName, driverPath });
168
+ }
169
+
170
+ console.log(`Edge WebDriver available at ${driverPath}`);
171
+
172
+ // await hackLocalBinSymlink();
173
+ }
174
+
175
+ async function downloadAndExtract({ version, driverName, driverPath }) {
176
+ let tmpPath = await createTmpDir();
177
+
178
+ let downloadPath = await download({ tmpPath, version });
179
+
180
+ await extract({ tmpPath, downloadPath, driverName, driverPath });
181
+
182
+ await fs.rm(tmpPath, { recursive: true, force: true });
183
+ }
184
+
185
+ async function download({ tmpPath, version }) {
186
+ let downloadName = getDownloadName();
187
+
188
+ let downloadPath = path.join(tmpPath, downloadName);
189
+
190
+ let downloadUrl = `${downloadHost}/${version}/${downloadName}`;
191
+
192
+ console.log(`Downloading ${downloadUrl}...`);
193
+
194
+ await pipeline(
195
+ got.stream(downloadUrl),
196
+ fs.createWriteStream(downloadPath),
197
+ );
198
+
199
+ return downloadPath;
200
+ }
201
+
202
+ async function extract({ tmpPath, downloadPath, driverName, driverPath }) {
203
+ console.log(`Extracting ${downloadPath}...`);
204
+
205
+ await extractZip(downloadPath, { dir: tmpPath });
206
+
207
+ let tmpDriverPath = path.join(tmpPath, driverName);
208
+
209
+ await fs.mkdir(path.dirname(driverPath), { recursive: true });
210
+
211
+ await fs.rename(tmpDriverPath, driverPath);
212
+ }
213
+
214
+ // eslint-disable-next-line no-unused-vars
215
+ async function hackLocalBinSymlink() {
216
+ let packagePath = require.resolve('../package');
217
+ let { bin } = require(packagePath);
218
+ let packageRoot = path.dirname(packagePath);
219
+
220
+ for (let [name, _path] of Object.entries(bin)) {
221
+ let dest = path.join(packageRoot, 'node_modules/.bin', name);
222
+ let source = path.relative(path.dirname(dest), path.join(packageRoot, _path));
223
+
224
+ try {
225
+ await fs.unlink(dest);
226
+ } catch (err) {
227
+ if (err.code !== 'ENOENT') {
228
+ throw err;
229
+ }
230
+ }
231
+
232
+ await fs.symlink(source, dest);
233
+
234
+ console.log(`${dest} -> ${source}`);
235
+ }
236
+ }
237
+
238
+ module.exports = {
239
+ getDriverPath,
240
+ install,
241
+ };
package/src/tmp.js ADDED
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ const { promisify } = require('util');
4
+ const createTmpDir = promisify(require('tmp').dir);
5
+
6
+ module.exports = {
7
+ createTmpDir,
8
+ };
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ // https://medium.com/@dtinth/making-unhandled-promise-rejections-crash-the-node-js-process-ffc27cfcc9dd
4
+ process.on('unhandledRejection', up => { throw up; });