forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: enable plugin mechanism for async func #70
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
56ff7ad
✨ feat: enable asycn function plugin
YADROOKIE 10c240f
♻️ refactor enable aysnc function plugin
YADROOKIE 7034730
Merge branch 'OpenFunction:master' into feature/plugin-async
YADROOKIE be20c3f
Merge branch 'feature/plugin-async' of https://github.com/YADROOKIE/f…
YADROOKIE c2146ee
🎨 formate code
YADROOKIE 7c1d9e8
🎨 improve aysnc function plugin code
YADROOKIE c844496
🎨 rename test file gi
YADROOKIE af1ac61
🎨 improve & formate code
YADROOKIE 4bb5ad6
🎨 format code
YADROOKIE 0d5ceb3
🎨 extract common test data into a separate file for reuse & use Reco…
YADROOKIE 184c2a9
🎨 delete test data
YADROOKIE eae45d8
🎨 test data in common file
YADROOKIE a95dad2
🎨 remove annotation
YADROOKIE b6d1c50
Merge branch 'OpenFunction:master' into feature/plugin-async
YADROOKIE 1c9b4a8
Merge branch 'OpenFunction:master' into feature/plugin-async
YADROOKIE 48946da
Merge branch 'OpenFunction:master' into feature/plugin-async
YADROOKIE a2f2db7
🎨 improve the codegit
YADROOKIE fa7c1f5
🎨 imporove plugin loader
YADROOKIE 37a91af
process conflict
YADROOKIE c1074ad
Merge branch 'OpenFunction:master' into feature/plugin-async
YADROOKIE 75d2422
resolve conflict
YADROOKIE 8403803
add api docs
YADROOKIE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import * as minimist from 'minimist'; | |
|
||
import {SignatureType, isValidSignatureType} from './types'; | ||
import {OpenFunctionContext} from './openfunction/function_context'; | ||
import {PluginContext} from './plugin_context'; | ||
|
||
const debug = Debug('common:options'); | ||
|
||
|
@@ -53,6 +54,10 @@ export interface FrameworkOptions { | |
* The context to use for the function when serving. | ||
*/ | ||
context?: OpenFunctionContext; | ||
/** | ||
* The context to use for the function when serving. | ||
*/ | ||
plugin?: PluginContext; | ||
/** | ||
* Whether or not the --help CLI flag was provided. | ||
*/ | ||
|
@@ -134,7 +139,23 @@ const FunctionContextOption = new ConfigurableOption( | |
} | ||
} | ||
); | ||
const PluginContextOption = new ConfigurableOption( | ||
'plugin', | ||
'PLUGIN_CONTEXT', | ||
undefined, | ||
x => { | ||
// Try to parse plugin string | ||
debug('ℹ️ Plugin loaded: %s', x); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this line? |
||
try { | ||
const context = JSON.parse(x); | ||
return context as PluginContext; | ||
} catch (e) { | ||
debug('Failed to parse plugin: %s', e); | ||
return undefined; | ||
} | ||
} | ||
); | ||
export const helpText = `Example usage: | ||
functions-framework --target=helloWorld --port=8080 | ||
Documentation: | ||
|
@@ -158,6 +179,7 @@ export const parseOptions = ( | |
SignatureOption.cliOption, | ||
SourceLocationOption.cliOption, | ||
FunctionContextOption.cliOption, | ||
PluginContextOption.cliOption, | ||
], | ||
}); | ||
return { | ||
|
@@ -166,6 +188,7 @@ export const parseOptions = ( | |
sourceLocation: SourceLocationOption.parse(argv, envVars), | ||
signatureType: SignatureOption.parse(argv, envVars), | ||
context: FunctionContextOption.parse(argv, envVars), | ||
plugin: PluginContextOption.parse(argv, envVars), | ||
printHelp: cliArgs[2] === '-h' || cliArgs[2] === '--help', | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import {PluginContext} from './plugin_context'; | ||
|
||
export async function loadPlugins( | ||
codeLocation: string | ||
): Promise<Map<string, Function>> { | ||
const funcMap: Map<string, Function> = new Map(); | ||
const param = path.resolve(`${codeLocation}/plugins`); | ||
const plugin_files: Array<string> = []; | ||
const files = fs.readdirSync(param); | ||
files.forEach(f => { | ||
if (f.endsWith('.js')) { | ||
plugin_files.push(path.join(param, f)); | ||
} | ||
}); | ||
console.log(plugin_files); | ||
plugin_files.forEach(f => { | ||
const pluginModule = require(f); | ||
Object.keys(pluginModule).forEach(key => { | ||
funcMap.set(key, pluginModule[key]); | ||
}); | ||
}); | ||
return funcMap; | ||
} | ||
|
||
export async function getPlugins( | ||
codeLocation: string, | ||
pluginContext: PluginContext | ||
): Promise<PluginContext> { | ||
const funcMapResult: Map<string, Function> = new Map(); | ||
const funcMap = await loadPlugins(codeLocation); | ||
pluginContext.pluginMap = funcMap; | ||
pluginContext.prePluginFuncs = []; | ||
pluginContext.postPluginFuncs = []; | ||
pluginContext.prePlugins.forEach(p => { | ||
const func = funcMap.get(p); | ||
if (func) { | ||
funcMapResult.set(p, func); | ||
pluginContext.prePluginFuncs!.push(func); | ||
} else { | ||
console.error('----------------error----------------'); | ||
console.error( | ||
`pre-plugin-function[${p}] is not found \nDid you specify the correct target plugin-function to execute?` | ||
); | ||
console.error('-------------------------------------'); | ||
} | ||
}); | ||
pluginContext.postPlugins.forEach(p => { | ||
const func = funcMap.get(p); | ||
if (func) { | ||
funcMapResult.set(p, func); | ||
pluginContext.postPluginFuncs!.push(func); | ||
} else { | ||
console.error('----------------error----------------'); | ||
console.error( | ||
`post-plugin-function[${p}] is not found \nDid you specify the correct target plugin-function to execute?` | ||
); | ||
console.error('-------------------------------------'); | ||
} | ||
}); | ||
|
||
return pluginContext; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {OpenFunctionRuntime} from './functions'; | ||
/** | ||
* The OpenFunction's plugin context. | ||
* @public | ||
*/ | ||
export interface PluginContext { | ||
/** | ||
* The name of the pre plugins. | ||
*/ | ||
prePlugins: Array<string>; | ||
/** | ||
* The name of the pre plugins. | ||
*/ | ||
postPlugins: Array<string>; | ||
/** | ||
* The func of the pre plugins. | ||
*/ | ||
prePluginFuncs?: Array<Function>; | ||
/** | ||
* The func of the pre plugins. | ||
*/ | ||
postPluginFuncs?: Array<Function>; | ||
/** | ||
* The refect between name func. | ||
*/ | ||
pluginMap?: Map<string, Function>; | ||
} | ||
|
||
/** | ||
* The OpenFunction's plugin context runtime. | ||
* @public | ||
*/ | ||
export interface PluginContextRuntime { | ||
/** | ||
* The refect between name func. | ||
*/ | ||
pluginContext: PluginContext; | ||
/** | ||
* OpenFunctionRuntime. | ||
*/ | ||
context?: OpenFunctionRuntime; | ||
/** | ||
* data. | ||
*/ | ||
data?: object; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this line?