From 762d170e1dbbeac8b24b4117ced5d912eff12c20 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Wed, 10 Apr 2024 14:42:21 -0700 Subject: [PATCH] core(lantern): duplicate isNonNetworkProtocol inside lib/lantern --- core/lib/lantern/network-node.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/core/lib/lantern/network-node.js b/core/lib/lantern/network-node.js index e6adb97c0ce1..b445117cb44b 100644 --- a/core/lib/lantern/network-node.js +++ b/core/lib/lantern/network-node.js @@ -7,8 +7,32 @@ import * as Lantern from './types/lantern.js'; import {NetworkRequestTypes} from './lantern.js'; import {BaseNode} from './base-node.js'; -// TODO(15841): bring impl of isNonNetworkRequest inside lantern and remove this. -import UrlUtils from '../url-utils.js'; + +const NON_NETWORK_SCHEMES = [ + 'blob', // @see https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL + 'data', // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs + 'intent', // @see https://developer.chrome.com/docs/multidevice/android/intents/ + 'file', // @see https://en.wikipedia.org/wiki/File_URI_scheme + 'filesystem', // @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystem + 'chrome-extension', +]; + +/** + * Use `NetworkRequest.isNonNetworkRequest(req)` if working with a request. + * Note: the `protocol` field from CDP can be 'h2', 'http', (not 'https'!) or it'll be url's scheme. + * https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/protocol/network_handler.cc;l=598-611;drc=56d4a9a9deb30be73adcee8737c73bcb2a5ab64f + * However, a `new URL(href).protocol` has a colon suffix. + * https://url.spec.whatwg.org/#dom-url-protocol + * A URL's `scheme` is specced as the `protocol` sans-colon, but isn't exposed on a URL object. + * This method can take all 3 of these string types as a parameter. + * @param {string} protocol Either a networkRequest's `protocol` per CDP or a `new URL(href).protocol` + * @return {boolean} + */ +function isNonNetworkProtocol(protocol) { + // Strip off any colon + const urlScheme = protocol.includes(':') ? protocol.slice(0, protocol.indexOf(':')) : protocol; + return NON_NETWORK_SCHEMES.includes(urlScheme); +} /** * @template [T=any] @@ -75,9 +99,9 @@ class NetworkNode extends BaseNode { */ get isNonNetworkProtocol() { // The 'protocol' field in devtools a string more like a `scheme` - return UrlUtils.isNonNetworkProtocol(this.request.protocol) || + return isNonNetworkProtocol(this.request.protocol) || // But `protocol` can fail to be populated if the request fails, so fallback to scheme. - UrlUtils.isNonNetworkProtocol(this.request.parsedURL.scheme); + isNonNetworkProtocol(this.request.parsedURL.scheme); } /**