You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.8 KiB
TypeScript
150 lines
3.8 KiB
TypeScript
4 years ago
|
export enum MessageType {
|
||
|
REQUEST = 1,
|
||
|
REQUEST_BODY = 2,
|
||
|
RESPONSE = 3,
|
||
|
RESPONSE_BODY = 4,
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export type Header = Record<string, string[]>
|
||
4 years ago
|
|
||
4 years ago
|
export interface IRequest {
|
||
|
method: string
|
||
|
url: string
|
||
|
proto: string
|
||
|
header: Header
|
||
|
body?: ArrayBuffer
|
||
|
}
|
||
|
|
||
|
export interface IResponse {
|
||
|
statusCode: number
|
||
|
header: Header
|
||
|
body?: ArrayBuffer
|
||
|
}
|
||
|
|
||
|
export interface IMessage {
|
||
|
type: MessageType
|
||
|
id: string
|
||
|
waitIntercept: boolean
|
||
|
content?: ArrayBuffer | IRequest | IResponse
|
||
|
}
|
||
|
|
||
|
export interface IFlow {
|
||
|
id: string
|
||
|
no?: number
|
||
|
waitIntercept: boolean
|
||
|
request: IRequest
|
||
|
response?: IResponse
|
||
|
}
|
||
|
|
||
|
const allMessageBytes = [
|
||
|
MessageType.REQUEST,
|
||
|
MessageType.REQUEST_BODY,
|
||
|
MessageType.RESPONSE,
|
||
|
MessageType.RESPONSE_BODY,
|
||
|
]
|
||
4 years ago
|
|
||
|
|
||
|
// type: 1/2/3/4
|
||
|
// messageFlow
|
||
|
// version 1 byte + type 1 byte + id 36 byte + waitIntercept 1 byte + content left bytes
|
||
4 years ago
|
export const parseMessage = (data: ArrayBuffer): IMessage | null => {
|
||
4 years ago
|
if (data.byteLength < 39) return null
|
||
|
const meta = new Int8Array(data.slice(0, 39))
|
||
|
const version = meta[0]
|
||
|
if (version !== 1) return null
|
||
4 years ago
|
const type = meta[1] as MessageType
|
||
4 years ago
|
if (!allMessageBytes.includes(type)) return null
|
||
|
const id = new TextDecoder().decode(data.slice(2, 38))
|
||
|
const waitIntercept = meta[38] === 1
|
||
|
|
||
4 years ago
|
const resp: IMessage = {
|
||
|
type,
|
||
4 years ago
|
id,
|
||
|
waitIntercept,
|
||
|
}
|
||
|
if (data.byteLength === 39) return resp
|
||
4 years ago
|
if (type === MessageType.REQUEST_BODY || type === MessageType.RESPONSE_BODY) {
|
||
4 years ago
|
resp.content = data.slice(39)
|
||
|
return resp
|
||
|
}
|
||
|
|
||
4 years ago
|
const contentStr = new TextDecoder().decode(data.slice(39))
|
||
|
let content: any
|
||
4 years ago
|
try {
|
||
4 years ago
|
content = JSON.parse(contentStr)
|
||
4 years ago
|
} catch (err) {
|
||
|
return null
|
||
|
}
|
||
|
|
||
|
resp.content = content
|
||
|
return resp
|
||
|
}
|
||
|
|
||
|
|
||
4 years ago
|
export enum SendMessageType {
|
||
|
CHANGE_REQUEST = 11,
|
||
|
CHANGE_RESPONSE = 12,
|
||
|
DROP_REQUEST = 13,
|
||
|
DROP_RESPONSE = 14,
|
||
|
CHANGE_BREAK_POINT_RULES = 21,
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// type: 11/12/13/14
|
||
4 years ago
|
// messageEdit
|
||
|
// version 1 byte + type 1 byte + id 36 byte + header len 4 byte + header content bytes + body len 4 byte + [body content bytes]
|
||
4 years ago
|
export const buildMessageEdit = (messageType: SendMessageType, flow: IFlow) => {
|
||
|
if (messageType === SendMessageType.DROP_REQUEST || messageType === SendMessageType.DROP_RESPONSE) {
|
||
4 years ago
|
const view = new Uint8Array(38)
|
||
|
view[0] = 1
|
||
|
view[1] = messageType
|
||
|
view.set(new TextEncoder().encode(flow.id), 2)
|
||
|
return view
|
||
|
}
|
||
|
|
||
4 years ago
|
let header: Omit<IRequest, 'body'> | Omit<IResponse, 'body'>
|
||
|
let body: ArrayBuffer | undefined
|
||
|
|
||
|
if (messageType === SendMessageType.CHANGE_REQUEST) {
|
||
4 years ago
|
({ body, ...header } = flow.request)
|
||
4 years ago
|
} else if (messageType === SendMessageType.CHANGE_RESPONSE) {
|
||
|
({ body, ...header } = flow.response as IResponse)
|
||
4 years ago
|
} else {
|
||
|
throw new Error('invalid message type')
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
const bodyLen = (body && body.byteLength) ? body.byteLength : 0
|
||
4 years ago
|
const headerBytes = new TextEncoder().encode(JSON.stringify(header))
|
||
4 years ago
|
const len = 2 + 36 + 4 + headerBytes.byteLength + 4 + bodyLen
|
||
4 years ago
|
const data = new ArrayBuffer(len)
|
||
|
const view = new Uint8Array(data)
|
||
|
view[0] = 1
|
||
|
view[1] = messageType
|
||
4 years ago
|
view.set(new TextEncoder().encode(flow.id), 2)
|
||
4 years ago
|
view.set(headerBytes, 2 + 36 + 4)
|
||
4 years ago
|
if (bodyLen) view.set(body as any, 2 + 36 + 4 + headerBytes.byteLength + 4)
|
||
4 years ago
|
|
||
|
const view2 = new DataView(data)
|
||
|
view2.setUint32(2 + 36, headerBytes.byteLength)
|
||
4 years ago
|
view2.setUint32(2 + 36 + 4 + headerBytes.byteLength, bodyLen)
|
||
4 years ago
|
|
||
|
return view
|
||
|
}
|
||
|
|
||
|
|
||
|
// type: 21
|
||
|
// messageMeta
|
||
|
// version 1 byte + type 1 byte + content left bytes
|
||
4 years ago
|
export const buildMessageMeta = (messageType: SendMessageType, rules: any) => {
|
||
|
if (messageType !== SendMessageType.CHANGE_BREAK_POINT_RULES) {
|
||
4 years ago
|
throw new Error('invalid message type')
|
||
|
}
|
||
|
|
||
|
const rulesBytes = new TextEncoder().encode(JSON.stringify(rules))
|
||
|
const view = new Uint8Array(2 + rulesBytes.byteLength)
|
||
|
view[0] = 1
|
||
|
view[1] = messageType
|
||
|
view.set(rulesBytes, 2)
|
||
|
|
||
|
return view
|
||
|
}
|