/* @flow */
"use strict";

// TODO: Use `URL` class, see http://nodejs.org/api/url.html#url_class_url
const parse = require("url").parse; // eslint-disable-line node/no-deprecated-api

/**
 * Get unit from value node
 *
 * Returns `null` if the unit is not found.
 */
module.exports = function(urlString /*: string*/) /*: ?string*/ {
  const url = parse(urlString);
  const protocol = url.protocol;

  if (protocol === null || typeof protocol === "undefined") {
    return null;
  }

  const scheme = protocol.slice(0, -1); // strip trailing `:`

  // The URL spec does not require a scheme to be followed by `//`, but checking
  // for it allows this rule to differentiate <scheme>:<hostname> urls from
  // <hostname>:<port> urls. `data:` scheme urls are an exception to this rule.
  const slashIndex = protocol.length;
  const expectedSlashes = urlString.slice(slashIndex, slashIndex + 2);
  const isSchemeLessUrl = expectedSlashes !== "//" && scheme !== "data";

  if (isSchemeLessUrl) {
    return null;
  }

  return scheme;
};

@@ -24,164 +24,39 @@
 %22;%0A%0A
-// TODO: Use %60URL%60 class, see http://nodejs.org/api/url.html#url_class_url%0Aconst parse = require(%22url%22).parse; // eslint-disable-line node/no-deprecated-api
+const %7B URL %7D = require(%22url%22);
 %0A%0A/*
@@ -208,70 +208,120 @@
 %7B%0A  
-const url = parse(urlString);%0A  const protocol = url.protocol;
+let protocol = null;%0A%0A  try %7B%0A    protocol = new URL(urlString).protocol;%0A  %7D catch (err) %7B%0A    return null;%0A  %7D
 %0A%0A  

Replace Node.js legacy API for URL parsing. Fixes #3803 (#4024)