Skip to content

Commit

Permalink
Merge pull request #58 from Etherna/feature/BNET-103-build-wasm
Browse files Browse the repository at this point in the history
Feature/bnet 103 build wasm
  • Loading branch information
tmm360 authored Jul 26, 2024
2 parents f8530a0 + 07c8608 commit f18e05b
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions BeeNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeeNet.Core.UnitTest", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeeNet.Util.UnitTest", "test\BeeNet.Util.UnitTest\BeeNet.Util.UnitTest.csproj", "{BBC17EDC-660C-4006-A14C-377693290D5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeeNet.Util.Wasm", "src\BeeNet.Util.Wasm\BeeNet.Util.Wasm.csproj", "{7257332C-5291-4814-B00B-3732BE90FB2E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -76,6 +78,10 @@ Global
{BBC17EDC-660C-4006-A14C-377693290D5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBC17EDC-660C-4006-A14C-377693290D5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBC17EDC-660C-4006-A14C-377693290D5E}.Release|Any CPU.Build.0 = Release|Any CPU
{7257332C-5291-4814-B00B-3732BE90FB2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7257332C-5291-4814-B00B-3732BE90FB2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7257332C-5291-4814-B00B-3732BE90FB2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7257332C-5291-4814-B00B-3732BE90FB2E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,6 +96,7 @@ Global
{EC667802-776E-4306-922C-AA23C7D5835B} = {7932D52A-3D44-4723-9E77-1711F7486417}
{63368E76-475A-45BE-B6DB-86402C041C39} = {990C5C57-6E00-4E76-A23A-519F19290ACB}
{BBC17EDC-660C-4006-A14C-377693290D5E} = {990C5C57-6E00-4E76-A23A-519F19290ACB}
{7257332C-5291-4814-B00B-3732BE90FB2E} = {7932D52A-3D44-4723-9E77-1711F7486417}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BC8368EB-8E3B-4FBB-B657-D1C9375AFAFC}
Expand Down
30 changes: 30 additions & 0 deletions src/BeeNet.Util.Wasm/BeeNet.Util.Wasm.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Etherna.BeeNet.Wasm</RootNamespace>

<Authors>Etherna SA</Authors>
<Description>Utility services to work with Ethereum Swarm with WASM</Description>

<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>

<RepositoryUrl>https://github.com/Etherna/bee-net</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseFile>COPYING</PackageLicenseFile>
<PackageLicenseFile>COPYING-LESSER</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\COPYING" Pack="true" PackagePath="\" />
<None Include="..\..\COPYING-LESSER" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BeeNet.Util\BeeNet.Util.csproj" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions src/BeeNet.Util.Wasm/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Etherna.BeeNet.Services;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;

#pragma warning disable CA1303
Console.WriteLine("Main, don't remove!");
#pragma warning restore CA1303

[SuppressMessage("Design", "CA1052:Static holder types should be Static or NotInheritable")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("Design", "CA1050:Declare types in namespaces")]
public partial class BeeNetWasmUtil
{
[JSExport]
internal static async Task<string> GetHashStringAsync(
byte[] data,
string fileContentType,
string fileName)
{
var calculatorService = new CalculatorService();
var result = await calculatorService.EvaluateFileUploadAsync(
data,
fileContentType,
fileName).ConfigureAwait(false);
return result.Hash.ToString();
}
}
4 changes: 4 additions & 0 deletions src/BeeNet.Util.Wasm/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: System.Runtime.Versioning.SupportedOSPlatform("browser")]
13 changes: 13 additions & 0 deletions src/BeeNet.Util.Wasm/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"BeeNet.Util.Wasm": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:7080;http://localhost:5094",
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
}
}
}
10 changes: 10 additions & 0 deletions src/BeeNet.Util.Wasm/runtimeconfig.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"wasmHostProperties": {
"perHostConfig": [
{
"name": "browser",
"host": "browser"
}
]
}
}
17 changes: 17 additions & 0 deletions src/BeeNet.Util.Wasm/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<!-- Licensed to the .NET Foundation under one or more agreements. -->
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
<html>

<head>
<title>BeeNet.Util.Wasm</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type='module' src="./main.js"></script>
</head>

<body>
<span id="out"></span>
</body>

</html>
33 changes: 33 additions & 0 deletions src/BeeNet.Util.Wasm/wwwroot/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { dotnet } from './_framework/dotnet.js'

const { setModuleImports, getAssemblyExports, getConfig } = await dotnet
.withDiagnosticTracing(false)
.withApplicationArgumentsFromQuery()
.create();

setModuleImports('main.js', {
window: {
location: {
href: () => globalThis.window.location.href
}
}
});

const config = getConfig();
const exports = await getAssemblyExports(config.mainAssemblyName);

let utf8Encode = new TextEncoder();
let array = utf8Encode.encode("Bzz over the world!");

const hash = await exports.BeeNetWasmUtil.GetHashStringAsync(
array,
"text/plain",
"hello.txt"
);
console.log(hash);

document.getElementById('out').innerHTML = hash;
await dotnet.run();

0 comments on commit f18e05b

Please sign in to comment.