Stream: jco

Topic: Basic hello-wasi-http-js sample from ground up


view this post on Zulip Ralph (Jan 19 2024 at 14:22):

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.

view this post on Zulip Ralph (Jan 19 2024 at 14:22):

But that's custom wit.

view this post on Zulip Ralph (Jan 19 2024 at 14:24):

so for wasi:http, do I need to implement this?

view this post on Zulip Ralph (Jan 19 2024 at 14:24):

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
);
}

view this post on Zulip Ralph (Jan 19 2024 at 14:26):

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?

view this post on Zulip Ralph (Jan 19 2024 at 14:26):

or is there a js binding generator for wasi:http/proxy that I need to locate and them merely "import" the types to use?

view this post on Zulip Ralph (Jan 19 2024 at 14:27):

i get that concept, and I'm golden here.

view this post on Zulip Ralph (Jan 19 2024 at 15:14):

wait, are these they: do I just need to reference them in the package.json?

view this post on Zulip Ralph (Jan 19 2024 at 15:14):

},
"devDependencies": {
"@bytecodealliance/jco": "^0.14.2",
"@bytecodealliance/preview2-shim": "^0.14.2",

view this post on Zulip Ramon Klass (Jan 19 2024 at 15:23):

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

view this post on Zulip Ralph (Jan 19 2024 at 15:27):

I'm on preview 12_05, so that's fine

view this post on Zulip Ralph (Jan 19 2024 at 15:33):

sorry for not using

view this post on Zulip Ralph (Jan 19 2024 at 15:33):


view this post on Zulip Guy Bedford (Jan 19 2024 at 18:33):

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.

view this post on Zulip Ralph (Jan 19 2024 at 19:08):

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.....

view this post on Zulip Ralph (Jan 19 2024 at 19:10):

when you say generating bindings, what do you mean that's not:
image.png

view this post on Zulip Guy Bedford (Jan 19 2024 at 19:15):

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

view this post on Zulip Guy Bedford (Jan 19 2024 at 19:16):

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

view this post on Zulip Ramon Klass (Jan 19 2024 at 19:16):

at least with jco transpile, it does put the .d.ts files in the right locations and IDEs can just autocomplete imports

view this post on Zulip Guy Bedford (Jan 19 2024 at 19:17):

yeah so if we had something similar for ComponentizeJS it might be useful to generate types that work for authoring components to arbitrary WIT

view this post on Zulip Ralph (Jan 19 2024 at 19:17):

well, from my position I've been building all the wasi:http languages as best I can

view this post on Zulip Ralph (Jan 19 2024 at 19:18):

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.

view this post on Zulip Ralph (Jan 19 2024 at 19:19):

but I couldn't figure out where to get the .js/.ts types for wasi:http

view this post on Zulip Ralph (Jan 19 2024 at 19:20):

npm install preview2-shim gave them to me, which sure seemed easy to me......

view this post on Zulip Ralph (Jan 19 2024 at 19:21):

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.

view this post on Zulip Ramon Klass (Jan 19 2024 at 19:22):

I think that's where componentizeJS comes in, it should implement the "make a component out of a js file and spidermonkey" step

view this post on Zulip Ralph (Jan 19 2024 at 19:23):

that's what I think, too, but isn't jco component that comment?

view this post on Zulip Ralph (Jan 19 2024 at 19:23):

command?

view this post on Zulip Ralph (Jan 19 2024 at 19:23):

because help says that YOU MUST have a component on the argument line

view this post on Zulip Ramon Klass (Jan 19 2024 at 19:24):

yeah I also was confused, from the docs both implement the same, @Guy Bedford probably needs to answer that

view this post on Zulip Ralph (Jan 19 2024 at 19:24):

whoops, no, I'm wrong

view this post on Zulip Ralph (Jan 19 2024 at 19:24):

apologies

view this post on Zulip Ralph (Jan 19 2024 at 19:25):

It appears I need to: jco componentize -w <path> -o hello-wasi-http-js.wasm index.js

view this post on Zulip Ramon Klass (Jan 19 2024 at 19:25):

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?

view this post on Zulip Ralph (Jan 19 2024 at 19:25):

and that should work

view this post on Zulip Ralph (Jan 19 2024 at 19:26):

yeah, just working over Guy's stuff so I can complain because he and wassim and yosh and others worked so hard

view this post on Zulip Ralph (Jan 19 2024 at 19:26):

:-)

view this post on Zulip Ralph (Jan 19 2024 at 19:26):

goint to give this a try

view this post on Zulip Ralph (Jan 19 2024 at 19:26):

if it works, I'm going to drink and tell Guy it's amazing

view this post on Zulip Ramon Klass (Jan 19 2024 at 19:28):

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