Skip to content

Commit b25264f

Browse files
committed
Cleanup ws handling types and docs
1 parent d3ebaad commit b25264f

File tree

1 file changed

+98
-56
lines changed

1 file changed

+98
-56
lines changed

js/src/messaging.ts

Lines changed: 98 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import { ProcessMessage } from 'e2b'
44
/**
55
* Represents an error that occurred during the execution of a cell.
66
* The error contains the name of the error, the value of the error, and the traceback.
7-
*
8-
* @property {string} name - Name of the error.
9-
* @property {string} value - Value of the error.
10-
* @property {string} traceback - The traceback of the error.
117
*/
128
export class ExecutionError {
13-
name: string
14-
value: string
15-
tracebackRaw: string[]
16-
17-
constructor(name: string, value: string, tracebackRaw: string[]) {
18-
this.name = name
19-
this.value = value
20-
this.tracebackRaw = tracebackRaw
21-
}
9+
constructor(
10+
/**
11+
* Name of the error.
12+
**/
13+
public name: string,
14+
/**
15+
* Value of the error.
16+
**/
17+
public value: string,
18+
/**
19+
* The raw traceback of the error.
20+
**/
21+
public tracebackRaw: string[],
22+
) { }
2223

23-
public traceback(): string {
24+
/**
25+
* Returns the traceback of the error as a string.
26+
*/
27+
traceback(): string {
2428
return this.tracebackRaw.join('\n')
2529
}
2630
}
@@ -45,40 +49,56 @@ export type RawData = {
4549
* The result can contain multiple types of data, such as text, images, plots, etc. Each type of data is represented
4650
* as a string, and the result can contain multiple types of data. The text representation is always present, and
4751
* the other representations are optional.
48-
*
49-
* @property {string} text - Text representation of the data. Always present.
50-
* @property {string} [html] - HTML representation of the data.
51-
* @property {string} [markdown] - Markdown representation of the data.
52-
* @property {string} [svg] - SVG representation of the data.
53-
* @property {string} [png] - PNG representation of the data.
54-
* @property {string} [jpeg] - JPEG representation of the data.
55-
* @property {string} [pdf] - PDF representation of the data.
56-
* @property {string} [latex] - LaTeX representation of the data.
57-
* @property {string} [json] - JSON representation of the data.
58-
* @property {string} [javascript] - JavaScript representation of the data.
59-
* @property {any} [extra] - Extra data that can be included. Not part of the standard types.
60-
* @property {boolean} isMainResult - Whether this data is the main result of the cell. There can be multiple display calls in a cell.
61-
* @property {RawData} raw - Dictionary that maps MIME types to their corresponding string representations of the data.
6252
*/
63-
6453
export class Data {
54+
/**
55+
* Text representation of the data. Always present.
56+
*/
6557
readonly text: string
58+
/**
59+
* HTML representation of the data.
60+
*/
6661
readonly html?: string
62+
/**
63+
* Markdown representation of the data.
64+
*/
6765
readonly markdown?: string
66+
/**
67+
* SVG representation of the data.
68+
*/
6869
readonly svg?: string
70+
/**
71+
* PNG representation of the data.
72+
*/
6973
readonly png?: string
74+
/**
75+
* JPEG representation of the data.
76+
*/
7077
readonly jpeg?: string
78+
/**
79+
* PDF representation of the data.
80+
*/
7181
readonly pdf?: string
82+
/**
83+
* LaTeX representation of the data.
84+
*/
7285
readonly latex?: string
86+
/**
87+
* JSON representation of the data.
88+
*/
7389
readonly json?: string
90+
/**
91+
* JavaScript representation of the data.
92+
*/
7493
readonly javascript?: string
94+
/**
95+
* Extra data that can be included. Not part of the standard types.
96+
*/
7597
readonly extra?: any
7698

77-
isMainResult: boolean
99+
readonly raw: RawData
78100

79-
raw: RawData
80-
81-
constructor(data: RawData, isMainResult: boolean) {
101+
constructor(data: RawData, public readonly isMainResult: boolean) {
82102
this.text = data['text/plain']
83103
this.html = data['text/html']
84104
this.markdown = data['text/markdown']
@@ -93,6 +113,7 @@ export class Data {
93113
this.raw = data
94114

95115
this.extra = {}
116+
// TODO: For in check
96117
for (const key in data) {
97118
if (![
98119
'text/plain',
@@ -114,28 +135,41 @@ export class Data {
114135

115136
/**
116137
* Data printed to stdout and stderr during execution, usually by print statements, logs, warnings, subprocesses, etc.
117-
*
118-
* @property {string[]} stdout - List of strings printed to stdout by prints, subprocesses, etc.
119-
* @property {string[]} stderr - List of strings printed to stderr by prints, subprocesses, etc.
120138
*/
121139
export type Logs = {
140+
/**
141+
* List of strings printed to stdout by prints, subprocesses, etc.
142+
*/
122143
stdout: string[]
144+
/**
145+
* List of strings printed to stderr by prints, subprocesses, etc.
146+
*/
123147
stderr: string[]
124148
}
149+
125150
/**
126151
* Represents the result of a cell execution.
127-
* @property {Data} data - List of result of the cell (interactively interpreted last line), display calls, e.g. matplotlib plots.
128-
* @property {Logs} logs - "Logs printed to stdout and stderr during execution."
129-
* @property {ExecutionError | null} error - An Error object if an error occurred, null otherwise.
130152
*/
131153
export class Result {
132154
constructor(
155+
/**
156+
* List of result of the cell (interactively interpreted last line), display calls, e.g. matplotlib plots.
157+
*/
133158
public data: Data[],
159+
/**
160+
* Logs printed to stdout and stderr during execution.
161+
*/
134162
public logs: Logs,
135-
public error?: ExecutionError
163+
/**
164+
* An Error object if an error occurred, null otherwise.
165+
*/
166+
public error?: ExecutionError,
136167
) { }
137168

138-
public get text(): string | undefined {
169+
/**
170+
* Returns the text representation of the main result of the cell.
171+
*/
172+
get text(): string | undefined {
139173
for (const data of this.data) {
140174
if (data.isMainResult) {
141175
return data.text
@@ -149,10 +183,10 @@ export class Result {
149183
* It's an internal class used by JupyterKernelWebSocket.
150184
*/
151185
class CellExecution {
152-
public result: Result
153-
public onStdout?: (out: ProcessMessage) => Promise<void> | void
154-
public onStderr?: (out: ProcessMessage) => Promise<void> | void
155-
public inputAccepted: boolean = false
186+
result: Result
187+
onStdout?: (out: ProcessMessage) => Promise<void> | void
188+
onStderr?: (out: ProcessMessage) => Promise<void> | void
189+
inputAccepted: boolean = false
156190

157191
constructor(
158192
onStdout?: (out: ProcessMessage) => Promise<void> | void,
@@ -170,8 +204,19 @@ interface Cells {
170204

171205
export class JupyterKernelWebSocket {
172206
// native websocket
173-
private ws: IWebSocket
174-
private readonly url: string
207+
private _ws?: IWebSocket
208+
209+
private set ws(ws: IWebSocket) {
210+
this._ws = ws
211+
}
212+
213+
private get ws() {
214+
if (!this._ws) {
215+
throw new Error('WebSocket is not connected.')
216+
}
217+
return this._ws
218+
}
219+
175220
private idAwaiter: {
176221
[id: string]: (data?: any) => void
177222
} = {}
@@ -183,17 +228,14 @@ export class JupyterKernelWebSocket {
183228
* Does not start WebSocket connection!
184229
* You need to call connect() method first.
185230
*/
186-
public constructor(url: string) {
187-
this.ws = undefined as any
188-
this.url = url
189-
}
231+
constructor(private readonly url: string) { }
190232

191233
// public
192234
/**
193235
* Starts WebSocket connection.
194236
*/
195-
public connect() {
196-
this.ws = new IWebSocket(this.url)
237+
connect() {
238+
this._ws = new IWebSocket(this.url)
197239
return this.listen()
198240
}
199241

@@ -334,8 +376,8 @@ export class JupyterKernelWebSocket {
334376
*/
335377
private listen() {
336378
return new Promise((resolve, reject) => {
337-
// @ts-ignore
338-
this.ws.onopen = (e: IWebSocket.OpenEvent) => {
379+
380+
this.ws.onopen = (e: unknown) => {
339381
resolve(e)
340382
}
341383

@@ -382,7 +424,7 @@ export class JupyterKernelWebSocket {
382424
/**
383425
* Closes WebSocket connection.
384426
*/
385-
public close() {
427+
close() {
386428
this.ws.close()
387429
}
388430
}

0 commit comments

Comments
 (0)