-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathenvelopes_default.go
58 lines (51 loc) · 1.04 KB
/
envelopes_default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build !sonic
package nostr
import (
"errors"
"strings"
)
func NewMessageParser() MessageParser {
return messageParser{}
}
type messageParser struct{}
func (messageParser) ParseMessage(message string) (Envelope, error) {
firstQuote := strings.IndexRune(message, '"')
if firstQuote == -1 {
return nil, errors.New("malformed json")
}
secondQuote := strings.IndexRune(message[firstQuote+1:], '"')
if secondQuote == -1 {
return nil, errors.New("malformed json")
}
label := message[firstQuote+1 : firstQuote+1+secondQuote]
var v Envelope
switch label {
case "EVENT":
v = &EventEnvelope{}
case "REQ":
v = &ReqEnvelope{}
case "COUNT":
v = &CountEnvelope{}
case "NOTICE":
x := NoticeEnvelope("")
v = &x
case "EOSE":
x := EOSEEnvelope("")
v = &x
case "OK":
v = &OKEnvelope{}
case "AUTH":
v = &AuthEnvelope{}
case "CLOSED":
v = &ClosedEnvelope{}
case "CLOSE":
x := CloseEnvelope("")
v = &x
default:
return nil, UnknownLabel
}
if err := v.FromJSON(message); err != nil {
return nil, err
}
return v, nil
}