@t2nagi/filebrowser-quantum-mcp 0.0.0-stage → 0.1.0-rc.1
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/LICENSE +202 -0
- package/NOTICE +21 -0
- package/README.ja.md +335 -0
- package/README.md +328 -2
- package/dist/client/api.js +222 -0
- package/dist/client/errors.js +120 -0
- package/dist/client/http.js +172 -0
- package/dist/client/types.js +2 -0
- package/dist/config.js +168 -0
- package/dist/core/approvals.js +75 -0
- package/dist/core/confirm.js +66 -0
- package/dist/core/content.js +28 -0
- package/dist/core/display.js +65 -0
- package/dist/core/inspect.js +180 -0
- package/dist/core/lock.js +54 -0
- package/dist/core/messages.js +131 -0
- package/dist/core/names.js +114 -0
- package/dist/core/paths.js +41 -0
- package/dist/core/reads.js +39 -0
- package/dist/core/recovery.js +55 -0
- package/dist/core/replace.js +131 -0
- package/dist/core/shares.js +46 -0
- package/dist/core/sources.js +18 -0
- package/dist/core/state.js +154 -0
- package/dist/core/text.js +44 -0
- package/dist/core/writeflow.js +497 -0
- package/dist/index.js +83 -0
- package/dist/log.js +32 -0
- package/dist/server.js +117 -0
- package/dist/tools/archive.js +159 -0
- package/dist/tools/common.js +68 -0
- package/dist/tools/create-folder.js +94 -0
- package/dist/tools/delete.js +195 -0
- package/dist/tools/find-duplicates.js +59 -0
- package/dist/tools/get-info.js +53 -0
- package/dist/tools/index.js +45 -0
- package/dist/tools/list-dir.js +68 -0
- package/dist/tools/list-sources.js +45 -0
- package/dist/tools/move-copy.js +231 -0
- package/dist/tools/read-file.js +69 -0
- package/dist/tools/rename.js +99 -0
- package/dist/tools/search.js +93 -0
- package/dist/tools/shares.js +219 -0
- package/dist/tools/unarchive.js +147 -0
- package/dist/tools/write-common.js +89 -0
- package/dist/tools/write-file.js +160 -0
- package/dist/version.js +5 -0
- package/npm-shrinkwrap.json +2657 -0
- package/package.json +57 -4
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Record of a write that timed out or was cancelled after it was sent (design §5.4).
|
|
2
|
+
// Attached once as `previousOperation` to the next tool result.
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { sanitizeServerMessage } from "../client/errors.js";
|
|
5
|
+
const TTL_MS = 60 * 60 * 1000;
|
|
6
|
+
export const plannedOperationSchema = z.strictObject({
|
|
7
|
+
fromSource: z.string().optional(),
|
|
8
|
+
from: z.string().optional(),
|
|
9
|
+
toSource: z.string(),
|
|
10
|
+
to: z.string(),
|
|
11
|
+
});
|
|
12
|
+
export const previousOperationSchema = z
|
|
13
|
+
.strictObject({
|
|
14
|
+
tool: z.string(),
|
|
15
|
+
at: z.string(),
|
|
16
|
+
plannedOperations: z.array(plannedOperationSchema),
|
|
17
|
+
tempPaths: z.array(z.string()),
|
|
18
|
+
sent: z.boolean(),
|
|
19
|
+
outcome: z.literal("unknown"),
|
|
20
|
+
message: z.string(),
|
|
21
|
+
})
|
|
22
|
+
.describe("A previous write that timed out or was cancelled after it was sent. It may have completed on the server: check the state (fb_list_dir, fb_get_info) before retrying.");
|
|
23
|
+
export class RecoveryLog {
|
|
24
|
+
log;
|
|
25
|
+
now;
|
|
26
|
+
last;
|
|
27
|
+
constructor(log, now = Date.now) {
|
|
28
|
+
this.log = log;
|
|
29
|
+
this.now = now;
|
|
30
|
+
}
|
|
31
|
+
record(tool, ops, tempPaths, message) {
|
|
32
|
+
const entry = {
|
|
33
|
+
tool,
|
|
34
|
+
at: new Date(this.now()).toISOString(),
|
|
35
|
+
plannedOperations: ops,
|
|
36
|
+
tempPaths,
|
|
37
|
+
sent: true,
|
|
38
|
+
outcome: "unknown",
|
|
39
|
+
message: sanitizeServerMessage(message),
|
|
40
|
+
};
|
|
41
|
+
this.last = entry;
|
|
42
|
+
// warn: visible with the default log level. Source names and in-source paths only.
|
|
43
|
+
this.log.warn(`${tool}: the request was sent but its result is unknown (${entry.message}). Planned: ${ops
|
|
44
|
+
.map((o) => `${o.fromSource ? `${o.fromSource}:${o.from} -> ` : ""}${o.toSource}:${o.to}`)
|
|
45
|
+
.join("; ")}${tempPaths.length ? `; temporary: ${tempPaths.join(", ")}` : ""}`);
|
|
46
|
+
}
|
|
47
|
+
/** The pending record, once. */
|
|
48
|
+
take() {
|
|
49
|
+
const entry = this.last;
|
|
50
|
+
this.last = undefined;
|
|
51
|
+
if (!entry || this.now() - Date.parse(entry.at) > TTL_MS)
|
|
52
|
+
return undefined;
|
|
53
|
+
return entry;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Overwrite by "create under a temporary name, then rename over the target" (design §6.2.5).
|
|
2
|
+
// Used by fb_write_file, fb_copy and fb_archive. A failure while creating leaves the
|
|
3
|
+
// existing file untouched; once the temporary item is sent, the steps run to the end
|
|
4
|
+
// even if the tool call is cancelled.
|
|
5
|
+
import { kindFromItemMessage, sanitizeServerMessage, ToolError } from "../client/errors.js";
|
|
6
|
+
import { baseName, Inspector, parentOf, sameProbe } from "./inspect.js";
|
|
7
|
+
import { temporaryName } from "./names.js";
|
|
8
|
+
import { joinPath } from "./paths.js";
|
|
9
|
+
export async function replaceViaTemp(args) {
|
|
10
|
+
const { client, facts, run, source, target } = args;
|
|
11
|
+
const look = () => new Inspector(client, facts, {});
|
|
12
|
+
// Step 2: a free temporary name next to the target.
|
|
13
|
+
let tempPath;
|
|
14
|
+
for (let i = 0; i < 5 && tempPath === undefined; i++) {
|
|
15
|
+
const candidate = joinPath(parentOf(target), temporaryName(baseName(target)));
|
|
16
|
+
if ((await look().probe(source, candidate)).kind === "absent")
|
|
17
|
+
tempPath = candidate;
|
|
18
|
+
}
|
|
19
|
+
if (tempPath === undefined) {
|
|
20
|
+
throw new ToolError("server_error", "Could not find a free temporary name.", {
|
|
21
|
+
source,
|
|
22
|
+
path: target,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const temp = tempPath;
|
|
26
|
+
const op = {
|
|
27
|
+
...(args.from ? { fromSource: args.from.source, from: args.from.path } : {}),
|
|
28
|
+
toSource: source,
|
|
29
|
+
to: target,
|
|
30
|
+
};
|
|
31
|
+
// Step 3: create under the temporary name.
|
|
32
|
+
try {
|
|
33
|
+
await run.send([op], [temp], () => args.create(temp));
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
if (err instanceof ToolError && err.kind === "timeout" && err.details?.sent === true) {
|
|
37
|
+
// Result unknown: never delete the temporary item (the server may still be writing).
|
|
38
|
+
run.leftovers.push({
|
|
39
|
+
source,
|
|
40
|
+
path: temp,
|
|
41
|
+
kind: "unknown",
|
|
42
|
+
originalStillExists: true,
|
|
43
|
+
intendedPath: target,
|
|
44
|
+
});
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
// A clear failure: the existing file is untouched. Remove what we created, if anything.
|
|
48
|
+
if ((await look()
|
|
49
|
+
.probe(source, temp)
|
|
50
|
+
.catch(() => ({ kind: "unknown" }))).kind === "present") {
|
|
51
|
+
try {
|
|
52
|
+
await client.deleteResource(source, temp);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
run.leftovers.push({
|
|
56
|
+
source,
|
|
57
|
+
path: temp,
|
|
58
|
+
kind: "partial_copy",
|
|
59
|
+
originalStillExists: true,
|
|
60
|
+
intendedPath: target,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
// Step 4: the temporary item must be readable (an exclusion rule may hide it).
|
|
67
|
+
const created = await look().probe(source, temp);
|
|
68
|
+
if (created.kind !== "present") {
|
|
69
|
+
run.leftovers.push({
|
|
70
|
+
source,
|
|
71
|
+
path: temp,
|
|
72
|
+
kind: "unreachable_temp",
|
|
73
|
+
originalStillExists: true,
|
|
74
|
+
intendedPath: target,
|
|
75
|
+
});
|
|
76
|
+
throw new ToolError("server_error", "The new content was written under a temporary name that the server does not let us read (probably an exclusion rule). The existing file was not replaced.", { source, path: target, details: { leftovers: run.leftovers } });
|
|
77
|
+
}
|
|
78
|
+
// Step 5: the target must still be what the user approved.
|
|
79
|
+
const now = await look().probe(source, target);
|
|
80
|
+
if (!sameProbe(now, args.approved)) {
|
|
81
|
+
run.leftovers.push({
|
|
82
|
+
source,
|
|
83
|
+
path: temp,
|
|
84
|
+
kind: "temp_with_new_content",
|
|
85
|
+
originalStillExists: now.kind !== "absent",
|
|
86
|
+
intendedPath: target,
|
|
87
|
+
});
|
|
88
|
+
throw new ToolError("conflict", "The destination changed before it was replaced, so it was left as it is. The new content is under the temporary name in leftovers.", {
|
|
89
|
+
source,
|
|
90
|
+
path: target,
|
|
91
|
+
details: {
|
|
92
|
+
reason: "changed_before_send",
|
|
93
|
+
conflicts: [],
|
|
94
|
+
allowedResolutions: [],
|
|
95
|
+
leftovers: run.leftovers,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
// Step 6: rename over the target (atomic on the server's disk).
|
|
100
|
+
const failure = (message) => {
|
|
101
|
+
run.leftovers.push({
|
|
102
|
+
source,
|
|
103
|
+
path: temp,
|
|
104
|
+
kind: "temp_with_new_content",
|
|
105
|
+
originalStillExists: true,
|
|
106
|
+
intendedPath: target,
|
|
107
|
+
});
|
|
108
|
+
return new ToolError(message ? kindFromItemMessage(message) : "server_error", `Replacing the existing file failed${message ? ` (server: ${sanitizeServerMessage(message)})` : ""}. The existing file is unchanged; the new content is under the temporary name in leftovers. Use fb_rename to move it into place if needed.`, { source, path: target, details: { leftovers: run.leftovers } });
|
|
109
|
+
};
|
|
110
|
+
let result;
|
|
111
|
+
try {
|
|
112
|
+
result = await run.send([{ fromSource: source, from: temp, toSource: source, to: target }], [temp], () => client.moveCopy("rename", [
|
|
113
|
+
{ fromSource: source, fromPath: temp, toSource: source, toPath: target },
|
|
114
|
+
]));
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
if (err instanceof ToolError && err.kind === "timeout") {
|
|
118
|
+
run.leftovers.push({
|
|
119
|
+
source,
|
|
120
|
+
path: temp,
|
|
121
|
+
kind: "unknown",
|
|
122
|
+
originalStillExists: true,
|
|
123
|
+
intendedPath: target,
|
|
124
|
+
});
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
throw failure(err instanceof ToolError ? err.message : "");
|
|
128
|
+
}
|
|
129
|
+
if (result.failed.length > 0)
|
|
130
|
+
throw failure(result.failed[0]?.message ?? "");
|
|
131
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Share-link ids (design §6.3 fb_list_shares). The hash is an anonymous access key, so it
|
|
2
|
+
// never leaves the process: tools return a random id that maps to it here.
|
|
3
|
+
import { randomBytes } from "node:crypto";
|
|
4
|
+
export class ShareIds {
|
|
5
|
+
byId = new Map();
|
|
6
|
+
byHash = new Map();
|
|
7
|
+
idFor(hash) {
|
|
8
|
+
let id = this.byHash.get(hash);
|
|
9
|
+
if (!id) {
|
|
10
|
+
id = `share_${randomBytes(6).toString("hex")}`;
|
|
11
|
+
this.byHash.set(hash, id);
|
|
12
|
+
this.byId.set(id, hash);
|
|
13
|
+
}
|
|
14
|
+
return id;
|
|
15
|
+
}
|
|
16
|
+
hashFor(id) {
|
|
17
|
+
return this.byId.get(id);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The user's own shares in allowed sources and inside the user's scope (design §3.2,
|
|
22
|
+
* §6.2.8). Shares of other users (visible to admin tokens) are never returned.
|
|
23
|
+
*/
|
|
24
|
+
export function ownShares(list, facts) {
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const entry of list) {
|
|
27
|
+
if (!entry.hash || !entry.source || !entry.path || entry.username !== facts.username)
|
|
28
|
+
continue;
|
|
29
|
+
const info = facts.sources.find((s) => s.name === entry.source);
|
|
30
|
+
if (!info)
|
|
31
|
+
continue;
|
|
32
|
+
const scope = info.scope.replace(/\/+$/, "") || "/";
|
|
33
|
+
const path = entry.path.replace(/\/+$/, "") || "/";
|
|
34
|
+
let relative;
|
|
35
|
+
if (scope === "/")
|
|
36
|
+
relative = path;
|
|
37
|
+
else if (path === scope)
|
|
38
|
+
relative = "/";
|
|
39
|
+
else if (path.startsWith(`${scope}/`))
|
|
40
|
+
relative = path.slice(scope.length);
|
|
41
|
+
else
|
|
42
|
+
continue;
|
|
43
|
+
out.push({ hash: entry.hash, source: entry.source, path: relative, entry });
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Source name resolution (design §5.1).
|
|
2
|
+
import { ToolError } from "../client/errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the source for a tool call: argument, then FILEBROWSER_DEFAULT_SOURCE,
|
|
5
|
+
* then the only accessible source. Sources outside FILEBROWSER_ALLOWED_SOURCES (or
|
|
6
|
+
* not accessible to the user) are reported as source_not_found without saying which.
|
|
7
|
+
*/
|
|
8
|
+
export function resolveSource(facts, requested) {
|
|
9
|
+
const name = requested ?? facts.defaultSource ?? undefined;
|
|
10
|
+
if (name === undefined) {
|
|
11
|
+
throw new ToolError("invalid_argument", "No source was given and there is no default. Call fb_list_sources and pass `source`.");
|
|
12
|
+
}
|
|
13
|
+
const info = facts.sources.find((s) => s.name === name);
|
|
14
|
+
if (!info) {
|
|
15
|
+
throw new ToolError("source_not_found", "The source does not exist or is not available. Use fb_list_sources to see the available sources.", { source: name });
|
|
16
|
+
}
|
|
17
|
+
return info;
|
|
18
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Startup checks and the facts they establish (design §3.2, §5.1, §6.1).
|
|
2
|
+
// If FileBrowser cannot be reached at startup, the checks are retried before the
|
|
3
|
+
// first tool call; until they pass, every tool answers network_error.
|
|
4
|
+
import { ToolError } from "../client/errors.js";
|
|
5
|
+
export class FatalStartupError extends Error {
|
|
6
|
+
name = "FatalStartupError";
|
|
7
|
+
}
|
|
8
|
+
export class ServerState {
|
|
9
|
+
config;
|
|
10
|
+
client;
|
|
11
|
+
log;
|
|
12
|
+
onFatal;
|
|
13
|
+
facts;
|
|
14
|
+
readyListeners = [];
|
|
15
|
+
/** Facts if the startup checks have passed, else undefined. */
|
|
16
|
+
get current() {
|
|
17
|
+
return this.facts;
|
|
18
|
+
}
|
|
19
|
+
/** Called once when the checks pass (also when they pass late, on a retry). */
|
|
20
|
+
onReady(listener) {
|
|
21
|
+
this.readyListeners.push(listener);
|
|
22
|
+
}
|
|
23
|
+
pending;
|
|
24
|
+
constructor(config, client, log,
|
|
25
|
+
/** Called when a later check finds an admin token: the process must stop. */
|
|
26
|
+
onFatal) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.client = client;
|
|
29
|
+
this.log = log;
|
|
30
|
+
this.onFatal = onFatal;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Startup check. Fatal problems (admin token, bad default source) throw
|
|
34
|
+
* FatalStartupError. Connection problems are logged and retried later.
|
|
35
|
+
*/
|
|
36
|
+
async initialize() {
|
|
37
|
+
try {
|
|
38
|
+
await this.load();
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
if (err instanceof FatalStartupError)
|
|
42
|
+
throw err;
|
|
43
|
+
if (err instanceof ToolError && err.kind === "unauthorized") {
|
|
44
|
+
throw new FatalStartupError(err.message);
|
|
45
|
+
}
|
|
46
|
+
const reason = isTransportFailure(err)
|
|
47
|
+
? "Could not reach FileBrowser at startup"
|
|
48
|
+
: `The startup checks failed (${err instanceof ToolError ? `${err.kind}: ${err.message}` : "unexpected error"})`;
|
|
49
|
+
this.log.warn(`${reason}. Share tools stay hidden; the checks run again before the first tool call.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Facts for tool handlers. Retries the startup check if it has not passed yet. */
|
|
53
|
+
async ready() {
|
|
54
|
+
if (this.facts)
|
|
55
|
+
return this.facts;
|
|
56
|
+
try {
|
|
57
|
+
return await this.load();
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
if (err instanceof FatalStartupError) {
|
|
61
|
+
this.onFatal(err);
|
|
62
|
+
throw new ToolError("permission_denied", err.message);
|
|
63
|
+
}
|
|
64
|
+
// Keep the real cause (unauthorized, unexpected_redirect, server_error, ...): only
|
|
65
|
+
// failures to reach the server become the generic network_error below.
|
|
66
|
+
if (err instanceof ToolError && !isTransportFailure(err))
|
|
67
|
+
throw err;
|
|
68
|
+
if (!(err instanceof ToolError)) {
|
|
69
|
+
this.log.error(`startup checks: unexpected error: ${err instanceof Error ? err.message : String(err)}`);
|
|
70
|
+
}
|
|
71
|
+
// Carry the cause of the failed check (for example ENOTFOUND: the host name could not
|
|
72
|
+
// be resolved), so that the reason is visible without reading the server log.
|
|
73
|
+
throw new ToolError("network_error", "The startup checks against FileBrowser have not passed yet, so no tool can run. Check FILEBROWSER_URL, that the server is running, and that its host name resolves (see details.cause).", err instanceof ToolError
|
|
74
|
+
? { details: { checkError: err.kind, ...(err.details ?? {}) } }
|
|
75
|
+
: {});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
load() {
|
|
79
|
+
if (!this.pending) {
|
|
80
|
+
this.pending = this.check().then((facts) => {
|
|
81
|
+
this.facts = facts;
|
|
82
|
+
for (const listener of this.readyListeners)
|
|
83
|
+
listener();
|
|
84
|
+
return facts;
|
|
85
|
+
}, (err) => {
|
|
86
|
+
this.pending = undefined;
|
|
87
|
+
throw err;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return this.pending;
|
|
91
|
+
}
|
|
92
|
+
async check() {
|
|
93
|
+
// Independent GETs: send the share probe alongside, and settle it without leaving
|
|
94
|
+
// an unhandled rejection if getSelf fails first.
|
|
95
|
+
const probe = this.client.probeShareList().then(() => ({ ok: true }), (err) => ({ ok: false, err }));
|
|
96
|
+
const self = await this.client.getSelf();
|
|
97
|
+
const isAdmin = checkAdmin(self, this.config, this.log);
|
|
98
|
+
const sources = buildSources(self.scopes ?? [], this.config);
|
|
99
|
+
const userCanShare = self.permissions?.share === true;
|
|
100
|
+
if (this.config.defaultSource && !sources.some((s) => s.name === this.config.defaultSource)) {
|
|
101
|
+
throw new FatalStartupError(`FILEBROWSER_DEFAULT_SOURCE "${this.config.defaultSource}" is not one of the sources this user can access.`);
|
|
102
|
+
}
|
|
103
|
+
const shared = await probe;
|
|
104
|
+
const tokenCanShare = shared.ok;
|
|
105
|
+
if (!shared.ok) {
|
|
106
|
+
if (!(shared.err instanceof ToolError && shared.err.status === 403))
|
|
107
|
+
throw shared.err;
|
|
108
|
+
this.log.warn('The API token has no "share" permission (share/list returned 403): writes into shared folders cannot be detected.');
|
|
109
|
+
}
|
|
110
|
+
const defaultSource = this.config.defaultSource ?? (sources.length === 1 ? (sources[0]?.name ?? null) : null);
|
|
111
|
+
return {
|
|
112
|
+
username: self.username,
|
|
113
|
+
isAdmin,
|
|
114
|
+
userCanShare,
|
|
115
|
+
tokenCanShare,
|
|
116
|
+
sources,
|
|
117
|
+
defaultSource,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function isTransportFailure(err) {
|
|
122
|
+
return !(err instanceof ToolError) || err.kind === "network_error" || err.kind === "timeout";
|
|
123
|
+
}
|
|
124
|
+
function checkAdmin(self, config, log) {
|
|
125
|
+
const admin = self.permissions?.admin;
|
|
126
|
+
if (admin === false)
|
|
127
|
+
return false;
|
|
128
|
+
if (admin === true && config.allowAdminUser) {
|
|
129
|
+
log.warn("Running with an admin user's token (FILEBROWSER_ALLOW_ADMIN_USER=true).");
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
if (admin === true) {
|
|
133
|
+
throw new FatalStartupError("The API token belongs to an admin user. Create a dedicated non-admin user for MCP, or set FILEBROWSER_ALLOW_ADMIN_USER=true if you accept the risk.");
|
|
134
|
+
}
|
|
135
|
+
throw new FatalStartupError("Could not tell whether the API token belongs to an admin user (permissions.admin is missing). Refusing to start.");
|
|
136
|
+
}
|
|
137
|
+
function buildSources(scopes, config) {
|
|
138
|
+
const allowed = config.allowedSources;
|
|
139
|
+
const writable = config.writableSources ?? allowed;
|
|
140
|
+
return scopes
|
|
141
|
+
.filter((s) => typeof s.name === "string" && (!allowed || allowed.includes(s.name)))
|
|
142
|
+
.map((s) => ({
|
|
143
|
+
name: s.name,
|
|
144
|
+
scope: s.scope,
|
|
145
|
+
permissions: {
|
|
146
|
+
view: s.permissions?.view === true,
|
|
147
|
+
download: s.permissions?.download === true,
|
|
148
|
+
modify: s.permissions?.modify === true,
|
|
149
|
+
create: s.permissions?.create === true,
|
|
150
|
+
delete: s.permissions?.delete === true,
|
|
151
|
+
},
|
|
152
|
+
writable: !config.readOnly && (!writable || writable.includes(s.name)),
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// UTF-8 slicing that keeps character boundaries (design §6.3 fb_read_file).
|
|
2
|
+
function isContinuation(byte) {
|
|
3
|
+
return byte !== undefined && (byte & 0xc0) === 0x80;
|
|
4
|
+
}
|
|
5
|
+
/** Length in bytes of the UTF-8 character that starts with this lead byte. */
|
|
6
|
+
function charLength(lead) {
|
|
7
|
+
if (lead < 0x80)
|
|
8
|
+
return 1;
|
|
9
|
+
if (lead >= 0xf0)
|
|
10
|
+
return 4;
|
|
11
|
+
if (lead >= 0xe0)
|
|
12
|
+
return 3;
|
|
13
|
+
if (lead >= 0xc0)
|
|
14
|
+
return 2;
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
export function sliceUtf8(buffer, offset, maxBytes) {
|
|
18
|
+
const length = buffer.length;
|
|
19
|
+
if (offset >= length) {
|
|
20
|
+
return { text: "", start: offset, bytes: 0, nextOffset: null };
|
|
21
|
+
}
|
|
22
|
+
let start = offset;
|
|
23
|
+
while (start < length && isContinuation(buffer[start]))
|
|
24
|
+
start++;
|
|
25
|
+
if (start >= length) {
|
|
26
|
+
return { text: "", start, bytes: 0, nextOffset: null };
|
|
27
|
+
}
|
|
28
|
+
let end = Math.min(start + maxBytes, length);
|
|
29
|
+
if (end < length) {
|
|
30
|
+
// Move back to the start of the character that would be cut.
|
|
31
|
+
while (end > start && isContinuation(buffer[end]))
|
|
32
|
+
end--;
|
|
33
|
+
}
|
|
34
|
+
if (end === start) {
|
|
35
|
+
// maxBytes is smaller than the first character: return that one character anyway.
|
|
36
|
+
end = Math.min(start + charLength(buffer[start] ?? 0), length);
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
text: buffer.subarray(start, end).toString("utf8"),
|
|
40
|
+
start,
|
|
41
|
+
bytes: end - start,
|
|
42
|
+
nextOffset: end < length ? end : null,
|
|
43
|
+
};
|
|
44
|
+
}
|