@crossplane-org/function-sdk-typescript 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +683 -0
- package/dist/example-function.d.ts +11 -0
- package/dist/example-function.d.ts.map +1 -0
- package/dist/example-function.js +93 -0
- package/dist/example-function.js.map +1 -0
- package/dist/function/function.d.ts +115 -0
- package/dist/function/function.d.ts.map +1 -0
- package/dist/function/function.js +111 -0
- package/dist/function/function.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +69 -0
- package/dist/main.js.map +1 -0
- package/dist/proto/google/protobuf/duration.d.ts +107 -0
- package/dist/proto/google/protobuf/duration.d.ts.map +1 -0
- package/dist/proto/google/protobuf/duration.js +90 -0
- package/dist/proto/google/protobuf/duration.js.map +1 -0
- package/dist/proto/google/protobuf/struct.d.ts +115 -0
- package/dist/proto/google/protobuf/struct.d.ts.map +1 -0
- package/dist/proto/google/protobuf/struct.js +452 -0
- package/dist/proto/google/protobuf/struct.js.map +1 -0
- package/dist/proto/run_function.d.ts +494 -0
- package/dist/proto/run_function.d.ts.map +1 -0
- package/dist/proto/run_function.js +2230 -0
- package/dist/proto/run_function.js.map +1 -0
- package/dist/request/request.d.ts +198 -0
- package/dist/request/request.d.ts.map +1 -0
- package/dist/request/request.js +219 -0
- package/dist/request/request.js.map +1 -0
- package/dist/resource/resource.d.ts +101 -0
- package/dist/resource/resource.d.ts.map +1 -0
- package/dist/resource/resource.js +98 -0
- package/dist/resource/resource.js.map +1 -0
- package/dist/response/response.d.ts +273 -0
- package/dist/response/response.d.ts.map +1 -0
- package/dist/response/response.js +339 -0
- package/dist/response/response.js.map +1 -0
- package/dist/runtime/runtime.d.ts +121 -0
- package/dist/runtime/runtime.d.ts.map +1 -0
- package/dist/runtime/runtime.js +124 -0
- package/dist/runtime/runtime.js.map +1 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +17 -0
- package/dist/vitest.config.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Example function implementation demonstrating how to use the SDK
|
|
2
|
+
import { Resource, RunFunctionRequest, RunFunctionResponse, } from "./proto/run_function.js";
|
|
3
|
+
import { Pod } from "kubernetes-models/v1";
|
|
4
|
+
import { fatal, normal, setDesiredComposedResources, to, } from "./response/response.js";
|
|
5
|
+
import { getDesiredComposedResources, getDesiredCompositeResource, getObservedCompositeResource, } from "./request/request.js";
|
|
6
|
+
/**
|
|
7
|
+
* ExampleFunction is a sample implementation showing how to use the SDK
|
|
8
|
+
* This creates a Deployment and Pod as example resources
|
|
9
|
+
*/
|
|
10
|
+
export class ExampleFunction {
|
|
11
|
+
async RunFunction(req, logger) {
|
|
12
|
+
const startTime = Date.now();
|
|
13
|
+
// Set up a minimal response from the request
|
|
14
|
+
let rsp = to(req);
|
|
15
|
+
try {
|
|
16
|
+
// Get our Observed Composite
|
|
17
|
+
const oxr = getObservedCompositeResource(req);
|
|
18
|
+
logger?.debug({ oxr }, "Observed composite resource");
|
|
19
|
+
// Get our Desired Composite
|
|
20
|
+
const dxr = getDesiredCompositeResource(req);
|
|
21
|
+
logger?.debug({ dxr }, "Desired composite resource");
|
|
22
|
+
// List the Desired Composed resources
|
|
23
|
+
let dcds = getDesiredComposedResources(req);
|
|
24
|
+
// Create resource from a JSON object
|
|
25
|
+
dcds["deployment"] = Resource.fromJSON({
|
|
26
|
+
resource: {
|
|
27
|
+
apiVersion: "apps/v1",
|
|
28
|
+
kind: "Deployment",
|
|
29
|
+
metadata: {
|
|
30
|
+
name: "my-deployment",
|
|
31
|
+
namespace: "foo",
|
|
32
|
+
},
|
|
33
|
+
spec: {
|
|
34
|
+
replicas: 3,
|
|
35
|
+
selector: {
|
|
36
|
+
matchLabels: {
|
|
37
|
+
app: "my-app",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
template: {
|
|
41
|
+
metadata: {
|
|
42
|
+
labels: {
|
|
43
|
+
app: "my-app",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
spec: {
|
|
47
|
+
containers: [
|
|
48
|
+
{
|
|
49
|
+
name: "my-container",
|
|
50
|
+
image: "my-image:latest",
|
|
51
|
+
ports: [
|
|
52
|
+
{
|
|
53
|
+
containerPort: 80,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
// Create a resource from a Model at https://github.com/tommy351/kubernetes-models-ts
|
|
64
|
+
const pod = new Pod({
|
|
65
|
+
metadata: {
|
|
66
|
+
name: "pod",
|
|
67
|
+
namespace: "default",
|
|
68
|
+
},
|
|
69
|
+
spec: {
|
|
70
|
+
containers: [],
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
pod.validate();
|
|
74
|
+
dcds["pod"] = Resource.fromJSON({ resource: pod.toJSON() });
|
|
75
|
+
// Merge dcds with existing resources using the response helper
|
|
76
|
+
rsp = setDesiredComposedResources(rsp, dcds);
|
|
77
|
+
const duration = Date.now() - startTime;
|
|
78
|
+
logger?.info({ duration: `${duration}ms` }, "Function completed successfully");
|
|
79
|
+
normal(rsp, "processing complete");
|
|
80
|
+
return rsp;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const duration = Date.now() - startTime;
|
|
84
|
+
logger?.error({
|
|
85
|
+
error: error instanceof Error ? error.message : String(error),
|
|
86
|
+
duration: `${duration}ms`,
|
|
87
|
+
}, "Function invocation failed");
|
|
88
|
+
fatal(rsp, error instanceof Error ? error.message : String(error));
|
|
89
|
+
return rsp;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=example-function.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example-function.js","sourceRoot":"","sources":["../src/example-function.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,OAAO,EACH,QAAQ,EACR,kBAAkB,EAClB,mBAAmB,GACtB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EACH,KAAK,EACL,MAAM,EACN,2BAA2B,EAC3B,EAAE,GACL,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACH,2BAA2B,EAC3B,2BAA2B,EAC3B,4BAA4B,GAC/B,MAAM,sBAAsB,CAAC;AAG9B;;;GAGG;AACH,MAAM,OAAO,eAAe;IACxB,KAAK,CAAC,WAAW,CACb,GAAuB,EACvB,MAAe;QAEf,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE7B,6CAA6C;QAC7C,IAAI,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;QAElB,IAAI,CAAC;YACD,6BAA6B;YAC7B,MAAM,GAAG,GAAG,4BAA4B,CAAC,GAAG,CAAC,CAAC;YAC9C,MAAM,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE,6BAA6B,CAAC,CAAC;YAEtD,4BAA4B;YAC5B,MAAM,GAAG,GAAG,2BAA2B,CAAC,GAAG,CAAC,CAAC;YAC7C,MAAM,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE,4BAA4B,CAAC,CAAC;YAErD,sCAAsC;YACtC,IAAI,IAAI,GAAG,2BAA2B,CAAC,GAAG,CAAC,CAAC;YAE5C,qCAAqC;YACrC,IAAI,CAAC,YAAY,CAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC;gBACnC,QAAQ,EAAE;oBACN,UAAU,EAAE,SAAS;oBACrB,IAAI,EAAE,YAAY;oBAClB,QAAQ,EAAE;wBACN,IAAI,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IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;YACxC,MAAM,EAAE,KAAK,CAAC;gBACV,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;gBAC7D,QAAQ,EAAE,GAAG,QAAQ,IAAI;aAC5B,EAAE,4BAA4B,CAAC,CAAC;YAEjC,KAAK,CAAC,GAAG,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YACnE,OAAO,GAAG,CAAC;QACf,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core function interfaces and classes for building Crossplane functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides the fundamental building blocks for creating Crossplane
|
|
5
|
+
* composition functions in TypeScript. It includes the FunctionHandler interface
|
|
6
|
+
* that users implement, the FunctionRunner that wraps handlers, and utilities
|
|
7
|
+
* for creating gRPC servers.
|
|
8
|
+
*/
|
|
9
|
+
import * as grpc from "@grpc/grpc-js";
|
|
10
|
+
import { RunFunctionRequest, RunFunctionResponse } from "../proto/run_function.js";
|
|
11
|
+
import type { Logger } from "pino";
|
|
12
|
+
/**
|
|
13
|
+
* FunctionHandler is the interface that users must implement to create a function.
|
|
14
|
+
*
|
|
15
|
+
* Implement this interface to define the logic of your composition function.
|
|
16
|
+
* The RunFunction method will be called by Crossplane for each invocation,
|
|
17
|
+
* receiving the current state and returning the desired state.
|
|
18
|
+
*/
|
|
19
|
+
export interface FunctionHandler {
|
|
20
|
+
/**
|
|
21
|
+
* RunFunction is called for each function invocation.
|
|
22
|
+
*
|
|
23
|
+
* This method receives the observed state of the composite resource (XR) and
|
|
24
|
+
* any composed resources, along with the desired state accumulated by previous
|
|
25
|
+
* functions in the pipeline. It should return the desired state after applying
|
|
26
|
+
* this function's logic.
|
|
27
|
+
*
|
|
28
|
+
* @param req - The RunFunctionRequest containing:
|
|
29
|
+
* - observed: Current state of resources in the cluster
|
|
30
|
+
* - desired: Desired state from previous functions
|
|
31
|
+
* - input: Function-specific configuration
|
|
32
|
+
* - context: Data passed from previous functions
|
|
33
|
+
* @param logger - Optional Pino logger instance for structured logging
|
|
34
|
+
* @returns Promise resolving to RunFunctionResponse containing:
|
|
35
|
+
* - desired: Updated desired state for resources
|
|
36
|
+
* - results: Status messages (normal, warning, or fatal)
|
|
37
|
+
* - context: Data to pass to next function
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* class MyFunction implements FunctionHandler {
|
|
42
|
+
* async RunFunction(req: RunFunctionRequest, logger?: Logger): Promise<RunFunctionResponse> {
|
|
43
|
+
* let rsp = to(req);
|
|
44
|
+
* const oxr = getObservedCompositeResource(req);
|
|
45
|
+
*
|
|
46
|
+
* // Your logic here
|
|
47
|
+
*
|
|
48
|
+
* normal(rsp, "Processing complete");
|
|
49
|
+
* return rsp;
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
RunFunction(req: RunFunctionRequest, logger?: Logger): Promise<RunFunctionResponse>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* FunctionRunner wraps a FunctionHandler to provide error handling and logging.
|
|
58
|
+
*
|
|
59
|
+
* This class implements the gRPC service for Crossplane functions. It delegates
|
|
60
|
+
* to the user-provided FunctionHandler and ensures consistent error handling,
|
|
61
|
+
* logging, and response formatting. Most users won't interact with this class
|
|
62
|
+
* directly - it's used internally by the runtime.
|
|
63
|
+
*/
|
|
64
|
+
export declare class FunctionRunner {
|
|
65
|
+
private logger?;
|
|
66
|
+
private handler;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new FunctionRunner.
|
|
69
|
+
*
|
|
70
|
+
* @param handler - User-provided implementation of FunctionHandler
|
|
71
|
+
* @param logger - Optional Pino logger instance for error logging
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const myFunction = new MyFunction();
|
|
76
|
+
* const runner = new FunctionRunner(myFunction, logger);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
constructor(handler: FunctionHandler, logger?: Logger);
|
|
80
|
+
/**
|
|
81
|
+
* Run the function with error handling and logging.
|
|
82
|
+
*
|
|
83
|
+
* This method wraps the user's RunFunction implementation with error handling.
|
|
84
|
+
* If the handler throws an error, it will be caught, logged, and returned as
|
|
85
|
+
* a fatal result in the response. This ensures Crossplane always receives a
|
|
86
|
+
* valid response even when the function encounters unexpected errors.
|
|
87
|
+
*
|
|
88
|
+
* @param req - The RunFunctionRequest from Crossplane
|
|
89
|
+
* @param logger - Optional logger (overrides constructor logger if provided)
|
|
90
|
+
* @returns Promise resolving to RunFunctionResponse (either from handler or error response)
|
|
91
|
+
*/
|
|
92
|
+
RunFunction(req: RunFunctionRequest, logger?: Logger): Promise<RunFunctionResponse>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a gRPC server configured for Crossplane function handling.
|
|
96
|
+
*
|
|
97
|
+
* This function creates and configures a gRPC server with the FunctionRunner
|
|
98
|
+
* service registered. The server is ready to receive RunFunction requests from
|
|
99
|
+
* Crossplane via the FunctionRunnerService gRPC service definition.
|
|
100
|
+
*
|
|
101
|
+
* This is typically used internally by the runtime. Most users should use
|
|
102
|
+
* newGrpcServer from the runtime module instead.
|
|
103
|
+
*
|
|
104
|
+
* @param functionRunner - The FunctionRunner instance to handle requests
|
|
105
|
+
* @param logger - Logger instance for debug logging
|
|
106
|
+
* @returns A configured gRPC Server with the function service registered
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const runner = new FunctionRunner(new MyFunction(), logger);
|
|
111
|
+
* const server = getServer(runner, logger);
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export declare function getServer(functionRunner: FunctionRunner, logger: Logger): grpc.Server;
|
|
115
|
+
//# sourceMappingURL=function.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function.d.ts","sourceRoot":"","sources":["../../src/function/function.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAEH,kBAAkB,EAClB,mBAAmB,EACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAMnC;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,WAAW,CACP,GAAG,EAAE,kBAAkB,EACvB,MAAM,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACnC;AAED;;;;;;;GAOG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAkB;IAEjC;;;;;;;;;;;OAWG;gBACS,OAAO,EAAE,eAAe,EAAE,MAAM,CAAC,EAAE,MAAM;IAKrD;;;;;;;;;;;OAWG;IACG,WAAW,CACb,GAAG,EAAE,kBAAkB,EACvB,MAAM,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,mBAAmB,CAAC;CAoBlC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CACrB,cAAc,EAAE,cAAc,EAC9B,MAAM,EAAE,MAAM,GACf,IAAI,CAAC,MAAM,CA0Bb"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core function interfaces and classes for building Crossplane functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides the fundamental building blocks for creating Crossplane
|
|
5
|
+
* composition functions in TypeScript. It includes the FunctionHandler interface
|
|
6
|
+
* that users implement, the FunctionRunner that wraps handlers, and utilities
|
|
7
|
+
* for creating gRPC servers.
|
|
8
|
+
*/
|
|
9
|
+
import * as grpc from "@grpc/grpc-js";
|
|
10
|
+
import { FunctionRunnerServiceService, RunFunctionRequest, RunFunctionResponse, } from "../proto/run_function.js";
|
|
11
|
+
import { fatal, to, } from "../response/response.js";
|
|
12
|
+
/**
|
|
13
|
+
* FunctionRunner wraps a FunctionHandler to provide error handling and logging.
|
|
14
|
+
*
|
|
15
|
+
* This class implements the gRPC service for Crossplane functions. It delegates
|
|
16
|
+
* to the user-provided FunctionHandler and ensures consistent error handling,
|
|
17
|
+
* logging, and response formatting. Most users won't interact with this class
|
|
18
|
+
* directly - it's used internally by the runtime.
|
|
19
|
+
*/
|
|
20
|
+
export class FunctionRunner {
|
|
21
|
+
logger;
|
|
22
|
+
handler;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new FunctionRunner.
|
|
25
|
+
*
|
|
26
|
+
* @param handler - User-provided implementation of FunctionHandler
|
|
27
|
+
* @param logger - Optional Pino logger instance for error logging
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const myFunction = new MyFunction();
|
|
32
|
+
* const runner = new FunctionRunner(myFunction, logger);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(handler, logger) {
|
|
36
|
+
this.handler = handler;
|
|
37
|
+
this.logger = logger;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Run the function with error handling and logging.
|
|
41
|
+
*
|
|
42
|
+
* This method wraps the user's RunFunction implementation with error handling.
|
|
43
|
+
* If the handler throws an error, it will be caught, logged, and returned as
|
|
44
|
+
* a fatal result in the response. This ensures Crossplane always receives a
|
|
45
|
+
* valid response even when the function encounters unexpected errors.
|
|
46
|
+
*
|
|
47
|
+
* @param req - The RunFunctionRequest from Crossplane
|
|
48
|
+
* @param logger - Optional logger (overrides constructor logger if provided)
|
|
49
|
+
* @returns Promise resolving to RunFunctionResponse (either from handler or error response)
|
|
50
|
+
*/
|
|
51
|
+
async RunFunction(req, logger) {
|
|
52
|
+
const startTime = Date.now();
|
|
53
|
+
const log = logger || this.logger;
|
|
54
|
+
try {
|
|
55
|
+
// Delegate to the user-provided handler
|
|
56
|
+
return await this.handler.RunFunction(req, log);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const duration = Date.now() - startTime;
|
|
60
|
+
log?.error({
|
|
61
|
+
error: error instanceof Error ? error.message : String(error),
|
|
62
|
+
duration: `${duration}ms`,
|
|
63
|
+
}, "Function invocation failed");
|
|
64
|
+
// Return a minimal error response
|
|
65
|
+
const rsp = to(req);
|
|
66
|
+
fatal(rsp, error instanceof Error ? error.message : String(error));
|
|
67
|
+
return rsp;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a gRPC server configured for Crossplane function handling.
|
|
73
|
+
*
|
|
74
|
+
* This function creates and configures a gRPC server with the FunctionRunner
|
|
75
|
+
* service registered. The server is ready to receive RunFunction requests from
|
|
76
|
+
* Crossplane via the FunctionRunnerService gRPC service definition.
|
|
77
|
+
*
|
|
78
|
+
* This is typically used internally by the runtime. Most users should use
|
|
79
|
+
* newGrpcServer from the runtime module instead.
|
|
80
|
+
*
|
|
81
|
+
* @param functionRunner - The FunctionRunner instance to handle requests
|
|
82
|
+
* @param logger - Logger instance for debug logging
|
|
83
|
+
* @returns A configured gRPC Server with the function service registered
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const runner = new FunctionRunner(new MyFunction(), logger);
|
|
88
|
+
* const server = getServer(runner, logger);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function getServer(functionRunner, logger) {
|
|
92
|
+
const server = new grpc.Server();
|
|
93
|
+
// Implement the service using the generated interface
|
|
94
|
+
const implementation = {
|
|
95
|
+
runFunction: (call, callback) => {
|
|
96
|
+
functionRunner.RunFunction(call.request, logger)
|
|
97
|
+
.then((response) => {
|
|
98
|
+
logger.debug("RunFunction succeeded, sending response");
|
|
99
|
+
callback(null, response);
|
|
100
|
+
})
|
|
101
|
+
.catch((error) => {
|
|
102
|
+
logger.error("RunFunction failed:", error);
|
|
103
|
+
callback(error);
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
logger.debug(`adding service to grpc server: ${FunctionRunnerServiceService.runFunction.path}`);
|
|
108
|
+
server.addService(FunctionRunnerServiceService, implementation);
|
|
109
|
+
return server;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=function.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function.js","sourceRoot":"","sources":["../../src/function/function.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EACH,4BAA4B,EAC5B,kBAAkB,EAClB,mBAAmB,GACtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACH,KAAK,EACL,EAAE,GACL,MAAM,yBAAyB,CAAC;AAkDjC;;;;;;;GAOG;AACH,MAAM,OAAO,cAAc;IACf,MAAM,CAAU;IAChB,OAAO,CAAkB;IAEjC;;;;;;;;;;;OAWG;IACH,YAAY,OAAwB,EAAE,MAAe;QACjD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,WAAW,CACb,GAAuB,EACvB,MAAe;QAEf,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;QAElC,IAAI,CAAC;YACD,wCAAwC;YACxC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;Q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AC;KACJ,CAAC;IAEF,MAAM,CAAC,KAAK,CACR,kCAAkC,4BAA4B,CAAC,WAAW,CAAC,IAAI,EAAE,CACpF,CAAC;IACF,MAAM,CAAC,UAAU,CAAC,4BAA4B,EAAE,cAAc,CAAC,CAAC;IAChE,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { FunctionHandler } from "./function/function.js";
|
|
2
|
+
export { FunctionRunner, getServer } from "./function/function.js";
|
|
3
|
+
export { getDesiredCompositeResource, getObservedCompositeResource, getDesiredComposedResources, getObservedComposedResources, getInput, getContextKey, getRequiredResources, getCredentials, } from "./request/request.js";
|
|
4
|
+
export { to, fatal, normal, warning, setDesiredComposedResources, setDesiredCompositeStatus, setDesiredCompositeResource, updateDesiredComposedResources, update, setContextKey, setOutput, DEFAULT_TTL, } from "./response/response.js";
|
|
5
|
+
export { asObject, asStruct, fromObject, toObject, newDesiredComposed, mustStructObject, mustStructJSON, type Composite, type ObservedComposed, type DesiredComposed, type ConnectionDetails, } from "./resource/resource.js";
|
|
6
|
+
export { newGrpcServer, startServer, getServerCredentials, type ServerOptions, } from "./runtime/runtime.js";
|
|
7
|
+
export { RunFunctionRequest, RunFunctionResponse, Resource, Severity, Result, State, Ready, Target, Status, Condition, Resources, Credentials, CredentialData, FunctionRunnerServiceService, } from "./proto/run_function.js";
|
|
8
|
+
export type { Logger } from "pino";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnE,OAAO,EACH,2BAA2B,EAC3B,4BAA4B,EAC5B,2BAA2B,EAC3B,4BAA4B,EAC5B,QAAQ,EACR,aAAa,EACb,oBAAoB,EACpB,cAAc,GACjB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACH,EAAE,EACF,KAAK,EACL,MAAM,EACN,OAAO,EACP,2BAA2B,EAC3B,yBAAyB,EACzB,2BAA2B,EAC3B,8BAA8B,EAC9B,MAAM,EACN,aAAa,EACb,SAAS,EACT,WAAW,GACd,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACH,QAAQ,EACR,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,iBAAiB,GACzB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACH,aAAa,EACb,WAAW,EACX,oBAAoB,EACpB,KAAK,aAAa,GACrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACH,kBAAkB,EAClB,mBAAmB,EACnB,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,KAAK,EACL,KAAK,EACL,MAAM,EACN,MAAM,EACN,SAAS,EACT,SAAS,EACT,WAAW,EACX,cAAc,EACd,4BAA4B,GAC/B,MAAM,yBAAyB,CAAC;AAEjC,YAAY,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Main SDK exports for the Crossplane Function SDK
|
|
2
|
+
export { FunctionRunner, getServer } from "./function/function.js";
|
|
3
|
+
// Request helpers
|
|
4
|
+
export { getDesiredCompositeResource, getObservedCompositeResource, getDesiredComposedResources, getObservedComposedResources, getInput, getContextKey, getRequiredResources, getCredentials, } from "./request/request.js";
|
|
5
|
+
// Response helpers
|
|
6
|
+
export { to, fatal, normal, warning, setDesiredComposedResources, setDesiredCompositeStatus, setDesiredCompositeResource, updateDesiredComposedResources, update, setContextKey, setOutput, DEFAULT_TTL, } from "./response/response.js";
|
|
7
|
+
// Resource utilities
|
|
8
|
+
export { asObject, asStruct, fromObject, toObject, newDesiredComposed, mustStructObject, mustStructJSON, } from "./resource/resource.js";
|
|
9
|
+
// Runtime utilities
|
|
10
|
+
export { newGrpcServer, startServer, getServerCredentials, } from "./runtime/runtime.js";
|
|
11
|
+
// Protocol buffer types
|
|
12
|
+
export { RunFunctionRequest, RunFunctionResponse, Resource, Severity, Result, State, Ready, Target, Status, Condition, Resources, Credentials, CredentialData, FunctionRunnerServiceService, } from "./proto/run_function.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AAInD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnE,kBAAkB;AAClB,OAAO,EACH,2BAA2B,EAC3B,4BAA4B,EAC5B,2BAA2B,EAC3B,4BAA4B,EAC5B,QAAQ,EACR,aAAa,EACb,oBAAoB,EACpB,cAAc,GACjB,MAAM,sBAAsB,CAAC;AAE9B,mBAAmB;AACnB,OAAO,EACH,EAAE,EACF,KAAK,EACL,MAAM,EACN,OAAO,EACP,2BAA2B,EAC3B,yBAAyB,EACzB,2BAA2B,EAC3B,8BAA8B,EAC9B,MAAM,EACN,aAAa,EACb,SAAS,EACT,WAAW,GACd,MAAM,wBAAwB,CAAC;AAEhC,qBAAqB;AACrB,OAAO,EACH,QAAQ,EACR,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,GAKjB,MAAM,wBAAwB,CAAC;AAEhC,oBAAoB;AACpB,OAAO,EACH,aAAa,EACb,WAAW,EACX,oBAAoB,GAEvB,MAAM,sBAAsB,CAAC;AAE9B,wBAAwB;AACxB,OAAO,EACH,kBAAkB,EAClB,mBAAmB,EACnB,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,KAAK,EACL,KAAK,EACL,MAAM,EACN,MAAM,EACN,SAAS,EACT,SAAS,EACT,WAAW,EACX,cAAc,EACd,4BAA4B,GAC/B,MAAM,yBAAyB,CAAC"}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":""}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { newGrpcServer, startServer } from "./runtime/runtime.js";
|
|
4
|
+
import { pino } from "pino";
|
|
5
|
+
import { FunctionRunner } from "./function/function.js";
|
|
6
|
+
import { ExampleFunction } from "./example-function.js";
|
|
7
|
+
// defaultAddress where the function will listen for gRPC connections
|
|
8
|
+
const defaultAddress = "0.0.0.0:9443";
|
|
9
|
+
// defaultTlsServerCertsDir is the directory where the XP package reconciler stores generated TLS certs
|
|
10
|
+
const defaultTlsServerCertsDir = "/tls/server";
|
|
11
|
+
const logger = pino({
|
|
12
|
+
level: "info",
|
|
13
|
+
});
|
|
14
|
+
const program = new Command("function-typescript-example")
|
|
15
|
+
.option("--address", "Address at which to listen for gRPC connections", defaultAddress)
|
|
16
|
+
.option("-d, --debug", "Emit debug logs.", false)
|
|
17
|
+
.option("--insecure", "Run without mTLS credentials.", false)
|
|
18
|
+
.option("--tls-server-certs-dir [Directory]", "Serve using mTLS certificates in this directory. The directory should contain tls.key, tls.crt, and ca.crt files.", defaultTlsServerCertsDir);
|
|
19
|
+
program.parse(process.argv);
|
|
20
|
+
function parseArgs(args) {
|
|
21
|
+
return {
|
|
22
|
+
address: args?.address || defaultAddress,
|
|
23
|
+
debug: args.debug,
|
|
24
|
+
insecure: args.insecure,
|
|
25
|
+
tlsServerCertsDir: args.tlsServerCertsDir,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function main() {
|
|
29
|
+
const args = program.opts();
|
|
30
|
+
const opts = parseArgs(args);
|
|
31
|
+
const logger = pino({
|
|
32
|
+
level: opts?.debug ? "debug" : "info",
|
|
33
|
+
formatters: {
|
|
34
|
+
level: (label) => {
|
|
35
|
+
return { severity: label.toUpperCase() };
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
logger.debug({ "options passed to function": opts });
|
|
40
|
+
try {
|
|
41
|
+
// Create an instance of your function implementation
|
|
42
|
+
// This is an example - users would replace ExampleFunction with their own implementation
|
|
43
|
+
const exampleFunction = new ExampleFunction();
|
|
44
|
+
// Create the function runner with your handler
|
|
45
|
+
const fnRunner = new FunctionRunner(exampleFunction, logger);
|
|
46
|
+
// Create and start the gRPC server
|
|
47
|
+
const server = newGrpcServer(fnRunner, logger);
|
|
48
|
+
startServer(server, opts, logger);
|
|
49
|
+
// Keep the process running to handle gRPC requests
|
|
50
|
+
process.on('SIGINT', () => {
|
|
51
|
+
logger.info('shutting down gracefully...');
|
|
52
|
+
server.tryShutdown((err) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
logger.error(err, 'error during shutdown');
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
logger.info('server shut down successfully');
|
|
58
|
+
process.exit(0);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
logger.error(err);
|
|
64
|
+
// eslint-disable-next-line no-process-exit
|
|
65
|
+
process.exit(-1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
main();
|
|
69
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAElE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B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ackage = "google.protobuf";
|
|
3
|
+
/**
|
|
4
|
+
* A Duration represents a signed, fixed-length span of time represented
|
|
5
|
+
* as a count of seconds and fractions of seconds at nanosecond
|
|
6
|
+
* resolution. It is independent of any calendar and concepts like "day"
|
|
7
|
+
* or "month". It is related to Timestamp in that the difference between
|
|
8
|
+
* two Timestamp values is a Duration and it can be added or subtracted
|
|
9
|
+
* from a Timestamp. Range is approximately +-10,000 years.
|
|
10
|
+
*
|
|
11
|
+
* # Examples
|
|
12
|
+
*
|
|
13
|
+
* Example 1: Compute Duration from two Timestamps in pseudo code.
|
|
14
|
+
*
|
|
15
|
+
* Timestamp start = ...;
|
|
16
|
+
* Timestamp end = ...;
|
|
17
|
+
* Duration duration = ...;
|
|
18
|
+
*
|
|
19
|
+
* duration.seconds = end.seconds - start.seconds;
|
|
20
|
+
* duration.nanos = end.nanos - start.nanos;
|
|
21
|
+
*
|
|
22
|
+
* if (duration.seconds < 0 && duration.nanos > 0) {
|
|
23
|
+
* duration.seconds += 1;
|
|
24
|
+
* duration.nanos -= 1000000000;
|
|
25
|
+
* } else if (duration.seconds > 0 && duration.nanos < 0) {
|
|
26
|
+
* duration.seconds -= 1;
|
|
27
|
+
* duration.nanos += 1000000000;
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
|
|
31
|
+
*
|
|
32
|
+
* Timestamp start = ...;
|
|
33
|
+
* Duration duration = ...;
|
|
34
|
+
* Timestamp end = ...;
|
|
35
|
+
*
|
|
36
|
+
* end.seconds = start.seconds + duration.seconds;
|
|
37
|
+
* end.nanos = start.nanos + duration.nanos;
|
|
38
|
+
*
|
|
39
|
+
* if (end.nanos < 0) {
|
|
40
|
+
* end.seconds -= 1;
|
|
41
|
+
* end.nanos += 1000000000;
|
|
42
|
+
* } else if (end.nanos >= 1000000000) {
|
|
43
|
+
* end.seconds += 1;
|
|
44
|
+
* end.nanos -= 1000000000;
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* Example 3: Compute Duration from datetime.timedelta in Python.
|
|
48
|
+
*
|
|
49
|
+
* td = datetime.timedelta(days=3, minutes=10)
|
|
50
|
+
* duration = Duration()
|
|
51
|
+
* duration.FromTimedelta(td)
|
|
52
|
+
*
|
|
53
|
+
* # JSON Mapping
|
|
54
|
+
*
|
|
55
|
+
* In JSON format, the Duration type is encoded as a string rather than an
|
|
56
|
+
* object, where the string ends in the suffix "s" (indicating seconds) and
|
|
57
|
+
* is preceded by the number of seconds, with nanoseconds expressed as
|
|
58
|
+
* fractional seconds. For example, 3 seconds with 0 nanoseconds should be
|
|
59
|
+
* encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
|
|
60
|
+
* be expressed in JSON format as "3.000000001s", and 3 seconds and 1
|
|
61
|
+
* microsecond should be expressed in JSON format as "3.000001s".
|
|
62
|
+
*/
|
|
63
|
+
export interface Duration {
|
|
64
|
+
/**
|
|
65
|
+
* Signed seconds of the span of time. Must be from -315,576,000,000
|
|
66
|
+
* to +315,576,000,000 inclusive. Note: these bounds are computed from:
|
|
67
|
+
* 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
|
|
68
|
+
*/
|
|
69
|
+
seconds: number;
|
|
70
|
+
/**
|
|
71
|
+
* Signed fractions of a second at nanosecond resolution of the span
|
|
72
|
+
* of time. Durations less than one second are represented with a 0
|
|
73
|
+
* `seconds` field and a positive or negative `nanos` field. For durations
|
|
74
|
+
* of one second or more, a non-zero value for the `nanos` field must be
|
|
75
|
+
* of the same sign as the `seconds` field. Must be from -999,999,999
|
|
76
|
+
* to +999,999,999 inclusive.
|
|
77
|
+
*/
|
|
78
|
+
nanos: number;
|
|
79
|
+
}
|
|
80
|
+
export declare const Duration: MessageFns<Duration>;
|
|
81
|
+
export interface DataLoaderOptions {
|
|
82
|
+
cache?: boolean;
|
|
83
|
+
}
|
|
84
|
+
export interface DataLoaders {
|
|
85
|
+
rpcDataLoaderOptions?: DataLoaderOptions;
|
|
86
|
+
getDataLoader<T>(identifier: string, constructorFn: () => T): T;
|
|
87
|
+
}
|
|
88
|
+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
|
|
89
|
+
export type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
|
|
90
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
91
|
+
} : Partial<T>;
|
|
92
|
+
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|
93
|
+
export type Exact<P, I extends P> = P extends Builtin ? P : P & {
|
|
94
|
+
[K in keyof P]: Exact<P[K], I[K]>;
|
|
95
|
+
} & {
|
|
96
|
+
[K in Exclude<keyof I, KeysOfUnion<P>>]: never;
|
|
97
|
+
};
|
|
98
|
+
export interface MessageFns<T> {
|
|
99
|
+
encode(message: T, writer?: BinaryWriter): BinaryWriter;
|
|
100
|
+
decode(input: BinaryReader | Uint8Array, length?: number): T;
|
|
101
|
+
fromJSON(object: any): T;
|
|
102
|
+
toJSON(message: T): unknown;
|
|
103
|
+
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
|
|
104
|
+
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
|
|
105
|
+
}
|
|
106
|
+
export {};
|
|
107
|
+
//# sourceMappingURL=duration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../../../src/proto/google/protobuf/duration.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAErE,eAAO,MAAM,eAAe,oBAAoB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,MAAM,WAAW,QAAQ;IACvB;;;;OAIG;IACH,OAAO,EAAE,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C,CAAC;CAAE,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;CAAE,CAAC;AAiBnG,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC;IACxD,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;IAC7D,QAAQ,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;IACzB,MAAM,CAAC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;IAC5B,MAAM,CAAC,CAAC,SAAS,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACxD,WAAW,CAAC,CAAC,SAAS,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;CAC/D"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
|
2
|
+
// versions:
|
|
3
|
+
// protoc-gen-ts_proto v2.8.3
|
|
4
|
+
// protoc v6.33.0
|
|
5
|
+
// source: google/protobuf/duration.proto
|
|
6
|
+
/* eslint-disable */
|
|
7
|
+
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
|
|
8
|
+
export const protobufPackage = "google.protobuf";
|
|
9
|
+
function createBaseDuration() {
|
|
10
|
+
return { seconds: 0, nanos: 0 };
|
|
11
|
+
}
|
|
12
|
+
export const Duration = {
|
|
13
|
+
encode(message, writer = new BinaryWriter()) {
|
|
14
|
+
if (message.seconds !== 0) {
|
|
15
|
+
writer.uint32(8).int64(message.seconds);
|
|
16
|
+
}
|
|
17
|
+
if (message.nanos !== 0) {
|
|
18
|
+
writer.uint32(16).int32(message.nanos);
|
|
19
|
+
}
|
|
20
|
+
return writer;
|
|
21
|
+
},
|
|
22
|
+
decode(input, length) {
|
|
23
|
+
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
|
|
24
|
+
const end = length === undefined ? reader.len : reader.pos + length;
|
|
25
|
+
const message = createBaseDuration();
|
|
26
|
+
while (reader.pos < end) {
|
|
27
|
+
const tag = reader.uint32();
|
|
28
|
+
switch (tag >>> 3) {
|
|
29
|
+
case 1: {
|
|
30
|
+
if (tag !== 8) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
message.seconds = longToNumber(reader.int64());
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
case 2: {
|
|
37
|
+
if (tag !== 16) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
message.nanos = reader.int32();
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if ((tag & 7) === 4 || tag === 0) {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
reader.skip(tag & 7);
|
|
48
|
+
}
|
|
49
|
+
return message;
|
|
50
|
+
},
|
|
51
|
+
fromJSON(object) {
|
|
52
|
+
return {
|
|
53
|
+
seconds: isSet(object.seconds) ? globalThis.Number(object.seconds) : 0,
|
|
54
|
+
nanos: isSet(object.nanos) ? globalThis.Number(object.nanos) : 0,
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
toJSON(message) {
|
|
58
|
+
const obj = {};
|
|
59
|
+
if (message.seconds !== 0) {
|
|
60
|
+
obj.seconds = Math.round(message.seconds);
|
|
61
|
+
}
|
|
62
|
+
if (message.nanos !== 0) {
|
|
63
|
+
obj.nanos = Math.round(message.nanos);
|
|
64
|
+
}
|
|
65
|
+
return obj;
|
|
66
|
+
},
|
|
67
|
+
create(base) {
|
|
68
|
+
return Duration.fromPartial(base ?? {});
|
|
69
|
+
},
|
|
70
|
+
fromPartial(object) {
|
|
71
|
+
const message = createBaseDuration();
|
|
72
|
+
message.seconds = object.seconds ?? 0;
|
|
73
|
+
message.nanos = object.nanos ?? 0;
|
|
74
|
+
return message;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
function longToNumber(int64) {
|
|
78
|
+
const num = globalThis.Number(int64.toString());
|
|
79
|
+
if (num > globalThis.Number.MAX_SAFE_INTEGER) {
|
|
80
|
+
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
|
|
81
|
+
}
|
|
82
|
+
if (num < globalThis.Number.MIN_SAFE_INTEGER) {
|
|
83
|
+
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
|
|
84
|
+
}
|
|
85
|
+
return num;
|
|
86
|
+
}
|
|
87
|
+
function isSet(value) {
|
|
88
|
+
return value !== null && value !== undefined;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=duration.js.map
|