Skip to content

Commit 9597e96

Browse files
committed
Add JS support to node package
1 parent 57ef17b commit 9597e96

5 files changed

Lines changed: 164 additions & 80 deletions

File tree

host-go/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ test\:no-deps:
2121
@$(MAKE) clean
2222
go test ./...
2323

24+
JS_TEST_FLAGS=-exec="$$(go env GOROOT)/lib/wasm/go_js_wasm_exec" -shuffle=on -timeout 10m
25+
2426
.PHONY: test\:ci
2527
test\:ci:
2628
# We do not make the deps here, the ci does that seperately to avoid compiling stuff
@@ -29,4 +31,4 @@ test\:ci:
2931

3032
.PHONY: test\:js
3133
test\:js:
32-
GOOS=js GOARCH=wasm gotestsum --format testname -- -exec wasmbrowsertest ./...
34+
GOOS=js GOARCH=wasm go test ./... $(JS_TEST_FLAGS)

host-go/node/node.go

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,15 @@ import (
1010

1111
badgerds "github.com/dgraph-io/badger/v4"
1212
"github.com/sourcenetwork/corekv"
13-
sourceP2P "github.com/sourcenetwork/go-p2p"
1413

1514
"github.com/sourcenetwork/corekv/badger"
1615
"github.com/sourcenetwork/immutable"
17-
"github.com/sourcenetwork/lens/host-go/p2p"
1816
"github.com/sourcenetwork/lens/host-go/runtimes"
1917
"github.com/sourcenetwork/lens/host-go/store"
2018
)
2119

2220
type closer = func() error
2321

24-
type Node struct {
25-
onClose []closer
26-
Options Options
27-
Store store.TxnStore
28-
P2P immutable.Option[*p2p.P2P]
29-
}
30-
3122
func New(ctx context.Context, opts ...Option) (*Node, error) {
3223
var o Options
3324
for _, option := range opts {
@@ -77,52 +68,26 @@ func New(ctx context.Context, opts ...Option) (*Node, error) {
7768
o.IndexstoreNamespace = immutable.Some("i")
7869
}
7970

80-
var p2pSys immutable.Option[*p2p.P2P]
81-
if !o.DisableP2P {
82-
var host p2p.Host
83-
if o.P2P.HasValue() {
84-
host = o.P2P.Value()
85-
} else {
86-
p2pOptions := append(
87-
o.P2POptions,
88-
sourceP2P.WithBlockstoreNamespace(o.BlockstoreNamespace.Value()),
89-
sourceP2P.WithRootstore(o.Rootstore.Value()),
90-
)
91-
92-
if o.BlockstoreChunkSize.HasValue() {
93-
p2pOptions = append(p2pOptions, sourceP2P.WithBlockstoreChunkSize(o.BlockstoreChunkSize.Value()))
94-
}
95-
96-
var err error
97-
host, err = sourceP2P.NewPeer(
98-
ctx,
99-
p2pOptions...,
100-
)
101-
if err != nil {
102-
return nil, err
103-
}
104-
}
105-
106-
p2pSys = immutable.Some(p2p.New(host, o.Rootstore.Value(), o.IndexstoreNamespace.Value()))
107-
}
108-
109-
node := &Node{
110-
onClose: onClose,
111-
Options: o,
112-
Store: store.New(
71+
node, err := createNode(
72+
ctx,
73+
store.New(
11374
o.TxnSource.Value(),
11475
o.PoolSize.Value(),
11576
o.Runtime.Value(),
11677
o.BlockstoreNamespace.Value(),
11778
o.BlockstoreChunkSize,
11879
o.IndexstoreNamespace.Value(),
11980
),
120-
P2P: p2pSys,
81+
o,
82+
onClose,
83+
)
84+
if err != nil {
85+
return nil, err
12186
}
12287

12388
// Reload on create, if the store is persisted, this will read any Lenses already in the store and
12489
// add them to the repository.
125-
err := node.Store.Reload(ctx)
90+
err = node.Store.Reload(ctx)
12691
if err != nil {
12792
return nil, err
12893
}

host-go/node/node_p2p.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
//go:build !js
6+
7+
package node
8+
9+
import (
10+
"context"
11+
12+
"github.com/sourcenetwork/corekv"
13+
sourceP2P "github.com/sourcenetwork/go-p2p"
14+
"github.com/sourcenetwork/immutable"
15+
16+
"github.com/sourcenetwork/lens/host-go/engine/module"
17+
"github.com/sourcenetwork/lens/host-go/p2p"
18+
"github.com/sourcenetwork/lens/host-go/store"
19+
)
20+
21+
type Options struct {
22+
Path immutable.Option[string]
23+
Rootstore immutable.Option[corekv.TxnReaderWriter]
24+
TxnSource immutable.Option[store.TxnSource]
25+
PoolSize immutable.Option[int]
26+
Runtime immutable.Option[module.Runtime]
27+
BlockstoreNamespace immutable.Option[string]
28+
BlockstoreChunkSize immutable.Option[int]
29+
IndexstoreNamespace immutable.Option[string]
30+
P2P immutable.Option[p2p.Host]
31+
DisableP2P bool
32+
33+
P2POptions []sourceP2P.NodeOpt
34+
}
35+
36+
type Node struct {
37+
onClose []closer
38+
Options Options
39+
Store store.TxnStore
40+
P2P immutable.Option[*p2p.P2P]
41+
}
42+
43+
func WithP2P(host p2p.Host) Option {
44+
return func(opts *Options) {
45+
opts.P2P = immutable.Some(host)
46+
}
47+
}
48+
49+
func WithP2PDisabled(disableP2P bool) Option {
50+
return func(opts *Options) {
51+
opts.DisableP2P = disableP2P
52+
}
53+
}
54+
55+
func WithP2Poptions(opts ...sourceP2P.NodeOpt) Option {
56+
return func(opt *Options) {
57+
opt.P2POptions = opts
58+
}
59+
}
60+
61+
func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []closer) (*Node, error) {
62+
var p2pSys immutable.Option[*p2p.P2P]
63+
if !o.DisableP2P {
64+
var host p2p.Host
65+
if o.P2P.HasValue() {
66+
host = o.P2P.Value()
67+
} else {
68+
p2pOptions := append(
69+
o.P2POptions,
70+
sourceP2P.WithBlockstoreNamespace(o.BlockstoreNamespace.Value()),
71+
sourceP2P.WithRootstore(o.Rootstore.Value()),
72+
)
73+
74+
if o.BlockstoreChunkSize.HasValue() {
75+
p2pOptions = append(p2pOptions, sourceP2P.WithBlockstoreChunkSize(o.BlockstoreChunkSize.Value()))
76+
}
77+
78+
var err error
79+
host, err = sourceP2P.NewPeer(
80+
ctx,
81+
p2pOptions...,
82+
)
83+
if err != nil {
84+
return nil, err
85+
}
86+
}
87+
88+
p2pSys = immutable.Some(p2p.New(host, o.Rootstore.Value(), o.IndexstoreNamespace.Value()))
89+
}
90+
91+
return &Node{
92+
onClose: onClose,
93+
Options: o,
94+
Store: store,
95+
P2P: p2pSys,
96+
}, nil
97+
}

host-go/node/node_p2p_free.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
//go:build js
6+
7+
package node
8+
9+
import (
10+
"context"
11+
12+
"github.com/sourcenetwork/corekv"
13+
"github.com/sourcenetwork/immutable"
14+
15+
"github.com/sourcenetwork/lens/host-go/engine/module"
16+
"github.com/sourcenetwork/lens/host-go/store"
17+
)
18+
19+
type Options struct {
20+
Path immutable.Option[string]
21+
Rootstore immutable.Option[corekv.TxnReaderWriter]
22+
TxnSource immutable.Option[store.TxnSource]
23+
PoolSize immutable.Option[int]
24+
Runtime immutable.Option[module.Runtime]
25+
BlockstoreNamespace immutable.Option[string]
26+
BlockstoreChunkSize immutable.Option[int]
27+
IndexstoreNamespace immutable.Option[string]
28+
DisableP2P bool
29+
}
30+
31+
type Node struct {
32+
onClose []closer
33+
Options Options
34+
Store store.TxnStore
35+
}
36+
37+
// WARNING - This option exists only for compatibility reasons, it can never have a
38+
// `false` value.
39+
func WithP2PDisabled(disableP2P bool) Option {
40+
if !disableP2P {
41+
panic("P2P cannot be enabled on JavaScript builds")
42+
}
43+
44+
return func(opts *Options) {
45+
opts.DisableP2P = disableP2P
46+
}
47+
}
48+
49+
func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []closer) (*Node, error) {
50+
return &Node{
51+
onClose: onClose,
52+
Options: o,
53+
Store: store,
54+
}, nil
55+
}

host-go/node/option.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,11 @@ package node
66

77
import (
88
"github.com/sourcenetwork/corekv"
9-
sourceP2P "github.com/sourcenetwork/go-p2p"
109
"github.com/sourcenetwork/immutable"
1110
"github.com/sourcenetwork/lens/host-go/engine/module"
12-
"github.com/sourcenetwork/lens/host-go/p2p"
1311
"github.com/sourcenetwork/lens/host-go/store"
1412
)
1513

16-
type Options struct {
17-
Path immutable.Option[string]
18-
Rootstore immutable.Option[corekv.TxnReaderWriter]
19-
TxnSource immutable.Option[store.TxnSource]
20-
PoolSize immutable.Option[int]
21-
Runtime immutable.Option[module.Runtime]
22-
BlockstoreNamespace immutable.Option[string]
23-
BlockstoreChunkSize immutable.Option[int]
24-
IndexstoreNamespace immutable.Option[string]
25-
P2P immutable.Option[p2p.Host]
26-
DisableP2P bool
27-
28-
P2POptions []sourceP2P.NodeOpt
29-
}
30-
3114
// Option is a funtion that sets a config value on the db.
3215
type Option func(*Options)
3316

@@ -69,24 +52,6 @@ func WithIndextoreNamespace(indexstoreNamespace string) Option {
6952
}
7053
}
7154

72-
func WithP2P(host p2p.Host) Option {
73-
return func(opts *Options) {
74-
opts.P2P = immutable.Some(host)
75-
}
76-
}
77-
78-
func WithP2PDisabled(disableP2P bool) Option {
79-
return func(opts *Options) {
80-
opts.DisableP2P = disableP2P
81-
}
82-
}
83-
84-
func WithP2Poptions(opts ...sourceP2P.NodeOpt) Option {
85-
return func(opt *Options) {
86-
opt.P2POptions = opts
87-
}
88-
}
89-
9055
func WithTxnSource(txnSource store.TxnSource) Option {
9156
return func(opt *Options) {
9257
opt.TxnSource = immutable.Some(txnSource)

0 commit comments

Comments
 (0)