Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jupyter-js-services

Package Overview
Dependencies
Maintainers
2
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jupyter-js-services - npm Package Compare versions

Comparing version 0.5.5 to 0.6.0

36

lib/isession.d.ts
import { IDisposable } from 'phosphor-disposable';
import { ISignal } from 'phosphor-signaling';
import { IKernel, IKernelId, IKernelSpecIds, KernelStatus } from './ikernel';
import { IKernel, IKernelId, IKernelSpecIds, IKernelMessage, KernelStatus } from './ikernel';
import { IAjaxSettings } from 'jupyter-js-utils';

@@ -81,6 +81,14 @@ /**

/**
* A signal emitted when the session dies.
* A signal emitted when the session is shut down.
*/
sessionDied: ISignal<INotebookSession, void>;
/**
* A signal emitted when the session status changes.
*/
statusChanged: ISignal<INotebookSession, KernelStatus>;
/**
* A signal emitted for unhandled kernel message.
*/
unhandledMessage: ISignal<INotebookSession, IKernelMessage>;
/**
* Unique id of the session.

@@ -103,3 +111,5 @@ *

* #### Notes
* This is a read-only property.
* This is a read-only property, and can be altered by [changeKernel].
* Use the [statusChanged] and [unhandledMessage] signals on the session
* instead of the ones on the kernel.
*/

@@ -115,2 +125,6 @@ kernel: IKernel;

/**
* Optional default settings for ajax requests, if applicable.
*/
ajaxSettings?: IAjaxSettings;
/**
* Rename or move a notebook.

@@ -126,2 +140,14 @@ *

/**
* Change the kernel.
*
* @params name - The name of the new kernel.
*
* @returns - A promise that resolves with the new kernel.
*
* #### Notes
* This shuts down the existing kernel and creates a new kernel,
* keeping the existing session ID and notebook path.
*/
changeKernel(name: string): Promise<IKernel>;
/**
* Kill the kernel and shutdown the session.

@@ -134,6 +160,2 @@ *

shutdown(ajaxSettings?: IAjaxSettings): Promise<void>;
/**
* Optional default settings for ajax requests, if applicable.
*/
ajaxSettings?: IAjaxSettings;
}

285

lib/session.js

@@ -6,3 +6,2 @@ // Copyright (c) Jupyter Development Team.

var phosphor_signaling_1 = require('phosphor-signaling');
var ikernel_1 = require('./ikernel');
var kernel_1 = require('./kernel');

@@ -107,3 +106,3 @@ var validate = require('./validate');

return success.data;
}, onSessionError);
}, Private.onSessionError);
}

@@ -124,22 +123,5 @@ exports.listRunningSessions = listRunningSessions;

function startNewSession(options) {
var baseUrl = options.baseUrl || utils.getBaseUrl();
var url = utils.urlPathJoin(baseUrl, SESSION_SERVICE_URL);
var model = {
kernel: { name: options.kernelName },
notebook: { path: options.notebookPath }
};
var ajaxSettings = utils.copy(options.ajaxSettings) || {};
ajaxSettings.method = 'POST';
ajaxSettings.dataType = 'json';
ajaxSettings.data = JSON.stringify(model);
ajaxSettings.contentType = 'application/json';
ajaxSettings.cache = false;
return utils.ajaxRequest(url, ajaxSettings).then(function (success) {
if (success.xhr.status !== 201) {
throw Error('Invalid Status: ' + success.xhr.status);
}
var sessionId = success.data;
validate.validateSessionId(success.data);
return createSession(sessionId, options);
}, onSessionError);
return Private.startSession(options).then(function (sessionId) {
return Private.createSession(sessionId, options);
});
}

@@ -164,3 +146,3 @@ exports.startNewSession = startNewSession;

function connectToSession(id, options) {
var session = runningSessions.get(id);
var session = Private.runningSessions.get(id);
if (session) {

@@ -177,3 +159,3 @@ return Promise.resolve(session);

}
return createSession(sessionIds[0], options);
return Private.createSession(sessionIds[0], options);
});

@@ -183,30 +165,2 @@ }

/**
* Create a Promise for a NotebookSession object.
*
* Fulfilled when the NotebookSession is Starting, or rejected if Dead.
*/
function createSession(sessionId, options) {
var baseUrl = options.baseUrl || utils.getBaseUrl();
options.notebookPath = sessionId.notebook.path;
var kernelOptions = {
name: sessionId.kernel.name,
baseUrl: options.baseUrl,
wsUrl: options.wsUrl,
username: options.username,
clientId: options.clientId,
ajaxSettings: options.ajaxSettings
};
return kernel_1.connectToKernel(sessionId.kernel.id, kernelOptions).then(function (kernel) {
var session = new NotebookSession(options, sessionId.id, kernel);
runningSessions.set(session.id, session);
return session;
}).catch(function (error) {
return typedThrow('Session failed to start: ' + error.message);
});
}
/**
* A module private store for running sessions.
*/
var runningSessions = new Map();
/**
* Session object for accessing the session REST api. The session

@@ -226,3 +180,3 @@ * should be used to start kernels and then shut them down -- for

this._url = '';
this._isDead = false;
this._options = null;
this.ajaxSettings = options.ajaxSettings || {};

@@ -233,3 +187,5 @@ this._id = id;

this._url = utils.urlPathJoin(options.baseUrl, SESSION_SERVICE_URL, this._id);
this._kernel.statusChanged.connect(this._kernelStatusChanged, this);
this._kernel.statusChanged.connect(this.onKernelStatus, this);
this._kernel.unhandledMessage.connect(this.onUnhandledMessage, this);
this._options = utils.copy(options);
}

@@ -241,3 +197,3 @@ Object.defineProperty(NotebookSession.prototype, "sessionDied", {

get: function () {
return NotebookSession.sessionDiedSignal.bind(this);
return Private.sessionDiedSignal.bind(this);
},

@@ -247,2 +203,22 @@ enumerable: true,

});
Object.defineProperty(NotebookSession.prototype, "statusChanged", {
/**
* A signal emitted when the kernel status changes.
*/
get: function () {
return Private.statusChangedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(NotebookSession.prototype, "unhandledMessage", {
/**
* A signal emitted for an unhandled kernel message.
*/
get: function () {
return Private.unhandledMessageSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(NotebookSession.prototype, "id", {

@@ -266,3 +242,5 @@ /**

* #### Notes
* This is a read-only property.
* This is a read-only property, and can be altered by [changeKernel].
* Use the [statusChanged] and [unhandledMessage] signals on the session
* instead of the ones on the kernel.
*/

@@ -275,5 +253,5 @@ get: function () {

});
Object.defineProperty(NotebookSession.prototype, "status", {
Object.defineProperty(NotebookSession.prototype, "notebookPath", {
/**
* The current status of the session, and is a delegate to the kernel status.
* Get the notebook path.
*

@@ -284,3 +262,3 @@ * #### Notes

get: function () {
return this._kernel.status;
return this._notebookPath;
},

@@ -290,11 +268,11 @@ enumerable: true,

});
Object.defineProperty(NotebookSession.prototype, "notebookPath", {
Object.defineProperty(NotebookSession.prototype, "status", {
/**
* Get the notebook path.
* The current status of the session.
*
* #### Notes
* This is a read-only property.
* This is a read-only property, and is a delegate to the kernel status.
*/
get: function () {
return this._notebookPath;
return this._kernel.status;
},

@@ -328,3 +306,3 @@ enumerable: true,

get: function () {
return (this._kernel === null);
return this._options === null;
},

@@ -338,6 +316,7 @@ enumerable: true,

NotebookSession.prototype.dispose = function () {
this._kernel.dispose();
this._options = null;
this._kernel = null;
this._isDead = true;
phosphor_signaling_1.clearSignalData(this);
runningSessions.delete(this._id);
Private.runningSessions.delete(this._id);
};

@@ -355,4 +334,4 @@ /**

var _this = this;
if (this._isDead) {
return Promise.reject(new Error('Session is dead'));
if (this.isDisposed) {
return Promise.reject(new Error('Session is disposed'));
}

@@ -376,18 +355,61 @@ var model = {

_this._notebookPath = data.notebook.path;
}, onSessionError);
}, Private.onSessionError);
};
/**
* Change the kernel.
*
* @params name - The name of the new kernel.
*
* @returns - A promise that resolves with the new kernel.
*
* #### Notes
* This shuts down the existing kernel and creates a new kernel,
* keeping the existing session ID and notebook path.
*/
NotebookSession.prototype.changeKernel = function (name) {
var _this = this;
if (this.isDisposed) {
return Promise.reject(new Error('Session is disposed'));
}
var options = utils.copy(this._options);
options.ajaxSettings = this.ajaxSettings;
options.kernelName = name;
options.notebookPath = this.notebookPath;
return this.delete().then(function () {
return Private.startSession(options);
}).then(function (sessionId) {
return Private.createKernel(sessionId, options);
}).then(function (kernel) {
kernel.statusChanged.connect(_this.onKernelStatus, _this);
kernel.unhandledMessage.connect(_this.onUnhandledMessage, _this);
_this._kernel = kernel;
return kernel;
});
};
/**
* Kill the kernel and shutdown the session.
*
* @returns - The promise fulfilled on a valid response from the server.
*
* #### Notes
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/jupyter-js-services/master/rest_api.yaml#!/sessions), and validates the response.
*
* The promise is fulfilled on a valid response and rejected otherwise.
* Emits a [sessionDied] signal on success.
*/
NotebookSession.prototype.shutdown = function () {
var _this = this;
if (this._isDead) {
return Promise.reject(new Error('Session is dead'));
if (this.isDisposed) {
return Promise.reject(new Error('Session is disposed'));
}
this._isDead = true;
return this.delete().then(function () {
_this.sessionDied.emit(void 0);
});
};
/**
* Shut down the existing session.
*
* #### Notes
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/jupyter-js-services/master/rest_api.yaml#!/sessions), and validates the response.
*/
NotebookSession.prototype.delete = function () {
var _this = this;
var ajaxSettings = this.ajaxSettings;

@@ -401,35 +423,114 @@ ajaxSettings.method = 'DELETE';

}
_this.sessionDied.emit(void 0);
_this.kernel.dispose();
_this._kernel.dispose();
_this._kernel = null;
_this._options = null;
}, function (rejected) {
_this._isDead = false;
if (rejected.xhr.status === 410) {
throw Error('The kernel was deleted but the session was not');
}
onSessionError(rejected);
Private.onSessionError(rejected);
});
};
/**
* React to changes in the Kernel status.
* Handle to changes in the Kernel status.
*/
NotebookSession.prototype._kernelStatusChanged = function (sender, state) {
if (state == ikernel_1.KernelStatus.Dead) {
this.shutdown();
}
NotebookSession.prototype.onKernelStatus = function (sender, state) {
this.statusChanged.emit(state);
};
/**
* A signal emitted when the session dies.
*
* **See also:** [[sessionDied]]
* Handle unhandled kernel messages.
*/
NotebookSession.sessionDiedSignal = new phosphor_signaling_1.Signal();
NotebookSession.prototype.onUnhandledMessage = function (sender, msg) {
this.unhandledMessage.emit(msg);
};
return NotebookSession;
})();
/**
* Handle an error on a session Ajax call.
* A namespace for notebook session private data.
*/
function onSessionError(error) {
console.error("API request failed (" + error.statusText + "): ");
throw Error(error.statusText);
}
var Private;
(function (Private) {
/**
* A signal emitted when the session is shut down.
*/
Private.sessionDiedSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted when the session kernel status changes.
*/
Private.statusChangedSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted for an unhandled kernel message.
*/
Private.unhandledMessageSignal = new phosphor_signaling_1.Signal();
/**
* The running sessions.
*/
Private.runningSessions = new Map();
/**
* Create a new session, or return an existing session if a session if
* the notebook path already exists
*/
function startSession(options) {
var baseUrl = options.baseUrl || utils.getBaseUrl();
var url = utils.urlPathJoin(baseUrl, SESSION_SERVICE_URL);
var model = {
kernel: { name: options.kernelName },
notebook: { path: options.notebookPath }
};
var ajaxSettings = utils.copy(options.ajaxSettings) || {};
ajaxSettings.method = 'POST';
ajaxSettings.dataType = 'json';
ajaxSettings.data = JSON.stringify(model);
ajaxSettings.contentType = 'application/json';
ajaxSettings.cache = false;
return utils.ajaxRequest(url, ajaxSettings).then(function (success) {
if (success.xhr.status !== 201) {
throw Error('Invalid Status: ' + success.xhr.status);
}
validate.validateSessionId(success.data);
return success.data;
}, onSessionError);
}
Private.startSession = startSession;
/**
* Create a Promise for a kernel object given a sessionId and options.
*/
function createKernel(sessionId, options) {
var baseUrl = options.baseUrl || utils.getBaseUrl();
options.notebookPath = sessionId.notebook.path;
var kernelOptions = {
name: sessionId.kernel.name,
baseUrl: options.baseUrl,
wsUrl: options.wsUrl,
username: options.username,
clientId: options.clientId,
ajaxSettings: options.ajaxSettings
};
return kernel_1.connectToKernel(sessionId.kernel.id, kernelOptions);
}
Private.createKernel = createKernel;
/**
* Create a NotebookSession object.
*
* @returns - A promise that resolves with a started session.
*/
function createSession(sessionId, options) {
return createKernel(sessionId, options).then(function (kernel) {
var session = new NotebookSession(options, sessionId.id, kernel);
Private.runningSessions.set(session.id, session);
return session;
}).catch(function (error) {
return typedThrow('Session failed to start: ' + error.message);
});
}
Private.createSession = createSession;
/**
* Handle an error on a session Ajax call.
*/
function onSessionError(error) {
console.error("API request failed (" + error.statusText + "): ");
throw Error(error.statusText);
}
Private.onSessionError = onSessionError;
})(Private || (Private = {}));
/**

@@ -436,0 +537,0 @@ * Throw a typed error.

{
"name": "jupyter-js-services",
"version": "0.5.5",
"version": "0.6.0",
"description": "Client APIs for the Jupyter services REST APIs",

@@ -34,6 +34,6 @@ "main": "lib/index.js",

"prepublish": "npm run build",
"test:coverage": "cd test && istanbul cover _mocha -- build/index.js",
"test:coverage": "cd test && istanbul cover _mocha -- build/test*.js",
"test:integration": "cd test && python integration_test.py",
"test:debug": "mocha test/build/index.js --debug-brk",
"test": "mocha test/build/index.js"
"test:debug": "mocha test/build/test*.js --debug-brk",
"test": "mocha test/build/test*.js"
},

@@ -40,0 +40,0 @@ "repository": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc