braidfs 0.0.65 → 0.0.67
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.
- package/index.js +13 -4
- package/index.sh +16 -24
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -161,8 +161,7 @@ async function main() {
|
|
|
161
161
|
var proxy = await proxy_url.cache[normalize_url(path)]
|
|
162
162
|
|
|
163
163
|
var parents = JSON.parse(decodeURIComponent(m[2]))
|
|
164
|
-
var parent_text = (await braid_text.get(proxy.url,
|
|
165
|
-
{ parents })).body
|
|
164
|
+
var parent_text = proxy?.version_to_text_cache.get(JSON.stringify(parents)) ?? (await braid_text.get(proxy.url, { parents })).body
|
|
166
165
|
|
|
167
166
|
var text = await new Promise(done => {
|
|
168
167
|
const chunks = []
|
|
@@ -462,16 +461,26 @@ async function proxy_url(url) {
|
|
|
462
461
|
// store a recent mapping of content-hashes to their versions,
|
|
463
462
|
// to support the command line: braidfs editing filename < file
|
|
464
463
|
self.hash_to_version_cache = new Map()
|
|
464
|
+
self.version_to_text_cache = new Map()
|
|
465
465
|
function add_to_version_cache(text, version) {
|
|
466
|
+
if (self.hash_to_version_cache.size)
|
|
467
|
+
[...self.hash_to_version_cache.values()].pop().time = Date.now()
|
|
468
|
+
|
|
466
469
|
var hash = sha256(text)
|
|
467
|
-
self.hash_to_version_cache.
|
|
468
|
-
|
|
470
|
+
var value = self.hash_to_version_cache.get(hash)
|
|
471
|
+
if (value) {
|
|
472
|
+
self.hash_to_version_cache.delete(hash)
|
|
473
|
+
self.version_to_text_cache.delete(JSON.stringify(value.version))
|
|
474
|
+
}
|
|
475
|
+
self.hash_to_version_cache.set(hash, { version })
|
|
476
|
+
self.version_to_text_cache.set(JSON.stringify(version), text)
|
|
469
477
|
|
|
470
478
|
var too_old = Date.now() - 30000
|
|
471
479
|
for (var [key, value] of self.hash_to_version_cache) {
|
|
472
480
|
if (value.time > too_old ||
|
|
473
481
|
self.hash_to_version_cache.size <= 1) break
|
|
474
482
|
self.hash_to_version_cache.delete(key)
|
|
483
|
+
self.version_to_text_cache.delete(JSON.stringify(value.version))
|
|
475
484
|
}
|
|
476
485
|
}
|
|
477
486
|
|
package/index.sh
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
|
|
3
|
-
# Resolve the actual path of the script, even if it's a symlink
|
|
4
|
-
SOURCE="${BASH_SOURCE[0]}"
|
|
5
|
-
while [ -h "$SOURCE" ]; do # Resolve $SOURCE until it's no longer a symlink
|
|
6
|
-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
7
|
-
SOURCE="$(readlink "$SOURCE")"
|
|
8
|
-
# If $SOURCE was a relative symlink, resolve it relative to the symlink's directory
|
|
9
|
-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
|
|
10
|
-
done
|
|
11
|
-
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
12
|
-
|
|
13
3
|
# Function to calculate Base64-encoded SHA256 hash
|
|
14
4
|
calculate_sha256() {
|
|
15
5
|
if command -v shasum > /dev/null; then
|
|
16
|
-
|
|
6
|
+
cat | shasum -a 256 | cut -d ' ' -f 1 | xxd -r -p | base64
|
|
17
7
|
elif command -v sha256sum > /dev/null; then
|
|
18
|
-
|
|
8
|
+
cat | sha256sum | cut -d ' ' -f 1 | xxd -r -p | base64
|
|
19
9
|
else
|
|
20
10
|
echo "Error: Neither shasum nor sha256sum is available." >&2
|
|
21
11
|
exit 1
|
|
@@ -64,11 +54,8 @@ if [ "$1" = "editing" ]; then
|
|
|
64
54
|
FILENAME="$(pwd)/$FILENAME"
|
|
65
55
|
fi
|
|
66
56
|
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
# Calculate SHA256 hash
|
|
71
|
-
HASH=$(calculate_sha256 "$INPUT")
|
|
57
|
+
# Calculate SHA256 hash directly from stdin
|
|
58
|
+
HASH=$(calculate_sha256)
|
|
72
59
|
|
|
73
60
|
# Make HTTP request
|
|
74
61
|
RESPONSE=$(curl -s "http://localhost:${PORT}/.braidfs/get_version/$(urlencode "$FILENAME")/$(urlencode "$HASH")")
|
|
@@ -85,15 +72,20 @@ elif [ "$1" = "edited" ]; then
|
|
|
85
72
|
|
|
86
73
|
PARENT_VERSION="$3"
|
|
87
74
|
|
|
88
|
-
#
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
# Make HTTP request
|
|
92
|
-
RESPONSE=$(curl -s -X PUT -d "$INPUT" "http://localhost:${PORT}/.braidfs/set_version/$(urlencode "$FILENAME")/$(urlencode "$PARENT_VERSION")")
|
|
93
|
-
echo "$RESPONSE"
|
|
94
|
-
exit 0
|
|
75
|
+
# Make HTTP request (getting body from stdin)
|
|
76
|
+
curl -s -X PUT --data-binary @- "http://localhost:${PORT}/.braidfs/set_version/$(urlencode "$FILENAME")/$(urlencode "$PARENT_VERSION")"
|
|
95
77
|
|
|
96
78
|
# For all other commands, pass through to the Node.js script
|
|
97
79
|
else
|
|
80
|
+
# Resolve the actual path of the script, even if it's a symlink
|
|
81
|
+
SOURCE="${BASH_SOURCE[0]}"
|
|
82
|
+
while [ -h "$SOURCE" ]; do # Resolve $SOURCE until it's no longer a symlink
|
|
83
|
+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
84
|
+
SOURCE="$(readlink "$SOURCE")"
|
|
85
|
+
# If $SOURCE was a relative symlink, resolve it relative to the symlink's directory
|
|
86
|
+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
|
|
87
|
+
done
|
|
88
|
+
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
89
|
+
|
|
98
90
|
node "$SCRIPT_DIR/index.js" "$@"
|
|
99
91
|
fi
|