-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathdevtools.ts
More file actions
103 lines (97 loc) · 2.84 KB
/
devtools.ts
File metadata and controls
103 lines (97 loc) · 2.84 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
import { loadValue } from '@logux/client'
import { busyDuring } from './busy.ts'
import { HTTPStatusError } from './errors.ts'
import { getFeed, getFeedLatestPosts, getFeeds } from './feed.ts'
import { loadFilters } from './filter.ts'
import { createDownloadTask } from './lib/download.ts'
import { loaders } from './loader/index.ts'
import {
addPost,
deletePost,
getPost,
getPosts,
processOriginPost
} from './post.ts'
import { proxyDebug } from './request.ts'
import { router } from './router.ts'
/**
* Create test feeds and posts for new client.
*/
export async function fillFeedsWithPosts(): Promise<void> {
await busyDuring(async () => {
let task = createDownloadTask()
let feeds = await loadValue(getFeeds())
await Promise.all(
feeds.list.map(async feed => {
let old = await loadValue(getPosts({ feedId: feed.id }))
for (let post of old.list) {
await deletePost(post.id)
}
let posts = await getFeedLatestPosts(feed, task).next()
let filters = await loadFilters({ feedId: feed.id })
for (let origin of posts) {
let reading = filters(origin) ?? feed.reading
if (reading !== 'delete') {
await addPost(processOriginPost(origin, feed.id, reading))
}
}
})
)
})
}
/**
* Show post data in browser DevTools on opening post in popup.
*/
export function enablePostDebug(): void {
router.subscribe(async page => {
for (let popup of page.popups) {
if (popup.popup === 'post') {
let id: string | undefined
if (popup.param.startsWith('id:')) {
id = popup.param.slice(3)
} else if (popup.param.startsWith('read:')) {
id = popup.param.slice(5)
}
if (id) {
let post = await loadValue(getPost(id))
if (!post) return
let feed = await loadValue(getFeed(post.feedId))
let source = await loaders[feed!.loader].getPostSource(
feed!,
post.originId
)
console.log(post)
console.log(source)
}
}
}
})
}
function decodeHeader(value: null | string): string {
return value ? value.replaceAll('\\n', '\n') : ''
}
/**
* Create a channel to print response's headers from the proxy.
*/
export function enableProxyDebug(): void {
proxyDebug.set(headers => {
console.log(
`Proxy request:\n${decodeHeader(headers.get('x-slowreader-request'))}`
)
console.log(
`Proxy response: \n${decodeHeader(headers.get('x-slowreader-response'))}`
)
})
}
export function printWarning(e: unknown): {
details: unknown[]
title: string
} {
let title = e instanceof Error ? e.message : String(e)
let details: unknown[] = []
if (e instanceof Error) {
details.push(e)
if (e instanceof HTTPStatusError) details.push(e.response)
}
return { details, title }
}