@irtio/runtime 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 irtio contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,129 @@
1
+ import {
2
+ parseHibernationBlob,
3
+ writeHibernationBlob
4
+ } from "./chunk-X5S364FY.js";
5
+
6
+ // src/migrate.ts
7
+ import { PRESENCE_COLLECTION, withBuiltins } from "@irtio/protocol";
8
+ import {
9
+ createState,
10
+ decodeSnapshot,
11
+ encodeSnapshot,
12
+ normalizeRecord,
13
+ schemaFromCanonical
14
+ } from "@irtio/schema";
15
+ function migrateSnapshot(blob, chain, options) {
16
+ if (chain.length === 0) throw new Error("migrateSnapshot: empty migration chain");
17
+ const startedAt = performance.now();
18
+ const first = chain[0];
19
+ const last = chain[chain.length - 1];
20
+ for (let i = 1; i < chain.length; i++) {
21
+ if (chain[i].version <= chain[i - 1].version) {
22
+ throw new Error("migrateSnapshot: chain versions must ascend");
23
+ }
24
+ }
25
+ const parsed = parseHibernationBlob(blob);
26
+ let schema = schemaFromCanonical(first.schemaJson);
27
+ let plain = decodeSnapshot(withBuiltins(schema), parsed.snapshot).state;
28
+ let state = toMigrationState(withBuiltins(schema), plain);
29
+ delete state[PRESENCE_COLLECTION];
30
+ const applied = [];
31
+ for (let i = 1; i < chain.length; i++) {
32
+ const step = chain[i];
33
+ const nextSchema = schemaFromCanonical(step.schemaJson);
34
+ if (step.migration) {
35
+ const helpers = {
36
+ version: step.version,
37
+ fromVersion: chain[i - 1].version,
38
+ roomId: options.roomId,
39
+ log: (...args) => options.log?.("info", [`migration v${step.version}:`, ...args])
40
+ };
41
+ let produced;
42
+ try {
43
+ produced = step.migration.up(state, helpers);
44
+ } catch (err) {
45
+ throw new Error(
46
+ `migration to v${step.version} threw: ${err instanceof Error ? err.stack ?? err.message : String(err)}`
47
+ );
48
+ }
49
+ if (produced !== void 0) state = produced;
50
+ applied.push(step.version);
51
+ }
52
+ schema = nextSchema;
53
+ }
54
+ plain = fromMigrationState(withBuiltins(schema), state, last.version);
55
+ const bytes = writeHibernationBlob(
56
+ { seed: parsed.seed, rngState: parsed.rngState, tick: parsed.tick, mode: parsed.mode },
57
+ encodeSnapshot(withBuiltins(schema), plain, { tick: parsed.tick })
58
+ );
59
+ return {
60
+ bytes,
61
+ fromVersion: first.version,
62
+ toVersion: last.version,
63
+ applied,
64
+ ms: performance.now() - startedAt
65
+ };
66
+ }
67
+ function toMigrationState(schema, plain) {
68
+ const out = {};
69
+ for (const c of schema.collections) {
70
+ const held = plain[c.name];
71
+ if (c.kind === "entity") {
72
+ const coll = held;
73
+ const map = {};
74
+ for (const [id, rec] of coll.records) {
75
+ map[id] = { owner: rec.owner, value: { ...rec.value } };
76
+ }
77
+ out[c.name] = map;
78
+ } else {
79
+ out[c.name] = { ...held };
80
+ }
81
+ }
82
+ return out;
83
+ }
84
+ function fromMigrationState(schema, state, version) {
85
+ const known = new Set(schema.collections.map((c) => c.name));
86
+ for (const name of Object.keys(state)) {
87
+ if (!known.has(name)) {
88
+ throw new Error(
89
+ `migration produced a collection ${JSON.stringify(name)} that v${version}'s schema does not declare`
90
+ );
91
+ }
92
+ }
93
+ const plain = createState(schema);
94
+ for (const c of schema.collections) {
95
+ if (c.name === PRESENCE_COLLECTION) continue;
96
+ const given = state[c.name];
97
+ if (given === void 0) continue;
98
+ if (c.kind === "entity") {
99
+ const coll = plain[c.name];
100
+ for (const [id, rec] of Object.entries(given)) {
101
+ if (typeof rec !== "object" || rec === null) {
102
+ throw new Error(`migration: ${c.name}.${id} must be a { owner, value } record`);
103
+ }
104
+ try {
105
+ coll.add(id, rec.value ?? {}, { owner: typeof rec.owner === "string" ? rec.owner : "" });
106
+ } catch (err) {
107
+ throw new Error(
108
+ `migration: ${c.name}.${id} does not fit v${version}'s schema: ${err instanceof Error ? err.message : String(err)}`
109
+ );
110
+ }
111
+ }
112
+ } else {
113
+ try {
114
+ plain[c.name] = normalizeRecord(c, given);
115
+ } catch (err) {
116
+ throw new Error(
117
+ `migration: ${c.name} does not fit v${version}'s schema: ${err instanceof Error ? err.message : String(err)}`
118
+ );
119
+ }
120
+ }
121
+ }
122
+ return plain;
123
+ }
124
+
125
+ export {
126
+ migrateSnapshot,
127
+ toMigrationState,
128
+ fromMigrationState
129
+ };