diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-10-31 05:30:32 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-11-08 17:54:27 +0100 |
commit | 85476d9915f23fc45e64b5242e804623f50cd20a (patch) | |
tree | 9fdca1d74c1bcf3f9dafc26cd4d2786c3ae8b81e /web/src/js/ducks | |
parent | 62ca89649237cb0aff63b1fd3d7729b42134bdd1 (diff) | |
download | mitmproxy-85476d9915f23fc45e64b5242e804623f50cd20a.tar.gz mitmproxy-85476d9915f23fc45e64b5242e804623f50cd20a.tar.bz2 mitmproxy-85476d9915f23fc45e64b5242e804623f50cd20a.zip |
clean up mitmweb
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r-- | web/src/js/ducks/app.js | 27 | ||||
-rw-r--r-- | web/src/js/ducks/eventLog.js | 65 | ||||
-rw-r--r-- | web/src/js/ducks/flowView.js | 25 | ||||
-rw-r--r-- | web/src/js/ducks/flows.js | 108 | ||||
-rw-r--r-- | web/src/js/ducks/msgQueue.js | 113 | ||||
-rw-r--r-- | web/src/js/ducks/settings.js | 50 | ||||
-rw-r--r-- | web/src/js/ducks/ui/index.js | 1 | ||||
-rw-r--r-- | web/src/js/ducks/utils/list.js | 12 | ||||
-rw-r--r-- | web/src/js/ducks/websocket.js | 93 |
9 files changed, 24 insertions, 470 deletions
diff --git a/web/src/js/ducks/app.js b/web/src/js/ducks/app.js deleted file mode 100644 index f1dcb490..00000000 --- a/web/src/js/ducks/app.js +++ /dev/null @@ -1,27 +0,0 @@ -import { connect as wsConnect, disconnect as wsDisconnect } from './websocket' - -export const INIT = 'APP_INIT' - -const defaultState = {} - -export function reduce(state = defaultState, action) { - switch (action.type) { - - default: - return state - } -} - -export function init() { - return dispatch => { - dispatch(wsConnect()) - dispatch({ type: INIT }) - } -} - -export function destruct() { - return dispatch => { - dispatch(wsDisconnect()) - dispatch({ type: DESTRUCT }) - } -} diff --git a/web/src/js/ducks/eventLog.js b/web/src/js/ducks/eventLog.js index f72d7bd6..76572a5d 100644 --- a/web/src/js/ducks/eventLog.js +++ b/web/src/js/ducks/eventLog.js @@ -1,17 +1,12 @@ import reduceList, * as listActions from './utils/list' import reduceView, * as viewActions from './utils/view' -import * as websocketActions from './websocket' -import * as msgQueueActions from './msgQueue' -export const MSG_TYPE = 'UPDATE_EVENTLOG' -export const DATA_URL = '/events' - -export const ADD = 'EVENTLOG_ADD' -export const RECEIVE = 'EVENTLOG_RECEIVE' -export const TOGGLE_VISIBILITY = 'EVENTLOG_TOGGLE_VISIBILITY' -export const TOGGLE_FILTER = 'EVENTLOG_TOGGLE_FILTER' -export const UNKNOWN_CMD = 'EVENTLOG_UNKNOWN_CMD' -export const FETCH_ERROR = 'EVENTLOG_FETCH_ERROR' +export const ADD = 'EVENTS_ADD' +export const RECEIVE = 'EVENTS_RECEIVE' +export const TOGGLE_VISIBILITY = 'EVENTS_TOGGLE_VISIBILITY' +export const TOGGLE_FILTER = 'EVENTS_TOGGLE_FILTER' +export const UNKNOWN_CMD = 'EVENTS_UNKNOWN_CMD' +export const FETCH_ERROR = 'EVENTS_FETCH_ERROR' const defaultState = { logId: 0, @@ -54,8 +49,8 @@ export default function reduce(state = defaultState, action) { case RECEIVE: return { ...state, - list: reduceList(state.list, listActions.receive(action.list)), - view: reduceView(state.view, viewActions.receive(action.list, log => state.filters[log.level])), + list: reduceList(state.list, listActions.receive(action.events)), + view: reduceView(state.view, viewActions.receive(action.events, log => state.filters[log.level])), } default: @@ -63,58 +58,14 @@ export default function reduce(state = defaultState, action) { } } -/** - * @public - */ export function toggleFilter(filter) { return { type: TOGGLE_FILTER, filter } } -/** - * @public - * - * @todo move to ui? - */ export function toggleVisibility() { return { type: TOGGLE_VISIBILITY } } -/** - * @public - */ export function add(message, level = 'web') { return { type: ADD, message, level } } - -/** - * This action creater takes all WebSocket events - * - * @public websocket - */ -export function handleWsMsg(msg) { - switch (msg.cmd) { - - case websocketActions.CMD_ADD: - return add(msg.data.message, msg.data.level) - - case websocketActions.CMD_RESET: - return fetchData() - - default: - return { type: UNKNOWN_CMD, msg } - } -} - -/** - * @public websocket - */ -export function fetchData() { - return msgQueueActions.fetchData(MSG_TYPE) -} - -/** - * @public msgQueue - */ -export function receiveData(list) { - return { type: RECEIVE, list } -} diff --git a/web/src/js/ducks/flowView.js b/web/src/js/ducks/flowView.js index dd5bea41..1af96573 100644 --- a/web/src/js/ducks/flowView.js +++ b/web/src/js/ducks/flowView.js @@ -121,6 +121,16 @@ export default function reduce(state = defaultState, action) { } case flowActions.REMOVE: + /* FIXME: Implement select switch on remove + return (dispatch, getState) => { + let currentIndex = getState().flowView.indexOf[getState().flows.selected[0]] + let maxIndex = getState().flowView.data.length - 1 + let deleteLastEntry = maxIndex == 0 + if (deleteLastEntry) + dispatch(select()) + else + dispatch(selectRelative(currentIndex == maxIndex ? -1 : 1) ) + */ return { ...reduceView( state, @@ -135,7 +145,7 @@ export default function reduce(state = defaultState, action) { ...reduceView( state, viewActions.receive( - action.list, + action.flows, makeFilter(state.filter), makeSort(state.sort) ) @@ -149,33 +159,20 @@ export default function reduce(state = defaultState, action) { } } -/** - * @public - */ export function updateFilter(filter) { return (dispatch, getState) => { dispatch({ type: UPDATE_FILTER, filter, flows: getState().flows.data }) } } -/** - * @public - */ export function updateHighlight(highlight) { return { type: UPDATE_HIGHLIGHT, highlight } } -/** - * @public - */ export function updateSort(column, desc) { return { type: UPDATE_SORT, column, desc } } - -/** - * @public - */ export function selectRelative(shift) { return (dispatch, getState) => { let currentSelectionIndex = getState().flowView.indexOf[getState().flows.selected[0]] diff --git a/web/src/js/ducks/flows.js b/web/src/js/ducks/flows.js index 404db0d1..519b5e7d 100644 --- a/web/src/js/ducks/flows.js +++ b/web/src/js/ducks/flows.js @@ -2,12 +2,6 @@ import { fetchApi } from '../utils' import reduceList, * as listActions from './utils/list' import { selectRelative } from './flowView' -import * as msgQueueActions from './msgQueue' -import * as websocketActions from './websocket' - -export const MSG_TYPE = 'UPDATE_FLOWS' -export const DATA_URL = '/flows' - export const ADD = 'FLOWS_ADD' export const UPDATE = 'FLOWS_UPDATE' export const REMOVE = 'FLOWS_REMOVE' @@ -47,7 +41,7 @@ export default function reduce(state = defaultState, action) { case RECEIVE: return { ...state, - ...reduceList(state, listActions.receive(action.list)), + ...reduceList(state, listActions.receive(action.flows)), } case SELECT: @@ -64,51 +58,30 @@ export default function reduce(state = defaultState, action) { } } -/** - * @public - */ export function accept(flow) { return dispatch => fetchApi(`/flows/${flow.id}/accept`, { method: 'POST' }) } -/** - * @public - */ export function acceptAll() { return dispatch => fetchApi('/flows/accept', { method: 'POST' }) } -/** - * @public - */ export function remove(flow) { return dispatch => fetchApi(`/flows/${flow.id}`, { method: 'DELETE' }) } -/** - * @public - */ export function duplicate(flow) { return dispatch => fetchApi(`/flows/${flow.id}/duplicate`, { method: 'POST' }) } -/** - * @public - */ export function replay(flow) { return dispatch => fetchApi(`/flows/${flow.id}/replay`, { method: 'POST' }) } -/** - * @public - */ export function revert(flow) { return dispatch => fetchApi(`/flows/${flow.id}/revert`, { method: 'POST' }) } -/** - * @public - */ export function update(flow, data) { return dispatch => fetchApi.put(`/flows/${flow.id}`, data) } @@ -121,24 +94,15 @@ export function uploadContent(flow, file, type) { } -/** - * @public - */ export function clear() { return dispatch => fetchApi('/clear', { method: 'POST' }) } -/** - * @public - */ export function download() { window.location = '/flows/dump' return { type: REQUEST_ACTION } } -/** - * @public - */ export function upload(file) { const body = new FormData() body.append('file', file) @@ -152,73 +116,3 @@ export function select(id) { flowIds: id ? [id] : [] } } - - -/** - * This action creater takes all WebSocket events - * - * @public websocket - */ -export function handleWsMsg(msg) { - switch (msg.cmd) { - - case websocketActions.CMD_ADD: - return addFlow(msg.data) - - case websocketActions.CMD_UPDATE: - return updateFlow(msg.data) - - case websocketActions.CMD_REMOVE: - return removeFlow(msg.data.id) - - case websocketActions.CMD_RESET: - return fetchFlows() - - default: - return { type: UNKNOWN_CMD, msg } - } -} - -/** - * @public websocket - */ -export function fetchFlows() { - return msgQueueActions.fetchData(MSG_TYPE) -} - -/** - * @public msgQueue - */ -export function receiveData(list) { - return { type: RECEIVE, list } -} - -/** - * @private - */ -export function addFlow(item) { - return { type: ADD, item } -} - -/** - * @private - */ -export function updateFlow(item) { - return { type: UPDATE, item } -} - -/** - * @private - */ -export function removeFlow(id) { - return (dispatch, getState) => { - let currentIndex = getState().flowView.indexOf[getState().flows.selected[0]] - let maxIndex = getState().flowView.data.length - 1 - let deleteLastEntry = maxIndex == 0 - if (deleteLastEntry) - dispatch(select()) - else - dispatch(selectRelative(currentIndex == maxIndex ? -1 : 1) ) - dispatch({ type: REMOVE, id }) - } -} diff --git a/web/src/js/ducks/msgQueue.js b/web/src/js/ducks/msgQueue.js deleted file mode 100644 index 6d82f4c2..00000000 --- a/web/src/js/ducks/msgQueue.js +++ /dev/null @@ -1,113 +0,0 @@ -import { fetchApi } from '../utils' -import * as websocketActions from './websocket' -import * as eventLogActions from './eventLog' -import * as flowsActions from './flows' -import * as settingsActions from './settings' - -export const INIT = 'MSG_QUEUE_INIT' -export const ENQUEUE = 'MSG_QUEUE_ENQUEUE' -export const CLEAR = 'MSG_QUEUE_CLEAR' -export const FETCH_ERROR = 'MSG_QUEUE_FETCH_ERROR' - -const handlers = { - [eventLogActions.MSG_TYPE] : eventLogActions, - [flowsActions.MSG_TYPE] : flowsActions, - [settingsActions.MSG_TYPE] : settingsActions, -} - -const defaultState = {} - -export default function reduce(state = defaultState, action) { - switch (action.type) { - - case INIT: - return { - ...state, - [action.queue]: [], - } - - case ENQUEUE: - return { - ...state, - [action.queue]: [...state[action.queue], action.msg], - } - - case CLEAR: - return { - ...state, - [action.queue]: null, - } - - default: - return state - } -} - -/** - * @public websocket - */ -export function handleWsMsg(msg) { - return (dispatch, getState) => { - const handler = handlers[msg.type] - if (msg.cmd === websocketActions.CMD_RESET) { - return dispatch(fetchData(handler.MSG_TYPE)) - } - if (getState().msgQueue[handler.MSG_TYPE]) { - return dispatch({ type: ENQUEUE, queue: handler.MSG_TYPE, msg }) - } - return dispatch(handler.handleWsMsg(msg)) - } -} - -/** - * @public - */ -export function fetchData(type) { - return dispatch => { - const handler = handlers[type] - - dispatch(init(handler.MSG_TYPE)) - - fetchApi(handler.DATA_URL) - .then(res => res.json()) - .then(json => dispatch(receive(type, json))) - .catch(error => dispatch(fetchError(type, error))) - } -} - -/** - * @private - */ -export function receive(type, res) { - return (dispatch, getState) => { - const handler = handlers[type] - const queue = getState().msgQueue[handler.MSG_TYPE] || [] - - dispatch(clear(handler.MSG_TYPE)) - dispatch(handler.receiveData(res.data)) - for (const msg of queue) { - dispatch(handler.handleWsMsg(msg)) - } - } -} - -/** - * @private - */ -export function init(queue) { - return { type: INIT, queue } -} - -/** - * @private - */ -export function clear(queue) { - return { type: CLEAR, queue } -} - -/** - * @private - */ -export function fetchError(type, error) { - return { type: FETCH_ERROR, type, error } -} diff --git a/web/src/js/ducks/settings.js b/web/src/js/ducks/settings.js index 6b21baec..32afe3be 100644 --- a/web/src/js/ducks/settings.js +++ b/web/src/js/ducks/settings.js @@ -1,12 +1,7 @@ import { fetchApi } from '../utils' -import * as websocketActions from './websocket' -import * as msgQueueActions from './msgQueue' -export const MSG_TYPE = 'UPDATE_SETTINGS' -export const DATA_URL = '/settings' - -export const RECEIVE = 'RECEIVE' -export const UPDATE = 'UPDATE' +export const RECEIVE = 'SETTINGS_RECEIVE' +export const UPDATE = 'SETTINGS_UPDATE' export const REQUEST_UPDATE = 'REQUEST_UPDATE' export const UNKNOWN_CMD = 'SETTINGS_UNKNOWN_CMD' @@ -23,7 +18,7 @@ export default function reducer(state = defaultState, action) { case UPDATE: return { ...state, - ...action.settings, + ...action.data, } default: @@ -31,46 +26,7 @@ export default function reducer(state = defaultState, action) { } } -/** - * @public msgQueue - */ -export function handleWsMsg(msg) { - switch (msg.cmd) { - - case websocketActions.CMD_UPDATE: - return updateSettings(msg.data) - - default: - console.error('unknown settings update', msg) - return { type: UNKNOWN_CMD, msg } - } -} - -/** - * @public - */ export function update(settings) { fetchApi.put('/settings', settings) return { type: REQUEST_UPDATE } } - -/** - * @public websocket - */ -export function fetchData() { - return msgQueueActions.fetchData(MSG_TYPE) -} - -/** - * @public msgQueue - */ -export function receiveData(settings) { - return { type: RECEIVE, settings } -} - -/** - * @private - */ -export function updateSettings(settings) { - return { type: UPDATE, settings } -} diff --git a/web/src/js/ducks/ui/index.js b/web/src/js/ducks/ui/index.js index f3c5f59e..1d989eb1 100644 --- a/web/src/js/ducks/ui/index.js +++ b/web/src/js/ducks/ui/index.js @@ -2,6 +2,7 @@ import { combineReducers } from 'redux' import flow from './flow' import header from './header' +// TODO: Just move ducks/ui/* into ducks/? export default combineReducers({ flow, header, diff --git a/web/src/js/ducks/utils/list.js b/web/src/js/ducks/utils/list.js index fdeb5856..339fb921 100644 --- a/web/src/js/ducks/utils/list.js +++ b/web/src/js/ducks/utils/list.js @@ -76,30 +76,18 @@ export default function reduce(state = defaultState, action) { } } -/** - * @public - */ export function add(item) { return { type: ADD, item } } -/** - * @public - */ export function update(item) { return { type: UPDATE, item } } -/** - * @public - */ export function remove(id) { return { type: REMOVE, id } } -/** - * @public - */ export function receive(list) { return { type: RECEIVE, list } } diff --git a/web/src/js/ducks/websocket.js b/web/src/js/ducks/websocket.js deleted file mode 100644 index 21400bb5..00000000 --- a/web/src/js/ducks/websocket.js +++ /dev/null @@ -1,93 +0,0 @@ -import { ConnectionActions } from '../actions.js' -import { AppDispatcher } from '../dispatcher.js' - -import * as msgQueueActions from './msgQueue' -import * as eventLogActions from './eventLog' -import * as flowsActions from './flows' -import * as settingsActions from './settings' - -export const CMD_ADD = 'add' -export const CMD_UPDATE = 'update' -export const CMD_REMOVE = 'remove' -export const CMD_RESET = 'reset' - -export const SYM_SOCKET = Symbol('WEBSOCKET_SYM_SOCKET') - -export const CONNECT = 'WEBSOCKET_CONNECT' -export const CONNECTED = 'WEBSOCKET_CONNECTED' -export const DISCONNECT = 'WEBSOCKET_DISCONNECT' -export const DISCONNECTED = 'WEBSOCKET_DISCONNECTED' -export const ERROR = 'WEBSOCKET_ERROR' -export const MESSAGE = 'WEBSOCKET_MESSAGE' - -/* we may want to have an error message attribute here at some point */ -const defaultState = { connected: false, socket: null } - -export default function reduce(state = defaultState, action) { - switch (action.type) { - - case CONNECT: - return { ...state, [SYM_SOCKET]: action.socket } - - case CONNECTED: - return { ...state, connected: true } - - case DISCONNECT: - return { ...state, connected: false } - - case DISCONNECTED: - return { ...state, [SYM_SOCKET]: null, connected: false } - - default: - return state - } -} - -export function connect() { - return dispatch => { - const socket = new WebSocket(location.origin.replace('http', 'ws') + '/updates') - - socket.addEventListener('open', () => dispatch(onConnect())) - socket.addEventListener('close', () => dispatch(onDisconnect())) - socket.addEventListener('message', msg => dispatch(onMessage(JSON.parse(msg.data)))) - socket.addEventListener('error', error => dispatch(onError(error))) - - dispatch({ type: CONNECT, socket }) - } -} - -export function disconnect() { - return (dispatch, getState) => { - getState().settings[SYM_SOCKET].close() - dispatch({ type: DISCONNECT }) - } -} - -export function onConnect() { - // workaround to make sure that our state is already available. - return dispatch => { - dispatch({ type: CONNECTED }) - dispatch(settingsActions.fetchData()) - dispatch(flowsActions.fetchFlows()) - dispatch(eventLogActions.fetchData()) - } -} - -export function onMessage(msg) { - return msgQueueActions.handleWsMsg(msg) -} - -export function onDisconnect() { - return dispatch => { - dispatch(eventLogActions.add('WebSocket connection closed.')) - dispatch({ type: DISCONNECTED }) - } -} - -export function onError(error) { - // @todo let event log subscribe WebSocketActions.ERROR - return dispatch => { - dispatch(eventLogActions.add('WebSocket connection error.')) - dispatch({ type: ERROR, error }) - } -} |