Skip to content

Commit 5d06eb4

Browse files
authored
feat: use SSE for analyzer service (#5)
* teleemetry service with sse * cleanup * use serrvice.vuetelemetry.com url
1 parent 4d214a7 commit 5d06eb4

File tree

2 files changed

+81
-28
lines changed

2 files changed

+81
-28
lines changed

src/background.js

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,55 @@ browser.runtime.onMessage.addListener(
2828
const showcase = tabsStorage[tabId]
2929
if (showcase.hasVue && !showcase.slug) {
3030
try {
31-
const res = await fetch(`https://vuetelemetry.com/api/analyze?url=${message.payload.url}`, {
32-
method: 'GET'
33-
})
34-
.then((response) => {
35-
if (!response.ok) {
31+
if (typeof EventSource === 'undefined') {
32+
console.log('EventSource is not supported in current borwser!')
33+
return
34+
}
35+
const sse = new EventSource(
36+
`https://service.vuetelemetry.com?url=${message.payload.url}`
37+
)
38+
sse.addEventListener('message', (event) => {
39+
try {
40+
const res = JSON.parse(event.data)
41+
if (!res.error && !res.isAdultContent) {
42+
showcase.isPublic = res.isPublic
43+
showcase.slug = res.slug
44+
sse.close()
45+
46+
// temporary fix when hit CSP
47+
if (!showcase.modules.length && res.modules.length) {
48+
showcase.modules = res.modules
49+
}
50+
if (!showcase.plugins.length && res.plugins.length) {
51+
showcase.plugins = res.plugins
52+
}
53+
} else {
3654
throw new Error('API call to VT failed')
3755
}
38-
return response.json()
39-
})
40-
showcase.isPublic = res.body.isPublic
41-
showcase.slug = res.body.slug
56+
} catch (err) {
57+
sse.close()
58+
}
59+
})
4260

43-
// temporary fix when hit CSP
44-
if (!showcase.modules.length && res.body.modules.length) {
45-
showcase.modules = res.body.modules
46-
}
47-
if (!showcase.plugins.length && res.body.plugins.length) {
48-
showcase.plugins = res.body.plugins
49-
}
61+
// const res = await fetch(`https://vuetelemetry.com/api/analyze?url=${message.payload.url}`, {
62+
// method: 'GET'
63+
// })
64+
// .then((response) => {
65+
// if (!response.ok) {
66+
// throw new Error('API call to VT failed')
67+
// }
68+
// return response.json()
69+
// })
70+
// showcase.isPublic = res.body.isPublic
71+
// showcase.slug = res.body.slug
72+
73+
// // temporary fix when hit CSP
74+
// if (!showcase.modules.length && res.body.modules.length) {
75+
// showcase.modules = res.body.modules
76+
// }
77+
// if (!showcase.plugins.length && res.body.plugins.length) {
78+
// showcase.plugins = res.body.plugins
79+
// }
5080
} catch (err) {}
5181
}
5282
// tabsStorage[tabId] = message.payload

src/popup/App.vue

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,43 @@ export default {
295295
this.saving = true
296296
this.savingError = false
297297
try {
298-
await fetch(`https://vuetelemetry.com/api/analyze?url=${this.showcase.url}&isPublic=true&force=true`, {
299-
method: 'GET'
300-
})
301-
.then((response) => {
302-
if (!response.ok) {
298+
if (typeof EventSource === 'undefined') {
299+
console.log('EventSource is not supported in current borwser!')
300+
return
301+
}
302+
const sse = new EventSource(
303+
`https://service.vuetelemetry.com?url=${this.showcase.url}&isPublic=true&force=true`
304+
)
305+
sse.addEventListener('message', (event) => {
306+
try {
307+
const res = JSON.parse(event.data)
308+
if (!res.error && !res.isAdultContent) {
309+
this.showcase.slug = res.slug
310+
this.showcase.isPublic = res.isPublic
311+
this.saving = false
312+
sse.close()
313+
} else {
303314
throw new Error('API call to VT failed')
304315
}
305-
return response.json()
306-
})
307-
.then(({ body }) => {
308-
this.showcase.slug = body.slug
309-
this.showcase.isPublic = body.isPublic
310-
this.saving = false
311-
})
316+
} catch (err) {
317+
sse.close()
318+
}
319+
})
320+
321+
// await fetch(`https://vuetelemetry.com/api/analyze?url=${this.showcase.url}&isPublic=true&force=true`, {
322+
// method: 'GET'
323+
// })
324+
// .then((response) => {
325+
// if (!response.ok) {
326+
// throw new Error('API call to VT failed')
327+
// }
328+
// return response.json()
329+
// })
330+
// .then(({ body }) => {
331+
// this.showcase.slug = body.slug
332+
// this.showcase.isPublic = body.isPublic
333+
// this.saving = false
334+
// })
312335
const tabId = this.currentTab.id
313336
await this.sendToBackground({
314337
from: 'popup',

0 commit comments

Comments
 (0)