UNPKG

3.22 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.validateModels = exports.validateModel = exports.validateMessage = void 0;
6const validate_1 = require("../validate");
7/**
8 * Required fields for `IKernelHeader`.
9 */
10const HEADER_FIELDS = ['username', 'version', 'session', 'msg_id', 'msg_type'];
11/**
12 * Required fields and types for contents of various types of `kernel.IMessage`
13 * messages on the iopub channel.
14 */
15const IOPUB_CONTENT_FIELDS = {
16 stream: { name: 'string', text: 'string' },
17 display_data: { data: 'object', metadata: 'object' },
18 execute_input: { code: 'string', execution_count: 'number' },
19 execute_result: {
20 execution_count: 'number',
21 data: 'object',
22 metadata: 'object'
23 },
24 error: { ename: 'string', evalue: 'string', traceback: 'object' },
25 status: {
26 execution_state: [
27 'string',
28 ['starting', 'idle', 'busy', 'restarting', 'dead']
29 ]
30 },
31 clear_output: { wait: 'boolean' },
32 comm_open: { comm_id: 'string', target_name: 'string', data: 'object' },
33 comm_msg: { comm_id: 'string', data: 'object' },
34 comm_close: { comm_id: 'string' },
35 shutdown_reply: { restart: 'boolean' } // Emitted by the IPython kernel.
36};
37/**
38 * Validate the header of a kernel message.
39 */
40function validateHeader(header) {
41 for (let i = 0; i < HEADER_FIELDS.length; i++) {
42 (0, validate_1.validateProperty)(header, HEADER_FIELDS[i], 'string');
43 }
44}
45/**
46 * Validate a kernel message object.
47 */
48function validateMessage(msg) {
49 (0, validate_1.validateProperty)(msg, 'metadata', 'object');
50 (0, validate_1.validateProperty)(msg, 'content', 'object');
51 (0, validate_1.validateProperty)(msg, 'channel', 'string');
52 validateHeader(msg.header);
53 if (msg.channel === 'iopub') {
54 validateIOPubContent(msg);
55 }
56}
57exports.validateMessage = validateMessage;
58/**
59 * Validate content an kernel message on the iopub channel.
60 */
61function validateIOPubContent(msg) {
62 if (msg.channel === 'iopub') {
63 const fields = IOPUB_CONTENT_FIELDS[msg.header.msg_type];
64 // Check for unknown message type.
65 if (fields === undefined) {
66 return;
67 }
68 const names = Object.keys(fields);
69 const content = msg.content;
70 for (let i = 0; i < names.length; i++) {
71 let args = fields[names[i]];
72 if (!Array.isArray(args)) {
73 args = [args];
74 }
75 (0, validate_1.validateProperty)(content, names[i], ...args);
76 }
77 }
78}
79/**
80 * Validate a `Kernel.IModel` object.
81 */
82function validateModel(model) {
83 (0, validate_1.validateProperty)(model, 'name', 'string');
84 (0, validate_1.validateProperty)(model, 'id', 'string');
85}
86exports.validateModel = validateModel;
87/**
88 * Validate an array of `IModel` objects.
89 */
90function validateModels(models) {
91 if (!Array.isArray(models)) {
92 throw new Error('Invalid kernel list');
93 }
94 models.forEach(d => validateModel(d));
95}
96exports.validateModels = validateModels;
97//# sourceMappingURL=validate.js.map
\No newline at end of file