├── .vscode ├── extensions.json └── settings.json ├── README.md ├── app.ts ├── demo.ts ├── deno.json ├── deno.jsonc ├── deno.lock ├── index.html ├── main.ts ├── main_bench.ts └── main_test.ts /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "denoland.vscode-deno" 4 | ] 5 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "deno.enable": true, 3 | "deno.lint": true, 4 | "deno.unstable": true 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | https://www.youtube.com/watch?v=sGG1xm5vZzY 2 | -------------------------------------------------------------------------------- /app.ts: -------------------------------------------------------------------------------- 1 | import { Hono } from "https://deno.land/x/hono@v3.11.6/mod.ts" 2 | import { cors, serveStatic } from "https://deno.land/x/hono@v3.11.6/middleware.ts" 3 | import { streamSSE } from "https://deno.land/x/hono@v3.11.6/helper/streaming/index.ts" 4 | 5 | const db = await Deno.openKv() 6 | 7 | const app = new Hono() 8 | let i = 0 9 | 10 | interface LastVisit { 11 | country: string 12 | city: string 13 | flag: string 14 | } 15 | 16 | app.use(cors()) 17 | 18 | app.get('/', serveStatic({ path: './index.html' })) 19 | 20 | app.post('/visit', async (c) => { 21 | const { city, flag, country } = await c.req.json() 22 | 23 | await db.atomic() 24 | .set(["lastVisit"], { country, city, flag }) 25 | .sum(["visits"], 1n) 26 | .commit() 27 | 28 | return c.json({ message: 'ok' }) 29 | }) 30 | 31 | app.get('/visit', (c) => { 32 | return streamSSE(c, async (stream) => { 33 | const watcher = db.watch([["lastVisit"]]) 34 | 35 | for await (const entry of watcher) { 36 | const { value } = entry[0] 37 | 38 | if (value != null) { 39 | await stream.writeSSE({ data: JSON.stringify(value), event: 'update', id: String(i++) }) 40 | } 41 | } 42 | 43 | // while (true) { 44 | // const { value } = await db.get(["visits"]) 45 | // await stream.writeSSE({ data: Number(value).toString(), event: 'update', id: String(i++) }) 46 | // await stream.sleep(1000) 47 | // // const message = `Son las ${new Date().toLocaleTimeString()}` 48 | // // await stream.writeSSE({ data: message, event: 'update', id: String(i++) }) 49 | // // await stream.sleep(1000) 50 | // } 51 | }) 52 | }) 53 | 54 | // app.get('/counter', (c) => { 55 | // return streamSSE(c, async (stream) => { 56 | // const visitsKey = ["visits"] 57 | // const listOfKeysToWatch = [visitsKey] 58 | // const watcher = db.watch(listOfKeysToWatch) 59 | 60 | // for await (const entry of watcher) { 61 | // const { value } = entry[0] 62 | // if (value != null) { 63 | // await stream.writeSSE({ data: value.toString(), event: 'update', id: String(i++) }) 64 | // } 65 | // } 66 | 67 | // // while (true) { 68 | // // const { value } = await db.get(["visits"]) 69 | // // await stream.writeSSE({ data: Number(value).toString(), event: 'update', id: String(i++) }) 70 | // // await stream.sleep(1000) 71 | // // // const message = `Son las ${new Date().toLocaleTimeString()}` 72 | // // // await stream.writeSSE({ data: message, event: 'update', id: String(i++) }) 73 | // // // await stream.sleep(1000) 74 | // // } 75 | // }) 76 | // }) 77 | 78 | Deno.serve(app.fetch) -------------------------------------------------------------------------------- /demo.ts: -------------------------------------------------------------------------------- 1 | const db = await Deno.openKv() 2 | 3 | // const facundoPreferences = { 4 | // username: "facundoCapua", 5 | // theme: "light", 6 | // language: "es-ES", 7 | // } 8 | 9 | // const miduPreferences = { 10 | // username: "midudev", 11 | // theme: "dark", 12 | // language: "en-US", 13 | // } 14 | 15 | // await db.set(["preferences", "facundo"], facundoPreferences); 16 | // await db.set(["preferences", "midudev"], miduPreferences); 17 | 18 | const entries = db.list({ prefix: ["preferences"] }) 19 | 20 | 21 | for await (const entry of entries) { // <- esto es javascript 22 | console.log(entry) 23 | } 24 | 25 | await db.delete(["preferences", "facundo"]) -------------------------------------------------------------------------------- /deno.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": { 3 | "dev": "deno run --watch main.ts" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /deno.jsonc: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": { 3 | "dev": "deno run --watch main.ts" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /deno.lock: -------------------------------------------------------------------------------- 1 | { 2 | "version": "3", 3 | "redirects": { 4 | "https://deno.land/x/hono/mod.ts": "https://deno.land/x/hono@v3.11.6/mod.ts" 5 | }, 6 | "remote": { 7 | "https://deno.land/x/hono@v3.11.6/adapter/deno/serve-static.ts": "ba10cf6aaf39da942b0d49c3b9877ddba69d41d414c6551d890beb1085f58eea", 8 | "https://deno.land/x/hono@v3.11.6/client/client.ts": "ff340f58041203879972dd368b011ed130c66914f789826610869a90603406bf", 9 | "https://deno.land/x/hono@v3.11.6/client/index.ts": "3ff4cf246f3543f827a85a2c84d66a025ac350ee927613629bda47e854bfb7ba", 10 | "https://deno.land/x/hono@v3.11.6/client/utils.ts": "053273c002963b549d38268a1b423ac8ca211a8028bdab1ed0b781a62aa5e661", 11 | "https://deno.land/x/hono@v3.11.6/compose.ts": "37d6e33b7db80e4c43a0629b12fd3a1e3406e7d9e62a4bfad4b30426ea7ae4f1", 12 | "https://deno.land/x/hono@v3.11.6/context.ts": "5cac561498e1f4af7a355673a047f07f47349056c5167ed9aec8a20cdcac8464", 13 | "https://deno.land/x/hono@v3.11.6/helper/cookie/index.ts": "a05ce7e3bafe1f8c7f45d04abf79822716011be75904fe1aad2e99126fd985b9", 14 | "https://deno.land/x/hono@v3.11.6/helper/html/index.ts": "a4ac61c54cd432e708dd019aced59273e5e053933f4804b0b5758d9916468b3e", 15 | "https://deno.land/x/hono@v3.11.6/helper/streaming/index.ts": "90dd4424d8d8271efd602ab7a8fcc0539a1573c98ee03e4c910bc0bbd7d20f27", 16 | "https://deno.land/x/hono@v3.11.6/hono-base.ts": "ab40351c5198cedef9e235ca0a958a161fec822b62aa7fb1eaa36559dc05e0d5", 17 | "https://deno.land/x/hono@v3.11.6/hono.ts": "2cc4c292e541463a4d6f83edbcea58048d203e9564ae62ec430a3d466b49a865", 18 | "https://deno.land/x/hono@v3.11.6/http-exception.ts": "6071df078b5f76d279684d52fe82a590f447a64ffe1b75eb5064d0c8a8d2d676", 19 | "https://deno.land/x/hono@v3.11.6/jsx/components.ts": "f69d385d768f2b6656615a2991514b610bfe6ac87e042fa9723d2b92dd92a62b", 20 | "https://deno.land/x/hono@v3.11.6/jsx/index.ts": "4e447da487478dd6f7ceda92256cdeb3fb8ca42c336b22553c279a1927594941", 21 | "https://deno.land/x/hono@v3.11.6/jsx/streaming.ts": "37a6b23d1419e210e6063c7d315f984a993a4f42b39cbcd77323d1c0af75bc62", 22 | "https://deno.land/x/hono@v3.11.6/middleware.ts": "57b2047c4b9d775a052a9c44a3b805802c1d1cb477ab9c4bb6185d27382d1b96", 23 | "https://deno.land/x/hono@v3.11.6/middleware/basic-auth/index.ts": "5f88b1bc909d0db51fd72ec236db642271e3c597ac9ca2d8d191c0bb7d2ffdef", 24 | "https://deno.land/x/hono@v3.11.6/middleware/bearer-auth/index.ts": "1bfe631db1661cd342a2220614af5e21455ebea11b8c3ed5f6df7ef8d02b9a54", 25 | "https://deno.land/x/hono@v3.11.6/middleware/cache/index.ts": "0d3a742d0b1639e47f11da6ab66f755eb379a431b073391a94a9f551c5a2c430", 26 | "https://deno.land/x/hono@v3.11.6/middleware/compress/index.ts": "c053a4c9bb605f0320e014b513cfd44c6cbde6ed49373fd659fa02d697f9df17", 27 | "https://deno.land/x/hono@v3.11.6/middleware/cors/index.ts": "8ed6459d4d8990e5f398255aa139e3026f773abee8acd7e9dc1090fcf3f42e83", 28 | "https://deno.land/x/hono@v3.11.6/middleware/etag/index.ts": "3392aabea4d02dfec51455c5919bff9aad76538b9fde375dd542fbc3f389dd3a", 29 | "https://deno.land/x/hono@v3.11.6/middleware/jsx-renderer/index.ts": "8c12ac5b61608a4688e28794927990d1d07ca11de8b9e79bb85e33b1696d790c", 30 | "https://deno.land/x/hono@v3.11.6/middleware/jwt/index.ts": "2779850fc1e16a41da03ba93430a08439002fa8cc0e122ee6f444a98172bc891", 31 | "https://deno.land/x/hono@v3.11.6/middleware/logger/index.ts": "4baf9217b61f5e9e937c3e4e6cd87508c83603fcee77c33edba0a6ae2cc41ccd", 32 | "https://deno.land/x/hono@v3.11.6/middleware/powered-by/index.ts": "6faba0cf042278d60b317b690640bb0b58747690cf280fa09024424c5174e66d", 33 | "https://deno.land/x/hono@v3.11.6/middleware/pretty-json/index.ts": "2216ce4c9910be009fecac63367c3626b46137d4cf7cb9a82913e501104b4a88", 34 | "https://deno.land/x/hono@v3.11.6/middleware/secure-headers/index.ts": "05dfc8fbb94a565efbb55633090b81157e97f23393ff80a23e299ad7ac222e34", 35 | "https://deno.land/x/hono@v3.11.6/middleware/timing/index.ts": "241702aa10ab66cc832e8b556c57c236f3bf338a8817d802cb142eae0f852582", 36 | "https://deno.land/x/hono@v3.11.6/mod.ts": "90114a97be9111b348129ad0143e764a64921f60dd03b8f3da529db98a0d3a82", 37 | "https://deno.land/x/hono@v3.11.6/request.ts": "71a67261d14bba95e9a8b4782a1c1704d12d54a77390ce404741df15ab99a109", 38 | "https://deno.land/x/hono@v3.11.6/router.ts": "880316f561918fc197481755aac2165fdbe2f530b925c5357a9f98d6e2cc85c7", 39 | "https://deno.land/x/hono@v3.11.6/router/linear-router/index.ts": "8a2a7144c50b1f4a92d9ee99c2c396716af144c868e10608255f969695efccd0", 40 | "https://deno.land/x/hono@v3.11.6/router/linear-router/router.ts": "bc63e8b5bc1dabc815306d50bebd1bb5877ffa3936ba2ad7550d093c95ee6bd1", 41 | "https://deno.land/x/hono@v3.11.6/router/pattern-router/index.ts": "304a66c50e243872037ed41c7dd79ed0c89d815e17e172e7ad7cdc4bc07d3383", 42 | "https://deno.land/x/hono@v3.11.6/router/pattern-router/router.ts": "a9a5a2a182cce8c3ae82139892cc0502be7dd8f579f31e76d0302b19b338e548", 43 | "https://deno.land/x/hono@v3.11.6/router/reg-exp-router/index.ts": "52755829213941756159b7a963097bafde5cc4fc22b13b1c7c9184dc0512d1db", 44 | "https://deno.land/x/hono@v3.11.6/router/reg-exp-router/node.ts": "5b3fb80411db04c65df066e69fedb2c8c0844753c2633d703336de84d569252c", 45 | "https://deno.land/x/hono@v3.11.6/router/reg-exp-router/router.ts": "7f7af7ce45a4327f9ac7dbdee186f7ba9a24f2eff14c720e3f670be001e71780", 46 | "https://deno.land/x/hono@v3.11.6/router/reg-exp-router/trie.ts": "852ce7207e6701e47fa30889a0d2b8bfcd56d8862c97e7bc9831e0a64bd8835f", 47 | "https://deno.land/x/hono@v3.11.6/router/smart-router/index.ts": "74f9b4fe15ea535900f2b9b048581915f12fe94e531dd2b0032f5610e61c3bef", 48 | "https://deno.land/x/hono@v3.11.6/router/smart-router/router.ts": "f1848a2a1eefa316a11853ae12e749552747771fb8a11fe713ae04ea6461140b", 49 | "https://deno.land/x/hono@v3.11.6/router/trie-router/index.ts": "3eb75e7f71ba81801631b30de6b1f5cefb2c7239c03797e2b2cbab5085911b41", 50 | "https://deno.land/x/hono@v3.11.6/router/trie-router/node.ts": "3af15fa9c9994a8664a2b7a7c11233504b5bb9d4fcf7bb34cf30d7199052c39f", 51 | "https://deno.land/x/hono@v3.11.6/router/trie-router/router.ts": "54ced78d35676302c8fcdda4204f7bdf5a7cc907fbf9967c75674b1e394f830d", 52 | "https://deno.land/x/hono@v3.11.6/utils/body.ts": "7a16a6656331a96bcae57642f8d5e3912bd361cbbcc2c0d2157ecc3f218f7a92", 53 | "https://deno.land/x/hono@v3.11.6/utils/buffer.ts": "9066a973e64498cb262c7e932f47eed525a51677b17f90893862b7279dc0773e", 54 | "https://deno.land/x/hono@v3.11.6/utils/cookie.ts": "19920ba6756944aae1ad8585c3ddeaa9df479733f59d05359db096f7361e5e4b", 55 | "https://deno.land/x/hono@v3.11.6/utils/crypto.ts": "bda0e141bbe46d3a4a20f8fbcb6380d473b617123d9fdfa93e4499410b537acc", 56 | "https://deno.land/x/hono@v3.11.6/utils/encode.ts": "3b7c7d736123b5073542b34321700d4dbf5ff129c138f434bb2144a4d425ee89", 57 | "https://deno.land/x/hono@v3.11.6/utils/filepath.ts": "18461b055a914d6da85077f453051b516281bb17cf64fa74bf5ef604dc9d2861", 58 | "https://deno.land/x/hono@v3.11.6/utils/html.ts": "4d2f3975238819fa756e0e51cb7090702136b1e3c54c5049694e26c62d2fcd84", 59 | "https://deno.land/x/hono@v3.11.6/utils/jwt/index.ts": "5e4b82a42eb3603351dfce726cd781ca41cb57437395409d227131aec348d2d5", 60 | "https://deno.land/x/hono@v3.11.6/utils/jwt/jwt.ts": "02ff7bbf1298ffcc7a40266842f8eac44b6c136453e32d4441e24d0cbfba3a95", 61 | "https://deno.land/x/hono@v3.11.6/utils/jwt/types.ts": "58ddf908f76ba18d9c62ddfc2d1e40cc2e306bf987409a6169287efa81ce2546", 62 | "https://deno.land/x/hono@v3.11.6/utils/mime.ts": "0105d2b5e8e91f07acc70f5d06b388313995d62af23c802fcfba251f5a744d95", 63 | "https://deno.land/x/hono@v3.11.6/utils/stream.ts": "1789dcc73c5b0ede28f83d7d34e47ae432c20e680907cb3275a9c9187f293983", 64 | "https://deno.land/x/hono@v3.11.6/utils/url.ts": "5fc3307ef3cb2e6f34ec2a03e3d7f2126c6a9f5f0eab677222df3f0e40bd7567", 65 | "https://deno.land/x/hono@v3.11.6/validator/index.ts": "6c986e8b91dcf857ecc8164a506ae8eea8665792a4ff7215471df669c632ae7c", 66 | "https://deno.land/x/hono@v3.11.6/validator/validator.ts": "f6139f0b6eb2040bc9f7ed4148905d5affc898235952772b2d6585fdae8d4731" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 69 | 70 | -------------------------------------------------------------------------------- /main.ts: -------------------------------------------------------------------------------- 1 | export function add(a: number, b: number): number { 2 | return a + b; 3 | } 4 | 5 | // Learn more at https://deno.land/manual/examples/module_metadata#concepts 6 | if (import.meta.main) { 7 | console.log("Add 2 + 3 =", add(2, 3)); 8 | } 9 | -------------------------------------------------------------------------------- /main_bench.ts: -------------------------------------------------------------------------------- 1 | import { add } from "./main.ts"; 2 | 3 | Deno.bench(function addSmall() { 4 | add(1, 2); 5 | }); 6 | 7 | Deno.bench(function addBig() { 8 | add(2 ** 32, 2 ** 32); 9 | }); 10 | -------------------------------------------------------------------------------- /main_test.ts: -------------------------------------------------------------------------------- 1 | import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; 2 | import { add } from "./main.ts"; 3 | 4 | Deno.test(function addTest() { 5 | assertEquals(add(2, 3), 5); 6 | }); 7 | --------------------------------------------------------------------------------