Repository: microsoft/playwright
File: packages/trace-viewer/src/third_party/devtools.ts (TypeScript)

Code before:
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import { TraceModel } from '@isomorphic/trace/traceModel';
import type { Entry } from '@trace/har';

// The following function is derived from Chromium's source code
// https://github.com/ChromeDevTools/devtools-frontend/blob/83cbe41b4107e188a1f66fdf6ea3a9cca42587c6/front_end/panels/network/NetworkLogView.ts#L2363
export async function generateCurlCommand(model: TraceModel | undefined, resource: Entry): Promise<string> {
  const platform = navigator.platform.includes('Win') ? 'win' : 'unix';
  let command: string[] = [];
  // Most of these headers are derived from the URL and are automatically added by cURL.
  // The |Accept-Encoding| header is ignored to prevent decompression errors. crbug.com/1015321
  const ignoredHeaders =
    new Set<string>(['accept-encoding', 'host', 'method', 'path', 'scheme', 'version', 'authority', 'protocol']);

  function escapeStringWin(str: string): string {
    /* Always escape the " characters so that we can use caret escaping.

   Because cmd.exe parser and MS Crt arguments parsers use some of the
   same escape characters, they can interact with each other in
   horrible ways, the order of operations is critical.

   Replace \ with \\ first because it is an escape character for certain
   conditions in both parsers.

   Replace all " with \" to ensure the first parser does not remove it.

   Then escape all characters we are not sure about with ^ to ensure it

Review comment (suggestion):
```suggestion
import type { TraceModel } from '@isomorphic/trace/traceModel';
```

Code after:
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import type { TraceModel } from '@isomorphic/trace/traceModel';
import type { Entry } from '@trace/har';

// The following function is derived from Chromium's source code
// https://github.com/ChromeDevTools/devtools-frontend/blob/83cbe41b4107e188a1f66fdf6ea3a9cca42587c6/front_end/panels/network/NetworkLogView.ts#L2363
export async function generateCurlCommand(model: TraceModel | undefined, resource: Entry): Promise<string> {
  const platform = navigator.platform.includes('Win') ? 'win' : 'unix';
  let command: string[] = [];
  // Most of these headers are derived from the URL and are automatically added by cURL.
  // The |Accept-Encoding| header is ignored to prevent decompression errors. crbug.com/1015321
  const ignoredHeaders =
    new Set<string>(['accept-encoding', 'host', 'method', 'path', 'scheme', 'version', 'authority', 'protocol']);

  function escapeStringWin(str: string): string {
    /* Always escape the " characters so that we can use caret escaping.

   Because cmd.exe parser and MS Crt arguments parsers use some of the
   same escape characters, they can interact with each other in
   horrible ways, the order of operations is critical.

   Replace \ with \\ first because it is an escape character for certain
   conditions in both parsers.

   Replace all " with \" to ensure the first parser does not remove it.

   Then escape all characters we are not sure about with ^ to ensure it