Skip to content

Commit 5cf8c0f

Browse files
authored
Disallow non-numbers for ts/lc handling on states (#3057)
* Disallow non-numbers for ts/lc handling on states * adjust tests * changelog
1 parent 977fbdc commit 5cf8c0f

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
## __WORK IN PROGRESS__
55
-->
66

7+
## __WORK IN PROGRESS__
8+
* (@Apollon77) Allows only numbers for ts and tc fields in state when provided for setState
9+
710
## 7.0.7 (2025-04-17) - Lucy
811
* (@foxriver76) fixed the edge-case problem on Windows (if adapter calls `readDir` on single file)
912
* (@foxriver76) fixed setting negative numbers via `state set` cli command

packages/controller/test/testStatesRedis.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('States-Redis: Test states in Redis', function () {
9595
expect(state!.val).to.be.equal(2);
9696
expect(state!.ack).to.be.false;
9797
expect(state!.ts).to.be.ok;
98+
expect(state!.lc).to.be.equal(state!.ts);
9899
expect(state!.q).to.be.equal(0);
99100

100101
// @ts-expect-error adding types later on
@@ -103,6 +104,7 @@ describe('States-Redis: Test states in Redis', function () {
103104
expect(state!.val).to.be.equal(2);
104105
expect(state!.ack).to.be.false;
105106
expect(state!.ts).to.be.ok;
107+
expect(state!.lc).to.be.equal(state!.ts);
106108
expect(state!.q).to.be.equal(0);
107109
done();
108110
}
@@ -111,6 +113,61 @@ describe('States-Redis: Test states in Redis', function () {
111113
states!.setState(testID, 2);
112114
}).timeout(10_000);
113115

116+
it('States-Redis: should setState with object state parameters', done => {
117+
const testID = 'testObject.0.test1';
118+
onStatesChanged = async (id, state) => {
119+
if (id === testID) {
120+
expect(state).to.be.ok;
121+
expect(state!.val).to.be.equal(3);
122+
expect(state!.ack).to.be.true;
123+
expect(state!.ts).to.be.equal(123456000);
124+
expect(state!.lc).to.be.equal(state!.ts);
125+
expect(state!.q).to.be.equal(1);
126+
127+
// @ts-expect-error adding types later on
128+
state = await states!.getStateAsync(testID);
129+
expect(state).to.be.ok;
130+
expect(state!.val).to.be.equal(3);
131+
expect(state!.ack).to.be.true;
132+
expect(state!.ts).to.be.equal(123456000);
133+
expect(state!.lc).to.be.equal(state!.ts);
134+
expect(state!.q).to.be.equal(1);
135+
done();
136+
}
137+
};
138+
139+
states!.setState(testID, { val: 3, ack: true, ts: 123456, q: 1 });
140+
}).timeout(10_000);
141+
142+
it('States-Redis: should setState with object state parameters ignoring null ts', done => {
143+
const testID = 'testObject.0.test1';
144+
onStatesChanged = async (id, state) => {
145+
if (id === testID) {
146+
expect(state).to.be.ok;
147+
expect(state!.val).to.be.equal(4);
148+
expect(state!.ack).to.be.true;
149+
expect(state!.ts).to.be.ok;
150+
expect(state!.ts).to.be.not.equal(123456000);
151+
expect(state!.lc).to.be.equal(state!.ts);
152+
expect(state!.q).to.be.equal(1);
153+
154+
// @ts-expect-error adding types later on
155+
state = await states!.getStateAsync(testID);
156+
expect(state).to.be.ok;
157+
expect(state!.val).to.be.equal(4);
158+
expect(state!.ack).to.be.true;
159+
expect(state!.ts).to.be.ok;
160+
expect(state!.ts).to.be.not.equal(123456000);
161+
expect(state!.lc).to.be.equal(state!.ts);
162+
expect(state!.q).to.be.equal(1);
163+
done();
164+
}
165+
};
166+
167+
// @ts-expect-error ignore types here for ts to test the case
168+
states!.setState(testID, { val: 4, ack: true, ts: null, q: 1 });
169+
}).timeout(10_000);
170+
114171
// todo: write more tests
115172

116173
after('States-Redis: Stop js-controller', async function () {

packages/db-states-redis/src/lib/states/statesInRedisClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ export class StateRedisClient {
750750
obj.ack = false;
751751
}
752752

753-
if (state.ts !== undefined) {
753+
if (typeof state.ts === 'number') {
754754
obj.ts = state.ts < 946681200000 ? state.ts * 1000 : state.ts; // if less 2000.01.01 00:00:00
755755
} else {
756756
obj.ts = new Date().getTime();
@@ -775,7 +775,7 @@ export class StateRedisClient {
775775

776776
let hasChanged;
777777

778-
if (state.lc !== undefined) {
778+
if (typeof state.lc === 'number') {
779779
obj.lc = state.lc;
780780
} else {
781781
// isDeepStrictEqual works on objects and primitive values

0 commit comments

Comments
 (0)