Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mbalabash committed Nov 20, 2023
1 parent fe91b9c commit 81b3111
Show file tree
Hide file tree
Showing 10 changed files with 1,121 additions and 733 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.0

- Dropped support for CommonJS
- Switched to using @puppeteer/browsers to download the browser

## 1.0.5

- Dropped support for [createBrowserFetcher](https://pptr.dev/api/puppeteer.puppeteernode.createbrowserfetcher#remarks)
Expand Down
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { isSuitableVersion, chromeVersion, downloadChromium } = require('./src/chrome')
const { findChromeBinaryOnDarwin } = require('./src/darwin')
const { findChromeBinaryOnLinux } = require('./src/linux')
const { findChromeBinaryOnWin32 } = require('./src/win32')
import { isSuitableVersion, chromeVersion, downloadChromium } from './src/chrome/index.js'
import { findChromeBinaryOnDarwin } from './src/darwin/index.js'
import { findChromeBinaryOnLinux } from './src/linux/index.js'
import { findChromeBinaryOnWin32 } from './src/win32/index.js'

async function findChrome({ min, max, download: { puppeteer, path, revision } = {} } = {}) {
export async function findChrome({ min, max, download: { puppeteer, path, revision } = {} } = {}) {
try {
let executablePath = findChromeBinaryPath()
let isSuitable = isSuitableVersion(executablePath, min, max)
Expand Down Expand Up @@ -53,5 +53,3 @@ function findChromeBinaryPath() {

return process.env.CHROMIUM_EXECUTABLE_PATH || process.env.PUPPETEER_EXECUTABLE_PATH || getter()
}

module.exports = { findChrome }
30 changes: 17 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "find-chrome-bin",
"version": "1.0.5",
"version": "2.0.0",
"description": "Finds local Chromium binary to use it with puppeteer-core",
"keywords": [
"chrome",
Expand All @@ -14,30 +14,34 @@
"license": "MIT",
"author": "mbalabash <maksim.balabash@gmail.com>",
"engines": {
"node": ">=14.0.0"
"node": ">=18.0.0"
},
"type": "module",
"types": "./index.d.ts",
"scripts": {
"unit": "tsm node_modules/uvu/bin.js test/",
"check": "eslint . && check-dts && size-limit",
"test": "yarn unit && yarn check"
},
"dependencies": {
"@puppeteer/browsers": "^1.8.0"
},
"devDependencies": {
"@logux/eslint-config": "^49.0.0",
"@size-limit/preset-small-lib": "^8.2.4",
"check-dts": "^0.7.1",
"eslint": "^8.36.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"@logux/eslint-config": "^52.0.2",
"@size-limit/preset-small-lib": "^11.0.0",
"check-dts": "^0.7.2",
"eslint": "^8.54.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-n": "^16.3.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-promise": "^6.1.1",
"nanospy": "^0.5.0",
"puppeteer-core": "^19.8.0",
"size-limit": "^8.2.4",
"nanospy": "^1.0.0",
"puppeteer-core": "^21.5.2",
"size-limit": "^11.0.0",
"tsm": "^2.3.0",
"typescript": "^5.0.2",
"typescript": "^5.3.2",
"uvu": "^0.5.6"
},
"eslintConfig": {
Expand Down
45 changes: 18 additions & 27 deletions src/chrome/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { execSync } = require('child_process')
import { execSync } from 'child_process'
import { install } from '@puppeteer/browsers'

const { MIN_CHROME_VERSION, MAX_CHROME_VERSION } = require('../utils')
const { getWin32ChromeVersionInfo } = require('../win32')
import { MIN_CHROME_VERSION, MAX_CHROME_VERSION } from '../utils/index.js'
import { getWin32ChromeVersionInfo } from '../win32/index.js'

function isSuitableVersion(executablePath, min = MIN_CHROME_VERSION, max = MAX_CHROME_VERSION) {
export function isSuitableVersion(
executablePath,
min = MIN_CHROME_VERSION,
max = MAX_CHROME_VERSION
) {
if (min > max) {
throw new Error(
"ERROR: Passed options for limiting chrome versions are incorrect. Min couldn't be bigger then Max."
Expand Down Expand Up @@ -34,42 +39,28 @@ function isSuitableVersion(executablePath, min = MIN_CHROME_VERSION, max = MAX_C
return false
}

function chromeVersion(executablePath) {
export function chromeVersion(executablePath) {
return (
process.platform === 'win32'
? getWin32ChromeVersionInfo(executablePath)
: execSync(`"${executablePath}" --version`).toString()
).trim()
}

async function downloadChromium(puppeteer, path, revision) {
export async function downloadChromium(puppeteer, path, revision) {
try {
let downloadHost =
process.env.PUPPETEER_DOWNLOAD_HOST ||
process.env.npm_config_puppeteer_download_host ||
process.env.npm_package_config_puppeteer_download_host
const config = {
buildId: revision,
browser: 'chrome',
unpack: true,
cacheDir: path
}

let browserFetcher = new puppeteer.BrowserFetcher({ path, host: downloadHost })

let revisionInfo = browserFetcher.revisionInfo(revision)

// If already downloaded
if (revisionInfo.local) return revisionInfo

let newRevisionInfo = await browserFetcher.download(revisionInfo.revision)

let localRevisions = await browserFetcher.localRevisions()
localRevisions = localRevisions.filter(r => r !== revisionInfo.revision)

// Remove previous revisions
let cleanupOldVersions = localRevisions.map(r => browserFetcher.remove(r))
await Promise.all(cleanupOldVersions)
let newRevisionInfo = await install(config)

return newRevisionInfo
} catch (error) {
console.error(`ERROR: Failed to download Chromium!`) // eslint-disable-line no-console
throw error
}
}

module.exports = { isSuitableVersion, chromeVersion, downloadChromium }
10 changes: 4 additions & 6 deletions src/darwin/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path')
const { execSync } = require('child_process')
import { join } from 'path'
import { execSync } from 'child_process'

const { newLineRegex, canAccess } = require('../utils')
import { newLineRegex, canAccess } from '../utils/index.js'

function findChromeBinaryOnDarwin(canary) {
export function findChromeBinaryOnDarwin(canary) {
let LSREGISTER =
'/System/Library/Frameworks/CoreServices.framework' +
'/Versions/A/Frameworks/LaunchServices.framework' +
Expand Down Expand Up @@ -34,5 +34,3 @@ function findChromeBinaryOnDarwin(canary) {

return undefined
}

module.exports = { findChromeBinaryOnDarwin }
12 changes: 5 additions & 7 deletions src/linux/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { join } = require('path')
const { execSync, execFileSync } = require('child_process')
const { homedir } = require('os')
import { join } from 'path'
import { execSync, execFileSync } from 'child_process'
import { homedir } from 'os'

const { newLineRegex, canAccess } = require('../utils')
import { newLineRegex, canAccess } from '../utils/index.js'

function findChromeBinaryOnLinux() {
export function findChromeBinaryOnLinux() {
let installations = []

// Look into the directories where .desktop are saved on gnome based distro's
Expand Down Expand Up @@ -93,5 +93,3 @@ function sort(installations, priorities) {
function uniq(arr) {
return Array.from(new Set(arr))
}

module.exports = { findChromeBinaryOnLinux }
17 changes: 5 additions & 12 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { accessSync } = require('fs')
import { accessSync } from 'fs'

const MIN_CHROME_VERSION = 85
export const MIN_CHROME_VERSION = 85

const MAX_CHROME_VERSION = Number.MAX_VALUE
export const MAX_CHROME_VERSION = Number.MAX_VALUE

const newLineRegex = /\r?\n/
export const newLineRegex = /\r?\n/

function canAccess(file) {
export function canAccess(file) {
if (!file) {
return false
}
Expand All @@ -18,10 +18,3 @@ function canAccess(file) {
return false
}
}

module.exports = {
MIN_CHROME_VERSION,
MAX_CHROME_VERSION,
newLineRegex,
canAccess
}
12 changes: 5 additions & 7 deletions src/win32/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { sep, join } = require('path')
const { execSync } = require('child_process')
import { sep, join } from 'path'
import { execSync } from 'child_process'

const { canAccess } = require('../utils')
import { canAccess } from '../utils/index.js'

function findChromeBinaryOnWin32(canary) {
export function findChromeBinaryOnWin32(canary) {
let suffix = canary
? `${sep}Google${sep}Chrome SxS${sep}Application${sep}chrome.exe`
: `${sep}Google${sep}Chrome${sep}Application${sep}chrome.exe`
Expand All @@ -24,7 +24,7 @@ function findChromeBinaryOnWin32(canary) {
return result
}

function getWin32ChromeVersionInfo(executablePath) {
export function getWin32ChromeVersionInfo(executablePath) {
let executablePathForNode = executablePath.replace(/\\/g, '\\\\')
let wmiResult = execSync(
`wmic datafile where name="${executablePathForNode}" GET Manufacturer,FileName,Version /format:csv`,
Expand Down Expand Up @@ -63,5 +63,3 @@ function getWin32ChromeVersionInfo(executablePath) {
throw new Error(`No version information found for '${executablePath}'`)
}
}

module.exports = { findChromeBinaryOnWin32, getWin32ChromeVersionInfo }
19 changes: 11 additions & 8 deletions test/find-chrome-bin.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const { test } = require('uvu')
const { join } = require('path')
const assert = require('uvu/assert')
const puppeteer = require('puppeteer-core')
const { restoreAll } = require('nanospy')
const { PUPPETEER_REVISIONS } = require('puppeteer-core/lib/cjs/puppeteer/revisions.js')
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { join, dirname } from 'node:path'
import puppeteer from 'puppeteer-core'
import { restoreAll } from 'nanospy'
import { PUPPETEER_REVISIONS } from 'puppeteer-core/lib/cjs/puppeteer/revisions.js'
import { fileURLToPath } from 'url';

const { findChrome } = require('../index.js')
import { findChrome } from '../index.js'

const __dirname = dirname(fileURLToPath(import.meta.url));

test.after.each(() => restoreAll())

Expand All @@ -20,7 +23,7 @@ test("should download when could not find chromium and 'puppeteer' options are s
let downloadedChromeInfo = await findChrome({
min: 70,
max: 70,
download: { puppeteer, revision: PUPPETEER_REVISIONS.chromium, path: join(__dirname, 'chrome') }
download: { puppeteer, revision: PUPPETEER_REVISIONS.chrome, path: join(__dirname, 'chrome') }
})

assert.is(downloadedChromeInfo.browser.length > 0, true)
Expand Down

0 comments on commit 81b3111

Please sign in to comment.