forked from bencebalogh/avro-schema-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.js
158 lines (131 loc) · 4.79 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
'use strict';
const { URL } = require('url');
const http = require('http');
const https = require('https');
const avsc = require('avsc');
const SchemaCache = require('./lib/schema-cache');
const {pushSchema, getSchemaById, getLatestVersionForSubject} = require('./lib/http-calls');
function schemas(registryUrl, auth = null) {
const parsed = new URL(registryUrl);
const registry = {
cache: new SchemaCache(),
protocol: parsed.protocol.startsWith('https') ? https : http,
host: parsed.hostname,
port: parsed.port,
path: parsed.path != null ? parsed.path : '/',
username: parsed.username,
password: parsed.password,
};
if(auth != null && (typeof auth === 'object')) {
registry.username = auth.username;
registry.password = auth.password;
}
const encodeFunction = (msg, schemaId, schema) => {
const encodedMessage = schema.toBuffer(msg);
const message = Buffer.alloc(encodedMessage.length + 5);
message.writeUInt8(0);
message.writeUInt32BE(schemaId, 1);
encodedMessage.copy(message, 5);
return message;
};
const getId = (subject, schema, parsedSchema) => {
let schemaId = registry.cache.getBySchema(schema);
if (!schemaId) {
schemaId = pushSchema(registry, subject, schema);
registry.cache.setBySchema(schema, schemaId);
}
return schemaId.then((id) => {
if (schemaId != Promise.resolve(parsedSchema)) {
registry.cache.setById(id, Promise.resolve(parsedSchema));
registry.cache.setBySchema(schema, Promise.resolve(id));
}
return id;
});
};
const getSchema = (id, parseOptions) => {
let schemaPromise = registry.cache.getById(id);
if (!schemaPromise) {
schemaPromise = getSchemaById(registry, id);
registry.cache.setById(schemaPromise);
}
return schemaPromise.then((schema) => {
const parsedSchema = avsc.parse(schema, parseOptions);
if (schemaPromise != Promise.resolve(parsedSchema)) {
registry.cache.setById(id, Promise.resolve(parsedSchema));
registry.cache.setBySchema(schema, Promise.resolve(id));
}
return parsedSchema;
});
};
const getSchemaAndId = (topic, parseOptions) => {
let promise = registry.cache.getByName(topic);
if (!promise) {
promise = getLatestVersionForSubject(registry, topic);
registry.cache.setByName(topic, promise);
}
return promise.then(({schema, id}) => {
const parsedSchema = avsc.parse(schema, parseOptions);
if (promise != Promise.resolve({schema, id})) {
registry.cache.setByName(topic, Promise.resolve({schema, id}));
registry.cache.setById(id, Promise.resolve(parsedSchema));
registry.cache.setBySchema(schema, Promise.resolve(id));
}
return {parsedSchema, id};
});
};
const decode = (msg, parseOptions) => {
if (msg.readUInt8(0) !== 0) {
return Promise.reject(new Error(`Message doesn't contain schema identifier byte.`));
}
const id = msg.readUInt32BE(1);
const buffer = msg.slice(5);
let schemaPromise = registry.cache.getById(id);
if (!schemaPromise) {
schemaPromise = getSchemaById(registry, id);
registry.cache.setById(schemaPromise);
}
return schemaPromise.then((schema) => {
const parsedSchema = avsc.parse(schema, parseOptions);
if (schemaPromise != Promise.resolve(parsedSchema)) {
registry.cache.setById(id, Promise.resolve(parsedSchema));
registry.cache.setBySchema(JSON.stringify(schema), Promise.resolve(id));
}
return parsedSchema.fromBuffer(buffer);
});
};
const encodeKey = (topic, schema, msg, parseOptions = null) => {
try {
const parsedSchema = avsc.parse(schema, parseOptions);
return getId(`${topic}-key`, schema, parsedSchema).then((id) => encodeFunction(msg, id, parsedSchema));
} catch (e) {
return Promise.reject(e);
}
};
const encodeMessage = (topic, schema, msg, parseOptions = null) => {
try {
const parsedSchema = avsc.parse(schema, parseOptions);
return getId(`${topic}-value`, schema, parsedSchema).then((id) => encodeFunction(msg, id, parsedSchema));
} catch (e) {
return Promise.reject(e);
}
};
const encodeById = (id, msg, parseOptions = null) => {
return getSchema(id, parseOptions).then((schema) => encodeFunction(msg, id, schema));
};
const encodeMessageByTopicName = (topic, msg, parseOptions = null) => {
return getSchemaAndId(topic, parseOptions).then(({parsedSchema, id}) => encodeFunction(msg, id, parsedSchema));
};
const getSchemaByTopicName = (topic, parseOptions = null) => {
return getSchemaAndId(topic, parseOptions);
};
return {
decode,
decodeMessage: decode,
encodeById,
encodeKey,
encodeMessage,
encodeMessageByTopicName,
getSchemaByTopicName,
};
}
module.exports = schemas;