Skip to content

Commit

Permalink
Merge branch 'full-wasm-support' of github.com:0xProject/goleveldb in…
Browse files Browse the repository at this point in the history
…to full-wasm-support
  • Loading branch information
albrow committed Nov 1, 2019
2 parents d5a1ee5 + 7deff69 commit 5ac28e2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion browser-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
qunit.Module("LevelDB")
qunit.Test("set and get", func(assert qunit.QUnitAssert) {
dbPath := "leveldb-testing-" + uuid.New().String()
dbPath := "leveldb/testing/db-" + uuid.New().String()
db, err := leveldb.OpenFile(dbPath, nil)
assertNoError(assert, err, "could not open db")

Expand Down
2 changes: 1 addition & 1 deletion browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestInBrowser(t *testing.T) {
}

cmd = exec.Command("go", "build", "-o", "main.wasm", ".")
cmd.Env = append(os.Environ(), []string{"GOOS=js", "GOARCH=wasm"}...)
cmd.Env = append(os.Environ(), []string{"GOOS=js", "GOARCH=wasm", "GO111MODULE=on"}...)
cmd.Dir = "./browser-tests"
if output, err := cmd.CombinedOutput(); err != nil {
t.Log(string(output))
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/syndtr/goleveldb

require (
github.com/0xProject/qunit v0.0.0-20190730000255-81c18fdf7752 // indirect
github.com/0xProject/qunit v0.0.0-20190730000255-81c18fdf7752
github.com/golang/snappy v0.0.1
github.com/google/uuid v1.1.1
github.com/onsi/ginkgo v1.7.0
github.com/onsi/gomega v1.4.3
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
44 changes: 39 additions & 5 deletions leveldb/storage/file_storage_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"syscall/js"
Expand Down Expand Up @@ -140,6 +141,19 @@ func (f *browserFSFile) Write(b []byte) (n int, err error) {
// regardless of the encoding used. Encoding to hex seems like the most
// reliable way to do it.
rawBytesWritten := js.Global().Get("browserFS").Call("writeSync", f.fd, hex.EncodeToString(b), f.currOffset, "hex")

// Note: This a workaround for an issue that can cause data inconsistency
// (specifically an empty MANIFEST file). Normally fsync is not required on
// every write unless WriteOptions.Sync is set to true. However, we have
// observed that leveldb does not call fsync after creating a new MANIFEST
// file and deleting the old one. In BrowserFS, this means that the contents
// are never actually written to the file. On the next boot, leveldb will
// attempt to read from the manifest file and return an error because it is
// empty.
if err := f.Sync(); err != nil {
return 0, err
}

bytesWritten := rawBytesWritten.Int()
f.currOffset += int64(bytesWritten)
if bytesWritten != len(b) {
Expand Down Expand Up @@ -339,12 +353,30 @@ func browserFSMkdirAll(path string, perm os.FileMode) (err error) {
}
}
}()
// TODO(albrow): Add support for recursive mkdir. BrowserFS doesn't support it
// out of the box.
if len(filepath.SplitList(path)) > 1 {
return errors.New("recursive mkdir not supported in js/wasm")
// Note: mkdirAll is not supported by BrowserFS so we have to manually create
// each directory.
names := strings.Split(path, string(os.PathSeparator))
for i := range names {
partialPath := filepath.Join(names[:i+1]...)
if err := browserFSMkdir(partialPath, perm); err != nil {
if os.IsExist(err) {
// If the directory already exists, that's fine.
continue
}
}
}
js.Global().Get("browserFS").Call("mkdirSync", path, int(perm))
return nil
}

func browserFSMkdir(dir string, perm os.FileMode) (err error) {
defer func() {
if e := recover(); e != nil {
if jsErr, ok := e.(js.Error); ok {
err = convertJSError(jsErr)
}
}
}()
js.Global().Get("browserFS").Call("mkdirSync", dir, int(perm))
return nil
}

Expand Down Expand Up @@ -429,6 +461,8 @@ func convertJSError(err js.Error) error {
return os.ErrNotExist
case "EISDIR":
return syscall.EISDIR
case "EEXIST":
return os.ErrExist
// TODO(albrow): Fill in more codes here.
}
}
Expand Down

0 comments on commit 5ac28e2

Please sign in to comment.