@zip.js/zip.js 2.7.40 → 2.7.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core/io.js CHANGED
@@ -31,7 +31,8 @@
31
31
 
32
32
  import {
33
33
  UNDEFINED_VALUE,
34
- FUNCTION_TYPE
34
+ FUNCTION_TYPE,
35
+ END_OF_CENTRAL_DIR_LENGTH
35
36
  } from "./constants.js";
36
37
  import { getConfiguration } from "./configuration.js";
37
38
 
@@ -296,35 +297,43 @@ function createHttpReader(httpReader, url, options) {
296
297
  const {
297
298
  preventHeadRequest,
298
299
  useRangeHeader,
299
- forceRangeRequests
300
+ forceRangeRequests,
301
+ combineSizeEocd
300
302
  } = options;
301
303
  options = Object.assign({}, options);
302
304
  delete options.preventHeadRequest;
303
305
  delete options.useRangeHeader;
304
306
  delete options.forceRangeRequests;
307
+ delete options.combineSizeEocd;
305
308
  delete options.useXHR;
306
309
  Object.assign(httpReader, {
307
310
  url,
308
311
  options,
309
312
  preventHeadRequest,
310
313
  useRangeHeader,
311
- forceRangeRequests
314
+ forceRangeRequests,
315
+ combineSizeEocd
312
316
  });
313
317
  }
314
318
 
315
319
  async function initHttpReader(httpReader, sendRequest, getRequestData) {
316
320
  const {
317
321
  url,
322
+ preventHeadRequest,
318
323
  useRangeHeader,
319
- forceRangeRequests
324
+ forceRangeRequests,
325
+ combineSizeEocd
320
326
  } = httpReader;
321
- if (isHttpFamily(url) && (useRangeHeader || forceRangeRequests)) {
322
- const { headers } = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader));
323
- if (!forceRangeRequests && headers.get(HTTP_HEADER_ACCEPT_RANGES) != HTTP_RANGE_UNIT) {
327
+ if (isHttpFamily(url) && (useRangeHeader || forceRangeRequests) && (typeof preventHeadRequest == "undefined" || preventHeadRequest)) {
328
+ const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader, combineSizeEocd ? -END_OF_CENTRAL_DIR_LENGTH : undefined));
329
+ if (!forceRangeRequests && response.headers.get(HTTP_HEADER_ACCEPT_RANGES) != HTTP_RANGE_UNIT) {
324
330
  throw new Error(ERR_HTTP_RANGE);
325
331
  } else {
332
+ if (combineSizeEocd) {
333
+ httpReader.eocdCache = new Uint8Array(await response.arrayBuffer());
334
+ }
326
335
  let contentSize;
327
- const contentRangeHeader = headers.get(HTTP_HEADER_CONTENT_RANGE);
336
+ const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
328
337
  if (contentRangeHeader) {
329
338
  const splitHeader = contentRangeHeader.trim().split(/\s*\/\s*/);
330
339
  if (splitHeader.length) {
@@ -349,9 +358,14 @@ async function readUint8ArrayHttpReader(httpReader, index, length, sendRequest,
349
358
  const {
350
359
  useRangeHeader,
351
360
  forceRangeRequests,
361
+ eocdCache,
362
+ size,
352
363
  options
353
364
  } = httpReader;
354
365
  if (useRangeHeader || forceRangeRequests) {
366
+ if (eocdCache && index == size - END_OF_CENTRAL_DIR_LENGTH && length == END_OF_CENTRAL_DIR_LENGTH) {
367
+ return eocdCache;
368
+ }
355
369
  const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader, index, length));
356
370
  if (response.status != 206) {
357
371
  throw new Error(ERR_HTTP_RANGE);
@@ -367,7 +381,7 @@ async function readUint8ArrayHttpReader(httpReader, index, length, sendRequest,
367
381
  }
368
382
 
369
383
  function getRangeHeaders(httpReader, index = 0, length = 1) {
370
- return Object.assign({}, getHeaders(httpReader), { [HTTP_HEADER_RANGE]: HTTP_RANGE_UNIT + "=" + index + "-" + (index + length - 1) });
384
+ return Object.assign({}, getHeaders(httpReader), { [HTTP_HEADER_RANGE]: HTTP_RANGE_UNIT + "=" + (index < 0 ? index : index + "-" + (index + length - 1)) });
371
385
  }
372
386
 
373
387
  function getHeaders({ options }) {
@@ -277,9 +277,10 @@ class ZipReader {
277
277
  commentUTF8,
278
278
  rawExtraField: directoryArray.subarray(extraFieldOffset, commentOffset)
279
279
  });
280
+ const decode = getOptionValue(zipReader, options, "decodeText") || decodeText;
280
281
  const [filename, comment] = await Promise.all([
281
- decodeText(rawFilename, filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437),
282
- decodeText(rawComment, commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437)
282
+ decode(rawFilename, filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437),
283
+ decode(rawComment, commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437)
283
284
  ]);
284
285
  Object.assign(fileEntry, {
285
286
  rawComment,
@@ -224,12 +224,13 @@ async function addFile(zipWriter, name, reader, options) {
224
224
  } else {
225
225
  options.directory = name.endsWith(DIRECTORY_SIGNATURE);
226
226
  }
227
- const rawFilename = encodeText(name);
227
+ const encode = getOptionValue(zipWriter, options, "encodeText", encodeText);
228
+ const rawFilename = encode(name);
228
229
  if (getLength(rawFilename) > MAX_16_BITS) {
229
230
  throw new Error(ERR_INVALID_ENTRY_NAME);
230
231
  }
231
232
  const comment = options.comment || "";
232
- const rawComment = encodeText(comment);
233
+ const rawComment = encode(comment);
233
234
  if (getLength(rawComment) > MAX_16_BITS) {
234
235
  throw new Error(ERR_INVALID_ENTRY_COMMENT);
235
236
  }
@@ -332,7 +333,7 @@ async function addFile(zipWriter, name, reader, options) {
332
333
  zip64DiskNumberStart,
333
334
  password,
334
335
  rawPassword,
335
- level,
336
+ level: !useCompressionStream && zipWriter.config.CompressionStream === UNDEFINED_VALUE ? 0 : level,
336
337
  useWebWorkers,
337
338
  encryptionStrength,
338
339
  extendedTimestamp,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A JavaScript library to zip and unzip files in the browser, Deno and Node.js",
4
4
  "author": "Gildas Lormeau",
5
5
  "license": "BSD-3-Clause",
6
- "version": "2.7.40",
6
+ "version": "2.7.42",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "zip",