@checkly/playwright-core 1.51.14 → 1.51.15-beta.1

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.
Files changed (49) hide show
  1. package/lib/vite/traceViewer/sw.bundle.js +3 -7888
  2. package/package.json +1 -1
  3. package/lib/common/socksProxy.js +0 -569
  4. package/lib/common/timeoutSettings.js +0 -73
  5. package/lib/common/types.js +0 -5
  6. package/lib/generated/recorderSource.js +0 -7
  7. package/lib/image_tools/colorUtils.js +0 -98
  8. package/lib/image_tools/compare.js +0 -108
  9. package/lib/image_tools/imageChannel.js +0 -70
  10. package/lib/image_tools/stats.js +0 -102
  11. package/lib/protocol/transport.js +0 -82
  12. package/lib/server/recorder/codeGenerator.js +0 -154
  13. package/lib/server/recorder/csharp.js +0 -311
  14. package/lib/server/recorder/java.js +0 -249
  15. package/lib/server/recorder/javascript.js +0 -230
  16. package/lib/server/recorder/jsonl.js +0 -48
  17. package/lib/server/recorder/language.js +0 -45
  18. package/lib/server/recorder/python.js +0 -276
  19. package/lib/server/recorder/recorderActions.js +0 -6
  20. package/lib/server/recorder/utils.js +0 -46
  21. package/lib/third_party/diff_match_patch.js +0 -2222
  22. package/lib/utils/ascii.js +0 -31
  23. package/lib/utils/comparators.js +0 -171
  24. package/lib/utils/crypto.js +0 -174
  25. package/lib/utils/debug.js +0 -46
  26. package/lib/utils/debugLogger.js +0 -91
  27. package/lib/utils/env.js +0 -49
  28. package/lib/utils/eventsHelper.js +0 -38
  29. package/lib/utils/glob.js +0 -84
  30. package/lib/utils/happy-eyeballs.js +0 -210
  31. package/lib/utils/headers.js +0 -52
  32. package/lib/utils/hostPlatform.js +0 -133
  33. package/lib/utils/httpServer.js +0 -237
  34. package/lib/utils/linuxUtils.js +0 -78
  35. package/lib/utils/manualPromise.js +0 -109
  36. package/lib/utils/mimeType.js +0 -30
  37. package/lib/utils/multimap.js +0 -75
  38. package/lib/utils/network.js +0 -160
  39. package/lib/utils/processLauncher.js +0 -248
  40. package/lib/utils/profiler.js +0 -53
  41. package/lib/utils/rtti.js +0 -44
  42. package/lib/utils/semaphore.js +0 -51
  43. package/lib/utils/spawnAsync.js +0 -45
  44. package/lib/utils/task.js +0 -58
  45. package/lib/utils/time.js +0 -37
  46. package/lib/utils/traceUtils.js +0 -44
  47. package/lib/utils/userAgent.js +0 -105
  48. package/lib/utils/wsServer.js +0 -127
  49. package/lib/utils/zipFile.js +0 -75
@@ -1,210 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createConnectionAsync = createConnectionAsync;
7
- exports.createSocket = createSocket;
8
- exports.createTLSSocket = createTLSSocket;
9
- exports.httpsHappyEyeballsAgent = exports.httpHappyEyeballsTimings = exports.httpHappyEyeballsAgent = void 0;
10
- exports.timingForSocket = timingForSocket;
11
- var dns = _interopRequireWildcard(require("dns"));
12
- var http = _interopRequireWildcard(require("http"));
13
- var https = _interopRequireWildcard(require("https"));
14
- var net = _interopRequireWildcard(require("net"));
15
- var tls = _interopRequireWildcard(require("tls"));
16
- var _manualPromise = require("./manualPromise");
17
- var _debug = require("./debug");
18
- var _time = require("./time");
19
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
20
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
21
- /**
22
- * Copyright (c) Microsoft Corporation.
23
- *
24
- * Licensed under the Apache License, Version 2.0 (the "License");
25
- * you may not use this file except in compliance with the License.
26
- * You may obtain a copy of the License at
27
- *
28
- * http://www.apache.org/licenses/LICENSE-2.0
29
- *
30
- * Unless required by applicable law or agreed to in writing, software
31
- * distributed under the License is distributed on an "AS IS" BASIS,
32
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33
- * See the License for the specific language governing permissions and
34
- * limitations under the License.
35
- */
36
-
37
- // Implementation(partial) of Happy Eyeballs 2 algorithm described in
38
- // https://www.rfc-editor.org/rfc/rfc8305
39
-
40
- // Same as in Chromium (https://source.chromium.org/chromium/chromium/src/+/5666ff4f5077a7e2f72902f3a95f5d553ea0d88d:net/socket/transport_connect_job.cc;l=102)
41
- const connectionAttemptDelayMs = 300;
42
- const timings = {
43
- values: {
44
- startTimeNow: 0,
45
- socket: 0,
46
- connect: 0,
47
- lookup: 0
48
- },
49
- agent: false
50
- };
51
- const kDNSLookupAt = Symbol('kDNSLookupAt');
52
- const kTCPConnectionAt = Symbol('kTCPConnectionAt');
53
- class HttpHappyEyeballsAgent extends http.Agent {
54
- createConnection(options, oncreate) {
55
- // There is no ambiguity in case of IP address.
56
- if (net.isIP(clientRequestArgsToHostName(options))) return net.createConnection(options);
57
- createConnectionAsync(options, oncreate, /* useTLS */false).catch(err => oncreate === null || oncreate === void 0 ? void 0 : oncreate(err));
58
- }
59
- }
60
- class HttpsHappyEyeballsAgent extends https.Agent {
61
- createConnection(options, oncreate) {
62
- timings.values.startTimeNow = performance.now();
63
- timings.agent = true;
64
- // There is no ambiguity in case of IP address.
65
- if (net.isIP(clientRequestArgsToHostName(options))) return tls.connect(options);
66
- createConnectionAsync(options, oncreate, /* useTLS */true).catch(err => oncreate === null || oncreate === void 0 ? void 0 : oncreate(err));
67
- }
68
- }
69
-
70
- // These options are aligned with the default Node.js globalAgent options.
71
- const httpsHappyEyeballsAgent = exports.httpsHappyEyeballsAgent = new HttpsHappyEyeballsAgent({
72
- keepAlive: true
73
- });
74
- const httpHappyEyeballsAgent = exports.httpHappyEyeballsAgent = new HttpHappyEyeballsAgent({
75
- keepAlive: true
76
- });
77
- const httpHappyEyeballsTimings = exports.httpHappyEyeballsTimings = timings;
78
- async function createSocket(host, port) {
79
- return new Promise((resolve, reject) => {
80
- if (net.isIP(host)) {
81
- const socket = net.createConnection({
82
- host,
83
- port
84
- });
85
- socket.on('connect', () => resolve(socket));
86
- socket.on('error', error => reject(error));
87
- } else {
88
- createConnectionAsync({
89
- host,
90
- port
91
- }, (err, socket) => {
92
- if (err) reject(err);
93
- if (socket) resolve(socket);
94
- }, /* useTLS */false).catch(err => reject(err));
95
- }
96
- });
97
- }
98
- async function createTLSSocket(options) {
99
- return new Promise((resolve, reject) => {
100
- (0, _debug.assert)(options.host, 'host is required');
101
- if (net.isIP(options.host)) {
102
- const socket = tls.connect(options);
103
- socket.on('secureConnect', () => resolve(socket));
104
- socket.on('error', error => reject(error));
105
- } else {
106
- createConnectionAsync(options, (err, socket) => {
107
- if (err) reject(err);
108
- if (socket) {
109
- socket.on('secureConnect', () => resolve(socket));
110
- socket.on('error', error => reject(error));
111
- }
112
- }, true).catch(err => reject(err));
113
- }
114
- });
115
- }
116
- async function createConnectionAsync(options, oncreate, useTLS) {
117
- const lookup = options.__testHookLookup || lookupAddresses;
118
- const hostname = clientRequestArgsToHostName(options);
119
- const addresses = await lookup(hostname);
120
- const dnsLookupAt = (0, _time.monotonicTime)();
121
- const sockets = new Set();
122
- let firstError;
123
- let errorCount = 0;
124
- const handleError = (socket, err) => {
125
- var _firstError;
126
- if (!sockets.delete(socket)) return;
127
- ++errorCount;
128
- (_firstError = firstError) !== null && _firstError !== void 0 ? _firstError : firstError = err;
129
- if (errorCount === addresses.length) oncreate === null || oncreate === void 0 || oncreate(firstError);
130
- };
131
- const connected = new _manualPromise.ManualPromise();
132
- for (const {
133
- address
134
- } of addresses) {
135
- const socket = useTLS ? tls.connect({
136
- ...options,
137
- port: options.port,
138
- host: address,
139
- servername: hostname
140
- }) : net.createConnection({
141
- ...options,
142
- port: options.port,
143
- host: address
144
- });
145
- // Socket created
146
- timings.values.socket = performance.now() - timings.values.startTimeNow;
147
- socket[kDNSLookupAt] = dnsLookupAt;
148
-
149
- // Each socket may fire only one of 'connect', 'timeout' or 'error' events.
150
- // None of these events are fired after socket.destroy() is called.
151
- socket.on('connect', () => {
152
- socket[kTCPConnectionAt] = (0, _time.monotonicTime)();
153
- timings.values.connect = performance.now() - timings.values.startTimeNow;
154
- connected.resolve();
155
- oncreate === null || oncreate === void 0 || oncreate(null, socket);
156
- // TODO: Cache the result?
157
- // Close other outstanding sockets.
158
- sockets.delete(socket);
159
- for (const s of sockets) s.destroy();
160
- sockets.clear();
161
- });
162
- socket.on('timeout', () => {
163
- // Timeout is not an error, so we have to manually close the socket.
164
- socket.destroy();
165
- handleError(socket, new Error('Connection timeout'));
166
- });
167
- socket.on('error', e => handleError(socket, e));
168
- sockets.add(socket);
169
- await Promise.race([connected, new Promise(f => setTimeout(f, connectionAttemptDelayMs))]);
170
- if (connected.isDone()) break;
171
- }
172
- }
173
- async function lookupAddresses(hostname) {
174
- const addresses = await dns.promises.lookup(hostname, {
175
- all: true,
176
- family: 0,
177
- verbatim: true
178
- });
179
- timings.values.lookup = performance.now() - timings.values.startTimeNow;
180
- let firstFamily = addresses.filter(({
181
- family
182
- }) => family === 6);
183
- let secondFamily = addresses.filter(({
184
- family
185
- }) => family === 4);
186
- // Make sure first address in the list is the same as in the original order.
187
- if (firstFamily.length && firstFamily[0] !== addresses[0]) {
188
- const tmp = firstFamily;
189
- firstFamily = secondFamily;
190
- secondFamily = tmp;
191
- }
192
- const result = [];
193
- // Alternate ipv6 and ipv4 addresses.
194
- for (let i = 0; i < Math.max(firstFamily.length, secondFamily.length); i++) {
195
- if (firstFamily[i]) result.push(firstFamily[i]);
196
- if (secondFamily[i]) result.push(secondFamily[i]);
197
- }
198
- return result;
199
- }
200
- function clientRequestArgsToHostName(options) {
201
- if (options.hostname) return options.hostname;
202
- if (options.host) return options.host;
203
- throw new Error('Either options.hostname or options.host must be provided');
204
- }
205
- function timingForSocket(socket) {
206
- return {
207
- dnsLookupAt: socket[kDNSLookupAt],
208
- tcpConnectionAt: socket[kTCPConnectionAt]
209
- };
210
- }
@@ -1,52 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.headersArrayToObject = headersArrayToObject;
7
- exports.headersObjectToArray = headersObjectToArray;
8
- /**
9
- * Copyright (c) Microsoft Corporation.
10
- *
11
- * Licensed under the Apache License, Version 2.0 (the "License");
12
- * you may not use this file except in compliance with the License.
13
- * You may obtain a copy of the License at
14
- *
15
- * http://www.apache.org/licenses/LICENSE-2.0
16
- *
17
- * Unless required by applicable law or agreed to in writing, software
18
- * distributed under the License is distributed on an "AS IS" BASIS,
19
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
- * See the License for the specific language governing permissions and
21
- * limitations under the License.
22
- */
23
-
24
- function headersObjectToArray(headers, separator, setCookieSeparator) {
25
- if (!setCookieSeparator) setCookieSeparator = separator;
26
- const result = [];
27
- for (const name in headers) {
28
- const values = headers[name];
29
- if (values === undefined) continue;
30
- if (separator) {
31
- const sep = name.toLowerCase() === 'set-cookie' ? setCookieSeparator : separator;
32
- for (const value of values.split(sep)) result.push({
33
- name,
34
- value: value.trim()
35
- });
36
- } else {
37
- result.push({
38
- name,
39
- value: values
40
- });
41
- }
42
- }
43
- return result;
44
- }
45
- function headersArrayToObject(headers, lowerCase) {
46
- const result = {};
47
- for (const {
48
- name,
49
- value
50
- } of headers) result[lowerCase ? name.toLowerCase() : name] = value;
51
- return result;
52
- }
@@ -1,133 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isOfficiallySupportedPlatform = exports.hostPlatform = void 0;
7
- var _os = _interopRequireDefault(require("os"));
8
- var _linuxUtils = require("./linuxUtils");
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- /**
11
- * Copyright (c) Microsoft Corporation.
12
- *
13
- * Licensed under the Apache License, Version 2.0 (the "License");
14
- * you may not use this file except in compliance with the License.
15
- * You may obtain a copy of the License at
16
- *
17
- * http://www.apache.org/licenses/LICENSE-2.0
18
- *
19
- * Unless required by applicable law or agreed to in writing, software
20
- * distributed under the License is distributed on an "AS IS" BASIS,
21
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
- * See the License for the specific language governing permissions and
23
- * limitations under the License.
24
- */
25
-
26
- function calculatePlatform() {
27
- const platform = _os.default.platform();
28
- if (platform === 'darwin') {
29
- const ver = _os.default.release().split('.').map(a => parseInt(a, 10));
30
- let macVersion = '';
31
- if (ver[0] < 18) {
32
- // Everything before 10.14 is considered 10.13.
33
- macVersion = 'mac10.13';
34
- } else if (ver[0] === 18) {
35
- macVersion = 'mac10.14';
36
- } else if (ver[0] === 19) {
37
- macVersion = 'mac10.15';
38
- } else {
39
- // ver[0] >= 20
40
- const LAST_STABLE_MACOS_MAJOR_VERSION = 15;
41
- // Best-effort support for MacOS beta versions.
42
- macVersion = 'mac' + Math.min(ver[0] - 9, LAST_STABLE_MACOS_MAJOR_VERSION);
43
- // BigSur is the first version that might run on Apple Silicon.
44
- if (_os.default.cpus().some(cpu => cpu.model.includes('Apple'))) macVersion += '-arm64';
45
- }
46
- return {
47
- hostPlatform: macVersion,
48
- isOfficiallySupportedPlatform: true
49
- };
50
- }
51
- if (platform === 'linux') {
52
- if (!['x64', 'arm64'].includes(_os.default.arch())) return {
53
- hostPlatform: '<unknown>',
54
- isOfficiallySupportedPlatform: false
55
- };
56
- const archSuffix = '-' + _os.default.arch();
57
- const distroInfo = (0, _linuxUtils.getLinuxDistributionInfoSync)();
58
-
59
- // Pop!_OS is ubuntu-based and has the same versions.
60
- // KDE Neon is ubuntu-based and has the same versions.
61
- // TUXEDO OS is ubuntu-based and has the same versions.
62
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'ubuntu' || (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'pop' || (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'neon' || (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'tuxedo') {
63
- const isOfficiallySupportedPlatform = (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'ubuntu';
64
- if (parseInt(distroInfo.version, 10) <= 19) return {
65
- hostPlatform: 'ubuntu18.04' + archSuffix,
66
- isOfficiallySupportedPlatform: false
67
- };
68
- if (parseInt(distroInfo.version, 10) <= 21) return {
69
- hostPlatform: 'ubuntu20.04' + archSuffix,
70
- isOfficiallySupportedPlatform
71
- };
72
- if (parseInt(distroInfo.version, 10) <= 22) return {
73
- hostPlatform: 'ubuntu22.04' + archSuffix,
74
- isOfficiallySupportedPlatform
75
- };
76
- return {
77
- hostPlatform: 'ubuntu24.04' + archSuffix,
78
- isOfficiallySupportedPlatform
79
- };
80
- }
81
- // Linux Mint is ubuntu-based but does not have the same versions
82
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'linuxmint') {
83
- const mintMajor = parseInt(distroInfo.version, 10);
84
- if (mintMajor <= 20) return {
85
- hostPlatform: 'ubuntu20.04' + archSuffix,
86
- isOfficiallySupportedPlatform: false
87
- };
88
- if (mintMajor === 21) return {
89
- hostPlatform: 'ubuntu22.04' + archSuffix,
90
- isOfficiallySupportedPlatform: false
91
- };
92
- return {
93
- hostPlatform: 'ubuntu24.04' + archSuffix,
94
- isOfficiallySupportedPlatform: false
95
- };
96
- }
97
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'debian' || (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'raspbian') {
98
- const isOfficiallySupportedPlatform = (distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.id) === 'debian';
99
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.version) === '11') return {
100
- hostPlatform: 'debian11' + archSuffix,
101
- isOfficiallySupportedPlatform
102
- };
103
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.version) === '12') return {
104
- hostPlatform: 'debian12' + archSuffix,
105
- isOfficiallySupportedPlatform
106
- };
107
- // use most recent supported release for 'debian testing' and 'unstable'.
108
- // they never include a numeric version entry in /etc/os-release.
109
- if ((distroInfo === null || distroInfo === void 0 ? void 0 : distroInfo.version) === '') return {
110
- hostPlatform: 'debian12' + archSuffix,
111
- isOfficiallySupportedPlatform
112
- };
113
- }
114
- return {
115
- hostPlatform: 'ubuntu20.04' + archSuffix,
116
- isOfficiallySupportedPlatform: false
117
- };
118
- }
119
- if (platform === 'win32') return {
120
- hostPlatform: 'win64',
121
- isOfficiallySupportedPlatform: true
122
- };
123
- return {
124
- hostPlatform: '<unknown>',
125
- isOfficiallySupportedPlatform: false
126
- };
127
- }
128
- const {
129
- hostPlatform,
130
- isOfficiallySupportedPlatform
131
- } = calculatePlatform();
132
- exports.isOfficiallySupportedPlatform = isOfficiallySupportedPlatform;
133
- exports.hostPlatform = hostPlatform;
@@ -1,237 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.HttpServer = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _path = _interopRequireDefault(require("path"));
9
- var _utilsBundle = require("../utilsBundle");
10
- var _debug = require("./debug");
11
- var _network = require("./network");
12
- var _manualPromise = require("./manualPromise");
13
- var _crypto = require("./crypto");
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
- /**
16
- * Copyright (c) Microsoft Corporation.
17
- *
18
- * Licensed under the Apache License, Version 2.0 (the "License");
19
- * you may not use this file except in compliance with the License.
20
- * You may obtain a copy of the License at
21
- *
22
- * http://www.apache.org/licenses/LICENSE-2.0
23
- *
24
- * Unless required by applicable law or agreed to in writing, software
25
- * distributed under the License is distributed on an "AS IS" BASIS,
26
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
- * See the License for the specific language governing permissions and
28
- * limitations under the License.
29
- */
30
-
31
- class HttpServer {
32
- constructor() {
33
- this._server = void 0;
34
- this._urlPrefixPrecise = '';
35
- this._urlPrefixHumanReadable = '';
36
- this._port = 0;
37
- this._started = false;
38
- this._routes = [];
39
- this._wsGuid = void 0;
40
- this._server = (0, _network.createHttpServer)(this._onRequest.bind(this));
41
- }
42
- server() {
43
- return this._server;
44
- }
45
- routePrefix(prefix, handler) {
46
- this._routes.push({
47
- prefix,
48
- handler
49
- });
50
- }
51
- routePath(path, handler) {
52
- this._routes.push({
53
- exact: path,
54
- handler
55
- });
56
- }
57
- port() {
58
- return this._port;
59
- }
60
- async _tryStart(port, host) {
61
- const errorPromise = new _manualPromise.ManualPromise();
62
- const errorListener = error => errorPromise.reject(error);
63
- this._server.on('error', errorListener);
64
- try {
65
- this._server.listen(port, host);
66
- await Promise.race([new Promise(cb => this._server.once('listening', cb)), errorPromise]);
67
- } finally {
68
- this._server.removeListener('error', errorListener);
69
- }
70
- }
71
- createWebSocket(transport, guid) {
72
- (0, _debug.assert)(!this._wsGuid, 'can only create one main websocket transport per server');
73
- this._wsGuid = guid || (0, _crypto.createGuid)();
74
- const wss = new _utilsBundle.wsServer({
75
- server: this._server,
76
- path: '/' + this._wsGuid
77
- });
78
- wss.on('connection', ws => {
79
- transport.onconnect();
80
- transport.sendEvent = (method, params) => ws.send(JSON.stringify({
81
- method,
82
- params
83
- }));
84
- transport.close = () => ws.close();
85
- ws.on('message', async message => {
86
- const {
87
- id,
88
- method,
89
- params
90
- } = JSON.parse(String(message));
91
- try {
92
- const result = await transport.dispatch(method, params);
93
- ws.send(JSON.stringify({
94
- id,
95
- result
96
- }));
97
- } catch (e) {
98
- ws.send(JSON.stringify({
99
- id,
100
- error: String(e)
101
- }));
102
- }
103
- });
104
- ws.on('close', () => transport.onclose());
105
- ws.on('error', () => transport.onclose());
106
- });
107
- }
108
- wsGuid() {
109
- return this._wsGuid;
110
- }
111
- async start(options = {}) {
112
- (0, _debug.assert)(!this._started, 'server already started');
113
- this._started = true;
114
- const host = options.host || 'localhost';
115
- if (options.preferredPort) {
116
- try {
117
- await this._tryStart(options.preferredPort, host);
118
- } catch (e) {
119
- if (!e || !e.message || !e.message.includes('EADDRINUSE')) throw e;
120
- await this._tryStart(undefined, host);
121
- }
122
- } else {
123
- await this._tryStart(options.port, host);
124
- }
125
- const address = this._server.address();
126
- (0, _debug.assert)(address, 'Could not bind server socket');
127
- if (typeof address === 'string') {
128
- this._urlPrefixPrecise = address;
129
- this._urlPrefixHumanReadable = address;
130
- } else {
131
- this._port = address.port;
132
- const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
133
- this._urlPrefixPrecise = `http://${resolvedHost}:${address.port}`;
134
- this._urlPrefixHumanReadable = `http://${host}:${address.port}`;
135
- }
136
- }
137
- async stop() {
138
- await new Promise(cb => this._server.close(cb));
139
- }
140
- urlPrefix(purpose) {
141
- return purpose === 'human-readable' ? this._urlPrefixHumanReadable : this._urlPrefixPrecise;
142
- }
143
- serveFile(request, response, absoluteFilePath, headers) {
144
- try {
145
- for (const [name, value] of Object.entries(headers || {})) response.setHeader(name, value);
146
- if (request.headers.range) this._serveRangeFile(request, response, absoluteFilePath);else this._serveFile(response, absoluteFilePath);
147
- return true;
148
- } catch (e) {
149
- return false;
150
- }
151
- }
152
- _serveFile(response, absoluteFilePath) {
153
- const content = _fs.default.readFileSync(absoluteFilePath);
154
- response.statusCode = 200;
155
- const contentType = _utilsBundle.mime.getType(_path.default.extname(absoluteFilePath)) || 'application/octet-stream';
156
- response.setHeader('Content-Type', contentType);
157
- response.setHeader('Content-Length', content.byteLength);
158
- response.end(content);
159
- }
160
- _serveRangeFile(request, response, absoluteFilePath) {
161
- const range = request.headers.range;
162
- if (!range || !range.startsWith('bytes=') || range.includes(', ') || [...range].filter(char => char === '-').length !== 1) {
163
- response.statusCode = 400;
164
- return response.end('Bad request');
165
- }
166
-
167
- // Parse the range header: https://datatracker.ietf.org/doc/html/rfc7233#section-2.1
168
- const [startStr, endStr] = range.replace(/bytes=/, '').split('-');
169
-
170
- // Both start and end (when passing to fs.createReadStream) and the range header are inclusive and start counting at 0.
171
- let start;
172
- let end;
173
- const size = _fs.default.statSync(absoluteFilePath).size;
174
- if (startStr !== '' && endStr === '') {
175
- // No end specified: use the whole file
176
- start = +startStr;
177
- end = size - 1;
178
- } else if (startStr === '' && endStr !== '') {
179
- // No start specified: calculate start manually
180
- start = size - +endStr;
181
- end = size - 1;
182
- } else {
183
- start = +startStr;
184
- end = +endStr;
185
- }
186
-
187
- // Handle unavailable range request
188
- if (Number.isNaN(start) || Number.isNaN(end) || start >= size || end >= size || start > end) {
189
- // Return the 416 Range Not Satisfiable: https://datatracker.ietf.org/doc/html/rfc7233#section-4.4
190
- response.writeHead(416, {
191
- 'Content-Range': `bytes */${size}`
192
- });
193
- return response.end();
194
- }
195
-
196
- // Sending Partial Content: https://datatracker.ietf.org/doc/html/rfc7233#section-4.1
197
- response.writeHead(206, {
198
- 'Content-Range': `bytes ${start}-${end}/${size}`,
199
- 'Accept-Ranges': 'bytes',
200
- 'Content-Length': end - start + 1,
201
- 'Content-Type': _utilsBundle.mime.getType(_path.default.extname(absoluteFilePath))
202
- });
203
- const readable = _fs.default.createReadStream(absoluteFilePath, {
204
- start,
205
- end
206
- });
207
- readable.pipe(response);
208
- }
209
- _onRequest(request, response) {
210
- response.setHeader('Access-Control-Allow-Origin', '*');
211
- response.setHeader('Access-Control-Request-Method', '*');
212
- response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
213
- if (request.headers.origin) response.setHeader('Access-Control-Allow-Headers', request.headers.origin);
214
- if (request.method === 'OPTIONS') {
215
- response.writeHead(200);
216
- response.end();
217
- return;
218
- }
219
- request.on('error', () => response.end());
220
- try {
221
- if (!request.url) {
222
- response.end();
223
- return;
224
- }
225
- const url = new URL('http://localhost' + request.url);
226
- for (const route of this._routes) {
227
- if (route.exact && url.pathname === route.exact && route.handler(request, response)) return;
228
- if (route.prefix && url.pathname.startsWith(route.prefix) && route.handler(request, response)) return;
229
- }
230
- response.statusCode = 404;
231
- response.end();
232
- } catch (e) {
233
- response.end();
234
- }
235
- }
236
- }
237
- exports.HttpServer = HttpServer;