-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshot_chunk.js
55 lines (46 loc) · 1.86 KB
/
snapshot_chunk.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
/*
* Copyright (c) 2016-2017 Rafał Michalski <[email protected]>
*/
"use strict";
const setPrototypeOf = Object.setPrototypeOf
, isBuffer = Buffer.isBuffer
const { defineConst } = require('../utils/helpers');
/* in node 4 and later Buffer hackishly descends from Uint8Array */
class SnapshotChunk extends Buffer {
/* Creates a new SnapshotChunk instance from a buffer without copying its bytes */
constructor(buffer, logIndex, snapshotByteOffset, snapshotTotalLength, logTerm) {
if (!isBuffer(buffer)) {
throw new TypeError("SnapshotChunk: provided buffer argument is not a buffer");
}
var chunk = setPrototypeOf(new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength), SnapshotChunk.prototype);
chunk.logIndex = logIndex;
chunk.snapshotByteOffset = snapshotByteOffset;
chunk.snapshotTotalLength = snapshotTotalLength;
chunk.logTerm = logTerm;
return chunk;
}
get isFirstChunk() {
return this.snapshotByteOffset === 0;
}
get isLastChunk() {
return this.snapshotByteOffset + this.length === this.snapshotTotalLength;
}
static isSnapshotChunk(chunk) {
return chunk instanceof SnapshotChunk;
}
/* Converts buffer instance to SnapshotChunk (replaces its __proto__) */
static bufferToSnapshotChunk(buffer, logIndex, snapshotByteOffset, snapshotTotalLength, logTerm) {
if (!isBuffer(buffer)) {
throw new TypeError("SnapshotChunk: provided buffer argument is not a buffer");
}
var chunk = setPrototypeOf(buffer, SnapshotChunk.prototype);
chunk.logIndex = logIndex;
chunk.snapshotByteOffset = snapshotByteOffset;
chunk.snapshotTotalLength = snapshotTotalLength;
chunk.logTerm = logTerm;
return chunk;
}
}
defineConst(SnapshotChunk.prototype, 'isSnapshotChunk', true);
defineConst(SnapshotChunk, 'SnapshotChunk', SnapshotChunk);
module.exports = exports = SnapshotChunk;