From f881868715d80ce3387a5e8f940f696c7797500f Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 13 Sep 2023 12:38:36 -0500 Subject: [PATCH] http_counter example updated and passing --- .github/workflows/test.yml | 2 +- .../http_counter/package-lock.json | 52 ++++---- .../motoko_examples/http_counter/package.json | 6 +- .../http_counter/src/index.did | 50 ++++---- .../motoko_examples/http_counter/src/index.ts | 111 +++++++++--------- 5 files changed, 109 insertions(+), 112 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index edcbfa2462..693ea48975 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -97,7 +97,6 @@ # "examples/guard_functions", # "examples/ic_api", # "examples/inline_types", -# "examples/motoko_examples/http_counter", # "examples/motoko_examples/persistent-storage", # "examples/motoko_examples/phone-book", # "examples/motoko_examples/simple-to-do", @@ -177,6 +176,7 @@ jobs: "examples/motoko_examples/factorial", "examples/motoko_examples/hello", "examples/motoko_examples/hello-world", + "examples/motoko_examples/http_counter", "examples/motoko_examples/minimal-counter-dapp", "examples/motoko_examples/quicksort", "examples/notify_raw", diff --git a/examples/motoko_examples/http_counter/package-lock.json b/examples/motoko_examples/http_counter/package-lock.json index 3d7e075128..61676e7817 100644 --- a/examples/motoko_examples/http_counter/package-lock.json +++ b/examples/motoko_examples/http_counter/package-lock.json @@ -8,12 +8,12 @@ "name": "http_counter", "version": "0.1.0", "dependencies": { - "azle": "0.17.1", - "encode-utf8": "2.0.0" + "azle": "0.17.1" }, "devDependencies": { "@dfinity/agent": "^0.19.0", - "ts-node": "^10.9.1" + "ts-node": "^10.9.1", + "typescript": "^5.2.2" } }, "node_modules/@cspotcode/source-map-consumer": { @@ -463,6 +463,18 @@ } } }, + "node_modules/azle/node_modules/typescript": { + "version": "4.4.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", + "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, "node_modules/base64-arraybuffer": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.2.0.tgz", @@ -568,14 +580,6 @@ "node": ">=0.3.1" } }, - "node_modules/encode-utf8": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/encode-utf8/-/encode-utf8-2.0.0.tgz", - "integrity": "sha512-3EyMFxZj1/7oMotElDQUEQcP7N4TIe1aJ0m1uBDoyQ8I2LBHhBsXx8P3KsPbqNlGzG+NYxFwEauUwMPHZg3YDQ==", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - } - }, "node_modules/esbuild": { "version": "0.14.25", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.14.25.tgz", @@ -1086,15 +1090,16 @@ } }, "node_modules/typescript": { - "version": "4.4.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", - "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=14.17" } }, "node_modules/universalify": { @@ -1384,6 +1389,11 @@ "make-error": "^1.1.1", "yn": "3.1.1" } + }, + "typescript": { + "version": "4.4.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", + "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==" } } }, @@ -1457,11 +1467,6 @@ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==" }, - "encode-utf8": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/encode-utf8/-/encode-utf8-2.0.0.tgz", - "integrity": "sha512-3EyMFxZj1/7oMotElDQUEQcP7N4TIe1aJ0m1uBDoyQ8I2LBHhBsXx8P3KsPbqNlGzG+NYxFwEauUwMPHZg3YDQ==" - }, "esbuild": { "version": "0.14.25", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.14.25.tgz", @@ -1724,9 +1729,10 @@ } }, "typescript": { - "version": "4.4.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", - "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==" + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true }, "universalify": { "version": "2.0.0", diff --git a/examples/motoko_examples/http_counter/package.json b/examples/motoko_examples/http_counter/package.json index 663d4d453b..7a2b6207f7 100644 --- a/examples/motoko_examples/http_counter/package.json +++ b/examples/motoko_examples/http_counter/package.json @@ -13,11 +13,11 @@ "test": "AZLE_REBUILD=true ts-node --transpile-only --ignore=false test/test.ts" }, "dependencies": { - "azle": "0.17.1", - "encode-utf8": "2.0.0" + "azle": "0.17.1" }, "devDependencies": { "@dfinity/agent": "^0.19.0", - "ts-node": "^10.9.1" + "ts-node": "^10.9.1", + "typescript": "^5.2.2" } } diff --git a/examples/motoko_examples/http_counter/src/index.did b/examples/motoko_examples/http_counter/src/index.did index 7c58c5617d..b04d5a154a 100644 --- a/examples/motoko_examples/http_counter/src/index.did +++ b/examples/motoko_examples/http_counter/src/index.did @@ -1,28 +1,22 @@ -type CallbackStrategy = record { - token : Token; - callback : func (Token) -> (StreamingCallbackHttpResponse) query; -}; -type HttpRequest = record { - url : text; - method : text; - body : vec nat8; - headers : vec record { text; text }; -}; -type HttpResponse = record { - body : vec nat8; - headers : vec record { text; text }; - upgrade : opt bool; - streaming_strategy : opt StreamingStrategy; - status_code : nat16; -}; -type StreamingCallbackHttpResponse = record { - token : opt Token; - body : vec nat8; -}; -type StreamingStrategy = variant { Callback : CallbackStrategy }; -type Token = record { arbitrary_data : text }; -service : () -> { - http_request : (HttpRequest) -> (HttpResponse) query; - http_request_update : (HttpRequest) -> (HttpResponse); - http_streaming : (Token) -> (StreamingCallbackHttpResponse) query; -} \ No newline at end of file +type rec_0 = record {url:text; method:text; body:vec nat8; headers:vec record {text; text}}; +type rec_6 = record {arbitrary_data:text}; +type rec_5 = record {arbitrary_data:text}; +type rec_4 = record {token:opt rec_5; body:vec nat8}; +type rec_3 = record {token:rec_6; callback:func (text) -> (rec_4) query}; +type rec_2 = variant {Callback:rec_3}; +type rec_1 = record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt rec_2; status_code:nat16}; +type rec_7 = record {url:text; method:text; body:vec nat8; headers:vec record {text; text}}; +type rec_13 = record {arbitrary_data:text}; +type rec_12 = record {arbitrary_data:text}; +type rec_11 = record {token:opt rec_12; body:vec nat8}; +type rec_10 = record {token:rec_13; callback:func (text) -> (rec_11) query}; +type rec_9 = variant {Callback:rec_10}; +type rec_8 = record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt rec_9; status_code:nat16}; +type rec_14 = record {arbitrary_data:text}; +type rec_16 = record {arbitrary_data:text}; +type rec_15 = record {token:opt rec_16; body:vec nat8}; +service: () -> { + http_request: (rec_0) -> (rec_1) query; + http_request_update: (rec_7) -> (rec_8); + http_streaming: (rec_14) -> (rec_15) query; +} diff --git a/examples/motoko_examples/http_counter/src/index.ts b/examples/motoko_examples/http_counter/src/index.ts index fbc746b55c..cb61abe26b 100644 --- a/examples/motoko_examples/http_counter/src/index.ts +++ b/examples/motoko_examples/http_counter/src/index.ts @@ -1,6 +1,7 @@ import { blob, ic, + init, nat, nat16, Opt, @@ -11,19 +12,21 @@ import { Variant, Vec, update, - record, text, candid, func, - variant, Some, None, bool, Service } from 'azle'; -import encodeUtf8 from 'encode-utf8'; -@record +class Token extends Record { + // add whatever fields you'd like + @candid(text) + arbitrary_data: text; +} + class StreamingCallbackHttpResponse extends Record { @candid(blob) body: blob; @@ -32,14 +35,9 @@ class StreamingCallbackHttpResponse extends Record { token: Opt; } -@record -class Token extends Record { - // add whatever fields you'd like - @candid(text) - arbitrary_data: text; -} +@func([text], StreamingCallbackHttpResponse, 'query') +class Callback {} -@record class CallbackStrategy extends Record { @candid(Callback) callback: Callback; @@ -48,10 +46,6 @@ class CallbackStrategy extends Record { token: Token; } -@func([text], StreamingCallbackHttpResponse, 'query') -class Callback {} - -@variant class StreamingStrategy extends Variant { @candid(CallbackStrategy) Callback?: CallbackStrategy; @@ -60,7 +54,6 @@ class StreamingStrategy extends Variant { type HeaderField = [text, text]; const HeaderField = Tuple(text, text); -@record class HttpResponse extends Record { @candid(nat16) status_code: nat16; @@ -78,7 +71,6 @@ class HttpResponse extends Record { upgrade: Opt; } -@record class HttpRequest extends Record { @candid(text) method: text; @@ -93,22 +85,14 @@ class HttpRequest extends Record { body: blob; } -let stableStorage = new StableBTreeMap(0, 25, 1_000); - -stableStorage.insert('counter', 0n); - -function isGzip(x: HeaderField): boolean { - return ( - x[0].toLowerCase() === 'accept-encoding' && - x[1].toLowerCase().includes('gzip') - ); -} +export default class extends Service { + stableStorage = new StableBTreeMap(text, nat, 0); -function encode(string: string): blob { - return new Uint8Array(encodeUtf8(string)); -} + @init([]) + init() { + this.stableStorage.insert('counter', 0n); + } -export default class extends Service { @query([HttpRequest], HttpResponse) http_request(req: HttpRequest): HttpResponse { console.log('Hello from http_request'); @@ -131,15 +115,17 @@ export default class extends Service { upgrade: Some(false) }; } + + const counterOpt = this.stableStorage.get('counter'); + const counter = + counterOpt.length === 0 + ? ic.trap('counter does not exist') + : counterOpt[0]; + return { status_code: 200, headers: [['content-type', 'text/plain']], - body: encode( - `Counter is ${match(stableStorage.get('counter'), { - Some: (x) => x, - None: () => 0n - })}\n${req.url}\n` - ), + body: encode(`Counter is ${counter}\n${req.url}\n`), streaming_strategy: None, upgrade: None }; @@ -187,26 +173,25 @@ export default class extends Service { @update([HttpRequest], HttpResponse) http_request_update(req: HttpRequest): HttpResponse { if (req.method === 'POST') { - const counter = match(stableStorage.get('counter'), { - Some: (x) => x, - None: () => 0n - }); + const counterOpt = this.stableStorage.get('counter'); + const counter = + counterOpt.length === 0 + ? ic.trap('counter does not exist') + : counterOpt[0]; - stableStorage.insert('counter', counter + 1n); + this.stableStorage.insert('counter', counter + 1n); if (req.headers.find(isGzip) === undefined) { + const counterOpt = this.stableStorage.get('counter'); + const counter = + counterOpt.length === 0 + ? ic.trap('counter does not exist') + : counterOpt[0]; + return { status_code: 201, headers: [['content-type', 'text/plain']], - body: encode( - `Counter updated to ${match( - stableStorage.get('counter'), - { - Some: (x) => x, - None: () => 0n - } - )}\n` - ), + body: encode(`Counter updated to ${counter}\n`), streaming_strategy: None, upgrade: None }; @@ -252,13 +237,14 @@ export default class extends Service { }; } case 'next': { + const counterOpt = this.stableStorage.get('counter'); + const counter = + counterOpt.length === 0 + ? ic.trap('counter does not exist') + : counterOpt[0]; + return { - body: encode( - `${match(stableStorage.get('counter'), { - Some: (x) => x, - None: () => 0n - })}` - ), + body: encode(`${counter}`), token: Some({ arbitrary_data: 'last' }) }; } @@ -274,3 +260,14 @@ export default class extends Service { } } } + +function isGzip(x: HeaderField): boolean { + return ( + x[0].toLowerCase() === 'accept-encoding' && + x[1].toLowerCase().includes('gzip') + ); +} + +function encode(string: string): blob { + return Buffer.from(string, 'utf-8'); +}