From fcf1e44fc8ee923912c16a84da1c24746b401c84 Mon Sep 17 00:00:00 2001 From: Michel Ribeiro Date: Fri, 19 Jul 2019 16:55:54 -0300 Subject: [PATCH 1/7] fix: Call version cache config property by cacheVersion --- src/target/typescript_nodeserver.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index 903418c..25c1bd6 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -136,10 +136,10 @@ END @io << ident ident "let cacheKey: string | null = null, decodedKey: string | null = null, cacheExpirationSeconds: number | null = null, cacheVersion: number | null = null;\n" @io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n" @io << ident ident ident "try {\n" - @io << ident ident ident ident "const {key, expirationSeconds, version} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" + @io << ident ident ident ident "const {key, expirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident ident ident "if (!key) throw \"\";\n" - @io << ident ident ident ident "cacheKey = crypto.createHash(\"sha256\").update(JSON.stringify(key)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(key); cacheExpirationSeconds = expirationSeconds; cacheVersion = version;\n" - @io << ident ident ident ident "const cache = await hook.getCache(cacheKey, version);console.log(JSON.stringify(cache));\n" + @io << ident ident ident ident "cacheKey = crypto.createHash(\"sha256\").update(JSON.stringify(key)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(key); cacheExpirationSeconds = expirationSeconds;\n" + @io << ident ident ident ident "const cache = await hook.getCache(cacheKey, cacheVersion);console.log(JSON.stringify(cache));\n" @io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n" @io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n" @io << ident ident "}\n" From 02f01899240458c651b34d332eac99e3b60de69b Mon Sep 17 00:00:00 2001 From: Michel Ribeiro Date: Fri, 19 Jul 2019 17:00:51 -0300 Subject: [PATCH 2/7] fix: Call key and expirationSeconds cache config property by cacheKey and cacheexpirationSeconds --- src/target/typescript_nodeserver.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index 25c1bd6..3dfca81 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -136,17 +136,17 @@ END @io << ident ident "let cacheKey: string | null = null, decodedKey: string | null = null, cacheExpirationSeconds: number | null = null, cacheVersion: number | null = null;\n" @io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n" @io << ident ident ident "try {\n" - @io << ident ident ident ident "const {key, expirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" + @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident ident ident "if (!key) throw \"\";\n" - @io << ident ident ident ident "cacheKey = crypto.createHash(\"sha256\").update(JSON.stringify(key)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(key); cacheExpirationSeconds = expirationSeconds;\n" - @io << ident ident ident ident "const cache = await hook.getCache(cacheKey, cacheVersion);console.log(JSON.stringify(cache));\n" + @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(cacheKey);\n" + @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, cacheVersion);console.log(JSON.stringify(cache));\n" @io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n" @io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n" @io << ident ident "}\n" @io << ident ident "const ret = await fn.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident op.return_type.typescript_check_decoded("ret", "\"#{op.pretty_name}.ret\"") @io << ident ident "const encodedRet = " + op.return_type.typescript_encode("ret") + ";\n" - @io << ident ident "if (cacheKey !== null && cacheVersion !== null) hook.setCache(cacheKey, cacheExpirationSeconds ? new Date(new Date().getTime() + (cacheExpirationSeconds * 1000)) : null, cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n" + @io << ident ident "if (cacheKeyEncoded !== null && cacheVersion !== null) hook.setCache(cacheKeyEncoded, cacheExpirationSeconds ? new Date(new Date().getTime() + (cacheExpirationSeconds * 1000)) : null, cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n" @io << ident ident "return encodedRet" @io << ident "},\n" end From f4ad274ae7e01d35f4279d195aabd0fff20444e8 Mon Sep 17 00:00:00 2001 From: davidcpires Date: Sun, 21 Jul 2019 18:29:31 -0300 Subject: [PATCH 3/7] Remove unnecessary space in object declaration keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Douglas Gadêlha --- src/target/typescript_nodeserver.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index 3dfca81..8a4e59b 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -136,7 +136,7 @@ END @io << ident ident "let cacheKey: string | null = null, decodedKey: string | null = null, cacheExpirationSeconds: number | null = null, cacheVersion: number | null = null;\n" @io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n" @io << ident ident ident "try {\n" - @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" + @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident ident ident "if (!key) throw \"\";\n" @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(cacheKey);\n" @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, cacheVersion);console.log(JSON.stringify(cache));\n" From dbfa21a38782ded5467addfc02dc114eee275282 Mon Sep 17 00:00:00 2001 From: davidcpires Date: Sun, 21 Jul 2019 18:30:16 -0300 Subject: [PATCH 4/7] Get rid of console.log on cache declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Douglas Gadêlha --- src/target/typescript_nodeserver.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index 8a4e59b..aa9d3db 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -139,7 +139,7 @@ END @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident ident ident "if (!key) throw \"\";\n" @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(cacheKey);\n" - @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, cacheVersion);console.log(JSON.stringify(cache));\n" + @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, cacheVersion);\n" @io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n" @io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n" @io << ident ident "}\n" From 8fb605665a1ab629b7e9132b90e2b67dd2553d45 Mon Sep 17 00:00:00 2001 From: Michel Ribeiro Date: Mon, 22 Jul 2019 23:24:51 -0300 Subject: [PATCH 5/7] fix: Remove unnecessary variable declaration --- src/target/typescript_nodeserver.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index aa9d3db..c94c323 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -133,7 +133,6 @@ END @io << "\n" end @io << "\n" - @io << ident ident "let cacheKey: string | null = null, decodedKey: string | null = null, cacheExpirationSeconds: number | null = null, cacheVersion: number | null = null;\n" @io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n" @io << ident ident ident "try {\n" @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" From ecf82f7de35aacaa18ee05c80319da6258b0d421 Mon Sep 17 00:00:00 2001 From: Michel Ribeiro Date: Mon, 22 Jul 2019 23:27:55 -0300 Subject: [PATCH 6/7] fix: Declare decodedKey as a const --- src/target/typescript_nodeserver.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index c94c323..b1fa6a6 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -138,7 +138,7 @@ END @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident ident ident "if (!key) throw \"\";\n" @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(cacheKey);\n" - @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, cacheVersion);\n" + @io << ident ident ident ident "const decodedKey = JSON.stringify(_cacheKey);\n" @io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n" @io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n" @io << ident ident "}\n" From c8a8cbe91af15f90f5d18cfdd81359ef55015a8c Mon Sep 17 00:00:00 2001 From: Michel Ribeiro Date: Mon, 22 Jul 2019 23:28:20 -0300 Subject: [PATCH 7/7] fix: Add _ as a prefix for cache variables --- src/target/typescript_nodeserver.cr | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/target/typescript_nodeserver.cr b/src/target/typescript_nodeserver.cr index b1fa6a6..caafae5 100644 --- a/src/target/typescript_nodeserver.cr +++ b/src/target/typescript_nodeserver.cr @@ -135,17 +135,18 @@ END @io << "\n" @io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n" @io << ident ident ident "try {\n" - @io << ident ident ident ident "const {key: cacheKey, expirationSeconds: cacheExpirationSeconds, version: cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" - @io << ident ident ident ident "if (!key) throw \"\";\n" - @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(cacheKey);\n" + @io << ident ident ident ident "const {key: _cacheKey, expirationSeconds: _cacheExpirationSeconds, version: _cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" + @io << ident ident ident ident "if (!_cacheKey) throw \"\";\n" + @io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(_cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100);\n" @io << ident ident ident ident "const decodedKey = JSON.stringify(_cacheKey);\n" + @io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, _cacheVersion);\n" @io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n" @io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n" @io << ident ident "}\n" @io << ident ident "const ret = await fn.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n" @io << ident ident op.return_type.typescript_check_decoded("ret", "\"#{op.pretty_name}.ret\"") @io << ident ident "const encodedRet = " + op.return_type.typescript_encode("ret") + ";\n" - @io << ident ident "if (cacheKeyEncoded !== null && cacheVersion !== null) hook.setCache(cacheKeyEncoded, cacheExpirationSeconds ? new Date(new Date().getTime() + (cacheExpirationSeconds * 1000)) : null, cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n" + @io << ident ident "if (cacheKeyEncoded !== null && _cacheVersion !== null) hook.setCache(cacheKeyEncoded, _cacheExpirationSeconds ? new Date(new Date().getTime() + (_cacheExpirationSeconds * 1000)) : null, _cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n" @io << ident ident "return encodedRet" @io << ident "},\n" end