MCPcopy Create free account
hub / github.com/dagger/dagger / invoke

Function invoke

sdk/typescript/src/module/entrypoint/invoke.ts:39–109  ·  view source on GitHub ↗
(
  executor: Executor,
  module: DaggerModule,
  ctx: InvokeCtx,
)

Source from the content-addressed store, hash-verified

37 * @param fnArgs The arguments of the function to call.
38 */
39export async function invoke(
40 executor: Executor,
41 module: DaggerModule,
42 ctx: InvokeCtx,
43) {
44 const object = loadInvokedObject(module, ctx.parentName)
45 if (!object) {
46 throw new Error(`could not find object ${ctx.parentName}`)
47 }
48
49 const method = loadInvokedMethod(object, ctx)
50 if (!method) {
51 throw new Error(`could not find method ${ctx.fnName}`)
52 }
53
54 const args = await loadArgs(executor, method, ctx)
55 const parentState = await loadParentState(executor, object, ctx)
56
57 // Disabling linter because the result could be anything.
58 // eslint-disable-next-line @typescript-eslint/no-explicit-any
59 let result: any
60
61 try {
62 result = await executor.getResult(
63 object.name,
64 method.name,
65 parentState,
66 args,
67 )
68 } catch (e) {
69 // If the function isn't found because it's
70 // not exported, we try to get the result from the registry.
71 if (e instanceof FunctionNotFound) {
72 result = await registry.getResult(
73 object.name,
74 method.name,
75 parentState,
76 args,
77 )
78 } else {
79 throw e
80 }
81 }
82
83 if (result) {
84 let returnType: DaggerObjectBase | DaggerEnumBase
85
86 // Handle alias serialization by getting the return type to load
87 // if the function called isn't a constructor.
88 if (!isConstructor(method)) {
89 // Guard to catch if a user returned a float number but the function's return
90 // type is set to `number` (integer).
91 if (
92 method.returnType!.kind === TypeDefKind.IntegerKind &&
93 isFloat(result)
94 ) {
95 throw new Error(
96 `cannot return float '${result}' if return type is 'number' (integer), please use 'float' as return type instead`,

Callers 2

entrypointFunction · 0.70
invoke.spec.tsFile · 0.50

Calls 9

loadInvokedObjectFunction · 0.85
loadInvokedMethodFunction · 0.85
loadArgsFunction · 0.85
loadParentStateFunction · 0.85
isConstructorFunction · 0.85
isFloatFunction · 0.85
loadObjectReturnTypeFunction · 0.85
loadResultFunction · 0.70
getResultMethod · 0.45

Tested by

no test coverage detected