@nmtjs/msgpack-format 0.15.0-beta.24
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 +7 -0
- package/README.md +9 -0
- package/package.json +27 -0
- package/src/client.ts +72 -0
- package/src/common.ts +49 -0
- package/src/server.ts +84 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# NeemataJS - RPC application server for real-time applications (proof of concept)
|
|
2
|
+
|
|
3
|
+
### Built with following in mind:
|
|
4
|
+
- transport-agnostic (like WebSockets, WebTransport, .etc)
|
|
5
|
+
- format-agnostic (like JSON, MessagePack, BSON, .etc)
|
|
6
|
+
- binary data streaming and event subscriptions
|
|
7
|
+
- contract-based API
|
|
8
|
+
- end-to-end type safety
|
|
9
|
+
- CPU-intensive task execution on separate workers
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nmtjs/msgpack-format",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"exports": {
|
|
5
|
+
"./client": "./dist/client.js",
|
|
6
|
+
"./server": "./dist/server.js"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@msgpack/msgpack": "^3.1.3",
|
|
10
|
+
"@nmtjs/common": "0.15.0-beta.24",
|
|
11
|
+
"@nmtjs/protocol": "0.15.0-beta.24"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@nmtjs/protocol": "0.15.0-beta.24",
|
|
15
|
+
"@nmtjs/common": "0.15.0-beta.24"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src",
|
|
20
|
+
"LICENSE.md",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"version": "0.15.0-beta.24",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"clean-build": "rm -rf ./dist"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { DecodeRPCContext } from '@nmtjs/protocol'
|
|
2
|
+
import type {
|
|
3
|
+
EncodeRPCContext,
|
|
4
|
+
ProtocolClientBlobStream,
|
|
5
|
+
ProtocolServerBlobStream,
|
|
6
|
+
} from '@nmtjs/protocol/client'
|
|
7
|
+
import { decode, encode } from '@msgpack/msgpack'
|
|
8
|
+
import { ProtocolBlob } from '@nmtjs/protocol'
|
|
9
|
+
import { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
10
|
+
|
|
11
|
+
import { decodeStreamExt, encodeStreamExt, extensionCodec } from './common.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* MessagePack encoding format with support for Neemata streams.
|
|
15
|
+
* Uses extension types to embed stream ID + metadata directly,
|
|
16
|
+
* eliminating the need for separate stream metadata chunks.
|
|
17
|
+
*/
|
|
18
|
+
export class MsgpackFormat extends BaseClientFormat {
|
|
19
|
+
contentType = 'application/msgpack'
|
|
20
|
+
|
|
21
|
+
encode(data: any): Uint8Array {
|
|
22
|
+
return encode(data, { ignoreUndefined: true })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
encodeRPC(
|
|
26
|
+
data: unknown,
|
|
27
|
+
context: EncodeRPCContext<ProtocolClientBlobStream>,
|
|
28
|
+
) {
|
|
29
|
+
if (typeof data === 'undefined') {
|
|
30
|
+
return new Uint8Array(0)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return encode(data, {
|
|
34
|
+
extensionCodec,
|
|
35
|
+
ignoreUndefined: true,
|
|
36
|
+
context: {
|
|
37
|
+
encodeStream: (object: unknown): Uint8Array | null => {
|
|
38
|
+
if (object instanceof ProtocolBlob) {
|
|
39
|
+
const stream = context.addStream(object)
|
|
40
|
+
return encodeStreamExt(stream.id, stream.metadata)
|
|
41
|
+
}
|
|
42
|
+
return null
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
decode(data: ArrayBufferView): any {
|
|
49
|
+
return decode(data)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
decodeRPC(
|
|
53
|
+
buffer: ArrayBufferView,
|
|
54
|
+
context: DecodeRPCContext<
|
|
55
|
+
(options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
|
|
56
|
+
>,
|
|
57
|
+
) {
|
|
58
|
+
if (buffer.byteLength === 0) {
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return decode(buffer, {
|
|
63
|
+
extensionCodec,
|
|
64
|
+
context: {
|
|
65
|
+
decodeStream: (data: Uint8Array) => {
|
|
66
|
+
const { id, metadata } = decodeStreamExt(data)
|
|
67
|
+
return context.addStream(id, metadata)
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ProtocolBlobMetadata } from '@nmtjs/protocol'
|
|
2
|
+
import { decode, ExtensionCodec, encode } from '@msgpack/msgpack'
|
|
3
|
+
|
|
4
|
+
// Extension type code for blob streams
|
|
5
|
+
export const STREAM_EXT_TYPE = 0x01
|
|
6
|
+
|
|
7
|
+
// Encodes stream ID + metadata into extension type payload
|
|
8
|
+
export const encodeStreamExt = (
|
|
9
|
+
id: number,
|
|
10
|
+
metadata: ProtocolBlobMetadata,
|
|
11
|
+
): Uint8Array => {
|
|
12
|
+
// Format: [id: u32 BE][metadata: msgpack]
|
|
13
|
+
const metadataBuffer = encode(metadata)
|
|
14
|
+
const buffer = new Uint8Array(4 + metadataBuffer.byteLength)
|
|
15
|
+
const view = new DataView(buffer.buffer)
|
|
16
|
+
view.setUint32(0, id, false) // big-endian
|
|
17
|
+
buffer.set(metadataBuffer, 4)
|
|
18
|
+
return buffer
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Decodes stream ID + metadata from extension type payload
|
|
22
|
+
export const decodeStreamExt = (
|
|
23
|
+
data: Uint8Array,
|
|
24
|
+
): { id: number; metadata: ProtocolBlobMetadata } => {
|
|
25
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength)
|
|
26
|
+
const id = view.getUint32(0, false)
|
|
27
|
+
const metadata = decode(data.subarray(4)) as ProtocolBlobMetadata
|
|
28
|
+
return { id, metadata }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Context type for encode/decode operations
|
|
32
|
+
export type MsgpackContext = {
|
|
33
|
+
encodeStream?: (object: unknown) => Uint8Array | null
|
|
34
|
+
decodeStream?: (data: Uint8Array) => unknown
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Shared extension codec - reused across all encode/decode operations
|
|
38
|
+
// Uses msgpack's context feature to pass dynamic encode/decode handlers
|
|
39
|
+
export const extensionCodec = new ExtensionCodec<MsgpackContext>()
|
|
40
|
+
|
|
41
|
+
extensionCodec.register({
|
|
42
|
+
type: STREAM_EXT_TYPE,
|
|
43
|
+
encode: (object: unknown, context: MsgpackContext): Uint8Array | null => {
|
|
44
|
+
return context.encodeStream!(object) ?? null
|
|
45
|
+
},
|
|
46
|
+
decode: (data: Uint8Array, _extType: number, context: MsgpackContext) => {
|
|
47
|
+
return context.decodeStream!(data)
|
|
48
|
+
},
|
|
49
|
+
})
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { DecodeRPCContext, EncodeRPCStreams } from '@nmtjs/protocol'
|
|
2
|
+
import type { ProtocolClientStream } from '@nmtjs/protocol/server'
|
|
3
|
+
import { decode, encode } from '@msgpack/msgpack'
|
|
4
|
+
import { ProtocolBlob } from '@nmtjs/protocol'
|
|
5
|
+
import { BaseServerFormat } from '@nmtjs/protocol/server'
|
|
6
|
+
|
|
7
|
+
import { decodeStreamExt, encodeStreamExt, extensionCodec } from './common.ts'
|
|
8
|
+
|
|
9
|
+
// Marker class for stream IDs (used internally for msgpack extension encoding)
|
|
10
|
+
class StreamIdMarker {
|
|
11
|
+
constructor(
|
|
12
|
+
public readonly streamId: number,
|
|
13
|
+
public readonly metadata: {
|
|
14
|
+
type: string
|
|
15
|
+
size?: number
|
|
16
|
+
filename?: string
|
|
17
|
+
},
|
|
18
|
+
) {}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class MsgpackFormat extends BaseServerFormat {
|
|
22
|
+
contentType = 'application/msgpack'
|
|
23
|
+
accept = ['application/msgpack']
|
|
24
|
+
|
|
25
|
+
encode(data: any) {
|
|
26
|
+
return typeof data !== 'undefined'
|
|
27
|
+
? Buffer.from(encode(data, { ignoreUndefined: true }))
|
|
28
|
+
: Buffer.alloc(0)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
encodeBlob(streamId: number, metadata: EncodeRPCStreams[number]) {
|
|
32
|
+
return new StreamIdMarker(streamId, metadata)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
encodeRPC(data: unknown, _streams: EncodeRPCStreams) {
|
|
36
|
+
if (typeof data === 'undefined') {
|
|
37
|
+
return Buffer.alloc(0)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return Buffer.from(
|
|
41
|
+
encode(data, {
|
|
42
|
+
extensionCodec,
|
|
43
|
+
ignoreUndefined: true,
|
|
44
|
+
context: {
|
|
45
|
+
encodeStream: (object: unknown): Uint8Array | null => {
|
|
46
|
+
if (object instanceof StreamIdMarker) {
|
|
47
|
+
return encodeStreamExt(object.streamId, object.metadata)
|
|
48
|
+
}
|
|
49
|
+
if (object instanceof ProtocolBlob && object.encode) {
|
|
50
|
+
const marker = object.encode(object.metadata) as StreamIdMarker
|
|
51
|
+
if (marker instanceof StreamIdMarker) {
|
|
52
|
+
return encodeStreamExt(marker.streamId, marker.metadata)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return null
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
decode(data: Buffer) {
|
|
63
|
+
return decode(data)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
decodeRPC(
|
|
67
|
+
buffer: Buffer,
|
|
68
|
+
context: DecodeRPCContext<() => ProtocolClientStream>,
|
|
69
|
+
) {
|
|
70
|
+
if (buffer.byteLength === 0) {
|
|
71
|
+
return undefined
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return decode(buffer, {
|
|
75
|
+
extensionCodec,
|
|
76
|
+
context: {
|
|
77
|
+
decodeStream: (data: Uint8Array) => {
|
|
78
|
+
const { id, metadata } = decodeStreamExt(data)
|
|
79
|
+
return context.addStream(id, metadata)
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
}
|