diff --git a/examples/apollo_server/dfx.json b/examples/apollo_server/dfx.json index ab6f5bc5de..4b134be7b1 100644 --- a/examples/apollo_server/dfx.json +++ b/examples/apollo_server/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "http", "build": "npx azle apollo_server", "wasm": ".azle/apollo_server/apollo_server.wasm", "gzip": true diff --git a/examples/audio_recorder/dfx.json b/examples/audio_recorder/dfx.json index 914ff13815..f09d1d5548 100644 --- a/examples/audio_recorder/dfx.json +++ b/examples/audio_recorder/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "custom", "build": "npx azle audio_recorder", "wasm": ".azle/audio_recorder/audio_recorder.wasm", "gzip": true, diff --git a/examples/audio_recorder/src/index.did b/examples/audio_recorder/src/index.did index ba5448ffc2..afd76b31da 100644 --- a/examples/audio_recorder/src/index.did +++ b/examples/audio_recorder/src/index.did @@ -1,10 +1,30 @@ +type User = record { + id: principal; + createdAt: nat64; + recordingIds: vec principal; + username: text; +}; + +type Recording = record { + id: principal; + audio: blob; + createdAt: nat64; + name: text; + userId: principal; +}; + +type AudioRecorderError = variant { + RecordingDoesNotExist: principal; + UserDoesNotExist: principal; +}; + service: () -> { - createRecording: (vec nat8, text, principal) -> (variant {Ok:record {id:principal; audio:vec nat8; userId:principal; name:text; createdAt:nat64}; Err:variant {RecordingDoesNotExist:principal; UserDoesNotExist:principal}}); - createUser: (text) -> (record {id:principal; username:text; recordingIds:vec principal; createdAt:nat64}); - deleteRecording: (principal) -> (variant {Ok:record {id:principal; audio:vec nat8; userId:principal; name:text; createdAt:nat64}; Err:variant {RecordingDoesNotExist:principal; UserDoesNotExist:principal}}); - deleteUser: (principal) -> (variant {Ok:record {id:principal; username:text; recordingIds:vec principal; createdAt:nat64}; Err:variant {RecordingDoesNotExist:principal; UserDoesNotExist:principal}}); - readRecordingById: (principal) -> (opt record {id:principal; audio:vec nat8; userId:principal; name:text; createdAt:nat64}) query; - readRecordings: () -> (vec record {id:principal; audio:vec nat8; userId:principal; name:text; createdAt:nat64}) query; - readUserById: (principal) -> (opt record {id:principal; username:text; recordingIds:vec principal; createdAt:nat64}) query; - readUsers: () -> (vec record {id:principal; username:text; recordingIds:vec principal; createdAt:nat64}) query; + createUser: (text) -> (User); + readUsers: () -> (vec User) query; + readUserById: (principal) -> (opt User) query; + deleteUser: (principal) -> (variant { Ok: User; Err: AudioRecorderError }); + createRecording: (vec nat8, text, principal) -> (variant { Ok: Recording; Err: AudioRecorderError }); + readRecordings: () -> (vec Recording) query; + readRecordingById: (principal) -> (opt Recording) query; + deleteRecording: (principal) -> (variant { Ok: Recording; Err: AudioRecorderError }); } diff --git a/examples/complex_types/dfx.json b/examples/complex_types/dfx.json index dfc726a56a..4bead9c4e9 100644 --- a/examples/complex_types/dfx.json +++ b/examples/complex_types/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "custom", "build": "npx azle complex_types", "wasm": ".azle/complex_types/complex_types.wasm", "gzip": true, diff --git a/examples/complex_types/src/complex_types.did b/examples/complex_types/src/complex_types.did deleted file mode 100644 index 78130971a9..0000000000 --- a/examples/complex_types/src/complex_types.did +++ /dev/null @@ -1,75 +0,0 @@ -type Box = record { - id : text; - username : text; - threads : vec record { - id : text; - title : text; - author : Box; - posts : vec Post; - }; - posts : vec Post; - reactions : vec record { - id : text; - post : Post; - author : Box; - reactionType : variant { Fire; ThumbsDown; ThumbsUp }; - }; -}; -type Post = record { - id : text; - "text" : text; - author : record { - id : text; - username : text; - threads : vec record { - id : text; - title : text; - author : Box; - posts : vec Post; - }; - posts : vec Post; - reactions : vec record { - id : text; - post : Post; - author : Box; - reactionType : variant { Fire; ThumbsDown; ThumbsUp }; - }; - }; - thread : record { id : text; title : text; author : Box; posts : vec Post }; - reactions : vec record { - id : text; - post : Post; - author : Box; - reactionType : variant { Fire; ThumbsDown; ThumbsUp }; - }; -}; -type Reaction = record { - id : text; - post : Post; - author : Box; - reactionType : ReactionType; -}; -type ReactionType = variant { Fire; ThumbsDown; ThumbsUp }; -type Thread = record { - id : text; - title : text; - author : Box; - posts : vec Post; -}; -type User = record { - id : text; - username : text; - threads : vec Thread; - posts : vec Post; - reactions : vec Reaction; -}; -service : () -> { - createPost : (text, text, text, nat32) -> (Post); - createReaction : (text, text, ReactionType, nat32) -> (Reaction); - createThread : (text, text, nat32) -> (Thread); - createUser : (text, nat32) -> (User); - getAllPosts : (nat32) -> (vec Post) query; - getAllReactions : (nat32) -> (vec Reaction) query; - getAllThreads : (nat32) -> (vec Thread) query; - getAllUsers : (nat32) -> (vec User) query; -} \ No newline at end of file diff --git a/examples/complex_types/src/index.did b/examples/complex_types/src/index.did index d75ebcf4b6..367c03853c 100644 --- a/examples/complex_types/src/index.did +++ b/examples/complex_types/src/index.did @@ -1,58 +1,46 @@ -type rec_441 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_441; posts:vec rec_440}; posts:vec rec_440; reactions:vec record {id:text; post:rec_440; author:rec_441; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_443 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_443; posts:vec rec_440}; posts:vec rec_440; reactions:vec record {id:text; post:rec_440; author:rec_443; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_442 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_442; posts:vec rec_440}; posts:vec rec_440; reactions:vec record {id:text; post:rec_440; author:rec_442; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_440 = record {id:text; text:text; author:rec_441; thread:record {id:text; title:text; author:rec_443; posts:vec rec_440}; reactions:vec record {id:text; post:rec_440; author:rec_442; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_485 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_485; posts:vec rec_484}; posts:vec rec_484; reactions:vec record {id:text; post:rec_484; author:rec_485; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_487 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_487; posts:vec rec_484}; posts:vec rec_484; reactions:vec record {id:text; post:rec_484; author:rec_487; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_486 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_486; posts:vec rec_484}; posts:vec rec_484; reactions:vec record {id:text; post:rec_484; author:rec_486; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_484 = record {id:text; text:text; author:rec_485; thread:record {id:text; title:text; author:rec_487; posts:vec rec_484}; reactions:vec record {id:text; post:rec_484; author:rec_486; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_483 = record {id:text; text:text; author:rec_480; thread:record {id:text; title:text; author:rec_480; posts:vec rec_483}; reactions:vec record {id:text; post:rec_483; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_481 = record {id:text; text:text; author:rec_480; thread:record {id:text; title:text; author:rec_480; posts:vec rec_481}; reactions:vec record {id:text; post:rec_481; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_482 = record {id:text; text:text; author:rec_480; thread:record {id:text; title:text; author:rec_480; posts:vec rec_482}; reactions:vec record {id:text; post:rec_482; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_480 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_480; posts:vec rec_483}; posts:vec rec_481; reactions:vec record {id:text; post:rec_482; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_547 = record {id:text; text:text; author:rec_544; thread:record {id:text; title:text; author:rec_544; posts:vec rec_547}; reactions:vec record {id:text; post:rec_547; author:rec_544; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_545 = record {id:text; text:text; author:rec_544; thread:record {id:text; title:text; author:rec_544; posts:vec rec_545}; reactions:vec record {id:text; post:rec_545; author:rec_544; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_546 = record {id:text; text:text; author:rec_544; thread:record {id:text; title:text; author:rec_544; posts:vec rec_546}; reactions:vec record {id:text; post:rec_546; author:rec_544; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_544 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_544; posts:vec rec_547}; posts:vec rec_545; reactions:vec record {id:text; post:rec_546; author:rec_544; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_549 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_549; posts:vec rec_548}; posts:vec rec_548; reactions:vec record {id:text; post:rec_548; author:rec_549; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_551 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_551; posts:vec rec_548}; posts:vec rec_548; reactions:vec record {id:text; post:rec_548; author:rec_551; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_550 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_550; posts:vec rec_548}; posts:vec rec_548; reactions:vec record {id:text; post:rec_548; author:rec_550; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_548 = record {id:text; text:text; author:rec_549; thread:record {id:text; title:text; author:rec_551; posts:vec rec_548}; reactions:vec record {id:text; post:rec_548; author:rec_550; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_603 = record {id:text; text:text; author:rec_600; thread:record {id:text; title:text; author:rec_600; posts:vec rec_603}; reactions:vec record {id:text; post:rec_603; author:rec_600; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_601 = record {id:text; text:text; author:rec_600; thread:record {id:text; title:text; author:rec_600; posts:vec rec_601}; reactions:vec record {id:text; post:rec_601; author:rec_600; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_602 = record {id:text; text:text; author:rec_600; thread:record {id:text; title:text; author:rec_600; posts:vec rec_602}; reactions:vec record {id:text; post:rec_602; author:rec_600; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_600 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_600; posts:vec rec_603}; posts:vec rec_601; reactions:vec record {id:text; post:rec_602; author:rec_600; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_457 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_457; posts:vec rec_456}; posts:vec rec_456; reactions:vec record {id:text; post:rec_456; author:rec_457; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_459 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_459; posts:vec rec_456}; posts:vec rec_456; reactions:vec record {id:text; post:rec_456; author:rec_459; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_458 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_458; posts:vec rec_456}; posts:vec rec_456; reactions:vec record {id:text; post:rec_456; author:rec_458; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_456 = record {id:text; text:text; author:rec_457; thread:record {id:text; title:text; author:rec_459; posts:vec rec_456}; reactions:vec record {id:text; post:rec_456; author:rec_458; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_517 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_517; posts:vec rec_516}; posts:vec rec_516; reactions:vec record {id:text; post:rec_516; author:rec_517; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_519 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_519; posts:vec rec_516}; posts:vec rec_516; reactions:vec record {id:text; post:rec_516; author:rec_519; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_518 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_518; posts:vec rec_516}; posts:vec rec_516; reactions:vec record {id:text; post:rec_516; author:rec_518; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_516 = record {id:text; text:text; author:rec_517; thread:record {id:text; title:text; author:rec_519; posts:vec rec_516}; reactions:vec record {id:text; post:rec_516; author:rec_518; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_515 = record {id:text; text:text; author:rec_512; thread:record {id:text; title:text; author:rec_512; posts:vec rec_515}; reactions:vec record {id:text; post:rec_515; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_513 = record {id:text; text:text; author:rec_512; thread:record {id:text; title:text; author:rec_512; posts:vec rec_513}; reactions:vec record {id:text; post:rec_513; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_514 = record {id:text; text:text; author:rec_512; thread:record {id:text; title:text; author:rec_512; posts:vec rec_514}; reactions:vec record {id:text; post:rec_514; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_512 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_512; posts:vec rec_515}; posts:vec rec_513; reactions:vec record {id:text; post:rec_514; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_579 = record {id:text; text:text; author:rec_576; thread:record {id:text; title:text; author:rec_576; posts:vec rec_579}; reactions:vec record {id:text; post:rec_579; author:rec_576; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_577 = record {id:text; text:text; author:rec_576; thread:record {id:text; title:text; author:rec_576; posts:vec rec_577}; reactions:vec record {id:text; post:rec_577; author:rec_576; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_578 = record {id:text; text:text; author:rec_576; thread:record {id:text; title:text; author:rec_576; posts:vec rec_578}; reactions:vec record {id:text; post:rec_578; author:rec_576; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_576 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_576; posts:vec rec_579}; posts:vec rec_577; reactions:vec record {id:text; post:rec_578; author:rec_576; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_581 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_581; posts:vec rec_580}; posts:vec rec_580; reactions:vec record {id:text; post:rec_580; author:rec_581; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_583 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_583; posts:vec rec_580}; posts:vec rec_580; reactions:vec record {id:text; post:rec_580; author:rec_583; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_582 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_582; posts:vec rec_580}; posts:vec rec_580; reactions:vec record {id:text; post:rec_580; author:rec_582; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_580 = record {id:text; text:text; author:rec_581; thread:record {id:text; title:text; author:rec_583; posts:vec rec_580}; reactions:vec record {id:text; post:rec_580; author:rec_582; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_619 = record {id:text; text:text; author:rec_616; thread:record {id:text; title:text; author:rec_616; posts:vec rec_619}; reactions:vec record {id:text; post:rec_619; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_617 = record {id:text; text:text; author:rec_616; thread:record {id:text; title:text; author:rec_616; posts:vec rec_617}; reactions:vec record {id:text; post:rec_617; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_618 = record {id:text; text:text; author:rec_616; thread:record {id:text; title:text; author:rec_616; posts:vec rec_618}; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; -type rec_616 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_616; posts:vec rec_619}; posts:vec rec_617; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; +type Post = record { + id: text; + author: User; + reactions: vec Reaction; + text: text; + thread: Thread; +}; + +type Reaction = record { + id: text; + author: User; + post: Post; + reactionType: ReactionType; +}; + +type ReactionType = variant { + Fire; + ThumbsUp; + ThumbsDown; +}; + +type Thread = record { + id: text; + author: User; + posts: vec Post; + title: text; +}; + +type User = record { + id: text; + posts: vec Post; + reactions: vec Reaction; + threads: vec Thread; + username: text; +}; + service: () -> { - createPost: (text, text, text, nat32) -> (rec_440); - createReaction: (text, text, variant {Fire; ThumbsDown; ThumbsUp}, nat32) -> (record {id:text; post:rec_484; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}); - createThread: (text, text, nat32) -> (record {id:text; title:text; author:rec_544; posts:vec rec_548}); - createUser: (text, nat32) -> (rec_600); - getAllPosts: (nat32) -> (vec rec_456) query; - getAllReactions: (nat32) -> (vec record {id:text; post:rec_516; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) query; - getAllThreads: (nat32) -> (vec record {id:text; title:text; author:rec_576; posts:vec rec_580}) query; - getAllUsers: (nat32) -> (vec rec_616) query; + createPost: (text, text, text, nat32) -> (Post); + getAllPosts: (nat32) -> (vec Post) query; + createReaction: (text, text, ReactionType, nat32) -> (Reaction); + getAllReactions: (nat32) -> (vec Reaction) query; + createThread: (text, text, nat32) -> (Thread); + getAllThreads: (nat32) -> (vec Thread) query; + createUser: (text, nat32) -> (User); + getAllUsers: (nat32) -> (vec User) query; } diff --git a/examples/ethers/dfx.json b/examples/ethers/dfx.json index 4b6608c675..1b57d97a49 100644 --- a/examples/ethers/dfx.json +++ b/examples/ethers/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "http", "build": "npx azle ethers", "wasm": ".azle/ethers/ethers.wasm", "gzip": true diff --git a/examples/express/dfx.json b/examples/express/dfx.json index 75a72bfa83..0992547e5c 100644 --- a/examples/express/dfx.json +++ b/examples/express/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/backend/index.ts", "candid": "src/backend/index.did", + "candid_gen": "http", "build": "npx azle express", "wasm": ".azle/express/express.wasm", "gzip": true, diff --git a/examples/fs/dfx.json b/examples/fs/dfx.json index 458071fe63..e97f33592b 100644 --- a/examples/fs/dfx.json +++ b/examples/fs/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "http", "build": "npx azle fs", "wasm": ".azle/fs/fs.wasm", "gzip": true diff --git a/examples/hello_world/dfx.json b/examples/hello_world/dfx.json index a8c0b0c838..8080e60bbd 100644 --- a/examples/hello_world/dfx.json +++ b/examples/hello_world/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/backend/index.ts", "candid": "src/backend/index.did", + "candid_gen": "http", "build": "npx azle backend", "wasm": ".azle/backend/backend.wasm", "gzip": true, diff --git a/examples/ic_evm_rpc/dfx.json b/examples/ic_evm_rpc/dfx.json index 24af2dc80b..7460cf656b 100644 --- a/examples/ic_evm_rpc/dfx.json +++ b/examples/ic_evm_rpc/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/server.ts", "candid": "src/server.did", + "candid_gen": "http", "build": "npx azle server", "wasm": ".azle/server/server.wasm", "gzip": true, diff --git a/examples/simple_user_accounts/dfx.json b/examples/simple_user_accounts/dfx.json index fc75af3de7..7843a39151 100644 --- a/examples/simple_user_accounts/dfx.json +++ b/examples/simple_user_accounts/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "custom", "build": "npx azle simple_user_accounts", "wasm": ".azle/simple_user_accounts/simple_user_accounts.wasm", "gzip": true, diff --git a/examples/simple_user_accounts/src/index.did b/examples/simple_user_accounts/src/index.did index 1fe0dd0102..14a6242fba 100644 --- a/examples/simple_user_accounts/src/index.did +++ b/examples/simple_user_accounts/src/index.did @@ -1,5 +1,10 @@ +type User = record { + id: text; + username: text; +}; + service: () -> { - getUserById: (text) -> (opt record {id:text; username:text}) query; - getAllUsers: () -> (vec record {id:text; username:text}) query; - createUser: (text) -> (record {id:text; username:text}); + createUser: (text) -> (User); + getAllUsers: () -> (vec User) query; + getUserById: (text) -> (opt User) query; } diff --git a/examples/sqlite/dfx.json b/examples/sqlite/dfx.json index fb6a441c0f..17a154818c 100644 --- a/examples/sqlite/dfx.json +++ b/examples/sqlite/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "http", "build": "npx azle sqlite", "wasm": ".azle/sqlite/sqlite.wasm", "gzip": true diff --git a/examples/web_assembly/dfx.json b/examples/web_assembly/dfx.json index 7a664221d6..a0d874cc62 100644 --- a/examples/web_assembly/dfx.json +++ b/examples/web_assembly/dfx.json @@ -4,6 +4,7 @@ "type": "custom", "main": "src/index.ts", "candid": "src/index.did", + "candid_gen": "http", "build": "npx azle web_assembly", "wasm": ".azle/web_assembly/web_assembly.wasm", "gzip": true diff --git a/src/compiler/index.ts b/src/compiler/index.ts index 15714d2544..8d096c36b0 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -15,6 +15,8 @@ import { compileTypeScriptToJavaScript } from './compile_typescript_code'; import { Err, ok } from './utils/result'; import { AzleError, + CandidGen, + CanisterMethods, CompilerInfo, JSCanisterConfig, Toml, @@ -32,11 +34,6 @@ import { createHash } from 'crypto'; azle(); async function azle() { - if (process.argv[2] === 'new') { - generateNewAzleProject(azleVersion); - return; - } - const stdioType = getStdIoType(); const dockerfileHash = getDockerfileHash(); @@ -59,55 +56,14 @@ async function azle() { wasmedgeQuickJsName ); - if (process.argv[2] === 'dockerfile-hash') { - execSync(`echo -n "${dockerfileHash}"`, { - stdio: 'inherit' - }); - return; - } - - if (process.argv[2] === 'clean') { - rmSync(GLOBAL_AZLE_CONFIG_DIR, { - recursive: true, - force: true - }); - - console.info(`~/.config/azle directory deleted`); - - rmSync('.azle', { - recursive: true, - force: true - }); - - console.info(`.azle directory deleted`); - - execSync( - `podman stop $(podman ps --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } - ); - - console.info(`azle containers stopped`); - - execSync( - `podman rm $(podman ps -a --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } - ); - - console.info(`azle containers removed`); - - execSync( - `podman image rm $(podman images --filter "reference=${dockerImagePrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } - ); - - console.info(`azle images removed`); + const commandExecuted = handleCli( + stdioType, + dockerfileHash, + dockerContainerPrefix, + dockerImagePrefix + ); + if (commandExecuted === true) { return; } @@ -124,106 +80,13 @@ async function azle() { mkdirSync(GLOBAL_AZLE_CONFIG_DIR, { recursive: true }); mkdirSync('.azle', { recursive: true }); - const imageHasBeenLoaded = hasImageBeenLoaded( + prepareDockerImage( + stdioType, dockerImageName, - stdioType - ); - - if (process.env.AZLE_USE_DOCKERFILE === 'true') { - try { - if (!imageHasBeenLoaded) { - if (existsSync(dockerImagePathTar)) { - console.info(yellow(`\nLoading image...\n`)); - - execSync(`podman load -i ${dockerImagePathTar}`, { - stdio: 'inherit' - }); - } else if (existsSync(dockerImagePathTarGz)) { - console.info(yellow(`\nLoading image...\n`)); - - execSync(`podman load -i ${dockerImagePathTarGz}`, { - stdio: 'inherit' - }); - } else { - throw new Error( - `${dockerImagePathTar} or ${dockerImagePathTarGz} does not exist` - ); - } - } - } catch (error) { - console.info(yellow(`\nBuilding image...\n`)); - - execSync( - `podman build -f ${__dirname}/Dockerfile -t ${dockerImageName} ${__dirname}`, - { - stdio: 'inherit' - } - ); - - console.info(yellow(`\nSaving image...\n`)); - - execSync( - `podman save -o ${dockerImagePathTar} ${dockerImageName}`, - { - stdio: 'inherit' - } - ); - - console.info(yellow(`\nCompiling...`)); - } - } else { - try { - if (!imageHasBeenLoaded) { - if (existsSync(dockerImagePathTar)) { - console.info(yellow(`\nLoading image...\n`)); - - execSync(`podman load -i ${dockerImagePathTar}`, { - stdio: 'inherit' - }); - } else if (existsSync(dockerImagePathTarGz)) { - console.info(yellow(`\nLoading image...\n`)); - - execSync(`podman load -i ${dockerImagePathTarGz}`, { - stdio: 'inherit' - }); - } else { - throw new Error( - `${dockerImagePathTar} or ${dockerImagePathTarGz} does not exist` - ); - } - } - } catch (error) { - console.info(yellow(`\nDownloading image...\n`)); - - execSync( - `curl -L https://github.com/demergent-labs/azle/releases/download/${azleVersion}/${dockerImageName}.tar.gz -o ${dockerImagePathTarGz}`, - { - stdio: 'inherit' - } - ); - - console.info(yellow(`\nLoading image...\n`)); - - execSync(`podman load -i ${dockerImagePathTarGz}`, { - stdio: 'inherit' - }); - - console.info(yellow(`\nCompiling...`)); - } - } - - execSync( - `podman inspect ${dockerContainerName} || podman create --name ${dockerContainerName} ${dockerImageName} tail -f /dev/null`, - { stdio: stdioType } - ); - - execSync(`podman start ${dockerContainerName}`, { - stdio: stdioType - }); - - execSync( - `podman cp ${dockerContainerName}:/wasmedge-quickjs ${wasmedgeQuickJsPath}`, - { stdio: stdioType } + dockerImagePathTar, + dockerImagePathTarGz, + dockerContainerName, + wasmedgeQuickJsPath ); const compilationResult = compileTypeScriptToJavaScript( @@ -286,65 +149,328 @@ async function azle() { ); } - // TODO a lot of this file writing and compiler_info.json - // TODO stuff is repeated which is messy and bad of course - writeFileSync(`${canisterPath}/canister/src/candid.did`, ''); // This is for the Rust canister to have access to the candid file - const envVars = getEnvVars(canisterConfig); - const compilerInfo0: CompilerInfo = { - canister_methods: { - candid: '', - queries: [], - updates: [], - callbacks: {} - }, - env_vars: envVars - }; - - const compilerInfoPath0 = join( + const compilerInfoPath = join( canisterPath, 'canister', 'src', 'compiler_info.json' ); - // TODO why not just write the dfx.json file here as well? - writeFileSync(compilerInfoPath0, JSON.stringify(compilerInfo0)); - - compileRustCode(dockerContainerName, canisterName, stdioType); - - const { candid, canisterMethods } = - generateCandidAndCanisterMethods( - `${canisterPath}/${canisterName}.wasm` - ); + const { candid, canisterMethods } = getCandidAndCanisterMethods( + canisterConfig.candid_gen, + canisterPath, + candidPath, + compilerInfoPath, + dockerContainerName, + canisterName, + stdioType, + envVars + ); writeFileSync(candidPath, candid); // This is for the dfx.json candid property - writeFileSync(`${canisterPath}/canister/src/candid.did`, candid); // This is for the Rust canister to have access to the candid file const compilerInfo: CompilerInfo = { - // TODO The spread is because canisterMethods is a function with properties + // The spread is because canisterMethods is a function with properties canister_methods: { ...canisterMethods - }, // TODO we should probably just grab the props out that we need + }, env_vars: envVars }; - const compilerInfoPath = join( + compileRustCodeWithCompilerInfo( canisterPath, - 'canister', - 'src', - 'compiler_info.json' + candid, + compilerInfoPath, + compilerInfo, + dockerContainerName, + canisterName, + stdioType ); + } + ); - // TODO why not just write the dfx.json file here as well? - writeFileSync(compilerInfoPath, JSON.stringify(compilerInfo)); + logSuccess(canisterName); +} + +function handleCli( + stdioType: IOType, + dockerfileHash: string, + dockerContainerPrefix: string, + dockerImagePrefix: string +): boolean { + const commandName = process.argv[2]; - compileRustCode(dockerContainerName, canisterName, stdioType); + if (commandName === 'new') { + generateNewAzleProject(azleVersion); + + return true; + } + + if (commandName === 'dockerfile-hash') { + execSync(`echo -n "${dockerfileHash}"`, { + stdio: 'inherit' + }); + + return true; + } + + if (commandName === 'clean') { + rmSync(GLOBAL_AZLE_CONFIG_DIR, { + recursive: true, + force: true + }); + + console.info(`~/.config/azle directory deleted`); + + rmSync('.azle', { + recursive: true, + force: true + }); + + console.info(`.azle directory deleted`); + + execSync( + `podman stop $(podman ps --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, + { + stdio: stdioType + } + ); + + console.info(`azle containers stopped`); + + execSync( + `podman rm $(podman ps -a --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, + { + stdio: stdioType + } + ); + + console.info(`azle containers removed`); + + execSync( + `podman image rm $(podman images --filter "reference=${dockerImagePrefix}" --format "{{.ID}}") || true`, + { + stdio: stdioType + } + ); + + console.info(`azle images removed`); + + return true; + } + + return false; +} + +function prepareDockerImage( + stdioType: IOType, + dockerImageName: string, + dockerImagePathTar: string, + dockerImagePathTarGz: string, + dockerContainerName: string, + wasmedgeQuickJsPath: string +) { + const imageHasBeenLoaded = hasImageBeenLoaded(dockerImageName, stdioType); + + if (process.env.AZLE_USE_DOCKERFILE === 'true') { + try { + if (!imageHasBeenLoaded) { + if (existsSync(dockerImagePathTar)) { + console.info(yellow(`\nLoading image...\n`)); + + execSync(`podman load -i ${dockerImagePathTar}`, { + stdio: 'inherit' + }); + } else if (existsSync(dockerImagePathTarGz)) { + console.info(yellow(`\nLoading image...\n`)); + + execSync(`podman load -i ${dockerImagePathTarGz}`, { + stdio: 'inherit' + }); + } else { + throw new Error( + `${dockerImagePathTar} or ${dockerImagePathTarGz} does not exist` + ); + } + } + } catch (error) { + console.info(yellow(`\nBuilding image...\n`)); + + execSync( + `podman build -f ${__dirname}/Dockerfile -t ${dockerImageName} ${__dirname}`, + { + stdio: 'inherit' + } + ); + + console.info(yellow(`\nSaving image...\n`)); + + execSync( + `podman save -o ${dockerImagePathTar} ${dockerImageName}`, + { + stdio: 'inherit' + } + ); + + console.info(yellow(`\nCompiling...`)); + } + } else { + try { + if (!imageHasBeenLoaded) { + if (existsSync(dockerImagePathTar)) { + console.info(yellow(`\nLoading image...\n`)); + + execSync(`podman load -i ${dockerImagePathTar}`, { + stdio: 'inherit' + }); + } else if (existsSync(dockerImagePathTarGz)) { + console.info(yellow(`\nLoading image...\n`)); + + execSync(`podman load -i ${dockerImagePathTarGz}`, { + stdio: 'inherit' + }); + } else { + throw new Error( + `${dockerImagePathTar} or ${dockerImagePathTarGz} does not exist` + ); + } + } + } catch (error) { + console.info(yellow(`\nDownloading image...\n`)); + + execSync( + `curl -L https://github.com/demergent-labs/azle/releases/download/${azleVersion}/${dockerImageName}.tar.gz -o ${dockerImagePathTarGz}`, + { + stdio: 'inherit' + } + ); + + console.info(yellow(`\nLoading image...\n`)); + + execSync(`podman load -i ${dockerImagePathTarGz}`, { + stdio: 'inherit' + }); + + console.info(yellow(`\nCompiling...`)); } + } + + execSync( + `podman inspect ${dockerContainerName} || podman create --name ${dockerContainerName} ${dockerImageName} tail -f /dev/null`, + { stdio: stdioType } ); - logSuccess(canisterName); + execSync(`podman start ${dockerContainerName}`, { + stdio: stdioType + }); + + execSync( + `podman cp ${dockerContainerName}:/wasmedge-quickjs ${wasmedgeQuickJsPath}`, + { stdio: stdioType } + ); +} + +function compileRustCodeWithCompilerInfo( + canisterPath: string, + candid: string, + compilerInfoPath: string, + compilerInfo: CompilerInfo, + dockerContainerName: string, + canisterName: string, + stdioType: IOType +) { + // This is for the Rust canister to have access to the candid file + writeFileSync(`${canisterPath}/canister/src/candid.did`, candid); + + // TODO why not just write the dfx.json file here as well? + writeFileSync(compilerInfoPath, JSON.stringify(compilerInfo)); + + compileRustCode(dockerContainerName, canisterName, stdioType); +} + +function getCandidAndCanisterMethods( + candidGen: CandidGen = 'automatic', + canisterPath: string, + candidPath: string, + compilerInfoPath: string, + dockerContainerName: string, + canisterName: string, + stdioType: IOType, + envVars: [string, string][] +): { + candid: string; + canisterMethods: CanisterMethods; +} { + if (candidGen === 'automatic' || candidGen === 'custom') { + const customCandid = + candidGen === 'custom' ? readFileSync(candidPath).toString() : ''; + + const compilerInfo: CompilerInfo = { + canister_methods: { + candid: customCandid, + queries: [], + updates: [], + callbacks: {} + }, + env_vars: envVars + }; + + compileRustCodeWithCompilerInfo( + canisterPath, + customCandid, + compilerInfoPath, + compilerInfo, + dockerContainerName, + canisterName, + stdioType + ); + + const { candid, canisterMethods } = generateCandidAndCanisterMethods( + `${canisterPath}/${canisterName}.wasm` + ); + + return { + candid: candidGen === 'custom' ? customCandid : candid, + canisterMethods + }; + } + + if (candidGen === 'http') { + if (require.main?.path === undefined) { + throw new Error(`require.main?.path must be defined`); + } + + const candid = readFileSync( + join(require.main?.path, 'server.did') + ).toString(); + + const canisterMethods: CanisterMethods = { + candid, + queries: [ + { + name: 'http_request', + composite: true + } + ], + updates: [ + { + name: 'http_request_update' + } + ], + init: { name: 'init' }, + post_upgrade: { name: 'postUpgrade' }, + callbacks: {} + }; + + return { + candid, + canisterMethods + }; + } + + throw new Error(`Unsupported candid_gen: ${candidGen}`); } function compilationErrorToAzleErrorResult(error: unknown): Err { diff --git a/src/compiler/utils/types.ts b/src/compiler/utils/types.ts index 2fc87dbedb..f6ea6cc0ee 100644 --- a/src/compiler/utils/types.ts +++ b/src/compiler/utils/types.ts @@ -20,6 +20,7 @@ export type JSCanisterConfig = Readonly<{ build: string; build_assets?: string; candid: string; + candid_gen?: CandidGen; wasm: string; env?: string[]; opt_level?: OptLevel; @@ -28,6 +29,8 @@ export type JSCanisterConfig = Readonly<{ export type OptLevel = '0' | '1' | '2' | '3' | '4'; +export type CandidGen = 'automatic' | 'custom' | 'http'; + export type CompilerInfo = { canister_methods: CanisterMethods; env_vars: [string, string][];