lattice-grid 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +179 -0
- data/lib/lattice/grid/aggregate_sql.rb +98 -0
- data/lib/lattice/grid/cells.rb +85 -0
- data/lib/lattice/grid/collab/axis.rb +40 -0
- data/lib/lattice/grid/collab/channel.rb +31 -0
- data/lib/lattice/grid/collab/channel_handler.rb +84 -0
- data/lib/lattice/grid/collab/connection.rb +72 -0
- data/lib/lattice/grid/collab/doc_model.rb +99 -0
- data/lib/lattice/grid/collab/doc_model_apply.rb +105 -0
- data/lib/lattice/grid/collab/doc_model_doc.rb +95 -0
- data/lib/lattice/grid/collab/doc_model_normalize.rb +120 -0
- data/lib/lattice/grid/collab/doc_server.rb +84 -0
- data/lib/lattice/grid/collab/doc_server_messages.rb +129 -0
- data/lib/lattice/grid/collab/iron_calc.rb +84 -0
- data/lib/lattice/grid/collab/iron_calc_ops.rb +118 -0
- data/lib/lattice/grid/collab/iron_calc_sheets.rb +69 -0
- data/lib/lattice/grid/collab/memory_store.rb +63 -0
- data/lib/lattice/grid/collab/refs.rb +131 -0
- data/lib/lattice/grid/collab/refs_shrink.rb +56 -0
- data/lib/lattice/grid/collab/wasm_book.rb +97 -0
- data/lib/lattice/grid/collab/wire.rb +135 -0
- data/lib/lattice/grid/collab/wire_model_ops.rb +33 -0
- data/lib/lattice/grid/collab/wire_ops.rb +120 -0
- data/lib/lattice/grid/collab/wire_style.rb +60 -0
- data/lib/lattice/grid/collab.rb +44 -0
- data/lib/lattice/grid/context.rb +82 -0
- data/lib/lattice/grid/dates.rb +76 -0
- data/lib/lattice/grid/filter_sql.rb +87 -0
- data/lib/lattice/grid/json.rb +60 -0
- data/lib/lattice/grid/keyset.rb +49 -0
- data/lib/lattice/grid/kind.rb +58 -0
- data/lib/lattice/grid/levels.rb +121 -0
- data/lib/lattice/grid/limits.rb +51 -0
- data/lib/lattice/grid/pager.rb +118 -0
- data/lib/lattice/grid/parser/filter_rules.rb +119 -0
- data/lib/lattice/grid/parser/group_rules.rb +105 -0
- data/lib/lattice/grid/parser/request_rules.rb +131 -0
- data/lib/lattice/grid/parser/tree_rules.rb +68 -0
- data/lib/lattice/grid/parser.rb +126 -0
- data/lib/lattice/grid/pivot_sql.rb +78 -0
- data/lib/lattice/grid/query.rb +76 -0
- data/lib/lattice/grid/request.rb +19 -0
- data/lib/lattice/grid/respond.rb +60 -0
- data/lib/lattice/grid/sql.rb +103 -0
- data/lib/lattice/grid/tree_sql.rb +59 -0
- data/lib/lattice/grid/values.rb +124 -0
- data/lib/lattice/grid/version.rb +7 -0
- data/lib/lattice/grid.rb +29 -0
- data/wasm/ironcalc_wasi.wasm +0 -0
- metadata +110 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
module Collab
|
|
6
|
+
# A port of `parseClientMessage` in packages/server/src/collab/wire.ts: the trust boundary
|
|
7
|
+
# of the collab server. It never raises. It gives a fresh copy with String keys that holds
|
|
8
|
+
# only the known keys. The codes, the limits and the messages are the TypeScript ones.
|
|
9
|
+
class Wire
|
|
10
|
+
# The result of a parse: a message, or an error text.
|
|
11
|
+
Result = Data.define(:message, :error) do
|
|
12
|
+
def ok? = error.nil?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# A bad message. The parser stops at the first one, as the TypeScript parser does.
|
|
16
|
+
class Bad < StandardError; end
|
|
17
|
+
|
|
18
|
+
ID = /\A[A-Za-z0-9_.-]+\z/
|
|
19
|
+
ID_TEXT = "/^[\\w.-]+$/"
|
|
20
|
+
SHEET_NAME = %r{\A[^\[\]:*?/\\']{1,31}\z}
|
|
21
|
+
NAME = /\A[A-Za-z_\\][A-Za-z0-9_.]{0,254}\z/
|
|
22
|
+
ERRORS = %w[#NULL! #DIV/0! #VALUE! #REF! #NAME? #NUM! #N/A].freeze
|
|
23
|
+
STYLE_FLAGS = %w[bold italic underline strike wrap].freeze
|
|
24
|
+
ALIGN = %w[left center right].freeze
|
|
25
|
+
VALIGN = %w[top middle bottom].freeze
|
|
26
|
+
SIDES = %w[top right bottom left].freeze
|
|
27
|
+
MAX_SAFE = (2**53) - 1
|
|
28
|
+
|
|
29
|
+
def self.parse(input)
|
|
30
|
+
Result.new(message: new.message(input), error: nil)
|
|
31
|
+
rescue Bad => e
|
|
32
|
+
Result.new(message: nil, error: e.message)
|
|
33
|
+
rescue StandardError
|
|
34
|
+
# A deep value can raise something else; it is still a bad message.
|
|
35
|
+
Result.new(message: nil, error: "the message is not valid")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def message(input)
|
|
39
|
+
fail!("the message must be an object") unless input.is_a?(Hash)
|
|
40
|
+
fail!("v must be #{VERSION}") unless input["v"] == VERSION && input["v"].is_a?(Numeric)
|
|
41
|
+
case input["type"]
|
|
42
|
+
when "join" then join(input)
|
|
43
|
+
when "submit" then submit(input)
|
|
44
|
+
when "presence" then presence(input)
|
|
45
|
+
else fail!("type is not a known message type")
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def join(input)
|
|
52
|
+
o = obj(input, "join", %w[v type docId clientId sinceSeq], %w[name])
|
|
53
|
+
out = { "v" => 1, "type" => "join" }.merge(ids_of(o, %w[docId clientId]),
|
|
54
|
+
"sinceSeq" => nullable(o["sinceSeq"]) { |x| seq(x, "sinceSeq") })
|
|
55
|
+
out["name"] = short(o["name"], "name") if o.key?("name")
|
|
56
|
+
out
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def submit(input)
|
|
60
|
+
o = obj(input, "submit", %w[v type docId clientId opId baseSeq ops])
|
|
61
|
+
ops = op_list(o["ops"])
|
|
62
|
+
{ "v" => 1, "type" => "submit" }.merge(ids_of(o, %w[docId clientId opId]),
|
|
63
|
+
"baseSeq" => seq(o["baseSeq"], "baseSeq"),
|
|
64
|
+
"ops" => ops.each_with_index.map { |x, i| op(x, "ops[#{i}]") })
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def op_list(input)
|
|
68
|
+
fail!("ops must be a list") unless input.is_a?(Array)
|
|
69
|
+
fail!("ops has more than #{LIMITS[:maxOps]} items") if input.size > LIMITS[:maxOps]
|
|
70
|
+
input
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def ids_of(input, keys) = keys.to_h { |k| [k, id(input[k], k)] }
|
|
74
|
+
|
|
75
|
+
def presence(input)
|
|
76
|
+
o = obj(input, "presence", %w[v type docId cursor selection editing])
|
|
77
|
+
{ "v" => 1, "type" => "presence", "docId" => id(o["docId"], "docId"),
|
|
78
|
+
"cursor" => nullable(o["cursor"]) { |x| cell(x, "cursor") },
|
|
79
|
+
"selection" => nullable(o["selection"]) { |x| range(x, "selection") },
|
|
80
|
+
"editing" => nullable(o["editing"]) { |x| cell(x, "editing") } }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def fail!(message)
|
|
84
|
+
raise Bad, message
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def nullable(input)
|
|
88
|
+
input.nil? ? nil : yield(input)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# A Hash with only the keys in `required` and `optional`.
|
|
92
|
+
def obj(input, path, required, optional = [])
|
|
93
|
+
fail!("#{path} must be an object") unless input.is_a?(Hash)
|
|
94
|
+
known = required + optional
|
|
95
|
+
input.each_key { |k| fail!("#{path}.#{k} is not known") unless known.include?(k) }
|
|
96
|
+
required.each { |k| fail!("#{path}.#{k} is missing") unless input.key?(k) }
|
|
97
|
+
input
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def text(input, path, max)
|
|
101
|
+
fail!("#{path} must be a string") unless input.is_a?(String)
|
|
102
|
+
fail!("#{path} has more than #{max} characters") if units(input) > max
|
|
103
|
+
fail!("#{path} has U+0000 or a lone surrogate") if !input.valid_encoding? || input.include?("\u0000")
|
|
104
|
+
input.dup
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The length in UTF-16 code units, as JavaScript counts it.
|
|
108
|
+
def units(input)
|
|
109
|
+
input.valid_encoding? ? input.encode("UTF-16LE").bytesize / 2 : input.length
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def short(input, path) = text(input, path, LIMITS[:maxShort])
|
|
113
|
+
|
|
114
|
+
def id(input, path)
|
|
115
|
+
s = text(input, path, LIMITS[:maxId])
|
|
116
|
+
fail!("#{path} must match #{ID_TEXT}") unless ID.match?(s)
|
|
117
|
+
s
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def ids(input, path)
|
|
121
|
+
fail!("#{path} must be a list") unless input.is_a?(Array)
|
|
122
|
+
fail!("#{path} must have 1 to #{LIMITS[:maxIds]} IDs") if input.empty? || input.size > LIMITS[:maxIds]
|
|
123
|
+
input.each_with_index.map { |x, i| id(x, "#{path}[#{i}]") }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# A safe integer >= 0. A whole Float is an integer, as in JSON.
|
|
127
|
+
def seq(input, path)
|
|
128
|
+
ok = input.is_a?(Numeric) && !input.is_a?(Complex) && input.finite? && input == input.to_i
|
|
129
|
+
fail!("#{path} must be an integer >= 0") unless ok && input >= 0 && input <= MAX_SAFE
|
|
130
|
+
input.to_i
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
module Collab
|
|
6
|
+
# The rules of the model ops of the wire parser: notes and frozen panes. The engine does not
|
|
7
|
+
# get these ops.
|
|
8
|
+
class Wire
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def set_note(input, path)
|
|
12
|
+
o = obj(input, path, %w[type sheet row col note])
|
|
13
|
+
note = nullable(o["note"]) { |x| text(x, "#{path}.note", LIMITS[:maxText]) }
|
|
14
|
+
{ "type" => "setNote" }.merge(cell(o.slice("sheet", "row", "col"), path), "note" => note)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def freeze_op(input, path)
|
|
18
|
+
o = obj(input, path, %w[type sheet rows cols])
|
|
19
|
+
{ "type" => "freeze", "sheet" => id(o["sheet"], "#{path}.sheet"),
|
|
20
|
+
"rows" => count(o["rows"], "#{path}.rows"), "cols" => count(o["cols"], "#{path}.cols") }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# An integer from 0 to maxIds. A whole Float is an integer, as in JSON.
|
|
24
|
+
def count(input, path)
|
|
25
|
+
ok = input.is_a?(Numeric) && !input.is_a?(Complex) && input.finite? && input == input.to_i
|
|
26
|
+
return input.to_i if ok && input.between?(0, LIMITS[:maxIds])
|
|
27
|
+
|
|
28
|
+
fail!("#{path} must be an integer from 0 to #{LIMITS[:maxIds]}")
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
module Collab
|
|
6
|
+
# The op and value rules of the wire parser.
|
|
7
|
+
class Wire
|
|
8
|
+
OPS = {
|
|
9
|
+
"setCell" => :set_cell, "insertRows" => :insert, "insertCols" => :insert,
|
|
10
|
+
"deleteRows" => :delete, "deleteCols" => :delete, "setFormat" => :set_format,
|
|
11
|
+
"setStyle" => :set_style, "merge" => :merge, "unmerge" => :merge, "setName" => :set_name,
|
|
12
|
+
"addSheet" => :add_sheet, "removeSheet" => :remove_sheet, "renameSheet" => :rename_sheet,
|
|
13
|
+
"setNote" => :set_note, "freeze" => :freeze_op
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def op(input, path)
|
|
19
|
+
fail!("#{path} must be an object") unless input.is_a?(Hash)
|
|
20
|
+
type = input["type"]
|
|
21
|
+
fail!("#{path}.type is not a known op") unless type.is_a?(String) && OPS.key?(type)
|
|
22
|
+
send(OPS.fetch(type), input, path)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def set_cell(input, path)
|
|
26
|
+
o = obj(input, path, %w[type sheet row col], %w[value formula])
|
|
27
|
+
out = { "type" => "setCell" }.merge(cell(o.slice("sheet", "row", "col"), path))
|
|
28
|
+
out["value"] = value(o["value"], "#{path}.value") if o.key?("value")
|
|
29
|
+
out["formula"] = formula(o["formula"], "#{path}.formula") if o.key?("formula")
|
|
30
|
+
out
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def formula(input, path)
|
|
34
|
+
f = text(input, path, LIMITS[:maxText])
|
|
35
|
+
fail!("#{path} must start with =") unless f.start_with?("=") && f.length >= 2
|
|
36
|
+
f
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def insert(input, path)
|
|
40
|
+
o = obj(input, path, %w[type sheet ids before])
|
|
41
|
+
{ "type" => o["type"], "sheet" => id(o["sheet"], "#{path}.sheet"), "ids" => ids(o["ids"], "#{path}.ids"),
|
|
42
|
+
"before" => nullable(o["before"]) { |x| id(x, "#{path}.before") } }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def delete(input, path)
|
|
46
|
+
o = obj(input, path, %w[type sheet ids])
|
|
47
|
+
{ "type" => o["type"], "sheet" => id(o["sheet"], "#{path}.sheet"), "ids" => ids(o["ids"], "#{path}.ids") }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def set_format(input, path)
|
|
51
|
+
o = obj(input, path, %w[type range format])
|
|
52
|
+
format = nullable(o["format"]) { |x| short(x, "#{path}.format") }
|
|
53
|
+
{ "type" => "setFormat", "range" => range(o["range"], "#{path}.range"), "format" => format }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def set_style(input, path)
|
|
57
|
+
o = obj(input, path, %w[type range style])
|
|
58
|
+
style = nullable(o["style"]) { |x| style(x, "#{path}.style") }
|
|
59
|
+
{ "type" => "setStyle", "range" => range(o["range"], "#{path}.range"), "style" => style }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def merge(input, path)
|
|
63
|
+
o = obj(input, path, %w[type range])
|
|
64
|
+
{ "type" => o["type"], "range" => range(o["range"], "#{path}.range") }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def set_name(input, path)
|
|
68
|
+
o = obj(input, path, %w[type name ref])
|
|
69
|
+
name = text(o["name"], "#{path}.name", 255)
|
|
70
|
+
fail!("#{path}.name is not a valid name") unless NAME.match?(name)
|
|
71
|
+
{ "type" => "setName", "name" => name,
|
|
72
|
+
"ref" => nullable(o["ref"]) { |x| text(x, "#{path}.ref", LIMITS[:maxText]) } }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def add_sheet(input, path)
|
|
76
|
+
o = obj(input, path, %w[type id name rows cols])
|
|
77
|
+
{ "type" => "addSheet", "id" => id(o["id"], "#{path}.id"), "name" => sheet_name(o["name"], "#{path}.name"),
|
|
78
|
+
"rows" => ids(o["rows"], "#{path}.rows"), "cols" => ids(o["cols"], "#{path}.cols") }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def remove_sheet(input, path)
|
|
82
|
+
o = obj(input, path, %w[type id])
|
|
83
|
+
{ "type" => "removeSheet", "id" => id(o["id"], "#{path}.id") }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def rename_sheet(input, path)
|
|
87
|
+
o = obj(input, path, %w[type id name])
|
|
88
|
+
{ "type" => "renameSheet", "id" => id(o["id"], "#{path}.id"),
|
|
89
|
+
"name" => sheet_name(o["name"], "#{path}.name") }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def sheet_name(input, path)
|
|
93
|
+
s = text(input, path, 31)
|
|
94
|
+
fail!("#{path} is not a valid sheet name") unless SHEET_NAME.match?(s)
|
|
95
|
+
s
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def value(input, path)
|
|
99
|
+
case input
|
|
100
|
+
when nil, true, false then input
|
|
101
|
+
when Integer, Float then finite(input, path)
|
|
102
|
+
when String then text(input, path, LIMITS[:maxText])
|
|
103
|
+
else error_value(input, path)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def error_value(input, path)
|
|
108
|
+
o = obj(input, path, %w[error])
|
|
109
|
+
fail!("#{path}.error is not known") unless ERRORS.include?(o["error"])
|
|
110
|
+
{ "error" => o["error"] }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def finite(input, path)
|
|
114
|
+
fail!("#{path} must be finite") unless input.finite?
|
|
115
|
+
input
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
module Collab
|
|
6
|
+
# The cell style and cell position rules of the wire parser.
|
|
7
|
+
class Wire
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def style(input, path)
|
|
11
|
+
o = obj(input, path, [], STYLE_FLAGS + %w[color fill align valign border])
|
|
12
|
+
out = flags(o, path).merge(choices(o, path))
|
|
13
|
+
out["border"] = border(o["border"], "#{path}.border") if o.key?("border")
|
|
14
|
+
out
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# The text keys and the keys with a list of known values, in the TypeScript order.
|
|
18
|
+
def choices(input, path)
|
|
19
|
+
known = { "color" => nil, "fill" => nil, "align" => ALIGN, "valign" => VALIGN }
|
|
20
|
+
known = known.select { |k, _| input.key?(k) }
|
|
21
|
+
known.to_h { |k, list| [k, list ? one_of(input, k, list, path) : short(input[k], "#{path}.#{k}")] }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def flags(input, path)
|
|
25
|
+
STYLE_FLAGS.select { |flag| input.key?(flag) }.to_h do |flag|
|
|
26
|
+
fail!("#{path}.#{flag} must be a boolean") unless [true, false].include?(input[flag])
|
|
27
|
+
|
|
28
|
+
[flag, input[flag]]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def one_of(input, key, known, path)
|
|
33
|
+
fail!("#{path}.#{key} is not known") unless known.include?(input[key])
|
|
34
|
+
input[key]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def border(input, path)
|
|
38
|
+
obj(input, path, [], SIDES).to_h { |side, v| [side, short(v, "#{path}.#{side}")] }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def cell(input, path)
|
|
42
|
+
o = obj(input, path, %w[sheet row col])
|
|
43
|
+
{ "sheet" => id(o["sheet"], "#{path}.sheet"), "row" => id(o["row"], "#{path}.row"),
|
|
44
|
+
"col" => id(o["col"], "#{path}.col") }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def corner(input, path)
|
|
48
|
+
o = obj(input, path, %w[row col])
|
|
49
|
+
{ "row" => id(o["row"], "#{path}.row"), "col" => id(o["col"], "#{path}.col") }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def range(input, path)
|
|
53
|
+
o = obj(input, path, %w[sheet start end])
|
|
54
|
+
{ "sheet" => id(o["sheet"], "#{path}.sheet"), "start" => corner(o["start"], "#{path}.start"),
|
|
55
|
+
"end" => corner(o["end"], "#{path}.end") }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "monitor"
|
|
5
|
+
|
|
6
|
+
module Lattice
|
|
7
|
+
module Grid
|
|
8
|
+
# The collab op log server (ADR 0016): a port of packages/server/src/collab. Load it with
|
|
9
|
+
# `require "lattice/grid/collab"`. The formula engine needs the `wasmtime` gem.
|
|
10
|
+
module Collab
|
|
11
|
+
VERSION = 1
|
|
12
|
+
|
|
13
|
+
# The same limits as COLLAB_LIMITS in packages/server/src/collab/wire.ts.
|
|
14
|
+
LIMITS = {
|
|
15
|
+
maxOps: 1000,
|
|
16
|
+
maxIds: 10_000,
|
|
17
|
+
maxText: 32_767,
|
|
18
|
+
maxShort: 256,
|
|
19
|
+
maxId: 64
|
|
20
|
+
}.freeze
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
require_relative "collab/wire"
|
|
26
|
+
require_relative "collab/wire_ops"
|
|
27
|
+
require_relative "collab/wire_model_ops"
|
|
28
|
+
require_relative "collab/wire_style"
|
|
29
|
+
require_relative "collab/refs"
|
|
30
|
+
require_relative "collab/refs_shrink"
|
|
31
|
+
require_relative "collab/wasm_book"
|
|
32
|
+
require_relative "collab/iron_calc"
|
|
33
|
+
require_relative "collab/iron_calc_ops"
|
|
34
|
+
require_relative "collab/iron_calc_sheets"
|
|
35
|
+
require_relative "collab/axis"
|
|
36
|
+
require_relative "collab/doc_model"
|
|
37
|
+
require_relative "collab/doc_model_normalize"
|
|
38
|
+
require_relative "collab/doc_model_apply"
|
|
39
|
+
require_relative "collab/doc_model_doc"
|
|
40
|
+
require_relative "collab/memory_store"
|
|
41
|
+
require_relative "collab/connection"
|
|
42
|
+
require_relative "collab/doc_server"
|
|
43
|
+
require_relative "collab/doc_server_messages"
|
|
44
|
+
require_relative "collab/channel_handler"
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
# The state of one request: the model, the allow-list, the SQL and cell helpers, and the request.
|
|
6
|
+
class Context
|
|
7
|
+
attr_reader :model, :columns, :key, :sql, :cells, :request
|
|
8
|
+
|
|
9
|
+
def initialize(scope, columns, request)
|
|
10
|
+
@scope = scope
|
|
11
|
+
@model = scope.klass
|
|
12
|
+
@columns = columns
|
|
13
|
+
@key = columns.find(&:key)&.name
|
|
14
|
+
@sql = Sql.new(@model, columns)
|
|
15
|
+
@cells = Cells.new(@model, columns)
|
|
16
|
+
@request = request
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The scope of the request: the filter, the quick search, the group keys and the tree level.
|
|
20
|
+
def base
|
|
21
|
+
@base ||= with_tree(with_keys(matched))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The caller scope with the filter and the quick search.
|
|
25
|
+
def matched
|
|
26
|
+
@matched ||= with_search(with_filter(caller_scope))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The caller's conditions stay; the order, the select, the limit and the offset go.
|
|
30
|
+
def caller_scope
|
|
31
|
+
@scope.unscope(:order, :select, :limit, :offset)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def tree
|
|
35
|
+
@tree ||= request.tree && TreeSql.new(self)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The leaf order: the request sort, then the key.
|
|
39
|
+
def leaf_keys
|
|
40
|
+
keys = request.sort.map { |s| [s.column, s.dir, s.nulls || "last"] }
|
|
41
|
+
key ? keys + [[key, "asc", "last"]] : keys
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def group_level?
|
|
45
|
+
request.group.keys.size < request.group.by.size
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def exists?(cond)
|
|
49
|
+
base.where(cond).exists?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def select_all(relation)
|
|
53
|
+
return model.connection.select_all(relation, "Lattice").to_a unless model.respond_to?(:with_connection)
|
|
54
|
+
|
|
55
|
+
model.with_connection { |c| c.select_all(relation, "Lattice").to_a }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def with_filter(rel)
|
|
61
|
+
request.filter ? rel.where(FilterSql.new(sql).filter(request.filter)) : rel
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def with_search(rel)
|
|
65
|
+
term = request.search
|
|
66
|
+
return rel if term.nil? || term.empty?
|
|
67
|
+
|
|
68
|
+
names = columns.select { |c| c.search && c.type == "string" }.map(&:name)
|
|
69
|
+
rel.where(FilterSql.new(sql).search(term, names))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def with_tree(rel)
|
|
73
|
+
tree ? rel.where(tree.level) : rel
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def with_keys(rel)
|
|
77
|
+
by = request.group.by
|
|
78
|
+
request.group.keys.each_with_index.reduce(rel) { |r, (k, i)| r.where(sql.equal(by[i], k)) }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
5
|
+
module Lattice
|
|
6
|
+
module Grid
|
|
7
|
+
# ISO 8601 dates. Each field must exist on the (proleptic Gregorian) calendar, and the written year
|
|
8
|
+
# is 0001 to 9999. Hour 24 is only the end of the day. A date-time without a zone is UTC.
|
|
9
|
+
module Dates
|
|
10
|
+
ISO_DATE = /\A(?!0000)(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
|
|
11
|
+
(?:T(?<hour>\d{2}):(?<min>\d{2})(?::(?<sec>\d{2})(?:\.(?<frac>\d{1,9}))?)?
|
|
12
|
+
(?:Z|(?<sign>[+-])(?<oh>\d{2}):(?<om>\d{2}))?)?\z/x
|
|
13
|
+
NUMBERS = %i[year month day hour min sec oh om].freeze
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def valid?(value)
|
|
18
|
+
!fields(value).nil?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The UTC instant of a date string, or nil. It never raises.
|
|
22
|
+
def instant(value)
|
|
23
|
+
f = fields(value)
|
|
24
|
+
return unless f
|
|
25
|
+
|
|
26
|
+
Time.utc(f[:year], f[:month], f[:day]) + seconds(f) - offset(f)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def seconds(fields)
|
|
30
|
+
(fields[:hour] * 3600) + (fields[:min] * 60) + fields[:sec] + fields[:frac]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The instant as text that every database reads: `2020-01-01 10:00:00.000000`.
|
|
34
|
+
def utc_text(value)
|
|
35
|
+
instant(value)&.strftime("%Y-%m-%d %H:%M:%S.%6N")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The fields of a valid date string, or nil.
|
|
39
|
+
def fields(value)
|
|
40
|
+
match = date_match(value)
|
|
41
|
+
f = match && numbers(match)
|
|
42
|
+
f if f && calendar?(f) && clock?(f) && zone?(f)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def date_match(value)
|
|
46
|
+
ISO_DATE.match(value) if Kind.string?(value) && value.valid_encoding?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def numbers(match)
|
|
50
|
+
NUMBERS.to_h { |name| [name, match[name].to_i] }.merge(frac: "0.#{match[:frac]}".to_r, sign: match[:sign])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def calendar?(fields)
|
|
54
|
+
Date.valid_date?(fields[:year], fields[:month], fields[:day], Date::GREGORIAN)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def clock?(fields)
|
|
58
|
+
fields[:min] <= 59 && fields[:sec] <= 59 && (fields[:hour] <= 23 || end_of_day?(fields))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def end_of_day?(fields)
|
|
62
|
+
fields[:hour] == 24 && fields[:min].zero? && fields[:sec].zero? && fields[:frac].zero?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def zone?(fields)
|
|
66
|
+
fields[:oh] <= 23 && fields[:om] <= 59
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def offset(fields)
|
|
70
|
+
return 0 unless fields[:sign]
|
|
71
|
+
|
|
72
|
+
(fields[:sign] == "-" ? -1 : 1) * ((fields[:oh] * 3600) + (fields[:om] * 60))
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lattice
|
|
4
|
+
module Grid
|
|
5
|
+
# The filter tree and the quick search as Arel nodes. SQL three-valued logic gives the protocol rules
|
|
6
|
+
# for null: a comparison with null is unknown, and only a true result keeps a row.
|
|
7
|
+
class FilterSql
|
|
8
|
+
COMPARE = { "eq" => :eq, "ne" => :not_eq, "lt" => :lt, "lte" => :lteq, "gt" => :gt, "gte" => :gteq }.freeze
|
|
9
|
+
LISTS = { "in" => :in, "notIn" => :not_in }.freeze
|
|
10
|
+
LIKE = { "contains" => "%%%s%%", "startsWith" => "%s%%", "endsWith" => "%%%s" }.freeze
|
|
11
|
+
|
|
12
|
+
def initialize(sql)
|
|
13
|
+
@sql = sql
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def filter(node)
|
|
17
|
+
case node
|
|
18
|
+
when Logic then logic(node)
|
|
19
|
+
when Not then Arel::Nodes::Not.new(Arel::Nodes::Grouping.new(filter(node.node)))
|
|
20
|
+
else leaf(node)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# A case-insensitive substring match on the search columns.
|
|
25
|
+
def search(term, names)
|
|
26
|
+
names.map { |n| like(n, "contains", term) }.reduce { |a, b| a.or(b) } || Sql::NEVER
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def logic(node)
|
|
32
|
+
parts = node.nodes.map { |n| filter(n) }
|
|
33
|
+
return Arel::Nodes::Grouping.new(Arel::Nodes::And.new(parts)) if node.op == "and"
|
|
34
|
+
|
|
35
|
+
parts.reduce { |a, b| a.or(b) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def leaf(node)
|
|
39
|
+
op = node.op
|
|
40
|
+
return compare(node, COMPARE[op]) if COMPARE.key?(op)
|
|
41
|
+
return list(node, LISTS[op]) if LISTS.key?(op)
|
|
42
|
+
return like(node.column, op, node.value) if LIKE.key?(op)
|
|
43
|
+
|
|
44
|
+
blank_or_range(node)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def blank_or_range(node)
|
|
48
|
+
case node.op
|
|
49
|
+
when "blank" then blank(node.column)
|
|
50
|
+
when "notBlank" then Arel::Nodes::Not.new(blank(node.column))
|
|
51
|
+
else range(node)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def compare(node, method)
|
|
56
|
+
@sql.ordered(node.column).public_send(method, @sql.value(node.column, node.value))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def list(node, method)
|
|
60
|
+
@sql.ordered(node.column).public_send(method, node.value.map { |v| @sql.value(node.column, v) })
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# `between` includes both ends; `inRange` excludes the end.
|
|
64
|
+
def range(node)
|
|
65
|
+
column = @sql.ordered(node.column)
|
|
66
|
+
low, high = node.value.map { |v| @sql.value(node.column, v) }
|
|
67
|
+
upper = node.op == "between" ? column.lteq(high) : column.lt(high)
|
|
68
|
+
Arel::Nodes::Grouping.new(Arel::Nodes::And.new([column.gteq(low), upper]))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# On a string column, blank is null or the empty string. On other columns, it is null.
|
|
72
|
+
def blank(name)
|
|
73
|
+
column = @sql.table[name]
|
|
74
|
+
return Arel::Nodes::Grouping.new(column.eq(nil)) unless @sql.type(name) == "string"
|
|
75
|
+
|
|
76
|
+
column.eq(nil).or(column.eq(@sql.bind("", Sql::STRING)))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# LOWER(column) LIKE LOWER(pattern) ESCAPE '\'. `%`, `_` and `\` in the value are literal characters.
|
|
80
|
+
def like(name, operator, text)
|
|
81
|
+
pattern = format(LIKE.fetch(operator), text.gsub(/[\\%_]/) { |c| "\\#{c}" })
|
|
82
|
+
lower = @sql.function("LOWER", @sql.table[name])
|
|
83
|
+
lower.matches(@sql.function("LOWER", @sql.bind(pattern, Sql::STRING)), "\\", true)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|