Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): detach custom dependency, support node_module caching #1658

Merged
merged 15 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add custom dep path
  • Loading branch information
skyoct committed Nov 19, 2023
commit ac3c70e5d1a3680d807a3c43e4fcf3b51e0a7d20
8 changes: 6 additions & 2 deletions runtimes/nodejs/Dockerfile.init
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
FROM lafyun/runtime-node:latest
FROM node:18

CMD [ "sh", "/app/init.sh" ]
WORKDIR /app/dependencies

COPY ./init.sh /app/dependencies/init.sh

CMD [ "sh", "/app/dependencies/init.sh" ]
6 changes: 4 additions & 2 deletions runtimes/nodejs/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
set -e
# node ./dist/init.js

# npm init
echo "npm init -y"
npm init -y

# skip init if $DEPENDENCIES is empty
if [ -z "$DEPENDENCIES" ]; then
echo "No dependencies to install."
cp -r /app/* /tmp/app
exit 0
fi

echo "npm install $DEPENDENCIES $NPM_INSTALL_FLAGS"
npm install $DEPENDENCIES $NPM_INSTALL_FLAGS
cp -r /app/* /tmp/app
4 changes: 4 additions & 0 deletions runtimes/nodejs/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ export default class Config {
static get DISABLE_MODULE_CACHE(): boolean {
return process.env.DISABLE_MODULE_CACHE === 'true'
}

static get CUSTOM_DEPENDENCY_BASE_PATH(): string {
return process.env.CUSTOM_DEPENDENCY_BASE_PATH || '/data/custom_dependency'
}
}
21 changes: 18 additions & 3 deletions runtimes/nodejs/src/handler/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { logger } from '../support/logger'
import { IRequest } from '../support/types'
import { parseToken } from '../support/token'
import { FunctionCache } from '../support/engine'
import Config from '../config'
import * as fs from 'fs'

const nodeModulesRoot = path.resolve(__dirname, '../../node_modules')

/**
* Gets declaration files of a dependency package
*/
export async function handlePackageTypings(req: IRequest, res: Response) {
const requestId = req['requestId']

// verify the debug token
const token = req.get('x-laf-develop-token')
Expand Down Expand Up @@ -77,9 +78,23 @@ export async function handlePackageTypings(req: IRequest, res: Response) {
})
}

// get custom dependency types
const customDependencyPath = `${Config.CUSTOM_DEPENDENCY_BASE_PATH}/node_modules/`
if (fs.existsSync(`${customDependencyPath}/${packageName}`)) {
console.log('custom dependency path', customDependencyPath)
getThreePartyPackageTypings(req, res, customDependencyPath, packageName)
} else {
getThreePartyPackageTypings(req, res, nodeModulesRoot, packageName)
}
}



async function getThreePartyPackageTypings(req: IRequest, res: Response, basePath: string, packageName: string) {
const requestId = req['requestId']
try {
// Gets other three-party package types
const pkd = new PackageDeclaration(packageName, nodeModulesRoot)
const pkd = new PackageDeclaration(packageName, basePath)
await pkd.load()
return res.send({
code: 0,
Expand All @@ -92,4 +107,4 @@ export async function handlePackageTypings(req: IRequest, res: Response) {
error: error.toString(),
})
}
}
}
18 changes: 18 additions & 0 deletions runtimes/nodejs/src/support/engine/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { FunctionCache, FunctionModuleGlobalContext } from '.'
import Config from '../../config'
import { Console } from '.'
import * as vm from 'vm'
import * as fs from 'fs'



export class FunctionModule {
protected static cache: Map<string, any> = new Map()

private static customRequire: any = null;

static get(functionName: string): any {
const moduleName = `@/${functionName}`
return this.require(moduleName, [])
Expand Down Expand Up @@ -40,6 +45,19 @@ export class FunctionModule {
}
return mod
}

// check custom dependency exists
const dependencyPath = `${Config.CUSTOM_DEPENDENCY_BASE_PATH}/node_modules/`
if (fs.existsSync(`${dependencyPath}/${name}`)) {
if (!FunctionModule.customRequire) {
FunctionModule.customRequire = require('module').createRequire(
dependencyPath,
)
}
console.log('custom dependency path', dependencyPath)
skyoct marked this conversation as resolved.
Show resolved Hide resolved
return FunctionModule.customRequire(name)
}

return require(name)
}

Expand Down
Loading