Skip to content

Commit 45fdedd

Browse files
committed
Move some common type definitions into archive/common.js and some comments.
1 parent 4fc0c3d commit 45fdedd

File tree

11 files changed

+75
-83
lines changed

11 files changed

+75
-83
lines changed

archive/common.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/**
22
* common.js
33
*
4-
* Provides common functionality for compressing and decompressing.
4+
* Provides common definitions or functionality needed by multiple modules.
55
*
66
* Licensed under the MIT License
77
*
88
* Copyright(c) 2023 Google Inc.
99
*/
1010

11-
// Requires the following JavaScript features: MessageChannel, MessagePort, and dynamic imports.
11+
/**
12+
* @typedef FileInfo An object that is sent to the implementation representing a file to compress.
13+
* @property {string} fileName The name of the file. TODO: Includes the path?
14+
* @property {number} lastModTime The number of ms since the Unix epoch (1970-01-01 at midnight).
15+
* @property {Uint8Array} fileData The bytes of the file.
16+
*/
1217

1318
/**
1419
* @typedef Implementation
@@ -51,3 +56,23 @@ export async function getConnectedPort(implFilename) {
5156
});
5257
});
5358
}
59+
60+
// Zip-specific things.
61+
62+
export const LOCAL_FILE_HEADER_SIG = 0x04034b50;
63+
export const CENTRAL_FILE_HEADER_SIG = 0x02014b50;
64+
export const END_OF_CENTRAL_DIR_SIG = 0x06054b50;
65+
export const CRC32_MAGIC_NUMBER = 0xedb88320;
66+
export const ARCHIVE_EXTRA_DATA_SIG = 0x08064b50;
67+
export const DIGITAL_SIGNATURE_SIG = 0x05054b50;
68+
export const END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50;
69+
export const DATA_DESCRIPTOR_SIG = 0x08074b50;
70+
71+
/**
72+
* @readonly
73+
* @enum {number}
74+
*/
75+
export const ZipCompressionMethod = {
76+
STORE: 0, // Default.
77+
DEFLATE: 8, // As per http://tools.ietf.org/html/rfc1951.
78+
};

archive/compress.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Copyright(c) 2023 Google Inc.
99
*/
1010

11-
import { getConnectedPort } from './common.js';
11+
import { ZipCompressionMethod, getConnectedPort } from './common.js';
1212

1313
// NOTE: THIS IS A VERY HACKY WORK-IN-PROGRESS! THE API IS NOT FROZEN! USE AT YOUR OWN RISK!
1414

@@ -19,15 +19,6 @@ import { getConnectedPort } from './common.js';
1919
* @property {Uint8Array} fileData The bytes of the file.
2020
*/
2121

22-
/**
23-
* @readonly
24-
* @enum {number}
25-
*/
26-
export const ZipCompressionMethod = {
27-
STORE: 0, // Default.
28-
// DEFLATE: 8,
29-
};
30-
3122
// export const DeflateCompressionMethod = {
3223
// NO_COMPRESSION: 0,
3324
// COMPRESSION_FIXED_HUFFMAN: 1,
@@ -36,17 +27,15 @@ export const ZipCompressionMethod = {
3627

3728
/**
3829
* Data elements are packed into bytes in order of increasing bit number within the byte,
39-
i.e., starting with the least-significant bit of the byte.
30+
* i.e., starting with the least-significant bit of the byte.
4031
* Data elements other than Huffman codes are packed starting with the least-significant bit of the
41-
data element.
32+
* data element.
4233
* Huffman codes are packed starting with the most-significant bit of the code.
4334
*/
4435

4536
/**
4637
* @typedef CompressorOptions
4738
* @property {ZipCompressionMethod} zipCompressionMethod
48-
* @property {DeflateCompressionMethod=} deflateCompressionMethod Only present if
49-
* zipCompressionMethod is set to DEFLATE.
5039
*/
5140

5241
/**
@@ -91,6 +80,7 @@ export class Zipper {
9180
* @private
9281
*/
9382
this.zipCompressionMethod = options.zipCompressionMethod || ZipCompressionMethod.STORE;
83+
if (this.zipCompressionMethod === ZipCompressionMethod.DEFLATE) throw `DEFLATE not supported.`;
9484

9585
/**
9686
* @type {CompressStatus}

archive/events.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export const UnarchiveEventType = {
1919
ERROR: 'error'
2020
};
2121

22-
/**
23-
* An unarchive event.
24-
*/
22+
// TODO: Use CustomEvent and a @template and remove these boilerplate events.
23+
24+
/** An unarchive event. */
2525
export class UnarchiveEvent extends Event {
2626
/**
2727
* @param {string} type The event type.
@@ -31,9 +31,7 @@ export class UnarchiveEvent extends Event {
3131
}
3232
}
3333

34-
/**
35-
* Updates all Archiver listeners that an append has occurred.
36-
*/
34+
/** Updates all Unarchiver listeners that an append has occurred. */
3735
export class UnarchiveAppendEvent extends UnarchiveEvent {
3836
/**
3937
* @param {number} numBytes The number of bytes appended.
@@ -49,9 +47,7 @@ export class UnarchiveAppendEvent extends UnarchiveEvent {
4947
}
5048
}
5149

52-
/**
53-
* Useful for passing info up to the client (for debugging).
54-
*/
50+
/** Useful for passing info up to the client (for debugging). */
5551
export class UnarchiveInfoEvent extends UnarchiveEvent {
5652
/**
5753
* @param {string} msg The info message.
@@ -67,9 +63,7 @@ export class UnarchiveInfoEvent extends UnarchiveEvent {
6763
}
6864
}
6965

70-
/**
71-
* An unrecoverable error has occured.
72-
*/
66+
/** An unrecoverable error has occured. */
7367
export class UnarchiveErrorEvent extends UnarchiveEvent {
7468
/**
7569
* @param {string} msg The error message.
@@ -85,18 +79,14 @@ export class UnarchiveErrorEvent extends UnarchiveEvent {
8579
}
8680
}
8781

88-
/**
89-
* Start event.
90-
*/
82+
/** Start event. */
9183
export class UnarchiveStartEvent extends UnarchiveEvent {
9284
constructor() {
9385
super(UnarchiveEventType.START);
9486
}
9587
}
9688

97-
/**
98-
* Finish event.
99-
*/
89+
/** Finish event. */
10090
export class UnarchiveFinishEvent extends UnarchiveEvent {
10191
/**
10292
* @param {Object} metadata A collection fo metadata about the archive file.
@@ -108,9 +98,7 @@ export class UnarchiveFinishEvent extends UnarchiveEvent {
10898
}
10999

110100
// TODO(bitjs): Fully document these. They are confusing.
111-
/**
112-
* Progress event.
113-
*/
101+
/** Progress event. */
114102
export class UnarchiveProgressEvent extends UnarchiveEvent {
115103
/**
116104
* @param {string} currentFilename
@@ -136,9 +124,7 @@ export class UnarchiveProgressEvent extends UnarchiveEvent {
136124
}
137125
}
138126

139-
/**
140-
* Extract event.
141-
*/
127+
/** Extract event. */
142128
export class UnarchiveExtractEvent extends UnarchiveEvent {
143129
/**
144130
* @param {UnarchivedFile} unarchivedFile

archive/unrar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ let hostPort;
2828

2929
// State - consider putting these into a class.
3030
let unarchiveState = UnarchiveState.NOT_STARTED;
31+
/** @type {ByteStream} */
3132
let bytestream = null;
3233
let allLocalFiles = null;
3334
let logToConsole = false;

archive/untar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ let hostPort;
2424

2525
// State - consider putting these into a class.
2626
let unarchiveState = UnarchiveState.NOT_STARTED;
27+
/** @type {ByteStream} */
2728
let bytestream = null;
2829
let allLocalFiles = null;
2930
let logToConsole = false;

archive/unzip.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import { BitStream } from '../io/bitstream.js';
1616
import { ByteBuffer } from '../io/bytebuffer.js';
1717
import { ByteStream } from '../io/bytestream.js';
18+
import { ARCHIVE_EXTRA_DATA_SIG, CENTRAL_FILE_HEADER_SIG, CRC32_MAGIC_NUMBER,
19+
DATA_DESCRIPTOR_SIG, DIGITAL_SIGNATURE_SIG, END_OF_CENTRAL_DIR_SIG,
20+
LOCAL_FILE_HEADER_SIG } from './common.js';
1821

1922
const UnarchiveState = {
2023
NOT_STARTED: 0,
@@ -28,6 +31,7 @@ let hostPort;
2831

2932
// State - consider putting these into a class.
3033
let unarchiveState = UnarchiveState.NOT_STARTED;
34+
/** @type {ByteStream} */
3135
let bytestream = null;
3236
let allLocalFiles = null;
3337
let logToConsole = false;
@@ -60,24 +64,14 @@ const postProgress = function () {
6064
});
6165
};
6266

63-
const zLocalFileHeaderSignature = 0x04034b50;
64-
const zArchiveExtraDataSignature = 0x08064b50;
65-
const zCentralFileHeaderSignature = 0x02014b50;
66-
const zDigitalSignatureSignature = 0x05054b50;
67-
const zEndOfCentralDirSignature = 0x06054b50;
68-
const zEndOfCentralDirLocatorSignature = 0x07064b50;
69-
const zDataDescriptorSignature = 0x08074b50;
70-
7167
// mask for getting the Nth bit (zero-based)
7268
const BIT = [0x01, 0x02, 0x04, 0x08,
7369
0x10, 0x20, 0x40, 0x80,
7470
0x100, 0x200, 0x400, 0x800,
7571
0x1000, 0x2000, 0x4000, 0x8000];
7672

7773
class ZipLocalFile {
78-
/**
79-
* @param {ByteStream} bstream
80-
*/
74+
/** @param {ByteStream} bstream */
8175
constructor(bstream) {
8276
if (typeof bstream != typeof {} || !bstream.readNumber || typeof bstream.readNumber != typeof function () { }) {
8377
return null;
@@ -125,9 +119,9 @@ class ZipLocalFile {
125119
let foundDataDescriptor = false;
126120
let numBytesSeeked = 0;
127121
while (!foundDataDescriptor) {
128-
while (bstream.peekNumber(4) !== zLocalFileHeaderSignature &&
129-
bstream.peekNumber(4) !== zArchiveExtraDataSignature &&
130-
bstream.peekNumber(4) !== zCentralFileHeaderSignature) {
122+
while (bstream.peekNumber(4) !== LOCAL_FILE_HEADER_SIG &&
123+
bstream.peekNumber(4) !== ARCHIVE_EXTRA_DATA_SIG &&
124+
bstream.peekNumber(4) !== CENTRAL_FILE_HEADER_SIG) {
131125
numBytesSeeked++;
132126
bstream.readBytes(1);
133127
}
@@ -143,7 +137,7 @@ class ZipLocalFile {
143137

144138
// From the PKZIP App Note: "The signature value 0x08074b50 is also used by some ZIP
145139
// implementations as a marker for the Data Descriptor record".
146-
if (maybeDescriptorSig === zDataDescriptorSignature) {
140+
if (maybeDescriptorSig === DATA_DESCRIPTOR_SIG) {
147141
if (maybeCompressedSize === (numBytesSeeked - 16)) {
148142
foundDataDescriptor = true;
149143
descriptorSize = 16;
@@ -606,7 +600,7 @@ function archiveUnzip() {
606600
let bstream = bytestream.tee();
607601

608602
// loop until we don't see any more local files or we find a data descriptor.
609-
while (bstream.peekNumber(4) == zLocalFileHeaderSignature) {
603+
while (bstream.peekNumber(4) == LOCAL_FILE_HEADER_SIG) {
610604
// Note that this could throw an error if the bstream overflows, which is caught in the
611605
// message handler.
612606
const oneLocalFile = new ZipLocalFile(bstream);
@@ -636,7 +630,7 @@ function archiveUnzip() {
636630
totalFilesInArchive = allLocalFiles.length;
637631

638632
// archive extra data record
639-
if (bstream.peekNumber(4) == zArchiveExtraDataSignature) {
633+
if (bstream.peekNumber(4) == ARCHIVE_EXTRA_DATA_SIG) {
640634
if (logToConsole) {
641635
info(' Found an Archive Extra Data Signature');
642636
}
@@ -649,13 +643,13 @@ function archiveUnzip() {
649643

650644
// central directory structure
651645
// TODO: handle the rest of the structures (Zip64 stuff)
652-
if (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
646+
if (bstream.peekNumber(4) == CENTRAL_FILE_HEADER_SIG) {
653647
if (logToConsole) {
654648
info(' Found a Central File Header');
655649
}
656650

657651
// read all file headers
658-
while (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
652+
while (bstream.peekNumber(4) == CENTRAL_FILE_HEADER_SIG) {
659653
bstream.readNumber(4); // signature
660654
const cdfh = {
661655
versionMadeBy: bstream.readNumber(2),
@@ -688,7 +682,7 @@ function archiveUnzip() {
688682
}
689683

690684
// digital signature
691-
if (bstream.peekNumber(4) == zDigitalSignatureSignature) {
685+
if (bstream.peekNumber(4) == DIGITAL_SIGNATURE_SIG) {
692686
if (logToConsole) {
693687
info(' Found a Digital Signature');
694688
}
@@ -699,7 +693,7 @@ function archiveUnzip() {
699693
}
700694

701695
let metadata = {};
702-
if (bstream.peekNumber(4) == zEndOfCentralDirSignature) {
696+
if (bstream.peekNumber(4) == END_OF_CENTRAL_DIR_SIG) {
703697
bstream.readNumber(4); // signature
704698
const eocds = {
705699
numberOfThisDisk: bstream.readNumber(2),

0 commit comments

Comments
 (0)