Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown encoding: base64url #97

Open
vishal-sarvaiya-sub004 opened this issue May 24, 2024 · 5 comments
Open

Unknown encoding: base64url #97

vishal-sarvaiya-sub004 opened this issue May 24, 2024 · 5 comments

Comments

@vishal-sarvaiya-sub004
Copy link

vishal-sarvaiya-sub004 commented May 24, 2024

// main.js

mainWindow = new BrowserWindow({
    width: userWidth || 1366,
    height: userHeight || 768,
    minWidth: 630,
    minHeight: 375,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: false,
      contextIsolation: true,
      enableRemoteModule: false,
    },
    icon: path.join(__dirname, 'assets', 'Icon.png'),
    title: 'Electron FCM Demo',
  })

  await mainWindow.loadURL('http://localhost/ElectronFCMDemo/index.php');
  setupPushReceiver(mainWindow.webContents);

// preload.js

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('ipcRenderer', {
  send: (channel, data) => {
    ipcRenderer.send("messageFromPreload", "Event >> send message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", data)
    // Whitelist channels as needed
    ipcRenderer.send(channel, data);
  },
  on: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> on message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)
    // set logs to check channel name and data
    ipcRenderer.on(channel, (event, ...args) => func(event, ...args));
  },
  once: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> once message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)

    ipcRenderer.once(channel, (event, ...args) => func(event, ...args));
  },
  removeListener: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> removeListener message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)

    ipcRenderer.removeListener(channel, func);
  },
  removeAllListeners: (channel) => {
    ipcRenderer.send("messageFromPreload", "Event >> removeAllListeners message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)
    ipcRenderer.removeAllListeners(channel);
  },
});

// index.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Electron FCM Example</title>
</head>
<body>
<h1>Electron FCM Example</h1>

<script>
  (async () => {
    if ('serviceWorker' in navigator) {
      await navigator.serviceWorker.ready;
      let registration = await navigator.serviceWorker.register('./firebase-messaging-sw.js').catch((error) => {
        console.error('Service Worker registration failed:', error);
      });
      console.log('Service Worker registered with scope:', registration.scope);
      console.log('Requesting notification permission...');
    }
  })()
</script>

<script>

  // Listen for service successfully started
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_SERVICE_STARTED", (_, token) => {
    ipcRenderer.send("messageFromPreload", token)
    console.log(`Electron >> service successfully started: ${token}`);
  });
  // Handle notification errors
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_SERVICE_ERROR", (_, error) => {
    ipcRenderer.send("messageFromPreload", error)
    console.log('Electron >> notification error', error);
  });
  // Send FCM token to backend
  ipcRenderer.on("PUSH_RECEIVER:::TOKEN_UPDATED", (_, token) => {
    ipcRenderer.send("messageFromPreload", token)
    console.log(`Electron >> token updated: ${token}`);
  });
  // Display notification
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_RECEIVED", (_, notification) => {
    ipcRenderer.send("messageFromPreload", notification)
    console.log('Electron >> notification received:', notification);
  });
  // Start service
  ipcRenderer.send("PUSH_RECEIVER:::START_NOTIFICATION_SERVICE", "<MY SENDER ID>");
</script>

</body>
</html>

Here, Once my app started, I am getting token but as soon as I get crash
I am getting Error below

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: base64url
    at fromString (buffer.js:454:13)
    at Function.from (buffer.js:308:12)
    at decode (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:41:19)
    at parseParams (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:254:19)
    at Object.decrypt (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:366:16)
    at decrypt (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\utils\decrypt\index.js:21:25)
    at Client._onDataMessage (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\client.js:174:17)
    at Client._onMessage (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\client.js:163:12)
    at Parser.emit (events.js:315:20)
    at Parser._onGotMessageBytes (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:233:10)
    at Parser._waitForData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:128:14)
    at Parser._onGotMessageSize (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:191:12)
    at Parser._onGotMessageTag (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:155:10)
    at Parser._waitForData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:122:14)
    at Parser._onData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:78:12)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  code: 'ERR_UNKNOWN_ENCODING'
}
@kikino1989
Copy link

kikino1989 commented Aug 2, 2024

@vishal-sarvaiya-sub004 Did you find a workaround for this issue? 🙏

@iamvishu
Copy link

iamvishu commented Aug 3, 2024

@kikino1989 I found a solution for it

@AliAkhgar
Copy link

@kikino1989 I found a solution for it

can you share your solution?
thanks.

@iamvishu
Copy link

iamvishu commented Dec 12, 2024

@AliAkhgar I got a temporary solution!

I know this is not a good or accepted approach, but I fixed that for now.

Navigate to node_modules\http_ece\ece.js file and goto line no 41
There is return Buffer.from(b, 'base64url');. I simply change base64url to base64 and it's working

If you delete your node_modeules directory, You have to repeat the above step.

@AliAkhgar
Copy link

@AliAkhgar I got a temporary solution!

I know this is not a good or accepted approach, but I fixed that for now.

Navigate to node_modules\http_ece\ece.js file and goto line no 41 There is return Buffer.from(b, 'base64url');. I simply change base64url to base64 and it's working

If you delete your node_modeules directory, You have to repeat the above step.

Thanks for the info!
Yeah, I was wondering to do so, it's not a safe practice but appreciate it.
I had to rebase my project to a new electron boilerplate and problem is solved.
Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants