Skip to content

Commit

Permalink
fix(runtime): append null check to object check (labring#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
bestlyg authored Nov 15, 2023
1 parent 849a221 commit f2a3a40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions runtimes/nodejs/src/handler/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FunctionDebugExecutor,
} from '../support/engine'
import pako from 'pako'
import { base64ToUint8Array, uint8ArrayToBase64 } from '../support/utils'
import { base64ToUint8Array, isObject, uint8ArrayToBase64 } from '../support/utils'

export async function handleInvokeFunction(req: IRequest, res: Response) {
const name = req.params?.name
Expand Down Expand Up @@ -90,7 +90,7 @@ async function invokeFunction(

// reject request if interceptor return false
if (
typeof result.data === 'object' &&
isObject(result.data) &&
result.data.__type__ === '__interceptor__' &&
result.data.__res__ == false
) {
Expand Down Expand Up @@ -200,7 +200,7 @@ async function invokeDebug(

// reject request if interceptor return false
if (
typeof result.data === 'object' &&
isObject(result.data) &&
result.data.__type__ === '__interceptor__' &&
result.data.__res__ == false
) {
Expand Down
3 changes: 2 additions & 1 deletion runtimes/nodejs/src/support/engine/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as util from 'util'
import chalk from 'chalk'
import { padStart } from 'lodash'
import Config from '../../config'
import { isObject } from '../utils'

enum LogLevel {
DEBUG = 'DEBUG',
Expand Down Expand Up @@ -31,7 +32,7 @@ export class Console {
let content = params
.map((param) => {
if (typeof param === 'string') return this._colorize(level, param)
if (typeof param === 'object') {
if (isObject(param)) {
return this._colorize(
level,
util.inspect(param, { depth: Config.LOG_DEPTH, colors: true }),
Expand Down
13 changes: 11 additions & 2 deletions runtimes/nodejs/src/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function deepFreeze(object: Object) {
for (const name of propNames) {
const value = object[name]

if (value && typeof value === 'object') {
if (isObject(value)) {
deepFreeze(value)
}
}
Expand Down Expand Up @@ -114,4 +114,13 @@ export function uint8ArrayToBase64(buffer: Uint8Array) {
export function base64ToUint8Array(base64: string) {
const buffer = Buffer.from(base64, 'base64')
return new Uint8Array(buffer)
}
}

/**
* is object.
* @param obj
* @returns
*/
export function isObject(obj: unknown): obj is Object {
return obj !== null && typeof obj === 'object'
}

0 comments on commit f2a3a40

Please sign in to comment.