Skip to content

Conversation

@nasdf
Copy link
Member

@nasdf nasdf commented May 2, 2025

Relevant issue(s)

Resolves #2442

Description

This PR adds a JS client implementation to DefraDB.

Sorry for the messy commit history. I accidentally started this on a previous feature branch.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

Added integration test client wrapper.

Specify the platform(s) on which this was tested:

  • MacOS

@nasdf nasdf added the feature New feature or request label May 2, 2025
@nasdf nasdf added this to the DefraDB v0.18 milestone May 2, 2025
@nasdf nasdf self-assigned this May 2, 2025
},
},
}
// func TestSubscriptionWithUpdateAllMutations(t *testing.T) {
Copy link
Member Author

@nasdf nasdf May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • todo: uncomment when fixed

@codecov
Copy link

codecov bot commented May 2, 2025

Codecov Report

Attention: Patch coverage is 42.07459% with 497 lines in your changes missing coverage. Please review.

Project coverage is 77.10%. Comparing base (1281b6a) to head (52f44ca).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
js/client_collection.go 38.82% 120 Missing and 25 partials ⚠️
js/client_store.go 44.91% 88 Missing and 31 partials ⚠️
js/client_db.go 33.33% 68 Missing and 20 partials ⚠️
js/client_lens.go 0.00% 77 Missing ⚠️
js/utils.go 50.60% 30 Missing and 11 partials ⚠️
js/js.go 0.00% 20 Missing ⚠️
client/document.go 50.00% 2 Missing and 1 partial ⚠️
js/client.go 91.67% 2 Missing and 1 partial ⚠️
cli/start.go 80.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3658      +/-   ##
===========================================
- Coverage    78.01%   77.10%   -0.91%     
===========================================
  Files          405      418      +13     
  Lines        37341    38197     +856     
===========================================
+ Hits         29129    29451     +322     
- Misses        6431     6872     +441     
- Partials      1781     1874      +93     
Flag Coverage Δ
all-tests 77.10% <42.07%> (-0.91%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
acp/identity/identity.go 70.80% <ø> (ø)
cli/utils.go 67.57% <100.00%> (ø)
client/db.go 89.29% <ø> (ø)
errors/errors.go 100.00% <ø> (ø)
errors/stack_trace.go 100.00% <100.00%> (ø)
errors/stack_trace_js.go 100.00% <100.00%> (ø)
js/client_tx.go 100.00% <100.00%> (ø)
net/config/config.go 100.00% <ø> (ø)
net/host.go 76.32% <100.00%> (ø)
net/peer.go 74.65% <100.00%> (ø)
... and 11 more

... and 18 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1281b6a...52f44ca. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nasdf nasdf marked this pull request as ready for review May 5, 2025 20:48
@nasdf nasdf requested a review from a team May 5, 2025 20:48
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good Keenan! Is really neat considering what it manages to achieve :)

Is a shame we can quite do a pure wasm build, yet, but maybe Chris's C binding work will solve that soon.

I didn't review any code that moved, please let me know if anything was changed in the code that did move.

I have a few questions, but I don't think any answers would cause me to want to block the PR so I'm approving now.

@$(MAKE) clean:coverage


.PHONY: test\:coverage-js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why is this a new command? Would GOOS=js GOARCH=wasm make test:coverage not work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work because not all of the packages are JS / WASM compatible. If we wanted to go that route we'd need to add a bunch of //go:build !js tags to the files in those packages..

}

ctx := identity.WithContext(cmd.Context(), immutable.Some(ident))
ctx := acpIdentity.WithContext(cmd.Context(), immutable.Some(ident))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why did you remove the unaliased import instead of the aliased one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was seeing a linter warning in my IDE about the same packagebeing imported twice.

// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build !js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: This is a bit annoying to have to include, why is it required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these can be removed now. I'll double check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like they are not needed. I've removed all of the build flags from tests/bench.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks Keenan


//go:build js

package js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Does this execute the js client as wasm in a JS runtime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, everything (including the test framework) is executed in a NodeJS runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! That's really great, thanks Keenan.

test := testUtils.TestCase{
Description: "Positive increments of a P Counter with Int type causing overflow behaviour",
SupportedClientTypes: immutable.Some([]testUtils.ClientType{
// JS client does not support 64 bit integers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add/document this somewhere incase there are others who wonder about this later.

viewType ViewType
// skipNetworkTests will skip any tests that involve network actions
skipNetworkTests = false
// skipBackupTests will skip any tests taht involve backup actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Typo taht

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

cliClient = false
jsClient = true
skipNetworkTests = true
skipBackupTests = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Please document why net and backup are skipped (within this func)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Let me know if its a good enough explanation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'why' regarding backup is still a little vague to me. Feel free to keep the comment as-is if you feel like though - I think I'm questioning the design decision as much as the documentation.

I do not see a good reason (other than dev-time priority) to not support importing data in a JS runtime, and can easily imagine that to be very useful.

Exporting to file is harder as you mentioned in another comment, however returning the json could be very useful too (and then let the user decide what they want to do with it, instead of forcing it to file).

I don't remember ever looking at the implementation of import/export, but to me working with files (or any other medium) seems to be a very lightweight bonus option that should be sat on top of the core feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it could be useful. I'd prefer to do this in a separate PR since this one is already large. It will likely require changes to the backup API as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, completely on onboard with that, and it is a different priority really to this main body of work.

panic("not implemented")
}

func (w *Wrapper) BasicImport(ctx context.Context, filepath string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why aren't Import/Export implemented?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a short discussion about it in the standup. This backup API implementation doesn't make much sense in JS environments. There's also technical issues with where the files should be written in a browser.

@nasdf nasdf requested a review from shahzadlone May 6, 2025 14:01
Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for doing the suggested changes and investigating why develop change was sneaking in. Just left a question regarding some acp integration test structure refactoring with js build that was done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I am a bit confused, so stuff was moved from acp.go into acp_setup.go and acp_setup_js.go. And only the acp_setup.go is ignored when building with js build-tag, both other files are included with both js and non-js builds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files that end in _js.go automatically have the JS build tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was wondering was does this mean that when we don't build with js build tag, then the _js.go will still be loaded and is not being ignored, is that expected?

i.e. acp_setup.go is being ignored when we build with js tag so was thinking that if we aren't then the acp_setup_js.go will be ignored similarly (which it is not atm).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works the same as the _test.go suffix. The files are only included when you use the js build tag.

test := testUtils.TestCase{
Description: "Positive increments of a P Counter with Int type causing overflow behaviour",
SupportedClientTypes: immutable.Some([]testUtils.ClientType{
// JS client does not support 64 bit integers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add/document this somewhere incase there are others who wonder about this later.

@nasdf nasdf merged commit bb6c439 into sourcenetwork:develop May 12, 2025
44 of 46 checks passed
@islamaliev
Copy link
Contributor

Bug bash result: created #3778

ChrisBQu pushed a commit to ChrisBQu/defradb that referenced this pull request Jun 16, 2025
## Relevant issue(s)

Resolves sourcenetwork#2442 

## Description

This PR adds a JS client implementation to DefraDB.

Sorry for the messy commit history. I accidentally started this on a
previous feature branch.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

Added integration test client wrapper.

Specify the platform(s) on which this was tested:
- MacOS
@fredcarle fredcarle mentioned this pull request Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WASM test wrapper

4 participants