Skip to content

Commit

Permalink
refactor(http): prepare for noUncheckedIndexedAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
gabelluardo committed Feb 9, 2024
1 parent 6da4e4e commit b407c65
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
6 changes: 3 additions & 3 deletions http/_negotiation/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function parseEncoding(
for (const param of params) {
const p = param.trim().split("=");
if (p[0] === "q") {
q = parseFloat(p[1]);
q = parseFloat(p[1]!);
break;
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ function parseAcceptEncoding(accept: string): EncodingSpecificity[] {
let minQuality = 1;

for (let i = 0; i < accepts.length; i++) {
const encoding = parseEncoding(accepts[i].trim(), i);
const encoding = parseEncoding(accepts[i]!.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)]!);
}
9 changes: 5 additions & 4 deletions http/_negotiation/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function parseLanguage(
return undefined;
}

const [, prefix, suffix] = match;
const prefix = match.at(1)!;
const suffix = match.at(2)!;
const full = suffix ? `${prefix}-${suffix}` : prefix;

let q = 1;
Expand All @@ -57,7 +58,7 @@ function parseLanguage(
for (const param of params) {
const [key, value] = param.trim().split("=");
if (key === "q") {
q = parseFloat(value);
q = parseFloat(value!);
break;
}
}
Expand All @@ -71,7 +72,7 @@ function parseAcceptLanguage(accept: string): LanguageSpecificity[] {
const result: LanguageSpecificity[] = [];

for (let i = 0; i < accepts.length; i++) {
const language = parseLanguage(accepts[i].trim(), i);
const language = parseLanguage(accepts[i]!.trim(), i);
if (language) {
result.push(language);
}
Expand Down Expand Up @@ -140,5 +141,5 @@ export function preferredLanguages(
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]);
.map((priority) => provided[priorities.indexOf(priority)]!);
}
18 changes: 10 additions & 8 deletions http/_negotiation/media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function splitMediaTypes(accept: string): string[] {

let j = 0;
for (let i = 1; i < accepts.length; i++) {
if (quoteCount(accepts[j]) % 2 === 0) {
accepts[++j] = accepts[i];
if (quoteCount(accepts[j]!) % 2 === 0) {
accepts[++j] = accepts[i]!;
} else {
accepts[j] += `,${accepts[i]}`;
}
Expand All @@ -73,8 +73,8 @@ function splitParameters(str: string): string[] {

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

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

View check run for this annotation

Codecov / codecov/patch

http/_negotiation/media_type.ts#L76-L77

Added lines #L76 - L77 were not covered by tests
} else {
parameters[j] += `;${parameters[i]}`;
}
Expand All @@ -87,7 +87,7 @@ function splitParameters(str: string): string[] {

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

function parseMediaType(
Expand All @@ -102,7 +102,9 @@ function parseMediaType(

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

if (parameters) {
const kvps = splitParameters(parameters).map(splitKeyValuePair);
Expand All @@ -129,7 +131,7 @@ function parseAccept(accept: string): MediaTypeSpecificity[] {

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

if (mediaType) {
mediaTypes.push(mediaType);
Expand Down Expand Up @@ -233,5 +235,5 @@ export function preferredMediaTypes(
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]);
.map((priority) => provided[priorities.indexOf(priority)]!);
}
8 changes: 4 additions & 4 deletions http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ function parseSetCookie(value: string): Cookie | null {
return [key, values.join("=")];
});
const cookie: Cookie = {
name: attrs[0][0],
value: attrs[0][1],
name: attrs[0]![0]!,
value: attrs[0]![1]!,
};

for (const [key, value] of attrs.slice(1)) {
switch (key.toLocaleLowerCase()) {
switch (key!.toLocaleLowerCase()) {
case "expires":
cookie.expires = new Date(value);
cookie.expires = new Date(value as string);
break;
case "max-age":
cookie.maxAge = Number(value);
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]!,
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

0 comments on commit b407c65

Please sign in to comment.