alpha
Login
or
Join now
gwen.works
/
churros-notella
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
This repository has no description
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
fix(devx): Fix mock sender script
author
Gwen Le Bihan
date
1 year ago
(Feb 19, 2025, 8:54 PM +0100)
commit
e7ef1ca5
e7ef1ca547555d7924d6a4ae6ab1c6819340bea6
parent
1a18cc83
1a18cc83a310408921980c9495d131377659f9fe
+45
-39
1 changed file
Expand all
Collapse all
Unified
Split
scripts
mocksender.ts
+45
-39
scripts/mocksender.ts
Reviewed
···
1
1
//@ts-nocheck
2
2
3
3
-
import { connect, JetStreamManager, NatsConnection, StringCodec } from 'nats';
4
4
-
import { STREAM_NAME, SUBJECT_NAME } from '../constants.js';
5
5
-
import { Message } from '../types.js';
3
3
+
import { connect, JetStreamManager, NatsConnection, StringCodec } from "nats"
4
4
+
import {
5
5
+
STREAM_NAME,
6
6
+
SUBJECT_NAME,
7
7
+
type Message,
8
8
+
Event,
9
9
+
} from "../typescript/index.js"
6
10
7
11
async function setupStream(
8
12
jetStreamManager: JetStreamManager,
9
13
streamName: string,
10
10
-
subject: string,
14
14
+
subject: string
11
15
) {
12
16
try {
13
17
// Try to add the stream (will not recreate if it exists)
14
18
await jetStreamManager.streams.add({
15
19
name: streamName,
16
20
subjects: [subject],
17
17
-
});
18
18
-
console.log(`Stream '${streamName}' created or already exists.`);
21
21
+
})
22
22
+
console.log(`Stream '${streamName}' created or already exists.`)
19
23
} catch (err) {
20
20
-
console.error(`Error setting up stream: ${err.message}`);
24
24
+
console.error(`Error setting up stream: ${err.message}`)
21
25
}
22
26
}
23
27
24
24
-
enum Event {
25
25
-
CommentReply = 'comment_reply',
26
26
-
GodchildRequest = 'godchild_request',
27
27
-
NewComment = 'new_comment',
28
28
-
NewPost = 'new_post',
29
29
-
NewTicket = 'new_ticket',
30
30
-
}
31
31
-
32
28
function randomMessage(): Message {
33
29
const events = [
34
34
-
Event.CommentReply,
35
30
Event.GodchildRequest,
36
36
-
Event.NewComment,
37
31
Event.NewPost,
38
32
Event.NewTicket,
39
39
-
] as const;
40
40
-
const event = events[Math.floor(Math.random() * events.length)];
41
41
-
const id = Math.random().toString(36).substring(7);
42
42
-
return { event, id };
33
33
+
] as const
34
34
+
const event = events[Math.floor(Math.random() * events.length)]
35
35
+
const id = Math.random().toString(36).substring(7)
36
36
+
const send_at = new Date()
37
37
+
send_at.setSeconds(send_at.getSeconds() + Math.floor(Math.random() * 10))
38
38
+
return {
39
39
+
event,
40
40
+
id,
41
41
+
send_at,
42
42
+
clear_schedule_for:
43
43
+
Math.random() > 0.5
44
44
+
? []
45
45
+
: Math.random() > 0.5
46
46
+
? [Event.GodchildRequest, event]
47
47
+
: [event],
48
48
+
}
43
49
}
44
50
45
51
async function publishMessages(
46
52
nc: NatsConnection,
47
53
subject: string,
48
54
messageCount: number,
49
49
-
delayMs: number,
55
55
+
delayMs: number
50
56
) {
51
51
-
const js = nc.jetstream();
52
52
-
const sc = StringCodec();
57
57
+
const js = nc.jetstream()
58
58
+
const sc = StringCodec()
53
59
54
60
for (let i = 1; i <= messageCount; i++) {
55
55
-
const message = `Mock Order #${i}`;
56
56
-
await js.publish(subject, sc.encode(JSON.stringify(randomMessage())));
57
57
-
console.log(`Sent message: ${message}`);
58
58
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
61
61
+
const message = `Mock Order #${i}`
62
62
+
await js.publish(subject, sc.encode(JSON.stringify(randomMessage())))
63
63
+
console.log(`Sent message: ${message}`)
64
64
+
await new Promise((resolve) => setTimeout(resolve, delayMs))
59
65
}
60
66
}
61
67
62
68
async function main() {
63
69
// Connect to the NATS server
64
64
-
const nc = await connect({ servers: 'localhost:4222' });
65
65
-
console.log('Connected to NATS');
70
70
+
const nc = await connect({ servers: "localhost:4222" })
71
71
+
console.log("Connected to NATS")
66
72
67
73
// Create a JetStream manager to manage streams
68
68
-
const jsm = await nc.jetstreamManager();
74
74
+
const jsm = await nc.jetstreamManager()
69
75
70
76
// Ensure the stream exists
71
71
-
await setupStream(jsm, STREAM_NAME, SUBJECT_NAME);
77
77
+
await setupStream(jsm, STREAM_NAME, SUBJECT_NAME)
72
78
73
79
// Publish messages at intervals
74
74
-
const messageCount = 1000;
75
75
-
const delayMs = 10; // 1 second delay between messages
76
76
-
await publishMessages(nc, SUBJECT_NAME, messageCount, delayMs);
80
80
+
const messageCount = 1000
81
81
+
const delayMs = 10 // 1 second delay between messages
82
82
+
await publishMessages(nc, SUBJECT_NAME, messageCount, delayMs)
77
83
78
78
-
console.log('Finished sending messages.');
79
79
-
await nc.close();
84
84
+
console.log("Finished sending messages.")
85
85
+
await nc.close()
80
86
}
81
87
82
88
main().catch((err) => {
83
83
-
console.error(`Error in sender: ${err.message}`);
84
84
-
});
89
89
+
console.error(`Error in sender: ${err.message}`)
90
90
+
})