How to use the whatwg-mimetype.parse 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 jsdom / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
if (typeof body === "string") {
              encoding = "UTF-8";
            }
            const { buffer, formData, contentType } = extractBody(body);
            mimeType = contentType;
            flag.body = buffer || formData;
            flag.formData = Boolean(formData);
          }

          const existingContentType = xhrUtils.getRequestHeader(flag.requestHeaders, "content-type");
          if (mimeType !== null && existingContentType === null) {
            flag.requestHeaders["Content-Type"] = mimeType;
          } else if (existingContentType !== null && encoding !== null) {
            // Waiting for better spec: https://github.com/whatwg/xhr/issues/188. This seems like a good guess at what
            // the spec will be, in the meantime.
            const parsed = MIMEType.parse(existingContentType);
            if (parsed) {
              const charset = parsed.parameters.get("charset");
              if (charset && !asciiCaseInsensitiveMatch(charset, encoding) && encoding !== null) {
                parsed.parameters.set("charset", encoding);
                xhrUtils.updateRequestHeader(flag.requestHeaders, "content-type", parsed.toString());
              }
            }
          }
        }
      } finally {
        if (properties.beforeSend) {
          properties.beforeSend = false;
        } else {
          throw new DOMException("The object is in an invalid state.", "InvalidStateError");
        }
      }
github flaviuse / mern-authentication / client / node_modules / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
if (typeof body === "string") {
              encoding = "UTF-8";
            }
            const { buffer, formData, contentType } = extractBody(body);
            mimeType = contentType;
            flag.body = buffer || formData;
            flag.formData = Boolean(formData);
          }

          const existingContentType = xhrUtils.getRequestHeader(flag.requestHeaders, "content-type");
          if (mimeType !== null && existingContentType === null) {
            flag.requestHeaders["Content-Type"] = mimeType;
          } else if (existingContentType !== null && encoding !== null) {
            // Waiting for better spec: https://github.com/whatwg/xhr/issues/188. This seems like a good guess at what
            // the spec will be, in the meantime.
            const parsed = MIMEType.parse(existingContentType);
            if (parsed) {
              const charset = parsed.parameters.get("charset");
              if (charset && !asciiCaseInsensitiveMatch(charset, encoding) && encoding !== null) {
                parsed.parameters.set("charset", encoding);
              }
              xhrUtils.updateRequestHeader(flag.requestHeaders, "content-type", parsed.toString());
            }
          }
        }
      } finally {
        if (properties.beforeSend) {
          properties.beforeSend = false;
        } else {
          throw new DOMException("The object is in an invalid state.", "InvalidStateError");
        }
      }
github flaviuse / mern-authentication / client / node_modules / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
overrideMimeType(mime) {
      mime = String(mime);

      const { readyState } = this;
      if (readyState === XMLHttpRequest.LOADING || readyState === XMLHttpRequest.DONE) {
        throw new DOMException("The object is in an invalid state.", "InvalidStateError");
      }

      this[xhrSymbols.flag].overrideMIMEType = "application/octet-stream";

      // Waiting for better spec: https://github.com/whatwg/xhr/issues/157
      const parsed = MIMEType.parse(mime);
      if (parsed) {
        this[xhrSymbols.flag].overrideMIMEType = parsed.essence;

        const charset = parsed.parameters.get("charset");
        if (charset) {
          this[xhrSymbols.flag].overrideCharset = whatwgEncoding.labelToName(charset);
        }
      }
    }
github jsdom / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
overrideMimeType(mime) {
      mime = String(mime);

      const { readyState } = this;
      if (readyState === XMLHttpRequest.LOADING || readyState === XMLHttpRequest.DONE) {
        throw new DOMException("The object is in an invalid state.", "InvalidStateError");
      }

      this[xhrSymbols.flag].overrideMIMEType = "application/octet-stream";

      // Waiting for better spec: https://github.com/whatwg/xhr/issues/157
      const parsed = MIMEType.parse(mime);
      if (parsed) {
        this[xhrSymbols.flag].overrideMIMEType = parsed.essence;

        const charset = parsed.parameters.get("charset");
        if (charset) {
          this[xhrSymbols.flag].overrideCharset = whatwgEncoding.labelToName(charset);
        }
      }
    }
github jsdom / jsdom / lib / jsdom / living / nodes / HTMLFrameElement-impl.js View on Github external
function onFrameLoaded(data) {
    const sniffOptions = {
      defaultEncoding: document._encoding
    };

    if (request.response) {
      const contentType = MIMEType.parse(request.response.headers["content-type"]) || new MIMEType("text/plain");
      sniffOptions.transportLayerEncodingLabel = contentType.parameters.get("charset");

      if (contentType) {
        if (contentType.isXML()) {
          contentDoc._parsingMode = "xml";
        }
        contentDoc.contentType = contentType.essence;
      }
    }

    const encoding = sniffHTMLEncoding(data, sniffOptions);
    contentDoc._encoding = encoding;

    const html = whatwgEncoding.decode(data, contentDoc._encoding);

    try {
github sx1989827 / DOClever / node_modules / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
function finalCharset(xhr) {
  const flag = xhr[xhrSymbols.flag];
  if (flag.overrideCharset) {
    return flag.overrideCharset;
  }
  const parsedContentType = MIMEType.parse(getResponseHeader(xhr, "content-type"));
  if (parsedContentType) {
    return whatwgEncoding.labelToName(parsedContentType.parameters.get("charset"));
  }
  return null;
}
github zubairghori / Ultimate_todo_list / node_modules / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
function finalCharset(xhr) {
  const flag = xhr[xhrSymbols.flag];
  if (flag.overrideCharset) {
    return flag.overrideCharset;
  }
  const parsedContentType = MIMEType.parse(getResponseHeader(xhr, "content-type"));
  if (parsedContentType) {
    return whatwgEncoding.labelToName(parsedContentType.parameters.get("charset"));
  }
  return null;
}
github zubairghori / Ultimate_todo_list / node_modules / jsdom / lib / jsdom / living / xmlhttprequest.js View on Github external
}
      if (properties.responseXMLCache) {
        return properties.responseXMLCache;
      }
      const responseBuffer = properties.responseBuffer ?
                             properties.responseBuffer.slice(0, properties.totalReceivedChunkSize) :
                             null;

      if (!responseBuffer) {
        return null;
      }

      const contentType = finalMIMEType(this);
      let isHTML = false;
      let isXML = false;
      const parsed = MIMEType.parse(contentType);
      if (parsed) {
        isHTML = parsed.isHTML();
        isXML = parsed.isXML();
        if (!isXML && !isHTML) {
          return null;
        }
      }

      if (this.responseType === "" && isHTML) {
        return null;
      }

      const encoding = finalCharset(this) || whatwgEncoding.getBOMEncoding(responseBuffer) || "UTF-8";
      const resText = whatwgEncoding.decode(responseBuffer, encoding);

      if (!resText) {

whatwg-mimetype

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

MIT
Latest version published 10 months ago

Package Health Score

74 / 100
Full package analysis

Popular whatwg-mimetype functions