withruntime 0.28.0 → 1.0.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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/lib/withruntime/generated/apps.rb +192 -0
  3. data/lib/withruntime/generated/audit.rb +78 -0
  4. data/lib/withruntime/generated/browsers.rb +192 -0
  5. data/lib/withruntime/generated/builds.rb +192 -0
  6. data/lib/withruntime/generated/ci.rb +192 -0
  7. data/lib/withruntime/generated/containers.rb +192 -0
  8. data/lib/withruntime/generated/desktops.rb +192 -0
  9. data/lib/withruntime/generated/domains.rb +152 -0
  10. data/lib/withruntime/generated/egress_ips.rb +81 -0
  11. data/lib/withruntime/generated/events.rb +60 -0
  12. data/lib/withruntime/generated/feedback.rb +96 -0
  13. data/lib/withruntime/generated/functions.rb +192 -0
  14. data/lib/withruntime/generated/identities.rb +68 -0
  15. data/lib/withruntime/generated/images.rb +391 -0
  16. data/lib/withruntime/generated/job_runs.rb +110 -0
  17. data/lib/withruntime/generated/jobs.rb +333 -0
  18. data/lib/withruntime/generated/keys.rb +261 -0
  19. data/lib/withruntime/generated/kit.rb +111 -0
  20. data/lib/withruntime/generated/me.rb +69 -0
  21. data/lib/withruntime/generated/networks.rb +72 -0
  22. data/lib/withruntime/generated/notebooks.rb +192 -0
  23. data/lib/withruntime/generated/otel.rb +94 -0
  24. data/lib/withruntime/generated/ports.rb +118 -0
  25. data/lib/withruntime/generated/previews.rb +124 -0
  26. data/lib/withruntime/generated/projects.rb +108 -0
  27. data/lib/withruntime/generated/roles.rb +68 -0
  28. data/lib/withruntime/generated/routes.rb +11 -0
  29. data/lib/withruntime/generated/runtime.rb +188 -0
  30. data/lib/withruntime/generated/sandboxes.rb +1835 -0
  31. data/lib/withruntime/generated/secrets.rb +294 -0
  32. data/lib/withruntime/generated/sessions.rb +143 -0
  33. data/lib/withruntime/generated/sites.rb +192 -0
  34. data/lib/withruntime/generated/snapshots.rb +32 -0
  35. data/lib/withruntime/generated/tunnels.rb +165 -0
  36. data/lib/withruntime/generated/usage.rb +287 -0
  37. data/lib/withruntime/generated/volumes.rb +61 -0
  38. data/lib/withruntime/generated/webhooks.rb +91 -0
  39. data/lib/withruntime/generated/workspaces.rb +192 -0
  40. data/lib/withruntime/signing.rb +162 -0
  41. data/lib/withruntime/version.rb +2 -2
  42. data/lib/withruntime.rb +5 -43
  43. metadata +45 -35
  44. data/lib/withruntime/agents.rb +0 -877
  45. data/lib/withruntime/api_defaults.rb +0 -17
  46. data/lib/withruntime/apps.rb +0 -377
  47. data/lib/withruntime/browsers.rb +0 -104
  48. data/lib/withruntime/builds.rb +0 -218
  49. data/lib/withruntime/ci.rb +0 -164
  50. data/lib/withruntime/client.rb +0 -140
  51. data/lib/withruntime/containers.rb +0 -39
  52. data/lib/withruntime/credentials.rb +0 -111
  53. data/lib/withruntime/fields.rb +0 -40
  54. data/lib/withruntime/file_watch.rb +0 -73
  55. data/lib/withruntime/images.rb +0 -258
  56. data/lib/withruntime/network_products.rb +0 -49
  57. data/lib/withruntime/parity_products.rb +0 -51
  58. data/lib/withruntime/products.rb +0 -560
  59. data/lib/withruntime/record.rb +0 -99
  60. data/lib/withruntime/reservations.rb +0 -76
  61. data/lib/withruntime/sandbox.rb +0 -775
  62. data/lib/withruntime/tar.rb +0 -292
  63. data/lib/withruntime/transport.rb +0 -575
  64. data/lib/withruntime/tunnel.rb +0 -275
  65. data/lib/withruntime/wallet.rb +0 -68
  66. data/lib/withruntime/websocket.rb +0 -384
@@ -1,275 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WithRuntime
4
- # One authenticated WebSocket multiplexes connections to the sandbox's loopback.
5
- class Tunnel
6
- WINDOW = 1 << 20
7
- def initialize(sandbox)
8
- @lock, @credit, @send_lock = Mutex.new, ConditionVariable.new, Mutex.new
9
- @streams, @next, @sent, @credited, @closed = {}, 1, 0, 0, false
10
- @socket = ResumableSocket.new(sandbox.transport, sandbox.path("/tunnel"))
11
- deadline = @socket.now + @socket.timeout
12
- loop do
13
- frame = @socket.read(deadline)
14
- raise Error.new("Tunnel ended before ready.", code: "tunnel_refused") unless frame
15
- next unless frame[0] == 1
16
- reply = JSON.parse(frame[1])
17
- raise Error.from(0, reply, nil) if reply["type"] == "error"
18
- break if reply["type"] == "ready"
19
- end
20
- @reader = Thread.new do
21
- begin
22
- while (frame = @socket.read)
23
- receive(*frame)
24
- end
25
- close
26
- rescue StandardError => error
27
- finish(error)
28
- end
29
- end
30
- rescue StandardError
31
- @socket&.close
32
- raise
33
- end
34
- def connect(port)
35
- raise ArgumentError, "port must be 1 to 65535" unless port.is_a?(Integer) && port.between?(1, 65535)
36
- open_stream("tcp #{port}")
37
- end
38
- def ssh(public_key) = open_stream("ssh #{public_key}")
39
- def open_stream(target)
40
- stream = @lock.synchronize do
41
- raise closed_error if @closed
42
- raise Error.new("Tunnel stream IDs exhausted.", code: "tunnel_exhausted") if @next > 0xffffffff
43
- id = @next
44
- @next += 1
45
- @streams[id] = Stream.new(self, id)
46
- end
47
- begin
48
- control("o", stream.id, target)
49
- stream.wait_open(@socket.timeout)
50
- stream
51
- rescue StandardError
52
- stream.close
53
- raise
54
- end
55
- end
56
- def closed_error = @error || Error.new("The tunnel is closed.", code: "tunnel_closed")
57
- def control(kind, id, data = "")
58
- raise closed_error if @lock.synchronize { @closed }
59
- @socket.send_frame(2, kind.b + [id].pack("N") + data.b)
60
- end
61
- def write(stream, bytes)
62
- @send_lock.synchronize do
63
- (0...bytes.bytesize).step(65536) do |at|
64
- part = bytes.byteslice(at, 65536)
65
- @lock.synchronize do
66
- @credit.wait(@lock) while !@closed && !stream.closed? && @sent + part.bytesize - @credited > WINDOW
67
- raise closed_error if @closed
68
- raise IOError, "Stream closed" if stream.closed?
69
- @sent += part.bytesize
70
- end
71
- control("d", stream.id, part)
72
- end
73
- end
74
- bytes.bytesize
75
- end
76
- def forget(id)
77
- @lock.synchronize { @streams.delete(id); @credit.broadcast }
78
- end
79
- def async_close(id)
80
- Thread.new do
81
- control("c", id)
82
- rescue StandardError
83
- nil
84
- end
85
- end
86
- def receive(op, data)
87
- if op == 1
88
- reply = JSON.parse(data)
89
- raise Error.from(0, reply, nil) if reply["type"] == "error"
90
- return
91
- end
92
- raise Error.new("Malformed tunnel frame.", code: "tunnel_protocol") if data.bytesize < 5
93
- kind, id, bytes = data.byteslice(0, 1), data.byteslice(1, 4).unpack1("N"), data.byteslice(5..)
94
- if kind == "a"
95
- raise Error.new("Malformed tunnel credit.", code: "tunnel_protocol") unless bytes.bytesize == 8
96
- amount = bytes.unpack1("Q>")
97
- @lock.synchronize do
98
- return if @closed
99
- raise Error.new("Invalid tunnel credit.", code: "tunnel_protocol") unless amount.between?(@credited, @sent)
100
- @credited = amount
101
- @credit.broadcast
102
- end
103
- return
104
- end
105
- stream = @lock.synchronize { @streams[id] }
106
- return unless stream
107
- case kind
108
- when "o" then stream.opened
109
- when "d"
110
- unless stream.push(bytes)
111
- stream.finish(Error.new("Read responses while writing; this stream's unread buffer is full.", code: "tunnel_receive_overflow"))
112
- async_close(id)
113
- end
114
- when "e" then stream.eof
115
- when "c"
116
- code = { "closed" => "port_closed", "reserved" => "port_reserved", "no-sshd" => "sshd_missing" }.fetch(bytes, "tunnel_refused")
117
- stream.finish(bytes == "done" ? nil : Error.new("Sandbox connection ended: #{bytes}", code: code))
118
- end
119
- end
120
- def finish(error = nil)
121
- streams = @lock.synchronize do
122
- return if @closed
123
- @closed, @error = true, error
124
- @credit.broadcast
125
- values = @streams.values
126
- @streams = {}
127
- values
128
- end
129
- @socket.close
130
- streams.each { |stream| stream.finish(error) }
131
- nil
132
- end
133
- def close = finish
134
-
135
- class Stream
136
- attr_reader :id
137
- def initialize(tunnel, id)
138
- @tunnel, @id, @buffer = tunnel, id, StreamBuffer.new
139
- @lock, @opened, @write_lock = Mutex.new, ConditionVariable.new, Mutex.new
140
- @closed = @ready = @eof = false
141
- end
142
- def closed? = @closed
143
- def opened = @lock.synchronize { @ready = true; @opened.broadcast }
144
- def wait_open(timeout)
145
- deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
146
- @lock.synchronize do
147
- until @ready || @closed
148
- left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
149
- raise Error.new("Opening the tunnel stream timed out.", code: "timeout") unless left.positive?
150
- @opened.wait(@lock, left)
151
- end
152
- raise(@error || Error.new("The stream closed before opening.", code: "tunnel_refused")) unless @ready
153
- end
154
- end
155
- def push(bytes) = @buffer.push(bytes)
156
- def eof = @buffer.finish
157
- def read(length = 65536) = @buffer.read(length)
158
- def write(bytes)
159
- @write_lock.synchronize do
160
- raise IOError, "Stream input is closed" if @closed || @eof
161
- @tunnel.write(self, bytes.to_s.b)
162
- end
163
- end
164
- def close_write
165
- @write_lock.synchronize do
166
- return if @eof || @closed
167
- @eof = true
168
- @tunnel.control("e", @id)
169
- end
170
- nil
171
- end
172
- def finish(error = nil)
173
- changed = @lock.synchronize do
174
- next false if @closed
175
- @closed, @error = true, error
176
- @opened.broadcast
177
- true
178
- end
179
- if changed
180
- @buffer.finish(error)
181
- @tunnel.forget(@id)
182
- end
183
- nil
184
- end
185
- def close
186
- return if @closed
187
- finish
188
- @tunnel.async_close(@id)
189
- nil
190
- end
191
- end
192
- end
193
-
194
- # Copies each accepted local TCP connection over a stream in one tunnel.
195
- class PortForward
196
- attr_reader :host, :port
197
- def initialize(sandbox, remote_port, host:, local_port:)
198
- raise ArgumentError, "port must be 1 to 65535" unless remote_port.is_a?(Integer) && remote_port.between?(1, 65535)
199
- @lock, @clients, @closed = Mutex.new, [], false
200
- @listener = TCPServer.new(host, local_port)
201
- @host, @port = @listener.addr[3], @listener.addr[1]
202
- @tunnel = sandbox.open_tunnel
203
- @accept = Thread.new do
204
- loop do
205
- local = @listener.accept
206
- @lock.synchronize { @clients << local }
207
- if @closed
208
- close_local(local)
209
- break
210
- end
211
- Thread.new(local) { |client| forward(client, remote_port) }
212
- end
213
- rescue IOError, SystemCallError
214
- close
215
- end
216
- rescue StandardError
217
- @listener&.close
218
- @tunnel&.close
219
- raise
220
- end
221
- def forward(local, remote_port)
222
- stream = @tunnel.connect(remote_port)
223
- directions, lock = 2, Mutex.new
224
- finish = lambda do
225
- last = lock.synchronize { directions -= 1; directions.zero? }
226
- if last
227
- stream.close
228
- close_local(local)
229
- end
230
- end
231
- Thread.new do
232
- begin
233
- loop { stream.write(local.readpartial(65536)) }
234
- rescue EOFError
235
- stream.close_write
236
- rescue StandardError
237
- stream.close
238
- close_local(local)
239
- ensure
240
- finish.call
241
- end
242
- end
243
- begin
244
- while (data = stream.read)
245
- local.write(data)
246
- end
247
- local.close_write
248
- rescue StandardError
249
- stream.close
250
- close_local(local)
251
- ensure
252
- finish.call
253
- end
254
- rescue StandardError
255
- close_local(local)
256
- end
257
- def close_local(local)
258
- @lock.synchronize { @clients.delete(local) }
259
- local.close unless local.closed?
260
- rescue IOError, SystemCallError
261
- nil
262
- end
263
- def close
264
- clients = @lock.synchronize do
265
- return if @closed
266
- @closed = true
267
- @clients.dup
268
- end
269
- @listener.close unless @listener.closed?
270
- clients.each { |client| close_local(client) }
271
- @tunnel.close
272
- nil
273
- end
274
- end
275
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
- require "net/http"
5
- require "uri"
6
-
7
- module WithRuntime
8
- # An account opened with a wallet alone: no key, no sign-in, no browser.
9
- #
10
- # opened = WithRuntime::Wallet.open_account(20, accept_terms: true)
11
- # # send exactly opened["amount"] to one of opened["networks"], then, once paid:
12
- # key = WithRuntime::Wallet.claim_account(opened["claimCode"])["apiKey"]
13
- #
14
- # Opening one means agreeing to https://withruntime.com/legal/terms, so ask the
15
- # person you work for first. The claim code is shown once and is the only way
16
- # to the account's key: keep it secret. These two calls go to the website;
17
- # every call after the key goes to the API as usual.
18
- module Wallet
19
- module_function
20
-
21
- # Opens an account and a stablecoin top-up of +usd+ dollars ($10 to
22
- # $500: it is a new account's first week). Returns "claimCode", "amount" (the exact token amount, to six
23
- # decimals), "networks" (an "address" on each and its "tokens") and "payBy".
24
- def open_account(usd, accept_terms:, auth_url: nil, timeout: 30)
25
- post(auth_url, "/api/wallet/topups", { "usd" => usd, "acceptTerms" => accept_terms }, timeout)
26
- end
27
-
28
- # Once the top-up is paid, {"status" => "paid", "apiKey" => ...}, once.
29
- # Before that, the top-up's "status"; call again.
30
- def claim_account(claim_code, auth_url: nil, timeout: 30)
31
- post(auth_url, "/api/wallet/claim", { "claimCode" => claim_code }, timeout)
32
- end
33
-
34
- def origin(auth_url)
35
- value = auth_url.to_s.empty? ? ENV["RUNTIME_AUTH_URL"].to_s : auth_url
36
- (value.empty? ? "https://withruntime.com" : value).chomp("/")
37
- end
38
-
39
- # One POST, never redirected, with no Authorization header.
40
- def post(auth_url, path, body, timeout)
41
- base = origin(auth_url)
42
- uri = URI("#{base}#{path}")
43
- http = Net::HTTP.new(uri.host, uri.port, nil)
44
- http.use_ssl = uri.scheme == "https"
45
- http.open_timeout = http.read_timeout = http.write_timeout = timeout
46
- request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json")
47
- request.body = JSON.generate(body)
48
- answer = begin
49
- http.start { |connection| connection.request(request) }
50
- rescue *Transport::NETWORK_ERRORS
51
- raise Error.new("No answer from #{base}.", code: "network", hint: "Retry in a minute.")
52
- end
53
- said = begin
54
- JSON.parse(answer.body.to_s)
55
- rescue JSON::ParserError
56
- nil
57
- end
58
- said = {} unless said.is_a?(Hash)
59
- status = answer.code.to_i
60
- return said if status.between?(200, 299)
61
-
62
- raise Error.new(said["error"].is_a?(String) ? said["error"] : "The request failed (HTTP #{status}).",
63
- code: said["code"].is_a?(String) ? said["code"] : (status == 429 ? "rate_limited" : "request_failed"),
64
- status: status)
65
- end
66
- private_class_method :origin, :post
67
- end
68
- end