Skip to content

Commit

Permalink
fix: 修复 trigger 函数日志 _id 类型问题;导出 编译函数;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 30, 2021
1 parent c43ecee commit e0fab5b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 2 additions & 7 deletions packages/cloud-function-engine/src/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FunctionEngine } from "."
import { CloudFunctionStruct, FunctionContext, FunctionResult, RequireFuncType } from "./types"
import * as ts from 'typescript'
import * as assert from "assert"
import { compileTs2js } from "./utils"

/**
* 云函数
Expand Down Expand Up @@ -101,12 +101,7 @@ export class CloudFunction {
* @param {string} source ts 代码字符串
*/
compile2js() {
const jscode = ts.transpile(this.code, {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2017,
removeComments: true,
})

const jscode = compileTs2js(this.code)
this._data.compiledCode = jscode
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud-function-engine/src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Trigger {
const tri = new Trigger()
tri.type = data.type
tri.desc = data.desc
tri.id = data._id
tri.id = data._id.toString()
tri.func_id = data.func_id
tri.name = data.name
tri.last_exec_time = data.last_exec_time ?? 0
Expand Down
15 changes: 15 additions & 0 deletions packages/cloud-function-engine/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ts from 'typescript'

// 获取当前时间戳,毫秒
export function now(): number {
Expand All @@ -12,4 +13,18 @@ export function nanosecond2ms(nanoseconds: bigint): number {
// 再用 float 除1000,保留微秒小数点
const ret = parseFloat(_t.toString()) / 1000
return ret
}

/**
* 编译 Typescript 代码到 js
* @param source Typescript 源代码
*/
export function compileTs2js(source: string) {
const jscode = ts.transpile(source, {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2017,
removeComments: true,
})

return jscode
}

0 comments on commit e0fab5b

Please sign in to comment.