소스 검색

Initialize CheerpX disk backend for the main image

Alessandro Pignotti 9 달 전
부모
커밋
f586c4bcf2
2개의 변경된 파일43개의 추가작업 그리고 2개의 파일을 삭제
  1. 2 1
      diskConfig.cloud.js
  2. 41 1
      src/App.svelte

+ 2 - 1
diskConfig.cloud.js

@@ -1 +1,2 @@
-export const imageUrl = "wss://disks-staging.webvm.io/debian_large_20230522_5044875331.ext2";
+export const diskImageUrl = "wss://disks-staging.webvm.io/debian_large_20230522_5044875331.ext2";
+export const diskImageType = "cloud";

+ 41 - 1
src/App.svelte

@@ -6,7 +6,7 @@
 	import Nav from 'labs/packages/global-navbar/src/Nav.svelte';
 	import SideBar from './SideBar.svelte';
 	import * as CheerpX from '@leaningtech/cheerpx';
-	import { imageUrl } from 'diskConfig'
+	import { diskImageUrl, diskImageType } from 'diskConfig'
 
 	var term = new Terminal({cursorBlink:true, convertEol:true, fontFamily:"monospace", fontWeight: 400, fontWeightBold: 700});
 	var fitAddon = new FitAddon();
@@ -21,6 +21,46 @@
 		fitAddon.fit();
 		window.addEventListener("resize", function(ev){ fitAddon.fit(); });
 		term.focus();
+		initCheerpX();
+	}
+	async function initCheerpX()
+	{
+		// TODO: Check for SAB support
+		// TODO: Initialize network
+		var blockDevice = null;
+		switch(diskImageType)
+		{
+			case "cloud":
+				try
+				{
+					blockDevice = await CheerpX.CloudDevice.create(diskImageUrl);
+				}
+				catch(e)
+				{
+					// Report the failure and try again with plain HTTP
+					var wssProtocol = "wss:";
+					if(diskImageUrl.startsWith(wssProtocol))
+					{
+						// WebSocket protocol failed, try agin using plain HTTP
+						plausible("WS Disk failure");
+						blockDevice = await CheerpX.CloudDevice.create("https:" + diskImageUrl.substr(wssProtocol.length));
+					}
+					else
+					{
+						// No other recovery option
+						throw e;
+					}
+				}
+				break;
+			case "bytes":
+				blockDevice = await CheerpX.HttpBytesDevice.create(diskImageUrl);
+				break;
+			case "github":
+				blockDevice = await CheerpX.GitHubDevice.create(diskImageUrl);
+				break;
+			default:
+				throw new Error("Unrecognized device type");
+		}
 	}
 	onMount(initTerminal);
 </script>