Migration step 6 ("token details") adds the three prop buckets to `entity`
and creates `entity_status`, the `(status, source)` set of 5.5. A token's
status rows are rewritten with its row, so a status change and the `token`
frame that reports it commit together.
server/props.nim owns the buckets, their caps (64 keys per bucket, a key of
1..64 code points, a value of at most 8 KB of serialized JSON) and the
property-schema registry: empty at start with a permissive fallback, because
DESIGN 8 says v1 never interprets props; a key the rules module registers is
enforced (kind and bucket). server/text.nim is the one copy of the 6.5 text
rule, shared by names and prop keys.
tokens.nim gains `props` and `statuses` on the record, the sorted-unique
entry helpers, `isLocked`, `parseRadius`/`parseStatusKind`/`parseStatusSource`,
the prop and status columns in insert/replace, and `loadStatuses` joined on
the scene. Every helper builds a fresh sequence: a copied record shares its
seq buffer, so an in-place edit would reach the authoritative list before
the transaction commits (4.2 step 6).
162 lines
7.2 KiB
Nim
162 lines
7.2 KiB
Nim
## Property buckets, their caps and the property-schema registry (DESIGN 6.1 prop payload rules, 8,
|
|
## 9; M3 slice 7, issue #22).
|
|
##
|
|
## A token (and, from slice 8, a character) carries three JSON objects by visibility bucket:
|
|
## `public` (anyone who sees it), `owner` (the token's controller + the GM) and `gm` (the GM only).
|
|
## The egress (views.nim) decides which buckets a recipient gets; nothing here knows about sockets.
|
|
##
|
|
## Caps [FIXED (owner-delegated, 2026-11-15, #22)]: at most 64 keys per bucket, a key is a string of
|
|
## 1..64 code points with no control characters, and a value's serialized JSON is at most 8 KB.
|
|
## 6.1 delegated these numbers (#11) and slice 7 fixes them.
|
|
##
|
|
## The registry (DESIGN 8/9): `key -> PropSchema{kind, bucket}` plus a fallback rule. v1 ships it
|
|
## **empty**, and its fallback is permissive (`pkAny`, any bucket), because DESIGN 8 says the server
|
|
## never interprets props in v1: any key may be written to any bucket, under the caps above. A key
|
|
## the rules module (FFF, post-v1) registers is enforced from then on: a value of the wrong JSON
|
|
## kind, or a write to a bucket the schema does not name, is `invalid`. That is the "the registry
|
|
## validates, the server does not interpret" rule of DESIGN 9 in one place: `checkWrite` is the only
|
|
## gate, so flipping the fallback to strict is a one-line change when FFF lands.
|
|
##
|
|
## Ordering: props are `JsonNode` objects, so key order is insertion order and a reload preserves
|
|
## it. JSON object equality ignores order (std/json), so a re-serialisation can never produce a
|
|
## spurious `token` frame (views.Known compares built frames).
|
|
import std/[json, options, tables]
|
|
import text
|
|
|
|
type
|
|
PropBucket* = enum
|
|
pbPublic = "public"
|
|
pbOwner = "owner"
|
|
pbGm = "gm"
|
|
|
|
PropKind* = enum
|
|
pkAny, pkString, pkNumber, pkBool, pkObject, pkArray
|
|
## The value kinds a schema can demand; `pkAny` accepts any JSON value (the v1 fallback).
|
|
|
|
PropSchema* = object
|
|
kind*: PropKind
|
|
bucket*: Option[PropBucket] ## none = the key may live in any bucket
|
|
|
|
Registry* = object
|
|
## `schemas` is what the rules module registered; `fallback` answers every other key.
|
|
schemas*: Table[string, PropSchema]
|
|
fallback*: PropSchema
|
|
|
|
Props* = array[PropBucket, JsonNode]
|
|
## One JSON object per bucket, never nil. A record's props always have all three buckets even
|
|
## when they are empty, so egress can filter by bucket alone.
|
|
|
|
const
|
|
MaxPropKeys* = 64
|
|
## 6.1, numbers delegated (#11), fixed by slice 7.
|
|
MaxPropKeyLen* = 64
|
|
## 1..64 code points, same rule as a character name.
|
|
MaxPropValueBytes* = 8192
|
|
## 6.1: a value's serialized JSON is at most 8 KB.
|
|
|
|
proc newProps*(): Props =
|
|
for b in PropBucket: result[b] = newJObject()
|
|
|
|
proc normalize*(p: Props): Props =
|
|
## Stored-props invariants: every bucket is an object, never nil.
|
|
result = p
|
|
for b in PropBucket:
|
|
if result[b].isNil or result[b].kind != JObject: result[b] = newJObject()
|
|
|
|
proc parseBucket*(raw: string): Option[PropBucket] =
|
|
for b in PropBucket:
|
|
if $b == raw: return some(b)
|
|
|
|
proc bucketName*(b: PropBucket): string = $b
|
|
|
|
proc validPropKey*(key: string): bool = validText(key, MaxPropKeyLen)
|
|
|
|
proc valueBytes*(value: JsonNode): int =
|
|
## The cap is on the serialized size (6.1), which is the number that actually crosses the wire.
|
|
if value.isNil: 0 else: ($value).len
|
|
|
|
proc defaultRegistry*(): Registry =
|
|
## v1's registry: nothing registered, every key accepted (DESIGN 8: props are not interpreted).
|
|
Registry(fallback: PropSchema(kind: pkAny, bucket: none(PropBucket)))
|
|
|
|
proc schemaFor*(reg: Registry, key: string): PropSchema =
|
|
reg.schemas.getOrDefault(key, reg.fallback)
|
|
|
|
proc kindMatches*(kind: PropKind, value: JsonNode): bool =
|
|
if value.isNil: return false
|
|
case kind
|
|
of pkAny: true
|
|
of pkString: value.kind == JString
|
|
of pkNumber: value.kind in {JInt, JFloat}
|
|
of pkBool: value.kind == JBool
|
|
of pkObject: value.kind == JObject
|
|
of pkArray: value.kind == JArray
|
|
|
|
proc checkWrite*(reg: Registry, bucket: PropBucket, key: string, value: JsonNode): bool =
|
|
## The registry gate for one write. Structural checks live in `parseProps`/`setProp`; this is the
|
|
## per-key schema (kind, bucket). Unregistered keys use the fallback rule.
|
|
let schema = reg.schemaFor(key)
|
|
if schema.bucket.isSome and schema.bucket.get != bucket: return false
|
|
kindMatches(schema.kind, value)
|
|
|
|
proc setProp*(p: var Props, bucket: PropBucket, key: string, value: JsonNode): bool =
|
|
## Sets one value. false = the bucket is at its key cap and the key is new (nothing changed).
|
|
if p[bucket].isNil or p[bucket].kind != JObject: p[bucket] = newJObject()
|
|
if not p[bucket].hasKey(key) and p[bucket].len >= MaxPropKeys: return false
|
|
p[bucket][key] = value
|
|
true
|
|
|
|
proc unsetProp*(p: var Props, bucket: PropBucket, key: string): bool =
|
|
## Removes one value; false = it was not there (unsetting an absent key is still `ok` on the wire).
|
|
if p[bucket].isNil or p[bucket].kind != JObject: return false
|
|
result = p[bucket].hasKey(key)
|
|
if result: p[bucket].delete(key)
|
|
|
|
proc contains*(p: Props, bucket: PropBucket, key: string): bool =
|
|
p[bucket].kind == JObject and p[bucket].hasKey(key)
|
|
|
|
proc propsJson*(p: Props): JsonNode =
|
|
## All three buckets: the shape a GM frame carries (6.1).
|
|
result = newJObject()
|
|
for b in PropBucket: result[$b] = p[b]
|
|
|
|
proc clone*(p: Props): Props =
|
|
## A copy whose buckets are fresh objects: a value node is shared (never mutated), but adding or
|
|
## removing a key cannot reach the original bucket. `main.nim` copies a record before it edits it,
|
|
## and a Nim array of refs does not give that for free.
|
|
for b in PropBucket:
|
|
var bucket = newJObject()
|
|
if not p[b].isNil and p[b].kind == JObject:
|
|
for key, value in p[b]: bucket[key] = value
|
|
result[b] = bucket
|
|
|
|
proc valid*(p: Props): bool =
|
|
## The stored-record invariant: every bucket an object, every key and value within the caps.
|
|
result = true # Nim's default for `bool` is false: set it before the early returns below
|
|
for b in PropBucket:
|
|
if p[b].isNil or p[b].kind != JObject: return false
|
|
if p[b].len > MaxPropKeys: return false
|
|
for key, value in p[b]:
|
|
if not validPropKey(key): return false
|
|
if value.kind == JNull: return false # 6.1: a value is JSON, null is not a value
|
|
if valueBytes(value) > MaxPropValueBytes: return false
|
|
|
|
proc parseProps*(reg: Registry, node: JsonNode): Option[Props] =
|
|
## `props` on `token.create` (6.1): absent/null = all empty; else an object whose keys are the
|
|
## three bucket names and whose values are objects that pass the caps and the registry.
|
|
## `some(...)` = valid, `none` = refused `invalid`.
|
|
if node.isNil or node.kind == JNull: return some(newProps())
|
|
if node.kind != JObject: return none(Props)
|
|
var parsed = newProps()
|
|
for name, bucketNode in node:
|
|
let bucket = parseBucket(name)
|
|
if bucket.isNone: return none(Props)
|
|
if bucketNode.kind != JObject: return none(Props)
|
|
for key, value in bucketNode:
|
|
if not validPropKey(key): return none(Props)
|
|
if value.kind == JNull: return none(Props)
|
|
if valueBytes(value) > MaxPropValueBytes: return none(Props)
|
|
if not reg.checkWrite(bucket.get, key, value): return none(Props)
|
|
if not parsed.setProp(bucket.get, key, value): return none(Props)
|
|
some(parsed)
|