@jupyterlite/settings 0.7.0-beta.1 → 0.7.0-rc.1

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/lib/index.d.ts CHANGED
@@ -1 +1,17 @@
1
- export * from './settings';
1
+ /**
2
+ * @deprecated This package is deprecated. Please import from @jupyterlite/services instead.
3
+ *
4
+ * This package now acts as a shim that re-exports settings-related components from
5
+ * @jupyterlite/services for backward compatibility.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Old (deprecated):
10
+ * import { Settings } from '@jupyterlite/settings';
11
+ *
12
+ * // New (recommended):
13
+ * import { Settings } from '@jupyterlite/services';
14
+ * ```
15
+ */
16
+ export { Settings } from '@jupyterlite/services';
17
+ export type { SettingsFile } from '@jupyterlite/services';
package/lib/index.js CHANGED
@@ -1,4 +1,20 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
- export * from './settings';
3
+ /**
4
+ * @deprecated This package is deprecated. Please import from @jupyterlite/services instead.
5
+ *
6
+ * This package now acts as a shim that re-exports settings-related components from
7
+ * @jupyterlite/services for backward compatibility.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // Old (deprecated):
12
+ * import { Settings } from '@jupyterlite/settings';
13
+ *
14
+ * // New (recommended):
15
+ * import { Settings } from '@jupyterlite/services';
16
+ * ```
17
+ */
18
+ // Re-export settings-related classes
19
+ export { Settings } from '@jupyterlite/services';
4
20
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAE3D,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAE3D;;;;;;;;;;;;;;GAcG;AAEH,qCAAqC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jupyterlite/settings",
3
- "version": "0.7.0-beta.1",
4
- "description": "JupyterLite - Settings",
3
+ "version": "0.7.0-rc.1",
4
+ "description": "JupyterLite - Settings (DEPRECATED: Use @jupyterlite/services instead)",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
7
7
  "url": "https://github.com/jupyterlite/jupyterlite/issues"
@@ -43,17 +43,12 @@
43
43
  "watch": "tsc -b --watch"
44
44
  },
45
45
  "dependencies": {
46
- "@jupyterlab/coreutils": "~6.5.0-rc.0",
47
- "@jupyterlab/settingregistry": "~4.5.0-rc.0",
48
- "@jupyterlite/localforage": "^0.7.0-beta.1",
49
- "@lumino/coreutils": "^2.2.1",
50
- "json5": "^2.2.0",
51
- "localforage": "^1.9.0"
46
+ "@jupyterlite/services": "^0.7.0-rc.1"
52
47
  },
53
48
  "devDependencies": {
54
49
  "@babel/core": "^7.11.6",
55
50
  "@babel/preset-env": "^7.12.1",
56
- "@jupyterlab/testutils": "~4.5.0-rc.0",
51
+ "@jupyterlab/testutils": "~4.5.0",
57
52
  "@types/jest": "^29.5.3",
58
53
  "jest": "^29.6.2",
59
54
  "rimraf": "~5.0.1",
package/src/index.ts CHANGED
@@ -1,4 +1,24 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
 
4
- export * from './settings';
4
+ /**
5
+ * @deprecated This package is deprecated. Please import from @jupyterlite/services instead.
6
+ *
7
+ * This package now acts as a shim that re-exports settings-related components from
8
+ * @jupyterlite/services for backward compatibility.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Old (deprecated):
13
+ * import { Settings } from '@jupyterlite/settings';
14
+ *
15
+ * // New (recommended):
16
+ * import { Settings } from '@jupyterlite/services';
17
+ * ```
18
+ */
19
+
20
+ // Re-export settings-related classes
21
+ export { Settings } from '@jupyterlite/services';
22
+
23
+ // Re-export settings-related types
24
+ export type { SettingsFile } from '@jupyterlite/services';
package/lib/settings.d.ts DELETED
@@ -1,91 +0,0 @@
1
- import { ServerConnection, Setting, SettingManager } from '@jupyterlab/services';
2
- import { ISettingRegistry } from '@jupyterlab/settingregistry';
3
- import type localforage from 'localforage';
4
- /**
5
- * The settings file to request
6
- */
7
- export type SettingsFile = 'all.json' | 'all_federated.json';
8
- /**
9
- * A class to manage settings in the browser.
10
- */
11
- export declare class Settings extends SettingManager implements Setting.IManager {
12
- /**
13
- * Create a new settings service.
14
- */
15
- constructor(options: Settings.IOptions);
16
- /**
17
- * A promise that resolves when the settings storage is fully initialized
18
- */
19
- get ready(): Promise<void>;
20
- /**
21
- * A lazy reference to initialized storage
22
- */
23
- protected get storage(): Promise<LocalForage>;
24
- /**
25
- * Finish any initialization after server has started and all extensions are applied.
26
- */
27
- initialize(): Promise<void>;
28
- /**
29
- * Prepare the storage
30
- */
31
- protected initStorage(): Promise<void>;
32
- /**
33
- * Get default options for localForage instances
34
- */
35
- protected get defaultStorageOptions(): LocalForageOptions;
36
- /**
37
- * Create a settings store.
38
- */
39
- protected defaultSettingsStorage(): LocalForage;
40
- /**
41
- * Get settings by plugin id
42
- *
43
- * @param pluginId the id of the plugin
44
- *
45
- */
46
- fetch(pluginId: string): Promise<ISettingRegistry.IPlugin>;
47
- /**
48
- * Get all the settings
49
- */
50
- list(query?: 'ids'): Promise<{
51
- ids: string[];
52
- values: ISettingRegistry.IPlugin[];
53
- }>;
54
- /**
55
- * Save settings for a given plugin id
56
- *
57
- * @param pluginId The id of the plugin
58
- * @param raw The raw settings
59
- *
60
- */
61
- save(id: string, raw: string): Promise<void>;
62
- /**
63
- * Clear all stored settings
64
- *
65
- * @returns A promise which resolves when the settings are cleared
66
- */
67
- clear(): Promise<void>;
68
- /**
69
- * Get all the settings for core or federated plugins
70
- */
71
- private _getAll;
72
- private _storageName;
73
- private _storageDrivers;
74
- private _storage;
75
- private _localforage;
76
- private _ready;
77
- }
78
- /**
79
- * A namespace for settings metadata.
80
- */
81
- export declare namespace Settings {
82
- /**
83
- * Initialization options for settings.
84
- */
85
- interface IOptions {
86
- localforage: typeof localforage;
87
- storageName?: string | null;
88
- storageDrivers?: string[] | null;
89
- serverSettings?: ServerConnection.ISettings;
90
- }
91
- }
package/lib/settings.js DELETED
@@ -1,185 +0,0 @@
1
- import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
- import { SettingManager } from '@jupyterlab/services';
3
- import { PromiseDelegate } from '@lumino/coreutils';
4
- import * as json5 from 'json5';
5
- /**
6
- * The name of the local storage.
7
- */
8
- const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
9
- /**
10
- * A class to manage settings in the browser.
11
- */
12
- export class Settings extends SettingManager {
13
- /**
14
- * Create a new settings service.
15
- */
16
- constructor(options) {
17
- super({
18
- serverSettings: options.serverSettings,
19
- });
20
- this._storageName = DEFAULT_STORAGE_NAME;
21
- this._storageDrivers = null;
22
- this._localforage = options.localforage;
23
- this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
24
- this._storageDrivers = options.storageDrivers || null;
25
- this._ready = new PromiseDelegate();
26
- void this.initialize().catch(console.warn);
27
- }
28
- /**
29
- * A promise that resolves when the settings storage is fully initialized
30
- */
31
- get ready() {
32
- return this._ready.promise;
33
- }
34
- /**
35
- * A lazy reference to initialized storage
36
- */
37
- get storage() {
38
- return this.ready.then(() => this._storage);
39
- }
40
- /**
41
- * Finish any initialization after server has started and all extensions are applied.
42
- */
43
- async initialize() {
44
- await this.initStorage();
45
- this._ready.resolve(void 0);
46
- }
47
- /**
48
- * Prepare the storage
49
- */
50
- async initStorage() {
51
- this._storage = this.defaultSettingsStorage();
52
- }
53
- /**
54
- * Get default options for localForage instances
55
- */
56
- get defaultStorageOptions() {
57
- var _a;
58
- const driver = ((_a = this._storageDrivers) === null || _a === void 0 ? void 0 : _a.length) ? this._storageDrivers : null;
59
- return {
60
- version: 1,
61
- name: this._storageName,
62
- ...(driver ? { driver } : {}),
63
- };
64
- }
65
- /**
66
- * Create a settings store.
67
- */
68
- defaultSettingsStorage() {
69
- return this._localforage.createInstance({
70
- description: 'Offline Storage for Settings',
71
- storeName: 'settings',
72
- ...this.defaultStorageOptions,
73
- });
74
- }
75
- /**
76
- * Get settings by plugin id
77
- *
78
- * @param pluginId the id of the plugin
79
- *
80
- */
81
- async fetch(pluginId) {
82
- const all = await this.list();
83
- const settings = all.values;
84
- const setting = settings.find((setting) => {
85
- return setting.id === pluginId;
86
- });
87
- if (!setting) {
88
- throw new Error(`Setting ${pluginId} not found`);
89
- }
90
- return setting;
91
- }
92
- /**
93
- * Get all the settings
94
- */
95
- async list(query) {
96
- var _a, _b;
97
- const allCore = await this._getAll('all.json');
98
- let allFederated = [];
99
- try {
100
- allFederated = await this._getAll('all_federated.json');
101
- }
102
- catch {
103
- // handle the case where there is no federated extension
104
- }
105
- // JupyterLab 4 expects all settings to be returned in one go
106
- // so append the settings from federated plugins to the core ones
107
- const all = allCore.concat(allFederated);
108
- // return existing user settings if they exist
109
- const storage = await this.storage;
110
- const settings = await Promise.all(all.map(async (plugin) => {
111
- var _a;
112
- const { id } = plugin;
113
- const raw = (_a = (await storage.getItem(id))) !== null && _a !== void 0 ? _a : plugin.raw;
114
- return {
115
- ...Private.override(plugin),
116
- raw,
117
- settings: json5.parse(raw),
118
- };
119
- }));
120
- // format the settings
121
- const ids = (_a = settings.map((plugin) => plugin.id)) !== null && _a !== void 0 ? _a : [];
122
- let values = [];
123
- if (!query) {
124
- values =
125
- (_b = settings.map((plugin) => {
126
- plugin.data = { composite: {}, user: {} };
127
- return plugin;
128
- })) !== null && _b !== void 0 ? _b : [];
129
- }
130
- return { ids, values };
131
- }
132
- /**
133
- * Save settings for a given plugin id
134
- *
135
- * @param pluginId The id of the plugin
136
- * @param raw The raw settings
137
- *
138
- */
139
- async save(id, raw) {
140
- await (await this.storage).setItem(id, raw);
141
- }
142
- /**
143
- * Clear all stored settings
144
- *
145
- * @returns A promise which resolves when the settings are cleared
146
- */
147
- async clear() {
148
- await (await this.storage).clear();
149
- }
150
- /**
151
- * Get all the settings for core or federated plugins
152
- */
153
- async _getAll(file) {
154
- var _a;
155
- const settingsUrl = (_a = PageConfig.getOption('settingsUrl')) !== null && _a !== void 0 ? _a : '/';
156
- const all = (await (await fetch(URLExt.join(settingsUrl, file))).json());
157
- return all;
158
- }
159
- }
160
- /**
161
- * A namespace for private data
162
- */
163
- var Private;
164
- (function (Private) {
165
- const _overrides = JSON.parse(PageConfig.getOption('settingsOverrides') || '{}');
166
- /**
167
- * Override the defaults of the schema with ones from PageConfig
168
- *
169
- * @see https://github.com/jupyterlab/jupyterlab_server/blob/v2.5.2/jupyterlab_server/settings_handler.py#L216-L227
170
- */
171
- function override(plugin) {
172
- if (_overrides[plugin.id]) {
173
- if (!plugin.schema.properties) {
174
- // probably malformed, or only provides keyboard shortcuts, etc.
175
- plugin.schema.properties = {};
176
- }
177
- for (const [prop, propDefault] of Object.entries(_overrides[plugin.id] || {})) {
178
- plugin.schema.properties[prop].default = propDefault;
179
- }
180
- }
181
- return plugin;
182
- }
183
- Private.override = override;
184
- })(Private || (Private = {}));
185
- //# sourceMappingURL=settings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"settings.js","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAA6B,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAIjF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAS/B;;GAEG;AACH,MAAM,oBAAoB,GAAG,qBAAqB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,cAAc;IAC1C;;OAEG;IACH,YAAY,OAA0B;QACpC,KAAK,CAAC;YACJ,cAAc,EA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CAAC,OAAO,CAAC,IAAkB;;QACtC,MAAM,WAAW,GAAG,MAAA,UAAU,CAAC,SAAS,CAAC,aAAa,CAAC,mCAAI,GAAG,CAAC;QAC/D,MAAM,GAAG,GAAG,CAAC,MAAM,CACjB,MAAM,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAC5C,CAAC,IAAI,EAAE,CAA+B,CAAC;QACxC,OAAO,GAAG,CAAC;IACb,CAAC;CAOF;AAiBD;;GAEG;AACH,IAAU,OAAO,CAqBhB;AArBD,WAAU,OAAO;IACf,MAAM,UAAU,GACd,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,mBAAmB,CAAC,IAAI,IAAI,CAAC,CAAC;IAEhE;;;;OAIG;IACH,SAAgB,QAAQ,CAAC,MAAgC;QACvD,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;gBAC9B,gEAAgE;gBAChE,MAAM,CAAC,MAAM,CAAC,UAAU,GAAG,EAAE,CAAC;YAChC,CAAC;YACD,KAAK,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;gBAC9E,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,OAAO,GAAG,WAAW,CAAC;YACvD,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAXe,gBAAQ,WAWvB,CAAA;AACH,CAAC,EArBS,OAAO,KAAP,OAAO,QAqBhB"}
package/src/settings.ts DELETED
@@ -1,236 +0,0 @@
1
- import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
-
3
- import { ServerConnection, Setting, SettingManager } from '@jupyterlab/services';
4
-
5
- import { ISettingRegistry } from '@jupyterlab/settingregistry';
6
-
7
- import { PromiseDelegate } from '@lumino/coreutils';
8
-
9
- import * as json5 from 'json5';
10
-
11
- import type localforage from 'localforage';
12
-
13
- /**
14
- * The settings file to request
15
- */
16
- export type SettingsFile = 'all.json' | 'all_federated.json';
17
-
18
- /**
19
- * The name of the local storage.
20
- */
21
- const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
22
-
23
- /**
24
- * A class to manage settings in the browser.
25
- */
26
- export class Settings extends SettingManager implements Setting.IManager {
27
- /**
28
- * Create a new settings service.
29
- */
30
- constructor(options: Settings.IOptions) {
31
- super({
32
- serverSettings: options.serverSettings,
33
- });
34
- this._localforage = options.localforage;
35
- this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
36
- this._storageDrivers = options.storageDrivers || null;
37
-
38
- this._ready = new PromiseDelegate();
39
- void this.initialize().catch(console.warn);
40
- }
41
-
42
- /**
43
- * A promise that resolves when the settings storage is fully initialized
44
- */
45
- get ready(): Promise<void> {
46
- return this._ready.promise;
47
- }
48
-
49
- /**
50
- * A lazy reference to initialized storage
51
- */
52
- protected get storage(): Promise<LocalForage> {
53
- return this.ready.then(() => this._storage as LocalForage);
54
- }
55
-
56
- /**
57
- * Finish any initialization after server has started and all extensions are applied.
58
- */
59
- async initialize() {
60
- await this.initStorage();
61
- this._ready.resolve(void 0);
62
- }
63
-
64
- /**
65
- * Prepare the storage
66
- */
67
- protected async initStorage() {
68
- this._storage = this.defaultSettingsStorage();
69
- }
70
-
71
- /**
72
- * Get default options for localForage instances
73
- */
74
- protected get defaultStorageOptions(): LocalForageOptions {
75
- const driver = this._storageDrivers?.length ? this._storageDrivers : null;
76
- return {
77
- version: 1,
78
- name: this._storageName,
79
- ...(driver ? { driver } : {}),
80
- };
81
- }
82
-
83
- /**
84
- * Create a settings store.
85
- */
86
- protected defaultSettingsStorage(): LocalForage {
87
- return this._localforage.createInstance({
88
- description: 'Offline Storage for Settings',
89
- storeName: 'settings',
90
- ...this.defaultStorageOptions,
91
- });
92
- }
93
-
94
- /**
95
- * Get settings by plugin id
96
- *
97
- * @param pluginId the id of the plugin
98
- *
99
- */
100
- async fetch(pluginId: string): Promise<ISettingRegistry.IPlugin> {
101
- const all = await this.list();
102
- const settings = all.values as ISettingRegistry.IPlugin[];
103
- const setting = settings.find((setting: ISettingRegistry.IPlugin) => {
104
- return setting.id === pluginId;
105
- });
106
- if (!setting) {
107
- throw new Error(`Setting ${pluginId} not found`);
108
- }
109
- return setting;
110
- }
111
-
112
- /**
113
- * Get all the settings
114
- */
115
- async list(
116
- query?: 'ids',
117
- ): Promise<{ ids: string[]; values: ISettingRegistry.IPlugin[] }> {
118
- const allCore = await this._getAll('all.json');
119
- let allFederated: ISettingRegistry.IPlugin[] = [];
120
- try {
121
- allFederated = await this._getAll('all_federated.json');
122
- } catch {
123
- // handle the case where there is no federated extension
124
- }
125
-
126
- // JupyterLab 4 expects all settings to be returned in one go
127
- // so append the settings from federated plugins to the core ones
128
- const all = allCore.concat(allFederated);
129
-
130
- // return existing user settings if they exist
131
- const storage = await this.storage;
132
- const settings = await Promise.all(
133
- all.map(async (plugin) => {
134
- const { id } = plugin;
135
- const raw = ((await storage.getItem(id)) as string) ?? plugin.raw;
136
- return {
137
- ...Private.override(plugin),
138
- raw,
139
- settings: json5.parse(raw),
140
- };
141
- }),
142
- );
143
-
144
- // format the settings
145
- const ids = settings.map((plugin: ISettingRegistry.IPlugin) => plugin.id) ?? [];
146
-
147
- let values: ISettingRegistry.IPlugin[] = [];
148
- if (!query) {
149
- values =
150
- settings.map((plugin: ISettingRegistry.IPlugin) => {
151
- plugin.data = { composite: {}, user: {} };
152
- return plugin;
153
- }) ?? [];
154
- }
155
-
156
- return { ids, values };
157
- }
158
-
159
- /**
160
- * Save settings for a given plugin id
161
- *
162
- * @param pluginId The id of the plugin
163
- * @param raw The raw settings
164
- *
165
- */
166
- async save(id: string, raw: string): Promise<void> {
167
- await (await this.storage).setItem(id, raw);
168
- }
169
-
170
- /**
171
- * Clear all stored settings
172
- *
173
- * @returns A promise which resolves when the settings are cleared
174
- */
175
- async clear(): Promise<void> {
176
- await (await this.storage).clear();
177
- }
178
-
179
- /**
180
- * Get all the settings for core or federated plugins
181
- */
182
- private async _getAll(file: SettingsFile): Promise<ISettingRegistry.IPlugin[]> {
183
- const settingsUrl = PageConfig.getOption('settingsUrl') ?? '/';
184
- const all = (await (
185
- await fetch(URLExt.join(settingsUrl, file))
186
- ).json()) as ISettingRegistry.IPlugin[];
187
- return all;
188
- }
189
-
190
- private _storageName: string = DEFAULT_STORAGE_NAME;
191
- private _storageDrivers: string[] | null = null;
192
- private _storage: LocalForage | undefined;
193
- private _localforage: typeof localforage;
194
- private _ready: PromiseDelegate<void>;
195
- }
196
-
197
- /**
198
- * A namespace for settings metadata.
199
- */
200
- export namespace Settings {
201
- /**
202
- * Initialization options for settings.
203
- */
204
- export interface IOptions {
205
- localforage: typeof localforage;
206
- storageName?: string | null;
207
- storageDrivers?: string[] | null;
208
- serverSettings?: ServerConnection.ISettings;
209
- }
210
- }
211
-
212
- /**
213
- * A namespace for private data
214
- */
215
- namespace Private {
216
- const _overrides: Record<string, ISettingRegistry.IPlugin['schema']['default']> =
217
- JSON.parse(PageConfig.getOption('settingsOverrides') || '{}');
218
-
219
- /**
220
- * Override the defaults of the schema with ones from PageConfig
221
- *
222
- * @see https://github.com/jupyterlab/jupyterlab_server/blob/v2.5.2/jupyterlab_server/settings_handler.py#L216-L227
223
- */
224
- export function override(plugin: ISettingRegistry.IPlugin): ISettingRegistry.IPlugin {
225
- if (_overrides[plugin.id]) {
226
- if (!plugin.schema.properties) {
227
- // probably malformed, or only provides keyboard shortcuts, etc.
228
- plugin.schema.properties = {};
229
- }
230
- for (const [prop, propDefault] of Object.entries(_overrides[plugin.id] || {})) {
231
- plugin.schema.properties[prop].default = propDefault;
232
- }
233
- }
234
- return plugin;
235
- }
236
- }