@difizen/libro-kernel 0.0.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/es/basemanager.d.ts +94 -0
  4. package/es/basemanager.d.ts.map +1 -0
  5. package/es/basemanager.js +110 -0
  6. package/es/contents/contents-drive.d.ts +189 -0
  7. package/es/contents/contents-drive.d.ts.map +1 -0
  8. package/es/contents/contents-drive.js +792 -0
  9. package/es/contents/contents-manager.d.ts +229 -0
  10. package/es/contents/contents-manager.d.ts.map +1 -0
  11. package/es/contents/contents-manager.js +551 -0
  12. package/es/contents/contents-module.d.ts +3 -0
  13. package/es/contents/contents-module.d.ts.map +1 -0
  14. package/es/contents/contents-module.js +4 -0
  15. package/es/contents/contents-protocol.d.ts +487 -0
  16. package/es/contents/contents-protocol.d.ts.map +1 -0
  17. package/es/contents/contents-protocol.js +1 -0
  18. package/es/contents/index.d.ts +6 -0
  19. package/es/contents/index.d.ts.map +1 -0
  20. package/es/contents/index.js +5 -0
  21. package/es/contents/validate.d.ts +10 -0
  22. package/es/contents/validate.d.ts.map +1 -0
  23. package/es/contents/validate.js +22 -0
  24. package/es/index.d.ts +10 -0
  25. package/es/index.d.ts.map +1 -0
  26. package/es/index.js +9 -0
  27. package/es/index.less +0 -0
  28. package/es/kernel/comm.d.ts +92 -0
  29. package/es/kernel/comm.d.ts.map +1 -0
  30. package/es/kernel/comm.js +216 -0
  31. package/es/kernel/future.d.ts +178 -0
  32. package/es/kernel/future.d.ts.map +1 -0
  33. package/es/kernel/future.js +587 -0
  34. package/es/kernel/index.d.ts +8 -0
  35. package/es/kernel/index.d.ts.map +1 -0
  36. package/es/kernel/index.js +8 -0
  37. package/es/kernel/kernel-connection.d.ts +550 -0
  38. package/es/kernel/kernel-connection.d.ts.map +1 -0
  39. package/es/kernel/kernel-connection.js +1957 -0
  40. package/es/kernel/kernel-module.d.ts +3 -0
  41. package/es/kernel/kernel-module.d.ts.map +1 -0
  42. package/es/kernel/kernel-module.js +32 -0
  43. package/es/kernel/libro-kernel-manager.d.ts +69 -0
  44. package/es/kernel/libro-kernel-manager.d.ts.map +1 -0
  45. package/es/kernel/libro-kernel-manager.js +349 -0
  46. package/es/kernel/libro-kernel-protocol.d.ts +675 -0
  47. package/es/kernel/libro-kernel-protocol.d.ts.map +1 -0
  48. package/es/kernel/libro-kernel-protocol.js +60 -0
  49. package/es/kernel/libro-kernel-utils.d.ts +95 -0
  50. package/es/kernel/libro-kernel-utils.d.ts.map +1 -0
  51. package/es/kernel/libro-kernel-utils.js +130 -0
  52. package/es/kernel/libro-kernel.d.ts +14 -0
  53. package/es/kernel/libro-kernel.d.ts.map +1 -0
  54. package/es/kernel/libro-kernel.js +54 -0
  55. package/es/kernel/messages.d.ts +845 -0
  56. package/es/kernel/messages.d.ts.map +1 -0
  57. package/es/kernel/messages.js +457 -0
  58. package/es/kernel/restapi.d.ts +78 -0
  59. package/es/kernel/restapi.d.ts.map +1 -0
  60. package/es/kernel/restapi.js +367 -0
  61. package/es/kernel/serialize.d.ts +10 -0
  62. package/es/kernel/serialize.d.ts.map +1 -0
  63. package/es/kernel/serialize.js +214 -0
  64. package/es/kernel/validate.d.ts +15 -0
  65. package/es/kernel/validate.d.ts.map +1 -0
  66. package/es/kernel/validate.js +125 -0
  67. package/es/kernelspec/index.d.ts +5 -0
  68. package/es/kernelspec/index.d.ts.map +1 -0
  69. package/es/kernelspec/index.js +4 -0
  70. package/es/kernelspec/kernelspec-module.d.ts +3 -0
  71. package/es/kernelspec/kernelspec-module.d.ts.map +1 -0
  72. package/es/kernelspec/kernelspec-module.js +4 -0
  73. package/es/kernelspec/kernelspec.d.ts +33 -0
  74. package/es/kernelspec/kernelspec.d.ts.map +1 -0
  75. package/es/kernelspec/kernelspec.js +1 -0
  76. package/es/kernelspec/manager.d.ts +81 -0
  77. package/es/kernelspec/manager.d.ts.map +1 -0
  78. package/es/kernelspec/manager.js +248 -0
  79. package/es/kernelspec/restapi.d.ts +71 -0
  80. package/es/kernelspec/restapi.d.ts.map +1 -0
  81. package/es/kernelspec/restapi.js +107 -0
  82. package/es/kernelspec/validate.d.ts +10 -0
  83. package/es/kernelspec/validate.d.ts.map +1 -0
  84. package/es/kernelspec/validate.js +69 -0
  85. package/es/libro-kernel-connection-manager.d.ts +19 -0
  86. package/es/libro-kernel-connection-manager.d.ts.map +1 -0
  87. package/es/libro-kernel-connection-manager.js +142 -0
  88. package/es/module.d.ts +3 -0
  89. package/es/module.d.ts.map +1 -0
  90. package/es/module.js +9 -0
  91. package/es/page-config.d.ts +36 -0
  92. package/es/page-config.d.ts.map +1 -0
  93. package/es/page-config.js +129 -0
  94. package/es/protocol.d.ts +13 -0
  95. package/es/protocol.d.ts.map +1 -0
  96. package/es/protocol.js +8 -0
  97. package/es/server/connection-error.d.ts +36 -0
  98. package/es/server/connection-error.d.ts.map +1 -0
  99. package/es/server/connection-error.js +109 -0
  100. package/es/server/index.d.ts +6 -0
  101. package/es/server/index.d.ts.map +1 -0
  102. package/es/server/index.js +5 -0
  103. package/es/server/server-connection-protocol.d.ts +49 -0
  104. package/es/server/server-connection-protocol.d.ts.map +1 -0
  105. package/es/server/server-connection-protocol.js +0 -0
  106. package/es/server/server-connection.d.ts +25 -0
  107. package/es/server/server-connection.d.ts.map +1 -0
  108. package/es/server/server-connection.js +159 -0
  109. package/es/server/server-manager.d.ts +22 -0
  110. package/es/server/server-manager.d.ts.map +1 -0
  111. package/es/server/server-manager.js +163 -0
  112. package/es/server/server-module.d.ts +3 -0
  113. package/es/server/server-module.d.ts.map +1 -0
  114. package/es/server/server-module.js +4 -0
  115. package/es/session/index.d.ts +5 -0
  116. package/es/session/index.d.ts.map +1 -0
  117. package/es/session/index.js +4 -0
  118. package/es/session/libro-session-manager.d.ts +71 -0
  119. package/es/session/libro-session-manager.d.ts.map +1 -0
  120. package/es/session/libro-session-manager.js +539 -0
  121. package/es/session/libro-session-protocol.d.ts +50 -0
  122. package/es/session/libro-session-protocol.d.ts.map +1 -0
  123. package/es/session/libro-session-protocol.js +21 -0
  124. package/es/session/libro-session.d.ts +12 -0
  125. package/es/session/libro-session.d.ts.map +1 -0
  126. package/es/session/libro-session.js +19 -0
  127. package/es/session/restapi.d.ts +28 -0
  128. package/es/session/restapi.d.ts.map +1 -0
  129. package/es/session/restapi.js +214 -0
  130. package/es/session/session-module.d.ts +3 -0
  131. package/es/session/session-module.d.ts.map +1 -0
  132. package/es/session/session-module.js +18 -0
  133. package/es/session/validate.d.ts +14 -0
  134. package/es/session/validate.d.ts.map +1 -0
  135. package/es/session/validate.js +37 -0
  136. package/es/utils.d.ts +4 -0
  137. package/es/utils.d.ts.map +1 -0
  138. package/es/utils.js +29 -0
  139. package/es/validate-property.d.ts +2 -0
  140. package/es/validate-property.d.ts.map +1 -0
  141. package/es/validate-property.js +35 -0
  142. package/package.json +62 -0
  143. package/src/basemanager.ts +133 -0
  144. package/src/contents/contents-drive.ts +495 -0
  145. package/src/contents/contents-manager.ts +465 -0
  146. package/src/contents/contents-module.ts +6 -0
  147. package/src/contents/contents-protocol.ts +604 -0
  148. package/src/contents/index.ts +5 -0
  149. package/src/contents/validate.ts +29 -0
  150. package/src/index.tsx +9 -0
  151. package/src/kernel/comm.ts +220 -0
  152. package/src/kernel/future.ts +474 -0
  153. package/src/kernel/index.ts +7 -0
  154. package/src/kernel/kernel-connection.ts +1770 -0
  155. package/src/kernel/kernel-module.ts +50 -0
  156. package/src/kernel/libro-kernel-manager.ts +199 -0
  157. package/src/kernel/libro-kernel-protocol.ts +858 -0
  158. package/src/kernel/libro-kernel-utils.ts +152 -0
  159. package/src/kernel/libro-kernel.ts +39 -0
  160. package/src/kernel/messages.ts +1104 -0
  161. package/src/kernel/restapi.ts +183 -0
  162. package/src/kernel/serialize.ts +262 -0
  163. package/src/kernel/validate.ts +101 -0
  164. package/src/kernelspec/index.ts +5 -0
  165. package/src/kernelspec/kernelspec-module.ts +9 -0
  166. package/src/kernelspec/kernelspec.ts +37 -0
  167. package/src/kernelspec/manager.ts +173 -0
  168. package/src/kernelspec/restapi.ts +104 -0
  169. package/src/kernelspec/validate.ts +80 -0
  170. package/src/libro-kernel-connection-manager.ts +73 -0
  171. package/src/module.ts +19 -0
  172. package/src/page-config.ts +106 -0
  173. package/src/protocol.ts +24 -0
  174. package/src/server/connection-error.ts +60 -0
  175. package/src/server/index.ts +5 -0
  176. package/src/server/server-connection-protocol.ts +57 -0
  177. package/src/server/server-connection.ts +144 -0
  178. package/src/server/server-manager.ts +76 -0
  179. package/src/server/server-module.ts +9 -0
  180. package/src/session/index.ts +4 -0
  181. package/src/session/libro-session-manager.ts +377 -0
  182. package/src/session/libro-session-protocol.ts +61 -0
  183. package/src/session/libro-session.ts +33 -0
  184. package/src/session/restapi.ts +126 -0
  185. package/src/session/session-module.ts +26 -0
  186. package/src/session/validate.ts +39 -0
  187. package/src/utils.ts +28 -0
  188. package/src/validate-property.ts +38 -0
@@ -0,0 +1,61 @@
1
+ import type { IKernelModel } from '../kernel/libro-kernel-protocol.js';
2
+ import type { LibroKernel } from '../kernel/libro-kernel.js';
3
+
4
+ import type { LibroSession } from './libro-session.js';
5
+
6
+ export interface NotebookInSession {
7
+ name: string;
8
+ path: string;
9
+ }
10
+
11
+ export interface SessionMeta {
12
+ id: SessionId;
13
+ name: string;
14
+
15
+ type: string;
16
+
17
+ path: string;
18
+
19
+ kernel: LibroKernel;
20
+
21
+ notebook: NotebookInSession;
22
+ }
23
+
24
+ export const SessionMetaOption = Symbol('SessionMetaOption');
25
+
26
+ export const SessionId = Symbol('LibroSessionId');
27
+ export type SessionId = string;
28
+
29
+ export const LibroSessionFactory = Symbol('LibroSessionFactory');
30
+ export type LibroSessionFactory = (session: SessionMeta) => LibroSession;
31
+
32
+ /**
33
+ * The session model returned by the server.
34
+ *
35
+ * #### Notes
36
+ * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
37
+ */
38
+ export interface SessionIModel {
39
+ /**
40
+ * The unique identifier for the session client.
41
+ */
42
+ readonly id: string;
43
+ readonly name: string;
44
+ readonly path: string;
45
+ readonly type: string;
46
+ readonly kernel: IKernelModel | null;
47
+ }
48
+
49
+ /**
50
+ * A session request.
51
+ *
52
+ * #### Notes
53
+ * The `path` and `type` session model parameters are required. The `name`
54
+ * parameter is not technically required, but is often assumed to be nonempty,
55
+ * so we require it too.
56
+ *
57
+ * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
58
+ */
59
+ export type ISessionOptions = Pick<SessionIModel, 'path' | 'type' | 'name'> & {
60
+ kernel?: Partial<Pick<IKernelModel, 'name'>>;
61
+ };
@@ -0,0 +1,33 @@
1
+ import { transient, inject } from '@difizen/mana-app';
2
+
3
+ import type { LibroKernel } from '../kernel/libro-kernel.js';
4
+
5
+ import type {
6
+ NotebookInSession,
7
+ SessionId,
8
+ SessionMeta,
9
+ } from './libro-session-protocol.js';
10
+ import { SessionMetaOption } from './libro-session-protocol.js';
11
+
12
+ @transient()
13
+ export class LibroSession {
14
+ id: SessionId;
15
+ name: string;
16
+
17
+ type: string;
18
+
19
+ path: string;
20
+
21
+ kernel: LibroKernel;
22
+
23
+ notebook: NotebookInSession;
24
+
25
+ constructor(@inject(SessionMetaOption) meta: SessionMeta) {
26
+ this.id = meta.id;
27
+ this.name = meta.name;
28
+ this.type = meta.type;
29
+ this.path = meta.path;
30
+ this.kernel = meta.kernel;
31
+ this.notebook = meta.notebook;
32
+ }
33
+ }
@@ -0,0 +1,126 @@
1
+ // import { default as axios } from 'axios';
2
+ import { URL } from '@difizen/libro-common';
3
+ import { inject, singleton } from '@difizen/mana-app';
4
+
5
+ import type { ISettings } from '../server/index.js';
6
+ import { createResponseError } from '../server/index.js';
7
+ import { ServerConnection } from '../server/server-connection.js';
8
+
9
+ import type {
10
+ ISessionOptions,
11
+ SessionIModel,
12
+ SessionMeta,
13
+ } from './libro-session-protocol.js';
14
+ import { updateLegacySessionModel, validateModel } from './validate.js';
15
+
16
+ export const SESSION_SERVICE_URL = 'api/sessions';
17
+
18
+ export type DeepPartial<T> = {
19
+ [P in keyof T]?: DeepPartial<T[P]>;
20
+ };
21
+
22
+ /**
23
+ * Get a session url.
24
+ */
25
+ export function getSessionUrl(baseUrl: string, id: string): string {
26
+ return URL.join(baseUrl, SESSION_SERVICE_URL, id);
27
+ }
28
+
29
+ @singleton()
30
+ export class SessionRestAPI {
31
+ @inject(ServerConnection) serverConnection: ServerConnection;
32
+
33
+ // async startNewSession(params: {
34
+ // fileName: string;
35
+ // kernelName: string;
36
+ // path: string;
37
+ // type: string;
38
+ // }) {
39
+ // try {
40
+ // const res = await axios.post(`/api/sessions`, {
41
+ // kernel: { name: params.kernelName }, // 来源于当前文件选择了哪一个kernel,名称来自于kernelspecs
42
+ // name: params.fileName,
43
+ // path: params.path,
44
+ // type: params.type,
45
+ // });
46
+ // if (res.status === 201 && res.data) {
47
+ // return res.data;
48
+ // }
49
+ // } catch (ex) {
50
+ // return undefined;
51
+ // }
52
+ // }
53
+
54
+ /**
55
+ * Create a new session, or return an existing session if the session path
56
+ * already exists.
57
+ */
58
+ async startSession(
59
+ options: ISessionOptions,
60
+ serverSettings?: Partial<ISettings>,
61
+ ): Promise<SessionIModel> {
62
+ const settings = { ...this.serverConnection.settings, ...serverSettings };
63
+ const url = URL.join(settings.baseUrl, SESSION_SERVICE_URL);
64
+ const init = {
65
+ method: 'POST',
66
+ body: JSON.stringify(options),
67
+ };
68
+ const response = await this.serverConnection.makeRequest(url, init, settings);
69
+
70
+ if (response.status !== 201) {
71
+ const err = await createResponseError(response);
72
+ throw err;
73
+ }
74
+ const data = await response.json();
75
+ updateLegacySessionModel(data);
76
+ validateModel(data);
77
+ return data;
78
+ }
79
+
80
+ /**
81
+ * List the running sessions.
82
+ */
83
+ async listRunning(serverSettings?: Partial<ISettings>): Promise<SessionMeta[]> {
84
+ const settings = { ...this.serverConnection.settings, ...serverSettings };
85
+ const url = URL.join(settings.baseUrl, SESSION_SERVICE_URL);
86
+ const response = await this.serverConnection.makeRequest(url, {}, settings);
87
+ if (response.status !== 200) {
88
+ const err = await createResponseError(response);
89
+ throw err;
90
+ }
91
+ const data = await response.json();
92
+
93
+ if (!Array.isArray(data)) {
94
+ throw new Error('Invalid Session list');
95
+ }
96
+ data.forEach((m) => {
97
+ updateLegacySessionModel(m);
98
+ validateModel(m);
99
+ });
100
+ return data;
101
+ }
102
+
103
+ /**
104
+ * Send a PATCH to the server, updating the session path or the kernel.
105
+ */
106
+ async updateSession(
107
+ model: Pick<SessionIModel, 'id'> & DeepPartial<Omit<SessionIModel, 'id'>>,
108
+ serverSettings?: Partial<ISettings>,
109
+ ): Promise<SessionIModel> {
110
+ const settings = { ...this.serverConnection.settings, ...serverSettings };
111
+ const url = getSessionUrl(settings.baseUrl, model.id);
112
+ const init = {
113
+ method: 'PATCH',
114
+ body: JSON.stringify(model),
115
+ };
116
+ const response = await this.serverConnection.makeRequest(url, init, settings);
117
+ if (response.status !== 200) {
118
+ const err = await createResponseError(response);
119
+ throw err;
120
+ }
121
+ const data = await response.json();
122
+ updateLegacySessionModel(data);
123
+ validateModel(data);
124
+ return data;
125
+ }
126
+ }
@@ -0,0 +1,26 @@
1
+ import { ManaModule } from '@difizen/mana-app';
2
+
3
+ import { LibroSessionManager } from './libro-session-manager.js';
4
+ import type { SessionMeta } from './libro-session-protocol.js';
5
+ import { LibroSessionFactory, SessionMetaOption } from './libro-session-protocol.js';
6
+ import { LibroSession } from './libro-session.js';
7
+ import { SessionRestAPI } from './restapi.js';
8
+
9
+ export const LibroSessionModule = ManaModule.create().register(
10
+ SessionRestAPI,
11
+ LibroSession,
12
+ LibroSessionManager,
13
+ {
14
+ token: LibroSessionFactory,
15
+ useFactory: (ctx) => {
16
+ return (sessionMeta: SessionMeta) => {
17
+ const child = ctx.container.createChild();
18
+ child.register({
19
+ token: SessionMetaOption,
20
+ useValue: sessionMeta,
21
+ });
22
+ return child.get(LibroSession);
23
+ };
24
+ },
25
+ },
26
+ );
@@ -0,0 +1,39 @@
1
+ import { validateModel as validateKernelModel } from '../kernel/validate.js';
2
+ import { validateProperty } from '../validate-property.js';
3
+
4
+ import type { SessionMeta } from './libro-session-protocol.js';
5
+
6
+ /**
7
+ * Validate an `Session.IModel` object.
8
+ */
9
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
10
+ export function validateModel(data: any): asserts data is SessionMeta {
11
+ validateProperty(data, 'id', 'string');
12
+ validateProperty(data, 'type', 'string');
13
+ validateProperty(data, 'name', 'string');
14
+ validateProperty(data, 'path', 'string');
15
+ validateProperty(data, 'kernel', 'object');
16
+ validateKernelModel(data.kernel);
17
+ }
18
+
19
+ /**
20
+ * Update model from legacy session data.
21
+ */
22
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
23
+ export function updateLegacySessionModel(data: any): void {
24
+ if (data.path === undefined && data.notebook !== undefined) {
25
+ data.path = data.notebook.path;
26
+ data.type = 'notebook';
27
+ data.name = '';
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Validate an array of `Session.IModel` objects.
33
+ */
34
+ export function validateModels(models: SessionMeta[]): asserts models is SessionMeta[] {
35
+ if (!Array.isArray(models)) {
36
+ throw new Error('Invalid session list');
37
+ }
38
+ models.forEach((d) => validateModel(d));
39
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,28 @@
1
+ // 从localstorage获取值
2
+ export const getPersist = (
3
+ key = '',
4
+ needParse = false,
5
+ defaultVal: string | undefined = undefined,
6
+ ) => {
7
+ const jsonStr = window.localStorage.getItem(key) || defaultVal;
8
+ if (!needParse) {
9
+ return jsonStr;
10
+ }
11
+ try {
12
+ if (!jsonStr) {
13
+ return defaultVal;
14
+ }
15
+ return JSON.parse(jsonStr);
16
+ } catch (ex) {
17
+ return defaultVal;
18
+ }
19
+ };
20
+
21
+ // 向localstorage更新值
22
+ export const setPersist = (key = '', val = '') => {
23
+ window.localStorage.setItem(key, val);
24
+ };
25
+
26
+ export const removePersist = (key = '') => {
27
+ window.localStorage.removeItem(key);
28
+ };
@@ -0,0 +1,38 @@
1
+ export function validateProperty(
2
+ object: any,
3
+ name: string,
4
+ typeName?: string,
5
+ values: any[] = [],
6
+ ): void {
7
+ // eslint-disable-next-line no-prototype-builtins
8
+ if (!object.hasOwnProperty(name)) {
9
+ throw new Error(`Missing property '${name}`);
10
+ }
11
+ const value = object[name];
12
+ if (typeName !== void 0) {
13
+ let valid = true;
14
+ if (typeName === 'array') {
15
+ valid = Array.isArray(value);
16
+ } else if (typeName === 'object') {
17
+ valid = typeof value !== 'undefined';
18
+ } else {
19
+ valid = typeof value === typeName;
20
+ }
21
+ if (!valid) {
22
+ throw new Error(`Property '${name}' is not of type '${typeName}'`);
23
+ }
24
+ if (values.length > 0) {
25
+ let isValid = true;
26
+ if (['string', 'number', 'boolean'].includes(typeName)) {
27
+ isValid = values.includes(value);
28
+ } else {
29
+ isValid = values.findIndex((v) => v === value) >= 0;
30
+ }
31
+ if (!isValid) {
32
+ throw new Error(
33
+ `Property '${name}' is not one of the valid values ${JSON.stringify(values)}`,
34
+ );
35
+ }
36
+ }
37
+ }
38
+ }