1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-22 08:07:30 +00:00

Ability to record and playback network requests

This commit is contained in:
Cadence Ember 2020-06-22 00:34:46 +12:00
parent 6fd9907b60
commit 6b4c9e9660
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412
4 changed files with 21 additions and 3 deletions

View File

@ -277,7 +277,7 @@ let constants = {
additional_routes: [],
database_version: 8
database_version: 9
}
// Override values from config and export the result

View File

@ -1,5 +1,6 @@
const NodeFetch = require("./request_backends/node-fetch")
const Got = require("./request_backends/got")
const SavedRequestManager = require("./saved_requests/manager")
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([
["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.log === undefined) settings.log = true
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") {
return new NodeFetch(url, Object.assign({
headers: {
@ -32,6 +36,8 @@ function request(url, options = {}, settings = {}) {
followRedirect: false,
throwHttpErrors: false
}, options))
} else if (constants.request_backend === "saved") {
return new SavedRequestManager(url).request()
} else {
throw new Error("Invalid value for setting `request_backend`.")
}

View File

@ -10,7 +10,10 @@ class NodeFetch {
}
response() {
return this.instance
return this.instance.then(res => ({
status: res.status,
headers: new Map(Object.entries(res.headers))
}))
}
json() {

View File

@ -103,6 +103,15 @@ const deltas = new Map([
db.prepare("ALTER TABLE UserSettings ADD COLUMN infinite_scroll TEXT NOT NULL DEFAULT 'normal'")
.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()
})()
}]
])