Skip to content
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

Guard functions #1855

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/guard_functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/guard_functions/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "guard_functions_end_to_end_test_functional_syntax",
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "jest"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/generate_candid_and_canister_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function generateCandidAndCanisterMethods(wasmFilePath: string): {

const memory = new Uint8Array((wasmInstance.exports.memory as any).buffer);

let candidBytes = [];
let candidBytes: number[] = [];
let i = candidPointer;
while (memory[i] !== 0) {
candidBytes.push(memory[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type CanisterMethods = {
export type CanisterMethod = {
name: string;
composite?: boolean;
guard?: () => void;
guard_name?: string;
};

export type Plugin = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CanisterMethods = {
type CanisterMethod = {
name: string;
composite?: boolean;
guard?: () => void;
guard_name?: string;
};

declare global {
Expand Down
26 changes: 26 additions & 0 deletions src/lib/stable/guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { handleUncaughtError } from './error';

// TODO guards can be asynchronous right? At least update guards
// TODO query guards should be able to be composite as well
// TODO seems like in ic-cdk they cannot be asynchronous
// TODO but seems like ICP would allow them to be
export function createGlobalGuard(
guard: (() => any) | undefined,
guardedMethodName: string
): string | undefined {
if (guard === undefined) {
return undefined;
}

const guardName = `_azleGuard_${guardedMethodName}`;

globalThis._azleGuardFunctions[guardName] = () => {
try {
guard();
} catch (error) {
handleUncaughtError(error);
}
};

return guardName;
}
9 changes: 7 additions & 2 deletions src/lib/stable/query.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IDL } from '@dfinity/candid';

import { executeWithCandidSerde } from './execute_with_candid_serde';
import { createGlobalGuard } from './guard';

export function query(
paramIdls: IDL.Type[],
returnIdl?: IDL.Type,
options?: {
composite?: boolean;
manual?: boolean;
guard?: () => void; // TODO can guard functions be async?
}
): MethodDecorator {
return <T>(
Expand All @@ -32,8 +34,11 @@ export function query(

globalThis._azleCanisterMethods.queries.push({
name: propertyKey as string,
composite: options?.composite ?? false
// TODO implement guard
composite: options?.composite ?? false,
guard_name:
options?.guard === undefined
? undefined
: createGlobalGuard(options?.guard, propertyKey as string)
});

globalThis._azleCanisterMethods.callbacks[propertyKey as string] =
Expand Down
9 changes: 7 additions & 2 deletions src/lib/stable/update.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { IDL } from '@dfinity/candid';

import { executeWithCandidSerde } from './execute_with_candid_serde';
import { createGlobalGuard } from './guard';

export function update(
paramIdls: IDL.Type[],
returnIdl?: IDL.Type,
options?: {
manual?: boolean;
guard?: () => void; // TODO can guard functions be async?
}
): MethodDecorator {
return <T>(
Expand All @@ -30,8 +32,11 @@ export function update(
descriptor.value = methodCallback as any;

globalThis._azleCanisterMethods.updates.push({
name: propertyKey as string
// TODO implement guard
name: propertyKey as string,
guard_name:
options?.guard === undefined
? undefined
: createGlobalGuard(options?.guard, propertyKey as string)
});

globalThis._azleCanisterMethods.callbacks[propertyKey as string] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
14 changes: 14 additions & 0 deletions tests/end_to_end/candid_rpc/class_syntax/guard_functions/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"canisters": {
"guard_functions": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/guard_functions",
"node_compatibility": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading
Loading