@nmtjs/json-format 0.15.0-beta.2 → 0.15.0-beta.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/client.d.ts +15 -0
- package/dist/client.js +1 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +1 -0
- package/dist/common.js.map +1 -0
- package/dist/server.d.ts +12 -0
- package/dist/server.js +1 -0
- package/dist/server.js.map +1 -0
- package/package.json +7 -6
- package/src/client.ts +123 -0
- package/src/common.ts +26 -0
- package/src/server.ts +79 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DecodeRPCContext } from '@nmtjs/protocol';
|
|
2
|
+
import type { EncodeRPCContext, ProtocolClientBlobStream, ProtocolServerBlobStream } from '@nmtjs/protocol/client';
|
|
3
|
+
import { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
4
|
+
/**
|
|
5
|
+
* Custom JSON encoding format with support for Neemata streams.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JsonFormat extends BaseClientFormat {
|
|
8
|
+
contentType: string;
|
|
9
|
+
encode(data: any, _replacer?: (key: string, value: any) => any): ArrayBufferView;
|
|
10
|
+
encodeRPC(data: unknown, context: EncodeRPCContext<ProtocolClientBlobStream>): Uint8Array<ArrayBuffer>;
|
|
11
|
+
decode(data: ArrayBufferView, _reviver?: (key: string, value: any) => any): any;
|
|
12
|
+
decodeRPC(_buffer: ArrayBufferView, context: DecodeRPCContext<(options?: {
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}) => ProtocolServerBlobStream>): any;
|
|
15
|
+
}
|
package/dist/client.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,6BAA6B;AAQ7B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,UA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AAE,CAAC;gBACnD,MAAM,EAAE,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAA;gBACrC,MAAM,QAAQ,GAAG,OAAO,CAAC,EAAE,CAAC,CAAA;gBAC5B,OAAO,OAAO,CAAC,SAAS,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAA;YACxC,CAAC;YACD,OAAO,KAAK,CAAA;QACd,CAAC,CAAA;QAED,IAAI,OAAO,UAAU,KAAK,WAAW;YAAE,OAAO,SAAS,CAAA;aAClD,IAAI,UAAU;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAA;;YAC3D,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IACxC,CAAC;CACF"}
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { 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 = [streams: StreamsMetadata, payload?: any];
|
|
7
|
+
export type ServerEncodedRPC = [streams: StreamsMetadata, payload?: any];
|
package/dist/common.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAEA,6DAA6D;AAC7D,MAAM,oBAAoB,GAAG,qBAAqB,CAAA;AAElD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,EAAU,EAAE,EAAE;IAC9C,OAAO,GAAG,oBAAoB,GAAG,EAAE,EAAE,CAAA;AACvC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,KAAa,EAAE,EAAE;IACnD,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAA;IACzD,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;AAClC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAU,EAAE,EAAE;IACvC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,CAAC,MAAM;QACZ,KAAK,CAAC,UAAU,CAAC,oBAAoB,CAAC,CACvC,CAAA;AACH,CAAC,CAAA"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DecodeRPCContext, EncodeRPCStreams } from '@nmtjs/protocol';
|
|
2
|
+
import type { ProtocolClientStream } from '@nmtjs/protocol/server';
|
|
3
|
+
import { BaseServerFormat } from '@nmtjs/protocol/server';
|
|
4
|
+
export declare class JsonFormat extends BaseServerFormat {
|
|
5
|
+
contentType: string;
|
|
6
|
+
accept: string[];
|
|
7
|
+
encode(data: any): Buffer<ArrayBuffer>;
|
|
8
|
+
encodeBlob(streamId: number): string;
|
|
9
|
+
encodeRPC(data: unknown, streams: EncodeRPCStreams): Uint8Array<ArrayBuffer>;
|
|
10
|
+
decode(data: Buffer, _reviver?: (key: string, value: any) => any): any;
|
|
11
|
+
decodeRPC(buffer: Buffer, context: DecodeRPCContext<() => ProtocolClientStream>): any;
|
|
12
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEzD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEhF,MAAM,OAAO,UAAW,SAAQ,gBAAgB;IAC9C,WAAW,GA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nmtjs/protocol": "0.15.0-beta.
|
|
9
|
+
"@nmtjs/common": "0.15.0-beta.4",
|
|
10
|
+
"@nmtjs/protocol": "0.15.0-beta.4"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@nmtjs/common": "0.15.0-beta.
|
|
14
|
-
"@nmtjs/protocol": "0.15.0-beta.
|
|
13
|
+
"@nmtjs/common": "0.15.0-beta.4",
|
|
14
|
+
"@nmtjs/protocol": "0.15.0-beta.4"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"dist",
|
|
18
|
+
"src",
|
|
18
19
|
"LICENSE.md",
|
|
19
20
|
"README.md"
|
|
20
21
|
],
|
|
21
|
-
"version": "0.15.0-beta.
|
|
22
|
+
"version": "0.15.0-beta.4",
|
|
22
23
|
"scripts": {
|
|
23
24
|
"clean-build": "rm -rf ./dist",
|
|
24
|
-
"build": "tsc",
|
|
25
|
+
"build": "tsc --declaration --sourcemap",
|
|
25
26
|
"dev": "tsc --watch",
|
|
26
27
|
"type-check": "tsc --noEmit"
|
|
27
28
|
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// ./ <reference lib="dom" />
|
|
2
|
+
|
|
3
|
+
import type { DecodeRPCContext, EncodeRPCStreams } from '@nmtjs/protocol'
|
|
4
|
+
import type {
|
|
5
|
+
EncodeRPCContext,
|
|
6
|
+
ProtocolClientBlobStream,
|
|
7
|
+
ProtocolServerBlobStream,
|
|
8
|
+
} from '@nmtjs/protocol/client'
|
|
9
|
+
import {
|
|
10
|
+
concat,
|
|
11
|
+
decodeNumber,
|
|
12
|
+
decodeText,
|
|
13
|
+
encodeNumber,
|
|
14
|
+
encodeText,
|
|
15
|
+
ProtocolBlob,
|
|
16
|
+
} from '@nmtjs/protocol'
|
|
17
|
+
import { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
18
|
+
|
|
19
|
+
import { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Custom JSON encoding format with support for Neemata streams.
|
|
23
|
+
*/
|
|
24
|
+
export class JsonFormat extends BaseClientFormat {
|
|
25
|
+
contentType = 'application/json'
|
|
26
|
+
|
|
27
|
+
encode(
|
|
28
|
+
data: any,
|
|
29
|
+
_replacer?: (key: string, value: any) => any,
|
|
30
|
+
): ArrayBufferView {
|
|
31
|
+
return encodeText(JSON.stringify(data, _replacer))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
encodeRPC(
|
|
35
|
+
data: unknown,
|
|
36
|
+
context: EncodeRPCContext<ProtocolClientBlobStream>,
|
|
37
|
+
) {
|
|
38
|
+
const buffers: (ArrayBufferView | ArrayBuffer)[] = []
|
|
39
|
+
const streams: EncodeRPCStreams = {}
|
|
40
|
+
let hasStreams = false
|
|
41
|
+
|
|
42
|
+
let payloadBuffer: ArrayBufferView | undefined
|
|
43
|
+
let streamsBuffer: ArrayBufferView
|
|
44
|
+
|
|
45
|
+
function _replacer(_key: string, value: any) {
|
|
46
|
+
if (value instanceof ProtocolBlob) {
|
|
47
|
+
hasStreams = true
|
|
48
|
+
const stream = context.addStream(value)
|
|
49
|
+
streams[stream.id] = stream.metadata
|
|
50
|
+
return serializeStreamId(stream.id)
|
|
51
|
+
}
|
|
52
|
+
return value
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (typeof data !== 'undefined') {
|
|
56
|
+
payloadBuffer = this.encode(data, _replacer)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (hasStreams) {
|
|
60
|
+
streamsBuffer = this.encode(streams)
|
|
61
|
+
buffers.push(
|
|
62
|
+
encodeNumber(streamsBuffer.byteLength, 'Uint32'),
|
|
63
|
+
streamsBuffer,
|
|
64
|
+
)
|
|
65
|
+
} else {
|
|
66
|
+
buffers.push(encodeNumber(0, 'Uint32'))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (typeof payloadBuffer !== 'undefined') {
|
|
70
|
+
buffers.push(payloadBuffer)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return concat(...buffers)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
decode(
|
|
77
|
+
data: ArrayBufferView,
|
|
78
|
+
_reviver?: (key: string, value: any) => any,
|
|
79
|
+
): any {
|
|
80
|
+
return JSON.parse(decodeText(data), _reviver)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
decodeRPC(
|
|
84
|
+
_buffer: ArrayBufferView,
|
|
85
|
+
context: DecodeRPCContext<
|
|
86
|
+
(options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
|
|
87
|
+
>,
|
|
88
|
+
) {
|
|
89
|
+
const buffer = new Uint8Array(
|
|
90
|
+
_buffer.buffer,
|
|
91
|
+
_buffer.byteOffset,
|
|
92
|
+
_buffer.byteLength,
|
|
93
|
+
)
|
|
94
|
+
const streamsLength = Number(decodeNumber(buffer, 'Uint32'))
|
|
95
|
+
const hasStreams = streamsLength > 0
|
|
96
|
+
const payloadBuffer = buffer.subarray(
|
|
97
|
+
Uint32Array.BYTES_PER_ELEMENT + streamsLength,
|
|
98
|
+
)
|
|
99
|
+
const hasPayload = payloadBuffer.byteLength > 0
|
|
100
|
+
|
|
101
|
+
const streams = hasStreams
|
|
102
|
+
? (this.decode(
|
|
103
|
+
buffer.subarray(
|
|
104
|
+
Uint32Array.BYTES_PER_ELEMENT,
|
|
105
|
+
Uint32Array.BYTES_PER_ELEMENT + streamsLength,
|
|
106
|
+
),
|
|
107
|
+
) as EncodeRPCStreams)
|
|
108
|
+
: {}
|
|
109
|
+
|
|
110
|
+
const replacer = (_key: string, value: any) => {
|
|
111
|
+
if (typeof value === 'string' && isStreamId(value)) {
|
|
112
|
+
const id = deserializeStreamId(value)
|
|
113
|
+
const metadata = streams[id]
|
|
114
|
+
return context.addStream(id, metadata)
|
|
115
|
+
}
|
|
116
|
+
return value
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof hasPayload === 'undefined') return undefined
|
|
120
|
+
else if (hasStreams) return this.decode(payloadBuffer, replacer)
|
|
121
|
+
else return this.decode(payloadBuffer)
|
|
122
|
+
}
|
|
123
|
+
}
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ProtocolBlobMetadata } from '@nmtjs/protocol'
|
|
2
|
+
|
|
3
|
+
// TODO: is this a good way to serialize streams within json?
|
|
4
|
+
const STREAM_SERIALIZE_KEY = '%neemata:stream:%\f'
|
|
5
|
+
|
|
6
|
+
export const serializeStreamId = (id: number) => {
|
|
7
|
+
return `${STREAM_SERIALIZE_KEY}${id}`
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const deserializeStreamId = (value: string) => {
|
|
11
|
+
const streamId = value.slice(STREAM_SERIALIZE_KEY.length)
|
|
12
|
+
return Number.parseInt(streamId)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const isStreamId = (value: any) => {
|
|
16
|
+
return (
|
|
17
|
+
typeof value === 'string' &&
|
|
18
|
+
value.length &&
|
|
19
|
+
value.startsWith(STREAM_SERIALIZE_KEY)
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type StreamsMetadata = Record<number, ProtocolBlobMetadata>
|
|
24
|
+
|
|
25
|
+
export type ClientEncodedRPC = [streams: StreamsMetadata, payload?: any]
|
|
26
|
+
export type ServerEncodedRPC = [streams: StreamsMetadata, payload?: any]
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { DecodeRPCContext, EncodeRPCStreams } from '@nmtjs/protocol'
|
|
2
|
+
import type { ProtocolClientStream } from '@nmtjs/protocol/server'
|
|
3
|
+
import { concat, decodeNumber, encodeNumber } from '@nmtjs/protocol'
|
|
4
|
+
import { BaseServerFormat } from '@nmtjs/protocol/server'
|
|
5
|
+
|
|
6
|
+
import { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'
|
|
7
|
+
|
|
8
|
+
export class JsonFormat extends BaseServerFormat {
|
|
9
|
+
contentType = 'application/json'
|
|
10
|
+
accept = ['application/json']
|
|
11
|
+
|
|
12
|
+
encode(data: any) {
|
|
13
|
+
return data ? Buffer.from(JSON.stringify(data), 'utf-8') : Buffer.alloc(0)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
encodeBlob(streamId: number) {
|
|
17
|
+
return serializeStreamId(streamId)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
encodeRPC(data: unknown, streams: EncodeRPCStreams) {
|
|
21
|
+
const buffers: (ArrayBufferView | ArrayBuffer)[] = []
|
|
22
|
+
const hasStreams = Object.keys(streams).length > 0
|
|
23
|
+
if (hasStreams) {
|
|
24
|
+
const encodedStreams = this.encode(streams)
|
|
25
|
+
buffers.push(
|
|
26
|
+
encodeNumber(encodedStreams.byteLength, 'Uint32'),
|
|
27
|
+
encodedStreams,
|
|
28
|
+
)
|
|
29
|
+
} else {
|
|
30
|
+
buffers.push(encodeNumber(0, 'Uint32'))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof data !== 'undefined') {
|
|
34
|
+
buffers.push(this.encode(data))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return concat(...buffers)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
decode(data: Buffer, _reviver?: (key: string, value: any) => any) {
|
|
41
|
+
return JSON.parse(data.toString('utf-8'), _reviver)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
decodeRPC(
|
|
45
|
+
buffer: Buffer,
|
|
46
|
+
context: DecodeRPCContext<() => ProtocolClientStream>,
|
|
47
|
+
) {
|
|
48
|
+
const streamsLength = Number(decodeNumber(buffer, 'Uint32'))
|
|
49
|
+
const hasStreams = streamsLength > 0
|
|
50
|
+
const payloadBuffer = buffer.subarray(
|
|
51
|
+
Uint32Array.BYTES_PER_ELEMENT + streamsLength,
|
|
52
|
+
)
|
|
53
|
+
const hasPayload = payloadBuffer.byteLength > 0
|
|
54
|
+
|
|
55
|
+
let streams: EncodeRPCStreams = {}
|
|
56
|
+
|
|
57
|
+
if (hasStreams) {
|
|
58
|
+
streams = this.decode(
|
|
59
|
+
buffer.subarray(
|
|
60
|
+
Uint32Array.BYTES_PER_ELEMENT,
|
|
61
|
+
Uint32Array.BYTES_PER_ELEMENT + streamsLength,
|
|
62
|
+
),
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const replacer = (_key: string, value: any) => {
|
|
67
|
+
if (typeof value === 'string' && isStreamId(value)) {
|
|
68
|
+
const id = deserializeStreamId(value)
|
|
69
|
+
const metadata = streams[id]
|
|
70
|
+
return context.addStream(id, metadata)
|
|
71
|
+
}
|
|
72
|
+
return value
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!hasPayload) return undefined
|
|
76
|
+
else if (hasStreams) return this.decode(payloadBuffer, replacer)
|
|
77
|
+
else return this.decode(payloadBuffer)
|
|
78
|
+
}
|
|
79
|
+
}
|