tar
Version:
tar for JavaScript
83 lines • 3.25 kB
JavaScript
;
// Text and byte helpers that work on a plain Uint8Array in any
// JavaScript runtime, and type checks for the input of a caller.
//
// A check with `instanceof` is false for an object that a different
// realm made, such as a Node.js `Buffer` in a `node:vm` context, or a
// `Blob` of a different window. These checks are true for an object
// of each realm.
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeAscii = exports.encodeUtf8Into = exports.decodeUtf8 = exports.utf8Length = exports.isReadableStream = exports.isBlob = exports.isArrayBuffer = exports.isUint8Array = void 0;
/** True for a `Uint8Array`, and for a subclass such as `Buffer`. */
const isUint8Array = (x) => ArrayBuffer.isView(x) &&
x[Symbol.toStringTag] === 'Uint8Array';
exports.isUint8Array = isUint8Array;
/** True for an `ArrayBuffer`. */
const isArrayBuffer = (x) => Object.prototype.toString.call(x) === '[object ArrayBuffer]';
exports.isArrayBuffer = isArrayBuffer;
/** True for an object that has the size and the stream of a `Blob`. */
const isBlob = (x) => typeof x?.size === 'number' &&
typeof x.stream === 'function';
exports.isBlob = isBlob;
/** True for an object that has the reader of a `ReadableStream`. */
const isReadableStream = (x) => typeof x?.getReader === 'function';
exports.isReadableStream = isReadableStream;
// A name can start with U+FEFF. The decoder must keep it.
const decoder = new TextDecoder('utf-8', { ignoreBOM: true });
const encoder = new TextEncoder();
/** The number of bytes in the UTF-8 form of the string. */
const utf8Length = (str) => {
let len = str.length;
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i);
if (c < 0x80)
continue;
if (c < 0x800) {
len += 1;
}
else if (c >= 0xd800 &&
c <= 0xdbff &&
(str.charCodeAt(i + 1) & 0xfc00) === 0xdc00) {
// A surrogate pair is 2 code units, and 4 bytes.
len += 2;
i++;
}
else {
len += 2;
}
}
return len;
};
exports.utf8Length = utf8Length;
/** Decode the bytes from `start` to `end` as UTF-8. */
const decodeUtf8 = (buf, start = 0, end = buf.length) => {
// Most names in an archive are short ASCII. That case needs no
// decoder call.
let str = '';
for (let i = start; i < end; i++) {
const c = buf[i];
if (c >= 0x80)
return decoder.decode(buf.subarray(start, end));
str += String.fromCharCode(c);
}
return str;
};
exports.decodeUtf8 = decodeUtf8;
/**
* Write the string as UTF-8 into `size` bytes at `off`. Fill the bytes
* that are left with zeros. A character that does not fit completely
* is not written.
*/
const encodeUtf8Into = (str, buf, off, size) => {
const { written } = encoder.encodeInto(str, buf.subarray(off, off + size));
buf.fill(0, off + written, off + size);
};
exports.encodeUtf8Into = encodeUtf8Into;
/** Write a string of ASCII characters at `off`. */
const writeAscii = (str, buf, off) => {
for (let i = 0; i < str.length; i++) {
buf[off + i] = str.charCodeAt(i);
}
};
exports.writeAscii = writeAscii;
//# sourceMappingURL=bytes.js.map