mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-11-22 16:17:29 +00:00
Ability to record and playback network requests
This commit is contained in:
parent
6fd9907b60
commit
6b4c9e9660
@ -277,7 +277,7 @@ let constants = {
|
|||||||
|
|
||||||
additional_routes: [],
|
additional_routes: [],
|
||||||
|
|
||||||
database_version: 8
|
database_version: 9
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override values from config and export the result
|
// Override values from config and export the result
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const NodeFetch = require("./request_backends/node-fetch")
|
const NodeFetch = require("./request_backends/node-fetch")
|
||||||
const Got = require("./request_backends/got")
|
const Got = require("./request_backends/got")
|
||||||
|
const SavedRequestManager = require("./saved_requests/manager")
|
||||||
|
|
||||||
const constants = require("../constants")
|
const constants = require("../constants")
|
||||||
|
|
||||||
@ -7,7 +8,8 @@ const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
|
|||||||
|
|
||||||
const backendStatusLineMap = new Map([
|
const backendStatusLineMap = new Map([
|
||||||
["node-fetch", "NF "],
|
["node-fetch", "NF "],
|
||||||
["got", "GOT"]
|
["got", "GOT"],
|
||||||
|
["saved", "SAV"]
|
||||||
])
|
])
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,6 +19,8 @@ function request(url, options = {}, settings = {}) {
|
|||||||
if (settings.statusLine === undefined) settings.statusLine = "OUT"
|
if (settings.statusLine === undefined) settings.statusLine = "OUT"
|
||||||
if (settings.log === undefined) settings.log = true
|
if (settings.log === undefined) settings.log = true
|
||||||
if (settings.log) console.log(` -> [${settings.statusLine}-${backendStatusLineMap.get(constants.request_backend)}] ${url}`) // todo: make more like pinski?
|
if (settings.log) console.log(` -> [${settings.statusLine}-${backendStatusLineMap.get(constants.request_backend)}] ${url}`) // todo: make more like pinski?
|
||||||
|
const save = !!settings.save
|
||||||
|
|
||||||
if (constants.request_backend === "node-fetch") {
|
if (constants.request_backend === "node-fetch") {
|
||||||
return new NodeFetch(url, Object.assign({
|
return new NodeFetch(url, Object.assign({
|
||||||
headers: {
|
headers: {
|
||||||
@ -32,6 +36,8 @@ function request(url, options = {}, settings = {}) {
|
|||||||
followRedirect: false,
|
followRedirect: false,
|
||||||
throwHttpErrors: false
|
throwHttpErrors: false
|
||||||
}, options))
|
}, options))
|
||||||
|
} else if (constants.request_backend === "saved") {
|
||||||
|
return new SavedRequestManager(url).request()
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid value for setting `request_backend`.")
|
throw new Error("Invalid value for setting `request_backend`.")
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,10 @@ class NodeFetch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response() {
|
response() {
|
||||||
return this.instance
|
return this.instance.then(res => ({
|
||||||
|
status: res.status,
|
||||||
|
headers: new Map(Object.entries(res.headers))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
json() {
|
json() {
|
||||||
|
@ -103,6 +103,15 @@ const deltas = new Map([
|
|||||||
db.prepare("ALTER TABLE UserSettings ADD COLUMN infinite_scroll TEXT NOT NULL DEFAULT 'normal'")
|
db.prepare("ALTER TABLE UserSettings ADD COLUMN infinite_scroll TEXT NOT NULL DEFAULT 'normal'")
|
||||||
.run()
|
.run()
|
||||||
})()
|
})()
|
||||||
|
}],
|
||||||
|
// version 8 to version 9
|
||||||
|
[9, function() {
|
||||||
|
db.transaction(() => {
|
||||||
|
db.prepare("DROP TABLE IF EXISTS SavedRequests")
|
||||||
|
.run()
|
||||||
|
db.prepare("CREATE TABLE SavedRequests (url TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (url))")
|
||||||
|
.run()
|
||||||
|
})()
|
||||||
}]
|
}]
|
||||||
])
|
])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user