Skip to content
/ ski Public

ski is a JavaScript runtime environment for Go powered by Goja.

License

Notifications You must be signed in to change notification settings

shiroyk/ski

Repository files navigation

ski

GitHub go.mod Go version Go Report Card GitHub
ski is a JavaScript runtime environment for Go powered by Goja.

Description

ski provides a collection of built-in modules that implement partial Node.js compatibility and Web APIs, allowing you to write JavaScript code that can seamlessly interact with Go.

The modules include essential functionality like Buffer manipulation, encoding/decoding, HTTP client (fetch), streams, timers, and URL handling.

Modules

Partial Node.js compatibility and web standard implementations.

buffer

buffer module implements.

  • Buffer
  • Blob
  • File
export default async function () {
  console.log(Buffer.from("Y2lhbGxv", "base64").toString());
  const blob = new Blob(["hello world"], { type: "text/plain" });
  console.log(await blob.text());
}

encoding

encoding module provides base64 decode/encode and TextDecoder/TextEncoder.

  • base64
  • TextDecoder
  • TextEncoder
export default function () {
  const encoder = new TextEncoder();
  const data = encoder.encode("hello");
  return encoding.base64.encode(data);
}

fetch

fetch module provides HTTP client functionality. Web API implementations:

  • fetch
  • FormData
  • Headers
  • Request
  • Response
  • AbortController
  • AbortSignal

Other:

  • cookieJar
  • http
export default async () => {
  const res = await fetch("http://example.com", {
    headers: {
      "X-Custom": "custom value"
    }
  });
  console.log(await response.text());
}

stream

stream module implements.

  • ReadableStream
export default async () => {
  const res = new Response("test");
  const reader = await res.body.getReader();
  // ...
}

timers

timers module provides JavaScript timer functions.

export default async () => {
  return await new Promise((resolve) => {
    let count = 0;
    const id = setInterval(() => {
      count++;
    }, 100);

    setTimeout(() => {
      clearInterval(id);
      resolve(count);
    }, 250);
  });
}

url

url module implements WHATWG URL Standard.

  • URL
  • URLSearchParams
export default async () => {
  console.log(new URL('http://example.com'));
}

cache

cache module provides for store string or bytes.

import cache from "ski/cache";

export default function () {
  cache.set("hello", "world");
}

crypto

crypto module provides cryptographic functionality including hashing and encryption/decryption.

Supported algorithms:

  • aes
  • des
  • md5
  • hmac
  • ripemd160
  • tripleDes
  • sha1
  • sha256
  • sha384
  • sha512
  • sha512_224
  • sha512_256
import crypto from "ski/crypto";

export default function () {
  return crypto.md5('hello').hex();
}

Example

Vue.js Server side rendering.
See more examples in examples.

package main

import (
	"context"

	"github.com/shiroyk/ski"
	"github.com/shiroyk/ski/js"
	"github.com/shiroyk/ski/js/modulestest"
	"github.com/shiroyk/ski/modules"
)

const app = `
const app = createSSRApp({
	data: () => ({ count: 1 }),
	render() {
		return h('h1', { onClick: () => this.count++ }, "Count: " + this.count) 
	}
});
`

const index = `<!DOCTYPE html>
<html>
  <head>
	<title>Vue SSR Example</title>
	<style>
	#app {
	  display: flex; align-items: center; justify-content: center; 
	}
	</style>
	<script type="importmap">
	  {"imports":{"vue":"https://esm.sh/vue@3"}}
	</script>
	<script type="module">
		import { h, createSSRApp } from 'vue';
		` + app + `
		app.mount('#app');
	</script>
  </head>
  <body>
	<div id="app">${html}</div>
  </body>
</html>`

func main() {
	modules.Register("server", modules.ModuleFunc(modulestest.HttpServer))
	module, err := js.CompileModule(`module`, `
	import { h, createSSRApp } from "https://esm.sh/vue@3";
	import { renderToString } from "https://esm.sh/@vue/server-renderer@3";
	import createServer from "ski/server";

        createServer("localhost:8000", async (req, res) => {
		`+app+`
		const html = await renderToString(app);
		res.end(`+"`"+index+"`"+`);
	});
    	`)
	if err != nil {
		panic(err)
	}

	_, err = ski.RunModule(context.Background(), module)
	if err != nil {
		panic(err)
	}
}

License

ski is distributed under the MIT license.

About

ski is a JavaScript runtime environment for Go powered by Goja.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published