blob: 2b0a785ab78978a9a90803151de95004a9358ce0 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import { Buffer } from 'buffer';
export default class MarkerSerializer {
constructor(ioc) {
this.ioc = ioc;
this.ioc.serializers[ioc.DataType.MARKER] = this;
}
deserialize(buffer, fullyQualifiedFormat = true) {
let len = 0;
let cursor = buffer;
try {
if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
throw new Error('buffer is missing');
}
if (buffer.length < (fullyQualifiedFormat ? 3 : 1)) {
throw new Error('buffer is too short for marker');
}
if (fullyQualifiedFormat) {
const type_code = cursor.readUInt8();
len++;
if (type_code !== this.ioc.DataType.MARKER) {
throw new Error('unexpected {type_code}');
}
cursor = cursor.slice(1);
const value_flag = cursor.readUInt8();
len++;
if (value_flag !== 0) {
throw new Error('unexpected {value_flag}');
}
cursor = cursor.slice(1);
}
const value = cursor.readUInt8();
len++;
if (value !== 0) {
throw new Error('unexpected marker value');
}
return { v: 'marker', len };
} catch (err) {
throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
}
}
}