Skip to content

Commit

Permalink
Store YBlob as bytes, not base64-encoded string
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 5, 2024
1 parent 947c46e commit 5cc02a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
18 changes: 6 additions & 12 deletions jupyter_ydoc/yblob.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import base64
from functools import partial
from typing import Any, Callable, Optional, Union

Expand All @@ -13,10 +12,7 @@
class YBlob(YBaseDoc):
"""
Extends :class:`YBaseDoc`, and represents a blob document.
It is currently encoded as base64 because of:
https://github.com/y-crdt/ypy/issues/108#issuecomment-1377055465
The Y document can be set from bytes or from str, in which case it is assumed to be encoded as
base64.
The Y document is set from bytes.
Schema:
Expand Down Expand Up @@ -46,7 +42,7 @@ def version(self) -> str:
:return: Document's version.
:rtype: str
"""
return "1.0.0"
return "2.0.0"

def get(self) -> bytes:
"""
Expand All @@ -55,18 +51,16 @@ def get(self) -> bytes:
:return: Document's content.
:rtype: bytes
"""
return base64.b64decode(self._ysource.get("base64", "").encode())
return self._ysource.get("bytes", b"")

def set(self, value: Union[bytes, str]) -> None:
def set(self, value: bytes) -> None:
"""
Sets the content of the document.
:param value: The content of the document.
:type value: Union[bytes, str]
:type value: bytes
"""
if isinstance(value, bytes):
value = base64.b64encode(value).decode()
self._ysource["base64"] = value
self._ysource["bytes"] = value

def observe(self, callback: Callable[[str, Any], None]) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ requires-python = ">=3.7"
keywords = ["jupyter", "ypy"]
dependencies = [
"importlib_metadata >=3.6; python_version<'3.10'",
"pycrdt >=0.8.1,<0.9.0",
"pycrdt >=0.8.3,<0.9.0",
]

[[project.authors]]
Expand Down

0 comments on commit 5cc02a0

Please sign in to comment.