Git Product home page Git Product logo

Comments (11)

guybedford avatar guybedford commented on August 19, 2024 2

Oh, you need to call OutgoingBody.finish(outgoingBody) where you import the OutgoingBody in the import as it's a static method.

from componentizejs.

guybedford avatar guybedford commented on August 19, 2024 1

@brooksmtownsend makes sense, yes it is leaning entirely on the bindings model here. But also, in terms of things we might do to catch these types of issues:

  1. For resources which require to be "explicitly dropped", we could provide a warning on the JS GC that warns that the resource was not explicitly dropped, and then at the end of the application lifecycle, provide these warnings.
  2. For resources which require to be "explicitly consumed", we could have some kind of metadata which notes this and then instead of giving the warning above, instead give a warning with a suggestion of the consuming API method to use.

It's just a little bit of metadata but it could make the user experience a bunch better.

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

For easier testing, here is the raw JS:

import { ResponseOutparam, OutgoingResponse, OutgoingBody, Fields, } from "wasi:http/[email protected]";
// Implementation of wasi-http incoming-handler
//
// NOTE: To understand the types involved, take a look at wit/deps/http/types.wit
function handle(req, resp) {
    // Start building an outgoing response
    const outgoingResponse = new OutgoingResponse(new Fields());
    // Set the status code for the response
    outgoingResponse.setStatusCode(200);
    // Access the outgoing response body
    const outgoingBody = outgoingResponse.body();
    // Create a stream for the response body
    let outputStream = outgoingBody.write();
    // Write hello world to the response stream
    outputStream.blockingWriteAndFlush(new Uint8Array(new TextEncoder().encode("Hello from Typescript!\n")));
    // Set the created response
    ResponseOutparam.set(resp, { tag: "ok", val: outgoingResponse });
    OutgoingBody.finish(outgoingBody, undefined);
}
export const incomingHandler = {
    handle,
};

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

Just a note that I tried to recreate https://github.com/bytecodealliance/jco/blob/7b41c3551d26840a65682803e2c64810645ad732/test/fixtures/componentize/wasi-http-proxy/source.js#L32, and I see the comment there for TODO which might be causing the failure? Unsure

from componentizejs.

guybedford avatar guybedford commented on August 19, 2024

Yes, I believe you need to explicitly drop for WASI to work here - outputStream[Symbol.dispose]().

There's also an example of this in https://github.com/bytecodealliance/ComponentizeJS/blob/main/test/cases/http-request/source.js.

With the explicit resource management proposal (https://github.com/tc39/proposal-explicit-resource-management), and the enforcement of using being presented at TC39 tomorrow, this can be fully solved in future bindgen approaches.

But for now, yes it's a major footgun.

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

Hm I'm surprised, that kept the same behavior 🤔

import { ResponseOutparam, OutgoingResponse, Fields, } from "wasi:http/[email protected]";
// Implementation of wasi-http incoming-handler
//
// NOTE: To understand the types involved, take a look at wit/deps/http/types.wit
function handle(req, resp) {
    // Start building an outgoing response
    const outgoingResponse = new OutgoingResponse(new Fields());
    // Access the outgoing response body
    let outgoingBody = outgoingResponse.body();
    {
        // Create a stream for the response body
        let outputStream = outgoingBody.write();
        // Write hello world to the response stream
        outputStream.blockingWriteAndFlush(new Uint8Array(new TextEncoder().encode("Hello from Typescript!\n")));
        // @ts-ignore: This is required in order to dispose the stream before we return
        outputStream[Symbol.dispose]();
    }
    // Set the status code for the response
    outgoingResponse.setStatusCode(200);
    // Set the created response
    ResponseOutparam.set(resp, { tag: "ok", val: outgoingResponse });
}
export const incomingHandler = {
    handle,
};

I had to tell TS to ignore the warning but that's not a big deal. I'm glad to hear that TC39 will talk about this 🙂

from componentizejs.

guybedford avatar guybedford commented on August 19, 2024

Great, does that resolve the issue then for now at least?

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

@guybedford No it's the same behavior 😢 It didn't seem to affect the problem.

from componentizejs.

guybedford avatar guybedford commented on August 19, 2024

Also, if you want to actually stream the response, connect the stream by calling ResponseOutparam.set(resp, { tag: "ok", val: outgoingResponse }); earlier, before the stream is written to.

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

Calling OutgoingBody.finish(outgoingBody, undefined) was what I needed!

from componentizejs.

brooksmtownsend avatar brooksmtownsend commented on August 19, 2024

@guybedford just wanted to note that I'm gonna close this since my issue is resolved, but let me know if I can assist with updating the bindings to do this automatically. It feels generally like logic that is specific to resources and not ComponentizeJS, so it's implemented correctly here.

from componentizejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.