This repository was archived by the owner on Aug 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinit.luau
More file actions
252 lines (224 loc) · 8.56 KB
/
Copy pathinit.luau
File metadata and controls
252 lines (224 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
--!strict
--!native
--[=[
snack
Packs any supported Luau value — nil, boolean, number, string, vector,
buffer, or table — into the smallest practical binary form (a `buffer`),
and bites it back out again.
local snack = require(path.to.snack)
local bite = snack.bite
local greeting: snack.String = snack("hello")
print(bite(greeting)) --> "hello"
A `Snack<T>` is a plain `buffer` at runtime. The table type it is declared
as is a phantom type: it never exists at runtime and only carries `T` so
that `bite` can hand the original type back at compile time.
This module holds the public API and its exported types; the wire format
lives in Tags, encoding in Encoder + Writer, and decoding/walking in
Decoder.
]=]
-- `@self` = this module's own directory: init.luau represents the `src`
-- directory itself, so a plain `./` here would resolve to siblings of src/.
local Decoder = require("@self/Decoder")
local Digest = require("@self/Digest")
local Encoder = require("@self/Encoder")
local decodeValue = Decoder.decodeValue
local skipValue = Decoder.skipValue
export type Snack<T> = {
--[[ Phantom field. It never exists at runtime — a snack is really a
`buffer` — it only carries T so bite/wrap can recover the type. ]]
__snack: T,
}
export type Any = Snack<any>
export type Nil = Snack<nil>
export type Boolean = Snack<boolean>
export type Number = Snack<number>
export type String = Snack<string>
export type Table<T = any> = Snack<T>
export type Vector = Snack<typeof(vector.create(0, 0, 0))>
export type Buffer = Snack<buffer>
export type Pack = {
--[[ Phantom field, like Snack.__snack: a pack is really a `buffer`
holding zero or more snacks laid end to end. The distinct field keeps
Pack and Snack<T> from being assignable to each other, so a
multi-entry pack cannot accidentally be passed to `bite`. ]]
__pack: true,
}
export type DigestAlgorithm = "hex" | "base64" | "base64-urlsafe"
--[[ What `undigest` returns. Digesting is pure transcoding, so the decoded
buffer is byte-identical to what went in — which means it is whatever it
was before: a snack if a snack was digested, a pack if a pack was. The
intersection lets both readings typecheck: pass it to `bite`, or to the
pack functions, to match what you put in. ]]
export type Undigested = Snack<any> & Pack
--- Unpacks a snack back into the value that was passed to `snack(...)`.
--- Errors if given anything other than a buffer produced by snack (or a
--- byte-identical copy of one).
local function bite<T>(s: Snack<T>): T
local bytes = s :: any
if type(bytes) ~= "buffer" then
error(`snack.bite: expected a snack, got a value of type "{typeof(s)}"`, 2)
end
local value, position = decodeValue(bytes, 0)
if position ~= buffer.len(bytes) then
error("snack: malformed snack (trailing bytes)", 0)
end
return value :: T
end
--- Number of bytes the snack occupies.
local function size(s: Snack<any>): number
local bytes = s :: any
if type(bytes) ~= "buffer" then
error(`snack.size: expected a snack, got a value of type "{typeof(s)}"`, 2)
end
return buffer.len(bytes)
end
--- The snack's underlying `buffer` (the same object, not a copy). Use this
--- when handing a snack to an API typed against `buffer`, such as a
--- DataStore or a RemoteEvent.
local function raw(s: Snack<any>): buffer
local bytes = s :: any
if type(bytes) ~= "buffer" then
error(`snack.raw: expected a snack, got a value of type "{typeof(s)}"`, 2)
end
return bytes
end
--- Re-brands a `buffer` (for example one loaded back from a DataStore) as a
--- Snack<T> without copying or validating it. Annotate the result with the
--- type you expect: `local s: snack.String = snack.wrap(bytes)`.
local function wrap<T>(bytes: buffer): Snack<T>
if type(bytes) ~= "buffer" then
error(`snack.wrap: expected a buffer, got a value of type "{typeof(bytes)}"`, 2)
end
return bytes :: any
end
--- True if `value` is a buffer containing one complete, well-formed snack.
local function is(value: any): boolean
if type(value) ~= "buffer" then
return false
end
return (pcall(bite, value :: any))
end
--- Concatenates any number of snacks and/or packs into a single Pack with
--- zero framing overhead: the result is exactly the pieces' bytes laid end
--- to end (the format is self-delimiting, so no separators are needed).
--- Nothing is re-encoded — this is pure `buffer.copy` — which also makes it
--- the append and merge operation: `pack(p, s)` appends, `pack(p1, p2)`
--- merges, and `pack()` is the empty pack.
local function pack(...: Snack<any> | Pack): Pack
local pieceCount = select("#", ...)
local total = 0
for index = 1, pieceCount do
local piece = select(index, ...) :: any
if type(piece) ~= "buffer" then
error(`snack.pack: expected a snack or pack at argument #{index}, got a value of type "{typeof(piece)}"`, 2)
end
total += buffer.len(piece)
end
local combined = buffer.create(total)
local offset = 0
for index = 1, pieceCount do
local piece = (select(index, ...) :: any) :: buffer
buffer.copy(combined, offset, piece, 0, buffer.len(piece))
offset += buffer.len(piece)
end
return combined :: any
end
--- Decodes every entry of a pack. Returns the values as an array plus the
--- entry count — the count matters because entries can legitimately be nil,
--- which would make `#` unreliable on the array alone.
local function unpackEntries(p: Pack): ({ any }, number)
local bytes = p :: any
if type(bytes) ~= "buffer" then
error(`snack.unpack: expected a pack, got a value of type "{typeof(p)}"`, 2)
end
local length = buffer.len(bytes)
local values = {}
local entryCount = 0
local position = 0
while position < length do
local value, nextPosition = decodeValue(bytes, position)
entryCount += 1
values[entryCount] = value
position = nextPosition
end
return values, entryCount
end
--- Number of entries in a pack, found by walking entry boundaries without
--- decoding anything. Errors on malformed bytes, so `pcall(snack.count, p)`
--- doubles as pack validation.
local function count(p: Pack): number
local bytes = p :: any
if type(bytes) ~= "buffer" then
error(`snack.count: expected a pack, got a value of type "{typeof(p)}"`, 2)
end
local length = buffer.len(bytes)
local entries = 0
local position = 0
while position < length do
position = skipValue(bytes, position)
entries += 1
end
return entries
end
--- Decodes only the pack entry at `index` (1-based), skipping over the
--- others without decoding them. Errors if the index is past the last entry.
local function nibble(p: Pack, index: number): any
local bytes = p :: any
if type(bytes) ~= "buffer" then
error(`snack.nibble: expected a pack, got a value of type "{typeof(p)}"`, 2)
end
if type(index) ~= "number" or index % 1 ~= 0 or index < 1 then
error(`snack.nibble: index must be a positive integer, got {tostring(index)}`, 2)
end
local length = buffer.len(bytes)
local entry = 0
local position = 0
while position < length do
entry += 1
if entry == index then
return (decodeValue(bytes, position))
end
position = skipValue(bytes, position)
end
error(`snack.nibble: index {index} is out of range for a pack of {entry} entries`, 2)
end
--- Transcodes a snack or pack into a printable string for transports that
--- cannot carry binary (HTTP bodies, headers, URLs). `algorithm` is "hex"
--- (the default), "base64", or "base64-urlsafe".
local function digest(value: Snack<any> | Pack, algorithm: DigestAlgorithm?): string
local bytes = value :: any
if type(bytes) ~= "buffer" then
error(`snack.digest: expected a snack or pack, got a value of type "{typeof(value)}"`, 2)
end
return Digest.encode(bytes, algorithm or "hex")
end
--- Reverses `digest`, restoring the exact bytes that were digested — a
--- digested snack comes back a snack, a digested pack comes back a pack.
--- The algorithm must match the one used to digest (default "hex").
--- Transcoding is validated (bad characters, lengths, and padding error),
--- but the decoded bytes are not: pair with `snack.is` or
--- `pcall(snack.count, ...)` when the text comes from an untrusted peer.
local function undigest(text: string, algorithm: DigestAlgorithm?): Undigested
if type(text) ~= "string" then
error(`snack.undigest: expected a string, got a value of type "{typeof(text)}"`, 2)
end
return Digest.decode(text, algorithm or "hex") :: any
end
local snack = {
bite = bite,
size = size,
raw = raw,
wrap = wrap,
is = is,
pack = pack,
unpack = unpackEntries,
count = count,
nibble = nibble,
digest = digest,
undigest = undigest,
}
return setmetatable(snack, {
__call = function<T>(_module: any, value: T): Snack<T>
return Encoder.encode(value) :: any
end,
})