-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
336 lines (323 loc) · 12 KB
/
index.js
File metadata and controls
336 lines (323 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const puppeteer = require('puppeteer')
const cheerio = require('cheerio')
const fs = require('fs-extra')
const path = require('path')
const url = require('url')
const axios = require('axios')
/**
* Global Variables
*/
const visitedUrlSet = new Set()
const queuedUrlSet = new Set()
let completedItemCount = 0
const supportedAssetExtensions = [
'.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp', '.ico',
'.css', '.js', '.woff', '.woff2', '.ttf', '.otf', '.eot',
'.mp4', '.webm', '.ogg', '.mp3', '.wav'
]
const assetRegexPattern = new RegExp('([a-zA-Z0-9_\\-\\/.]+(' + supportedAssetExtensions.map(ext => ext.replace('.', '\\.')).join('|') + '))', 'gi')
/**
* Print Progress Status
* Params: queueLength, completedCount
*/
function printProgressStatus(queueLength, completedCount) {
const progressPercent = ((completedCount / (queueLength + completedCount)) * 100).toFixed(1)
process.stdout.write(`\r[${completedCount}/${queueLength + completedCount}] | Queue Left: ${queueLength} | Progress: ${progressPercent}% `)
}
/**
* Check Local File Validity
* Params: localFilePath
*/
async function isLocalFileValid(localFilePath) {
try {
const fileStat = await fs.stat(localFilePath)
if (fileStat.isFile() && fileStat.size > 0) {
return true
} else {
return false
}
} catch {
return false
}
}
/**
* Find All Asset Links By Regex
* Params: textContent, baseUrl
*/
function findAllAssetLinksByRegex(textContent, baseUrl) {
const foundAssets = []
let matchedResult
while ((matchedResult = assetRegexPattern.exec(textContent)) !== null) {
let assetLink = matchedResult[1]
if (assetLink.startsWith('data:')) { continue }
if (assetLink.startsWith('http') || assetLink.startsWith('//')) {
assetLink = assetLink.startsWith('//') ? 'https:' + assetLink : assetLink
} else {
assetLink = url.resolve(baseUrl, assetLink)
}
if (!(queuedUrlSet.has(assetLink))) {
foundAssets.push(assetLink)
}
}
return foundAssets
}
/**
* Parse CSS File And Queue Asset
* Params: cssFilePath, cssFileUrl, outputFolderPath, assetQueueList
*/
async function parseCssFileAndQueueAsset(cssFilePath, cssFileUrl, outputFolderPath, assetQueueList) {
let cssContent
try {
cssContent = await fs.readFile(cssFilePath, 'utf-8')
} catch { return }
const cssBaseDir = cssFileUrl.endsWith('/') ? cssFileUrl : cssFileUrl.substring(0, cssFileUrl.lastIndexOf('/') + 1)
const urlRegex = /url\((['"]?)([^'")]+)\1\)/g
let matchedResult
while ((matchedResult = urlRegex.exec(cssContent)) !== null) {
let cssAssetPath = matchedResult[2]
if (cssAssetPath.startsWith('data:')) { continue }
let cssAssetUrl = cssAssetPath.startsWith('http') || cssAssetPath.startsWith('//')
? (cssAssetPath.startsWith('//') ? 'https:' + cssAssetPath : cssAssetPath)
: url.resolve(cssBaseDir, cssAssetPath)
if (
cssAssetUrl.match(/\.(woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|css|js)$/i)
&& !(queuedUrlSet.has(cssAssetUrl))
) {
console.log('[CSS url()]', cssAssetUrl)
assetQueueList.push({ url: cssAssetUrl, type: 'asset' })
queuedUrlSet.add(cssAssetUrl)
}
}
const importUrlRegex = /@import\s+url\((['"]?)([^'")]+)\1\)/g
while ((matchedResult = importUrlRegex.exec(cssContent)) !== null) {
let importAssetPath = matchedResult[2]
if (importAssetPath.startsWith('data:')) { continue }
let importAssetUrl = importAssetPath.startsWith('http') || importAssetPath.startsWith('//')
? (importAssetPath.startsWith('//') ? 'https:' + importAssetPath : importAssetPath)
: url.resolve(cssBaseDir, importAssetPath)
if (
importAssetUrl.match(/\.css$/i)
&& !(queuedUrlSet.has(importAssetUrl))
) {
console.log('[CSS @import]', importAssetUrl)
assetQueueList.push({ url: importAssetUrl, type: 'asset' })
queuedUrlSet.add(importAssetUrl)
}
}
const allAssetLinks = findAllAssetLinksByRegex(cssContent, cssBaseDir)
for (const regexAssetUrl of allAssetLinks) {
if (
regexAssetUrl.match(/\.(woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|css|js)$/i)
&& !(queuedUrlSet.has(regexAssetUrl))
) {
console.log('[CSS regex]', regexAssetUrl)
assetQueueList.push({ url: regexAssetUrl, type: 'asset' })
queuedUrlSet.add(regexAssetUrl)
}
}
}
/**
* Download With Retry
* Params: fn, retries, delayMs
*/
async function downloadWithRetry(fn, retries = 3, delayMs = 2000) {
for (let retryAttempt = 1; retryAttempt <= retries; retryAttempt++) {
try {
return await fn()
} catch {
if (retryAttempt === retries) { throw new Error('Retry Failed') }
await new Promise(resolve => setTimeout(resolve, delayMs))
}
}
}
/**
* Start Cloning Process
* Params: rootWebsiteUrl, outputFolderPath
*/
async function startCloningProcess(rootWebsiteUrl, outputFolderPath) {
const browserInstance = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] })
const browserPage = await browserInstance.newPage()
const mainQueueList = []
mainQueueList.push({ url: rootWebsiteUrl, type: 'page' })
queuedUrlSet.add(rootWebsiteUrl)
while (mainQueueList.length > 0) {
const currentQueueItem = mainQueueList.shift()
if (currentQueueItem.type === 'page') {
await downloadWithRetry(() => downloadHtmlPage(currentQueueItem.url, rootWebsiteUrl, outputFolderPath, mainQueueList, browserPage))
} else {
await downloadWithRetry(() => downloadAssetFile(currentQueueItem.url, outputFolderPath, mainQueueList))
}
completedItemCount++
printProgressStatus(mainQueueList.length, completedItemCount)
}
await browserInstance.close()
process.stdout.write('\nDone!\n')
}
/**
* Download HTML Page
* Params: pageUrl, rootWebsiteUrl, outputFolderPath, queueList, browserPage
*/
async function downloadHtmlPage(pageUrl, rootWebsiteUrl, outputFolderPath, queueList, browserPage) {
if (visitedUrlSet.has(pageUrl)) { return }
visitedUrlSet.add(pageUrl)
console.log(`\n[Page] Downloading: ${pageUrl}`)
try {
await browserPage.goto(pageUrl, { waitUntil: 'networkidle2', timeout: 60000 })
} catch { return }
const pageContent = await browserPage.content()
const $ = cheerio.load(pageContent)
const htmlBaseDir = pageUrl.endsWith('/') ? pageUrl : pageUrl.substring(0, pageUrl.lastIndexOf('/') + 1)
const htmlAssetTags = [
['link', 'href'],
['script', 'src'],
['img', 'src'],
['video', 'src'],
['audio', 'src'],
['source', 'src'],
['iframe', 'src'],
['source', 'srcset']
]
for (const [tagName, attrName] of htmlAssetTags) {
$(`${tagName}[${attrName}]`).each((_, tagEl) => {
let assetSourceUrl = $(tagEl).attr(attrName)
if (!assetSourceUrl) { return }
if (assetSourceUrl.startsWith('http') || assetSourceUrl.startsWith('//')) {
assetSourceUrl = assetSourceUrl.startsWith('//') ? 'https:' + assetSourceUrl : assetSourceUrl
} else {
assetSourceUrl = url.resolve(htmlBaseDir, assetSourceUrl)
}
if (
assetSourceUrl.match(/\.(woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|css|js)$/i)
&& !(queuedUrlSet.has(assetSourceUrl))
) {
console.log('[TAG]', assetSourceUrl)
queueList.push({ url: assetSourceUrl, type: 'asset' })
queuedUrlSet.add(assetSourceUrl)
}
})
}
$('style').each((_, styleTag) => {
const cssTextContent = $(styleTag).html()
const cssUrlRegex = /url\((['"]?)([^'")]+)\1\)/g
let matchedCss
while ((matchedCss = cssUrlRegex.exec(cssTextContent)) !== null) {
let styleAssetPath = matchedCss[2]
if (styleAssetPath.startsWith('data:')) { continue }
let styleAssetUrl = styleAssetPath.startsWith('http') || styleAssetPath.startsWith('//')
? (styleAssetPath.startsWith('//') ? 'https:' + styleAssetPath : styleAssetPath)
: url.resolve(htmlBaseDir, styleAssetPath)
if (
styleAssetUrl.match(/\.(woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|css|js)$/i)
&& !(queuedUrlSet.has(styleAssetUrl))
) {
console.log('[STYLE url()]', styleAssetUrl)
queueList.push({ url: styleAssetUrl, type: 'asset' })
queuedUrlSet.add(styleAssetUrl)
}
}
})
const foundAssetsByRegex = findAllAssetLinksByRegex(pageContent, pageUrl)
for (const regexAssetUrl of foundAssetsByRegex) {
if (
regexAssetUrl.match(/\.(woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|css|js)$/i)
&& !(queuedUrlSet.has(regexAssetUrl))
) {
console.log('[HTML regex]', regexAssetUrl)
queueList.push({ url: regexAssetUrl, type: 'asset' })
queuedUrlSet.add(regexAssetUrl)
}
}
await saveClonedFile(pageUrl, rootWebsiteUrl, outputFolderPath, pageContent)
$('a[href]').each((_, anchorTag) => {
let hrefLink = $(anchorTag).attr('href')
if (
hrefLink &&
!(hrefLink.startsWith('http')) &&
!(hrefLink.startsWith('//')) &&
!(hrefLink.startsWith('mailto:')) &&
!(hrefLink.startsWith('#'))
) {
hrefLink = url.resolve(htmlBaseDir, hrefLink)
if (!(queuedUrlSet.has(hrefLink))) {
queueList.push({ url: hrefLink, type: 'page' })
queuedUrlSet.add(hrefLink)
}
} else if (
hrefLink &&
hrefLink.startsWith(rootWebsiteUrl) &&
!(queuedUrlSet.has(hrefLink))
) {
queueList.push({ url: hrefLink, type: 'page' })
queuedUrlSet.add(hrefLink)
}
})
}
/**
* Download Asset File
* Params: assetUrl, outputFolderPath, assetQueueList
*/
async function downloadAssetFile(assetUrl, outputFolderPath, assetQueueList) {
if (visitedUrlSet.has(assetUrl)) { return }
visitedUrlSet.add(assetUrl)
console.log(`\n[Asset] Downloading: ${assetUrl}`)
try {
const parsedAssetUrl = new url.URL(assetUrl)
let assetFilePath = parsedAssetUrl.pathname
if (assetFilePath.endsWith('/')) { assetFilePath += 'index.html' }
if (assetFilePath === '') { assetFilePath = '/index.html' }
const localAssetPath = path.join(outputFolderPath, assetFilePath)
if (await isLocalFileValid(localAssetPath)) {
if (assetUrl.endsWith('.css')) {
await parseCssFileAndQueueAsset(localAssetPath, assetUrl, outputFolderPath, assetQueueList)
}
return
}
const response = await axios.get(assetUrl, { responseType: 'arraybuffer', timeout: 60000, headers: { 'User-Agent': 'Mozilla/5.0' } })
await saveClonedFile(assetUrl, '', outputFolderPath, response.data, true)
if (assetUrl.endsWith('.css')) {
await parseCssFileAndQueueAsset(localAssetPath, assetUrl, outputFolderPath, assetQueueList)
}
} catch {
return
}
}
/**
* Save Cloned File
* Params: fileUrl, rootWebsiteUrl, outputFolderPath, fileContent, isBinary
*/
async function saveClonedFile(fileUrl, rootWebsiteUrl, outputFolderPath, fileContent, isBinary) {
const parsedFileUrl = new url.URL(fileUrl)
let outputFilePath = parsedFileUrl.pathname
if (outputFilePath.endsWith('/')) { outputFilePath += 'index.html' }
if (outputFilePath === '') { outputFilePath = '/index.html' }
const localOutputFilePath = path.join(outputFolderPath, outputFilePath)
await fs.mkdir(path.dirname(localOutputFilePath), { recursive: true })
if (!isBinary && (outputFilePath.endsWith('.html') || outputFilePath.endsWith('.css'))) {
fileContent = fileContent.replace(
/(url\(['"]?[^)'"]+\.(woff2?|woff|ttf|otf|eot|svg|png|jpe?g|gif|webp|ico|css|js))([?#][^)'"]*)?(['"]?\))/gmi,
function (_, a, b, c, d) {
return a + (d ? d : ')')
}
)
fileContent = fileContent.replace(
/((?:src|href)=["'][^"']+\.(?:woff2?|woff|ttf|otf|eot|svg|png|jpe?g|gif|webp|ico|css|js))([?#][^"']*)/gi,
'$1'
)
}
if (isBinary) {
await fs.writeFile(localOutputFilePath, fileContent)
} else {
await fs.writeFile(localOutputFilePath, fileContent, 'utf-8')
}
}
/**
* CLI Entrypoint
*/
const inputUserUrl = process.argv[2]
const outputTargetFolder = process.argv[3] || './output'
if (!(inputUserUrl)) {
console.log('Missing Url')
process.exit(1)
}
const normalizedRootUrl = inputUserUrl.endsWith('/') ? inputUserUrl : inputUserUrl + '/'
startCloningProcess(normalizedRootUrl, outputTargetFolder)