denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.1k stars 5.4k forks source link

TypeError: expected i32 #27091

Open joeldomke opened 21 hours ago

joeldomke commented 21 hours ago

Version: Deno 2.1.1 1.45.2

I wrote an LTI integration using ltijs as an node app. Then I tried to move the code into an edge function and it fails with the following error:

runtime has escaped from the event loop unexpectedly: event loop error: TypeError: expected i32
    at IncomingMessageForClient._read (node:http:781:10)
    at Readable.read (ext:deno_node/_stream.mjs:2995:16)
    at resume_ (ext:deno_node/_stream.mjs:3345:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:33:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:175:21)
failed to send request to user worker: event loop error: TypeError: expected i32
    at IncomingMessageForClient._read (node:http:781:10)
    at Readable.read (ext:deno_node/_stream.mjs:2995:16)
    at resume_ (ext:deno_node/_stream.mjs:3345:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:33:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:175:21)
user worker failed to respond: event loop error: TypeError: expected i32
    at IncomingMessageForClient._read (node:http:781:10)
    at Readable.read (ext:deno_node/_stream.mjs:2995:16)
    at resume_ (ext:deno_node/_stream.mjs:3345:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:33:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:175:21)
InvalidWorkerResponse: event loop error: TypeError: expected i32
    at IncomingMessageForClient._read (node:http:781:10)
    at Readable.read (ext:deno_node/_stream.mjs:2995:16)
    at resume_ (ext:deno_node/_stream.mjs:3345:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:33:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:175:21)
    at async Promise.allSettled (index 1)
    at async UserWorker.fetch (ext:sb_user_workers/user_workers.js:72:61)
    at async Object.handler (file:///root/index.ts:173:14)
    at async respond (ext:sb_core_main_js/js/http.js:163:14) {
  name: "InvalidWorkerResponse"
}

While working with the node app I was able to modify the ltijs code in the node_modules to pinpoint any issues. This is no longer possible since I moved everything into Supabase edge functions.

The library uses the node module debug for logging. But when I try to turn on logging using an environment variable I get a completely new error.

runtime has escaped from the event loop unexpectedly: event loop error: NotSupported: The operation is not supported
    at Object.setEnv [as set] (ext:sb_env/env.js:5:9)
    at Object.set (ext:deno_node/_process/process.ts:66:14)
    at Function.save (file:///var/tmp/sb-compile-edge-runtime/node_modules/localhost/debug/4.3.7/src/node.js:205:21)
    at Function.enable (file:///var/tmp/sb-compile-edge-runtime/node_modules/localhost/debug/4.3.7/src/common.js:163:15)
    at file:///Users/joel/IdeaProjects/business-simulation/supabase/functions/ltijs/index.ts:6:7

Does this mean that certain node features required by this library are not yet supported by Deno?

Without logging or being able to modify the ltijs library I am unsure how to further narrow down the problem.

Here is my code:

import { Provider as lti } from 'ltijs';
import Database from './DB.ts';
import pg from 'pg';

// Setup ltijs-sequelize using the same arguments as Sequelize's generic contructor
// Database is hosted under http://localhost:54322/
const db = new Database('postgres', 'postgres', 'postgres', {
    host: '172.17.0.1',
    port: 54322,
    dialect: 'postgres',
    schema: 'lti',
    dialectModule: pg,
    logging: true,
});

// Setup provider
lti.setup(
    'LTIKEY', // Key used to sign cookies and tokens
    {
        plugin: db,
    },
    {
        // Options
        appRoute: '/ltijs/launch',
        loginRoute: '/ltijs/login', // Optionally, specify some of the reserved routes
        cookies: {
            secure: false, // Set secure to true if the testing platform is in a different domain and https is being used
            sameSite: '', // Set sameSite to 'None' if the testing platform is in a different domain and https is being used
        },
        devMode: true, // Set DevMode to false if running in a production environment with https
    },
);

// Set lti launch callback
lti.onConnect((token, req, res) => {
    console.log('onConnect');
    console.log(token);
    return res.send("It's alive!");
});

const setup = async () => {
    // Deploy server and open connection to the database
    await lti.deploy({ port: 3000 }); // Specifying port. Defaults to 3000

    // Register platform
    await lti.registerPlatform({
        url: 'https://saltire.lti.app/platform',
        name: 'Saltire LTI App',
        clientId: 'saltire.lti.app',
        authenticationEndpoint: 'https://saltire.lti.app/platform/auth',
        accesstokenEndpoint: 'https://saltire.lti.app/platform/token/30d3313d7c655cdc2e722bafeb7374b9',
        authConfig: {
            method: 'JWK_SET',
            key: 'https://saltire.lti.app/platform/jwks/30d3313d7c655cdc2e722bafeb7374b9',
        },
    });
};

setup();
joeldomke commented 20 hours ago

First, the login route is called without any issues. This shows the library is somewhat working and the database connection is also working. The second request is to the app route.

There is a session validator used by the express server that is skipped for the login route. I guess that is where the call to the app route is failing but this method has more than 250 lines of code and also depends on other libraries as far as I can tell.

I understand that you can't debug that code. If the stack trace is not enough to point toward an issue I would be grateful for any hints on how to narrow this problem down.

joeldomke commented 20 hours ago

Okay, I just noticed that I have Deno 2.1.1 installed. But since I am using the Supabase Edgeruntime it is an older version:

Using supabase-edge-runtime-1.62.2 (compatible with Deno v1.45.2)

joeldomke commented 20 hours ago

Switching from 'npm:ltijstohttps://esm.sh/ltijs@5.9.5` produces

Download https://esm.sh/error.js?type=unsupported-node-builtin-module&name=http2&importer=http2-wrapper@1.0.3
Download https://esm.sh/error.js?type=unsupported-node-builtin-module&name=async_hooks&importer=on-finished@2.4.1
Download https://esm.sh/error.js?type=unsupported-node-builtin-module&name=async_hooks&importer=raw-body@2.5.2
worker boot error: failed to create the graph: Import 'https://esm.sh/error.js?type=unsupported-node-builtin-module&name=http2&importer=http2-wrapper@1.0.3' failed: 500 Internal Server Error
    at https://esm.sh/v135/http2-wrapper@1.0.3/esnext/http2-wrapper.mjs:4:23
worker boot error: failed to create the graph: Import 'https://esm.sh/error.js?type=unsupported-node-builtin-module&name=http2&importer=http2-wrapper@1.0.3' failed: 500 Internal Server Error
    at https://esm.sh/v135/http2-wrapper@1.0.3/esnext/http2-wrapper.mjs:4:23
InvalidWorkerCreation: worker boot error: failed to create the graph: Import 'https://esm.sh/error.js?type=unsupported-node-builtin-module&name=http2&importer=http2-wrapper@1.0.3' failed: 500 Internal Server Error
    at https://esm.sh/v135/http2-wrapper@1.0.3/esnext/http2-wrapper.mjs:4:23
    at async UserWorker.create (ext:sb_user_workers/user_workers.js:139:15)
    at async Object.handler (file:///root/index.ts:156:22)
    at async respond (ext:sb_core_main_js/js/http.js:163:14) {
  name: "InvalidWorkerCreation"
}

So I guess something around http2 is just not supported by the Deno version I use.

marvinhagemeister commented 59 minutes ago

The http and http2 module received lots of bug fixes leading up to Deno 2.x . Does the error occur in latest Deno version too or only in older ones?