kafka-rest
Version:
NodeJS wrapper for the Kafka REST Proxy
54 lines (47 loc) • 1.77 kB
JavaScript
/**
* Copyright 2015 Confluent 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.
*/
;
var Schema = require('./schema'),
util = require('util');
/**
* AvroSchema represents an Avro schema. Since Avro schemas are just JSON data,
* this currently doesn't do any parsing/validation, it just serializes itself
* for the producer.
*/
var AvroSchema = module.exports = function(schema) {
// Since we don't parse, there's a limited amount of validation we can do
// here.
if (typeof(schema) != "object" && typeof("schema") != "string") {
throw new Error("AvroSchemas can only be strings or objects.");
}
this.schema = schema;
this.serialized = this.schema;
if (typeof(schema) === "object") {
this.serialized = JSON.stringify(this.schema);
}
};
util.inherits(AvroSchema, Schema);
AvroSchema.prototype.toSchemaString = function() {
return this.serialized;
};
AvroSchema.prototype.getContentType = function(client) {
return "application/vnd.kafka.avro.v" + client.config.version + "+json";
};
AvroSchema.getContentType = AvroSchema.prototype.getContentType;
AvroSchema.decodeMessage = function(msg) {
// Avro data is returned in JSON format, nothing to do here.
return msg;
};