|
| 1 | +function dir(ident) { |
| 2 | + if (ident === '=>') { |
| 3 | + return 'request'; |
| 4 | + } |
| 5 | + if (ident === '<=') { |
| 6 | + return 'response'; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +function parse(trace) { |
| 11 | + // better regex, please, start here: |
| 12 | + // http://scriptular.com/#%5E(%3F%3A%5Ba-z0-9%5D%7B4%7D%3A)%20((%3F%3A%5Ba-z0-9%5D%7B2%7D%20)%7B1%2C16%7D)%7C%7C%7C%7C%7C%7C%7C%7C%5B%2200a0%3A%2011%2022%2033%2044%2055%2066%2077%2088%2099%2010%2011%2012%2013%2014%2015%2016%2017%20%20%7B.%20%20%5C%22headers%5C%22%3A%20%7B%22%5D |
| 13 | + const traceLines = trace.split('\n'); |
| 14 | + const dataPattern = /^(?:[a-z0-9]{4}:) ((?:[a-z0-9]{2} ){1,16})/; |
| 15 | + const dirPattern = /^(=>|<=)/; |
| 16 | + |
| 17 | + // find ASCI bytes in raw lines |
| 18 | + |
| 19 | + // will contain array of arrays with direction and data |
| 20 | + // e.g [['<=', "47 45 54 20 2f 73 68 6f 70 70 69 6e 67 2d 63 61"]] |
| 21 | + const asciiHexSets = []; |
| 22 | + let lastDir = ''; |
| 23 | + |
| 24 | + for (const line of traceLines) { |
| 25 | + const dirMatch = dirPattern.exec(line); |
| 26 | + if (dirMatch !== null) { |
| 27 | + lastDir = dirMatch[0].trim(); |
| 28 | + } |
| 29 | + |
| 30 | + const dataMatch = dataPattern.exec(line); |
| 31 | + if (dataMatch !== null) { |
| 32 | + const data = dataMatch[1].trim(); |
| 33 | + asciiHexSets.push([lastDir, data]); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // split lines by spaces and make array of ASCII hex bytes |
| 38 | + const asciiHexBuffer = { request: [], response: [] }; |
| 39 | + for (const [direction, data] of asciiHexSets) { |
| 40 | + for (const byte of data.split(' ')) { |
| 41 | + asciiHexBuffer[dir(direction)].push(byte); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // convert ASCII hex to ASCII integers codes |
| 46 | + const asciiIntBuffer = { request: [], response: [] }; |
| 47 | + for (const key of Object.keys(asciiHexBuffer)) { |
| 48 | + const hexs = asciiHexBuffer[key]; |
| 49 | + for (const hex of hexs) { |
| 50 | + asciiIntBuffer[key].push(parseInt(hex, 16)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // convert ACII codes to charactes |
| 55 | + const stringBuffer = { request: [], response: [] }; |
| 56 | + for (const key of Object.keys(asciiIntBuffer)) { |
| 57 | + const codes = asciiIntBuffer[key]; |
| 58 | + for (const code of codes) { |
| 59 | + stringBuffer[key].push(String.fromCharCode(code)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + request: stringBuffer.request.join(''), |
| 65 | + response: stringBuffer.response.join('') |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function parseToString(trace) { |
| 70 | + const message = parse(trace); |
| 71 | + let output = ''; |
| 72 | + |
| 73 | + const request = []; |
| 74 | + const requestLines = message.request.split('\r\n'); |
| 75 | + for (const line of requestLines) { |
| 76 | + request.push(`> ${line}`); |
| 77 | + } |
| 78 | + output += request.join('\r\n'); |
| 79 | + output += '\n'; |
| 80 | + output += '\r\n'; |
| 81 | + const response = []; |
| 82 | + const responseLines = message.response.split('\r\n'); |
| 83 | + for (const line of responseLines) { |
| 84 | + response.push(`< ${line}`); |
| 85 | + } |
| 86 | + output += response.join('\r\n'); |
| 87 | + output += '\n'; |
| 88 | + return output; |
| 89 | +} |
| 90 | + |
| 91 | +function parseBackRequestAndResponseFromString(string) { |
| 92 | + const output = {request: '', response: ''}; |
| 93 | + |
| 94 | + const request = []; |
| 95 | + const stringLines = string.split('\r\n'); |
| 96 | + for (const line of stringLines) { |
| 97 | + if (line.startsWith('> ')) { |
| 98 | + request.push(line.replace(/^> /, '')); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // removing trailing LF |
| 103 | + output.request = request.join('\r\n').replace(/\n$/, ''); |
| 104 | + |
| 105 | + const response = []; |
| 106 | + for (const line of stringLines) { |
| 107 | + if (line.startsWith('< ')) { |
| 108 | + response.push(line.replace(/^< /, '')); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // removing trailing LF |
| 113 | + output.response = response.join('\r\n').replace(/\n$/, ''); |
| 114 | + |
| 115 | + return output; |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = { |
| 119 | + parseBackRequestAndResponseFromString, |
| 120 | + parseBack: parseBackRequestAndResponseFromString, |
| 121 | + parseToString, |
| 122 | + parse |
| 123 | +}; |
0 commit comments