Skip to content

Commit

Permalink
add an effect for securing hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Nov 15, 2024
1 parent 7f24589 commit bc88b43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "midas"
version = "1.1.0"
version = "1.2.0"
target = "javascript"

description = "Task definition and runners for Gleam"
Expand Down
25 changes: 24 additions & 1 deletion src/midas/task.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import gleam/option.{type Option}
import gleam/uri.{type Uri}
import snag.{type Snag}

pub type HashAlgorithm {
SHA1
SHA256
SHA384
SHA512
}

pub type Effect(a) {
Done(a)
Abort(Snag)
Expand All @@ -19,6 +26,11 @@ pub type Effect(a) {
request: Request(BitArray),
resume: fn(Result(Response(BitArray), FetchError)) -> Effect(a),
)
Hash(
algorithm: HashAlgorithm,
bytes: BitArray,
resume: fn(Result(BitArray, String)) -> Effect(a),
)
List(directory: String, resume: fn(Result(List(String), String)) -> Effect(a))
Log(message: String, resume: fn(Result(Nil, Nil)) -> Effect(a))
Read(file: String, resume: fn(Result(BitArray, String)) -> Effect(a))
Expand Down Expand Up @@ -51,12 +63,13 @@ pub fn do(eff, then) {
Bundle(m, f, resume) -> Bundle(m, f, fn(reply) { do(resume(reply), then) })
Follow(lift, resume) -> Follow(lift, fn(reply) { do(resume(reply), then) })
Fetch(lift, resume) -> Fetch(lift, fn(reply) { do(resume(reply), then) })
Hash(algorithm, bytes, resume) ->
Hash(algorithm, bytes, fn(reply) { do(resume(reply), then) })
List(lift, resume) -> List(lift, fn(reply) { do(resume(reply), then) })
Log(lift, resume) -> Log(lift, fn(reply) { do(resume(reply), then) })
Read(lift, resume) -> Read(lift, fn(reply) { do(resume(reply), then) })
Serve(port, handle, resume) ->
Serve(port, handle, fn(reply) { do(resume(reply), then) })

Write(file, bytes, resume) ->
Write(file, bytes, fn(reply) { do(resume(reply), then) })
Zip(lift, resume) -> Zip(lift, fn(reply) { do(resume(reply), then) })
Expand Down Expand Up @@ -84,6 +97,8 @@ fn do_sequential(tasks, acc) {
Fetch(value, fn(reply) { do_sequential([then(reply), ..rest], acc) })
Follow(value, then) ->
Follow(value, fn(reply) { do_sequential([then(reply), ..rest], acc) })
Hash(a, b, then) ->
Hash(a, b, fn(reply) { do_sequential([then(reply), ..rest], acc) })
List(value, then) ->
List(value, fn(reply) { do_sequential([then(reply), ..rest], acc) })
Log(value, then) ->
Expand Down Expand Up @@ -164,6 +179,14 @@ fn list_error_reason(message) {
snag.new("Failed to list: " <> message)
}

pub fn hash(algorithm, bytes) {
Hash(algorithm, bytes, result_to_effect(_, hash_error_reason))
}

fn hash_error_reason(message) {
snag.new("Failed to hash: " <> message)
}

pub fn read(file) {
Read(file, result_to_effect(_, read_error_reason))
}
Expand Down

0 comments on commit bc88b43

Please sign in to comment.