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

fix(ext/web): use mimesniff in FileAPI #16233

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
7 changes: 6 additions & 1 deletion ext/web/01_mimesniff.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ function parseMimeType(input) {
position = res.position;

// 11.8.2.
position++;
const res2 = collectSequenceOfCodepoints(
input,
position,
(c) => c !== "\u003B",
);
position = res2.position;
} else { // 11.9.
// 11.9.1.
const res = collectSequenceOfCodepoints(
Expand Down
13 changes: 3 additions & 10 deletions ext/web/09_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ const {
MathMax,
MathMin,
ObjectPrototypeIsPrototypeOf,
RegExpPrototypeTest,
// TODO(lucacasonato): add SharedArrayBuffer to primordials
// SharedArrayBufferPrototype
SafeFinalizationRegistry,
SafeRegExp,
StringPrototypeCharAt,
StringPrototypeToLowerCase,
StringPrototypeSlice,
Symbol,
SymbolFor,
Expand All @@ -50,6 +47,7 @@ const {
Uint8Array,
} = primordials;
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
import * as mimesniff from "ext:deno_web/01_mimesniff.js";

// TODO(lucacasonato): this needs to not be hardcoded and instead depend on
// host os.
Expand Down Expand Up @@ -171,18 +169,13 @@ function processBlobParts(parts, endings) {
return { parts: processedParts, size };
}

const NORMALIZE_PATTERN = new SafeRegExp(/^[\x20-\x7E]*$/);

/**
* @param {string} str
* @returns {string}
*/
function normalizeType(str) {
let normalizedType = str;
if (!RegExpPrototypeTest(NORMALIZE_PATTERN, str)) {
normalizedType = "";
}
return StringPrototypeToLowerCase(normalizedType);
const mime = mimesniff.parseMimeType(str);
return mime !== null ? mimesniff.serializeMimeType(mime) : "";
}

/**
Expand Down
Loading