mirror of
https://github.com/codedread/bitjs
synced 2025-10-06 02:39:55 +02:00
Start the move to es6: Change all vars to lets/consts
This commit is contained in:
parent
87cdd92981
commit
639a23e69b
11 changed files with 514 additions and 555 deletions
|
@ -15,21 +15,21 @@ importScripts('../io/bytestream.js');
|
|||
importScripts('archive.js');
|
||||
|
||||
// Progress variables.
|
||||
var currentFilename = "";
|
||||
var currentFileNumber = 0;
|
||||
var currentBytesUnarchivedInFile = 0;
|
||||
var currentBytesUnarchived = 0;
|
||||
var totalUncompressedBytesInArchive = 0;
|
||||
var totalFilesInArchive = 0;
|
||||
let currentFilename = "";
|
||||
let currentFileNumber = 0;
|
||||
let currentBytesUnarchivedInFile = 0;
|
||||
let currentBytesUnarchived = 0;
|
||||
let totalUncompressedBytesInArchive = 0;
|
||||
let totalFilesInArchive = 0;
|
||||
|
||||
// Helper functions.
|
||||
var info = function(str) {
|
||||
const info = function(str) {
|
||||
postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
|
||||
};
|
||||
var err = function(str) {
|
||||
const err = function(str) {
|
||||
postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
|
||||
};
|
||||
var postProgress = function() {
|
||||
const postProgress = function() {
|
||||
postMessage(new bitjs.archive.UnarchiveProgressEvent(
|
||||
currentFilename,
|
||||
currentFileNumber,
|
||||
|
@ -40,14 +40,14 @@ var postProgress = function() {
|
|||
};
|
||||
|
||||
// Removes all characters from the first zero-byte in the string onwards.
|
||||
var readCleanString = function(bstr, numBytes) {
|
||||
var str = bstr.readString(numBytes);
|
||||
var zIndex = str.indexOf(String.fromCharCode(0));
|
||||
const readCleanString = function(bstr, numBytes) {
|
||||
const str = bstr.readString(numBytes);
|
||||
const zIndex = str.indexOf(String.fromCharCode(0));
|
||||
return zIndex != -1 ? str.substr(0, zIndex) : str;
|
||||
};
|
||||
|
||||
// takes a ByteStream and parses out the local file information
|
||||
var TarLocalFile = function(bstream) {
|
||||
const TarLocalFile = function(bstream) {
|
||||
this.isValid = false;
|
||||
|
||||
// Read in the header block
|
||||
|
@ -89,7 +89,7 @@ var TarLocalFile = function(bstream) {
|
|||
// A regular file.
|
||||
if (this.typeflag == 0) {
|
||||
info(" This is a regular file.");
|
||||
var sizeInBytes = parseInt(this.size);
|
||||
const sizeInBytes = parseInt(this.size);
|
||||
this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.size);
|
||||
if (this.name.length > 0 && this.size > 0 && this.fileData && this.fileData.buffer) {
|
||||
this.isValid = true;
|
||||
|
@ -98,7 +98,7 @@ var TarLocalFile = function(bstream) {
|
|||
bstream.readBytes(this.size);
|
||||
|
||||
// Round up to 512-byte blocks.
|
||||
var remaining = 512 - bstream.ptr % 512;
|
||||
const remaining = 512 - bstream.ptr % 512;
|
||||
if (remaining > 0 && remaining < 512) {
|
||||
bstream.readBytes(remaining);
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ var TarLocalFile = function(bstream) {
|
|||
// Takes an ArrayBuffer of a tar file in
|
||||
// returns null on error
|
||||
// returns an array of DecompressedFile objects on success
|
||||
var untar = function(arrayBuffer) {
|
||||
const untar = function(arrayBuffer) {
|
||||
currentFilename = "";
|
||||
currentFileNumber = 0;
|
||||
currentBytesUnarchivedInFile = 0;
|
||||
|
@ -119,12 +119,12 @@ var untar = function(arrayBuffer) {
|
|||
totalFilesInArchive = 0;
|
||||
|
||||
postMessage(new bitjs.archive.UnarchiveStartEvent());
|
||||
var bstream = new bitjs.io.ByteStream(arrayBuffer);
|
||||
var localFiles = [];
|
||||
const bstream = new bitjs.io.ByteStream(arrayBuffer);
|
||||
const localFiles = [];
|
||||
|
||||
// While we don't encounter an empty block, keep making TarLocalFiles.
|
||||
while (bstream.peekNumber(4) != 0) {
|
||||
var oneLocalFile = new TarLocalFile(bstream);
|
||||
const oneLocalFile = new TarLocalFile(bstream);
|
||||
if (oneLocalFile && oneLocalFile.isValid) {
|
||||
localFiles.push(oneLocalFile);
|
||||
totalUncompressedBytesInArchive += oneLocalFile.size;
|
||||
|
@ -134,29 +134,7 @@ var untar = function(arrayBuffer) {
|
|||
|
||||
// got all local files, now sort them
|
||||
localFiles.sort(function(a,b) {
|
||||
var aname = a.filename;
|
||||
var bname = b.filename;
|
||||
return aname > bname ? 1 : -1;
|
||||
|
||||
// extract the number at the end of both filenames
|
||||
/*
|
||||
var aname = a.filename;
|
||||
var bname = b.filename;
|
||||
var aindex = aname.length, bindex = bname.length;
|
||||
|
||||
// Find the last number character from the back of the filename.
|
||||
while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex;
|
||||
while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex;
|
||||
|
||||
// Find the first number character from the back of the filename
|
||||
while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex;
|
||||
while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex;
|
||||
|
||||
// parse them into numbers and return comparison
|
||||
var anum = parseInt(aname.substr(aindex), 10),
|
||||
bnum = parseInt(bname.substr(bindex), 10);
|
||||
return anum - bnum;
|
||||
*/
|
||||
return a.filename > b.filename ? 1 : -1;
|
||||
});
|
||||
|
||||
// report # files and total length
|
||||
|
@ -165,8 +143,8 @@ var untar = function(arrayBuffer) {
|
|||
}
|
||||
|
||||
// now do the shipping of each file
|
||||
for (var i = 0; i < localFiles.length; ++i) {
|
||||
var localfile = localFiles[i];
|
||||
for (let i = 0; i < localFiles.length; ++i) {
|
||||
const localfile = localFiles[i];
|
||||
info("Sending file '" + localfile.filename + "' up");
|
||||
|
||||
// update progress
|
||||
|
@ -185,6 +163,6 @@ var untar = function(arrayBuffer) {
|
|||
|
||||
// event.data.file has the ArrayBuffer.
|
||||
onmessage = function(event) {
|
||||
var ab = event.data.file;
|
||||
const ab = event.data.file;
|
||||
untar(ab);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue