@nmtjs/json-format 0.12.4 → 0.12.6
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/client.d.ts +48 -0
- package/dist/client.js +77 -106
- package/dist/common.d.ts +3 -0
- package/dist/common.js +8 -7
- package/dist/server.d.ts +34 -0
- package/dist/server.js +75 -93
- package/package.json +10 -8
- package/dist/client.js.map +0 -1
- package/dist/common.js.map +0 -1
- package/dist/server.js.map +0 -1
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type DecodeRPCContext, type EncodeRPCContext, type ProtocolRPC } from '@nmtjs/protocol';
|
|
2
|
+
import { BaseClientFormat, type ProtocolClientBlobStream, type ProtocolServerBlobStream } from '@nmtjs/protocol/client';
|
|
3
|
+
/**
|
|
4
|
+
* Custom JSON encoding format with support for Neemata streams.
|
|
5
|
+
*/
|
|
6
|
+
export declare class JsonFormat extends BaseClientFormat {
|
|
7
|
+
contentType: string;
|
|
8
|
+
encode(data: any): ArrayBuffer;
|
|
9
|
+
encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext): {
|
|
10
|
+
buffer: ArrayBuffer;
|
|
11
|
+
streams: Record<number, ProtocolClientBlobStream>;
|
|
12
|
+
};
|
|
13
|
+
decode(data: ArrayBuffer): any;
|
|
14
|
+
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
|
|
15
|
+
callId: any;
|
|
16
|
+
error: any;
|
|
17
|
+
result?: undefined;
|
|
18
|
+
streams?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
callId: any;
|
|
21
|
+
result: any;
|
|
22
|
+
streams: Record<number, ProtocolServerBlobStream>;
|
|
23
|
+
error?: undefined;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Standard JSON encoding format with no Neemata streams support.
|
|
28
|
+
*/
|
|
29
|
+
export declare class StandardJsonFormat extends BaseClientFormat {
|
|
30
|
+
contentType: string;
|
|
31
|
+
encode(data: any): ArrayBuffer;
|
|
32
|
+
encodeRPC(rpc: ProtocolRPC): {
|
|
33
|
+
buffer: ArrayBuffer;
|
|
34
|
+
streams: Record<number, ProtocolClientBlobStream>;
|
|
35
|
+
};
|
|
36
|
+
decode(data: ArrayBuffer): any;
|
|
37
|
+
decodeRPC(buffer: ArrayBuffer): {
|
|
38
|
+
callId: any;
|
|
39
|
+
error: any;
|
|
40
|
+
result?: undefined;
|
|
41
|
+
streams?: undefined;
|
|
42
|
+
} | {
|
|
43
|
+
callId: any;
|
|
44
|
+
result: any;
|
|
45
|
+
streams: Record<number, ProtocolServerBlobStream>;
|
|
46
|
+
error?: undefined;
|
|
47
|
+
};
|
|
48
|
+
}
|
package/dist/client.js
CHANGED
|
@@ -1,113 +1,84 @@
|
|
|
1
|
-
import { decodeText, encodeText, ProtocolBlob } from
|
|
2
|
-
import { BaseClientFormat } from
|
|
1
|
+
import { decodeText, encodeText, ProtocolBlob, } from '@nmtjs/protocol';
|
|
2
|
+
import { BaseClientFormat, } from '@nmtjs/protocol/client';
|
|
3
3
|
import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
|
|
4
4
|
/**
|
|
5
|
-
* Custom JSON encoding format with support for Neemata streams.
|
|
6
|
-
*/
|
|
5
|
+
* Custom JSON encoding format with support for Neemata streams.
|
|
6
|
+
*/
|
|
7
7
|
export class JsonFormat extends BaseClientFormat {
|
|
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
|
-
const stream = context.addStream(id, callId, metadata);
|
|
59
|
-
streams[id] = stream;
|
|
60
|
-
return stream;
|
|
61
|
-
}
|
|
62
|
-
return value;
|
|
63
|
-
};
|
|
64
|
-
const result = typeof payload === "undefined" ? undefined : JSON.parse(payload, replacer);
|
|
65
|
-
return {
|
|
66
|
-
callId,
|
|
67
|
-
result,
|
|
68
|
-
streams
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
8
|
+
contentType = 'application/x-neemata-json';
|
|
9
|
+
encode(data) {
|
|
10
|
+
return encodeText(JSON.stringify(data));
|
|
11
|
+
}
|
|
12
|
+
encodeRPC(rpc, context) {
|
|
13
|
+
const { callId, namespace, procedure } = rpc;
|
|
14
|
+
const streamsMetadata = {};
|
|
15
|
+
const streams = {};
|
|
16
|
+
const replacer = (key, value) => {
|
|
17
|
+
if (value instanceof ProtocolBlob) {
|
|
18
|
+
const stream = context.addStream(value);
|
|
19
|
+
streamsMetadata[stream.id] = stream.metadata;
|
|
20
|
+
streams[stream.id] = stream;
|
|
21
|
+
return serializeStreamId(stream.id);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
const payload = typeof rpc.payload === 'undefined'
|
|
26
|
+
? undefined
|
|
27
|
+
: JSON.stringify(rpc.payload, replacer);
|
|
28
|
+
const buffer = typeof payload === 'undefined'
|
|
29
|
+
? this.encode([callId, namespace, procedure, streamsMetadata])
|
|
30
|
+
: this.encode([callId, namespace, procedure, streamsMetadata, payload]);
|
|
31
|
+
return { buffer, streams };
|
|
32
|
+
}
|
|
33
|
+
decode(data) {
|
|
34
|
+
return JSON.parse(decodeText(data));
|
|
35
|
+
}
|
|
36
|
+
decodeRPC(buffer, context) {
|
|
37
|
+
const streams = {};
|
|
38
|
+
const [callId, error, streamsMetadata, payload] = this.decode(buffer);
|
|
39
|
+
if (error)
|
|
40
|
+
return { callId, error };
|
|
41
|
+
else {
|
|
42
|
+
const replacer = (key, value) => {
|
|
43
|
+
if (typeof value === 'string' && isStreamId(value)) {
|
|
44
|
+
const id = deserializeStreamId(value);
|
|
45
|
+
const metadata = streamsMetadata[id];
|
|
46
|
+
const stream = context.addStream(id, callId, metadata);
|
|
47
|
+
streams[id] = stream;
|
|
48
|
+
return stream;
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
const result = typeof payload === 'undefined'
|
|
53
|
+
? undefined
|
|
54
|
+
: JSON.parse(payload, replacer);
|
|
55
|
+
return { callId, result, streams };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
72
58
|
}
|
|
73
59
|
/**
|
|
74
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
75
|
-
*/
|
|
60
|
+
* Standard JSON encoding format with no Neemata streams support.
|
|
61
|
+
*/
|
|
76
62
|
export class StandardJsonFormat extends BaseClientFormat {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
decodeRPC(buffer) {
|
|
99
|
-
const streams = {};
|
|
100
|
-
const [callId, error, result] = this.decode(buffer);
|
|
101
|
-
if (error) return {
|
|
102
|
-
callId,
|
|
103
|
-
error
|
|
104
|
-
};
|
|
105
|
-
else return {
|
|
106
|
-
callId,
|
|
107
|
-
result,
|
|
108
|
-
streams
|
|
109
|
-
};
|
|
110
|
-
}
|
|
63
|
+
contentType = 'application/json';
|
|
64
|
+
encode(data) {
|
|
65
|
+
return encodeText(JSON.stringify(data));
|
|
66
|
+
}
|
|
67
|
+
encodeRPC(rpc) {
|
|
68
|
+
const { callId, namespace, procedure, payload } = rpc;
|
|
69
|
+
const streams = {};
|
|
70
|
+
const buffer = this.encode([callId, namespace, procedure, payload]);
|
|
71
|
+
return { buffer, streams };
|
|
72
|
+
}
|
|
73
|
+
decode(data) {
|
|
74
|
+
return JSON.parse(decodeText(data));
|
|
75
|
+
}
|
|
76
|
+
decodeRPC(buffer) {
|
|
77
|
+
const streams = {};
|
|
78
|
+
const [callId, error, result] = this.decode(buffer);
|
|
79
|
+
if (error)
|
|
80
|
+
return { callId, error };
|
|
81
|
+
else
|
|
82
|
+
return { callId, result, streams };
|
|
83
|
+
}
|
|
111
84
|
}
|
|
112
|
-
|
|
113
|
-
//# sourceMappingURL=client.js.map
|
package/dist/common.d.ts
ADDED
package/dist/common.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
// TODO: is this a good way to serialize streams within json?
|
|
2
|
+
const STREAM_SERIALIZE_KEY = '%neemata:stream:%\f';
|
|
2
3
|
export const serializeStreamId = (id) => {
|
|
3
|
-
|
|
4
|
+
return `${STREAM_SERIALIZE_KEY}${id}`;
|
|
4
5
|
};
|
|
5
6
|
export const deserializeStreamId = (value) => {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
const streamId = value.slice(STREAM_SERIALIZE_KEY.length);
|
|
8
|
+
return Number.parseInt(streamId);
|
|
8
9
|
};
|
|
9
10
|
export const isStreamId = (value) => {
|
|
10
|
-
|
|
11
|
+
return (typeof value === 'string' &&
|
|
12
|
+
value.length &&
|
|
13
|
+
value.startsWith(STREAM_SERIALIZE_KEY));
|
|
11
14
|
};
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=common.js.map
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type DecodeRPCContext, type EncodeRPCContext, type ProtocolRPCResponse } from '@nmtjs/protocol';
|
|
2
|
+
import { BaseServerFormat } from '@nmtjs/protocol/server';
|
|
3
|
+
/**
|
|
4
|
+
* Custom JSON encoding format with Neemata streams support.
|
|
5
|
+
*/
|
|
6
|
+
export declare class JsonFormat extends BaseServerFormat {
|
|
7
|
+
contentType: string;
|
|
8
|
+
accept: string[];
|
|
9
|
+
encode(data: any): ArrayBuffer;
|
|
10
|
+
encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer;
|
|
11
|
+
decode(data: ArrayBuffer): any;
|
|
12
|
+
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
|
|
13
|
+
callId: any;
|
|
14
|
+
namespace: any;
|
|
15
|
+
procedure: any;
|
|
16
|
+
payload: any;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Standard JSON encoding format with no Neemata streams support.
|
|
21
|
+
*/
|
|
22
|
+
export declare class StandardJsonFormat extends BaseServerFormat {
|
|
23
|
+
contentType: string;
|
|
24
|
+
accept: string[];
|
|
25
|
+
encode(data: any): ArrayBuffer;
|
|
26
|
+
encodeRPC(rpc: ProtocolRPCResponse): ArrayBuffer;
|
|
27
|
+
decode(buffer: ArrayBuffer): any;
|
|
28
|
+
decodeRPC(buffer: ArrayBuffer): {
|
|
29
|
+
callId: any;
|
|
30
|
+
namespace: any;
|
|
31
|
+
procedure: any;
|
|
32
|
+
payload: any;
|
|
33
|
+
};
|
|
34
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -1,100 +1,82 @@
|
|
|
1
|
-
import { decodeText, encodeText, ProtocolBlob } from
|
|
2
|
-
import { BaseServerFormat } from
|
|
1
|
+
import { decodeText, encodeText, ProtocolBlob, } from '@nmtjs/protocol';
|
|
2
|
+
import { BaseServerFormat } from '@nmtjs/protocol/server';
|
|
3
3
|
import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
|
|
4
4
|
/**
|
|
5
|
-
* Custom JSON encoding format with Neemata streams support.
|
|
6
|
-
*/
|
|
5
|
+
* Custom JSON encoding format with Neemata streams support.
|
|
6
|
+
*/
|
|
7
7
|
export class JsonFormat extends BaseServerFormat {
|
|
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
|
-
procedure,
|
|
58
|
-
payload
|
|
59
|
-
};
|
|
60
|
-
}
|
|
8
|
+
contentType = 'application/x-neemata-json';
|
|
9
|
+
accept = ['application/x-neemata-json'];
|
|
10
|
+
encode(data) {
|
|
11
|
+
return encodeText(JSON.stringify(data));
|
|
12
|
+
}
|
|
13
|
+
encodeRPC(rpc, context) {
|
|
14
|
+
const { callId, error } = rpc;
|
|
15
|
+
if (error)
|
|
16
|
+
return this.encode([callId, error]);
|
|
17
|
+
else {
|
|
18
|
+
const streams = {};
|
|
19
|
+
const replacer = (key, value) => {
|
|
20
|
+
if (value instanceof ProtocolBlob) {
|
|
21
|
+
const stream = context.addStream(value);
|
|
22
|
+
streams[stream.id] = stream.metadata;
|
|
23
|
+
return serializeStreamId(stream.id);
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
const isUndefined = typeof rpc.result === 'undefined';
|
|
28
|
+
const payload = JSON.stringify(rpc.result, replacer);
|
|
29
|
+
return this.encode(isUndefined
|
|
30
|
+
? [callId, null, streams]
|
|
31
|
+
: [callId, null, streams, payload]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
decode(data) {
|
|
35
|
+
return JSON.parse(decodeText(data));
|
|
36
|
+
}
|
|
37
|
+
decodeRPC(buffer, context) {
|
|
38
|
+
const [callId, namespace, procedure, streams, formatPayload] = this.decode(buffer);
|
|
39
|
+
const replacer = (key, value) => {
|
|
40
|
+
if (typeof value === 'string' && isStreamId(value)) {
|
|
41
|
+
const id = deserializeStreamId(value);
|
|
42
|
+
const metadata = streams[id];
|
|
43
|
+
return context.addStream(id, callId, metadata);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
};
|
|
47
|
+
const payload = typeof formatPayload === 'undefined'
|
|
48
|
+
? undefined
|
|
49
|
+
: JSON.parse(formatPayload, replacer);
|
|
50
|
+
return {
|
|
51
|
+
callId,
|
|
52
|
+
namespace,
|
|
53
|
+
procedure,
|
|
54
|
+
payload,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
61
57
|
}
|
|
62
58
|
/**
|
|
63
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
64
|
-
*/
|
|
59
|
+
* Standard JSON encoding format with no Neemata streams support.
|
|
60
|
+
*/
|
|
65
61
|
export class StandardJsonFormat extends BaseServerFormat {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
decode(buffer) {
|
|
87
|
-
return JSON.parse(decodeText(buffer));
|
|
88
|
-
}
|
|
89
|
-
decodeRPC(buffer) {
|
|
90
|
-
const [callId, namespace, procedure, payload] = this.decode(buffer);
|
|
91
|
-
return {
|
|
92
|
-
callId,
|
|
93
|
-
namespace,
|
|
94
|
-
procedure,
|
|
95
|
-
payload
|
|
96
|
-
};
|
|
97
|
-
}
|
|
62
|
+
contentType = 'application/json';
|
|
63
|
+
accept = ['application/json', 'application/vnd.api+json'];
|
|
64
|
+
encode(data) {
|
|
65
|
+
return encodeText(JSON.stringify(data));
|
|
66
|
+
}
|
|
67
|
+
encodeRPC(rpc) {
|
|
68
|
+
const { callId, error } = rpc;
|
|
69
|
+
if (error)
|
|
70
|
+
return this.encode([callId, error, null]);
|
|
71
|
+
else {
|
|
72
|
+
return this.encode([callId, null, rpc.result]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
decode(buffer) {
|
|
76
|
+
return JSON.parse(decodeText(buffer));
|
|
77
|
+
}
|
|
78
|
+
decodeRPC(buffer) {
|
|
79
|
+
const [callId, namespace, procedure, payload] = this.decode(buffer);
|
|
80
|
+
return { callId, namespace, procedure, payload };
|
|
81
|
+
}
|
|
98
82
|
}
|
|
99
|
-
|
|
100
|
-
//# sourceMappingURL=server.js.map
|
package/package.json
CHANGED
|
@@ -3,21 +3,23 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
5
|
"./client": {
|
|
6
|
+
"types": "./dist/client.d.ts",
|
|
6
7
|
"import": "./dist/client.js",
|
|
7
|
-
"
|
|
8
|
+
"module-sync": "./dist/client.js"
|
|
8
9
|
},
|
|
9
10
|
"./server": {
|
|
11
|
+
"types": "./dist/server.d.ts",
|
|
10
12
|
"import": "./dist/server.js",
|
|
11
|
-
"
|
|
13
|
+
"module-sync": "./dist/server.js"
|
|
12
14
|
}
|
|
13
15
|
},
|
|
14
16
|
"dependencies": {
|
|
15
|
-
"@nmtjs/protocol": "0.12.
|
|
16
|
-
"@nmtjs/common": "0.12.
|
|
17
|
+
"@nmtjs/protocol": "0.12.6",
|
|
18
|
+
"@nmtjs/common": "0.12.6"
|
|
17
19
|
},
|
|
18
20
|
"peerDependencies": {
|
|
19
|
-
"@nmtjs/common": "0.12.
|
|
20
|
-
"@nmtjs/protocol": "0.12.
|
|
21
|
+
"@nmtjs/common": "0.12.6",
|
|
22
|
+
"@nmtjs/protocol": "0.12.6"
|
|
21
23
|
},
|
|
22
24
|
"files": [
|
|
23
25
|
"src",
|
|
@@ -25,9 +27,9 @@
|
|
|
25
27
|
"LICENSE.md",
|
|
26
28
|
"README.md"
|
|
27
29
|
],
|
|
28
|
-
"version": "0.12.
|
|
30
|
+
"version": "0.12.6",
|
|
29
31
|
"scripts": {
|
|
30
|
-
"build": "
|
|
32
|
+
"build": "tsc",
|
|
31
33
|
"type-check": "tsc --noEmit"
|
|
32
34
|
}
|
|
33
35
|
}
|
package/dist/client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAEE,YAEA,YACA,oBAGK,iBAAiB;AACxB,SACE,wBAGK,wBAAwB;AAC/B,SAAS,qBAAqB,YAAY,yBAAyB,aAAa;;;;AAKhF,OAAO,MAAM,mBAAmB,iBAAiB;CAC/C,cAAc;CAEd,OAAOA,MAAwB;AAC7B,SAAO,WAAW,KAAK,UAAU,KAAK,CAAC;CACxC;CAED,UAAUC,KAAkBC,SAA2B;EACrD,MAAM,EAAE,QAAQ,WAAW,WAAW,GAAG;EACzC,MAAMC,kBAAwD,CAAE;EAChE,MAAMC,UAAoD,CAAE;EAC5D,MAAM,WAAW,CAACC,KAAaC,UAAe;AAC5C,OAAI,iBAAiB,cAAc;IACjC,MAAM,SAAS,QAAQ,UAAU,MAAM;AACvC,oBAAgB,OAAO,MAAM,OAAO;AACpC,YAAQ,OAAO,MAAM;AACrB,WAAO,kBAAkB,OAAO,GAAG;GACpC;AACD,UAAO;EACR;EACD,MAAM,iBACG,IAAI,YAAY,cACnB,YACA,KAAK,UAAU,IAAI,SAAS,SAAS;EAE3C,MAAM,gBACG,YAAY,cACf,KAAK,OAAO;GAAC;GAAQ;GAAW;GAAW;EAAgB,EAAC,GAC5D,KAAK,OAAO;GAAC;GAAQ;GAAW;GAAW;GAAiB;EAAQ,EAAC;AAC3E,SAAO;GAAE;GAAQ;EAAS;CAC3B;CAED,OAAOC,MAAwB;AAC7B,SAAO,KAAK,MAAM,WAAW,KAAK,CAAC;CACpC;CAED,UAAUC,QAAqBC,SAA2B;EACxD,MAAMC,UAAoD,CAAE;EAC5D,MAAM,CAAC,QAAQ,OAAO,iBAAiB,QAAQ,GAAG,KAAK,OAAO,OAAO;AACrE,MAAI,MAAO,QAAO;GAAE;GAAQ;EAAO;OAC9B;GACH,MAAM,WAAW,CAACL,KAAaC,UAAe;AAC5C,eAAW,UAAU,YAAY,WAAW,MAAM,EAAE;KAClD,MAAM,KAAK,oBAAoB,MAAM;KACrC,MAAM,WAAW,gBAAgB;KACjC,MAAM,SAAS,QAAQ,UAAU,IAAI,QAAQ,SAAS;AACtD,aAAQ,MAAM;AACd,YAAO;IACR;AACD,WAAO;GACR;GACD,MAAM,gBACG,YAAY,cACf,YACA,KAAK,MAAM,SAAS,SAAS;AACnC,UAAO;IAAE;IAAQ;IAAQ;GAAS;EACnC;CACF;AACF;;;;AAKD,OAAO,MAAM,2BAA2B,iBAAiB;CACvD,cAAc;CAEd,OAAON,MAAW;AAChB,SAAO,WAAW,KAAK,UAAU,KAAK,CAAC;CACxC;CAED,UAAUC,KAAkB;EAC1B,MAAM,EAAE,QAAQ,WAAW,WAAW,SAAS,GAAG;EAClD,MAAMG,UAAoD,CAAE;EAC5D,MAAM,SAAS,KAAK,OAAO;GAAC;GAAQ;GAAW;GAAW;EAAQ,EAAC;AACnE,SAAO;GAAE;GAAQ;EAAS;CAC3B;CAED,OAAOG,MAAmB;AACxB,SAAO,KAAK,MAAM,WAAW,KAAK,CAAC;CACpC;CAED,UAAUC,QAAqB;EAC7B,MAAME,UAAoD,CAAE;EAC5D,MAAM,CAAC,QAAQ,OAAO,OAAO,GAAG,KAAK,OAAO,OAAO;AACnD,MAAI,MAAO,QAAO;GAAE;GAAQ;EAAO;MAC9B,QAAO;GAAE;GAAQ;GAAQ;EAAS;CACxC;AACF","names":["data: any","rpc: ProtocolRPC","context: EncodeRPCContext","streamsMetadata: Record<number, ProtocolBlobMetadata>","streams: Record<number, ProtocolClientBlobStream>","key: string","value: any","data: ArrayBuffer","buffer: ArrayBuffer","context: DecodeRPCContext","streams: Record<number, ProtocolServerBlobStream>"],"sources":["../src/client.ts"],"sourcesContent":["import {\n type DecodeRPCContext,\n decodeText,\n type EncodeRPCContext,\n encodeText,\n ProtocolBlob,\n type ProtocolBlobMetadata,\n type ProtocolRPC,\n} from '@nmtjs/protocol'\nimport {\n BaseClientFormat,\n type ProtocolClientBlobStream,\n type ProtocolServerBlobStream,\n} from '@nmtjs/protocol/client'\nimport { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'\n\n/**\n * Custom JSON encoding format with support for Neemata streams.\n */\nexport class JsonFormat extends BaseClientFormat {\n contentType = 'application/x-neemata-json'\n\n encode(data: any): ArrayBuffer {\n return encodeText(JSON.stringify(data))\n }\n\n encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext) {\n const { callId, namespace, procedure } = rpc\n const streamsMetadata: Record<number, ProtocolBlobMetadata> = {}\n const streams: Record<number, ProtocolClientBlobStream> = {}\n const replacer = (key: string, value: any) => {\n if (value instanceof ProtocolBlob) {\n const stream = context.addStream(value)\n streamsMetadata[stream.id] = stream.metadata\n streams[stream.id] = stream\n return serializeStreamId(stream.id)\n }\n return value\n }\n const payload =\n typeof rpc.payload === 'undefined'\n ? undefined\n : JSON.stringify(rpc.payload, replacer)\n\n const buffer =\n typeof payload === 'undefined'\n ? this.encode([callId, namespace, procedure, streamsMetadata])\n : this.encode([callId, namespace, procedure, streamsMetadata, payload])\n return { buffer, streams }\n }\n\n decode(data: ArrayBuffer): any {\n return JSON.parse(decodeText(data))\n }\n\n decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext) {\n const streams: Record<number, ProtocolServerBlobStream> = {}\n const [callId, error, streamsMetadata, payload] = this.decode(buffer)\n if (error) return { callId, error }\n else {\n const replacer = (key: string, value: any) => {\n if (typeof value === 'string' && isStreamId(value)) {\n const id = deserializeStreamId(value)\n const metadata = streamsMetadata[id]\n const stream = context.addStream(id, callId, metadata)\n streams[id] = stream\n return stream\n }\n return value\n }\n const result =\n typeof payload === 'undefined'\n ? undefined\n : JSON.parse(payload, replacer)\n return { callId, result, streams }\n }\n }\n}\n\n/**\n * Standard JSON encoding format with no Neemata streams support.\n */\nexport class StandardJsonFormat extends BaseClientFormat {\n contentType = 'application/json'\n\n encode(data: any) {\n return encodeText(JSON.stringify(data))\n }\n\n encodeRPC(rpc: ProtocolRPC) {\n const { callId, namespace, procedure, payload } = rpc\n const streams: Record<number, ProtocolClientBlobStream> = {}\n const buffer = this.encode([callId, namespace, procedure, payload])\n return { buffer, streams }\n }\n\n decode(data: ArrayBuffer) {\n return JSON.parse(decodeText(data))\n }\n\n decodeRPC(buffer: ArrayBuffer) {\n const streams: Record<number, ProtocolServerBlobStream> = {}\n const [callId, error, result] = this.decode(buffer)\n if (error) return { callId, error }\n else return { callId, result, streams }\n }\n}\n"],"version":3,"file":"client.js"}
|
package/dist/common.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AACA,MAAM,uBAAuB;AAE7B,OAAO,MAAM,oBAAoB,CAACA,OAAe;AAC/C,SAAQ,EAAE,qBAAqB,EAAE,GAAG;AACrC;AAED,OAAO,MAAM,sBAAsB,CAACC,UAAkB;CACpD,MAAM,WAAW,MAAM,MAAM,qBAAqB,OAAO;AACzD,QAAO,OAAO,SAAS,SAAS;AACjC;AAED,OAAO,MAAM,aAAa,CAACC,UAAe;AACxC,eACS,UAAU,YACjB,MAAM,UACN,MAAM,WAAW,qBAAqB;AAEzC","names":["id: number","value: string","value: any"],"sources":["../src/common.ts"],"sourcesContent":["// TODO: is this a good way to serialize streams within json?\nconst STREAM_SERIALIZE_KEY = '%neemata:stream:%\\f'\n\nexport const serializeStreamId = (id: number) => {\n return `${STREAM_SERIALIZE_KEY}${id}`\n}\n\nexport const deserializeStreamId = (value: string) => {\n const streamId = value.slice(STREAM_SERIALIZE_KEY.length)\n return Number.parseInt(streamId)\n}\n\nexport const isStreamId = (value: any) => {\n return (\n typeof value === 'string' &&\n value.length &&\n value.startsWith(STREAM_SERIALIZE_KEY)\n )\n}\n"],"version":3,"file":"common.js"}
|
package/dist/server.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAEE,YAEA,YACA,oBAEK,iBAAiB;AACxB,SAAS,wBAAwB,wBAAwB;AACzD,SAAS,qBAAqB,YAAY,yBAAyB,aAAa;;;;AAKhF,OAAO,MAAM,mBAAmB,iBAAiB;CAC/C,cAAc;CACd,SAAS,CAAC,4BAA6B;CAEvC,OAAOA,MAAwB;AAC7B,SAAO,WAAW,KAAK,UAAU,KAAK,CAAC;CACxC;CAED,UAAUC,KAA0BC,SAAwC;EAC1E,MAAM,EAAE,QAAQ,OAAO,GAAG;AAC1B,MAAI,MAAO,QAAO,KAAK,OAAO,CAAC,QAAQ,KAAM,EAAC;OACzC;GACH,MAAMC,UAAe,CAAE;GACvB,MAAM,WAAW,CAACC,KAAaC,UAAe;AAC5C,QAAI,iBAAiB,cAAc;KACjC,MAAM,SAAS,QAAQ,UAAU,MAAM;AACvC,aAAQ,OAAO,MAAM,OAAO;AAC5B,YAAO,kBAAkB,OAAO,GAAG;IACpC;AACD,WAAO;GACR;GACD,MAAM,qBAAqB,IAAI,WAAW;GAC1C,MAAM,UAAU,KAAK,UAAU,IAAI,QAAQ,SAAS;AACpD,UAAO,KAAK,OACV,cACI;IAAC;IAAQ;IAAM;GAAQ,IACvB;IAAC;IAAQ;IAAM;IAAS;GAAQ,EACrC;EACF;CACF;CAED,OAAOC,MAAwB;AAC7B,SAAO,KAAK,MAAM,WAAW,KAAK,CAAC;CACpC;CAED,UAAUC,QAAqBC,SAA2B;EACxD,MAAM,CAAC,QAAQ,WAAW,WAAW,SAAS,cAAc,GAC1D,KAAK,OAAO,OAAO;EAErB,MAAM,WAAW,CAACJ,KAAaC,UAAe;AAC5C,cAAW,UAAU,YAAY,WAAW,MAAM,EAAE;IAClD,MAAM,KAAK,oBAAoB,MAAM;IACrC,MAAM,WAAW,QAAQ;AACzB,WAAO,QAAQ,UAAU,IAAI,QAAQ,SAAS;GAC/C;AACD,UAAO;EACR;EAED,MAAM,iBACG,kBAAkB,cACrB,YACA,KAAK,MAAM,eAAe,SAAS;AAEzC,SAAO;GACL;GACA;GACA;GACA;EACD;CACF;AACF;;;;AAKD,OAAO,MAAM,2BAA2B,iBAAiB;CACvD,cAAc;CACd,SAAS,CAAC,oBAAoB,0BAA2B;CAEzD,OAAOL,MAAW;AAChB,SAAO,WAAW,KAAK,UAAU,KAAK,CAAC;CACxC;CAED,UAAUC,KAA0B;EAClC,MAAM,EAAE,QAAQ,OAAO,GAAG;AAC1B,MAAI,MAAO,QAAO,KAAK,OAAO;GAAC;GAAQ;GAAO;EAAK,EAAC;OAC/C;AACH,UAAO,KAAK,OAAO;IAAC;IAAQ;IAAM,IAAI;GAAO,EAAC;EAC/C;CACF;CAED,OAAOM,QAAqB;AAC1B,SAAO,KAAK,MAAM,WAAW,OAAO,CAAC;CACtC;CAED,UAAUA,QAAqB;EAC7B,MAAM,CAAC,QAAQ,WAAW,WAAW,QAAQ,GAAG,KAAK,OAAO,OAAO;AACnE,SAAO;GAAE;GAAQ;GAAW;GAAW;EAAS;CACjD;AACF","names":["data: any","rpc: ProtocolRPCResponse","context: EncodeRPCContext","streams: any","key: string","value: any","data: ArrayBuffer","buffer: ArrayBuffer","context: DecodeRPCContext"],"sources":["../src/server.ts"],"sourcesContent":["import {\n type DecodeRPCContext,\n decodeText,\n type EncodeRPCContext,\n encodeText,\n ProtocolBlob,\n type ProtocolRPCResponse,\n} from '@nmtjs/protocol'\nimport { BaseServerFormat } from '@nmtjs/protocol/server'\nimport { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'\n\n/**\n * Custom JSON encoding format with Neemata streams support.\n */\nexport class JsonFormat extends BaseServerFormat {\n contentType = 'application/x-neemata-json'\n accept = ['application/x-neemata-json']\n\n encode(data: any): ArrayBuffer {\n return encodeText(JSON.stringify(data))\n }\n\n encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer {\n const { callId, error } = rpc\n if (error) return this.encode([callId, error])\n else {\n const streams: any = {}\n const replacer = (key: string, value: any) => {\n if (value instanceof ProtocolBlob) {\n const stream = context.addStream(value)\n streams[stream.id] = stream.metadata\n return serializeStreamId(stream.id)\n }\n return value\n }\n const isUndefined = typeof rpc.result === 'undefined'\n const payload = JSON.stringify(rpc.result, replacer)\n return this.encode(\n isUndefined\n ? [callId, null, streams]\n : [callId, null, streams, payload],\n )\n }\n }\n\n decode(data: ArrayBuffer): any {\n return JSON.parse(decodeText(data))\n }\n\n decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext) {\n const [callId, namespace, procedure, streams, formatPayload] =\n this.decode(buffer)\n\n const replacer = (key: string, value: any) => {\n if (typeof value === 'string' && isStreamId(value)) {\n const id = deserializeStreamId(value)\n const metadata = streams[id]\n return context.addStream(id, callId, metadata)\n }\n return value\n }\n\n const payload =\n typeof formatPayload === 'undefined'\n ? undefined\n : JSON.parse(formatPayload, replacer)\n\n return {\n callId,\n namespace,\n procedure,\n payload,\n }\n }\n}\n\n/**\n * Standard JSON encoding format with no Neemata streams support.\n */\nexport class StandardJsonFormat extends BaseServerFormat {\n contentType = 'application/json'\n accept = ['application/json', 'application/vnd.api+json']\n\n encode(data: any) {\n return encodeText(JSON.stringify(data))\n }\n\n encodeRPC(rpc: ProtocolRPCResponse) {\n const { callId, error } = rpc\n if (error) return this.encode([callId, error, null])\n else {\n return this.encode([callId, null, rpc.result])\n }\n }\n\n decode(buffer: ArrayBuffer) {\n return JSON.parse(decodeText(buffer))\n }\n\n decodeRPC(buffer: ArrayBuffer) {\n const [callId, namespace, procedure, payload] = this.decode(buffer)\n return { callId, namespace, procedure, payload }\n }\n}\n"],"version":3,"file":"server.js"}
|