@obsidize/tar-browserify
Version:
Browser-based tar utility for packing and unpacking tar files (stream-capable)
62 lines (61 loc) • 2.93 kB
JavaScript
import { Constants } from '../common/constants';
import { TarUtility } from '../common/tar-utility';
import { UstarHeaderField } from './ustar/ustar-header-field';
import { UstarHeaderLinkIndicatorType } from './ustar/ustar-header-link-indicator-type';
export var TarHeaderUtility;
(function (TarHeaderUtility) {
TarHeaderUtility.CHECKSUM_SEED_STRING = ''.padStart(UstarHeaderField.headerChecksum.size, ' ');
TarHeaderUtility.CHECKSUM_SEED = TarUtility.generateChecksum(TarUtility.encodeString(TarHeaderUtility.CHECKSUM_SEED_STRING));
TarHeaderUtility.ALL_FIELDS = UstarHeaderField.all();
TarHeaderUtility.CHECKSUM_FIELDS = UstarHeaderField.checksumSet();
function isUstarSector(input, offset) {
return UstarHeaderField.ustarIndicator.sliceString(input, offset).startsWith(Constants.USTAR_TAG);
}
TarHeaderUtility.isUstarSector = isUstarSector;
function isTarHeaderLinkIndicatorTypeDirectory(type) {
return type === UstarHeaderLinkIndicatorType.DIRECTORY;
}
TarHeaderUtility.isTarHeaderLinkIndicatorTypeDirectory = isTarHeaderLinkIndicatorTypeDirectory;
function isTarHeaderLinkIndicatorTypeFile(type) {
switch (type) {
case UstarHeaderLinkIndicatorType.NORMAL_FILE:
case UstarHeaderLinkIndicatorType.NORMAL_FILE_ALT1:
case UstarHeaderLinkIndicatorType.NORMAL_FILE_ALT2:
case UstarHeaderLinkIndicatorType.CONTIGUOUS_FILE:
return true;
default:
return false;
}
}
TarHeaderUtility.isTarHeaderLinkIndicatorTypeFile = isTarHeaderLinkIndicatorTypeFile;
function isTarHeaderLinkIndicatorTypePax(type) {
switch (type) {
case UstarHeaderLinkIndicatorType.LOCAL_EXTENDED_HEADER:
case UstarHeaderLinkIndicatorType.GLOBAL_EXTENDED_HEADER:
return true;
default:
return false;
}
}
TarHeaderUtility.isTarHeaderLinkIndicatorTypePax = isTarHeaderLinkIndicatorTypePax;
/**
* Searches the given input buffer for a USTAR header tar sector, starting at the given offset.
* Returns -1 if no valid header sector is found.
*/
function findNextUstarSectorOffset(input, offset = 0) {
const NOT_FOUND = -1;
if (!TarUtility.isUint8Array(input)) {
return NOT_FOUND;
}
const maxOffset = input.byteLength;
let nextOffset = Math.max(0, offset);
while (nextOffset < maxOffset && !isUstarSector(input, nextOffset)) {
nextOffset = TarUtility.advanceSectorOffset(nextOffset, maxOffset);
}
if (nextOffset < maxOffset) {
return nextOffset;
}
return NOT_FOUND;
}
TarHeaderUtility.findNextUstarSectorOffset = findNextUstarSectorOffset;
})(TarHeaderUtility || (TarHeaderUtility = {}));