Skip to content

Commit 4c299bf

Browse files
committed
Better handle unexpected conditions when deserializing
For example, when deserialzing from corrupted storage.
1 parent ff5fc61 commit 4c299bf

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/js/s14e-serializer.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,32 +1097,36 @@ export const serialize = (data, options = {}) => {
10971097
return ratio <= 0.85 ? t : s;
10981098
};
10991099

1100-
export const deserialize = s => {
1101-
if ( s.startsWith(MAGICLZ4PREFIX) ) {
1102-
refCounter = 1;
1103-
readStr = s;
1104-
readEnd = s.length;
1105-
readPtr = MAGICLZ4PREFIX.length;
1106-
const lz4 = _deserialize();
1107-
readRefs.clear();
1108-
readStr = '';
1109-
const lz4Util = new LZ4BlockJS();
1110-
const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size);
1111-
s = textCodec.decode(new Uint8Array(uint8ArrayAfter));
1112-
}
1113-
if ( s.startsWith(MAGICPREFIX) === false ) { return; }
1100+
const deserializeById = (blockid, s) => {
11141101
refCounter = 1;
11151102
readStr = s;
11161103
readEnd = s.length;
1117-
readPtr = MAGICPREFIX.length;
1104+
readPtr = blockid.length;
11181105
const data = _deserialize();
11191106
readRefs.clear();
11201107
readStr = '';
1121-
uint8Input = null;
11221108
if ( readPtr === FAILMARK ) { return; }
11231109
return data;
11241110
};
11251111

1112+
export const deserialize = s => {
1113+
if ( s.startsWith(MAGICLZ4PREFIX) ) {
1114+
const lz4 = deserializeById(MAGICLZ4PREFIX, s);
1115+
if ( lz4 ) {
1116+
const lz4Util = new LZ4BlockJS();
1117+
const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size);
1118+
if ( uint8ArrayAfter ) {
1119+
s = textCodec.decode(new Uint8Array(uint8ArrayAfter));
1120+
}
1121+
}
1122+
}
1123+
const data = s.startsWith(MAGICPREFIX)
1124+
? deserializeById(MAGICPREFIX, s)
1125+
: undefined;
1126+
uint8Input = null;
1127+
return data;
1128+
};
1129+
11261130
export const isSerialized = s =>
11271131
typeof s === 'string' &&
11281132
(s.startsWith(MAGICLZ4PREFIX) || s.startsWith(MAGICPREFIX));

0 commit comments

Comments
 (0)