forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
543 lines (458 loc) · 12.4 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';
import {
JSONObject
} from '@phosphor/coreutils';
import * as minimist
from 'minimist';
import * as path
from 'path-posix';
import * as urlparse
from 'url-parse';
// Stub for requirejs.
declare var requirejs: any;
// Export the Promise Delegate for now to preserve API.
export { PromiseDelegate } from '@phosphor/coreutils';
/**
* Copy the contents of one object to another, recursively.
*
* From [stackoverflow](http://stackoverflow.com/a/12317051).
*/
export
function extend(target: any, source: any): any {
target = target || {};
for (let prop in source) {
if (typeof source[prop] === 'object') {
target[prop] = extend(target[prop], source[prop]);
} else {
target[prop] = source[prop];
}
}
return target;
}
/**
* Get a deep copy of a JSON object.
*/
export
function copy(object: JSONObject): JSONObject {
return JSON.parse(JSON.stringify(object));
}
/**
* Get a random 32 character hex string (not a formal UUID)
*/
export
function uuid(): string {
let s: string[] = [];
let hexDigits = '0123456789abcdef';
let nChars = hexDigits.length;
for (let i = 0; i < 32; i++) {
s[i] = hexDigits.charAt(Math.floor(Math.random() * nChars));
}
return s.join('');
}
/**
* A URL object.
*
* This interface is from the npm package URL object interface. We
* include it here so that downstream libraries do not have to include
* the `url` typing files, since the npm `url` package is not in the
* @types system.
*/
export interface IUrl {
href?: string;
protocol?: string;
hostname?: string;
port?: string;
host?: string;
pathname?: string;
hash?: string;
search?: string;
}
/**
* Parse a url into a URL object.
*
* @param url - The URL string to parse.
*
* @returns A URL object.
*/
export
function urlParse(url: string): IUrl {
if (typeof document !== 'undefined') {
let a = document.createElement('a');
a.href = url;
return a;
}
return urlparse(url);
}
/**
* Join a sequence of url components with `'/'`.
*/
export
function urlPathJoin(...parts: string[]): string {
// Adapted from url-join.
// Copyright (c) 2016 José F. Romaniello, MIT License.
// https://github.com/jfromaniello/url-join/blob/v1.1.0/lib/url-join.js
let str = [].slice.call(parts, 0).join('/');
// make sure protocol is followed by two slashes
str = str.replace(/:\//g, '://');
// remove consecutive slashes
str = str.replace(/([^:\s])\/+/g, '$1/');
// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
// replace ? in parameters with &
str = str.replace(/(\?.+)\?/g, '$1&');
return str;
}
/**
* Encode the components of a multi-segment url.
*
* #### Notes
* Preserves the `'/'` separators.
* Should not include the base url, since all parts are escaped.
*/
export
function urlEncodeParts(uri: string): string {
return urlPathJoin(...uri.split('/').map(encodeURIComponent));
}
/**
* Return a serialized object string suitable for a query.
*
* From [stackoverflow](http://stackoverflow.com/a/30707423).
*/
export
function jsonToQueryString(json: JSONObject): string {
return '?' + Object.keys(json).map(key =>
encodeURIComponent(key) + '=' + encodeURIComponent(String(json[key]))
).join('&');
}
/**
* Input settings for an AJAX request.
*/
export
interface IAjaxSettings extends JSONObject {
/**
* The HTTP method to use. Defaults to `'GET'`.
*/
method?: string;
/**
* The return data type (used to parse the return data).
*/
dataType?: string;
/**
* The outgoing content type, used to set the `Content-Type` header.
*/
contentType?: string;
/**
* The request data.
*/
data?: any;
/**
* Whether to cache the response. Defaults to `true`.
*/
cache?: boolean;
/**
* The number of milliseconds a request can take before automatically
* being terminated. A value of 0 (which is the default) means there is
* no timeout.
*/
timeout?: number;
/**
* A mapping of request headers, used via `setRequestHeader`.
*/
requestHeaders?: { [key: string]: string; };
/**
* Is a Boolean that indicates whether or not cross-site Access-Control
* requests should be made using credentials such as cookies or
* authorization headers. Defaults to `false`.
*/
withCredentials?: boolean;
/**
* The user name associated with the request. Defaults to `''`.
*/
user?: string;
/**
* The password associated with the request. Defaults to `''`.
*/
password?: string;
}
/**
* Data for a successful AJAX request.
*/
export
interface IAjaxSuccess {
/**
* The `onload` event.
*/
readonly event: ProgressEvent;
/**
* The XHR object.
*/
readonly xhr: XMLHttpRequest;
/**
* The ajax settings associated with the request.
*/
readonly ajaxSettings: IAjaxSettings;
/**
* The data returned by the ajax call.
*/
readonly data: any;
}
/**
* Data for an unsuccesful AJAX request.
*/
export
interface IAjaxError {
/**
* The event triggering the error.
*/
readonly event: Event;
/**
* The XHR object.
*/
readonly xhr: XMLHttpRequest;
/**
* The ajax settings associated with the request.
*/
readonly ajaxSettings: IAjaxSettings;
/**
* The error message, if `onerror`.
*/
readonly throwError?: string;
}
function _getCookie(name: string) {
// from tornado docs: http://www.tornadoweb.org/en/stable/guide/security.html
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : void 0;
}
/**
* Asynchronous XMLHTTPRequest handler.
*
* @param url - The url to request.
*
* @param settings - The settings to apply to the request and response.
*
* #### Notes
* Based on this [example](http://www.html5rocks.com/en/tutorials/es6/promises/#toc-promisifying-xmlhttprequest).
*/
export
function ajaxRequest(url: string, ajaxSettings: IAjaxSettings): Promise<IAjaxSuccess> {
let method = ajaxSettings.method || 'GET';
// Ensure that requests have applied data.
if (!ajaxSettings.data) {
ajaxSettings.data = '{}';
ajaxSettings.contentType = 'application/json';
}
let user = ajaxSettings.user || '';
let password = ajaxSettings.password || '';
let headers = ajaxSettings.requestHeaders || {};
if (!ajaxSettings.cache) {
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache.
url += ((/\?/).test(url) ? '&' : '?') + (new Date()).getTime();
}
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(method, url, true, user, password);
if (ajaxSettings.contentType !== void 0) {
xhr.setRequestHeader('Content-Type', ajaxSettings.contentType);
}
if (ajaxSettings.timeout !== void 0) {
xhr.timeout = ajaxSettings.timeout;
}
if (!!ajaxSettings.withCredentials) {
xhr.withCredentials = true;
}
// Try to add the xsrf token if there is no existing authorization.
let token = headers['Authorization'];
if (!token && typeof document !== 'undefined' && document.cookie) {
let xsrfToken = _getCookie('_xsrf');
if (xsrfToken !== void 0) {
xhr.setRequestHeader('X-XSRFToken', xsrfToken);
}
}
for (let prop in headers) {
xhr.setRequestHeader(prop, headers[prop]);
}
xhr.onload = (event: ProgressEvent) => {
if (xhr.status >= 300) {
reject({ event, xhr, ajaxSettings, throwError: xhr.statusText });
}
let data = xhr.responseText;
try {
data = JSON.parse(data);
} catch (err) {
// no-op
}
resolve({ xhr, ajaxSettings, data, event });
};
xhr.onabort = (event: Event) => {
reject({ xhr, event, ajaxSettings });
};
xhr.onerror = (event: ErrorEvent) => {
reject({ xhr, event, ajaxSettings });
};
xhr.ontimeout = (ev: ProgressEvent) => {
reject({ xhr, event, ajaxSettings });
};
if (ajaxSettings.data) {
xhr.send(ajaxSettings.data);
} else {
xhr.send();
}
});
}
/**
* Create an ajax error from an ajax success.
*
* @param success - The original success object.
*
* @param throwError - The optional new error name. If not given
* we use "Invalid Status: <xhr.status>"
*/
export
function makeAjaxError(success: IAjaxSuccess, throwError?: string): IAjaxError {
let xhr = success.xhr;
let ajaxSettings = success.ajaxSettings;
let event = success.event;
throwError = throwError || `Invalid Status: ${xhr.status}`;
return { xhr, ajaxSettings, event, throwError };
}
/**
* Try to load an object from a module or a registry.
*
* Try to load an object from a module asynchronously if a module
* is specified, otherwise tries to load an object from the global
* registry, if the global registry is provided.
*/
export
function loadObject(name: string, moduleName: string, registry?: { [key: string]: any }): Promise<any> {
return new Promise((resolve, reject) => {
// Try loading the view module using require.js
if (moduleName) {
if (typeof requirejs === 'undefined') {
throw new Error('requirejs not found');
}
requirejs([moduleName], (mod: any) => {
if (mod[name] === void 0) {
let msg = `Object '${name}' not found in module '${moduleName}'`;
reject(new Error(msg));
} else {
resolve(mod[name]);
}
}, reject);
} else {
if (registry && registry[name]) {
resolve(registry[name]);
} else {
reject(new Error(`Object '${name}' not found in registry`));
}
}
});
};
/**
* 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.
*/
export
function ajaxSettingsWithToken(ajaxSettings?: IAjaxSettings, token?: string): IAjaxSettings {
if (!ajaxSettings) {
ajaxSettings = {};
} else {
ajaxSettings = copy(ajaxSettings);
}
if (!token) {
token = getConfigOption('token');
}
if (!token || token === '') {
return ajaxSettings;
}
if (!ajaxSettings.requestHeaders) {
ajaxSettings.requestHeaders = {};
}
ajaxSettings.requestHeaders['Authorization'] = `token ${token}`;
return ajaxSettings;
}