aboutsummaryrefslogtreecommitdiffstats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
committerbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
commit51d83224320b8bd4e81332802bb62158ae6deec5 (patch)
tree4bfb320d7960f78a68de857f26d43ff2f59bea57 /src/io
parent51ea622d58bfca19212fac1800cfb033bb85ec39 (diff)
downloadchawan-51d83224320b8bd4e81332802bb62158ae6deec5.tar.gz
More JS bindings
Diffstat (limited to 'src/io')
-rw-r--r--src/io/lineedit.nim4
-rw-r--r--src/io/loader.nim1
-rw-r--r--src/io/request.nim50
3 files changed, 41 insertions, 14 deletions
diff --git a/src/io/lineedit.nim b/src/io/lineedit.nim
index def2feb2..3f9034bf 100644
--- a/src/io/lineedit.nim
+++ b/src/io/lineedit.nim
@@ -167,8 +167,8 @@ proc readLine(state: var LineState): bool =
else:
state.fullRedraw()
of ACTION_LINED_DELETE:
- if state.cursor > 0 and state.cursor < state.news.len:
- let w = state.news[state.cursor - 1].lwidth()
+ if state.cursor >= 0 and state.cursor < state.news.len:
+ let w = state.news[state.cursor].lwidth()
state.news.delete(state.cursor..state.cursor)
if state.cursor == state.news.len and state.shift == 0:
state.kill(w)
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 7c68258e..1530bf5b 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -100,6 +100,7 @@ proc runFileLoader(loader: FileLoader, loadcb: proc()) =
quit(0)
proc doRequest*(loader: FileLoader, request: Request): Response =
+ new(result)
let stream = connectSocketStream(loader.process)
stream.swrite(request)
stream.flush()
diff --git a/src/io/request.nim b/src/io/request.nim
index 7553bdb5..6e6f3f40 100644
--- a/src/io/request.nim
+++ b/src/io/request.nim
@@ -2,12 +2,20 @@ import options
import streams
import tables
-import utils/twtstr
import types/url
+import js/javascript
+import utils/twtstr
type HttpMethod* = enum
- HTTP_CONNECT, HTTP_DELETE, HTTP_GET, HTTP_HEAD, HTTP_OPTIONS, HTTP_PATCH,
- HTTP_POST, HTTP_PUT, HTTP_TRACE
+ HTTP_CONNECT = "CONNECT"
+ HTTP_DELETE = "DELETE"
+ HTTP_GET = "GET"
+ HTTP_HEAD = "HEAD"
+ HTTP_OPTIONS = "OPTIONS"
+ HTTP_PATCH = "PATCH"
+ HTTP_POST = "POST"
+ HTTP_PUT = "PUT"
+ HTTP_TRACE = "TRACE"
type
Request* = ref RequestObj
@@ -18,13 +26,13 @@ type
body*: Option[string]
multipart*: Option[MimeData]
- Response* = object
+ Response* = ref object
body*: Stream
- res*: int
- contenttype*: string
- status*: int
- headers*: HeaderList
- redirect*: Option[Url]
+ res* {.jsget.}: int
+ contenttype* {.jsget.}: string
+ status* {.jsget.}: int
+ headers* {.jsget.}: HeaderList
+ redirect* {.jsget.}: Option[Url]
ReadableStream* = ref object of Stream
isource*: Stream
@@ -32,7 +40,7 @@ type
isend: bool
HeaderList* = ref object
- table*: Table[string, seq[string]]
+ table* {.jsget.}: Table[string, seq[string]]
# Originally from the stdlib
MimePart* = object
@@ -122,9 +130,9 @@ func newHeaderList*(table: Table[string, string]): HeaderList =
func newRequest*(url: Url,
httpmethod = HTTP_GET,
- headers: openarray[(string, string)] = [],
+ headers: seq[(string, string)] = @[],
body = none(string),
- multipart = none(MimeData)): Request =
+ multipart = none(MimeData)): Request {.jsctor.} =
new(result)
result.httpmethod = httpmethod
result.url = url
@@ -135,6 +143,16 @@ func newRequest*(url: Url,
result.body = body
result.multipart = multipart
+func newRequest*(url: Url,
+ httpmethod: HttpMethod,
+ headers: openarray[(string, string)],
+ body = none(string),
+ multipart = none(MimeData)): Request =
+ var s: seq[(string, string)]
+ for it in headers:
+ s.add(it)
+ return newRequest(url, httpmethod, s, body, multipart)
+
proc `[]=`*(multipart: var MimeData, k, v: string) =
multipart.content.add(MimePart(name: k, content: v))
@@ -154,3 +172,11 @@ func getOrDefault*(headers: HeaderList, k: string, default = ""): string =
headers.table[k][0]
else:
default
+
+proc readAll*(response: Response): string {.jsfunc.} =
+ return response.body.readAll()
+
+proc addRequestModule*(ctx: JSContext) =
+ ctx.registerType(Request)
+ ctx.registerType(Response)
+ ctx.registerType(HeaderList)