Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(http): prepare for noUncheckedIndexedAccess #4298

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions http/_negotiation/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function parseEncoding(
const params = match[2].split(";");
for (const param of params) {
const p = param.trim().split("=");
if (p[0] === "q") {
if (p[0] === "q" && p[1]) {
q = parseFloat(p[1]);
break;
}
Expand Down Expand Up @@ -91,8 +91,8 @@ function parseAcceptEncoding(accept: string): EncodingSpecificity[] {
let hasIdentity = false;
let minQuality = 1;

for (let i = 0; i < accepts.length; i++) {
const encoding = parseEncoding(accepts[i].trim(), i);
for (const [i, accept] of accepts.entries()) {
const encoding = parseEncoding(accept.trim(), i);

if (encoding) {
parsedAccepts.push(encoding);
Expand Down Expand Up @@ -158,5 +158,5 @@ export function preferredEncodings(
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]);
.map((priority) => provided[priorities.indexOf(priority)]!);
}
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 8 additions & 4 deletions http/_negotiation/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@
}

const [, prefix, suffix] = match;
if (!prefix) {
return undefined;
}

Check warning on line 54 in http/_negotiation/language.ts

View check run for this annotation

Codecov / codecov/patch

http/_negotiation/language.ts#L53-L54

Added lines #L53 - L54 were not covered by tests

const full = suffix ? `${prefix}-${suffix}` : prefix;

let q = 1;
if (match[3]) {
const params = match[3].split(";");
for (const param of params) {
const [key, value] = param.trim().split("=");
if (key === "q") {
if (key === "q" && value) {
q = parseFloat(value);
break;
}
Expand All @@ -70,8 +74,8 @@
const accepts = accept.split(",");
const result: LanguageSpecificity[] = [];

for (let i = 0; i < accepts.length; i++) {
const language = parseLanguage(accepts[i].trim(), i);
for (const [i, accept] of accepts.entries()) {
const language = parseLanguage(accept.trim(), i);
if (language) {
result.push(language);
}
Expand Down Expand Up @@ -140,5 +144,5 @@
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]);
.map((priority) => provided[priorities.indexOf(priority)]!);
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}
67 changes: 13 additions & 54 deletions http/_negotiation/media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,9 @@

const simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/;

function quoteCount(str: string): number {
let count = 0;
let index = 0;

while ((index = str.indexOf(`"`, index)) !== -1) {
count++;
index++;
}

return count;
}

function splitMediaTypes(accept: string): string[] {
const accepts = accept.split(",");

let j = 0;
for (let i = 1; i < accepts.length; i++) {
if (quoteCount(accepts[j]) % 2 === 0) {
accepts[++j] = accepts[i];
} else {
accepts[j] += `,${accepts[i]}`;
}
}

accepts.length = j + 1;

return accepts;
}

function splitParameters(str: string): string[] {
const parameters = str.split(";");

let j = 0;
for (let i = 1; i < parameters.length; i++) {
if (quoteCount(parameters[j]) % 2 === 0) {
parameters[++j] = parameters[i];
} else {
parameters[j] += `;${parameters[i]}`;
}
}

parameters.length = j + 1;

return parameters.map((p) => p.trim());
}

function splitKeyValuePair(str: string): [string, string | undefined] {
const [key, value] = str.split("=");
return [key.toLowerCase(), value];
return [key!.toLowerCase(), value];
}

function parseMediaType(
Expand All @@ -100,12 +54,17 @@
return;
}

const params: { [param: string]: string | undefined } = Object.create(null);
let q = 1;
const [, type, subtype, parameters] = match;
if (!type || !subtype) {
return;
}

Check warning on line 60 in http/_negotiation/media_type.ts

View check run for this annotation

Codecov / codecov/patch

http/_negotiation/media_type.ts#L59-L60

Added lines #L59 - L60 were not covered by tests

const params: { [param: string]: string | undefined } = Object.create(null);
let q = 1;
if (parameters) {
const kvps = splitParameters(parameters).map(splitKeyValuePair);
const kvps = parameters.split(";").map((p) => p.trim()).map(
splitKeyValuePair,
);

for (const [key, val] of kvps) {
const value = val && val[0] === `"` && val[val.length - 1] === `"`
Expand All @@ -125,11 +84,11 @@
}

function parseAccept(accept: string): MediaTypeSpecificity[] {
const accepts = splitMediaTypes(accept);
const accepts = accept.split(",").map((p) => p.trim());

const mediaTypes: MediaTypeSpecificity[] = [];
for (let i = 0; i < accepts.length; i++) {
const mediaType = parseMediaType(accepts[i].trim(), i);
for (const [index, accept] of accepts.entries()) {
const mediaType = parseMediaType(accept.trim(), index);

if (mediaType) {
mediaTypes.push(mediaType);
Expand Down Expand Up @@ -233,5 +192,5 @@
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]);
.map((priority) => provided[priorities.indexOf(priority)]!);
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 6 additions & 1 deletion http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,13 @@
.split(";")
.map((attr) => {
const [key, ...values] = attr.trim().split("=");
return [key, values.join("=")];
return [key!, values.join("=")] as const;
});

if (!attrs[0]) {
return null;
}

Check warning on line 301 in http/cookie.ts

View check run for this annotation

Codecov / codecov/patch

http/cookie.ts#L300-L301

Added lines #L300 - L301 were not covered by tests

const cookie: Cookie = {
name: attrs[0][0],
value: attrs[0][1],
Expand Down
2 changes: 1 addition & 1 deletion http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ export async function serveDir(
if (opts.headers && !isRedirectResponse) {
for (const header of opts.headers) {
const headerSplit = header.split(":");
const name = headerSplit[0];
const name = headerSplit[0]!;
const value = headerSplit.slice(1).join(":");
response.headers.append(name, value);
}
Expand Down
10 changes: 5 additions & 5 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Deno.test(

try {
assertEquals(server.addrs.length, 3);
assertEquals(server.addrs[0].transport, "tcp");
assertEquals(server.addrs[0]!.transport, "tcp");
assertEquals(
(server.addrs[0] as Deno.NetAddr).hostname,
listenerOneOptions.hostname,
Expand All @@ -164,7 +164,7 @@ Deno.test(
(server.addrs[0] as Deno.NetAddr).port,
listenerOneOptions.port,
);
assertEquals(server.addrs[1].transport, "tcp");
assertEquals(server.addrs[1]!.transport, "tcp");
assertEquals(
(server.addrs[1] as Deno.NetAddr).hostname,
listenerTwoOptions.hostname,
Expand All @@ -173,7 +173,7 @@ Deno.test(
(server.addrs[1] as Deno.NetAddr).port,
listenerTwoOptions.port,
);
assertEquals(server.addrs[2].transport, "tcp");
assertEquals(server.addrs[2]!.transport, "tcp");
assertEquals((server.addrs[2] as Deno.NetAddr).hostname, addrHostname);
assertEquals((server.addrs[2] as Deno.NetAddr).port, addrPort);
} finally {
Expand Down Expand Up @@ -973,14 +973,14 @@ Deno.test(
// the expected backoff delay.
for (let i = 0; i < rejectionCount; i++) {
assertEquals(
listener.acceptCallIntervals[i] >= expectedBackoffDelays[i],
listener.acceptCallIntervals[i]! >= expectedBackoffDelays[i]!,
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
true,
);
}

// Assert that the backoff delay has been reset following successfully
// accepting a connection, i.e. it doesn't remain at 1000ms.
assertEquals(listener.acceptCallIntervals[rejectionCount] < 1000, true);
assertEquals(listener.acceptCallIntervals[rejectionCount]! < 1000, true);
},
);

Expand Down
4 changes: 2 additions & 2 deletions http/user_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ function mapper(
if (!matchers[j]) {
break;
}
matches = matchers[j++].exec(ua);
matches = matchers[j++]!.exec(ua);

if (matches) {
for (const processor of processors) {
const match = matches[++k];
const match = matches[++k]!;
if (Array.isArray(processor)) {
if (processor.length === 2) {
const [prop, value] = processor;
Expand Down
Loading