Skip to content

Commit

Permalink
core(lantern): duplicate isNonNetworkProtocol inside lib/lantern
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Apr 10, 2024
1 parent 3365c3c commit 762d170
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions core/lib/lantern/network-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 762d170

Please sign in to comment.