Skip to content

Commit 6bc9522

Browse files
committed
fix(encoder): Buffer maybe undefined on browsers
1 parent 8f60673 commit 6bc9522

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
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": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen",

src/encoder/Encoder.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import type { Iterator } from "../encoding/decode";
77

88
import { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';
99
import { Root } from "./Root";
10-
import { getNextPowerOf2 } from "../utils";
1110

1211
import type { StateView } from "./StateView";
1312
import type { Metadata } from "../Metadata";
1413

1514
export class Encoder<T extends Schema = any> {
16-
static BUFFER_SIZE = Buffer.poolSize ?? 8 * 1024; // 8KB
15+
static BUFFER_SIZE = (typeof(Buffer) !== "undefined") && Buffer.poolSize || 8 * 1024; // 8KB
1716
sharedBuffer = Buffer.allocUnsafe(Encoder.BUFFER_SIZE);
1817

1918
context: TypeContext;
@@ -119,7 +118,7 @@ export class Encoder<T extends Schema = any> {
119118
// we can assume that n + 1 poolSize will suffice given that we are likely done with encoding at this point
120119
// multiples of poolSize are faster to allocate than arbitrary sizes
121120
// if we are on an older platform that doesn't implement pooling use 8kb as poolSize (that's the default for node)
122-
const newSize = Math.ceil(it.offset / (Buffer.poolSize ?? 8 * 1024)) * (Buffer.poolSize ?? 8 * 1024);
121+
const newSize = Math.ceil(it.offset / (Buffer.poolSize ?? 8 * 1024)) * (Buffer.poolSize ?? 8 * 1024);
123122

124123
console.warn(`@colyseus/schema buffer overflow. Encoded state is higher than default BUFFER_SIZE. Use the following to increase default BUFFER_SIZE:
125124

0 commit comments

Comments
 (0)