diff --git a/.eslintrc.json b/.eslintrc.json index c527c98..2065dd3 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,12 +1,12 @@ { "extends": "eslint:recommended", "env": { - "commonjs": true, "es6": true, "node": true }, "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": 2020, + "sourceType": "module" }, "globals": { "Atomics": "readonly", diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ad69ce7..7c34002 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,7 +29,8 @@ jobs: - name: test-install ffmpeg-static as dependency run: | file=$(npm pack -s) && file=$(realpath $file) - cd $(mktemp -d) && npm init -y + cd $(mktemp -d) + echo '{"name": "tmp", "version": "1.0.0", "private": true, "type": "module", "main": "index.js"}' >package.json npm i "$file" - file $(node -p 'require("ffmpeg-static")') - $(node -p 'require("ffmpeg-static")') --help + file $(node -e 'import("ffmpeg-static").then(m => console.log(m.default))') + $(node -e 'import("ffmpeg-static").then(m => console.log(m.default))') --help diff --git a/README.md b/README.md index fabc2d1..c70829a 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Because `ffmpeg-static` will download a binary specific to the OS/platform, you Returns the path of a statically linked ffmpeg binary on the local filesystem. ``` js -var pathToFfmpeg = require('ffmpeg-static'); +import pathToFfmpeg from 'ffmpeg-static'; console.log(pathToFfmpeg); ``` diff --git a/example.js b/example.js index d5de1e7..21b37d4 100755 --- a/example.js +++ b/example.js @@ -1,10 +1,10 @@ #!/usr/bin/env node 'use strict' -const {resolve} = require('path') -const shell = require('any-shell-escape') -const {exec} = require('child_process') -const pathToFfmpeg = require('.') +import {resolve} from 'node:path' +import shell from 'any-shell-escape' +import {exec} from 'node:child_process' +import pathToFfmpeg from './index.js'; const argv = process.argv.slice(2) if (argv.includes('-h') || argv.includes('--help')) { diff --git a/index.js b/index.js index 5e6b1b9..b8fa8a2 100644 --- a/index.js +++ b/index.js @@ -1,29 +1,31 @@ 'use strict' +import {arch as osArch, platform as osPlatform} from 'node:os' +import {fileURLToPath} from 'url' +import {join as pathJoin, dirname} from 'node:path' + +let ffmpegPath = null + if (process.env.FFMPEG_BIN) { - module.exports = process.env.FFMPEG_BIN + ffmpegPath = process.env.FFMPEG_BIN } else { - var os = require('os') - var path = require('path') - - var binaries = Object.assign(Object.create(null), { + const binaries = Object.assign(Object.create(null), { darwin: ['x64', 'arm64'], freebsd: ['x64'], linux: ['x64', 'ia32', 'arm64', 'arm'], win32: ['x64', 'ia32'] }) - var platform = process.env.npm_config_platform || os.platform() - var arch = process.env.npm_config_arch || os.arch() + const platform = process.env.npm_config_platform || osPlatform() + const arch = process.env.npm_config_arch || osArch() - var ffmpegPath = path.join( - __dirname, - platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg' - ) - - if (!binaries[platform] || binaries[platform].indexOf(arch) === -1) { - ffmpegPath = null + if (binaries[platform] && binaries[platform].includes(arch)) { + const __dirname = dirname(fileURLToPath(import.meta.url)) + ffmpegPath = pathJoin( + __dirname, + platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg' + ) } - - module.exports = ffmpegPath } + +export default ffmpegPath diff --git a/install.js b/install.js index e9cbbe3..065dc82 100644 --- a/install.js +++ b/install.js @@ -1,18 +1,23 @@ 'use strict' -var fs = require("fs"); -var os = require("os"); -const {encode: encodeQuery} = require('querystring') -const {strictEqual} = require('assert') -const envPaths = require('env-paths') -const FileCache = require('@derhuerst/http-basic/lib/FileCache').default -const {extname} = require('path') -var ProgressBar = require("progress"); -var request = require('@derhuerst/http-basic') -const {createGunzip} = require('zlib') -const {pipeline} = require('stream') -var ffmpegPath = require("."); -var pkg = require("./package"); +import {statSync, createWriteStream, chmodSync} from 'node:fs' +import {arch as osArch, platform as osPlatform} from 'node:os' +import HttpsProxyAgent from 'https-proxy-agent' +import {encode as encodeQuery} from 'node:querystring' +import {strictEqual} from 'node:assert' +import envPaths from 'env-paths' +import httpBasic from '@derhuerst/http-basic' +const {FileCache} = httpBasic +import {extname} from 'node:path' +import ProgressBar from 'progress' +import request from '@derhuerst/http-basic' +import {createGunzip} from 'node:zlib' +import {pipeline} from 'node:stream' +import ffmpegPath from './index.js' + +import { createRequire } from 'node:module' +const require = createRequire(import.meta.url) +const pkg = require('./package.json') const exitOnError = (err) => { console.error(err) @@ -28,7 +33,7 @@ if (!ffmpegPath) { } try { - if (fs.statSync(ffmpegPath).isFile()) { + if (statSync(ffmpegPath).isFile()) { console.info('ffmpeg is installed already.') process.exit(0) } @@ -45,7 +50,6 @@ const proxyUrl = ( process.env.http_proxy ) if (proxyUrl) { - const HttpsProxyAgent = require('https-proxy-agent') const {hostname, port, protocol} = new URL(proxyUrl) agent = new HttpsProxyAgent({hostname, port, protocol}) } @@ -109,7 +113,7 @@ function downloadFile(url, destinationPath, progressCallback = noop) { return; } - const file = fs.createWriteStream(destinationPath); + const file = createWriteStream(destinationPath); const streams = isGzUrl(url) ? [response.body, createGunzip(), file] : [response.body, file] @@ -159,8 +163,8 @@ const releaseName = ( pkg['ffmpeg-static']['binary-release-name'] || release ) -const arch = process.env.npm_config_arch || os.arch() -const platform = process.env.npm_config_platform || os.platform() +const arch = process.env.npm_config_arch || osArch() +const platform = process.env.npm_config_platform || osPlatform() const baseUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}` const downloadUrl = `${baseUrl}/${platform}-${arch}.gz` @@ -169,7 +173,7 @@ const licenseUrl = `${baseUrl}/${platform}-${arch}.LICENSE` downloadFile(downloadUrl, ffmpegPath, onProgress) .then(() => { - fs.chmodSync(ffmpegPath, 0o755) // make executable + chmodSync(ffmpegPath, 0o755) // make executable }) .catch(exitOnError) diff --git a/package-lock.json b/package-lock.json index 25d69e3..14852ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,8 @@ "hasInstallScript": true, "license": "GPL-3.0-or-later", "dependencies": { - "@derhuerst/http-basic": "^8.2.0", - "env-paths": "^2.2.0", + "@derhuerst/http-basic": "^8.2.2", + "env-paths": "^3.0.0", "https-proxy-agent": "^5.0.0", "progress": "^2.0.3" }, @@ -24,9 +24,9 @@ } }, "node_modules/@derhuerst/http-basic": { - "version": "8.2.1", - "resolved": "https://registry.npmjs.org/@derhuerst/http-basic/-/http-basic-8.2.1.tgz", - "integrity": "sha512-Rmn7qQQulw2sxJ8qGfZ7OuqMWuhz8V+L5xnYKMF5cXVcYqmgWqlVEAme90pF7Ya8OVhxVxLmhh0rI2k6t7ITWw==", + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/@derhuerst/http-basic/-/http-basic-8.2.2.tgz", + "integrity": "sha512-ek065nW95mleLHo8vZ+ob7QTQPNOwDEjCe27BX2flme/UTu9z2mD1uRRPko38u7al4tTZADMtozpll8PQHAZgg==", "dependencies": { "caseless": "^0.12.0", "concat-stream": "^1.6.2", @@ -309,11 +309,14 @@ } }, "node_modules/env-paths": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", - "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-3.0.0.tgz", + "integrity": "sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==", "engines": { - "node": ">=6" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/eslint": { @@ -1078,9 +1081,9 @@ }, "dependencies": { "@derhuerst/http-basic": { - "version": "8.2.1", - "resolved": "https://registry.npmjs.org/@derhuerst/http-basic/-/http-basic-8.2.1.tgz", - "integrity": "sha512-Rmn7qQQulw2sxJ8qGfZ7OuqMWuhz8V+L5xnYKMF5cXVcYqmgWqlVEAme90pF7Ya8OVhxVxLmhh0rI2k6t7ITWw==", + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/@derhuerst/http-basic/-/http-basic-8.2.2.tgz", + "integrity": "sha512-ek065nW95mleLHo8vZ+ob7QTQPNOwDEjCe27BX2flme/UTu9z2mD1uRRPko38u7al4tTZADMtozpll8PQHAZgg==", "requires": { "caseless": "^0.12.0", "concat-stream": "^1.6.2", @@ -1301,9 +1304,9 @@ } }, "env-paths": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", - "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-3.0.0.tgz", + "integrity": "sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==" }, "eslint": { "version": "8.7.0", diff --git a/package.json b/package.json index f7e8129..64cf884 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "ffmpeg-static", "version": "4.4.1", "description": "ffmpeg static binaries for Mac OSX and Linux and Windows", + "type": "module", "main": "index.js", "files": [ "index.js", @@ -47,8 +48,8 @@ "node": ">=16" }, "dependencies": { - "@derhuerst/http-basic": "^8.2.0", - "env-paths": "^2.2.0", + "@derhuerst/http-basic": "^8.2.2", + "env-paths": "^3.0.0", "https-proxy-agent": "^5.0.0", "progress": "^2.0.3" }, diff --git a/test.js b/test.js index eac83df..db013b7 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,11 @@ 'use strict' -const {ok, strictEqual} = require('assert') -const {isAbsolute} = require('path') -const fs = require('fs') -const {spawnSync} = require('child_process') -const shell = require('any-shell-escape') -const ffmpegPath = require('.') +import {ok, strictEqual} from 'node:assert' +import {isAbsolute} from 'node:path' +import {statSync, accessSync, constants as fsConstants} from 'node:fs' +import {spawnSync} from 'node:child_process' +import shell from 'any-shell-escape' +import ffmpegPath from './index.js' console.info('TAP version 12') console.info('1..4') @@ -13,10 +13,10 @@ console.info('1..4') ok(isAbsolute(ffmpegPath)) console.info('ok 1 - ffmpeg path is absolute') -ok(fs.statSync(ffmpegPath).isFile(ffmpegPath)) +ok(statSync(ffmpegPath).isFile(ffmpegPath)) console.info(`ok 2 - ${ffmpegPath} is a file`) -fs.accessSync(ffmpegPath, fs.constants.X_OK) +accessSync(ffmpegPath, fsConstants.X_OK) console.info(`ok 3 - ${ffmpegPath} is executable`) const {status} = spawnSync(ffmpegPath, ['--help'], {