diff --git a/media_types/_util.ts b/media_types/_util.ts index 114d9be40abf..34dc8e9becca 100644 --- a/media_types/_util.ts +++ b/media_types/_util.ts @@ -40,8 +40,9 @@ export function consumeValue(v: string): [value: string, rest: string] { if (r === `"`) { return [value, v.slice(i + 1)]; } - if (r === "\\" && i + 1 < v.length && isTSpecial(v[i + 1])) { - value += v[i + 1]; + const next = v[i + 1]; + if (r === "\\" && typeof next === "string" && isTSpecial(next)) { + value += next; i++; continue; } @@ -83,14 +84,15 @@ export function decode2331Encoding(v: string): string | undefined { if (sv.length !== 3) { return undefined; } - const charset = sv[0].toLowerCase(); + const [sv0, , sv2] = sv as [string, string, string]; + const charset = sv0.toLowerCase(); if (!charset) { return undefined; } if (charset !== "us-ascii" && charset !== "utf-8") { return undefined; } - const encv = decodeURI(sv[2]); + const encv = decodeURI(sv2); if (!encv) { return undefined; } @@ -133,7 +135,7 @@ function isTokenChar(r: string): boolean { } function isTSpecial(r: string): boolean { - return `()<>@,;:\\"/[]?=`.includes(r[0]); + return r[0] ? `()<>@,;:\\"/[]?=`.includes(r[0]) : false; } const CHAR_CODE_SPACE = " ".charCodeAt(0); diff --git a/media_types/format_media_type.ts b/media_types/format_media_type.ts index ad5412967afc..d277913d8ea0 100644 --- a/media_types/format_media_type.ts +++ b/media_types/format_media_type.ts @@ -23,7 +23,7 @@ export function formatMediaType( param?: Record | Iterable<[string, string]>, ): string { let b = ""; - const [major, sub] = type.split("/"); + const [major = "", sub] = type.split("/"); if (!sub) { if (!isToken(type)) { return ""; @@ -45,7 +45,7 @@ export function formatMediaType( if (!isToken(attribute)) { return ""; } - const value = param[attribute]; + const value = param[attribute]!; b += `; ${attribute.toLowerCase()}`; const needEnc = needsEncoding(value); diff --git a/media_types/parse_media_type.ts b/media_types/parse_media_type.ts index ce50be78b0fd..0f831beab17e 100644 --- a/media_types/parse_media_type.ts +++ b/media_types/parse_media_type.ts @@ -42,7 +42,7 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts"; export function parseMediaType( v: string, ): [mediaType: string, params: Record | undefined] { - const [base] = v.split(";"); + const [base] = v.split(";") as [string]; const mediaType = base.toLowerCase().trim(); const params: Record = {};