UNPKG

debug-server-next

Version:

Dev server for hippy-core.

80 lines (79 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeUrl = exports.isConnectionValid = exports.parseWsUrl = void 0; const enum_1 = require("@/@types/enum"); const log_1 = require("@/utils/log"); const config_1 = require("@/config"); const log = new log_1.Logger('url-utils'); /** * 解析 ws 连接的 url 参数 */ 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') || ''; return { clientId, clientRole, pathname: url.pathname, contextName, deviceName, extensionName, }; }; exports.parseWsUrl = parseWsUrl; /** * 根据 ws url 参数判断连接是否合法,如果是 devtools 来源的 ws 连接,必须存在调试对象 */ const isConnectionValid = (wsUrlParams, debugTarget) => { const { clientRole, pathname, contextName } = wsUrlParams; const { clientId } = wsUrlParams; log.info('clientRole: %s', clientRole); if (clientRole === enum_1.ClientRole.Devtools) log.info('isConnectionValid debug target: %j', debugTarget); if (pathname !== config_1.config.wsPath) { log.warn('invalid ws connection path!'); return false; } if (clientRole === enum_1.ClientRole.Devtools && !debugTarget) { log.warn('debugTarget not exist! %s', clientId); return false; } if (!Object.values(enum_1.ClientRole).includes(clientRole)) { log.warn('invalid client role!'); return false; } if (clientRole === enum_1.ClientRole.IOS && !contextName) { log.warn('invalid iOS connection, should request with contextName!'); return false; } if (!clientId) { log.warn('invalid ws connection!'); return false; } return true; }; exports.isConnectionValid = isConnectionValid; /** * 根据 query 对象创建 url */ 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;