Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node 16+, CommonJS -> ESM #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"extends": "eslint:recommended",
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020,
"sourceType": "module"
},
"globals": {
"Atomics": "readonly",
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

Expand Down
8 changes: 4 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
@@ -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')) {
Expand Down
34 changes: 18 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
42 changes: 23 additions & 19 deletions install.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
Expand All @@ -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})
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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`
Expand All @@ -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)

Expand Down
33 changes: 18 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
16 changes: 8 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'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')

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'], {
Expand Down