UNPKG

@hippy/debug-server-next

Version:
114 lines (113 loc) 4.36 kB
"use strict"; /* * Tencent is pleased to support the open source community by making * Hippy available. * * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getHomeUrl = exports.getBaseFolderOfPublicPath = exports.getWSProtocolByHttpProtocol = exports.makeUrl = exports.getWsInvalidReason = exports.parseWsUrl = void 0; const lodash_1 = require("lodash"); const enum_1 = require("@debug-server-next/@types/enum"); const config_1 = require("@debug-server-next/config"); /** * parse debug WebSocket url params */ const parseWsUrl = (reqUrl) => { const url = new URL(reqUrl, 'http://0.0.0.0'); const clientId = url.searchParams.get('clientId'); const contextName = url.searchParams.get('contextName'); const clientRole = url.searchParams.get('role'); const deviceName = url.searchParams.get('deviceName'); const extensionName = url.searchParams.get('extensionName') || ''; const hash = url.searchParams.get('hash') || ''; const platform = (url.searchParams.get('platform') || enum_1.DevicePlatform.Unknown); return { clientId, clientRole, pathname: url.pathname, contextName, deviceName, extensionName, hash, platform, }; }; exports.parseWsUrl = parseWsUrl; /** * check WS url params * - in devtools case, must exist debugTarget * - in HMR case, must has hash field */ const getWsInvalidReason = (wsUrlParams) => { const { clientRole, pathname } = wsUrlParams; if (pathname !== config_1.config.wsPath) return 'invalid ws connection path!'; if (!Object.values(enum_1.ClientRole).includes(clientRole)) return 'invalid client role!'; if (clientRole === enum_1.ClientRole.HMRClient || clientRole === enum_1.ClientRole.HMRServer) { if ('hash' in wsUrlParams) return ''; return 'invalid HMR ws params, must connect with hash field!'; } const { clientId } = wsUrlParams; // if (clientRole === ClientRole.Devtools && !('hash' in wsUrlParams) && config.isCluster) // return 'invalid ws connection, you ws url should carry `hash` query params!'; /** * no need to validate contextName, because iOS hippy v3.0 couldn't get contextName when debug connection * contextName will update by TDFRuntime.updateContextInfo event */ // if (clientRole === ClientRole.IOS && !contextName) return 'invalid iOS connection, should request with contextName!'; if (clientRole !== enum_1.ClientRole.JSRuntime && !clientId) return 'invalid ws connection!'; return ''; }; exports.getWsInvalidReason = getWsInvalidReason; /** * make full URL with query params */ const makeUrl = (baseUrl, query = {}) => { let fullUrl = baseUrl; const keys = Object.keys(query); for (const [i, key] of keys.entries()) { if (i === 0) { if (fullUrl.indexOf('?') === -1) { fullUrl += '?'; } } else { fullUrl += '&'; } fullUrl += `${key}=${encodeURIComponent(query[key])}`; } return fullUrl; }; exports.makeUrl = makeUrl; const getWSProtocolByHttpProtocol = (httpProtocol) => ({ https: 'wss', http: 'ws', }[httpProtocol] || 'ws'); exports.getWSProtocolByHttpProtocol = getWSProtocolByHttpProtocol; const getBaseFolderOfPublicPath = (publicPath) => { const url = new URL(publicPath, 'http://0.0.0.0'); return (0, lodash_1.trim)(url.pathname, '/'); }; exports.getBaseFolderOfPublicPath = getBaseFolderOfPublicPath; const getHomeUrl = () => { const { env } = global.debugAppArgv; return `${config_1.config.domain}/extensions/home.html?env=${env}`; }; exports.getHomeUrl = getHomeUrl;