From d32a5dc62b2f0497326de2c10c2eb135dc78302c Mon Sep 17 00:00:00 2001 From: danikaze Date: Thu, 22 Aug 2024 00:52:53 +0900 Subject: [PATCH 1/2] Fix #24 (error happening when contextBridgeApiKey is custom) --- lib/index.js | 2 +- src/index.js | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 867e035..270192d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.writeFileResponse=exports.writeFileRequest=exports.readFileResponse=exports.readFileRequest=exports.preloadBindings=exports.mainBindings=exports["default"]=exports.clearMainBindings=exports.changeLanguageRequest=void 0;var _lodash=require("lodash"),_utils=require("./utils");function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c"),this.rendererLog="".concat("[i18next-electron-fs-backend:","renderer]=>"),"undefined"==typeof this.backendOptions.i18nextElectronBackend?void console.error("".concat(this.rendererLog," i18nextElectronBackend is undefined, please ensure you are exposing i18nextElectronBackend via the contextBridge in your preload file.")):void this.setupIpcBindings()}},{key:"setupIpcBindings",value:function setupIpcBindings(){var a=this,b=this.backendOptions.i18nextElectronBackend;b.onReceive(readFileResponse,function(b){if("undefined"!=typeof a.readCallbacks[b.key]){var c;if(b.error)c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,{});else{var d;b.data=b.data.replace(/^\uFEFF/,"");try{d=JSON.parse(b.data)}catch(d){return d.message="Error parsing '".concat(b.filename,"'. Message: '").concat(d,"'."),c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],void(null!==c&&"function"==typeof c&&c(d))}c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,d)}}}),b.onReceive(writeFileResponse,function(b){for(var c,d=b.keys,e=0;e"),this.rendererLog="".concat("[i18next-electron-fs-backend:","renderer]=>"),"undefined"!=typeof this.backendOptions.i18nextElectronBackend)this.setupIpcBindings();else if(!d)return void console.error("".concat(this.rendererLog," i18nextElectronBackend is undefined, please ensure you are exposing i18nextElectronBackend via the contextBridge in your preload file."))}},{key:"setupIpcBindings",value:function setupIpcBindings(){var a=this,b=this.backendOptions.i18nextElectronBackend;b.onReceive(readFileResponse,function(b){if("undefined"!=typeof a.readCallbacks[b.key]){var c;if(b.error)c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,{});else{var d;b.data=b.data.replace(/^\uFEFF/,"");try{d=JSON.parse(b.data)}catch(d){return d.message="Error parsing '".concat(b.filename,"'. Message: '").concat(d,"'."),c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],void(null!==c&&"function"==typeof c&&c(d))}c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,d)}}}),b.onReceive(writeFileResponse,function(b){for(var c,d=b.keys,e=0;e`; if (typeof this.backendOptions.i18nextElectronBackend === "undefined") { - console.error(`${this.rendererLog} i18nextElectronBackend is undefined, please ensure you are exposing i18nextElectronBackend via the contextBridge in your preload file.`); - return; + if (!skipChecks) { + console.error(`${this.rendererLog} i18nextElectronBackend is undefined, please ensure you are exposing i18nextElectronBackend via the contextBridge in your preload file.`); + return; + } + } else { + this.setupIpcBindings(); } - - this.setupIpcBindings(); } // Sets up Ipc bindings so that we can keep any node-specific From 13acd498e5045d2abd76cfdcd738108829edcf92 Mon Sep 17 00:00:00 2001 From: danikaze Date: Thu, 22 Aug 2024 00:53:36 +0900 Subject: [PATCH 2/2] Improve types --- index.d.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 410fe7f..2fe93c4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,29 @@ -import { BackendModule } from "i18next"; +import { BackendModule, TFunction } from "i18next"; +import { + readFileRequest, + writeFileRequest, + readFileResponse, + writeFileResponse, +} from './src/index.js' + +export type ReadFileRequest = typeof readFileRequest; +export type WriteFileRequest = typeof writeFileRequest; +export type ReadFileResponse = typeof readFileResponse; +export type WriteFileResponse = typeof writeFileResponse; + +export interface PreloadBindings { + send: (channel: ReadFileRequest | WriteFileRequest, data?: any) => void; + onReceive: (channel: ReadFileResponse | WriteFileResponse, func: (data?: any) => void) => void; + onLanguageChange: (cb: (args: { lng: string }, t: TFunction) => void) => void; + clientOptions: { + environment: string | undefined; + platform: string; + resourcesPath: string; + } +} export function mainBindings(ipcMain: Electron.IpcMain, browserWindow: Electron.BrowserWindow, fs: any): any; export function clearMainBindings(ipcMain: Electron.IpcMain): any; -export function preloadBindings(ipcRenderer: Electron.IpcRenderer, process: NodeJS.Process): any; +export function preloadBindings(ipcRenderer: Electron.IpcRenderer, process: NodeJS.Process): PreloadBindings; export default {} as BackendModule;