Skip to content

Commit 95cda68

Browse files
committed
Add js bundle
1 parent c122ff5 commit 95cda68

File tree

1 file changed

+133
-130
lines changed

1 file changed

+133
-130
lines changed

client/dist/remote.js

Lines changed: 133 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
function validateRpcBasis(data) {
2-
return data?.jsonrpc === "2.0" && (typeof data.id === "number" || typeof data.id === "string" || data.id === null);
3-
}
4-
function validateRpcSuccess(data) {
5-
return "result" in data;
6-
}
7-
function validateRpcFailure(data) {
8-
return typeof data?.error?.code === "number" && typeof data.error.message === "string";
9-
}
101
class BadServerDataError extends Error {
112
constructor(id, message, errorCode, data){
123
super(message);
13-
this.id = id, this.name = this.constructor.name;
4+
this.id = id;
5+
this.name = this.constructor.name;
146
this.code = errorCode;
157
this.data = data;
168
}
179
}
10+
function send(resource, fetchInit) {
11+
return fetch(resource instanceof URL ? resource.href : resource, fetchInit).then((res)=>{
12+
if (!res.ok) {
13+
return Promise.reject(new BadServerDataError(null, `${res.status} '${res.statusText}' received instead of 200-299 range.`, -32002));
14+
} else if (res.status === 204 || res.headers.get("content-length") === "0") {
15+
return undefined;
16+
} else return res.json();
17+
}).catch((err)=>Promise.reject(new BadServerDataError(null, err.message, -32001))
18+
);
19+
}
20+
function validateRpcBasis(data1) {
21+
return data1?.jsonrpc === "2.0" && (typeof data1.id === "number" || typeof data1.id === "string" || data1.id === null);
22+
}
23+
function validateRpcSuccess(data1) {
24+
return "result" in data1;
25+
}
26+
function validateRpcFailure(data1) {
27+
return typeof data1?.error?.code === "number" && typeof data1.error.message === "string";
28+
}
1829
function validateResponse(data1) {
1930
if (validateRpcBasis(data1)) {
2031
if (validateRpcSuccess(data1)) return data1;
@@ -24,6 +35,18 @@ function validateResponse(data1) {
2435
}
2536
throw new BadServerDataError(null, "Received data is no RPC response object.", -32003);
2637
}
38+
const validateResponse1 = validateResponse;
39+
function processBatchArray(rpcResponseBatch) {
40+
return rpcResponseBatch.map((rpcResponse)=>validateResponse(rpcResponse).result
41+
);
42+
}
43+
function processBatchObject(rpcResponseBatch) {
44+
return rpcResponseBatch.reduce((acc, rpcResponse)=>{
45+
acc[rpcResponse.id] = validateResponse1(rpcResponse).result;
46+
return acc;
47+
}, {
48+
});
49+
}
2750
function bytesToUuid(bytes) {
2851
const bits = [
2952
...bytes
@@ -43,20 +66,22 @@ function bytesToUuid(bytes) {
4366
...bits.slice(10, 16),
4467
].join("");
4568
}
46-
const UUID_RE = new RegExp("^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", "i");
69+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
4770
function generate() {
4871
const rnds = crypto.getRandomValues(new Uint8Array(16));
4972
rnds[6] = rnds[6] & 15 | 64;
5073
rnds[8] = rnds[8] & 63 | 128;
5174
return bytesToUuid(rnds);
5275
}
76+
const generate1 = generate;
77+
const generateV4Uuid = generate1;
5378
function createRequest({ method , params , isNotification =false , id: id1 }) {
5479
const rpcRequest = {
5580
jsonrpc: "2.0",
5681
method
5782
};
5883
params && (rpcRequest.params = params);
59-
id1 = isNotification ? undefined : id1 !== undefined ? id1 : generate();
84+
id1 = isNotification ? undefined : id1 !== undefined ? id1 : generateV4Uuid();
6085
id1 !== undefined && (rpcRequest.id = id1);
6186
return rpcRequest;
6287
}
@@ -74,57 +99,64 @@ function createRequestBatch(batchObj, isNotification = false) {
7499
})
75100
);
76101
}
77-
const httpProxyHandler = {
78-
get (client, name) {
79-
if (client[name] !== undefined) {
80-
return client[name];
81-
} else {
82-
const proxyFunction = (args)=>client.call(name, args)
83-
;
84-
proxyFunction.notify = (args)=>client.call(name, args, {
85-
isNotification: true
86-
})
87-
;
88-
proxyFunction.auth = (jwt)=>(args)=>client.call(name, args, {
89-
jwt
90-
})
91-
;
92-
proxyFunction.batch = (args, isNotification = false)=>client.batch([
93-
name,
94-
...args
95-
], isNotification)
96-
;
97-
return proxyFunction;
102+
class Client {
103+
constructor(resource, options = {
104+
}){
105+
const headers = options.headers === undefined ? new Headers() : options.headers instanceof Headers ? options.headers : new Headers(Object.entries(options.headers));
106+
headers.set("Content-Type", "application/json");
107+
this.fetchInit = {
108+
...options,
109+
method: "POST",
110+
headers
111+
};
112+
this.resource = resource;
113+
}
114+
async batch(batchObj, isNotification) {
115+
const rpcResponseBatch = await send(this.resource, {
116+
...this.fetchInit,
117+
body: JSON.stringify(createRequestBatch(batchObj, isNotification))
118+
});
119+
try {
120+
if (rpcResponseBatch === undefined && isNotification) {
121+
return rpcResponseBatch;
122+
} else if (Array.isArray(rpcResponseBatch) && rpcResponseBatch.length > 0) {
123+
return Array.isArray(batchObj) ? processBatchArray(rpcResponseBatch) : processBatchObject(rpcResponseBatch);
124+
} else {
125+
throw new BadServerDataError(null, "The server returned an invalid batch response.", -32004);
126+
}
127+
} catch (err) {
128+
return Promise.reject(err);
98129
}
99130
}
100-
};
101-
const wsProxyHandler = {
102-
get (client, name) {
103-
if (client[name] !== undefined || name === "then") {
104-
return client[name];
105-
} else {
106-
const proxyFunction = (args)=>client.call(name, args)
107-
;
108-
proxyFunction.notify = (args)=>client.call(name, args, true)
109-
;
110-
proxyFunction.subscribe = ()=>client.subscribe(name)
111-
;
112-
return proxyFunction;
131+
async call(method, params, { isNotification , jwt } = {
132+
}) {
133+
const rpcRequestObj = createRequest({
134+
method,
135+
params,
136+
isNotification
137+
});
138+
if (jwt && this.fetchInit.headers instanceof Headers) {
139+
this.fetchInit.headers.set("Authorization", `Bearer ${jwt}`);
140+
}
141+
const rpcResponsePromise = send(this.resource, {
142+
...this.fetchInit,
143+
body: JSON.stringify(rpcRequestObj)
144+
});
145+
if (jwt && this.fetchInit.headers instanceof Headers) {
146+
this.fetchInit.headers.delete("Authorization");
147+
}
148+
const rpcResponse = await rpcResponsePromise;
149+
try {
150+
return rpcResponse === undefined && isNotification ? undefined : validateResponse(rpcResponse).result;
151+
} catch (err) {
152+
return Promise.reject(err);
113153
}
114154
}
115-
};
116-
function listen(socket) {
117-
return new Promise((resolve, reject)=>{
118-
socket.onopen = ()=>resolve(socket)
119-
;
120-
socket.onerror = (err)=>reject(err)
121-
;
122-
});
123155
}
124156
function isObject(obj) {
125157
return obj !== null && typeof obj === "object" && Array.isArray(obj) === false;
126158
}
127-
class Client {
159+
class Client1 {
128160
constructor(socket1){
129161
this.socket = socket1;
130162
this.getPayloadData(socket1);
@@ -249,90 +281,61 @@ class Client {
249281
};
250282
}
251283
}
252-
function send(resource, fetchInit) {
253-
return fetch(resource instanceof URL ? resource.href : resource, fetchInit).then((res)=>{
254-
if (!res.ok) {
255-
return Promise.reject(new BadServerDataError(null, `${res.status} '${res.statusText}' received instead of 200-299 range.`, -32002));
256-
} else if (res.status === 204 || res.headers.get("content-length") === "0") {
257-
return undefined;
258-
} else return res.json();
259-
}).catch((err)=>Promise.reject(new BadServerDataError(null, err.message, -32001))
260-
);
261-
}
262-
function processBatchArray(rpcResponseBatch) {
263-
return rpcResponseBatch.map((rpcResponse)=>validateResponse(rpcResponse).result
264-
);
265-
}
266-
function processBatchObject(rpcResponseBatch) {
267-
return rpcResponseBatch.reduce((acc, rpcResponse)=>{
268-
acc[rpcResponse.id] = validateResponse(rpcResponse).result;
269-
return acc;
270-
}, {
271-
});
272-
}
273-
class Client1 {
274-
constructor(resource, options = {
275-
}){
276-
const headers = options.headers === undefined ? new Headers() : options.headers instanceof Headers ? options.headers : new Headers(Object.entries(options.headers));
277-
headers.set("Content-Type", "application/json");
278-
this.fetchInit = {
279-
...options,
280-
method: "POST",
281-
headers
282-
};
283-
this.resource = resource;
284-
}
285-
async batch(batchObj, isNotification) {
286-
const rpcResponseBatch = await send(this.resource, {
287-
...this.fetchInit,
288-
body: JSON.stringify(createRequestBatch(batchObj, isNotification))
289-
});
290-
try {
291-
if (rpcResponseBatch === undefined && isNotification) {
292-
return rpcResponseBatch;
293-
} else if (Array.isArray(rpcResponseBatch) && rpcResponseBatch.length > 0) {
294-
return Array.isArray(batchObj) ? processBatchArray(rpcResponseBatch) : processBatchObject(rpcResponseBatch);
295-
} else {
296-
throw new BadServerDataError(null, "The server returned an invalid batch response.", -32004);
297-
}
298-
} catch (err) {
299-
return Promise.reject(err);
284+
const httpProxyHandler = {
285+
get (client, name) {
286+
if (client[name] !== undefined) {
287+
return client[name];
288+
} else {
289+
const proxyFunction = (args)=>client.call(name, args)
290+
;
291+
proxyFunction.notify = (args)=>client.call(name, args, {
292+
isNotification: true
293+
})
294+
;
295+
proxyFunction.auth = (jwt)=>(args)=>client.call(name, args, {
296+
jwt
297+
})
298+
;
299+
proxyFunction.batch = (args, isNotification = false)=>client.batch([
300+
name,
301+
...args
302+
], isNotification)
303+
;
304+
return proxyFunction;
300305
}
301306
}
302-
async call(method, params, { isNotification , jwt } = {
303-
}) {
304-
const rpcRequestObj = createRequest({
305-
method,
306-
params,
307-
isNotification
308-
});
309-
if (jwt && this.fetchInit.headers instanceof Headers) {
310-
this.fetchInit.headers.set("Authorization", `Bearer ${jwt}`);
311-
}
312-
const rpcResponsePromise = send(this.resource, {
313-
...this.fetchInit,
314-
body: JSON.stringify(rpcRequestObj)
315-
});
316-
if (jwt && this.fetchInit.headers instanceof Headers) {
317-
this.fetchInit.headers.delete("Authorization");
318-
}
319-
const rpcResponse = await rpcResponsePromise;
320-
try {
321-
return rpcResponse === undefined && isNotification ? undefined : validateResponse(rpcResponse).result;
322-
} catch (err) {
323-
return Promise.reject(err);
307+
};
308+
const wsProxyHandler = {
309+
get (client, name) {
310+
if (client[name] !== undefined || name === "then") {
311+
return client[name];
312+
} else {
313+
const proxyFunction = (args)=>client.call(name, args)
314+
;
315+
proxyFunction.notify = (args)=>client.call(name, args, true)
316+
;
317+
proxyFunction.subscribe = ()=>client.subscribe(name)
318+
;
319+
return proxyFunction;
324320
}
325321
}
322+
};
323+
function listen(socket2) {
324+
return new Promise((resolve, reject)=>{
325+
socket2.onopen = ()=>resolve(socket2)
326+
;
327+
socket2.onerror = (err)=>reject(err)
328+
;
329+
});
326330
}
327-
function createRemote(resourceOrSocket, options1) {
331+
function createRemote1(resourceOrSocket, options1) {
328332
if (resourceOrSocket instanceof WebSocket) {
329-
return listen(resourceOrSocket).then((socket2)=>new Proxy(new Client(socket2), wsProxyHandler)
333+
return listen(resourceOrSocket).then((socket2)=>new Proxy(new Client1(socket2), wsProxyHandler)
330334
).catch((err)=>Promise.reject(new BadServerDataError(null, "An error event occured on the WebSocket connection.", -32005, err.stack))
331335
);
332336
} else {
333-
return new Proxy(new Client1(resourceOrSocket, options1), httpProxyHandler);
337+
return new Proxy(new Client(resourceOrSocket, options1), httpProxyHandler);
334338
}
335339
}
336-
const createRemote1 = createRemote;
337-
export { createRemote as createRemote };
340+
export { createRemote1 as createRemote };
338341

0 commit comments

Comments
 (0)