nanoflags 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -20
- data/LICENSE +202 -202
- data/NOTICE +4 -4
- data/README.md +206 -152
- data/lib/nanoflags/client.rb +1225 -869
- data/lib/nanoflags/engine/js.rb +531 -531
- data/lib/nanoflags/engine/json.rb +200 -200
- data/lib/nanoflags/engine.rb +407 -407
- data/lib/nanoflags/evaluation_detail.rb +91 -87
- data/lib/nanoflags/http.rb +126 -119
- data/lib/nanoflags/keys.rb +14 -14
- data/lib/nanoflags/session.rb +137 -0
- data/lib/nanoflags/store.rb +59 -59
- data/lib/nanoflags/verify.rb +180 -180
- data/lib/nanoflags/version.rb +6 -6
- data/lib/nanoflags/websocket.rb +411 -0
- data/lib/nanoflags.rb +12 -10
- metadata +9 -6
|
@@ -1,200 +1,200 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "json"
|
|
4
|
-
require "strscan"
|
|
5
|
-
|
|
6
|
-
module NanoFlags
|
|
7
|
-
module Engine
|
|
8
|
-
# Raised by {Json.parse} for text that `JSON.parse` rejects.
|
|
9
|
-
class InvalidJsonError < ArgumentError; end
|
|
10
|
-
|
|
11
|
-
# `JSON.parse` / `JSON.stringify` with JavaScript semantics for the engine's value model ({Js}): numbers become
|
|
12
|
-
# Float (overflow becomes ±Infinity), objects become Hash in JavaScript key order, a repeated key keeps its first
|
|
13
|
-
# position and its last value, and lone UTF-16 surrogates survive as WTF-8.
|
|
14
|
-
module Json
|
|
15
|
-
module_function
|
|
16
|
-
|
|
17
|
-
PARSE_OPTS = begin
|
|
18
|
-
opts = { allow_nan: false, max_nesting: false, create_additions: false }
|
|
19
|
-
# json >= 2.13 warns about repeated keys unless told that JSON.parse semantics (last one wins) are wanted.
|
|
20
|
-
opts[:allow_duplicate_key] = true if Gem::Version.new(::JSON::VERSION) >= Gem::Version.new("2.13")
|
|
21
|
-
opts.freeze
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Parses JSON text exactly like JavaScript's `JSON.parse`.
|
|
25
|
-
def parse(text)
|
|
26
|
-
text = text.to_s
|
|
27
|
-
text = text.dup.force_encoding(Encoding::UTF_8) unless text.encoding == Encoding::UTF_8
|
|
28
|
-
# Fast path: the json extension, then the result is converted. Lone surrogates and invalid UTF-8 make it fail
|
|
29
|
-
# (or are not representable); the exact parser below handles those.
|
|
30
|
-
if text.valid_encoding?
|
|
31
|
-
begin
|
|
32
|
-
return convert(::JSON.parse(text, PARSE_OPTS))
|
|
33
|
-
rescue ::JSON::ParserError, EncodingError, ArgumentError
|
|
34
|
-
# fall through to the exact parser
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
Parser.new(text).parse_document
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def convert(v)
|
|
41
|
-
case v
|
|
42
|
-
when Integer then v.bit_length <= 53 ? v.to_f : Js.integer_to_float(v)
|
|
43
|
-
when Array then v.map! { |e| convert(e) }
|
|
44
|
-
when Hash
|
|
45
|
-
v.transform_values! { |e| convert(e) }
|
|
46
|
-
Js.js_order(v)
|
|
47
|
-
else v
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# JSON.stringify(v), or nil where JSON.stringify returns undefined.
|
|
52
|
-
def stringify(v) = Js.stringify(v)
|
|
53
|
-
|
|
54
|
-
# Byte-level `JSON.parse` for the inputs the json extension refuses: lone UTF-16 surrogates are kept as their
|
|
55
|
-
# three-byte WTF-8 form, invalid UTF-8 in the text becomes U+FFFD (as a TextDecoder would).
|
|
56
|
-
#
|
|
57
|
-
# @api private
|
|
58
|
-
class Parser
|
|
59
|
-
def initialize(text)
|
|
60
|
-
@s = StringScanner.new(text.b)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def parse_document
|
|
64
|
-
ws
|
|
65
|
-
v = value(0)
|
|
66
|
-
ws
|
|
67
|
-
fail!("unexpected trailing data") unless @s.eos?
|
|
68
|
-
v
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
private
|
|
72
|
-
|
|
73
|
-
def fail!(msg)
|
|
74
|
-
raise InvalidJsonError, "invalid JSON: #{msg} at offset #{@s.pos}"
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def ws
|
|
78
|
-
@s.skip(/[ \t\n\r]*/)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def value(depth)
|
|
82
|
-
fail!("nesting too deep") if depth > 10_000
|
|
83
|
-
fail!("unexpected end") if @s.eos?
|
|
84
|
-
case @s.peek(1)
|
|
85
|
-
when "{"
|
|
86
|
-
@s.pos += 1
|
|
87
|
-
h = {}
|
|
88
|
-
ws
|
|
89
|
-
return Js.js_order(h) if @s.skip(/\}/)
|
|
90
|
-
|
|
91
|
-
loop do
|
|
92
|
-
ws
|
|
93
|
-
fail!("expected a string key") unless @s.peek(1) == '"'
|
|
94
|
-
k = str
|
|
95
|
-
ws
|
|
96
|
-
fail!("expected ':'") unless @s.skip(/:/)
|
|
97
|
-
ws
|
|
98
|
-
h[k] = value(depth + 1)
|
|
99
|
-
ws
|
|
100
|
-
next if @s.skip(/,/)
|
|
101
|
-
return Js.js_order(h) if @s.skip(/\}/)
|
|
102
|
-
|
|
103
|
-
fail!("expected ',' or '}'")
|
|
104
|
-
end
|
|
105
|
-
when "["
|
|
106
|
-
@s.pos += 1
|
|
107
|
-
a = []
|
|
108
|
-
ws
|
|
109
|
-
return a if @s.skip(/\]/)
|
|
110
|
-
|
|
111
|
-
loop do
|
|
112
|
-
ws
|
|
113
|
-
a << value(depth + 1)
|
|
114
|
-
ws
|
|
115
|
-
next if @s.skip(/,/)
|
|
116
|
-
return a if @s.skip(/\]/)
|
|
117
|
-
|
|
118
|
-
fail!("expected ',' or ']'")
|
|
119
|
-
end
|
|
120
|
-
when '"' then str
|
|
121
|
-
when "t" then lit("true", true)
|
|
122
|
-
when "f" then lit("false", false)
|
|
123
|
-
when "n" then lit("null", nil)
|
|
124
|
-
else
|
|
125
|
-
t = @s.scan(/-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/)
|
|
126
|
-
fail!("unexpected character") if t.nil?
|
|
127
|
-
fail!("invalid number") if @s.match?(/[0-9.eE+-]/)
|
|
128
|
-
t.to_f
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def lit(word, v)
|
|
133
|
-
fail!("invalid literal") unless @s.skip(Regexp.new(Regexp.escape(word)))
|
|
134
|
-
v
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def hex4
|
|
138
|
-
h = @s.scan(/[0-9a-fA-F]{4}/)
|
|
139
|
-
h&.to_i(16)
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
def str
|
|
143
|
-
@s.pos += 1 # opening quote
|
|
144
|
-
out = +""
|
|
145
|
-
loop do
|
|
146
|
-
run = @s.scan(/[^"\\\x00-\x1F\x80-\xFF]+/n)
|
|
147
|
-
out << run if run
|
|
148
|
-
fail!("unterminated string") if @s.eos?
|
|
149
|
-
c = @s.peek(1)
|
|
150
|
-
if c == '"'
|
|
151
|
-
@s.pos += 1
|
|
152
|
-
return out.force_encoding(Encoding::UTF_8)
|
|
153
|
-
end
|
|
154
|
-
o = c.ord
|
|
155
|
-
fail!("control character in string") if o < 0x20
|
|
156
|
-
if o >= 0x80
|
|
157
|
-
# One UTF-8 sequence; an invalid one becomes U+FFFD (one per byte).
|
|
158
|
-
chunk = @s.peek(4)
|
|
159
|
-
cp, size = Js.code_points(chunk.dup.force_encoding(Encoding::UTF_8)).first
|
|
160
|
-
if (cp == 0xFFFD && size == 1) || (cp >= 0xD800 && cp <= 0xDFFF)
|
|
161
|
-
out << Js::REPLACEMENT.b
|
|
162
|
-
@s.pos += 1
|
|
163
|
-
else
|
|
164
|
-
out << chunk[0, size]
|
|
165
|
-
@s.pos += size
|
|
166
|
-
end
|
|
167
|
-
next
|
|
168
|
-
end
|
|
169
|
-
@s.pos += 1 # backslash
|
|
170
|
-
fail!("unterminated escape") if @s.eos?
|
|
171
|
-
e = @s.getch
|
|
172
|
-
case e
|
|
173
|
-
when '"', "\\", "/" then out << e
|
|
174
|
-
when "b" then out << "\b"
|
|
175
|
-
when "f" then out << "\f"
|
|
176
|
-
when "n" then out << "\n"
|
|
177
|
-
when "r" then out << "\r"
|
|
178
|
-
when "t" then out << "\t"
|
|
179
|
-
when "u"
|
|
180
|
-
u = hex4
|
|
181
|
-
fail!("invalid unicode escape") if u.nil?
|
|
182
|
-
if u.between?(0xD800, 0xDBFF) && @s.peek(2) == "\\u"
|
|
183
|
-
save = @s.pos
|
|
184
|
-
@s.pos += 2
|
|
185
|
-
lo = hex4
|
|
186
|
-
if lo&.between?(0xDC00, 0xDFFF)
|
|
187
|
-
out << Js.encode_unit(0x10000 + ((u - 0xD800) << 10) + (lo - 0xDC00)).b
|
|
188
|
-
next
|
|
189
|
-
end
|
|
190
|
-
@s.pos = save
|
|
191
|
-
end
|
|
192
|
-
out << Js.encode_unit(u).b
|
|
193
|
-
else fail!("invalid escape")
|
|
194
|
-
end
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "strscan"
|
|
5
|
+
|
|
6
|
+
module NanoFlags
|
|
7
|
+
module Engine
|
|
8
|
+
# Raised by {Json.parse} for text that `JSON.parse` rejects.
|
|
9
|
+
class InvalidJsonError < ArgumentError; end
|
|
10
|
+
|
|
11
|
+
# `JSON.parse` / `JSON.stringify` with JavaScript semantics for the engine's value model ({Js}): numbers become
|
|
12
|
+
# Float (overflow becomes ±Infinity), objects become Hash in JavaScript key order, a repeated key keeps its first
|
|
13
|
+
# position and its last value, and lone UTF-16 surrogates survive as WTF-8.
|
|
14
|
+
module Json
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
PARSE_OPTS = begin
|
|
18
|
+
opts = { allow_nan: false, max_nesting: false, create_additions: false }
|
|
19
|
+
# json >= 2.13 warns about repeated keys unless told that JSON.parse semantics (last one wins) are wanted.
|
|
20
|
+
opts[:allow_duplicate_key] = true if Gem::Version.new(::JSON::VERSION) >= Gem::Version.new("2.13")
|
|
21
|
+
opts.freeze
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Parses JSON text exactly like JavaScript's `JSON.parse`.
|
|
25
|
+
def parse(text)
|
|
26
|
+
text = text.to_s
|
|
27
|
+
text = text.dup.force_encoding(Encoding::UTF_8) unless text.encoding == Encoding::UTF_8
|
|
28
|
+
# Fast path: the json extension, then the result is converted. Lone surrogates and invalid UTF-8 make it fail
|
|
29
|
+
# (or are not representable); the exact parser below handles those.
|
|
30
|
+
if text.valid_encoding?
|
|
31
|
+
begin
|
|
32
|
+
return convert(::JSON.parse(text, PARSE_OPTS))
|
|
33
|
+
rescue ::JSON::ParserError, EncodingError, ArgumentError
|
|
34
|
+
# fall through to the exact parser
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
Parser.new(text).parse_document
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def convert(v)
|
|
41
|
+
case v
|
|
42
|
+
when Integer then v.bit_length <= 53 ? v.to_f : Js.integer_to_float(v)
|
|
43
|
+
when Array then v.map! { |e| convert(e) }
|
|
44
|
+
when Hash
|
|
45
|
+
v.transform_values! { |e| convert(e) }
|
|
46
|
+
Js.js_order(v)
|
|
47
|
+
else v
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# JSON.stringify(v), or nil where JSON.stringify returns undefined.
|
|
52
|
+
def stringify(v) = Js.stringify(v)
|
|
53
|
+
|
|
54
|
+
# Byte-level `JSON.parse` for the inputs the json extension refuses: lone UTF-16 surrogates are kept as their
|
|
55
|
+
# three-byte WTF-8 form, invalid UTF-8 in the text becomes U+FFFD (as a TextDecoder would).
|
|
56
|
+
#
|
|
57
|
+
# @api private
|
|
58
|
+
class Parser
|
|
59
|
+
def initialize(text)
|
|
60
|
+
@s = StringScanner.new(text.b)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def parse_document
|
|
64
|
+
ws
|
|
65
|
+
v = value(0)
|
|
66
|
+
ws
|
|
67
|
+
fail!("unexpected trailing data") unless @s.eos?
|
|
68
|
+
v
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def fail!(msg)
|
|
74
|
+
raise InvalidJsonError, "invalid JSON: #{msg} at offset #{@s.pos}"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def ws
|
|
78
|
+
@s.skip(/[ \t\n\r]*/)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def value(depth)
|
|
82
|
+
fail!("nesting too deep") if depth > 10_000
|
|
83
|
+
fail!("unexpected end") if @s.eos?
|
|
84
|
+
case @s.peek(1)
|
|
85
|
+
when "{"
|
|
86
|
+
@s.pos += 1
|
|
87
|
+
h = {}
|
|
88
|
+
ws
|
|
89
|
+
return Js.js_order(h) if @s.skip(/\}/)
|
|
90
|
+
|
|
91
|
+
loop do
|
|
92
|
+
ws
|
|
93
|
+
fail!("expected a string key") unless @s.peek(1) == '"'
|
|
94
|
+
k = str
|
|
95
|
+
ws
|
|
96
|
+
fail!("expected ':'") unless @s.skip(/:/)
|
|
97
|
+
ws
|
|
98
|
+
h[k] = value(depth + 1)
|
|
99
|
+
ws
|
|
100
|
+
next if @s.skip(/,/)
|
|
101
|
+
return Js.js_order(h) if @s.skip(/\}/)
|
|
102
|
+
|
|
103
|
+
fail!("expected ',' or '}'")
|
|
104
|
+
end
|
|
105
|
+
when "["
|
|
106
|
+
@s.pos += 1
|
|
107
|
+
a = []
|
|
108
|
+
ws
|
|
109
|
+
return a if @s.skip(/\]/)
|
|
110
|
+
|
|
111
|
+
loop do
|
|
112
|
+
ws
|
|
113
|
+
a << value(depth + 1)
|
|
114
|
+
ws
|
|
115
|
+
next if @s.skip(/,/)
|
|
116
|
+
return a if @s.skip(/\]/)
|
|
117
|
+
|
|
118
|
+
fail!("expected ',' or ']'")
|
|
119
|
+
end
|
|
120
|
+
when '"' then str
|
|
121
|
+
when "t" then lit("true", true)
|
|
122
|
+
when "f" then lit("false", false)
|
|
123
|
+
when "n" then lit("null", nil)
|
|
124
|
+
else
|
|
125
|
+
t = @s.scan(/-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/)
|
|
126
|
+
fail!("unexpected character") if t.nil?
|
|
127
|
+
fail!("invalid number") if @s.match?(/[0-9.eE+-]/)
|
|
128
|
+
t.to_f
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def lit(word, v)
|
|
133
|
+
fail!("invalid literal") unless @s.skip(Regexp.new(Regexp.escape(word)))
|
|
134
|
+
v
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def hex4
|
|
138
|
+
h = @s.scan(/[0-9a-fA-F]{4}/)
|
|
139
|
+
h&.to_i(16)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def str
|
|
143
|
+
@s.pos += 1 # opening quote
|
|
144
|
+
out = +""
|
|
145
|
+
loop do
|
|
146
|
+
run = @s.scan(/[^"\\\x00-\x1F\x80-\xFF]+/n)
|
|
147
|
+
out << run if run
|
|
148
|
+
fail!("unterminated string") if @s.eos?
|
|
149
|
+
c = @s.peek(1)
|
|
150
|
+
if c == '"'
|
|
151
|
+
@s.pos += 1
|
|
152
|
+
return out.force_encoding(Encoding::UTF_8)
|
|
153
|
+
end
|
|
154
|
+
o = c.ord
|
|
155
|
+
fail!("control character in string") if o < 0x20
|
|
156
|
+
if o >= 0x80
|
|
157
|
+
# One UTF-8 sequence; an invalid one becomes U+FFFD (one per byte).
|
|
158
|
+
chunk = @s.peek(4)
|
|
159
|
+
cp, size = Js.code_points(chunk.dup.force_encoding(Encoding::UTF_8)).first
|
|
160
|
+
if (cp == 0xFFFD && size == 1) || (cp >= 0xD800 && cp <= 0xDFFF)
|
|
161
|
+
out << Js::REPLACEMENT.b
|
|
162
|
+
@s.pos += 1
|
|
163
|
+
else
|
|
164
|
+
out << chunk[0, size]
|
|
165
|
+
@s.pos += size
|
|
166
|
+
end
|
|
167
|
+
next
|
|
168
|
+
end
|
|
169
|
+
@s.pos += 1 # backslash
|
|
170
|
+
fail!("unterminated escape") if @s.eos?
|
|
171
|
+
e = @s.getch
|
|
172
|
+
case e
|
|
173
|
+
when '"', "\\", "/" then out << e
|
|
174
|
+
when "b" then out << "\b"
|
|
175
|
+
when "f" then out << "\f"
|
|
176
|
+
when "n" then out << "\n"
|
|
177
|
+
when "r" then out << "\r"
|
|
178
|
+
when "t" then out << "\t"
|
|
179
|
+
when "u"
|
|
180
|
+
u = hex4
|
|
181
|
+
fail!("invalid unicode escape") if u.nil?
|
|
182
|
+
if u.between?(0xD800, 0xDBFF) && @s.peek(2) == "\\u"
|
|
183
|
+
save = @s.pos
|
|
184
|
+
@s.pos += 2
|
|
185
|
+
lo = hex4
|
|
186
|
+
if lo&.between?(0xDC00, 0xDFFF)
|
|
187
|
+
out << Js.encode_unit(0x10000 + ((u - 0xD800) << 10) + (lo - 0xDC00)).b
|
|
188
|
+
next
|
|
189
|
+
end
|
|
190
|
+
@s.pos = save
|
|
191
|
+
end
|
|
192
|
+
out << Js.encode_unit(u).b
|
|
193
|
+
else fail!("invalid escape")
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|