Coder Social home page Coder Social logo

jlleitschuh / env3d-jsweet-http-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from env3d/env3d-jsweet-http-server

0.0 2.0 0.0 10.86 MB

Forked from jsweet-http-server, this version includes modifications to work with env3d-jsweet frontend.

License: Other

Dockerfile 1.11% Shell 0.38% Java 80.38% HTML 6.27% JavaScript 11.86%

env3d-jsweet-http-server's Introduction

Forked from the jsweet http server

We did this to include the env3d library, as well as modified the server to only transpile to Typescript, as Typescript -> Javascript will be performed on the client. Below is the original README for reference.

JSweet HTTP Server

This project provides light HTTP server with a JSON Rest Web API to run the JSweet transpiler from any client, without having to install JSweet locally.

The server is built with NanoHttpd.

It comes also with a small JSweet client, which is the JSweet sandbox. You can try it live at http://www.jsweet.org/jsweet-live-sandbox/.

How to use

To transpile a file from Java to TypeScript or JavaScript, one must invoke the transpile Web Service on the JSweet server.

The JSweet team has a publicly available server at: http://sandbox.jsweet.org.

Here is a basic client written in JSweet:

import static jsweet.dom.Globals.console;
import jsweet.dom.FormData;
import jsweet.dom.XMLHttpRequest;
import jsweet.lang.JSON;
import jsweet.lang.Math;
import org.jsweet.JSweetServerTranspilationResponse;
[...]

public class JSweetClient {
	// use your own server if you need to
	static final String SERVER_URL = "http://sandbox.jsweet.org";
	public static void doInvoke() { 
		// the actual service invocation
		XMLHttpRequest currentRequest = new XMLHttpRequest();
		currentRequest.open("POST", SERVER_URL + "/transpile", true);
		currentRequest.onload = (e) -> {
			JSweetServerTranspilationResponse response = (JSweetServerTranspilationResponse) JSON
					.parse(currentRequest.responseText);
			if (response.success) {
				// JavaScript output
  				String jsContent = response.jsout;
				// TypeScript output
				String tsContent = response.tsout;
				// do whatever with the result
				[...]
			}
			return null;
		};
		currentRequest.onerror = (e) -> {
		  [...]
			return null;
		};
		FormData data = new FormData();
		// here the actual code to transpile
		data.append("javaCode", "public class C {}");
		// should be unique
		data.append("tid", "" + Math.random());
		// set to false if you only want the JavaScript output
		data.append("tsout", "true");
		try {
			currentRequest.send(data);
		} catch (Exception requestError) {
			console.error(requestError);
		}
	}
}

Build and run the server

Before compiling, you can edit the pom.xml to add dependencies to your server, so that your service can transpile code using any of the available candies listed here.

To compile the server, use Maven in the project's directory:

> mvn clean compile

To run the service under Unix-based OS (would need to be adapted for other OS):

> ./run.sh

NOTE: for the JSweet transpiler to work, you will need to make sure that Node.js is installed on the server. For more information, go to https://github.com/cincheo/jsweet.

Build and run the client

To compile the client, use Maven in the project's directory:

mvn -P client generate-sources

Start the client (will open www/index.html):

> firefox <SERVER_URL>

env3d-jsweet-http-server's People

Contributors

renaudpawlak avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.