@lvce-editor/main-process 6.37.1 → 6.37.3

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.
@@ -6459,6 +6459,8 @@ const ElectronBrowserViewEventListenerLogin = {
6459
6459
  key: key$4
6460
6460
  };
6461
6461
 
6462
+ const maxFaviconBytes = 1024 * 1024;
6463
+ const faviconFetchTimeout = 2000;
6462
6464
  const key$3 = PageFaviconUpdated;
6463
6465
  const attach$5 = (webContents, listener) => {
6464
6466
  webContents.on(PageFaviconUpdated, listener);
@@ -6466,9 +6468,73 @@ const attach$5 = (webContents, listener) => {
6466
6468
  const detach$3 = (webContents, listener) => {
6467
6469
  webContents.off(PageFaviconUpdated, listener);
6468
6470
  };
6469
- const handler$3 = (event, favicons) => {
6471
+ const getFaviconDataUrl = async (fetcher, favicon, signal) => {
6472
+ try {
6473
+ const response = await fetcher(favicon, {
6474
+ signal
6475
+ });
6476
+ if (!response.ok) {
6477
+ return '';
6478
+ }
6479
+ const contentLength = Number(response.headers.get('content-length'));
6480
+ if (contentLength > maxFaviconBytes) {
6481
+ return '';
6482
+ }
6483
+ const bytes = Buffer.from(await response.arrayBuffer());
6484
+ if (bytes.byteLength > maxFaviconBytes) {
6485
+ return '';
6486
+ }
6487
+ const contentType = response.headers.get('content-type') || 'image/x-icon';
6488
+ const mimeType = contentType.split(';', 1)[0].trim();
6489
+ if (!mimeType.startsWith('image/') && mimeType !== 'application/octet-stream') {
6490
+ return '';
6491
+ }
6492
+ return `data:${mimeType};base64,${bytes.toString('base64')}`;
6493
+ } catch {
6494
+ return '';
6495
+ }
6496
+ };
6497
+ const resolveWithFetcher = async (fetcher, favicons, signal) => {
6498
+ for (const favicon of favicons) {
6499
+ const dataUrl = await getFaviconDataUrl(fetcher, favicon, signal);
6500
+ if (dataUrl) {
6501
+ return dataUrl;
6502
+ }
6503
+ }
6504
+ return '';
6505
+ };
6506
+ const resolveWithTimeout = async (fetcher, favicons) => {
6507
+ const controller = new AbortController();
6508
+ let timeout;
6509
+ const timeoutPromise = new Promise(resolve => {
6510
+ timeout = setTimeout(() => {
6511
+ controller.abort();
6512
+ resolve('');
6513
+ }, faviconFetchTimeout);
6514
+ });
6515
+ try {
6516
+ return await Promise.race([resolveWithFetcher(fetcher, favicons, controller.signal), timeoutPromise]);
6517
+ } finally {
6518
+ clearTimeout(timeout);
6519
+ }
6520
+ };
6521
+ const resolveFavicon = async (event, favicons) => {
6522
+ const dataUrl = favicons.find(favicon => favicon.startsWith('data:'));
6523
+ if (dataUrl) {
6524
+ return [dataUrl];
6525
+ }
6526
+ const sessionFavicon = await resolveWithTimeout((url, options) => event.sender.session.fetch(url, options), favicons);
6527
+ if (sessionFavicon) {
6528
+ return [sessionFavicon];
6529
+ }
6530
+ const networkFavicon = await resolveWithTimeout((url, options) => fetch(url, options), favicons);
6531
+ return networkFavicon ? [networkFavicon] : favicons;
6532
+ };
6533
+ const handler$3 = async (event, favicons) => {
6534
+ const pageUrl = event.sender.getURL();
6535
+ const resolvedFavicons = await resolveFavicon(event, favicons);
6470
6536
  return {
6471
- messages: [['handlePageFaviconUpdated', favicons]],
6537
+ messages: event.sender.getURL() === pageUrl ? [['handlePageFaviconUpdated', resolvedFavicons]] : [],
6472
6538
  result: undefined
6473
6539
  };
6474
6540
  };
@@ -6786,18 +6852,31 @@ const attachEventListenersToWebContents = (webContentsId, webContents, browserWi
6786
6852
  attach(webContents);
6787
6853
  const values = Object.values(ElectronBrowserViewEventListeners);
6788
6854
  for (const value of values) {
6789
- const wrappedListener = (...args) => {
6790
- // @ts-ignore
6791
- const {
6792
- messages,
6793
- result
6794
- } = value.handler(...args, webContentsId);
6855
+ const handleResult = ({
6856
+ messages,
6857
+ result
6858
+ }) => {
6795
6859
  for (const message of messages) {
6796
6860
  const [key, ...rest] = message;
6797
6861
  send(`ElectronWebContents.${key}`, webContentsId, ...rest);
6798
6862
  }
6799
6863
  return result;
6800
6864
  };
6865
+ const handleAsyncResult = async handlerResult => {
6866
+ try {
6867
+ return handleResult(await handlerResult);
6868
+ } catch (error) {
6869
+ console.error(error);
6870
+ }
6871
+ };
6872
+ const wrappedListener = (...args) => {
6873
+ // @ts-ignore
6874
+ const handlerResult = value.handler(...args, webContentsId);
6875
+ if (handlerResult instanceof Promise) {
6876
+ return handleAsyncResult(handlerResult);
6877
+ }
6878
+ return handleResult(handlerResult);
6879
+ };
6801
6880
  value.attach(webContents, wrappedListener);
6802
6881
  }
6803
6882
  webContentsWithEventListeners.add(webContents);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-process",
3
- "version": "6.37.1",
3
+ "version": "6.37.3",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "electron"