Skip to content

Commit

Permalink
chore: golf, use for loop instead for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 17, 2023
1 parent 41d9957 commit 6a188f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function* generate<T>(
let is_preamble = true;
let payloads = [];
let idx_boundary = 0;
let tmp;

try {
let result: ReadableStreamReadResult<Uint8Array>;
Expand All @@ -43,18 +44,19 @@ async function* generate<T>(
boundary = '\r\n' + boundary;
len_boundary += 2;
} else {
let headers: Record<string, string> = {};
let idx_headers = current.indexOf('\r\n\r\n') + 4; // 4 -> '\r\n\r\n'.length
let arr_headers = String(buffer.slice(0, idx_headers))
let arr_headers = String(current.slice(0, idx_headers))
.trim()
.split('\r\n');

// parse headers
let tmp;
while ((tmp = arr_headers.shift())) {
tmp = tmp.split(': ');
headers[tmp.shift()!.toLowerCase()] = tmp.join(': ');
}
let headers: Record<string, string> = {};
let len = arr_headers.length;
for (
;
(tmp = arr_headers[--len]);
tmp = tmp.split(': '),
headers[tmp.shift()!.toLowerCase()] = tmp.join(': ')
);

let last_idx = current.lastIndexOf('\r\n', idx_headers); // 4 -> '\r\n\r\n'.length

Expand Down
17 changes: 9 additions & 8 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function* generate<T>(
let is_preamble = true;
let payloads = [];
let idx_boundary = 0;
let tmp;

outer: for await (let chunk of stream) {
idx_boundary = buffer.byteLength;
Expand All @@ -38,18 +39,18 @@ async function* generate<T>(
boundary = '\r\n' + boundary;
len_boundary += 2;
} else {
let headers: Record<string, string> = {};
let idx_headers = current.indexOf('\r\n\r\n') + 4; // 4 -> '\r\n\r\n'.length
let arr_headers = String(buffer.subarray(0, idx_headers))
let arr_headers = String(current.subarray(0, idx_headers))
.trim()
.split('\r\n');

// parse headers
let tmp;
while ((tmp = arr_headers.shift())) {
tmp = tmp.split(': ');
headers[tmp.shift()!.toLowerCase()] = tmp.join(': ');
}
let headers: Record<string, string> = {};
let len = arr_headers.length;
for (
;
(tmp = arr_headers[--len]);
tmp = tmp.split(': '), headers[tmp.shift()!.toLowerCase()] = tmp.join(': ')
);

let last_idx = current.lastIndexOf('\r\n', idx_headers);

Expand Down

0 comments on commit 6a188f4

Please sign in to comment.