-
Notifications
You must be signed in to change notification settings - Fork 3
/
Metadata.js
138 lines (106 loc) · 3.83 KB
/
Metadata.js
1
2
3
4
5
6
7
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2017-present, Netifi Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @flow
*/
'use strict';
/* eslint-disable consistent-return, no-bitwise */
import type {Encodable} from 'rsocket-types';
import {UTF8Encoder, BufferEncoder, createBuffer} from 'rsocket-core';
/**
* Version
*/
export const VERSION = 1;
export const VERSION_SIZE = 2;
export const SERVICE_LENGTH_SIZE = 2;
export const METHOD_LENGTH_SIZE = 2;
export const TRACING_LENGTH_SIZE = 2;
export function encodeMetadata(
service: string,
method: string,
tracing: Encodable,
metadata: Encodable,
): Buffer {
const serviceLength = UTF8Encoder.byteLength(service);
const methodLength = UTF8Encoder.byteLength(method);
const metadataLength = BufferEncoder.byteLength(metadata);
// We can't overload the method call directly and the code generator currently only populates
// the first 3 parameters
if (undefined === tracing) {
tracing = createBuffer(0);
}
const tracingLength = BufferEncoder.byteLength(tracing);
const buffer = createBuffer(
VERSION_SIZE +
SERVICE_LENGTH_SIZE +
serviceLength +
METHOD_LENGTH_SIZE +
methodLength +
TRACING_LENGTH_SIZE +
tracingLength +
metadataLength,
);
let offset = buffer.writeUInt16BE(VERSION, 0);
offset = buffer.writeUInt16BE(serviceLength, offset);
offset = UTF8Encoder.encode(service, buffer, offset, offset + serviceLength);
offset = buffer.writeUInt16BE(methodLength, offset);
offset = UTF8Encoder.encode(method, buffer, offset, offset + methodLength);
offset = buffer.writeUInt16BE(tracingLength, offset);
offset = BufferEncoder.encode(
tracing,
buffer,
offset,
offset + tracingLength,
);
BufferEncoder.encode(metadata, buffer, offset, offset + metadataLength);
return buffer;
}
export function getVersion(buffer: Buffer): number {
return buffer.readUInt16BE(0);
}
export function getService(buffer: Buffer): string {
let offset = VERSION_SIZE;
const serviceLength = buffer.readUInt16BE(offset);
offset += SERVICE_LENGTH_SIZE;
return UTF8Encoder.decode(buffer, offset, offset + serviceLength);
}
export function getMethod(buffer: Buffer): string {
let offset = VERSION_SIZE;
const serviceLength = buffer.readUInt16BE(offset);
offset += SERVICE_LENGTH_SIZE + serviceLength;
const methodLength = buffer.readUInt16BE(offset);
offset += METHOD_LENGTH_SIZE;
return UTF8Encoder.decode(buffer, offset, offset + methodLength);
}
export function getTracing(buffer: Buffer): Buffer {
let offset = VERSION_SIZE;
const serviceLength = buffer.readUInt16BE(offset);
offset += SERVICE_LENGTH_SIZE + serviceLength;
const methodLength = buffer.readUInt16BE(offset);
offset += METHOD_LENGTH_SIZE + methodLength;
const tracingLength = buffer.readUInt16BE(offset);
offset += TRACING_LENGTH_SIZE;
return BufferEncoder.decode(buffer, offset, offset + tracingLength);
}
export function getMetadata(buffer: Buffer): Buffer {
let offset = VERSION_SIZE;
const serviceLength = buffer.readUInt16BE(offset);
offset += SERVICE_LENGTH_SIZE + serviceLength;
const methodLength = buffer.readUInt16BE(offset);
offset += METHOD_LENGTH_SIZE + methodLength;
const tracingLength = buffer.readUInt16BE(offset);
offset += TRACING_LENGTH_SIZE + tracingLength;
return BufferEncoder.decode(buffer, offset, buffer.length);
}