Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: CI

env:
ZIG_VERSION: "0.15.2"
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
ZIG_VERSION: "0.16.0"

on:
push:
Expand All @@ -16,15 +17,30 @@ jobs:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Install Zig 0.15.2
- name: Install Zig 0.16.0
run: bash .github/scripts/install-zig.sh "${ZIG_VERSION}"

- name: Cache Zig build outputs
uses: actions/cache@v5
with:
path: |
.zig-cache
~/.cache/zig
key: zig-ubuntu-${{ hashFiles('src/**/*.zig', 'build.zig', 'build.zig.zon', 'test_runner.zig', 'support/autobahn/**/*.zig', 'support/autobahn/**/build.zig', 'support/autobahn/**/build.zig.zon', 'Dockerfile') }}
restore-keys: zig-ubuntu-

- name: non-blocking test
run: zig build test -Dforce_blocking=false
- name: blocking test
run: zig build test -Dforce_blocking=true

- name: build server native
run: cd support/autobahn/server && zig build
- name: build client native
run: cd support/autobahn/client && zig build

- name: build server x86-linux-gnu
run: cd support/autobahn/server && zig build -Dtarget=x86-linux-gnu
- name: build server x86_64-linux-gnu
Expand All @@ -33,7 +49,10 @@ jobs:
run: cd support/autobahn/server && zig build -Dtarget=x86_64-windows-gnu
- name: build client x86-linux-gnu
run: cd support/autobahn/client && zig build -Dtarget=x86-linux-gnu
- name: build server x86_64-linux-gnu
- name: build client x86_64-linux-gnu
run: cd support/autobahn/client && zig build -Dtarget=x86_64-linux-gnu
- name: build clietn x86_64-windows-gnu
- name: build client x86_64-windows-gnu
run: cd support/autobahn/client && zig build -Dtarget=x86_64-windows-gnu

- name: build Docker test image
run: docker build -t websocket-zig16-test .
77 changes: 77 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Release

env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
ZIG_VERSION: "0.16.0"

on:
push:
tags: ['v*']
workflow_dispatch:

jobs:
verify:
runs-on: ubuntu-latest
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v6

- name: Install Zig 0.16.0
run: bash .github/scripts/install-zig.sh "${ZIG_VERSION}"

- name: non-blocking test
run: zig build test -Dforce_blocking=false
- name: blocking test
run: zig build test -Dforce_blocking=true
- name: build server native
run: cd support/autobahn/server && zig build
- name: build client native
run: cd support/autobahn/client && zig build
- name: build Docker test image
run: docker build -t websocket-zig16-release .

source:
name: Prepare source archive
runs-on: ubuntu-latest
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v6

- name: Create source archive
run: |
archive_name="websocket-source-${GITHUB_REF_NAME}.tar.gz"
tar \
--exclude='.git' \
--exclude='.zig-cache' \
--exclude='zig-out' \
-czf "/tmp/${archive_name}" .
mv "/tmp/${archive_name}" .
echo "ARCHIVE_NAME=${archive_name}" >> "$GITHUB_ENV"

- name: Upload source archive
uses: actions/upload-artifact@v7
with:
name: websocket-source
path: ${{ env.ARCHIVE_NAME }}

release:
needs: [verify, source]
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- uses: actions/download-artifact@v8

- name: Create release
uses: softprops/action-gh-release@v2
with:
files: websocket-source/*.tar.gz
generate_release_notes: true
23 changes: 14 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from ubuntu:latest
run apt-get update && apt-get install -y wget xz-utils
FROM ubuntu:24.04

run wget "https://ziglang.org/builds/zig-linux-aarch64-0.14.0-dev.244+0d79aa017.tar.xz"
run tar -xJvf zig-linux-aarch64-0.14.0-dev.244+0d79aa017.tar.xz && \
mv /zig-linux-aarch64-0.14.0-dev.244+0d79aa017/ /zig && \
chmod a+x /zig && \
rm -fr /zig-*
ARG ZIG_VERSION=0.16.0

workdir /opt
entrypoint ["/zig/zig", "build", "test"]
RUN apt-get update && apt-get install -y bash ca-certificates curl python3 xz-utils && rm -rf /var/lib/apt/lists/*

COPY .github/scripts/install-zig.sh /tmp/install-zig.sh
RUN chmod +x /tmp/install-zig.sh && \
GITHUB_PATH=/tmp/zig-path bash /tmp/install-zig.sh "${ZIG_VERSION}" && \
install_dir="$(cat /tmp/zig-path)" && \
ln -s "${install_dir}/zig" /usr/local/bin/zig

WORKDIR /opt/websocket
COPY . .

ENTRYPOINT ["bash", "-lc", "zig build test -Dforce_blocking=false && zig build test -Dforce_blocking=true"]
7 changes: 7 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const compat_module = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/compat.zig"),
});

const websocket_module = b.addModule("websocket", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/websocket.zig"),
});
websocket_module.addImport("compat", compat_module);

{
const options = b.addOptions();
Expand All @@ -23,6 +29,7 @@ pub fn build(b: *std.Build) !void {
.test_runner = .{ .path = b.path("test_runner.zig"), .mode = .simple },
});
tests.root_module.link_libc = true;
tests.root_module.addImport("compat", compat_module);
const force_blocking = b.option(bool, "force_blocking", "Force blocking mode") orelse false;
const options = b.addOptions();
options.addOption(bool, "websocket_blocking", force_blocking);
Expand Down
21 changes: 11 additions & 10 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
.{
.name = .websocket,
.version = "0.1.0",
.dependencies = .{},
.fingerprint = 0x42ce80b97512f264,
.paths = .{
"readme.md",
"build.zig",
"build.zig.zon",
"src",
},
.name = .websocket,
.version = "0.1.0",
.dependencies = .{},
.fingerprint = 0x42ce80b97512f264,
.minimum_zig_version = "0.16.0",
.paths = .{
"readme.md",
"build.zig",
"build.zig.zon",
"src",
},
}
8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# A zig websocket server.
The master branch targets the latest stable of Zig (0.15.1). The dev branch targets the latest version of Zig. If you're looking for an older version, look for an zig-X.YZ branches.
The main branch targets Zig 0.16.0. If you're looking for an older version, look for a `zig-X.YZ` branch.

Skip to the [client section](#client).

Expand All @@ -11,8 +11,7 @@ const std = @import("std");
const ws = @import("websocket");

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const allocator = std.heap.smp_allocator;

var server = try ws.Server(Handler).init(allocator, .{
.port = 9224,
Expand Down Expand Up @@ -423,8 +422,7 @@ The `*websocket.Client` can be used in one of two ways. At its simplest, after c

```zig
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const allocator = std.heap.smp_allocator;

// create the client
var client = try websocket.Client.init(allocator, .{
Expand Down
3 changes: 2 additions & 1 deletion src/buffer.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const sync = @import("compat").sync;

const Allocator = std.mem.Allocator;

Expand Down Expand Up @@ -215,7 +216,7 @@ pub const Pool = struct {
available: usize,
buffers: [][]u8,
allocator: Allocator,
mutex: std.Thread.Mutex,
mutex: sync.Mutex,

pub fn init(allocator: Allocator, count: usize, buffer_size: usize) !Pool {
const buffers = try allocator.alloc([]u8, count);
Expand Down
Loading