-
Notifications
You must be signed in to change notification settings - Fork 8
integrate automerge document per space #68
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,14 @@ | |
"check:fix": "pnpm biome check --write src/*" | ||
}, | ||
"devDependencies": { | ||
"@automerge/automerge-repo": "^1.2.1", | ||
"@types/uuid": "^10.0.0", | ||
"uuid": "^11.0.3" | ||
}, | ||
"peerDependencies": { | ||
"uuid": "^11" | ||
}, | ||
"dependencies": { | ||
"bs58check": "^4.0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not excited to have this dependency, but will be included anyway when we use Automerge since they use it internally |
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import bs58check from 'bs58check'; | ||
|
||
import { decodeBase58, encodeBase58 } from './internal/base58Utils.js'; | ||
|
||
/** | ||
* Converts a raw Base58-encoded UUID into Base58Check | ||
*/ | ||
export function idToAutomergeId(rawBase58Uuid: string, _versionByte = 0x00) { | ||
const payload = decodeBase58(rawBase58Uuid); | ||
return bs58check.encode(payload); | ||
} | ||
|
||
/** | ||
* Converts a Base58Check-encoded UUID back to raw Base58 | ||
*/ | ||
export function automergeIdToId(base58CheckUuid: string) { | ||
const versionedPayload = bs58check.decode(base58CheckUuid); | ||
return encodeBase58(versionedPayload); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/graph-framework-utils/src/internal/base58Utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export const BASE58_ALLOWED_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
|
||
export function decodeBase58(str: string) { | ||
let x = BigInt(0); | ||
for (let i = 0; i < str.length; i++) { | ||
const charIndex = BASE58_ALLOWED_CHARS.indexOf(str[i]); | ||
if (charIndex < 0) { | ||
throw new Error('Invalid Base58 character'); | ||
} | ||
x = x * 58n + BigInt(charIndex); | ||
} | ||
|
||
const bytes: number[] = []; | ||
while (x > 0) { | ||
bytes.push(Number(x % 256n)); | ||
x = x >> 8n; | ||
} | ||
|
||
bytes.reverse(); | ||
// Pad to 16 bytes for a UUID | ||
while (bytes.length < 16) { | ||
bytes.unshift(0); | ||
} | ||
|
||
return new Uint8Array(bytes); | ||
} | ||
|
||
export function encodeBase58(data: Uint8Array) { | ||
let x = BigInt(0); | ||
for (const byte of data) { | ||
x = (x << 8n) + BigInt(byte); | ||
} | ||
|
||
let encoded = ''; | ||
while (x > 0) { | ||
const remainder = x % 58n; | ||
x = x / 58n; | ||
encoded = BASE58_ALLOWED_CHARS[Number(remainder)] + encoded; | ||
} | ||
|
||
// deal with leading zeros (0x00 bytes) | ||
for (let i = 0; i < data.length && data[i] === 0; i++) { | ||
encoded = `1${encoded}`; | ||
} | ||
|
||
return encoded; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isValidDocumentId } from '@automerge/automerge-repo'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { automergeIdToId, idToAutomergeId } from '../src/automergeId'; | ||
import { generateId } from '../src/generateId'; | ||
|
||
describe('id <> automergeId conversion', () => { | ||
it('converts an id to an automergeId and back', () => { | ||
const id = generateId(); | ||
const automergeId = idToAutomergeId(id); | ||
const id2 = automergeIdToId(automergeId); | ||
expect(id).toBe(id2); | ||
expect(isValidDocumentId(automergeId)).toBe(true); | ||
}); | ||
|
||
it('throws an error for invalid Base58 characters', () => { | ||
expect(() => idToAutomergeId('!@#$%^&*()')).toThrowError(); | ||
}); | ||
|
||
it('throws an error for invalid Base58Check strings', () => { | ||
expect(() => automergeIdToId('11111111111111111111111111111111')).toThrowError(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only used for testing - initially my code creating an automerge ID out of our IDs was broken