Skip to content

Commit

Permalink
cleanup(web, fetch): dedupe minesniff / "extract a MIME type" algorit…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreubotella committed Mar 20, 2022
1 parent 0bc286a commit 593801e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 71 deletions.
37 changes: 2 additions & 35 deletions ext/fetch/23_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const { guardFromHeaders } = window.__bootstrap.headers;
const { mixinBody, extractBody } = window.__bootstrap.fetchBody;
const { getLocationHref } = window.__bootstrap.location;
const mimesniff = window.__bootstrap.mimesniff;
const { extractMimeType } = window.__bootstrap.mimesniff;
const { blobFromObjectUrl } = window.__bootstrap.file;
const {
headersFromHeaderList,
Expand All @@ -32,9 +32,6 @@
ArrayPrototypeMap,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
MapPrototypeHas,
MapPrototypeGet,
MapPrototypeSet,
ObjectKeys,
ObjectPrototypeIsPrototypeOf,
RegExpPrototypeTest,
Expand Down Expand Up @@ -174,41 +171,11 @@
/** @type {AbortSignal} */
[_signal];
get [_mimeType]() {
let charset = null;
let essence = null;
let mimeType = null;
const values = getDecodeSplitHeader(
headerListFromHeaders(this[_headers]),
"Content-Type",
);
if (values === null) return null;
for (const value of values) {
const temporaryMimeType = mimesniff.parseMimeType(value);
if (
temporaryMimeType === null ||
mimesniff.essence(temporaryMimeType) == "*/*"
) {
continue;
}
mimeType = temporaryMimeType;
if (mimesniff.essence(mimeType) !== essence) {
charset = null;
const newCharset = MapPrototypeGet(mimeType.parameters, "charset");
if (newCharset !== undefined) {
charset = newCharset;
}
essence = mimesniff.essence(mimeType);
} else {
if (
MapPrototypeHas(mimeType.parameters, "charset") === null &&
charset !== null
) {
MapPrototypeSet(mimeType.parameters, "charset", charset);
}
}
}
if (mimeType === null) return null;
return mimeType;
return extractMimeType(values);
}
get [_body]() {
return this[_request].body;
Expand Down
37 changes: 2 additions & 35 deletions ext/fetch/23_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const { HTTP_TAB_OR_SPACE, regexMatcher } = window.__bootstrap.infra;
const { extractBody, mixinBody } = window.__bootstrap.fetchBody;
const { getLocationHref } = window.__bootstrap.location;
const mimesniff = window.__bootstrap.mimesniff;
const { extractMimeType } = window.__bootstrap.mimesniff;
const { URL } = window.__bootstrap.url;
const {
getDecodeSplitHeader,
Expand All @@ -30,9 +30,6 @@
const {
ArrayPrototypeMap,
ArrayPrototypePush,
MapPrototypeHas,
MapPrototypeGet,
MapPrototypeSet,
ObjectPrototypeIsPrototypeOf,
RangeError,
RegExp,
Expand Down Expand Up @@ -162,41 +159,11 @@

class Response {
get [_mimeType]() {
let charset = null;
let essence = null;
let mimeType = null;
const values = getDecodeSplitHeader(
headerListFromHeaders(this[_headers]),
"Content-Type",
);
if (values === null) return null;
for (const value of values) {
const temporaryMimeType = mimesniff.parseMimeType(value);
if (
temporaryMimeType === null ||
mimesniff.essence(temporaryMimeType) == "*/*"
) {
continue;
}
mimeType = temporaryMimeType;
if (mimesniff.essence(mimeType) !== essence) {
charset = null;
const newCharset = MapPrototypeGet(mimeType.parameters, "charset");
if (newCharset !== undefined) {
charset = newCharset;
}
essence = mimesniff.essence(mimeType);
} else {
if (
MapPrototypeHas(mimeType.parameters, "charset") === null &&
charset !== null
) {
MapPrototypeSet(mimeType.parameters, "charset", charset);
}
}
}
if (mimeType === null) return null;
return mimeType;
return extractMimeType(values);
}
get [_body]() {
return this[_response].body;
Expand Down
49 changes: 48 additions & 1 deletion ext/web/01_mimesniff.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const {
ArrayPrototypeIncludes,
Map,
MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
RegExpPrototypeTest,
Expand Down Expand Up @@ -207,5 +208,51 @@
return serialization;
}

window.__bootstrap.mimesniff = { parseMimeType, essence, serializeMimeType };
/**
* Part of the Fetch spec's "extract a MIME type" algorithm
* (https://fetch.spec.whatwg.org/#concept-header-extract-mime-type).
* @param {string[] | null} headerValues The result of getting, decoding and
* splitting the "Content-Type" header.
* @returns {MimeType | null}
*/
function extractMimeType(headerValues) {
if (headerValues === null) return null;

let charset = null;
let essence_ = null;
let mimeType = null;
for (const value of headerValues) {
const temporaryMimeType = parseMimeType(value);
if (
temporaryMimeType === null ||
essence(temporaryMimeType) == "*/*"
) {
continue;
}
mimeType = temporaryMimeType;
if (essence(mimeType) !== essence_) {
charset = null;
const newCharset = MapPrototypeGet(mimeType.parameters, "charset");
if (newCharset !== undefined) {
charset = newCharset;
}
essence_ = essence(mimeType);
} else {
if (
!MapPrototypeHas(mimeType.parameters, "charset") &&
charset !== null
) {
MapPrototypeSet(mimeType.parameters, "charset", charset);
}
}
}
return mimeType;
}

window.__bootstrap.mimesniff = {
parseMimeType,
essence,
serializeMimeType,
extractMimeType,
};
})(this);
3 changes: 3 additions & 0 deletions ext/web/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ declare namespace globalThis {
declare function parseMimeType(input: string): MimeType | null;
declare function essence(mimeType: MimeType): string;
declare function serializeMimeType(mimeType: MimeType): string;
declare function extractMimeType(
headerValues: string[] | null,
): MimeType | null;
}

declare var eventTarget: {
Expand Down

0 comments on commit 593801e

Please sign in to comment.