kibana/examples/v8_profiler_examples/server/lib/session.ts
Luke Elmers b6287708f6
Adds AGPL 3.0 license (#192025)
Updates files outside of x-pack to be triple-licensed under Elastic
License 2.0, AGPL 3.0, or SSPL 1.0.
2024-09-06 19:02:41 -06:00

80 lines
2.1 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { Logger } from '@kbn/core/server';
import { createDeferred } from './deferred';
let inspector: any = null;
try {
inspector = require('inspector');
} catch (err) {
// inspector will be null :-(
}
export async function createSession(logger: Logger): Promise<Session> {
logger.debug('creating session');
if (inspector == null) {
throw new Error('the inspector module is not available for this version of node');
}
let session = null;
try {
session = new inspector.Session();
} catch (err) {
throw new Error(`error creating inspector session: ${err.message}`);
}
try {
session.connect();
} catch (err) {
throw new Error(`error connecting inspector session: ${err.message}`);
}
return new Session(logger, session);
}
export class Session {
readonly logger: Logger;
private session: any;
constructor(logger: Logger, session: any) {
this.logger = logger;
this.session = session;
}
async destroy() {
this.session.disconnect();
this.session = null;
}
on(event: string, handler: any) {
this.session.on(event, handler);
}
async post(method: string, args?: any) {
this.logger.debug(`posting method ${method} ${JSON.stringify(args)}`);
if (this.session == null) {
throw new Error('session disconnected');
}
const deferred = createDeferred();
this.session.post(method, args, (err: any, response: any) => {
if (err) {
this.logger.debug(`error from method ${method}: ${err.message}`);
return deferred.reject(err);
}
deferred.resolve(response);
});
return deferred.promise;
}
}