MCPcopy Create free account
hub / github.com/denoland/std / decodeSlice

Function decodeSlice

msgpack/decode.ts:109–391  ·  view source on GitHub ↗

* Given a uint8array which contains a msgpack object, * return the value of the object as well as how many bytes * were consumed in obtaining this object

(
  uint8: Uint8Array,
  dataView: DataView,
  pointer: { consumed: number },
)

Source from the content-addressed store, hash-verified

107 * were consumed in obtaining this object
108 */
109function decodeSlice(
110 uint8: Uint8Array,
111 dataView: DataView,
112 pointer: { consumed: number },
113): ValueType {
114 if (pointer.consumed >= uint8.length) {
115 throw new EvalError("Messagepack decode reached end of array prematurely");
116 }
117 const type = dataView.getUint8(pointer.consumed);
118 pointer.consumed++;
119
120 if (type <= 0x7f) { // positive fixint - really small positive number
121 return type;
122 }
123
124 if ((type & FIXMAP_MASK) === FIXMAP_BITS) { // fixmap - small map
125 const size = type & ~FIXMAP_MASK;
126 return decodeMap(uint8, dataView, size, pointer);
127 }
128
129 if ((type & FIXARRAY_MASK) === FIXARRAY_BITS) { // fixarray - small array
130 const size = type & ~FIXARRAY_MASK;
131 return decodeArray(uint8, dataView, size, pointer);
132 }
133
134 if ((type & FIXSTR_MASK) === FIXSTR_BITS) { // fixstr - small string
135 const size = type & ~FIXSTR_MASK;
136 return decodeString(uint8, size, pointer);
137 }
138
139 if (type >= 0xe0) { // negative fixint - really small negative number
140 return type - 256;
141 }
142
143 switch (type) {
144 case 0xc0: // nil
145 return null;
146 case 0xc1: // (never used)
147 throw new Error(
148 "Messagepack decode encountered a type that is never used",
149 );
150 case 0xc2: // false
151 return false;
152 case 0xc3: // true
153 return true;
154 case 0xc4: { // bin 8 - small Uint8Array
155 if (pointer.consumed >= uint8.length) {
156 throw new EvalError(
157 "Messagepack decode reached end of array prematurely",
158 );
159 }
160 const length = dataView.getUint8(pointer.consumed);
161 pointer.consumed++;
162 const u8 = uint8.subarray(pointer.consumed, pointer.consumed + length);
163 if (u8.length !== length) {
164 throw new EvalError(
165 "Messagepack decode reached end of array prematurely",
166 );

Callers 3

decodeFunction · 0.85
decodeArrayFunction · 0.85
decodeMapFunction · 0.85

Calls 3

decodeArrayFunction · 0.85
decodeStringFunction · 0.85
decodeMapFunction · 0.70

Tested by

no test coverage detected