Skip to content

Commit 5341f58

Browse files
Refactoring go brr
1 parent feaf3c6 commit 5341f58

File tree

8 files changed

+279
-282
lines changed

8 files changed

+279
-282
lines changed

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"nanoparse": "^1.0.0",
2828
"node-fetch": "^3.2.4",
2929
"planckcolors": "^1.2.0",
30-
"unbug": "^1.0.0",
3130
"vm2": "^3.9.9"
3231
}
3332
}

src/help.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ POST *https://reqres.in/api/users*
241241
etag: W/"79-MEAYpc0MhwHX2uP7lKGAdEBMx8s"
242242
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
243243
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
244-
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=ntfm%2FdG82lCwNoI1DEY%2Fx%2F%2Bvbj9A%2Fn%2FE0xcyOqXjpQ7WiSA4iG5R%2BOnBjDzZ6CYFs6ltl1TSfVeZMB%2FmGhF1kjhXY%2FRtDz28ItiH37STrIJacYjwcItJ4kBm%2B6E%3D"}],"group":"cf-nel","max_age":604800}
244+
report-to: {"endpoints":[{"url":"https://a.nel.cloudflare.com/report/v3?s=ntfm%2FdG82lCwNoI1DEY%2Fx%2F%2Bvbj9A%2Fn%2FE0xcyOqXjpQ7WiSA4iG5R%2BOnBjDzZ6CYFs6ltl1TSfVeZMB%2FmGhF1kjhXY%2FRtDz28ItiH37STrIJacYjwcItJ4kBm%2B6E%3D"}],"group":"cf-nel","max_age":604800}
245245
server: cloudflare
246246
via: 1.1 vegur
247247
x-powered-by: Express

src/index.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22

33
import nanoparse from 'nanoparse';
4-
import unbug from 'unbug';
54
import color from 'planckcolors';
5+
import fuzzysort from 'fuzzysort';
66

77
import './warning.js';
88

@@ -13,35 +13,44 @@ import help from './help.js';
1313
const args = nanoparse(process.argv.slice(2));
1414
const command = args._[0];
1515

16-
const debug = unbug('controller');
17-
1816
const commands = {
19-
default: () => {
17+
default: (c) => {
2018
console.log(color.bold(color.red('Invalid command!')));
19+
20+
const allCommands = ['help', 'init', 'run', 'version'];
21+
const similar = fuzzysort.go(c, allCommands)
22+
const hl = similar.map(k => '\t' + fuzzysort.highlight(k, '\x1b[31m', '\x1B[0m'))
23+
24+
console.log()
25+
if (similar.length > 0) {
26+
console.log('Did you mean:')
27+
console.log(hl.join('\n'))
28+
} else {
29+
console.log('Available commands:')
30+
console.log(allCommands.map(k => '\t' + k).join('\n'))
31+
}
2132
},
22-
no_command: () => {
33+
noCommand: () => {
2334
console.log(color.red('No command detected!'));
2435
console.log('Run', color.bold(color.green('req help')), 'for more information.');
2536
},
2637
help,
2738
init,
2839
run,
2940
async version() {
41+
const {version} = (await import('../package.json', {assert: {type: 'json'}})).default
3042
console.log(
31-
'req', 'v' + (await import('../package.json', {assert: {type: 'json'}})).default.version
43+
'req', `v${version}`
3244
);
3345
},
3446
}
3547

3648
commands.v = commands.version;
3749

38-
debug('Starting up...')
39-
debug('Command:', command)
40-
debug('Args:', args)
41-
4250
if (!command) {
43-
commands.no_command();
51+
commands.noCommand();
4452
} else {
45-
const the_command = commands[command] || commands.default;
46-
the_command(args);
53+
const theCommand = commands[command];
54+
if (theCommand) theCommand(args);
55+
else commands.default(command);
4756
}

src/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import color from 'planckcolors';
2-
import {mkdirSync, readdirSync, readFileSync, writeFileSync} from 'fs';
2+
import {mkdirSync, readdirSync, writeFileSync} from 'fs';
33

44
export default function init() {
55
console.log('Initializing req in current directory...');

src/parse.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import jsonc from 'jsonc-simple-parser';
2-
const JSONC = jsonc;
3-
4-
const METHODS = ['DELETE', 'GET', 'HEAD', 'POST', 'PUT', 'CONNECT', 'OPTIONS', 'TRACE', 'COPY', 'LOCK', 'MKCOL', 'MOVE', 'PROPFIND', 'PROPPATCH', 'SEARCH', 'UNLOCK', 'BIND', 'REBIND', 'UNBIND', 'ACL', 'REPORT', 'MKACTIVITY', 'CHECKOUT', 'MERGE', 'M-SEARCH', 'NOTIFY', 'SUBSCRIBE', 'UNSUBSCRIBE', 'PATCH', 'PURGE', 'MKCALENDAR', 'LINK', 'UNLINK'];
5-
const HTTP_VERSIONS = ['0.9', '1.0', '1.1', '2.0'];
1+
import JSONC from 'jsonc-simple-parser';
2+
import {HTTP_VERSIONS, METHODS} from './util.js';
63

74
const requestRegex = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
85
const versionRegex = /HTTP\/(\d)\.(\d)$/;
@@ -13,12 +10,9 @@ const clean = line => line.replace(/^\/\/.+/g, '').replace(/^#.+/g, '').trim();
1310

1411
export function parse(text) {
1512
const ret = {
16-
method: undefined,
17-
url: undefined,
18-
version: undefined,
1913
headers: {},
20-
body: undefined,
2114
};
15+
2216
const lines = text.split('\n');
2317

2418
let parsedRequest = false;
@@ -38,9 +32,9 @@ export function parse(text) {
3832

3933
if (!parsedRequest) {
4034
let nextLine = lines[i + 1];
41-
while (nextLine && clean(nextLine) && nextLine.match(continuationRegex)) {
35+
while (nextLine && clean(nextLine) && continuationRegex.test(nextLine)) {
4236
const [, l] = nextLine.match(continuationRegex);
43-
if (l.match(versionRegex)) line += ' ';
37+
if (versionRegex.test(l)) line += ' ';
4438
line += l;
4539
i++;
4640
nextLine = lines[i + 1];
@@ -65,9 +59,7 @@ export function parse(text) {
6559
}
6660

6761
ret.method = method;
68-
6962
ret.url = url;
70-
7163
ret.version = realVersion;
7264

7365
parsedRequest = true;
@@ -93,7 +85,7 @@ export function parse(text) {
9385
}
9486
}
9587

96-
if (finalLine == 0) return ret;
88+
if (finalLine === 0) return ret;
9789

9890
const {body, type} = toBody(lines.slice(finalLine).join('\n'))
9991
ret.body = body;
@@ -105,10 +97,10 @@ export function parse(text) {
10597

10698
function toBody(source) {
10799
try {
108-
JSONC.parse(source);
100+
const parsed = JSONC.parse(source);
109101

110102
return {
111-
body: JSONC.stringify(JSONC.parse(source)),
103+
body: JSONC.stringify(parsed),
112104
type: 'json'
113105
}
114106
} catch (e) {
@@ -118,4 +110,3 @@ function toBody(source) {
118110
}
119111
}
120112
}
121-

0 commit comments

Comments
 (0)