braid-text 0.2.75 → 0.2.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +39 -1
  2. package/package.json +1 -1
  3. package/test/tests.js +56 -0
package/index.js CHANGED
@@ -464,7 +464,9 @@ function create_braid_text() {
464
464
  }
465
465
 
466
466
  braid_text.delete = async (key) => {
467
- await braid_text.put(key, {body: ''})
467
+ // Accept either a key string or a resource object
468
+ let resource = (typeof key == 'string') ? await get_resource(key) : key
469
+ await resource.delete()
468
470
  }
469
471
 
470
472
  braid_text.get = async (key, options) => {
@@ -1029,6 +1031,42 @@ function create_braid_text() {
1029
1031
 
1030
1032
  resource.length_cache = createSimpleCache(braid_text.length_cache_size)
1031
1033
 
1034
+ // Add delete method to resource
1035
+ resource.delete = async () => {
1036
+ // Free the diamond-types document
1037
+ if (resource.doc) resource.doc.free()
1038
+
1039
+ // Remove from in-memory cache
1040
+ delete braid_text.cache[key]
1041
+
1042
+ // Remove all files for this key from db_folder
1043
+ if (braid_text.db_folder) {
1044
+ var files = await get_files_for_key(key)
1045
+ for (var file of files) {
1046
+ try {
1047
+ await fs.promises.unlink(file)
1048
+ } catch (e) {
1049
+ // File might not exist, that's ok
1050
+ }
1051
+ }
1052
+
1053
+ // Remove meta file if it exists
1054
+ try {
1055
+ var encoded = encode_filename(key)
1056
+ await fs.promises.unlink(`${braid_text.db_folder}/.meta/${encoded}`)
1057
+ } catch (e) {
1058
+ // Meta file might not exist, that's ok
1059
+ }
1060
+ }
1061
+
1062
+ // Remove from filename mapping
1063
+ if (key_to_filename.has(key)) {
1064
+ var encoded = key_to_filename.get(key)
1065
+ ifilenames.delete(encoded.toLowerCase())
1066
+ key_to_filename.delete(key)
1067
+ }
1068
+ }
1069
+
1032
1070
  done(resource)
1033
1071
  })
1034
1072
  return await cache[key]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.75",
3
+ "version": "0.2.76",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
package/test/tests.js CHANGED
@@ -1840,6 +1840,62 @@ runTest(
1840
1840
  ''
1841
1841
  )
1842
1842
 
1843
+ runTest(
1844
+ "test deleting a resource completely removes all traces",
1845
+ async () => {
1846
+ let key = 'test-delete-complete-' + Math.random().toString(36).slice(2)
1847
+
1848
+ // Create a resource with some content
1849
+ // "hello world" is 11 characters, so version should be alice-10 (positions 0-10 inclusive)
1850
+ await braid_fetch(`/${key}`, {
1851
+ method: 'PUT',
1852
+ version: ['alice-10'],
1853
+ parents: [],
1854
+ body: 'hello world'
1855
+ })
1856
+
1857
+ // Verify it exists in cache using eval endpoint
1858
+ let r1 = await braid_fetch(`/eval`, {
1859
+ method: 'PUT',
1860
+ body: `res.end(braid_text.cache['/${key}'] ? 'exists' : 'missing')`
1861
+ })
1862
+ if ((await r1.text()) !== 'exists') return 'Resource not in cache after creation'
1863
+
1864
+ // Delete the resource
1865
+ await braid_fetch(`/${key}`, {method: 'DELETE'})
1866
+
1867
+ // Verify it's removed from cache
1868
+ let r2 = await braid_fetch(`/eval`, {
1869
+ method: 'PUT',
1870
+ body: `res.end(braid_text.cache['/${key}'] ? 'exists' : 'missing')`
1871
+ })
1872
+ if ((await r2.text()) !== 'missing') return 'Resource still in cache after deletion'
1873
+
1874
+ // Verify we can create it again from scratch with same key
1875
+ // "new content" is 11 characters, so version should be bob-10 (positions 0-10 inclusive)
1876
+ await braid_fetch(`/${key}`, {
1877
+ method: 'PUT',
1878
+ version: ['bob-10'],
1879
+ parents: [],
1880
+ body: 'new content'
1881
+ })
1882
+
1883
+ // Get the new resource and verify it's fresh (not the old one)
1884
+ let r = await braid_fetch(`/${key}`)
1885
+ let body = await r.text()
1886
+
1887
+ if (body !== 'new content') return `Expected 'new content', got '${body}'`
1888
+
1889
+ // Verify the version is from scratch (bob-10, not alice-10)
1890
+ let version = r.headers.get('version')
1891
+ if (!version.includes('bob-10')) return `Expected version to include bob-10, got: ${version}`
1892
+ if (version.includes('alice-')) return `Old version alice-10 should not be present, got: ${version}`
1893
+
1894
+ return 'ok'
1895
+ },
1896
+ 'ok'
1897
+ )
1898
+
1843
1899
  runTest(
1844
1900
  "test getting a binary update from a subscription",
1845
1901
  async () => {