single-file-core 1.5.85 → 1.5.86

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.85",
3
+ "version": "1.5.86",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -87,7 +87,8 @@ const PNG_IHDR_LENGTH = 25;
87
87
  const browser = globalThis.browser;
88
88
 
89
89
  export {
90
- process
90
+ process,
91
+ createArchive
91
92
  };
92
93
 
93
94
  async function process(pageData, options, lastModDate = new Date()) {
@@ -98,16 +99,24 @@ async function process(pageData, options, lastModDate = new Date()) {
98
99
  configure({ workerURI: "/lib/single-file-z-worker.js" });
99
100
  script = await (await fetch(browser.runtime.getURL(SCRIPT_PATH))).text();
100
101
  }
102
+ return createArchive(pageData, options, script, zipWriter => {
103
+ pageData.url = options.url;
104
+ pageData.archiveTime = (new Date()).toISOString();
105
+ return addPageResources(zipWriter, pageData, { password: options.password, disableCompression: options.disableCompression }, options.createRootDirectory ? String(Date.now()) + "_" + (options.tabId || 0) + "/" : "", options.url);
106
+ }, lastModDate);
107
+ }
108
+
109
+ async function createArchive(pageData, options, script, writeEntries, lastModDate = new Date()) {
101
110
  const zipDataWriter = new Uint8ArrayWriter();
102
111
  zipDataWriter.init();
103
112
  zipDataWriter.writable.size = 0;
104
113
  let extraDataOffset, extraData, embeddedImageDataOffset, endTag;
105
114
  if (options.embeddedImage) {
106
- options.embeddedImage = Array.from(options.embeddedImage);
115
+ options.embeddedImage = new Uint8Array(options.embeddedImage);
107
116
  const embeddedImageData = options.embeddedImage.slice(PNG_SIGNATURE_LENGTH + PNG_IHDR_LENGTH, options.embeddedImage.length - PNG_IEND_LENGTH);
108
117
  await writeData(zipDataWriter.writable, options.embeddedImage.slice(0, PNG_SIGNATURE_LENGTH + PNG_IHDR_LENGTH));
109
118
  if (options.selfExtractingArchive) {
110
- const embeddedImageText = TEXT_DECODER.decode(new Uint8Array(embeddedImageData));
119
+ const embeddedImageText = TEXT_DECODER.decode(embeddedImageData);
111
120
  const tagIndex = EMBEDDED_DATA_REGEXPS.slice(0, -1).findIndex(tests => !embeddedImageText.match(tests[1]));
112
121
  let startTag;
113
122
  [startTag, endTag] = tagIndex == -1 ? ["", ""] : EMBEDDED_DATA_TAGS[tagIndex];
@@ -135,9 +144,7 @@ async function process(pageData, options, lastModDate = new Date()) {
135
144
  }
136
145
  const zipWriter = new ZipWriter(zipDataWriter, { bufferedWrite: true, keepOrder: true, lastModDate, useCompressionStream: true });
137
146
  const startOffset = zipDataWriter.offset;
138
- pageData.url = options.url;
139
- pageData.archiveTime = (new Date()).toISOString();
140
- await addPageResources(zipWriter, pageData, { password: options.password, disableCompression: options.disableCompression }, options.createRootDirectory ? String(Date.now()) + "_" + (options.tabId || 0) + "/" : "", options.url);
147
+ await writeEntries(zipWriter);
141
148
  const data = await zipWriter.close(null, { preventClose: true });
142
149
  if (options.selfExtractingArchive) {
143
150
  const lfCodes = [];
@@ -149,12 +156,12 @@ async function process(pageData, options, lastModDate = new Date()) {
149
156
  const tagIndex = EXTRA_DATA_TAGS.indexOf(options.extractDataFromPageTags);
150
157
  const regExpsTag = EXTRA_DATA_REGEXPS[tagIndex];
151
158
  if (textContent.match(regExpsTag[0]) || textContent.match(regExpsTag[1])) {
152
- return findExtraDataTags(textContent, pageData, options, lastModDate, tagIndex + 1);
159
+ return findExtraDataTags(textContent, pageData, options, script, writeEntries, lastModDate, tagIndex + 1);
153
160
  }
154
161
  } else {
155
162
  const matchCommentTags = textContent.match(/<!--/i) || textContent.match(/--!?>/i);
156
163
  if (matchCommentTags) {
157
- return findExtraDataTags(textContent, pageData, options, lastModDate);
164
+ return findExtraDataTags(textContent, pageData, options, script, writeEntries, lastModDate);
158
165
  }
159
166
  }
160
167
  }
@@ -195,12 +202,12 @@ async function process(pageData, options, lastModDate = new Date()) {
195
202
  if (options.preventAppendedData || extraData.length > 65535 - endTags.length - (options.embeddedImage ? PNG_IEND_LENGTH : 0)) {
196
203
  if (!options.extraDataSize) {
197
204
  options.extraDataSize = Math.floor(extraData.length * 1.001);
198
- return process(pageData, options, lastModDate);
205
+ return createArchive(pageData, options, script, writeEntries, lastModDate);
199
206
  }
200
207
  } else {
201
208
  if (options.extraDataSize) {
202
209
  options.extraDataSize = undefined;
203
- return process(pageData, options, lastModDate);
210
+ return createArchive(pageData, options, script, writeEntries, lastModDate);
204
211
  } else {
205
212
  pageContent += extraData;
206
213
  }
@@ -217,7 +224,7 @@ async function process(pageData, options, lastModDate = new Date()) {
217
224
  } else {
218
225
  options.extraData = extraData;
219
226
  options.extraDataSize = Math.floor(extraData.length * 1.001);
220
- return process(pageData, options, lastModDate);
227
+ return createArchive(pageData, options, script, writeEntries, lastModDate);
221
228
  }
222
229
  }
223
230
  if (options.embeddedImage) {
@@ -225,7 +232,7 @@ async function process(pageData, options, lastModDate = new Date()) {
225
232
  return new Blob([
226
233
  pageContent,
227
234
  getCRC32(pageContent, embeddedImageDataOffset),
228
- new Uint8Array(options.embeddedImage.slice(options.embeddedImage.length - PNG_IEND_LENGTH))
235
+ options.embeddedImage.slice(options.embeddedImage.length - PNG_IEND_LENGTH)
229
236
  ], { type: "application/octet-stream" });
230
237
  } else {
231
238
  return new Blob([pageContent], { type: "application/octet-stream" });
@@ -376,16 +383,16 @@ function getPageTitle(pageData) {
376
383
  return pageData.title.replace(/</g, "&lt;").replace(/>/g, "&gt;") || "";
377
384
  }
378
385
 
379
- function findExtraDataTags(textContent, pageData, options, lastModDate, indexExtractDataFromPageTags = 0) {
386
+ function findExtraDataTags(textContent, pageData, options, script, writeEntries, lastModDate, indexExtractDataFromPageTags = 0) {
380
387
  const regExpsTag = EXTRA_DATA_REGEXPS[indexExtractDataFromPageTags];
381
388
  const plaintextTag = EXTRA_DATA_TAGS[indexExtractDataFromPageTags][0] == "<plaintext>";
382
389
  const matchTag = !plaintextTag && (textContent.match(regExpsTag[0]) || textContent.match(regExpsTag[1]));
383
390
  if (matchTag) {
384
391
  if (indexExtractDataFromPageTags < EXTRA_DATA_TAGS.length - 1) {
385
- return findExtraDataTags(textContent, pageData, options, lastModDate, indexExtractDataFromPageTags + 1);
392
+ return findExtraDataTags(textContent, pageData, options, script, writeEntries, lastModDate, indexExtractDataFromPageTags + 1);
386
393
  } else {
387
394
  options.extractDataFromPage = false;
388
- return process(pageData, options, lastModDate);
395
+ return createArchive(pageData, options, script, writeEntries, lastModDate);
389
396
  }
390
397
  } else {
391
398
  options.extractDataFromPageTags = EXTRA_DATA_TAGS[indexExtractDataFromPageTags];
@@ -393,7 +400,7 @@ function findExtraDataTags(textContent, pageData, options, lastModDate, indexExt
393
400
  // <plaintext> cannot be closed, the file must end with the zip data
394
401
  options.preventAppendedData = true;
395
402
  }
396
- return process(pageData, options, lastModDate);
403
+ return createArchive(pageData, options, script, writeEntries, lastModDate);
397
404
  }
398
405
  }
399
406