How to use the whatwg-mimetype function in whatwg-mimetype

To help you get started, we’ve selected a few whatwg-mimetype examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wantora / weautopagerize / src / lib / content / fetchHTMLText.js View on Github external
async function nativeFetch(url) {
  const response = await fetch(url, {
    credentials: "include",
    redirect: "follow",
    headers: {
      "User-Agent": navigator.userAgent, // https://github.com/wantora/weautopagerize/issues/6
    },
  });
  const responseURL = new URL(response.url);

  const contentType = response.headers.get("Content-Type");
  if (contentType === null) {
    throw new Error(`Content-Type Error: ${contentType}`);
  }
  const mimeType = new MIMEType(contentType);
  if (!mimeType.isHTML() && mimeType.essence !== "application/xhtml+xml") {
    throw new Error(`Content-Type Error: ${contentType}`);
  }

  const ab = await response.arrayBuffer();
  const charset = mimeType.parameters.get("charset") || document.characterSet;
  const textDecoder = new TextDecoder(charset);
  const responseText = textDecoder.decode(ab);

  return {responseURL, responseText};
}
github inrupt / wac-ldp / src / lib / rdf / ResourceDataUtils.ts View on Github external
export function determineRdfType (contentType: string | undefined): RdfType {
  if (!contentType) {
    return RdfType.NoPref
  }
  let rdfType
  try {
    const mimeType = new MIMEType(contentType)
    switch (mimeType.essence) {
      case 'application/ld+json':
        return RdfType.JsonLd
        break
      case 'text/turtle':
        return RdfType.Turtle
        break
      default:
        debug('not an RDF content-type', contentType, mimeType.essence)
        return RdfType.Unknown
    }
    debug({ rdfType, contentType, essence: mimeType.essence })
  } catch (e) {
    debug('error determining rdf type', e.message)
    return RdfType.Unknown
  }

whatwg-mimetype

Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard

MIT
Latest version published 6 months ago

Package Health Score

78 / 100
Full package analysis

Popular whatwg-mimetype functions