@urga-panel/ur-panels-core 1.0.2 → 1.0.3

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.
@@ -7,6 +7,7 @@ export type WVFrontServiceOts = ServiceOts & {
7
7
  usedService: {};
8
8
  };
9
9
  export declare class WVFrontService extends Service {
10
+ private static _requestIdCounter;
10
11
  static serviceInfo: {
11
12
  name: string;
12
13
  requiredServices: any[];
@@ -1,6 +1,7 @@
1
1
  import { Service } from "../../../types/Service.js";
2
2
  import { NSWebViewinterface } from "./nv.js";
3
3
  export class WVFrontService extends Service {
4
+ static _requestIdCounter = 0;
4
5
  static serviceInfo = {
5
6
  name: "WVFrontService",
6
7
  requiredServices: [],
@@ -27,28 +28,56 @@ export class WVFrontService extends Service {
27
28
  }
28
29
  async sendFetchRequest(url, options) {
29
30
  console.log("sendFetchRequest", url, options);
31
+ const id = `r_${Date.now()}_${++WVFrontService._requestIdCounter}`;
30
32
  return new Promise((resolve) => {
31
- const callback = (args) => {
32
- const param = args;
33
- // Convert param to a Response object
34
- //debugger;
35
- //@ts-ignore
36
- const byteArray = Object.values(param._bodyBlob._buffer);
37
- //@ts-ignore
38
- const uint8 = new Uint8Array(byteArray);
39
- //@ts-ignore
40
- const text = new TextDecoder().decode(uint8);
33
+ const handler = (args) => {
34
+ // support envelope { id, param } or legacy raw param
35
+ const envelope = args && typeof args === 'object' && 'id' in args ? args : { id: undefined, param: args };
36
+ if (envelope.id !== id)
37
+ return; // ignore unrelated responses
38
+ const param = envelope.param;
39
+ // Try to extract text safely from different shapes we might receive
40
+ let text = null;
41
+ try {
42
+ if (!param) {
43
+ text = '';
44
+ }
45
+ else if (typeof param === 'string') {
46
+ text = param;
47
+ }
48
+ else if (param._bodyBlob && param._bodyBlob._buffer) {
49
+ // @ts-ignore
50
+ const byteArray = Object.values(param._bodyBlob._buffer);
51
+ // @ts-ignore
52
+ const uint8 = new Uint8Array(byteArray);
53
+ text = new TextDecoder().decode(uint8);
54
+ }
55
+ else if (param.body) {
56
+ text = typeof param.body === 'string' ? param.body : JSON.stringify(param.body);
57
+ }
58
+ else {
59
+ text = JSON.stringify(param);
60
+ }
61
+ }
62
+ catch (e) {
63
+ console.error('sendFetchRequest: failed to decode response body', e);
64
+ text = '';
65
+ }
41
66
  const response = new Response(text, {
42
67
  status: 200,
43
68
  headers: { "Content-Type": "application/json" }
44
69
  });
45
- //debugger;
46
- console.log("sendFetchRequest received", param);
47
- this.oWebViewInterface.removeListener('sendFetchRequest', callback);
70
+ console.log("sendFetchRequest received (id)", id, envelope.param);
71
+ // remove listeners
72
+ this.oWebViewInterface.removeListener('sendFetchRequest', handler);
73
+ this.oWebViewInterface.removeListener('sendFetchRequestResponse', handler);
48
74
  resolve(response);
49
75
  };
50
- this.oWebViewInterface.on('sendFetchRequest', callback);
51
- this.oWebViewInterface.emit("sendFetchRequest", { url, options });
76
+ // listen for both legacy and explicit response events
77
+ this.oWebViewInterface.on('sendFetchRequest', handler);
78
+ this.oWebViewInterface.on('sendFetchRequestResponse', handler);
79
+ // emit request with id so receiver can correlate
80
+ this.oWebViewInterface.emit("sendFetchRequest", { id, url, options });
52
81
  });
53
82
  }
54
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urga-panel/ur-panels-core",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,6 +9,7 @@ export type WVFrontServiceOts = ServiceOts & {
9
9
  }
10
10
 
11
11
  export class WVFrontService extends Service {
12
+ private static _requestIdCounter = 0;
12
13
  static serviceInfo = {
13
14
  name: "WVFrontService",
14
15
  requiredServices: [],
@@ -40,31 +41,57 @@ export class WVFrontService extends Service {
40
41
 
41
42
  public async sendFetchRequest(url: string, options?: RequestInit): Promise<any> {
42
43
  console.log("sendFetchRequest", url, options);
44
+ const id = `r_${Date.now()}_${++WVFrontService._requestIdCounter}`;
43
45
 
44
46
  return new Promise((resolve) => {
45
- const callback = (args: any) => {
46
- const param = args as unknown;
47
- // Convert param to a Response object
48
- //debugger;
49
- //@ts-ignore
50
- const byteArray = Object.values(param._bodyBlob._buffer);
51
- //@ts-ignore
52
- const uint8 = new Uint8Array(byteArray);
53
- //@ts-ignore
54
- const text = new TextDecoder().decode(uint8);
55
- const response = new Response(text,
56
- {
57
- status: 200,
58
- headers: { "Content-Type": "application/json" }
47
+ const handler = (args: any) => {
48
+ // support envelope { id, param } or legacy raw param
49
+ const envelope = args && typeof args === 'object' && 'id' in args ? args : { id: undefined, param: args };
50
+ if (envelope.id !== id) return; // ignore unrelated responses
51
+
52
+ const param = envelope.param as any;
53
+
54
+ // Try to extract text safely from different shapes we might receive
55
+ let text: string | null = null;
56
+ try {
57
+ if (!param) {
58
+ text = '';
59
+ } else if (typeof param === 'string') {
60
+ text = param;
61
+ } else if (param._bodyBlob && param._bodyBlob._buffer) {
62
+ // @ts-ignore
63
+ const byteArray = Object.values(param._bodyBlob._buffer);
64
+ // @ts-ignore
65
+ const uint8 = new Uint8Array(byteArray);
66
+ text = new TextDecoder().decode(uint8);
67
+ } else if (param.body) {
68
+ text = typeof param.body === 'string' ? param.body : JSON.stringify(param.body);
69
+ } else {
70
+ text = JSON.stringify(param);
59
71
  }
60
- );
61
- //debugger;
62
- console.log("sendFetchRequest received", param);
63
- this.oWebViewInterface.removeListener('sendFetchRequest', callback);
72
+ } catch (e) {
73
+ console.error('sendFetchRequest: failed to decode response body', e);
74
+ text = '';
75
+ }
76
+
77
+ const response = new Response(text, {
78
+ status: 200,
79
+ headers: { "Content-Type": "application/json" }
80
+ });
81
+
82
+ console.log("sendFetchRequest received (id)", id, envelope.param);
83
+ // remove listeners
84
+ this.oWebViewInterface.removeListener('sendFetchRequest', handler);
85
+ this.oWebViewInterface.removeListener('sendFetchRequestResponse', handler);
64
86
  resolve(response);
65
87
  };
66
- this.oWebViewInterface.on('sendFetchRequest', callback);
67
- this.oWebViewInterface.emit("sendFetchRequest", { url, options });
88
+
89
+ // listen for both legacy and explicit response events
90
+ this.oWebViewInterface.on('sendFetchRequest', handler);
91
+ this.oWebViewInterface.on('sendFetchRequestResponse', handler);
92
+
93
+ // emit request with id so receiver can correlate
94
+ this.oWebViewInterface.emit("sendFetchRequest", { id, url, options });
68
95
  });
69
96
  }
70
97