{"record":{"id":"9ebaed796b0ee933","repo":"denoland/deno","slug":"err-invalid-mime-syntax","errorCode":"ERR_INVALID_MIME_SYNTAX","errorMessage":"The MIME syntax for a type in \"${str}\" is invalid at ${invalidTypeIndex}","messagePattern":"The MIME syntax for a type in \"(.+?)\" is invalid at (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/mime.ts","lineNumber":74,"sourceCode":"const SOLIDUS = \"/\";\nconst SEMICOLON = \";\";\n\nfunction parseTypeAndSubtype(\n  str: string,\n): [string, string, number] {\n  // Skip only HTTP whitespace from start\n  let position = safeStringSearch(str, END_BEGINNING_WHITESPACE);\n  // read until '/'\n  const typeEnd = StringPrototypeIndexOf(str, SOLIDUS, position);\n  const trimmedType = typeEnd === -1\n    ? StringPrototypeSlice(str, position)\n    : StringPrototypeSlice(str, position, typeEnd);\n  const invalidTypeIndex = safeStringSearch(\n    trimmedType,\n    NOT_HTTP_TOKEN_CODE_POINT,\n  );\n  if (trimmedType === \"\" || invalidTypeIndex !== -1 || typeEnd === -1) {\n    throw new ERR_INVALID_MIME_SYNTAX(\"type\", str, invalidTypeIndex);\n  }\n  // skip type and '/'\n  position = typeEnd + 1;\n  const type = toASCIILower(trimmedType);\n  // read until ';'\n  const subtypeEnd = StringPrototypeIndexOf(str, SEMICOLON, position);\n  const rawSubtype = subtypeEnd === -1\n    ? StringPrototypeSlice(str, position)\n    : StringPrototypeSlice(str, position, subtypeEnd);\n  position += rawSubtype.length;\n  if (subtypeEnd !== -1) {\n    // skip ';'\n    position += 1;\n  }\n  const trimmedSubtype = StringPrototypeSlice(\n    rawSubtype,\n    0,\n    safeStringSearch(rawSubtype, START_ENDING_WHITESPACE),","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/mime.ts#L56-L92","documentation":"While parsing a MIME type string (util.ts:74), the portion before '/' (the type) must be a non-empty HTTP token: after skipping leading HTTP whitespace, if no '/' exists, the type is empty, or the type contains a non-token code point, ERR_INVALID_MIME_SYNTAX is thrown naming 'type' and the offending index.","triggerScenarios":"Constructing MIMEType (or parsing via MIMEType.parse / node:mime polyfill used by headers such as Content-Type parsing paths) with strings like 'plain', '/json', 'te xt/plain', 'text\u0000/plain', or leading junk — the token scan NOT_HTTP_TOKEN_CODE_POINT locates the first illegal character.","commonSituations":"Forwarding user-supplied Content-Type/Accept values into a MIME API without validation; strings built with template literals that include a stray space or unicode char; empty type after whitespace trimming (' /plain'); data read from config with missing values yielding ''.","solutions":["Validate with a token regex before parsing: /^\\s*[!#$%&'*+.^_|~A-Za-z0-9-]+\\//.test(str).","Trim and sanity-check user input: require exactly one '/', non-empty type and subtype.","Fall back to a default MIME type (e.g. application/octet-stream) when input fails validation instead of forwarding it."],"exampleFix":"// before\nconst m = new MIMEType(userInput); // userInput = 'plain' or 'te xt/plain'\n\n// after\nif (!/^\\s*[!#$%&'*+.^_|~A-Za-z0-9-]+\\/[!#$%&'*+.^_|~A-Za-z0-9-]+\\s*(;|$)/.test(userInput ?? '')) {\n  throw new TypeError('invalid MIME type');\n}\nconst m = new MIMEType(userInput);","handlingStrategy":"validation","validationCode":"const MIME_RE = /^\\s*[!#$%&'*+.^_|~A-Za-z0-9-]+\\/[!#$%&'*+.^_|~A-Za-z0-9-]+\\s*(;|$)/;\nif (!MIME_RE.test(String(input ?? ''))) throw new TypeError('invalid MIME type string');","typeGuard":"function looksLikeMimeType(s: string): boolean {\n  return /^[!#$%&'*+.^_|~A-Za-z0-9-]+\\/[!#$%&'*+.^_|~A-Za-z0-9-]+$/.test(s.trim().split(';')[0]);\n}","tryCatchPattern":"try { new MIMEType(input); } catch (e) { if (e.code === 'ERR_INVALID_MIME_SYNTAX') { return fallbackType; } throw e; }","preventionTips":["Validate user-supplied Content-Type/Accept values at the boundary","Default to application/octet-stream when parsing is not guaranteed"],"tags":["mime","parsing","validation","node-compat"],"backgroundTag":"invalid-mime-syntax","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}