forked from uber/bufrw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
271 lines (230 loc) · 8.81 KB
/
base.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var assert = require('assert');
var errors = require('./errors');
module.exports.BufferRW = BufferRW;
module.exports.LengthResult = LengthResult;
module.exports.WriteResult = WriteResult;
module.exports.ReadResult = ReadResult;
function BufferRW(byteLength, readFrom, writeInto, isPooled) {
if (!(this instanceof BufferRW)) {
return new BufferRW(byteLength, readFrom, writeInto, isPooled);
}
// istanbul ignore else
if (byteLength && readFrom && writeInto) {
assert(typeof byteLength === 'function', 'expected byteLength to be function');
assert(typeof readFrom === 'function', 'expected readFrom to be function');
assert(typeof writeInto === 'function', 'expected writeInto to be function');
// istanbul ignore else
if (isPooled) {
this.poolByteLength = byteLength;
this.poolReadFrom = readFrom;
this.poolWriteInto = writeInto;
} else {
this.byteLength = byteLength;
this.readFrom = readFrom;
this.writeInto = writeInto;
}
} else {
// Args weren't specified. Expect either pool methods or regular
// methods to be overriden.
assert(
this.poolReadFrom !== BufferRW.prototype.poolReadFrom ||
this.readFrom !== BufferRW.prototype.readFrom,
'expected either poolReadFrom or readFrom to be overriden'
);
assert(
this.poolWriteInto !== BufferRW.prototype.poolWriteInto ||
this.writeInto !== BufferRW.prototype.writeInto,
'expected either poolWriteInto or writeInto to be overriden'
);
assert(
this.poolByteLength !== BufferRW.prototype.poolByteLength ||
this.byteLength !== BufferRW.prototype.byteLength,
'expected either poolByteLength or byteLength to be overriden'
);
}
}
BufferRW.prototype.readFrom = function readFrom(arg1, arg2, arg3) {
assert(this.poolReadFrom !== BufferRW.prototype.poolReadFrom, 'poolReadFrom is overridden');
var readResult = new ReadResult();
this.poolReadFrom(readResult, arg1, arg2, arg3);
return readResult;
};
BufferRW.prototype.writeInto = function writeInto(value, buffer, offset) {
assert(this.poolWriteInto !== BufferRW.prototype.poolWriteInto, 'poolWriteInto is overridden');
var writeResult = new WriteResult();
this.poolWriteInto(writeResult, value, buffer, offset);
return writeResult;
};
BufferRW.prototype.byteLength = function byteLength(arg1, arg2, arg3) {
assert(this.poolbyteLength !== BufferRW.prototype.poolByteLength, 'poolByteLength is overridden');
var lengthResult = new LengthResult();
this.poolByteLength(lengthResult, arg1, arg2, arg3);
return lengthResult;
};
// istanbul ignore next
BufferRW.prototype.poolReadFrom = function poolReadFrom(destResult, arg1, arg2, arg3) {
var res = this.readFrom(arg1, arg2, arg3);
return destResult.copyFrom(res);
};
// istanbul ignore next
BufferRW.prototype.poolWriteInto = function poolWriteInto(destResult, value, buffer, offset) {
var res = this.writeInto(value, buffer, offset);
return destResult.copyFrom(res);
};
// istanbul ignore next
BufferRW.prototype.poolByteLength = function poolByteLength(destResult, arg1, arg2, arg3) {
var res = this.byteLength(arg1, arg2, arg3);
return destResult.copyFrom(res);
};
function LengthResult(err, length) {
this.err = err || null;
this.length = length || 0;
}
LengthResult.prototype.reset = function reset(err, length) {
this.err = err;
this.length = length;
return this;
};
// istanbul ignore next
LengthResult.prototype.copyFrom = function copyFrom(srcRes) {
this.err = srcRes.err;
this.length = srcRes.length;
return this;
};
// istanbul ignore next
LengthResult.error = function error(err, length) {
return new LengthResult(err, length);
};
// istanbul ignore next
LengthResult.just = function just(length) {
return new LengthResult(null, length);
};
function WriteResult(err, offset) {
this.err = err || null;
this.offset = offset || 0;
}
WriteResult.prototype.reset = function reset(err, offset) {
this.err = err;
this.offset = offset;
return this;
};
// istanbul ignore next
WriteResult.prototype.copyFrom = function copyFrom(srcResult) {
this.err = srcResult.err;
this.offset = srcResult.offset;
};
// istanbul ignore next
WriteResult.error = function error(err, offset) {
return new WriteResult(err, offset);
};
// istanbul ignore next
/*jshint maxparams:6*/
WriteResult.poolRangedError = function poolRangedError(destResult, err, start, end, value) {
assert(typeof destResult === 'object' && destResult.constructor.name === 'WriteResult');
err.offest = start;
err.endOffset = end;
return destResult.reset(err, start, value);
};
// istanbul ignore next
WriteResult.rangedError = function rangedError(err, start, end, value) {
return WriteResult.poolRangedError(new WriteResult(), start, end, value);
};
// istanbul ignore next
WriteResult.just = function just(offset) {
return new WriteResult(null, offset);
};
// istanbul ignore next
WriteResult.shortError = function shortError(expected, actual, offset) {
return WriteResult.poolShortError(new WriteResult(), expected, actual, offset);
};
// istanbul ignore next
WriteResult.poolShortError = function poolShortError(destResult, expected, actual, offset) {
assert(typeof destResult === 'object' && destResult.constructor.name === 'WriteResult');
return destResult.reset(new errors.ShortBuffer({
expected: expected,
actual: actual,
offset: offset
}), offset);
};
function ReadResult(err, offset, value) {
this.err = err || null;
this.offset = offset || 0;
// istanbul ignore next
this.value = value === undefined ? null : value;
}
// istanbul ignore next
ReadResult.prototype.copyFrom = function copyFrom(srcResult) {
this.err = srcResult.err;
this.offset = srcResult.offset;
this.value = srcResult.value;
return this;
};
// istanbul ignore next
ReadResult.prototype.reset = function reset(err, offset, value) {
this.err = err;
this.offset = offset;
this.value = value;
return this;
};
// istanbul ignore next
ReadResult.error = function error(err, offset, value) {
return new ReadResult(err, offset, value);
};
// istanbul ignore next
ReadResult.poolRangedError = function poolRangedError(destResult, err, start, end, value) {
assert(typeof destResult === 'object' && destResult.constructor.name === 'ReadResult');
err.offest = start;
err.endOffset = end;
return destResult.reset(err, start, value);
};
// istanbul ignore next
ReadResult.rangedError = function rangedError(err, start, end, value) {
return ReadResult.poolRangedError(new ReadResult(), err, start, end, value);
};
// istanbul ignore next
ReadResult.just = function just(offset, value) {
return new ReadResult(null, offset, value);
};
// istanbul ignore next
ReadResult.shortError = function shortError(destResult, expected, actual, offset, endOffset) {
return ReadResult.poolShortError(new ReadResult(), expected, actual, offset, endOffset);
};
ReadResult.poolShortError = function poolShortError(destResult, expected, actual, offset, endOffset) {
assert(typeof destResult === 'object' && destResult.constructor.name === 'ReadResult');
var err;
if (endOffset === undefined) {
err = new errors.ShortBuffer({
expected: expected,
actual: actual,
offset: offset
});
} else {
err = new errors.ShortBufferRanged({
expected: expected,
actual: actual,
offset: offset,
endOffset: endOffset
});
}
return destResult.reset(err, offset);
};