@irtio/protocol 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -257,19 +257,31 @@ declare function deltaPayload(codecBytes: Uint8Array): Uint8Array;
257
257
  declare function writePayload(codecBytes: Uint8Array): Uint8Array;
258
258
  /**
259
259
  * The CORRECT frame payload: the schema-codec delta bytes with the judged client write tick
260
- * (u32) appended (week 8, D19). Additive: the codec's delta decoder never reads past the delta
261
- * body, so a decoder that does not know about the suffix ignores it; a reader that does
262
- * (`decodeDeltaFrom`) is left positioned exactly at it.
260
+ * (u32) appended (week 8, D19), and after it the server tick that write was *applied* at (u32,
261
+ * bug 1). Additive in both steps: the codec's delta decoder never reads past the delta body, so
262
+ * a decoder that does not know about a suffix ignores it; a reader that does (`decodeDeltaFrom`)
263
+ * is left positioned exactly at the first one.
264
+ *
265
+ * The two ticks are on different clocks and that is the point. `clientTick` is the client's own
266
+ * stamp, opaque to the server and echoed verbatim; `appliedTick` is the room's tick counter when
267
+ * the write landed. A predicting client stamps on its *predicted* clock, which leads authority,
268
+ * so without the second number it has no way to learn where on the server's clock its intent
269
+ * actually took effect, and its replay puts it a whole prediction lead too late.
263
270
  */
264
- declare function correctPayload(codecBytes: Uint8Array, clientTick: number): Uint8Array;
271
+ declare function correctPayload(codecBytes: Uint8Array, clientTick: number, appliedTick?: number): Uint8Array;
265
272
  /**
266
273
  * Reads the `clientTick` suffix a reader is positioned at after `decodeDeltaFrom`, or
267
274
  * `undefined` for a pre-week-8 CORRECT payload with no suffix.
268
275
  */
269
276
  declare function readCorrectClientTick(r: ByteReader): number | undefined;
277
+ /**
278
+ * Reads the `appliedTick` suffix that follows `clientTick`, or `undefined` from a server that
279
+ * does not send one. Call only after `readCorrectClientTick`, which leaves the reader on it.
280
+ */
281
+ declare function readCorrectAppliedTick(r: ByteReader): number | undefined;
270
282
  declare function encodeDeltaFrame(codecBytes: Uint8Array): Uint8Array;
271
283
  declare function encodeWriteFrame(codecBytes: Uint8Array): Uint8Array;
272
- declare function encodeCorrectFrame(codecBytes: Uint8Array, clientTick: number): Uint8Array;
284
+ declare function encodeCorrectFrame(codecBytes: Uint8Array, clientTick: number, appliedTick?: number): Uint8Array;
273
285
 
274
286
  interface Call {
275
287
  readonly reqId: number;
@@ -372,4 +384,4 @@ declare const relaySchema: Schema<{
372
384
  declare const RELAY_HASH8: Uint8Array;
373
385
  declare function isRelayHash8(hash8: Uint8Array): boolean;
374
386
 
375
- export { type Call, type ClientCallable, type Credential, ERROR_CATALOGUE, type ErrorCatalogueEntry, ErrorCode, type ErrorCodeDef, type ErrorCodeName, type ErrorPayload, type Frame, FrameType, type Hello, type Msg, type MsgTarget, PRESENCE_COLLECTION, PROTOCOL_VERSION, type Ping, type Pong, type PresenceRecord, RELAY_HASH8, type Reply, type Welcome, builtinRpcs, correctPayload, decodeCall, decodeErrorPayload, decodeFrame, decodeHello, decodeMsg, decodePing, decodePong, decodeReply, decodeWelcome, deltaPayload, encodeCall, encodeCorrectFrame, encodeDeltaFrame, encodeErrorPayload, encodeFrame, encodeHello, encodeMsg, encodePing, encodePong, encodeReply, encodeWelcome, encodeWriteFrame, errorByCode, formatError, isFrameType, isRelayHash8, presenceEntity, readCorrectClientTick, relaySchema, requestOwnership, rpcByIdOf, rpcIdOf, rpcTable, withBuiltins, writePayload };
387
+ export { type Call, type ClientCallable, type Credential, ERROR_CATALOGUE, type ErrorCatalogueEntry, ErrorCode, type ErrorCodeDef, type ErrorCodeName, type ErrorPayload, type Frame, FrameType, type Hello, type Msg, type MsgTarget, PRESENCE_COLLECTION, PROTOCOL_VERSION, type Ping, type Pong, type PresenceRecord, RELAY_HASH8, type Reply, type Welcome, builtinRpcs, correctPayload, decodeCall, decodeErrorPayload, decodeFrame, decodeHello, decodeMsg, decodePing, decodePong, decodeReply, decodeWelcome, deltaPayload, encodeCall, encodeCorrectFrame, encodeDeltaFrame, encodeErrorPayload, encodeFrame, encodeHello, encodeMsg, encodePing, encodePong, encodeReply, encodeWelcome, encodeWriteFrame, errorByCode, formatError, isFrameType, isRelayHash8, presenceEntity, readCorrectAppliedTick, readCorrectClientTick, relaySchema, requestOwnership, rpcByIdOf, rpcIdOf, rpcTable, withBuiltins, writePayload };
package/dist/index.js CHANGED
@@ -222,23 +222,28 @@ function deltaPayload(codecBytes) {
222
222
  function writePayload(codecBytes) {
223
223
  return codecBytes;
224
224
  }
225
- function correctPayload(codecBytes, clientTick) {
226
- const w = new ByteWriter(codecBytes.length + 4);
225
+ function correctPayload(codecBytes, clientTick, appliedTick) {
226
+ const extra = appliedTick === void 0 ? 4 : 8;
227
+ const w = new ByteWriter(codecBytes.length + extra);
227
228
  w.bytes(codecBytes);
228
229
  w.u32(clientTick);
230
+ if (appliedTick !== void 0) w.u32(appliedTick);
229
231
  return w.finish();
230
232
  }
231
233
  function readCorrectClientTick(r) {
232
234
  return r.remaining >= 4 ? r.u32() : void 0;
233
235
  }
236
+ function readCorrectAppliedTick(r) {
237
+ return r.remaining >= 4 ? r.u32() : void 0;
238
+ }
234
239
  function encodeDeltaFrame(codecBytes) {
235
240
  return encodeFrame(FrameType.DELTA, deltaPayload(codecBytes));
236
241
  }
237
242
  function encodeWriteFrame(codecBytes) {
238
243
  return encodeFrame(FrameType.WRITE, writePayload(codecBytes));
239
244
  }
240
- function encodeCorrectFrame(codecBytes, clientTick) {
241
- return encodeFrame(FrameType.CORRECT, correctPayload(codecBytes, clientTick));
245
+ function encodeCorrectFrame(codecBytes, clientTick, appliedTick) {
246
+ return encodeFrame(FrameType.CORRECT, correctPayload(codecBytes, clientTick, appliedTick));
242
247
  }
243
248
 
244
249
  // src/rpc.ts
@@ -436,6 +441,7 @@ export {
436
441
  isFrameType,
437
442
  isRelayHash8,
438
443
  presenceEntity,
444
+ readCorrectAppliedTick,
439
445
  readCorrectClientTick,
440
446
  relaySchema,
441
447
  requestOwnership,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@irtio/protocol",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "irtio wire protocol: frames, framing, session payloads, error codes, built-in presence",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -20,7 +20,7 @@
20
20
  "dist"
21
21
  ],
22
22
  "dependencies": {
23
- "@irtio/schema": "0.2.0"
23
+ "@irtio/schema": "0.3.0"
24
24
  },
25
25
  "scripts": {
26
26
  "build": "tsup",