Change to make Sharp an optional dependency

This commit is contained in:
Christian Bundy 2019-12-27 16:28:36 -08:00
parent 96636685ae
commit 42c72e7b8d
No known key found for this signature in database
GPG Key ID: EB541AAEF4366237
2 changed files with 53 additions and 31 deletions

View File

@ -1,9 +1,21 @@
{
"name": "@fraction/oasis",
"version": "2.9.7",
"main": "index.js",
"author": "Christian Bundy <christianbundy@fraction.io>",
"description": "friendly neighborhood scuttlebutt interface",
"repository": "git@github.com:fraction/oasis.git",
"license": "AGPL-3.0",
"author": "Christian Bundy <christianbundy@fraction.io>",
"main": "index.js",
"bin": {
"oasis": "./index.js"
},
"scripts": {
"dev": "nodemon --inspect index.js --debug --no-open",
"fix": "standard --fix && stylelint --fix src/assets/*.css",
"start": "node index.js",
"test": "standard && dependency-check ./package.json --unused --no-dev --ignore-module highlight.js --ignore-module @fraction/base16-css && cspell --no-summary '**/*.{js,md}' && stylelint src/assets/*.css && tsc --allowJs --resolveJsonModule --lib dom --checkJs --noEmit --skipLibCheck index.js",
"preversion": "npm test"
},
"dependencies": {
"@fraction/base16-css": "^1.1.0",
"@fraction/flotilla": "3.0.0",
@ -22,7 +34,6 @@
"pull-sort": "^1.0.2",
"pull-stream": "^3.6.12",
"require-style": "^1.1.0",
"sharp": "^0.23.0",
"ssb-client": "^4.7.7",
"ssb-config": "^3.4.3",
"ssb-markdown": "^6.0.0",
@ -32,11 +43,6 @@
"ssb-thread-schema": "^1.1.1",
"yargs": "^15.0.0"
},
"bin": {
"oasis": "./index.js"
},
"description": "friendly neighborhood scuttlebutt interface",
"repository": "git@github.com:fraction/oasis.git",
"devDependencies": {
"cspell": "^4.0.33",
"dependency-check": "^4.1.0",
@ -47,11 +53,7 @@
"stylelint-config-standard": "^19.0.0",
"typescript": "^3.7.2"
},
"scripts": {
"start": "node index.js",
"dev": "nodemon --inspect index.js --debug --no-open",
"fix": "standard --fix && stylelint --fix src/assets/*.css",
"test": "standard && dependency-check ./package.json --unused --no-dev --ignore-module highlight.js --ignore-module @fraction/base16-css && cspell --no-summary '**/*.{js,md}' && stylelint src/assets/*.css && tsc --allowJs --resolveJsonModule --lib dom --checkJs --noEmit --skipLibCheck index.js",
"preversion": "npm test"
"optionalDependencies": {
"sharp": "^0.23.0"
}
}

View File

@ -1,21 +1,36 @@
'use strict'
let sharp
const pull = require('pull-stream')
const sharp = require('sharp')
const debug = require('debug')('oasis')
const blob = require('./models/blob')
try {
sharp = require('sharp')
} catch (e) {
// Optional dependency
}
const fakePixel =
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'base64'
)
const fakeImage = (imageSize) =>
sharp({
create: {
width: imageSize,
height: imageSize,
channels: 4,
background: {
r: 0, g: 0, b: 0, alpha: 0.5
sharp
? sharp({
create: {
width: imageSize,
height: imageSize,
channels: 4,
background: {
r: 0, g: 0, b: 0, alpha: 0.5
}
}
}
}).png().toBuffer()
})
: new Promise((resolve) => resolve(fakePixel))
const fakeId = '&0000000000000000000000000000000000000000000=.sha256'
@ -39,13 +54,18 @@ module.exports = async function imagePage ({ blobId, imageSize }) {
resolve(result)
} else {
const buffer = Buffer.concat(bufferArray)
sharp(buffer)
.resize(imageSize, imageSize)
.png()
.toBuffer()
.then((data) => {
resolve(data)
})
if (sharp) {
sharp(buffer)
.resize(imageSize, imageSize)
.png()
.toBuffer()
.then((data) => {
resolve(data)
})
} else {
resolve(buffer)
}
}
})
)