react-native-electron-platform 0.0.25 → 0.0.27

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.
@@ -1,6 +1,6 @@
1
1
  {
2
- "appId": "com.app.desktop",
3
- "productName": "app",
2
+ "appId": "com.inv.desktop",
3
+ "productName": "inv",
4
4
  "asar": {
5
5
  "smartUnpack": true
6
6
  },
@@ -79,7 +79,7 @@
79
79
  "allowToChangeInstallationDirectory": true,
80
80
  "createDesktopShortcut": true,
81
81
  "createStartMenuShortcut": true,
82
- "shortcutName": "app",
82
+ "shortcutName": "inv",
83
83
  "artifactName": "${productName} Setup ${version}.${ext}"
84
84
  },
85
85
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-native-electron-platform",
3
- "version": "0.0.25",
4
- "description": "A boilerplate and utilities for running React Native applications in Electron",
3
+ "version": "0.0.27",
4
+ "description": "A boilerplate and utilities for running React Native applications in desktop environments using Electron.",
5
5
  "main": "index.mjs",
6
6
  "scripts": {
7
7
  "start": "electron index.mjs",
@@ -1,6 +1,14 @@
1
1
  import { ipcMain, dialog } from "electron";
2
+ import fs from "fs";
3
+ import https from "https";
4
+ import http from "http";
5
+ import path from "path";
2
6
 
3
7
  export function registerFileHandlers() {
8
+
9
+ // ======================================================
10
+ // SELECT FILE
11
+ // ======================================================
4
12
  ipcMain.handle("select-file", async () => {
5
13
  try {
6
14
  const result = await dialog.showOpenDialog({
@@ -22,12 +30,82 @@ export function registerFileHandlers() {
22
30
  status: "selected",
23
31
  filePath: result.filePaths[0],
24
32
  };
33
+
25
34
  } catch (err) {
35
+
26
36
  console.error("select-file error:", err);
37
+
38
+ return {
39
+ status: "error",
40
+ message: err.message,
41
+ };
42
+
43
+ }
44
+ });
45
+
46
+ // ======================================================
47
+ // UNIVERSAL FILE DOWNLOAD
48
+ // ======================================================
49
+ ipcMain.handle("download-file", async (_event, { url, filename }) => {
50
+ try {
51
+
52
+ const ext = path.extname(url) || "";
53
+
54
+ const { filePath } = await dialog.showSaveDialog({
55
+ title: "Save File",
56
+ defaultPath: filename || `download${ext}`,
57
+ });
58
+
59
+ if (!filePath) {
60
+ return {
61
+ status: "cancelled",
62
+ };
63
+ }
64
+
65
+ const protocol = url.startsWith("https") ? https : http;
66
+
67
+ return new Promise((resolve, reject) => {
68
+
69
+ const file = fs.createWriteStream(filePath);
70
+
71
+ protocol.get(url, (response) => {
72
+
73
+ response.pipe(file);
74
+
75
+ file.on("finish", () => {
76
+
77
+ file.close();
78
+
79
+ resolve({
80
+ status: "success",
81
+ filePath: filePath,
82
+ });
83
+
84
+ });
85
+
86
+ }).on("error", (err) => {
87
+
88
+ console.error("download-file error:", err);
89
+
90
+ reject({
91
+ status: "error",
92
+ message: err.message,
93
+ });
94
+
95
+ });
96
+
97
+ });
98
+
99
+ } catch (err) {
100
+
101
+ console.error("download-file error:", err);
102
+
27
103
  return {
28
104
  status: "error",
29
105
  message: err.message,
30
106
  };
107
+
31
108
  }
32
109
  });
110
+
33
111
  }
@@ -1,4 +1,4 @@
1
- import { ipcMain, dialog, BrowserWindow } from "electron";
1
+ import { ipcMain, dialog, BrowserWindow, net } from "electron";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
  import { Readable } from "stream";
@@ -45,29 +45,38 @@ export function registerPdfHandlers() {
45
45
  const { url, data, headers = {} } = payload;
46
46
  console.log("IPC post-pdf-preview:", { url });
47
47
 
48
- const res = await fetch(url, {
49
- method: "POST",
50
- headers: {
51
- "Content-Type": "application/json",
52
- Accept: "application/pdf",
53
- ...headers,
54
- },
55
- body: data,
56
- });
57
-
58
- if (!res.ok) {
59
- throw new Error(`HTTP ${res.status}`);
60
- }
61
-
62
48
  const fileName = `Report_${Date.now()}.pdf`;
63
49
  const tempPath = path.join(app.getPath("temp"), fileName);
64
50
 
65
- const nodeStream = Readable.fromWeb(res.body);
66
51
  await new Promise((resolve, reject) => {
67
- const fileStream = fs.createWriteStream(tempPath);
68
- nodeStream.pipe(fileStream);
69
- nodeStream.on("error", reject);
70
- fileStream.on("finish", resolve);
52
+ const req = net.request({
53
+ method: "POST",
54
+ url: url,
55
+ });
56
+
57
+ // Set headers
58
+ req.setHeader("Content-Type", "application/json");
59
+ req.setHeader("Accept", "application/pdf");
60
+ Object.entries(headers).forEach(([key, value]) => {
61
+ req.setHeader(key, value);
62
+ });
63
+
64
+ req.on('response', (res) => {
65
+ if (res.statusCode < 200 || res.statusCode >= 300) {
66
+ reject(new Error(`HTTP ${res.statusCode}`));
67
+ return;
68
+ }
69
+
70
+ const fileStream = fs.createWriteStream(tempPath);
71
+ res.pipe(fileStream);
72
+ res.on('error', reject);
73
+ fileStream.on('finish', resolve);
74
+ fileStream.on('error', reject);
75
+ });
76
+
77
+ req.on('error', reject);
78
+ req.write(data);
79
+ req.end();
71
80
  });
72
81
 
73
82
  return {
@@ -85,8 +94,21 @@ export function registerPdfHandlers() {
85
94
 
86
95
  ipcMain.handle("open-pdf-preview", async (_, pdfUrl) => {
87
96
  try {
88
- const res = await fetch(pdfUrl);
89
- const buffer = Buffer.from(await res.arrayBuffer());
97
+ // Check if it's already a file:// URL
98
+ if (pdfUrl.startsWith('file://')) {
99
+ // It's already a local file, just return it
100
+ return pdfUrl;
101
+ }
102
+
103
+ // It's an HTTP(S) URL, fetch it
104
+ const buffer = await new Promise((resolve, reject) => {
105
+ net.request(pdfUrl).on('response', (res) => {
106
+ const chunks = [];
107
+ res.on('data', (chunk) => chunks.push(chunk));
108
+ res.on('end', () => resolve(Buffer.concat(chunks)));
109
+ res.on('error', reject);
110
+ }).on('error', reject).end();
111
+ });
90
112
 
91
113
  const tempPath = path.join(app.getPath("temp"), `preview_${Date.now()}.pdf`);
92
114
  fs.writeFileSync(tempPath, buffer);
@@ -1,25 +1,67 @@
1
+ import { net } from "electron";
2
+
1
3
  export async function networkServiceCall(method, url, params = {}, headers = {}) {
2
4
  try {
3
5
  const upperMethod = method.toUpperCase();
4
- const options = {
5
- method: upperMethod,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...headers,
9
- },
10
- };
6
+
7
+ let body = undefined;
8
+ let finalUrl = url;
11
9
 
12
10
  if (upperMethod !== "GET") {
13
- options.body = JSON.stringify(params);
11
+ body = JSON.stringify(params);
14
12
  } else if (params && Object.keys(params).length > 0) {
15
13
  const query = new URLSearchParams(params).toString();
16
- url += `?${query}`;
14
+ finalUrl += `?${query}`;
17
15
  }
18
16
 
19
- const response = await fetch(url, options);
20
- const data = await response.json().catch(() => ({}));
17
+ const response = await new Promise((resolve, reject) => {
18
+ const req = net.request({
19
+ method: upperMethod,
20
+ url: finalUrl,
21
+ });
22
+
23
+ // Set headers
24
+ req.setHeader("Content-Type", "application/json");
25
+ Object.entries(headers).forEach(([key, value]) => {
26
+ req.setHeader(key, value);
27
+ });
28
+
29
+ let responseBody = '';
30
+
31
+ req.on('response', (res) => {
32
+ res.on('data', (chunk) => {
33
+ responseBody += chunk.toString();
34
+ });
35
+
36
+ res.on('end', () => {
37
+ resolve({
38
+ status: res.statusCode,
39
+ headers: res.headers,
40
+ body: responseBody,
41
+ });
42
+ });
43
+
44
+ res.on('error', reject);
45
+ });
46
+
47
+ req.on('error', reject);
48
+
49
+ if (body) {
50
+ req.write(body);
51
+ }
52
+
53
+ req.end();
54
+ });
55
+
56
+ let data = {};
57
+ try {
58
+ data = JSON.parse(response.body);
59
+ } catch (e) {
60
+ // If not JSON, use raw body
61
+ data = response.body;
62
+ }
21
63
 
22
- if (response.ok || data?.httpstatus === 200) {
64
+ if ((response.status >= 200 && response.status < 300) || data?.httpstatus === 200) {
23
65
  return { httpstatus: 200, data: data?.data || data };
24
66
  }
25
67
 
@@ -23,7 +23,7 @@ export function createMainWindow(__dirname) {
23
23
  nodeIntegration: false,
24
24
  contextIsolation: true,
25
25
  sandbox: false,
26
- webSecurity: true,
26
+ webSecurity: false,
27
27
  disableBlinkFeatures: "AutoLoadIconsForPage",
28
28
  nativeWindowOpen: true,
29
29
  spellcheck: true,
package/src/preload.mjs CHANGED
@@ -46,6 +46,15 @@ contextBridge.exposeInMainWorld("electronAPI", {
46
46
  */
47
47
  openURL: (url) => ipcRenderer.invoke('react-native-open-url', url),
48
48
 
49
+ /**
50
+ * UNIVERSAL FILE DOWNLOAD
51
+ * Supports csv, pdf, excel, images, zip, etc.
52
+ * @param {string} url - File URL
53
+ * @param {string} filename - Suggested filename
54
+ */
55
+ downloadFile: (url, filename) =>
56
+ ipcRenderer.invoke("download-file", { url, filename }),
57
+
49
58
  // ======================================================
50
59
  // CLIPBOARD OPERATIONS
51
60
  // ======================================================