Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export ClientToken from @y-sweet/react and @y-sweet/client #149

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions js-pkg/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WebsocketProvider, type WebsocketProviderParams } from './websocket'
import * as Y from 'yjs'
import { ClientToken } from '@y-sweet/sdk'
import { type ClientToken } from '@y-sweet/sdk'

export { WebsocketProvider, WebsocketProviderParams }
export { WebsocketProvider, WebsocketProviderParams, ClientToken }

/**
* Given a {@link ClientToken}, create a {@link WebsocketProvider} for it.
Expand All @@ -26,3 +26,43 @@ export function createYjsProvider(

return provider
}

function stringToBase64(input: string) {
if (typeof window !== 'undefined' && window.btoa) {
// Browser
return window.btoa(input)
} else if (typeof Buffer !== 'undefined') {
// Node.js
return Buffer.from(input).toString('base64')
} else {
throw new Error('Unable to encode to Base64')
}
}

function base64ToString(input: string) {
if (typeof window !== 'undefined' && window.atob) {
// Browser
return window.atob(input)
} else if (typeof Buffer !== 'undefined') {
// Node.js
return Buffer.from(input, 'base64').toString()
} else {
throw new Error('Unable to decode from Base64')
}
}

export function encodeClientToken(token: ClientToken): string {
const jsonString = JSON.stringify(token)
let base64 = stringToBase64(jsonString)
base64 = base64.replace('+', '-').replace('/', '_').replace(/=+$/, '')
return base64
}

export function decodeClientToken(token: string): ClientToken {
let base64 = token.replace('-', '+').replace('_', '/')
while (base64.length % 4) {
base64 += '='
}
const jsonString = base64ToString(base64)
return JSON.parse(jsonString)
}
1 change: 0 additions & 1 deletion js-pkg/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
},
"dependencies": {
"@y-sweet/client": "0.0.8",
"@y-sweet/sdk": "0.0.8",
"y-protocols": "^1.0.5"
}
}
12 changes: 9 additions & 3 deletions js-pkg/react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use client'

import { WebsocketProvider, WebsocketProviderParams, createYjsProvider } from '@y-sweet/client'
import { ClientToken, encodeClientToken } from '@y-sweet/sdk'
import {
WebsocketProvider,
WebsocketProviderParams,
createYjsProvider,
ClientToken,
encodeClientToken,
} from '@y-sweet/client'
import type { ReactNode } from 'react'
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
import type { Awareness } from 'y-protocols/awareness'
import * as Y from 'yjs'
export { createYjsProvider, WebsocketProvider, type WebsocketProviderParams }

export { createYjsProvider, WebsocketProvider, type WebsocketProviderParams, type ClientToken }

type YjsContextType = {
doc: Y.Doc
Expand Down
40 changes: 0 additions & 40 deletions js-pkg/sdk/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,43 +317,3 @@ export async function createDoc(connectionString?: string): Promise<DocCreationR
const manager = new DocumentManager(connectionString)
return await manager.createDoc()
}

function stringToBase64(input: string) {
if (typeof window !== 'undefined' && window.btoa) {
// Browser
return window.btoa(input)
} else if (typeof Buffer !== 'undefined') {
// Node.js
return Buffer.from(input).toString('base64')
} else {
throw new Error('Unable to encode to Base64')
}
}

function base64ToString(input: string) {
if (typeof window !== 'undefined' && window.atob) {
// Browser
return window.atob(input)
} else if (typeof Buffer !== 'undefined') {
// Node.js
return Buffer.from(input, 'base64').toString()
} else {
throw new Error('Unable to decode from Base64')
}
}

export function encodeClientToken(token: ClientToken): string {
const jsonString = JSON.stringify(token)
let base64 = stringToBase64(jsonString)
base64 = base64.replace('+', '-').replace('/', '_').replace(/=+$/, '')
return base64
}

export function decodeClientToken(token: string): ClientToken {
let base64 = token.replace('-', '+').replace('_', '/')
while (base64.length % 4) {
base64 += '='
}
const jsonString = base64ToString(base64)
return JSON.parse(jsonString)
}
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading