@nmtjs/json-format 0.14.5 → 0.15.0-beta.2
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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/client.js +50 -73
- package/dist/server.js +36 -58
- package/package.json +9 -15
- package/dist/client.d.ts +0 -50
- package/dist/common.d.ts +0 -12
- package/dist/server.d.ts +0 -33
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/client.js
CHANGED
|
@@ -1,93 +1,70 @@
|
|
|
1
|
-
|
|
1
|
+
// ./ <reference lib="dom" />
|
|
2
|
+
import { concat, decodeNumber, decodeText, encodeNumber, encodeText, ProtocolBlob, } from '@nmtjs/protocol';
|
|
2
3
|
import { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
3
4
|
import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
|
|
4
5
|
/**
|
|
5
6
|
* Custom JSON encoding format with support for Neemata streams.
|
|
6
7
|
*/
|
|
7
8
|
export class JsonFormat extends BaseClientFormat {
|
|
8
|
-
contentType = 'application/
|
|
9
|
-
encode(data) {
|
|
10
|
-
return encodeText(JSON.stringify(data));
|
|
9
|
+
contentType = 'application/json';
|
|
10
|
+
encode(data, _replacer) {
|
|
11
|
+
return encodeText(JSON.stringify(data, _replacer));
|
|
11
12
|
}
|
|
12
|
-
encodeRPC(
|
|
13
|
-
const
|
|
14
|
-
const streamsMetadata = {};
|
|
13
|
+
encodeRPC(data, context) {
|
|
14
|
+
const buffers = [];
|
|
15
15
|
const streams = {};
|
|
16
|
-
|
|
16
|
+
let hasStreams = false;
|
|
17
|
+
let payloadBuffer;
|
|
18
|
+
let streamsBuffer;
|
|
19
|
+
function _replacer(_key, value) {
|
|
17
20
|
if (value instanceof ProtocolBlob) {
|
|
21
|
+
hasStreams = true;
|
|
18
22
|
const stream = context.addStream(value);
|
|
19
|
-
|
|
20
|
-
streams[stream.id] = stream;
|
|
23
|
+
streams[stream.id] = stream.metadata;
|
|
21
24
|
return serializeStreamId(stream.id);
|
|
22
25
|
}
|
|
23
26
|
return value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
streamsMetadata,
|
|
33
|
-
])
|
|
34
|
-
: this.encode([
|
|
35
|
-
callId,
|
|
36
|
-
procedure,
|
|
37
|
-
streamsMetadata,
|
|
38
|
-
payload,
|
|
39
|
-
]);
|
|
40
|
-
return { buffer, streams };
|
|
41
|
-
}
|
|
42
|
-
decode(data) {
|
|
43
|
-
return JSON.parse(decodeText(data));
|
|
44
|
-
}
|
|
45
|
-
decodeRPC(buffer, context) {
|
|
46
|
-
const streams = {};
|
|
47
|
-
const [callId, error, streamsMetadata, payload] = this.decode(buffer);
|
|
48
|
-
if (error)
|
|
49
|
-
return { callId, error };
|
|
27
|
+
}
|
|
28
|
+
if (typeof data !== 'undefined') {
|
|
29
|
+
payloadBuffer = this.encode(data, _replacer);
|
|
30
|
+
}
|
|
31
|
+
if (hasStreams) {
|
|
32
|
+
streamsBuffer = this.encode(streams);
|
|
33
|
+
buffers.push(encodeNumber(streamsBuffer.byteLength, 'Uint32'), streamsBuffer);
|
|
34
|
+
}
|
|
50
35
|
else {
|
|
51
|
-
|
|
52
|
-
if (typeof value === 'string' && isStreamId(value)) {
|
|
53
|
-
const id = deserializeStreamId(value);
|
|
54
|
-
const metadata = streamsMetadata[id];
|
|
55
|
-
const stream = context.addStream(id, callId, metadata);
|
|
56
|
-
streams[id] = stream;
|
|
57
|
-
return stream;
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
};
|
|
61
|
-
const result = typeof payload === 'undefined'
|
|
62
|
-
? undefined
|
|
63
|
-
: JSON.parse(payload, replacer);
|
|
64
|
-
return { callId, result, streams };
|
|
36
|
+
buffers.push(encodeNumber(0, 'Uint32'));
|
|
65
37
|
}
|
|
38
|
+
if (typeof payloadBuffer !== 'undefined') {
|
|
39
|
+
buffers.push(payloadBuffer);
|
|
40
|
+
}
|
|
41
|
+
return concat(...buffers);
|
|
66
42
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
70
|
-
*/
|
|
71
|
-
export class StandardJsonFormat extends BaseClientFormat {
|
|
72
|
-
contentType = 'application/json';
|
|
73
|
-
encode(data) {
|
|
74
|
-
return encodeText(JSON.stringify(data));
|
|
75
|
-
}
|
|
76
|
-
encodeRPC(rpc) {
|
|
77
|
-
const { callId, procedure, payload } = rpc;
|
|
78
|
-
const streams = {};
|
|
79
|
-
const buffer = this.encode([callId, procedure, payload]);
|
|
80
|
-
return { buffer, streams };
|
|
81
|
-
}
|
|
82
|
-
decode(data) {
|
|
83
|
-
return JSON.parse(decodeText(data));
|
|
43
|
+
decode(data, _reviver) {
|
|
44
|
+
return JSON.parse(decodeText(data), _reviver);
|
|
84
45
|
}
|
|
85
|
-
decodeRPC(
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
46
|
+
decodeRPC(_buffer, context) {
|
|
47
|
+
const buffer = new Uint8Array(_buffer.buffer, _buffer.byteOffset, _buffer.byteLength);
|
|
48
|
+
const streamsLength = Number(decodeNumber(buffer, 'Uint32'));
|
|
49
|
+
const hasStreams = streamsLength > 0;
|
|
50
|
+
const payloadBuffer = buffer.subarray(Uint32Array.BYTES_PER_ELEMENT + streamsLength);
|
|
51
|
+
const hasPayload = payloadBuffer.byteLength > 0;
|
|
52
|
+
const streams = hasStreams
|
|
53
|
+
? this.decode(buffer.subarray(Uint32Array.BYTES_PER_ELEMENT, Uint32Array.BYTES_PER_ELEMENT + streamsLength))
|
|
54
|
+
: {};
|
|
55
|
+
const replacer = (_key, value) => {
|
|
56
|
+
if (typeof value === 'string' && isStreamId(value)) {
|
|
57
|
+
const id = deserializeStreamId(value);
|
|
58
|
+
const metadata = streams[id];
|
|
59
|
+
return context.addStream(id, metadata);
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
};
|
|
63
|
+
if (typeof hasPayload === 'undefined')
|
|
64
|
+
return undefined;
|
|
65
|
+
else if (hasStreams)
|
|
66
|
+
return this.decode(payloadBuffer, replacer);
|
|
90
67
|
else
|
|
91
|
-
return
|
|
68
|
+
return this.decode(payloadBuffer);
|
|
92
69
|
}
|
|
93
70
|
}
|
package/dist/server.js
CHANGED
|
@@ -1,77 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { concat, decodeNumber, encodeNumber } from '@nmtjs/protocol';
|
|
2
2
|
import { BaseServerFormat } from '@nmtjs/protocol/server';
|
|
3
3
|
import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
|
|
4
|
-
/**
|
|
5
|
-
* Custom JSON encoding format with Neemata streams support.
|
|
6
|
-
*/
|
|
7
4
|
export class JsonFormat extends BaseServerFormat {
|
|
8
|
-
contentType = 'application/
|
|
9
|
-
accept = ['application/
|
|
5
|
+
contentType = 'application/json';
|
|
6
|
+
accept = ['application/json'];
|
|
10
7
|
encode(data) {
|
|
11
|
-
return
|
|
8
|
+
return data ? Buffer.from(JSON.stringify(data), 'utf-8') : Buffer.alloc(0);
|
|
9
|
+
}
|
|
10
|
+
encodeBlob(streamId) {
|
|
11
|
+
return serializeStreamId(streamId);
|
|
12
12
|
}
|
|
13
|
-
encodeRPC(
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
encodeRPC(data, streams) {
|
|
14
|
+
const buffers = [];
|
|
15
|
+
const hasStreams = Object.keys(streams).length > 0;
|
|
16
|
+
if (hasStreams) {
|
|
17
|
+
const encodedStreams = this.encode(streams);
|
|
18
|
+
buffers.push(encodeNumber(encodedStreams.byteLength, 'Uint32'), encodedStreams);
|
|
19
|
+
}
|
|
17
20
|
else {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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]);
|
|
21
|
+
buffers.push(encodeNumber(0, 'Uint32'));
|
|
22
|
+
}
|
|
23
|
+
if (typeof data !== 'undefined') {
|
|
24
|
+
buffers.push(this.encode(data));
|
|
32
25
|
}
|
|
26
|
+
return concat(...buffers);
|
|
33
27
|
}
|
|
34
|
-
decode(data) {
|
|
35
|
-
return JSON.parse(
|
|
28
|
+
decode(data, _reviver) {
|
|
29
|
+
return JSON.parse(data.toString('utf-8'), _reviver);
|
|
36
30
|
}
|
|
37
31
|
decodeRPC(buffer, context) {
|
|
38
|
-
const
|
|
32
|
+
const streamsLength = Number(decodeNumber(buffer, 'Uint32'));
|
|
33
|
+
const hasStreams = streamsLength > 0;
|
|
34
|
+
const payloadBuffer = buffer.subarray(Uint32Array.BYTES_PER_ELEMENT + streamsLength);
|
|
35
|
+
const hasPayload = payloadBuffer.byteLength > 0;
|
|
36
|
+
let streams = {};
|
|
37
|
+
if (hasStreams) {
|
|
38
|
+
streams = this.decode(buffer.subarray(Uint32Array.BYTES_PER_ELEMENT, Uint32Array.BYTES_PER_ELEMENT + streamsLength));
|
|
39
|
+
}
|
|
39
40
|
const replacer = (_key, value) => {
|
|
40
41
|
if (typeof value === 'string' && isStreamId(value)) {
|
|
41
42
|
const id = deserializeStreamId(value);
|
|
42
43
|
const metadata = streams[id];
|
|
43
|
-
return context.addStream(id,
|
|
44
|
+
return context.addStream(id, metadata);
|
|
44
45
|
}
|
|
45
46
|
return value;
|
|
46
47
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
55
|
-
*/
|
|
56
|
-
export class StandardJsonFormat extends BaseServerFormat {
|
|
57
|
-
contentType = 'application/json';
|
|
58
|
-
accept = ['application/json', 'application/vnd.api+json'];
|
|
59
|
-
encode(data) {
|
|
60
|
-
return encodeText(JSON.stringify(data));
|
|
61
|
-
}
|
|
62
|
-
encodeRPC(rpc) {
|
|
63
|
-
const { callId, error } = rpc;
|
|
64
|
-
if (error)
|
|
65
|
-
return this.encode([callId, error, null]);
|
|
66
|
-
else {
|
|
67
|
-
return this.encode([callId, null, rpc.result]);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
decode(buffer) {
|
|
71
|
-
return JSON.parse(decodeText(buffer));
|
|
72
|
-
}
|
|
73
|
-
decodeRPC(buffer) {
|
|
74
|
-
const [callId, procedure, payload] = this.decode(buffer);
|
|
75
|
-
return { callId, procedure, payload };
|
|
48
|
+
if (!hasPayload)
|
|
49
|
+
return undefined;
|
|
50
|
+
else if (hasStreams)
|
|
51
|
+
return this.decode(payloadBuffer, replacer);
|
|
52
|
+
else
|
|
53
|
+
return this.decode(payloadBuffer);
|
|
76
54
|
}
|
|
77
55
|
}
|
package/package.json
CHANGED
|
@@ -2,33 +2,27 @@
|
|
|
2
2
|
"name": "@nmtjs/json-format",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
|
-
"./client":
|
|
6
|
-
|
|
7
|
-
"import": "./dist/client.js",
|
|
8
|
-
"module-sync": "./dist/client.js"
|
|
9
|
-
},
|
|
10
|
-
"./server": {
|
|
11
|
-
"types": "./dist/server.d.ts",
|
|
12
|
-
"import": "./dist/server.js",
|
|
13
|
-
"module-sync": "./dist/server.js"
|
|
14
|
-
}
|
|
5
|
+
"./client": "./dist/client.js",
|
|
6
|
+
"./server": "./dist/server.js"
|
|
15
7
|
},
|
|
16
8
|
"dependencies": {
|
|
17
|
-
"@nmtjs/common": "0.
|
|
18
|
-
"@nmtjs/protocol": "0.
|
|
9
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
10
|
+
"@nmtjs/protocol": "0.15.0-beta.2"
|
|
19
11
|
},
|
|
20
12
|
"peerDependencies": {
|
|
21
|
-
"@nmtjs/common": "0.
|
|
22
|
-
"@nmtjs/protocol": "0.
|
|
13
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
14
|
+
"@nmtjs/protocol": "0.15.0-beta.2"
|
|
23
15
|
},
|
|
24
16
|
"files": [
|
|
25
17
|
"dist",
|
|
26
18
|
"LICENSE.md",
|
|
27
19
|
"README.md"
|
|
28
20
|
],
|
|
29
|
-
"version": "0.
|
|
21
|
+
"version": "0.15.0-beta.2",
|
|
30
22
|
"scripts": {
|
|
23
|
+
"clean-build": "rm -rf ./dist",
|
|
31
24
|
"build": "tsc",
|
|
25
|
+
"dev": "tsc --watch",
|
|
32
26
|
"type-check": "tsc --noEmit"
|
|
33
27
|
}
|
|
34
28
|
}
|
package/dist/client.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPC } from '@nmtjs/protocol';
|
|
2
|
-
import type { ProtocolClientBlobStream, ProtocolServerBlobStream } from '@nmtjs/protocol/client';
|
|
3
|
-
import { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
4
|
-
import type { StreamsMetadata } from './common.ts';
|
|
5
|
-
/**
|
|
6
|
-
* Custom JSON encoding format with support for Neemata streams.
|
|
7
|
-
*/
|
|
8
|
-
export declare class JsonFormat extends BaseClientFormat {
|
|
9
|
-
contentType: string;
|
|
10
|
-
encode(data: any): ArrayBuffer;
|
|
11
|
-
encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext): {
|
|
12
|
-
buffer: ArrayBuffer;
|
|
13
|
-
streams: Record<number, ProtocolClientBlobStream>;
|
|
14
|
-
};
|
|
15
|
-
decode(data: ArrayBuffer): any;
|
|
16
|
-
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
|
|
17
|
-
callId: number;
|
|
18
|
-
error: import("@nmtjs/protocol").BaseProtocolError;
|
|
19
|
-
result?: undefined;
|
|
20
|
-
streams?: undefined;
|
|
21
|
-
} | {
|
|
22
|
-
callId: number;
|
|
23
|
-
result: any;
|
|
24
|
-
streams: Record<number, ProtocolServerBlobStream>;
|
|
25
|
-
error?: undefined;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
30
|
-
*/
|
|
31
|
-
export declare class StandardJsonFormat extends BaseClientFormat {
|
|
32
|
-
contentType: string;
|
|
33
|
-
encode(data: any): ArrayBuffer;
|
|
34
|
-
encodeRPC(rpc: ProtocolRPC): {
|
|
35
|
-
buffer: ArrayBuffer;
|
|
36
|
-
streams: Record<number, ProtocolClientBlobStream>;
|
|
37
|
-
};
|
|
38
|
-
decode(data: ArrayBuffer): any;
|
|
39
|
-
decodeRPC(buffer: ArrayBuffer): {
|
|
40
|
-
callId: number;
|
|
41
|
-
error: import("@nmtjs/protocol").BaseProtocolError;
|
|
42
|
-
result?: undefined;
|
|
43
|
-
streams?: undefined;
|
|
44
|
-
} | {
|
|
45
|
-
callId: number;
|
|
46
|
-
result: StreamsMetadata;
|
|
47
|
-
streams: Record<number, ProtocolServerBlobStream>;
|
|
48
|
-
error?: undefined;
|
|
49
|
-
};
|
|
50
|
-
}
|
package/dist/common.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { BaseProtocolError, ProtocolBlobMetadata } from '@nmtjs/protocol';
|
|
2
|
-
export declare const serializeStreamId: (id: number) => string;
|
|
3
|
-
export declare const deserializeStreamId: (value: string) => number;
|
|
4
|
-
export declare const isStreamId: (value: any) => boolean | 0;
|
|
5
|
-
export type StreamsMetadata = Record<number, ProtocolBlobMetadata>;
|
|
6
|
-
export type ClientEncodedRPC = [
|
|
7
|
-
callId: number,
|
|
8
|
-
procedure: string,
|
|
9
|
-
streams: StreamsMetadata,
|
|
10
|
-
payload?: any
|
|
11
|
-
];
|
|
12
|
-
export type ServerEncodedRPC = [callId: number, error: BaseProtocolError] | [callId: number, error: null, streams: StreamsMetadata, payload?: any];
|
package/dist/server.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPCResponse } from '@nmtjs/protocol';
|
|
2
|
-
import { BaseServerFormat } from '@nmtjs/protocol/server';
|
|
3
|
-
import type { StreamsMetadata } from './common.ts';
|
|
4
|
-
/**
|
|
5
|
-
* Custom JSON encoding format with Neemata streams support.
|
|
6
|
-
*/
|
|
7
|
-
export declare class JsonFormat extends BaseServerFormat {
|
|
8
|
-
contentType: string;
|
|
9
|
-
accept: string[];
|
|
10
|
-
encode(data: any): ArrayBuffer;
|
|
11
|
-
encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer;
|
|
12
|
-
decode(data: ArrayBuffer): any;
|
|
13
|
-
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
|
|
14
|
-
callId: number;
|
|
15
|
-
procedure: string;
|
|
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: number;
|
|
30
|
-
procedure: string;
|
|
31
|
-
payload: StreamsMetadata;
|
|
32
|
-
};
|
|
33
|
-
}
|