@flighthq/entity 0.3.0 → 0.3.1-next.1041.35b950c
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.
|
@@ -7,7 +7,7 @@ export function disableEntityRuntimeGuards() {
|
|
|
7
7
|
setEntityRuntimeWriteGuard(null);
|
|
8
8
|
}
|
|
9
9
|
// Opt-in development guard mode. When enabled, a direct write to an entity's runtime slot — or to an
|
|
10
|
-
// EntityRuntime's binding slot — that bypasses
|
|
10
|
+
// EntityRuntime's binding slot — that bypasses attachEntityBinding is reported,
|
|
11
11
|
// making "the write landed on the wrong entity" and raw-slot-poke bugs visible early. The write is still
|
|
12
12
|
// allowed: the guard observes, it does not block.
|
|
13
13
|
//
|
|
@@ -27,7 +27,7 @@ function warnOnDirectWrite(slot) {
|
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
29
|
logOnce('entity:direct-runtime-write', LogLevel.Warn, {
|
|
30
|
-
message: "An entity's runtime slot was written directly. Use
|
|
30
|
+
message: "An entity's runtime slot was written directly. Use attachEntityBinding, which allocates the slot for you; the write was allowed, but bypassing it is how a runtime ends up on the wrong entity.",
|
|
31
31
|
}, 'entity');
|
|
32
32
|
}
|
|
33
33
|
//# sourceMappingURL=enableEntityRuntimeGuards.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableEntityRuntimeGuards.js","sourceRoot":"","sources":["../src/enableEntityRuntimeGuards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAEjF,6FAA6F;AAC7F,MAAM,UAAU,0BAA0B;IACxC,yBAAyB,CAAC,KAAK,CAAC,CAAC;IACjC,0BAA0B,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,qGAAqG;AACrG,
|
|
1
|
+
{"version":3,"file":"enableEntityRuntimeGuards.js","sourceRoot":"","sources":["../src/enableEntityRuntimeGuards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAEjF,6FAA6F;AAC7F,MAAM,UAAU,0BAA0B;IACxC,yBAAyB,CAAC,KAAK,CAAC,CAAC;IACjC,0BAA0B,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,qGAAqG;AACrG,gFAAgF;AAChF,yGAAyG;AACzG,kDAAkD;AAClD,EAAE;AACF,2GAA2G;AAC3G,0GAA0G;AAC1G,0GAA0G;AAC1G,uGAAuG;AACvG,MAAM,UAAU,yBAAyB;IACvC,yBAAyB,CAAC,IAAI,CAAC,CAAC;IAChC,0BAA0B,CAAC,iBAAiB,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,iBAAiB,CAAC,IAA4B;IACrD,IAAI,IAAI,KAAK,cAAc,EAAE,CAAC;QAC5B,OAAO,CACL,6BAA6B,EAC7B,QAAQ,CAAC,IAAI,EACb;YACE,OAAO,EACL,0LAA0L;SAC7L,EACD,QAAQ,CACT,CAAC;QACF,OAAO;IACT,CAAC;IACD,OAAO,CACL,6BAA6B,EAC7B,QAAQ,CAAC,IAAI,EACb;QACE,OAAO,EACL,iMAAiM;KACpM,EACD,QAAQ,CACT,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/entity",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1-next.1041.35b950c",
|
|
4
4
|
"author": "Joshua Granick and other contributors",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@flighthq/log": "0.3.
|
|
42
|
-
"@flighthq/types": "0.3.
|
|
41
|
+
"@flighthq/log": "0.3.1-next.1041.35b950c",
|
|
42
|
+
"@flighthq/types": "0.3.1-next.1041.35b950c"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"typescript": "^5.3.0"
|
package/src/binding.test.ts
CHANGED
|
@@ -18,6 +18,18 @@ describe('attachEntityBinding', () => {
|
|
|
18
18
|
attachEntityBinding(entity, binding);
|
|
19
19
|
expect(getEntityRuntime(entity).binding).toStrictEqual(binding);
|
|
20
20
|
});
|
|
21
|
+
|
|
22
|
+
it('preserves an existing runtime while assigning its binding', () => {
|
|
23
|
+
const entity = createEntity();
|
|
24
|
+
const runtime = createEntityRuntime();
|
|
25
|
+
const binding = {};
|
|
26
|
+
entity[EntityRuntimeKey] = runtime;
|
|
27
|
+
|
|
28
|
+
attachEntityBinding(entity, binding);
|
|
29
|
+
|
|
30
|
+
expect(getEntityRuntime(entity)).toBe(runtime);
|
|
31
|
+
expect(runtime.binding).toBe(binding);
|
|
32
|
+
});
|
|
21
33
|
});
|
|
22
34
|
|
|
23
35
|
describe('getEntityBinding', () => {
|
|
@@ -46,7 +46,9 @@ describe('enableEntityRuntimeGuards', () => {
|
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
48
|
expect(entries.length).toBe(1);
|
|
49
|
-
|
|
49
|
+
// Names a function that actually exists: the point of the warning is that the caller can act on it,
|
|
50
|
+
// and this guard spent its life directing readers to `ensureEntityRuntime`, which never existed.
|
|
51
|
+
expect(messageOf(entries[0])).toContain('attachEntityBinding');
|
|
50
52
|
});
|
|
51
53
|
|
|
52
54
|
it('WARNS separately for a direct binding-slot write', () => {
|
package/src/guards.test.ts
CHANGED
|
@@ -45,6 +45,41 @@ describe('setEntityRuntimeGuardMode', () => {
|
|
|
45
45
|
}
|
|
46
46
|
expect(areEntityRuntimeGuardsEnabled()).toBe(false);
|
|
47
47
|
});
|
|
48
|
+
|
|
49
|
+
it('stays disabled when Proxy is unavailable', () => {
|
|
50
|
+
const originalProxy = globalThis.Proxy;
|
|
51
|
+
let enabled: boolean;
|
|
52
|
+
Object.defineProperty(globalThis, 'Proxy', { configurable: true, value: undefined, writable: true });
|
|
53
|
+
try {
|
|
54
|
+
setEntityRuntimeGuardMode(true);
|
|
55
|
+
enabled = areEntityRuntimeGuardsEnabled();
|
|
56
|
+
} finally {
|
|
57
|
+
Object.defineProperty(globalThis, 'Proxy', { configurable: true, value: originalProxy, writable: true });
|
|
58
|
+
setEntityRuntimeGuardMode(false);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
expect(enabled!).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('returns the original values when Proxy disappears after guards are enabled', () => {
|
|
65
|
+
const entity = createEntity({ x: 1 });
|
|
66
|
+
const runtime = createEntityRuntime();
|
|
67
|
+
const originalProxy = globalThis.Proxy;
|
|
68
|
+
let guardedEntity: typeof entity;
|
|
69
|
+
let guardedRuntime: typeof runtime;
|
|
70
|
+
setEntityRuntimeGuardMode(true);
|
|
71
|
+
Object.defineProperty(globalThis, 'Proxy', { configurable: true, value: undefined, writable: true });
|
|
72
|
+
try {
|
|
73
|
+
guardedEntity = createGuardedEntity(entity);
|
|
74
|
+
guardedRuntime = createGuardedEntityRuntime(runtime);
|
|
75
|
+
} finally {
|
|
76
|
+
Object.defineProperty(globalThis, 'Proxy', { configurable: true, value: originalProxy, writable: true });
|
|
77
|
+
setEntityRuntimeGuardMode(false);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
expect(guardedEntity!).toBe(entity);
|
|
81
|
+
expect(guardedRuntime!).toBe(runtime);
|
|
82
|
+
});
|
|
48
83
|
});
|
|
49
84
|
|
|
50
85
|
describe('setEntityRuntimeWriteGuard', () => {
|
|
@@ -78,4 +113,42 @@ describe('setEntityRuntimeWriteGuard', () => {
|
|
|
78
113
|
setEntityRuntimeGuardMode(false);
|
|
79
114
|
}
|
|
80
115
|
});
|
|
116
|
+
|
|
117
|
+
it('ignores writes to ordinary properties', () => {
|
|
118
|
+
const slots: EntityRuntimeWriteSlot[] = [];
|
|
119
|
+
setEntityRuntimeGuardMode(true);
|
|
120
|
+
setEntityRuntimeWriteGuard((slot) => slots.push(slot));
|
|
121
|
+
try {
|
|
122
|
+
const guardedEntity = createGuardedEntity(createEntity({ x: 1 }));
|
|
123
|
+
guardedEntity.x = 2;
|
|
124
|
+
const guardedRuntime = createGuardedEntityRuntime(createEntityRuntime()) as ReturnType<
|
|
125
|
+
typeof createEntityRuntime
|
|
126
|
+
> & { other?: number };
|
|
127
|
+
guardedRuntime.other = 3;
|
|
128
|
+
expect(guardedEntity.x).toBe(2);
|
|
129
|
+
expect(guardedRuntime.other).toBe(3);
|
|
130
|
+
expect(slots).toEqual([]);
|
|
131
|
+
} finally {
|
|
132
|
+
setEntityRuntimeWriteGuard(null);
|
|
133
|
+
setEntityRuntimeGuardMode(false);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('keeps existing proxies silent after guard mode is disabled', () => {
|
|
138
|
+
const slots: EntityRuntimeWriteSlot[] = [];
|
|
139
|
+
setEntityRuntimeGuardMode(true);
|
|
140
|
+
setEntityRuntimeWriteGuard((slot) => slots.push(slot));
|
|
141
|
+
const guardedEntity = createGuardedEntity(createEntity());
|
|
142
|
+
const guardedRuntime = createGuardedEntityRuntime(createEntityRuntime());
|
|
143
|
+
setEntityRuntimeGuardMode(false);
|
|
144
|
+
try {
|
|
145
|
+
guardedEntity[EntityRuntimeKey] = undefined;
|
|
146
|
+
guardedRuntime.binding = {};
|
|
147
|
+
expect(slots).toEqual([]);
|
|
148
|
+
expect(guardedRuntime.binding).toEqual({});
|
|
149
|
+
} finally {
|
|
150
|
+
setEntityRuntimeWriteGuard(null);
|
|
151
|
+
setEntityRuntimeGuardMode(false);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
81
154
|
});
|