Skip to content

Commit 7e1b19b

Browse files
committed
refactor(deno-runtime): move prepareEnvironment to main.ts
1 parent 59a8844 commit 7e1b19b

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

packages/apps/deno-runtime/handlers/app/construct.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Socket } from 'node:net';
2-
31
import type { IParseAppPackageResult } from '@rocket.chat/apps/dist/server/compiler/IParseAppPackageResult';
42

53
import { AppObjectRegistry } from '../../AppObjectRegistry';
@@ -11,19 +9,6 @@ import { RequestContext } from '../../lib/requestContext';
119
const ALLOWED_NATIVE_MODULES = ['path', 'url', 'crypto', 'buffer', 'stream', 'net', 'http', 'https', 'zlib', 'util', 'punycode', 'os', 'querystring', 'fs'];
1210
const ALLOWED_EXTERNAL_MODULES = ['uuid'];
1311

14-
function prepareEnvironment() {
15-
// Deno does not behave equally to Node when it comes to piping content to a socket
16-
// So we intervene here
17-
const originalFinal = Socket.prototype._final;
18-
// deno-lint-ignore no-explicit-any
19-
Socket.prototype._final = function _final(cb: any) {
20-
// Deno closes the readable stream in the Socket earlier than Node
21-
// The exact reason for that is yet unknown, so we'll need to simply delay the execution
22-
// which allows data to be read in a response
23-
setTimeout(() => originalFinal.call(this, cb), 1);
24-
};
25-
}
26-
2712
// As the apps are bundled, the only times they will call require are
2813
// 1. To require native modules
2914
// 2. To require external npm packages we may provide
@@ -87,8 +72,6 @@ export default async function handleConstructApp(request: RequestContext): Promi
8772
throw new Error('Invalid params', { cause: 'invalid_param_type' });
8873
}
8974

90-
prepareEnvironment();
91-
9275
AppObjectRegistry.set('id', appPackage.info.id);
9376
const source = sanitizeDeprecatedUsage(appPackage.files[appPackage.info.classFile]);
9477

packages/apps/deno-runtime/main.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Socket } from 'node:net';
12
import process from 'node:process';
23

34
import { JsonRpcError, type SuccessObject } from 'jsonrpc-lite';
@@ -127,9 +128,25 @@ async function main() {
127128
}
128129
}
129130

131+
function prepareEnvironment() {
132+
// Deno does not behave equally to Node when it comes to piping content to a socket
133+
// So we intervene here
134+
const originalFinal = Socket.prototype._final;
135+
// deno-lint-ignore no-explicit-any
136+
Socket.prototype._final = function _final(cb: any) {
137+
// Deno closes the readable stream in the Socket earlier than Node
138+
// The exact reason for that is yet unknown, so we'll need to simply delay the execution
139+
// which allows data to be read in a response
140+
setTimeout(() => originalFinal.call(this, cb), 1);
141+
};
142+
}
143+
130144
// This runtime communicates with the Apps-Engine host through stdout
131145
Messenger.setTransport(stdoutTransport);
132146

133147
registerErrorListeners();
134148

149+
// Process-global side effect; doing it once at startup is cleaner than inside construct
150+
prepareEnvironment();
151+
135152
main();

0 commit comments

Comments
 (0)