@applitools/socket
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./transport"), exports); | ||
__exportStar(require("./types"), exports); |
@@ -27,16 +27,25 @@ "use strict"; | ||
exports.makeSocket = void 0; | ||
const logger_1 = require("@applitools/logger"); | ||
const transports = __importStar(require("./transports")); | ||
const utils = __importStar(require("@applitools/utils")); | ||
function makeSocket(socket, options) { | ||
function makeSocket(target, options) { | ||
var _a, _b; | ||
let ready = false; | ||
const listeners = new Map(); | ||
const queue = new Set(); | ||
const offs = new Set(); | ||
const transport = utils.types.isString(options.transport) | ||
? transports[options.transport] | ||
: options.transport; | ||
const logger = options.logger; | ||
if (socket) | ||
attach(socket); | ||
const logger = (_b = (_a = options.logger) === null || _a === void 0 ? void 0 : _a.extend({ label: 'socket' })) !== null && _b !== void 0 ? _b : (0, logger_1.makeLogger)({ label: 'socket' }); | ||
use(target); | ||
return { | ||
connect, | ||
destroy, | ||
get ready() { | ||
return ready; | ||
}, | ||
get target() { | ||
return target; | ||
}, | ||
use, | ||
cleanup, | ||
emit, | ||
@@ -48,10 +57,8 @@ on, | ||
command, | ||
create, | ||
ref, | ||
unref, | ||
wait, | ||
}; | ||
function attach(sock) { | ||
if (!sock) | ||
return; | ||
transport.onError(sock, error => { | ||
function use(socket) { | ||
cleanup(); | ||
target = socket; | ||
const offError = transport.onError(target, error => { | ||
const fns = listeners.get('error'); | ||
@@ -61,8 +68,22 @@ if (fns) | ||
}); | ||
const attach = () => { | ||
socket = sock; | ||
offs.add(offError); | ||
if (transport.isReady(target)) { | ||
attach(); | ||
} | ||
else { | ||
const offReady = transport.onReady(target, () => { | ||
attach(); | ||
const fns = listeners.get('ready'); | ||
if (fns) | ||
fns.forEach(fn => fn()); | ||
}); | ||
offs.add(offReady); | ||
} | ||
function attach() { | ||
ready = true; | ||
queue.forEach(command => command()); | ||
queue.clear(); | ||
transport.onMessage(socket, message => { | ||
const offMessage = transport.onMessage(target, message => { | ||
const { name, key, payload } = deserialize(message); | ||
logger === null || logger === void 0 ? void 0 : logger.log(`Received event of type "${JSON.stringify({ name, key })}" with payload`, payload && JSON.stringify(payload, null, 4).slice(3000)); | ||
const fns = listeners.get(name); | ||
@@ -77,3 +98,4 @@ if (fns) | ||
}); | ||
transport.onClose(socket, () => { | ||
offs.add(offMessage); | ||
const offClose = transport.onClose(target, () => { | ||
const fns = listeners.get('close'); | ||
@@ -83,25 +105,15 @@ if (fns) | ||
}); | ||
}; | ||
if (transport.isReady(sock)) | ||
attach(); | ||
else | ||
transport.onReady(sock, () => attach()); | ||
offs.add(offClose); | ||
} | ||
} | ||
function connect(options) { | ||
if (transport.connect) | ||
attach(transport.connect(options)); | ||
function cleanup() { | ||
offs.forEach(off => off()); | ||
offs.clear(); | ||
} | ||
function destroy() { | ||
if (!socket) | ||
return; | ||
if (transport.destroy) | ||
transport.destroy(socket); | ||
socket = null; | ||
} | ||
function emit(type, payload) { | ||
const command = () => { | ||
logger === null || logger === void 0 ? void 0 : logger.log('[EMIT EVENT]', type, JSON.stringify(payload, null, 4)); | ||
transport.send(socket, serialize(type, payload)); | ||
logger === null || logger === void 0 ? void 0 : logger.log(`Emit event of type "${JSON.stringify(type)}" with payload`, payload && JSON.stringify(payload, null, 4).slice(3000)); | ||
transport.send(target, serialize(type, payload)); | ||
}; | ||
if (socket) | ||
if (ready) | ||
command(); | ||
@@ -141,6 +153,8 @@ else | ||
const key = utils.general.guid(); | ||
emit({ name, key }, payload); | ||
once({ name, key }, response => { | ||
var _a; | ||
if (response.error) { | ||
const error = new Error(response.error.message); | ||
error.reason = (_a = response.error.reason) !== null && _a !== void 0 ? _a : 'unknown'; | ||
error.info = response.error.info; | ||
error.stack = response.error.stack; | ||
@@ -151,2 +165,3 @@ return reject(error); | ||
}); | ||
emit({ name, key }, payload); | ||
}); | ||
@@ -157,10 +172,7 @@ } | ||
var _a, _b; | ||
logger === null || logger === void 0 ? void 0 : logger.log('[COMMAND]', name, JSON.stringify(payload, null, 4)); | ||
try { | ||
const result = await fn(payload); | ||
logger === null || logger === void 0 ? void 0 : logger.log(`[COMMAND] ${name} finished successfully with result`, result && JSON.stringify(result, null, 4).slice(0, 3000)); | ||
emit({ name, key }, { result }); | ||
} | ||
catch (error) { | ||
logger === null || logger === void 0 ? void 0 : logger.log(`[COMMAND] ${name} failed with an error`, error); | ||
emit({ name, key }, { | ||
@@ -177,10 +189,12 @@ error: { | ||
} | ||
function create(name, fn) { | ||
let temporary = utils.promises.makeControlledPromise(); | ||
let result = temporary; | ||
on(name, async (payload) => { | ||
logger === null || logger === void 0 ? void 0 : logger.log('[CREATE]', name, JSON.stringify(payload, null, 4)); | ||
result = temporary; | ||
function wait(name, fnOrOptions, options) { | ||
const result = utils.promises.makeControlledPromise(); | ||
let fn; | ||
if (utils.types.isFunction(fnOrOptions)) | ||
fn = fnOrOptions; | ||
else | ||
options = fnOrOptions; | ||
const off = on(name, async (payload) => { | ||
try { | ||
result.resolve(await fn(payload)); | ||
result.resolve((await (fn === null || fn === void 0 ? void 0 : fn(payload)))); | ||
} | ||
@@ -190,6 +204,9 @@ catch (error) { | ||
} | ||
finally { | ||
temporary = utils.promises.makeControlledPromise(); | ||
} | ||
}); | ||
if (options === null || options === void 0 ? void 0 : options.timeout) { | ||
utils.general.sleep(options.timeout).then(() => { | ||
off(); | ||
result.reject(new Error(`Event with name "${name}" wasn't emitted within ${options.timeout}ms`)); | ||
}); | ||
} | ||
return { | ||
@@ -199,25 +216,7 @@ then: (onResolved, onRejected) => result.then(onResolved, onRejected), | ||
} | ||
function ref() { | ||
if (!transport.ref) | ||
return () => undefined; | ||
const command = () => transport.ref(socket); | ||
if (socket) | ||
command(); | ||
else | ||
queue.add(command); | ||
return () => queue.delete(command); | ||
} | ||
function unref() { | ||
if (!transport.unref) | ||
return () => undefined; | ||
const command = () => transport.unref(socket); | ||
if (socket) | ||
command(); | ||
else | ||
queue.add(command); | ||
return () => queue.delete(command); | ||
} | ||
function serialize(type, payload) { | ||
var _a, _b; | ||
const message = utils.types.isString(type) ? { name: type, payload } : { name: type.name, key: type.key, payload }; | ||
return transport.format(JSON.stringify(message)); | ||
const data = JSON.stringify(message); | ||
return (_b = (_a = transport.format) === null || _a === void 0 ? void 0 : _a.call(transport, data)) !== null && _b !== void 0 ? _b : data; | ||
} | ||
@@ -224,0 +223,0 @@ function deserialize(message) { |
@@ -6,4 +6,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ipc = void 0; | ||
exports.ee = exports.ws = exports.ipc = void 0; | ||
var ipc_1 = require("./ipc"); | ||
Object.defineProperty(exports, "ipc", { enumerable: true, get: function () { return __importDefault(ipc_1).default; } }); | ||
var ws_1 = require("./ws"); | ||
Object.defineProperty(exports, "ws", { enumerable: true, get: function () { return __importDefault(ws_1).default; } }); | ||
var ee_1 = require("./ee"); | ||
Object.defineProperty(exports, "ee", { enumerable: true, get: function () { return __importDefault(ee_1).default; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transport = void 0; | ||
const net_1 = require("net"); | ||
exports.transport = { | ||
@@ -11,27 +10,20 @@ isReady(socket) { | ||
socket.on('ready', callback); | ||
return () => socket.off('ready', callback); | ||
}, | ||
onMessage(socket, callback) { | ||
socket.on('data', data => splitMessages(data).forEach(data => callback(data))); | ||
const handler = (data) => splitMessages(data).forEach(data => callback(data)); | ||
socket.on('data', handler); | ||
return () => socket.off('data', handler); | ||
}, | ||
onClose(socket, callback) { | ||
socket.on('close', callback); | ||
return () => socket.off('close', callback); | ||
}, | ||
onError(socket, callback) { | ||
socket.on('error', callback); | ||
return () => socket.off('error', callback); | ||
}, | ||
connect(options) { | ||
return (0, net_1.createConnection)(options); | ||
}, | ||
send(socket, data) { | ||
socket.write(data); | ||
}, | ||
destroy(socket) { | ||
socket.destroy(); | ||
}, | ||
ref(socket) { | ||
socket.ref(); | ||
}, | ||
unref(socket) { | ||
socket.unref(); | ||
}, | ||
format(data) { | ||
@@ -38,0 +30,0 @@ const header = Buffer.allocUnsafe(4); |
{ | ||
"name": "@applitools/socket", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Applitools implementation for bidi-communication protocol", | ||
@@ -57,3 +57,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@applitools/bongo": "^2.2.2", | ||
"@applitools/bongo": "^2.2.5", | ||
"@types/mocha": "^9.1.1", | ||
@@ -60,0 +60,0 @@ "@types/node": "12", |
export * from './socket'; | ||
export * from './transport'; | ||
export * from './types'; |
import { type Logger } from '@applitools/logger'; | ||
import { type Transport } from './transport'; | ||
import * as transports from './transports'; | ||
export interface Socket<TConnectOptions = never> { | ||
connect(options: TConnectOptions): void; | ||
destroy(): void; | ||
export type WaitOptions = { | ||
timeout: number; | ||
}; | ||
export interface Socket<TSocket = unknown> { | ||
readonly ready: boolean; | ||
readonly target: TSocket; | ||
use(socket: TSocket): void; | ||
cleanup(): void; | ||
emit(type: string | { | ||
@@ -25,10 +30,9 @@ name: string; | ||
command(name: string, fn: (payload?: any) => any): () => void; | ||
create<TResult>(name: string, fn: (payload?: any) => TResult): PromiseLike<TResult>; | ||
ref(): () => void; | ||
unref(): () => void; | ||
wait(name: string, options?: WaitOptions): PromiseLike<void>; | ||
wait<TResult>(name: string, fn: (payload?: any) => TResult, options?: WaitOptions): PromiseLike<TResult>; | ||
} | ||
export interface SocketOptions<TTransport extends keyof typeof transports | Transport<unknown, unknown>> { | ||
export interface SocketOptions<TTransport extends keyof typeof transports | Transport<unknown>> { | ||
transport: TTransport; | ||
logger?: Logger; | ||
} | ||
export declare function makeSocket<TTransport extends keyof typeof transports | Transport<unknown, unknown>, TSocket extends TTransport extends keyof typeof transports ? (typeof transports)[TTransport] extends Transport<infer USocket, unknown> ? USocket : never : TTransport extends Transport<infer USocket, unknown> ? USocket : never, TConnectOptions extends TTransport extends keyof typeof transports ? (typeof transports)[TTransport] extends Transport<unknown, infer UConnectOptions> ? UConnectOptions : never : TTransport extends Transport<unknown, infer UConnectOptions> ? UConnectOptions : never>(socket: TSocket | null, options: SocketOptions<TTransport>): Socket<TConnectOptions>; | ||
export declare function makeSocket<TTransport extends keyof typeof transports | Transport<unknown>, TSocket extends TTransport extends keyof typeof transports ? (typeof transports)[TTransport] extends Transport<infer USocket> ? USocket : never : TTransport extends Transport<infer USocket> ? USocket : never>(target: TSocket, options: SocketOptions<TTransport>): Socket<TSocket>; |
@@ -1,13 +0,9 @@ | ||
export interface Transport<TSocket, TConnectOptions = never> { | ||
onReady(socket: TSocket, callback: () => void): void; | ||
onMessage(socket: TSocket, callback: (data: Uint8Array | string) => void): void; | ||
onError(socket: TSocket, callback: (reason: any) => void): void; | ||
onClose(socket: TSocket, callback: () => void): void; | ||
export interface Transport<TSocket> { | ||
onReady(socket: TSocket, callback: () => void): () => void; | ||
onMessage(socket: TSocket, callback: (data: Uint8Array | string) => void): () => void; | ||
onError(socket: TSocket, callback: (reason: any) => void): () => void; | ||
onClose(socket: TSocket, callback: () => void): () => void; | ||
isReady(socket: TSocket): boolean; | ||
send(socket: TSocket, data: Uint8Array | string): void; | ||
format(data: Uint8Array | string): Uint8Array | string; | ||
connect?(options: TConnectOptions): TSocket; | ||
destroy?(socket: TSocket): void; | ||
ref?(socket: TSocket): void; | ||
unref?(socket: TSocket): void; | ||
format?(data: Uint8Array | string): Uint8Array | string; | ||
} |
export { default as ipc } from './ipc'; | ||
export { default as ws } from './ws'; | ||
export { default as ee } from './ee'; |
/// <reference types="node" /> | ||
import { type Transport } from '../transport'; | ||
import { type Socket, type IpcSocketConnectOpts } from 'net'; | ||
export declare const transport: Transport<Socket, IpcSocketConnectOpts>; | ||
import { type Socket } from 'net'; | ||
export declare const transport: Transport<Socket>; | ||
export default transport; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
35203
18
461
0