Code Monkey home page Code Monkey logo

Comments (4)

fa11erX avatar fa11erX commented on September 18, 2024 1

I tried the following code in my deno function.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { AccessToken } from "https://esm.sh/[email protected]";

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}

serve(async (req) => {
  const roomName = 'my_room';
  const participantName = 'dimitri';
  const at = new AccessToken('SECRET', 'SECRET', {
    identity: participantName,
  });

  // This is needed if you're planning to invoke your function from a browser.
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }

  try {
    const { name } = await req.json()
    at.addGrant({ roomJoin: true, room: roomName });
    const token = at.toJwt();

    const data = {
      message: `Hello ${name}!`,
      token: token,
      identity: participantName,
      roomName: roomName,
    }

    return new Response(JSON.stringify(data), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 200,
    })
  } catch (error) {
    return new Response(JSON.stringify({ error: error.message }), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 400,
    })
  }
})

I'm getting the following error during execution: "Identifier 'mod' has already been declared at file:///src/main.ts:3163:7". It seems like there's something conflicting with Deno.

from node-sdks.

davidzhao avatar davidzhao commented on September 18, 2024

I believe it should work. There's nothing we explicitly require Node for. Do you want to give it a try and report back with any issues that you are finding?

from node-sdks.

sandeepone avatar sandeepone commented on September 18, 2024

This is working with Deno but wont work in Supabase Functions. If you want to work with supabase functions copy the AccessToken file from the repo and replace jsonwebtoken with jose. Check the pull request #59

import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { AccessToken } from "https://esm.sh/[email protected]";

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}

interface RequestBody {
	name: string;
	roomName: string;
	identity: string;
}

serve(async (req: Request) => {
	// This is needed if you're planning to invoke your function from a browser.
	if (req.method === 'OPTIONS') {
		return new Response('ok', { headers: corsHeaders });
	}

	if (req.method !== 'POST') {
		return new Response(JSON.stringify({ error: 'Method Not Allowed' }), {
			headers: { ...corsHeaders, 'Content-Type': 'application/json' },
			status: 405,
		});
	}

	try {
		const { name, roomName, identity }: RequestBody = await req.json();

		const at = new AccessToken('SECRET', 'SECRET', {
			identity: identity,
			name: name,
			metadata: 'contact',
			ttl: '1h',
		});

		at.addGrant({
			roomJoin: true,
			room: roomName,
			canPublish: true,
			canSubscribe: true,
			canPublishData: true,
		});

		const token = await at.toJwt();

		const data = {
			accessToken: token,
			identity: identity,
			roomName: roomName,
		};

		return new Response(JSON.stringify(data), {
			headers: { ...corsHeaders, 'Content-Type': 'application/json' },
			status: 200,
		});
	} catch (error) {
		console.error('Invalid JSON payload:', error);
		return new Response(JSON.stringify({ error: error.message }), {
			headers: { ...corsHeaders, 'Content-Type': 'application/json' },
			status: 400,
		});
	}
}, { port: 8082 });

from node-sdks.

lukasIO avatar lukasIO commented on September 18, 2024

Las missing piece for this should be #122, closing

from node-sdks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.