Compare commits
5 Commits
trunk
...
http_drive
Author | SHA1 | Date |
---|---|---|
Emery Hemingway | 16926a789e | |
Emery Hemingway | 128df6dc03 | |
Emery Hemingway | 3996729824 | |
Emery Hemingway | dbe363052d | |
Emery Hemingway | 0dc419ebab |
294
README.md
294
README.md
|
@ -28,7 +28,6 @@ Example configuration:
|
|||
$cap <cache { dataspace: $nixspace lifetime: 3600.0 }> ]
|
||||
]
|
||||
```
|
||||
|
||||
### File System Usage
|
||||
|
||||
Summarize the size of file-system directory. Equivalent to `du -s -b`.
|
||||
|
@ -47,46 +46,6 @@ Query the size of a directory in bytes by observing `<file-system-usage "/SOME/P
|
|||
]
|
||||
```
|
||||
|
||||
### HTTP driver
|
||||
|
||||
Experimental HTTP server that services requests using [some version](https://git.syndicate-lang.org/syndicate-lang/syndicate-protocols/src/commit/9864ce0ec86fb2f916c2aab318a1e6994ab8834c/schemas/http.prs) of the http Syndicate protocol schema.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
|
||||
let ?not-found = dataspace
|
||||
$not-found ? <request _ ?res> [
|
||||
$res ! <status 503 "Service unavailable">
|
||||
$res ! <done "No binding here.">
|
||||
]
|
||||
|
||||
let ?greeting = dataspace
|
||||
$greeting ? <request _ ?res> [
|
||||
$res ! <status 200 "ok">
|
||||
$res ! <chunk "Hello world">
|
||||
$res ! <done "!">
|
||||
]
|
||||
|
||||
let ?http = dataspace
|
||||
$http [
|
||||
<http-bind #f 80 get [ ] $not-found>
|
||||
<http-bind #f 80 get [|...|] $not-found>
|
||||
<http-bind #f 80 get ["hello"] $greeting>
|
||||
]
|
||||
|
||||
? <service-object <daemon http-driver> ?cap> [
|
||||
$cap <http-driver { dataspace: $http }>
|
||||
]
|
||||
|
||||
<daemon http-driver {
|
||||
argv: [ "/bin/syndesizer" ]
|
||||
clearEnv: #t
|
||||
protocol: application/syndicate
|
||||
}>
|
||||
|
||||
<require-service <daemon http-driver>>
|
||||
```
|
||||
|
||||
### JSON Socket Translator
|
||||
|
||||
Communicate with sockets that send and receive lines of JSON using `<send …>` and `<recv …>` messages.
|
||||
|
@ -116,7 +75,7 @@ let ?mpvSpace = dataspace
|
|||
? <service-object <daemon syndesizer> ?cap> [
|
||||
$cap <json-socket-translator {
|
||||
dataspace: $mpvSpace
|
||||
socket: <unix "/run/user/1000/mpv.sock">
|
||||
socket: "/run/user/1000/mpv.sock"
|
||||
}>
|
||||
]
|
||||
]
|
||||
|
@ -164,6 +123,38 @@ let ?ds = dataspace
|
|||
]
|
||||
```
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
Readonly access to PostgreSQL databases. Asserts rows as records in response to SQL query assertions. Dynamic updates are not implemented.
|
||||
|
||||
Can be disabled by passing `--define:withPostgre=no` to the Nim compiler.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
<require-service <daemon syndesizer>>
|
||||
|
||||
let ?sqlspace = dataspace
|
||||
|
||||
? <service-object <daemon syndesizer> ?cap> [
|
||||
$cap <postgre {
|
||||
dataspace: $sqlspace
|
||||
connection: [
|
||||
["host" "example.com"]
|
||||
["dbname" "foobar"]
|
||||
["user" "hackme"]
|
||||
]
|
||||
}>
|
||||
]
|
||||
|
||||
let ?tuplespace = dataspace
|
||||
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
```
|
||||
|
||||
### Pulse proxy
|
||||
|
||||
A proxy actor that passes assertions and messages to a configured capability but only asserts observations on a a periodic pulse.
|
||||
|
@ -208,16 +199,56 @@ let ?tuplespace = dataspace
|
|||
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace [
|
||||
? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
? <sqlite-error ?msg ?ctx> [
|
||||
$log ! <log "-" { msg: $msg ctx: $ctx }>
|
||||
]
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
```
|
||||
|
||||
### Webooks
|
||||
|
||||
Listens for webhook requests and sends request data to a dataspace as messages.
|
||||
Request data is formated according to the http schema [defined in syndicate-protocols](https://git.syndicate-lang.org/syndicate-lang/syndicate-protocols/src/branch/main/schemas/http.prs), with the exception that messages bodies may be **bytes**, **string**, or **any** for the `content-type`s of `application/octet-stream`, `text/*`, and `application/json` respectively.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
<require-service <daemon syndesizer>>
|
||||
? <service-object <daemon syndesizer> ?cap> [
|
||||
$cap <webhooks {
|
||||
listen: <tcp "0.0.0.0" 1048>
|
||||
endpoints: {
|
||||
|
||||
# http://0.0.0.0:1048/my-endpoint
|
||||
["my-endpoint"]: $target-dataspace
|
||||
|
||||
# http://0.0.0.0:1048/some/multi-element/path
|
||||
["some", "multi-element", "path"]: $target-dataspace
|
||||
|
||||
}
|
||||
}>
|
||||
]
|
||||
```
|
||||
|
||||
### Websockets
|
||||
|
||||
connects to a websocket endpoint. During the lifetime of the connection a `<connected $URL>` assertion is made. Messages received from the server are sent to the dataspace wrapped in `<recv …>` records and messages observed as `<send …>` are sent to the server.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
<require-service <daemon syndesizer>>
|
||||
|
||||
let ?websocketspace = dataspace
|
||||
|
||||
? <service-object <daemon syndesizer> ?cap> [
|
||||
$cap <websocket {
|
||||
dataspace: $websocketspace
|
||||
url: "ws://127.0.0.1:5225/"
|
||||
}>
|
||||
]
|
||||
|
||||
$websocketspace ? <connected $websocketUrl> [
|
||||
<bind <ref { oid: "websocket" key: #x"" }> $websocketspace #f>
|
||||
]
|
||||
```
|
||||
### XML translator
|
||||
|
||||
Translates between Preserves and XML according to the [Conventions for Common Data Types](https://preserves.dev/conventions.html).
|
||||
|
@ -239,65 +270,30 @@ Examples:
|
|||
]
|
||||
```
|
||||
|
||||
---
|
||||
### XSLT processor
|
||||
|
||||
## http_client
|
||||
Perform XML stylesheet transformations. For a given textual XSLT stylesheet and a textual XML document generate an abstract XML document in Preserves form. Inputs may be XML text or paths to XML files.
|
||||
|
||||
The inverse of `http-driver`.
|
||||
|
||||
### Caveats
|
||||
- HTTPS is assumed unless the request is to port 80.
|
||||
- If the request or response sets `Content-Type` to `application/json` or `…/preserves`
|
||||
the body will be a parsed Preserves value.
|
||||
- No cache support.
|
||||
- Internal errors propagate using a `400 Internal client error` response.
|
||||
|
||||
Sample Syndicate server script:
|
||||
```
|
||||
# A top-level dataspace
|
||||
# Configuration example
|
||||
let ?ds = dataspace
|
||||
|
||||
# A dataspace for handling the HTTP response.
|
||||
let ?response = dataspace
|
||||
$response [
|
||||
?? <done { "code": "EUR" "exchange_middle": ?middle } > [
|
||||
$ds <exchange EUR RSD $middle>
|
||||
]
|
||||
]
|
||||
|
||||
$ds [
|
||||
<request
|
||||
# Request Euro to Dinar exchange rate.
|
||||
<http-request 0 "kurs.resenje.org" 443
|
||||
get ["api" "v1" "currencies" "eur" "rates" "today"]
|
||||
{Content-Type: "application/json"} {} #f
|
||||
>
|
||||
$response
|
||||
>
|
||||
|
||||
# Log all assertions.
|
||||
? ?any [
|
||||
$log ! <log "-" { assertion: $any }>
|
||||
? <xslt-transform "/stylesheet.xls" "/doc.xml" ?output> [
|
||||
? <xml-translation ?text $output> [
|
||||
$log ! <log "-" { xslt-output: $text }>
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
? <service-object <daemon http-client> ?cap> [
|
||||
$cap <http-client {
|
||||
dataspace: $ds
|
||||
}>
|
||||
]
|
||||
|
||||
<require-service <daemon http-client>>
|
||||
|
||||
? <built http-client ?path ?sum> [
|
||||
<daemon http-client {
|
||||
argv: [ "/bin/http_client" ]
|
||||
clearEnv: #t
|
||||
protocol: application/syndicate
|
||||
}>
|
||||
<require-service <daemon syndesizer>>
|
||||
? <service-object <daemon syndesizer> ?cap> $cap [
|
||||
<xml-translator { dataspace: $ds }>
|
||||
<xslt { dataspace: $ds }>
|
||||
]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## mintsturdyref
|
||||
|
||||
A utility for minting [Sturdyrefs](https://synit.org/book/operation/builtin/gatekeeper.html#sturdyrefs).
|
||||
|
@ -347,38 +343,30 @@ Sample Syndicate server script:
|
|||
|
||||
## msg
|
||||
|
||||
A utility that parses its command-line arguments as Preserves and send them as messages to `$SYNDICATE_ROUTE`.
|
||||
When called as `assert` (by a symlink or a rename) it will make assertions instead.
|
||||
A utility that sends messages to `$SYNDICATE_ROUTE`.
|
||||
|
||||
## PostgreSQL
|
||||
|
||||
Readonly access to PostgreSQL databases. Asserts rows as records in response to SQL query assertions. Dynamic updates are not implemented.
|
||||
## net_mapper
|
||||
|
||||
Can be disabled by passing `--define:withPostgre=no` to the Nim compiler.
|
||||
Publishes ICMP packet round-trip-times. See [net_mapper.prs](./net_mapper.prs) for a protocol description. [Source](./src/net_mapper.nim).
|
||||
|
||||
Example script:
|
||||
```
|
||||
# Configuration example
|
||||
<require-service <daemon postgre_actor>>
|
||||
? <machine-dataspace ?machine> [
|
||||
$machine ? <rtt "10.0.33.136" ?min ?avg ?max> [
|
||||
$log ! <log "-" { ping: { min: $min avg: $avg max: $max } }>
|
||||
]
|
||||
|
||||
let ?sqlspace = dataspace
|
||||
|
||||
? <service-object <daemon postgre_actor> ?cap> [
|
||||
$cap <postgre {
|
||||
dataspace: $sqlspace
|
||||
connection: [
|
||||
["host" "example.com"]
|
||||
["dbname" "foobar"]
|
||||
["user" "hackme"]
|
||||
]
|
||||
}>
|
||||
]
|
||||
|
||||
let ?tuplespace = dataspace
|
||||
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
$config [
|
||||
<require-service <daemon net_mapper>>
|
||||
<daemon net_mapper {
|
||||
argv: ["/bin/net_mapper"]
|
||||
protocol: application/syndicate
|
||||
}>
|
||||
? <service-object <daemon net_mapper> ?cap> [
|
||||
$cap { dataspace: $machine }
|
||||
]
|
||||
]
|
||||
]
|
||||
```
|
||||
|
||||
|
@ -386,63 +374,3 @@ $tuplespace ? [?id ?name] [
|
|||
|
||||
This utility serializes it's process environment to Preserves and prints it to stdout.
|
||||
It can be used to feed the environment variables of a nested child of the Syndicate server back to the server. For example, to retreive the environmental variables that a desktop manager passed on to its children.
|
||||
|
||||
## SQLite
|
||||
|
||||
Readonly access to SQLite databases. Asserts rows as records in response to SQL query assertions. Dynamic updates are not implemented.
|
||||
|
||||
Can be disabled by passing `--define:withSqlite=no` to the Nim compiler.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
<require-service <daemon sqlite_actor>>
|
||||
|
||||
let ?sqlspace = dataspace
|
||||
|
||||
? <service-object <daemon sqlite_actor> ?cap> [
|
||||
$cap <sqlite {
|
||||
dataspace: $sqlspace
|
||||
database: "/var/db/example.db"
|
||||
}>
|
||||
]
|
||||
|
||||
let ?tuplespace = dataspace
|
||||
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
```
|
||||
|
||||
## syndump
|
||||
|
||||
Utility for printing assertions and messages. Parses the command-line arguments as a pattern, connects a dataspace via `$SYNDICATE_ROUTE`, and writes observations to standard-output. Published assertions are prefixed by the `+` character, retractions by `-`, and messages by `!`.
|
||||
|
||||
Example
|
||||
```sh
|
||||
# Print patterns in use, filter down with AWK to only the published patterns.
|
||||
$ FS=':' syndump '<Observe ? _>' | awk -F : '/^+/ { print $2 }'
|
||||
```
|
||||
|
||||
## XSLT processor
|
||||
|
||||
Perform XML stylesheet transformations. For a given textual XSLT stylesheet and a textual XML document generate an abstract XML document in Preserves form. Inputs may be XML text or paths to XML files.
|
||||
|
||||
```
|
||||
# Configuration example
|
||||
let ?ds = dataspace
|
||||
$ds [
|
||||
? <xslt-transform "/stylesheet.xls" "/doc.xml" ?output> [
|
||||
? <xml-translation ?text $output> [
|
||||
$log ! <log "-" { xslt-output: $text }>
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
<require-service <daemon xslt_actor>>
|
||||
? <service-object <daemon xslt_actor> ?cap> $cap [
|
||||
<xml-translator { dataspace: $ds }>
|
||||
<xslt { dataspace: $ds }>
|
||||
]
|
||||
```
|
||||
|
|
3
Tupfile
3
Tupfile
|
@ -1,3 +1,2 @@
|
|||
include_rules
|
||||
: |> !nim_lk |> {lockfile}
|
||||
: {lockfile} |> !nim_cfg |> | ./<lock>
|
||||
: lock.json |> !nim_cfg |> | ./<lock>
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
version 1.
|
||||
|
||||
Base64Text = <base64 @txt string @bin bytes> .
|
||||
Base64File = <base64-file @txt string @path string @size int> .
|
21
config.prs
21
config.prs
|
@ -1,10 +1,6 @@
|
|||
version 1 .
|
||||
embeddedType EntityRef.Cap .
|
||||
|
||||
Base64DecoderArguments = <base64-decoder {
|
||||
dataspace: #:any
|
||||
}>.
|
||||
|
||||
CacheArguments = <cache {
|
||||
dataspace: #:any
|
||||
lifetime: float
|
||||
|
@ -19,24 +15,11 @@ JsonTranslatorArguments = <json-stdio-translator {
|
|||
dataspace: #:any
|
||||
}>.
|
||||
|
||||
JsonTranslatorConnected = <connected @address SocketAddress>.
|
||||
|
||||
TcpAddress = <tcp @host string @port int>.
|
||||
UnixAddress = <unix @path string>.
|
||||
|
||||
SocketAddress = TcpAddress / UnixAddress .
|
||||
|
||||
HttpClientArguments = <http-client {
|
||||
dataspace: #:any
|
||||
}>.
|
||||
|
||||
HttpDriverArguments = <http-driver {
|
||||
dataspace: #:any
|
||||
}>.
|
||||
JsonTranslatorConnected = <connected @path string>.
|
||||
|
||||
JsonSocketTranslatorArguments = <json-socket-translator {
|
||||
dataspace: #:any
|
||||
socket: SocketAddress
|
||||
socket: string
|
||||
}>.
|
||||
|
||||
PostgreArguments = <postgre {
|
||||
|
|
38
default.nix
38
default.nix
|
@ -1,34 +1,10 @@
|
|||
{
|
||||
pkgs ? import <nixpkgs> { },
|
||||
}:
|
||||
|
||||
let
|
||||
inherit (pkgs)
|
||||
lib
|
||||
buildNimPackage
|
||||
fetchFromGitea
|
||||
libxml2
|
||||
libxslt
|
||||
openssl
|
||||
pkg-config
|
||||
postgresql
|
||||
sqlite
|
||||
;
|
||||
in
|
||||
|
||||
buildNimPackage {
|
||||
pname = "syndicate_utils";
|
||||
version = "unstable";
|
||||
|
||||
src = if lib.inNixShell then null else lib.cleanSource ./.;
|
||||
|
||||
buildInputs = [
|
||||
postgresql.out
|
||||
sqlite
|
||||
libxml2
|
||||
libxslt
|
||||
openssl
|
||||
];
|
||||
{ pkgs ? import <nixpkgs> { } }:
|
||||
|
||||
pkgs.buildNimPackage {
|
||||
name = "syndicate_utils";
|
||||
propagatedNativeBuildInputs = [ pkgs.pkg-config ];
|
||||
propagatedBuildInputs =
|
||||
[ pkgs.postgresql pkgs.sqlite pkgs.libxml2 pkgs.libxslt ];
|
||||
lockFile = ./lock.json;
|
||||
src = pkgs.lib.sources.cleanSource ./.;
|
||||
}
|
||||
|
|
159
lock.json
159
lock.json
|
@ -3,24 +3,14 @@
|
|||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"cps"
|
||||
"bigints"
|
||||
],
|
||||
"path": "/nix/store/8gbhwni0akqskdb3qhn5nfgv6gkdz0vz-source",
|
||||
"rev": "c90530ac57f98a842b7be969115c6ef08bdcc564",
|
||||
"sha256": "0h8ghs2fqg68j3jdcg7grnxssmllmgg99kym2w0a3vlwca1zvr62",
|
||||
"srcDir": "",
|
||||
"url": "https://github.com/ehmry/cps/archive/c90530ac57f98a842b7be969115c6ef08bdcc564.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"getdns"
|
||||
],
|
||||
"path": "/nix/store/x9xmn7w4k6jg8nv5bnx148ibhnsfh362-source",
|
||||
"rev": "c73cbe288d9f9480586b8fa87f6d794ffb6a6ce6",
|
||||
"sha256": "1sbgx2x51szr22i72n7c8jglnfmr8m7y7ga0v85d58fwadiv7g6b",
|
||||
"path": "/nix/store/jvrm392g8adfsgf36prgwkbyd7vh5jsw-source",
|
||||
"ref": "20231006",
|
||||
"rev": "86ea14d31eea9275e1408ca34e6bfe9c99989a96",
|
||||
"sha256": "15pcpmnk1bnw3k8769rjzcpg00nahyrypwbxs88jnwr4aczp99j4",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.sr.ht/~ehmry/getdns-nim/archive/c73cbe288d9f9480586b8fa87f6d794ffb6a6ce6.tar.gz"
|
||||
"url": "https://github.com/ehmry/nim-bigints/archive/86ea14d31eea9275e1408ca34e6bfe9c99989a96.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
|
@ -36,107 +26,74 @@
|
|||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"preserves"
|
||||
"illwill"
|
||||
],
|
||||
"path": "/nix/store/hzb7af7lbd4kgd5y4hbgxv1lswig36yj-source",
|
||||
"rev": "fd498c6457cb9ad2f3179daa40da69eec00326dd",
|
||||
"sha256": "182xvw04vjw83mlcrkwkip29b44h0v8dapg2014k9011h90mdsj4",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.syndicate-lang.org/ehmry/preserves-nim/archive/fd498c6457cb9ad2f3179daa40da69eec00326dd.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"stew"
|
||||
],
|
||||
"path": "/nix/store/mqg8qzsbcc8xqabq2yzvlhvcyqypk72c-source",
|
||||
"rev": "3c91b8694e15137a81ec7db37c6c58194ec94a6a",
|
||||
"sha256": "17lfhfxp5nxvld78xa83p258y80ks5jb4n53152cdr57xk86y07w",
|
||||
"path": "/nix/store/3lmm3z36qn4gz7bfa209zv0pqrpm3di9-source",
|
||||
"ref": "v0.3.2",
|
||||
"rev": "1d12cb36ab7b76c31d2d25fa421013ecb382e625",
|
||||
"sha256": "0f9yncl5gbdja18mrqf5ixrdgrh95k0khda923dm1jd1x1b7ar8z",
|
||||
"srcDir": "",
|
||||
"url": "https://github.com/status-im/nim-stew/archive/3c91b8694e15137a81ec7db37c6c58194ec94a6a.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"syndicate"
|
||||
],
|
||||
"path": "/nix/store/dw30cq9gxz3353zgaq4a36ajq6chvbwc-source",
|
||||
"rev": "3a4dc1f13392830b587138199643d30fdbec8541",
|
||||
"sha256": "1mbd17rjm1fsx7d0ckzyjih2nzdjqs52ck9wscqcg9nvf3ib5mvh",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.syndicate-lang.org/ehmry/syndicate-nim/archive/3a4dc1f13392830b587138199643d30fdbec8541.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"sys"
|
||||
],
|
||||
"path": "/nix/store/syhxsjlsdqfap0hk4qp3s6kayk8cqknd-source",
|
||||
"rev": "4ef3b624db86e331ba334e705c1aa235d55b05e1",
|
||||
"sha256": "1q4qgw4an4mmmcbx48l6xk1jig1vc8p9cq9dbx39kpnb0890j32q",
|
||||
"srcDir": "src",
|
||||
"url": "https://github.com/ehmry/nim-sys/archive/4ef3b624db86e331ba334e705c1aa235d55b05e1.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"taps"
|
||||
],
|
||||
"path": "/nix/store/6y14ia52kr7jyaa0izx37mlablmq9s65-source",
|
||||
"rev": "8c8572cd971d1283e6621006b310993c632da247",
|
||||
"sha256": "1dp166bv9x773jmfqppg5i3v3rilgff013vb11yzwcid9l7s3iy8",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.sr.ht/~ehmry/nim_taps/archive/8c8572cd971d1283e6621006b310993c632da247.tar.gz"
|
||||
},
|
||||
{
|
||||
"date": "2024-04-02T15:38:57+01:00",
|
||||
"deepClone": false,
|
||||
"fetchLFS": false,
|
||||
"fetchSubmodules": true,
|
||||
"hash": "sha256-iZb9aAgYr4FGkqfIg49QWiCqeizIi047kFhugHiP8o0=",
|
||||
"leaveDotGit": false,
|
||||
"method": "git",
|
||||
"packages": [
|
||||
"solo5_dispatcher"
|
||||
],
|
||||
"path": "/nix/store/sf5dgj2ljvahcm6my7d61ibda51vnrii-solo5_dispatcher",
|
||||
"rev": "a7a894a96a2221284012800e6fd32923d83d20bd",
|
||||
"sha256": "13gjixw80vjqj0xlx2y85ixal82sa27q7j57j9383bqq11lgv5l9",
|
||||
"srcDir": "pkg",
|
||||
"url": "https://git.sr.ht/~ehmry/solo5_dispatcher"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"bigints"
|
||||
],
|
||||
"path": "/nix/store/jvrm392g8adfsgf36prgwkbyd7vh5jsw-source",
|
||||
"rev": "86ea14d31eea9275e1408ca34e6bfe9c99989a96",
|
||||
"sha256": "15pcpmnk1bnw3k8769rjzcpg00nahyrypwbxs88jnwr4aczp99j4",
|
||||
"srcDir": "src",
|
||||
"url": "https://github.com/ehmry/nim-bigints/archive/86ea14d31eea9275e1408ca34e6bfe9c99989a96.tar.gz"
|
||||
"url": "https://github.com/johnnovak/illwill/archive/1d12cb36ab7b76c31d2d25fa421013ecb382e625.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"nimcrypto"
|
||||
],
|
||||
"path": "/nix/store/h7lgq3by9mx8in03vzh0y964lnnlkalp-source",
|
||||
"rev": "ff6afc6a753bd645cad4568472c7733d1715e31e",
|
||||
"sha256": "0h9vpayp66pg66114bl0nsvlv1nzp7f0x5b35gbsbd7svzlcz5zj",
|
||||
"path": "/nix/store/zyr8zwh7vaiycn1s4r8cxwc71f2k5l0h-source",
|
||||
"ref": "traditional-api",
|
||||
"rev": "602c5d20c69c76137201b5d41f788f72afb95aa8",
|
||||
"sha256": "1dmdmgb6b9m5f8dyxk781nnd61dsk3hdxqks7idk9ncnpj9fng65",
|
||||
"srcDir": "",
|
||||
"url": "https://github.com/cheatfate/nimcrypto/archive/ff6afc6a753bd645cad4568472c7733d1715e31e.tar.gz"
|
||||
"url": "https://github.com/cheatfate/nimcrypto/archive/602c5d20c69c76137201b5d41f788f72afb95aa8.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"npeg"
|
||||
],
|
||||
"path": "/nix/store/xpn694ibgipj8xak3j4bky6b3k0vp7hh-source",
|
||||
"rev": "ec0cc6e64ea4c62d2aa382b176a4838474238f8d",
|
||||
"sha256": "1fi9ls3xl20bmv1ikillxywl96i9al6zmmxrbffx448gbrxs86kg",
|
||||
"path": "/nix/store/ffkxmjmigfs7zhhiiqm0iw2c34smyciy-source",
|
||||
"ref": "1.2.1",
|
||||
"rev": "26d62fdc40feb84c6533956dc11d5ee9ea9b6c09",
|
||||
"sha256": "0xpzifjkfp49w76qmaylan8q181bs45anmp46l4bwr3lkrr7bpwh",
|
||||
"srcDir": "src",
|
||||
"url": "https://github.com/zevv/npeg/archive/ec0cc6e64ea4c62d2aa382b176a4838474238f8d.tar.gz"
|
||||
"url": "https://github.com/zevv/npeg/archive/26d62fdc40feb84c6533956dc11d5ee9ea9b6c09.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"preserves"
|
||||
],
|
||||
"path": "/nix/store/6nnn5di5vip1vladlb7z56rbw18d1y7j-source",
|
||||
"ref": "20240208",
|
||||
"rev": "2825bceecf33a15b9b7942db5331a32cbc39b281",
|
||||
"sha256": "145vf46fy3wc52j6vs509fm9bi5lx7c53gskbkpcfbkv82l86dgk",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.syndicate-lang.org/ehmry/preserves-nim/archive/2825bceecf33a15b9b7942db5331a32cbc39b281.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"syndicate"
|
||||
],
|
||||
"path": "/nix/store/y9f3j4m7vmhf8gbpkvqa77jvzrc5ynlm-source",
|
||||
"ref": "20240208",
|
||||
"rev": "50a77995bcfe15e6062f54c6af0f55fba850c329",
|
||||
"sha256": "1avrk86c34qg39w8vlixsksli2gwgbsf29jhlap27ffzdbj2zbal",
|
||||
"srcDir": "src",
|
||||
"url": "https://git.syndicate-lang.org/ehmry/syndicate-nim/archive/50a77995bcfe15e6062f54c6af0f55fba850c329.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
"packages": [
|
||||
"ws"
|
||||
],
|
||||
"path": "/nix/store/zd51j4dphs6h1hyhdbzdv840c8813ai8-source",
|
||||
"ref": "0.5.0",
|
||||
"rev": "9536bf99ddf5948db221ccb7bb3663aa238a8e21",
|
||||
"sha256": "0j8z9jlvzb1h60v7rryvh2wx6vg99lra6i62whf3fknc53l641fz",
|
||||
"srcDir": "src",
|
||||
"url": "https://github.com/treeform/ws/archive/9536bf99ddf5948db221ccb7bb3663aa238a8e21.tar.gz"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
5
sql.prs
5
sql.prs
|
@ -2,7 +2,4 @@ version 1 .
|
|||
|
||||
# When asserted the actor reponds to @target rows as records
|
||||
# of the given label and row columns as record fields.
|
||||
Query = <query @statement [any ...] @target #:any> .
|
||||
|
||||
# When a query fails this is asserted instead.
|
||||
SqlError = <sql-error @msg string @context string>.
|
||||
Query = <query @statement string @target #:any> .
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
include_rules
|
||||
: foreach *.nim | $(SYNDICATE_PROTOCOL) ./<schema> |> !nim_bin |> {bin}
|
||||
: foreach *.nim | $(SYNDICATE_PROTOCOL) ./<schema> ./syndesizer/<checks> |> !nim_bin |> {bin}
|
||||
: foreach {bin} |> !assert_built |>
|
||||
: $(BIN_DIR)/msg |> !symlink |> $(BIN_DIR)/beep
|
||||
: $(BIN_DIR)/msg |> !symlink |> $(BIN_DIR)/assert
|
||||
: $(BIN_DIR)/msg |> cp %f %o |> $(BIN_DIR)/beep
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
include_rules
|
||||
NIM_FLAGS += --path:$(TUP_CWD)/../../../taps/pkg
|
||||
: foreach *.nim | $(SYNDICATE_PROTOCOL) ../<schema> |> !nim_bin |> | {bin}
|
||||
: foreach {bin} |> !assert_built |>
|
|
@ -0,0 +1,339 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[httpcore, options, parseutils, sets, streams, strutils, tables, times, uri]
|
||||
import pkg/sys/ioqueue
|
||||
import pkg/preserves
|
||||
import pkg/syndicate
|
||||
import pkg/syndicate/protocols/http
|
||||
import taps
|
||||
import ../schema/config
|
||||
|
||||
const
|
||||
SP = { ' ', '\x09', '\x0b', '\x0c', '\x0d' }
|
||||
SupportedVersion = "HTTP/1.1"
|
||||
IMF = initTimeFormat"ddd, dd MMM yyyy HH:mm:ss"
|
||||
|
||||
proc echo(args: varargs[string, `$`]) =
|
||||
stderr.writeLine(args)
|
||||
|
||||
proc `$`(b: seq[byte]): string = cast[string](b)
|
||||
|
||||
# a Date header on responses must be present if a clock is available
|
||||
|
||||
# An upgrade header can be used to switch over to native syndicate protocol.
|
||||
|
||||
# Check the response encoding matches or otherwise return 415
|
||||
|
||||
type HandlerEntity = ref object of Entity
|
||||
handler: proc (turn: var Turn; req: HttpRequest; cap: Cap)
|
||||
|
||||
method publish(e: HandlerEntity; turn: var Turn; a: AssertionRef; h: Handle) =
|
||||
var ctx = a.value.preservesTo HttpContext
|
||||
if ctx.isSome:
|
||||
var res = ctx.get.res.unembed Cap
|
||||
if res.isSome:
|
||||
e.handler(turn, ctx.get.req, res.get)
|
||||
else:
|
||||
echo "HandlerEntity got a non-Cap ", ctx.get.res
|
||||
else:
|
||||
echo "HandlerEntity got a non-HttpContext ", a.value
|
||||
|
||||
proc respond404(turn: var Turn; req: HttpRequest; cap: Cap) =
|
||||
message(turn, cap, HttpResponse(
|
||||
orKind: HttpResponseKind.status,
|
||||
status: HttpResponseStatus(
|
||||
code: 404,
|
||||
message: "resource not found",
|
||||
)))
|
||||
message(turn, cap, HttpResponse(
|
||||
orKind: HttpResponseKind.header,
|
||||
header: HttpResponseHeader(
|
||||
name: Symbol"content-length",
|
||||
value: "0",
|
||||
)))
|
||||
message(turn, cap, HttpResponse(orKind: HttpResponseKind.done))
|
||||
|
||||
proc bind404Handler(turn: var Turn; ds: Cap; port: Port) =
|
||||
stderr.writeLine "bind 404 handler to ", port
|
||||
var b: HttpBinding
|
||||
b.host = HostPattern(orKind: HostPatternKind.any)
|
||||
b.port = BiggestInt port
|
||||
b.method = MethodPattern(orKind: MethodPatternKind.any)
|
||||
b.path = @[PathPatternElement(orKind: PathPatternElementKind.rest)]
|
||||
b.handler = newCap(turn, HandlerEntity(handler: respond404)).toPreserves
|
||||
discard publish(turn, ds, b)
|
||||
|
||||
proc badRequest(conn: Connection; msg: string) =
|
||||
conn.send(SupportedVersion & " 400 " & msg, endOfMessage = true)
|
||||
close(conn)
|
||||
|
||||
proc extractQuery(s: var string): Table[Symbol, seq[QueryValue]] =
|
||||
let start = succ skipUntil(s, '?')
|
||||
if start < s.len:
|
||||
var query = s[start..s.high]
|
||||
s.setLen(start)
|
||||
for key, val in uri.decodeQuery(query):
|
||||
var list = result.getOrDefault(Symbol key)
|
||||
list.add QueryValue(orKind: QueryValueKind.string, string: val)
|
||||
result[Symbol key] = list
|
||||
|
||||
proc parseRequest(conn: Connection; text: string): (int, HttpRequest) =
|
||||
## Parse an `HttpRequest` request out of a `text` from a `Connection`.
|
||||
var
|
||||
token: string
|
||||
off: int
|
||||
|
||||
template advanceSp =
|
||||
let n = skipWhile(text, SP, off)
|
||||
if n < 1:
|
||||
badRequest(conn, "invalid request")
|
||||
return
|
||||
inc(off, n)
|
||||
|
||||
# method
|
||||
off.inc parseUntil(text, token, SP, off)
|
||||
result[1].method = token.toLowerAscii.Symbol
|
||||
advanceSp()
|
||||
|
||||
# target
|
||||
if text[off] == '/': inc(off) #TODO: always a leading slash?
|
||||
off.inc parseUntil(text, token, SP, off)
|
||||
advanceSp()
|
||||
|
||||
block:
|
||||
var version: string
|
||||
off.inc parseUntil(text, version, SP, off)
|
||||
advanceSp()
|
||||
if version != SupportedVersion:
|
||||
badRequest(conn, "version not supported")
|
||||
return
|
||||
|
||||
result[1].query = extractQuery(token)
|
||||
|
||||
result[1].path = split(token, '/')
|
||||
for p in result[1].path.mitems:
|
||||
# normalize the path
|
||||
for i, c in p:
|
||||
if c in {'A'..'Z'}:
|
||||
p[i] = char c.ord + 0x20
|
||||
|
||||
template advanceLine =
|
||||
inc off, skipWhile(text, {'\x0d'}, off)
|
||||
if text.high < off or text[off] != '\x0a':
|
||||
badRequest(conn, "invalid request")
|
||||
return
|
||||
inc off, 1
|
||||
|
||||
advanceLine()
|
||||
while off < text.len:
|
||||
off.inc parseUntil(text, token, {'\x0d', '\x0a'}, off)
|
||||
if token == "": break
|
||||
advanceLine()
|
||||
var
|
||||
(key, vals) = httpcore.parseHeader(token)
|
||||
k = key.toLowerAscii.Symbol
|
||||
v = result[1].headers.getOrDefault(k)
|
||||
for e in vals.mitems:
|
||||
e = e.toLowerAscii
|
||||
if k == Symbol"host":
|
||||
result[1].host = e
|
||||
if v == "": v = move e
|
||||
else:
|
||||
v.add ", "
|
||||
v.add e
|
||||
if k == Symbol"host":
|
||||
result[1].host = v
|
||||
result[1].headers[k] = v
|
||||
|
||||
result[0] = off
|
||||
|
||||
proc send(conn: Connection; chunk: Chunk) =
|
||||
case chunk.orKind
|
||||
of ChunkKind.string:
|
||||
conn.send(chunk.string, endOfMessage = false)
|
||||
of ChunkKind.bytes:
|
||||
conn.send(chunk.bytes, endOfMessage = false)
|
||||
|
||||
type
|
||||
Driver = ref object
|
||||
facet: Facet
|
||||
ds: Cap
|
||||
bindings: seq[HttpBinding]
|
||||
Session = ref object
|
||||
facet: Facet
|
||||
driver: Driver
|
||||
conn: Connection
|
||||
port: Port
|
||||
Exchange = ref object of Entity
|
||||
ses: Session
|
||||
req: HttpRequest
|
||||
stream: StringStream
|
||||
mode: HttpResponseKind
|
||||
|
||||
proc match(b: HttpBinding, r: HttpRequest): bool =
|
||||
## Check if `HttpBinding` `b` matches `HttpRequest` `r`.
|
||||
result =
|
||||
(b.host.orKind == HostPatternKind.any or
|
||||
b.host.host == r.host) and
|
||||
(b.port == r.port) and
|
||||
(b.method.orKind == MethodPatternKind.any or
|
||||
b.method.specific == r.method)
|
||||
if result:
|
||||
for i, p in b.path:
|
||||
if i > r.path.high: return false
|
||||
case p.orKind
|
||||
of PathPatternElementKind.wildcard: discard
|
||||
of PathPatternElementKind.label:
|
||||
if p.label != r.path[i]: return false
|
||||
of PathPatternElementKind.rest:
|
||||
return i == b.path.high
|
||||
# return false if ... isn't the last element
|
||||
|
||||
proc strongerThan(a, b: HttpBinding): bool =
|
||||
## Check if `a` is a stronger `HttpBinding` than `b`.
|
||||
result =
|
||||
(a.host.orKind != b.host.orKind and
|
||||
a.host.orKind == HostPatternKind.host) or
|
||||
(a.method.orKind != b.method.orKind and
|
||||
a.method.orKind == MethodPatternKind.specific)
|
||||
if not result:
|
||||
if a.path.len > b.path.len: return true
|
||||
for i in a.path.low..b.path.high:
|
||||
if a.path[i].orKind != b.path[i].orKind and
|
||||
a.path[i].orKind == PathPatternElementKind.label:
|
||||
return true
|
||||
|
||||
proc match(driver: Driver; req: HttpRequest): Option[HttpBinding] =
|
||||
for b in driver.bindings:
|
||||
if b.match req:
|
||||
if result.isNone or b.strongerThan(result.get):
|
||||
result = some b
|
||||
else:
|
||||
echo b, " does not match ", req
|
||||
|
||||
method message(e: Exchange; turn: var Turn; a: AssertionRef) =
|
||||
# Send responses back into a connection.
|
||||
var res: HttpResponse
|
||||
if e.mode != HttpResponseKind.done and res.fromPreserves a.value:
|
||||
case res.orKind
|
||||
of HttpResponseKind.status:
|
||||
if e.mode == res.orKind:
|
||||
e.stream.writeLine(SupportedVersion, " ", res.status.code, " ", res.status.message)
|
||||
e.stream.writeLine("date: ", now().format(IMF))
|
||||
# add Date header automatically - RFC 9110 Section 6.6.1.
|
||||
e.mode = HttpResponseKind.header
|
||||
of HttpResponseKind.header:
|
||||
if e.mode == res.orKind:
|
||||
e.stream.writeLine(res.header.name, ": ", res.header.value)
|
||||
of HttpResponseKind.chunk:
|
||||
if e.mode == HttpResponseKind.header:
|
||||
e.mode = res.orKind
|
||||
e.stream.writeLine()
|
||||
e.ses.conn.send(move e.stream.data, endOfMessage = false)
|
||||
e.ses.conn.send(res.chunk.chunk)
|
||||
of HttpResponseKind.done:
|
||||
if e.mode == HttpResponseKind.header:
|
||||
e.stream.writeLine()
|
||||
e.ses.conn.send(move e.stream.data, endOfMessage = false)
|
||||
e.mode = res.orKind
|
||||
e.ses.conn.send(res.done.chunk)
|
||||
stop(turn)
|
||||
# stop the facet scoped to the exchange
|
||||
# so that the response capability is withdrawn
|
||||
|
||||
proc service(turn: var Turn; exch: Exchange) =
|
||||
## Service an HTTP message exchange.
|
||||
var binding = exch.ses.driver.match exch.req
|
||||
if binding.isNone:
|
||||
echo "no binding for ", exch.req
|
||||
stop(turn)
|
||||
else:
|
||||
echo "driver matched binding ", binding.get
|
||||
var handler = binding.get.handler.unembed Cap
|
||||
if handler.isNone:
|
||||
stop(turn)
|
||||
else:
|
||||
publish(turn, handler.get, HttpContext(
|
||||
req: exch.req,
|
||||
res: embed newCap(turn, exch),
|
||||
))
|
||||
|
||||
proc service(ses: Session) =
|
||||
## Service a connection to an HTTP client.
|
||||
ses.facet.onStop do (turn: var Turn):
|
||||
close ses.conn
|
||||
ses.conn.onClosed do ():
|
||||
stop ses.facet
|
||||
ses.conn.onReceivedPartial do (data: seq[byte]; ctx: MessageContext; eom: bool):
|
||||
ses.facet.run do (turn: var Turn):
|
||||
var (n, req) = parseRequest(ses.conn, cast[string](data))
|
||||
if n > 0:
|
||||
req.port = BiggestInt ses.port
|
||||
inFacet(turn) do (turn: var Turn):
|
||||
preventInertCheck(turn)
|
||||
# start a new facet for this message exchange
|
||||
turn.service Exchange(
|
||||
facet: turn.facet,
|
||||
ses: ses,
|
||||
req: req,
|
||||
stream: newStringStream(),
|
||||
mode: HttpResponseKind.status
|
||||
)
|
||||
# ses.conn.receive()
|
||||
ses.conn.receive()
|
||||
|
||||
proc newListener(port: Port): Listener =
|
||||
var lp = newLocalEndpoint()
|
||||
lp.with port
|
||||
listen newPreconnection(local=[lp])
|
||||
|
||||
proc httpListen(turn: var Turn; driver: Driver; port: Port) =
|
||||
let facet = turn.facet
|
||||
var listener = newListener(port)
|
||||
# TODO: let listener
|
||||
listener.onListenError do (err: ref Exception):
|
||||
terminateFacet(facet, err)
|
||||
facet.onStop do (turn: var Turn):
|
||||
stop listener
|
||||
listener.onConnectionReceived do (conn: Connection):
|
||||
driver.facet.run do (turn: var Turn):
|
||||
# start a new turn
|
||||
linkActor(turn, "http-conn") do (turn: var Turn):
|
||||
preventInertCheck(turn)
|
||||
# facet is scoped to the lifetime of the connection
|
||||
service Session(
|
||||
facet: turn.facet,
|
||||
driver: driver,
|
||||
conn: conn,
|
||||
port: port,
|
||||
)
|
||||
|
||||
proc httpDriver(turn: var Turn; ds: Cap) =
|
||||
let driver = Driver(facet: turn.facet, ds: ds)
|
||||
|
||||
during(turn, ds, HttpBinding?:{
|
||||
1: grab(),
|
||||
}) do (port: BiggestInt):
|
||||
publish(turn, ds, HttpListener(port: port))
|
||||
|
||||
during(turn, ds, ?:HttpBinding) do (
|
||||
ho: HostPattern, po: int, me: MethodPattern, pa: PathPattern, e: Value):
|
||||
let b = HttpBinding(host: ho, port: po, `method`: me, path: pa, handler: e)
|
||||
driver.bindings.add b
|
||||
do:
|
||||
raiseAssert("need to remove binding " & $b)
|
||||
|
||||
during(turn, ds, ?:HttpListener) do (port: uint16):
|
||||
bind404Handler(turn, ds, Port port)
|
||||
httpListen(turn, driver, Port port)
|
||||
|
||||
proc spawnHttpDriver*(turn: var Turn; ds: Cap) =
|
||||
during(turn, ds, ?:HttpDriverArguments) do (ds: Cap):
|
||||
spawnActor("http-driver", turn) do (turn: var Turn):
|
||||
httpDriver(turn, ds)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: var Turn):
|
||||
resolveEnvironment(turn, spawnHttpDriver)
|
|
@ -1,91 +0,0 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
# TODO: write a TAPS HTTP client. Figure out how to externalise TLS.
|
||||
|
||||
import
|
||||
std/[httpclient, options, streams, strutils, tables, uri],
|
||||
pkg/taps,
|
||||
pkg/preserves,
|
||||
pkg/syndicate, pkg/syndicate/protocols/http,
|
||||
./schema/config
|
||||
|
||||
proc url(req: HttpRequest): Uri =
|
||||
result.scheme = if req.port == 80: "http" else: "https"
|
||||
result.hostname = req.host.present
|
||||
result.port = $req.port
|
||||
for i, p in req.path:
|
||||
if 0 < i: result.path.add '/'
|
||||
result.path.add p.encodeUrl
|
||||
for key, vals in req.query:
|
||||
if result.query.len > 0:
|
||||
result.query.add '&'
|
||||
result.query.add key.string.encodeUrl
|
||||
for i, val in vals:
|
||||
if i == 0: result.query.add '='
|
||||
elif i < vals.high: result.query.add ','
|
||||
result.query.add val.string.encodeUrl
|
||||
|
||||
proc bodyString(req: HttpRequest): string =
|
||||
if req.body.orKind == RequestBodyKind.present:
|
||||
return cast[string](req.body.present)
|
||||
|
||||
proc spawnHttpClient*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
|
||||
during(turn, root, ?:HttpClientArguments) do (ds: Cap):
|
||||
spawn("http-client", turn) do (turn: Turn):
|
||||
during(turn, ds, HttpContext.grabType) do (ctx: HttpContext):
|
||||
let peer = ctx.res.unembed(Cap).get
|
||||
var client = newHttpClient()
|
||||
try:
|
||||
var
|
||||
headers = newHttpHeaders()
|
||||
contentType = ""
|
||||
for key, val in ctx.req.headers:
|
||||
if key == Symbol"Content-Type":
|
||||
contentType = val
|
||||
client.headers[key.string] = val
|
||||
let stdRes = client.request(
|
||||
ctx.req.url,
|
||||
ctx.req.method.string.toUpper,
|
||||
ctx.req.bodyString, headers
|
||||
)
|
||||
var resp = HttpResponse(orKind: HttpResponseKind.status)
|
||||
resp.status.code = stdRes.status[0 .. 2].parseInt
|
||||
resp.status.message = stdRes.status[3 .. ^1]
|
||||
message(turn, peer, resp)
|
||||
resp = HttpResponse(orKind: HttpResponseKind.header)
|
||||
for key, val in stdRes.headers:
|
||||
if key == "Content-Type":
|
||||
contentType = val
|
||||
resp.header.name = key.Symbol
|
||||
resp.header.value = val
|
||||
message(turn, peer, resp)
|
||||
case contentType
|
||||
of "application/json", "text/preserves":
|
||||
message(turn, peer,
|
||||
initRecord("done", stdRes.bodyStream.readAll.parsePreserves))
|
||||
of "application/preserves":
|
||||
message(turn, peer,
|
||||
initRecord("done", stdRes.bodyStream.decodePreserves))
|
||||
else:
|
||||
resp = HttpResponse(orKind: HttpResponseKind.done)
|
||||
resp.done.chunk.string = stdRes.bodyStream.readAll()
|
||||
message(turn, peer, resp)
|
||||
except CatchableError as err:
|
||||
var resp = HttpResponse(orKind: HttpResponseKind.status)
|
||||
resp.status.code = 400
|
||||
resp.status.message = "Internal client error"
|
||||
message(turn, peer, resp)
|
||||
resp = HttpResponse(orKind: HttpResponseKind.done)
|
||||
resp.done.chunk.string = err.msg
|
||||
message(turn, peer, resp)
|
||||
client.close()
|
||||
do:
|
||||
client.close()
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnHttpClient(turn, ds)
|
|
@ -1 +0,0 @@
|
|||
define:ssl
|
|
@ -0,0 +1,114 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
## An actor for filesystem monitoring.
|
||||
|
||||
import std/[asyncdispatch, asyncfile, tables]
|
||||
import posix, posix/inotify
|
||||
import preserves
|
||||
import syndicate, syndicate/[bags, relays]
|
||||
import ./schema/inotify_actor
|
||||
|
||||
var IN_NONBLOCK {.importc, nodecl.}: cint
|
||||
|
||||
type
|
||||
BootArgs {.preservesDictionary.} = object
|
||||
dataspace: Cap
|
||||
|
||||
proc toMask(sym: Symbol): uint32 =
|
||||
case sym.string
|
||||
of "IN_ACCESS": IN_ACCESS
|
||||
of "IN_MODIFY": IN_MODIFY
|
||||
of "IN_ATTRIB": IN_ATTRIB
|
||||
of "IN_CLOSE_WRITE": IN_CLOSE_WRITE
|
||||
of "IN_CLOSE_NOWRITE": IN_CLOSE_NOWRITE
|
||||
of "IN_CLOSE": IN_CLOSE
|
||||
of "IN_OPEN": IN_OPEN
|
||||
of "IN_MOVED_FROM": IN_MOVED_FROM
|
||||
of "IN_MOVED_TO": IN_MOVED_TO
|
||||
of "IN_MOVE": IN_MOVE
|
||||
of "IN_CREATE": IN_CREATE
|
||||
of "IN_DELETE": IN_DELETE
|
||||
of "IN_DELETE_SELF": IN_DELETE_SELF
|
||||
of "IN_MOVE_SELF": IN_MOVE_SELF
|
||||
else: 0
|
||||
|
||||
func contains(event, bit: uint32): bool = (event and bit) != 0
|
||||
|
||||
iterator symbols(event: uint32): Symbol =
|
||||
if event.contains IN_ACCESS:
|
||||
yield Symbol"IN_ACCESS"
|
||||
if event.contains IN_MODIFY:
|
||||
yield Symbol"IN_MODIFY"
|
||||
if event.contains IN_ATTRIB:
|
||||
yield Symbol"IN_ATTRIB"
|
||||
if event.contains IN_CLOSE_WRITE:
|
||||
yield Symbol"IN_CLOSE_WRITE"
|
||||
if event.contains IN_CLOSE_NOWRITE:
|
||||
yield Symbol"IN_CLOSE_NOWRITE"
|
||||
if event.contains IN_OPEN:
|
||||
yield Symbol"IN_OPEN"
|
||||
if event.contains IN_MOVED_FROM:
|
||||
yield Symbol"IN_MOVED_FROM"
|
||||
if event.contains IN_MOVED_TO:
|
||||
yield Symbol"IN_MOVED_TO"
|
||||
if event.contains IN_CREATE:
|
||||
yield Symbol"IN_CREATE"
|
||||
if event.contains IN_DELETE:
|
||||
yield Symbol"IN_DELETE"
|
||||
if event.contains IN_DELETE_SELF:
|
||||
yield Symbol"IN_DELETE_SELF"
|
||||
if event.contains IN_MOVE_SELF:
|
||||
yield Symbol"IN_MOVE_SELF"
|
||||
if event.contains (IN_CLOSE_WRITE or IN_CLOSE_NOWRITE):
|
||||
yield Symbol"IN_CLOSE"
|
||||
if event.contains (IN_MOVED_FROM or IN_MOVED_TO):
|
||||
yield Symbol"IN_MOVE"
|
||||
|
||||
runActor("inotify_actor") do (root: Cap; turn: var Turn):
|
||||
let buf = newSeq[byte](8192)
|
||||
let eventPattern = ?Observe(pattern: !InotifyMessage) ?? { 0: grabLit(), 1: grabLit() }
|
||||
connectStdio(turn, root)
|
||||
during(turn, root, ?:BootArgs) do (ds: Cap):
|
||||
let inf = inotify_init1(IN_NONBLOCK)
|
||||
doAssert inf != -1, $inf & " - " & $strerror(errno)
|
||||
var
|
||||
registry = initTable[cint, string]()
|
||||
watchBag: Bag[cint]
|
||||
let
|
||||
anf = newAsyncFile(AsyncFD inf)
|
||||
facet = turn.facet
|
||||
var fut: Future[int]
|
||||
proc readEvents() {.gcsafe.} =
|
||||
fut = readBuffer(anf, buf[0].addr, buf.len)
|
||||
addCallback(fut, facet) do (turn: var Turn):
|
||||
let n = read(fut)
|
||||
doAssert n > 0
|
||||
for event in inotify_events(buf[0].addr, n):
|
||||
var msg = InotifyMessage(path: registry[event.wd], cookie: event.cookie.BiggestInt)
|
||||
if event.len > 0:
|
||||
let n = event.len
|
||||
msg.name.setLen(n)
|
||||
copyMem(msg.name[0].addr, event.name.addr, n)
|
||||
for i, c in msg.name:
|
||||
if c == '\0':
|
||||
msg.name.setLen(i)
|
||||
break
|
||||
for sym in event.mask.symbols:
|
||||
msg.event = sym
|
||||
message(turn, ds, msg)
|
||||
readEvents()
|
||||
readEvents()
|
||||
|
||||
during(turn, ds, eventPattern) do (path: string, kind: Symbol):
|
||||
let wd = inotify_add_watch(inf, path, kind.toMask or IN_MASK_ADD)
|
||||
doAssert wd > 0, $strerror(errno)
|
||||
registry[wd] = path
|
||||
discard watchBag.change(wd, 1)
|
||||
|
||||
do:
|
||||
if watchBag.change(wd, -1, clamp = true) == cdPresentToAbsent:
|
||||
discard close(wd)
|
||||
registry.del(wd)
|
||||
do:
|
||||
close(anf)
|
|
@ -7,8 +7,8 @@ when not defined(linux):
|
|||
{.error: "this component only tested for Linux".}
|
||||
|
||||
import std/oserrors
|
||||
import preserves, preserves/sugar
|
||||
import syndicate
|
||||
import preserves
|
||||
import syndicate, syndicate/relays
|
||||
import ./schema/mountpoints
|
||||
|
||||
type BootArgs {.preservesDictionary.} = object
|
||||
|
@ -20,34 +20,25 @@ proc mount(source, target, fsType: cstring; flags: culong; data: pointer): cint
|
|||
proc umount(target: cstring): cint {.importc, header: "<sys/mount.h>".}
|
||||
## `umount(2)`
|
||||
|
||||
proc spawnMountActor*(turn: Turn; ds: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "mount_actor") do (turn: Turn):
|
||||
let
|
||||
targetPat = observePattern(!Mountpoint, { @[%1]: grabLit() })
|
||||
sourcePat = observePattern(!Mountpoint, {
|
||||
@[%0]: grabLit(),
|
||||
@[%2]: grabLit(),
|
||||
})
|
||||
during(turn, ds, ?:BootArgs) do (ds: Cap):
|
||||
during(turn, ds, targetPat) do (target: string):
|
||||
during(turn, ds, sourcePat) do (source: string, fsType: string):
|
||||
var mountpoint = Mountpoint(
|
||||
source: source,
|
||||
target: target,
|
||||
`type`: fsType,
|
||||
)
|
||||
var rc = mount(source, target, fsType, 0, nil)
|
||||
if rc == 0:
|
||||
mountpoint.status = Status(orKind: StatusKind.success)
|
||||
else:
|
||||
mountpoint.status = Status(orKind: StatusKind.Failure)
|
||||
mountpoint.status.failure.msg = osErrorMsg(osLastError())
|
||||
discard publish(turn, ds, mountpoint)
|
||||
do:
|
||||
discard umount(target)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
discard spawnMountActor(turn, ds)
|
||||
runActor("mount_actor") do (turn: var Turn; root: Cap):
|
||||
let
|
||||
targetPat = ?Observe(pattern: !Mountpoint) ?? { 1: grabLit() }
|
||||
sourcePat = ?Observe(pattern: !Mountpoint) ?? { 0: grabLit(), 2: grabLit() }
|
||||
connectStdio(turn, root)
|
||||
during(turn, root, ?:BootArgs) do (ds: Cap):
|
||||
during(turn, ds, targetPat) do (target: string):
|
||||
during(turn, ds, sourcePat) do (source: string, fsType: string):
|
||||
var mountpoint = Mountpoint(
|
||||
source: source,
|
||||
target: target,
|
||||
`type`: fsType,
|
||||
)
|
||||
var rc = mount(source, target, fsType, 0, nil)
|
||||
if rc == 0:
|
||||
mountpoint.status = Status(orKind: StatusKind.success)
|
||||
else:
|
||||
mountpoint.status = Status(orKind: StatusKind.Failure)
|
||||
mountpoint.status.failure.msg = osErrorMsg(osLastError())
|
||||
discard publish(turn, ds, mountpoint)
|
||||
do:
|
||||
discard umount(target)
|
||||
|
|
24
src/msg.nim
24
src/msg.nim
|
@ -1,22 +1,20 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[sequtils, os, strutils]
|
||||
import std/[asyncdispatch, sequtils, os]
|
||||
import preserves, syndicate, syndicate/relays
|
||||
|
||||
setControlCHook(proc () {.noconv.} = quit())
|
||||
|
||||
runActor("msg") do (turn: Turn):
|
||||
proc main =
|
||||
let
|
||||
route = envRoute()
|
||||
data = map(commandLineParams(), parsePreserves)
|
||||
cmd = paramStr(0).extractFilename.normalize
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
case cmd
|
||||
of "assert":
|
||||
for e in data:
|
||||
publish(turn, ds, e)
|
||||
else: # "msg"
|
||||
|
||||
discard bootDataspace("msg") do (turn: var Turn; root: Cap):
|
||||
spawnRelays(turn, root)
|
||||
resolve(turn, root, route) do (turn: var Turn; ds: Cap):
|
||||
for e in data:
|
||||
message(turn, ds, e)
|
||||
sync(turn, ds) do (turn: Turn):
|
||||
stopActor(turn)
|
||||
|
||||
for _ in 1..2: poll()
|
||||
|
||||
main()
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
## A ping utility for Syndicate.
|
||||
|
||||
import std/[asyncdispatch, asyncnet, monotimes, nativesockets, net, os, strutils, tables, times]
|
||||
import preserves
|
||||
import syndicate, syndicate/relays
|
||||
|
||||
import ./schema/net_mapper
|
||||
|
||||
#[
|
||||
var
|
||||
SOL_IP {.importc, nodecl, header: "<sys/socket.h>".}: int
|
||||
IP_TTL {.importc, nodecl, header: "<netinet/in.h>".}: int
|
||||
]#
|
||||
|
||||
proc toPreservesHook(address: IpAddress): Value = toPreserves($address)
|
||||
|
||||
proc fromPreservesHook(address: var IpAddress; pr: Value): bool =
|
||||
try:
|
||||
if pr.isString:
|
||||
address = parseIpAddress(pr.string)
|
||||
result = true
|
||||
except ValueError: discard
|
||||
|
||||
when isMainModule:
|
||||
# verify that the hook catches
|
||||
var ip: IpAddress
|
||||
assert fromPreservesHook(ip, toPreservesHook(ip))
|
||||
|
||||
type
|
||||
IcmpHeader {.packed.} = object
|
||||
`type`: uint8
|
||||
code: uint8
|
||||
checksum: uint16
|
||||
|
||||
IcmpEchoFields {.packed.} = object
|
||||
header: IcmpHeader
|
||||
identifier: array[2, byte]
|
||||
sequenceNumber: uint16
|
||||
|
||||
IcmpEcho {.union.} = object
|
||||
fields: IcmpEchoFields
|
||||
buffer: array[8, uint8]
|
||||
|
||||
IcmpTypes = enum
|
||||
icmpEchoReply = 0,
|
||||
icmpEcho = 8,
|
||||
|
||||
proc initIcmpEcho(): IcmpEcho =
|
||||
result.fields.header.`type` = uint8 icmpEcho
|
||||
# doAssert urandom(result.fields.identifier) # Linux does this?
|
||||
|
||||
proc updateChecksum(msg: var IcmpEcho) =
|
||||
var sum: uint32
|
||||
msg.fields.header.checksum = 0
|
||||
for n in cast[array[4, uint16]](msg.buffer): sum = sum + uint32(n)
|
||||
while (sum and 0xffff0000'u32) != 0:
|
||||
sum = (sum and 0xffff) + (sum shr 16)
|
||||
msg.fields.header.checksum = not uint16(sum)
|
||||
|
||||
proc match(a, b: IcmpEchoFields): bool =
|
||||
({a.header.type, b.header.type} == {uint8 icmpEcho, uint8 icmpEchoReply}) and
|
||||
(a.header.code == b.header.code) and
|
||||
(a.sequenceNumber == b.sequenceNumber)
|
||||
|
||||
type
|
||||
Pinger = ref object
|
||||
facet: Facet
|
||||
ds: Cap
|
||||
rtt: RoundTripTime
|
||||
rttHandle: Handle
|
||||
sum: Duration
|
||||
count: int64
|
||||
msg: IcmpEcho
|
||||
socket: AsyncSocket
|
||||
sad: Sockaddr_storage
|
||||
sadLen: SockLen
|
||||
interval: Duration
|
||||
|
||||
proc newPinger(address: IpAddress; facet: Facet; ds: Cap): Pinger =
|
||||
result = Pinger(
|
||||
facet: facet,
|
||||
ds: ds,
|
||||
rtt: RoundTripTime(address: $address),
|
||||
msg: initIcmpEcho(),
|
||||
socket: newAsyncSocket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP, false, true),
|
||||
interval: initDuration(milliseconds = 500))
|
||||
toSockAddr(address, Port 0, result.sad, result.sadLen)
|
||||
# setSockOptInt(getFd socket, SOL_IP, IP_TTL, _)
|
||||
|
||||
proc close(ping: Pinger) = close(ping.socket)
|
||||
|
||||
proc sqr(dur: Duration): Duration =
|
||||
let us = dur.inMicroseconds
|
||||
initDuration(microseconds = us * us)
|
||||
|
||||
proc update(ping: Pinger; dur: Duration) {.inline.} =
|
||||
let secs = dur.inMicroseconds.float / 1_000_000.0
|
||||
if ping.count == 0: (ping.rtt.minimum, ping.rtt.maximum) = (secs, secs)
|
||||
elif secs < ping.rtt.minimum: ping.rtt.minimum = secs
|
||||
elif secs > ping.rtt.maximum: ping.rtt.maximum = secs
|
||||
ping.sum = ping.sum + dur
|
||||
inc ping.count
|
||||
ping.rtt.average = inMicroseconds(ping.sum div ping.count).float / 1_000_000.0
|
||||
|
||||
proc exchangeEcho(ping: Pinger) {.async.} =
|
||||
inc ping.msg.fields.sequenceNumber
|
||||
# updateChecksum(ping.msg) # Linux does this?
|
||||
let
|
||||
a = getMonoTime()
|
||||
r = sendto(ping.socket.getFd,
|
||||
unsafeAddr ping.msg.buffer[0], ping.msg.buffer.len, 0,
|
||||
cast[ptr SockAddr](unsafeAddr ping.sad), # neckbeard loser API
|
||||
ping.sadLen)
|
||||
if r == -1'i32:
|
||||
let osError = osLastError()
|
||||
raiseOSError(osError)
|
||||
while true:
|
||||
var
|
||||
(data, address, _) = await recvFrom(ping.socket, 128)
|
||||
b = getMonoTime()
|
||||
if address != $ping.rtt.address:
|
||||
stderr.writeLine "want ICMP from ", ping.rtt.address, " but received from ", address, " instead"
|
||||
elif data.len >= ping.msg.buffer.len:
|
||||
let
|
||||
period = b - a
|
||||
resp = cast[ptr IcmpEcho](unsafeAddr data[0])
|
||||
if match(ping.msg.fields, resp.fields):
|
||||
update(ping, period)
|
||||
return
|
||||
else:
|
||||
stderr.writeLine "ICMP mismatch"
|
||||
else:
|
||||
stderr.writeLine "reply data has a bad length ", data.len
|
||||
|
||||
proc kick(ping: Pinger) {.gcsafe.} =
|
||||
if not ping.socket.isClosed:
|
||||
addTimer(ping.interval.inMilliseconds.int, oneshot = true) do (fd: AsyncFD) -> bool:
|
||||
let fut = exchangeEcho(ping)
|
||||
fut.addCallback do ():
|
||||
if fut.failed and ping.rttHandle != Handle(0):
|
||||
ping.facet.run do (turn: var Turn):
|
||||
retract(turn, ping.rttHandle)
|
||||
reset ping.rttHandle
|
||||
else:
|
||||
ping.facet.run do (turn: var Turn):
|
||||
replace(turn, ping.ds, ping.rttHandle, ping.rtt)
|
||||
if ping.interval < initDuration(seconds = 20):
|
||||
ping.interval = ping.interval * 2
|
||||
kick(ping)
|
||||
|
||||
type Args {.preservesDictionary.} = object
|
||||
dataspace: Cap
|
||||
|
||||
runActor("net_mapper") do (root: Cap; turn: var Turn):
|
||||
connectStdio(turn, root)
|
||||
let rttObserver = ?Observe(pattern: !RoundTripTime) ?? {0: grabLit()}
|
||||
during(turn, root, ?:Args) do (ds: Cap):
|
||||
during(turn, ds, rttObserver) do (address: IpAddress):
|
||||
var ping: Pinger
|
||||
if address.family == IpAddressFamily.IPv4:
|
||||
ping = newPinger(address, turn.facet, ds)
|
||||
kick(ping)
|
||||
do:
|
||||
if not ping.isNil: close(ping)
|
|
@ -3,29 +3,40 @@
|
|||
|
||||
## See the rofi-script(5) manpage for documentation.
|
||||
|
||||
import std/[cmdline, envvars, strutils, tables]
|
||||
import std/[asyncdispatch, cmdline, envvars, strutils, tables]
|
||||
import preserves, syndicate, syndicate/relays
|
||||
import ./schema/rofi
|
||||
|
||||
if getEnv("ROFI_OUTSIDE") == "":
|
||||
quit("run this program in rofi")
|
||||
proc main =
|
||||
let
|
||||
route = envRoute()
|
||||
rofiPid = getEnv("ROFI_OUTSIDE")
|
||||
if rofiPid == "":
|
||||
quit("run this program in rofi")
|
||||
|
||||
runActor("rofi_script_actor") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
case paramCount()
|
||||
of 0:
|
||||
let pat = ?:Options
|
||||
onPublish(turn, ds, pat) do (options: seq[string]):
|
||||
stdout.writeLine options.join("\n")
|
||||
quit()
|
||||
runActor("rofi_script_actor") do (turn: var Turn; root: Cap):
|
||||
let rootFacet = turn.facet
|
||||
resolve(turn, root, route) do (turn: var Turn; ds: Cap):
|
||||
case paramCount()
|
||||
of 0:
|
||||
let pat = ?:Options
|
||||
onPublish(turn, ds, pat) do (options: seq[string]):
|
||||
stdout.writeLine options.join("\n")
|
||||
quit()
|
||||
|
||||
of 1:
|
||||
var select = Select(option: commandLineParams()[0])
|
||||
for (key, val) in envPairs():
|
||||
if key.startsWith "ROFI_":
|
||||
select.environment[Symbol key] = val
|
||||
message(turn, ds, select)
|
||||
sync(turn, ds, stopActor)
|
||||
of 1:
|
||||
var select = Select(option: commandLineParams()[0])
|
||||
for (key, val) in envPairs():
|
||||
if key.startsWith "ROFI_":
|
||||
select.environment[Symbol key] = val
|
||||
message(turn, ds, select)
|
||||
# TODO: sync not implemented correctly
|
||||
# sync(turn, ds, stopActor)
|
||||
callSoon do ():
|
||||
waitFor sleepAsync(1)
|
||||
quit()
|
||||
|
||||
else:
|
||||
quit("rofi passed an unexpected number of arguments")
|
||||
else:
|
||||
quit("rofi passed an unexpected number of arguments")
|
||||
|
||||
main()
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
|
||||
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))
|
|
@ -10,12 +10,6 @@ type
|
|||
WebsocketArguments* {.preservesRecord: "websocket".} = object
|
||||
`field0`*: WebsocketArgumentsField0
|
||||
|
||||
HttpClientArgumentsField0* {.preservesDictionary.} = object
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
|
||||
HttpClientArguments* {.preservesRecord: "http-client".} = object
|
||||
`field0`*: HttpClientArgumentsField0
|
||||
|
||||
JsonTranslatorArgumentsField0* {.preservesDictionary.} = object
|
||||
`argv`*: seq[string]
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
|
@ -23,29 +17,12 @@ type
|
|||
JsonTranslatorArguments* {.preservesRecord: "json-stdio-translator".} = object
|
||||
`field0`*: JsonTranslatorArgumentsField0
|
||||
|
||||
SocketAddressKind* {.pure.} = enum
|
||||
`TcpAddress`, `UnixAddress`
|
||||
`SocketAddress`* {.preservesOr.} = object
|
||||
case orKind*: SocketAddressKind
|
||||
of SocketAddressKind.`TcpAddress`:
|
||||
`tcpaddress`*: TcpAddress
|
||||
|
||||
of SocketAddressKind.`UnixAddress`:
|
||||
`unixaddress`*: UnixAddress
|
||||
|
||||
|
||||
Base64DecoderArgumentsField0* {.preservesDictionary.} = object
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
|
||||
Base64DecoderArguments* {.preservesRecord: "base64-decoder".} = object
|
||||
`field0`*: Base64DecoderArgumentsField0
|
||||
|
||||
JsonTranslatorConnected* {.preservesRecord: "connected".} = object
|
||||
`address`*: SocketAddress
|
||||
`path`*: string
|
||||
|
||||
JsonSocketTranslatorArgumentsField0* {.preservesDictionary.} = object
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
`socket`*: SocketAddress
|
||||
`socket`*: string
|
||||
|
||||
JsonSocketTranslatorArguments* {.preservesRecord: "json-socket-translator".} = object
|
||||
`field0`*: JsonSocketTranslatorArgumentsField0
|
||||
|
@ -56,12 +33,6 @@ type
|
|||
XsltArguments* {.preservesRecord: "xslt".} = object
|
||||
`field0`*: XsltArgumentsField0
|
||||
|
||||
HttpDriverArgumentsField0* {.preservesDictionary.} = object
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
|
||||
HttpDriverArguments* {.preservesRecord: "http-driver".} = object
|
||||
`field0`*: HttpDriverArgumentsField0
|
||||
|
||||
WebhooksArgumentsField0* {.preservesDictionary.} = object
|
||||
`endpoints`*: Table[seq[string], EmbeddedRef]
|
||||
`listen`*: Tcp
|
||||
|
@ -82,10 +53,6 @@ type
|
|||
SqliteArguments* {.preservesRecord: "sqlite".} = object
|
||||
`field0`*: SqliteArgumentsField0
|
||||
|
||||
TcpAddress* {.preservesRecord: "tcp".} = object
|
||||
`host`*: string
|
||||
`port`*: BiggestInt
|
||||
|
||||
CacheArgumentsField0* {.preservesDictionary.} = object
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
`lifetime`*: float
|
||||
|
@ -116,50 +83,36 @@ type
|
|||
PulseArguments* {.preservesRecord: "pulse".} = object
|
||||
`field0`*: PulseArgumentsField0
|
||||
|
||||
UnixAddress* {.preservesRecord: "unix".} = object
|
||||
`path`*: string
|
||||
|
||||
Tcp* {.preservesRecord: "tcp".} = object
|
||||
`host`*: string
|
||||
`port`*: BiggestInt
|
||||
|
||||
proc `$`*(x: WebsocketArguments | HttpClientArguments | JsonTranslatorArguments |
|
||||
SocketAddress |
|
||||
Base64DecoderArguments |
|
||||
proc `$`*(x: WebsocketArguments | JsonTranslatorArguments |
|
||||
JsonTranslatorConnected |
|
||||
JsonSocketTranslatorArguments |
|
||||
XsltArguments |
|
||||
HttpDriverArguments |
|
||||
WebhooksArguments |
|
||||
FileSystemUsageArguments |
|
||||
SqliteArguments |
|
||||
TcpAddress |
|
||||
CacheArguments |
|
||||
XmlTranslatorArguments |
|
||||
PostgreConnectionParameter |
|
||||
PostgreArguments |
|
||||
PulseArguments |
|
||||
UnixAddress |
|
||||
Tcp): string =
|
||||
`$`(toPreserves(x))
|
||||
|
||||
proc encode*(x: WebsocketArguments | HttpClientArguments |
|
||||
JsonTranslatorArguments |
|
||||
SocketAddress |
|
||||
Base64DecoderArguments |
|
||||
proc encode*(x: WebsocketArguments | JsonTranslatorArguments |
|
||||
JsonTranslatorConnected |
|
||||
JsonSocketTranslatorArguments |
|
||||
XsltArguments |
|
||||
HttpDriverArguments |
|
||||
WebhooksArguments |
|
||||
FileSystemUsageArguments |
|
||||
SqliteArguments |
|
||||
TcpAddress |
|
||||
CacheArguments |
|
||||
XmlTranslatorArguments |
|
||||
PostgreConnectionParameter |
|
||||
PostgreArguments |
|
||||
PulseArguments |
|
||||
UnixAddress |
|
||||
Tcp): seq[byte] =
|
||||
encode(toPreserves(x))
|
||||
|
|
|
@ -4,15 +4,11 @@ import
|
|||
|
||||
type
|
||||
Query* {.preservesRecord: "query".} = object
|
||||
`statement`*: seq[Value]
|
||||
`statement`*: string
|
||||
`target`* {.preservesEmbedded.}: Value
|
||||
|
||||
SqlError* {.preservesRecord: "sql-error".} = object
|
||||
`msg`*: string
|
||||
`context`*: string
|
||||
|
||||
proc `$`*(x: Query | SqlError): string =
|
||||
proc `$`*(x: Query): string =
|
||||
`$`(toPreserves(x))
|
||||
|
||||
proc encode*(x: Query | SqlError): seq[byte] =
|
||||
proc encode*(x: Query): seq[byte] =
|
||||
encode(toPreserves(x))
|
||||
|
|
|
@ -3,26 +3,42 @@
|
|||
|
||||
## Syndicate multitool.
|
||||
|
||||
import syndicate, syndicate/relays, syndicate/drivers/timers
|
||||
import syndicate, syndicate/relays, syndicate/actors/timers
|
||||
|
||||
const
|
||||
withPostgre* {.booldefine.}: bool = true
|
||||
withSqlite* {.booldefine.}: bool = true
|
||||
|
||||
import ./syndesizer/[
|
||||
base64_decoder,
|
||||
cache_actor,
|
||||
file_system_usage,
|
||||
http_driver,
|
||||
json_socket_translator,
|
||||
json_translator,
|
||||
pulses,
|
||||
xml_translator]
|
||||
webhooks,
|
||||
websockets,
|
||||
xml_translator,
|
||||
xslt_actor]
|
||||
|
||||
runActor("syndesizer") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
discard spawnTimerDriver(turn, ds)
|
||||
discard spawnBase64Decoder(turn, ds)
|
||||
discard spawnCacheActor(turn, ds)
|
||||
discard spawnFileSystemUsageActor(turn, ds)
|
||||
discard spawnHttpDriver(turn, ds)
|
||||
discard spawnJsonSocketTranslator(turn, ds)
|
||||
discard spawnJsonStdioTranslator(turn, ds)
|
||||
discard spawnPulseActor(turn, ds)
|
||||
discard spawnXmlTranslator(turn, ds)
|
||||
when withPostgre:
|
||||
import ./syndesizer/postgre_actor
|
||||
|
||||
when withSqlite:
|
||||
import ./syndesizer/sqlite_actor
|
||||
|
||||
runActor("syndesizer") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnTimers(turn, root)
|
||||
discard spawnCacheActor(turn, root)
|
||||
discard spawnFileSystemUsageActor(turn, root)
|
||||
discard spawnJsonSocketTranslator(turn, root)
|
||||
discard spawnJsonStdioTranslator(turn, root)
|
||||
discard spawnPulseActor(turn, root)
|
||||
discard spawnWebhookActor(turn, root)
|
||||
discard spawnWebsocketActor(turn, root)
|
||||
discard spawnXmlTranslator(turn, root)
|
||||
discard spawnXsltActor(turn, root)
|
||||
when withPostgre:
|
||||
discard spawnPostgreActor(turn, root)
|
||||
when withSqlite:
|
||||
discard spawnSqliteActor(turn, root)
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
include_rules
|
||||
: foreach *.nim | $(SYNDICATE_PROTOCOL) ../<schema> |> !nim_bin |> {bin}
|
||||
: foreach {bin} |> !assert_built |>
|
||||
: foreach *.nim | $(SYNDICATE_PROTOCOL) ../<schema> |> !nim_check |> | ./<checks>
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[base64, os]
|
||||
import pkg/hashlib/misc/blake2
|
||||
import preserves, preserves/sugar, syndicate
|
||||
import ../schema/config
|
||||
import ../schema/base64 as schema
|
||||
|
||||
export Base64DecoderArguments
|
||||
export schema
|
||||
|
||||
proc spawnBase64Decoder*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "base64-decoder") do (turn: Turn):
|
||||
let tmpDir = getTempDir()
|
||||
during(turn, root, ?:Base64DecoderArguments) do (ds: Cap):
|
||||
|
||||
let decTextPat = observePattern(!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 = observePattern(!Base64Text, { @[%1]: grabLit() })
|
||||
during(turn, ds, encTextPat) do (bin: seq[byte]):
|
||||
discard publish(turn, ds, Base64Text(
|
||||
txt: encode(bin),
|
||||
bin: bin,
|
||||
))
|
||||
|
||||
let decFilePat = observePattern( !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: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnBase64Decoder(turn, ds)
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
import std/times
|
||||
import preserves, syndicate,
|
||||
syndicate/durings,
|
||||
syndicate/drivers/timers
|
||||
syndicate/[durings, relays],
|
||||
syndicate/actors/timers
|
||||
|
||||
import ../schema/config
|
||||
|
||||
|
@ -18,9 +18,9 @@ type CacheEntity {.final.} = ref object of Entity
|
|||
pattern: Pattern
|
||||
lifetime: float64
|
||||
|
||||
method publish(cache: CacheEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
||||
method publish(cache: CacheEntity; turn: var Turn; ass: AssertionRef; h: Handle) =
|
||||
## Re-assert pattern captures in a sub-facet.
|
||||
discard inFacet(turn) do (turn: Turn):
|
||||
discard inFacet(turn) do (turn: var Turn):
|
||||
# TODO: a seperate facet for every assertion, too much?
|
||||
var ass = depattern(cache.pattern, ass.value.sequence)
|
||||
# Build an assertion with what he have of the pattern and capture.
|
||||
|
@ -30,12 +30,12 @@ method publish(cache: CacheEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
|||
stop(turn) # end this facet
|
||||
|
||||
proc isObserve(pat: Pattern): bool =
|
||||
pat.orKind == PatternKind.group and
|
||||
pat.group.type.orKind == GroupTypeKind.rec and
|
||||
pat.group.type.rec.label.isSymbol"Observe"
|
||||
pat.orKind == PatternKind.DCompound and
|
||||
pat.dcompound.orKind == DCompoundKind.rec and
|
||||
pat.dcompound.rec.label.isSymbol"Observe"
|
||||
|
||||
proc spawnCacheActor*(turn: Turn; root: Cap): Actor =
|
||||
spawnActor(turn, "cache_actor") do (turn: Turn):
|
||||
proc spawnCacheActor*(turn: var Turn; root: Cap): Actor =
|
||||
spawn("cache_actor", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:CacheArguments) do (ds: Cap, lifetime: float64):
|
||||
onPublish(turn, ds, ?:Observe) do (pat: Pattern, obs: Cap):
|
||||
var cache: CacheEntity
|
||||
|
@ -51,8 +51,7 @@ proc spawnCacheActor*(turn: Turn; root: Cap): Actor =
|
|||
discard observe(turn, ds, pat, cache)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
discard spawnTimerDriver(turn, ds)
|
||||
discard spawnCacheActor(turn, ds)
|
||||
runActor("cache_actor") do (turn: var Turn; root: Cap):
|
||||
spawnTimers(turn, root)
|
||||
connectStdio(turn, root)
|
||||
discard spawnCacheActor(turn, root)
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[dirs, os, paths]
|
||||
import preserves, preserves/sugar
|
||||
import syndicate
|
||||
import preserves
|
||||
import syndicate, syndicate/relays
|
||||
|
||||
import ../schema/[assertions, config]
|
||||
|
||||
proc spawnFileSystemUsageActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("file-system-usage", turn) do (turn: Turn):
|
||||
proc spawnFileSystemUsageActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("file-system-usage", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:FileSystemUsageArguments) do (ds: Cap):
|
||||
let pat = observePattern(!FileSystemUsage, { @[%0]: grab() })
|
||||
var pat = ?Observe(pattern: !FileSystemUsage) ?? { 0: grab() }
|
||||
during(turn, ds, pat) do (lit: Literal[string]):
|
||||
var ass = FileSystemUsage(path: lit.value)
|
||||
if fileExists(ass.path): ass.size = getFileSize(ass.path)
|
||||
|
@ -22,7 +22,6 @@ proc spawnFileSystemUsageActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
|||
# TODO: updates?
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
discard spawnFileSystemUsageActor(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnFileSystemUsageActor(turn, root)
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
## Thin wrapper over `syndicate/drivers/http_driver`.
|
||||
|
||||
import pkg/preserves, pkg/syndicate
|
||||
import pkg/syndicate/drivers/http_driver
|
||||
import pkg/taps
|
||||
import ../schema/config
|
||||
|
||||
proc spawnHttpDriver*(turn: Turn; ds: Cap): Actor {.discardable.}=
|
||||
http_driver.spawnHttpDriver(turn, ds)
|
||||
during(turn, ds, ?:HttpDriverArguments) do (ds: Cap):
|
||||
http_driver.spawnHttpDriver(turn, ds)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
|
||||
when defined(solo5):
|
||||
import solo5
|
||||
acquireDevices([("eth0", netBasic)], netAcquireHook)
|
||||
|
||||
proc envRoute: Route =
|
||||
var pr = parsePreserves $solo5_start_info.cmdline
|
||||
if result.fromPreserves pr:
|
||||
return
|
||||
elif pr.isSequence:
|
||||
for e in pr:
|
||||
if result.fromPreserves e:
|
||||
return
|
||||
quit("failed to parse command line for route to Syndicate gatekeeper")
|
||||
|
||||
runActor("main") do (turn: Turn):
|
||||
let ds = newDataspace(turn)
|
||||
spawnRelays(turn, ds)
|
||||
resolve(turn, ds, envRoute(), spawnHttpDriver)
|
||||
|
||||
else:
|
||||
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnHttpDriver(turn, ds)
|
|
@ -1,2 +0,0 @@
|
|||
define:ipv6Enabled
|
||||
include:"std/assertions"
|
|
@ -1,77 +1,39 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[json, options]
|
||||
import pkg/sys/[ioqueue, sockets]
|
||||
import preserves, preserves/jsonhooks, syndicate
|
||||
import std/[asyncdispatch, asyncnet, json]
|
||||
from std/nativesockets import AF_UNIX, SOCK_STREAM, Protocol
|
||||
import preserves, preserves/jsonhooks, syndicate, syndicate/relays
|
||||
|
||||
import ../schema/[config, json_messages]
|
||||
import ../schema/config, ../json_messages
|
||||
|
||||
template translateSocketBody {.dirty.} =
|
||||
# Template workaround for CPS and parameterized types.
|
||||
var
|
||||
guard = initGuard(facet)
|
||||
dec = newBufferedDecoder(0)
|
||||
buf = new string #TODO: get a pointer into the decoder
|
||||
alive = true
|
||||
proc kill(turn: Turn) =
|
||||
alive = false
|
||||
proc setup(turn: Turn) =
|
||||
# Closure, not CPS.
|
||||
onMessage(turn, ds, ?:SendJson) do (data: JsonNode):
|
||||
if alive:
|
||||
discard trampoline:
|
||||
whelp write(socket[], $data & "\n")
|
||||
else:
|
||||
stderr.writeLine "dropped send of ", data
|
||||
discard publish(turn, ds, initRecord("connected", sa.toPreserves))
|
||||
onStop(facet, kill)
|
||||
run(facet, setup)
|
||||
while alive:
|
||||
# TODO: parse buffer
|
||||
buf[].setLen(0x4000)
|
||||
let n = read(socket[], buf)
|
||||
if n < 1:
|
||||
stderr.writeLine "socket read returned ", n
|
||||
else:
|
||||
buf[].setLen(n)
|
||||
dec.feed(buf[])
|
||||
var data = dec.parse()
|
||||
if data.isSome:
|
||||
proc send(turn: Turn) =
|
||||
# Closure, not CPS.
|
||||
message(turn, ds, initRecord("recv", data.get))
|
||||
run(facet, send)
|
||||
stderr.writeLine "close socket ", sa
|
||||
close(socket[])
|
||||
proc spawnJsonSocketTranslator*(turn: var Turn; root: Cap): Actor =
|
||||
spawn("json-socket-translator", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:JsonSocketTranslatorArguments) do (ds: Cap, socketPath: string):
|
||||
let socket = newAsyncSocket(
|
||||
domain = AF_UNIX,
|
||||
sockType = SOCK_STREAM,
|
||||
protocol = cast[Protocol](0),
|
||||
buffered = false,
|
||||
)
|
||||
addCallback(connectUnix(socket, socketPath), turn) do (turn: var Turn):
|
||||
let a = JsonTranslatorConnected(path: socketPath)
|
||||
discard publish(turn, ds, a)
|
||||
|
||||
proc translateSocket(facet: Facet; ds: Cap; sa: TcpAddress) {.asyncio.} =
|
||||
var
|
||||
socket = new AsyncConn[Protocol.Tcp]
|
||||
conn = connectTcpAsync(sa.host, Port sa.port)
|
||||
socket[] = conn
|
||||
translateSocketBody()
|
||||
let socketFacet = turn.facet
|
||||
proc processOutput(fut: Future[string]) {.gcsafe.} =
|
||||
run(socketFacet) do (turn: var Turn):
|
||||
var data = fut.read.parseJson
|
||||
message(turn, ds, RecvJson(data: data))
|
||||
socket.recvLine.addCallback(processOutput)
|
||||
socket.recvLine.addCallback(processOutput)
|
||||
|
||||
proc translateSocket(facet: Facet; ds: Cap; sa: UnixAddress) {.asyncio.} =
|
||||
var
|
||||
socket = new AsyncConn[Protocol.Unix]
|
||||
conn = connectUnixAsync(sa.path)
|
||||
socket[] = conn
|
||||
translateSocketBody()
|
||||
|
||||
proc spawnJsonSocketTranslator*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "json-socket-translator") do (turn: Turn):
|
||||
during(turn, root, ?:JsonSocketTranslatorArguments) do (ds: Cap, sa: TcpAddress):
|
||||
linkActor(turn, "json-socket-translator") do (turn: Turn):
|
||||
discard trampoline:
|
||||
whelp translateSocket(turn.facet, ds, sa)
|
||||
during(turn, root, ?:JsonSocketTranslatorArguments) do (ds: Cap, sa: UnixAddress):
|
||||
linkActor(turn, "json-socket-translator") do (turn: Turn):
|
||||
discard trampoline:
|
||||
whelp translateSocket(turn.facet, ds, sa)
|
||||
onMessage(turn, ds, ?:SendJson) do (data: JsonNode):
|
||||
asyncCheck(turn, send(socket, $data & "\n"))
|
||||
do:
|
||||
close(socket)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnJsonSocketTranslator(turn, ds)
|
||||
runActor("json_socket_translator") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnJsonSocketTranslator(turn, root)
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
|
||||
import std/[json, osproc]
|
||||
import preserves
|
||||
import syndicate
|
||||
import syndicate, syndicate/relays
|
||||
|
||||
import ../schema/[config, json_messages]
|
||||
import ../schema/config
|
||||
import ../json_messages
|
||||
|
||||
proc runChild(params: seq[string]): string =
|
||||
if params.len < 1:
|
||||
|
@ -19,15 +20,14 @@ proc runChild(params: seq[string]): string =
|
|||
if result == "":
|
||||
stderr.writeLine "no ouput"
|
||||
|
||||
proc spawnJsonStdioTranslator*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "json-stdio-translator") do (turn: Turn):
|
||||
proc spawnJsonStdioTranslator*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("json-stdio-translator", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:JsonTranslatorArguments) do (argv: seq[string], ds: Cap):
|
||||
var js = parseJson(runChild(argv))
|
||||
message(turn, ds, RecvJson(data: js))
|
||||
discard publish(turn, ds, RecvJson(data: js))
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnJsonStdioTranslator(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnJsonStdioTranslator(turn, root)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import preserves, syndicate
|
||||
import ./schema/[config, sql]
|
||||
|
||||
import preserves, syndicate, syndicate/relays
|
||||
import ../schema/[config, sql]
|
||||
|
||||
{.passL: "-lpq".}
|
||||
|
||||
|
@ -87,26 +88,8 @@ proc splitParams(params: StringPairs): (cstringArray, cstringArray) =
|
|||
for i, _ in params: strings[i] = params[i][1]
|
||||
result[1] = allocCStringArray(strings)
|
||||
|
||||
proc renderSql(tokens: openarray[Value]): string =
|
||||
for token in tokens:
|
||||
if result.len > 0: result.add ' '
|
||||
case token.kind
|
||||
of pkSymbol:
|
||||
result.add token.symbol.string
|
||||
of pkString:
|
||||
result.add '\''
|
||||
result.add token.string
|
||||
result.add '\''
|
||||
of pkFloat, pkRegister, pkBigInt:
|
||||
result.add $token
|
||||
of pkBoolean:
|
||||
if token.bool: result.add '1'
|
||||
else: result.add '0'
|
||||
else:
|
||||
return ""
|
||||
|
||||
proc spawnPostgreActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("postgre", turn) do (turn: Turn):
|
||||
proc spawnPostgreActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("postgre", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:PostgreArguments) do (params: StringPairs, ds: Cap):
|
||||
var
|
||||
conn: PGconn
|
||||
|
@ -120,29 +103,21 @@ proc spawnPostgreActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
|||
statusHandle = publish(turn, ds,
|
||||
initRecord("status", toSymbol($status), msg.toPreserves))
|
||||
if status == CONNECTION_OK:
|
||||
during(turn, ds, ?:Query) do (statement: seq[Value], target: Cap):
|
||||
var text = renderSql statement
|
||||
if text == "":
|
||||
discard publish(turn, ds, SqlError(msg: "invalid statement", context: $statement))
|
||||
else:
|
||||
var
|
||||
res = PQexec(conn, text)
|
||||
st = PQresultStatus(res)
|
||||
if st == PGRES_TUPLES_OK or st == PGRES_SINGLE_TUPLE:
|
||||
let tuples = PQntuples(res)
|
||||
let fields = PQnfields(res)
|
||||
if tuples > 0 and fields > 0:
|
||||
for r in 0..<tuples:
|
||||
var tupl = initSequence(fields)
|
||||
for f in 0..<fields:
|
||||
tupl[f] = toPreserves($PQgetvalue(res, r, f))
|
||||
discard publish(turn, target, tupl)
|
||||
else:
|
||||
discard publish(turn, ds, SqlError(
|
||||
msg: $PQresStatus(st),
|
||||
context: $PQresultErrorMessage(res),
|
||||
))
|
||||
PQclear(res)
|
||||
during(turn, ds, ?:Query) do (statement: string, target: Cap):
|
||||
var res = PQexec(conn, statement)
|
||||
var st = PQresultStatus(res)
|
||||
discard publish(turn, ds, toRecord(
|
||||
"error", statement, toSymbol($PQresStatus(st)), $PQresultErrorMessage(res)))
|
||||
if st == PGRES_TUPLES_OK or st == PGRES_SINGLE_TUPLE:
|
||||
let tuples = PQntuples(res)
|
||||
let fields = PQnfields(res)
|
||||
if tuples > 0 and fields > 0:
|
||||
for r in 0..<tuples:
|
||||
var tupl = initSequence(fields)
|
||||
for f in 0..<fields:
|
||||
tupl[f] = toPreserves($PQgetvalue(res, r, f))
|
||||
discard publish(turn, target, tupl)
|
||||
PQclear(res)
|
||||
else:
|
||||
stderr.writeLine "refusing to do anything when status is ", status
|
||||
do:
|
||||
|
@ -151,8 +126,6 @@ proc spawnPostgreActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
|||
PQfinish(conn)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnPostgreActor(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnPostgreActor(turn, root)
|
|
@ -2,8 +2,9 @@
|
|||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[options, tables, times]
|
||||
import preserves, preserves/sugar
|
||||
import syndicate, syndicate/drivers/timers
|
||||
import preserves, syndicate,
|
||||
syndicate/relays,
|
||||
syndicate/actors/timers
|
||||
|
||||
import ../schema/[assertions, config]
|
||||
|
||||
|
@ -17,7 +18,7 @@ type PulseEntity {.final.} = ref object of Entity
|
|||
observePattern: Pattern
|
||||
observing: bool
|
||||
|
||||
proc schedule(turn: Turn; pulse: PulseEntity) =
|
||||
proc schedule(turn: var Turn; pulse: PulseEntity) =
|
||||
## Schedule the next pulse.
|
||||
## The next pulse will be schedule using the current time as
|
||||
## reference point and not the moment of the previous pulse.
|
||||
|
@ -27,7 +28,7 @@ proc schedule(turn: Turn; pulse: PulseEntity) =
|
|||
observer: pulse.self,
|
||||
))
|
||||
|
||||
method publish(pulse: PulseEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
||||
method publish(pulse: PulseEntity; turn: var Turn; ass: AssertionRef; h: Handle) =
|
||||
## Publish observers in reponse to <later-than …> assertions.
|
||||
pulse.timers.target.retract(turn, pulse.timerHandle)
|
||||
schedule(turn, pulse)
|
||||
|
@ -36,7 +37,7 @@ method publish(pulse: PulseEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
|||
pulse.target.publish(turn, a, h)
|
||||
pulse.target.sync(turn, pulse.self)
|
||||
|
||||
method message(pulse: PulseEntity; turn: Turn; v: AssertionRef) =
|
||||
method message(pulse: PulseEntity; turn: var Turn; v: AssertionRef) =
|
||||
## Retract observers in response to a sync message.
|
||||
pulse.observing = false
|
||||
for h in pulse.observers.keys:
|
||||
|
@ -46,7 +47,7 @@ type ProxyEntity {.final.} = ref object of Entity
|
|||
## A proxy `Entity` that diverts observers to a `PulseEntity`.
|
||||
pulse: PulseEntity
|
||||
|
||||
method publish(proxy: ProxyEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
||||
method publish(proxy: ProxyEntity; turn: var Turn; ass: AssertionRef; h: Handle) =
|
||||
## Proxy assertions that are not observations.
|
||||
if proxy.pulse.observePattern.matches ass.value:
|
||||
if proxy.pulse.observers.len == 0:
|
||||
|
@ -55,7 +56,7 @@ method publish(proxy: ProxyEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
|||
else:
|
||||
proxy.pulse.target.publish(turn, ass, h)
|
||||
|
||||
method retract(proxy: ProxyEntity; turn: Turn; h: Handle) =
|
||||
method retract(proxy: ProxyEntity; turn: var Turn; h: Handle) =
|
||||
## Retract proxied assertions.
|
||||
var obs: AssertionRef
|
||||
if proxy.pulse.observers.pop(h, obs):
|
||||
|
@ -66,15 +67,15 @@ method retract(proxy: ProxyEntity; turn: Turn; h: Handle) =
|
|||
else:
|
||||
proxy.pulse.target.retract(turn, h)
|
||||
|
||||
method message(proxy: ProxyEntity; turn: Turn; v: AssertionRef) =
|
||||
method message(proxy: ProxyEntity; turn: var Turn; v: AssertionRef) =
|
||||
## Proxy mesages.
|
||||
proxy.pulse.target.message(turn, v)
|
||||
|
||||
method sync(proxy: ProxyEntity; turn: Turn; peer: Cap) =
|
||||
method sync(proxy: ProxyEntity; turn: var Turn; peer: Cap) =
|
||||
## Proxy sync.
|
||||
proxy.pulse.target.sync(turn, peer)
|
||||
|
||||
proc newProxyEntity(turn: Turn; timers, ds: Cap; period: float): ProxyEntity =
|
||||
proc newProxyEntity(turn: var Turn; timers, ds: Cap; period: float): ProxyEntity =
|
||||
new result
|
||||
result.pulse = PulseEntity(
|
||||
target: ds.target,
|
||||
|
@ -84,11 +85,11 @@ proc newProxyEntity(turn: Turn; timers, ds: Cap; period: float): ProxyEntity =
|
|||
)
|
||||
result.pulse.self = newCap(turn, result.pulse)
|
||||
|
||||
proc spawnPulseActor*(turn: Turn; root: Cap): Actor =
|
||||
proc spawnPulseActor*(turn: var Turn; root: Cap): Actor =
|
||||
## Spawn an actor that retracts and re-asserts observers on
|
||||
## a timed pulse. Requires a timer service on the `root` capability.
|
||||
spawnActor(turn, "pulse") do (turn: Turn):
|
||||
let grabPeriod = observePattern(!Pulse, { @[%0]: grab() })
|
||||
spawn("pulse", turn) do (turn: var Turn):
|
||||
let grabPeriod = ?Observe(pattern: !Pulse) ?? { 0: grab() }
|
||||
during(turn, root, ?:PulseArguments) do (ds: Cap):
|
||||
during(turn, ds, grabPeriod) do (lit: Literal[float]):
|
||||
if lit.value < 0.000_1:
|
||||
|
@ -99,7 +100,7 @@ proc spawnPulseActor*(turn: Turn; root: Cap): Actor =
|
|||
discard publish(turn, ds, pulse)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
discard spawnPulseActor(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
spawnTimers(turn, root)
|
||||
connectStdio(turn, root)
|
||||
discard spawnPulseActor(turn, root)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import preserves, syndicate
|
||||
import ./schema/[config, sql]
|
||||
import preserves, syndicate, syndicate/relays
|
||||
import ../schema/[config, sql]
|
||||
|
||||
# Avoid Sqlite3 from the standard library because it is
|
||||
# only held together by wishful thinking and dlload.
|
||||
|
@ -54,19 +54,8 @@ proc finalize(stmt: Stmt): cint {.importSqlite3.}
|
|||
|
||||
doAssert libversion_number() == SQLITE_VERSION_NUMBER
|
||||
|
||||
proc assertError(facet: Facet; cap: Cap; db: Sqlite3; context: string) =
|
||||
run(facet) do (turn: Turn):
|
||||
publish(turn, cap, SqlError(
|
||||
msg: $errmsg(db),
|
||||
context: context,
|
||||
))
|
||||
|
||||
proc assertError(facet: Facet; cap: Cap; msg, context: string) =
|
||||
run(facet) do (turn: Turn):
|
||||
publish(turn, cap, SqlError(
|
||||
msg: msg,
|
||||
context: context,
|
||||
))
|
||||
proc logError(db: Sqlite3; context: string) =
|
||||
writeLine(stderr, errmsg(db), ": ", context)
|
||||
|
||||
proc extractValue(stmt: Stmt; col: cint): Value =
|
||||
case column_type(stmt, col)
|
||||
|
@ -89,61 +78,36 @@ proc extractTuple(stmt: Stmt; arity: cint): Value =
|
|||
result = initSequence(arity)
|
||||
for col in 0..<arity: result[col] = extractValue(stmt, col)
|
||||
|
||||
proc renderSql(tokens: openarray[Value]): string =
|
||||
for token in tokens:
|
||||
if result.len > 0: result.add ' '
|
||||
case token.kind
|
||||
of pkSymbol:
|
||||
result.add token.symbol.string
|
||||
of pkString:
|
||||
result.add '\''
|
||||
result.add token.string
|
||||
result.add '\''
|
||||
of pkFloat, pkRegister, pkBigInt:
|
||||
result.add $token
|
||||
of pkBoolean:
|
||||
if token.bool: result.add '1'
|
||||
else: result.add '0'
|
||||
else:
|
||||
return ""
|
||||
|
||||
proc spawnSqliteActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("sqlite-actor", turn) do (turn: Turn):
|
||||
proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("sqlite-actor", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:SqliteArguments) do (path: string, ds: Cap):
|
||||
linkActor(turn, path) do (turn: Turn):
|
||||
let facet = turn.facet
|
||||
stderr.writeLine("opening SQLite database ", path)
|
||||
var db: Sqlite3
|
||||
if open_v2(path, addr db, SQLITE_OPEN_READONLY, nil) != SQLITE_OK:
|
||||
assertError(facet, ds, db, path)
|
||||
else:
|
||||
turn.onStop do (turn: Turn):
|
||||
close(db)
|
||||
stderr.writeLine("closed SQLite database ", path)
|
||||
during(turn, ds, ?:Query) do (statement: seq[Value], target: Cap):
|
||||
var
|
||||
stmt: Stmt
|
||||
text = renderSql statement
|
||||
if text == "":
|
||||
assertError(facet, target, "invalid statement", $statement)
|
||||
elif prepare_v2(db, text, text.len.cint, addr stmt, nil) != SQLITE_OK:
|
||||
assertError(facet, target, db, text)
|
||||
else:
|
||||
try:
|
||||
let arity = column_count(stmt)
|
||||
var res = step(stmt)
|
||||
while res == SQLITE_ROW:
|
||||
var rec = extractTuple(stmt, arity)
|
||||
discard publish(turn, target, rec)
|
||||
res = step(stmt)
|
||||
assert res != 100
|
||||
if res != SQLITE_DONE:
|
||||
assertError(facet, target, db, text)
|
||||
finally:
|
||||
if finalize(stmt) != SQLITE_OK: assertError(facet, target, db, text)
|
||||
stderr.writeLine("opening SQLite database ", path)
|
||||
var db: Sqlite3
|
||||
if open_v2(path, addr db, SQLITE_OPEN_READONLY, nil) != SQLITE_OK:
|
||||
logError(db, path)
|
||||
else:
|
||||
during(turn, ds, ?:Query) do (statement: string, target: Cap):
|
||||
var stmt: Stmt
|
||||
if prepare_v2(db, statement, statement.len.cint, addr stmt, nil) != SQLITE_OK:
|
||||
logError(db, statement)
|
||||
else:
|
||||
try:
|
||||
let arity = column_count(stmt)
|
||||
var res = step(stmt)
|
||||
while res == SQLITE_ROW:
|
||||
var rec = extractTuple(stmt, arity)
|
||||
discard publish(turn, target, rec)
|
||||
res = step(stmt)
|
||||
assert res != 100
|
||||
if res != SQLITE_DONE:
|
||||
logError(db, statement)
|
||||
finally:
|
||||
if finalize(stmt) != SQLITE_OK: logError(db, statement)
|
||||
do:
|
||||
close(db)
|
||||
stderr.writeLine("closed SQLite database ", path)
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnSqliteActor(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnSqliteActor(turn, root)
|
|
@ -0,0 +1,105 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
## An actor for relaying Webhooks.
|
||||
|
||||
import std/[asyncdispatch, asynchttpserver, net, strutils, tables, uri]
|
||||
|
||||
import preserves, preserves/jsonhooks
|
||||
import syndicate, syndicate/[bags, relays]
|
||||
import syndicate/protocols/http
|
||||
|
||||
import ../schema/config
|
||||
|
||||
type
|
||||
CapBag = Bag[Cap]
|
||||
Endpoints = Table[seq[string], Cap]
|
||||
|
||||
func splitPath(s: string): seq[string] = s.strip(chars={'/'}).split('/')
|
||||
|
||||
proc toRecord(req: Request; seqnum: BiggestInt; path: seq[string]): Value =
|
||||
## Convert a request value from the std/asynchttpserver module
|
||||
## to a request type from syndicate/protocols/http.
|
||||
var record: HttpRequest
|
||||
record.sequenceNumber = seqnum
|
||||
record.host = req.hostname
|
||||
record.`method` = Symbol($req.reqMethod)
|
||||
record.path = path
|
||||
for key, val in req.headers.pairs:
|
||||
record.headers[Symbol key] = val
|
||||
for key, val in decodeQuery(req.url.query):
|
||||
record.query[Symbol key] =
|
||||
@[QueryValue(orKind: QueryValueKind.string, string: val)]
|
||||
let contentType = req.headers.getOrDefault("content-type")
|
||||
result = toPreserves record
|
||||
if req.body.len > 0:
|
||||
result[7] =
|
||||
case contentType.toString
|
||||
of "application/json":
|
||||
req.body.parsePreserves
|
||||
of "application/octet-stream":
|
||||
cast[seq[byte]](req.body).toPreserves
|
||||
else:
|
||||
req.body.toPreserves
|
||||
|
||||
proc spawnWebhookActor*(turn: var Turn; root: Cap): Actor =
|
||||
spawn("webhooks", turn) do (turn: var Turn):
|
||||
let pat = grabRecord("webhooks", grabDictionary({ "listen": ?:config.Tcp }))
|
||||
# Grab the details on listening for requests.
|
||||
# Disregard endpoints so the server doesn't restart as those change.
|
||||
during(turn, root, pat) do (host: string; port: Port):
|
||||
let endpointsPat = grabRecord("webhooks", grabDictionary({
|
||||
"listen": ?config.Tcp(host: host, port: BiggestInt port),
|
||||
"endpoints": grab(),
|
||||
}))
|
||||
# construct a pattern for grabbing endpoints when the server is ready
|
||||
var seqNum: BiggestInt
|
||||
let facet = turn.facet
|
||||
let endpoints = newTable[seq[string], CapBag]()
|
||||
# use a bag so the same capability registered multiple
|
||||
# times with the same path does not get duplicate messages
|
||||
|
||||
proc cb(req: Request): Future[void] =
|
||||
inc(seqNum)
|
||||
let path = req.url.path.splitPath
|
||||
if not endpoints.hasKey path:
|
||||
result = respond(req, Http404,
|
||||
"no capabilities registered at $1\n" % [req.url.path])
|
||||
else:
|
||||
result = respond(req, Http200, "")
|
||||
proc act(turn: var Turn) {.gcsafe.} =
|
||||
let rec = req.toRecord(seqNum, path)
|
||||
for cap in endpoints[path]:
|
||||
message(turn, cap, rec)
|
||||
run(facet, act)
|
||||
|
||||
let server = newAsyncHttpServer()
|
||||
stderr.writeLine("listening for webhooks at ", host, ":", port)
|
||||
if host.isIpAddress:
|
||||
var ip = parseIpAddress host
|
||||
case ip.family
|
||||
of IPv6:
|
||||
asyncCheck(turn, server.serve(port, cb, host, domain = AF_INET6))
|
||||
of IPv4:
|
||||
asyncCheck(turn, server.serve(port, cb, host, domain = AF_INET))
|
||||
else:
|
||||
asyncCheck(turn, server.serve(port, cb, host, domain = AF_INET6))
|
||||
asyncCheck(turn, server.serve(port, cb, host, domain = AF_INET))
|
||||
|
||||
during(turn, root, endpointsPat) do (eps: Endpoints):
|
||||
for path, cap in eps:
|
||||
if not endpoints.hasKey path:
|
||||
endpoints[path] = CapBag()
|
||||
discard endpoints[path].change(cap, +1)
|
||||
do:
|
||||
for path, cap in eps:
|
||||
discard endpoints[path].change(cap, -1)
|
||||
|
||||
do:
|
||||
stderr.writeLine("closing for webhook server at ", host, ":", port)
|
||||
close(server)
|
||||
|
||||
when isMainModule:
|
||||
runActor("webhooks") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnWebhookActor(turn, root)
|
|
@ -0,0 +1,55 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[asyncdispatch, json]
|
||||
import preserves
|
||||
import syndicate, syndicate/relays
|
||||
import ws
|
||||
|
||||
import ../schema/config, ../json_messages
|
||||
|
||||
type WebSocket = ws.WebSocket
|
||||
# not the object from the transportAddress schema
|
||||
|
||||
proc spawnWebsocketActor*(turn: var Turn; root: Cap): Actor =
|
||||
spawn("websocket-actor", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:WebsocketArguments) do (ds: Cap, url: string):
|
||||
let facet = turn.facet
|
||||
var
|
||||
ws: WebSocket
|
||||
connectedHandle: Handle
|
||||
newWebSocket(url).addCallback(turn) do (turn: var Turn; sock: WebSocket):
|
||||
ws = sock
|
||||
connectedHandle = publish(turn, ds, initRecord("connected", url.toPreserves))
|
||||
var fut: Future[(Opcode, string)]
|
||||
proc recvMessage() {.gcsafe.} =
|
||||
fut = receivePacket ws
|
||||
addCallback(fut, facet) do (turn: var Turn):
|
||||
let (opcode, data) = read fut
|
||||
case opcode
|
||||
of Text:
|
||||
message(turn, ds,
|
||||
RecvJson(data: data.parseJson))
|
||||
of Binary:
|
||||
message(turn, ds,
|
||||
initRecord("recv", cast[seq[byte]](data).toPreserves))
|
||||
of Ping:
|
||||
asyncCheck(turn, ws.send(data, Pong))
|
||||
of Pong, Cont:
|
||||
discard
|
||||
of Close:
|
||||
retract(turn, connectedHandle)
|
||||
stderr.writeLine "closed connection with ", url
|
||||
stop(turn)
|
||||
return
|
||||
recvMessage()
|
||||
recvMessage()
|
||||
onMessage(turn, ds, ?:SendJson) do (data: JsonNode):
|
||||
asyncCheck(turn, ws.send($data, Text))
|
||||
do:
|
||||
close(ws)
|
||||
|
||||
when isMainModule:
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnWebsocketActor(turn, root)
|
|
@ -2,7 +2,7 @@
|
|||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[options, parsexml, xmlparser, xmltree]
|
||||
import preserves, preserves/sugar, preserves/xmlhooks
|
||||
import preserves, preserves/xmlhooks
|
||||
import syndicate
|
||||
|
||||
import ../schema/[assertions, config]
|
||||
|
@ -17,18 +17,17 @@ proc translatePreserves(pr: Value): XmlTranslation {.gcsafe.} =
|
|||
var xn = result.pr.preservesTo(XmlNode)
|
||||
if xn.isSome: result.xml = $get(xn)
|
||||
|
||||
proc spawnXmlTranslator*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "xml-translator") do (turn: Turn):
|
||||
proc spawnXmlTranslator*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("xml-translator", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:XmlTranslatorArguments) do (ds: Cap):
|
||||
let xmlPat = observePattern(!XmlTranslation, {@[%0]:grab()})
|
||||
during(turn, ds, xmlPat) do (xs: Literal[string]):
|
||||
let obsPat = ?Observe(pattern: !XmlTranslation)
|
||||
during(turn, ds, obsPat ?? {0: grab()}) do (xs: Literal[string]):
|
||||
publish(turn, ds, translateXml(xs.value))
|
||||
let prPat = observePattern(!XmlTranslation, {@[%1]:grab()})
|
||||
during(turn, ds, prPat) do (pr: Literal[Value]):
|
||||
during(turn, ds, obsPat ?? {1: grab()}) do (pr: Literal[Value]):
|
||||
publish(turn, ds, translatePreserves(pr.value))
|
||||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnXmlTranslator(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnXmlTranslator(turn, root)
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[os, strutils]
|
||||
import preserves, preserves/sugar, syndicate
|
||||
import ./schema/[assertions, config]
|
||||
import preserves, syndicate
|
||||
import ../schema/[assertions, config]
|
||||
|
||||
{.passC: staticExec("pkg-config --cflags libxslt").}
|
||||
{.passL: staticExec("pkg-config --libs libxslt").}
|
||||
|
@ -173,11 +173,11 @@ proc toPreservesHook*(xn: xmlNodePtr): Value =
|
|||
preserveSiblings(items, xn)
|
||||
items[0]
|
||||
|
||||
proc spawnXsltActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
||||
spawnActor(turn, "xslt") do (turn: Turn):
|
||||
proc spawnXsltActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("xslt", turn) do (turn: var Turn):
|
||||
initLibXml()
|
||||
during(turn, root, ?:XsltArguments) do (ds: Cap):
|
||||
let sheetsPat = observePattern(!XsltTransform, {@[%0]: grab(), @[%1]: grab()})
|
||||
let sheetsPat = ?Observe(pattern: !XsltTransform) ?? {0: grab(), 1: grab()}
|
||||
during(turn, ds, sheetsPat) do (stylesheet: Literal[string], input: Literal[string]):
|
||||
let cur = loadStylesheet(stylesheet.value)
|
||||
if cur.isNil:
|
||||
|
@ -206,6 +206,6 @@ proc spawnXsltActor*(turn: Turn; root: Cap): Actor {.discardable.} =
|
|||
|
||||
when isMainModule:
|
||||
import syndicate/relays
|
||||
runActor("main") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
spawnXsltActor(turn, ds)
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnXsltActor(turn, root)
|
|
@ -0,0 +1,133 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
## This was all Tony's idea, except for the silly name.
|
||||
|
||||
import std/[asyncdispatch, os, terminal]
|
||||
import preserves
|
||||
import syndicate, syndicate/[durings, relays]
|
||||
import illwill
|
||||
|
||||
proc exitProc() {.noconv.} =
|
||||
illwillDeinit()
|
||||
showCursor()
|
||||
quit QuitSuccess
|
||||
|
||||
setControlCHook(exitProc)
|
||||
|
||||
proc parsePattern(pr: Value): Pattern =
|
||||
let
|
||||
dropSigil = initRecord("lit", "_".toSymbol)
|
||||
grabSigil = initRecord("lit", "?".toSymbol)
|
||||
var pr = grab(pr).toPreserves
|
||||
apply(pr) do (pr: var Value):
|
||||
if pr == dropSigil:
|
||||
pr = initRecord("_")
|
||||
elif pr == grabSigil:
|
||||
pr = initRecord("bind", initRecord("_"))
|
||||
doAssert result.fromPreserves(pr)
|
||||
|
||||
proc inputPattern: Pattern =
|
||||
var args = commandLineParams()
|
||||
if args.len != 1:
|
||||
quit "expected a single pattern argument"
|
||||
else:
|
||||
var input = pop args
|
||||
if input == "":
|
||||
quit "expected Preserves Pattern on stdin"
|
||||
else:
|
||||
var pr: Value
|
||||
try: pr = decodePreserves(input)
|
||||
except ValueError: discard
|
||||
try: pr = parsePreserves(input)
|
||||
except ValueError: discard
|
||||
if pr.isFalse:
|
||||
quit "failed to parse Preserves argument"
|
||||
result = parsePattern(pr)
|
||||
|
||||
type TermEntity {.final.} = ref object of Entity
|
||||
pattern: Pattern
|
||||
value: Value
|
||||
|
||||
method publish(te: TermEntity; turn: var Turn; v: AssertionRef; h: Handle) =
|
||||
te.value = v.value
|
||||
var termBuf = newTerminalBuffer(terminalWidth(), terminalHeight())
|
||||
var y = 1
|
||||
termBuf.write(1, y, $te.pattern, styleBright)
|
||||
inc(y)
|
||||
termBuf.drawHorizLine(0, termBuf.width(), y)
|
||||
inc(y)
|
||||
termBuf.write(0, y, $h, styleBright)
|
||||
for i, e in te.value.sequence:
|
||||
inc(y)
|
||||
termBuf.write(1, y, $e)
|
||||
termBuf.display()
|
||||
|
||||
method retract(te: TermEntity; turn: var Turn; h: Handle) =
|
||||
var termBuf = newTerminalBuffer(terminalWidth(), terminalHeight())
|
||||
var y = 1
|
||||
termBuf.write(1, y, $te.pattern, styleDim)
|
||||
inc y
|
||||
termBuf.drawHorizLine(0, termBuf.width(), y, true)
|
||||
inc(y)
|
||||
termBuf.write(0, y, $h, styleBright)
|
||||
if te.value.isSequence:
|
||||
for i, e in te.value.sequence:
|
||||
inc(y)
|
||||
termBuf.write(1, y, $e)
|
||||
else:
|
||||
inc(y)
|
||||
termBuf.write(1, y, $te.value)
|
||||
termBuf.display()
|
||||
|
||||
type DumpEntity {.final.} = ref object of Entity
|
||||
discard
|
||||
|
||||
method publish(dump: DumpEntity; turn: var Turn; ass: AssertionRef; h: Handle) =
|
||||
stdout.writeLine($ass.value)
|
||||
stdout.flushFile()
|
||||
|
||||
method message*(dump: DumpEntity; turn: var Turn; ass: AssertionRef) =
|
||||
stdout.writeLine($ass.value)
|
||||
stdout.flushFile()
|
||||
|
||||
proc exit {.noconv.} =
|
||||
illwillDeinit()
|
||||
showCursor()
|
||||
quit()
|
||||
|
||||
setControlCHook(exit)
|
||||
|
||||
proc main =
|
||||
let
|
||||
route = envRoute()
|
||||
pat = inputPattern()
|
||||
|
||||
if stdout.is_a_TTY:
|
||||
illwillInit()
|
||||
hideCursor()
|
||||
|
||||
discard bootDataspace("syndex_card") do (turn: var Turn; root: Cap):
|
||||
resolve(turn, root, route) do (turn: var Turn; ds: Cap):
|
||||
var termBuf = newTerminalBuffer(terminalWidth(), terminalHeight())
|
||||
termBuf.write(1, 1, $pat, styleBright)
|
||||
termBuf.drawHorizLine(1, termBuf.width(), 2)
|
||||
termBuf.display()
|
||||
|
||||
discard observe(turn, ds, pat, TermEntity(pattern: pat))
|
||||
|
||||
while true:
|
||||
try: poll()
|
||||
except CatchableError:
|
||||
illwillDeinit()
|
||||
showCursor()
|
||||
quit getCurrentExceptionMsg()
|
||||
|
||||
else:
|
||||
let entity = DumpEntity()
|
||||
runActor("syndex_card") do (root: Cap; turn: var Turn):
|
||||
spawnRelays(turn, root)
|
||||
resolve(turn, root, route) do (turn: var Turn; ds: Cap):
|
||||
discard observe(turn, ds, pat, entity)
|
||||
|
||||
main()
|
|
@ -1,69 +0,0 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[os, tables]
|
||||
import preserves, syndicate, syndicate/[durings, relays]
|
||||
|
||||
proc parsePattern(pr: Value): Pattern =
|
||||
let
|
||||
dropSigil = initRecord("lit", "_".toSymbol)
|
||||
grabSigil = initRecord("lit", "?".toSymbol)
|
||||
var pr = grab(pr).toPreserves
|
||||
apply(pr) do (pr: var Value):
|
||||
if pr == dropSigil:
|
||||
pr = initRecord("_")
|
||||
elif pr == grabSigil:
|
||||
pr = initRecord("bind", initRecord("_"))
|
||||
doAssert result.fromPreserves(pr)
|
||||
|
||||
proc inputPatterns: seq[Pattern] =
|
||||
var args = commandLineParams()
|
||||
result.setLen(args.len)
|
||||
for i, input in args:
|
||||
try: result[i] = input.parsePreserves.parsePattern
|
||||
except ValueError:
|
||||
quit "failed to parse Preserves argument"
|
||||
|
||||
type DumpEntity {.final.} = ref object of Entity
|
||||
assertions: Table[Handle, seq[Value]]
|
||||
|
||||
proc toLine(values: seq[Value]; prefix: char): string =
|
||||
result = newStringOfCap(1024)
|
||||
let sep = getEnv("FS", " ")
|
||||
result.add(prefix)
|
||||
for v in values:
|
||||
add(result, sep)
|
||||
add(result, $v)
|
||||
add(result, '\n')
|
||||
|
||||
method publish(dump: DumpEntity; turn: Turn; ass: AssertionRef; h: Handle) =
|
||||
var values = ass.value.sequence
|
||||
stdout.write(values.toLine('+'))
|
||||
stdout.flushFile()
|
||||
dump.assertions[h] = values
|
||||
|
||||
method retract(dump: DumpEntity; turn: Turn; h: Handle) =
|
||||
var values: seq[Value]
|
||||
if dump.assertions.pop(h, values):
|
||||
stdout.write(values.toLine('-'))
|
||||
stdout.flushFile()
|
||||
|
||||
method message*(dump: DumpEntity; turn: Turn; ass: AssertionRef) =
|
||||
stdout.write(ass.value.sequence.toLine('!'))
|
||||
stdout.flushFile()
|
||||
|
||||
proc exitProc() {.noconv.} =
|
||||
stdout.write('\n')
|
||||
quit()
|
||||
|
||||
proc main =
|
||||
let
|
||||
patterns = inputPatterns()
|
||||
entity = DumpEntity()
|
||||
runActor("syndex_card") do (turn: Turn):
|
||||
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
|
||||
for pat in patterns:
|
||||
discard observe(turn, ds, pat, entity)
|
||||
|
||||
setControlCHook(exitProc)
|
||||
main()
|
|
@ -1,13 +1,13 @@
|
|||
# Package
|
||||
|
||||
version = "20240509"
|
||||
version = "20240319"
|
||||
author = "Emery Hemingway"
|
||||
description = "Utilites for Syndicated Actors and Synit"
|
||||
license = "unlicense"
|
||||
srcDir = "src"
|
||||
bin = @["http_client", "mintsturdyref", "mount_actor", "msg", "postgre_actor", "preserve_process_environment", "rofi_script_actor", "sqlite_actor", "syndesizer", "syndump", "xslt_actor"]
|
||||
bin = @["mintsturdyref", "mount_actor", "msg", "net_mapper", "preserve_process_environment", "syndesizer", "syndex_card"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "https://git.syndicate-lang.org/ehmry/syndicate-nim.git >= 20240507", "https://github.com/ehmry/nim-sys.git#4ef3b624db86e331ba334e705c1aa235d55b05e1", "https://git.sr.ht/~ehmry/nim_taps >= 20240405"
|
||||
requires "syndicate#b209548f5d15f7391c08fcaec3615ed843f8a410", "https://git.sr.ht/~ehmry/nim_taps#6f1252d0d17cd56fd707b831c893758ddca08755"
|
||||
|
|
Loading…
Reference in New Issue