1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.validateModels = exports.validateModel = exports.validateMessage = void 0;
|
6 | const validate_1 = require("../validate");
|
7 |
|
8 |
|
9 |
|
10 | const HEADER_FIELDS = ['username', 'version', 'session', 'msg_id', 'msg_type'];
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const 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' }
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 | function 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 |
|
47 |
|
48 | function 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 | }
|
57 | exports.validateMessage = validateMessage;
|
58 |
|
59 |
|
60 |
|
61 | function validateIOPubContent(msg) {
|
62 | if (msg.channel === 'iopub') {
|
63 | const fields = IOPUB_CONTENT_FIELDS[msg.header.msg_type];
|
64 |
|
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 |
|
81 |
|
82 | function validateModel(model) {
|
83 | (0, validate_1.validateProperty)(model, 'name', 'string');
|
84 | (0, validate_1.validateProperty)(model, 'id', 'string');
|
85 | }
|
86 | exports.validateModel = validateModel;
|
87 |
|
88 |
|
89 |
|
90 | function validateModels(models) {
|
91 | if (!Array.isArray(models)) {
|
92 | throw new Error('Invalid kernel list');
|
93 | }
|
94 | models.forEach(d => validateModel(d));
|
95 | }
|
96 | exports.validateModels = validateModels;
|
97 |
|
\ | No newline at end of file |