forked from bencebalogh/avro-schema-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.js
163 lines (142 loc) · 4.26 KB
/
registry.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
const url = require('url');
const http = require('http');
const https = require('https');
const avsc = require('avsc');
function fetchSchema(transport, protocol, host, port, schemaId) {
return new Promise((resolve, reject) => {
const requestOptions = {
host,
port,
path: `/schemas/ids/${schemaId}`
};
transport.get(requestOptions, (res) => {
let data = '';
res.on('data', (d) => {
data += d;
});
res.on('error', (e) => {
reject(e);
});
res.on('end', () => {
if (res.statusCode !== 200) {
const error = JSON.parse(data);
return reject(new Error(`Schema registry error: ${error.error_code} - ${error.message}`));
}
const schema = JSON.parse(data).schema;
resolve(schema);
});
});
});
}
function pushSchema(transport, protocol, host, port, path, topic, schemaString) {
return new Promise((resolve, reject) => {
const body = JSON.stringify({schema: schemaString});
const reqestOptions = {
host: `${host}`,
port: port,
method: 'POST',
path: `${path}subjects/${topic}-value/versions`,
headers: {
'Content-Type': 'application/vnd.schemaregistry.v1+json',
'Content-Length': Buffer.byteLength(body),
}
};
const req = transport.request(reqestOptions, (res) => {
let data = '';
res.on('data', (d) => {
data += d;
});
res.on('error', (e) => {
reject(e);
});
res.on('end', () => {
if (res.statusCode !== 200) {
const error = JSON.parse(data);
return reject(new Error(`Schema registry error: ${error.error_code} - ${error.message}`));
}
const resp = JSON.parse(data);
resolve(resp.id);
});
}
);
req.write(body);
req.end();
}
)
;
}
class SchemaCache {
constructor() {
this.schemasById = new Map();
this.schemasBySchema = new Map();
}
set(schemaId, schema) {
this.schemasById.set(schemaId, schema);
if (!(schema instanceof Promise)) {
this.schemasBySchema.set(JSON.stringify(schema), schemaId);
}
}
getById(schemaId) {
return this.schemasById.get(schemaId);
}
getBySchema(schema) {
return this.schemasBySchema.get(JSON.stringify(schema));
}
}
function schemas(registryUrl) {
const parsed = url.parse(registryUrl);
const schemas = new SchemaCache();
const registry = {
protocol: parsed.protocol,
host: parsed.hostname,
port: parsed.port,
path: parsed.path,
};
const protocol = registry.protocol === 'https:' ? https : http;
const decodeMessage = (msg) => {
let schemaId;
return new Promise((resolve, reject) => {
if (msg.readUInt8(0) !== 0) {
return reject(new Error(`Message doesn't contain schema identifier byte.`));
}
schemaId = msg.readUInt32BE(1);
let promise = schemas.getById(schemaId);
if (promise) {
return resolve(promise);
}
promise = fetchSchema(protocol, registry.protocol, registry.host, registry.port, schemaId);
schemas.set(schemaId, promise);
promise.then(result => {
schemas.set(schemaId, result);
}).catch(reject);
return resolve(promise);
})
.then((schema) => {
return avsc.parse(schema).fromBuffer(msg.slice(5));
});
};
const encodeMessage = (topic, schema, msg) => (() => new Promise((resolve, reject) => {
const schemaString = JSON.stringify(schema);
let id = schemas.getBySchema(schema);
if (id) {
return resolve(id)
}
const promise = pushSchema(protocol, registry.protocol, registry.host, registry.port, parsed.path, topic, schemaString);
promise.then(id => {
schemas.set(id, schema);
return schema;
}).catch(reject);
return resolve(promise);
}))()
.then((schemaId) => {
const encodedMessage = avsc.parse(schema).toBuffer(msg);
const message = Buffer.alloc(encodedMessage.length + 5);
message.writeUInt8(0);
message.writeUInt32BE(schemaId, 1);
encodedMessage.copy(message, 5);
return message;
});
return {encodeMessage, decodeMessage};
};
module.exports = schemas;