MCPcopy Index your code
hub / github.com/nodejs/node / #updateContext

Method #updateContext

lib/internal/url.js:916–959  ·  view source on GitHub ↗

* Update the internal context state for URL. * @param {string} href New href string from `bindingUrl.update`. * @param {boolean} [shouldUpdateSearchParams] If the update has potential to update search params (href/search).

(href, shouldUpdateSearchParams = false)

Source from the content-addressed store, hash-verified

914 * @param {boolean} [shouldUpdateSearchParams] If the update has potential to update search params (href/search).
915 */
916 #updateContext(href, shouldUpdateSearchParams = false) {
917 const previousSearch = shouldUpdateSearchParams && this.#searchParams &&
918 (this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
919
920 this.#context.href = href;
921
922 const {
923 0: protocol_end,
924 1: username_end,
925 2: host_start,
926 3: host_end,
927 4: port,
928 5: pathname_start,
929 6: search_start,
930 7: hash_start,
931 8: scheme_type,
932 } = bindingUrl.urlComponents;
933
934 this.#context.protocol_end = protocol_end;
935 this.#context.username_end = username_end;
936 this.#context.host_start = host_start;
937 this.#context.host_end = host_end;
938 this.#context.port = port;
939 this.#context.pathname_start = pathname_start;
940 this.#context.search_start = search_start;
941 this.#context.hash_start = hash_start;
942 this.#context.scheme_type = scheme_type;
943
944 if (this.#searchParams) {
945 // If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
946 // Only do this when we're expecting it to have changed, otherwise a change to hash etc.
947 // would incorrectly compare the URLSearchParams state to the empty URL search state.
948 if (shouldUpdateSearchParams) {
949 const currentSearch = this.#getSearchFromContext();
950 if (previousSearch !== currentSearch) {
951 setURLSearchParams(this.#searchParams, currentSearch);
952 this.#searchParamsModified = false;
953 }
954 }
955
956 // If we have a URLSearchParams, ensure that URL is up-to-date with any modification to it.
957 this.#ensureSearchParamsUpdated();
958 }
959 }
960
961 toString() {
962 // Updates to URLSearchParams are lazily propagated to URL, so we need to check we're in sync.

Callers 13

constructorMethod · 0.95
hrefMethod · 0.95
protocolMethod · 0.95
usernameMethod · 0.95
passwordMethod · 0.95
hostMethod · 0.95
hostnameMethod · 0.95
portMethod · 0.95
pathnameMethod · 0.95
searchMethod · 0.95
hashMethod · 0.95

Calls 3

#getSearchFromParamsMethod · 0.95
#getSearchFromContextMethod · 0.95

Tested by

no test coverage detected