OK, bear with me, but I want to use javascript (NOT my language, though easy learn) to create a wasi:http component for preview 2. I can do custom wit components easily enough using this sample: https://github.com/bytecodealliance/ComponentizeJS/blob/main/EXAMPLE.md. What is done up front is to take a .wit file and then implement the function listed there, nothing fancy. So far so good. So I have the wasi wit block in a repo.... But I need to understand the binding step for js. In the example above, you merely reference hello
and then implement hello
and call jco componentize
and you have a component.
But that's custom wit.
so for wasi:http, do I need to implement this?
interface incoming-handler {
use types.{incoming-request, response-outparam};
/// This function is invoked with an incoming HTTP Request, and a resource
/// response-outparam
which provides the capability to reply with an HTTP
/// Response. The response is sent by calling the response-outparam.set
/// method, which allows execution to continue after the response has been
/// sent. This enables both streaming to the response body, and performing other
/// work.
///
/// The implementor of this function must write a response to the
/// response-outparam
before returning, or else the caller will respond
/// with an error on its behalf.
handle: func(
request: incoming-request,
response-out: response-outparam
);
}
what I'm missing as a newbie here is the binding mechanism between the WIT function " and the js needed to implement it. Do I simply "implement the function" name, as I can do with the above hello
function?
or is there a js binding generator for wasi:http/proxy that I need to locate and them merely "import" the types to use?
i get that concept, and I'm golden here.
wait, are these they: do I just need to reference them in the package.json?
},
"devDependencies": {
"@bytecodealliance/jco": "^0.14.2",
"@bytecodealliance/preview2-shim": "^0.14.2",
I'm not sure if the shim includes wasi-http but in general yes, you add jco to your devDependencies and preview2-shim to your dependencies and then jco transpile'd components "just work" but as I said I only use preview2, I didn't follow. the wasi-http changes yet
I'm on preview 12_05, so that's fine
sorry for not using
in terms of the JS to define to componentize, you'd want to write a JS file like:
export const incomingHandler = {
handle(request, response) {
// ...
},
};
we do have an example of using outgoing handler in ComponentizeJS in https://github.com/bytecodealliance/jco/blob/main/test/fixtures/componentize/wasi-http-proxy/source.js although it doesn't contain the incoming handler currently, so you'd need to check the types to figure it out.
Generating template JS / TypeScript bindings for these ComponentizeJS workflows has been a TODO for a while... agreed it should be a thing.
thanks Guy; if I've imported preview2-shim and npm intalled, I seem to get the incomingHandler type and it resolves, so hopefully that's really all I need to do.....
when you say generating bindings, what do you mean that's not:
image.png
that looks right, although in ComponentizeJS the bindings have a different import convention - import 'wasi:http@0.2.0-rc-2024-01-16/incoming-handler
etc instead
but if you are just having this import for IDE types to work out making it an import type { IncomingHandler } from '@bytecodealliance/preview2/http'
might make sense with tsc
itself stripping that out
at least with jco transpile, it does put the .d.ts files in the right locations and IDEs can just autocomplete imports
yeah so if we had something similar for ComponentizeJS it might be useful to generate types that work for authoring components to arbitrary WIT
well, from my position I've been building all the wasi:http languages as best I can
I have dan's rust, Joe's go, and Joel's Python implementations of incomingHandler(). So here I am, wanting to make a JavaScript incomingHandler() impl for comparison.
but I couldn't figure out where to get the .js/.ts types for wasi:http
npm install preview2-shim gave them to me, which sure seemed easy to me......
but note, once I have this running I'll be at the point where I need to create a module from the .js (with spidermonkey) and then I can make that module a component.
I think that's where componentizeJS comes in, it should implement the "make a component out of a js file and spidermonkey" step
that's what I think, too, but isn't jco component that comment?
command?
because help says that YOU MUST have a component on the argument line
yeah I also was confused, from the docs both implement the same, @Guy Bedford probably needs to answer that
whoops, no, I'm wrong
apologies
It appears I need to: jco componentize -w <path> -o hello-wasi-http-js.wasm index.js
transpile takes a component and turns that into browser and node-compatible js, componentize seems to implement the same as componentizeJS does, if going by the docs?
and that should work
yeah, just working over Guy's stuff so I can complain because he and wassim and yosh and others worked so hard
:-)
goint to give this a try
if it works, I'm going to drink and tell Guy it's amazing
it was a weird feeling the first time I ran my own pyo3+python based example in a browser, it's impressive how far the component model has come :)
Last updated: Nov 22 2024 at 16:03 UTC