<commit_before>/**
 * Copyright 2018 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import NodeWebSocket from 'ws';
import {ConnectionTransport} from '../common/ConnectionTransport.js';
import {packageVersion} from '../generated/version.js';
import {promises as dns} from 'dns';

/**
 * @internal
 */
export class NodeWebSocketTransport implements ConnectionTransport {
  static async create(urlString: string): Promise<NodeWebSocketTransport> {
    // TODO(jrandolf): Starting in Node 17, IPv6 is favoured over IPv4 due to a change
    // in a default option:
    // - https://github.com/nodejs/node/issues/40537,
    // Due to this, for Firefox, we must parse and resolve the `localhost` hostname
    // manually with the previous behavior according to:
    // - https://nodejs.org/api/dns.html#dnslookuphostname-options-callback
    // because of https://bugzilla.mozilla.org/show_bug.cgi?id=1769994.
    const url = new URL(urlString);
    if (url.hostname === 'localhost') {
      const {address} = await dns.lookup(url.hostname, {verbatim: false});
      url.hostname = address;
    }

    return new Promise((resolve, reject) => {
      const ws = new NodeWebSocket(url, [], {
        followRedirects: true,
        perMessageDeflate: false,
        maxPayload: 256 * 1024 * 1024, // 256Mb
        headers: {<commit_msg>fix: import URL (#8670)<commit_after>/**
 * Copyright 2018 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import NodeWebSocket from 'ws';
import {ConnectionTransport} from '../common/ConnectionTransport.js';
import {packageVersion} from '../generated/version.js';
import {promises as dns} from 'dns';
import {URL} from 'url';

/**
 * @internal
 */
export class NodeWebSocketTransport implements ConnectionTransport {
  static async create(urlString: string): Promise<NodeWebSocketTransport> {
    // TODO(jrandolf): Starting in Node 17, IPv6 is favoured over IPv4 due to a change
    // in a default option:
    // - https://github.com/nodejs/node/issues/40537,
    // Due to this, for Firefox, we must parse and resolve the `localhost` hostname
    // manually with the previous behavior according to:
    // - https://nodejs.org/api/dns.html#dnslookuphostname-options-callback
    // because of https://bugzilla.mozilla.org/show_bug.cgi?id=1769994.
    const url = new URL(urlString);
    if (url.hostname === 'localhost') {
      const {address} = await dns.lookup(url.hostname, {verbatim: false});
      url.hostname = address;
    }

    return new Promise((resolve, reject) => {
      const ws = new NodeWebSocket(url, [], {
        followRedirects: true,
        perMessageDeflate: false,
        maxPayload: 256 * 1024 * 1024, // 256Mb
        headers: {
{'fraction_non_alphanumeric': 0.10805812417437252, 'fraction_numerical': 0.021136063408190225, 'mean_word_length': 3.433255269320843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1865065', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1023, 'n_words': 389}