@axpecter/lync 2.0.0 → 2.1.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.
Files changed (55) hide show
  1. package/README.md +475 -426
  2. package/package.json +1 -1
  3. package/src/Types.luau +63 -31
  4. package/src/api/Group.luau +101 -106
  5. package/src/api/Packet.luau +187 -186
  6. package/src/api/Query.luau +223 -284
  7. package/src/api/Scope.luau +46 -57
  8. package/src/api/Signal.luau +104 -137
  9. package/src/codec/Base.luau +69 -20
  10. package/src/codec/composite/Array.luau +179 -270
  11. package/src/codec/composite/Map.luau +97 -347
  12. package/src/codec/composite/Optional.luau +27 -24
  13. package/src/codec/composite/Shared.luau +96 -65
  14. package/src/codec/composite/Struct.luau +200 -360
  15. package/src/codec/composite/Tagged.luau +62 -187
  16. package/src/codec/composite/Tuple.luau +79 -103
  17. package/src/codec/datatype/Buffer.luau +49 -21
  18. package/src/codec/datatype/CFrame.luau +207 -30
  19. package/src/codec/datatype/Color.luau +21 -11
  20. package/src/codec/datatype/Instance.luau +28 -21
  21. package/src/codec/datatype/IntVector.luau +33 -21
  22. package/src/codec/datatype/NumberRange.luau +19 -10
  23. package/src/codec/datatype/Ray.luau +26 -22
  24. package/src/codec/datatype/Rect.luau +20 -16
  25. package/src/codec/datatype/Region.luau +51 -45
  26. package/src/codec/datatype/Sequence.luau +64 -56
  27. package/src/codec/datatype/String.luau +89 -56
  28. package/src/codec/datatype/UDim.luau +40 -22
  29. package/src/codec/datatype/Vector.luau +181 -17
  30. package/src/codec/meta/Auto.luau +295 -253
  31. package/src/codec/meta/Bitfield.luau +104 -148
  32. package/src/codec/meta/Custom.luau +8 -4
  33. package/src/codec/meta/Enum.luau +29 -57
  34. package/src/codec/meta/Float.luau +64 -0
  35. package/src/codec/meta/Nothing.luau +9 -5
  36. package/src/codec/meta/Unknown.luau +44 -36
  37. package/src/codec/primitive/Bool.luau +16 -26
  38. package/src/codec/primitive/Float16.luau +58 -64
  39. package/src/codec/primitive/Int.luau +68 -0
  40. package/src/codec/primitive/Number.luau +21 -43
  41. package/src/codec/primitive/Varint.luau +67 -46
  42. package/src/index.d.ts +249 -335
  43. package/src/init.luau +319 -207
  44. package/src/internal/Baseline.luau +29 -24
  45. package/src/internal/Channel.luau +333 -278
  46. package/src/internal/Middleware.luau +60 -85
  47. package/src/internal/Pool.luau +19 -30
  48. package/src/internal/Registry.luau +56 -113
  49. package/src/transport/Bridge.luau +64 -43
  50. package/src/transport/Client.luau +59 -170
  51. package/src/transport/Gate.luau +282 -142
  52. package/src/transport/Reader.luau +148 -140
  53. package/src/transport/Server.luau +138 -488
  54. package/src/api/Namespace.luau +0 -256
  55. package/src/codec/meta/Quantized.luau +0 -174
@@ -1,56 +1,74 @@
1
1
  --!strict
2
2
  --!native
3
- -- Shared helpers for composite codecs: delta flags, scratch pool, struct field extraction, bool packing.
3
+ -- Shared helpers for composite codecs: delta flags, scratch pool, bool packing.
4
4
 
5
- local Channel = require(script.Parent.Parent.Parent.internal.Channel)
5
+ local Base = require(script.Parent.Parent.Base)
6
6
  local Types = require(script.Parent.Parent.Parent.Types)
7
7
 
8
- type ChannelState = Types.ChannelState
8
+ -- Constants -----------------------------------------------------------
9
9
 
10
- local alloc = Channel.alloc
10
+ local DELTA_UNCHANGED = 0
11
+ local DELTA_FULL_OFFSET = 1
12
+ local DELTA_MULTI_OFFSET = 2
11
13
 
12
- -- Constants -----------------------------------------------------------
14
+ -- State ---------------------------------------------------------------
15
+
16
+ local _nextDeltaId = 0
17
+ local _scratchDepth = 0
18
+ local _scratchStack: { Types.ChannelState } = {}
19
+
20
+ -- Private -------------------------------------------------------------
13
21
 
22
+ local alloc = Base.alloc
14
23
  local band = bit32.band
15
24
  local bor = bit32.bor
25
+ local bxor = bit32.bxor
16
26
  local lshift = bit32.lshift
17
- local ceil = math.ceil
18
- local min = math.min
27
+ local rshift = bit32.rshift
19
28
 
20
- local FLAG_DELTA = 0
21
- local FLAG_FULL = 1
22
- local FLAG_UNCHANGED = 2
23
-
24
- -- State ---------------------------------------------------------------
25
-
26
- local _nextDeltaId = 1
27
- local _scratchStack = {} :: { ChannelState }
28
- local _scratchDepth = 0
29
+ local readu8 = buffer.readu8
30
+ local readu32 = buffer.readu32
31
+ local writeu8 = buffer.writeu8
29
32
 
30
33
  -- Public --------------------------------------------------------------
31
34
 
32
- local Shared = {
33
- FLAG_DELTA = FLAG_DELTA,
34
- FLAG_FULL = FLAG_FULL,
35
- FLAG_UNCHANGED = FLAG_UNCHANGED,
36
- }
35
+ local Shared = {}
36
+
37
+ Shared.DELTA_UNCHANGED = DELTA_UNCHANGED
38
+ Shared.DELTA_FULL_OFFSET = DELTA_FULL_OFFSET
39
+ Shared.DELTA_MULTI_OFFSET = DELTA_MULTI_OFFSET
37
40
 
38
41
  function Shared.allocDeltaId(): number
39
- local id = _nextDeltaId
40
42
  _nextDeltaId += 1
41
- return id
43
+ return _nextDeltaId
42
44
  end
43
45
 
44
- function Shared.acquireScratch(): ChannelState
46
+ function Shared.acquireScratch(): Types.ChannelState
45
47
  _scratchDepth += 1
46
- local scratch = _scratchStack[_scratchDepth]
47
- if not scratch then
48
- scratch = Channel.create()
49
- _scratchStack[_scratchDepth] = scratch
48
+ local ch = _scratchStack[_scratchDepth]
49
+ if not ch then
50
+ ch = {
51
+ buff = buffer.create(1024),
52
+ cursor = 0,
53
+ size = 1024,
54
+ refs = {},
55
+ refCount = 0,
56
+ lastId = -1,
57
+ countPos = 0,
58
+ itemCount = 0,
59
+ singleMode = false,
60
+ singlePos = 0,
61
+ deltas = {},
62
+ prevDump = nil,
63
+ prevDumpLen = 0,
64
+ currentPacket = nil,
65
+ }
66
+ _scratchStack[_scratchDepth] = ch
67
+ else
68
+ ch.cursor = 0
69
+ ch.refCount = 0
50
70
  end
51
- scratch.cursor = 0
52
- table.clear(scratch.refs)
53
- return scratch
71
+ return ch
54
72
  end
55
73
 
56
74
  function Shared.releaseScratch(): ()
@@ -58,42 +76,49 @@ function Shared.releaseScratch(): ()
58
76
  end
59
77
 
60
78
  function Shared.rangeEqual(a: buffer, offA: number, b: buffer, offB: number, len: number): boolean
61
- local aligned = band(len, 0xFFFFFFFC)
62
- for i = 0, aligned - 4, 4 do
63
- if buffer.readu32(a, offA + i) ~= buffer.readu32(b, offB + i) then
79
+ -- u32 chunks
80
+ local aligned = len - (len % 4)
81
+ local i = 0
82
+ while i < aligned do
83
+ if bxor(readu32(a, offA + i), readu32(b, offB + i)) ~= 0 then
64
84
  return false
65
85
  end
86
+ i += 4
66
87
  end
67
88
 
68
- for i = aligned, len - 1 do
69
- if buffer.readu8(a, offA + i) ~= buffer.readu8(b, offB + i) then
89
+ -- u8 remainder
90
+ while i < len do
91
+ if readu8(a, offA + i) ~= readu8(b, offB + i) then
70
92
  return false
71
93
  end
94
+ i += 1
72
95
  end
73
96
 
74
97
  return true
75
98
  end
76
99
 
77
- function Shared.extractFields(schema: { [string]: any }): ({ string }, { any }, { string })
78
- local dataKeys = {} :: { string }
79
- local dataCodecs = {} :: { any }
80
- local boolKeys = {} :: { string }
100
+ function Shared.extractFields(
101
+ schema: { [string]: Types.InternalCodec<any> }
102
+ ): ({ string }, { Types.InternalCodec<any> }, { string })
103
+ local dataKeys: { string } = {}
104
+ local dataCodecs: { Types.InternalCodec<any> } = {}
105
+ local boolKeys: { string } = {}
81
106
 
82
107
  for key, codec in schema do
83
- if codec._isBool then
108
+ if (codec :: any)._isBool then
84
109
  table.insert(boolKeys, key)
85
110
  else
86
111
  table.insert(dataKeys, key)
87
112
  end
88
113
  end
89
114
 
90
- table.sort(dataKeys)
91
- table.sort(boolKeys)
92
-
93
115
  if #dataKeys == 0 and #boolKeys == 0 then
94
- error("[Lync] Struct requires at least one field")
116
+ error("[Lync] Lync.struct: requires at least one field")
95
117
  end
96
118
 
119
+ table.sort(dataKeys)
120
+ table.sort(boolKeys)
121
+
97
122
  for i = 1, #dataKeys do
98
123
  dataCodecs[i] = schema[dataKeys[i]]
99
124
  end
@@ -105,27 +130,33 @@ function Shared.extractFields(schema: { [string]: any }): ({ string }, { any },
105
130
  return dataKeys, dataCodecs, boolKeys
106
131
  end
107
132
 
108
- function Shared.packBools(ch: ChannelState, boolKeys: { string }, boolCount: number, value: any): ()
109
- local boolByteCount = ceil(boolCount / 8)
110
- alloc(ch, boolByteCount)
133
+ function Shared.packBools(
134
+ ch: Types.ChannelState,
135
+ value: any,
136
+ boolKeys: { string },
137
+ boolCount: number
138
+ ): ()
139
+ local byteCount = (boolCount + 7) // 8
140
+ local cursor = ch.cursor
141
+ if cursor + byteCount > ch.size then
142
+ alloc(ch, byteCount)
143
+ end
111
144
  local b = ch.buff
112
- local c = ch.cursor
113
145
 
114
- for byteIdx = 0, boolByteCount - 1 do
146
+ local idx = 1
147
+ for byteIdx = 0, byteCount - 1 do
115
148
  local packed = 0
116
- local base = byteIdx * 8
117
- local limit = min(8, boolCount - base)
118
-
149
+ local limit = if idx + 8 <= boolCount + 1 then 8 else boolCount - idx + 1
119
150
  for bit = 0, limit - 1 do
120
- if value[boolKeys[base + bit + 1]] then
151
+ if value[boolKeys[idx]] then
121
152
  packed = bor(packed, lshift(1, bit))
122
153
  end
154
+ idx += 1
123
155
  end
124
-
125
- buffer.writeu8(b, c + byteIdx, packed)
156
+ writeu8(b, cursor + byteIdx, packed)
126
157
  end
127
158
 
128
- ch.cursor = c + boolByteCount
159
+ ch.cursor = cursor + byteCount
129
160
  end
130
161
 
131
162
  function Shared.unpackBools(
@@ -135,19 +166,19 @@ function Shared.unpackBools(
135
166
  boolCount: number,
136
167
  result: any
137
168
  ): number
138
- local boolByteCount = ceil(boolCount / 8)
139
-
140
- for byteIdx = 0, boolByteCount - 1 do
141
- local byte = buffer.readu8(src, pos + byteIdx)
142
- local base = byteIdx * 8
143
- local limit = min(8, boolCount - base)
169
+ local byteCount = (boolCount + 7) // 8
170
+ local idx = 1
144
171
 
172
+ for byteIdx = 0, byteCount - 1 do
173
+ local byte = readu8(src, pos + byteIdx)
174
+ local limit = if idx + 8 <= boolCount + 1 then 8 else boolCount - idx + 1
145
175
  for bit = 0, limit - 1 do
146
- result[boolKeys[base + bit + 1]] = band(byte, lshift(1, bit)) ~= 0
176
+ result[boolKeys[idx]] = band(byte, lshift(1, bit)) ~= 0
177
+ idx += 1
147
178
  end
148
179
  end
149
180
 
150
- return boolByteCount
181
+ return byteCount
151
182
  end
152
183
 
153
184
  return table.freeze(Shared)