+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(node-api): create a node.js wrapper library for the JSON-RPC workspace protocol #3070

Merged
merged 6 commits into from
Aug 23, 2022

Conversation

leops
Copy link
Contributor

@leops leops commented Aug 16, 2022

Summary

Closes #3048

This PR creates a new @rometools/backend-jsonrpc library that implements the Workspace API by wrapping the JSON-RPC protocol exposed by the daemon server. The library is split into a few modules that may be imported independently:

  • command resolves a file system path to the Rome binary
  • socket ensures the daemon process is spawned and opens a node.js Socket connection to the RPC channel
  • transport wraps an open socket and implements the JSON-RPC protocol as untyped method calls and responses
  • workspace is automatically generated from the JSON schema of the Workspace API, and wraps a Transport instance with an object implementing typed methods calls to the remote workspace

At the top level of the library a single createWorkspace method calls each of these in sequence, wrapping each layer with the next one and returning a Workspace object ready to use

Test Plan

I added a small test script that creates creates a workspace instance (using a local build of the binary from the Cargo target directory), executes a few method calls then shuts down

@leops leops requested a review from ematipico as a code owner August 16, 2022 16:30
@leops leops requested a review from a team August 16, 2022 16:30
@leops leops temporarily deployed to aws August 16, 2022 16:30 Inactive
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 16, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1633ef9
Status: ✅  Deploy successful!
Preview URL: https://3ffddac4.tools-8rn.pages.dev
Branch Preview URL: https://feature-node-api-jsonrpc.tools-8rn.pages.dev

View logs

@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 396 396 0
Failed 5550 5550 0
Panics 0 0 0
Coverage 6.66% 6.66% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12397 12397 0
Failed 3860 3860 0
Panics 0 0 0
Coverage 76.26% 76.26% 0.00%

@github-actions
Copy link

github-actions bot commented Aug 16, 2022

@leops leops temporarily deployed to aws August 17, 2022 07:39 Inactive
@leops leops temporarily deployed to aws August 17, 2022 08:39 Inactive
Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Is there any chance/way to make the Transport testable? I would like to test the error cases, to make sure it works as expected

@@ -81,6 +81,46 @@ jobs:
command: test
args: --workspace --verbose

test-node-api:
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this is the correct place. Although neither the workflow for JS file is the correct place. I mean, we should run these tests when we change Rust files AND JS files.

Maybe we should have a workflow just for this package?

Comment on lines +14 to +19
"JavaScript",
"TypeScript",
"format",
"lint",
"toolchain"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: are the keywords appropriate?

@@ -0,0 +1,28 @@
{
"name": "@rometools/backend-jsonrpc",
"version": "0.8.0-next",
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we should start from 0.0.1? Or 0.0.0 if we don't intend to publish it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This version number is the same as the main rome package and all the cli-* wrappers, but it gets updated automatically in the generate-packages.mjs script that runs during releases anyway so I could set it to 0.0.0 as a placeholder

Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

In my view it would be useful to add documentation to all public types/methods and to add a small readme to the project as well (which also indicates, that this API is considered internal and may change at any point in time).

Comment on lines +27 to +43
await transport.request("initialize", {
capabilities: {},
client_info: {
name: "@rometools/backend-jsonrpc",
version: "0.8.0-next",
},
});

return wrapTransport(transport);
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the transport fails to initialize a connection (or create a socket)? Does it throw any errors and if so which one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Creating the socket isn't really up to the transport layer (it expects to be provided with an existing Socket instance on construction), but the createSocket function will throw an exception is the socket cannot be opened (the socket emits an instance of Error through the "error" event), and the remote call to initialize may also throw an RPC error object if the remote server reports an error

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense for us to introduce a RomeBackendError that is thrown in any situation where the backend runs into an error (and we use RomeError in higher level APIs?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be useful to introduce a custom error class wrapping the RPC error objects since those contain structured informations (they may include a serialized RomeError for instance), but I'm not sure we can provide much value by wrapping the I/O errors coming from node itself because of an issue with the connection

/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer if we could delete all the setting that we aren't using. It makes it hard to see what our explicit settings are.

Copy link
Contributor

@ematipico ematipico Aug 17, 2022

Choose a reason for hiding this comment

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

I'd prefer to keep them because we haven't figure out a correct build process, so if we need some specific property turned on, we have the benefit of the comments. We can clean it once the build process is final

@@ -387,16 +68,10 @@ fn write_protocol() -> (String, Vec<String>) {
// ensure it looks good by running it through the formatter
let formatted = format_node(JsFormatContext::default(), module.syntax()).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@leops leops temporarily deployed to aws August 17, 2022 14:32 Inactive
@leops leops force-pushed the feature/node-api-jsonrpc branch from 9783183 to cd00bbd Compare August 18, 2022 15:13
@leops leops temporarily deployed to aws August 18, 2022 15:17 Inactive
@leops leops temporarily deployed to aws August 18, 2022 15:25 Inactive
@ematipico ematipico merged commit 9626afa into main Aug 23, 2022
@ematipico ematipico deleted the feature/node-api-jsonrpc branch August 23, 2022 07:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement a runtime backend
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载