protocol.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { protocol } from 'electron';
  2. import { join } from 'path';
  3. import { parse } from 'url';
  4. import { ERROR_PROTOCOL, WEBUI_PROTOCOL } from '~/constants/files';
  5. protocol.registerSchemesAsPrivileged([
  6. {
  7. scheme: 'bluevchat',
  8. privileges: {
  9. bypassCSP: true,
  10. secure: true,
  11. standard: true,
  12. supportFetchAPI: true,
  13. allowServiceWorkers: true,
  14. corsEnabled: false,
  15. },
  16. },
  17. ]);
  18. export const registerProtocol = (session: Electron.Session) => {
  19. session.protocol.registerFileProtocol(
  20. ERROR_PROTOCOL,
  21. (request, callback: any) => {
  22. const parsed = parse(request.url);
  23. if (parsed.hostname === 'network-error') {
  24. return callback({
  25. path: join(__dirname, '../static/pages/', `network-error.html`),
  26. });
  27. }
  28. },
  29. );
  30. if (process.env.NODE_ENV !== 'development') {
  31. session.protocol.registerFileProtocol(
  32. WEBUI_PROTOCOL,
  33. (request, callback: any) => {
  34. const parsed = parse(request.url);
  35. if (parsed.path === '/') {
  36. return callback({
  37. path: join(__dirname, `${parsed.hostname}.html`),
  38. });
  39. }
  40. callback({ path: join(__dirname, parsed.path) });
  41. },
  42. );
  43. }
  44. };