Skip to content

Commit

Permalink
feat: add template package and copying before publish
Browse files Browse the repository at this point in the history
  • Loading branch information
emilsivervik committed Sep 16, 2022
1 parent db80536 commit dfc9332
Show file tree
Hide file tree
Showing 9 changed files with 2,791 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.swp
.DS_Store
node_modules/
npm-debug.log

build/*
!build/index.sh

bin/*
13 changes: 13 additions & 0 deletions copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs');

(function(){
const package = process.argv.slice(2);
if(!package.length){ throw new Error('Package name undefined')}
fs.copyFileSync('install.js', `./${package}/install.js`);
fs.copyFileSync('index.js', `./${package}/index.js`);

const binaryPackage = require(`./${package}/_package.json`);
const templatePackage = require('./package.json');

fs.writeFileSync(`./${package}/package.json`, JSON.stringify({...templatePackage, ...binaryPackage}, null, 2))
}());
5 changes: 5 additions & 0 deletions ffmpeg-static/_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "ffmpeg-static",
"description": "ffmpeg static binaries for Mac OSX, Linux and Windows",
"binary": "ffmpeg"
}
31 changes: 31 additions & 0 deletions ffmpeg-static/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

var pkg = require("./package");

if (process.env.FFMPEG_BIN || process.env.FFPROBE_BIN) {
module.exports = process.env.FFMPEG_BIN || process.env.FFPROBE_BIN
} else {
var os = require('os')
var path = require('path')

var 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()

var binaryPath = path.join(
__dirname,
platform === 'win32' ? `${pkg.binary}.exe` : pkg.binary
)

if (!binaries[platform] || binaries[platform].indexOf(arch) === -1) {
binaryPath = null
}

module.exports = binaryPath
}
185 changes: 185 additions & 0 deletions ffmpeg-static/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
'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 binaryPath = require(".");
var pkg = require("./package");

const exitOnError = (err) => {
console.error(err)
process.exit(1)
}
const exitOnErrorOrWarnWith = (msg) => (err) => {
if (err.statusCode === 404) console.warn(msg)
else exitOnError(err)
}

if (!binaryPath) {
exitOnError(`${pkg.name} install failed: No binary found for architecture`)
}

try {
if (fs.statSync(binaryPath).isFile()) {
console.info(`${pkg.binary} is installed already.`)
process.exit(0)
}
} catch (err) {
if (err && err.code !== 'ENOENT') exitOnError(err)
}

let agent = false
// https://github.com/request/request/blob/a9557c9e7de2c57d92d9bab68a416a87d255cd3d/lib/getProxyFromURI.js#L66-L71
const proxyUrl = (
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
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})
}

// https://advancedweb.hu/how-s3-signed-urls-work/
const normalizeS3Url = (url) => {
url = new URL(url)
if (url.hostname.slice(-17) !== '.s3.amazonaws.com') return url.href
const query = Array.from(url.searchParams.entries())
.filter(([key]) => key.slice(0, 6).toLowerCase() !== 'x-amz-')
.reduce((query, [key, val]) => ({...query, [key]: val}), {})
url.search = encodeQuery(query)
return url.href
}
strictEqual(
normalizeS3Url('https://example.org/foo?bar'),
'https://example.org/foo?bar'
)
strictEqual(
normalizeS3Url('https://github-production-release-asset-2e65be.s3.amazonaws.com/29458513/26341680-4231-11ea-8e36-ae454621d74a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200405%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200405T225358Z&X-Amz-Expires=300&X-Amz-Signature=d6415097af04cf62ea9b69d3c1a421278e96bcb069afa48cf021ec3b6941bae4&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Ddarwin-x64&response-content-type=application%2Foctet-stream'),
'https://github-production-release-asset-2e65be.s3.amazonaws.com/29458513/26341680-4231-11ea-8e36-ae454621d74a?actor_id=0&response-content-disposition=attachment%3B%20filename%3Ddarwin-x64&response-content-type=application%2Foctet-stream'
)

const cache = new FileCache(envPaths(pkg.name).cache)
cache.getCacheKey = (url) => {
return FileCache.prototype.getCacheKey(normalizeS3Url(url))
}

const isGzUrl = (url) => {
const path = new URL(url).pathname.split('/')
const filename = path[path.length - 1]
return filename && extname(filename) === '.gz'
}

const noop = () => {}
function downloadFile(url, destinationPath, progressCallback = noop) {
let fulfill, reject;
let totalBytes = 0;

const promise = new Promise((x, y) => {
fulfill = x;
reject = y;
});

request('GET', url, {
agent,
followRedirects: true,
maxRedirects: 3,
gzip: true,
cache,
timeout: 30 * 1000, // 30s
retry: true,
}, (err, response) => {
if (err || response.statusCode !== 200) {
err = err || new Error('Download failed.')
if (response) {
err.url = response.url
err.statusCode = response.statusCode
}
reject(err)
return;
}

const file = fs.createWriteStream(destinationPath);
const streams = isGzUrl(url)
? [response.body, createGunzip(), file]
: [response.body, file]
pipeline(
...streams,
(err) => {
if (err) {
err.url = response.url
err.statusCode = response.statusCode
reject(err)
} else fulfill()
}
)

if (!response.fromCache && progressCallback) {
const cLength = response.headers["content-length"]
totalBytes = cLength ? parseInt(cLength, 10) : null
response.body.on('data', (chunk) => {
progressCallback(chunk.length, totalBytes);
});
}
});

return promise;
}

let progressBar = null;
function onProgress(deltaBytes, totalBytes) {
if (process.env.CI) return;
if (totalBytes === null) return;
if (!progressBar) {
progressBar = new ProgressBar(`Downloading ${pkg.binary} ${releaseName} [:bar] :percent :etas `, {
complete: "|",
incomplete: " ",
width: 20,
total: totalBytes
});
}

progressBar.tick(deltaBytes);
}

const release = (
process.env.BINARY_RELEASE ||
pkg['binary-release']['tag']
)
const releaseName = (
pkg['binary-release']['name'] ||
release
)
const arch = process.env.npm_config_arch || os.arch()
const platform = process.env.npm_config_platform || os.platform()
const downloadsUrl = (
process.env.BINARIES_URL ||
`https://github.com/eugeneware/${pkg.name}/releases/download`
)
const baseUrl = `${downloadsUrl}/${release}`
const downloadUrl = `${baseUrl}/${pkg.binary
}-${platform}-${arch}.gz`
const readmeUrl = `${baseUrl}/${platform}-${arch}.README`
const licenseUrl = `${baseUrl}/${platform}-${arch}.LICENSE`

downloadFile(downloadUrl, binaryPath, onProgress)
.then(() => {
fs.chmodSync(binaryPath, 0o755) // make executable
})
.catch(exitOnError)

.then(() => downloadFile(readmeUrl, `${binaryPath}.README`))
.catch(exitOnErrorOrWarnWith(`Failed to download the ${pkg.binary} README.`))

.then(() => downloadFile(licenseUrl, `${binaryPath}.LICENSE`))
.catch(exitOnErrorOrWarnWith(`Failed to download the ${pkg.binary} LICENSE.`))
14 changes: 7 additions & 7 deletions ffmpeg-static/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ffmpeg-static",
"version": "5.1.0",
"description": "ffmpeg static binaries for Mac OSX and Linux and Windows",
"description": "ffmpeg static binaries for Mac OSX, Linux and Windows",
"main": "index.js",
"files": [
"index.js",
Expand All @@ -14,11 +14,11 @@
"install": "node install.js",
"test": "node test.js",
"lint": "eslint .",
"prepublishOnly": "npm run lint && npm run install && npm test"
"prepublishOnly": "(cd .. && node copy.js ffmpeg-static) && npm run lint && npm run install && npm test"
},
"ffmpeg-static": {
"binary-release-tag": "b5.0.1",
"binary-release-name": "5.0.1"
"binary-release": {
"tag": "b5.0.1",
"name": "5.0.1"
},
"binary": "ffmpeg",
"repository": {
Expand Down Expand Up @@ -57,6 +57,6 @@
},
"devDependencies": {
"any-shell-escape": "^0.1.1",
"eslint": "^8.6.0"
"eslint": "^8.23.1"
}
}
}
6 changes: 3 additions & 3 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ function onProgress(deltaBytes, totalBytes) {

const release = (
process.env.BINARY_RELEASE ||
pkg[pkg.name]['binary-release-tag']
pkg['binary-release']['tag']
)
const releaseName = (
pkg[pkg.name]['binary-release-name'] ||
release
pkg['binary-release']['name'] ||
release
)
const arch = process.env.npm_config_arch || os.arch()
const platform = process.env.npm_config_platform || os.platform()
Expand Down
Loading

0 comments on commit dfc9332

Please sign in to comment.