blob: 02da275c44cf49c1d2f628d433aee29b0243d454 [file] [log] [blame]
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:261// 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 Lippe5a09d7a2020-01-23 13:41:295import * as Common from '../common/common.js';
Tim van der Lippe5a09d7a2020-01-23 13:41:296import * as Host from '../host/host.js';
7
Tim van der Lippe4d1ddf72020-01-23 13:02:148export class NodeURL {
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:269 /**
Tim van der Lippeaf23eb12020-03-19 16:49:0610 * @param {!{url: string}} object
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2611 */
Dmitry Gozmanb24fcc22018-10-31 23:42:4212 static patch(object) {
13 process(object, '');
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2614
15 /**
Tim van der Lippeaf23eb12020-03-19 16:49:0616 * @param {!{url: string}} object
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2617 * @param {string} path
18 */
19 function process(object, path) {
Tim van der Lippe5a09d7a2020-01-23 13:41:2920 if (object.url && NodeURL._isPlatformPath(object.url, Host.Platform.isWin())) {
21 object.url = Common.ParsedURL.ParsedURL.platformPathToURL(object.url);
Tim van der Lippe1d6e57a2019-09-30 11:55:3422 }
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2623 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 Lippe1d6e57a2019-09-30 11:55:3427 if (entryPath !== '.result.result.value' && value !== null && typeof value === 'object') {
Tim van der Lippeaf23eb12020-03-19 16:49:0628 process(/** @type {{url: string}} */ (value), entryPath);
Tim van der Lippe1d6e57a2019-09-30 11:55:3429 }
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2630 }
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 Kozyatinskiy03aa06a2018-09-13 20:55:2643 }
Mathias Bynensf06e8c02020-02-28 13:58:2844 return fileSystemPath.length ? fileSystemPath[0] === '/' : false;
Alexey Kozyatinskiy03aa06a2018-09-13 20:55:2645 }
Tim van der Lippe798ea142019-10-04 12:01:2346}