browsertime 16.16.0 → 17.0.0-beta.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.
Files changed (121) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/bin/browsertime.js +43 -43
  3. package/bin/browsertimeWebPageReplay.js +10 -9
  4. package/browserscripts/pageinfo/generator.js +1 -1
  5. package/index.js +14 -13
  6. package/lib/android/gnirehtet.js +14 -15
  7. package/lib/android/index.js +92 -89
  8. package/lib/android/root.js +6 -9
  9. package/lib/chrome/chromeDevtoolsProtocol.js +46 -45
  10. package/lib/chrome/har.js +12 -13
  11. package/lib/chrome/longTaskMetrics.js +3 -5
  12. package/lib/chrome/parseCpuTrace.js +13 -10
  13. package/lib/chrome/settings/chromeAndroidOptions.js +1 -3
  14. package/lib/chrome/settings/chromeDesktopOptions.js +1 -3
  15. package/lib/chrome/settings/traceCategories.js +1 -3
  16. package/lib/chrome/traceCategoriesParser.js +35 -36
  17. package/lib/chrome/webdriver/builder.js +18 -26
  18. package/lib/chrome/webdriver/chromium.js +56 -46
  19. package/lib/chrome/webdriver/setupChromiumOptions.js +32 -27
  20. package/lib/connectivity/humble.js +13 -14
  21. package/lib/connectivity/index.js +74 -77
  22. package/lib/connectivity/trafficShapeParser.js +31 -35
  23. package/lib/core/engine/collector.js +25 -22
  24. package/lib/core/engine/command/addText.js +24 -27
  25. package/lib/core/engine/command/android.js +7 -11
  26. package/lib/core/engine/command/cache.js +3 -4
  27. package/lib/core/engine/command/chromeDevToolsProtocol.js +20 -20
  28. package/lib/core/engine/command/click.js +48 -49
  29. package/lib/core/engine/command/debug.js +3 -4
  30. package/lib/core/engine/command/javaScript.js +18 -19
  31. package/lib/core/engine/command/measure.js +31 -37
  32. package/lib/core/engine/command/meta.js +1 -4
  33. package/lib/core/engine/command/mouse/clickAndHold.js +36 -39
  34. package/lib/core/engine/command/mouse/contextClick.js +17 -18
  35. package/lib/core/engine/command/mouse/doubleClick.js +17 -19
  36. package/lib/core/engine/command/mouse/index.js +5 -9
  37. package/lib/core/engine/command/mouse/mouseMove.js +19 -23
  38. package/lib/core/engine/command/mouse/singleClick.js +15 -19
  39. package/lib/core/engine/command/navigation.js +12 -14
  40. package/lib/core/engine/command/screenshot.js +1 -4
  41. package/lib/core/engine/command/scroll.js +4 -7
  42. package/lib/core/engine/command/select.js +26 -26
  43. package/lib/core/engine/command/set.js +25 -23
  44. package/lib/core/engine/command/stopWatch.js +5 -8
  45. package/lib/core/engine/command/switch.js +18 -20
  46. package/lib/core/engine/command/util/lcpHighlightScript.js +1 -2
  47. package/lib/core/engine/command/util/lsHighlightScript.js +1 -2
  48. package/lib/core/engine/command/wait.js +18 -18
  49. package/lib/core/engine/index.js +81 -71
  50. package/lib/core/engine/iteration.js +93 -79
  51. package/lib/core/engine/run.js +5 -9
  52. package/lib/core/pageCompleteChecks/defaultPageCompleteCheck.js +1 -2
  53. package/lib/core/pageCompleteChecks/pageCompleteCheckByInactivity.js +1 -3
  54. package/lib/core/pageCompleteChecks/spaInactivity.js +1 -3
  55. package/lib/core/seleniumRunner.js +92 -100
  56. package/lib/core/webdriver/index.js +24 -21
  57. package/lib/edge/webdriver/builder.js +21 -26
  58. package/lib/extensionserver/httpServer.js +17 -21
  59. package/lib/extensionserver/index.js +8 -12
  60. package/lib/extensionserver/setup.js +12 -12
  61. package/lib/firefox/geckoProfiler.js +27 -21
  62. package/lib/firefox/getHAR.js +7 -8
  63. package/lib/firefox/memoryReport.js +9 -11
  64. package/lib/firefox/perfStats.js +5 -7
  65. package/lib/firefox/settings/disableSafeBrowsingPreferences.js +1 -3
  66. package/lib/firefox/settings/disableTrackingProtectionPreferences.js +1 -3
  67. package/lib/firefox/settings/firefoxPreferences.js +2 -4
  68. package/lib/firefox/settings/geckoProfilerDefaults.js +5 -9
  69. package/lib/firefox/webdriver/builder.js +66 -63
  70. package/lib/firefox/webdriver/firefox.js +43 -48
  71. package/lib/safari/webdriver/builder.js +4 -5
  72. package/lib/safari/webdriver/safari.js +4 -8
  73. package/lib/screenshot/defaults.js +1 -3
  74. package/lib/screenshot/index.js +24 -21
  75. package/lib/support/browserScript.js +65 -56
  76. package/lib/support/cli.js +123 -85
  77. package/lib/support/dns.js +6 -7
  78. package/lib/support/engineUtils.js +85 -38
  79. package/lib/support/errors.js +4 -13
  80. package/lib/support/fileUtil.js +81 -68
  81. package/lib/support/filters.js +15 -20
  82. package/lib/support/getViewPort.js +6 -7
  83. package/lib/support/har/index.js +208 -216
  84. package/lib/support/images/index.js +54 -26
  85. package/lib/support/logging.js +20 -17
  86. package/lib/support/pathToFolder.js +18 -23
  87. package/lib/support/preURL.js +5 -6
  88. package/lib/support/processes.js +4 -6
  89. package/lib/support/setResourceTimingBufferSize.js +2 -4
  90. package/lib/support/statistics.js +58 -56
  91. package/lib/support/stop.js +7 -8
  92. package/lib/support/storageManager.js +44 -44
  93. package/lib/support/tcpdump.js +22 -16
  94. package/lib/support/userTiming.js +7 -13
  95. package/lib/support/util.js +204 -224
  96. package/lib/support/xvfb.js +11 -13
  97. package/lib/video/defaults.js +6 -10
  98. package/lib/video/postprocessing/finetune/addTextToVideo.js +15 -15
  99. package/lib/video/postprocessing/finetune/convertFps.js +14 -8
  100. package/lib/video/postprocessing/finetune/getFont.js +7 -9
  101. package/lib/video/postprocessing/finetune/getTimingMetrics.js +19 -18
  102. package/lib/video/postprocessing/finetune/index.js +40 -36
  103. package/lib/video/postprocessing/finetune/removeOrange.js +10 -11
  104. package/lib/video/postprocessing/visualmetrics/extraMetrics.js +17 -13
  105. package/lib/video/postprocessing/visualmetrics/getVideoMetrics.js +12 -13
  106. package/lib/video/postprocessing/visualmetrics/visualMetrics.js +117 -128
  107. package/lib/video/screenRecording/android/recorder.js +12 -17
  108. package/lib/video/screenRecording/desktop/convert.js +13 -11
  109. package/lib/video/screenRecording/desktop/desktopRecorder.js +29 -24
  110. package/lib/video/screenRecording/desktop/ffmpegRecorder.js +60 -63
  111. package/lib/video/screenRecording/desktop/osx/getSPDisplaysDataType.js +4 -6
  112. package/lib/video/screenRecording/desktop/osx/getScreen.js +9 -10
  113. package/lib/video/screenRecording/firefox/firefoxWindowRecorder.js +54 -54
  114. package/lib/video/screenRecording/ios/convertToMp4.js +10 -11
  115. package/lib/video/screenRecording/ios/iosRecorder.js +23 -21
  116. package/lib/video/screenRecording/iosSimulator/convertToMp4.js +10 -11
  117. package/lib/video/screenRecording/iosSimulator/recorder.js +10 -9
  118. package/lib/video/screenRecording/recorder.js +8 -9
  119. package/lib/video/screenRecording/setOrangeBackground.js +5 -6
  120. package/lib/video/video.js +28 -34
  121. package/package.json +19 -17
@@ -1,13 +1,14 @@
1
- 'use strict';
2
-
3
- const merge = require('lodash.merge');
4
- const pick = require('lodash.pick');
5
- const isEmpty = require('lodash.isempty');
6
- const get = require('lodash.get');
7
- const dayjs = require('dayjs');
8
- const version = require('../../../package').version;
9
- const pathToFolder = require('../pathToFolder');
10
- const log = require('intel').getLogger('browsertime');
1
+ import { createRequire } from 'node:module';
2
+ import merge from 'lodash.merge';
3
+ import pick from 'lodash.pick';
4
+ import isEmpty from 'lodash.isempty';
5
+ import get from 'lodash.get';
6
+ import dayjs from 'dayjs';
7
+ import pathToFolder from '../pathToFolder.js';
8
+ import intel from 'intel';
9
+ const log = intel.getLogger('browsertime');
10
+ const require = createRequire(import.meta.url);
11
+ const version = require('../../../package.json').version;
11
12
 
12
13
  function generateUniquePageId(baseId, existingIdMap) {
13
14
  let newId = baseId;
@@ -19,9 +20,9 @@ function generateUniquePageId(baseId, existingIdMap) {
19
20
  }
20
21
 
21
22
  function getDocumentRequests(entries, pageId) {
22
- let pageEntries = Array.from(entries);
23
+ let pageEntries = [...entries];
23
24
  if (pageId) {
24
- pageEntries = Array.from(entries.filter(entry => entry.pageref === pageId));
25
+ pageEntries = [...entries.filter(entry => entry.pageref === pageId)];
25
26
  }
26
27
 
27
28
  const requests = [];
@@ -61,7 +62,7 @@ function addExtrasToHAR(
61
62
  // We add the timings both as a hidden field and add
62
63
  // in pageTimings so we can easily show them in PerfCascade
63
64
  if (visualMetricsData) {
64
- const DO_NOT_INCLUDE_IN_HAR_TIMINGS = [
65
+ const DO_NOT_INCLUDE_IN_HAR_TIMINGS = new Set([
65
66
  'VisualReadiness',
66
67
  'videoRecordingStart',
67
68
  'SpeedIndex',
@@ -70,10 +71,10 @@ function addExtrasToHAR(
70
71
  'VisualProgress',
71
72
  'ContentfulSpeedIndexProgress',
72
73
  'PerceptualSpeedIndexProgress'
73
- ];
74
+ ]);
74
75
 
75
76
  for (let key of Object.keys(visualMetricsData)) {
76
- if (DO_NOT_INCLUDE_IN_HAR_TIMINGS.indexOf(key) === -1) {
77
+ if (!DO_NOT_INCLUDE_IN_HAR_TIMINGS.has(key)) {
77
78
  harPageTimings['_' + key.charAt(0).toLowerCase() + key.slice(1)] =
78
79
  visualMetricsData[key];
79
80
  _visualMetrics[key] = visualMetricsData[key];
@@ -157,183 +158,214 @@ function jsonifyVisualProgress(visualProgress) {
157
158
  return visualProgressJSON;
158
159
  }
159
160
 
160
- module.exports = {
161
- addBrowser: function (har, name, version, comment) {
162
- merge(har.log, {
163
- browser: {
164
- name,
165
- version,
166
- comment
167
- }
168
- });
169
-
170
- if (!comment) {
171
- delete har.log.browser.comment;
161
+ export function addBrowser(har, name, version, comment) {
162
+ merge(har.log, {
163
+ browser: {
164
+ name,
165
+ version,
166
+ comment
172
167
  }
168
+ });
173
169
 
174
- return har;
175
- },
176
-
177
- addCreator: function (har, comment) {
178
- merge(har.log, {
179
- creator: {
180
- name: 'Browsertime',
181
- version: version,
182
- comment: comment
183
- }
184
- });
170
+ if (!comment) {
171
+ delete har.log.browser.comment;
172
+ }
185
173
 
186
- if (!comment) {
187
- delete har.log.creator.comment;
174
+ return har;
175
+ }
176
+ export function addCreator(har, comment) {
177
+ merge(har.log, {
178
+ creator: {
179
+ name: 'Browsertime',
180
+ version: version,
181
+ comment: comment
188
182
  }
183
+ });
189
184
 
190
- return har;
191
- },
185
+ if (!comment) {
186
+ delete har.log.creator.comment;
187
+ }
192
188
 
193
- getMainDocumentTimings: function (har) {
194
- const timings = [];
195
- const entries = Array.from(har.log.entries);
189
+ return har;
190
+ }
191
+ export function getMainDocumentTimings(har) {
192
+ const timings = [];
193
+ const entries = [...har.log.entries];
196
194
 
197
- for (let page of har.log.pages) {
198
- const pageId = page.id;
199
- const url = page._url;
195
+ for (let page of har.log.pages) {
196
+ const pageId = page.id;
197
+ const url = page._url;
200
198
 
201
- let pageEntries = Array.from(entries);
202
- const finalURL = getFinalURL(pageEntries, pageId);
199
+ let pageEntries = [...entries];
200
+ const finalURL = getFinalURL(pageEntries, pageId);
203
201
 
204
- pageEntries = Array.from(
205
- pageEntries.filter(
206
- entry => entry.pageref === pageId && entry.request.url === finalURL
207
- )
208
- );
202
+ pageEntries = [
203
+ ...pageEntries.filter(
204
+ entry => entry.pageref === pageId && entry.request.url === finalURL
205
+ )
206
+ ];
209
207
 
210
- timings.push({ url, timings: pageEntries[0].timings });
208
+ timings.push({ url, timings: pageEntries[0].timings });
209
+ }
210
+ return timings;
211
+ }
212
+ export function getFullyLoaded(har) {
213
+ const fullyLoaded = [];
214
+ const entries = [...har.log.entries];
215
+
216
+ for (let page of har.log.pages) {
217
+ const pageStartDateTime = new Date(page.startedDateTime).getTime();
218
+ const pageId = page.id;
219
+ const url = page._url;
220
+
221
+ let pageEntries = [...entries];
222
+ pageEntries = [...pageEntries.filter(entry => entry.pageref === pageId)];
223
+
224
+ let pageEnd = 0;
225
+ for (let entry of pageEntries) {
226
+ let entryEnd =
227
+ new Date(entry.startedDateTime).getTime() +
228
+ entry.time -
229
+ new Date(pageStartDateTime).getTime();
230
+ if (entryEnd > pageEnd) {
231
+ pageEnd = entryEnd;
232
+ }
211
233
  }
212
- return timings;
213
- },
214
- getFullyLoaded: function (har) {
215
- const fullyLoaded = [];
216
- const entries = Array.from(har.log.entries);
217
-
218
- for (let page of har.log.pages) {
219
- const pageStartDateTime = new Date(page.startedDateTime).getTime();
220
- const pageId = page.id;
221
- const url = page._url;
222
-
223
- let pageEntries = Array.from(entries);
224
- pageEntries = Array.from(
225
- pageEntries.filter(entry => entry.pageref === pageId)
226
- );
227
-
228
- let pageEnd = 0;
229
- for (let entry of pageEntries) {
230
- let entryEnd =
231
- new Date(entry.startedDateTime).getTime() +
232
- entry.time -
233
- new Date(pageStartDateTime).getTime();
234
- if (entryEnd > pageEnd) {
235
- pageEnd = entryEnd;
236
- }
234
+ fullyLoaded.push({ url, fullyLoaded: Number(pageEnd.toFixed(0)) });
235
+ }
236
+ return fullyLoaded;
237
+ }
238
+ export function mergeHars(hars) {
239
+ if (isEmpty(hars)) {
240
+ return;
241
+ }
242
+ if (hars.length === 1) {
243
+ return hars[0];
244
+ }
245
+ let firstLog = hars[0].log;
246
+ let combinedHar = {
247
+ log: pick(firstLog, ['version', 'creator', 'browser', 'comment'])
248
+ };
249
+ let pagesById = new Map();
250
+ let allEntries = [];
251
+
252
+ for (const har of hars) {
253
+ let pages = har.log.pages;
254
+ let entries = har.log.entries;
255
+ for (const page of pages) {
256
+ let pageId = page.id;
257
+ if (pagesById.has(pageId)) {
258
+ const oldPageId = pageId;
259
+ pageId = generateUniquePageId(oldPageId, pagesById);
260
+ page.id = pageId;
261
+ entries = entries.map(entry => {
262
+ if (entry.pageref === oldPageId) {
263
+ entry.pageref = pageId;
264
+ }
265
+ return entry;
266
+ });
237
267
  }
238
- fullyLoaded.push({ url, fullyLoaded: Number(pageEnd.toFixed(0)) });
268
+ pagesById.set(pageId, page);
239
269
  }
240
- return fullyLoaded;
241
- },
270
+ allEntries = [...allEntries, ...entries];
271
+ }
242
272
 
243
- mergeHars: function (hars) {
244
- if (isEmpty(hars)) {
245
- return undefined;
246
- }
247
- if (hars.length === 1) {
248
- return hars[0];
249
- }
250
- let firstLog = hars[0].log;
251
- let combinedHar = {
252
- log: pick(firstLog, ['version', 'creator', 'browser', 'comment'])
253
- };
254
- let pagesById = new Map();
255
- let allEntries = [];
256
-
257
- hars.forEach(har => {
258
- let pages = har.log.pages;
259
- let entries = har.log.entries;
260
- pages.forEach(page => {
261
- let pageId = page.id;
262
- if (pagesById.has(pageId)) {
263
- const oldPageId = pageId;
264
- pageId = generateUniquePageId(oldPageId, pagesById);
265
- page.id = pageId;
266
- entries = entries.map(entry => {
267
- if (entry.pageref === oldPageId) {
268
- entry.pageref = pageId;
269
- }
270
- return entry;
271
- });
272
- }
273
- pagesById.set(pageId, page);
274
- });
275
- allEntries = allEntries.concat(entries);
276
- });
277
-
278
- combinedHar.log.pages = Array.from(pagesById.values());
279
- combinedHar.log.entries = allEntries;
280
-
281
- return combinedHar;
282
- },
283
-
284
- getEmptyHAR(url, browser) {
285
- return {
286
- log: {
287
- version: '1.2',
288
- creator: {
289
- name: 'Browsertime',
290
- version: version,
291
- comment: ''
292
- },
293
- browser: {
294
- name: browser,
295
- version: ''
296
- },
297
- pages: [
298
- {
299
- startedDateTime: dayjs().format(),
300
- id: 'failing_page',
301
- title: url,
302
- pageTimings: {},
303
- comment: ''
304
- }
305
- ],
306
- entries: [],
273
+ combinedHar.log.pages = [...pagesById.values()];
274
+ combinedHar.log.entries = allEntries;
275
+
276
+ return combinedHar;
277
+ }
278
+ export function getEmptyHAR(url, browser) {
279
+ return {
280
+ log: {
281
+ version: '1.2',
282
+ creator: {
283
+ name: 'Browsertime',
284
+ version: version,
307
285
  comment: ''
308
- }
309
- };
310
- },
311
- addExtraFieldsToHar(totalResults, har, options) {
312
- if (har) {
313
- let harPageNumber = 0;
314
- // We test one page
315
- // Let's do a better fix for this later on
316
- // right now this fixes https://github.com/sitespeedio/browsertime/issues/754
317
- if (har.log.pages.length === options.iterations) {
318
- for (let harPage of har.log.pages) {
319
- let pageNumber = harPageNumber % totalResults.length;
320
- const visualMetric =
321
- totalResults[pageNumber].visualMetrics[harPageNumber];
322
- const browserScript =
323
- totalResults[pageNumber].browserScripts[harPageNumber];
324
- const cpu = totalResults[pageNumber].cpu[harPageNumber];
325
- const googleWebVitals =
326
- totalResults[pageNumber].googleWebVitals[harPageNumber];
327
-
328
- addMetaToHAR(
329
- harPageNumber,
286
+ },
287
+ browser: {
288
+ name: browser,
289
+ version: ''
290
+ },
291
+ pages: [
292
+ {
293
+ startedDateTime: dayjs().format(),
294
+ id: 'failing_page',
295
+ title: url,
296
+ pageTimings: {},
297
+ comment: ''
298
+ }
299
+ ],
300
+ entries: [],
301
+ comment: ''
302
+ }
303
+ };
304
+ }
305
+ export function addExtraFieldsToHar(totalResults, har, options) {
306
+ if (har) {
307
+ let harPageNumber = 0;
308
+ // We test one page
309
+ // Let's do a better fix for this later on
310
+ // right now this fixes https://github.com/sitespeedio/browsertime/issues/754
311
+ if (har.log.pages.length === options.iterations) {
312
+ for (let harPage of har.log.pages) {
313
+ let pageNumber = harPageNumber % totalResults.length;
314
+ const visualMetric =
315
+ totalResults[pageNumber].visualMetrics[harPageNumber];
316
+ const browserScript =
317
+ totalResults[pageNumber].browserScripts[harPageNumber];
318
+ const cpu = totalResults[pageNumber].cpu[harPageNumber];
319
+ const googleWebVitals =
320
+ totalResults[pageNumber].googleWebVitals[harPageNumber];
321
+
322
+ addMetaToHAR(
323
+ harPageNumber,
324
+ harPage,
325
+ totalResults[pageNumber].info.url,
326
+ browserScript,
327
+ options
328
+ );
329
+
330
+ if (browserScript) {
331
+ addExtrasToHAR(
330
332
  harPage,
331
- totalResults[pageNumber].info.url,
332
- browserScript,
333
- options
333
+ visualMetric,
334
+ browserScript.timings,
335
+ cpu,
336
+ googleWebVitals
334
337
  );
335
-
336
- if (browserScript) {
338
+ }
339
+ harPageNumber++;
340
+ }
341
+ } else {
342
+ const numberOfPages = totalResults.length;
343
+ for (let page = 0; page < numberOfPages; page++) {
344
+ for (let iteration = 0; iteration < options.iterations; iteration++) {
345
+ let harIndex = iteration * totalResults.length;
346
+ harIndex += page;
347
+
348
+ const visualMetric = totalResults[page].visualMetrics[iteration];
349
+ const browserScript = totalResults[page].browserScripts[iteration];
350
+ const cpu = totalResults[page].cpu[iteration];
351
+ const googleWebVitals = totalResults[page].googleWebVitals[iteration];
352
+ const harPage = har.log.pages[harIndex];
353
+ // Only add meta if we have a HAR
354
+ if (harPage) {
355
+ addMetaToHAR(
356
+ iteration,
357
+ harPage,
358
+ totalResults[page].info.url,
359
+ browserScript,
360
+ options
361
+ );
362
+ } else {
363
+ log.error(
364
+ 'Could not add meta data to the HAR, miss page ' + harIndex
365
+ );
366
+ }
367
+ // Only add the metrics if we was able to collect the metrics and have a HAR
368
+ if (browserScript && harPage) {
337
369
  addExtrasToHAR(
338
370
  harPage,
339
371
  visualMetric,
@@ -342,48 +374,8 @@ module.exports = {
342
374
  googleWebVitals
343
375
  );
344
376
  }
345
- harPageNumber++;
346
- }
347
- } else {
348
- const numberOfPages = totalResults.length;
349
- for (let page = 0; page < numberOfPages; page++) {
350
- for (let iteration = 0; iteration < options.iterations; iteration++) {
351
- let harIndex = iteration * totalResults.length;
352
- harIndex += page;
353
-
354
- const visualMetric = totalResults[page].visualMetrics[iteration];
355
- const browserScript = totalResults[page].browserScripts[iteration];
356
- const cpu = totalResults[page].cpu[iteration];
357
- const googleWebVitals =
358
- totalResults[page].googleWebVitals[iteration];
359
- const harPage = har.log.pages[harIndex];
360
- // Only add meta if we have a HAR
361
- if (harPage) {
362
- addMetaToHAR(
363
- iteration,
364
- harPage,
365
- totalResults[page].info.url,
366
- browserScript,
367
- options
368
- );
369
- } else {
370
- log.error(
371
- 'Could not add meta data to the HAR, miss page ' + harIndex
372
- );
373
- }
374
- // Only add the metrics if we was able to collect the metrics and have a HAR
375
- if (browserScript && harPage) {
376
- addExtrasToHAR(
377
- harPage,
378
- visualMetric,
379
- browserScript.timings,
380
- cpu,
381
- googleWebVitals
382
- );
383
- }
384
- }
385
377
  }
386
378
  }
387
379
  }
388
380
  }
389
- };
381
+ }
@@ -1,25 +1,38 @@
1
- 'use strict';
1
+ import { join } from 'node:path';
2
+ import pathToFolder from '../pathToFolder.js';
2
3
 
3
- const pathToFolder = require('../pathToFolder');
4
- const path = require('path');
5
-
6
- let jimp;
7
- try {
8
- jimp = require('jimp');
9
- } catch (e) {
10
- jimp = null;
4
+ export async function savePngWithoutResize(
5
+ name,
6
+ data,
7
+ url,
8
+ storageManager,
9
+ dir,
10
+ options
11
+ ) {
12
+ return storageManager.writeData(
13
+ `${name}.png`,
14
+ data,
15
+ join(pathToFolder(url, options), dir)
16
+ );
11
17
  }
18
+ export async function savePng(
19
+ name,
20
+ data,
21
+ url,
22
+ storageManager,
23
+ config,
24
+ dir,
25
+ options
26
+ ) {
27
+ let jimp;
28
+ try {
29
+ jimp = await import('jimp');
30
+ } catch {
31
+ jimp = undefined;
32
+ }
12
33
 
13
- module.exports = {
14
- async savePngWithoutResize(name, data, url, storageManager, dir, options) {
15
- return storageManager.writeData(
16
- `${name}.png`,
17
- data,
18
- path.join(pathToFolder(url, options), dir)
19
- );
20
- },
21
- async savePng(name, data, url, storageManager, config, dir, options) {
22
- const buffer = await jimp.read(data).then(image => {
34
+ if (jimp) {
35
+ const buffer = await jimp.default.read(data).then(image => {
23
36
  return image
24
37
  .deflateLevel(config.png.compressionLevel)
25
38
  .scaleToFit(config.maxSize, config.maxSize)
@@ -29,12 +42,27 @@ module.exports = {
29
42
  return storageManager.writeData(
30
43
  `${name}.png`,
31
44
  buffer,
32
- path.join(pathToFolder(url, options), dir)
45
+ join(pathToFolder(url, options), dir)
33
46
  );
34
- },
35
-
36
- async saveJpg(name, data, url, storageManager, config, dir, options) {
37
- const buffer = await jimp.read(data).then(image => {
47
+ }
48
+ }
49
+ export async function saveJpg(
50
+ name,
51
+ data,
52
+ url,
53
+ storageManager,
54
+ config,
55
+ dir,
56
+ options
57
+ ) {
58
+ let jimp;
59
+ try {
60
+ jimp = await import('jimp');
61
+ } catch {
62
+ jimp = undefined;
63
+ }
64
+ if (jimp) {
65
+ const buffer = await jimp.default.read(data).then(image => {
38
66
  return image
39
67
  .quality(config.jpg.quality)
40
68
  .scaleToFit(config.maxSize, config.maxSize)
@@ -43,7 +71,7 @@ module.exports = {
43
71
  return storageManager.writeData(
44
72
  `${name}.jpg`,
45
73
  buffer,
46
- path.join(pathToFolder(url, options), dir)
74
+ join(pathToFolder(url, options), dir)
47
75
  );
48
76
  }
49
- };
77
+ }
@@ -1,38 +1,41 @@
1
- 'use strict';
1
+ import intel from 'intel';
2
+ const { INFO, DEBUG, VERBOSE, TRACE, NONE, basicConfig } = intel;
2
3
 
3
- const log = require('intel');
4
-
5
- module.exports.configure = function configure(options) {
4
+ export default function configure(options) {
6
5
  options = options || {};
7
6
 
8
- let level = log.INFO;
7
+ let level = INFO;
9
8
  switch (options.verbose) {
10
- case 1:
11
- level = log.DEBUG;
9
+ case 1: {
10
+ level = DEBUG;
12
11
  break;
13
- case 2:
14
- level = log.VERBOSE;
12
+ }
13
+ case 2: {
14
+ level = VERBOSE;
15
15
  break;
16
- case 3:
17
- level = log.TRACE;
16
+ }
17
+ case 3: {
18
+ level = TRACE;
18
19
  break;
19
- default:
20
+ }
21
+ default: {
20
22
  break;
23
+ }
21
24
  }
22
25
 
23
26
  if (options.silent) {
24
- level = log.NONE;
27
+ level = NONE;
25
28
  }
26
29
 
27
- if (level === log.INFO) {
28
- log.basicConfig({
30
+ if (level === INFO) {
31
+ basicConfig({
29
32
  format: '[%(date)s] %(levelname)s: %(message)s',
30
33
  level: level
31
34
  });
32
35
  } else {
33
- log.basicConfig({
36
+ basicConfig({
34
37
  format: '[%(date)s] %(levelname)s: [%(name)s] %(message)s',
35
38
  level: level
36
39
  });
37
40
  }
38
- };
41
+ }