utils.js
8.09 KB
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
"use strict";
const path = require("path");
const URL = require("whatwg-url-compat").createURLConstructor();
const domSymbolTree = require("./living/helpers/internal-constants").domSymbolTree;
const SYMBOL_TREE_POSITION = require("symbol-tree").TreePosition;
exports.URL = URL;
exports.toFileUrl = function (fileName) {
// Beyond just the `path.resolve`, this is mostly for the benefit of Windows,
// where we need to convert "\" to "/" and add an extra "/" prefix before the
// drive letter.
let pathname = path.resolve(process.cwd(), fileName).replace(/\\/g, "/");
if (pathname[0] !== "/") {
pathname = "/" + pathname;
}
// path might contain spaces, so convert those to %20
return "file://" + encodeURI(pathname);
};
/**
* Define a setter on an object
*
* This method replaces any existing setter but leaves getters in place.
*
* - `object` {Object} the object to define the setter on
* - `property` {String} the name of the setter
* - `setterFn` {Function} the setter
*/
exports.defineSetter = function defineSetter(object, property, setterFn) {
const descriptor = Object.getOwnPropertyDescriptor(object, property) || {
configurable: true,
enumerable: true
};
descriptor.set = setterFn;
Object.defineProperty(object, property, descriptor);
};
/**
* Define a getter on an object
*
* This method replaces any existing getter but leaves setters in place.
*
* - `object` {Object} the object to define the getter on
* - `property` {String} the name of the getter
* - `getterFn` {Function} the getter
*/
exports.defineGetter = function defineGetter(object, property, getterFn) {
const descriptor = Object.getOwnPropertyDescriptor(object, property) || {
configurable: true,
enumerable: true
};
descriptor.get = getterFn;
Object.defineProperty(object, property, descriptor);
};
/**
* Create an object with the given prototype
*
* Optionally augment the created object.
*
* - `prototype` {Object} the created object's prototype
* - `[properties]` {Object} properties to attach to the created object
*/
exports.createFrom = function createFrom(prototype, properties) {
properties = properties || {};
const descriptors = {};
for (const name of Object.getOwnPropertyNames(properties)) {
descriptors[name] = Object.getOwnPropertyDescriptor(properties, name);
}
for (const symbol of Object.getOwnPropertySymbols(properties)) {
descriptors[symbol] = Object.getOwnPropertyDescriptor(properties, symbol);
}
return Object.create(prototype, descriptors);
};
/**
* Create an inheritance relationship between two classes
*
* Optionally augment the inherited prototype.
*
* - `Superclass` {Function} the inherited class
* - `Subclass` {Function} the inheriting class
* - `[properties]` {Object} properties to attach to the inherited prototype
*/
exports.inheritFrom = function inheritFrom(Superclass, Subclass, properties) {
properties = properties || {};
Object.defineProperty(properties, "constructor", {
value: Subclass,
writable: true,
configurable: true
});
Subclass.prototype = exports.createFrom(Superclass.prototype, properties);
};
/**
* Define a set of properties on an object, by copying the property descriptors
* from the original object.
*
* - `object` {Object} the target object
* - `properties` {Object} the source from which to copy property descriptors
*/
exports.define = function define(object, properties) {
for (const name of Object.getOwnPropertyNames(properties)) {
const propDesc = Object.getOwnPropertyDescriptor(properties, name);
Object.defineProperty(object, name, propDesc);
}
};
/**
* Define a list of constants on a constructor and its .prototype
*
* - `Constructor` {Function} the constructor to define the constants on
* - `propertyMap` {Object} key/value map of properties to define
*/
exports.addConstants = function addConstants(Constructor, propertyMap) {
for (const property in propertyMap) {
const value = propertyMap[property];
addConstant(Constructor, property, value);
addConstant(Constructor.prototype, property, value);
}
};
function addConstant(object, property, value) {
Object.defineProperty(object, property, {
configurable: false,
enumerable: true,
writable: false,
value
});
}
let memoizeQueryTypeCounter = 0;
/**
* Returns a version of a method that memoizes specific types of calls on the object
*
* - `fn` {Function} the method to be memozied
*/
exports.memoizeQuery = function memoizeQuery(fn) {
// Only memoize query functions with arity <= 2
if (fn.length > 2) {
return fn;
}
const type = memoizeQueryTypeCounter++;
return function () {
if (!this._memoizedQueries) {
return fn.apply(this, arguments);
}
if (!this._memoizedQueries[type]) {
this._memoizedQueries[type] = Object.create(null);
}
let key;
if (arguments.length === 1 && typeof arguments[0] === "string") {
key = arguments[0];
} else if (arguments.length === 2 && typeof arguments[0] === "string" && typeof arguments[1] === "string") {
key = arguments[0] + "::" + arguments[1];
} else {
return fn.apply(this, arguments);
}
if (!(key in this._memoizedQueries[type])) {
this._memoizedQueries[type][key] = fn.apply(this, arguments);
}
return this._memoizedQueries[type][key];
};
};
exports.resolveHref = function resolveHref(baseUrl, href) {
try {
return new URL(href, baseUrl).href;
} catch (e) {
// can't throw since this utility is basically used everywhere
// do what the spec says regarding anchor tags: just don't parse it
// https://url.spec.whatwg.org/#dom-urlutils-href
return href;
}
};
exports.mapper = function (parent, filter, recursive) {
function skipRoot(node) {
return node !== parent && (!filter || filter(node));
}
return () => {
if (recursive !== false) { // default is not recursive
return domSymbolTree.treeToArray(parent, { filter: skipRoot });
}
return domSymbolTree.childrenToArray(parent, { filter });
};
};
function isValidAbsoluteURL(str) {
try {
/* eslint-disable no-new */
new URL(str);
/* eslint-enable no-new */
// If we can parse it, it's a valid absolute URL.
return true;
} catch (e) {
return false;
}
}
exports.isValidTargetOrigin = function (str) {
return str === "*" || str === "/" || isValidAbsoluteURL(str);
};
exports.simultaneousIterators = function* (first, second) {
for (;;) {
const firstResult = first.next();
const secondResult = second.next();
if (firstResult.done && secondResult.done) {
return;
}
yield [
firstResult.done ? null : firstResult.value,
secondResult.done ? null : secondResult.value
];
}
};
exports.treeOrderSorter = function (a, b) {
const compare = domSymbolTree.compareTreePosition(a, b);
if (compare & SYMBOL_TREE_POSITION.PRECEDING) { // b is preceding a
return 1;
}
if (compare & SYMBOL_TREE_POSITION.FOLLOWING) {
return -1;
}
// disconnected or equal:
return 0;
};
exports.lengthFromProperties = function (arrayLike) {
let max = -1;
const keys = Object.keys(arrayLike);
const highestKeyIndex = keys.length - 1;
// Abuses a v8 implementation detail for a very fast case
// (if this implementation detail changes, this method will still
// return correct results)
/* eslint-disable eqeqeq */
if (highestKeyIndex == keys[highestKeyIndex]) { // not ===
/* eslint-enable eqeqeq */
return keys.length;
}
for (let i = highestKeyIndex; i >= 0; --i) {
const asNumber = Number(keys[i]);
if (!Number.isNaN(asNumber) && asNumber > max) {
max = asNumber;
}
}
return max + 1;
};
const base64Regexp = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;
exports.parseDataUrl = function parseDataUrl(url) {
const urlParts = url.match(/^data:(.+?)(?:;(base64))?,(.*)$/);
let buffer;
if (urlParts[2] === "base64") {
if (urlParts[3] && !base64Regexp.test(urlParts[3])) {
throw new Error("Not a base64 string");
}
buffer = new Buffer(urlParts[3], "base64");
} else {
buffer = new Buffer(urlParts[3]);
}
return { buffer, type: urlParts[1] };
};