From 40e25f13163f1a0d1274caa8ae4f091e15710fba Mon Sep 17 00:00:00 2001 From: 0fatal <72899968+0fatal@users.noreply.github.com> Date: Tue, 17 Oct 2023 18:51:30 +0800 Subject: [PATCH] fix(runtime): fix db stream initialize (#1592) * fix(runtime): fix db stream initialize * chore: add type hint for web ide * lint --- runtimes/nodejs/src/handler/debug-func.ts | 10 +++++-- runtimes/nodejs/src/handler/invoke-func.ts | 3 --- runtimes/nodejs/src/index.ts | 2 -- runtimes/nodejs/src/storage-server.ts | 5 ---- .../conf-change-stream.ts | 2 ++ .../support/database-change-stream/index.ts | 26 ++++++++++++++----- .../website-hosting-change-stream.ts | 2 ++ .../src/support/function-engine/types.ts | 2 +- .../components/Editor/typesResolve/globals.ts | 10 +++++++ 9 files changed, 43 insertions(+), 19 deletions(-) diff --git a/runtimes/nodejs/src/handler/debug-func.ts b/runtimes/nodejs/src/handler/debug-func.ts index aed5b92f17..558d05a3f1 100644 --- a/runtimes/nodejs/src/handler/debug-func.ts +++ b/runtimes/nodejs/src/handler/debug-func.ts @@ -15,7 +15,9 @@ export async function handleDebugFunction(ctx: FunctionContext) { } const auth = parseToken(token) || null if (auth?.type !== 'develop') { - return ctx.response.status(403).send('permission denied: invalid develop token') + return ctx.response + .status(403) + .send('permission denied: invalid develop token') } // get func_data from header @@ -37,7 +39,11 @@ export async function handleDebugFunction(ctx: FunctionContext) { const func_name = ctx.request.params?.name if (!func_data) { - return ctx.response.send({ code: 1, error: 'function data not found', requestId }) + return ctx.response.send({ + code: 1, + error: 'function data not found', + requestId, + }) } const func = new CloudFunction(func_data) diff --git a/runtimes/nodejs/src/handler/invoke-func.ts b/runtimes/nodejs/src/handler/invoke-func.ts index 71ef19d124..890b60f1bc 100644 --- a/runtimes/nodejs/src/handler/invoke-func.ts +++ b/runtimes/nodejs/src/handler/invoke-func.ts @@ -11,7 +11,6 @@ import { DEFAULT_FUNCTION_NAME, INTERCEPTOR_FUNCTION_NAME } from '../constants' * Handler of invoking cloud function */ export async function handleInvokeFunction(req: IRequest, res: Response) { - const ctx: FunctionContext = { requestId: req.requestId, query: req.query, @@ -23,7 +22,6 @@ export async function handleInvokeFunction(req: IRequest, res: Response) { user: req.user, request: req, response: res, - varMap: new Map(), } // intercept the request, skip websocket request @@ -102,7 +100,6 @@ export async function handleInvokeFunction(req: IRequest, res: Response) { } async function invokeInterceptor(ctx: FunctionContext) { - const func_name = INTERCEPTOR_FUNCTION_NAME const requestId = ctx.requestId diff --git a/runtimes/nodejs/src/index.ts b/runtimes/nodejs/src/index.ts index 5b02ec399c..00ee8c6978 100644 --- a/runtimes/nodejs/src/index.ts +++ b/runtimes/nodejs/src/index.ts @@ -21,13 +21,11 @@ import xmlparser from 'express-xml-bodyparser' import './support/cloud-sdk' import storageServer from './storage-server' import { DatabaseChangeStream } from './support/database-change-stream' -import { FunctionCache } from './support/function-engine/cache' const app = express() DatabaseAgent.accessor.ready.then(() => { DatabaseChangeStream.initialize() - FunctionCache.initialize() }) if (process.env.NODE_ENV === 'development') { diff --git a/runtimes/nodejs/src/storage-server.ts b/runtimes/nodejs/src/storage-server.ts index 2c4f7d5199..a4446ca590 100644 --- a/runtimes/nodejs/src/storage-server.ts +++ b/runtimes/nodejs/src/storage-server.ts @@ -1,7 +1,6 @@ import express from 'express' import Config from './config' import { logger } from './support/logger' -import { DatabaseAgent } from './db' import './support/cloud-sdk' import { WebsiteHostingChangeStream } from './support/database-change-stream/website-hosting-change-stream' import proxy from 'express-http-proxy' @@ -9,10 +8,6 @@ import axios from 'axios' const app = express() -DatabaseAgent.accessor.ready.then(() => { - WebsiteHostingChangeStream.initialize() -}) - const tryPath = (bucket: string, path: string) => { const testPaths = path.endsWith('/') ? [path + 'index.html', '/index.html'] diff --git a/runtimes/nodejs/src/support/database-change-stream/conf-change-stream.ts b/runtimes/nodejs/src/support/database-change-stream/conf-change-stream.ts index cf7b2ab7ff..c79adcfcc6 100644 --- a/runtimes/nodejs/src/support/database-change-stream/conf-change-stream.ts +++ b/runtimes/nodejs/src/support/database-change-stream/conf-change-stream.ts @@ -4,6 +4,8 @@ import { DatabaseChangeStream } from '.' export class ConfChangeStream { static initialize() { + this.updateEnvironments() + DatabaseChangeStream.onStreamChange( CONFIG_COLLECTION, this.updateEnvironments, diff --git a/runtimes/nodejs/src/support/database-change-stream/index.ts b/runtimes/nodejs/src/support/database-change-stream/index.ts index 54a48d4ba8..8b1fd12e31 100644 --- a/runtimes/nodejs/src/support/database-change-stream/index.ts +++ b/runtimes/nodejs/src/support/database-change-stream/index.ts @@ -7,12 +7,25 @@ import { CONFIG_COLLECTION, WEBSITE_HOSTING_COLLECTION, } from '../../constants' +import { ConfChangeStream } from './conf-change-stream' +import { WebsiteHostingChangeStream } from './website-hosting-change-stream' +import { FunctionCache } from '../function-engine/cache' const collectionsToWatch = [ - CONFIG_COLLECTION, - CLOUD_FUNCTION_COLLECTION, - WEBSITE_HOSTING_COLLECTION, + { + name: CONFIG_COLLECTION, + handler: () => ConfChangeStream, + }, + { + name: WEBSITE_HOSTING_COLLECTION, + handler: () => WebsiteHostingChangeStream, + }, + { + name: CLOUD_FUNCTION_COLLECTION, + handler: () => FunctionCache, + }, ] as const + export class DatabaseChangeStream extends EventEmitter { private static instance: DatabaseChangeStream @@ -50,13 +63,14 @@ export class DatabaseChangeStream extends EventEmitter { static initialize() { const instance = DatabaseChangeStream.getInstance() - collectionsToWatch.forEach((collectionName) => { - instance.initializeForCollection(collectionName) + collectionsToWatch.forEach((v) => { + instance.initializeForCollection(v.name) + v.handler().initialize() }) } static onStreamChange( - collectionName: (typeof collectionsToWatch)[number], + collectionName: (typeof collectionsToWatch)[number]['name'], listener: (...args: any[]) => void, ) { const instance = DatabaseChangeStream.getInstance() diff --git a/runtimes/nodejs/src/support/database-change-stream/website-hosting-change-stream.ts b/runtimes/nodejs/src/support/database-change-stream/website-hosting-change-stream.ts index 45dddc14ee..a315697bbf 100644 --- a/runtimes/nodejs/src/support/database-change-stream/website-hosting-change-stream.ts +++ b/runtimes/nodejs/src/support/database-change-stream/website-hosting-change-stream.ts @@ -6,6 +6,8 @@ export class WebsiteHostingChangeStream { static websiteHosting = [] static initialize() { + this.onStreamChange() + DatabaseChangeStream.onStreamChange( WEBSITE_HOSTING_COLLECTION, this.onStreamChange.bind(this), diff --git a/runtimes/nodejs/src/support/function-engine/types.ts b/runtimes/nodejs/src/support/function-engine/types.ts index 83ac833de4..6605fda59e 100644 --- a/runtimes/nodejs/src/support/function-engine/types.ts +++ b/runtimes/nodejs/src/support/function-engine/types.ts @@ -52,7 +52,7 @@ export interface FunctionContext { method?: string socket?: WebSocket request?: Request - response?: Response + response?: Response __function_name?: string __is_required?: boolean [key: string]: any diff --git a/web/src/components/Editor/typesResolve/globals.ts b/web/src/components/Editor/typesResolve/globals.ts index 1abc49ea8b..c96032186a 100644 --- a/web/src/components/Editor/typesResolve/globals.ts +++ b/web/src/components/Editor/typesResolve/globals.ts @@ -97,6 +97,8 @@ interface FunctionContext { * WebSocket object */ socket?: WebSocket + + [key: string]: any } interface IModule { @@ -110,10 +112,18 @@ interface IExports { main: (ctx: FunctionContext) => any } +interface IProcess { + /** + * Environment + */ + env: any +} + declare const module: IModule declare const exports: IExports declare const console: FunctionConsole declare const global: typeof globalThis +declare const process: IProcess /** * The main function, entry of the cloud function