@chainlink/cre-sdk 0.0.1-alpha → 0.0.2-alpha
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/dist/generated/capabilities/internal/nodeaction/v1/node_action_pb.d.ts +69 -0
- package/dist/generated/capabilities/internal/nodeaction/v1/node_action_pb.js +29 -0
- package/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.d.ts +13 -18
- package/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.js +71 -260
- package/dist/generated-sdk/capabilities/internal/actionandtrigger/v1/basic_sdk_gen.d.ts +4 -9
- package/dist/generated-sdk/capabilities/internal/actionandtrigger/v1/basic_sdk_gen.js +14 -45
- package/dist/generated-sdk/capabilities/internal/basicaction/v1/basicaction_sdk_gen.d.ts +3 -7
- package/dist/generated-sdk/capabilities/internal/basicaction/v1/basicaction_sdk_gen.js +9 -36
- package/dist/generated-sdk/capabilities/internal/basictrigger/v1/basic_sdk_gen.d.ts +2 -8
- package/dist/generated-sdk/capabilities/internal/basictrigger/v1/basic_sdk_gen.js +8 -21
- package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.d.ts +5 -8
- package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.js +16 -60
- package/dist/generated-sdk/capabilities/internal/nodeaction/v1/basicaction_sdk_gen.d.ts +19 -0
- package/dist/generated-sdk/capabilities/internal/nodeaction/v1/basicaction_sdk_gen.js +36 -0
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.d.ts +3 -7
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.js +9 -36
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.d.ts +2 -8
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.js +8 -21
- package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.d.ts +2 -8
- package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.js +8 -21
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/sdk/cre/index.d.ts +5 -19
- package/dist/sdk/cre/index.js +1 -17
- package/dist/sdk/errors.d.ts +12 -0
- package/dist/sdk/errors.js +21 -0
- package/dist/sdk/impl/runtime-impl.d.ts +38 -0
- package/dist/sdk/impl/runtime-impl.js +190 -0
- package/dist/sdk/impl/runtime-impl.test.js +334 -0
- package/dist/sdk/index.d.ts +3 -0
- package/dist/sdk/index.js +2 -0
- package/dist/sdk/runtime.d.ts +24 -0
- package/dist/sdk/utils/capabilities/capability-error.d.ts +0 -3
- package/dist/sdk/utils/capabilities/capability-error.js +0 -2
- package/dist/sdk/utils/config/configHandler.test.d.ts +1 -0
- package/dist/sdk/utils/config/configHandler.test.js +193 -0
- package/dist/sdk/utils/config/index.d.ts +4 -6
- package/dist/sdk/utils/config/index.js +10 -34
- package/dist/sdk/utils/index.d.ts +0 -3
- package/dist/sdk/utils/index.js +0 -3
- package/dist/sdk/utils/values/consensus_aggregators.d.ts +1 -1
- package/dist/sdk/{runtime → wasm}/host-bindings.d.ts +0 -1
- package/dist/sdk/{runtime → wasm}/host-bindings.js +0 -4
- package/dist/sdk/wasm/index.d.ts +1 -0
- package/dist/sdk/wasm/index.js +1 -0
- package/dist/sdk/wasm/runner.d.ts +14 -0
- package/dist/sdk/wasm/runner.js +114 -0
- package/dist/sdk/wasm/runner.test.d.ts +1 -0
- package/dist/sdk/wasm/runner.test.js +270 -0
- package/dist/sdk/wasm/runtime.d.ts +7 -0
- package/dist/sdk/wasm/runtime.js +55 -0
- package/dist/sdk/workflow.d.ts +13 -15
- package/dist/sdk/workflow.js +1 -21
- package/package.json +2 -2
- package/dist/sdk/engine/execute.d.ts +0 -4
- package/dist/sdk/engine/execute.js +0 -12
- package/dist/sdk/engine/execute.test.js +0 -129
- package/dist/sdk/engine/handleExecutionPhase.d.ts +0 -4
- package/dist/sdk/engine/handleExecutionPhase.js +0 -34
- package/dist/sdk/engine/handleSubscribePhase.d.ts +0 -3
- package/dist/sdk/engine/handleSubscribePhase.js +0 -23
- package/dist/sdk/logger.d.ts +0 -7
- package/dist/sdk/logger.js +0 -18
- package/dist/sdk/runtime/errors.d.ts +0 -23
- package/dist/sdk/runtime/errors.js +0 -30
- package/dist/sdk/runtime/index.d.ts +0 -3
- package/dist/sdk/runtime/index.js +0 -2
- package/dist/sdk/runtime/run-in-node-mode.d.ts +0 -12
- package/dist/sdk/runtime/run-in-node-mode.js +0 -47
- package/dist/sdk/runtime/run-in-node-mode.test.js +0 -116
- package/dist/sdk/runtime/runtime.d.ts +0 -36
- package/dist/sdk/runtime/runtime.js +0 -84
- package/dist/sdk/runtime/runtime.test.d.ts +0 -1
- package/dist/sdk/runtime/runtime.test.js +0 -58
- package/dist/sdk/testhelpers/dangerously-call-capability.d.ts +0 -10
- package/dist/sdk/testhelpers/dangerously-call-capability.js +0 -26
- package/dist/sdk/testhelpers/mock-host-bindings.d.ts +0 -15
- package/dist/sdk/testhelpers/mock-host-bindings.js +0 -25
- package/dist/sdk/testhelpers/mock-runtime.d.ts +0 -2
- package/dist/sdk/testhelpers/mock-runtime.js +0 -16
- package/dist/sdk/utils/await-async-request.d.ts +0 -9
- package/dist/sdk/utils/await-async-request.js +0 -27
- package/dist/sdk/utils/capabilities/call-capability.d.ts +0 -22
- package/dist/sdk/utils/capabilities/call-capability.js +0 -33
- package/dist/sdk/utils/capabilities/callback-id.d.ts +0 -3
- package/dist/sdk/utils/capabilities/callback-id.js +0 -22
- package/dist/sdk/utils/capabilities/http/fetch.d.ts +0 -44
- package/dist/sdk/utils/capabilities/http/fetch.js +0 -63
- package/dist/sdk/utils/do-request-async.d.ts +0 -12
- package/dist/sdk/utils/do-request-async.js +0 -17
- package/dist/sdk/utils/error-boundary.d.ts +0 -2
- package/dist/sdk/utils/error-boundary.js +0 -30
- package/dist/sdk/utils/get-request.d.ts +0 -1
- package/dist/sdk/utils/get-request.js +0 -15
- package/dist/sdk/utils/lazy-promise.d.ts +0 -59
- package/dist/sdk/utils/lazy-promise.js +0 -81
- package/dist/sdk/utils/random/get-rand.d.ts +0 -3
- package/dist/sdk/utils/random/get-rand.js +0 -6
- package/dist/sdk/utils/random/random.d.ts +0 -35
- package/dist/sdk/utils/random/random.js +0 -123
- package/dist/sdk/utils/secrets/await-async-secret.d.ts +0 -1
- package/dist/sdk/utils/secrets/await-async-secret.js +0 -29
- package/dist/sdk/utils/secrets/do-get-secret.d.ts +0 -1
- package/dist/sdk/utils/secrets/do-get-secret.js +0 -14
- package/dist/sdk/utils/secrets/get-secret.d.ts +0 -1
- package/dist/sdk/utils/secrets/get-secret.js +0 -10
- package/dist/sdk/utils/send-error.d.ts +0 -1
- package/dist/sdk/utils/send-error.js +0 -16
- package/dist/sdk/utils/send-response-value.d.ts +0 -2
- package/dist/sdk/utils/send-response-value.js +0 -15
- package/dist/sdk/utils/time/get-time.d.ts +0 -16
- package/dist/sdk/utils/time/get-time.js +0 -21
- /package/dist/sdk/{engine/execute.test.d.ts → impl/runtime-impl.test.d.ts} +0 -0
- /package/dist/sdk/{runtime/run-in-node-mode.test.d.ts → runtime.js} +0 -0
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type CapabilityResponse } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
import type { CallCapabilityParams } from '../utils/capabilities/call-capability';
|
|
3
|
-
/**
|
|
4
|
-
* Dangerous call capability.
|
|
5
|
-
* This function does not perform any runtime guard checks.
|
|
6
|
-
* Use this function only in test code.
|
|
7
|
-
*
|
|
8
|
-
* @see callCapability implementation for actual proper usage.
|
|
9
|
-
* */
|
|
10
|
-
export declare function dangerouslyCallCapability({ capabilityId, method, mode, payload, }: CallCapabilityParams): Promise<CapabilityResponse>;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
import { awaitAsyncRequest } from '../utils/await-async-request';
|
|
3
|
-
import { doRequestAsync } from '../utils/do-request-async';
|
|
4
|
-
import { LazyPromise } from '../utils/lazy-promise';
|
|
5
|
-
/**
|
|
6
|
-
* Dangerous call capability.
|
|
7
|
-
* This function does not perform any runtime guard checks.
|
|
8
|
-
* Use this function only in test code.
|
|
9
|
-
*
|
|
10
|
-
* @see callCapability implementation for actual proper usage.
|
|
11
|
-
* */
|
|
12
|
-
export function dangerouslyCallCapability({ capabilityId, method, mode = Mode.DON, payload, }) {
|
|
13
|
-
const callbackId = doRequestAsync({
|
|
14
|
-
capabilityId,
|
|
15
|
-
method,
|
|
16
|
-
mode,
|
|
17
|
-
payload,
|
|
18
|
-
});
|
|
19
|
-
return new LazyPromise(async () => {
|
|
20
|
-
return awaitAsyncRequest(callbackId, {
|
|
21
|
-
capabilityId,
|
|
22
|
-
method,
|
|
23
|
-
mode,
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
export declare const calls: string[];
|
|
3
|
-
export declare const mockHostBindings: {
|
|
4
|
-
sendResponse: import("bun:test").Mock<(_response: Uint8Array) => number>;
|
|
5
|
-
switchModes: import("bun:test").Mock<(mode: Mode) => void>;
|
|
6
|
-
log: import("bun:test").Mock<(_message: string) => void>;
|
|
7
|
-
callCapability: import("bun:test").Mock<(_request: string) => number>;
|
|
8
|
-
awaitCapabilities: import("bun:test").Mock<(_awaitRequest: string, _maxResponseLen: number) => string>;
|
|
9
|
-
getSecrets: import("bun:test").Mock<(_request: string, _maxResponseLen: number) => number>;
|
|
10
|
-
awaitSecrets: import("bun:test").Mock<(_awaitRequest: string, _maxResponseLen: number) => string>;
|
|
11
|
-
versionV2: import("bun:test").Mock<() => void>;
|
|
12
|
-
randomSeed: import("bun:test").Mock<(_mode: Mode.DON | Mode.NODE) => number>;
|
|
13
|
-
getWasiArgs: import("bun:test").Mock<() => string>;
|
|
14
|
-
now: import("bun:test").Mock<() => number>;
|
|
15
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { mock } from 'bun:test';
|
|
2
|
-
import { Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
-
// Mock hostBindings before importing runInNodeMode
|
|
4
|
-
export const calls = [];
|
|
5
|
-
export const mockHostBindings = {
|
|
6
|
-
sendResponse: mock((_response) => 0),
|
|
7
|
-
switchModes: mock((mode) => {
|
|
8
|
-
calls.push(mode === Mode.NODE ? 'NODE' : mode === Mode.DON ? 'DON' : 'UNSPECIFIED');
|
|
9
|
-
}),
|
|
10
|
-
log: mock((_message) => { }),
|
|
11
|
-
callCapability: mock((_request) => 1),
|
|
12
|
-
awaitCapabilities: mock((_awaitRequest, _maxResponseLen) => btoa('mock_await_capabilities_response')),
|
|
13
|
-
getSecrets: mock((_request, _maxResponseLen) => 1),
|
|
14
|
-
awaitSecrets: mock((_awaitRequest, _maxResponseLen) => btoa('mock_await_secrets_response')),
|
|
15
|
-
versionV2: mock(() => { }),
|
|
16
|
-
randomSeed: mock((_mode) => Math.random()),
|
|
17
|
-
getWasiArgs: mock(() => '["mock.wasm", ""]'),
|
|
18
|
-
now: mock(() => Date.now()),
|
|
19
|
-
};
|
|
20
|
-
// Set up global mocks before any modules are imported
|
|
21
|
-
Object.assign(globalThis, mockHostBindings);
|
|
22
|
-
// Mock the module
|
|
23
|
-
mock.module('../runtime/host-bindings', () => ({
|
|
24
|
-
hostBindings: mockHostBindings,
|
|
25
|
-
}));
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
import { logger } from '../logger';
|
|
3
|
-
import { getRand } from '../utils/random/get-rand';
|
|
4
|
-
import { getSecret } from '../utils/secrets/get-secret';
|
|
5
|
-
import { getTimeAsDate } from '../utils/time/get-time';
|
|
6
|
-
export const mockedRuntime = {
|
|
7
|
-
mode: Mode.DON,
|
|
8
|
-
logger,
|
|
9
|
-
isNodeRuntime: false,
|
|
10
|
-
assertDonSafe: () => { },
|
|
11
|
-
assertNodeSafe: () => { },
|
|
12
|
-
switchModes: (() => mockedRuntime),
|
|
13
|
-
getSecret,
|
|
14
|
-
getRand: () => getRand(Mode.DON),
|
|
15
|
-
now: () => getTimeAsDate(),
|
|
16
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
import { type CapabilityResponse } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
-
type Params = {
|
|
4
|
-
capabilityId: string;
|
|
5
|
-
method: string;
|
|
6
|
-
mode: Mode;
|
|
7
|
-
};
|
|
8
|
-
export declare function awaitAsyncRequest(callbackId: number, { capabilityId, method, mode }: Params): Promise<CapabilityResponse>;
|
|
9
|
-
export {};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { create, fromBinary, toBinary } from '@bufbuild/protobuf';
|
|
2
|
-
import { AwaitCapabilitiesRequestSchema, AwaitCapabilitiesResponseSchema, } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
-
import { hostBindings } from '../runtime/host-bindings';
|
|
4
|
-
import { CapabilityError } from './capabilities/capability-error';
|
|
5
|
-
// Implementation signature (general fallback)
|
|
6
|
-
export async function awaitAsyncRequest(callbackId, { capabilityId, method, mode }) {
|
|
7
|
-
// Create proper AwaitCapabilitiesRequest protobuf message
|
|
8
|
-
const awaitRequest = create(AwaitCapabilitiesRequestSchema, {
|
|
9
|
-
ids: [callbackId],
|
|
10
|
-
});
|
|
11
|
-
// Encode to protobuf bytes
|
|
12
|
-
const awaitRequestBytes = toBinary(AwaitCapabilitiesRequestSchema, awaitRequest);
|
|
13
|
-
const response = hostBindings.awaitCapabilities(awaitRequestBytes, 1024 * 1024);
|
|
14
|
-
// Convert array of numbers to Uint8Array if needed
|
|
15
|
-
const responseBytes = Array.isArray(response) ? new Uint8Array(response) : response;
|
|
16
|
-
const awaitResponse = fromBinary(AwaitCapabilitiesResponseSchema, responseBytes);
|
|
17
|
-
const capabilityResponse = awaitResponse.responses[callbackId];
|
|
18
|
-
if (!capabilityResponse) {
|
|
19
|
-
throw new CapabilityError(`No response found for callback ID ${callbackId}`, {
|
|
20
|
-
capabilityId,
|
|
21
|
-
method,
|
|
22
|
-
mode,
|
|
23
|
-
callbackId,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
return capabilityResponse;
|
|
27
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type CapabilityResponse, Mode } from '../../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
export type CallCapabilityParams = {
|
|
3
|
-
capabilityId: string;
|
|
4
|
-
method: string;
|
|
5
|
-
mode: Mode;
|
|
6
|
-
payload: {
|
|
7
|
-
typeUrl: string;
|
|
8
|
-
value: Uint8Array;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
type CallCapabilityReturn = {
|
|
12
|
-
result: () => Promise<CapabilityResponse>;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Calls a capability asynchronously and returns a promise for the response.
|
|
16
|
-
* The actual call is deferred until the promise is awaited (lazy execution).
|
|
17
|
-
*
|
|
18
|
-
* @param params - The capability call parameters
|
|
19
|
-
* @returns A promise that resolves to the capability response
|
|
20
|
-
*/
|
|
21
|
-
export declare function callCapability({ capabilityId, method, mode, payload, }: CallCapabilityParams): CallCapabilityReturn;
|
|
22
|
-
export {};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Mode } from '../../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
import { runtimeGuards } from '../../runtime/runtime';
|
|
3
|
-
import { awaitAsyncRequest } from '../await-async-request';
|
|
4
|
-
import { doRequestAsync } from '../do-request-async';
|
|
5
|
-
/**
|
|
6
|
-
* Calls a capability asynchronously and returns a promise for the response.
|
|
7
|
-
* The actual call is deferred until the promise is awaited (lazy execution).
|
|
8
|
-
*
|
|
9
|
-
* @param params - The capability call parameters
|
|
10
|
-
* @returns A promise that resolves to the capability response
|
|
11
|
-
*/
|
|
12
|
-
export function callCapability({ capabilityId, method, mode = Mode.DON, payload, }) {
|
|
13
|
-
// Guards:
|
|
14
|
-
// - Block DON-mode calls while currently in NODE mode
|
|
15
|
-
// - Block NODE-mode calls while currently in DON mode
|
|
16
|
-
if (mode === Mode.DON)
|
|
17
|
-
runtimeGuards.assertDonSafe();
|
|
18
|
-
if (mode === Mode.NODE)
|
|
19
|
-
runtimeGuards.assertNodeSafe();
|
|
20
|
-
const callbackId = doRequestAsync({
|
|
21
|
-
capabilityId,
|
|
22
|
-
method,
|
|
23
|
-
mode,
|
|
24
|
-
payload,
|
|
25
|
-
});
|
|
26
|
-
return {
|
|
27
|
-
result: async () => awaitAsyncRequest(callbackId, {
|
|
28
|
-
capabilityId,
|
|
29
|
-
method,
|
|
30
|
-
mode,
|
|
31
|
-
}),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Mode } from '../../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
// Tracks async requests using DON mode, increments to the positive side, 0, 1, 2, 3 ...
|
|
3
|
-
let donCall = 0;
|
|
4
|
-
// Tracks async requests using Node mode, increments to the negative side, -1, -2, -3, -4 ...
|
|
5
|
-
let nodeCall = -1;
|
|
6
|
-
export const getLastCallbackId = (mode) => {
|
|
7
|
-
if (mode !== Mode.DON && mode !== Mode.NODE) {
|
|
8
|
-
throw new Error(`Unsupported capability mode: ${mode}`);
|
|
9
|
-
}
|
|
10
|
-
return mode === Mode.DON ? donCall : nodeCall;
|
|
11
|
-
};
|
|
12
|
-
export const incrementCallbackId = (mode) => {
|
|
13
|
-
if (mode !== Mode.DON && mode !== Mode.NODE) {
|
|
14
|
-
throw new Error(`Unsupported capability mode: ${mode}`);
|
|
15
|
-
}
|
|
16
|
-
if (mode === Mode.DON) {
|
|
17
|
-
donCall++;
|
|
18
|
-
return donCall;
|
|
19
|
-
}
|
|
20
|
-
nodeCall--;
|
|
21
|
-
return nodeCall;
|
|
22
|
-
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export type CreFetchRequest = {
|
|
2
|
-
url: string;
|
|
3
|
-
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
|
|
4
|
-
headers?: Record<string, string>;
|
|
5
|
-
body?: string;
|
|
6
|
-
timeoutMs?: number;
|
|
7
|
-
cacheSettings?: {
|
|
8
|
-
readFromCache?: boolean;
|
|
9
|
-
maxAgeMs?: number;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Enhanced HTTP fetch utility with validation and sensible defaults.
|
|
14
|
-
*
|
|
15
|
-
* Validates input parameters using Zod schema and sets default values:
|
|
16
|
-
* - Default HTTP method is "GET"
|
|
17
|
-
* - URL is required and must be valid
|
|
18
|
-
* - HTTP method must be one of the standard methods
|
|
19
|
-
*
|
|
20
|
-
* @param input - Request configuration object
|
|
21
|
-
* @returns Promise resolving to the HTTP response
|
|
22
|
-
* @throws ZodError if input validation fails
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* // Simple GET request (method defaults to "GET")
|
|
27
|
-
* const response = await creFetch({ url: "https://api.example.com/data" });
|
|
28
|
-
*
|
|
29
|
-
* // POST request with body and headers
|
|
30
|
-
* const response = await creFetch({
|
|
31
|
-
* url: "https://api.example.com/submit",
|
|
32
|
-
* method: "POST",
|
|
33
|
-
* headers: { "Content-Type": "application/json" },
|
|
34
|
-
* body: JSON.stringify({ key: "value" })
|
|
35
|
-
* });
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare const creFetch: (input: CreFetchRequest) => Promise<{
|
|
39
|
-
statusCode: number;
|
|
40
|
-
headers: {
|
|
41
|
-
[key: string]: string;
|
|
42
|
-
};
|
|
43
|
-
body: string;
|
|
44
|
-
}>;
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { cre } from '../../../cre';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
const httpMethodSchema = z.enum([
|
|
4
|
-
'GET',
|
|
5
|
-
'POST',
|
|
6
|
-
'PUT',
|
|
7
|
-
'DELETE',
|
|
8
|
-
'PATCH',
|
|
9
|
-
'HEAD',
|
|
10
|
-
'OPTIONS',
|
|
11
|
-
'TRACE',
|
|
12
|
-
'CONNECT',
|
|
13
|
-
]);
|
|
14
|
-
const cacheSettingsSchema = z
|
|
15
|
-
.object({
|
|
16
|
-
readFromCache: z.boolean().optional(),
|
|
17
|
-
maxAgeMs: z.number().int().optional(),
|
|
18
|
-
})
|
|
19
|
-
.optional();
|
|
20
|
-
const creFetchRequestSchema = z.object({
|
|
21
|
-
url: z.string().startsWith('http'),
|
|
22
|
-
method: httpMethodSchema.optional().default('GET'),
|
|
23
|
-
headers: z.record(z.string()).optional(),
|
|
24
|
-
body: z.string().optional(),
|
|
25
|
-
timeoutMs: z.number().int().positive().optional(),
|
|
26
|
-
cacheSettings: cacheSettingsSchema,
|
|
27
|
-
});
|
|
28
|
-
/**
|
|
29
|
-
* Enhanced HTTP fetch utility with validation and sensible defaults.
|
|
30
|
-
*
|
|
31
|
-
* Validates input parameters using Zod schema and sets default values:
|
|
32
|
-
* - Default HTTP method is "GET"
|
|
33
|
-
* - URL is required and must be valid
|
|
34
|
-
* - HTTP method must be one of the standard methods
|
|
35
|
-
*
|
|
36
|
-
* @param input - Request configuration object
|
|
37
|
-
* @returns Promise resolving to the HTTP response
|
|
38
|
-
* @throws ZodError if input validation fails
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* // Simple GET request (method defaults to "GET")
|
|
43
|
-
* const response = await creFetch({ url: "https://api.example.com/data" });
|
|
44
|
-
*
|
|
45
|
-
* // POST request with body and headers
|
|
46
|
-
* const response = await creFetch({
|
|
47
|
-
* url: "https://api.example.com/submit",
|
|
48
|
-
* method: "POST",
|
|
49
|
-
* headers: { "Content-Type": "application/json" },
|
|
50
|
-
* body: JSON.stringify({ key: "value" })
|
|
51
|
-
* });
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
export const creFetch = async (input) => {
|
|
55
|
-
const validatedInput = creFetchRequestSchema.parse(input);
|
|
56
|
-
const httpClient = new cre.capabilities.HTTPClient();
|
|
57
|
-
const resp = await httpClient.sendRequest(validatedInput).result();
|
|
58
|
-
return {
|
|
59
|
-
statusCode: resp.statusCode,
|
|
60
|
-
headers: resp.headers,
|
|
61
|
-
body: new TextDecoder().decode(resp.body),
|
|
62
|
-
};
|
|
63
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type Mode } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
2
|
-
type Params = {
|
|
3
|
-
capabilityId: string;
|
|
4
|
-
method: string;
|
|
5
|
-
mode: Mode;
|
|
6
|
-
payload: {
|
|
7
|
-
typeUrl: string;
|
|
8
|
-
value: Uint8Array;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
export declare const doRequestAsync: ({ capabilityId, method, mode, payload }: Params) => number;
|
|
12
|
-
export {};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { create, toBinary } from '@bufbuild/protobuf';
|
|
2
|
-
import { CapabilityRequestSchema, } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
-
import { hostBindings } from '../runtime/host-bindings';
|
|
4
|
-
import { getLastCallbackId, incrementCallbackId } from './capabilities/callback-id';
|
|
5
|
-
export const doRequestAsync = ({ capabilityId, method, mode, payload }) => {
|
|
6
|
-
const callbackId = getLastCallbackId(mode);
|
|
7
|
-
incrementCallbackId(mode);
|
|
8
|
-
const req = create(CapabilityRequestSchema, {
|
|
9
|
-
id: capabilityId,
|
|
10
|
-
method,
|
|
11
|
-
payload,
|
|
12
|
-
callbackId,
|
|
13
|
-
});
|
|
14
|
-
const reqBytes = toBinary(CapabilityRequestSchema, req);
|
|
15
|
-
hostBindings.callCapability(reqBytes);
|
|
16
|
-
return callbackId;
|
|
17
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { DonModeError, NodeModeError } from '../runtime/errors';
|
|
2
|
-
export const errorBoundary = (e) => {
|
|
3
|
-
// TODO: links should be configurable
|
|
4
|
-
if (e instanceof DonModeError || e instanceof NodeModeError) {
|
|
5
|
-
console.log(`
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
[${e.constructor.name}]: ${e.message}.
|
|
9
|
-
|
|
10
|
-
Learn more about mode switching here: https://documentation-preview-git-cre-priv-577744-chainlink-labs-devrel.vercel.app/cre/getting-started/part-2-fetching-data#step-2-understand-the-runinnodemode-pattern
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
`);
|
|
14
|
-
}
|
|
15
|
-
else if (e instanceof Error) {
|
|
16
|
-
console.log(e.message);
|
|
17
|
-
console.log(e.stack);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
console.log(e);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
export const withErrorBoundary = async (fn) => {
|
|
24
|
-
try {
|
|
25
|
-
await fn();
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
errorBoundary(e);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const getRequest: () => import("../../generated/sdk/v1alpha/sdk_pb").ExecuteRequest;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { fromBinary } from '@bufbuild/protobuf';
|
|
2
|
-
import { ExecuteRequestSchema } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
-
export const getRequest = () => {
|
|
4
|
-
const argsString = getWasiArgs();
|
|
5
|
-
const args = JSON.parse(argsString);
|
|
6
|
-
// SDK expects exactly 2 args:
|
|
7
|
-
// 1st is the script name
|
|
8
|
-
// 2nd is the base64 encoded request
|
|
9
|
-
if (args.length !== 2) {
|
|
10
|
-
throw new Error('Invalid request: must contain payload');
|
|
11
|
-
}
|
|
12
|
-
const base64Request = args[1];
|
|
13
|
-
const bytes = Buffer.from(base64Request, 'base64');
|
|
14
|
-
return fromBinary(ExecuteRequestSchema, bytes);
|
|
15
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LazyPromise is a utility class that implements the Promise interface
|
|
3
|
-
* but defers execution of the actual work until the promise is first accessed.
|
|
4
|
-
*
|
|
5
|
-
* This is useful when you want to return a promise immediately without
|
|
6
|
-
* starting the async work, but ensure the work begins as soon as someone
|
|
7
|
-
* tries to await, .then(), .catch(), or .finally() the promise.
|
|
8
|
-
*
|
|
9
|
-
* Example usage:
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const lazyPromise = new LazyPromise(() => {
|
|
12
|
-
* // This expensive operation won't start until the promise is accessed
|
|
13
|
-
* return fetch('/api/data');
|
|
14
|
-
* });
|
|
15
|
-
*
|
|
16
|
-
* // Promise is returned immediately, no work done yet
|
|
17
|
-
* // Work starts when you await it:
|
|
18
|
-
* const result = await lazyPromise;
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* This abstraction has been created for calling capabilities in a non-blocking way.
|
|
22
|
-
*/
|
|
23
|
-
export declare class LazyPromise<T> implements Promise<T> {
|
|
24
|
-
private readonly factory;
|
|
25
|
-
/** Tracks whether the factory function has been executed */
|
|
26
|
-
private started;
|
|
27
|
-
/** The actual promise that gets created when work starts */
|
|
28
|
-
private promise;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new LazyPromise
|
|
31
|
-
* @param factory - Function that returns the value or promise to resolve with
|
|
32
|
-
*/
|
|
33
|
-
constructor(factory: () => T | Promise<T>);
|
|
34
|
-
/**
|
|
35
|
-
* Ensures the factory function has been executed and the promise is created.
|
|
36
|
-
* This is called by all promise methods (.then, .catch, .finally) to start the work.
|
|
37
|
-
*/
|
|
38
|
-
private ensureStarted;
|
|
39
|
-
/**
|
|
40
|
-
* Attaches callbacks for the resolution and/or rejection of the Promise.
|
|
41
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
42
|
-
*/
|
|
43
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
44
|
-
/**
|
|
45
|
-
* Attaches a callback for only the rejection of the Promise.
|
|
46
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
47
|
-
*/
|
|
48
|
-
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult>;
|
|
49
|
-
/**
|
|
50
|
-
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected).
|
|
51
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
52
|
-
*/
|
|
53
|
-
finally(onfinally?: (() => void) | null): Promise<T>;
|
|
54
|
-
/**
|
|
55
|
-
* Required to make this behave like a native Promise.
|
|
56
|
-
* This allows instanceof checks and proper string representation.
|
|
57
|
-
*/
|
|
58
|
-
[Symbol.toStringTag]: string;
|
|
59
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LazyPromise is a utility class that implements the Promise interface
|
|
3
|
-
* but defers execution of the actual work until the promise is first accessed.
|
|
4
|
-
*
|
|
5
|
-
* This is useful when you want to return a promise immediately without
|
|
6
|
-
* starting the async work, but ensure the work begins as soon as someone
|
|
7
|
-
* tries to await, .then(), .catch(), or .finally() the promise.
|
|
8
|
-
*
|
|
9
|
-
* Example usage:
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const lazyPromise = new LazyPromise(() => {
|
|
12
|
-
* // This expensive operation won't start until the promise is accessed
|
|
13
|
-
* return fetch('/api/data');
|
|
14
|
-
* });
|
|
15
|
-
*
|
|
16
|
-
* // Promise is returned immediately, no work done yet
|
|
17
|
-
* // Work starts when you await it:
|
|
18
|
-
* const result = await lazyPromise;
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* This abstraction has been created for calling capabilities in a non-blocking way.
|
|
22
|
-
*/
|
|
23
|
-
export class LazyPromise {
|
|
24
|
-
factory;
|
|
25
|
-
/** Tracks whether the factory function has been executed */
|
|
26
|
-
started = false;
|
|
27
|
-
/** The actual promise that gets created when work starts */
|
|
28
|
-
promise;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new LazyPromise
|
|
31
|
-
* @param factory - Function that returns the value or promise to resolve with
|
|
32
|
-
*/
|
|
33
|
-
constructor(factory) {
|
|
34
|
-
this.factory = factory;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Ensures the factory function has been executed and the promise is created.
|
|
38
|
-
* This is called by all promise methods (.then, .catch, .finally) to start the work.
|
|
39
|
-
*/
|
|
40
|
-
ensureStarted() {
|
|
41
|
-
if (!this.started) {
|
|
42
|
-
this.started = true;
|
|
43
|
-
try {
|
|
44
|
-
const result = this.factory();
|
|
45
|
-
this.promise = Promise.resolve(result);
|
|
46
|
-
}
|
|
47
|
-
catch (err) {
|
|
48
|
-
this.promise = Promise.reject(err);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Attaches callbacks for the resolution and/or rejection of the Promise.
|
|
54
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
55
|
-
*/
|
|
56
|
-
then(onfulfilled, onrejected) {
|
|
57
|
-
this.ensureStarted();
|
|
58
|
-
return this.promise.then(onfulfilled, onrejected);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Attaches a callback for only the rejection of the Promise.
|
|
62
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
63
|
-
*/
|
|
64
|
-
catch(onrejected) {
|
|
65
|
-
this.ensureStarted();
|
|
66
|
-
return this.promise.catch(onrejected);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected).
|
|
70
|
-
* This triggers the lazy execution if it hasn't started yet.
|
|
71
|
-
*/
|
|
72
|
-
finally(onfinally) {
|
|
73
|
-
this.ensureStarted();
|
|
74
|
-
return this.promise.finally(onfinally);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Required to make this behave like a native Promise.
|
|
78
|
-
* This allows instanceof checks and proper string representation.
|
|
79
|
-
*/
|
|
80
|
-
[Symbol.toStringTag] = 'Promise';
|
|
81
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export declare class Source {
|
|
2
|
-
private s0;
|
|
3
|
-
private s1;
|
|
4
|
-
constructor(seed: bigint);
|
|
5
|
-
Seed(seed: bigint): void;
|
|
6
|
-
private splitMix64;
|
|
7
|
-
private next;
|
|
8
|
-
Int63(): bigint;
|
|
9
|
-
Uint32(): number;
|
|
10
|
-
Uint64(): bigint;
|
|
11
|
-
Int31(): number;
|
|
12
|
-
Float64(): number;
|
|
13
|
-
Int63n(n: bigint): bigint;
|
|
14
|
-
Int31n(n: number): number;
|
|
15
|
-
Intn(n: number): number;
|
|
16
|
-
Int(): number;
|
|
17
|
-
Float32(): number;
|
|
18
|
-
Perm(n: number): number[];
|
|
19
|
-
}
|
|
20
|
-
export declare class Rand {
|
|
21
|
-
private src;
|
|
22
|
-
constructor(seed: bigint);
|
|
23
|
-
Float64(): number;
|
|
24
|
-
Float32(): number;
|
|
25
|
-
Int63(): bigint;
|
|
26
|
-
Int63n(n: bigint): bigint;
|
|
27
|
-
Int31(): number;
|
|
28
|
-
Int31n(n: number): number;
|
|
29
|
-
Int(): number;
|
|
30
|
-
Intn(n: number): number;
|
|
31
|
-
Uint32(): number;
|
|
32
|
-
Uint64(): bigint;
|
|
33
|
-
Perm(n: number): number[];
|
|
34
|
-
Seed(seed: bigint): void;
|
|
35
|
-
}
|