@mashroom-content/mashroom-content-asset-processing
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.writeCacheEntry = exports.readCacheEntry = void 0; | ||
var _fsExtra = require("fs-extra"); | ||
var _fs = require("fs"); | ||
const writeCacheEntry = async (cacheFilePath, asset) => { | ||
@@ -19,10 +16,5 @@ (0, _fsExtra.writeJSONSync)(`${cacheFilePath}_meta.json`, asset.meta); | ||
}; | ||
exports.writeCacheEntry = writeCacheEntry; | ||
const readCacheEntry = async (cacheFilePath, cacheDefaultTTLSec, logger) => { | ||
var _metaFromCacheEntry$e; | ||
let cacheFileStat; | ||
try { | ||
@@ -33,9 +25,8 @@ cacheFileStat = (0, _fs.statSync)(cacheFilePath); | ||
} | ||
const metaFromCacheEntry = (0, _fsExtra.readJSONSync)(`${cacheFilePath}_meta.json`); | ||
const expires = (_metaFromCacheEntry$e = metaFromCacheEntry.expires) !== null && _metaFromCacheEntry$e !== void 0 ? _metaFromCacheEntry$e : cacheFileStat.mtimeMs + cacheDefaultTTLSec * 1000; | ||
const expires = metaFromCacheEntry.expires ?? cacheFileStat.mtimeMs + cacheDefaultTTLSec * 1000; | ||
if (expires > Date.now()) { | ||
logger.debug('Loading asset from cache:', cacheFilePath); | ||
const meta = { ...metaFromCacheEntry, | ||
const meta = { | ||
...metaFromCacheEntry, | ||
size: cacheFileStat.size | ||
@@ -49,6 +40,4 @@ }; | ||
} | ||
return undefined; | ||
}; | ||
exports.readCacheEntry = readCacheEntry; |
@@ -7,13 +7,8 @@ "use strict"; | ||
exports.default = void 0; | ||
var _fs = require("fs"); | ||
var _path = require("path"); | ||
var _mimeTypes = require("mime-types"); | ||
var _default = async fileUri => { | ||
const filePath = fileUri.replace('file://', ''); | ||
let fileStats; | ||
try { | ||
@@ -24,3 +19,2 @@ fileStats = (0, _fs.statSync)(filePath); | ||
} | ||
const stream = (0, _fs.createReadStream)(filePath); | ||
@@ -38,3 +32,2 @@ const mimeType = (0, _mimeTypes.lookup)(filePath) || ''; | ||
}; | ||
exports.default = _default; |
@@ -7,21 +7,12 @@ "use strict"; | ||
exports.default = void 0; | ||
var _stream = require("stream"); | ||
var _path = require("path"); | ||
var _url = require("url"); | ||
var _nodeFetch = _interopRequireDefault(require("node-fetch")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _default = async httpUri => { | ||
const response = await (0, _nodeFetch.default)(httpUri); | ||
if (!response.ok) { | ||
throw new Error(`Http asset not found: ${httpUri}`); | ||
} | ||
const stream = new _stream.Readable().wrap(response.body); | ||
const stream = response.body; | ||
const mimeType = response.headers.get('content-type') || ''; | ||
@@ -34,35 +25,28 @@ const sizeHeader = response.headers.get('content-length'); | ||
let filename; | ||
if (contentDispositionHeader) { | ||
var _contentDispositionHe; | ||
filename = (_contentDispositionHe = contentDispositionHeader.split(';').find(s => s.trim().startsWith('filename='))) === null || _contentDispositionHe === void 0 ? void 0 : _contentDispositionHe.split('=')[1].replace(/"/g, ''); | ||
} | ||
if (!filename) { | ||
filename = (0, _path.basename)(new _url.URL(httpUri).pathname); | ||
} | ||
let expires; | ||
if (expiresHeader) { | ||
try { | ||
expires = new Date(expiresHeader).getTime(); | ||
} catch (e) {// Ignore | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} | ||
if (!expires && cacheControlHeader) { | ||
try { | ||
var _cacheControlHeader$s; | ||
const maxAgeStr = (_cacheControlHeader$s = cacheControlHeader.split(',').find(s => s.trim().startsWith('max-age='))) === null || _cacheControlHeader$s === void 0 ? void 0 : _cacheControlHeader$s.split('=')[1]; | ||
if (maxAgeStr) { | ||
expires = Date.now() + parseInt(maxAgeStr) * 1000; | ||
} | ||
} catch (e) {// Ignore | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} | ||
return { | ||
@@ -78,3 +62,2 @@ stream, | ||
}; | ||
exports.default = _default; |
"use strict"; | ||
var _worker_threads = require("worker_threads"); | ||
var _mimeTypes = require("mime-types"); | ||
var _fetchHttpAsset = _interopRequireDefault(require("./fetchHttpAsset")); | ||
var _fetchFileAsset = _interopRequireDefault(require("./fetchFileAsset")); | ||
var _processAsset = _interopRequireDefault(require("./processAsset")); | ||
var _cache = require("./cache"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const params = _worker_threads.workerData; | ||
const processAssetAndPutToCache = async () => { | ||
try { | ||
var _params$convert; | ||
// Read asset | ||
let asset; | ||
if (params.assetUri.startsWith('http://') || params.assetUri.startsWith('https://')) { | ||
@@ -30,9 +20,10 @@ asset = await (0, _fetchHttpAsset.default)(params.assetUri); | ||
asset = await (0, _fetchFileAsset.default)(params.assetUri); | ||
} // Process | ||
} | ||
// Process | ||
const newMimeType = (_params$convert = params.convert) !== null && _params$convert !== void 0 && _params$convert.format ? (0, _mimeTypes.lookup)(params.convert.format) || '' : asset.meta.mimeType; | ||
asset = { | ||
stream: await (0, _processAsset.default)(asset.stream, params.defaultQuality, params.scaleUp, params.resize, params.convert), | ||
meta: { ...asset.meta, | ||
meta: { | ||
...asset.meta, | ||
mimeType: newMimeType, | ||
@@ -54,3 +45,2 @@ size: undefined | ||
}; | ||
processAssetAndPutToCache(); |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.default = void 0; | ||
var _MashroomContentAssetProcServiceImpl = _interopRequireDefault(require("./MashroomContentAssetProcServiceImpl")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const bootstrap = async (pluginName, pluginConfig, pluginContextHolder) => { | ||
@@ -26,4 +23,3 @@ const { | ||
}; | ||
var _default = bootstrap; | ||
exports.default = _default; |
@@ -7,27 +7,15 @@ "use strict"; | ||
exports.default = void 0; | ||
var _path = require("path"); | ||
var _crypto = require("crypto"); | ||
var _worker_threads = require("worker_threads"); | ||
var _fsExtra = require("fs-extra"); | ||
var _mimeTypes = require("mime-types"); | ||
var _fetchFileAsset = _interopRequireDefault(require("./fetchFileAsset")); | ||
var _fetchHttpAsset = _interopRequireDefault(require("./fetchHttpAsset")); | ||
var _processAsset = _interopRequireDefault(require("./processAsset")); | ||
var _cache = require("./cache"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class MashroomContentAssetProcServiceImpl { | ||
_cacheFolder = '/tmp'; | ||
_currentlyProcessedAssets = {}; | ||
constructor(scaleUp, defaultQuality, cacheEnable, cacheDefaultTTLSec, cacheFolder, pluginContextHolder) { | ||
@@ -43,3 +31,2 @@ this.scaleUp = scaleUp; | ||
this._logger = loggerFactory('mashroom.content.assets'); | ||
if (this.cacheEnable) { | ||
@@ -51,26 +38,20 @@ if ((0, _path.isAbsolute)(cacheFolder)) { | ||
} | ||
(0, _fsExtra.ensureDirSync)(this._cacheFolder); | ||
this._logger.info('Using asset cache folder:', this._cacheFolder); | ||
} | ||
} | ||
async processAssetFromUri(assetUri, resize, convert) { | ||
const cacheFilePath = (0, _path.resolve)(this._cacheFolder, this._getCacheKey(assetUri, resize, convert)); | ||
const assetHash = Buffer.from(cacheFilePath).toString('base64'); | ||
if (this.cacheEnable) { | ||
try { | ||
const cachedAsset = await (0, _cache.readCacheEntry)(cacheFilePath, this.cacheDefaultTTLSec, this._logger); | ||
if (cachedAsset) { | ||
return cachedAsset; | ||
} | ||
} catch (e) {// Ignore | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} | ||
let asset; | ||
if (assetUri.startsWith('http://') || assetUri.startsWith('https://')) { | ||
@@ -80,5 +61,5 @@ asset = await (0, _fetchHttpAsset.default)(assetUri); | ||
asset = await (0, _fetchFileAsset.default)(assetUri); | ||
} // Image processing | ||
} | ||
// Image processing | ||
if (asset.meta.mimeType.startsWith('image/') && (convert || resize)) { | ||
@@ -91,3 +72,4 @@ if (!this.cacheEnable) { | ||
stream: await this.processAsset(asset.stream, resize, convert), | ||
meta: { ...asset.meta, | ||
meta: { | ||
...asset.meta, | ||
mimeType: newMimeType, | ||
@@ -112,3 +94,4 @@ size: undefined | ||
}; | ||
const worker = new _worker_threads.Worker((0, _path.resolve)(__dirname, 'assetProcessingWorker'), { | ||
this._logger.debug(`Converting image ${assetUri}`, resize, convert); | ||
const worker = new _worker_threads.Worker((0, _path.resolve)(__dirname, 'imageProcessingWorker'), { | ||
workerData | ||
@@ -118,3 +101,3 @@ }); | ||
delete this._currentlyProcessedAssets[assetHash]; | ||
this._logger.debug(`Image converted: ${assetUri}`); | ||
if (!message.success) { | ||
@@ -130,20 +113,17 @@ this._logger.error('Image processing failed!', message.message, message.stack); | ||
} | ||
return asset; | ||
} | ||
async processAsset(asset, resize, convert) { | ||
return (0, _processAsset.default)(asset, this.defaultQuality, this.scaleUp, resize, convert); | ||
} | ||
_getCacheKey(assetUri, resize, convert) { | ||
const resizeJson = resize ? JSON.stringify({ ...resize | ||
const resizeJson = resize ? JSON.stringify({ | ||
...resize | ||
}) : ''; | ||
const convertJson = convert ? JSON.stringify({ ...convert | ||
const convertJson = convert ? JSON.stringify({ | ||
...convert | ||
}) : ''; | ||
return (0, _crypto.createHash)('sha256').update(`${assetUri}_${resizeJson}_${convertJson}`).digest('base64').replace(/[/=+]/g, '__'); | ||
} | ||
} | ||
exports.default = MashroomContentAssetProcServiceImpl; |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.default = void 0; | ||
var _sharp = _interopRequireDefault(require("sharp")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _default = async (asset, defaultQuality, scaleUp, resize, convert) => { | ||
@@ -17,5 +14,3 @@ if (!(resize !== null && resize !== void 0 && resize.width) && !(resize !== null && resize !== void 0 && resize.height) && !(convert !== null && convert !== void 0 && convert.format)) { | ||
} | ||
const procs = []; | ||
if (convert !== null && convert !== void 0 && convert.format) { | ||
@@ -30,3 +25,2 @@ const { | ||
} | ||
if (resize !== null && resize !== void 0 && resize.width || resize !== null && resize !== void 0 && resize.height) { | ||
@@ -43,6 +37,4 @@ const { | ||
} | ||
return procs.reduce((stream, proc) => stream.pipe(proc), asset); | ||
}; | ||
exports.default = _default; |
{ | ||
"name": "@mashroom-content/mashroom-content-asset-processing", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"description": "Mashroom Content Asset services", | ||
@@ -14,6 +14,6 @@ "homepage": "https://www.mashroom-server.com", | ||
"dependencies": { | ||
"fs-extra": "^10.0.0", | ||
"mime-types": "^2.1.34", | ||
"node-fetch": "^2.6.6", | ||
"sharp": "^0.29.3" | ||
"fs-extra": "^10.1.0", | ||
"mime-types": "^2.1.35", | ||
"node-fetch": "^2.6.7", | ||
"sharp": "^0.31.2" | ||
}, | ||
@@ -23,5 +23,5 @@ "devDependencies": { | ||
"@types/mime-types": "^2.1.1", | ||
"@types/node-fetch": "^2.5.12", | ||
"@types/sharp": "^0.29.5", | ||
"image-size": "^1.0.0" | ||
"@types/node-fetch": "^2.6.2", | ||
"@types/sharp": "^0.31.0", | ||
"image-size": "^1.0.2" | ||
}, | ||
@@ -28,0 +28,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
18249
12
389
+ Addednode-addon-api@5.1.0(transitive)
+ Addedsharp@0.31.3(transitive)
- Removeddetect-libc@1.0.3(transitive)
- Removednode-addon-api@4.3.0(transitive)
- Removedsharp@0.29.3(transitive)
Updatedfs-extra@^10.1.0
Updatedmime-types@^2.1.35
Updatednode-fetch@^2.6.7
Updatedsharp@^0.31.2