Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | 5a09d7a | 2020-01-23 13:41:29 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
Tim van der Lippe | 5a09d7a | 2020-01-23 13:41:29 | [diff] [blame] | 6 | import * as Host from '../host/host.js'; |
| 7 | |
Tim van der Lippe | 4d1ddf7 | 2020-01-23 13:02:14 | [diff] [blame] | 8 | export class NodeURL { |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 9 | /** |
Tim van der Lippe | af23eb1 | 2020-03-19 16:49:06 | [diff] [blame] | 10 | * @param {!{url: string}} object |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 11 | */ |
Dmitry Gozman | b24fcc2 | 2018-10-31 23:42:42 | [diff] [blame] | 12 | static patch(object) { |
| 13 | process(object, ''); |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 14 | |
| 15 | /** |
Tim van der Lippe | af23eb1 | 2020-03-19 16:49:06 | [diff] [blame] | 16 | * @param {!{url: string}} object |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 17 | * @param {string} path |
| 18 | */ |
| 19 | function process(object, path) { |
Tim van der Lippe | 5a09d7a | 2020-01-23 13:41:29 | [diff] [blame] | 20 | if (object.url && NodeURL._isPlatformPath(object.url, Host.Platform.isWin())) { |
| 21 | object.url = Common.ParsedURL.ParsedURL.platformPathToURL(object.url); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 22 | } |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 23 | for (const entry of Object.entries(object)) { |
| 24 | const key = entry[0]; |
| 25 | const value = entry[1]; |
| 26 | const entryPath = path + '.' + key; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 27 | if (entryPath !== '.result.result.value' && value !== null && typeof value === 'object') { |
Tim van der Lippe | af23eb1 | 2020-03-19 16:49:06 | [diff] [blame] | 28 | process(/** @type {{url: string}} */ (value), entryPath); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 29 | } |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param {string} fileSystemPath |
| 36 | * @param {boolean} isWindows |
| 37 | * @return {boolean} |
| 38 | */ |
| 39 | static _isPlatformPath(fileSystemPath, isWindows) { |
| 40 | if (isWindows) { |
| 41 | const re = /^([a-z]:[\/\\]|\\\\)/i; |
| 42 | return re.test(fileSystemPath); |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 43 | } |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 44 | return fileSystemPath.length ? fileSystemPath[0] === '/' : false; |
Alexey Kozyatinskiy | 03aa06a | 2018-09-13 20:55:26 | [diff] [blame] | 45 | } |
Tim van der Lippe | 798ea14 | 2019-10-04 12:01:23 | [diff] [blame] | 46 | } |