@urga-panel/ur-panels-core 1.0.2 → 1.0.4
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.
- package/dist/services/abstract/webviewServices/WVFrontService.d.ts +1 -0
- package/dist/services/abstract/webviewServices/WVFrontService.js +44 -15
- package/dist/types/Service.js +1 -1
- package/package.json +1 -1
- package/src/services/abstract/webviewServices/WVFrontService.ts +47 -20
- package/src/types/Service.ts +1 -1
|
@@ -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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
this.oWebViewInterface.removeListener('sendFetchRequest',
|
|
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
|
-
|
|
51
|
-
this.oWebViewInterface.
|
|
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/dist/types/Service.js
CHANGED
|
@@ -23,7 +23,7 @@ export class Service {
|
|
|
23
23
|
this.opts = opts;
|
|
24
24
|
this.tag = opts.tag;
|
|
25
25
|
if (!opts.usedService["LogService"]) {
|
|
26
|
-
console.error("LogService not found for", opts.tag);
|
|
26
|
+
//console.error("LogService not found for", opts.tag);
|
|
27
27
|
this.log = this.logSetup();
|
|
28
28
|
return;
|
|
29
29
|
}
|
package/package.json
CHANGED
|
@@ -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
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
67
|
-
|
|
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
|
|
package/src/types/Service.ts
CHANGED
|
@@ -41,7 +41,7 @@ export abstract class Service {
|
|
|
41
41
|
constructor(private opts: ServiceOts) {
|
|
42
42
|
this.tag = opts.tag;
|
|
43
43
|
if (!opts.usedService["LogService"]) {
|
|
44
|
-
console.error("LogService not found for", opts.tag);
|
|
44
|
+
//console.error("LogService not found for", opts.tag);
|
|
45
45
|
this.log = this.logSetup();
|
|
46
46
|
return;
|
|
47
47
|
}
|