@nmtjs/json-format 0.12.6 → 0.12.8
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/package.json +5 -6
- package/src/client.ts +0 -107
- package/src/common.ts +0 -19
- package/src/server.ts +0 -104
package/package.json
CHANGED
|
@@ -14,20 +14,19 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/
|
|
17
|
+
"@nmtjs/common": "0.12.8",
|
|
18
|
+
"@nmtjs/protocol": "0.12.8"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/
|
|
21
|
+
"@nmtjs/protocol": "0.12.8",
|
|
22
|
+
"@nmtjs/common": "0.12.8"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
|
-
"src",
|
|
26
25
|
"dist",
|
|
27
26
|
"LICENSE.md",
|
|
28
27
|
"README.md"
|
|
29
28
|
],
|
|
30
|
-
"version": "0.12.
|
|
29
|
+
"version": "0.12.8",
|
|
31
30
|
"scripts": {
|
|
32
31
|
"build": "tsc",
|
|
33
32
|
"type-check": "tsc --noEmit"
|
package/src/client.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DecodeRPCContext,
|
|
3
|
-
decodeText,
|
|
4
|
-
type EncodeRPCContext,
|
|
5
|
-
encodeText,
|
|
6
|
-
ProtocolBlob,
|
|
7
|
-
type ProtocolBlobMetadata,
|
|
8
|
-
type ProtocolRPC,
|
|
9
|
-
} from '@nmtjs/protocol'
|
|
10
|
-
import {
|
|
11
|
-
BaseClientFormat,
|
|
12
|
-
type ProtocolClientBlobStream,
|
|
13
|
-
type ProtocolServerBlobStream,
|
|
14
|
-
} from '@nmtjs/protocol/client'
|
|
15
|
-
import { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Custom JSON encoding format with support for Neemata streams.
|
|
19
|
-
*/
|
|
20
|
-
export class JsonFormat extends BaseClientFormat {
|
|
21
|
-
contentType = 'application/x-neemata-json'
|
|
22
|
-
|
|
23
|
-
encode(data: any): ArrayBuffer {
|
|
24
|
-
return encodeText(JSON.stringify(data))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext) {
|
|
28
|
-
const { callId, namespace, procedure } = rpc
|
|
29
|
-
const streamsMetadata: Record<number, ProtocolBlobMetadata> = {}
|
|
30
|
-
const streams: Record<number, ProtocolClientBlobStream> = {}
|
|
31
|
-
const replacer = (key: string, value: any) => {
|
|
32
|
-
if (value instanceof ProtocolBlob) {
|
|
33
|
-
const stream = context.addStream(value)
|
|
34
|
-
streamsMetadata[stream.id] = stream.metadata
|
|
35
|
-
streams[stream.id] = stream
|
|
36
|
-
return serializeStreamId(stream.id)
|
|
37
|
-
}
|
|
38
|
-
return value
|
|
39
|
-
}
|
|
40
|
-
const payload =
|
|
41
|
-
typeof rpc.payload === 'undefined'
|
|
42
|
-
? undefined
|
|
43
|
-
: JSON.stringify(rpc.payload, replacer)
|
|
44
|
-
|
|
45
|
-
const buffer =
|
|
46
|
-
typeof payload === 'undefined'
|
|
47
|
-
? this.encode([callId, namespace, procedure, streamsMetadata])
|
|
48
|
-
: this.encode([callId, namespace, procedure, streamsMetadata, payload])
|
|
49
|
-
return { buffer, streams }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
decode(data: ArrayBuffer): any {
|
|
53
|
-
return JSON.parse(decodeText(data))
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext) {
|
|
57
|
-
const streams: Record<number, ProtocolServerBlobStream> = {}
|
|
58
|
-
const [callId, error, streamsMetadata, payload] = this.decode(buffer)
|
|
59
|
-
if (error) return { callId, error }
|
|
60
|
-
else {
|
|
61
|
-
const replacer = (key: string, value: any) => {
|
|
62
|
-
if (typeof value === 'string' && isStreamId(value)) {
|
|
63
|
-
const id = deserializeStreamId(value)
|
|
64
|
-
const metadata = streamsMetadata[id]
|
|
65
|
-
const stream = context.addStream(id, callId, metadata)
|
|
66
|
-
streams[id] = stream
|
|
67
|
-
return stream
|
|
68
|
-
}
|
|
69
|
-
return value
|
|
70
|
-
}
|
|
71
|
-
const result =
|
|
72
|
-
typeof payload === 'undefined'
|
|
73
|
-
? undefined
|
|
74
|
-
: JSON.parse(payload, replacer)
|
|
75
|
-
return { callId, result, streams }
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
82
|
-
*/
|
|
83
|
-
export class StandardJsonFormat extends BaseClientFormat {
|
|
84
|
-
contentType = 'application/json'
|
|
85
|
-
|
|
86
|
-
encode(data: any) {
|
|
87
|
-
return encodeText(JSON.stringify(data))
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
encodeRPC(rpc: ProtocolRPC) {
|
|
91
|
-
const { callId, namespace, procedure, payload } = rpc
|
|
92
|
-
const streams: Record<number, ProtocolClientBlobStream> = {}
|
|
93
|
-
const buffer = this.encode([callId, namespace, procedure, payload])
|
|
94
|
-
return { buffer, streams }
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
decode(data: ArrayBuffer) {
|
|
98
|
-
return JSON.parse(decodeText(data))
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
decodeRPC(buffer: ArrayBuffer) {
|
|
102
|
-
const streams: Record<number, ProtocolServerBlobStream> = {}
|
|
103
|
-
const [callId, error, result] = this.decode(buffer)
|
|
104
|
-
if (error) return { callId, error }
|
|
105
|
-
else return { callId, result, streams }
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/common.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// TODO: is this a good way to serialize streams within json?
|
|
2
|
-
const STREAM_SERIALIZE_KEY = '%neemata:stream:%\f'
|
|
3
|
-
|
|
4
|
-
export const serializeStreamId = (id: number) => {
|
|
5
|
-
return `${STREAM_SERIALIZE_KEY}${id}`
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const deserializeStreamId = (value: string) => {
|
|
9
|
-
const streamId = value.slice(STREAM_SERIALIZE_KEY.length)
|
|
10
|
-
return Number.parseInt(streamId)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const isStreamId = (value: any) => {
|
|
14
|
-
return (
|
|
15
|
-
typeof value === 'string' &&
|
|
16
|
-
value.length &&
|
|
17
|
-
value.startsWith(STREAM_SERIALIZE_KEY)
|
|
18
|
-
)
|
|
19
|
-
}
|
package/src/server.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DecodeRPCContext,
|
|
3
|
-
decodeText,
|
|
4
|
-
type EncodeRPCContext,
|
|
5
|
-
encodeText,
|
|
6
|
-
ProtocolBlob,
|
|
7
|
-
type ProtocolRPCResponse,
|
|
8
|
-
} from '@nmtjs/protocol'
|
|
9
|
-
import { BaseServerFormat } from '@nmtjs/protocol/server'
|
|
10
|
-
import { deserializeStreamId, isStreamId, serializeStreamId } from './common.ts'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Custom JSON encoding format with Neemata streams support.
|
|
14
|
-
*/
|
|
15
|
-
export class JsonFormat extends BaseServerFormat {
|
|
16
|
-
contentType = 'application/x-neemata-json'
|
|
17
|
-
accept = ['application/x-neemata-json']
|
|
18
|
-
|
|
19
|
-
encode(data: any): ArrayBuffer {
|
|
20
|
-
return encodeText(JSON.stringify(data))
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer {
|
|
24
|
-
const { callId, error } = rpc
|
|
25
|
-
if (error) return this.encode([callId, error])
|
|
26
|
-
else {
|
|
27
|
-
const streams: any = {}
|
|
28
|
-
const replacer = (key: string, value: any) => {
|
|
29
|
-
if (value instanceof ProtocolBlob) {
|
|
30
|
-
const stream = context.addStream(value)
|
|
31
|
-
streams[stream.id] = stream.metadata
|
|
32
|
-
return serializeStreamId(stream.id)
|
|
33
|
-
}
|
|
34
|
-
return value
|
|
35
|
-
}
|
|
36
|
-
const isUndefined = typeof rpc.result === 'undefined'
|
|
37
|
-
const payload = JSON.stringify(rpc.result, replacer)
|
|
38
|
-
return this.encode(
|
|
39
|
-
isUndefined
|
|
40
|
-
? [callId, null, streams]
|
|
41
|
-
: [callId, null, streams, payload],
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
decode(data: ArrayBuffer): any {
|
|
47
|
-
return JSON.parse(decodeText(data))
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext) {
|
|
51
|
-
const [callId, namespace, procedure, streams, formatPayload] =
|
|
52
|
-
this.decode(buffer)
|
|
53
|
-
|
|
54
|
-
const replacer = (key: string, value: any) => {
|
|
55
|
-
if (typeof value === 'string' && isStreamId(value)) {
|
|
56
|
-
const id = deserializeStreamId(value)
|
|
57
|
-
const metadata = streams[id]
|
|
58
|
-
return context.addStream(id, callId, metadata)
|
|
59
|
-
}
|
|
60
|
-
return value
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const payload =
|
|
64
|
-
typeof formatPayload === 'undefined'
|
|
65
|
-
? undefined
|
|
66
|
-
: JSON.parse(formatPayload, replacer)
|
|
67
|
-
|
|
68
|
-
return {
|
|
69
|
-
callId,
|
|
70
|
-
namespace,
|
|
71
|
-
procedure,
|
|
72
|
-
payload,
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Standard JSON encoding format with no Neemata streams support.
|
|
79
|
-
*/
|
|
80
|
-
export class StandardJsonFormat extends BaseServerFormat {
|
|
81
|
-
contentType = 'application/json'
|
|
82
|
-
accept = ['application/json', 'application/vnd.api+json']
|
|
83
|
-
|
|
84
|
-
encode(data: any) {
|
|
85
|
-
return encodeText(JSON.stringify(data))
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
encodeRPC(rpc: ProtocolRPCResponse) {
|
|
89
|
-
const { callId, error } = rpc
|
|
90
|
-
if (error) return this.encode([callId, error, null])
|
|
91
|
-
else {
|
|
92
|
-
return this.encode([callId, null, rpc.result])
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
decode(buffer: ArrayBuffer) {
|
|
97
|
-
return JSON.parse(decodeText(buffer))
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
decodeRPC(buffer: ArrayBuffer) {
|
|
101
|
-
const [callId, namespace, procedure, payload] = this.decode(buffer)
|
|
102
|
-
return { callId, namespace, procedure, payload }
|
|
103
|
-
}
|
|
104
|
-
}
|