single-file-cli 2.3.0 → 2.4.0

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.
@@ -1,228 +0,0 @@
1
- /*
2
- * Copyright 2010-2026 Gildas Lormeau
3
- * contact : gildas.lormeau <at> gmail.com
4
- *
5
- * This file is part of SingleFile.
6
- *
7
- * The code in this file is free software: you can redistribute it and/or
8
- * modify it under the terms of the GNU Affero General Public License
9
- * (GNU AGPL) as published by the Free Software Foundation, either version 3
10
- * of the License, or (at your option) any later version.
11
- *
12
- * The code in this file is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
- * General Public License for more details.
16
- *
17
- * As additional permission under GNU AGPL version 3 section 7, you may
18
- * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
19
- * AGPL normally required by section 4, provided you include this license
20
- * notice and a URL through which recipients can access the Corresponding
21
- * Source.
22
- */
23
-
24
- /* global URL */
25
-
26
- import {
27
- configure,
28
- createArchive,
29
- TextReader,
30
- Uint8ArrayReader,
31
- Uint8ArrayWriter,
32
- ZipReader
33
- } from "./single-file-archive.js";
34
-
35
- const PAGES_PREFIX = "pages/";
36
- const PAGES_FILENAME = "sfz-pages.json";
37
- const TOC_FILENAME = "sfz-toc.html";
38
- const TOC_TITLE = "Table of contents";
39
- const TOC_STYLE = "body{font-family:system-ui,sans-serif;margin:2em auto;max-width:40em;padding:0 1em;background-color:#fff;color:#000}" +
40
- "a{color:#0000ee}a:visited{color:#551a8b}" +
41
- "summary{cursor:pointer;font-weight:bold;margin:.5em 0}" +
42
- "details{padding-left:1em}ul{margin:.25em 0;padding-left:1.5em}" +
43
- "@media(prefers-color-scheme:dark){body{background-color:#111;color:#eee}a{color:#8ab4f8}a:visited{color:#c58af9}}";
44
- const COMMENT_HEADER = "Page saved with SingleFile";
45
- const SYMLINK_UNIX_MODE = 0o120777;
46
-
47
- export { createPagesArchive };
48
-
49
- async function createPagesArchive(pages, options) {
50
- configure({ useWebWorkers: false });
51
- const manifest = {
52
- pages: pages.map((page, pageIndex) => ({
53
- path: getPagePath(pageIndex),
54
- url: page.url,
55
- originalUrls: page.originalUrls,
56
- title: page.title
57
- }))
58
- };
59
- if (options.markUnarchivedLinks) {
60
- manifest.markUnarchivedLinks = true;
61
- }
62
- if (options.pageTransitions && options.pageTransitions != "auto") {
63
- manifest.pageTransitions = options.pageTransitions;
64
- }
65
- const pageData = {
66
- doctype: "<!DOCTYPE html>",
67
- content: "",
68
- title: pages[0].title || "",
69
- comment: options.insertSingleFileComment ? getComment(pages[0].url, options) : undefined,
70
- tocContent: getTOCContent(pages)
71
- };
72
- const archiveOptions = {
73
- url: pages[0].url,
74
- multiPageArchive: true,
75
- selfExtractingArchive: options.selfExtractingArchive,
76
- extractDataFromPage: options.extractDataFromPage,
77
- preventAppendedData: options.preventAppendedData,
78
- includeBOM: options.includeBOM,
79
- insertMetaCSP: options.insertMetaCSP,
80
- insertCanonicalLink: options.insertCanonicalLink,
81
- insertMetaNoIndex: options.insertMetaNoIndex
82
- };
83
- const writtenEntries = options.dedupPages ? new Map() : undefined;
84
- const aliases = {};
85
- const blob = await createArchive(pageData, archiveOptions, options.zipScript, async zipWriter => {
86
- for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) {
87
- const pagePath = getPagePath(pageIndex);
88
- const zipReader = new ZipReader(new Uint8ArrayReader(await pages[pageIndex].getData()));
89
- for (const entry of await zipReader.getEntries()) {
90
- const filename = pagePath + entry.filename;
91
- const rawData = await entry.getData(new Uint8ArrayWriter(), { passThrough: true, checkSignature: false });
92
- const canonicalFilename = writtenEntries && findDuplicate(writtenEntries, filename, entry, rawData);
93
- if (canonicalFilename === undefined) {
94
- await zipWriter.add(filename, new Uint8ArrayReader(rawData), {
95
- passThrough: true,
96
- compressionMethod: entry.compressionMethod,
97
- uncompressedSize: entry.uncompressedSize,
98
- signature: entry.signature,
99
- comment: entry.comment,
100
- lastModDate: entry.lastModDate
101
- });
102
- } else {
103
- // the duplicate becomes a symlink entry so that external
104
- // extractors still produce complete page folders, the router
105
- // resolves it from the manifest alias map instead
106
- aliases[filename] = canonicalFilename;
107
- await zipWriter.add(filename, new TextReader(getRelativePath(filename, canonicalFilename)), {
108
- msDosCompatible: false,
109
- unixMode: SYMLINK_UNIX_MODE,
110
- level: 0,
111
- comment: entry.comment,
112
- lastModDate: entry.lastModDate
113
- });
114
- }
115
- }
116
- await zipReader.close();
117
- }
118
- if (Object.keys(aliases).length) {
119
- manifest.aliases = aliases;
120
- }
121
- if (options.tocPage) {
122
- await zipWriter.add(TOC_FILENAME, new TextReader(getTOCPageContent(manifest.pages)));
123
- }
124
- await zipWriter.add(PAGES_FILENAME, new TextReader(JSON.stringify(manifest, null, 2)));
125
- });
126
- return new Uint8Array(await blob.arrayBuffer());
127
- }
128
-
129
- function findDuplicate(writtenEntries, filename, entry, rawData) {
130
- if (entry.directory || !entry.uncompressedSize) {
131
- return;
132
- }
133
- const key = [entry.compressionMethod, entry.uncompressedSize, entry.signature, rawData.length].join(":");
134
- const candidates = writtenEntries.get(key);
135
- if (candidates) {
136
- const match = candidates.find(candidate => equalData(candidate.rawData, rawData));
137
- if (match) {
138
- return match.filename;
139
- }
140
- candidates.push({ filename, rawData });
141
- } else {
142
- writtenEntries.set(key, [{ filename, rawData }]);
143
- }
144
- }
145
-
146
- function equalData(dataLeft, dataRight) {
147
- return dataLeft.length == dataRight.length && dataLeft.every((value, index) => value == dataRight[index]);
148
- }
149
-
150
- function getRelativePath(filename, targetFilename) {
151
- const baseSegments = filename.split("/").slice(0, -1);
152
- const targetSegments = targetFilename.split("/");
153
- while (baseSegments.length && targetSegments.length > 1 && baseSegments[0] == targetSegments[0]) {
154
- baseSegments.shift();
155
- targetSegments.shift();
156
- }
157
- return "../".repeat(baseSegments.length) + targetSegments.join("/");
158
- }
159
-
160
- function getPagePath(pageIndex) {
161
- return pageIndex == 0 ? "" : PAGES_PREFIX + (pageIndex + 1) + "/";
162
- }
163
-
164
- function getComment(url, options) {
165
- return "\n " + COMMENT_HEADER +
166
- " \n url: " + url +
167
- (options.removeSavedDate ? " " : " \n saved date: " + new Date()) + "\n";
168
- }
169
-
170
- function getTOCPageContent(pages) {
171
- const origins = new Set(pages.map(page => new URL(page.url).origin));
172
- const rootGroup = { groups: new Map(), pages: [] };
173
- pages.forEach(page => {
174
- const url = new URL(page.url);
175
- const segments = url.pathname.split("/").slice(1, -1);
176
- if (origins.size > 1) {
177
- segments.unshift(url.origin);
178
- }
179
- let group = rootGroup;
180
- segments.forEach(segment => {
181
- if (!group.groups.has(segment)) {
182
- group.groups.set(segment, { groups: new Map(), pages: [] });
183
- }
184
- group = group.groups.get(segment);
185
- });
186
- group.pages.push(page);
187
- });
188
- const title = pages[0].title ? TOC_TITLE + " - " + pages[0].title : TOC_TITLE;
189
- return "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">" +
190
- "<title>" + escapeUnicodeHTML(title) + "</title><style>" + TOC_STYLE + "</style></head><body><main><h1>" +
191
- escapeUnicodeHTML(TOC_TITLE) + "</h1>" + getTOCGroupContent(rootGroup) + "</main></body></html>";
192
- }
193
-
194
- // nested details/summary groups stay collapsible without scripts on purpose,
195
- // the page must remain usable after a plain unzip
196
- function getTOCGroupContent(group) {
197
- let content = "";
198
- if (group.pages.length) {
199
- content += "<ul>" + group.pages.map(page =>
200
- "<li><a href=\"" + escapeUnicodeHTML(page.path + "index.html") + "\">" + escapeUnicodeHTML(page.title || page.url) + "</a></li>").join("") + "</ul>";
201
- }
202
- group.groups.forEach((childGroup, segment) => {
203
- content += "<details open><summary>" + escapeUnicodeHTML(segment) + "</summary>" + getTOCGroupContent(childGroup) + "</details>";
204
- });
205
- return content;
206
- }
207
-
208
- // unlike the prelude TOC below, the stored page is a UTF-8 entry: only the
209
- // markup delimiters need escaping, but crawled titles remain untrusted
210
- function escapeUnicodeHTML(value) {
211
- return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
212
- }
213
-
214
- function getTOCContent(pages) {
215
- return "<nav><ul>" +
216
- pages.map(page => "<li><a href=\"" + escapeHTML(page.url) + "\">" + escapeHTML(page.title || page.url) + "</a></li>").join("") +
217
- "</ul></nav>";
218
- }
219
-
220
- // the prelude declares the windows-1252 charset, non-ASCII characters must be
221
- // encoded as HTML entities to survive it
222
- function escapeHTML(value) {
223
- return Array.from(value).map(character => {
224
- const codePoint = character.codePointAt(0);
225
- return codePoint < 32 || codePoint > 126 || character == "&" || character == "<" || character == ">" || character == "\"" ?
226
- "&#" + codePoint + ";" : character;
227
- }).join("");
228
- }