braid-text 0.2.69 → 0.2.71

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.
@@ -0,0 +1,10 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(node test/test.js:*)",
5
+ "Bash(node:*)"
6
+ ],
7
+ "deny": [],
8
+ "ask": []
9
+ }
10
+ }
package/index.js CHANGED
@@ -1068,6 +1068,11 @@ function create_braid_text() {
1068
1068
  }
1069
1069
  }
1070
1070
 
1071
+ // Populate key_to_filename mapping from existing files
1072
+ var files = (await fs.promises.readdir(braid_text.db_folder))
1073
+ .filter(x => /\.\d+$/.test(x))
1074
+ init_filename_mapping(files)
1075
+
1071
1076
  done()
1072
1077
  })
1073
1078
  await db_folder_init.p
@@ -2202,26 +2207,61 @@ function create_braid_text() {
2202
2207
  return i
2203
2208
  }
2204
2209
 
2205
- function encode_filename(filename) {
2206
- // Swap all "!" and "/" characters
2207
- let swapped = filename.replace(/[!/]/g, (match) => (match === "!" ? "/" : "!"))
2210
+ var {
2211
+ encode_file_path_component, ensure_unique_case_insensitive_path_component
2212
+ } = require('url-file-db/canonical_path')
2213
+
2214
+ // Mapping between keys and their encoded filenames
2215
+ // Populated at init time, used to avoid re-encoding and handle case collisions
2216
+ var key_to_filename = new Map()
2217
+ var ifilenames = new Set()
2218
+
2219
+ function encode_filename(key) {
2220
+ // Return cached encoding if we've seen this key before
2221
+ if (key_to_filename.has(key)) {
2222
+ return key_to_filename.get(key)
2223
+ }
2224
+
2225
+ // Swap all "!" and "/" characters so paths are more readable on disk
2226
+ var swapped = key.replace(/[!/]/g, (match) => (match === "!" ? "/" : "!"))
2208
2227
 
2209
- // Encode the filename using encodeURIComponent()
2210
- let encoded = encodeURIComponent(swapped)
2228
+ // Encode unsafe filesystem characters
2229
+ var encoded = encode_file_path_component(swapped)
2230
+
2231
+ // Resolve case collisions for case-insensitive filesystems (Mac/Windows)
2232
+ encoded = ensure_unique_case_insensitive_path_component(encoded, ifilenames)
2233
+
2234
+ // Cache the mapping
2235
+ key_to_filename.set(key, encoded)
2236
+ ifilenames.add(encoded.toLowerCase())
2211
2237
 
2212
2238
  return encoded
2213
2239
  }
2214
2240
 
2215
2241
  function decode_filename(encodedFilename) {
2216
- // Decode the filename using decodeURIComponent()
2217
- let decoded = decodeURIComponent(encodedFilename)
2242
+ // Decode the filename
2243
+ var decoded = decodeURIComponent(encodedFilename)
2218
2244
 
2219
- // Swap all "/" and "!" characters
2245
+ // Swap all "/" and "!" characters back
2220
2246
  decoded = decoded.replace(/[!/]/g, (match) => (match === "/" ? "!" : "/"))
2221
2247
 
2222
2248
  return decoded
2223
2249
  }
2224
2250
 
2251
+ // Populate key_to_filename mapping from existing files on disk
2252
+ function init_filename_mapping(files) {
2253
+ for (var file of files) {
2254
+ // Extract the encoded key (strip extension like .0, .1, etc.)
2255
+ var encoded = file.replace(/\.\d+$/, '')
2256
+ var key = decode_filename(encoded)
2257
+
2258
+ if (!key_to_filename.has(key)) {
2259
+ key_to_filename.set(key, encoded)
2260
+ ifilenames.add(encoded.toLowerCase())
2261
+ } else throw new Error('filename conflict detected')
2262
+ }
2263
+ }
2264
+
2225
2265
  function validate_version_array(x) {
2226
2266
  if (!Array.isArray(x)) throw new Error(`invalid version array: not an array`)
2227
2267
  x.sort()
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.69",
3
+ "version": "0.2.71",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
7
7
  "homepage": "https://braid.org",
8
8
  "dependencies": {
9
9
  "@braid.org/diamond-types-node": "^2.0.0",
10
- "braid-http": "~1.3.83"
10
+ "braid-http": "~1.3.83",
11
+ "url-file-db": "^0.0.10"
11
12
  }
12
13
  }