@lvce-editor/main-process 6.14.1 → 6.15.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.
@@ -9,6 +9,8 @@ import { mkdirSync, createWriteStream, readFileSync, openAsBlob, existsSync, wri
9
9
  import * as NodePath from 'node:path';
10
10
  import { dirname, join as join$1 } from 'node:path';
11
11
  import { homedir, tmpdir } from 'node:os';
12
+ import { pbkdf2Sync, createDecipheriv, createHash } from 'node:crypto';
13
+ import { DatabaseSync } from 'node:sqlite';
12
14
  import { MessageChannel } from 'node:worker_threads';
13
15
 
14
16
  const scheme = 'lvce-oss';
@@ -4603,6 +4605,409 @@ const beep$1 = () => {
4603
4605
  shell.beep();
4604
4606
  };
4605
4607
 
4608
+ const chromeV10Prefix = Buffer.from('v10');
4609
+ const chromeV10Key = pbkdf2Sync('peanuts', 'saltysalt', 1, 16, 'sha1');
4610
+ const chromeV10Iv = Buffer.alloc(16, 0x20);
4611
+ const domainHashLength = 32;
4612
+ class UnsupportedChromeCookieEncryptionError extends Error {
4613
+ constructor() {
4614
+ super('Unsupported Chrome cookie encryption format');
4615
+ this.name = 'UnsupportedChromeCookieEncryptionError';
4616
+ }
4617
+ }
4618
+ const decrypt$1 = (hostKey, encryptedValue, databaseVersion) => {
4619
+ const buffer = Buffer.from(encryptedValue);
4620
+ if (buffer.length <= chromeV10Prefix.length || !buffer.subarray(0, chromeV10Prefix.length).equals(chromeV10Prefix)) {
4621
+ throw new UnsupportedChromeCookieEncryptionError();
4622
+ }
4623
+ const decipher = createDecipheriv('aes-128-cbc', chromeV10Key, chromeV10Iv);
4624
+ const plaintext = Buffer.concat([decipher.update(buffer.subarray(chromeV10Prefix.length)), decipher.final()]);
4625
+ if (databaseVersion < 24) {
4626
+ return plaintext.toString('utf8');
4627
+ }
4628
+ if (plaintext.length < domainHashLength) {
4629
+ throw new Error('Chrome cookie domain integrity check failed');
4630
+ }
4631
+ const expectedDomainHash = createHash('sha256').update(hostKey).digest();
4632
+ const actualDomainHash = plaintext.subarray(0, domainHashLength);
4633
+ if (!actualDomainHash.equals(expectedDomainHash)) {
4634
+ throw new Error('Chrome cookie domain integrity check failed');
4635
+ }
4636
+ return plaintext.subarray(domainHashLength).toString('utf8');
4637
+ };
4638
+
4639
+ const chromeEpochOffsetMicroseconds = 11_644_473_600_000_000n;
4640
+ const microsecondsPerSecond = 1_000_000n;
4641
+ const getSameSite = sameSite => {
4642
+ switch (sameSite) {
4643
+ case 0:
4644
+ return 'no_restriction';
4645
+ case 1:
4646
+ return 'lax';
4647
+ case 2:
4648
+ return 'strict';
4649
+ default:
4650
+ return 'unspecified';
4651
+ }
4652
+ };
4653
+ const getExpirationDate = expiresUtc => {
4654
+ const chromeMicroseconds = BigInt(expiresUtc);
4655
+ return Number(chromeMicroseconds - chromeEpochOffsetMicroseconds) / Number(microsecondsPerSecond);
4656
+ };
4657
+ const getValue = (row, databaseVersion) => {
4658
+ if (row.value) {
4659
+ return row.value;
4660
+ }
4661
+ return decrypt$1(row.hostKey, row.encryptedValue, databaseVersion);
4662
+ };
4663
+ const convert = (row, databaseVersion, now = Date.now() / 1000) => {
4664
+ if (!row.hostKey || row.topFrameSiteKey) {
4665
+ return undefined;
4666
+ }
4667
+ const host = row.hostKey.startsWith('.') ? row.hostKey.slice(1) : row.hostKey;
4668
+ if (!host) {
4669
+ return undefined;
4670
+ }
4671
+ const secure = Boolean(row.isSecure);
4672
+ const details = {
4673
+ httpOnly: Boolean(row.isHttpOnly),
4674
+ name: row.name,
4675
+ path: row.path || '/',
4676
+ sameSite: getSameSite(row.sameSite),
4677
+ secure,
4678
+ url: `${secure ? 'https' : 'http'}://${host}/`,
4679
+ value: getValue(row, databaseVersion)
4680
+ };
4681
+ if (row.hostKey.startsWith('.')) {
4682
+ details.domain = row.hostKey;
4683
+ }
4684
+ if (row.hasExpires) {
4685
+ const expirationDate = getExpirationDate(row.expiresUtc);
4686
+ if (!Number.isFinite(expirationDate) || expirationDate <= now) {
4687
+ return undefined;
4688
+ }
4689
+ details.expirationDate = expirationDate;
4690
+ }
4691
+ return details;
4692
+ };
4693
+
4694
+ /* eslint-disable n/no-unsupported-features/node-builtins -- Electron 43 provides the node:sqlite API used by the main process */
4695
+ const getDatabaseError = error => {
4696
+ const message = error instanceof Error ? error.message : String(error);
4697
+ if (message.toLowerCase().includes('locked') || message.toLowerCase().includes('busy')) {
4698
+ return new Error('Chrome cookie database is busy. Close Chrome and try again.');
4699
+ }
4700
+ return new Error('Failed to read the Chrome cookie database');
4701
+ };
4702
+ const withDatabase = (path, fn) => {
4703
+ let database;
4704
+ try {
4705
+ database = new DatabaseSync(path, {
4706
+ readOnly: true
4707
+ });
4708
+ database.exec('PRAGMA busy_timeout = 1000');
4709
+ return fn(database);
4710
+ } catch (error) {
4711
+ if (error instanceof Error && error.message.startsWith('Unsupported Chrome cookie database version')) {
4712
+ throw error;
4713
+ }
4714
+ throw getDatabaseError(error);
4715
+ } finally {
4716
+ database?.close();
4717
+ }
4718
+ };
4719
+ const getVersion = database => {
4720
+ const row = database.prepare(`SELECT value FROM meta WHERE key = 'version'`).get();
4721
+ const version = Number(row?.value);
4722
+ if (version !== 24) {
4723
+ throw new Error(`Unsupported Chrome cookie database version ${Number.isFinite(version) ? version : 'unknown'}`);
4724
+ }
4725
+ return version;
4726
+ };
4727
+ const getCookieCount = path => {
4728
+ return withDatabase(path, database => {
4729
+ getVersion(database);
4730
+ const row = database.prepare('SELECT COUNT(*) AS count FROM cookies').get();
4731
+ return row.count;
4732
+ });
4733
+ };
4734
+ const readCookies = path => {
4735
+ return withDatabase(path, database => {
4736
+ const version = getVersion(database);
4737
+ const rows = database.prepare(`
4738
+ SELECT
4739
+ encrypted_value AS encryptedValue,
4740
+ CAST(expires_utc AS TEXT) AS expiresUtc,
4741
+ has_expires AS hasExpires,
4742
+ host_key AS hostKey,
4743
+ is_httponly AS isHttpOnly,
4744
+ is_secure AS isSecure,
4745
+ name,
4746
+ path,
4747
+ samesite AS sameSite,
4748
+ top_frame_site_key AS topFrameSiteKey,
4749
+ value
4750
+ FROM cookies
4751
+ `).all();
4752
+ return {
4753
+ rows,
4754
+ version
4755
+ };
4756
+ });
4757
+ };
4758
+
4759
+ const isValidProfileDirectory = value => {
4760
+ return value !== '' && value !== '.' && value !== '..' && !value.includes('/') && !value.includes('\\');
4761
+ };
4762
+ const getCookieDatabasePath = (chromeDataDirectory, profileDirectory) => {
4763
+ const profilePath = join$1(chromeDataDirectory, profileDirectory);
4764
+ const candidates = [join$1(profilePath, 'Cookies'), join$1(profilePath, 'Network', 'Cookies')];
4765
+ for (const candidate of candidates) {
4766
+ if (existsSync(candidate)) {
4767
+ return candidate;
4768
+ }
4769
+ }
4770
+ throw new Error(`Chrome cookie database was not found for profile ${profileDirectory}`);
4771
+ };
4772
+ const getMostRecentlyActiveProfile = infoCache => {
4773
+ let selected;
4774
+ let selectedActiveTime = -Infinity;
4775
+ for (const [directory, info] of Object.entries(infoCache)) {
4776
+ if (!isValidProfileDirectory(directory)) {
4777
+ continue;
4778
+ }
4779
+ const activeTime = typeof info.active_time === 'number' && Number.isFinite(info.active_time) ? info.active_time : -Infinity;
4780
+ if (selected === undefined || activeTime > selectedActiveTime) {
4781
+ selected = directory;
4782
+ selectedActiveTime = activeTime;
4783
+ }
4784
+ }
4785
+ return selected;
4786
+ };
4787
+ const getChromeDataDirectory = () => {
4788
+ const configDirectory = process.env.XDG_CONFIG_HOME || join$1(homedir(), '.config');
4789
+ return join$1(configDirectory, 'google-chrome');
4790
+ };
4791
+ const getActiveProfile = chromeDataDirectory => {
4792
+ const localStatePath = join$1(chromeDataDirectory, 'Local State');
4793
+ if (!existsSync(localStatePath)) {
4794
+ throw new Error(`Google Chrome profile data was not found at ${chromeDataDirectory}`);
4795
+ }
4796
+ let localState;
4797
+ try {
4798
+ localState = JSON.parse(readFileSync(localStatePath, 'utf8'));
4799
+ } catch {
4800
+ throw new Error('Google Chrome profile metadata is invalid');
4801
+ }
4802
+ const infoCache = localState.profile?.info_cache || {};
4803
+ const orderedProfile = localState.profile?.profiles_order?.find(isValidProfileDirectory);
4804
+ const directory = getMostRecentlyActiveProfile(infoCache) || orderedProfile || 'Default';
4805
+ const name = infoCache[directory]?.name || directory;
4806
+ const cookieDatabasePath = getCookieDatabasePath(chromeDataDirectory, directory);
4807
+ return {
4808
+ cookieDatabasePath,
4809
+ directory,
4810
+ name
4811
+ };
4812
+ };
4813
+
4814
+ const MainFrame = 'mainFrame';
4815
+ const Script = 'script';
4816
+ const Xhr = 'xhr';
4817
+
4818
+ const Get = 'GET';
4819
+ const Head = 'HEAD';
4820
+ const Options = 'OPTIONS';
4821
+ const Post = 'POST';
4822
+
4823
+ // @ts-ignore
4824
+
4825
+ const getBeforeRequestResponseMainFrame = (method, url) => {
4826
+ return {};
4827
+ };
4828
+ const cancelIfStartsWith = (url, canceledUrls) => {
4829
+ for (const canceledUrl of canceledUrls) {
4830
+ if (url.startsWith(canceledUrl)) {
4831
+ return {
4832
+ cancel: true
4833
+ };
4834
+ }
4835
+ }
4836
+ return {};
4837
+ };
4838
+ const getBeforeRequestResponseXhrPost = url => {
4839
+ const canceledUrls = ['https://www.youtube.com/api/stats/qoe', 'https://www.youtube.com/youtubei/v1/log_event', 'https://play.google.com/log'];
4840
+ return cancelIfStartsWith(url, canceledUrls);
4841
+ };
4842
+ const getBeforeRequestResponseXhrGet = url => {
4843
+ const canceledUrls = ['https://www.youtube.com/pagead/', 'https://www.youtube.com/api/stats/qoe', 'https://www.youtube.com/api/stats/ads', 'https://www.youtube.com/api/stats/delayplay', 'https://www.youtube.com/ptracking', 'https://www.youtube.com/api/timedtext', 'https://www.youtube.com/pcs/activeview', 'https://googleads.g.doubleclick.net/pagead/id'];
4844
+ return cancelIfStartsWith(url, canceledUrls);
4845
+ };
4846
+ const getBeforeRequestResponseXhrOptions = url => {
4847
+ const canceledUrls = ['https://play.google.com/log'];
4848
+ return cancelIfStartsWith(url, canceledUrls);
4849
+ };
4850
+ const getBeforeRequestResponseXhrHead = url => {
4851
+ const canceledUrls = ['https://www.youtube.com/generate_204'];
4852
+ return cancelIfStartsWith(url, canceledUrls);
4853
+ };
4854
+ const getBeforeRequestResponseXhr = (method, url) => {
4855
+ switch (method) {
4856
+ case Get:
4857
+ return getBeforeRequestResponseXhrGet(url);
4858
+ case Head:
4859
+ return getBeforeRequestResponseXhrHead(url);
4860
+ case Options:
4861
+ return getBeforeRequestResponseXhrOptions(url);
4862
+ case Post:
4863
+ return getBeforeRequestResponseXhrPost(url);
4864
+ default:
4865
+ return {};
4866
+ }
4867
+ };
4868
+ const getBeforeRequestResponseDefault = (method, url) => {
4869
+ return {};
4870
+ };
4871
+ const getBeforeRequestResponseScript = (method, url) => {
4872
+ const canceledUrls = ['https://static.doubleclick.net'];
4873
+ return cancelIfStartsWith(url, canceledUrls);
4874
+ };
4875
+ const getBeforeRequestResponse = details => {
4876
+ const {
4877
+ method,
4878
+ resourceType,
4879
+ url
4880
+ } = details;
4881
+ switch (resourceType) {
4882
+ case MainFrame:
4883
+ return getBeforeRequestResponseMainFrame();
4884
+ case Script:
4885
+ return getBeforeRequestResponseScript(method, url);
4886
+ case Xhr:
4887
+ return getBeforeRequestResponseXhr(method, url);
4888
+ default:
4889
+ return getBeforeRequestResponseDefault();
4890
+ }
4891
+ };
4892
+
4893
+ /**
4894
+ *
4895
+ * @param {Electron.OnBeforeRequestListenerDetails } details
4896
+ * @param {(response: globalThis.Electron.CallbackResponse) => void} callback
4897
+ */
4898
+ const handleBeforeRequest = (details, callback) => {
4899
+ const response = getBeforeRequestResponse(details);
4900
+ callback(response);
4901
+ };
4902
+ const filter = {
4903
+ // urls: ['https://*.youtube.com/*'],
4904
+ urls: ['<all_urls>']
4905
+ };
4906
+
4907
+ const state = {
4908
+ session: undefined
4909
+ };
4910
+ const isAllowedPermission = permission => {
4911
+ switch (permission) {
4912
+ case ClipBoardRead:
4913
+ case ClipBoardSanitizedWrite:
4914
+ case FullScreen:
4915
+ case GeoLocation:
4916
+ case WindowPlacement:
4917
+ return true;
4918
+ default:
4919
+ return false;
4920
+ }
4921
+ };
4922
+ const handlePermissionRequest = (webContents, permission, callback, details) => {
4923
+ callback(isAllowedPermission(permission));
4924
+ };
4925
+ const handlePermissionCheck = (webContents, permission, origin, details) => {
4926
+ return isAllowedPermission(permission);
4927
+ };
4928
+ const createSession = () => {
4929
+ const sessionId = `persist:browserView`;
4930
+ const session = Electron.session.fromPartition(sessionId, {
4931
+ cache: true
4932
+ });
4933
+ session.setPermissionRequestHandler(handlePermissionRequest);
4934
+ session.setPermissionCheckHandler(handlePermissionCheck);
4935
+ session.webRequest.onBeforeRequest(filter, handleBeforeRequest);
4936
+ // session.webRequest.addSessionChromeExtensions(session)
4937
+ return session;
4938
+ };
4939
+ const getSession = () => {
4940
+ state.session ||= createSession();
4941
+ return state.session;
4942
+ };
4943
+
4944
+ const assertLinux = () => {
4945
+ if (process.platform !== 'linux') {
4946
+ throw new Error('Importing Chrome cookies is only supported on Linux');
4947
+ }
4948
+ };
4949
+ const getInfoFromDirectory = chromeDataDirectory => {
4950
+ const profile = getActiveProfile(chromeDataDirectory);
4951
+ const cookieCount = getCookieCount(profile.cookieDatabasePath);
4952
+ return {
4953
+ cookieCount,
4954
+ profileDirectory: profile.directory,
4955
+ profileName: profile.name
4956
+ };
4957
+ };
4958
+ const getInfo = () => {
4959
+ assertLinux();
4960
+ return getInfoFromDirectory(getChromeDataDirectory());
4961
+ };
4962
+ const importFromDirectory = async (chromeDataDirectory, session) => {
4963
+ const profile = getActiveProfile(chromeDataDirectory);
4964
+ const {
4965
+ rows,
4966
+ version
4967
+ } = readCookies(profile.cookieDatabasePath);
4968
+ const cookies = [];
4969
+ let skipped = 0;
4970
+ let unsupportedEncryption = 0;
4971
+ for (const row of rows) {
4972
+ try {
4973
+ const cookie = convert(row, version);
4974
+ if (cookie) {
4975
+ cookies.push(cookie);
4976
+ } else {
4977
+ skipped++;
4978
+ }
4979
+ } catch (error) {
4980
+ skipped++;
4981
+ if (error instanceof UnsupportedChromeCookieEncryptionError) {
4982
+ unsupportedEncryption++;
4983
+ }
4984
+ }
4985
+ }
4986
+ if (cookies.length === 0 && unsupportedEncryption > 0) {
4987
+ throw new Error('Unsupported Chrome cookie encryption format');
4988
+ }
4989
+ let imported = 0;
4990
+ let failed = 0;
4991
+ for (const cookie of cookies) {
4992
+ try {
4993
+ await session.cookies.set(cookie);
4994
+ imported++;
4995
+ } catch {
4996
+ failed++;
4997
+ }
4998
+ }
4999
+ await session.cookies.flushStore();
5000
+ return {
5001
+ failed,
5002
+ imported,
5003
+ skipped
5004
+ };
5005
+ };
5006
+ const importCookies = async () => {
5007
+ assertLinux();
5008
+ return importFromDirectory(getChromeDataDirectory(), getSession());
5009
+ };
5010
+
4606
5011
  const handleTimeout = () => {
4607
5012
  throw new Error('oops');
4608
5013
  };
@@ -5939,136 +6344,6 @@ const ElectronBrowserViewEventListeners = {
5939
6344
  windowOpen: ElectronBrowserViewEventListenerWindowOpen
5940
6345
  };
5941
6346
 
5942
- const MainFrame = 'mainFrame';
5943
- const Script = 'script';
5944
- const Xhr = 'xhr';
5945
-
5946
- const Get = 'GET';
5947
- const Head = 'HEAD';
5948
- const Options = 'OPTIONS';
5949
- const Post = 'POST';
5950
-
5951
- // @ts-ignore
5952
-
5953
- const getBeforeRequestResponseMainFrame = (method, url) => {
5954
- return {};
5955
- };
5956
- const cancelIfStartsWith = (url, canceledUrls) => {
5957
- for (const canceledUrl of canceledUrls) {
5958
- if (url.startsWith(canceledUrl)) {
5959
- return {
5960
- cancel: true
5961
- };
5962
- }
5963
- }
5964
- return {};
5965
- };
5966
- const getBeforeRequestResponseXhrPost = url => {
5967
- const canceledUrls = ['https://www.youtube.com/api/stats/qoe', 'https://www.youtube.com/youtubei/v1/log_event', 'https://play.google.com/log'];
5968
- return cancelIfStartsWith(url, canceledUrls);
5969
- };
5970
- const getBeforeRequestResponseXhrGet = url => {
5971
- const canceledUrls = ['https://www.youtube.com/pagead/', 'https://www.youtube.com/api/stats/qoe', 'https://www.youtube.com/api/stats/ads', 'https://www.youtube.com/api/stats/delayplay', 'https://www.youtube.com/ptracking', 'https://www.youtube.com/api/timedtext', 'https://www.youtube.com/pcs/activeview', 'https://googleads.g.doubleclick.net/pagead/id'];
5972
- return cancelIfStartsWith(url, canceledUrls);
5973
- };
5974
- const getBeforeRequestResponseXhrOptions = url => {
5975
- const canceledUrls = ['https://play.google.com/log'];
5976
- return cancelIfStartsWith(url, canceledUrls);
5977
- };
5978
- const getBeforeRequestResponseXhrHead = url => {
5979
- const canceledUrls = ['https://www.youtube.com/generate_204'];
5980
- return cancelIfStartsWith(url, canceledUrls);
5981
- };
5982
- const getBeforeRequestResponseXhr = (method, url) => {
5983
- switch (method) {
5984
- case Get:
5985
- return getBeforeRequestResponseXhrGet(url);
5986
- case Head:
5987
- return getBeforeRequestResponseXhrHead(url);
5988
- case Options:
5989
- return getBeforeRequestResponseXhrOptions(url);
5990
- case Post:
5991
- return getBeforeRequestResponseXhrPost(url);
5992
- default:
5993
- return {};
5994
- }
5995
- };
5996
- const getBeforeRequestResponseDefault = (method, url) => {
5997
- return {};
5998
- };
5999
- const getBeforeRequestResponseScript = (method, url) => {
6000
- const canceledUrls = ['https://static.doubleclick.net'];
6001
- return cancelIfStartsWith(url, canceledUrls);
6002
- };
6003
- const getBeforeRequestResponse = details => {
6004
- const {
6005
- method,
6006
- resourceType,
6007
- url
6008
- } = details;
6009
- switch (resourceType) {
6010
- case MainFrame:
6011
- return getBeforeRequestResponseMainFrame();
6012
- case Script:
6013
- return getBeforeRequestResponseScript(method, url);
6014
- case Xhr:
6015
- return getBeforeRequestResponseXhr(method, url);
6016
- default:
6017
- return getBeforeRequestResponseDefault();
6018
- }
6019
- };
6020
-
6021
- /**
6022
- *
6023
- * @param {Electron.OnBeforeRequestListenerDetails } details
6024
- * @param {(response: globalThis.Electron.CallbackResponse) => void} callback
6025
- */
6026
- const handleBeforeRequest = (details, callback) => {
6027
- const response = getBeforeRequestResponse(details);
6028
- callback(response);
6029
- };
6030
- const filter = {
6031
- // urls: ['https://*.youtube.com/*'],
6032
- urls: ['<all_urls>']
6033
- };
6034
-
6035
- const state = {
6036
- session: undefined
6037
- };
6038
- const isAllowedPermission = permission => {
6039
- switch (permission) {
6040
- case ClipBoardRead:
6041
- case ClipBoardSanitizedWrite:
6042
- case FullScreen:
6043
- case GeoLocation:
6044
- case WindowPlacement:
6045
- return true;
6046
- default:
6047
- return false;
6048
- }
6049
- };
6050
- const handlePermissionRequest = (webContents, permission, callback, details) => {
6051
- callback(isAllowedPermission(permission));
6052
- };
6053
- const handlePermissionCheck = (webContents, permission, origin, details) => {
6054
- return isAllowedPermission(permission);
6055
- };
6056
- const createSession = () => {
6057
- const sessionId = `persist:browserView`;
6058
- const session = Electron.session.fromPartition(sessionId, {
6059
- cache: true
6060
- });
6061
- session.setPermissionRequestHandler(handlePermissionRequest);
6062
- session.setPermissionCheckHandler(handlePermissionCheck);
6063
- session.webRequest.onBeforeRequest(filter, handleBeforeRequest);
6064
- // session.webRequest.addSessionChromeExtensions(session)
6065
- return session;
6066
- };
6067
- const getSession = () => {
6068
- state.session ||= createSession();
6069
- return state.session;
6070
- };
6071
-
6072
6347
  const send = (method, ...params) => {
6073
6348
  const rpc = get(EmbedsProcess);
6074
6349
  if (!rpc) {
@@ -6657,6 +6932,8 @@ const trash = async path => {
6657
6932
  const commandMap = {
6658
6933
  'AppWindow.createAppWindow': createAppWindow,
6659
6934
  'Beep.beep': beep$1,
6935
+ 'ChromeCookieImport.getInfo': getInfo,
6936
+ 'ChromeCookieImport.importCookies': importCookies,
6660
6937
  'Crash.crashMainProcess': crashMainProcess$1,
6661
6938
  'CreateMessagePort.createMessagePort': createMessagePort,
6662
6939
  'CreatePidMap.createPidMap': createPidMap,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-process",
3
- "version": "6.14.1",
3
+ "version": "6.15.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "electron"