-
Notifications
You must be signed in to change notification settings - Fork 6
refactor: Differentiate commands and command replies #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Differentiate commands and command replies #94
Conversation
5c0d615 to
a30d99a
Compare
rekmarks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Just some minor inline things.
…ker (#100) This merges the work I've been doing on accessing SQLite from a web worker. This PR supercedes my earlier PR #74, since so much had changed in the main branch that it was easier to resync the changes in a new branch rather than to try to reconcile all the now irrelevant parts. Stuff that is still to do / stuff to note (a number of things are left undone in interest of moving things along, since they can be readily handled as separate issues/PRs): - This code creates a web worker that should eventually subsume the kernel, hence the file is called `kernel-worker.ts`. However, this does not replace the existing kernel (or `Kernel`) that's in the current source tree, but rather lives alongside it. The source file undoubtedly should be moved to the `kernel` package, and then split into the parts that go into the `Kernel` class and the parts that go into the script that launches the kernel (#57). - Messaging to and from the web worker is not yet integrated with our streams abstraction; for now I'm just using `postMessage` in the stupidest possible way. Integrating with streams proved cross-cutting enough with what I was already doing that I figured that was best handled in a separate pass. (#todo) - In the same vein, this would really like to have the rationalization of the messaging layer that @grypez is in the midst of, most especially more proper handling of reply messages. In particular, I found that the way we handle parameters is kind of sketchy, mostly because virtually the only operation we have that really uses parameters is `CapTpCall` and it is handled as a special case. Consequently I ended up having to add a case to the `isCommand` type guard to accept an array as one of the valid parameter types in order to accommodate the `kvSet` and `kvGet` kernel commands that I added. This is kind of a hack; while those two operations will go away, I expect there to be numerous commands to come that will really want proper parameter handling. (#94) - The database usage right now is just a simple key/value store that can be accessed via the console "API". This k/v thing is strictly for testing and validation purposes and should go away. However, inside `kernel-worker.ts` you'll find a bundle of preliminary TypeScript type defs for the state that the kernel should actually be storing. My plan is to generate corresponding database schemata as part of putting in the vat-to-vat message routing infrastructure (assuming nobody else gets to this first, which y'all have been amazingly good at doing). - In keeping with this being a PoC, there are no tests. Those will come when there's something worth testing.
bec0434 to
1bd23aa
Compare
9013a64 to
697c769
Compare
Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>
697c769 to
d1b4aa0
Compare
packages/utils/src/types.ts
Outdated
| | CommandLike<CommandMethod.KVSet, { key: string; value: string }>; | ||
|
|
||
| export type VatMessage = { | ||
| export type CommandInterface<Return = void | Promise<void>> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Interface" is a bit ambiguous for this type. Can we replace it with "Function"? Ditto for CommandReplyInterface.
| export type CommandInterface<Return = void | Promise<void>> = { | |
| export type CommandFunction<Return = void | Promise<void>> = { |
| isCommandLike(value) && | ||
| (typeof value.params === 'string' || | ||
| value.params === null || | ||
| isObject(value.params) || // XXX certainly wrong, needs better TypeScript magic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this line from Chip's PR interact with the command types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not significantly looser than what we were already doing, which is checking that the method is an allowable type independently of the whether the params are an allowable type.
Toward #80, provides a typed distinction between issued commands and their replies.
Changes:
@ocap/streamsStreamPairnow requires distinctReadandWritetemplate parameters.@ocap/utilsCommandReply.CommandFunctionandCommandReplyFunctionto prevent mismatching method and params types in functions that accept these properties as independent arguments.VatMessageintoVatCommandandVatCommandReply.streamEnvelopeKitis for issued commands, while the newstreamEnvelopeReplyKitis for replies. Exports from the reply kit are named alike but have the suffixReply, excepting the factorymakeStreamEnvelopeReplyHandlerand typeStreamEnvelopeReplyHandler.@ocap/extension@ocap/kernelStreamEnvelopeHandlernow expects issued commands, not command replies, at both typescript time and runtime.StreamEnvelopeReplyHandlerwhen receiving messages. The handler has been renamed tostreamEnvelopeReplyHandlerto obviate this change.