UNPKG

16.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5// Copyright (c) Jupyter Development Team.
6// Distributed under the terms of the Modified BSD License.
7/*-----------------------------------------------------------------------------
8| Copyright (c) 2014-2017, PhosphorJS Contributors
9|
10| Distributed under the terms of the BSD 3-Clause License.
11|
12| The full license is in the file LICENSE, distributed with this software.
13|----------------------------------------------------------------------------*/
14/**
15 * The namespace for JSON-specific functions.
16 */
17exports.JSONExt = void 0;
18(function (JSONExt) {
19 /**
20 * A shared frozen empty JSONObject
21 */
22 JSONExt.emptyObject = Object.freeze({});
23 /**
24 * A shared frozen empty JSONArray
25 */
26 JSONExt.emptyArray = Object.freeze([]);
27 /**
28 * Test whether a JSON value is a primitive.
29 *
30 * @param value - The JSON value of interest.
31 *
32 * @returns `true` if the value is a primitive,`false` otherwise.
33 */
34 function isPrimitive(value) {
35 return (value === null ||
36 typeof value === 'boolean' ||
37 typeof value === 'number' ||
38 typeof value === 'string');
39 }
40 JSONExt.isPrimitive = isPrimitive;
41 function isArray(value) {
42 return Array.isArray(value);
43 }
44 JSONExt.isArray = isArray;
45 function isObject(value) {
46 return !isPrimitive(value) && !isArray(value);
47 }
48 JSONExt.isObject = isObject;
49 /**
50 * Compare two JSON values for deep equality.
51 *
52 * @param first - The first JSON value of interest.
53 *
54 * @param second - The second JSON value of interest.
55 *
56 * @returns `true` if the values are equivalent, `false` otherwise.
57 */
58 function deepEqual(first, second) {
59 // Check referential and primitive equality first.
60 if (first === second) {
61 return true;
62 }
63 // If one is a primitive, the `===` check ruled out the other.
64 if (isPrimitive(first) || isPrimitive(second)) {
65 return false;
66 }
67 // Test whether they are arrays.
68 var a1 = isArray(first);
69 var a2 = isArray(second);
70 // Bail if the types are different.
71 if (a1 !== a2) {
72 return false;
73 }
74 // If they are both arrays, compare them.
75 if (a1 && a2) {
76 return deepArrayEqual(first, second);
77 }
78 // At this point, they must both be objects.
79 return deepObjectEqual(first, second);
80 }
81 JSONExt.deepEqual = deepEqual;
82 /**
83 * Create a deep copy of a JSON value.
84 *
85 * @param value - The JSON value to copy.
86 *
87 * @returns A deep copy of the given JSON value.
88 */
89 function deepCopy(value) {
90 // Do nothing for primitive values.
91 if (isPrimitive(value)) {
92 return value;
93 }
94 // Deep copy an array.
95 if (isArray(value)) {
96 return deepArrayCopy(value);
97 }
98 // Deep copy an object.
99 return deepObjectCopy(value);
100 }
101 JSONExt.deepCopy = deepCopy;
102 /**
103 * Compare two JSON arrays for deep equality.
104 */
105 function deepArrayEqual(first, second) {
106 // Check referential equality first.
107 if (first === second) {
108 return true;
109 }
110 // Test the arrays for equal length.
111 if (first.length !== second.length) {
112 return false;
113 }
114 // Compare the values for equality.
115 for (var i = 0, n = first.length; i < n; ++i) {
116 if (!deepEqual(first[i], second[i])) {
117 return false;
118 }
119 }
120 // At this point, the arrays are equal.
121 return true;
122 }
123 /**
124 * Compare two JSON objects for deep equality.
125 */
126 function deepObjectEqual(first, second) {
127 // Check referential equality first.
128 if (first === second) {
129 return true;
130 }
131 // Check for the first object's keys in the second object.
132 for (var key in first) {
133 if (first[key] !== undefined && !(key in second)) {
134 return false;
135 }
136 }
137 // Check for the second object's keys in the first object.
138 for (var key in second) {
139 if (second[key] !== undefined && !(key in first)) {
140 return false;
141 }
142 }
143 // Compare the values for equality.
144 for (var key in first) {
145 // Get the values.
146 var firstValue = first[key];
147 var secondValue = second[key];
148 // If both are undefined, ignore the key.
149 if (firstValue === undefined && secondValue === undefined) {
150 continue;
151 }
152 // If only one value is undefined, the objects are not equal.
153 if (firstValue === undefined || secondValue === undefined) {
154 return false;
155 }
156 // Compare the values.
157 if (!deepEqual(firstValue, secondValue)) {
158 return false;
159 }
160 }
161 // At this point, the objects are equal.
162 return true;
163 }
164 /**
165 * Create a deep copy of a JSON array.
166 */
167 function deepArrayCopy(value) {
168 var result = new Array(value.length);
169 for (var i = 0, n = value.length; i < n; ++i) {
170 result[i] = deepCopy(value[i]);
171 }
172 return result;
173 }
174 /**
175 * Create a deep copy of a JSON object.
176 */
177 function deepObjectCopy(value) {
178 var result = {};
179 for (var key in value) {
180 // Ignore undefined values.
181 var subvalue = value[key];
182 if (subvalue === undefined) {
183 continue;
184 }
185 result[key] = deepCopy(subvalue);
186 }
187 return result;
188 }
189})(exports.JSONExt || (exports.JSONExt = {}));
190
191// Copyright (c) Jupyter Development Team.
192// Distributed under the terms of the Modified BSD License.
193/*-----------------------------------------------------------------------------
194| Copyright (c) 2014-2017, PhosphorJS Contributors
195|
196| Distributed under the terms of the BSD 3-Clause License.
197|
198| The full license is in the file LICENSE, distributed with this software.
199|----------------------------------------------------------------------------*/
200/**
201 * An object which stores MIME data for general application use.
202 *
203 * #### Notes
204 * This class does not attempt to enforce "correctness" of MIME types
205 * and their associated data. Since this class is designed to transfer
206 * arbitrary data and objects within the same application, it assumes
207 * that the user provides correct and accurate data.
208 */
209var MimeData = /** @class */ (function () {
210 function MimeData() {
211 this._types = [];
212 this._values = [];
213 }
214 /**
215 * Get an array of the MIME types contained within the dataset.
216 *
217 * @returns A new array of the MIME types, in order of insertion.
218 */
219 MimeData.prototype.types = function () {
220 return this._types.slice();
221 };
222 /**
223 * Test whether the dataset has an entry for the given type.
224 *
225 * @param mime - The MIME type of interest.
226 *
227 * @returns `true` if the dataset contains a value for the given
228 * MIME type, `false` otherwise.
229 */
230 MimeData.prototype.hasData = function (mime) {
231 return this._types.indexOf(mime) !== -1;
232 };
233 /**
234 * Get the data value for the given MIME type.
235 *
236 * @param mime - The MIME type of interest.
237 *
238 * @returns The value for the given MIME type, or `undefined` if
239 * the dataset does not contain a value for the type.
240 */
241 MimeData.prototype.getData = function (mime) {
242 var i = this._types.indexOf(mime);
243 return i !== -1 ? this._values[i] : undefined;
244 };
245 /**
246 * Set the data value for the given MIME type.
247 *
248 * @param mime - The MIME type of interest.
249 *
250 * @param data - The data value for the given MIME type.
251 *
252 * #### Notes
253 * This will overwrite any previous entry for the MIME type.
254 */
255 MimeData.prototype.setData = function (mime, data) {
256 this.clearData(mime);
257 this._types.push(mime);
258 this._values.push(data);
259 };
260 /**
261 * Remove the data entry for the given MIME type.
262 *
263 * @param mime - The MIME type of interest.
264 *
265 * #### Notes
266 * This is a no-op if there is no entry for the given MIME type.
267 */
268 MimeData.prototype.clearData = function (mime) {
269 var i = this._types.indexOf(mime);
270 if (i !== -1) {
271 this._types.splice(i, 1);
272 this._values.splice(i, 1);
273 }
274 };
275 /**
276 * Remove all data entries from the dataset.
277 */
278 MimeData.prototype.clear = function () {
279 this._types.length = 0;
280 this._values.length = 0;
281 };
282 return MimeData;
283}());
284
285// Copyright (c) Jupyter Development Team.
286// Distributed under the terms of the Modified BSD License.
287/*-----------------------------------------------------------------------------
288| Copyright (c) 2014-2017, PhosphorJS Contributors
289|
290| Distributed under the terms of the BSD 3-Clause License.
291|
292| The full license is in the file LICENSE, distributed with this software.
293|----------------------------------------------------------------------------*/
294/**
295 * A class which wraps a promise into a delegate object.
296 *
297 * #### Notes
298 * This class is useful when the logic to resolve or reject a promise
299 * cannot be defined at the point where the promise is created.
300 */
301var PromiseDelegate = /** @class */ (function () {
302 /**
303 * Construct a new promise delegate.
304 */
305 function PromiseDelegate() {
306 var _this = this;
307 this.promise = new Promise(function (resolve, reject) {
308 _this._resolve = resolve;
309 _this._reject = reject;
310 });
311 }
312 /**
313 * Resolve the wrapped promise with the given value.
314 *
315 * @param value - The value to use for resolving the promise.
316 */
317 PromiseDelegate.prototype.resolve = function (value) {
318 var resolve = this._resolve;
319 resolve(value);
320 };
321 /**
322 * Reject the wrapped promise with the given value.
323 *
324 * @reason - The reason for rejecting the promise.
325 */
326 PromiseDelegate.prototype.reject = function (reason) {
327 var reject = this._reject;
328 reject(reason);
329 };
330 return PromiseDelegate;
331}());
332
333// Copyright (c) Jupyter Development Team.
334// Distributed under the terms of the Modified BSD License.
335/*-----------------------------------------------------------------------------
336| Copyright (c) 2014-2017, PhosphorJS Contributors
337|
338| Distributed under the terms of the BSD 3-Clause License.
339|
340| The full license is in the file LICENSE, distributed with this software.
341|----------------------------------------------------------------------------*/
342/**
343 * A runtime object which captures compile-time type information.
344 *
345 * #### Notes
346 * A token captures the compile-time type of an interface or class in
347 * an object which can be used at runtime in a type-safe fashion.
348 */
349var Token = /** @class */ (function () {
350 /**
351 * Construct a new token.
352 *
353 * @param name - A human readable name for the token.
354 */
355 function Token(name) {
356 this.name = name;
357 this._tokenStructuralPropertyT = null;
358 }
359 return Token;
360}());
361
362// Copyright (c) Jupyter Development Team.
363// Distributed under the terms of the Modified BSD License.
364/*-----------------------------------------------------------------------------
365| Copyright (c) 2014-2017, PhosphorJS Contributors
366|
367| Distributed under the terms of the BSD 3-Clause License.
368|
369| The full license is in the file LICENSE, distributed with this software.
370|----------------------------------------------------------------------------*/
371// Fallback
372function fallbackRandomValues(buffer) {
373 var value = 0;
374 for (var i = 0, n = buffer.length; i < n; ++i) {
375 if (i % 4 === 0) {
376 value = (Math.random() * 0xffffffff) >>> 0;
377 }
378 buffer[i] = value & 0xff;
379 value >>>= 8;
380 }
381}
382
383// Copyright (c) Jupyter Development Team.
384/**
385 * The namespace for random number related functionality.
386 */
387exports.Random = void 0;
388(function (Random) {
389 /**
390 * A function which generates random bytes.
391 *
392 * @param buffer - The `Uint8Array` to fill with random bytes.
393 *
394 * #### Notes
395 * A cryptographically strong random number generator will be used if
396 * available. Otherwise, `Math.random` will be used as a fallback for
397 * randomness.
398 *
399 * The following RNGs are supported, listed in order of precedence:
400 * - `window.crypto.getRandomValues`
401 * - `window.msCrypto.getRandomValues`
402 * - `require('crypto').randomFillSync
403 * - `require('crypto').randomBytes
404 * - `Math.random`
405 */
406 Random.getRandomValues = (function () {
407 // Look up the crypto module if available.
408 var crypto = (typeof require !== 'undefined' && require('crypto')) || null;
409 // Node 7+
410 if (crypto && typeof crypto.randomFillSync === 'function') {
411 return function getRandomValues(buffer) {
412 return crypto.randomFillSync(buffer);
413 };
414 }
415 // Node 0.10+
416 if (crypto && typeof crypto.randomBytes === 'function') {
417 return function getRandomValues(buffer) {
418 var bytes = crypto.randomBytes(buffer.length);
419 for (var i = 0, n = bytes.length; i < n; ++i) {
420 buffer[i] = bytes[i];
421 }
422 };
423 }
424 // Fallback
425 return fallbackRandomValues;
426 })();
427})(exports.Random || (exports.Random = {}));
428
429// Copyright (c) Jupyter Development Team.
430// Distributed under the terms of the Modified BSD License.
431/*-----------------------------------------------------------------------------
432| Copyright (c) 2014-2017, PhosphorJS Contributors
433|
434| Distributed under the terms of the BSD 3-Clause License.
435|
436| The full license is in the file LICENSE, distributed with this software.
437|----------------------------------------------------------------------------*/
438/**
439 * A function which creates a function that generates UUID v4 identifiers.
440 *
441 * @returns A new function that creates a UUID v4 string.
442 *
443 * #### Notes
444 * This implementation complies with RFC 4122.
445 *
446 * This uses `Random.getRandomValues()` for random bytes, which in
447 * turn will use the underlying `crypto` module of the platform if
448 * it is available. The fallback for randomness is `Math.random`.
449 */
450function uuid4Factory(getRandomValues) {
451 // Create a 16 byte array to hold the random values.
452 var bytes = new Uint8Array(16);
453 // Create a look up table from bytes to hex strings.
454 var lut = new Array(256);
455 // Pad the single character hex digits with a leading zero.
456 for (var i = 0; i < 16; ++i) {
457 lut[i] = '0' + i.toString(16);
458 }
459 // Populate the rest of the hex digits.
460 for (var i = 16; i < 256; ++i) {
461 lut[i] = i.toString(16);
462 }
463 // Return a function which generates the UUID.
464 return function uuid4() {
465 // Get a new batch of random values.
466 getRandomValues(bytes);
467 // Set the UUID version number to 4.
468 bytes[6] = 0x40 | (bytes[6] & 0x0f);
469 // Set the clock sequence bit to the RFC spec.
470 bytes[8] = 0x80 | (bytes[8] & 0x3f);
471 // Assemble the UUID string.
472 return (lut[bytes[0]] +
473 lut[bytes[1]] +
474 lut[bytes[2]] +
475 lut[bytes[3]] +
476 '-' +
477 lut[bytes[4]] +
478 lut[bytes[5]] +
479 '-' +
480 lut[bytes[6]] +
481 lut[bytes[7]] +
482 '-' +
483 lut[bytes[8]] +
484 lut[bytes[9]] +
485 '-' +
486 lut[bytes[10]] +
487 lut[bytes[11]] +
488 lut[bytes[12]] +
489 lut[bytes[13]] +
490 lut[bytes[14]] +
491 lut[bytes[15]]);
492 };
493}
494
495// Copyright (c) Jupyter Development Team.
496/**
497 * The namespace for UUID related functionality.
498 */
499exports.UUID = void 0;
500(function (UUID) {
501 /**
502 * A function which generates UUID v4 identifiers.
503 *
504 * @returns A new UUID v4 string.
505 *
506 * #### Notes
507 * This implementation complies with RFC 4122.
508 *
509 * This uses `Random.getRandomValues()` for random bytes, which in
510 * turn will use the underlying `crypto` module of the platform if
511 * it is available. The fallback for randomness is `Math.random`.
512 */
513 UUID.uuid4 = uuid4Factory(exports.Random.getRandomValues);
514})(exports.UUID || (exports.UUID = {}));
515
516exports.MimeData = MimeData;
517exports.PromiseDelegate = PromiseDelegate;
518exports.Token = Token;
519//# sourceMappingURL=index.node.js.map