@shockpkg/core
Version:
shockpkg core
149 lines (134 loc) • 2.77 kB
JavaScript
import { createInflateRaw as zlibCreateInflateRaw } from 'node:zlib';
/**
* Package object.
*/
export class Package {
/**
* Package name.
*/
/**
* File name.
*/
/**
* File size.
*/
/**
* SHA256 hash of file.
*/
/**
* SHA1 hash of file.
*/
/**
* MD5 hash of file.
*/
/**
* Source path, URL for root, file path for child packages.
*/
/**
* Zipped info if a child package or null if a root package.
*/
/**
* Child packages.
*/
/**
* The parent package this package is found in.
*/
/**
* Package constructor.
*
* @param info Package info.
* @param parent Package parent.
*/
constructor(info, parent = null) {
const {
zipped
} = info;
if (parent && !zipped) {
throw new Error(`Missing zipped info: ${info.name}`);
} else if (!parent && zipped) {
throw new Error(`Unexpected zipped info: ${info.name}`);
}
this.name = info.name;
this.file = info.file;
this.size = info.size;
this.sha256 = info.sha256;
this.sha1 = info.sha1;
this.md5 = info.md5;
this.source = info.source;
this.zipped = zipped || null;
this.parent = parent;
this.packages = this._createPackages(info.packages);
}
/**
* Get zipped compression method.
*
* @returns Compression method.
*/
getZippedCompression() {
const {
zipped
} = this;
if (!zipped) {
throw new Error('Not a child package');
}
return +zipped.split('-')[0];
}
/**
* Get zipped data slice.
*
* @returns Data start and size.
*/
getZippedSlice() {
const {
zipped
} = this;
if (!zipped) {
throw new Error('Not a child package');
}
const parts = zipped.split('-');
return [+parts[1], +parts[2]];
}
/**
* Get zipped data decompressor.
*
* @returns Transform stream or null if entry not compressed.
*/
getZippedDecompressor() {
const method = this.getZippedCompression();
switch (method) {
case 0:
{
return null;
}
case 8:
{
return zlibCreateInflateRaw();
}
default:
{
// Do nothing.
}
}
throw new Error(`Unsupported zipped compression: ${method}`);
}
/**
* Create child packages list.
*
* @param infos Package infos.
* @returns Package instance.
*/
_createPackages(infos = []) {
return infos.map(info => this._createPackage(info));
}
/**
* Create a child package.
*
* @param info Package info.
* @returns Package instance.
*/
_createPackage(info) {
const Constructor = this.constructor;
return new Constructor(info, this);
}
}
//# sourceMappingURL=package.mjs.map