aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/backends
diff options
context:
space:
mode:
authorMatthew Shao <me@matshao.com>2017-08-07 10:57:12 +0800
committerMatthew Shao <me@matshao.com>2017-08-07 10:57:12 +0800
commit94a0b82ceedf1847a9bc67a6338369583f8410f9 (patch)
treeac061d508ca76231429b34034e89dcfcc998dd46 /web/src/js/backends
parent27f4c6d394f611b2b975a99751a306e09d17d68c (diff)
downloadmitmproxy-94a0b82ceedf1847a9bc67a6338369583f8410f9.tar.gz
mitmproxy-94a0b82ceedf1847a9bc67a6338369583f8410f9.tar.bz2
mitmproxy-94a0b82ceedf1847a9bc67a6338369583f8410f9.zip
[web] Add static backend.
Diffstat (limited to 'web/src/js/backends')
-rw-r--r--web/src/js/backends/static.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/web/src/js/backends/static.js b/web/src/js/backends/static.js
new file mode 100644
index 00000000..676468ce
--- /dev/null
+++ b/web/src/js/backends/static.js
@@ -0,0 +1,54 @@
+/*
+ * This backend uses the REST API only to host static instances,
+ * without any Websocket connection.
+ */
+import { fetchApi } from "../utils"
+
+const CMD_RESET = 'reset'
+
+export default class StaticBackend {
+ constructor(store) {
+ this.activeFetches = {}
+ this.store = store
+ this.onOpen()
+ }
+
+ onOpen() {
+ this.fetchData("settings")
+ this.fetchData("flows")
+ this.fetchData("events")
+ this.fetchData("options")
+ }
+
+ fetchData(resource) {
+ let queue = []
+ this.activeFetches[resource] = queue
+ fetchApi(`/${resource}`)
+ .then(res => res.json())
+ .then(json => {
+ if (this.activeFetches[resource] === queue)
+ this.receive(resource, json)
+ })
+ }
+
+ onMessage(msg) {
+ if (msg.cmd === CMD_RESET) {
+ return this.fetchData(msg.resource)
+ }
+ if (msg.resource in this.activeFetches) {
+ this.activeFetches[msg.resource].push(msg)
+ } else {
+ let type = `${msg.resource}_${msg.cmd}`.toUpperCase()
+ this.store.dispatch({ type, ...msg})
+ }
+ }
+
+ receive(resource, data) {
+ let type = `${resource}_RECEIVE`.toUpperCase()
+ this.store.dispatch({ type, cmd: "receive", resource, data })
+ let queue = this.activeFetches[resource]
+ delete this.activeFetches[resource]
+ queue.forEach(msg => this.onMessage(msg))
+ }
+
+}