Skip to content

Commit c6e4cbf

Browse files
committed
Generalized concept of AppendableBuffer
1 parent 127422c commit c6e4cbf

File tree

207 files changed

+3857
-3708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+3857
-3708
lines changed

compiled/download.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload-download.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/io.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export declare type ValueCallback<E> = (err: Error | null, value: E | null) => v
4040
export declare type TypeAndValueCallback<E> = (err: Error | null, type: Type<E> | null, value: E | null) => void;
4141
/**
4242
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]])
43-
* followed by a null byte to a writable stream.
43+
* to a writable stream and then closes the stream.
4444
* Calls `callback` when done.
4545
*
4646
* Example:
@@ -65,7 +65,7 @@ export declare type TypeAndValueCallback<E> = (err: Error | null, type: Type<E>
6565
export declare function writeType({type, outStream}: WriteParams<any>, callback?: ErrCallback): Writable;
6666
/**
6767
* Writes the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]])
68-
* followed by a null byte to a writable stream.
68+
* to a writable stream and then closes the stream.
6969
* Calls `callback` when done.
7070
*
7171
* Example:
@@ -91,7 +91,7 @@ export declare function writeValue<E>({type, value, outStream}: WriteTypeValuePa
9191
/**
9292
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]]),
9393
* followed by the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]]),
94-
* and then a null byte to a writable stream.
94+
* to a writable stream and then closes the stream.
9595
* Calls `callback` when done.
9696
*
9797
* Example:

dist/io.js

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ const http = require("http");
66
const stream_1 = require("stream");
77
const zlib = require("zlib");
88
const accepts = require("accepts");
9+
const appendable_stream_1 = require("./lib/appendable-stream");
910
const assert_1 = require("./lib/assert");
10-
const buffer_stream_1 = require("./lib/buffer-stream");
11-
const growable_buffer_1 = require("./lib/growable-buffer");
1211
const r = require("./read");
1312
const abstract_1 = require("./types/abstract");
1413
function toArrayBuffer(buffer) {
1514
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
1615
}
17-
const WRITABLE_STREAMS = [stream_1.Writable, stream_1.Duplex, http.OutgoingMessage];
1816
/**
1917
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]])
20-
* followed by a null byte to a writable stream.
18+
* to a writable stream and then closes the stream.
2119
* Calls `callback` when done.
2220
*
2321
* Example:
@@ -41,30 +39,25 @@ const WRITABLE_STREAMS = [stream_1.Writable, stream_1.Duplex, http.OutgoingMessa
4139
*/
4240
function writeType({ type, outStream }, callback) {
4341
assert_1.default.instanceOf(type, abstract_1.default);
44-
assert_1.default.instanceOf(outStream, WRITABLE_STREAMS);
4542
if (callback === undefined)
4643
callback = () => { };
4744
assert_1.default.instanceOf(callback, Function);
48-
let typeBuffer;
45+
const typeStream = new appendable_stream_1.default(outStream);
46+
outStream.on('error', callback);
4947
try {
50-
typeBuffer = type.toBuffer();
48+
type.addToBuffer(typeStream);
49+
outStream.on('finish', () => callback(null));
5150
}
5251
catch (err) {
5352
callback(err);
54-
return outStream;
5553
}
56-
const typeStream = new buffer_stream_1.default(typeBuffer);
57-
return typeStream.pipe(outStream)
58-
.on('error', function (err) {
59-
this.end();
60-
callback(err);
61-
})
62-
.on('finish', () => callback(null));
54+
typeStream.end();
55+
return outStream;
6356
}
6457
exports.writeType = writeType;
6558
/**
6659
* Writes the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]])
67-
* followed by a null byte to a writable stream.
60+
* to a writable stream and then closes the stream.
6861
* Calls `callback` when done.
6962
*
7063
* Example:
@@ -88,30 +81,26 @@ exports.writeType = writeType;
8881
*/
8982
function writeValue({ type, value, outStream }, callback) {
9083
assert_1.default.instanceOf(type, abstract_1.default);
91-
assert_1.default.instanceOf(outStream, WRITABLE_STREAMS);
9284
if (callback === undefined)
9385
callback = () => { };
9486
assert_1.default.instanceOf(callback, Function);
95-
const valueBuffer = new growable_buffer_1.default;
87+
const valueStream = new appendable_stream_1.default(outStream);
88+
outStream.on('error', callback);
9689
try {
97-
type.writeValue(valueBuffer, value);
90+
type.writeValue(valueStream, value);
91+
outStream.on('finish', () => callback(null));
9892
}
9993
catch (err) {
10094
callback(err);
101-
return outStream;
10295
}
103-
return new buffer_stream_1.default(valueBuffer).pipe(outStream)
104-
.on('error', function (err) {
105-
this.end();
106-
callback(err);
107-
})
108-
.on('finish', () => callback(null));
96+
valueStream.end();
97+
return outStream;
10998
}
11099
exports.writeValue = writeValue;
111100
/**
112101
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]]),
113102
* followed by the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]]),
114-
* and then a null byte to a writable stream.
103+
* to a writable stream and then closes the stream.
115104
* Calls `callback` when done.
116105
*
117106
* Example:
@@ -135,26 +124,20 @@ exports.writeValue = writeValue;
135124
*/
136125
function writeTypeAndValue({ type, value, outStream }, callback) {
137126
assert_1.default.instanceOf(type, abstract_1.default);
138-
assert_1.default.instanceOf(outStream, WRITABLE_STREAMS);
139127
if (callback === undefined)
140128
callback = () => { };
141129
assert_1.default.instanceOf(callback, Function);
142-
let typeBuffer;
130+
const typeValueStream = new appendable_stream_1.default(outStream);
131+
outStream.on('error', callback);
143132
try {
144-
typeBuffer = type.toBuffer();
133+
type.addToBuffer(typeValueStream);
134+
type.writeValue(typeValueStream, value);
135+
outStream.on('finish', () => callback(null));
145136
}
146137
catch (err) {
147138
callback(err);
148-
return outStream;
149139
}
150-
const typeStream = new buffer_stream_1.default(typeBuffer);
151-
typeStream.pipe(outStream, { end: false })
152-
.on('error', function () {
153-
this.end();
154-
});
155-
typeStream.on('bs-written', () => {
156-
writeValue({ type, value, outStream }, callback);
157-
});
140+
typeValueStream.end();
158141
return outStream;
159142
}
160143
exports.writeTypeAndValue = writeTypeAndValue;

dist/lib/appendable-stream.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// <reference types="node" />
2+
import { Writable } from 'stream';
3+
import AppendableBuffer from './appendable';
4+
/**
5+
* A wrapper around a writable stream
6+
* to implement [[AppendableBuffer]].
7+
* The stream must be explicitly closed
8+
* by calling [[AppendableStream.end]]
9+
* after all bytes have been written.
10+
*/
11+
export default class AppendableStream implements AppendableBuffer {
12+
private readonly outStream;
13+
private writtenBytes;
14+
/**
15+
* @param outStream The underlying writable stream
16+
*/
17+
constructor(outStream: Writable);
18+
/**
19+
* Appends a byte to the end
20+
* of the written data
21+
* @param value The unsigned byte value to add
22+
*/
23+
add(value: number): this;
24+
/**
25+
* Appends a contiguous set of bytes
26+
* to the end of the written data
27+
* @param buffer The bytes to add
28+
*/
29+
addAll(buffer: ArrayBuffer): this;
30+
/**
31+
* Closes the underlying stream
32+
*/
33+
end(): void;
34+
/**
35+
* The number of bytes that have been written
36+
*/
37+
readonly length: number;
38+
}

dist/lib/appendable-stream.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const http_1 = require("http");
4+
const stream_1 = require("stream");
5+
const assert_1 = require("./assert");
6+
const WRITABLE_STREAMS = [stream_1.Writable, stream_1.Duplex, http_1.OutgoingMessage];
7+
/**
8+
* A wrapper around a writable stream
9+
* to implement [[AppendableBuffer]].
10+
* The stream must be explicitly closed
11+
* by calling [[AppendableStream.end]]
12+
* after all bytes have been written.
13+
*/
14+
class AppendableStream {
15+
/**
16+
* @param outStream The underlying writable stream
17+
*/
18+
constructor(outStream) {
19+
assert_1.default.instanceOf(outStream, WRITABLE_STREAMS);
20+
this.outStream = outStream;
21+
this.writtenBytes = 0;
22+
}
23+
/**
24+
* Appends a byte to the end
25+
* of the written data
26+
* @param value The unsigned byte value to add
27+
*/
28+
add(value) {
29+
assert_1.default.integer(value);
30+
assert_1.default.between(0, value, 0x100, 'Not a byte: ' + String(value));
31+
this.outStream.write(Buffer.from([value]));
32+
this.writtenBytes++;
33+
return this;
34+
}
35+
/**
36+
* Appends a contiguous set of bytes
37+
* to the end of the written data
38+
* @param buffer The bytes to add
39+
*/
40+
addAll(buffer) {
41+
assert_1.default.instanceOf(buffer, ArrayBuffer);
42+
this.outStream.write(Buffer.from(buffer));
43+
this.writtenBytes += buffer.byteLength;
44+
return this;
45+
}
46+
/**
47+
* Closes the underlying stream
48+
*/
49+
end() {
50+
this.outStream.end();
51+
}
52+
/**
53+
* The number of bytes that have been written
54+
*/
55+
get length() {
56+
return this.writtenBytes;
57+
}
58+
}
59+
exports.default = AppendableStream;

dist/lib/appendable.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A "writable" interface, sufficient
3+
* to be able to write type and value bytes.
4+
* Implemented by [[GrowableBuffer]], as well as
5+
* [[AppendableStream]] (a wrapper around a writable stream).
6+
*/
7+
export default interface AppendableBuffer {
8+
/**
9+
* The number of bytes that have been written
10+
*/
11+
readonly length: number;
12+
/**
13+
* Adds a byte after the end
14+
* of the written data
15+
* @param value The unsigned byte value to add
16+
*/
17+
add(value: number): this;
18+
/**
19+
* Adds a contiguous set of bytes
20+
* after the end of the written data
21+
* @param buffer The bytes to add.
22+
* The byte at position `i` in `buffer` will be written to
23+
* position `this.length + i`.
24+
*/
25+
addAll(buffer: ArrayBuffer): this;
26+
}

dist/lib/appendable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

dist/lib/bit-math.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Efficiently computes `Math.floor(n / 8)`
2+
* Efficiently computes `Math.floor(n / 8)`,
3+
* for positive `n`
34
* @param n The number in question
45
*/
56
export declare function dividedByEight(n: number): number;
@@ -8,3 +9,8 @@ export declare function dividedByEight(n: number): number;
89
* @param n The number in question
910
*/
1011
export declare function modEight(n: number): number;
12+
/**
13+
* Efficiently computes `n * 8`
14+
* @param n The number in question
15+
*/
16+
export declare function timesEight(n: number): number;

0 commit comments

Comments
 (0)