{build,wasmserve}: use wasmserve, drop apple_pie
This commit is contained in:
parent
ebb4b9c2fe
commit
5be9f04d85
18 changed files with 747 additions and 133 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
|
@ -19,9 +19,6 @@
|
|||
[submodule "freetype/upstream"]
|
||||
path = libs/freetype/upstream
|
||||
url = https://github.com/hexops/freetype
|
||||
[submodule "tools/libs/apple_pie"]
|
||||
path = tools/libs/apple_pie
|
||||
url = https://github.com/Luukdegram/apple_pie
|
||||
[submodule "glfw/upstream"]
|
||||
path = libs/glfw/upstream
|
||||
url = https://github.com/hexops/glfw
|
||||
|
|
|
|||
57
build.zig
57
build.zig
|
|
@ -122,9 +122,9 @@ pub fn build(b: *std.build.Builder) void {
|
|||
example_compile_step.dependOn(&example_app.getInstallStep().?.step);
|
||||
|
||||
const example_run_cmd = example_app.run();
|
||||
example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step);
|
||||
example_run_cmd.dependOn(&example_app.getInstallStep().?.step);
|
||||
const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example");
|
||||
example_run_step.dependOn(&example_run_cmd.step);
|
||||
example_run_step.dependOn(example_run_cmd);
|
||||
}
|
||||
|
||||
if (target.toTarget().cpu.arch != .wasm32) {
|
||||
|
|
@ -144,9 +144,9 @@ pub fn build(b: *std.build.Builder) void {
|
|||
shaderexp_compile_step.dependOn(&shaderexp_app.getInstallStep().?.step);
|
||||
|
||||
const shaderexp_run_cmd = shaderexp_app.run();
|
||||
shaderexp_run_cmd.step.dependOn(&shaderexp_app.getInstallStep().?.step);
|
||||
shaderexp_run_cmd.dependOn(&shaderexp_app.getInstallStep().?.step);
|
||||
const shaderexp_run_step = b.step("run-shaderexp", "Run shaderexp");
|
||||
shaderexp_run_step.dependOn(&shaderexp_run_cmd.step);
|
||||
shaderexp_run_step.dependOn(shaderexp_run_cmd);
|
||||
}
|
||||
|
||||
const compile_all = b.step("compile-all", "Compile all examples and applications");
|
||||
|
|
@ -314,9 +314,7 @@ pub const App = struct {
|
|||
app.getInstallStep().?.step.dependOn(&install_js.step);
|
||||
}
|
||||
|
||||
const html_generator = app.b.addExecutable("html-generator", (comptime thisDir()) ++ "/tools/html-generator.zig");
|
||||
html_generator.main_pkg_path = (comptime thisDir());
|
||||
|
||||
const html_generator = app.b.addExecutable("html-generator", (comptime thisDir()) ++ "/tools/html-generator/main.zig");
|
||||
const run_html_generator = html_generator.run();
|
||||
const html_file_name = std.mem.concat(
|
||||
app.b.allocator,
|
||||
|
|
@ -360,45 +358,24 @@ pub const App = struct {
|
|||
return app.step.install_step;
|
||||
}
|
||||
|
||||
pub fn run(app: *const App) *std.build.RunStep {
|
||||
pub fn run(app: *const App) *std.build.Step {
|
||||
if (app.platform == .web) {
|
||||
ensureDependencySubmodule(app.b.allocator, "tools/libs/apple_pie") catch unreachable;
|
||||
|
||||
const http_server = app.b.addExecutable("http-server", (comptime thisDir()) ++ "/tools/http-server.zig");
|
||||
http_server.addPackage(.{
|
||||
.name = "apple_pie",
|
||||
.source = .{ .path = "tools/libs/apple_pie/src/apple_pie.zig" },
|
||||
});
|
||||
|
||||
// NOTE: The launch actually takes place in reverse order. The browser is launched first
|
||||
// and then the http-server.
|
||||
// This is because running the server would block the process (a limitation of current
|
||||
// RunStep). So we assume that (xdg-)open is a launcher and not a blocking process.
|
||||
|
||||
const address = std.process.getEnvVarOwned(app.b.allocator, "MACH_ADDRESS") catch app.b.allocator.dupe(u8, "127.0.0.1") catch unreachable;
|
||||
const port = std.process.getEnvVarOwned(app.b.allocator, "MACH_PORT") catch app.b.allocator.dupe(u8, "8080") catch unreachable;
|
||||
defer {
|
||||
app.b.allocator.free(address);
|
||||
app.b.allocator.free(port);
|
||||
}
|
||||
const address_parsed = std.net.Address.parseIp4(address, std.fmt.parseInt(u16, port, 10) catch unreachable) catch unreachable;
|
||||
|
||||
const launch = app.b.addSystemCommand(&.{
|
||||
switch (builtin.os.tag) {
|
||||
.macos, .windows => "open",
|
||||
else => "xdg-open", // Assume linux-like
|
||||
const wasmserve = @import("tools/wasmserve/wasmserve.zig");
|
||||
const serve_step = wasmserve.serve(
|
||||
app.step,
|
||||
.{
|
||||
.install_dir = web_install_dir,
|
||||
.watch_paths = &.{"tools/wasmserve/wasmserve.zig"},
|
||||
.listen_address = address_parsed,
|
||||
},
|
||||
app.b.fmt("http://{s}:{s}/{s}.html", .{ address, port, app.name }),
|
||||
});
|
||||
launch.step.dependOn(&app.getInstallStep().?.step);
|
||||
|
||||
const serve = http_server.run();
|
||||
serve.addArgs(&.{ app.name, address, port });
|
||||
serve.step.dependOn(&launch.step);
|
||||
serve.cwd = app.b.getInstallPath(web_install_dir, "");
|
||||
|
||||
return serve;
|
||||
) catch unreachable;
|
||||
return &serve_step.step;
|
||||
} else {
|
||||
return app.step.run();
|
||||
return &app.step.run().step;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
const source = @embedFile("../www/template.html");
|
||||
const source = @embedFile("template.html");
|
||||
const app_name_needle = "{ app_name }";
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
|
@ -11,16 +12,18 @@ pub fn main() !void {
|
|||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
if (args.len < 3) {
|
||||
std.debug.print("Usage: html-generator <output-name> <application-name>\n", .{});
|
||||
std.debug.print("Usage: html-generator <output-name> <app-name>\n", .{});
|
||||
return;
|
||||
}
|
||||
|
||||
const output_name = args[1];
|
||||
const application_name = args[2];
|
||||
const app_name = args[2];
|
||||
|
||||
const file = try std.fs.cwd().createFile(output_name, std.fs.File.CreateFlags{});
|
||||
const file = try std.fs.cwd().createFile(output_name, .{});
|
||||
defer file.close();
|
||||
var buf = try allocator.alloc(u8, std.mem.replacementSize(u8, source, app_name_needle, app_name));
|
||||
defer allocator.free(buf);
|
||||
|
||||
const writer = file.writer();
|
||||
try std.fmt.format(writer, source, .{application_name});
|
||||
_ = std.mem.replace(u8, source, app_name_needle, app_name, buf);
|
||||
_ = try file.write(buf);
|
||||
}
|
||||
53
tools/html-generator/template.html
Normal file
53
tools/html-generator/template.html
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{ app_name }</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script type="module">
|
||||
import { mach } from "./mach.js";
|
||||
import { zig } from "./mach-sysjs.js";
|
||||
import setupWasmserve from "./wasmserve.js";
|
||||
|
||||
setupWasmserve();
|
||||
|
||||
let imports = {
|
||||
env: { ...mach, ...zig },
|
||||
};
|
||||
|
||||
fetch("{ app_name }.wasm")
|
||||
.then(response => response.arrayBuffer())
|
||||
.then(buffer => WebAssembly.instantiate(buffer, imports))
|
||||
.then(results => results.instance)
|
||||
.then(instance => {
|
||||
zig.init(instance);
|
||||
mach.init(instance);
|
||||
instance.exports.wasmInit();
|
||||
|
||||
let frame = true;
|
||||
let last_update_time = performance.now();
|
||||
let update = function () {
|
||||
if (!frame) return;
|
||||
if (mach.machHasEvent() ||
|
||||
(last_update_time + (mach.wait_event_timeout * 1000)) <= performance.now()) {
|
||||
instance.exports.wasmUpdate();
|
||||
last_update_time = performance.now();
|
||||
}
|
||||
window.requestAnimationFrame(update);
|
||||
};
|
||||
|
||||
window.requestAnimationFrame(update);
|
||||
|
||||
window.addEventListener("mach-close", () => {
|
||||
instance.exports.wasmDeinit();
|
||||
frame = false;
|
||||
});
|
||||
})
|
||||
.catch(err => console.error(err));
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
const std = @import("std");
|
||||
const http = @import("apple_pie");
|
||||
const file_server = http.FileServer;
|
||||
|
||||
pub const io_mode = .evented;
|
||||
|
||||
pub fn main() !u8 {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
if (args.len < 4) {
|
||||
std.debug.print("Usage: http-server <application-name> <address> <port>\n", .{});
|
||||
return 0;
|
||||
}
|
||||
|
||||
const application_name = args[1];
|
||||
const address = args[2];
|
||||
const port = try std.fmt.parseUnsigned(u16, args[3], 10);
|
||||
|
||||
std.debug.print("Served at http://{s}:{}/{s}.html\n", .{ address, port, application_name });
|
||||
|
||||
try file_server.init(allocator, .{ .dir_path = "." });
|
||||
defer file_server.deinit();
|
||||
|
||||
try http.listenAndServe(
|
||||
allocator,
|
||||
try std.net.Address.parseIp(address, port),
|
||||
{},
|
||||
file_server.serve,
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
Subproject commit 55b25cf50018fe314606765ca984a0a841743eb5
|
||||
2
tools/wasmserve/.gitattributes
vendored
Normal file
2
tools/wasmserve/.gitattributes
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
* text=auto eol=lf
|
||||
upstream/** linguist-vendored
|
||||
18
tools/wasmserve/.gitignore
vendored
Normal file
18
tools/wasmserve/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# This file is for zig-specific build artifacts.
|
||||
# If you have OS-specific or editor-specific files to ignore,
|
||||
# such as *.swp or .DS_Store, put those in your global
|
||||
# ~/.gitignore and put this in your ~/.gitconfig:
|
||||
#
|
||||
# [core]
|
||||
# excludesfile = ~/.gitignore
|
||||
#
|
||||
# Cheers!
|
||||
# -andrewrk
|
||||
|
||||
zig-cache/
|
||||
zig-out/
|
||||
/release/
|
||||
/debug/
|
||||
/build/
|
||||
/build-*/
|
||||
/docgen_tmp/
|
||||
13
tools/wasmserve/LICENSE
Normal file
13
tools/wasmserve/LICENSE
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Copyright 2021, Hexops Contributors (given via the Git commit history).
|
||||
|
||||
All documentation, image, sound, font, and 2D/3D model files are CC-BY-4.0 licensed unless
|
||||
otherwise noted. You may get a copy of this license at https://creativecommons.org/licenses/by/4.0
|
||||
|
||||
Files in a directory with a separate LICENSE file may contain files under different license terms,
|
||||
described within that LICENSE file.
|
||||
|
||||
All other files are licensed under the Apache License, Version 2.0 (see LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
or the MIT license (see LICENSE-MIT or http://opensource.org/licenses/MIT), at your option.
|
||||
|
||||
All files in the project without exclusions may not be copied, modified, or distributed except
|
||||
according to the terms above.
|
||||
202
tools/wasmserve/LICENSE-APACHE
Normal file
202
tools/wasmserve/LICENSE-APACHE
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
25
tools/wasmserve/LICENSE-MIT
Normal file
25
tools/wasmserve/LICENSE-MIT
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Copyright (c) 2021 Hexops Contributors (given via the Git commit history).
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
31
tools/wasmserve/README.md
Normal file
31
tools/wasmserve/README.md
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# mach/wasmserve
|
||||
|
||||
Small web server specifically for serving Zig WASM applications in development
|
||||
|
||||
## Getting started
|
||||
|
||||
### Adding dependency
|
||||
|
||||
In a `libs` subdirectory of the root of your project:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/machlibs/wasmserve
|
||||
```
|
||||
|
||||
Then in your `build.zig` add:
|
||||
|
||||
```zig
|
||||
...
|
||||
const wasmserve = @import("libs/wasmserve/wasmserve.zig");
|
||||
|
||||
pub fn build(b: *Builder) void {
|
||||
...
|
||||
const serve_step = try wasmserve.serve(exe, .{ .watch_paths = &.{"src/main.zig"} });
|
||||
const run_step = b.step("run", "Run development web server");
|
||||
run_step.dependOn(&serve_step.step);
|
||||
}
|
||||
```
|
||||
|
||||
## Join the community
|
||||
|
||||
Join the Mach community [on Discord](https://discord.gg/XNG3NZgCqp) or [Matrix](https://matrix.to/#/#hexops:matrix.org) to discuss this project, ask questions, get help, etc.
|
||||
15
tools/wasmserve/build.zig
Normal file
15
tools/wasmserve/build.zig
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
const std = @import("std");
|
||||
const wasmserve = @import("wasmserve.zig");
|
||||
|
||||
pub fn build(b: *std.build.Builder) !void {
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const exe = b.addSharedLibrary("test", "test/main.zig", .unversioned);
|
||||
exe.setBuildMode(mode);
|
||||
exe.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding, .abi = .none });
|
||||
exe.install();
|
||||
|
||||
const serve_step = try wasmserve.serve(exe, .{ .watch_paths = &.{"wasmserve.zig"} });
|
||||
const run_step = b.step("test", "Start a testing server");
|
||||
run_step.dependOn(&serve_step.step);
|
||||
}
|
||||
55
tools/wasmserve/mime.zig
Normal file
55
tools/wasmserve/mime.zig
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
pub const mime_list = [_]struct { ext: []const []const u8, mime: []const u8 }{
|
||||
.{ .ext = &.{".aac"}, .mime = "audio/aac" },
|
||||
.{ .ext = &.{".avif"}, .mime = "image/avif" },
|
||||
.{ .ext = &.{".avi"}, .mime = "video/x-msvideo" },
|
||||
.{ .ext = &.{".bin"}, .mime = "application/octet-stream" },
|
||||
.{ .ext = &.{".bmp"}, .mime = "image/bmp" },
|
||||
.{ .ext = &.{".bz"}, .mime = "application/x-bzip" },
|
||||
.{ .ext = &.{".bz2"}, .mime = "application/x-bzip2" },
|
||||
.{ .ext = &.{".css"}, .mime = "text/css" },
|
||||
.{ .ext = &.{".csv"}, .mime = "text/csv" },
|
||||
.{ .ext = &.{".eot"}, .mime = "application/vnd.ms-fontobject" },
|
||||
.{ .ext = &.{".gz"}, .mime = "application/gzip" },
|
||||
.{ .ext = &.{".gif"}, .mime = "image/gif" },
|
||||
.{ .ext = &.{ ".htm", ".html" }, .mime = "text/html" },
|
||||
.{ .ext = &.{".ico"}, .mime = "image/vnd.microsoft.icon" },
|
||||
.{ .ext = &.{".ics"}, .mime = "text/calendar" },
|
||||
.{ .ext = &.{".jar"}, .mime = "application/java-archive" },
|
||||
.{ .ext = &.{ "..jpeg", "..jpg" }, .mime = "image/jpeg" },
|
||||
.{ .ext = &.{".js"}, .mime = "text/javascript" },
|
||||
.{ .ext = &.{".json"}, .mime = "application/json" },
|
||||
.{ .ext = &.{".md"}, .mime = "text/x-markdown" },
|
||||
.{ .ext = &.{".mjs"}, .mime = "text/javascript" },
|
||||
.{ .ext = &.{".mp3"}, .mime = "audio/mpeg" },
|
||||
.{ .ext = &.{".mp4"}, .mime = "video/mp4" },
|
||||
.{ .ext = &.{".mpeg"}, .mime = "video/mpeg" },
|
||||
.{ .ext = &.{".oga"}, .mime = "audio/ogg" },
|
||||
.{ .ext = &.{".ogv"}, .mime = "video/ogg" },
|
||||
.{ .ext = &.{".ogx"}, .mime = "application/ogg" },
|
||||
.{ .ext = &.{".opus"}, .mime = "audio/opus" },
|
||||
.{ .ext = &.{".otf"}, .mime = "font/otf" },
|
||||
.{ .ext = &.{".png"}, .mime = "image/png" },
|
||||
.{ .ext = &.{".pdf"}, .mime = "application/pdf" },
|
||||
.{ .ext = &.{".rar"}, .mime = "application/vnd.rar" },
|
||||
.{ .ext = &.{".rtf"}, .mime = "application/rtf" },
|
||||
.{ .ext = &.{".sh"}, .mime = "application/x-sh" },
|
||||
.{ .ext = &.{".svg"}, .mime = "image/svg+xml" },
|
||||
.{ .ext = &.{".tar"}, .mime = "application/x-tar" },
|
||||
.{ .ext = &.{ ".tif", ".tiff" }, .mime = "image/tiff" },
|
||||
.{ .ext = &.{".toml"}, .mime = "text/toml" },
|
||||
.{ .ext = &.{".ts"}, .mime = "video/mp2t" },
|
||||
.{ .ext = &.{".ttf"}, .mime = "font/ttf" },
|
||||
.{ .ext = &.{".txt"}, .mime = "text/plain" },
|
||||
.{ .ext = &.{".wasm"}, .mime = "application/wasm" },
|
||||
.{ .ext = &.{".wav"}, .mime = "audio/wav" },
|
||||
.{ .ext = &.{".weba"}, .mime = "audio/webm" },
|
||||
.{ .ext = &.{".webm"}, .mime = "video/webm" },
|
||||
.{ .ext = &.{".webp"}, .mime = "image/webp" },
|
||||
.{ .ext = &.{".woff"}, .mime = "font/woff" },
|
||||
.{ .ext = &.{".woff2"}, .mime = "font/woff2" },
|
||||
.{ .ext = &.{".yml"}, .mime = "application/x-yaml" },
|
||||
.{ .ext = &.{".xhtml"}, .mime = "application/xhtml+xml" },
|
||||
.{ .ext = &.{".xml"}, .mime = "application/xml" },
|
||||
.{ .ext = &.{".zip"}, .mime = "application/zip" },
|
||||
.{ .ext = &.{".7z"}, .mime = "application/x-7z-compressed" },
|
||||
};
|
||||
7
tools/wasmserve/test/main.zig
Normal file
7
tools/wasmserve/test/main.zig
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
var x: i16 = 1;
|
||||
x += 1;
|
||||
std.testing.expect(x == 2) catch unreachable;
|
||||
}
|
||||
289
tools/wasmserve/wasmserve.zig
Normal file
289
tools/wasmserve/wasmserve.zig
Normal file
|
|
@ -0,0 +1,289 @@
|
|||
const std = @import("std");
|
||||
const mime = @import("mime.zig");
|
||||
const net = std.net;
|
||||
const mem = std.mem;
|
||||
const fs = std.fs;
|
||||
const build = std.build;
|
||||
|
||||
const js_path = "/www/wasmserve.js";
|
||||
const default_mime = "text/plain";
|
||||
const buffer_size = 2048;
|
||||
const esc = struct {
|
||||
pub const reset = "\x1b[0m";
|
||||
pub const bold = "\x1b[1m";
|
||||
pub const underline = "\x1b[4m";
|
||||
pub const red = "\x1b[31m";
|
||||
pub const yellow = "\x1b[33m";
|
||||
pub const cyan = "\x1b[36m";
|
||||
pub const gray = "\x1b[90m";
|
||||
};
|
||||
|
||||
pub const Options = struct {
|
||||
install_dir: ?build.InstallDir = null,
|
||||
watch_paths: []const []const u8 = &.{},
|
||||
listen_address: net.Address = net.Address.initIp4([4]u8{ 127, 0, 0, 1 }, 8080),
|
||||
};
|
||||
|
||||
pub fn serve(step: *build.LibExeObjStep, options: Options) !*Wasmserve {
|
||||
const self = step.builder.allocator.create(Wasmserve) catch unreachable;
|
||||
const install_dir = options.install_dir orelse build.InstallDir{ .lib = {} };
|
||||
self.* = Wasmserve{
|
||||
.step = build.Step.init(.run, "wasmserve", step.builder.allocator, Wasmserve.make),
|
||||
.b = step.builder,
|
||||
.exe_step = step,
|
||||
.install_dir = install_dir,
|
||||
.install_dir_iter = try fs.cwd().makeOpenPathIterable(step.builder.getInstallPath(install_dir, ""), .{}),
|
||||
.address = options.listen_address,
|
||||
.subscriber = null,
|
||||
.watch_paths = options.watch_paths,
|
||||
.mtimes = std.AutoHashMap(fs.File.INode, i128).init(step.builder.allocator),
|
||||
};
|
||||
self.step.dependOn(&step.install_step.?.step);
|
||||
return self;
|
||||
}
|
||||
|
||||
const Wasmserve = struct {
|
||||
step: build.Step,
|
||||
b: *build.Builder,
|
||||
exe_step: *build.LibExeObjStep,
|
||||
install_dir: build.InstallDir,
|
||||
install_dir_iter: fs.IterableDir,
|
||||
address: net.Address,
|
||||
subscriber: ?*net.StreamServer.Connection,
|
||||
watch_paths: []const []const u8,
|
||||
mtimes: std.AutoHashMap(fs.File.INode, i128),
|
||||
|
||||
const NotifyMessage = enum {
|
||||
reload,
|
||||
};
|
||||
|
||||
pub fn make(step: *build.Step) !void {
|
||||
const self = @fieldParentPtr(Wasmserve, "step", step);
|
||||
|
||||
self.compile();
|
||||
std.debug.assert(mem.eql(u8, fs.path.extension(self.exe_step.out_filename), ".wasm"));
|
||||
|
||||
const install_js = self.b.addInstallFileWithDir(
|
||||
.{ .path = comptime thisDir() ++ js_path },
|
||||
self.install_dir,
|
||||
fs.path.basename(js_path),
|
||||
);
|
||||
try install_js.step.make();
|
||||
|
||||
const watch_thread = try std.Thread.spawn(.{}, watch, .{self});
|
||||
defer watch_thread.detach();
|
||||
try self.runServer();
|
||||
}
|
||||
|
||||
fn runServer(self: *Wasmserve) !void {
|
||||
var server = net.StreamServer.init(.{ .reuse_address = true });
|
||||
defer server.deinit();
|
||||
try server.listen(self.address);
|
||||
|
||||
var addr_buf = @as([45]u8, undefined);
|
||||
var fbs = std.io.fixedBufferStream(&addr_buf);
|
||||
if (self.address.format("", .{}, fbs.writer())) {
|
||||
std.log.info("Started listening at " ++ esc.cyan ++ esc.underline ++ "http://{s}" ++ esc.reset ++ "...", .{fbs.getWritten()});
|
||||
} else |err| logErr(err, @src());
|
||||
|
||||
while (server.accept()) |conn| {
|
||||
self.respond(conn) catch |err| {
|
||||
logErr(err, @src());
|
||||
continue;
|
||||
};
|
||||
} else |err| logErr(err, @src());
|
||||
}
|
||||
|
||||
fn respond(self: *Wasmserve, conn: net.StreamServer.Connection) !void {
|
||||
errdefer respondError(conn.stream, 500, "Internal Server Error") catch |err| logErr(err, @src());
|
||||
|
||||
var recv_buf: [buffer_size]u8 = undefined;
|
||||
const first_line = conn.stream.reader().readUntilDelimiter(&recv_buf, '\n') catch |err| {
|
||||
switch (err) {
|
||||
error.StreamTooLong => try respondError(conn.stream, 414, "Too Long Request"),
|
||||
else => try respondError(conn.stream, 400, "Bad Request"),
|
||||
}
|
||||
return;
|
||||
};
|
||||
var first_line_iter = mem.split(u8, first_line, " ");
|
||||
_ = first_line_iter.next(); // skip method
|
||||
if (first_line_iter.next()) |uri| {
|
||||
if (uri[0] != '/') {
|
||||
try respondError(conn.stream, 400, "Bad Request");
|
||||
return;
|
||||
}
|
||||
|
||||
const url = dropFragment(uri)[1..];
|
||||
if (mem.eql(u8, url, "notify")) {
|
||||
_ = try conn.stream.write("HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/event-stream\r\nCache-Control: No-Cache\r\n\r\n");
|
||||
self.subscriber = try self.b.allocator.create(net.StreamServer.Connection);
|
||||
self.subscriber.?.* = conn;
|
||||
return;
|
||||
}
|
||||
if (self.researchPath(url)) |file_path| {
|
||||
try self.respondFile(conn.stream, file_path);
|
||||
return;
|
||||
} else |_| {}
|
||||
|
||||
try respondError(conn.stream, 404, "Not Found");
|
||||
} else {
|
||||
try respondError(conn.stream, 400, "Bad Request");
|
||||
}
|
||||
}
|
||||
|
||||
fn respondFile(self: Wasmserve, stream: net.Stream, path: []const u8) !void {
|
||||
const ext = fs.path.extension(path);
|
||||
var file_mime: []const u8 = "text/plain";
|
||||
inline for (mime.mime_list) |entry| {
|
||||
for (entry.ext) |ext_entry| {
|
||||
if (std.mem.eql(u8, ext, ext_entry))
|
||||
file_mime = entry.mime;
|
||||
}
|
||||
}
|
||||
|
||||
const file = try self.install_dir_iter.dir.openFile(path, .{});
|
||||
defer file.close();
|
||||
const file_size = try file.getEndPos();
|
||||
|
||||
try stream.writer().print(
|
||||
"HTTP/1.1 200 OK\r\n" ++
|
||||
"Connection: close\r\n" ++
|
||||
"Content-Length: {d}\r\n" ++
|
||||
"Content-Type: {s}\r\n" ++
|
||||
"\r\n",
|
||||
.{ file_size, file_mime },
|
||||
);
|
||||
|
||||
const zero_iovec = &[0]std.os.iovec_const{};
|
||||
var send_total: usize = 0;
|
||||
while (true) {
|
||||
const send_len = try std.os.sendfile(
|
||||
stream.handle,
|
||||
file.handle,
|
||||
send_total,
|
||||
file_size,
|
||||
zero_iovec,
|
||||
zero_iovec,
|
||||
0,
|
||||
);
|
||||
if (send_len == 0)
|
||||
break;
|
||||
send_total += send_len;
|
||||
}
|
||||
}
|
||||
|
||||
fn respondError(stream: net.Stream, code: u32, desc: []const u8) !void {
|
||||
try stream.writer().print(
|
||||
"HTTP/1.1 {d} {s}\r\n" ++
|
||||
"Connection: close\r\n" ++
|
||||
"Content-Length: {d}\r\n" ++
|
||||
"Content-Type: text/html\r\n" ++
|
||||
"\r\n<!DOCTYPE html><html><body><h1>{s}</h1></body></html>",
|
||||
.{ code, desc, desc.len + 50, desc },
|
||||
);
|
||||
}
|
||||
|
||||
fn researchPath(self: Wasmserve, path: []const u8) ![]const u8 {
|
||||
var walker = try self.install_dir_iter.walk(self.b.allocator);
|
||||
defer walker.deinit();
|
||||
while (try walker.next()) |walk_entry| {
|
||||
if (walk_entry.kind != .File) continue;
|
||||
if (mem.eql(u8, walk_entry.path, path) or (path.len == 0 and mem.eql(u8, walk_entry.path, "index.html")))
|
||||
return try self.b.allocator.dupe(u8, walk_entry.path);
|
||||
}
|
||||
return error.FileNotFound;
|
||||
}
|
||||
|
||||
fn watch(self: *Wasmserve) void {
|
||||
timer_loop: while (true) : (std.time.sleep(100 * std.time.ns_per_ms)) {
|
||||
for (self.watch_paths) |path| {
|
||||
var dir = fs.cwd().openIterableDir(path, .{}) catch {
|
||||
if (self.checkForUpdate(path)) |is_updated| {
|
||||
if (is_updated)
|
||||
continue :timer_loop;
|
||||
} else |err| logErr(err, @src());
|
||||
continue;
|
||||
};
|
||||
defer dir.close();
|
||||
var walker = dir.walk(self.b.allocator) catch |err| {
|
||||
logErr(err, @src());
|
||||
continue;
|
||||
};
|
||||
defer walker.deinit();
|
||||
while (walker.next() catch |err| {
|
||||
logErr(err, @src());
|
||||
continue;
|
||||
}) |walk_entry| {
|
||||
if (walk_entry.kind != .File) continue;
|
||||
if (self.checkForUpdate(walk_entry.path)) |is_updated| {
|
||||
if (is_updated)
|
||||
continue :timer_loop;
|
||||
} else |err| {
|
||||
logErr(err, @src());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn checkForUpdate(self: *Wasmserve, path: []const u8) !bool {
|
||||
const stat = try fs.cwd().statFile(path);
|
||||
const entry = try self.mtimes.getOrPut(stat.inode);
|
||||
if (entry.found_existing and stat.mtime > entry.value_ptr.*) {
|
||||
std.log.info(esc.yellow ++ esc.underline ++ "{s}" ++ esc.reset ++ " updated", .{path});
|
||||
self.compile();
|
||||
entry.value_ptr.* = stat.mtime;
|
||||
return true;
|
||||
}
|
||||
entry.value_ptr.* = stat.mtime;
|
||||
return false;
|
||||
}
|
||||
|
||||
fn notify(self: *Wasmserve, msg: NotifyMessage) void {
|
||||
if (self.subscriber) |s|
|
||||
_ = s.stream.writer().print("data: {s}\n\n", .{@tagName(msg)}) catch |err| logErr(err, @src());
|
||||
}
|
||||
|
||||
fn compile(self: *Wasmserve) void {
|
||||
std.log.info("Compiling...", .{});
|
||||
self.exe_step.install_step.?.step.done_flag = false;
|
||||
if (self.exe_step.install_step.?.step.make()) {
|
||||
self.notify(.reload);
|
||||
} else |err| {
|
||||
logErr(err, @src());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn dropFragment(input: []const u8) []const u8 {
|
||||
for (input) |c, i|
|
||||
if (c == '?' or c == '#')
|
||||
return input[0..i];
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
fn logErr(err: anyerror, src: std.builtin.SourceLocation) void {
|
||||
if (@errorReturnTrace()) |bt| {
|
||||
std.log.err(esc.red ++ esc.bold ++ "{s}" ++ esc.reset ++ " >>>\n{s}", .{ @errorName(err), bt });
|
||||
} else {
|
||||
var file_name_buf: [1024]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&file_name_buf);
|
||||
const allocator = fba.allocator();
|
||||
const file_path = fs.path.relative(allocator, ".", src.file) catch @as([]const u8, src.file);
|
||||
std.log.err(esc.red ++ esc.bold ++ "{s}" ++ esc.reset ++
|
||||
" at " ++ esc.underline ++ "{s}:{d}:{d}" ++ esc.reset ++
|
||||
esc.gray ++ " fn {s}()" ++ esc.reset, .{
|
||||
@errorName(err),
|
||||
file_path,
|
||||
src.line,
|
||||
src.column,
|
||||
src.fn_name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn thisDir() []const u8 {
|
||||
return fs.path.dirname(@src().file) orelse ".";
|
||||
}
|
||||
11
tools/wasmserve/www/wasmserve.js
Normal file
11
tools/wasmserve/www/wasmserve.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let evtSource = new EventSource("/notify");
|
||||
|
||||
function setup() {
|
||||
evtSource.addEventListener("message", function (e) {
|
||||
if (e.data === "reload") {
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default setup;
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import {{ mach }} from "./mach.js";
|
||||
import {{ zig }} from "./mach-sysjs.js";
|
||||
|
||||
let imports = {{
|
||||
env: {{ ...mach, ...zig }},
|
||||
}};
|
||||
|
||||
fetch("{s}.wasm")
|
||||
.then(response => response.arrayBuffer())
|
||||
.then(buffer => WebAssembly.instantiate(buffer, imports))
|
||||
.then(results => results.instance)
|
||||
.then(instance => {{
|
||||
zig.init(instance);
|
||||
mach.init(instance);
|
||||
instance.exports.wasmInit();
|
||||
|
||||
let frame = true;
|
||||
let last_update_time = performance.now();
|
||||
let update = function() {{
|
||||
if (!frame) return;
|
||||
if (mach.machHasEvent() ||
|
||||
(last_update_time + (mach.wait_event_timeout * 1000)) <= performance.now()) {{
|
||||
instance.exports.wasmUpdate();
|
||||
last_update_time = performance.now();
|
||||
}}
|
||||
window.requestAnimationFrame(update);
|
||||
}};
|
||||
|
||||
window.requestAnimationFrame(update);
|
||||
|
||||
window.addEventListener("mach-close", () => {{
|
||||
instance.exports.wasmDeinit();
|
||||
frame = false;
|
||||
}});
|
||||
}})
|
||||
.catch(err => console.error(err));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue