sleepy-socket 0.6.1 → 0.7.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.
@@ -0,0 +1,46 @@
1
+ import { id } from './utils.js'
2
+
3
+ export const MessageType = {
4
+ Welcome: 'welcome',
5
+ Heartbeat: 'heartbeat',
6
+ Request: 'request',
7
+ Response: 'response',
8
+ Notification: 'notification',
9
+ } as const
10
+
11
+ export type MessageType = typeof MessageType[keyof typeof MessageType]
12
+
13
+ export type MessageHeaders = Headers | Record<string, string>
14
+
15
+ export type MessageOptions = {
16
+ id?: string
17
+ headers?: MessageHeaders
18
+ body?: unknown
19
+ [key: string]: unknown
20
+ }
21
+
22
+ export type Message = {
23
+ id: string
24
+ clientId: string
25
+ type: MessageType
26
+ timestamp: string
27
+ headers: MessageHeaders
28
+ body: unknown
29
+ [key: string]: unknown
30
+ }
31
+
32
+ export function createMessage (
33
+ clientId: string,
34
+ type: MessageType,
35
+ opts: MessageOptions = {},
36
+ ): Message {
37
+ return {
38
+ ...opts,
39
+ id: opts.id ?? id(),
40
+ clientId,
41
+ type,
42
+ timestamp: new Date().toISOString(),
43
+ headers: opts.headers ?? new Headers(),
44
+ body: opts.body ?? null,
45
+ }
46
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,22 @@
1
+ export type IdGenerator = () => string
2
+
3
+ export const defaultIdGenerator: IdGenerator = () => crypto.randomUUID()
4
+
5
+ let _uuidFn: IdGenerator = defaultIdGenerator
6
+
7
+ export function joinRoute (...segments: string[]): string {
8
+ const joined = segments
9
+ .filter(Boolean)
10
+ .join('/')
11
+ .replace(/\/{2,}/g, '/')
12
+
13
+ return joined.startsWith('/') ? joined : `/${joined}`
14
+ }
15
+
16
+ export function id (): string {
17
+ return _uuidFn()
18
+ }
19
+
20
+ export function setIdGenerator (fn: IdGenerator): void {
21
+ _uuidFn = fn
22
+ }
package/src/messages.js DELETED
@@ -1,21 +0,0 @@
1
- import { id } from './utils'
2
-
3
- export const TYPES = {
4
- WELCOME: 'welcome',
5
- HEARTBEAT: 'heartbeat',
6
- REQUEST: 'request',
7
- RESPONSE: 'response',
8
- NOTIFICATION: 'notification',
9
- }
10
-
11
- export function createMessage (clientId, type, opts = {}) {
12
- return {
13
- ...opts,
14
- id: opts.id ?? id(),
15
- clientId,
16
- type,
17
- timestamp: new Date().toISOString(),
18
- headers: opts.headers ?? new Headers(),
19
- body: opts.body ?? null,
20
- }
21
- }
package/src/utils.js DELETED
@@ -1,19 +0,0 @@
1
- let _uuidFn = () => crypto.randomUUID()
2
-
3
- export function joinRoute (...segments) {
4
- const joined = segments
5
- .filter(Boolean)
6
- .join('/')
7
- .replace(/\/{2,}/g, '/')
8
-
9
- return joined.startsWith('/') ? joined : `/${joined}`
10
- }
11
-
12
-
13
- export function id () {
14
- return _uuidFn()
15
- }
16
-
17
- export function setIdGenerator (fn) {
18
- _uuidFn = fn
19
- }