Skip to content

Commit

Permalink
Initial code commit (#1)
Browse files Browse the repository at this point in the history
* Initial code commit
* Add deps for jsguard
  • Loading branch information
JohnStarich authored Feb 25, 2023
1 parent e3e10b6 commit 4f9b361
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches: [ main ]
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19.x
- name: Lint
run: make lint

test:
strategy:
matrix:
platform:
- ubuntu-latest
go:
- 1.18.x
- 1.19.x
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: Install Chrome
uses: browser-actions/setup-chrome@latest
- name: Test
run: make test
- name: Publish test coverage
if: "matrix.platform == 'ubuntu-latest' && matrix.go == '1.19.x'"
run: make test-publish-coverage
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
enable:
# Default linters, plus these:
- exportloopref
- gocognit
- goconst
- gocritic
- gofmt
- gosec
- misspell
- paralleltest
- revive

issues:
include:
# Re-enable default excluded rules
- EXC0001
- EXC0002
- EXC0012
- EXC0013
- EXC0014
- EXC0015
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SHELL := /usr/bin/env bash
BROWSERTEST_VERSION = v0.7
LINT_VERSION = 1.50.1
GO_BIN = $(shell printf '%s/bin' "$$(go env GOPATH)")

.PHONY: all
all: lint test

.PHONY: lint-deps
lint-deps:
@if ! which golangci-lint >/dev/null || [[ "$$(golangci-lint version 2>&1)" != *${LINT_VERSION}* ]]; then \
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "${GO_BIN}" v${LINT_VERSION}; \
fi
@if ! which jsguard >/dev/null; then \
go install github.com/hack-pad/safejs/jsguard/cmd/jsguard; \
fi

.PHONY: lint
lint: lint-deps
"${GO_BIN}/golangci-lint" run
GOOS=js GOARCH=wasm "${GO_BIN}/golangci-lint" run
GOOS=js GOARCH=wasm "${GO_BIN}/jsguard" ./...

.PHONY: test-deps
test-deps:
@if [[ ! -f "${GO_BIN}/go_js_wasm_exec" ]]; then \
set -ex; \
go install github.com/agnivade/wasmbrowsertest@${BROWSERTEST_VERSION}; \
mv "${GO_BIN}/wasmbrowsertest" "${GO_BIN}/go_js_wasm_exec"; \
fi

.PHONY: test
test: test-deps
go test wasm_tags_test.go # Verify build tags and whatnot first
go test -race -coverprofile=native-cover.out ./... # Test non-js side
GOOS=js GOARCH=wasm go test -coverprofile=js-cover.out -covermode=atomic ./... # Test js side
{ echo 'mode: atomic'; cat *-cover.out | grep -v '^mode'; } > cover.out && rm *-cover.out # Combine JS and non-JS coverage.
go tool cover -func cover.out | grep total:

.PHONY: test-publish-coverage
test-publish-coverage:
go install github.com/mattn/[email protected]
COVERALLS_TOKEN=$$GITHUB_TOKEN goveralls -coverprofile="cover.out" -service=github
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/hack-pad/go-webworkers

go 1.19

require (
github.com/hack-pad/go-indexeddb v0.3.2 // indirect
github.com/hack-pad/safejs v0.1.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/tools v0.5.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/hack-pad/go-indexeddb v0.3.2 h1:DTqeJJYc1usa45Q5r52t01KhvlSN02+Oq+tQbSBI91A=
github.com/hack-pad/go-indexeddb v0.3.2/go.mod h1:QvfTevpDVlkfomY498LhstjwbPW6QC4VC/lxYb0Kom0=
github.com/hack-pad/safejs v0.1.0 h1:qPS6vjreAqh2amUqj4WNG1zIw7qlRQJ9K10eDKMCnE8=
github.com/hack-pad/safejs v0.1.0/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
79 changes: 79 additions & 0 deletions wasm_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//go:build !js

package safejs

import (
"bufio"
"go/build/constraint"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)

func TestAllWasmTags(t *testing.T) {
t.Parallel()
const rootDir = "."
walkErr := filepath.Walk(rootDir, func(path string, info fs.FileInfo, err error) (resultErr error) {
switch {
case err != nil:
return err
case info.IsDir(),
filepath.Ext(path) != ".go":
return nil
case path == "wasm_tags_test.go":
// ignore this file, since it must run with file system support enabled
return nil
}

f, err := os.Open(path)
if err != nil {
return err
}
defer handleCloseErr(f, &resultErr)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if !strings.HasPrefix(line, "//") {
// hit non-comment line, so no build tags exist (see https://golang.org/cmd/go/#hdr-Build_constraints)
t.Errorf("File %q does not contain a js,wasm build tag", path)
break
}

expr, err := constraint.Parse(line)
if err != nil {
t.Logf("Build constraint failed to parse line in file %q: %q; %v", path, line, err)
continue
}
if isJSWasm(expr) {
break
}
}
return scanner.Err()
})
if walkErr != nil {
t.Error("Walk failed:", walkErr)
}
}

func isJSWasm(expr constraint.Expr) bool {
switch expr := expr.(type) {
case *constraint.AndExpr:
x, y := expr.X.String(), expr.Y.String()
return (x == "js" && y == "wasm") || (x == "wasm" && y == "js")
default:
return false
}
}

func handleCloseErr(closer io.Closer, resultErr *error) {
err := closer.Close()
if err != nil && *resultErr == nil {
*resultErr = err
}
}
16 changes: 16 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build js && wasm
// +build js,wasm

// Package worker provides a Web Workers driver for Go code compiled to WebAssembly.
package worker

import "errors"

// Worker is a Web Worker, which represents a background task that can be created via script.
// Workers can send messages back to its creator.
type Worker struct{}

// NewWorker returns a new Worker
func NewWorker() (*Worker, error) {
return nil, errors.New("not implemented")
}
14 changes: 14 additions & 0 deletions worker/worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build js && wasm
// +build js,wasm

package worker

import "testing"

func TestNewWorker(t *testing.T) {
t.Parallel()
_, err := NewWorker()
if err == nil || err.Error() != "not implemented" {
t.Error("NewWorker should not be implemented, got:", err)
}
}

0 comments on commit 4f9b361

Please sign in to comment.