Skip to content

Runtime APIs: support MIMEType.parse() from node:util #7663

Open

Description

@valtlai

The MIMEType.parse(string) static method is like the new MIMEType(input) constructor, but instead of throwing on parse error, it returns null.

Node.js docs: https://nodejs.org/api/util.html#mimetypeparsestring

My use case
import { MIMEType } from 'node:util';

// A simple polyfill
MIMEType.parse ??= (string) => {
  try {
    return new MIMEType(string);
  } catch {
    return null;
  }
};

export default {
  fetch(request) {
    // ...

    // No need to handle parse error here
    if (
      MIMEType.parse(request.headers.get('Content-Type'))?.essence !==
      'application/x-www-form-urlencoded'
    ) {
      return Response.json({
        message: 'Content-Type not application/x-www-form-urlencoded',
      }, { status: 400 });
    }

    // ...
  },
};

Activity

  1. changed the title [-]Runtime APIs: support `MIMEType.parse()` from `node:utils`[/-] [+]Runtime APIs: support `MIMEType.parse()` from `node:util`[/+] on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions