Skip to content

Commit

Permalink
feat: add custom dep path
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct committed Nov 19, 2023
1 parent 5844873 commit ac3c70e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
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)
return FunctionModule.customRequire(name)
}

return require(name)
}

Expand Down

0 comments on commit ac3c70e

Please sign in to comment.