Remote JS executor to work with tb-node of previous version

This commit is contained in:
ViacheslavKlimov 2022-10-12 19:18:34 +03:00
parent 831332be7b
commit 2d262146ad
4 changed files with 60 additions and 28 deletions

View File

@ -41,34 +41,34 @@ message RemoteJsResponse {
}
message JsCompileRequest {
string scriptHash = 1;
string functionName = 2;
string scriptBody = 3;
string functionName = 3;
string scriptBody = 4;
string scriptHash = 5;
}
message JsReleaseRequest {
string scriptHash = 1;
string functionName = 2;
string functionName = 3;
string scriptHash = 4;
}
message JsReleaseResponse {
bool success = 1;
string scriptHash = 2;
string scriptHash = 4;
}
message JsCompileResponse {
bool success = 1;
string scriptHash = 2;
JsInvokeErrorCode errorCode = 3;
string errorDetails = 4;
JsInvokeErrorCode errorCode = 4;
string errorDetails = 5;
string scriptHash = 6;
}
message JsInvokeRequest {
string scriptHash = 1;
string functionName = 2;
string scriptBody = 3;
int32 timeout = 4;
repeated string args = 5;
string functionName = 3;
string scriptBody = 4;
int32 timeout = 5;
repeated string args = 6;
string scriptHash = 7;
}
message JsInvokeResponse {

View File

@ -16,6 +16,8 @@
export interface TbMessage {
scriptIdMSB: string; // deprecated
scriptIdLSB: string; // deprecated
scriptHash: string;
}

View File

@ -18,7 +18,7 @@ import config from 'config';
import { _logger } from '../config/logger';
import { JsExecutor, TbScript } from './jsExecutor';
import { performance } from 'perf_hooks';
import { isString, parseJsErrorDetails, toUUIDString, UUIDFromBuffer, UUIDToBits } from './utils';
import { isString, parseJsErrorDetails, toUUIDString, UUIDFromBuffer, UUIDToBits, isNotUUID } from './utils';
import { IQueue } from '../queue/queue.models';
import {
JsCompileRequest,
@ -176,7 +176,7 @@ export class JsInvokeMessageProcessor {
this.logger.debug('[%s] Sending success invoke response, scriptId: [%s]', requestId, scriptId);
this.sendResponse(requestId, responseTopic, headers, scriptId, undefined, invokeResponse);
} else {
let err = {
const err = {
name: 'Error',
message: 'script invocation result exceeds maximum allowed size of ' + maxResultSize + ' symbols'
}
@ -200,7 +200,7 @@ export class JsInvokeMessageProcessor {
},
(err: any) => {
let errorCode = COMPILATION_ERROR;
if (err && isString(err.name) && err.name.includes('script body not found')) {
if (err?.name === 'script body not found') {
errorCode = NOT_FOUND_ERROR;
}
const invokeResponse = JsInvokeMessageProcessor.createInvokeResponse("", false, errorCode, err);
@ -301,12 +301,26 @@ export class JsInvokeMessageProcessor {
}
private static createCompileResponse(scriptId: string, success: boolean, errorCode?: number, err?: any): JsCompileResponse {
return {
errorCode: errorCode,
success: success,
errorDetails: parseJsErrorDetails(err),
scriptHash: scriptId,
};
if (isNotUUID(scriptId)) {
return {
errorCode: errorCode,
success: success,
errorDetails: parseJsErrorDetails(err),
scriptIdMSB: "0",
scriptIdLSB: "0",
scriptHash: scriptId
};
} else { // this is for backward compatibility (to be able to work with tb-node of previous version) - todo: remove in the next release
let scriptIdBits = UUIDToBits(scriptId);
return {
errorCode: errorCode,
success: success,
errorDetails: parseJsErrorDetails(err),
scriptIdMSB: scriptIdBits[0],
scriptIdLSB: scriptIdBits[1],
scriptHash: ""
};
}
}
private static createInvokeResponse(result: string, success: boolean, errorCode?: number, err?: any): JsInvokeResponse {
@ -319,14 +333,26 @@ export class JsInvokeMessageProcessor {
}
private static createReleaseResponse(scriptId: string, success: boolean): JsReleaseResponse {
return {
success: success,
scriptHash: scriptId,
};
if (isNotUUID(scriptId)) {
return {
success: success,
scriptIdMSB: "0",
scriptIdLSB: "0",
scriptHash: scriptId,
};
} else { // todo: remove in the next release
let scriptIdBits = UUIDToBits(scriptId);
return {
success: success,
scriptIdMSB: scriptIdBits[0],
scriptIdLSB: scriptIdBits[1],
scriptHash: ""
}
}
}
private static getScriptId(request: TbMessage): string {
return request.scriptHash;
return request.scriptHash ? request.scriptHash : toUUIDString(request.scriptIdMSB, request.scriptIdLSB);
}
private incrementUseScriptId(scriptId: string) {

View File

@ -58,3 +58,7 @@ export function parseJsErrorDetails(err: any): string | undefined {
}
return details;
}
export function isNotUUID(candidate: string) {
return candidate.length != 36 || !candidate.includes('-');
}