codekingpro/portable-devtools
114k
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ByteStream = 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}
21class ByteStream {
22 static BLOCK_SIZE = 1024;
23 buf;
24 view;
25 start = 0;
26 constructor(buffer) {
27 if (buffer) {
28 this.buf = buffer;
29 this.view = Buffer.from(buffer);
30 }
31 else {
32 this.buf = Buffer.alloc(0);
33 this.view = Buffer.from(this.buf);
34 }
35 }
36 get buffer() {
37 return this.view.subarray(0, this.start);
38 }
39 get length() {
40 return this.view.byteLength;
41 }
42 get position() {
43 return this.start;
44 }
45 seek(position) {
46 this.start = position;
47 }
48 // Returns a Buffer containing the specified number of bytes starting at the
49 // given start position.
50 slice(start, len) {
51 const end = start + len;
52 if (end > this.length) {
53 throw new StreamError('request past end of buffer');
54 }
55 return this.view.subarray(start, end);
56 }
57 appendChar(char) {
58 this.ensureCapacity(1);
59 this.view[this.start] = char;
60 this.start += 1;
61 }
62 appendUint16(num) {
63 this.ensureCapacity(2);
64 const value = new Uint16Array([num]);
65 const view = new Uint8Array(value.buffer);
66 this.view[this.start] = view[1];
67 this.view[this.start + 1] = view[0];
68 this.start += 2;
69 }
70 appendUint24(num) {
71 this.ensureCapacity(3);
72 const value = new Uint32Array([num]);
73 const view = new Uint8Array(value.buffer);
74 this.view[this.start] = view[2];
75 this.view[this.start + 1] = view[1];
76 this.view[this.start + 2] = view[0];
77 this.start += 3;
78 }
79 appendView(view) {
80 this.ensureCapacity(view.length);
81 this.view.set(view, this.start);
82 this.start += view.length;
83 }
84 getBlock(size) {
85 if (size <= 0) {
86 return Buffer.alloc(0);
87 }
88 if (this.start + size > this.view.length) {
89 throw new Error('request past end of buffer');
90 }
91 const result = this.view.subarray(this.start, this.start + size);
92 this.start += size;
93 return result;
94 }
95 getUint8() {
96 return this.getBlock(1)[0];
97 }
98 getUint16() {
99 const block = this.getBlock(2);
100 return (block[0] << 8) | block[1];
101 }
102 ensureCapacity(size) {
103 if (this.start + size > this.view.byteLength) {
104 const blockSize = ByteStream.BLOCK_SIZE + (size > ByteStream.BLOCK_SIZE ? size : 0);
105 this.realloc(this.view.byteLength + blockSize);
106 }
107 }
108 realloc(size) {
109 const newArray = Buffer.alloc(size);
110 const newView = Buffer.from(newArray);
111 // Copy the old buffer into the new one
112 newView.set(this.view);
113 this.buf = newArray;
114 this.view = newView;
115 }
116}
117exports.ByteStream = ByteStream;
118 