1- import { transformSync } from '@babel/core' ;
2- import presetEnv from '@babel/preset-env' ;
31import type { IIntegration , INewIncomingIntegration , IUpdateIncomingIntegration } from '@rocket.chat/core-typings' ;
42import type { ServerMethods } from '@rocket.chat/ddp-client' ;
53import { Integrations , Subscriptions , Users , Rooms } from '@rocket.chat/models' ;
@@ -9,6 +7,7 @@ import { Meteor } from 'meteor/meteor';
97import { addUserRolesAsync } from '../../../../../server/lib/roles/addUserRoles' ;
108import { hasAllPermissionAsync , hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission' ;
119import { notifyOnIntegrationChanged } from '../../../../lib/server/lib/notifyListener' ;
10+ import { compileIntegrationScript } from '../../lib/compileIntegrationScript' ;
1211import { isScriptEngineFrozen , validateScriptEngine } from '../../lib/validateScriptEngine' ;
1312
1413const validChannelChars = [ '@' , '#' ] ;
@@ -84,49 +83,28 @@ export const updateIncomingIntegration = async (
8483
8584 const isFrozen = isScriptEngineFrozen ( scriptEngine ) ;
8685
87- if ( ! isFrozen ) {
88- let scriptCompiled : string | undefined ;
89- let scriptError : Pick < Error , 'name' | 'message' | 'stack' > | undefined ;
90-
91- if ( integration . scriptEnabled === true && integration . script && integration . script . trim ( ) !== '' ) {
92- try {
93- const result = transformSync ( integration . script , {
94- presets : [ presetEnv ] ,
95- compact : true ,
96- minified : true ,
97- comments : false ,
98- } ) ;
99-
100- // TODO: Webhook Integration Editor should inform the user if the script is compiled successfully
101- scriptCompiled = result ?. code ?? undefined ;
102- scriptError = undefined ;
103- await Integrations . updateOne (
104- { _id : integrationId } ,
105- {
106- $set : {
107- scriptCompiled,
108- } ,
109- $unset : { scriptError : 1 as const } ,
110- } ,
111- ) ;
112- } catch ( e ) {
113- scriptCompiled = undefined ;
114- if ( e instanceof Error ) {
115- const { name, message, stack } = e ;
116- scriptError = { name, message, stack } ;
117- }
118- await Integrations . updateOne (
119- { _id : integrationId } ,
120- {
121- $set : {
122- scriptError,
123- } ,
124- $unset : {
125- scriptCompiled : 1 as const ,
126- } ,
127- } ,
128- ) ;
129- }
86+ // Default to transpiling with Babel for backwards compatibility; integrations
87+ // can opt-out per-record by setting `skipTranspile: true` (removed in 9.0.0).
88+ const skipTranspile = integration . skipTranspile === true ;
89+
90+ if ( ! isFrozen && integration . scriptEnabled === true && integration . script && integration . script . trim ( ) !== '' ) {
91+ const { script, error } = compileIntegrationScript ( integration . script , { transpile : ! skipTranspile } ) ;
92+ if ( error ) {
93+ await Integrations . updateOne (
94+ { _id : integrationId } ,
95+ {
96+ $set : { scriptError : error , skipTranspile } ,
97+ $unset : { scriptCompiled : 1 as const } ,
98+ } ,
99+ ) ;
100+ } else {
101+ await Integrations . updateOne (
102+ { _id : integrationId } ,
103+ {
104+ $set : { scriptCompiled : script , skipTranspile } ,
105+ $unset : { scriptError : 1 as const } ,
106+ } ,
107+ ) ;
130108 }
131109 }
132110
@@ -192,6 +170,7 @@ export const updateIncomingIntegration = async (
192170 ...( typeof integration . script !== 'undefined' && { script : integration . script } ) ,
193171 scriptEnabled : integration . scriptEnabled ,
194172 ...( scriptEngine && { scriptEngine } ) ,
173+ skipTranspile,
195174 } ) ,
196175 ...( typeof integration . overrideDestinationChannelEnabled !== 'undefined' && {
197176 overrideDestinationChannelEnabled : integration . overrideDestinationChannelEnabled ,
0 commit comments