HapticX / happyx

Macro-oriented asynchronous web-framework written in Nim with β™₯
https://hapticx.github.io/happyx/
MIT License
536 stars 18 forks source link

walkDir does not work #239

Closed array-in-a-matrix closed 9 months ago

array-in-a-matrix commented 9 months ago

Describe the bug πŸ› A clear and concise description of what the bug is.

The walkDir iterator outputs nothing regardless of the contents of the directory.

To Reproduce πŸ‘¨β€πŸ”¬ Steps to reproduce the behavior:

  1. Create SPA
  2. Use this code:
    
    import happyx, os

appRoutes("app"): "/": tDiv: for i in walkDir("."): # does not work on all paths {i} # only an empty div is created regardless of how many files/directories exist

3. Run `hpx dev`

Output HTML:
```html
<html><head>
    <meta charset="utf8">
    <title>app</title>

  </head>
  <body>
    <div id="app"><div></div></div> <!-- only 1 empty div  created -->
    <script src="main.js"></script>
  <script>let socket = new WebSocket('ws://127.0.0.1:5000/hcr');
socket.onmessage = (event) => {
  if(event.data === 'true'){
    window.location.reload();
  }
};

function intervalSending(){
  socket.send('reload')
}

setInterval(intervalSending, 1000);
</script>
</body></html>

Expected behavior πŸ€” A clear and concise description of what you expected to happen.

The expected output should be like the following code:

import os
for i in walkDir("."):
  echo i # outputs current directory's contents

Using this code on https://play.nim-lang.org, outputs the following:

(kind: pcDir, path: "./nimble")
(kind: pcDir, path: "./nim")

Please complete the following information πŸ“ƒ

Additional context ✌ Add any other context about the problem here.

This may affect other iterators like walkDirs and walkDirRec. I did not check that though.

quimt commented 9 months ago

The expected output should be like the following code:

import os
for i in walkDir("."):
  echo i # outputs current directory's contents

Well, that code doesn't work when placed outside the route either, in global scope. It only works if you put it in a static:block, in which case it is run as nimscript during the compilation process. The issue with most of the path procs from std/os is that they are not supported in browser js scripts, since the browser sandboxes code and hides access to the directory structure on the front end.

Since a SPA project is on the front-end, you either want to show the directory structure in which the project exists, which can only be gotten at compiletime, or you want your SPA script to be able to search around independently on a visitor's directory structure, which is a huge security no-no that browser sandboxing is meant to avoid. Or, you don't want to use a SPA at all and instead want to use an SSR to dynamically explore your own file structure.

array-in-a-matrix commented 9 months ago

Oh right, everything is being loaded client side so the file directory isn't available. Thank you for your explanation! I will probably switch to SSR or SSG then.