Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 4a8abce

Browse files
author
Michal Holasek
authored
Merge pull request #22 from realityking/decaffinate
Decaffinate parser
2 parents 0f98222 + add54e4 commit 4a8abce

File tree

12 files changed

+127
-133
lines changed

12 files changed

+127
-133
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
node_modules
2-
/lib

.npmignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
src/
21
.git*
3-

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- 4
43
- 6
54
- 8
65
- 'stable'

lib/parser.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Parse curl --trace option output to raw HTTP message",
55
"main": "lib/parser.js",
66
"engines": {
7-
"node": ">= 4"
7+
"node": ">= 6"
88
},
99
"scripts": {
1010
"test": "scripts/test",

scripts/build

Lines changed: 0 additions & 9 deletions
This file was deleted.

scripts/prepublish

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
2-
./scripts/test &&
3-
./scripts/build
2+
./scripts/test

scripts/test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
2-
./scripts/build
32
./node_modules/mocha/bin/mocha --reporter spec --compilers=coffee:coffeescript/register ./test/**/*-test.coffee

src/parser.coffee

Lines changed: 0 additions & 106 deletions
This file was deleted.

test/integration/cli-test.coffee

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ fs = require('fs')
55
cmdPrefix = ''
66

77
describe "Command line", () ->
8-
before (done) ->
9-
#CLI is linked with native JS in /lib so re-compile Coffee /src to /lib
10-
cmd = './scripts/build'
11-
cli = exec cmdPrefix + cmd, (error, out, err) ->
12-
if error
13-
done error
14-
done()
15-
168
describe "parsing from standard input with --raw", () ->
179
stdout = ""
1810
stderr = ""

0 commit comments

Comments
 (0)