-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathindex.ts
272 lines (249 loc) Β· 8.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { PayloadMessage } from "@nteract/types";
import { from, Observable, Subscriber } from "rxjs";
import { filter, map, mergeMap } from "rxjs/operators";
import { executeRequest, message } from "./messages";
import { ExecuteRequest, JupyterMessage, MessageType } from "./types";
export * from "./types";
export interface CreateMessageFields extends Partial<JupyterMessage> {
header?: never;
}
// TODO: Deprecate
export function createMessage<MT extends MessageType>(
msg_type: MT,
fields: CreateMessageFields = {}
): JupyterMessage<MT> {
return { ...message({ msg_type }), ...fields };
}
// TODO: Deprecate
export function createExecuteRequest(code: string = ""): ExecuteRequest {
return executeRequest(code, {});
}
/**
* creates a comm open message
* @param {string} comm_id uuid
* @param {string} target_name comm handler
* @param {any} data up to the target handler
* @param {string} target_module [Optional] used to select a module that is responsible for handling the target_name
* @return {jmp.Message} Message ready to send on the shell channel
*/
export function createCommOpenMessage(
comm_id: string,
target_name: string,
data: any = {},
target_module: string
) {
const msg = createMessage("comm_open", {
content: { comm_id, target_name, data }
});
if (target_module) {
msg.content.target_module = target_module;
}
return msg;
}
/**
* creates a comm message for sending to a kernel
* @param {string} comm_id unique identifier for the comm
* @param {Object} data any data to send for the comm
* @param {Uint8Array} buffers arbitrary binary data to send on the comm
* @return {jmp.Message} jupyter message for comm_msg
*/
export function createCommMessage(
comm_id: string,
data: any = {},
buffers: Uint8Array = new Uint8Array([])
) {
return createMessage("comm_msg", { content: { comm_id, data }, buffers });
}
/**
* creates a comm close message for sending to a kernel
* @param {Object} parent_header header from a parent jupyter message
* @param {string} comm_id unique identifier for the comm
* @param {Object} data any data to send for the comm
* @return {jmp.Message} jupyter message for comm_msg
*/
export function createCommCloseMessage(
parent_header: any,
comm_id: string,
data: any = {}
) {
return createMessage("comm_close", {
content: { comm_id, data },
parent_header
});
}
/**
* operator for getting all messages that declare their parent header as
* parentMessage's header.
*
* @param parentMessage The parent message whose children we should fetch
*
* @returns A function that takes an Observable of kernel messages and returns
* messages that are children of parentMessage.
*/
export function childOf(
parentMessage: JupyterMessage
): (source: Observable<JupyterMessage<MessageType, any>>) => any {
return (source: Observable<JupyterMessage>) => {
const parentMessageID: string = parentMessage.header.msg_id;
return Observable.create((subscriber: Subscriber<JupyterMessage>) =>
source.subscribe(
msg => {
// strictly speaking, in order for the message to be a child of the
// parent message, it has to both be a message and have a parent to
// begin with
if (!msg || !msg.parent_header || !msg.parent_header.msg_id) {
if (process.env.DEBUG === "true") {
console.warn("no parent_header.msg_id on message", msg);
}
return;
}
if (parentMessageID === msg.parent_header.msg_id) {
subscriber.next(msg);
}
},
// be sure to handle errors and completions as appropriate and
// send them along
err => subscriber.error(err),
() => subscriber.complete()
)
);
};
}
/**
* operator for getting all messages with the given comm id
*
* @param comm_id The comm id that we are filtering by
*
* @returns A function that takes an Observable of kernel messages and returns
* messages that have the given comm id
*/
export function withCommId(
comm_id: string
): (source: Observable<JupyterMessage<MessageType, any>>) => any {
return (source: Observable<JupyterMessage>) => {
return Observable.create((subscriber: Subscriber<JupyterMessage>) =>
source.subscribe(
msg => {
if (msg && msg.content && msg.content.comm_id === comm_id) {
subscriber.next(msg);
}
},
// be sure to handle errors and completions as appropriate and
// send them along
err => subscriber.error(err),
() => subscriber.complete()
)
);
};
}
/**
* ofMessageType is an Rx Operator that filters on msg.header.msg_type
* being one of messageTypes.
*
* @param messageTypes The message types to filter on
*
* @returns An Observable containing only messages of the specified types
*/
export const ofMessageType = <T extends MessageType>(
...messageTypes: Array<T | [T]>
): ((source: Observable<JupyterMessage>) => Observable<JupyterMessage<T>>) => {
// Switch to the splat mode
if (messageTypes.length === 1 && Array.isArray(messageTypes[0])) {
return ofMessageType(...messageTypes[0]);
}
return (source: Observable<JupyterMessage>) =>
Observable.create((subscriber: Subscriber<JupyterMessage>) =>
source.subscribe(
msg => {
if (!msg.header || !msg.header.msg_type) {
subscriber.error(new Error("no header.msg_type on message"));
return;
}
if (messageTypes.includes(msg.header.msg_type as any)) {
subscriber.next(msg);
}
},
// be sure to handle errors and completions as appropriate and
// send them along
err => subscriber.error(err),
() => subscriber.complete()
)
);
};
/**
* Create an object that adheres to the jupyter notebook specification.
* http://jupyter-client.readthedocs.io/en/latest/messaging.html
*
* @param msg Message that has content which can be converted to nbformat
*
* @returns Message with the associated output type
*/
export function convertOutputMessageToNotebookFormat(msg: JupyterMessage) {
return {
...msg.content,
output_type: msg.header.msg_type
};
}
/**
* Convert raw Jupyter messages that are output messages into nbformat style
* outputs
*
* > o$ = iopub$.pipe(
* childOf(originalMessage),
* outputs()
* )
*/
export const outputs = () => (source: Observable<JupyterMessage>) =>
source.pipe(
ofMessageType("execute_result", "display_data", "stream", "error"),
map(convertOutputMessageToNotebookFormat)
);
/**
* Get all messages for updating a display output.
*/
export const updatedOutputs = () => (source: Observable<JupyterMessage>) =>
source.pipe(
ofMessageType("update_display_data"),
map(msg => ({ ...msg.content, output_type: "display_data" }))
);
/**
* Get all the payload message content from an observable of jupyter messages
*
* > p$ = shell$.pipe(
* childOf(originalMessage),
* payloads()
* )
*/
export const payloads = () => (
source: Observable<JupyterMessage>
): Observable<PayloadMessage> =>
source.pipe(
ofMessageType("execute_reply"),
map(entry => entry.content.payload),
filter(p => !!p),
mergeMap((p: Observable<PayloadMessage>) => from(p))
);
/**
* Get all the execution counts from an observable of jupyter messages
*/
export const executionCounts = () => (source: Observable<JupyterMessage>) =>
source.pipe(
ofMessageType("execute_input", "execute_reply"),
map(entry => entry.content.execution_count)
);
/**
* Get all statuses of all running kernels.
*/
export const kernelStatuses = () => (source: Observable<JupyterMessage>) =>
source.pipe(
ofMessageType("status"),
map(entry => entry.content.execution_state)
);
export const inputRequests = () => (source: Observable<JupyterMessage>) =>
source.pipe(
ofMessageType("input_request"),
map(entry => entry.content)
);
export * from "./messages";
import { encode, decode } from "./wire-protocol";
export const wireProtocol = { encode, decode };