54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const zlib = require('zlib');
|
|
|
|
const { sha256sum } = require('./checksum');
|
|
const logger = require('./logger');
|
|
|
|
module.exports = {};
|
|
/**
|
|
* This cache is to ensure that we're not processing files found in LAYERS_DIR
|
|
* and is intended to hold a key-value mapping from a layer `key` to a `Layer`
|
|
* class
|
|
*
|
|
*/
|
|
module.exports.LAYERS_CHECKSUM_CACHE = {};
|
|
|
|
class Layer {
|
|
constructor(org, image, tag, filePath) {
|
|
this.filePath = filePath;
|
|
this.key = Layer.keyFor(org, image, tag, path.basename(filePath));
|
|
this.digest = null;
|
|
this.tarDigest = null;
|
|
}
|
|
|
|
static keyFor(org, image, tag, filename) {
|
|
return `${org}/${image}:${tag}|${filename}`;
|
|
}
|
|
|
|
/**
|
|
* The process function will ensure that the file has been considered and
|
|
* checksums have been created
|
|
*/
|
|
process() {
|
|
logger.info(`Processing ${this} with ${this.filePath}`);
|
|
|
|
if ((this.digest == null) || (this.tarDigest == null)) {
|
|
// We'll need the contents of the layer for checksumming and gunziping
|
|
const layerContents = fs.readFileSync(this.filePath);
|
|
|
|
logger.debug(`Computing digest for ${this}`);
|
|
this.digest = sha256sum(layerContents);
|
|
|
|
logger.debug(`Computing tarDigest for ${this}`);
|
|
this.tarDigest = sha256sum(zlib.gunzipSync(layerContents));
|
|
}
|
|
}
|
|
|
|
toString() {
|
|
return `Layer(${this.key})`;
|
|
}
|
|
}
|
|
|
|
module.exports.Layer = Layer;
|