Skip to content

Commit

Permalink
Merge pull request #337 from HapticX/dev
Browse files Browse the repository at this point in the history
v4.5.0
  • Loading branch information
Ethosa committed Aug 29, 2024
2 parents 3bdb813 + 694aad9 commit 5c0d3cb
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 148 deletions.
2 changes: 1 addition & 1 deletion happyx.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

description = "Macro-oriented asynchronous web-framework written with ♥"
author = "HapticX"
version = "4.4.4"
version = "4.5.0"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
Expand Down
4 changes: 2 additions & 2 deletions src/happyx/core/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ const
nim_2_0_0* = (NimMajor, NimMinor, NimPatch) >= (2, 0, 0)
# Framework version
HpxMajor* = 4
HpxMinor* = 4
HpxPatch* = 4
HpxMinor* = 5
HpxPatch* = 0
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch


Expand Down
82 changes: 19 additions & 63 deletions src/happyx/private/macro_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ proc formatNode*(node: NimNode): NimNode =
node


proc liveviewParam*(name: string): NimNode =
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident(name)),
ident(name)
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident(name))),
newDotExpr(ident"self", ident(name))
), newNimNode(nnkElse).add(
newLit""
))


proc useComponent*(statement: NimNode, inCycle, inComponent: bool,
cycleTmpVar: string, compTmpVar: NimNode, cycleVars: var seq[NimNode],
returnTagRef: bool = true, constructor: bool = false,
Expand Down Expand Up @@ -229,69 +241,13 @@ proc useComponent*(statement: NimNode, inCycle, inComponent: bool,
objConstr.add(stringId)
when not defined(js) and enableLiveViews:
objConstr.add(
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"urlPath"),
ident"urlPath"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"urlPath")),
newDotExpr(ident"self", ident"urlPath")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"hostname"),
ident"hostname"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"hostname")),
newDotExpr(ident"self", ident"hostname")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"query"),
ident"query"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"query")),
newDotExpr(ident"self", ident"query")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"queryArr"),
ident"queryArr"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"queryArr")),
newDotExpr(ident"self", ident"queryArr")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"reqMethod"),
ident"reqMethod"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"reqMethod")),
newDotExpr(ident"self", ident"reqMethod")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"inCookies"),
ident"inCookies"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"inCookies")),
newDotExpr(ident"self", ident"inCookies")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"headers"),
ident"headers"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"headers")),
newDotExpr(ident"self", ident"headers")
), newNimNode(nnkElse).add(
newLit""
)),
liveviewParam("urlPath"),
liveviewParam("hostname"),
liveviewParam("query"),
liveviewParam("queryArr"),
liveviewParam("reqMethod"),
liveviewParam("inCookies"),
liveviewParam("headers"),
)
if statement[1].kind == nnkCall:
for i in 1..<statement[1].len:
Expand Down
25 changes: 22 additions & 3 deletions src/happyx/routing/mounting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import
std/macrocache,
std/strformat,
# HappyX
../core/[exceptions]
../core/[exceptions],
./decorators

when not declared(CacheTable.hasKey):
import ../private/macro_utils
Expand All @@ -35,10 +36,22 @@ proc findAndReplaceMount*(body: NimNode) =
##
## Don't use it in product
##
var offset = 0
var
offset = 0
nextRouteDecorators: seq[NimNode] = @[]
for i in 0..<body.len:
let idx = i+offset
if body[idx].kind == nnkCommand and body[idx][0] == ident"mount":
# Decorators
if body[idx].kind == nnkPrefix and $body[idx][0] == "@" and body[idx][1].kind == nnkIdent:
# @Decorator
nextRouteDecorators.add(body[idx].copy())
# @Decorator()
elif body[idx].kind == nnkCall and body[idx][0].kind == nnkPrefix and $body[idx][0][0] == "@" and body[idx].len == 1:
nextRouteDecorators.add(body[idx].copy())
# @Decorator(arg1, arg2, ...)
elif body[idx].kind == nnkCall and body[idx][0].kind == nnkPrefix and $body[idx][0][0] == "@" and body[idx].len > 1:
nextRouteDecorators.add(body[idx].copy())
elif body[idx].kind == nnkCommand and body[idx][0] == ident"mount":
if body[idx][1].kind == nnkInfix and body[idx][1][0] == ident"->":
# handle mount
let
Expand All @@ -63,10 +76,16 @@ proc findAndReplaceMount*(body: NimNode) =
elif statement[1].kind in [nnkStrLit, nnkTripleStrLit]:
statement[1] = newLit($route & $statement[1])
# Add mount routes
for decorator in nextRouteDecorators:
inc offset
body.insert(i, decorator)
if (statement.kind in [nnkCall, nnkCommand] and $statement[0] != "mount") or
(statement.kind == nnkPrefix and $statement[0] == "@"):
inc offset
body.insert(i, statement)
nextRouteDecorators = @[]
else:
nextRouteDecorators = @[]


macro mount*(mountName, body: untyped): untyped =
Expand Down
70 changes: 7 additions & 63 deletions src/happyx/spa/components.nim
Original file line number Diff line number Diff line change
Expand Up @@ -390,81 +390,25 @@ macro component*(name, body: untyped): untyped =
initObjConstr.add(
newColonExpr(
ident"urlPath",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"urlPath"),
ident"urlPath"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"urlPath")),
newDotExpr(ident"self", ident"urlPath")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("urlPath")
), newColonExpr(
ident"hostname",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"hostname"),
ident"hostname"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"hostname")),
newDotExpr(ident"self", ident"hostname")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("hostname")
), newColonExpr(
ident"query",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"query"),
ident"query"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"query")),
newDotExpr(ident"self", ident"query")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("query")
), newColonExpr(
ident"queryArr",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"queryArr"),
ident"queryArr"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"queryArr")),
newDotExpr(ident"self", ident"queryArr")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("queryArr")
), newColonExpr(
ident"reqMethod",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"reqMethod"),
ident"reqMethod"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"reqMethod")),
newDotExpr(ident"self", ident"reqMethod")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("reqMethod")
), newColonExpr(
ident"inCookies",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"inCookies"),
ident"inCookies"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"inCookies")),
newDotExpr(ident"self", ident"inCookies")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("inCookies")
), newColonExpr(
ident"headers",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"headers"),
ident"headers"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"headers")),
newDotExpr(ident"self", ident"headers")
), newNimNode(nnkElse).add(
newLit""
))
liveviewParam("headers")
),
)
initProc[2] = generics.copy()
Expand Down
9 changes: 6 additions & 3 deletions src/happyx/spa/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ when enableDefaultComponents:
var components* = newTable[cstring, BaseComponent]()
else:
var
requestResult* = newTable[string, string]()
componentsResult* = newTable[string, string]()
requestResult* {.threadvar.}: TableRef[string, string]
componentsResult* {.threadvar.}: TableRef[string, string]
requestResult = newTable[string, string]()
componentsResult = newTable[string, string]()


when defined(js):
Expand Down Expand Up @@ -213,12 +215,13 @@ else:
requestResult[host] = "route:" & path
proc injectJs*(host, script: string) =
requestResult[host] = "script:" & fmt"<script>{script}</script>"
proc js*(host, script: string) =
requestResult[host] = "script:" & fmt"<script>{script}</script>"
proc route*(comp: BaseComponent, path: string) =
componentsResult[comp.uniqCompId] = "route:" & path
proc js*(comp: BaseComponent, script: string) =
componentsResult[comp.uniqCompId] = "script:" & fmt"<script>{script}</script>"
proc rerender*(query, queryArr, reqMethod, inCookies, headers: auto, hostname, urlPath: string) =
echo "rerender for " & hostname
requestResult[hostname] = "rerender:" & liveviewRoutes[urlPath](
query, queryArr, reqMethod, inCookies, headers, nil
).children[1].ugly()
Expand Down
23 changes: 17 additions & 6 deletions src/happyx/spa/tag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ proc tagFromString*(source: string): TagRef {.inline.} =
let xmlNode = parseHtml(source)
result = initTag("div", @[], true)
result.xmlTree2Tag(nil, xmlNode)
result = result.children[0].children[0].TagRef
when defined(js):
result = result.children[0].children[0].TagRef
else:
result = result.children[0].children[0]


proc addArg*(self: TagRef, arg: string) =
Expand All @@ -467,11 +470,13 @@ proc addArgIter*(self: TagRef, arg: string) =
## See also `addArg function #addArg,TagRef,string`_
when defined(js):
self.setAttribute(cstring(arg), "")
for i in self.children:
i.TagRef.addArgIter(arg)
else:
if self.args.len == 0:
self.args.add(arg)
for i in self.children:
i.TagRef.addArgIter(arg)
for i in self.children:
i.addArgIter(arg)


proc toSeqIter*(self: TagRef): seq[TagRef] =
Expand All @@ -485,7 +490,7 @@ proc toSeqIter*(self: TagRef): seq[TagRef] =
result.add(i)
else:
for child in self.children:
for i in child.TagRef.toSeqIter:
for i in child.toSeqIter:
result.add(i)
return result

Expand All @@ -506,7 +511,10 @@ func lvl*(self: TagRef): int =
var tag = self
when defined(js):
while not tag.parentElement.isNil:
tag = tag.parentElement.TagRef
when defined(js):
tag = tag.parentElement.TagRef
else:
tag = tag.parentElement
if not tag.onlyChildren:
inc result
else:
Expand All @@ -527,7 +535,10 @@ func `[]`*(self: TagRef, attrName: string): string =

func `[]`*(self: TagRef, index: int): TagRef =
## Returns tag by index
self.children[index].TagRef
when defined(js):
self.children[index].TagRef
else:
self.children[index]


func `[]=`*(self: TagRef, attrName: string, attrValue: string) =
Expand Down
4 changes: 1 addition & 3 deletions src/happyx/ssr/form_data.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type FormDataItem* = object
proc parseFormData*(formData: string): (StringTableRef, TableRef[string, FormDataItem]) =
## Parses `form-data` into `StringTableRef`
result = (newStringTable(), newTable[string, FormDataItem]())
let
lineSeparator = "\r\n"
lines = formData.split("\r\n")
let lines = formData.split("\r\n")
var
key = ""
data = ""
Expand Down
1 change: 0 additions & 1 deletion src/happyx/ssr/request_models.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import
# stdlib
std/macros,
std/macrocache,
std/strtabs,
std/strutils,
std/strformat,
# Happyx
Expand Down
3 changes: 3 additions & 0 deletions src/happyx/ssr/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,9 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
), statement[2]
))
nextRouteDecorators = @[]
# mount ...
elif statement.kind == nnkCommand and statement[0] == ident"mount" and statement[1].kind == nnkInfix and statement[1][0] == ident"->":
nextRouteDecorators = @[]
# reqMethod "/...":
# ...
elif statement[0].kind == nnkIdent and statement[0] != ident"mount" and statement[1].kind in {nnkStrLit, nnkTripleStrLit, nnkInfix}:
Expand Down
2 changes: 1 addition & 1 deletion src/happyx/ssr/types.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import
std/logging,
std/tables,
../spa/[tag, renderer],
../spa/renderer,
../core/constants

when enableHttpx:
Expand Down
7 changes: 5 additions & 2 deletions src/happyx/ssr/websockets.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import
std/macros,
../core/constants,
../private/macro_utils
../core/constants


when enableHttpBeast:
import ../private/macro_utils


proc handleWebsockets*(wsClosedConnection: NimNode): tuple[wsStmtList, insertWsList: NimNode] =
Expand Down
Loading

0 comments on commit 5c0d3cb

Please sign in to comment.