flipper-common
Version:
Server & UI shared Flipper utilities
75 lines • 2.73 kB
JavaScript
;
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deconstructPluginKey = exports.deconstructClientId = exports.buildGenericClientIdFromQuery = exports.buildGenericClientId = exports.buildClientId = void 0;
function buildClientId(clientInfo) {
const escapedName = escape(clientInfo.app);
const result = `${escapedName}#${clientInfo.os}#${clientInfo.device}#${clientInfo.device_id}`;
// N.B.: device_id can be empty, which designates the host device
for (const key of ['app', 'os', 'device']) {
if (!clientInfo[key]) {
console.error(`Attempted to build clientId with invalid ${key}: "${clientInfo[key]} (identifier: ${result})`);
}
}
return result;
}
exports.buildClientId = buildClientId;
function buildGenericClientId(client) {
const escapedName = escape(client.appName);
return `${client.os}:${client.deviceName}:${escapedName}`.toLowerCase();
}
exports.buildGenericClientId = buildGenericClientId;
function buildGenericClientIdFromQuery(clientQuery) {
const escapedName = escape(clientQuery.app);
return `${clientQuery.os}:${clientQuery.device}:${escapedName}`.toLowerCase();
}
exports.buildGenericClientIdFromQuery = buildGenericClientIdFromQuery;
function deconstructClientId(clientId) {
if (!clientId || clientId.split('#').length !== 4) {
console.error(`Attempted to deconstruct invalid clientId: "${clientId}"`);
}
let [app, os, device, device_id] = clientId.split('#');
app = unescape(app);
return {
app,
os,
device,
device_id,
};
}
exports.deconstructClientId = deconstructClientId;
function deconstructPluginKey(pluginKey) {
const parts = pluginKey.split('#');
if (parts.length === 2) {
// Device plugin
return {
type: 'device',
client: parts[0],
pluginName: parts[1],
};
}
else {
// Client plugin
const lastHashIndex = pluginKey.lastIndexOf('#');
const clientId = pluginKey.slice(0, lastHashIndex);
const pluginName = pluginKey.slice(lastHashIndex + 1);
if (!pluginName) {
console.error(`Attempted to deconstruct invalid pluginKey: "${pluginKey}"`);
}
return {
type: 'client',
...deconstructClientId(clientId),
client: clientId,
pluginName,
};
}
}
exports.deconstructPluginKey = deconstructPluginKey;
//# sourceMappingURL=clientUtils.js.map