Skip to content

Commit

Permalink
chore: use crypto api for generating uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Jul 8, 2024
1 parent 1256953 commit 4d2f98a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 19 deletions.
10 changes: 1 addition & 9 deletions examples/chat/src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { generateHonoObject } from "hono-do";

function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

export const Chat = generateHonoObject("/chat", (app) => {
const messages: {
timestamp: string;
Expand All @@ -26,7 +18,7 @@ export const Chat = generateHonoObject("/chat", (app) => {

async function handleWebSocketUpgrade() {
const [client, server] = Object.values(new WebSocketPair());
const clientId = uuidv4();
const clientId = crypto.randomUUID();
server.accept();

sessions.set(clientId, server);
Expand Down
10 changes: 1 addition & 9 deletions examples/hibernatable-chat/src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { generateHonoObject } from "hono-do";

function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

declare module "hono-do" {
interface HonoObjectVars {
messages: {
Expand All @@ -31,7 +23,7 @@ export const Chat = generateHonoObject("/chat", (app, state, vars) => {

async function handleWebSocketUpgrade() {
const [client, server] = Object.values(new WebSocketPair());
const clientId = uuidv4();
const clientId = crypto.randomUUID();
state.acceptWebSocket(server);

server.serializeAttachment({ clientId });
Expand Down
2 changes: 1 addition & 1 deletion examples/hibernatable-chat/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ export const Template = html`
</script>
</body>
</html>
`.trim();
`;

0 comments on commit 4d2f98a

Please sign in to comment.