-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (107 loc) · 5.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const HttpPort = require('ut-port-http');
module.exports = function(...params) {
let jsonRpcPortErrors;
return class JsonRpcPort extends HttpPort(...params) {
constructor() {
super(...arguments);
if (!this.errors || !this.errors.getError) throw new Error('Please use the latest version of ut-port');
jsonRpcPortErrors = jsonRpcPortErrors || require('./errors')(this.errors);
this.requestId = 1;
}
get defaults() {
return {
url: global.window && global.window.location.origin,
raw: {
json: true,
jar: true,
strictSSL: false
},
parseResponse: false,
requestTimeout: 300000,
minLatency: 100
};
}
handlers() {
const formatResponse = (msg, responseHeaders) => {
if (responseHeaders) msg.responseHeaders = responseHeaders;
return msg;
};
return {
receive: (msg, $meta) => {
const responseHeaders = $meta.forward?.returnResponseHeaders && $meta?.response?.headers;
if ($meta.mtid === 'error') {
if (msg && msg.body && msg.body.error && msg.body.error.type) {
const Error = jsonRpcPortErrors.rpc(msg.body.error);
Error.statusCode = msg.statusCode;
Error.statusText = msg.statusText;
Error.statusMessage = msg.statusMessage;
throw formatResponse(Error, responseHeaders);
}
return msg;
}
if (msg && msg.payload) {
if (!msg.payload.jsonrpc) {
return formatResponse(msg.payload, responseHeaders);
} else if (msg.payload.result !== undefined && msg.payload.error === undefined) {
if (msg.payload.id == null) {
$meta.mtid = 'discard';
}
if (msg.payload.$meta) {
const {validation, calls} = msg.payload.$meta;
Object.assign($meta, {validation, calls});
}
return formatResponse(msg.payload.result, responseHeaders);
} else if (typeof msg.payload.error === 'object') {
throw jsonRpcPortErrors.rpc(msg.payload.error);
}
throw jsonRpcPortErrors.wrongJsonRpcFormat(msg);
}
throw jsonRpcPortErrors.generic(msg);
},
send: (msg, $meta) => {
const timeout = $meta.timeout && this.timing && Math.floor(this.timing.diff(this.timing.now(), $meta.timeout));
if (Number.isFinite(timeout) && timeout <= this.config.minLatency) throw this.errors.timeout();
const $http = (msg && msg.$http) || {};
const isFormData = msg && msg.formData && (
global.window
? msg.formData instanceof window.FormData
: msg.formData.constructor.name === 'FormData'
);
if ($http.returnResponseHeaders) {
$meta.forward = {
...$meta.forward,
returnResponseHeaders: $http.returnResponseHeaders
};
}
const result = {
uri: $http.uri || `/rpc/${$meta.method.replace(/\//ig, '%2F')}`,
url: $http.url,
withCredentials: $http.withCredentials,
httpMethod: $http.httpMethod || 'POST',
headers: $http.headers,
requestTimeout: timeout,
blob: $http.blob,
...isFormData
? global.window
? {payload: msg.formData}
: {formData: msg.formData}
: {
payload: {
jsonrpc: '2.0',
method: $meta.method,
timeout: timeout && (timeout - this.config.minLatency),
params: (msg && !(msg instanceof Array) && Object.assign({}, msg)) || msg
}
}
};
if (!isFormData && $http.mtid !== 'notification' && $meta.mtid === 'request') {
result.payload.id = this.requestId++;
}
if (!isFormData && $http) delete result.payload.params.$http;
return result;
}
};
}
};
};