Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved fetch call from rendering process to main process #633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bridges/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ const utilsBridge = {
initFontList: (): Promise<Array<string>> => {
return ipcRenderer.invoke("init-font-list")
},

fetchText: (url: string, isHtml: boolean = false) => {
return ipcRenderer.invoke("fetchText", url, isHtml)
}
}

declare global {
Expand Down
7 changes: 3 additions & 4 deletions src/components/article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SourceTextDirection,
} from "../scripts/models/source"
import { shareSubmenu } from "./context-menu"
import { platformCtrl, decodeFetchResponse } from "../scripts/utils"
import { platformCtrl } from "../scripts/utils"

const FONT_SIZE_OPTIONS = [12, 13, 14, 15, 16, 17, 18, 19, 20]

Expand Down Expand Up @@ -330,9 +330,8 @@ class Article extends React.Component<ArticleProps, ArticleState> {
this.setState({ fullContent: "", loaded: false, error: false })
const link = this.props.item.link
try {
const result = await fetch(link)
if (!result || !result.ok) throw new Error()
const html = await decodeFetchResponse(result, true)
const html = await window.utils.fetchText(link, true)
if (!html) throw new Error()
if (link === this.props.item.link) {
this.setState({ fullContent: html })
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, shell, dialog, app, session, clipboard } from "electron"
import { ipcMain, shell, dialog, app, session, clipboard, net } from "electron"
import { WindowManager } from "./window"
import fs = require("fs")
import { ImageCallbackTypes, TouchBarTexts } from "../schema-types"
Expand Down Expand Up @@ -215,7 +215,7 @@ export function setUtilsListeners(manager: WindowManager) {
`new Promise(resolve => {
const dismiss = () => {
document.removeEventListener("mousedown", dismiss)
document.removeEventListener("scroll", dismiss)
document.removeEventListener("scroll", dismiss)
resolve()
}
document.addEventListener("mousedown", dismiss)
Expand Down Expand Up @@ -308,4 +308,12 @@ export function setUtilsListeners(manager: WindowManager) {
disableQuoting: true,
})
})

ipcMain.handle("fetchText", async (_, url, isHtml) => {
const response = await net.fetch(url)
if (response.status < 200 || response.status >= 300) {
throw new Error('Server error (' + response.status + ') ' + response.statusText)
}
return await response.text()
})
}
89 changes: 21 additions & 68 deletions src/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,17 @@ const rssParser = new Parser({
type extractGeneric<Type> = Type extends Parser<infer _, infer U> ? U : never
export type MyParserItem = extractGeneric<typeof rssParser> & Parser.Item

const CHARSET_RE = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i
const XML_ENCODING_RE = /^<\?xml.+encoding="(.+?)".*?\?>/i
export async function decodeFetchResponse(response: Response, isHTML = false) {
const buffer = await response.arrayBuffer()
let ctype =
response.headers.has("content-type") &&
response.headers.get("content-type")
let charset =
ctype && CHARSET_RE.test(ctype) ? CHARSET_RE.exec(ctype)[1] : undefined
let content = new TextDecoder(charset).decode(buffer)
if (charset === undefined) {
if (isHTML) {
const dom = domParser.parseFromString(content, "text/html")
charset = dom
.querySelector("meta[charset]")
?.getAttribute("charset")
?.toLowerCase()
if (!charset) {
ctype = dom
.querySelector("meta[http-equiv='Content-Type']")
?.getAttribute("content")
charset =
ctype &&
CHARSET_RE.test(ctype) &&
CHARSET_RE.exec(ctype)[1].toLowerCase()
}
} else {
charset =
XML_ENCODING_RE.test(content) &&
XML_ENCODING_RE.exec(content)[1].toLowerCase()
}
if (charset && charset !== "utf-8" && charset !== "utf8") {
content = new TextDecoder(charset).decode(buffer)
}
}
return content
}

export async function parseRSS(url: string) {
let result: Response
let html: string
try {
result = await fetch(url, { credentials: "omit" })
} catch {
html = await window.utils.fetchText(url)
} catch (e) {
throw new Error(intl.get("log.networkError"))
}
if (result && result.ok) {
try {
return await rssParser.parseString(
await decodeFetchResponse(result)
)
} catch {
throw new Error(intl.get("log.parseError"))
}
} else {
throw new Error(result.status + " " + result.statusText)
try {
return await rssParser.parseString(html)
} catch {
throw new Error(intl.get("log.parseError"))
}
}

Expand All @@ -98,23 +54,20 @@ export const domParser = new DOMParser()
export async function fetchFavicon(url: string) {
try {
url = url.split("/").slice(0, 3).join("/")
let result = await fetch(url, { credentials: "omit" })
if (result.ok) {
let html = await result.text()
let dom = domParser.parseFromString(html, "text/html")
let links = dom.getElementsByTagName("link")
for (let link of links) {
let rel = link.getAttribute("rel")
if (
(rel === "icon" || rel === "shortcut icon") &&
link.hasAttribute("href")
) {
let href = link.getAttribute("href")
let parsedUrl = Url.parse(url)
if (href.startsWith("//")) return parsedUrl.protocol + href
else if (href.startsWith("/")) return url + href
else return href
}
const html = await window.utils.fetchText(url)
let dom = domParser.parseFromString(html, "text/html")
let links = dom.getElementsByTagName("link")
for (let link of links) {
let rel = link.getAttribute("rel")
if (
(rel === "icon" || rel === "shortcut icon") &&
link.hasAttribute("href")
) {
let href = link.getAttribute("href")
let parsedUrl = Url.parse(url)
if (href.startsWith("//")) return parsedUrl.protocol + href
else if (href.startsWith("/")) return url + href
else return href
}
}
url = url + "/favicon.ico"
Expand Down