@jupyterlite/settings 0.2.2 → 0.3.0-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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/settings",
3
- "version": "0.2.2",
3
+ "version": "0.3.0-alpha.0",
4
4
  "description": "JupyterLite - Settings",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -27,7 +27,8 @@
27
27
  "lib/*.js.map",
28
28
  "lib/*.js",
29
29
  "style/*.css",
30
- "style/index.js"
30
+ "style/index.js",
31
+ "src/**/*.{ts,tsx}"
31
32
  ],
32
33
  "scripts": {
33
34
  "build": "tsc -b",
@@ -42,9 +43,9 @@
42
43
  "watch": "tsc -b --watch"
43
44
  },
44
45
  "dependencies": {
45
- "@jupyterlab/coreutils": "~6.0.9",
46
- "@jupyterlab/settingregistry": "~4.0.9",
47
- "@jupyterlite/localforage": "^0.2.2",
46
+ "@jupyterlab/coreutils": "~6.1.1",
47
+ "@jupyterlab/settingregistry": "~4.1.1",
48
+ "@jupyterlite/localforage": "^0.3.0-alpha.0",
48
49
  "@lumino/coreutils": "^2.1.2",
49
50
  "json5": "^2.2.0",
50
51
  "localforage": "^1.9.0"
@@ -52,12 +53,12 @@
52
53
  "devDependencies": {
53
54
  "@babel/core": "^7.11.6",
54
55
  "@babel/preset-env": "^7.12.1",
55
- "@jupyterlab/testutils": "~4.0.9",
56
+ "@jupyterlab/testutils": "~4.1.1",
56
57
  "@types/jest": "^29.5.3",
57
58
  "jest": "^29.6.2",
58
59
  "rimraf": "~5.0.1",
59
60
  "ts-jest": "^29.1.1",
60
- "typescript": "~5.0.4"
61
+ "typescript": "~5.1.6"
61
62
  },
62
63
  "publishConfig": {
63
64
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './settings';
5
+ export * from './tokens';
@@ -0,0 +1,195 @@
1
+ import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
+
3
+ import { PromiseDelegate } from '@lumino/coreutils';
4
+
5
+ import * as json5 from 'json5';
6
+
7
+ import type localforage from 'localforage';
8
+
9
+ import { IPlugin, ISettings, SettingsFile } from './tokens';
10
+
11
+ /**
12
+ * The name of the local storage.
13
+ */
14
+ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
15
+
16
+ /**
17
+ * A class to handle requests to /api/settings
18
+ */
19
+ export class Settings implements ISettings {
20
+ constructor(options: Settings.IOptions) {
21
+ this._localforage = options.localforage;
22
+ this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
23
+ this._storageDrivers = options.storageDrivers || null;
24
+
25
+ this._ready = new PromiseDelegate();
26
+ }
27
+
28
+ /**
29
+ * A promise that resolves when the settings storage is fully initialized
30
+ */
31
+ get ready(): Promise<void> {
32
+ return this._ready.promise;
33
+ }
34
+
35
+ /**
36
+ * A lazy reference to initialized storage
37
+ */
38
+ protected get storage(): Promise<LocalForage> {
39
+ return this.ready.then(() => this._storage as LocalForage);
40
+ }
41
+
42
+ /**
43
+ * Finish any initialization after server has started and all extensions are applied.
44
+ */
45
+ async initialize() {
46
+ await this.initStorage();
47
+ this._ready.resolve(void 0);
48
+ }
49
+
50
+ /**
51
+ * Prepare the storage
52
+ */
53
+ protected async initStorage() {
54
+ this._storage = this.defaultSettingsStorage();
55
+ }
56
+
57
+ /**
58
+ * Get default options for localForage instances
59
+ */
60
+ protected get defaultStorageOptions(): LocalForageOptions {
61
+ const driver = this._storageDrivers?.length ? this._storageDrivers : null;
62
+ return {
63
+ version: 1,
64
+ name: this._storageName,
65
+ ...(driver ? { driver } : {}),
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Create a settings store.
71
+ */
72
+ protected defaultSettingsStorage(): LocalForage {
73
+ return this._localforage.createInstance({
74
+ description: 'Offline Storage for Settings',
75
+ storeName: 'settings',
76
+ ...this.defaultStorageOptions,
77
+ });
78
+ }
79
+
80
+ /**
81
+ * Get settings by plugin id
82
+ *
83
+ * @param pluginId the id of the plugin
84
+ *
85
+ */
86
+ async get(pluginId: string): Promise<IPlugin | undefined> {
87
+ const all = await this.getAll();
88
+ const settings = all.settings as IPlugin[];
89
+ const setting = settings.find((setting: IPlugin) => {
90
+ return setting.id === pluginId;
91
+ });
92
+ return setting;
93
+ }
94
+
95
+ /**
96
+ * Get all the settings
97
+ */
98
+ async getAll(): Promise<{ settings: IPlugin[] }> {
99
+ const allCore = await this._getAll('all.json');
100
+ let allFederated: IPlugin[] = [];
101
+ try {
102
+ allFederated = await this._getAll('all_federated.json');
103
+ } catch {
104
+ // handle the case where there is no federated extension
105
+ }
106
+
107
+ // JupyterLab 4 expects all settings to be returned in one go
108
+ // so append the settings from federated plugins to the core ones
109
+ const all = allCore.concat(allFederated);
110
+
111
+ // return existing user settings if they exist
112
+ const storage = await this.storage;
113
+ const settings = await Promise.all(
114
+ all.map(async (plugin) => {
115
+ const { id } = plugin;
116
+ const raw = ((await storage.getItem(id)) as string) ?? plugin.raw;
117
+ return {
118
+ ...Private.override(plugin),
119
+ raw,
120
+ settings: json5.parse(raw),
121
+ };
122
+ }),
123
+ );
124
+ return { settings };
125
+ }
126
+
127
+ /**
128
+ * Save settings for a given plugin id
129
+ *
130
+ * @param pluginId The id of the plugin
131
+ * @param raw The raw settings
132
+ *
133
+ */
134
+ async save(pluginId: string, raw: string): Promise<void> {
135
+ await (await this.storage).setItem(pluginId, raw);
136
+ }
137
+
138
+ /**
139
+ * Get all the settings for core or federated plugins
140
+ */
141
+ private async _getAll(file: SettingsFile): Promise<IPlugin[]> {
142
+ const settingsUrl = PageConfig.getOption('settingsUrl') ?? '/';
143
+ const all = (await (
144
+ await fetch(URLExt.join(settingsUrl, file))
145
+ ).json()) as IPlugin[];
146
+ return all;
147
+ }
148
+
149
+ private _storageName: string = DEFAULT_STORAGE_NAME;
150
+ private _storageDrivers: string[] | null = null;
151
+ private _storage: LocalForage | undefined;
152
+ private _localforage: typeof localforage;
153
+ private _ready: PromiseDelegate<void>;
154
+ }
155
+
156
+ /**
157
+ * A namespace for settings metadata.
158
+ */
159
+ export namespace Settings {
160
+ /**
161
+ * Initialization options for settings.
162
+ */
163
+ export interface IOptions {
164
+ localforage: typeof localforage;
165
+ storageName?: string | null;
166
+ storageDrivers?: string[] | null;
167
+ }
168
+ }
169
+
170
+ /**
171
+ * A namespace for private data
172
+ */
173
+ namespace Private {
174
+ const _overrides: Record<string, IPlugin['schema']['default']> = JSON.parse(
175
+ PageConfig.getOption('settingsOverrides') || '{}',
176
+ );
177
+
178
+ /**
179
+ * Override the defaults of the schema with ones from PageConfig
180
+ *
181
+ * @see https://github.com/jupyterlab/jupyterlab_server/blob/v2.5.2/jupyterlab_server/settings_handler.py#L216-L227
182
+ */
183
+ export function override(plugin: IPlugin): IPlugin {
184
+ if (_overrides[plugin.id]) {
185
+ if (!plugin.schema.properties) {
186
+ // probably malformed, or only provides keyboard shortcuts, etc.
187
+ plugin.schema.properties = {};
188
+ }
189
+ for (const [prop, propDefault] of Object.entries(_overrides[plugin.id] || {})) {
190
+ plugin.schema.properties[prop].default = propDefault;
191
+ }
192
+ }
193
+ return plugin;
194
+ }
195
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
2
+
3
+ import { JSONObject, PartialJSONObject, Token } from '@lumino/coreutils';
4
+
5
+ /**
6
+ * The token for the settings service.
7
+ */
8
+ export const ISettings = new Token<ISettings>('@jupyterlite/settings:ISettings');
9
+
10
+ /**
11
+ * The settings file to request
12
+ */
13
+ export type SettingsFile = 'all.json' | 'all_federated.json';
14
+
15
+ /**
16
+ * An interface for the plugin settings.
17
+ */
18
+ export interface IPlugin extends PartialJSONObject {
19
+ /**
20
+ * The name of the plugin.
21
+ */
22
+ id: string;
23
+
24
+ /**
25
+ * The settings for the plugin.
26
+ */
27
+ settings: JSONObject;
28
+
29
+ /**
30
+ * The raw user settings data as a string containing JSON with comments.
31
+ */
32
+ raw: string;
33
+
34
+ /**
35
+ * The JSON schema for the plugin.
36
+ */
37
+ schema: ISettingRegistry.ISchema;
38
+
39
+ /**
40
+ * The published version of the NPM package containing the plugin.
41
+ */
42
+ version: string;
43
+ }
44
+
45
+ /**
46
+ * The interface for the Settings service.
47
+ */
48
+ export interface ISettings {
49
+ /**
50
+ * A promise that resolves after the settings have been full initialized
51
+ */
52
+ ready: Promise<void>;
53
+
54
+ /**
55
+ * Get settings by plugin id
56
+ *
57
+ * @param pluginId the id of the plugin
58
+ *
59
+ */
60
+ get(pluginId: string): Promise<IPlugin | undefined>;
61
+
62
+ /**
63
+ * Get all the settings
64
+ */
65
+ getAll(): Promise<{ settings: IPlugin[] }>;
66
+
67
+ /**
68
+ * Save settings for a given plugin id
69
+ *
70
+ * @param pluginId The id of the plugin
71
+ * @param raw The raw settings
72
+ *
73
+ */
74
+ save(pluginId: string, raw: string): Promise<void>;
75
+ }