Skip to content

Commit da227cf

Browse files
committed
fix encoding exact 255-length strings. closes #216
1 parent 0d80b36 commit da227cf

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "4.0.8",
3+
"version": "4.0.9",
44
"description": "Binary state serializer with delta encoding for games",
55
"type": "module",
66
"bin": {

src/encoding/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function string(bytes: BufferLike, value: string, it: Iterator) {
192192
// str 8
193193
else if (length < 0x100) {
194194
bytes[it.offset++] = 0xd9;
195-
bytes[it.offset++] = length % 255;
195+
bytes[it.offset++] = length;
196196
size = 2;
197197
}
198198
// str 16

test/Schema.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,26 @@ describe("Type: Schema", () => {
299299
assert.strictEqual(decoded.longstring, longstring);
300300
});
301301

302+
it("string with exactly 255 UTF-8 bytes", () => {
303+
class Data extends Schema {
304+
@type("string") str: string;
305+
@type("number") num: number;
306+
}
307+
308+
// Create a string that is exactly 255 UTF-8 bytes
309+
const str255 = "a".repeat(255);
310+
assert.strictEqual(Buffer.byteLength(str255, "utf8"), 255);
311+
312+
let data = new Data();
313+
data.str = str255;
314+
data.num = 42;
315+
316+
const decoded = new Data();
317+
decoded.decode(data.encode());
318+
assert.strictEqual(decoded.str, str255);
319+
assert.strictEqual(decoded.num, 42);
320+
});
321+
302322
it("bigints", () => {
303323
class Data extends Schema {
304324
@type("biguint64") u64: bigint;

0 commit comments

Comments
 (0)