Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: openjscad.xyz/?uri= support for "data" scheme in addition to "http(s)" #1318

Open
Hermann-SW opened this issue Jan 16, 2024 · 7 comments
Labels

Comments

@Hermann-SW
Copy link
Contributor

Hermann-SW commented Jan 16, 2024

Expected Behavior

Providing a "data" scheme would work the same as providing "http" scheme uri.

I tried with below script, that opens $browser with "...?uri=data:text/plain;base64,..." for passed input file.
But it does not work because of missing "data" scheme support.

pi@raspberrypi5:~/JSCAD $ cat doit
#!/bin/bash
browser=firefox
#browser=chromium-browser

$browser "openjscad.xyz?uri=data:text/plain;base64,`base64 -w 0 $1`"
pi@raspberrypi5:~/JSCAD $ 

Actual Behavior

Fails to open script passed via "data" scheme.

Steps to Reproduce the Problem

  1. execute ./doit script.js and see the error message on openjscad.org

Specifications

  • Version: latest
  • Platform: openjscad.xyz
  • Environment: browser

Biggest gain of "data" scheme support would be being able to share link without having to store script on a publically accessible webserver first.

I have implemented similar concept in my GraphvizFiddle, "Share" button creates a long URL containing configuration that can be shared, here an example:
https://stamm-wilbrandt.de/GraphvizFiddle/?1705330894960#digraph%20G%20%7Brankdir%3DLR%3B%20JS-%3ECAD%7D%0A

I am aware of the 8KB URL length limit of openjscad.xyz, but "data:text/plain;base64," would allow for up to 6KB length scripts. "data:application/gzip;base64," would allow for much larger scripts, with their gziped size limited by 6KB.

P.S:
In case "data" scheme will be added, a script like below would be nice to have in the repo, for direct use (eg. under OpenJSCAD.org/packages/examples):

#!/bin/bash
#browser=firefox
browser=chromium-browser

$browser "openjscad.xyz?uri=data:application/gzip;base64,`gzip -c $1 | base64 -w 0`"
@hrgdavor
Copy link
Contributor

@z3dev if I remember correctly you are like me not so keen fixing opejscad web ui.

https://github.com/hrgdavor/jscadui/tree/main/apps/jscad-web
jscad.app has come a long way and there this type of url works already. What would be the steps to make that prototype suitable to replace current official https://openjscad.xyz ?

https://jscad.app/#data:text/plain;charset=utf-8;base64,aW1wb3J0ICogYXMganNjYWQgZnJvbSAnQGpzY2FkL21vZGVsaW5nJwpjb25zdCB7IGN1YmUsIHNwaGVyZSB9ID0ganNjYWQucHJpbWl0aXZlcwoKZXhwb3J0IGNvbnN0IG1haW4gPSAoKSA9PiB7CiAgcmV0dXJuICAgICBzcGhlcmUoeyByYWRpdXM6IDE0IH0pCn0=

also the multifile scripts with import like this jscad example:

https://jscad.app/#https://raw.githubusercontent.com/jscad/OpenJSCAD.org/master/packages/examples/import/STLImport/index.js
image

there is also support for new input type=file

so script like this can allow uploading custom stl to work on
https://jscad.app/#https://raw.githubusercontent.com/gilboonet/gilboonet.github.io/master/sq_edit/sqEdit.js

image

remote file download is first attempted from browser directly, and if CORS headers are not ok, it falls back to remote serverside proxy

@Hermann-SW
Copy link
Contributor Author

Hermann-SW commented Jan 17, 2024

@z3dev if I remember correctly you are like me not so keen fixing opejscad web ui.

I take what works.

https://github.com/hrgdavor/jscadui/tree/main/apps/jscad-web jscad.app has come a long way and there this type of url works already. What would be the steps to make that prototype suitable to replace current official https://openjscad.xyz ?

https://jscad.app/#data:text/plain;charset=utf-8;base64,aW1wb3J0ICogYXMganNjYWQgZnJvbSAnQGpzY2FkL21vZGVsaW5nJwpjb25zdCB7IGN1YmUsIHNwaGVyZSB9ID0ganNjYWQucHJpbWl0aXZlcwoKZXhwb3J0IGNvbnN0IG1haW4gPSAoKSA9PiB7CiAgcmV0dXJuICAgICBzcGhlcmUoeyByYWRpdXM6IDE0IH0pCn0=

Nice.

also the multifile scripts with import like this jscad example:

Good.

I tested jscad.app and it seems to even load same model a bit faster than openjscad.xyz.

jscad.app problems:

  1. My minimal code does work on openjscad.xyz, but not on jscad.app:
    https://openjscad.xyz/?uri=https://stamm-wilbrandt.de/en/forum/JSCAD.minimal.js

    If it works on openjscad.xyz, why does jscad.app have a problem with it?

  2. In case you want to replace openjscad.xyz, functional parity on scripts is needed

@hrgdavor
Copy link
Contributor

@Hermann-SW
https://jscad.app/remote?url=https://stamm-wilbrandt.de/en/forum/JSCAD.minimal.js
returns a more complicated script for some reason
image
that script is executed fine, and does look like one of yours :)

and direct url from my browser returns the simple sphere script
https://stamm-wilbrandt.de/en/forum/JSCAD.minimal.js

could be some kind of caching issue ? did you have a different script on that url ?
@platypii could you investigate the remote script ?

@hrgdavor
Copy link
Contributor

hrgdavor commented Jan 17, 2024

@Hermann-SW also here is excerpt from jscadui app about CORS. this can benefit you also if hosting jascad, as you can then load external scripts without making a proxy script

using url to load external script and CORS

If you want to share a script from your website you should setup CORS, and make sure to use HTTPS!

if you do not setup CORS jscad.app can fallback to /remote to download the script, but this workaround may not be available forever (such enpoint could be abused to hide IP for attacks).

For hostings (that are uaually cheap and abundant) on CPanel adding .htaccess to your folder should work.

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
</IfModule>

If you are using github you should be fine, as gists and github pages have those CORS headers.

@Hermann-SW
Copy link
Contributor Author

Hermann-SW commented Jan 17, 2024

@hrgdavor
Can you please download
https://stamm-wilbrandt.de/en/forum/JSCAD.minimal.js
with wget?

It should look like this:

module.exports=function main(){return require('@jscad/modeling').primitives.sphere({radius: 50})}

Copy that script into jscad.app and press SHIFT+ENTER, that gives bottom message:

Error: no main function exported

Why does that script work with openjscad.xyz, and does not work with jscad.app?

@Hermann-SW
Copy link
Contributor Author

Hermann-SW commented Jan 17, 2024

@hrgdavor
This script works, opens a local file base64 encoded with data URL in jscad.app:

pi@raspberrypi5:~/JSCAD $ cat doit.b 
#!/bin/bash
browser=firefox
browser=chromium-browser

$browser "jscad.app/#data:text/plain;base64,`base64 -w 0 $1`"
pi@raspberrypi5:~/JSCAD $ 

The URL length jscad.app allows for is bigger than the 8KB of openjscad.xyz, I have not tested how big it is. In order to use data link for bigger scripts as well I would like to use this script:

pi@raspberrypi5:~/JSCAD $ cat doit2.b 
#!/bin/bash
browser=firefox
browser=chromium-browser

$browser "jscad.app/#data:application/gzip;base64,`gzip -c $1 | base64 -w 0`"
pi@raspberrypi5:~/JSCAD $ 

It creates the gzipped script in jscad.app editor, with many unreadable characters.
Would it be possible for jscad.app to gunzip the base64 decoded string in addition, when media type is "application/gzip" and not "text/plain"? That is what browsers do to my knowledge.

@hrgdavor
Copy link
Contributor

@Hermann-SW browsers only support gzip at transport level, not in data url

https://stackoverflow.com/questions/28793110/img-src-data-with-gzipped-file

since I use fflate for 3mf, this additional option to unzip the script could be implemented without too much trouble

regarding the script not working, it lloks like openjscad allows for default export to be the main function as well as object with .main function. I was unaware of that use case, so it is not possible right now, but can be added quickly.

also I am available on discord cadhub/jscad channel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants