-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrejim.tcl
194 lines (152 loc) · 4 KB
/
rejim.tcl
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# A basic RESP2 Redis/Valkey/KeyDB/etc. client library.
# Pronounced "regime" for some reason.
# Copyright (c) 2019-2020, 2024 D. Bohdan.
# License: MIT.
namespace eval rejim {
variable version 0.2.0
variable jim [expr { ![catch {
proc x y {} {}
rename x {}
}] }]
if {$jim} {
proc byte-range {string first last} {
string byterange $string $first $last
}
proc byte-length string {
string bytelength $string
}
} else {
proc byte-range {string first last} {
string range $string $first $last
}
proc byte-length string {
string length $string
}
}
}
proc rejim::command {handle commandList} {
fconfigure $handle -translation binary -buffering none
puts -nonewline $handle [serialize $commandList]
set result [parse $handle]
return $result
}
proc rejim::parse handle {
fconfigure $handle -translation binary -buffering none
set typeByte [read $handle 1]
set firstData [byte-range [read-until $handle \r] 0 end-1]
read $handle 1 ;# Discard \n.
switch -- $typeByte {
+ -
- {
set type [expr { $typeByte eq {+} ? {simple} : {error} }]
return [list $type $firstData]
}
: {
return [list integer $firstData]
}
$ {
set len $firstData
if {$len == -1} {
return null
}
if {$len < -1} {
error [list invalid bulk string length: $len]
}
set data [read $handle $len]
read $handle 2 ;# Discard \r\n.
return [list bulk $data]
}
* {
set n $firstData
if {$n < 0} {
error [list invalid number of array elements: $n]
}
set list {}
for {set i 0} {$i < $n} {incr i} {
lappend list [parse $handle]
}
return [concat array $list]
}
default {
error [list unknown message type: $typeByte]
}
}
}
proc rejim::read-until {handle needle} {
fconfigure $handle -translation binary -buffering none
# We only use this proc to find short strings. The performance of reading
# one byte at a time shouldn't matter.
if {[byte-length $needle] != 1} {
error [list $needle isn't one byte]
}
set data {}
while 1 {
if {[eof $handle]} break
set last [read $handle 1]
append data $last
if {$last eq $needle} break
}
if {[info exists last] && $last ne $needle} {
error [list stream ended before $needle]
}
return $data
}
proc rejim::serialize list {
set resp *[llength $list]\r\n
foreach el $list {
append resp $[byte-length $el]\r\n$el\r\n
}
return $resp
}
proc rejim::serialize-tagged tagged {
set data [lassign $tagged tag]
unset tagged
switch -- $tag {
array {
return *[llength $data]\r\n[join [lmap x $data {
serialize-tagged $x
}] {}]
}
bulk {
return \$[byte-length $data]\r\n$data\r\n
}
error -
integer -
simple {
set c [dict get {
error -
integer :
simple +
} $tag]
return $c$data\r\n
}
null {
return \$-1\r\n
}
default {
error [list unknown tag: $tag]
}
}
}
proc rejim::strip-tags {response {null %NULL%}} {
set tag [lindex $response 0]
switch -- $tag {
bulk -
error -
integer -
simple {
return [lindex $response 1]
}
null {
return $null
}
array {
return [lmap x [lrange $response 1 end] {
strip-tags $x $null
}]
}
default {
error [list unknown tag: $tag]
}
}
}