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(media-types): minor cleanups #4743

Merged
merged 1 commit into from
May 16, 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
4 changes: 2 additions & 2 deletions media_types/_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ for (const type of Object.keys(db) as KeyOfDb[]) {
continue;
}

// @ts-ignore work around denoland/dnt#148
// @ts-ignore Work around https://github.com/denoland/dnt/issues/148
extensions.set(type, exts);

for (const ext of exts) {
Expand All @@ -33,7 +33,7 @@ for (const type of Object.keys(db) as KeyOfDb[]) {
if (
current !== "application/octet-stream" &&
(from > to ||
// @ts-ignore work around denoland/dnt#148
// @ts-ignore work around https://github.com/denoland/dnt/issues/148
(from === to && current.startsWith("application/")))
) {
continue;
Expand Down
6 changes: 0 additions & 6 deletions media_types/_util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/** Supporting functions for media_types that do not make part of the public
* API.
*
* @module
* @private
*/
export interface DBEntry {
source: string;
compressible?: boolean;
Expand Down
6 changes: 1 addition & 5 deletions media_types/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ import { extensionsByType } from "./extensions_by_type.ts";
* ```
*/
export function extension(type: string): string | undefined {
const exts = extensionsByType(type);
if (exts) {
return exts[0];
}
return undefined;
return extensionsByType(type)?.[0];
}
20 changes: 10 additions & 10 deletions media_types/format_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ export function formatMediaType(
type: string,
param?: Record<string, string> | Iterable<[string, string]>,
): string {
let b = "";
let serializedMediaType = "";
const [major = "", sub] = type.split("/");
if (!sub) {
if (!isToken(type)) {
return "";
}
b += type.toLowerCase();
serializedMediaType += type.toLowerCase();
} else {
if (!isToken(major) || !isToken(sub)) {
return "";
}
b += `${major.toLowerCase()}/${sub.toLowerCase()}`;
serializedMediaType += `${major.toLowerCase()}/${sub.toLowerCase()}`;
}

if (param) {
Expand All @@ -62,25 +62,25 @@ export function formatMediaType(
return "";
}
const value = param[attribute]!;
b += `; ${attribute.toLowerCase()}`;
serializedMediaType += `; ${attribute.toLowerCase()}`;

const needEnc = needsEncoding(value);
if (needEnc) {
b += "*";
serializedMediaType += "*";
}
b += "=";
serializedMediaType += "=";

if (needEnc) {
b += `utf-8''${encodeURIComponent(value)}`;
serializedMediaType += `utf-8''${encodeURIComponent(value)}`;
continue;
}

if (isToken(value)) {
b += value;
serializedMediaType += value;
continue;
}
b += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`;
serializedMediaType += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`;
}
}
return b;
return serializedMediaType;
}
6 changes: 3 additions & 3 deletions media_types/get_charset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import { db, type KeyOfDb } from "./_db.ts";
export function getCharset(type: string): string | undefined {
try {
const [mediaType, params] = parseMediaType(type);
if (params && params["charset"]) {
return params["charset"];
if (params?.charset) {
return params.charset;
}
const entry = db[mediaType as KeyOfDb] as DBEntry;
if (entry && entry.charset) {
if (entry?.charset) {
return entry.charset;
}
if (mediaType.startsWith("text/")) {
Expand Down
42 changes: 20 additions & 22 deletions media_types/parse_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts";
* params keys will be normalized to lower case, but preserves the casing of
* the value.
*
* @param v The media type to parse.
* @param type The media type to parse.
*
* @returns A tuple where the first element is the media type and the second
* element is the optional parameters or `undefined` if there are none.
Expand All @@ -33,23 +33,23 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts";
* ```
*/
export function parseMediaType(
v: string,
type: string,
): [mediaType: string, params: Record<string, string> | undefined] {
const [base] = v.split(";") as [string];
const [base] = type.split(";") as [string];
const mediaType = base.toLowerCase().trim();

const params: Record<string, string> = {};
// Map of base parameter name -> parameter name -> value
// for parameters containing a '*' character.
const continuation = new Map<string, Record<string, string>>();

v = v.slice(base.length);
while (v.length) {
v = v.trimStart();
if (v.length === 0) {
type = type.slice(base.length);
while (type.length) {
type = type.trimStart();
if (type.length === 0) {
break;
}
const [key, value, rest] = consumeMediaParam(v);
const [key, value, rest] = consumeMediaParam(type);
if (!key) {
if (rest.trim() === ";") {
// ignore trailing semicolons
Expand All @@ -70,17 +70,17 @@ export function parseMediaType(
throw new TypeError("Duplicate key parsed.");
}
pmap[key] = value;
v = rest;
type = rest;
}

// Stitch together any continuations or things with stars
// (i.e. RFC 2231 things with stars: "foo*0" or "foo*")
let str = "";
for (const [key, pieceMap] of continuation) {
const singlePartKey = `${key}*`;
const v = pieceMap[singlePartKey];
if (v) {
const decv = decode2331Encoding(v);
const type = pieceMap[singlePartKey];
if (type) {
const decv = decode2331Encoding(type);
if (decv) {
params[key] = decv;
}
Expand All @@ -91,25 +91,25 @@ export function parseMediaType(
let valid = false;
for (let n = 0;; n++) {
const simplePart = `${key}*${n}`;
let v = pieceMap[simplePart];
if (v) {
let type = pieceMap[simplePart];
if (type) {
valid = true;
str += v;
str += type;
continue;
}
const encodedPart = `${simplePart}*`;
v = pieceMap[encodedPart];
if (!v) {
type = pieceMap[encodedPart];
if (!type) {
break;
}
valid = true;
if (n === 0) {
const decv = decode2331Encoding(v);
const decv = decode2331Encoding(type);
if (decv) {
str += decv;
}
} else {
const decv = decodeURI(v);
const decv = decodeURI(type);
str += decv;
}
}
Expand All @@ -118,7 +118,5 @@ export function parseMediaType(
}
}

return Object.keys(params).length
? [mediaType, params]
: [mediaType, undefined];
return [mediaType, Object.keys(params).length ? params : undefined];
}
2 changes: 1 addition & 1 deletion media_types/type_by_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ import { types } from "./_db.ts";
*/
export function typeByExtension(extension: string): string | undefined {
extension = extension.startsWith(".") ? extension.slice(1) : extension;
// @ts-ignore Workaround around for https://github.com/denoland/dnt/issues/148
// @ts-ignore Work around https://github.com/denoland/dnt/issues/148
return types.get(extension.toLowerCase());
}