Skip to content

Commit fc99cca

Browse files
davidbrochartpre-commit-ci[bot]github-actions[bot]
authored
Store YBlob as bytes, not base64-encoded string (#209)
* Store YBlob as bytes, not base64-encoded string * Add test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Automatic application of license header --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 947c46e commit fc99cca

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ jupyter_ydoc/_version.py
147147
!.yarn/versions
148148
docs/source/api
149149
docs/source/changelog.md
150+
# pixi environments
151+
.pixi

jupyter_ydoc/yblob.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
import base64
54
from functools import partial
6-
from typing import Any, Callable, Optional, Union
5+
from typing import Any, Callable, Optional
76

87
from pycrdt import Doc, Map
98

@@ -13,10 +12,7 @@
1312
class YBlob(YBaseDoc):
1413
"""
1514
Extends :class:`YBaseDoc`, and represents a blob document.
16-
It is currently encoded as base64 because of:
17-
https://github.com/y-crdt/ypy/issues/108#issuecomment-1377055465
18-
The Y document can be set from bytes or from str, in which case it is assumed to be encoded as
19-
base64.
15+
The Y document is set from bytes.
2016
2117
Schema:
2218
@@ -46,7 +42,7 @@ def version(self) -> str:
4642
:return: Document's version.
4743
:rtype: str
4844
"""
49-
return "1.0.0"
45+
return "2.0.0"
5046

5147
def get(self) -> bytes:
5248
"""
@@ -55,18 +51,16 @@ def get(self) -> bytes:
5551
:return: Document's content.
5652
:rtype: bytes
5753
"""
58-
return base64.b64decode(self._ysource.get("base64", "").encode())
54+
return self._ysource.get("bytes", b"")
5955

60-
def set(self, value: Union[bytes, str]) -> None:
56+
def set(self, value: bytes) -> None:
6157
"""
6258
Sets the content of the document.
6359
6460
:param value: The content of the document.
65-
:type value: Union[bytes, str]
61+
:type value: bytes
6662
"""
67-
if isinstance(value, bytes):
68-
value = base64.b64encode(value).decode()
69-
self._ysource["base64"] = value
63+
self._ysource["bytes"] = value
7064

7165
def observe(self, callback: Callable[[str, Any], None]) -> None:
7266
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ requires-python = ">=3.7"
1313
keywords = ["jupyter", "ypy"]
1414
dependencies = [
1515
"importlib_metadata >=3.6; python_version<'3.10'",
16-
"pycrdt >=0.8.1,<0.9.0",
16+
"pycrdt >=0.8.3,<0.9.0",
1717
]
1818

1919
[[project.authors]]

tests/test_ydocs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
from jupyter_ydoc import YBlob
5+
6+
7+
def test_yblob():
8+
yblob = YBlob()
9+
assert yblob.get() == b""
10+
yblob.set(b"012")
11+
assert yblob.get() == b"012"
12+
changes = []
13+
14+
def callback(topic, event):
15+
print(topic, event)
16+
changes.append((topic, event))
17+
18+
yblob.observe(callback)
19+
yblob.set(b"345")
20+
assert len(changes) == 1
21+
topic, event = changes[0]
22+
assert topic == "source"
23+
assert event.keys["bytes"]["oldValue"] == b"012"
24+
assert event.keys["bytes"]["newValue"] == b"345"

0 commit comments

Comments
 (0)