react-native-electron-platform 0.0.26 → 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.26",
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,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