@pagepocket/lib 0.4.2 → 0.5.1
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/README.md +66 -17
- package/dist/completion.d.ts +4 -0
- package/dist/completion.js +29 -0
- package/dist/content-store.d.ts +21 -0
- package/dist/content-store.js +96 -0
- package/dist/css-rewrite.d.ts +3 -4
- package/dist/css-rewrite.js +48 -49
- package/dist/hack-html.d.ts +1 -1
- package/dist/hack-html.js +1 -1
- package/dist/hackers/replay-xhr.js +26 -10
- package/dist/index.d.ts +8 -3
- package/dist/index.js +19 -5
- package/dist/network-store.d.ts +51 -0
- package/dist/network-store.js +159 -0
- package/dist/pagepocket.d.ts +6 -20
- package/dist/pagepocket.js +96 -70
- package/dist/path-resolver.d.ts +5 -0
- package/dist/path-resolver.js +92 -0
- package/dist/replay-script.d.ts +11 -1
- package/dist/replay-script.js +156 -173
- package/dist/resource-filter.d.ts +2 -0
- package/dist/resource-filter.js +34 -0
- package/dist/rewrite-links.d.ts +12 -14
- package/dist/rewrite-links.js +185 -197
- package/dist/snapshot-builder.d.ts +15 -0
- package/dist/snapshot-builder.js +275 -0
- package/dist/snapshot.d.ts +2 -0
- package/dist/snapshot.js +12 -0
- package/dist/types.d.ts +181 -38
- package/dist/utils.d.ts +19 -0
- package/dist/utils.js +109 -0
- package/dist/writers.d.ts +3 -0
- package/dist/writers.js +175 -0
- package/package.json +2 -2
package/dist/writers.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toZip = exports.writeToFS = void 0;
|
|
4
|
+
const uni_fs_1 = require("@pagepocket/uni-fs");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const normalizePath = (value) => value.replace(/\\/g, "/");
|
|
7
|
+
const joinPath = (base, relative) => {
|
|
8
|
+
const cleanBase = normalizePath(base).replace(/\/+$/, "");
|
|
9
|
+
const cleanRel = normalizePath(relative).replace(/^\/+/, "");
|
|
10
|
+
if (!cleanBase) {
|
|
11
|
+
return cleanRel;
|
|
12
|
+
}
|
|
13
|
+
return `${cleanBase}/${cleanRel}`;
|
|
14
|
+
};
|
|
15
|
+
const splitPathExtension = (value) => {
|
|
16
|
+
const clean = normalizePath(value);
|
|
17
|
+
const lastSlash = clean.lastIndexOf("/");
|
|
18
|
+
const lastDot = clean.lastIndexOf(".");
|
|
19
|
+
if (lastDot > lastSlash) {
|
|
20
|
+
return {
|
|
21
|
+
filename: clean.slice(0, lastDot),
|
|
22
|
+
extension: clean.slice(lastDot + 1)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return { filename: clean, extension: "" };
|
|
26
|
+
};
|
|
27
|
+
const streamToUint8Array = async (stream) => {
|
|
28
|
+
const reader = stream.getReader();
|
|
29
|
+
const chunks = [];
|
|
30
|
+
let total = 0;
|
|
31
|
+
while (true) {
|
|
32
|
+
const result = await reader.read();
|
|
33
|
+
if (result.done)
|
|
34
|
+
break;
|
|
35
|
+
if (result.value) {
|
|
36
|
+
chunks.push(result.value);
|
|
37
|
+
total += result.value.byteLength;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const output = new Uint8Array(total);
|
|
41
|
+
let offset = 0;
|
|
42
|
+
for (const chunk of chunks) {
|
|
43
|
+
output.set(chunk, offset);
|
|
44
|
+
offset += chunk.byteLength;
|
|
45
|
+
}
|
|
46
|
+
return output;
|
|
47
|
+
};
|
|
48
|
+
const writeToFS = async (snapshot, outDir, options) => {
|
|
49
|
+
let filesWritten = 0;
|
|
50
|
+
let totalBytes = 0;
|
|
51
|
+
for (const file of snapshot.files) {
|
|
52
|
+
const relative = (0, utils_1.stripLeadingSlash)(file.path);
|
|
53
|
+
const outputPath = joinPath(outDir, relative);
|
|
54
|
+
const { filename, extension } = splitPathExtension(outputPath);
|
|
55
|
+
const stream = await snapshot.content.open(file.source);
|
|
56
|
+
const data = await streamToUint8Array(stream);
|
|
57
|
+
await (0, uni_fs_1.write)(filename, extension, data);
|
|
58
|
+
filesWritten += 1;
|
|
59
|
+
totalBytes += data.byteLength;
|
|
60
|
+
}
|
|
61
|
+
if (options?.clearCache ?? true) {
|
|
62
|
+
await snapshot.content.dispose?.();
|
|
63
|
+
}
|
|
64
|
+
return { filesWritten, totalBytes };
|
|
65
|
+
};
|
|
66
|
+
exports.writeToFS = writeToFS;
|
|
67
|
+
const crc32Table = (() => {
|
|
68
|
+
const table = new Uint32Array(256);
|
|
69
|
+
for (let i = 0; i < 256; i += 1) {
|
|
70
|
+
let c = i;
|
|
71
|
+
for (let k = 0; k < 8; k += 1) {
|
|
72
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
73
|
+
}
|
|
74
|
+
table[i] = c >>> 0;
|
|
75
|
+
}
|
|
76
|
+
return table;
|
|
77
|
+
})();
|
|
78
|
+
const crc32 = (data) => {
|
|
79
|
+
let crc = 0 ^ -1;
|
|
80
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
81
|
+
crc = (crc >>> 8) ^ crc32Table[(crc ^ data[i]) & 0xff];
|
|
82
|
+
}
|
|
83
|
+
return (crc ^ -1) >>> 0;
|
|
84
|
+
};
|
|
85
|
+
const writeUint16 = (value) => {
|
|
86
|
+
const buffer = new Uint8Array(2);
|
|
87
|
+
const view = new DataView(buffer.buffer);
|
|
88
|
+
view.setUint16(0, value, true);
|
|
89
|
+
return buffer;
|
|
90
|
+
};
|
|
91
|
+
const writeUint32 = (value) => {
|
|
92
|
+
const buffer = new Uint8Array(4);
|
|
93
|
+
const view = new DataView(buffer.buffer);
|
|
94
|
+
view.setUint32(0, value, true);
|
|
95
|
+
return buffer;
|
|
96
|
+
};
|
|
97
|
+
const concatBytes = (chunks) => {
|
|
98
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
99
|
+
const output = new Uint8Array(total);
|
|
100
|
+
let offset = 0;
|
|
101
|
+
for (const chunk of chunks) {
|
|
102
|
+
output.set(chunk, offset);
|
|
103
|
+
offset += chunk.byteLength;
|
|
104
|
+
}
|
|
105
|
+
return output;
|
|
106
|
+
};
|
|
107
|
+
const toZip = async (snapshot, options) => {
|
|
108
|
+
const localChunks = [];
|
|
109
|
+
const centralChunks = [];
|
|
110
|
+
let offset = 0;
|
|
111
|
+
for (const file of snapshot.files) {
|
|
112
|
+
const name = (0, utils_1.stripLeadingSlash)(file.path);
|
|
113
|
+
const nameBytes = new TextEncoder().encode(name);
|
|
114
|
+
const stream = await snapshot.content.open(file.source);
|
|
115
|
+
const data = await streamToUint8Array(stream);
|
|
116
|
+
const crc = crc32(data);
|
|
117
|
+
const localHeader = concatBytes([
|
|
118
|
+
writeUint32(0x04034b50),
|
|
119
|
+
writeUint16(20),
|
|
120
|
+
writeUint16(0),
|
|
121
|
+
writeUint16(0),
|
|
122
|
+
writeUint16(0),
|
|
123
|
+
writeUint16(0),
|
|
124
|
+
writeUint32(crc),
|
|
125
|
+
writeUint32(data.byteLength),
|
|
126
|
+
writeUint32(data.byteLength),
|
|
127
|
+
writeUint16(nameBytes.byteLength),
|
|
128
|
+
writeUint16(0),
|
|
129
|
+
nameBytes
|
|
130
|
+
]);
|
|
131
|
+
localChunks.push(localHeader, data);
|
|
132
|
+
const centralHeader = concatBytes([
|
|
133
|
+
writeUint32(0x02014b50),
|
|
134
|
+
writeUint16(20),
|
|
135
|
+
writeUint16(20),
|
|
136
|
+
writeUint16(0),
|
|
137
|
+
writeUint16(0),
|
|
138
|
+
writeUint16(0),
|
|
139
|
+
writeUint16(0),
|
|
140
|
+
writeUint32(crc),
|
|
141
|
+
writeUint32(data.byteLength),
|
|
142
|
+
writeUint32(data.byteLength),
|
|
143
|
+
writeUint16(nameBytes.byteLength),
|
|
144
|
+
writeUint16(0),
|
|
145
|
+
writeUint16(0),
|
|
146
|
+
writeUint16(0),
|
|
147
|
+
writeUint16(0),
|
|
148
|
+
writeUint32(0),
|
|
149
|
+
writeUint32(offset),
|
|
150
|
+
nameBytes
|
|
151
|
+
]);
|
|
152
|
+
centralChunks.push(centralHeader);
|
|
153
|
+
offset += localHeader.byteLength + data.byteLength;
|
|
154
|
+
}
|
|
155
|
+
const centralDirectory = concatBytes(centralChunks);
|
|
156
|
+
const endRecord = concatBytes([
|
|
157
|
+
writeUint32(0x06054b50),
|
|
158
|
+
writeUint16(0),
|
|
159
|
+
writeUint16(0),
|
|
160
|
+
writeUint16(snapshot.files.length),
|
|
161
|
+
writeUint16(snapshot.files.length),
|
|
162
|
+
writeUint32(centralDirectory.byteLength),
|
|
163
|
+
writeUint32(offset),
|
|
164
|
+
writeUint16(0)
|
|
165
|
+
]);
|
|
166
|
+
const zipBytes = concatBytes([...localChunks, centralDirectory, endRecord]);
|
|
167
|
+
const output = options?.asBlob && typeof Blob !== "undefined"
|
|
168
|
+
? new Blob([zipBytes], { type: "application/zip" })
|
|
169
|
+
: zipBytes;
|
|
170
|
+
if (options?.clearCache ?? true) {
|
|
171
|
+
await snapshot.content.dispose?.();
|
|
172
|
+
}
|
|
173
|
+
return output;
|
|
174
|
+
};
|
|
175
|
+
exports.toZip = toZip;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pagepocket/lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Library for rewriting HTML snapshots and inlining local resources.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"cheerio": "^1.0.0-rc.12",
|
|
15
|
-
"@pagepocket/uni-fs": "0.
|
|
15
|
+
"@pagepocket/uni-fs": "0.5.1"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/node": "^20.11.30",
|