@jupyterlite/contents 0.2.1 → 0.2.3

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.
package/src/tokens.ts ADDED
@@ -0,0 +1,202 @@
1
+ import { IDisposable } from '@lumino/disposable';
2
+ import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
3
+ import { PageConfig } from '@jupyterlab/coreutils';
4
+ import mime from 'mime';
5
+
6
+ import { Contents as ServerContents } from '@jupyterlab/services';
7
+
8
+ import { Token } from '@lumino/coreutils';
9
+
10
+ /**
11
+ * The token for the settings service.
12
+ */
13
+ export const IContents = new Token<IContents>('@jupyterlite/contents:IContents');
14
+
15
+ /**
16
+ * The interface for the contents service.
17
+ */
18
+ export interface IContents {
19
+ /**
20
+ * A promise that resolves after the contents have been full initialized.
21
+ */
22
+ ready: Promise<void>;
23
+
24
+ /**
25
+ * Create a new untitled file or directory in the specified directory path.
26
+ *
27
+ * @param options: The options used to create the file.
28
+ *
29
+ * @returns A promise which resolves with the created file content when the file is created.
30
+ */
31
+ newUntitled(
32
+ options?: ServerContents.ICreateOptions,
33
+ ): Promise<ServerContents.IModel | null>;
34
+
35
+ /**
36
+ * Copy a file into a given directory.
37
+ *
38
+ * @param path - The original file path.
39
+ * @param toDir - The destination directory path.
40
+ *
41
+ * @returns A promise which resolves with the new contents model when the
42
+ * file is copied.
43
+ *
44
+ * #### Notes
45
+ * The server will select the name of the copied file.
46
+ */
47
+ copy(path: string, toDir: string): Promise<ServerContents.IModel>;
48
+
49
+ /**
50
+ * Get a file or directory.
51
+ *
52
+ * @param path: The path to the file.
53
+ * @param options: The options used to fetch the file.
54
+ *
55
+ * @returns A promise which resolves with the file content.
56
+ */
57
+ get(
58
+ path: string,
59
+ options?: ServerContents.IFetchOptions,
60
+ ): Promise<ServerContents.IModel | null>;
61
+
62
+ /**
63
+ * Rename a file or directory.
64
+ *
65
+ * @param oldLocalPath - The original file path.
66
+ * @param newLocalPath - The new file path.
67
+ *
68
+ * @returns A promise which resolves with the new file content model when the file is renamed.
69
+ */
70
+ rename(oldLocalPath: string, newLocalPath: string): Promise<ServerContents.IModel>;
71
+
72
+ /**
73
+ * Save a file.
74
+ *
75
+ * @param path - The desired file path.
76
+ * @param options - Optional overrides to the model.
77
+ *
78
+ * @returns A promise which resolves with the file content model when the file is saved.
79
+ */
80
+ save(
81
+ path: string,
82
+ options?: Partial<ServerContents.IModel>,
83
+ ): Promise<ServerContents.IModel | null>;
84
+
85
+ /**
86
+ * Delete a file.
87
+ *
88
+ * @param path - The path to the file.
89
+ */
90
+ delete(path: string): Promise<void>;
91
+
92
+ /**
93
+ * Create a checkpoint for a file.
94
+ *
95
+ * @param path - The path of the file.
96
+ *
97
+ * @returns A promise which resolves with the new checkpoint model when the
98
+ * checkpoint is created.
99
+ */
100
+ createCheckpoint(path: string): Promise<ServerContents.ICheckpointModel>;
101
+
102
+ /**
103
+ * List available checkpoints for a file.
104
+ *
105
+ * @param path - The path of the file.
106
+ *
107
+ * @returns A promise which resolves with a list of checkpoint models for
108
+ * the file.
109
+ */
110
+ listCheckpoints(path: string): Promise<ServerContents.ICheckpointModel[]>;
111
+
112
+ /**
113
+ * Restore a file to a known checkpoint state.
114
+ *
115
+ * @param path - The path of the file.
116
+ * @param checkpointID - The id of the checkpoint to restore.
117
+ *
118
+ * @returns A promise which resolves when the checkpoint is restored.
119
+ */
120
+ restoreCheckpoint(path: string, checkpointID: string): Promise<void>;
121
+
122
+ /**
123
+ * Delete a checkpoint for a file.
124
+ *
125
+ * @param path - The path of the file.
126
+ * @param checkpointID - The id of the checkpoint to delete.
127
+ *
128
+ * @returns A promise which resolves when the checkpoint is deleted.
129
+ */
130
+ deleteCheckpoint(path: string, checkpointID: string): Promise<void>;
131
+ }
132
+
133
+ /**
134
+ * Commonly-used mimetypes
135
+ */
136
+ export namespace MIME {
137
+ export const JSON = 'application/json';
138
+ export const PLAIN_TEXT = 'text/plain';
139
+ export const OCTET_STREAM = 'octet/stream';
140
+ }
141
+
142
+ /**
143
+ * A namespace for file constructs.
144
+ */
145
+ export namespace FILE {
146
+ /**
147
+ * Build-time configured file types.
148
+ */
149
+ const TYPES: Record<string, Partial<IRenderMime.IFileType>> = JSON.parse(
150
+ PageConfig.getOption('fileTypes') || '{}',
151
+ );
152
+
153
+ /**
154
+ * Get a mimetype (or fallback).
155
+ */
156
+ export function getType(ext: string, defaultType: string | null = null): string {
157
+ ext = ext.toLowerCase();
158
+ for (const fileType of Object.values(TYPES)) {
159
+ for (const fileExt of fileType.extensions || []) {
160
+ if (fileExt === ext && fileType.mimeTypes && fileType.mimeTypes.length) {
161
+ return fileType.mimeTypes[0];
162
+ }
163
+ }
164
+ }
165
+
166
+ return mime.getType(ext) || defaultType || MIME.OCTET_STREAM;
167
+ }
168
+
169
+ /**
170
+ * Determine whether the given extension matches a given fileFormat.
171
+ */
172
+ export function hasFormat(
173
+ ext: string,
174
+ fileFormat: 'base64' | 'text' | 'json',
175
+ ): boolean {
176
+ ext = ext.toLowerCase();
177
+ for (const fileType of Object.values(TYPES)) {
178
+ if (fileType.fileFormat !== fileFormat) {
179
+ continue;
180
+ }
181
+ for (const fileExt of fileType.extensions || []) {
182
+ if (fileExt === ext) {
183
+ return true;
184
+ }
185
+ }
186
+ }
187
+ return false;
188
+ }
189
+ }
190
+
191
+ /**
192
+ * The token for the BroadcastChannel broadcaster.
193
+ */
194
+ export const IBroadcastChannelWrapper = new Token<IBroadcastChannelWrapper>(
195
+ '@jupyterlite/contents:IBroadcastChannelWrapper',
196
+ );
197
+
198
+ export interface IBroadcastChannelWrapper extends IDisposable {
199
+ enable(): void;
200
+ disable(): void;
201
+ enabled: boolean;
202
+ }