Skip to content

Commit baa52c4

Browse files
committed
Add validation class and bun platform config
1 parent 4cb432a commit baa52c4

5 files changed

Lines changed: 144 additions & 19 deletions

File tree

gem/Gemfile.lock

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ PATH
22
remote: .
33
specs:
44
triangulum (0.0.0)
5+
base64 (~> 0.3)
6+
json (~> 2.19)
7+
open3 (~> 0.2)
8+
tucana (~> 0.0, >= 0.0.62)
59

610
GEM
711
remote: https://rubygems.org/
812
specs:
913
addressable (2.8.9)
1014
public_suffix (>= 2.0.2, < 8.0)
1115
ast (2.4.3)
16+
base64 (0.3.0)
1217
bigdecimal (4.0.1)
1318
date (3.5.1)
1419
diff-lcs (1.6.2)
1520
erb (6.0.2)
21+
google-protobuf (4.34.1-x86_64-linux-gnu)
22+
bigdecimal
23+
rake (~> 13.3)
24+
googleapis-common-protos-types (1.22.0)
25+
google-protobuf (~> 4.26)
26+
grpc (1.78.1-x86_64-linux-gnu)
27+
google-protobuf (>= 3.25, < 5.0)
28+
googleapis-common-protos-types (~> 1.0)
1629
io-console (0.8.2)
1730
irb (1.17.0)
1831
pp (>= 0.6.0)
@@ -27,6 +40,7 @@ GEM
2740
lint_roller (1.1.0)
2841
mcp (0.8.0)
2942
json-schema (>= 4.1)
43+
open3 (0.2.1)
3044
parallel (1.27.0)
3145
parser (3.3.10.2)
3246
ast (~> 2.4.1)
@@ -84,23 +98,26 @@ GEM
8498
lint_roller (~> 1.1)
8599
rubocop (~> 1.72, >= 1.72.1)
86100
ruby-progressbar (1.13.0)
101+
rubyzip (2.4.1)
87102
stringio (3.2.0)
88103
tsort (0.2.0)
104+
tucana (0.0.62)
105+
grpc (~> 1.64)
89106
unicode-display_width (3.2.0)
90107
unicode-emoji (~> 4.1)
91108
unicode-emoji (4.2.0)
92109

93110
PLATFORMS
94-
ruby
95111
x86_64-linux
96112

97113
DEPENDENCIES
98-
irb
114+
irb (~> 1.17)
99115
rake (~> 13.0)
100116
rspec (~> 3.0)
101117
rubocop (~> 1.21)
102118
rubocop-rake (~> 0.7.0)
103119
rubocop-rspec (~> 3.0)
120+
rubyzip (~> 2.3)
104121
triangulum!
105122

106123
BUNDLED WITH

gem/lib/triangulum.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# frozen_string_literal: true
22

3-
require_relative 'triangulum/version'
3+
require 'base64'
4+
require 'json'
5+
require 'open3'
46

57
module Triangulum
68
class Error < StandardError; end
7-
# Your code goes here...
89
end
10+
11+
require_relative 'triangulum/version'
12+
require_relative 'triangulum/validation'

gem/lib/triangulum/bun.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module Triangulum
4+
# @!visibility private
5+
module Bun
6+
VERSION = 'v1.3.11'
7+
8+
# rubocop:disable Layout/LineLength
9+
# rubygems platform name => [bun release zip filename, sha256 checksum]
10+
NATIVE_PLATFORMS = {
11+
'arm64-darwin' => %w[bun-darwin-aarch64.zip 6f5a3467ed9caec4795bf78cd476507d9f870c7d57b86c945fcb338126772ffc],
12+
'x86_64-darwin' => %w[bun-darwin-x64.zip c4fe2b9247218b0295f24e895aaec8fee62e74452679a9026b67eacbd611a286],
13+
'x86_64-linux-gnu' => %w[bun-linux-x64.zip 8611ba935af886f05a6f38740a15160326c15e5d5d07adef966130b4493607ed],
14+
'x86_64-linux-musl' => %w[bun-linux-x64-musl.zip b0fce3bc4fab52f26a1e0d8886dc07fd0c0eb2a274cb343b59c83a2d5997b5b1],
15+
'aarch64-linux-gnu' => %w[bun-linux-aarch64.zip d13944da12a53ecc74bf6a720bd1d04c4555c038dfe422365356a7be47691fdf],
16+
'aarch64-linux-musl' => %w[bun-linux-aarch64-musl.zip 0f5bf5dc3f276053196274bb84f90a44e2fa40c9432bd6757e3247a8d9476a3d]
17+
}.freeze
18+
# rubocop:enable Layout/LineLength
19+
20+
def self.download_url(filename)
21+
"https://github.com/oven-sh/bun/releases/download/bun-#{VERSION}/#{filename}"
22+
end
23+
end
24+
end

gem/lib/triangulum/validation.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# frozen_string_literal: true
2+
3+
module Triangulum
4+
# == Triangulum::Validation
5+
# This class implements the validation using the typescript package
6+
class Validation
7+
class TriangulumFailed < Triangulum::Error
8+
end
9+
10+
Result = Struct.new(:valid?, :return_type, :diagnostics)
11+
Diagnostic = Struct.new(:message, :code, :severity, :node_id, :parameter_index)
12+
13+
ENTRYPOINT = File.expand_path('js/single-validation.js', __dir__)
14+
BUN_EXE = Dir.glob(File.expand_path('../../exe/*/bun', __dir__)).find do |path|
15+
platform = Gem::Platform.new(File.basename(File.dirname(path)))
16+
Gem::Platform.match_gem?(platform, Gem::Platform.local.to_s)
17+
end
18+
19+
attr_reader :flow, :runtime_function_definitions, :data_types
20+
21+
def initialize(flow, runtime_function_definitions, data_types)
22+
@flow = flow
23+
@runtime_function_definitions = runtime_function_definitions
24+
@data_types = data_types
25+
end
26+
27+
def validate
28+
input = serialize_input
29+
30+
output = run_ts_triangulum(input)
31+
32+
parse_output(output)
33+
end
34+
35+
private
36+
37+
def run_ts_triangulum(input)
38+
stdout_s, stderr_s, status = Open3.capture3(
39+
BUN_EXE, 'run', ENTRYPOINT,
40+
stdin_data: input
41+
)
42+
43+
raise TriangulumFailed, "OUT:\n#{stdout_s}\n\nERR:\n#{stderr_s}" unless status.success?
44+
45+
stdout_s
46+
end
47+
48+
def serialize_input
49+
input = []
50+
51+
input << Base64.strict_encode64(flow.to_proto)
52+
input << ''
53+
54+
runtime_function_definitions.each do |rfd|
55+
input << Base64.strict_encode64(rfd.to_proto)
56+
end
57+
58+
input << ''
59+
60+
data_types.each do |dt|
61+
input << Base64.strict_encode64(dt.to_proto)
62+
end
63+
64+
input << ''
65+
66+
input.join("\n")
67+
end
68+
69+
def parse_output(output)
70+
json = JSON.parse(output, symbolize_names: true)
71+
72+
Result.new(
73+
json[:isValid],
74+
json[:returnType],
75+
json[:diagnostics].map do |diagnostic|
76+
Diagnostic.new(
77+
diagnostic[:message],
78+
diagnostic[:code],
79+
diagnostic[:severity],
80+
diagnostic[:nodeId],
81+
diagnostic[:parameterIndex]
82+
)
83+
end
84+
)
85+
end
86+
end
87+
end

gem/triangulum.gemspec

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,30 @@ Gem::Specification.new do |spec|
77
spec.version = Triangulum::VERSION
88
spec.authors = ['Niklas van Schrick']
99
spec.email = ['mc.taucher2003@gmail.com']
10+
spec.license = 'MIT'
1011

1112
spec.summary = 'Triangulum is the CodeZero validation layer'
1213
spec.homepage = 'https://github.com/code0-tech/triangulum'
1314
spec.required_ruby_version = '>= 3.1.0'
1415

1516
spec.metadata['homepage_uri'] = spec.homepage
16-
spec.metadata['source_code_uri'] = spec.homepage
1717
spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"
1818
spec.metadata['rubygems_mfa_required'] = 'true'
1919

2020
# Specify which files should be added to the gem when it is released.
21-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22-
gemspec = File.basename(__FILE__)
23-
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
24-
ls.readlines("\x0", chomp: true).reject do |f|
25-
(f == gemspec) ||
26-
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
27-
end
28-
end
21+
spec.files = Dir.glob('lib/**/*', base: __dir__).select { |f| File.file?(File.join(__dir__, f)) } + ['README.md']
2922
spec.bindir = 'exe'
3023
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
3124
spec.require_paths = ['lib']
3225

33-
# Uncomment to register a new dependency of your gem
34-
# spec.add_dependency "example-gem", "~> 1.0"
26+
spec.add_dependency 'base64', '~> 0.3'
27+
spec.add_dependency 'json', '~> 2.19'
28+
spec.add_dependency 'open3', '~> 0.2'
29+
spec.add_dependency 'tucana', '~> 0.0', '>= 0.0.62'
3530

36-
# For more information and examples about making a new gem, check out our
37-
# guide at: https://bundler.io/guides/creating_gem.html
38-
39-
spec.add_development_dependency 'irb'
31+
spec.add_development_dependency 'irb', '~> 1.17'
4032
spec.add_development_dependency 'rake', '~> 13.0'
33+
spec.add_development_dependency 'rubyzip', '~> 2.3'
4134

4235
spec.add_development_dependency 'rspec', '~> 3.0'
4336

0 commit comments

Comments
 (0)