Skip to content

Commit

Permalink
Migrate to new io library. (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Apr 4, 2024
1 parent 11ed716 commit c96ab56
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 43 deletions.
10 changes: 5 additions & 5 deletions examples/package.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sdk: ^2.0.0-alpha.124
sdk: ^2.0.0-alpha.144
prefixes:
certificate_roots: toit-cert-roots
host: pkg-host
Expand All @@ -12,13 +12,13 @@ packages:
pkg-host:
url: github.com/toitlang/pkg-host
name: host
version: 1.13.0
hash: 64ee9e7bf1fd0e748e778a61269cd653fd748af8
version: 1.15.0
hash: 2588faa0c7553eca9a85cb6f829f3df41d9466f2
pkg-http:
url: github.com/toitlang/pkg-http
name: http
version: 2.3.4
hash: 39fe52083b4c745cc37420649a48277b45728fd2
version: 2.7.0
hash: ba6a3c73462aa8a3baae47881e9b2349725c22c3
toit-cert-roots:
url: github.com/toitware/toit-cert-roots
name: certificate_roots
Expand Down
2 changes: 1 addition & 1 deletion examples/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ dependencies:
version: ^1.6.1
host:
url: github.com/toitlang/pkg-host
version: ^1.13.0
version: ^1.15.0
supabase:
path: ..
4 changes: 1 addition & 3 deletions examples/utils/local_storage.toit
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import host.os
import host.file
import host.directory
import supabase
import writer show Writer
import system show platform PLATFORM_WINDOWS

// TODO(florian): A lot of this functionality should come from the CLI package.
Expand Down Expand Up @@ -62,8 +61,7 @@ class ConfigLocalStorage implements supabase.LocalStorage:
config_dir := dirname_ path_
directory.mkdir --recursive config_dir
stream := file.Stream.for_write path_
writer := Writer stream
writer.write (json.encode config_)
stream.out.write (json.encode config_)
// TODO(florian): we would like to call 'close' here.
// writer.close
stream.close
Expand Down
10 changes: 5 additions & 5 deletions package.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
sdk: ^2.0.0-alpha.128
sdk: ^2.0.0-alpha.144
prefixes:
host: pkg-host
http: pkg-http
packages:
pkg-host:
url: github.com/toitlang/pkg-host
name: host
version: 1.13.0
hash: 64ee9e7bf1fd0e748e778a61269cd653fd748af8
version: 1.15.0
hash: 2588faa0c7553eca9a85cb6f829f3df41d9466f2
pkg-http:
url: github.com/toitlang/pkg-http
name: http
version: 2.3.4
hash: 39fe52083b4c745cc37420649a48277b45728fd2
version: 2.7.0
hash: ba6a3c73462aa8a3baae47881e9b2349725c22c3
6 changes: 3 additions & 3 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: supabase
description: A Supabase client.
environment:
sdk: ^2.0.0-alpha.128
sdk: ^2.0.0-alpha.144
dependencies:
host:
url: github.com/toitlang/pkg-host
version: ^1.13.0
version: ^1.15.0
http:
url: github.com/toitlang/pkg-http
version: ^2.3.4
version: ^2.7.0
7 changes: 4 additions & 3 deletions src/auth.toit
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,16 @@ class Auth:
session_latch := monitor.Latch
server_task := task::
server.listen server_socket:: | request/http.Request writer/http.ResponseWriter |
out := writer.out
if request.path.starts_with "/success":
finish_oauth_sign_in request.path
writer.write "You can close this window now."
out.write "You can close this window now."
session_latch.set true
else if request.path.starts_with "/auth":
redirect_code := ""
if redirect_url:
redirect_code = """window.location.href = "$redirect_url" + window.location.hash;"""
writer.write """
out.write """
<html>
<body>
<p id="body">
Expand All @@ -184,7 +185,7 @@ class Auth:
</html>
"""
else:
writer.write "Invalid request."
out.write "Invalid request."

session_latch.get
sleep --ms=1 // Give the server time to respond with the success message.
Expand Down
15 changes: 7 additions & 8 deletions src/supabase.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
import bytes
import http
import net
import net.x509
import http.status_codes
import encoding.json
import encoding.url
import reader show Reader BufferedReader
import io
import tls

import .auth
Expand Down Expand Up @@ -275,8 +274,8 @@ class Client:
// TODO(florian): we should only do this if the payload is a Map.
encoded := json.encode payload
headers.set "Content-Type" "application/json"
request := http_client_.new_request method host_ path --headers=headers
request.body = bytes.Reader encoded
request := http_client_.new_request method --host=host_ --path=path --headers=headers
request.body = io.Reader encoded
response = request.send
else:
if method != http.POST: throw "UNIMPLEMENTED"
Expand Down Expand Up @@ -345,8 +344,8 @@ class Client:
// Still check whether there is a response.
// When performing an RPC we can't know in advance whether the function
// returns something or not.
buffered_body := BufferedReader body
if not buffered_body.can_ensure 1:
buffered_body := body
if not buffered_body.try-ensure-buffered 1:
return null

try:
Expand Down Expand Up @@ -593,14 +592,14 @@ class Storage:
If $public is true, downloads the data through the public URL.
*/
download --path/string --public/bool=false -> ByteArray:
download --path=path --public=public: | reader/Reader |
download --path=path --public=public: | reader/io.Reader |
return utils.read_all reader
unreachable

/**
Downloads the data stored in $path from the storage.
Calls the given $block with a $Reader for the resource.
Calls the given $block with an $io.Reader for the resource.
If $public is true, downloads the data through the public URL.
*/
Expand Down
16 changes: 7 additions & 9 deletions src/utils_.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
import reader show Reader SizedReader UNEXPECTED_END_OF_READER_EXCEPTION
import bytes
import io
import io show Reader

// TODO(kasper): This is unlikely to be the best place to share this
// code. Should we consider having this as part of the core libraries?
read_all reader/Reader -> ByteArray:
size/int? := reader is SizedReader
? (reader as SizedReader).size
: null
size/int? := reader.content-size

first := reader.read
if not first:
if size and size > 0: throw UNEXPECTED_END_OF_READER_EXCEPTION
if size and size > 0: throw Reader.UNEXPECTED_END_OF_READER
return #[]

second := reader.read
if not second:
if size:
missing := size - first.size
if missing > 0: throw UNEXPECTED_END_OF_READER_EXCEPTION
if missing > 0: throw Reader.UNEXPECTED_END_OF_READER
else if missing < 0: throw "OUT_OF_BOUNDS"
return first

Expand All @@ -36,10 +34,10 @@ read_all reader/Reader -> ByteArray:
while chunk := reader.read:
result.replace offset chunk
offset += chunk.size
if offset != size: throw UNEXPECTED_END_OF_READER_EXCEPTION
if offset != size: throw Reader.UNEXPECTED_END_OF_READER
return result
else:
buffer := bytes.Buffer.with_initial_size first.size + second.size
buffer := io.Buffer.with-capacity first.size + second.size
buffer.write first
first = null // Allow garbage collection.
buffer.write second
Expand Down
10 changes: 5 additions & 5 deletions tests/package.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sdk: ^2.0.0-alpha.124
sdk: ^2.0.0-alpha.144
prefixes:
host: pkg-host
supabase: ..
Expand All @@ -11,10 +11,10 @@ packages:
pkg-host:
url: github.com/toitlang/pkg-host
name: host
version: 1.13.0
hash: 64ee9e7bf1fd0e748e778a61269cd653fd748af8
version: 1.15.0
hash: 2588faa0c7553eca9a85cb6f829f3df41d9466f2
pkg-http:
url: github.com/toitlang/pkg-http
name: http
version: 2.3.4
hash: 39fe52083b4c745cc37420649a48277b45728fd2
version: 2.7.0
hash: ba6a3c73462aa8a3baae47881e9b2349725c22c3
2 changes: 1 addition & 1 deletion tests/package.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
host:
url: github.com/toitlang/pkg-host
version: ^1.13.0
version: ^1.15.0
supabase:
path: ..

0 comments on commit c96ab56

Please sign in to comment.