-
Notifications
You must be signed in to change notification settings - Fork 13.7k
feat(apps): introduce alternative node-runtime #41019
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@rocket.chat/apps': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Adds an alternative runtime runner for apps. It can be enabled via environment variable `APPS_ENGINE_RUNTIME_BACKEND='node'` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 'use strict'; | ||
|
|
||
| /* | ||
| * Mocha configuration for REST API integration tests. | ||
| */ | ||
|
|
||
| module.exports = /** @satisfies {import('mocha').MochaOptions} */ ({ | ||
| ...require('./.mocharc.base.json'), // see https://github.com/mochajs/mocha/issues/3916 | ||
| timeout: 10000, | ||
| bail: false, | ||
| retries: 0, | ||
| file: 'tests/end-to-end/teardown.ts', | ||
| reporter: 'tests/end-to-end/reporter.ts', | ||
| spec: ['tests/end-to-end/apps/*'], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import * as Messenger from '@rocket.chat/apps/base-runtime/dist/lib/messenger'; | ||
|
|
||
| export default function registerErrorListeners() { | ||
| process.on('uncaughtException', (error: Error, origin: 'uncaughtException' | 'unhandledRejection') => { | ||
| Messenger.sendNotification({ | ||
| method: origin, | ||
| params: [error.stack || error], | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { registerHooks } from 'node:module'; | ||
| import path from 'node:path'; | ||
|
|
||
| // This file compiles to dist/lib/loader-hook.js. | ||
| // Three levels up from dist/lib/ lands on packages/apps/ — the @rocket.chat/apps package root. | ||
| const appsPackageDir = path.resolve(__dirname, '../../..'); | ||
|
|
||
| const PACKAGE_PREFIX = '@rocket.chat/apps'; | ||
|
|
||
| registerHooks({ | ||
| resolve(specifier, context, nextResolve) { | ||
| if (specifier === PACKAGE_PREFIX || specifier.startsWith(`${PACKAGE_PREFIX}/`)) { | ||
| const subpath = specifier.slice(PACKAGE_PREFIX.length).replace(/^\//, ''); | ||
| const localPath = subpath ? path.join(appsPackageDir, subpath) : appsPackageDir; | ||
| return nextResolve(localPath, context); | ||
| } | ||
| return nextResolve(specifier, context); | ||
| }, | ||
|
d-gubert marked this conversation as resolved.
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const ALLOWED_MODULES = [ | ||
| 'path', | ||
| 'url', | ||
| 'crypto', | ||
| 'buffer', | ||
| 'stream', | ||
| 'net', | ||
| 'http', | ||
| 'https', | ||
| 'zlib', | ||
| 'util', | ||
| 'punycode', | ||
| 'os', | ||
| 'querystring', | ||
| 'fs', | ||
| // External libraries | ||
| 'uuid', | ||
| '@rocket.chat/apps-engine', | ||
| ]; | ||
|
|
||
| // As the apps are bundled, the only times they will call require are | ||
| // 1. To require native modules | ||
| // 2. To require external npm packages we may provide | ||
| // 3. To require apps-engine files | ||
| export const sandboxRequire = (module: string) => { | ||
| // Normalize Node built-in specifiers: accept both 'crypto' and 'node:crypto' | ||
| const normalized = module.replace('node:', ''); | ||
|
|
||
| // We allow variants like 'fs', 'node:fs' or 'node:fs/promises', or even '@rocket.chat/apps-engine/**' | ||
| if (!ALLOWED_MODULES.some((mod) => normalized.startsWith(mod))) { | ||
|
d-gubert marked this conversation as resolved.
|
||
| throw new Error(`Module ${module} is not allowed`); | ||
| } | ||
|
|
||
| // This is THE purpose of this function, we can't escape a dinamyc require call | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-return, import/no-dynamic-require, @typescript-eslint/no-require-imports | ||
| return require(normalized); | ||
| }; | ||
|
d-gubert marked this conversation as resolved.
|
||
16 changes: 16 additions & 0 deletions
16
packages/apps/node-runtime/src/lib/transports/stdoutTransport.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { Transport } from '@rocket.chat/apps/base-runtime/dist/lib/messenger'; | ||
|
|
||
| /** | ||
| * Transport that writes messages to the process' standard output. | ||
| * | ||
| * This is the transport used when the runtime is executed as a subprocess by | ||
| * the Apps-Engine framework, and it is specific to platforms that expose a | ||
| * Node-compatible `process.stdout`. | ||
| */ | ||
| export const stdoutTransport: Transport = { | ||
| async send(message: Uint8Array): Promise<void> { | ||
| await new Promise<void>((resolve, reject) => { | ||
| process.stdout.write(message, (err) => (err ? reject(err) : resolve())); | ||
| }); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import './lib/loader-hook'; | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
d-gubert marked this conversation as resolved.
|
||
|
|
||
| import { setSandboxGlobals, setSandboxRequire } from '@rocket.chat/apps/base-runtime/dist/handlers/app/construct'; | ||
| import { setTransport } from '@rocket.chat/apps/base-runtime/dist/lib/messenger'; | ||
| import { startMainLoop } from '@rocket.chat/apps/base-runtime/dist/mainLoop'; | ||
|
|
||
| import registerErrorListeners from './error-handlers'; | ||
| import { sandboxRequire } from './lib/require'; | ||
| import { stdoutTransport } from './lib/transports/stdoutTransport'; | ||
|
|
||
| if (!process.argv.includes('--subprocess')) { | ||
| console.error(` | ||
| This is the Node wrapper for the Rocket.Chat Apps runtime. It is not meant to be executed stand-alone; | ||
| It is instead meant to be executed as a subprocess by the Apps-Engine framework. | ||
| `); | ||
|
|
||
| process.exit(1); | ||
| } | ||
|
|
||
| // This runtime communicates with the Apps-Engine host through stdout | ||
| setTransport(stdoutTransport); | ||
|
|
||
| // The sandbox `require` handed to the app is Node's own global `require`; it | ||
| // needs no extra globals beyond the common ones the base eval shell binds. | ||
| setSandboxRequire(sandboxRequire); | ||
| setSandboxGlobals({}); | ||
|
|
||
| registerErrorListeners(); | ||
|
|
||
| void startMainLoop(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "rootDir": "./src", | ||
| "outDir": "./dist", | ||
| "baseUrl": ".", | ||
| "types": ["node"], | ||
| "lib": ["es2023"], | ||
| "module": "nodenext", | ||
| "moduleResolution": "nodenext", | ||
| "target": "es2023", | ||
| "declaration": true, | ||
| "paths": { | ||
| "@rocket.chat/apps/*": ["../*"] | ||
| } | ||
| }, | ||
| "include": ["./src/**/*"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.