Skip to content

Commit 9442ded

Browse files
committed
Simplify har-view using keyof typeof PAGES.
1 parent 1e2ad6a commit 9442ded

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

browser-extension/tests/har-view.ts

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,18 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
88
const app = express()
99
const PORT = 3001
1010

11-
// Store HAR data
12-
const harCache = new Map<string, any>()
13-
14-
// Create mapping from HAR filename to original URL
15-
const harToUrlMap = Object.fromEntries(
16-
Object.entries(PAGES).map(([key, url]) => [`${key}.har`, url]),
17-
)
11+
// Store HAR json
12+
const harCache = new Map<keyof typeof PAGES, any>()
1813

1914
// Extract URL parts for location patching
20-
function getUrlParts(filename: string) {
21-
const originalUrl = harToUrlMap[filename]
22-
if (!originalUrl) {
23-
return null
24-
}
25-
26-
try {
27-
const url = new URL(originalUrl)
28-
return {
29-
host: url.host,
30-
hostname: url.hostname,
31-
href: originalUrl,
32-
pathname: url.pathname,
33-
}
34-
} catch {
35-
return null
15+
function getUrlParts(key: keyof typeof PAGES) {
16+
const originalUrl = PAGES[key]
17+
const url = new URL(originalUrl)
18+
return {
19+
host: url.host,
20+
hostname: url.hostname,
21+
href: originalUrl,
22+
pathname: url.pathname,
3623
}
3724
}
3825

@@ -50,23 +37,23 @@ async function checkDevServer(): Promise<boolean> {
5037
}
5138

5239
// Load and cache HAR file
53-
async function loadHar(filename: string) {
54-
if (harCache.has(filename)) {
55-
return harCache.get(filename)
40+
async function loadHar(key: keyof typeof PAGES) {
41+
if (harCache.has(key)) {
42+
return harCache.get(key)
5643
}
5744

58-
const harPath = path.join(__dirname, 'har', filename)
45+
const harPath = path.join(__dirname, 'har', `${key}.har`)
5946
const harContent = await fs.readFile(harPath, 'utf-8')
6047
const harData = JSON.parse(harContent)
61-
harCache.set(filename, harData)
48+
harCache.set(key, harData)
6249
return harData
6350
}
6451

6552
// Add redirect routes for each PAGES URL to handle refreshes
6653
Object.entries(PAGES).forEach(([key, url]) => {
6754
const urlObj = new URL(url)
6855
app.get(urlObj.pathname, (_req, res) => {
69-
res.redirect(`/page/${key}.har/gitcasso`)
56+
res.redirect(`/page/${key}/gitcasso`)
7057
})
7158
})
7259

@@ -94,8 +81,8 @@ app.get('/', async (_req, res) => {
9481
<li>
9582
<div style="margin-bottom: 10px; font-weight: bold; color: #555;">${basename}</div>
9683
<div style="display: flex; gap: 10px;">
97-
<a href="/page/${file}" style="flex: 1; text-align: center;">🔍 Clean</a>
98-
<a href="/page/${file}/gitcasso" style="flex: 1; text-align: center; ${!devServerRunning ? 'opacity: 0.5; pointer-events: none;' : ''}">
84+
<a href="/page/${basename}" style="flex: 1; text-align: center;">🔍 Clean</a>
85+
<a href="/page/${basename}/gitcasso" style="flex: 1; text-align: center; ${!devServerRunning ? 'opacity: 0.5; pointer-events: none;' : ''}">
9986
🚀 Gitcasso-enabled
10087
</a>
10188
</div>
@@ -147,14 +134,14 @@ app.get('/', async (_req, res) => {
147134
})
148135

149136
// Serve the main HTML page from HAR
150-
app.get('/page/:filename', async (req, res) => {
137+
app.get('/page/:key', async (req, res) => {
151138
try {
152-
const filename = req.params.filename
153-
if (!filename.endsWith('.har')) {
154-
return res.status(400).send('Invalid file type')
139+
const key = req.params.key as keyof typeof PAGES
140+
if (!(key in PAGES)) {
141+
return res.status(400).send('Invalid key - not found in PAGES')
155142
}
156143

157-
const harData = await loadHar(filename)
144+
const harData = await loadHar(key)
158145

159146
// Find the main HTML response
160147
const mainEntry = harData.log.entries.find(
@@ -173,7 +160,7 @@ app.get('/page/:filename', async (req, res) => {
173160
// Replace external URLs with local asset URLs
174161
html = html.replace(
175162
/https:\/\/(github\.com|assets\.github\.com|avatars\.githubusercontent\.com|user-images\.githubusercontent\.com)/g,
176-
`/asset/${filename.replace('.har', '')}`,
163+
`/asset/${key}`,
177164
)
178165

179166
return res.send(html)
@@ -184,20 +171,17 @@ app.get('/page/:filename', async (req, res) => {
184171
})
185172

186173
// Serve the main HTML page from HAR with Gitcasso content script injected
187-
app.get('/page/:filename/gitcasso', async (req, res) => {
174+
app.get('/page/:key/gitcasso', async (req, res) => {
188175
try {
189-
const filename = req.params.filename
190-
if (!filename.endsWith('.har')) {
191-
return res.status(400).send('Invalid file type')
176+
const key = req.params.key as keyof typeof PAGES
177+
if (!(key in PAGES)) {
178+
return res.status(400).send('Invalid key - not found in PAGES')
192179
}
193180

194181
// Get original URL parts for location patching
195-
const urlParts = getUrlParts(filename)
196-
if (!urlParts) {
197-
return res.status(400).send('Unknown HAR file - not found in har-index.ts')
198-
}
182+
const urlParts = getUrlParts(key)
199183

200-
const harData = await loadHar(filename)
184+
const harData = await loadHar(key)
201185

202186
// Find the main HTML response
203187
const mainEntry = harData.log.entries.find(
@@ -216,7 +200,7 @@ app.get('/page/:filename/gitcasso', async (req, res) => {
216200
// Replace external URLs with local asset URLs
217201
html = html.replace(
218202
/https:\/\/(github\.com|assets\.github\.com|avatars\.githubusercontent\.com|user-images\.githubusercontent\.com)/g,
219-
`/asset/${filename.replace('.har', '')}`,
203+
`/asset/${key}`,
220204
)
221205

222206
// Inject patched content script with location patching
@@ -286,12 +270,15 @@ app.get('/page/:filename/gitcasso', async (req, res) => {
286270
})
287271

288272
// Serve assets from HAR file
289-
app.get('/asset/:harname/*', async (req, res) => {
273+
app.get('/asset/:key/*', async (req, res) => {
290274
try {
291-
const harname = `${req.params.harname}.har`
275+
const key = req.params.key as keyof typeof PAGES
276+
if (!(key in PAGES)) {
277+
return res.status(400).send('Invalid key - not found in PAGES')
278+
}
292279
const assetPath = (req.params as any)[0] as string
293280

294-
const harData = await loadHar(harname)
281+
const harData = await loadHar(key)
295282

296283
// Find matching asset in HAR
297284
const assetEntry = harData.log.entries.find((entry: any) => {

0 commit comments

Comments
 (0)