UNPKG

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