Add base64 decoder

This commit is contained in:
Emery Hemingway 2024-04-01 19:24:18 +01:00
parent c9b38dd86e
commit 2b80be0fcf
6 changed files with 88 additions and 0 deletions

4
base64.prs Normal file
View File

@ -0,0 +1,4 @@
version 1.
Base64Text = <base64 @txt string @bin bytes> .
Base64File = <base64-file @txt string @path string @size int> .

View File

@ -1,6 +1,10 @@
version 1 .
embeddedType EntityRef.Cap .
Base64DecoderArguments = <base64-decoder {
dataspace: #:any
}>.
CacheArguments = <cache {
dataspace: #:any
lifetime: float

19
src/schema/base64.nim Normal file
View File

@ -0,0 +1,19 @@
import
preserves
type
Base64File* {.preservesRecord: "base64-file".} = object
`txt`*: string
`path`*: string
`size`*: BiggestInt
Base64Text* {.preservesRecord: "base64".} = object
`txt`*: string
`bin`*: seq[byte]
proc `$`*(x: Base64File | Base64Text): string =
`$`(toPreserves(x))
proc encode*(x: Base64File | Base64Text): seq[byte] =
encode(toPreserves(x))

View File

@ -17,6 +17,12 @@ type
JsonTranslatorArguments* {.preservesRecord: "json-stdio-translator".} = object
`field0`*: JsonTranslatorArgumentsField0
Base64DecoderArgumentsField0* {.preservesDictionary.} = object
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
Base64DecoderArguments* {.preservesRecord: "base64-decoder".} = object
`field0`*: Base64DecoderArgumentsField0
JsonTranslatorConnected* {.preservesRecord: "connected".} = object
`path`*: string
@ -88,6 +94,7 @@ type
`port`*: BiggestInt
proc `$`*(x: WebsocketArguments | JsonTranslatorArguments |
Base64DecoderArguments |
JsonTranslatorConnected |
JsonSocketTranslatorArguments |
XsltArguments |
@ -103,6 +110,7 @@ proc `$`*(x: WebsocketArguments | JsonTranslatorArguments |
`$`(toPreserves(x))
proc encode*(x: WebsocketArguments | JsonTranslatorArguments |
Base64DecoderArguments |
JsonTranslatorConnected |
JsonSocketTranslatorArguments |
XsltArguments |

View File

@ -10,6 +10,7 @@ const
withSqlite* {.booldefine.}: bool = true
import ./syndesizer/[
base64_decoder,
cache_actor,
file_system_usage,
json_socket_translator,
@ -27,6 +28,7 @@ when withSqlite:
runActor("syndesizer") do (turn: var Turn):
resolveEnvironment(turn) do (turn: var Turn; ds: Cap):
discard spawnTimerDriver(turn, ds)
discard spawnBase64Decoder(turn, ds)
discard spawnCacheActor(turn, ds)
discard spawnFileSystemUsageActor(turn, ds)
discard spawnJsonSocketTranslator(turn, ds)

View File

@ -0,0 +1,51 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[base64, os]
import pkg/hashlib/misc/blake2
import preserves, syndicate
import ../schema/config
import ../schema/base64 as schema
export Base64DecoderArguments
export schema
proc spawnBase64Decoder*(turn: var Turn; root: Cap): Actor {.discardable.} =
spawnActor(turn, "base64-decoder") do (turn: var Turn):
let tmpDir = getTempDir()
during(turn, root, ?:Base64DecoderArguments) do (ds: Cap):
let decTextPat = ?Observe(pattern: !Base64Text) ?? { 0: grabLit() }
during(turn, ds, decTextPat) do (txt: string):
discard publish(turn, ds, Base64Text(
txt: txt,
bin: cast[seq[byte]](decode(txt)),
))
let encTextPat = ?Observe(pattern: !Base64Text) ?? { 1: grabLit() }
during(turn, ds, encTextPat) do (bin: seq[byte]):
discard publish(turn, ds, Base64Text(
txt: encode(bin),
bin: bin,
))
let decFilePat = ?Observe(pattern: !Base64File) ?? { 0: grabLit() }
during(turn, ds, decFilePat) do (txt: string):
var bin = decode(txt)
var ctx = init[BLAKE2B_512]()
ctx.update(bin)
let
digest = $ctx.final()
path = tmpDir / digest
writeFile(path, bin)
discard publish(turn, ds, Base64File(
txt: txt,
path: path,
size: bin.len,
))
when isMainModule:
import syndicate/relays
runActor("main") do (turn: var Turn):
resolveEnvironment(turn) do (turn: var Turn; ds: Cap):
spawnBase64Decoder(turn, ds)