mirror of
https://github.com/denoland/deno.git
synced 2024-11-29 16:30:56 -05:00
cleanup(web, fetch): dedupe minesniff / "extract a MIME type" algorithm (#14044)
Closes #14002
This commit is contained in:
parent
74307a6950
commit
25fdecff6a
4 changed files with 55 additions and 71 deletions
|
@ -18,7 +18,7 @@
|
||||||
const { guardFromHeaders } = window.__bootstrap.headers;
|
const { guardFromHeaders } = window.__bootstrap.headers;
|
||||||
const { mixinBody, extractBody } = window.__bootstrap.fetchBody;
|
const { mixinBody, extractBody } = window.__bootstrap.fetchBody;
|
||||||
const { getLocationHref } = window.__bootstrap.location;
|
const { getLocationHref } = window.__bootstrap.location;
|
||||||
const mimesniff = window.__bootstrap.mimesniff;
|
const { extractMimeType } = window.__bootstrap.mimesniff;
|
||||||
const { blobFromObjectUrl } = window.__bootstrap.file;
|
const { blobFromObjectUrl } = window.__bootstrap.file;
|
||||||
const {
|
const {
|
||||||
headersFromHeaderList,
|
headersFromHeaderList,
|
||||||
|
@ -32,9 +32,6 @@
|
||||||
ArrayPrototypeMap,
|
ArrayPrototypeMap,
|
||||||
ArrayPrototypeSlice,
|
ArrayPrototypeSlice,
|
||||||
ArrayPrototypeSplice,
|
ArrayPrototypeSplice,
|
||||||
MapPrototypeHas,
|
|
||||||
MapPrototypeGet,
|
|
||||||
MapPrototypeSet,
|
|
||||||
ObjectKeys,
|
ObjectKeys,
|
||||||
ObjectPrototypeIsPrototypeOf,
|
ObjectPrototypeIsPrototypeOf,
|
||||||
RegExpPrototypeTest,
|
RegExpPrototypeTest,
|
||||||
|
@ -174,41 +171,11 @@
|
||||||
/** @type {AbortSignal} */
|
/** @type {AbortSignal} */
|
||||||
[_signal];
|
[_signal];
|
||||||
get [_mimeType]() {
|
get [_mimeType]() {
|
||||||
let charset = null;
|
|
||||||
let essence = null;
|
|
||||||
let mimeType = null;
|
|
||||||
const values = getDecodeSplitHeader(
|
const values = getDecodeSplitHeader(
|
||||||
headerListFromHeaders(this[_headers]),
|
headerListFromHeaders(this[_headers]),
|
||||||
"Content-Type",
|
"Content-Type",
|
||||||
);
|
);
|
||||||
if (values === null) return null;
|
return extractMimeType(values);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
get [_body]() {
|
get [_body]() {
|
||||||
return this[_request].body;
|
return this[_request].body;
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
const { HTTP_TAB_OR_SPACE, regexMatcher } = window.__bootstrap.infra;
|
const { HTTP_TAB_OR_SPACE, regexMatcher } = window.__bootstrap.infra;
|
||||||
const { extractBody, mixinBody } = window.__bootstrap.fetchBody;
|
const { extractBody, mixinBody } = window.__bootstrap.fetchBody;
|
||||||
const { getLocationHref } = window.__bootstrap.location;
|
const { getLocationHref } = window.__bootstrap.location;
|
||||||
const mimesniff = window.__bootstrap.mimesniff;
|
const { extractMimeType } = window.__bootstrap.mimesniff;
|
||||||
const { URL } = window.__bootstrap.url;
|
const { URL } = window.__bootstrap.url;
|
||||||
const {
|
const {
|
||||||
getDecodeSplitHeader,
|
getDecodeSplitHeader,
|
||||||
|
@ -30,9 +30,6 @@
|
||||||
const {
|
const {
|
||||||
ArrayPrototypeMap,
|
ArrayPrototypeMap,
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
MapPrototypeHas,
|
|
||||||
MapPrototypeGet,
|
|
||||||
MapPrototypeSet,
|
|
||||||
ObjectPrototypeIsPrototypeOf,
|
ObjectPrototypeIsPrototypeOf,
|
||||||
RangeError,
|
RangeError,
|
||||||
RegExp,
|
RegExp,
|
||||||
|
@ -162,41 +159,11 @@
|
||||||
|
|
||||||
class Response {
|
class Response {
|
||||||
get [_mimeType]() {
|
get [_mimeType]() {
|
||||||
let charset = null;
|
|
||||||
let essence = null;
|
|
||||||
let mimeType = null;
|
|
||||||
const values = getDecodeSplitHeader(
|
const values = getDecodeSplitHeader(
|
||||||
headerListFromHeaders(this[_headers]),
|
headerListFromHeaders(this[_headers]),
|
||||||
"Content-Type",
|
"Content-Type",
|
||||||
);
|
);
|
||||||
if (values === null) return null;
|
return extractMimeType(values);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
get [_body]() {
|
get [_body]() {
|
||||||
return this[_response].body;
|
return this[_response].body;
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
const {
|
const {
|
||||||
ArrayPrototypeIncludes,
|
ArrayPrototypeIncludes,
|
||||||
Map,
|
Map,
|
||||||
|
MapPrototypeGet,
|
||||||
MapPrototypeHas,
|
MapPrototypeHas,
|
||||||
MapPrototypeSet,
|
MapPrototypeSet,
|
||||||
RegExpPrototypeTest,
|
RegExpPrototypeTest,
|
||||||
|
@ -207,5 +208,51 @@
|
||||||
return serialization;
|
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);
|
})(this);
|
||||||
|
|
3
ext/web/internal.d.ts
vendored
3
ext/web/internal.d.ts
vendored
|
@ -60,6 +60,9 @@ declare namespace globalThis {
|
||||||
declare function parseMimeType(input: string): MimeType | null;
|
declare function parseMimeType(input: string): MimeType | null;
|
||||||
declare function essence(mimeType: MimeType): string;
|
declare function essence(mimeType: MimeType): string;
|
||||||
declare function serializeMimeType(mimeType: MimeType): string;
|
declare function serializeMimeType(mimeType: MimeType): string;
|
||||||
|
declare function extractMimeType(
|
||||||
|
headerValues: string[] | null,
|
||||||
|
): MimeType | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare var eventTarget: {
|
declare var eventTarget: {
|
||||||
|
|
Loading…
Reference in a new issue