@nemigo/electron 0.1.1 → 0.3.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.
@@ -5,8 +5,10 @@
5
5
  * @see https://www.electronjs.org/docs/latest/tutorial/security#13-disable-or-limit-navigation
6
6
  */
7
7
  export declare class NetworkSecurity {
8
- #private;
9
- constructor(allowedOrigins?: string[], allowedExternalUrls?: string[]);
8
+ readonly allowedInternalOrigins: Set<string>;
9
+ readonly allowedExternalOrigins: Set<string>;
10
+ extractOrigin(url: string): string;
11
+ constructor(allowedInternal?: string[], allowedExternal?: string[], init?: boolean);
10
12
  init(): this;
11
13
  private handleNavigation;
12
14
  private handleWindowOpen;
package/dist/security.js CHANGED
@@ -6,11 +6,17 @@ import { app, shell } from "electron";
6
6
  * @see https://www.electronjs.org/docs/latest/tutorial/security#13-disable-or-limit-navigation
7
7
  */
8
8
  export class NetworkSecurity {
9
- #allowedOrigins;
10
- #allowedExternalUrls;
11
- constructor(allowedOrigins = [], allowedExternalUrls = []) {
12
- this.#allowedOrigins = new Set(allowedOrigins);
13
- this.#allowedExternalUrls = new Set(allowedExternalUrls);
9
+ allowedInternalOrigins;
10
+ allowedExternalOrigins;
11
+ extractOrigin(url) {
12
+ const { origin } = new URL(url);
13
+ return origin;
14
+ }
15
+ constructor(allowedInternal = [], allowedExternal = [], init = true) {
16
+ this.allowedInternalOrigins = new Set(allowedInternal.map((v) => this.extractOrigin(v)));
17
+ this.allowedExternalOrigins = new Set(allowedExternal.map((v) => this.extractOrigin(v)));
18
+ if (init)
19
+ this.init();
14
20
  }
15
21
  init() {
16
22
  app.on("web-contents-created", (_, contents) => {
@@ -22,7 +28,7 @@ export class NetworkSecurity {
22
28
  handleNavigation(contents) {
23
29
  contents.on("will-navigate", (e, url) => {
24
30
  const { origin } = new URL(url);
25
- if (!this.#allowedOrigins.has(origin)) {
31
+ if (!this.allowedInternalOrigins.has(origin)) {
26
32
  e.preventDefault();
27
33
  console.warn(`Blocked navigating to disallowed origin: ${origin}`);
28
34
  }
@@ -32,7 +38,7 @@ export class NetworkSecurity {
32
38
  contents.setWindowOpenHandler(({ url }) => {
33
39
  const { origin } = new URL(url);
34
40
  // Если URL в разрешенном списке — открываем во внешнем браузере
35
- if (this.#allowedExternalUrls.has(origin)) {
41
+ if (this.allowedExternalOrigins.has(origin)) {
36
42
  shell.openExternal(url).catch(console.error);
37
43
  }
38
44
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/electron",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "Vlad Logvin",
@@ -17,14 +17,6 @@
17
17
  "./security": {
18
18
  "types": "./dist/security.d.ts",
19
19
  "default": "./dist/security.js"
20
- },
21
- "./vite": {
22
- "types": "./dist/vite/index.d.ts",
23
- "default": "./dist/vite/index.js"
24
- },
25
- "./vite/hot-reboot": {
26
- "types": "./dist/vite/hot-reboot.d.ts",
27
- "default": "./dist/vite/hot-reboot.js"
28
20
  }
29
21
  },
30
22
  "peerDependencies": {
@@ -1,5 +0,0 @@
1
- import type { Plugin } from "vite";
2
- /**
3
- * Перезапуск Electron-приложения, если какие-то файлы изменились
4
- */
5
- export declare const electronViteHotReboot: () => Plugin;
@@ -1,28 +0,0 @@
1
- import { spawn } from "node:child_process";
2
- import { exit } from "node:process";
3
- import electron from "electron";
4
- /**
5
- * Перезапуск Electron-приложения, если какие-то файлы изменились
6
- */
7
- export const electronViteHotReboot = () => {
8
- let electronApp = null;
9
- return {
10
- name: "electron-vite-hot-reboot",
11
- config() {
12
- return { build: { watch: {} } };
13
- },
14
- writeBundle() {
15
- // Убиваем прошлый экземпляр, если он существовал
16
- if (electronApp !== null) {
17
- electronApp.removeListener("exit", exit);
18
- electronApp.kill("SIGINT");
19
- electronApp = null;
20
- }
21
- // Запускаем новый экземпляр
22
- // @ts-expect-error <в "electron" лежит путь к бинарнику, но TS об это не знает>
23
- electronApp = spawn(electron, ["--inspect", "."], { stdio: "inherit" });
24
- // Выходим из процесса вместе с выходом из приложения
25
- electronApp.addListener("exit", exit);
26
- },
27
- };
28
- };
@@ -1,7 +0,0 @@
1
- import type { AliasOptions } from "vite";
2
- export interface ElectronViteConfigOptions {
3
- isDEV: boolean;
4
- node: number;
5
- alias?: AliasOptions;
6
- }
7
- export declare const defineElectronViteConfig: (options: ElectronViteConfigOptions) => import("vite").UserConfig;
@@ -1,27 +0,0 @@
1
- import { electronViteHotReboot } from "./hot-reboot.js";
2
- import { defineConfig } from "vite";
3
- export const defineElectronViteConfig = (options) => defineConfig({
4
- resolve: {
5
- alias: options.alias,
6
- },
7
- build: {
8
- ssr: true,
9
- minify: options.isDEV ? false : "esbuild",
10
- sourcemap: options.isDEV ? "inline" : false,
11
- target: `node${options.node}`,
12
- lib: {
13
- entry: [
14
- "src/index.ts",
15
- ],
16
- formats: ["es"],
17
- },
18
- rollupOptions: {
19
- output: {
20
- entryFileNames: "[name].js",
21
- },
22
- },
23
- emptyOutDir: true,
24
- reportCompressedSize: false,
25
- },
26
- plugins: options.isDEV ? [electronViteHotReboot()] : [],
27
- });