@checkly/playwright-core 1.48.23 → 1.48.24-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 (71) hide show
  1. package/lib/generated/clockSource.js +1 -2
  2. package/lib/generated/consoleApiSource.js +1 -2
  3. package/lib/generated/injectedScriptSource.js +1 -2
  4. package/lib/generated/pollingRecorderSource.js +1 -2
  5. package/lib/generated/utilityScriptSource.js +1 -2
  6. package/lib/generated/webSocketMockSource.js +1 -2
  7. package/lib/vite/traceViewer/assets/codeMirrorModule-lDjkI8Ax.js +24 -0
  8. package/lib/vite/traceViewer/assets/inspectorTab-BPzhNk9r.js +64 -0
  9. package/lib/vite/traceViewer/assets/workbench-DLv_q9ji.js +9 -0
  10. package/lib/vite/traceViewer/embedded.BIubxTi3.js +2 -0
  11. package/lib/vite/traceViewer/embedded.html +3 -3
  12. package/lib/vite/traceViewer/index.DPD22sZn.js +2 -0
  13. package/lib/vite/traceViewer/index.html +3 -3
  14. package/lib/vite/traceViewer/recorder.BaRuS6Pc.js +2 -0
  15. package/lib/vite/traceViewer/recorder.html +2 -2
  16. package/lib/vite/traceViewer/uiMode.B11wexdJ.js +5 -0
  17. package/lib/vite/traceViewer/uiMode.html +3 -3
  18. package/package.json +1 -1
  19. package/lib/client/clientStackTrace.js +0 -65
  20. package/lib/client/fileUtils.js +0 -31
  21. package/lib/client/platform.js +0 -71
  22. package/lib/client/timeoutSettings.js +0 -65
  23. package/lib/client/webSocket.js +0 -106
  24. package/lib/server/callLog.js +0 -79
  25. package/lib/server/harBackend.js +0 -157
  26. package/lib/server/localUtils.js +0 -203
  27. package/lib/server/recorder/chat.js +0 -177
  28. package/lib/server/storageScript.js +0 -160
  29. package/lib/server/timeoutSettings.js +0 -74
  30. package/lib/server/utils/ascii.js +0 -31
  31. package/lib/server/utils/comparators.js +0 -159
  32. package/lib/server/utils/crypto.js +0 -171
  33. package/lib/server/utils/debug.js +0 -38
  34. package/lib/server/utils/debugLogger.js +0 -93
  35. package/lib/server/utils/env.js +0 -53
  36. package/lib/server/utils/eventsHelper.js +0 -38
  37. package/lib/server/utils/expectUtils.js +0 -33
  38. package/lib/server/utils/fileUtils.js +0 -204
  39. package/lib/server/utils/happyEyeballs.js +0 -209
  40. package/lib/server/utils/hostPlatform.js +0 -145
  41. package/lib/server/utils/httpServer.js +0 -233
  42. package/lib/server/utils/image_tools/colorUtils.js +0 -98
  43. package/lib/server/utils/image_tools/compare.js +0 -108
  44. package/lib/server/utils/image_tools/imageChannel.js +0 -70
  45. package/lib/server/utils/image_tools/stats.js +0 -102
  46. package/lib/server/utils/linuxUtils.js +0 -58
  47. package/lib/server/utils/network.js +0 -160
  48. package/lib/server/utils/nodePlatform.js +0 -140
  49. package/lib/server/utils/pipeTransport.js +0 -82
  50. package/lib/server/utils/processLauncher.js +0 -248
  51. package/lib/server/utils/profiler.js +0 -52
  52. package/lib/server/utils/socksProxy.js +0 -570
  53. package/lib/server/utils/spawnAsync.js +0 -45
  54. package/lib/server/utils/task.js +0 -58
  55. package/lib/server/utils/userAgent.js +0 -91
  56. package/lib/server/utils/wsServer.js +0 -128
  57. package/lib/server/utils/zipFile.js +0 -75
  58. package/lib/server/utils/zones.js +0 -54
  59. package/lib/utils/isomorphic/ariaSnapshot.js +0 -392
  60. package/lib/utils/isomorphic/assert.js +0 -25
  61. package/lib/utils/isomorphic/colors.js +0 -65
  62. package/lib/utils/isomorphic/headers.js +0 -52
  63. package/lib/utils/isomorphic/manualPromise.js +0 -107
  64. package/lib/utils/isomorphic/multimap.js +0 -73
  65. package/lib/utils/isomorphic/rtti.js +0 -41
  66. package/lib/utils/isomorphic/semaphore.js +0 -51
  67. package/lib/utils/isomorphic/stackTrace.js +0 -169
  68. package/lib/utils/isomorphic/time.js +0 -25
  69. package/lib/utils/isomorphic/timeoutRunner.js +0 -66
  70. package/lib/utils/isomorphic/types.js +0 -5
  71. package/lib/utils.js +0 -447
@@ -1,177 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Chat = void 0;
7
- exports.asString = asString;
8
- var _transport = require("../transport");
9
- /**
10
- * Copyright (c) Microsoft Corporation.
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- class Chat {
26
- constructor(wsEndpoint) {
27
- this._history = [];
28
- this._connectionPromise = void 0;
29
- this._chatSinks = new Map();
30
- this._wsEndpoint = void 0;
31
- this._wsEndpoint = wsEndpoint;
32
- }
33
- clearHistory() {
34
- this._history = [];
35
- }
36
- async post(prompt) {
37
- await this._append('user', prompt);
38
- let text = await asString(await this._post());
39
- if (text.startsWith('```json') && text.endsWith('```')) text = text.substring('```json'.length, text.length - '```'.length);
40
- for (let i = 0; i < 3; ++i) {
41
- try {
42
- return JSON.parse(text);
43
- } catch (e) {
44
- await this._append('user', String(e));
45
- }
46
- }
47
- throw new Error('Failed to parse response: ' + text);
48
- }
49
- async _append(user, content) {
50
- this._history.push({
51
- user,
52
- content
53
- });
54
- }
55
- async _connection() {
56
- if (!this._connectionPromise) {
57
- this._connectionPromise = _transport.WebSocketTransport.connect(undefined, this._wsEndpoint).then(transport => {
58
- return new Connection(transport, (method, params) => this._dispatchEvent(method, params), () => {});
59
- });
60
- }
61
- return this._connectionPromise;
62
- }
63
- _dispatchEvent(method, params) {
64
- if (method === 'chatChunk') {
65
- const {
66
- chatId,
67
- chunk
68
- } = params;
69
- const chunkSink = this._chatSinks.get(chatId);
70
- chunkSink(chunk);
71
- if (!chunk) this._chatSinks.delete(chatId);
72
- }
73
- }
74
- async _post() {
75
- const connection = await this._connection();
76
- const result = await connection.send('chat', {
77
- history: this._history
78
- });
79
- const {
80
- chatId
81
- } = result;
82
- const {
83
- iterable,
84
- addChunk
85
- } = iterablePump();
86
- this._chatSinks.set(chatId, addChunk);
87
- return iterable;
88
- }
89
- }
90
- exports.Chat = Chat;
91
- async function asString(stream) {
92
- let result = '';
93
- for await (const chunk of stream) result += chunk;
94
- return result;
95
- }
96
- function iterablePump() {
97
- let controller;
98
- const stream = new ReadableStream({
99
- start: c => controller = c
100
- });
101
- const iterable = async function* () {
102
- const reader = stream.getReader();
103
- while (true) {
104
- const {
105
- done,
106
- value
107
- } = await reader.read();
108
- if (done) break;
109
- yield value;
110
- }
111
- }();
112
- return {
113
- iterable,
114
- addChunk: chunk => {
115
- if (chunk) controller.enqueue(chunk);else controller.close();
116
- }
117
- };
118
- }
119
- class Connection {
120
- constructor(transport, onEvent, onClose) {
121
- this._transport = void 0;
122
- this._lastId = 0;
123
- this._closed = false;
124
- this._pending = new Map();
125
- this._onEvent = void 0;
126
- this._onClose = void 0;
127
- this._transport = transport;
128
- this._onEvent = onEvent;
129
- this._onClose = onClose;
130
- this._transport.onmessage = this._dispatchMessage.bind(this);
131
- this._transport.onclose = this._close.bind(this);
132
- }
133
- send(method, params) {
134
- const id = this._lastId++;
135
- const message = {
136
- id,
137
- method,
138
- params
139
- };
140
- this._transport.send(message);
141
- return new Promise((resolve, reject) => {
142
- this._pending.set(id, {
143
- resolve,
144
- reject
145
- });
146
- });
147
- }
148
- _dispatchMessage(message) {
149
- if (message.id === undefined) {
150
- this._onEvent(message.method, message.params);
151
- return;
152
- }
153
- const callback = this._pending.get(message.id);
154
- this._pending.delete(message.id);
155
- if (!callback) return;
156
- if (message.error) {
157
- callback.reject(new Error(message.error.message));
158
- return;
159
- }
160
- callback.resolve(message.result);
161
- }
162
- _close() {
163
- this._closed = true;
164
- this._transport.onmessage = undefined;
165
- this._transport.onclose = undefined;
166
- for (const {
167
- reject
168
- } of this._pending.values()) reject(new Error('Connection closed'));
169
- this._onClose();
170
- }
171
- isClosed() {
172
- return this._closed;
173
- }
174
- close() {
175
- if (!this._closed) this._transport.close();
176
- }
177
- }
@@ -1,160 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.collect = collect;
7
- exports.restore = restore;
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
- async function collect(serializers, isFirefox, recordIndexedDB) {
25
- async function collectDB(dbInfo) {
26
- if (!dbInfo.name) throw new Error('Database name is empty');
27
- if (!dbInfo.version) throw new Error('Database version is unset');
28
- function idbRequestToPromise(request) {
29
- return new Promise((resolve, reject) => {
30
- request.addEventListener('success', () => resolve(request.result));
31
- request.addEventListener('error', () => reject(request.error));
32
- });
33
- }
34
- function isPlainObject(v) {
35
- const ctor = v === null || v === void 0 ? void 0 : v.constructor;
36
- if (isFirefox) {
37
- const constructorImpl = ctor === null || ctor === void 0 ? void 0 : ctor.toString();
38
- if (constructorImpl.startsWith('function Object() {') && constructorImpl.includes('[native code]')) return true;
39
- }
40
- return ctor === Object;
41
- }
42
- function trySerialize(value) {
43
- let trivial = true;
44
- const encoded = serializers.serializeAsCallArgument(value, v => {
45
- const isTrivial = isPlainObject(v) || Array.isArray(v) || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || Object.is(v, null);
46
- if (!isTrivial) trivial = false;
47
- return {
48
- fallThrough: v
49
- };
50
- });
51
- if (trivial) return {
52
- trivial: value
53
- };
54
- return {
55
- encoded
56
- };
57
- }
58
- const db = await idbRequestToPromise(indexedDB.open(dbInfo.name));
59
- const transaction = db.transaction(db.objectStoreNames, 'readonly');
60
- const stores = await Promise.all([...db.objectStoreNames].map(async storeName => {
61
- const objectStore = transaction.objectStore(storeName);
62
- const keys = await idbRequestToPromise(objectStore.getAllKeys());
63
- const records = await Promise.all(keys.map(async key => {
64
- const record = {};
65
- if (objectStore.keyPath === null) {
66
- const {
67
- encoded,
68
- trivial
69
- } = trySerialize(key);
70
- if (trivial) record.key = trivial;else record.keyEncoded = encoded;
71
- }
72
- const value = await idbRequestToPromise(objectStore.get(key));
73
- const {
74
- encoded,
75
- trivial
76
- } = trySerialize(value);
77
- if (trivial) record.value = trivial;else record.valueEncoded = encoded;
78
- return record;
79
- }));
80
- const indexes = [...objectStore.indexNames].map(indexName => {
81
- const index = objectStore.index(indexName);
82
- return {
83
- name: index.name,
84
- keyPath: typeof index.keyPath === 'string' ? index.keyPath : undefined,
85
- keyPathArray: Array.isArray(index.keyPath) ? index.keyPath : undefined,
86
- multiEntry: index.multiEntry,
87
- unique: index.unique
88
- };
89
- });
90
- return {
91
- name: storeName,
92
- records: records,
93
- indexes,
94
- autoIncrement: objectStore.autoIncrement,
95
- keyPath: typeof objectStore.keyPath === 'string' ? objectStore.keyPath : undefined,
96
- keyPathArray: Array.isArray(objectStore.keyPath) ? objectStore.keyPath : undefined
97
- };
98
- }));
99
- return {
100
- name: dbInfo.name,
101
- version: dbInfo.version,
102
- stores
103
- };
104
- }
105
- return {
106
- localStorage: Object.keys(localStorage).map(name => ({
107
- name,
108
- value: localStorage.getItem(name)
109
- })),
110
- indexedDB: recordIndexedDB ? await Promise.all((await indexedDB.databases()).map(collectDB)).catch(e => {
111
- throw new Error('Unable to serialize IndexedDB: ' + e.message);
112
- }) : undefined
113
- };
114
- }
115
- async function restore(originState, serializers) {
116
- var _originState$indexedD;
117
- for (const {
118
- name,
119
- value
120
- } of originState.localStorage || []) localStorage.setItem(name, value);
121
- await Promise.all(((_originState$indexedD = originState.indexedDB) !== null && _originState$indexedD !== void 0 ? _originState$indexedD : []).map(async dbInfo => {
122
- const openRequest = indexedDB.open(dbInfo.name, dbInfo.version);
123
- openRequest.addEventListener('upgradeneeded', () => {
124
- const db = openRequest.result;
125
- for (const store of dbInfo.stores) {
126
- var _store$keyPathArray;
127
- const objectStore = db.createObjectStore(store.name, {
128
- autoIncrement: store.autoIncrement,
129
- keyPath: (_store$keyPathArray = store.keyPathArray) !== null && _store$keyPathArray !== void 0 ? _store$keyPathArray : store.keyPath
130
- });
131
- for (const index of store.indexes) {
132
- var _index$keyPathArray;
133
- objectStore.createIndex(index.name, (_index$keyPathArray = index.keyPathArray) !== null && _index$keyPathArray !== void 0 ? _index$keyPathArray : index.keyPath, {
134
- unique: index.unique,
135
- multiEntry: index.multiEntry
136
- });
137
- }
138
- }
139
- });
140
- function idbRequestToPromise(request) {
141
- return new Promise((resolve, reject) => {
142
- request.addEventListener('success', () => resolve(request.result));
143
- request.addEventListener('error', () => reject(request.error));
144
- });
145
- }
146
-
147
- // after `upgradeneeded` finishes, `success` event is fired.
148
- const db = await idbRequestToPromise(openRequest);
149
- const transaction = db.transaction(db.objectStoreNames, 'readwrite');
150
- await Promise.all(dbInfo.stores.map(async store => {
151
- const objectStore = transaction.objectStore(store.name);
152
- await Promise.all(store.records.map(async record => {
153
- var _record$value, _record$key;
154
- await idbRequestToPromise(objectStore.add((_record$value = record.value) !== null && _record$value !== void 0 ? _record$value : serializers.parseEvaluationResultValue(record.valueEncoded), (_record$key = record.key) !== null && _record$key !== void 0 ? _record$key : serializers.parseEvaluationResultValue(record.keyEncoded)));
155
- }));
156
- }));
157
- })).catch(e => {
158
- throw new Error('Unable to restore IndexedDB: ' + e.message);
159
- });
160
- }
@@ -1,74 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.TimeoutSettings = exports.DEFAULT_TIMEOUT = exports.DEFAULT_LAUNCH_TIMEOUT = void 0;
7
- var _debug = require("./utils/debug");
8
- /**
9
- * Copyright 2019 Google Inc. All rights reserved.
10
- * Modifications copyright (c) Microsoft Corporation.
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- // Keep in sync with client.
26
- const DEFAULT_TIMEOUT = exports.DEFAULT_TIMEOUT = 30000;
27
- const DEFAULT_LAUNCH_TIMEOUT = exports.DEFAULT_LAUNCH_TIMEOUT = 3 * 60 * 1000; // 3 minutes
28
-
29
- class TimeoutSettings {
30
- constructor(parent) {
31
- this._parent = void 0;
32
- this._defaultTimeout = void 0;
33
- this._defaultNavigationTimeout = void 0;
34
- this._parent = parent;
35
- }
36
- setDefaultTimeout(timeout) {
37
- this._defaultTimeout = timeout;
38
- }
39
- setDefaultNavigationTimeout(timeout) {
40
- this._defaultNavigationTimeout = timeout;
41
- }
42
- defaultNavigationTimeout() {
43
- return this._defaultNavigationTimeout;
44
- }
45
- defaultTimeout() {
46
- return this._defaultTimeout;
47
- }
48
- navigationTimeout(options) {
49
- if (typeof options.timeout === 'number') return options.timeout;
50
- if (this._defaultNavigationTimeout !== undefined) return this._defaultNavigationTimeout;
51
- if ((0, _debug.debugMode)()) return 0;
52
- if (this._defaultTimeout !== undefined) return this._defaultTimeout;
53
- if (this._parent) return this._parent.navigationTimeout(options);
54
- return DEFAULT_TIMEOUT;
55
- }
56
- timeout(options) {
57
- if (typeof options.timeout === 'number') return options.timeout;
58
- if ((0, _debug.debugMode)()) return 0;
59
- if (this._defaultTimeout !== undefined) return this._defaultTimeout;
60
- if (this._parent) return this._parent.timeout(options);
61
- return DEFAULT_TIMEOUT;
62
- }
63
- static timeout(options) {
64
- if (typeof options.timeout === 'number') return options.timeout;
65
- if ((0, _debug.debugMode)()) return 0;
66
- return DEFAULT_TIMEOUT;
67
- }
68
- static launchTimeout(options) {
69
- if (typeof options.timeout === 'number') return options.timeout;
70
- if ((0, _debug.debugMode)()) return 0;
71
- return DEFAULT_LAUNCH_TIMEOUT;
72
- }
73
- }
74
- exports.TimeoutSettings = TimeoutSettings;
@@ -1,31 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.jsonStringifyForceASCII = jsonStringifyForceASCII;
7
- exports.wrapInASCIIBox = wrapInASCIIBox;
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 wrapInASCIIBox(text, padding = 0) {
25
- const lines = text.split('\n');
26
- const maxLength = Math.max(...lines.map(line => line.length));
27
- return ['╔' + '═'.repeat(maxLength + padding * 2) + '╗', ...lines.map(line => '║' + ' '.repeat(padding) + line + ' '.repeat(maxLength - line.length + padding) + '║'), '╚' + '═'.repeat(maxLength + padding * 2) + '╝'].join('\n');
28
- }
29
- function jsonStringifyForceASCII(object) {
30
- return JSON.stringify(object).replace(/[\u007f-\uffff]/g, c => '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4));
31
- }
@@ -1,159 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.compareBuffersOrStrings = compareBuffersOrStrings;
7
- exports.getComparator = getComparator;
8
- var _compare = require("./image_tools/compare");
9
- var _pixelmatch = _interopRequireDefault(require("../../third_party/pixelmatch"));
10
- var _utilsBundle = require("../../utilsBundle");
11
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
- /**
13
- * Copyright 2017 Google Inc. All rights reserved.
14
- * Modifications copyright (c) Microsoft Corporation.
15
- *
16
- * Licensed under the Apache License, Version 2.0 (the "License");
17
- * you may not use this file except in compliance with the License.
18
- * You may obtain a copy of the License at
19
- *
20
- * http://www.apache.org/licenses/LICENSE-2.0
21
- *
22
- * Unless required by applicable law or agreed to in writing, software
23
- * distributed under the License is distributed on an "AS IS" BASIS,
24
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
- * See the License for the specific language governing permissions and
26
- * limitations under the License.
27
- */
28
-
29
- // @ts-ignore
30
-
31
- function getComparator(mimeType) {
32
- if (mimeType === 'image/png') return compareImages.bind(null, 'image/png');
33
- if (mimeType === 'image/jpeg') return compareImages.bind(null, 'image/jpeg');
34
- if (mimeType === 'text/plain') return compareText;
35
- return compareBuffersOrStrings;
36
- }
37
- const JPEG_JS_MAX_BUFFER_SIZE_IN_MB = 5 * 1024; // ~5 GB
38
-
39
- function compareBuffersOrStrings(actualBuffer, expectedBuffer) {
40
- if (typeof actualBuffer === 'string') return compareText(actualBuffer, expectedBuffer);
41
- if (!actualBuffer || !(actualBuffer instanceof Buffer)) return {
42
- errorMessage: 'Actual result should be a Buffer or a string.'
43
- };
44
- if (Buffer.compare(actualBuffer, expectedBuffer)) return {
45
- errorMessage: 'Buffers differ'
46
- };
47
- return null;
48
- }
49
- function compareImages(mimeType, actualBuffer, expectedBuffer, options = {}) {
50
- var _options$comparator, _ref;
51
- if (!actualBuffer || !(actualBuffer instanceof Buffer)) return {
52
- errorMessage: 'Actual result should be a Buffer.'
53
- };
54
- validateBuffer(expectedBuffer, mimeType);
55
- let actual = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(actualBuffer) : _utilsBundle.jpegjs.decode(actualBuffer, {
56
- maxMemoryUsageInMB: JPEG_JS_MAX_BUFFER_SIZE_IN_MB
57
- });
58
- let expected = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(expectedBuffer) : _utilsBundle.jpegjs.decode(expectedBuffer, {
59
- maxMemoryUsageInMB: JPEG_JS_MAX_BUFFER_SIZE_IN_MB
60
- });
61
- const size = {
62
- width: Math.max(expected.width, actual.width),
63
- height: Math.max(expected.height, actual.height)
64
- };
65
- let sizesMismatchError = '';
66
- if (expected.width !== actual.width || expected.height !== actual.height) {
67
- sizesMismatchError = `Expected an image ${expected.width}px by ${expected.height}px, received ${actual.width}px by ${actual.height}px. `;
68
- actual = resizeImage(actual, size);
69
- expected = resizeImage(expected, size);
70
- }
71
- const diff = new _utilsBundle.PNG({
72
- width: size.width,
73
- height: size.height
74
- });
75
- let count;
76
- if (options.comparator === 'ssim-cie94') {
77
- count = (0, _compare.compare)(expected.data, actual.data, diff.data, size.width, size.height, {
78
- // All ΔE* formulae are originally designed to have the difference of 1.0 stand for a "just noticeable difference" (JND).
79
- // See https://en.wikipedia.org/wiki/Color_difference#CIELAB_%CE%94E*
80
- maxColorDeltaE94: 1.0
81
- });
82
- } else if (((_options$comparator = options.comparator) !== null && _options$comparator !== void 0 ? _options$comparator : 'pixelmatch') === 'pixelmatch') {
83
- var _options$threshold;
84
- count = (0, _pixelmatch.default)(expected.data, actual.data, diff.data, size.width, size.height, {
85
- threshold: (_options$threshold = options.threshold) !== null && _options$threshold !== void 0 ? _options$threshold : 0.2
86
- });
87
- } else {
88
- throw new Error(`Configuration specifies unknown comparator "${options.comparator}"`);
89
- }
90
- const maxDiffPixels1 = options.maxDiffPixels;
91
- const maxDiffPixels2 = options.maxDiffPixelRatio !== undefined ? expected.width * expected.height * options.maxDiffPixelRatio : undefined;
92
- let maxDiffPixels;
93
- if (maxDiffPixels1 !== undefined && maxDiffPixels2 !== undefined) maxDiffPixels = Math.min(maxDiffPixels1, maxDiffPixels2);else maxDiffPixels = (_ref = maxDiffPixels1 !== null && maxDiffPixels1 !== void 0 ? maxDiffPixels1 : maxDiffPixels2) !== null && _ref !== void 0 ? _ref : 0;
94
- const ratio = Math.ceil(count / (expected.width * expected.height) * 100) / 100;
95
- const pixelsMismatchError = count > maxDiffPixels ? `${count} pixels (ratio ${ratio.toFixed(2)} of all image pixels) are different.` : '';
96
- if (pixelsMismatchError || sizesMismatchError) return {
97
- errorMessage: sizesMismatchError + pixelsMismatchError,
98
- diff: _utilsBundle.PNG.sync.write(diff)
99
- };
100
- return null;
101
- }
102
- function validateBuffer(buffer, mimeType) {
103
- if (mimeType === 'image/png') {
104
- const pngMagicNumber = [137, 80, 78, 71, 13, 10, 26, 10];
105
- if (buffer.length < pngMagicNumber.length || !pngMagicNumber.every((byte, index) => buffer[index] === byte)) throw new Error('could not decode image as PNG.');
106
- } else if (mimeType === 'image/jpeg') {
107
- const jpegMagicNumber = [255, 216];
108
- if (buffer.length < jpegMagicNumber.length || !jpegMagicNumber.every((byte, index) => buffer[index] === byte)) throw new Error('could not decode image as JPEG.');
109
- }
110
- }
111
- function compareText(actual, expectedBuffer) {
112
- if (typeof actual !== 'string') return {
113
- errorMessage: 'Actual result should be a string'
114
- };
115
- let expected = expectedBuffer.toString('utf-8');
116
- if (expected === actual) return null;
117
- // Eliminate '\'
118
- if (!actual.endsWith('\n')) actual += '\n';
119
- if (!expected.endsWith('\n')) expected += '\n';
120
- const lines = _utilsBundle.diff.createPatch('file', expected, actual, undefined, undefined, {
121
- context: 5
122
- }).split('\n');
123
- const coloredLines = lines.slice(4).map(line => {
124
- if (line.startsWith('-')) return _utilsBundle.colors.red(line);
125
- if (line.startsWith('+')) return _utilsBundle.colors.green(line);
126
- if (line.startsWith('@@')) return _utilsBundle.colors.dim(line);
127
- return line;
128
- });
129
- const errorMessage = coloredLines.join('\n');
130
- return {
131
- errorMessage
132
- };
133
- }
134
- function resizeImage(image, size) {
135
- if (image.width === size.width && image.height === size.height) return image;
136
- const buffer = new Uint8Array(size.width * size.height * 4);
137
- for (let y = 0; y < size.height; y++) {
138
- for (let x = 0; x < size.width; x++) {
139
- const to = (y * size.width + x) * 4;
140
- if (y < image.height && x < image.width) {
141
- const from = (y * image.width + x) * 4;
142
- buffer[to] = image.data[from];
143
- buffer[to + 1] = image.data[from + 1];
144
- buffer[to + 2] = image.data[from + 2];
145
- buffer[to + 3] = image.data[from + 3];
146
- } else {
147
- buffer[to] = 0;
148
- buffer[to + 1] = 0;
149
- buffer[to + 2] = 0;
150
- buffer[to + 3] = 0;
151
- }
152
- }
153
- }
154
- return {
155
- data: Buffer.from(buffer),
156
- width: size.width,
157
- height: size.height
158
- };
159
- }