@percy/core 1.0.0-beta.76 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -18
- package/package.json +24 -14
- package/dist/api.js +0 -76
- package/dist/browser.js +0 -363
- package/dist/config.js +0 -358
- package/dist/discovery.js +0 -130
- package/dist/index.js +0 -15
- package/dist/install.js +0 -173
- package/dist/network.js +0 -365
- package/dist/page.js +0 -293
- package/dist/percy.js +0 -427
- package/dist/queue.js +0 -196
- package/dist/server.js +0 -471
- package/dist/session.js +0 -140
- package/dist/snapshot.js +0 -279
- package/dist/utils.js +0 -170
- package/post-install.js +0 -23
- package/test/helpers/server.js +0 -35
- package/types/index.d.ts +0 -101
package/dist/server.js
DELETED
|
@@ -1,471 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.ServerResponse = exports.ServerError = exports.Server = exports.IncomingMessage = void 0;
|
|
7
|
-
|
|
8
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
|
-
var _path = _interopRequireDefault(require("path"));
|
|
11
|
-
|
|
12
|
-
var _http = _interopRequireDefault(require("http"));
|
|
13
|
-
|
|
14
|
-
var _ws = _interopRequireDefault(require("ws"));
|
|
15
|
-
|
|
16
|
-
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
|
17
|
-
|
|
18
|
-
var _contentDisposition = _interopRequireDefault(require("content-disposition"));
|
|
19
|
-
|
|
20
|
-
var _pathToRegexp = require("path-to-regexp");
|
|
21
|
-
|
|
22
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
|
-
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
25
|
-
|
|
26
|
-
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
27
|
-
|
|
28
|
-
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
29
|
-
|
|
30
|
-
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
31
|
-
|
|
32
|
-
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
33
|
-
|
|
34
|
-
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
35
|
-
|
|
36
|
-
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
37
|
-
|
|
38
|
-
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
39
|
-
|
|
40
|
-
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
|
41
|
-
|
|
42
|
-
// custom incoming message adds a `url` and `body` properties containing the parsed URL and message
|
|
43
|
-
// buffer respectively; both available after the 'end' event is emitted
|
|
44
|
-
class IncomingMessage extends _http.default.IncomingMessage {
|
|
45
|
-
constructor(socket) {
|
|
46
|
-
let buffer = [];
|
|
47
|
-
super(socket).on('data', d => buffer.push(d)).on('end', () => {
|
|
48
|
-
var _this$headers$content;
|
|
49
|
-
|
|
50
|
-
this.url = new URL(this.url, `http://${this.headers.host}`);
|
|
51
|
-
if (buffer.length) this.body = Buffer.concat(buffer);
|
|
52
|
-
|
|
53
|
-
if (this.body && (_this$headers$content = this.headers['content-type']) !== null && _this$headers$content !== void 0 && _this$headers$content.includes('json')) {
|
|
54
|
-
try {
|
|
55
|
-
this.body = JSON.parse(this.body);
|
|
56
|
-
} catch {}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
} // custom server response adds additional convenience methods
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
exports.IncomingMessage = IncomingMessage;
|
|
65
|
-
|
|
66
|
-
class ServerResponse extends _http.default.ServerResponse {
|
|
67
|
-
// responds with a status, headers, and body; the second argument can be an content-type string,
|
|
68
|
-
// or a headers object, with content-length being automatically set when a `body` is provided
|
|
69
|
-
send(status, headers, body) {
|
|
70
|
-
if (typeof headers === 'string') {
|
|
71
|
-
this.setHeader('Content-Type', headers);
|
|
72
|
-
headers = null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (body != null && !this.hasHeader('Content-Length')) {
|
|
76
|
-
this.setHeader('Content-Length', Buffer.byteLength(body));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return this.writeHead(status, headers).end(body);
|
|
80
|
-
} // responds with a status and content with a plain/text content-type
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
text(status, content) {
|
|
84
|
-
if (arguments.length < 2) [status, content] = [200, status];
|
|
85
|
-
return this.send(status, 'text/plain', content.toString());
|
|
86
|
-
} // responds with a status and stringified `data` with a json content-type
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
json(status, data) {
|
|
90
|
-
if (arguments.length < 2) [status, data] = [200, status];
|
|
91
|
-
return this.send(status, 'application/json', JSON.stringify(data));
|
|
92
|
-
} // responds with a status and streams a file with appropriate headers
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
file(status, filepath) {
|
|
96
|
-
if (arguments.length < 2) [status, filepath] = [200, status];
|
|
97
|
-
filepath = _path.default.resolve(filepath);
|
|
98
|
-
|
|
99
|
-
let {
|
|
100
|
-
size
|
|
101
|
-
} = _fs.default.lstatSync(filepath);
|
|
102
|
-
|
|
103
|
-
let range = parseByteRange(this.req.headers.range, size); // support simple range requests
|
|
104
|
-
|
|
105
|
-
if (this.req.headers.range) {
|
|
106
|
-
let byteRange = range ? `${range.start}-${range.end}` : '*';
|
|
107
|
-
this.setHeader('Content-Range', `bytes ${byteRange}/${size}`);
|
|
108
|
-
if (!range) return this.send(416);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
this.writeHead(range ? 206 : status, {
|
|
112
|
-
'Accept-Ranges': 'bytes',
|
|
113
|
-
'Content-Type': _mimeTypes.default.contentType(_path.default.extname(filepath)),
|
|
114
|
-
'Content-Length': range ? range.end - range.start + 1 : size,
|
|
115
|
-
'Content-Disposition': (0, _contentDisposition.default)(filepath, {
|
|
116
|
-
type: 'inline'
|
|
117
|
-
})
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
_fs.default.createReadStream(filepath, range).pipe(this);
|
|
121
|
-
|
|
122
|
-
return this;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
} // custom server error with a status and default reason
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
exports.ServerResponse = ServerResponse;
|
|
129
|
-
|
|
130
|
-
class ServerError extends Error {
|
|
131
|
-
static throw(status, reason) {
|
|
132
|
-
throw new this(status, reason);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
constructor(status = 500, reason) {
|
|
136
|
-
super(reason || _http.default.STATUS_CODES[status]);
|
|
137
|
-
this.status = status;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
} // custom server class handles routing requests and provides alternate methods and properties
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
exports.ServerError = ServerError;
|
|
144
|
-
|
|
145
|
-
var _sockets = /*#__PURE__*/new WeakMap();
|
|
146
|
-
|
|
147
|
-
var _defaultPort = /*#__PURE__*/new WeakMap();
|
|
148
|
-
|
|
149
|
-
var _up = /*#__PURE__*/new WeakMap();
|
|
150
|
-
|
|
151
|
-
var _handleUpgrade = /*#__PURE__*/new WeakSet();
|
|
152
|
-
|
|
153
|
-
var _routes = /*#__PURE__*/new WeakMap();
|
|
154
|
-
|
|
155
|
-
var _route = /*#__PURE__*/new WeakSet();
|
|
156
|
-
|
|
157
|
-
var _handleRequest = /*#__PURE__*/new WeakSet();
|
|
158
|
-
|
|
159
|
-
class Server extends _http.default.Server {
|
|
160
|
-
constructor({
|
|
161
|
-
port
|
|
162
|
-
} = {}) {
|
|
163
|
-
super({
|
|
164
|
-
IncomingMessage,
|
|
165
|
-
ServerResponse
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
_classPrivateMethodInitSpec(this, _handleRequest);
|
|
169
|
-
|
|
170
|
-
_classPrivateMethodInitSpec(this, _route);
|
|
171
|
-
|
|
172
|
-
_classPrivateMethodInitSpec(this, _handleUpgrade);
|
|
173
|
-
|
|
174
|
-
_classPrivateFieldInitSpec(this, _sockets, {
|
|
175
|
-
writable: true,
|
|
176
|
-
value: new Set()
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
_classPrivateFieldInitSpec(this, _defaultPort, {
|
|
180
|
-
writable: true,
|
|
181
|
-
value: void 0
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
_classPrivateFieldInitSpec(this, _up, {
|
|
185
|
-
writable: true,
|
|
186
|
-
value: []
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
_classPrivateFieldInitSpec(this, _routes, {
|
|
190
|
-
writable: true,
|
|
191
|
-
value: [{
|
|
192
|
-
priority: -1,
|
|
193
|
-
handle: (req, res, next) => {
|
|
194
|
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
195
|
-
|
|
196
|
-
if (req.method === 'OPTIONS') {
|
|
197
|
-
let allowHeaders = req.headers['access-control-request-headers'] || '*';
|
|
198
|
-
let allowMethods = [...new Set(_classPrivateFieldGet(this, _routes).flatMap(route => (!route.match || route.match(req.url.pathname)) && route.methods || []))].join(', ');
|
|
199
|
-
res.setHeader('Access-Control-Allow-Headers', allowHeaders);
|
|
200
|
-
res.setHeader('Access-Control-Allow-Methods', allowMethods);
|
|
201
|
-
res.writeHead(204).end();
|
|
202
|
-
} else {
|
|
203
|
-
res.setHeader('Access-Control-Expose-Headers', '*');
|
|
204
|
-
return next();
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}, {
|
|
208
|
-
priority: 3,
|
|
209
|
-
handle: req => ServerError.throw(404)
|
|
210
|
-
}]
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
_classPrivateFieldSet(this, _defaultPort, port); // handle requests on end
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
this.on('request', (req, res) => {
|
|
217
|
-
req.on('end', () => _classPrivateMethodGet(this, _handleRequest, _handleRequest2).call(this, req, res));
|
|
218
|
-
}); // handle websocket upgrades
|
|
219
|
-
|
|
220
|
-
this.on('upgrade', (req, sock, head) => {
|
|
221
|
-
_classPrivateMethodGet(this, _handleUpgrade, _handleUpgrade2).call(this, req, sock, head);
|
|
222
|
-
}); // track open connections to terminate when the server closes
|
|
223
|
-
|
|
224
|
-
this.on('connection', socket => {
|
|
225
|
-
let handleClose = () => _classPrivateFieldGet(this, _sockets).delete(socket);
|
|
226
|
-
|
|
227
|
-
_classPrivateFieldGet(this, _sockets).add(socket.on('close', handleClose));
|
|
228
|
-
});
|
|
229
|
-
} // return the listening port or any default port
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
get port() {
|
|
233
|
-
var _super$address$port, _super$address;
|
|
234
|
-
|
|
235
|
-
return (_super$address$port = (_super$address = super.address()) === null || _super$address === void 0 ? void 0 : _super$address.port) !== null && _super$address$port !== void 0 ? _super$address$port : _classPrivateFieldGet(this, _defaultPort);
|
|
236
|
-
} // return a string representation of the server address
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
address() {
|
|
240
|
-
let port = this.port;
|
|
241
|
-
let host = 'http://localhost';
|
|
242
|
-
return port ? `${host}:${port}` : host;
|
|
243
|
-
} // return a promise that resolves when the server is listening
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
listen(port = _classPrivateFieldGet(this, _defaultPort)) {
|
|
247
|
-
return new Promise((resolve, reject) => {
|
|
248
|
-
let handle = err => off() && err ? reject(err) : resolve(this);
|
|
249
|
-
|
|
250
|
-
let off = () => this.off('error', handle).off('listening', handle);
|
|
251
|
-
|
|
252
|
-
super.listen(port, handle).once('error', handle);
|
|
253
|
-
});
|
|
254
|
-
} // return a promise that resolves when the server closes
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
close() {
|
|
258
|
-
return new Promise(resolve => {
|
|
259
|
-
_classPrivateFieldGet(this, _sockets).forEach(socket => socket.destroy());
|
|
260
|
-
|
|
261
|
-
super.close(resolve);
|
|
262
|
-
});
|
|
263
|
-
} // handle websocket upgrades
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
websocket(pathname, handle) {
|
|
267
|
-
if (!handle) [pathname, handle] = [null, pathname];
|
|
268
|
-
|
|
269
|
-
_classPrivateFieldGet(this, _up).push({
|
|
270
|
-
match: pathname && (0, _pathToRegexp.match)(pathname),
|
|
271
|
-
handle: (req, sock, head) => new Promise(resolve => {
|
|
272
|
-
let wss = new _ws.default.Server({
|
|
273
|
-
noServer: true,
|
|
274
|
-
clientTracking: false
|
|
275
|
-
});
|
|
276
|
-
wss.handleUpgrade(req, sock, head, resolve);
|
|
277
|
-
}).then(ws => handle(ws, req))
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
if (pathname) {
|
|
281
|
-
_classPrivateFieldGet(this, _up).sort((a, b) => (a.match ? -1 : 1) - (b.match ? -1 : 1));
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return this;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// set request routing and handling for pathnames and methods
|
|
288
|
-
route(method, pathname, handle) {
|
|
289
|
-
if (arguments.length === 1) [handle, method] = [method];
|
|
290
|
-
if (arguments.length === 2) [handle, pathname] = [pathname];
|
|
291
|
-
if (arguments.length === 2 && !Array.isArray(method) && method[0] === '/') [pathname, method] = [method];
|
|
292
|
-
return _classPrivateMethodGet(this, _route, _route2).call(this, {
|
|
293
|
-
priority: !pathname ? 0 : !method ? 1 : 2,
|
|
294
|
-
methods: method && [].concat(method).map(m => m.toUpperCase()),
|
|
295
|
-
match: pathname && (0, _pathToRegexp.match)(pathname),
|
|
296
|
-
handle
|
|
297
|
-
});
|
|
298
|
-
} // install a route that serves requested files from the provided directory
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
serve(pathname, directory, options) {
|
|
302
|
-
var _options;
|
|
303
|
-
|
|
304
|
-
if (typeof directory !== 'string') [options, directory] = [directory];
|
|
305
|
-
if (!directory) [pathname, directory] = ['/', pathname];
|
|
306
|
-
|
|
307
|
-
let root = _path.default.resolve(directory);
|
|
308
|
-
|
|
309
|
-
let mountPattern = (0, _pathToRegexp.pathToRegexp)(pathname, null, {
|
|
310
|
-
end: false
|
|
311
|
-
});
|
|
312
|
-
let rewritePath = createRewriter((_options = options) === null || _options === void 0 ? void 0 : _options.rewrites, (pathname, rewrite) => {
|
|
313
|
-
try {
|
|
314
|
-
let filepath = decodeURIComponent(pathname.replace(mountPattern, ''));
|
|
315
|
-
if (!isPathInside(root, filepath)) ServerError.throw();
|
|
316
|
-
return rewrite(filepath);
|
|
317
|
-
} catch {
|
|
318
|
-
throw new ServerError(400);
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
return _classPrivateMethodGet(this, _route, _route2).call(this, {
|
|
322
|
-
priority: 2,
|
|
323
|
-
methods: ['GET'],
|
|
324
|
-
match: pathname => mountPattern.test(pathname),
|
|
325
|
-
handle: async (req, res, next) => {
|
|
326
|
-
try {
|
|
327
|
-
var _options2;
|
|
328
|
-
|
|
329
|
-
let pathname = rewritePath(req.url.pathname);
|
|
330
|
-
let file = await getFile(root, pathname, (_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.cleanUrls);
|
|
331
|
-
if (!(file !== null && file !== void 0 && file.stats.isFile())) return await next();
|
|
332
|
-
return res.file(file.path);
|
|
333
|
-
} catch (err) {
|
|
334
|
-
let statusPage = _path.default.join(root, `${err.status}.html`);
|
|
335
|
-
|
|
336
|
-
if (!_fs.default.existsSync(statusPage)) throw err;
|
|
337
|
-
return res.file(err.status, statusPage);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
} // route and respond to requests; handling errors if necessary
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
} // create a url rewriter from provided rewrite rules
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
exports.Server = Server;
|
|
348
|
-
|
|
349
|
-
function _handleUpgrade2(req, sock, head) {
|
|
350
|
-
let up = _classPrivateFieldGet(this, _up).find(u => !u.match || u.match(req.url));
|
|
351
|
-
|
|
352
|
-
if (up) return up.handle(req, sock, head);
|
|
353
|
-
sock.write(`HTTP/1.1 400 ${_http.default.STATUS_CODES[400]}\r\n` + 'Connection: close\r\n\r\n');
|
|
354
|
-
sock.destroy();
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function _route2(route) {
|
|
358
|
-
let i = _classPrivateFieldGet(this, _routes).findIndex(r => r.priority >= route.priority);
|
|
359
|
-
|
|
360
|
-
_classPrivateFieldGet(this, _routes).splice(i, 0, route);
|
|
361
|
-
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
async function _handleRequest2(req, res) {
|
|
366
|
-
var _res$req;
|
|
367
|
-
|
|
368
|
-
// support node < 15.7.0
|
|
369
|
-
(_res$req = res.req) !== null && _res$req !== void 0 ? _res$req : res.req = req;
|
|
370
|
-
|
|
371
|
-
try {
|
|
372
|
-
// invoke routes like middleware
|
|
373
|
-
await async function cont(routes, i = 0) {
|
|
374
|
-
let next = () => cont(routes, i + 1);
|
|
375
|
-
|
|
376
|
-
let {
|
|
377
|
-
methods,
|
|
378
|
-
match,
|
|
379
|
-
handle
|
|
380
|
-
} = routes[i];
|
|
381
|
-
let result = !methods || methods.includes(req.method);
|
|
382
|
-
result && (result = !match || match(req.url.pathname));
|
|
383
|
-
if (result) req.params = result.params;
|
|
384
|
-
return result ? handle(req, res, next) : next();
|
|
385
|
-
}(_classPrivateFieldGet(this, _routes));
|
|
386
|
-
} catch (error) {
|
|
387
|
-
var _req$headers$accept, _req$headers$content;
|
|
388
|
-
|
|
389
|
-
let {
|
|
390
|
-
status = 500,
|
|
391
|
-
message
|
|
392
|
-
} = error; // fallback error handling
|
|
393
|
-
|
|
394
|
-
if ((_req$headers$accept = req.headers.accept) !== null && _req$headers$accept !== void 0 && _req$headers$accept.includes('json') || (_req$headers$content = req.headers['content-type']) !== null && _req$headers$content !== void 0 && _req$headers$content.includes('json')) {
|
|
395
|
-
res.json(status, {
|
|
396
|
-
error: message
|
|
397
|
-
});
|
|
398
|
-
} else {
|
|
399
|
-
res.text(status, message);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
function createRewriter(rewrites = [], cb) {
|
|
405
|
-
let normalize = p => _path.default.posix.normalize(_path.default.posix.join('/', p));
|
|
406
|
-
|
|
407
|
-
if (!Array.isArray(rewrites)) rewrites = Object.entries(rewrites);
|
|
408
|
-
let rewrite = [{
|
|
409
|
-
// resolve and normalize the path before rewriting
|
|
410
|
-
apply: p => _path.default.posix.resolve(normalize(p))
|
|
411
|
-
}].concat(rewrites.map(([src, dest]) => {
|
|
412
|
-
// compile rewrite rules into functions
|
|
413
|
-
let match = (0, _pathToRegexp.match)(normalize(src));
|
|
414
|
-
let toPath = (0, _pathToRegexp.compile)(normalize(dest));
|
|
415
|
-
return {
|
|
416
|
-
match,
|
|
417
|
-
apply: r => toPath(r.params)
|
|
418
|
-
};
|
|
419
|
-
})).reduceRight((next, rule) => pathname => {
|
|
420
|
-
var _rule$match, _rule$match2;
|
|
421
|
-
|
|
422
|
-
// compose all rewrites into a single function
|
|
423
|
-
let result = (_rule$match = (_rule$match2 = rule.match) === null || _rule$match2 === void 0 ? void 0 : _rule$match2.call(rule, pathname)) !== null && _rule$match !== void 0 ? _rule$match : pathname;
|
|
424
|
-
if (result) pathname = rule.apply(result);
|
|
425
|
-
return next(pathname);
|
|
426
|
-
}, p => p); // allow additional pathname processing around the rewriter
|
|
427
|
-
|
|
428
|
-
return p => cb(p, rewrite);
|
|
429
|
-
} // returns true if the pathname is inside the root pathname
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
function isPathInside(root, pathname) {
|
|
433
|
-
let abs = _path.default.resolve(_path.default.join(root, pathname));
|
|
434
|
-
|
|
435
|
-
return !abs.lastIndexOf(root, 0) && (abs[root.length] === _path.default.sep || !abs[root.length]);
|
|
436
|
-
} // get the absolute path and stats of a possible file
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
async function getFile(root, pathname, cleanUrls) {
|
|
440
|
-
for (let filename of [pathname].concat(cleanUrls ? _path.default.join(pathname, 'index.html') : [], cleanUrls && pathname.length > 2 ? pathname.replace(/\/?$/, '.html') : [])) {
|
|
441
|
-
let filepath = _path.default.resolve(_path.default.join(root, filename));
|
|
442
|
-
|
|
443
|
-
let stats = await _fs.default.promises.lstat(filepath).catch(() => {});
|
|
444
|
-
if (stats !== null && stats !== void 0 && stats.isFile()) return {
|
|
445
|
-
path: filepath,
|
|
446
|
-
stats
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
} // returns the start and end of a byte range or undefined if unable to parse
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
const RANGE_REGEXP = /^bytes=(\d*)?-(\d*)?(?:\b|$)/;
|
|
453
|
-
|
|
454
|
-
function parseByteRange(range, size) {
|
|
455
|
-
var _range$match;
|
|
456
|
-
|
|
457
|
-
let [, start, end = size] = (_range$match = range === null || range === void 0 ? void 0 : range.match(RANGE_REGEXP)) !== null && _range$match !== void 0 ? _range$match : [0, 0, 0];
|
|
458
|
-
start = Math.max(parseInt(start, 10), 0);
|
|
459
|
-
end = Math.min(parseInt(end, 10), size - 1);
|
|
460
|
-
if (isNaN(start)) [start, end] = [size - end, size - 1];
|
|
461
|
-
if (start >= 0 && start < end) return {
|
|
462
|
-
start,
|
|
463
|
-
end
|
|
464
|
-
};
|
|
465
|
-
} // include ServerError and createRewriter as static properties
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
Server.Error = ServerError;
|
|
469
|
-
Server.createRewriter = createRewriter;
|
|
470
|
-
var _default = Server;
|
|
471
|
-
exports.default = _default;
|
package/dist/session.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.Session = void 0;
|
|
7
|
-
|
|
8
|
-
var _events = _interopRequireDefault(require("events"));
|
|
9
|
-
|
|
10
|
-
var _logger = _interopRequireDefault(require("@percy/logger"));
|
|
11
|
-
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
-
|
|
16
|
-
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
17
|
-
|
|
18
|
-
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
19
|
-
|
|
20
|
-
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
21
|
-
|
|
22
|
-
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
23
|
-
|
|
24
|
-
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
25
|
-
|
|
26
|
-
var _callbacks = /*#__PURE__*/new WeakMap();
|
|
27
|
-
|
|
28
|
-
class Session extends _events.default {
|
|
29
|
-
constructor(browser, {
|
|
30
|
-
params,
|
|
31
|
-
sessionId: parentId
|
|
32
|
-
}) {
|
|
33
|
-
var _this$parent;
|
|
34
|
-
|
|
35
|
-
super();
|
|
36
|
-
|
|
37
|
-
_classPrivateFieldInitSpec(this, _callbacks, {
|
|
38
|
-
writable: true,
|
|
39
|
-
value: new Map()
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
_defineProperty(this, "log", (0, _logger.default)('core:session'));
|
|
43
|
-
|
|
44
|
-
_defineProperty(this, "children", new Map());
|
|
45
|
-
|
|
46
|
-
_defineProperty(this, "_handleTargetCrashed", () => {
|
|
47
|
-
this.closedReason = 'Session crashed!';
|
|
48
|
-
this.close();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
_defineProperty(this, "_handleClosedError", error => {
|
|
52
|
-
if (!error.message.endsWith(this.closedReason)) {
|
|
53
|
-
this.log.debug(error, this.meta);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
this.browser = browser;
|
|
58
|
-
this.sessionId = params.sessionId;
|
|
59
|
-
this.targetId = params.targetInfo.targetId;
|
|
60
|
-
this.type = params.targetInfo.type;
|
|
61
|
-
this.isDocument = this.type === 'page' || this.type === 'iframe';
|
|
62
|
-
this.parent = browser.sessions.get(parentId);
|
|
63
|
-
(_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.children.set(this.sessionId, this);
|
|
64
|
-
this.on('Inspector.targetCrashed', this._handleTargetCrashed);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async close() {
|
|
68
|
-
if (!this.browser) return;
|
|
69
|
-
await this.browser.send('Target.closeTarget', {
|
|
70
|
-
targetId: this.targetId
|
|
71
|
-
}).catch(this._handleClosedError);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async send(method, params) {
|
|
75
|
-
/* istanbul ignore next: race condition paranoia */
|
|
76
|
-
if (this.closedReason) {
|
|
77
|
-
throw new Error(`Protocol error (${method}): ${this.closedReason}`);
|
|
78
|
-
} // send a raw message to the browser so we can provide a sessionId
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
let id = await this.browser.send({
|
|
82
|
-
sessionId: this.sessionId,
|
|
83
|
-
method,
|
|
84
|
-
params
|
|
85
|
-
}); // will resolve or reject when a matching response is received
|
|
86
|
-
|
|
87
|
-
return new Promise((resolve, reject) => {
|
|
88
|
-
_classPrivateFieldGet(this, _callbacks).set(id, {
|
|
89
|
-
error: new Error(),
|
|
90
|
-
resolve,
|
|
91
|
-
reject,
|
|
92
|
-
method
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
_handleMessage(data) {
|
|
98
|
-
if (data.id && _classPrivateFieldGet(this, _callbacks).has(data.id)) {
|
|
99
|
-
// resolve or reject a pending promise created with #send()
|
|
100
|
-
let callback = _classPrivateFieldGet(this, _callbacks).get(data.id);
|
|
101
|
-
|
|
102
|
-
_classPrivateFieldGet(this, _callbacks).delete(data.id);
|
|
103
|
-
/* istanbul ignore next: races with browser._handleMessage() */
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (data.error) {
|
|
107
|
-
callback.reject(Object.assign(callback.error, {
|
|
108
|
-
message: `Protocol error (${callback.method}): ${data.error.message}` + ('data' in data.error ? `: ${data.error.data}` : '')
|
|
109
|
-
}));
|
|
110
|
-
} else {
|
|
111
|
-
callback.resolve(data.result);
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
// emit the message as an event
|
|
115
|
-
this.emit(data.method, data.params);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
_handleClose() {
|
|
120
|
-
var _this$parent2;
|
|
121
|
-
|
|
122
|
-
this.closedReason || (this.closedReason = 'Session closed.'); // reject any pending callbacks
|
|
123
|
-
|
|
124
|
-
for (let callback of _classPrivateFieldGet(this, _callbacks).values()) {
|
|
125
|
-
callback.reject(Object.assign(callback.error, {
|
|
126
|
-
message: `Protocol error (${callback.method}): ${this.closedReason}`
|
|
127
|
-
}));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
_classPrivateFieldGet(this, _callbacks).clear();
|
|
131
|
-
|
|
132
|
-
(_this$parent2 = this.parent) === null || _this$parent2 === void 0 ? void 0 : _this$parent2.children.delete(this.sessionId);
|
|
133
|
-
this.browser = null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
exports.Session = Session;
|
|
139
|
-
var _default = Session;
|
|
140
|
-
exports.default = _default;
|