forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
344 lines (315 loc) · 8.06 KB
/
utils.ts
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
337
338
339
340
341
342
343
344
import './dom-parser.ts'
import {
createDownloadTask,
enableTestTime,
getLoaderForText,
getTestEnvironment,
loaders,
type PreviewCandidate,
previewCandidates,
previewCandidatesLoading,
setBaseTestRoute,
setPreviewUrl,
setRequestMethod,
setupEnvironment,
userId
} from '@slowreader/core'
import type { ReadableAtom } from 'nanostores'
import { readFile } from 'node:fs/promises'
import { isAbsolute, join } from 'node:path'
import readline from 'node:readline'
import { isatty } from 'node:tty'
import { styleText } from 'node:util'
export interface LoaderTestFeed {
homeUrl?: string
title: string
url: string
}
export async function readText(path: string): Promise<string> {
let absolute = path
if (!isAbsolute(absolute)) {
absolute = join(process.env.INIT_CWD ?? process.cwd(), path)
}
let buffer = await readFile(absolute)
return buffer.toString('utf-8')
}
export function isString(attr: null | string): attr is string {
return typeof attr === 'string' && attr.length > 0
}
export function enableTestClient(): void {
setupEnvironment(getTestEnvironment())
enableTestTime()
userId.set('10')
setBaseTestRoute({ params: {}, route: 'home' })
setRequestMethod(fetch)
}
export function timeout<Value>(
ms: number,
promise: Promise<Value>
): Promise<Value> {
return Promise.race([
promise,
new Promise<Value>((resolve, reject) =>
setTimeout(() => {
reject(new Error('Timeout'))
}, ms)
)
])
}
export function waitFor<Value>(
store: ReadableAtom,
value: Value
): Promise<void> {
return new Promise<void>(resolve => {
let unbind = store.listen(state => {
if (state === value) {
unbind()
resolve()
}
})
})
}
interface NoFileError extends Error {
code: string
path: string
}
function isNoFileError(e: unknown): e is NoFileError {
return e instanceof Error && `code` in e && e.code === 'ENOENT'
}
let progress = 0
let totalJobs = 0
export function initializeProgressBar(totalValue: number): void {
if (!process.env.CI) {
totalJobs = totalValue
renderProgressBar()
}
}
function renderProgressBar(): void {
let filled = Math.floor((process.stderr.columns * progress) / totalJobs)
process.stderr.write(
'█'.repeat(filled) + '░'.repeat(process.stderr.columns - filled) + '\n'
)
readline.moveCursor(process.stderr, 0, 0)
}
const SIMPLE_SHELL = !isatty(1) || process.env.CI
function updateProgressBar(): void {
if (totalJobs > 0 && progress < totalJobs && !SIMPLE_SHELL) {
progress += 1
readline.moveCursor(process.stderr, 0, -1)
readline.clearLine(process.stderr, 0)
if (progress < totalJobs) {
renderProgressBar()
}
}
}
export function print(msg: string): void {
if (totalJobs > 0 && progress < totalJobs && !SIMPLE_SHELL) {
readline.moveCursor(process.stderr, 0, -1)
readline.clearLine(process.stderr, 0)
process.stderr.write(`${msg}\n`)
renderProgressBar()
} else {
process.stderr.write(`${msg}\n`)
}
}
let errors = 0
export function error(err: string | unknown, details?: string): void {
errors += 1
let msg: string
if (isNoFileError(err)) {
msg = `File not found: ${err.path}`
} else if (err instanceof Error) {
msg = err.stack ?? err.message
} else {
msg = String(err)
}
print('')
print(
styleText('bold', styleText('bgRed', ' ERROR ')) +
' ' +
styleText('bold', styleText('red', msg))
)
if (details) print(details)
print('')
updateProgressBar()
}
export function finish(msg: string): void {
print('')
let postfix = ''
if (errors > 0) {
postfix =
', ' + styleText('red', styleText('bold', `${errors} errors found`))
}
print(styleText('gray', msg + postfix))
process.exit(errors > 0 ? 1 : 0)
}
export function success(msg: string, details?: string): void {
if (details) {
msg += ` ${styleText('gray', details)}`
}
print(styleText('green', styleText('bold', '✓ ') + msg))
updateProgressBar()
}
export function semiSuccess(msg: string, note: string): void {
print(
styleText(
'yellow',
styleText('bold', '✓ ') + msg + ' ' + styleText('bold', note)
)
)
updateProgressBar()
}
export async function fetchAndParsePosts(
url: string,
badSource = false
): Promise<void> {
try {
let task = createDownloadTask()
let response = await task.text(url)
if (badSource && response.status >= 400) {
semiSuccess(url, `${response.status}`)
return
}
if (
badSource &&
response.redirected &&
response.contentType === 'text/html' &&
response.text.toLocaleLowerCase().includes('<html')
) {
semiSuccess(url, `redirect to HTML`)
return
}
let candidate: false | PreviewCandidate = getLoaderForText(response)
if (!candidate) {
error(`Can not found loader for feed ${url}`)
return
}
let loader = loaders[candidate.loader]
let { list } = loader.getPosts(task, url, response).get()
if (list.length === 0) {
if (badSource) {
semiSuccess(url, '0 posts')
} else {
error(`Can not found posts for feed ${url}`)
}
} else {
success(url, list.length + (list.length > 1 ? ' posts' : ' post'))
}
} catch (e) {
error(e, `During loading posts for ${url}`)
}
}
function normalizeUrl(url: string): string {
return url
.replace(/^(https?:)?\/\//, '')
.replace(/\/\/www\./, '//')
.replace(/\/$/, '')
.toLowerCase()
}
export async function findRSSfromHome(
feed: LoaderTestFeed,
tries = 0
): Promise<boolean> {
let unbindPreview = previewCandidates.listen(() => {})
try {
let homeUrl = feed.homeUrl || getHomeUrl(feed.url)
setPreviewUrl(homeUrl)
try {
await timeout(10_000, waitFor(previewCandidatesLoading, false))
} catch (e) {
if (e instanceof Error && e.message === 'Timeout' && tries > 0) {
return findRSSfromHome(feed, tries - 1)
} else {
throw e
}
}
let normalizedUrls = previewCandidates.get().map(i => normalizeUrl(i.url))
if (normalizedUrls.includes(normalizeUrl(feed.url))) {
success(`Feed ${feed.title} has feed URL at home`)
return true
} else if (previewCandidates.get().length === 0) {
error(
`Can’t find any feed from home URL or ${feed.title}`,
`Home URL: ${homeUrl}\nFeed URL: ${feed.url}`
)
return false
} else {
error(
`Can’t find ${feed.title} feed from home URL`,
`Home URL: ${homeUrl}\n` +
`Found: ${previewCandidates
.get()
.map(i => i.url)
.join('\n ')}\n` +
`Feed URL: ${feed.url}`
)
return false
}
} catch (e) {
error(
e,
`During searching for feed from home URL\n` +
`Home URL: ${feed.homeUrl}\n` +
`Feed URL: ${feed.url}`
)
return false
} finally {
unbindPreview()
}
}
export async function completeTasks(
tasks: (() => Promise<void>)[]
): Promise<void> {
return new Promise(resolve => {
let running = 4
function runTask(): void {
let task = tasks.pop()
if (task) {
task().then(runTask)
} else {
running -= 1
if (running === 0) resolve()
}
}
for (let i = 0; i < running; i++) {
runTask()
}
})
}
function getHomeUrl(feedUrl: string): string {
let url = new URL(feedUrl)
url.pathname = '/'
return url.toString()
}
export interface CLI {
run(cb: (args: string[]) => Promise<void> | void): Promise<void>
wrongArg(message: string): void
}
export function createCLI(help: string, usage?: string): CLI {
return {
async run(cb) {
let args = process.argv.slice(2)
if (
args.includes('--help') ||
args.includes('-h') ||
args.includes('help')
) {
print(help)
if (usage) print('Usage:\n' + usage)
process.exit(0)
} else {
try {
await cb(args)
} catch (e) {
error(e)
process.exit(1)
}
}
},
wrongArg(message) {
error(message)
if (usage) print('Usage:\n' + usage)
process.exit(1)
}
}
}