Skip to content

Commit

Permalink
feat(runtime): support __websocket__ builtin function (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Feb 28, 2023
1 parent bb90765 commit 24d8731
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 243 deletions.
9 changes: 0 additions & 9 deletions runtimes/nodejs/.env.template

This file was deleted.

33 changes: 11 additions & 22 deletions runtimes/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,32 @@
```sh
cd runtimes/nodejs

# proxy app cluster traffic to local, replace `APPID` with your prepared appid
# connect the cluster if not connected
telepresence connect

export APPID=your-app-id

# proxy app cluster traffic to local, replace `APPID` with your prepared appid
telepresence intercept $APPID -n $APPID -p 8000:8000 -e $(pwd)/.env

# after intercept command, you can use following command to check if intercept active
telepresence list -n $APPID

# Start local service first, required nodejs version >= 18.0.0
npm install

npm run build

npm start
# intercept
telepresence intercept APPID -n APPID -p 8000:8000 -e $(pwd)/.env

# after intercept command, you can use following command to check if intercept active
telepresence list -n APPID
# if success, you would see like below
Your-APPID: intercepted
Intercept name : APPID-APPID
State : ACTIVE
Workload kind : Deployment
Destination : 127.0.0.1:8000
Service Port Identifier: 8000
Intercepting : all TCP requests
```

> Clean up
```bash
telepresence leave APPID-APPID
telepresence leave $APPID-$APPID
```

## Troubleshooting

- `telepresence helm install` failed for `arm64/Apple Chip` cluster:

```bash
telepresence helm install \
--set image.name=pentusha/telepresence-ubuntu-multiarch \
--set image.registry=docker.io \
--set image.tag=2.10.4
```
- `telepresence helm install` failed for `arm64 / Apple Chip` cluster, please upgrade your telepresence to `v2.11.1` or later.
39 changes: 0 additions & 39 deletions runtimes/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion runtimes/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"ejs": "^3.1.8",
"express": "^4.18.2",
"express-xml-bodyparser": "^0.3.0",
"fs-extra": "^9.1.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
"log4js": "^6.7.1",
Expand Down
32 changes: 9 additions & 23 deletions runtimes/nodejs/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
import { deepFreeze } from "./support/utils"

/**
* Constants collection
* Collection names
*/
export const Constants = {
/**
* collection name of cloud functions
*/
function_collection: '__functions__',
export const CLOUD_FUNCTION_COLLECTION = '__functions__'
export const POLICY_COLLECTION = '__policies__'
export const FUNCTION_LOG_COLLECTION = '__function_logs__'
export const CONFIG_COLLECTION = '__config__'

/**
* collection name of policy which used for authenticating `client-access-database` requests
*/
policy_collection: '__policies__',

/**
* collection name of cloud functions' log
*/
function_log_collection: "__function_logs__",

/**
* collection name of application configuration
*/
config_collection: '__config__'
}

deepFreeze(Constants)
export const WEBSOCKET_FUNCTION_NAME = '__websocket__'
export const INTERCEPTOR_FUNCTION_NAME = '__interceptor__'
export const DEFAULT_FUNCTION_NAME = '__default__'
export const INIT_FUNCTION_NAME = '__init__'
7 changes: 1 addition & 6 deletions runtimes/nodejs/src/handler/db-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2022-02-03 00:39:18
* @Description:
*/

import { Response } from 'express'
import { Proxy } from 'database-proxy'
import Config from '../config'
Expand Down
7 changes: 0 additions & 7 deletions runtimes/nodejs/src/handler/debug-func.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2022-02-03 00:59:03
* @Description:
*/

import { Response } from 'express'
import { FunctionContext } from '../support/function-engine'
import { logger } from '../support/logger'
Expand Down
15 changes: 3 additions & 12 deletions runtimes/nodejs/src/handler/invoke-func.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2022-02-03 00:59:07
* @Description:
*/

import { Response } from 'express'
import { FunctionContext } from '../support/function-engine'
import { logger } from '../support/logger'
import { CloudFunction } from '../support/function-engine'
import { IRequest } from '../support/types'
import { handleDebugFunction } from './debug-func'
import { parseToken } from '../support/token'

const DEFAULT_FUNCTION_NAME = '__default__'
const INTERCEPTOR_FUNCTION_NAME = '__interceptor__'
import { DEFAULT_FUNCTION_NAME, INTERCEPTOR_FUNCTION_NAME } from '../constants'

/**
* Handler of invoking cloud function
*/
export async function handleInvokeFunction(req: IRequest, res: Response) {
// intercept the request, skip websocket request
if (false === req.method.startsWith('WebSocket')) {
if (false === req.method.startsWith('WebSocket:')) {
const passed = await invokeInterceptor(req, res)
if (passed === false) return
}
Expand Down Expand Up @@ -112,7 +103,7 @@ async function invokeInterceptor(req: IRequest, res: Response) {
const func_name = INTERCEPTOR_FUNCTION_NAME

// load function data from db
let funcData = await CloudFunction.getFunctionByName(func_name)
const funcData = await CloudFunction.getFunctionByName(func_name)
// pass if no interceptor
if (!funcData) {
return true
Expand Down
15 changes: 3 additions & 12 deletions runtimes/nodejs/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fse = require('fs-extra')
import path = require('path')
import { Constants } from './constants'
import { execSync } from 'child_process'
import Config from './config'

import { logger } from './support/logger'
import { FUNCTION_LOG_COLLECTION } from './constants'

async function main() {
try {
Expand All @@ -28,13 +26,6 @@ main()
process.exit(2)
})


export function isCloudSdkPackageExists() {
const target = path.resolve(__dirname, '../../../node_modules/@')
const pkgJsonPath = path.join(target, 'package.json')
return fse.existsSync(pkgJsonPath)
}

/**
* Install packages
* @param packages
Expand Down Expand Up @@ -76,12 +67,12 @@ export async function ensureCollectionIndexes(): Promise<any> {
const { DatabaseAgent } = require('./db')
await DatabaseAgent.accessor.ready
const db = DatabaseAgent.db
await db.collection(Constants.function_log_collection).createIndexes([
await db.collection(FUNCTION_LOG_COLLECTION).createIndexes([
{
key: { created_at: 1 },
expireAfterSeconds: Config.FUNCTION_LOG_EXPIRED_TIME,
},
])

return true
}
}
2 changes: 0 additions & 2 deletions runtimes/nodejs/src/support/function-engine/engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// import fetch from 'node-fetch'
import { URL } from 'node:url'

import * as vm from 'vm'
import { nanosecond2ms } from '../utils'
import { FunctionConsole } from './console'
Expand Down
6 changes: 3 additions & 3 deletions runtimes/nodejs/src/support/function-engine/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './types'
import * as assert from 'assert'
import { DatabaseAgent } from '../../db'
import { Constants } from '../../constants'
import { CLOUD_FUNCTION_COLLECTION } from '../../constants'

/**
* CloudFunction Class
Expand Down Expand Up @@ -120,7 +120,7 @@ export class CloudFunction {
const db = DatabaseAgent.db

const doc = await db
.collection<ICloudFunctionData>(Constants.function_collection)
.collection<ICloudFunctionData>(CLOUD_FUNCTION_COLLECTION)
.findOne({ name: func_name })

return doc
Expand All @@ -135,7 +135,7 @@ export class CloudFunction {
const db = DatabaseAgent.db

const doc = await db
.collection<ICloudFunctionData>(Constants.function_collection)
.collection<ICloudFunctionData>(CLOUD_FUNCTION_COLLECTION)
.findOne({
// _id: new ObjectId(func_id)
id: func_id,
Expand Down
1 change: 0 additions & 1 deletion runtimes/nodejs/src/support/function-engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export interface ICloudFunctionData {
source: CloudFunctionSource
desc: string
tags: string[]
websocket: boolean
methods: string[]
createdAt: Date
updatedAt: Date
Expand Down
13 changes: 2 additions & 11 deletions runtimes/nodejs/src/support/function-log.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2022-02-03 00:58:33
* @Description:
*/

import { Constants } from '../constants'
import { FUNCTION_LOG_COLLECTION } from '../constants'
import { DatabaseAgent } from '../db'
import { FunctionConsole, FunctionContext } from './function-engine'

Expand All @@ -20,9 +13,7 @@ FunctionConsole.write = async (message: string, ctx: FunctionContext) => {
const db = DatabaseAgent.db
if (!db) return

const collection = db.collection<IFunctionLog>(
Constants.function_log_collection,
)
const collection = db.collection<IFunctionLog>(FUNCTION_LOG_COLLECTION)
const doc = {
request_id: ctx.requestId,
func: ctx.__function_name,
Expand Down
8 changes: 4 additions & 4 deletions runtimes/nodejs/src/support/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { Params, Policy } from 'database-proxy'
import { logger } from './logger'
import { CloudFunction } from './function-engine'
import { DatabaseAgent } from '../db'
import { Constants } from '../constants'
import { generateUUID } from './utils'
import { POLICY_COLLECTION } from '../constants'

export type InjectionGetter = (payload: any, params: Params) => Promise<any>

Expand Down Expand Up @@ -62,7 +62,7 @@ export class PolicyAgent {
public static async get(name: string) {
// get from cache
const cached = this._data.get(name)

// TODO: consider a better cache policy ?
if (cached && Date.now() - cached.cached_at < 1000 * 5) {
return cached
Expand Down Expand Up @@ -121,7 +121,7 @@ export class PolicyAgent {
public static async loadPolicies() {
const db = DatabaseAgent.db
const docs = await db
.collection(Constants.policy_collection)
.collection(POLICY_COLLECTION)
.find<PolicyDataStruct>({})
.toArray()

Expand All @@ -136,7 +136,7 @@ export class PolicyAgent {
public static async loadPolicy(name: string) {
const db = DatabaseAgent.db
const doc = await db
.collection(Constants.policy_collection)
.collection(POLICY_COLLECTION)
.findOne<PolicyDataStruct>({ name })

return doc
Expand Down
Loading

0 comments on commit 24d8731

Please sign in to comment.