foobar
foobar
DDeno
Created by foobar on 7/2/2024 in #help
Share a data between multiple instances Deno Deploy
Hi, I want to share data between multiple instance. My first instance create a key. Others new instances have to get the original key from first instance. I use BroadcastChannel to do it. But it relies on the strong assumption that the first instance can't dissapear ? Is it true ? Or do you have a more elegant way to do it (kv ?) ?
/**
* Channel for broadcasting key
*/
const channel = new BroadcastChannel("KEY");

let firstInstance = true;
// all reload generate a new key
let key = generateKey()

channel.onmessage = async (event: MessageEvent<Message>) => {
const message = event.data;
if (message.nature === "newinstance" && firstInstance) {
// only first instance send key
// QUESTION : can first instance disappear
channel.postMessage({ nature: "key", data: key });
}
if (message.nature === "key" && firstInstance) {
// only last instance change
key = message.data;
// it is a new instance
firstInstance = false;
}
};

/**
* All instance post the message
* if 1 instance => no post
* if 2nd instance => only 1st instance receive message => send key
* if 3rd instance => only 1st instance send message because 2nd instance has firstInstance = false
*/
channel.postMessage({ nature: "newinstance" });
/**
* Channel for broadcasting key
*/
const channel = new BroadcastChannel("KEY");

let firstInstance = true;
// all reload generate a new key
let key = generateKey()

channel.onmessage = async (event: MessageEvent<Message>) => {
const message = event.data;
if (message.nature === "newinstance" && firstInstance) {
// only first instance send key
// QUESTION : can first instance disappear
channel.postMessage({ nature: "key", data: key });
}
if (message.nature === "key" && firstInstance) {
// only last instance change
key = message.data;
// it is a new instance
firstInstance = false;
}
};

/**
* All instance post the message
* if 1 instance => no post
* if 2nd instance => only 1st instance receive message => send key
* if 3rd instance => only 1st instance send message because 2nd instance has firstInstance = false
*/
channel.postMessage({ nature: "newinstance" });
8 replies
DDeno
Created by foobar on 6/27/2024 in #help
Jsr, esm, x and external ref
Hi, I want to migrate most of my import to jsr to simplify code. I use Hono and 3rd party middleware on Deno. I import them with esm / deno.land Is there a better way to link dependencies with hono (or library with dependencies) ? My actual settings
"imports": {
"@/": "./",
"@scalar/hono-api-reference": "npm:@scalar/hono-api-reference",
"hono": "jsr:@hono/hono@^4.4.8",
// For hono swagger
"hono/swagger-ui": "https://esm.sh/@hono/swagger-ui@0.2.2?external=hono&target=es2022",
"hono/zod-validator": "https://esm.sh/@hono/zod-validator@0.2.1?external=hono,zod&target=es2022",
"hono/zod-openapi": "https://esm.sh/@hono/zod-openapi@0.11.1?external=hono,zod,hono/zod-validator&target=es2022",
"zod": "https://deno.land/x/zod@v3.23.8/mod.ts"
}
"imports": {
"@/": "./",
"@scalar/hono-api-reference": "npm:@scalar/hono-api-reference",
"hono": "jsr:@hono/hono@^4.4.8",
// For hono swagger
"hono/swagger-ui": "https://esm.sh/@hono/swagger-ui@0.2.2?external=hono&target=es2022",
"hono/zod-validator": "https://esm.sh/@hono/zod-validator@0.2.1?external=hono,zod&target=es2022",
"hono/zod-openapi": "https://esm.sh/@hono/zod-openapi@0.11.1?external=hono,zod,hono/zod-validator&target=es2022",
"zod": "https://deno.land/x/zod@v3.23.8/mod.ts"
}
8 replies
DDeno
Created by foobar on 5/24/2024 in #help
Debug test in visual studio & deno.jsonc
No description
4 replies
DDeno
Created by foobar on 5/20/2024 in #help
Data sync input between parent and children
I have the following scheme (cf picture): - a route that get initial data - data with items are sent to an island - item of data are sent to a child island Child islands have only input with no form nor validate button. How to track and sync input change in children and reflect it to parent ?
4 replies
DDeno
Created by foobar on 4/9/2024 in #help
Audit KV data in deno deploy
Hi, I use kivi extension to audit data in local KV. How could I do this in deno deploy ? I was used before to handle data like MongoDB. You could read, update, drop some data. What is the spirit/ideas with KV when you want to manipulate data located in a serverless environnement ? Thanks
2 replies
DDeno
Created by foobar on 2/20/2024 in #help
npm import : class is not found but it is well declared in node_module
I try to use Tatum SDK in deno (https://github.com/tatumio/tatum-js). I pick up the easiest code example https://docs.tatum.io/ I try 2 ways of imports of tatum library but I have different errors in both ways. 1. with npm don't recognize TatumSDK in import (but in node_modules it is exported in service/tatum). How can I tell the owner to slighty modify his code in order that TatumSDK is recognize (Ethereum, Network are recognized) ?
import { Ethereum, Network, TatumSDK } from "npm:@tatumio/tatum";

const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM });
const latestBlock = await tatum.rpc.blockNumber();
console.log(`Latest block is ${latestBlock.result}`);
tatum.destroy();
import { Ethereum, Network, TatumSDK } from "npm:@tatumio/tatum";

const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM });
const latestBlock = await tatum.rpc.blockNumber();
console.log(`Latest block is ${latestBlock.result}`);
tatum.destroy();
2. with esm Library BigNumber.js has a version for deno ! error: Uncaught (in promise) TypeError: R.BigNumber is not a constructor
// BigNumber.js has a version for deno !
// error: Uncaught (in promise) TypeError: R.BigNumber is not a constructor
import { Ethereum, Network, TatumSDK } from "https://esm.sh/@tatumio/tatum";

// fake call to prevent error
window.location = { hostname: "fake" };

const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM });
const latestBlock = await tatum.rpc.blockNumber();
console.log(`Latest block is ${latestBlock.result}`);
tatum.destroy();
// BigNumber.js has a version for deno !
// error: Uncaught (in promise) TypeError: R.BigNumber is not a constructor
import { Ethereum, Network, TatumSDK } from "https://esm.sh/@tatumio/tatum";

// fake call to prevent error
window.location = { hostname: "fake" };

const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM });
const latestBlock = await tatum.rpc.blockNumber();
console.log(`Latest block is ${latestBlock.result}`);
tatum.destroy();
5 replies
DDeno
Created by foobar on 1/15/2024 in #help
Fresh, deno deploy microservices and auth
No description
12 replies
DDeno
Created by foobar on 1/12/2024 in #help
Get input from an island, make calculus then render ?
I have a route with 2 islands inside - one to get input from user - one to render data Once I've got the user input, I retrieve data, make some calculus and want to render (charts for example) I want to stay in the spirit of SSR ie calculus are made on the server, how can I get user input in the route handler function and then render ? Thx
3 replies
DDeno
Created by foobar on 10/28/2023 in #help
WASM / Extract and manipulate Array of Date
I try to extract and manipulate an array of date in a function The rust fn doesn't compile. How to manipulate Date and convert it ? Does someone have a repository/link with example of Rust + Deno library ? Cargo.toml
[dependencies]
wasm-bindgen = "=0.2.87"
time = { version = "0.3", features = ["parsing", "macros"] }
js-sys = "0.3.64"
[dependencies]
wasm-bindgen = "=0.2.87"
time = { version = "0.3", features = ["parsing", "macros"] }
js-sys = "0.3.64"
mod.ts
import { instantiate } from "./lib/rs_lib.generated.js";

const { DateArrayToString } = await instantiate();

console.log(DateArrayToString( [new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )]))
import { instantiate } from "./lib/rs_lib.generated.js";

const { DateArrayToString } = await instantiate();

console.log(DateArrayToString( [new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )]))
lib.rs
use wasm_bindgen::prelude::*;

use js_sys::Array;
use js_sys::Date;

#[wasm_bindgen]
pub fn DateArrayToString(arr:Array) -> JsString {
let result : JsString;
for i in 0..arr.length() {
// problem is here
let d : Date = arr.get(i).as_ref();
// Concat
result = result.concat(d.to_string());
}
return result;
}
use wasm_bindgen::prelude::*;

use js_sys::Array;
use js_sys::Date;

#[wasm_bindgen]
pub fn DateArrayToString(arr:Array) -> JsString {
let result : JsString;
for i in 0..arr.length() {
// problem is here
let d : Date = arr.get(i).as_ref();
// Concat
result = result.concat(d.to_string());
}
return result;
}
3 replies
DDeno
Created by foobar on 10/20/2023 in #help
wasmbuild & rust module path
No description
3 replies
DDeno
Created by foobar on 8/6/2023 in #help
GetIP + Ctx from middleware to a route not passed
Hi, I have test to retrieve IP visitor Code taken from Discord is always returning "localhost" (when Deno Deploy) Also this value of "IP" get from the middleware is not passed to a route. What is wrong ? Repo is here : https://github.com/hapaxlife/fresh-show-ip _middleware
import { MiddlewareHandlerContext } from "$fresh/server.ts";

export interface State {
hostname: string;
}

function getIp(_req: Request, ctx: MiddlewareHandlerContext<State>) {
ctx.state = {}
if (ctx.remoteAddr.transport === "tcp") {
ctx.state.hostname = ctx.remoteAddr.hostname;
} else {
ctx.state.hostname = "not tcp";
}
ctx.state.data = "test data"
console.log("getIp : " + ctx.state.hostname)
return ctx.next();
}

export const handler = [
getIp,
];
import { MiddlewareHandlerContext } from "$fresh/server.ts";

export interface State {
hostname: string;
}

function getIp(_req: Request, ctx: MiddlewareHandlerContext<State>) {
ctx.state = {}
if (ctx.remoteAddr.transport === "tcp") {
ctx.state.hostname = ctx.remoteAddr.hostname;
} else {
ctx.state.hostname = "not tcp";
}
ctx.state.data = "test data"
console.log("getIp : " + ctx.state.hostname)
return ctx.next();
}

export const handler = [
getIp,
];
route : old way
import { Head } from "$fresh/runtime.ts";
import type { HandlerContext } from "$fresh/server.ts";
import { State } from "./_middleware.ts"

export default function Home(req: Request, ctx: HandlerContext<State>) {

console.log("Home " + ctx.state)
const hostname = ctx.state?.hostname || "no ip"

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
import { Head } from "$fresh/runtime.ts";
import type { HandlerContext } from "$fresh/server.ts";
import { State } from "./_middleware.ts"

export default function Home(req: Request, ctx: HandlerContext<State>) {

console.log("Home " + ctx.state)
const hostname = ctx.state?.hostname || "no ip"

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
Route : new way
import { Head } from "$fresh/runtime.ts";
import { State } from "./_middleware.ts"
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";

export const handler: Handlers = {
async GET(_req: Request, ctx: HandlerContext<State>) {
console.log("GET Handler")
console.log(ctx.state)
const resp = await ctx.render(ctx.state);
return resp;
},
};

export default function Page({ data }: PageProps<State>) {
const {hostname } = data;

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
import { Head } from "$fresh/runtime.ts";
import { State } from "./_middleware.ts"
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";

export const handler: Handlers = {
async GET(_req: Request, ctx: HandlerContext<State>) {
console.log("GET Handler")
console.log(ctx.state)
const resp = await ctx.render(ctx.state);
return resp;
},
};

export default function Page({ data }: PageProps<State>) {
const {hostname } = data;

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
1 replies