Implement JS flipper client
Summary: Standardize WS implementation for JS environments. Why do we need a separate server implementation for browsers? Browser targets cannot authenticate via the default certificate exchange flow. We need a dedicated client for them that works over an insecure channel (without the cert exchange). Major changes: 1. Renamed `flipper-js-client-sdk` to `js-flipper` for consistency with `react-native-flipper` 2. Updated `js-flipper` implementation to match our other existing clients Documentation will be updated in a separate subsequent PR. https://fb.quip.com/2mboA0xbgoxl Reviewed By: mweststrate Differential Revision: D31688105 fbshipit-source-id: 418aa80e0fd86361c089cf54b0d44a8b4f748efa
This commit is contained in:
committed by
Facebook GitHub Bot
parent
2be631ea4d
commit
9a47f41056
70
js/js-flipper/src/util.ts
Normal file
70
js/js-flipper/src/util.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
// https://github.com/microsoft/TypeScript/issues/36931#issuecomment-846131999
|
||||
type Assert = (condition: unknown) => asserts condition;
|
||||
export const assert: Assert = (condition) => {
|
||||
if (!condition) {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
|
||||
export const safeJSONStringify = (data: unknown): string => {
|
||||
try {
|
||||
return JSON.stringify(data);
|
||||
} catch {
|
||||
return 'Unable to serialize';
|
||||
}
|
||||
};
|
||||
|
||||
export const isPromise = (val: unknown): val is Promise<unknown> =>
|
||||
typeof val === 'object' &&
|
||||
val !== null &&
|
||||
typeof (val as Promise<unknown>).then === 'function' &&
|
||||
typeof (val as Promise<unknown>).catch === 'function';
|
||||
|
||||
// TODO: Share types wiht desktop
|
||||
type OS =
|
||||
| 'iOS'
|
||||
| 'Android'
|
||||
| 'Metro'
|
||||
| 'Windows'
|
||||
| 'MacOS'
|
||||
| 'Browser'
|
||||
| 'Linux';
|
||||
|
||||
// https://stackoverflow.com/a/31456668
|
||||
const detectDeviceType = () =>
|
||||
typeof process === 'object' &&
|
||||
typeof process.versions === 'object' &&
|
||||
typeof process.versions.node !== 'undefined'
|
||||
? 'Node.js'
|
||||
: 'Browser';
|
||||
export const detectOS = (): OS => {
|
||||
if (detectDeviceType() === 'Browser') {
|
||||
return 'Browser';
|
||||
}
|
||||
switch (require('os').type()) {
|
||||
case 'Linux':
|
||||
return 'Linux';
|
||||
case 'Darwin':
|
||||
return 'MacOS';
|
||||
default:
|
||||
return 'Windows';
|
||||
}
|
||||
};
|
||||
|
||||
export const detectDevice = (): string => {
|
||||
if (detectDeviceType() === 'Browser') {
|
||||
return window.navigator.userAgent;
|
||||
}
|
||||
return require('os').release();
|
||||
};
|
||||
|
||||
export const awaitTimeout = (timeout: number) => new Promise(resolve => setTimeout)
|
||||
Reference in New Issue
Block a user