orvyn-lang 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/GUIDE.md +198 -0
  2. package/LANGUAGE.md +818 -30
  3. package/README.md +211 -250
  4. package/brand/orvyn-sunset-animated.svg +15 -0
  5. package/package.json +3 -1
  6. package/src/cli/orvyn.js +207 -16
  7. package/src/core/signal.js +104 -6
  8. package/src/devtools/runtime.js +25 -1
  9. package/src/lang/a11y.js +152 -0
  10. package/src/lang/auth.js +239 -0
  11. package/src/lang/bundle-native.js +10 -6
  12. package/src/lang/codegen.js +615 -58
  13. package/src/lang/device.js +41 -0
  14. package/src/lang/doctor.js +60 -0
  15. package/src/lang/env.js +27 -0
  16. package/src/lang/errors.js +513 -0
  17. package/src/lang/form.js +69 -8
  18. package/src/lang/index.js +2 -1
  19. package/src/lang/lexer.js +2 -0
  20. package/src/lang/list.js +44 -0
  21. package/src/lang/native/android/MainActivity.kt +65 -4
  22. package/src/lang/native/android/OrvynDevice.kt +163 -0
  23. package/src/lang/native/android/OrvynEngine.kt +176 -7
  24. package/src/lang/native/android/OrvynRenderer.kt +970 -50
  25. package/src/lang/native/android.js +74 -11
  26. package/src/lang/native/assets.js +39 -0
  27. package/src/lang/native/build.js +140 -63
  28. package/src/lang/native/compatibility.js +57 -0
  29. package/src/lang/native/config.js +108 -0
  30. package/src/lang/native/ios/OrvynApp.swift +6 -0
  31. package/src/lang/native/ios/OrvynEngine.swift +291 -4
  32. package/src/lang/native/ios/OrvynViews.swift +769 -41
  33. package/src/lang/native/ios.js +81 -2
  34. package/src/lang/native-runtime.js +541 -55
  35. package/src/lang/native-size.js +53 -0
  36. package/src/lang/page-files.js +71 -0
  37. package/src/lang/page.js +63 -0
  38. package/src/lang/parser.js +278 -15
  39. package/src/lang/prerender.js +300 -0
  40. package/src/lang/read-import.js +12 -0
  41. package/src/lang/router.js +266 -0
  42. package/src/lang/runtime-shared.js +796 -10
  43. package/src/lang/runtime.js +399 -51
  44. package/src/lang/select.js +13 -0
  45. package/src/lang/server-code.js +233 -0
  46. package/src/lang/server-export.js +78 -0
  47. package/src/lang/server-libs.js +53 -0
  48. package/src/lang/server.js +409 -0
  49. package/src/lang/style-check.js +60 -0
  50. package/src/lang/test-runner.js +134 -0
  51. package/src/lang/types.js +317 -0
  52. package/src/lang/vite-plugin.js +108 -6
  53. package/src/lang/web-overlay.js +578 -0
  54. package/src/lang/widgets.js +94 -0
  55. package/src/lang/words.js +38 -0
  56. package/src/runtime/animations.js +24 -0
  57. package/src/runtime/appear.js +73 -0
  58. package/src/runtime/marquee.js +57 -0
  59. package/src/runtime/mount.js +120 -27
  60. package/src/runtime/patch.js +90 -11
  61. package/src/runtime/style.js +50 -9
  62. package/src/styling/fluid.js +129 -6
  63. package/src/styling/spec.js +319 -0
@@ -7,8 +7,13 @@ package dev.orvyn.runtime
7
7
 
8
8
  import android.annotation.SuppressLint
9
9
  import android.app.Activity
10
+ import android.content.Intent
11
+ import android.net.Uri
10
12
  import android.os.Handler
11
13
  import android.os.Looper
14
+ import android.security.keystore.KeyGenParameterSpec
15
+ import android.security.keystore.KeyProperties
16
+ import android.util.Base64
12
17
  import android.util.Log
13
18
  import android.webkit.ConsoleMessage
14
19
  import android.webkit.JavascriptInterface
@@ -17,6 +22,12 @@ import android.webkit.WebView
17
22
  import org.json.JSONObject
18
23
  import java.net.HttpURLConnection
19
24
  import java.net.URL
25
+ import java.security.KeyStore
26
+ import java.util.concurrent.ConcurrentHashMap
27
+ import javax.crypto.Cipher
28
+ import javax.crypto.KeyGenerator
29
+ import javax.crypto.SecretKey
30
+ import javax.crypto.spec.GCMParameterSpec
20
31
  import kotlin.concurrent.thread
21
32
 
22
33
  class OrvynEngine(
@@ -25,12 +36,54 @@ class OrvynEngine(
25
36
  private val onError: (String) -> Unit
26
37
  ) {
27
38
  private val webView = WebView(activity)
39
+ val device = OrvynDevice(activity) { id, json, error ->
40
+ js("__orvynDeviceDone($id, ${JSONObject.quote(json)}, ${JSONObject.quote(error)})")
41
+ }
28
42
  // saved state → SharedPreferences (kept after the app closes)
29
43
  private val storage = activity.getSharedPreferences("orvyn", android.content.Context.MODE_PRIVATE)
44
+ // secure credentials → KeyStore-encrypted SharedPreferences (separate from saved state)
45
+ private val secureStorage = activity.getSharedPreferences("orvyn_secure", android.content.Context.MODE_PRIVATE)
30
46
  private val main = Handler(Looper.getMainLooper())
47
+ private val fetchConnections = ConcurrentHashMap<Int, HttpURLConnection>()
48
+
49
+ private fun secretKey(): SecretKey {
50
+ val alias = "orvyn_secure_key"
51
+ val ks = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
52
+ (ks.getKey(alias, null) as? SecretKey)?.let { return it }
53
+ val gen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
54
+ gen.init(
55
+ KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
56
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
57
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
58
+ .setKeySize(256)
59
+ .build()
60
+ )
61
+ return gen.generateKey()
62
+ }
63
+
64
+ private fun encryptSecret(plain: String): String {
65
+ val cipher = Cipher.getInstance("AES/GCM/NoPadding")
66
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey())
67
+ val iv = cipher.iv
68
+ val encrypted = cipher.doFinal(plain.toByteArray(Charsets.UTF_8))
69
+ val combined = ByteArray(iv.size + encrypted.size)
70
+ System.arraycopy(iv, 0, combined, 0, iv.size)
71
+ System.arraycopy(encrypted, 0, combined, iv.size, encrypted.size)
72
+ return Base64.encodeToString(combined, Base64.NO_WRAP)
73
+ }
74
+
75
+ private fun decryptSecret(encoded: String): String? {
76
+ val combined = Base64.decode(encoded, Base64.NO_WRAP)
77
+ if (combined.size <= 12) return null
78
+ val iv = combined.copyOfRange(0, 12)
79
+ val encrypted = combined.copyOfRange(12, combined.size)
80
+ val cipher = Cipher.getInstance("AES/GCM/NoPadding")
81
+ cipher.init(Cipher.DECRYPT_MODE, secretKey(), GCMParameterSpec(128, iv))
82
+ return String(cipher.doFinal(encrypted), Charsets.UTF_8)
83
+ }
31
84
 
32
85
  @SuppressLint("SetJavaScriptEnabled")
33
- fun start() {
86
+ fun start(initialUrl: String? = null, devServer: String? = null) {
34
87
  webView.settings.javaScriptEnabled = true
35
88
  webView.addJavascriptInterface(Bridge(), "OrvynHost")
36
89
  webView.webChromeClient = object : WebChromeClient() {
@@ -40,16 +93,65 @@ class OrvynEngine(
40
93
  }
41
94
  }
42
95
 
43
- val app = activity.assets.open("app.js").bufferedReader().use { it.readText() }
44
- .replace("</script", "<\\/script")
96
+ if (devServer == null) {
97
+ load(activity.assets.open("app.js").bufferedReader().use { it.readText() }, initialUrl)
98
+ } else {
99
+ watchDevServer(devServer, initialUrl)
100
+ }
101
+ }
45
102
 
103
+ // `orvyn dev android`: load code from the computer and reload when it changes
104
+ private fun watchDevServer(server: String, initialUrl: String?) {
105
+ thread(isDaemon = true) {
106
+ var loaded: String? = null
107
+ while (!activity.isDestroyed) {
108
+ try {
109
+ val version = URL("$server/version").readText()
110
+ if (version != loaded) {
111
+ val code = URL("$server/app.js").readText()
112
+ loaded = version
113
+ main.post { load(code, initialUrl) }
114
+ }
115
+ } catch (error: Exception) {
116
+ if (loaded == null) {
117
+ loaded = ""
118
+ main.post { onError("Can't reach the Orvyn dev server. Is `orvyn dev android` still running?") }
119
+ }
120
+ }
121
+ Thread.sleep(1000)
122
+ }
123
+ }
124
+ }
125
+
126
+ // a fresh page, so nothing from the previous version keeps running
127
+ private fun load(code: String, initialUrl: String?) {
128
+ val app = code.replace("</script", "<\\/script")
129
+ val metrics = activity.resources.displayMetrics
130
+ val viewportWidth = metrics.widthPixels / metrics.density
131
+ val viewportHeight = metrics.heightPixels / metrics.density
132
+
133
+ val initialUrlJs = if (initialUrl != null) "window.__orvynInitialUrl = ${JSONObject.quote(initialUrl)};" else ""
46
134
  val shim = """
135
+ $initialUrlJs
136
+ window.__orvynPlatform = "android";
137
+ window.__orvynViewport = { width: $viewportWidth, height: $viewportHeight };
47
138
  window.__orvynRender = function (json) { OrvynHost.render(json) };
139
+ window.__orvynDevice = function (id, kind) { OrvynHost.device(id, kind) };
140
+ window.__orvynOpenExternal = function (url) { OrvynHost.openExternal(url) };
141
+ window.__orvynScrollTo = function (label) { OrvynHost.scrollTo(String(label)) };
48
142
  window.__orvynFetch = function (id, url, method, body, headers) {
49
143
  OrvynHost.fetch(id, url, method, body == null ? null : String(body), headers)
50
144
  };
145
+ window.__orvynAbortFetch = function (id) {
146
+ OrvynHost.abortFetch(id)
147
+ };
51
148
  window.__orvynStorageGet = function (key) { return OrvynHost.storageGet(key) };
52
- window.__orvynStorageSet = function (key, value) { OrvynHost.storageSet(key, value) };
149
+ window.__orvynStorageSet = function (key, value) { return OrvynHost.storageSet(key, value) };
150
+ window.__orvynStorageRemove = function (key) { return OrvynHost.storageRemove(key) };
151
+ window.__orvynSecureGet = function (key) { return OrvynHost.secureGet(key) };
152
+ window.__orvynSecureSet = function (key, value) { return OrvynHost.secureSet(key, value) };
153
+ window.__orvynSecureRemove = function (key) { return OrvynHost.secureRemove(key) };
154
+ window.__orvynSecureClear = function () { return OrvynHost.secureClear() };
53
155
  window.onerror = function (message) { OrvynHost.error(String(message)) };
54
156
  window.addEventListener("unhandledrejection", function (event) {
55
157
  OrvynHost.error(String(event.reason && event.reason.message || event.reason))
@@ -60,6 +162,14 @@ class OrvynEngine(
60
162
  webView.loadDataWithBaseURL("https://orvyn.local/", html, "text/html", "utf-8", null)
61
163
  }
62
164
 
165
+ fun openUrl(url: String) {
166
+ js("window.__orvynDeepLink && window.__orvynDeepLink(${JSONObject.quote(url)})")
167
+ }
168
+
169
+ fun goBack() {
170
+ js("window.__orvynBack && window.__orvynBack()")
171
+ }
172
+
63
173
  fun fire(handler: Int, value: String?) {
64
174
  if (value == null) {
65
175
  js("__orvynEvent($handler)")
@@ -73,18 +183,67 @@ class OrvynEngine(
73
183
  }
74
184
 
75
185
  inner class Bridge {
186
+ @JavascriptInterface
187
+ fun scrollTo(label: String) {
188
+ main.post { (activity as? MainActivity)?.scrollToLabel(label) }
189
+ }
190
+
191
+ @JavascriptInterface
192
+ fun openExternal(url: String) {
193
+ main.post {
194
+ try { activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) }
195
+ catch (error: Exception) { Log.w("Orvyn", "Could not open link", error) }
196
+ }
197
+ }
198
+
76
199
  @JavascriptInterface
77
200
  fun render(json: String) {
78
201
  val tree = JSONObject(json)
79
202
  main.post { onRender(tree) }
80
203
  }
81
204
 
205
+ @JavascriptInterface
206
+ fun device(id: Int, kind: String) {
207
+ main.post { this@OrvynEngine.device.ask(id, kind) }
208
+ }
209
+
82
210
  @JavascriptInterface
83
211
  fun storageGet(key: String): String? = storage.getString(key, null)
84
212
 
85
213
  @JavascriptInterface
86
- fun storageSet(key: String, value: String) {
87
- storage.edit().putString(key, value).apply()
214
+ fun storageSet(key: String, value: String): String? {
215
+ return if (storage.edit().putString(key, value).commit()) null else "Storage write failed"
216
+ }
217
+
218
+ @JavascriptInterface
219
+ fun storageRemove(key: String): String? {
220
+ return if (storage.edit().remove(key).commit()) null else "Storage delete failed"
221
+ }
222
+
223
+ @JavascriptInterface
224
+ fun secureGet(key: String): String? {
225
+ val encoded = secureStorage.getString(key, null) ?: return null
226
+ return try { decryptSecret(encoded) } catch (_: Exception) { null }
227
+ }
228
+
229
+ @JavascriptInterface
230
+ fun secureSet(key: String, value: String): String? {
231
+ return try {
232
+ val encoded = encryptSecret(value)
233
+ if (secureStorage.edit().putString(key, encoded).commit()) null else "Secure storage write failed"
234
+ } catch (error: Exception) {
235
+ error.message ?: "Secure storage failed"
236
+ }
237
+ }
238
+
239
+ @JavascriptInterface
240
+ fun secureRemove(key: String): String? {
241
+ return if (secureStorage.edit().remove(key).commit()) null else "Secure storage delete failed"
242
+ }
243
+
244
+ @JavascriptInterface
245
+ fun secureClear(): String? {
246
+ return if (secureStorage.edit().clear().commit()) null else "Secure storage clear failed"
88
247
  }
89
248
 
90
249
  @JavascriptInterface
@@ -93,14 +252,21 @@ class OrvynEngine(
93
252
  main.post { onError(message) }
94
253
  }
95
254
 
255
+ @JavascriptInterface
256
+ fun abortFetch(id: Int) {
257
+ fetchConnections.remove(id)?.disconnect()
258
+ }
259
+
96
260
  @JavascriptInterface
97
261
  fun fetch(id: Int, url: String, method: String, body: String?, headers: String) {
98
262
  thread {
99
263
  var status = 0
100
264
  var text: String
101
265
  var contentType = ""
266
+ var connection: HttpURLConnection? = null
102
267
  try {
103
- val connection = URL(url).openConnection() as HttpURLConnection
268
+ connection = URL(url).openConnection() as HttpURLConnection
269
+ fetchConnections[id] = connection
104
270
  connection.requestMethod = method
105
271
  val headerJson = JSONObject(headers)
106
272
  for (key in headerJson.keys()) {
@@ -117,6 +283,9 @@ class OrvynEngine(
117
283
  } catch (error: Exception) {
118
284
  status = 0
119
285
  text = error.message ?: "Network error"
286
+ } finally {
287
+ fetchConnections.remove(id)
288
+ connection?.disconnect()
120
289
  }
121
290
  main.post {
122
291
  js("__orvynFetchDone($id, $status, ${JSONObject.quote(text)}, ${JSONObject.quote(contentType)})")