Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PageConfig.Extension #7216

Merged
merged 1 commit into from Sep 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 60 additions & 10 deletions packages/coreutils/src/pageconfig.ts
Expand Up @@ -14,7 +14,7 @@ declare var process: any;
declare var require: any;

/**
* The namespace for Page Config functions.
* The namespace for `PageConfig` functions.
*/
export namespace PageConfig {
/**
Expand All @@ -37,7 +37,7 @@ export namespace PageConfig {
*/
export function getOption(name: string): string {
if (configData) {
return configData[name] || Private.getBodyData(name);
return configData[name] || getBodyData(name);
}
configData = Object.create(null);
let found = false;
Expand Down Expand Up @@ -85,7 +85,7 @@ export namespace PageConfig {
}
}
}
return configData![name] || Private.getBodyData(name);
return configData![name] || getBodyData(name);
}

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ export namespace PageConfig {
* Get the authorization token for a Jupyter application.
*/
export function getToken(): string {
return getOption('token') || Private.getBodyData('jupyterApiToken');
return getOption('token') || getBodyData('jupyterApiToken');
}

/**
Expand All @@ -175,18 +175,13 @@ export namespace PageConfig {
* Private page config data for the Jupyter application.
*/
let configData: { [key: string]: string } | null = null;
}

/**
* A namespace for module private data.
*/
namespace Private {
/**
* Get a url-encoded item from `body.data` and decode it
* We should never have any encoded URLs anywhere else in code
* until we are building an actual request.
*/
export function getBodyData(key: string): string {
function getBodyData(key: string): string {
if (typeof document === 'undefined' || !document.body) {
return '';
}
Expand All @@ -196,4 +191,59 @@ namespace Private {
}
return decodeURIComponent(val);
}

/**
* The namespace for page config `Extension` functions.
*/
export namespace Extension {
/**
* Populate an array from page config.
*
* @param key - The page config key (e.g., `deferredExtensions`).
*
* #### Notes
* This is intended for `deferredExtensions` and `disabledExtensions`.
*/
function populate(key: string): { raw: string; rule: RegExp }[] {
try {
const raw = getOption(key);
if (raw) {
return JSON.parse(raw).map((pattern: string) => {
return { raw: pattern, rule: new RegExp(pattern) };
});
}
} catch (error) {
console.warn(`Unable to parse ${key}.`, error);
}
return [];
}

/**
* The collection of deferred extensions in page config.
*/
export const deferred = populate('deferredExtensions');

/**
* The collection of disabled extensions in page config.
*/
export const disabled = populate('disabledExtensions');

/**
* Returns whether a plugin is deferred.
*
* @param id - The plugin ID.
*/
export function isDeferred(id: string): boolean {
return deferred.some(val => val.raw === id || val.rule.test(id));
}

/**
* Returns whether a plugin is disabled.
*
* @param id - The plugin ID.
*/
export function isDisabled(id: string): boolean {
return disabled.some(val => val.raw === id || val.rule.test(id));
}
}
}