浏览代码

Update usages of pageconfig

Steven Silvester 8 年之前
父节点
当前提交
f4fadd18ca

+ 2 - 6
packages/help-extension/src/index.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  utils
-} from '@jupyterlab/services';
-
 import {
   Message
 } from '@phosphor/messaging';
@@ -23,7 +19,7 @@ import {
 } from '@jupyterlab/apputils';
 
 import {
-  URLExt
+  PageConfig, URLExt
 } from '@jupyterlab/coreutils';
 
 
@@ -227,7 +223,7 @@ function activate(app: JupyterLab, mainMenu: IMainMenu, palette: ICommandPalette
 
   commands.addCommand(CommandIDs.launchClassic, {
     label: 'Launch Classic Notebook',
-    execute: () => { window.open(utils.getBaseUrl() + 'tree'); }
+    execute: () => { window.open(PageConfig.getBaseUrl() + 'tree'); }
   });
 
   RESOURCES.forEach(args => { palette.addItem({ args, command, category }); });

+ 2 - 2
packages/services/src/config/index.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  URLExt
+  PageConfig, URLExt
 } from '@jupyterlab/coreutils';
 
 import {
@@ -104,7 +104,7 @@ class DefaultConfigSection implements IConfigSection {
    * Construct a new config section.
    */
   constructor(options: ConfigSection.IOptions) {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     this.ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     this._url = URLExt.join(baseUrl, SERVICE_CONFIG_URL,
                             encodeURIComponent(options.name));

+ 2 - 2
packages/services/src/contents/index.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  URLExt
+  PageConfig, URLExt
 } from '@jupyterlab/coreutils';
 
 import {
@@ -356,7 +356,7 @@ class ContentsManager implements Contents.IManager {
    * @param options - The options used to initialize the object.
    */
   constructor(options: ContentsManager.IOptions = {}) {
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     this._ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
   }
 

+ 9 - 9
packages/services/src/kernel/default.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  URLExt, uuid
+  PageConfig, URLExt, uuid
 } from '@jupyterlab/coreutils';
 
 import {
@@ -73,12 +73,12 @@ class DefaultKernel implements Kernel.IKernel {
   constructor(options: Kernel.IOptions, id: string) {
     this._name = options.name;
     this._id = id;
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
-    this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    this._wsUrl = options.wsUrl || PageConfig.getWsUrl(this._baseUrl);
     this._ajaxSettings = JSON.stringify(
       utils.ajaxSettingsWithToken(options.ajaxSettings, options.token)
     );
-    this._token = options.token || utils.getConfigOption('token');
+    this._token = options.token || PageConfig.getOption('token');
     this._clientId = options.clientId || uuid();
     this._username = options.username || '';
     this._futures = new Map<string, KernelFutureHandler>();
@@ -1099,7 +1099,7 @@ namespace Private {
    */
   export
   function getSpecs(options: Kernel.IOptions = {}): Promise<Kernel.ISpecModels> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, KERNELSPEC_SERVICE_URL);
     let ajaxSettings: IAjaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'GET';
@@ -1128,7 +1128,7 @@ namespace Private {
    */
   export
   function listRunning(options: Kernel.IOptions = {}): Promise<Kernel.IModel[]> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, KERNEL_SERVICE_URL);
     let ajaxSettings: IAjaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'GET';
@@ -1179,7 +1179,7 @@ namespace Private {
   export
   function startNew(options?: Kernel.IOptions): Promise<Kernel.IKernel> {
     options = options || {};
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, KERNEL_SERVICE_URL);
     let ajaxSettings: IAjaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'POST';
@@ -1237,7 +1237,7 @@ namespace Private {
    */
   export
   function shutdown(id: string, options: Kernel.IOptions = {}): Promise<void> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     return shutdownKernel(id, baseUrl, ajaxSettings);
   }
@@ -1341,7 +1341,7 @@ namespace Private {
   export
   function getKernelModel(id: string, options?: Kernel.IOptions): Promise<Kernel.IModel> {
     options = options || {};
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, KERNEL_SERVICE_URL,
                                 encodeURIComponent(id));
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);

+ 7 - 3
packages/services/src/kernel/manager.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
+
 import {
   ArrayExt, IIterator, iter
 } from '@phosphor/algorithm';
@@ -36,9 +40,9 @@ class KernelManager implements Kernel.IManager {
    * @param options - The default options for kernel.
    */
   constructor(options: Kernel.IOptions = {}) {
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
-    this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
-    this._token = options.token || utils.getConfigOption('token');
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    this._wsUrl = options.wsUrl || PageConfig.getWsUrl(this._baseUrl);
+    this._token = options.token || PageConfig.getOption('token');
     this._ajaxSettings = JSON.stringify(utils.ajaxSettingsWithToken(options.ajaxSettings, options.token));
 
     // Initialize internal data.

+ 7 - 3
packages/services/src/manager.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
+
 import {
   JSONObject
 } from '@phosphor/coreutils';
@@ -30,7 +34,7 @@ import {
 } from './terminal';
 
 import {
-  IAjaxSettings, getBaseUrl, getWsUrl, ajaxSettingsWithToken
+  IAjaxSettings, ajaxSettingsWithToken
 } from './utils';
 
 
@@ -44,8 +48,8 @@ class ServiceManager implements ServiceManager.IManager {
    */
   constructor(options?: ServiceManager.IOptions) {
     options = options || {};
-    options.wsUrl = options.wsUrl || getWsUrl();
-    options.baseUrl = options.baseUrl || getBaseUrl();
+    options.wsUrl = options.wsUrl || PageConfig.getWsUrl();
+    options.baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     options.ajaxSettings = ajaxSettingsWithToken(options.ajaxSettings, options.token);
     this._sessionManager = new SessionManager(options);
     this._contentsManager = new ContentsManager(options);

+ 8 - 8
packages/services/src/session/default.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
  import {
-   URLExt, uuid
+   PageConfig, URLExt, uuid
  } from '@jupyterlab/coreutils';
 
 import {
@@ -55,12 +55,12 @@ class DefaultSession implements Session.ISession {
   constructor(options: Session.IOptions, id: string, kernel: Kernel.IKernel) {
     this._id = id;
     this._path = options.path;
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     this._uuid = uuid();
     this._ajaxSettings = JSON.stringify(
       utils.ajaxSettingsWithToken(options.ajaxSettings || {}, options.token)
     );
-    this._token = options.token || utils.getConfigOption('token');
+    this._token = options.token || PageConfig.getOption('token');
     Private.runningSessions.push(this);
     this.setupKernel(kernel);
     this._options = JSONExt.deepCopy(options);
@@ -483,7 +483,7 @@ namespace Private {
   function createKernel(options: Session.IOptions): Promise<Kernel.IKernel> {
     let kernelOptions: Kernel.IOptions = {
       name: options.kernelName || '',
-      baseUrl: options.baseUrl || utils.getBaseUrl(),
+      baseUrl: options.baseUrl || PageConfig.getBaseUrl(),
       wsUrl: options.wsUrl || '',
       username: options.username || '',
       clientId: options.clientId || '',
@@ -554,7 +554,7 @@ namespace Private {
   export
   function getSessionModel(id: string, options?: Session.IOptions): Promise<Session.IModel> {
     options = options || {};
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = getSessionUrl(baseUrl, id);
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'GET';
@@ -600,7 +600,7 @@ namespace Private {
    */
   export
   function listRunning(options: Session.IOptions = {}): Promise<Session.IModel[]> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, SESSION_SERVICE_URL);
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'GET';
@@ -644,7 +644,7 @@ namespace Private {
    */
   export
   function shutdown(id: string, options: Session.IOptions = {}): Promise<void> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     return shutdownSession(id, baseUrl, ajaxSettings);
   }
@@ -697,7 +697,7 @@ namespace Private {
    */
   export
   function startSession(options: Session.IOptions): Promise<Session.IModel> {
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = URLExt.join(baseUrl, SESSION_SERVICE_URL);
     let model = {
       kernel: { name: options.kernelName, id: options.kernelId },

+ 6 - 5
packages/services/src/session/manager.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
+
 import {
   ArrayExt, IIterator, iter
 } from '@phosphor/algorithm';
@@ -17,9 +21,6 @@ import {
   Kernel
 } from '../kernel';
 
-import * as utils
-  from '../utils';
-
 import {
   IAjaxSettings
 } from '../utils';
@@ -40,8 +41,8 @@ class SessionManager implements Session.IManager {
    * @param options - The default options for each session.
    */
   constructor(options: Session.IOptions = {}) {
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
-    this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    this._wsUrl = options.wsUrl || PageConfig.getWsUrl(this._baseUrl);
     this._ajaxSettings = JSON.stringify(options.ajaxSettings || {});
 
     // Initialize internal data.

+ 8 - 8
packages/services/src/terminal/default.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  URLExt
+  PageConfig, URLExt
 } from '@jupyterlab/coreutils';
 
 import {
@@ -45,12 +45,12 @@ class DefaultTerminalSession implements TerminalSession.ISession {
    */
   constructor(name: string, options: TerminalSession.IOptions = {}) {
     this._name = name;
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
-    this._token = options.token || utils.getConfigOption('token');
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    this._token = options.token || PageConfig.getOption('token');
     this._ajaxSettings = JSON.stringify(
       utils.ajaxSettingsWithToken(options.ajaxSettings, this._token)
     );
-    this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
+    this._wsUrl = options.wsUrl || PageConfig.getWsUrl(this._baseUrl);
     this._readyPromise = this._initializeSocket();
     this.terminated = new Signal<this, void>(this);
   }
@@ -251,7 +251,7 @@ namespace DefaultTerminalSession {
    */
   export
   function isAvailable(): boolean {
-    let available = String(utils.getConfigOption('terminalsAvailable'));
+    let available = String(PageConfig.getOption('terminalsAvailable'));
     return available.toLowerCase() === 'true';
   }
 
@@ -267,8 +267,8 @@ namespace DefaultTerminalSession {
     if (!TerminalSession.isAvailable()) {
       throw Private.unavailableMsg;
     }
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
-    let url = Private.getBaseUrl(baseUrl);
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    let url = baseUrl || PageConfig.getBaseUrl();
     let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
     ajaxSettings.method = 'POST';
     ajaxSettings.dataType = 'json';
@@ -305,7 +305,7 @@ namespace DefaultTerminalSession {
     if (!TerminalSession.isAvailable()) {
       return Promise.reject(Private.unavailableMsg);
     }
-    let baseUrl = options.baseUrl || utils.getBaseUrl();
+    let baseUrl = options.baseUrl || PageConfig.getBaseUrl();
     let url = Private.getTermUrl(baseUrl, name);
     if (url in Private.running) {
       return Promise.resolve(Private.running[url]);

+ 6 - 2
packages/services/src/terminal/manager.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
+
 import {
   ArrayExt, IIterator, iter
 } from '@phosphor/algorithm';
@@ -34,8 +38,8 @@ class TerminalManager implements TerminalSession.IManager {
    * Construct a new terminal manager.
    */
   constructor(options: TerminalManager.IOptions = {}) {
-    this._baseUrl = options.baseUrl || utils.getBaseUrl();
-    this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
+    this._baseUrl = options.baseUrl || PageConfig.getBaseUrl();
+    this._wsUrl = options.wsUrl || PageConfig.getWsUrl(this._baseUrl);
     this._ajaxSettings = JSON.stringify(options.ajaxSettings || {});
 
     // Set up state handling if terminals are available.

+ 5 - 106
packages/services/src/utils.ts

@@ -1,17 +1,14 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
-'use strict';
+
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
 
 import {
   JSONExt, JSONObject
 } from '@phosphor/coreutils';
 
-import * as minimist
-  from 'minimist';
-
-import * as path
-  from 'path-posix';
-
 
 // Stub for requirejs.
 declare var requirejs: any;
@@ -279,104 +276,6 @@ function loadObject(name: string, moduleName: string, registry?: { [key: string]
 };
 
 
-/**
- * Global config data for the Jupyter application.
- */
-let configData: any = null;
-
-
-/**
- * Declare a stub for the node process variable.
- */
-declare var process: any;
-
-
-/**
- *  Make an object fully immutable by freezing each object in it.
- */
-function deepFreeze(obj: any): any {
-
-  // Freeze properties before freezing self
-  Object.getOwnPropertyNames(obj).forEach(function(name) {
-    let prop = obj[name];
-
-    // Freeze prop if it is an object
-    if (typeof prop === 'object' && prop !== null && !Object.isFrozen(prop)) {
-      deepFreeze(prop);
-    }
-  });
-
-  // Freeze self
-  return Object.freeze(obj);
-}
-
-
-/**
- * Get global configuration data for the Jupyter application.
- *
- * @param name - The name of the configuration option.
- *
- * @returns The config value or `undefined` if not found.
- *
- * #### Notes
- * For browser based applications, it is assumed that the page HTML
- * includes a script tag with the id `jupyter-config-data` containing the
- * configuration as valid JSON.
- */
-export
-function getConfigOption(name: string): string {
-  if (configData) {
-    return configData[name];
-  }
-  if (typeof document === 'undefined') {
-    configData = minimist(process.argv.slice(2));
-  } else {
-    let el = document.getElementById('jupyter-config-data');
-    if (el) {
-      configData = JSON.parse(el.textContent);
-    } else {
-      configData = {};
-    }
-  }
-  configData = deepFreeze(configData);
-  return configData[name];
-}
-
-
-/**
- * Get the base URL for a Jupyter application.
- */
-export
-function getBaseUrl(): string {
-  let baseUrl = getConfigOption('baseUrl');
-  if (!baseUrl || baseUrl === '/') {
-    baseUrl = (typeof location === 'undefined' ?
-               'http://localhost:8888/' : location.origin + '/');
-  }
-  return baseUrl;
-}
-
-
-/**
- * Get the base websocket URL for a Jupyter application.
- */
-export
-function getWsUrl(baseUrl?: string): string {
-  let wsUrl = getConfigOption('wsUrl');
-  if (!wsUrl) {
-    baseUrl = baseUrl || getBaseUrl();
-    if (baseUrl.indexOf('http') !== 0) {
-      if (typeof location !== 'undefined') {
-        baseUrl = path.join(location.origin, baseUrl);
-      } else {
-        baseUrl = path.join('http://localhost:8888/', baseUrl);
-      }
-    }
-    wsUrl = 'ws' + baseUrl.slice(4);
-  }
-  return wsUrl;
-}
-
 /**
  * Add token to ajaxSettings.requestHeaders if defined.
  * Always returns a copy of ajaxSettings, and a dict.
@@ -389,7 +288,7 @@ function ajaxSettingsWithToken(ajaxSettings?: IAjaxSettings, token?: string): IA
     ajaxSettings = JSONExt.deepCopy(ajaxSettings);
   }
   if (!token) {
-    token = getConfigOption('token');
+    token = PageConfig.getOption('token');
   }
   if (!token || token === '') {
     return ajaxSettings;

+ 2 - 6
packages/services/test/src/kernel/kernel.spec.ts

@@ -4,7 +4,7 @@
 import expect = require('expect.js');
 
 import {
-  uuid
+  PageConfig, uuid
 } from '@jupyterlab/coreutils';
 
 import {
@@ -15,10 +15,6 @@ import {
   JSONObject
 } from '@phosphor/coreutils';
 
-import {
-  getBaseUrl
-} from '../../../lib/utils';
-
 import {
   Kernel, KernelMessage
 } from '../../../lib/kernel';
@@ -434,7 +430,7 @@ describe('kernel', () => {
     context('#baseUrl', () => {
 
       it('should be the base url of the server', () => {
-        expect(kernel.baseUrl).to.be(getBaseUrl());
+        expect(kernel.baseUrl).to.be(PageConfig.getBaseUrl());
       });
 
     });

+ 3 - 3
packages/services/test/src/session/session.spec.ts

@@ -4,7 +4,7 @@
 import expect = require('expect.js');
 
 import {
-  uuid
+  PageConfig, uuid
 } from '@jupyterlab/coreutils';
 
 import {
@@ -12,7 +12,7 @@ import {
 } from '@phosphor/algorithm';
 
 import {
-  IAjaxError, getBaseUrl
+  IAjaxError
 } from '../../../lib/utils';
 
 import {
@@ -487,7 +487,7 @@ describe('session', () => {
     context('#baseUrl', () => {
 
       it('should be the base url of the server', () => {
-        expect(session.baseUrl).to.be(getBaseUrl());
+        expect(session.baseUrl).to.be(PageConfig.getBaseUrl());
       });
 
     });

+ 5 - 5
packages/services/test/src/terminal/terminal.spec.ts

@@ -3,6 +3,10 @@
 
 import expect = require('expect.js');
 
+import {
+  PageConfig
+} from '@jupyterlab/coreutils';
+
 import {
   toArray
 } from '@phosphor/algorithm';
@@ -15,10 +19,6 @@ import {
   TerminalSession
 } from '../../../lib/terminal';
 
-import {
-  getBaseUrl
-} from '../../../lib/utils';
-
 import {
   TerminalTester
 } from '../utils';
@@ -186,7 +186,7 @@ describe('terminals', () => {
     context('#baseUrl', () => {
 
       it('should be the base url of the server', () => {
-        expect(session.baseUrl).to.be(getBaseUrl());
+        expect(session.baseUrl).to.be(PageConfig.getBaseUrl());
       });
 
     });

+ 1 - 29
packages/services/test/src/utils.spec.ts

@@ -5,7 +5,7 @@
 import expect = require('expect.js');
 
 import {
-  getConfigOption, getBaseUrl, getWsUrl, ajaxRequest, loadObject
+  ajaxRequest, loadObject
 } from '../../lib/utils';
 
 import {
@@ -20,34 +20,6 @@ global.requirejs = require('requirejs');
 
 describe('@jupyterlab/services', () => {
 
-  describe('getConfigOption()', () => {
-
-    it('should get a config option passed on the command line', () => {
-      expect(getConfigOption('foo')).to.be('bar');
-    });
-
-    it('should return `undefined` for a option that was not given', () => {
-      expect(getConfigOption('baz')).to.be(void 0);
-    });
-
-  });
-
-  describe('getBaseUrl()', () => {
-
-    it('should get the default base url', () => {
-      expect(getBaseUrl()).to.be('http://localhost:8888/');
-    });
-
-  });
-
-  describe('getWsUrl()', () => {
-
-    it('should get the default ws url', () => {
-      expect(getWsUrl()).to.be('ws://localhost:8888/');
-    });
-
-  });
-
   describe('ajaxRequest()', () => {
 
     it('should handle default values', (done) => {