Skip to content

Commit

Permalink
fix(runtime): fix db stream initialize (#1592)
Browse files Browse the repository at this point in the history
* fix(runtime): fix db stream initialize

* chore: add type hint for web ide

* lint
  • Loading branch information
0fatal authored Oct 17, 2023
1 parent fdbfc5c commit 40e25f1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
10 changes: 8 additions & 2 deletions runtimes/nodejs/src/handler/debug-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions runtimes/nodejs/src/handler/invoke-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
5 changes: 0 additions & 5 deletions runtimes/nodejs/src/storage-server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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'
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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DatabaseChangeStream } from '.'

export class ConfChangeStream {
static initialize() {
this.updateEnvironments()

DatabaseChangeStream.onStreamChange(
CONFIG_COLLECTION,
this.updateEnvironments,
Expand Down
26 changes: 20 additions & 6 deletions runtimes/nodejs/src/support/database-change-stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class WebsiteHostingChangeStream {
static websiteHosting = []

static initialize() {
this.onStreamChange()

DatabaseChangeStream.onStreamChange(
WEBSITE_HOSTING_COLLECTION,
this.onStreamChange.bind(this),
Expand Down
2 changes: 1 addition & 1 deletion runtimes/nodejs/src/support/function-engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions web/src/components/Editor/typesResolve/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ interface FunctionContext {
* WebSocket object
*/
socket?: WebSocket
[key: string]: any
}
interface IModule {
Expand All @@ -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
Expand Down

0 comments on commit 40e25f1

Please sign in to comment.