• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ByteStream = exports.StreamError = void 0;
4/*
5Copyright 2023 The Sigstore Authors.
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19class StreamError extends Error {
20}
21exports.StreamError = StreamError;
22class ByteStream {
23    constructor(buffer) {
24        this.start = 0;
25        if (buffer) {
26            this.buf = buffer;
27            this.view = Buffer.from(buffer);
28        }
29        else {
30            this.buf = new ArrayBuffer(0);
31            this.view = Buffer.from(this.buf);
32        }
33    }
34    get buffer() {
35        return this.view.subarray(0, this.start);
36    }
37    get length() {
38        return this.view.byteLength;
39    }
40    get position() {
41        return this.start;
42    }
43    seek(position) {
44        this.start = position;
45    }
46    // Returns a Buffer containing the specified number of bytes starting at the
47    // given start position.
48    slice(start, len) {
49        const end = start + len;
50        if (end > this.length) {
51            throw new StreamError('request past end of buffer');
52        }
53        return this.view.subarray(start, end);
54    }
55    appendChar(char) {
56        this.ensureCapacity(1);
57        this.view[this.start] = char;
58        this.start += 1;
59    }
60    appendUint16(num) {
61        this.ensureCapacity(2);
62        const value = new Uint16Array([num]);
63        const view = new Uint8Array(value.buffer);
64        this.view[this.start] = view[1];
65        this.view[this.start + 1] = view[0];
66        this.start += 2;
67    }
68    appendUint24(num) {
69        this.ensureCapacity(3);
70        const value = new Uint32Array([num]);
71        const view = new Uint8Array(value.buffer);
72        this.view[this.start] = view[2];
73        this.view[this.start + 1] = view[1];
74        this.view[this.start + 2] = view[0];
75        this.start += 3;
76    }
77    appendView(view) {
78        this.ensureCapacity(view.length);
79        this.view.set(view, this.start);
80        this.start += view.length;
81    }
82    getBlock(size) {
83        if (size <= 0) {
84            return Buffer.alloc(0);
85        }
86        if (this.start + size > this.view.length) {
87            throw new Error('request past end of buffer');
88        }
89        const result = this.view.subarray(this.start, this.start + size);
90        this.start += size;
91        return result;
92    }
93    getUint8() {
94        return this.getBlock(1)[0];
95    }
96    getUint16() {
97        const block = this.getBlock(2);
98        return (block[0] << 8) | block[1];
99    }
100    ensureCapacity(size) {
101        if (this.start + size > this.view.byteLength) {
102            const blockSize = ByteStream.BLOCK_SIZE + (size > ByteStream.BLOCK_SIZE ? size : 0);
103            this.realloc(this.view.byteLength + blockSize);
104        }
105    }
106    realloc(size) {
107        const newArray = new ArrayBuffer(size);
108        const newView = Buffer.from(newArray);
109        // Copy the old buffer into the new one
110        newView.set(this.view);
111        this.buf = newArray;
112        this.view = newView;
113    }
114}
115exports.ByteStream = ByteStream;
116ByteStream.BLOCK_SIZE = 1024;
117