@hamak/ui-remote-fs 0.10.1 → 0.12.0
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/dist/api/types/remote-fs-error.types.d.ts +28 -0
- package/dist/api/types/remote-fs-error.types.d.ts.map +1 -1
- package/dist/api/types/remote-fs-error.types.js +18 -0
- package/dist/impl/actions/remote-fs-actions.d.ts +22 -2
- package/dist/impl/actions/remote-fs-actions.d.ts.map +1 -1
- package/dist/impl/actions/remote-fs-actions.js +11 -3
- package/dist/impl/middleware/file-content-type.d.ts +4 -52
- package/dist/impl/middleware/file-content-type.d.ts.map +1 -1
- package/dist/impl/middleware/file-content-type.js +4 -91
- package/dist/impl/middleware/remote-fs-middleware.d.ts +22 -0
- package/dist/impl/middleware/remote-fs-middleware.d.ts.map +1 -1
- package/dist/impl/middleware/remote-fs-middleware.js +293 -22
- package/dist/impl/middleware/same-content.d.ts +23 -0
- package/dist/impl/middleware/same-content.d.ts.map +1 -0
- package/dist/impl/middleware/same-content.js +62 -0
- package/dist/impl/middleware/store-sync-middleware.d.ts +5 -4
- package/dist/impl/middleware/store-sync-middleware.d.ts.map +1 -1
- package/dist/impl/middleware/store-sync-middleware.js +269 -57
- package/dist/impl/plugin/remote-fs-plugin-factory.d.ts +5 -4
- package/dist/impl/plugin/remote-fs-plugin-factory.d.ts.map +1 -1
- package/dist/impl/plugin/remote-fs-plugin-factory.js +1 -2
- package/dist/impl/providers/http-workspace-client.d.ts +5 -1
- package/dist/impl/providers/http-workspace-client.d.ts.map +1 -1
- package/dist/impl/providers/http-workspace-client.js +170 -5
- package/dist/spi/middleware/remote-fs-middleware.types.d.ts +21 -2
- package/dist/spi/middleware/remote-fs-middleware.types.d.ts.map +1 -1
- package/dist/spi/providers/i-workspace-client.d.ts +21 -3
- package/dist/spi/providers/i-workspace-client.d.ts.map +1 -1
- package/package.json +9 -8
|
@@ -9,15 +9,18 @@ import { Pathway } from '@hamak/shared-utils';
|
|
|
9
9
|
import { RemoteFsActionTypes } from '../../api/index.js';
|
|
10
10
|
import { rfsActions, } from '../actions/remote-fs-actions.js';
|
|
11
11
|
import { decodeFileContent } from './file-content-type.js';
|
|
12
|
+
import { sameContent } from './same-content.js';
|
|
12
13
|
/**
|
|
13
14
|
* Create store sync middleware
|
|
14
15
|
*
|
|
15
16
|
* This middleware intercepts remote FS completion actions and synchronizes them
|
|
16
17
|
* to the Redux Store FileSystem. It handles:
|
|
17
|
-
* - LS_COMPLETED: Creates directories and file entries in Store FS
|
|
18
|
+
* - LS_COMPLETED: Creates directories and file entries in Store FS, without
|
|
19
|
+
* disturbing the body of an entry that has already been read (#71)
|
|
18
20
|
* - MKDIR_COMPLETED: Creates directory in Store FS
|
|
19
21
|
* - GET_COMPLETED: Creates/updates file content in Store FS
|
|
20
|
-
* - POST/PUT_COMPLETED:
|
|
22
|
+
* - POST/PUT_COMPLETED: Writes the completion payload's own body into the
|
|
23
|
+
* node, and re-GETs when it carried none (or when `autoReload` says so) (#71)
|
|
21
24
|
* - DELETE_COMPLETED: Removes node from Store FS
|
|
22
25
|
*
|
|
23
26
|
* @param config Middleware configuration
|
|
@@ -27,13 +30,12 @@ import { decodeFileContent } from './file-content-type.js';
|
|
|
27
30
|
* ```typescript
|
|
28
31
|
* const middleware = createStoreSyncMiddleware({
|
|
29
32
|
* fileSystemAdapter: storeFs,
|
|
30
|
-
* pathTranslator: new PathTranslator(Pathway.ofRoot().resolve('remote'))
|
|
31
|
-
* autoReload: true
|
|
33
|
+
* pathTranslator: new PathTranslator(Pathway.ofRoot().resolve('remote'))
|
|
32
34
|
* });
|
|
33
35
|
* ```
|
|
34
36
|
*/
|
|
35
37
|
export function createStoreSyncMiddleware(config) {
|
|
36
|
-
const { fileSystemAdapter, pathTranslator, autoReload
|
|
38
|
+
const { fileSystemAdapter, pathTranslator, autoReload, transformContent, scopeToMount = false, onContentError, } = config;
|
|
37
39
|
const storeFsActions = fileSystemAdapter.getActions();
|
|
38
40
|
// Selects the fs slice out of root state for the existence probe below.
|
|
39
41
|
// Defaults to the adapter's own slice key (`'fs'` in the canonical store) so a
|
|
@@ -41,6 +43,193 @@ export function createStoreSyncMiddleware(config) {
|
|
|
41
43
|
// config.sliceSelector. See amah/app-framework#31.
|
|
42
44
|
const sliceSelector = config.sliceSelector ??
|
|
43
45
|
((state) => state?.[fileSystemAdapter.sliceName]);
|
|
46
|
+
const nodeAt = (store, localPath) => fileSystemAdapter.createSelector(sliceSelector, localPath)(store.getState());
|
|
47
|
+
const keyOf = (localPath) => localPath.join('/');
|
|
48
|
+
/**
|
|
49
|
+
* The name a body is classified by: the one the response carries, else the
|
|
50
|
+
* path's last segment. A mount may address files by id, or a request may be
|
|
51
|
+
* redirected, so the path is not guaranteed to end in the file's own name —
|
|
52
|
+
* and the suffix is the whole basis of the decision. Every path that decodes
|
|
53
|
+
* a body uses this, so they cannot disagree about its type.
|
|
54
|
+
*/
|
|
55
|
+
const fileNameOf = (localPath, data) => data?.name ?? localPath[localPath.length - 1] ?? '';
|
|
56
|
+
/**
|
|
57
|
+
* Was this node edited after `sent` was written?
|
|
58
|
+
*
|
|
59
|
+
* A write completes asynchronously, so the user may have typed on while it
|
|
60
|
+
* was in flight: the node then holds newer text than the one the server
|
|
61
|
+
* stored. Overwriting it would discard those keystrokes AND mark the node
|
|
62
|
+
* clean, leaving nothing to recover them from.
|
|
63
|
+
*
|
|
64
|
+
* Neither signal alone answers it:
|
|
65
|
+
* - "is the node dirty" fires on every ordinary save, because a node is
|
|
66
|
+
* dirty precisely because it was edited and that edit triggered the save;
|
|
67
|
+
* - "does the node differ from what was sent" fires on every programmatic
|
|
68
|
+
* write — a host PUTting generated content the node never held — and
|
|
69
|
+
* would leave the node showing the old bytes.
|
|
70
|
+
* An in-flight edit is the conjunction: the node was edited locally AND no
|
|
71
|
+
* longer holds what was sent. A string sent for a file whose node holds a
|
|
72
|
+
* parsed value is compared after decoding it the same way a read would.
|
|
73
|
+
*
|
|
74
|
+
* Every node store-sync creates carries a memo (see {@link storeContent}),
|
|
75
|
+
* so the dirty half of the test is always available for remote files.
|
|
76
|
+
*/
|
|
77
|
+
const editedSinceSent = (store, localPath, sent, name) => {
|
|
78
|
+
const node = nodeAt(store, localPath);
|
|
79
|
+
if (node?.type !== 'file' || node.content === undefined) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (node.state?.memo?.modified !== true) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
if (sameContent(node.content, sent)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (typeof sent === 'string' && typeof node.content !== 'string') {
|
|
89
|
+
return !sameContent(node.content, decodeFileContent(name, sent).content);
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Put `content`, already in store shape, into the node as the server's copy.
|
|
95
|
+
*
|
|
96
|
+
* A new node is created and then given its content through
|
|
97
|
+
* `setFileContent(fromRemote)` as well: `setFile` seeds no memo, and a node
|
|
98
|
+
* without one has no edit tracking — no dirty indicator ever lights, and the
|
|
99
|
+
* in-flight guard above could never fire for it, so a write echo would
|
|
100
|
+
* silently discard the user's keystrokes. That is exactly the node an
|
|
101
|
+
* on-demand host creates by reading a file before anything lists its
|
|
102
|
+
* directory.
|
|
103
|
+
*/
|
|
104
|
+
const storeContent = (store, localPath, content) => {
|
|
105
|
+
const node = nodeAt(store, localPath);
|
|
106
|
+
if (node === undefined) {
|
|
107
|
+
store.dispatch(storeFsActions.setFile(localPath, content, 'xs:any', {
|
|
108
|
+
override: true,
|
|
109
|
+
contentIsPresent: true,
|
|
110
|
+
}));
|
|
111
|
+
store.dispatch(storeFsActions.setFileContent(localPath, content, true));
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
if (node.type === 'file') {
|
|
115
|
+
// `fromRemote: true` — this is the server's own copy, so it clears the
|
|
116
|
+
// node's dirty memo rather than counting as a local edit.
|
|
117
|
+
store.dispatch(storeFsActions.setFileContent(localPath, content, true));
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
// A directory of the same name: nothing was written.
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Take what a successful write sent as the server's copy.
|
|
125
|
+
*
|
|
126
|
+
* - A string is decoded and transformed exactly as a read would.
|
|
127
|
+
* - A value the node already holds is an ordinary save: it is already in
|
|
128
|
+
* store shape, so it is confirmed as-is — transforming it again would
|
|
129
|
+
* change its shape under the user.
|
|
130
|
+
* - Any other value is a programmatic write, new to the store, so it gets
|
|
131
|
+
* the same `transformContent` a read would give it.
|
|
132
|
+
*/
|
|
133
|
+
const adoptSentContent = (store, localPath, sent, name) => {
|
|
134
|
+
if (sent === undefined) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (typeof sent === 'string') {
|
|
138
|
+
writeContentToStore(store, localPath, { name, content: sent });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const node = nodeAt(store, localPath);
|
|
142
|
+
if (node?.type === 'file' && sameContent(node.content, sent)) {
|
|
143
|
+
storeContent(store, localPath, node.content);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
storeContent(store, localPath, transformContent ? transformContent(sent) : sent);
|
|
147
|
+
};
|
|
148
|
+
/** Extension-state key under which store-sync records what it last synced. */
|
|
149
|
+
const SYNC_STATE_KEY = 'remote-fs';
|
|
150
|
+
/**
|
|
151
|
+
* Remember the server version of a node, so a later listing can tell a file
|
|
152
|
+
* that changed on the server from one that did not (#70, #71).
|
|
153
|
+
*
|
|
154
|
+
* `fileSystemAdapter` is typed loosely and a host may hand in its own; one
|
|
155
|
+
* without `setExtensionState` simply gets no listing invalidation rather
|
|
156
|
+
* than a TypeError on every versioned completion.
|
|
157
|
+
*/
|
|
158
|
+
const recordVersion = (store, localPath, version) => {
|
|
159
|
+
if (typeof version !== 'string' ||
|
|
160
|
+
typeof storeFsActions.setExtensionState !== 'function' ||
|
|
161
|
+
nodeAt(store, localPath) === undefined) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
store.dispatch(storeFsActions.setExtensionState(localPath, SYNC_STATE_KEY, { version }));
|
|
165
|
+
};
|
|
166
|
+
const recordedVersion = (node) => {
|
|
167
|
+
const version = node?.state?.extensionStates?.[SYNC_STATE_KEY]?.version;
|
|
168
|
+
return typeof version === 'string' ? version : undefined;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Refreshes a listing has already asked for, by path, with the version the
|
|
172
|
+
* listing reported. Without it every listing that lands before the GET
|
|
173
|
+
* returns would dispatch the same GET again, and a backend that reports
|
|
174
|
+
* `version` on listings but not on reads would re-fetch the file on every
|
|
175
|
+
* listing forever, because nothing would ever record the new version.
|
|
176
|
+
*/
|
|
177
|
+
const pendingRefresh = new Map();
|
|
178
|
+
/**
|
|
179
|
+
* Write a completion payload's body into the store-fs node at `localPath`.
|
|
180
|
+
*
|
|
181
|
+
* Shared by GET_COMPLETED and POST/PUT_COMPLETED so a file that was just
|
|
182
|
+
* written lands exactly the way a file that was just read does — decoded by
|
|
183
|
+
* the same suffix table, through the same transform, into the same node
|
|
184
|
+
* (#71).
|
|
185
|
+
*
|
|
186
|
+
* A GET calls this unconditionally: an absent body is itself the answer, and
|
|
187
|
+
* storing `undefined` with `contentLoaded: true` is what "this file is empty"
|
|
188
|
+
* has always meant here. A write only calls it when the response carried a
|
|
189
|
+
* body — see {@link carriedBody}. Returns whether anything was written.
|
|
190
|
+
*/
|
|
191
|
+
const writeContentToStore = (store, localPath, data) => {
|
|
192
|
+
// Classify by suffix before parsing, so a non-JSON body (.md, .sql,
|
|
193
|
+
// .txt, .css, .xqm, ...) never hits JSON.parse, and a malformed
|
|
194
|
+
// .json body is contained rather than thrown into the dispatch
|
|
195
|
+
// chain (which would otherwise unwind into createRemoteFsMiddleware's
|
|
196
|
+
// try and turn a successful GET into a spurious GET_FAILED). See #44.
|
|
197
|
+
// Prefer the name the response carries: the request path is not
|
|
198
|
+
// guaranteed to end in the file's own name (a mount addressing files
|
|
199
|
+
// by id, a redirect), and the suffix is the whole basis of the
|
|
200
|
+
// decision. Fall back to the path's last segment.
|
|
201
|
+
const fileName = fileNameOf(localPath, data);
|
|
202
|
+
const decoded = decodeFileContent(fileName, data?.content);
|
|
203
|
+
if (decoded.error) {
|
|
204
|
+
// Reporting only — the failure is already contained by
|
|
205
|
+
// decodeFileContent, which falls back to the raw text. A host that
|
|
206
|
+
// supplies onContentError takes over the reporting entirely, so it
|
|
207
|
+
// does not get its own message plus ours.
|
|
208
|
+
if (onContentError) {
|
|
209
|
+
onContentError(decoded.error, localPath, data?.content ?? '');
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
console.warn(`[ui-remote-fs] store-sync: ${fileName} claims to be JSON but failed to parse; storing raw text.`, decoded.error);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
let content = decoded.content;
|
|
216
|
+
// Apply content transform if provided. Unchanged guard (#31/#44):
|
|
217
|
+
// JSON files still get the parsed value here, bit-identical to
|
|
218
|
+
// before; non-JSON and malformed-JSON-fallback bodies now also
|
|
219
|
+
// reach this point (they used to throw before ever arriving),
|
|
220
|
+
// and are handed to transformContent as-is, same as any other
|
|
221
|
+
// resolved content.
|
|
222
|
+
if (content !== undefined && transformContent) {
|
|
223
|
+
content = transformContent(content);
|
|
224
|
+
}
|
|
225
|
+
return storeContent(store, localPath, content);
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Did this completion payload bring the file's bytes back with it? A write
|
|
229
|
+
* whose response carries the stored `FileInfo` can be echoed straight into
|
|
230
|
+
* the node; one that answers with metadata only cannot.
|
|
231
|
+
*/
|
|
232
|
+
const carriedBody = (data) => data?.content !== undefined && data?.content !== null;
|
|
44
233
|
const storeSyncMiddleware = (store) => (next) => (action) => {
|
|
45
234
|
const result = next(action);
|
|
46
235
|
const anyAction = action;
|
|
@@ -76,10 +265,35 @@ export function createStoreSyncMiddleware(config) {
|
|
|
76
265
|
dispatch(storeFsActions.mkdir(localPath, true, fileInfo.extensions));
|
|
77
266
|
}
|
|
78
267
|
else {
|
|
79
|
-
//
|
|
268
|
+
// A file already read whose version moved on the server is stale.
|
|
269
|
+
// Keep showing it — blanking it is what #71 was about — and fetch
|
|
270
|
+
// the new body instead. Skipped for a node with unsaved edits: the
|
|
271
|
+
// next save of those is refused as a version mismatch (#70), which
|
|
272
|
+
// is where that conflict belongs. A backend without versions gives
|
|
273
|
+
// nothing to compare, so the body is simply kept.
|
|
274
|
+
const existing = nodeAt(store, localPath);
|
|
275
|
+
const known = recordedVersion(existing);
|
|
276
|
+
if (existing?.type === 'file' &&
|
|
277
|
+
existing.state?.contentLoaded &&
|
|
278
|
+
known !== undefined &&
|
|
279
|
+
typeof fileInfo.version === 'string' &&
|
|
280
|
+
fileInfo.version !== known &&
|
|
281
|
+
existing.state?.memo?.modified !== true) {
|
|
282
|
+
const key = keyOf(localPath);
|
|
283
|
+
if (pendingRefresh.get(key) !== fileInfo.version) {
|
|
284
|
+
pendingRefresh.set(key, fileInfo.version);
|
|
285
|
+
dispatch(rfsActions.ofGetRequest(localPath));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// Create the file entry without content. `preserveContent` keeps
|
|
289
|
+
// the body of an entry that was already fetched: a listing carries
|
|
290
|
+
// no content, so replacing the node would blank a document that a
|
|
291
|
+
// surface is displaying (#71). An entry whose content was never
|
|
292
|
+
// loaded is refreshed exactly as before.
|
|
80
293
|
dispatch(storeFsActions.setFile(localPath, undefined, 'xs:any', {
|
|
81
294
|
override: true,
|
|
82
295
|
contentIsPresent: false,
|
|
296
|
+
preserveContent: true,
|
|
83
297
|
extensionStates: fileInfo.extensions,
|
|
84
298
|
}));
|
|
85
299
|
}
|
|
@@ -98,63 +312,61 @@ export function createStoreSyncMiddleware(config) {
|
|
|
98
312
|
const completedAction = anyAction;
|
|
99
313
|
const { request, data } = completedAction.payload;
|
|
100
314
|
const localPath = Pathway.of(request.payload.path).getSegments();
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
// Reporting only — the failure is already contained by
|
|
114
|
-
// decodeFileContent, which falls back to the raw text. A host that
|
|
115
|
-
// supplies onContentError takes over the reporting entirely, so it
|
|
116
|
-
// does not get its own message plus ours.
|
|
117
|
-
if (onContentError) {
|
|
118
|
-
onContentError(decoded.error, localPath, data?.content ?? '');
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
console.warn(`[ui-remote-fs] store-sync: ${fileName} claims to be JSON but failed to parse; storing raw text.`, decoded.error);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
let content = decoded.content;
|
|
125
|
-
// Apply content transform if provided. Unchanged guard (#31/#44):
|
|
126
|
-
// JSON files still get the parsed value here, bit-identical to
|
|
127
|
-
// before; non-JSON and malformed-JSON-fallback bodies now also
|
|
128
|
-
// reach this point (they used to throw before ever arriving),
|
|
129
|
-
// and are handed to transformContent as-is, same as any other
|
|
130
|
-
// resolved content.
|
|
131
|
-
if (content !== undefined && transformContent) {
|
|
132
|
-
content = transformContent(content);
|
|
133
|
-
}
|
|
134
|
-
// Check if file exists in Store FS
|
|
135
|
-
const fileSystemNode = fileSystemAdapter.createSelector(sliceSelector, localPath)(store.getState());
|
|
136
|
-
if (fileSystemNode === undefined) {
|
|
137
|
-
// File doesn't exist, create it with content
|
|
138
|
-
dispatch(storeFsActions.setFile(localPath, content, 'xs:any', {
|
|
139
|
-
override: true,
|
|
140
|
-
contentIsPresent: true,
|
|
141
|
-
}));
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
// File exists, update content only if it's a file
|
|
145
|
-
if (fileSystemNode?.type === 'file') {
|
|
146
|
-
dispatch(storeFsActions.setFileContent(localPath, content, true));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
315
|
+
// A read that reports no version still settles a refresh a listing
|
|
316
|
+
// asked for: the listing's version is the one this body belongs to.
|
|
317
|
+
const listed = pendingRefresh.get(keyOf(localPath));
|
|
318
|
+
pendingRefresh.delete(keyOf(localPath));
|
|
319
|
+
writeContentToStore(store, localPath, data);
|
|
320
|
+
recordVersion(store, localPath, typeof data?.version === 'string' ? data.version : listed);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case RemoteFsActionTypes.GET_FAILED: {
|
|
324
|
+
// Let the next listing try again.
|
|
325
|
+
const failedAction = anyAction;
|
|
326
|
+
pendingRefresh.delete(keyOf(Pathway.of(failedAction.payload.request.payload.path).getSegments()));
|
|
149
327
|
break;
|
|
150
328
|
}
|
|
151
329
|
case RemoteFsActionTypes.POST_COMPLETED:
|
|
152
330
|
case RemoteFsActionTypes.PUT_COMPLETED: {
|
|
153
331
|
const completedAction = anyAction;
|
|
154
|
-
const { request } = completedAction.payload;
|
|
332
|
+
const { request, data } = completedAction.payload;
|
|
155
333
|
const localPath = Pathway.of(request.payload.path).getSegments();
|
|
156
|
-
//
|
|
157
|
-
|
|
334
|
+
// Keystrokes that landed while the write was in flight are newer than
|
|
335
|
+
// anything this completion can tell us, so neither the echo nor the
|
|
336
|
+
// reload may run: both would discard them and mark the node clean. The
|
|
337
|
+
// node stays dirty, so the next save carries them. The server does hold
|
|
338
|
+
// this version now, though, so a later listing must not treat it as a
|
|
339
|
+
// change made elsewhere.
|
|
340
|
+
const name = fileNameOf(localPath, data);
|
|
341
|
+
if (editedSinceSent(store, localPath, request.payload.content, name)) {
|
|
342
|
+
recordVersion(store, localPath, data?.version);
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
// The completion already carries what the server stored, so write it
|
|
346
|
+
// into the node now. Waiting for a re-GET left every surface reading
|
|
347
|
+
// the node showing the OLD bytes until the round-trip landed (#71).
|
|
348
|
+
const echoed = carriedBody(data) && writeContentToStore(store, localPath, data);
|
|
349
|
+
// No body came back, but the write succeeded, so the server holds
|
|
350
|
+
// exactly what was sent: adopt that now rather than after a reload.
|
|
351
|
+
// For an ordinary save it is what the node already holds, and this
|
|
352
|
+
// just clears the dirty flag; for a programmatic write it is the new
|
|
353
|
+
// content. Without this a metadata-only backend with
|
|
354
|
+
// `autoReload: false` would leave every saved file flagged modified
|
|
355
|
+
// forever, and a generated file would keep showing its old bytes.
|
|
356
|
+
if (!echoed) {
|
|
357
|
+
adoptSentContent(store, localPath, request.payload.content, name);
|
|
358
|
+
}
|
|
359
|
+
// After the echo, so a POST that just created the node records too.
|
|
360
|
+
recordVersion(store, localPath, data?.version);
|
|
361
|
+
// Left unset, `autoReload` follows the response: a write that brought
|
|
362
|
+
// its own body needs no round-trip, and one that did not still gets
|
|
363
|
+
// the re-GET it has always had. `@hamak/filesystem-server` answers a
|
|
364
|
+
// write with metadata only, so without this fallback a save would
|
|
365
|
+
// leave the node untouched — the dirty memo would never clear, a POST
|
|
366
|
+
// would never create its node, and extension state synced on GET (the
|
|
367
|
+
// git status badge) would stop refreshing. Setting it forces the
|
|
368
|
+
// behaviour either way.
|
|
369
|
+
if (autoReload ?? !echoed) {
|
|
158
370
|
dispatch(rfsActions.ofGetRequest(localPath));
|
|
159
371
|
}
|
|
160
372
|
break;
|
|
@@ -39,8 +39,10 @@ export interface RemoteFsPluginFactoryConfig {
|
|
|
39
39
|
*/
|
|
40
40
|
jsonIndent?: number;
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
42
|
+
* Re-GET a file after a PUT/POST completes. Left unset it follows the
|
|
43
|
+
* response: skipped when the write answered with `content` (already written
|
|
44
|
+
* into the node), performed when it answered with metadata only. `true`
|
|
45
|
+
* always re-GETs, `false` never does. See amah/app-framework#71.
|
|
44
46
|
*/
|
|
45
47
|
autoReload?: boolean;
|
|
46
48
|
/**
|
|
@@ -80,8 +82,7 @@ export interface RemoteFsPluginFactoryConfig {
|
|
|
80
82
|
* const plugin = createRemoteFsPlugin({
|
|
81
83
|
* workspaceId: '0',
|
|
82
84
|
* mountPoint: Pathway.ofRoot().resolve('remote'),
|
|
83
|
-
* baseUrl: 'http://localhost:3000/api'
|
|
84
|
-
* autoReload: true
|
|
85
|
+
* baseUrl: 'http://localhost:3000/api'
|
|
85
86
|
* });
|
|
86
87
|
* ```
|
|
87
88
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-fs-plugin-factory.d.ts","sourceRoot":"","sources":["../../../src/impl/plugin/remote-fs-plugin-factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAKnD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,WAAW,CAAC;AAU9E;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,UAAU,EAAE,OAAO,CAAC;IAEpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB
|
|
1
|
+
{"version":3,"file":"remote-fs-plugin-factory.d.ts","sourceRoot":"","sources":["../../../src/impl/plugin/remote-fs-plugin-factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAKnD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,WAAW,CAAC;AAU9E;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,UAAU,EAAE,OAAO,CAAC;IAEpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,GAAG,CAAC;IAEzC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAEhC;;OAEG;IACH,oBAAoB,CAAC,EAAE,eAAe,CAAC;CACxC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,2BAA2B,GAAG,YAAY,CA0ItF;AAED,eAAe,oBAAoB,CAAC"}
|
|
@@ -29,8 +29,7 @@ import { STORE_MANAGER_TOKEN, STORE_EXTENSIONS_TOKEN, } from '@hamak/ui-store-ap
|
|
|
29
29
|
* const plugin = createRemoteFsPlugin({
|
|
30
30
|
* workspaceId: '0',
|
|
31
31
|
* mountPoint: Pathway.ofRoot().resolve('remote'),
|
|
32
|
-
* baseUrl: 'http://localhost:3000/api'
|
|
33
|
-
* autoReload: true
|
|
32
|
+
* baseUrl: 'http://localhost:3000/api'
|
|
34
33
|
* });
|
|
35
34
|
* ```
|
|
36
35
|
*/
|
|
@@ -98,7 +98,11 @@ export declare class HttpWorkspaceClient implements IWorkspaceClient {
|
|
|
98
98
|
*/
|
|
99
99
|
readFile(path: string[], options?: RequestOptions): Promise<FileInfo>;
|
|
100
100
|
/**
|
|
101
|
-
* Write or update a file's content (uses PUT method)
|
|
101
|
+
* Write or update a file's content (uses PUT method).
|
|
102
|
+
*
|
|
103
|
+
* With `options.ifMatch` set to a version, the request carries
|
|
104
|
+
* `If-Match: "<version>"` and the server only writes when the file still has
|
|
105
|
+
* that version; otherwise it rejects with a `VersionMismatchError` (#70).
|
|
102
106
|
*/
|
|
103
107
|
putFile(path: string[], content: any, options?: RequestOptions): Promise<FileInfo>;
|
|
104
108
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-workspace-client.d.ts","sourceRoot":"","sources":["../../../src/impl/providers/http-workspace-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAc,EAAE,aAAa,EAAc,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"http-workspace-client.d.ts","sourceRoot":"","sources":["../../../src/impl/providers/http-workspace-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAc,EAAE,aAAa,EAAc,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AA4G/C;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;IAE9B;;;;;;;;;OASG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,mBAAoB,YAAW,gBAAgB;IAC1D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAgB;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,MAAM,EAAE,yBAAyB;IAoB7C;;OAEG;IACH,OAAO,CAAC,UAAU;IAIlB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAmBhB;;OAEG;IACH,OAAO,CAAC,WAAW;IAsHnB;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;IAYxB;;OAEG;IACG,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAc9E;;OAEG;IACG,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IAclF;;OAEG;IACG,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IAc3E;;;;;;OAMG;IACG,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IAoBxF;;OAEG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IAc7E;;OAEG;IACH,cAAc,IAAI,MAAM;IAIxB;;OAEG;IACH,UAAU,IAAI,MAAM;CAGrB;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,IAAI,CAAC,yBAAyB,EAAE,aAAa,GAAG,SAAS,CAAC,GACnE,mBAAmB,CAMrB"}
|
|
@@ -5,6 +5,84 @@
|
|
|
5
5
|
* Migrated from amk/libs/server/ws/ws-client/src/lib/server-ws-ws-client.ts
|
|
6
6
|
*/
|
|
7
7
|
import axios from 'axios';
|
|
8
|
+
import { isErrorObject, VERSION_MISMATCH, } from '../../api/index.js';
|
|
9
|
+
/**
|
|
10
|
+
* Resolve a non-blank message from an ordered list of candidates, falling
|
|
11
|
+
* back to the underlying axios message, then a fixed sentence.
|
|
12
|
+
*
|
|
13
|
+
* Both the structured-passthrough path and the raw-body path in
|
|
14
|
+
* {@link HttpWorkspaceClient.handleError} need to pick a message out of a few
|
|
15
|
+
* candidate fields in priority order, skipping any that are blank/nullish —
|
|
16
|
+
* a body like `{ message: '' }` must not win over a usable `error` field, and
|
|
17
|
+
* a candidate that is present but empty must not surface as an empty message
|
|
18
|
+
* (#77 second review, findings B/C).
|
|
19
|
+
*/
|
|
20
|
+
function resolveMessage(candidates, axiosMessage) {
|
|
21
|
+
for (const candidate of candidates) {
|
|
22
|
+
if (typeof candidate === 'string' && candidate.trim().length > 0) {
|
|
23
|
+
return candidate;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (typeof axiosMessage === 'string' && axiosMessage.trim().length > 0) {
|
|
27
|
+
return axiosMessage;
|
|
28
|
+
}
|
|
29
|
+
return 'Unknown error occurred';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Render an `error` field that is not an {@link ErrorObject} as a sentence,
|
|
33
|
+
* so a body carrying no `message` still reaches the screen as something
|
|
34
|
+
* better than "Unknown error".
|
|
35
|
+
*
|
|
36
|
+
* `null`/`undefined` render as `''` rather than the literal words "null" or
|
|
37
|
+
* "undefined" — the caller treats an empty result as "no usable message" and
|
|
38
|
+
* falls back accordingly (#77 review). An empty object (`{}`) or empty array
|
|
39
|
+
* (`[]`) also render as `''` — `JSON.stringify` would otherwise turn those
|
|
40
|
+
* into the literal text "{}" or "[]", which reads as a message but carries no
|
|
41
|
+
* information, defeating the axios-message fallback (#77 third review,
|
|
42
|
+
* finding 2).
|
|
43
|
+
*/
|
|
44
|
+
function stringifyError(error) {
|
|
45
|
+
if (error === null || error === undefined) {
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
if (typeof error === 'string') {
|
|
49
|
+
return error;
|
|
50
|
+
}
|
|
51
|
+
if (error instanceof Error) {
|
|
52
|
+
return error.message;
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(error)) {
|
|
55
|
+
if (error.length === 0) {
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (typeof error === 'object') {
|
|
60
|
+
if (Object.keys(error).length === 0) {
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
const nestedMessage = error.message;
|
|
64
|
+
if (typeof nestedMessage === 'string' && nestedMessage.trim().length > 0) {
|
|
65
|
+
return nestedMessage;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(error);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return String(error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A non-null, non-array object — safe to spread as `params`.
|
|
77
|
+
*
|
|
78
|
+
* `isErrorObject` only checks `code`/`message`, so `data.error.params` can be
|
|
79
|
+
* anything the server sent, including a string or an array; spreading either
|
|
80
|
+
* one produces numeric-index junk keys rather than being dropped (#77 third
|
|
81
|
+
* review, finding 1).
|
|
82
|
+
*/
|
|
83
|
+
function isPlainObjectParams(candidate) {
|
|
84
|
+
return typeof candidate === 'object' && candidate !== null && !Array.isArray(candidate);
|
|
85
|
+
}
|
|
8
86
|
/**
|
|
9
87
|
* HTTP-based implementation of IWorkspaceClient
|
|
10
88
|
*
|
|
@@ -96,9 +174,88 @@ export class HttpWorkspaceClient {
|
|
|
96
174
|
handleError(error) {
|
|
97
175
|
if (axios.isAxiosError(error)) {
|
|
98
176
|
const axiosError = error;
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
177
|
+
// A refused conditional write (#70): 412 with the server's own body,
|
|
178
|
+
// surfaced as a typed error so the caller can tell "the file moved"
|
|
179
|
+
// from "the request failed".
|
|
180
|
+
if (axiosError.response?.status === 412 && axiosError.response.data?.code === VERSION_MISMATCH) {
|
|
181
|
+
const mismatch = {
|
|
182
|
+
code: VERSION_MISMATCH,
|
|
183
|
+
message: 'The file changed on the server since it was read; nothing was written',
|
|
184
|
+
params: {
|
|
185
|
+
current: axiosError.response.data.current ?? undefined,
|
|
186
|
+
status: 412,
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
throw mismatch;
|
|
190
|
+
}
|
|
191
|
+
// The server answered with a body of its own (amah/app-framework#77).
|
|
192
|
+
// Keep every field it sent: `message` is the sentence written for a
|
|
193
|
+
// person, `code` lets the host branch on the cause, and `params.body`
|
|
194
|
+
// carries the rest — `hint` above all, which a host renders as a link.
|
|
195
|
+
//
|
|
196
|
+
// The plain ErrorObject shape (`{ code, message }`, no `error` key) is
|
|
197
|
+
// just as valid a server response as one carrying `error`, so the
|
|
198
|
+
// branch has to fire on any of the three fields, not just `error`
|
|
199
|
+
// (#77 review).
|
|
200
|
+
const data = axiosError.response?.data;
|
|
201
|
+
if (data &&
|
|
202
|
+
typeof data === 'object' &&
|
|
203
|
+
(data.error !== undefined || typeof data.message === 'string' || typeof data.code === 'string')) {
|
|
204
|
+
// A server that already speaks ErrorObject is passed through, but
|
|
205
|
+
// still merged with `params.status`/`params.body` like the other
|
|
206
|
+
// path below — otherwise a sibling field such as `hint` next to the
|
|
207
|
+
// nested error object would be silently dropped (#77 review).
|
|
208
|
+
//
|
|
209
|
+
// The transport fields (`status`/`statusText`/`body`) are spread
|
|
210
|
+
// LAST, after the nested object's own `params`, so they always win:
|
|
211
|
+
// a server-supplied `params.status` or `params.body` must not shadow
|
|
212
|
+
// the real HTTP status/body, and `hint` (whether top-level or nested
|
|
213
|
+
// under `params`) must survive. Other server param keys (e.g. `path`)
|
|
214
|
+
// still pass through untouched (#77 second review, finding A).
|
|
215
|
+
if (isErrorObject(data.error)) {
|
|
216
|
+
const passthroughMessage = resolveMessage([data.error.message, typeof data.message === 'string' ? data.message : undefined], axiosError.message);
|
|
217
|
+
const passthroughCode = data.error.code.trim().length > 0 ? data.error.code : 'REMOTE';
|
|
218
|
+
// `data.error.params` is only ever spread when it is itself a safe
|
|
219
|
+
// object to spread — a server that sent `params: 'oops'` (a string)
|
|
220
|
+
// would otherwise explode into numeric-index keys ({"0":"o",...})
|
|
221
|
+
// rather than being ignored (#77 third review, finding 1).
|
|
222
|
+
const nestedParams = isPlainObjectParams(data.error.params)
|
|
223
|
+
? data.error.params
|
|
224
|
+
: undefined;
|
|
225
|
+
const passthrough = {
|
|
226
|
+
...data.error,
|
|
227
|
+
code: passthroughCode,
|
|
228
|
+
message: passthroughMessage,
|
|
229
|
+
params: {
|
|
230
|
+
...nestedParams,
|
|
231
|
+
status: axiosError.response?.status,
|
|
232
|
+
statusText: axiosError.response?.statusText,
|
|
233
|
+
body: data,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
throw passthrough;
|
|
237
|
+
}
|
|
238
|
+
// Resolve a non-empty message: the server's own `message`, else the
|
|
239
|
+
// stringified `error` field, else the underlying axios message, else
|
|
240
|
+
// a fixed fallback. An empty/blank/nullish candidate at any step is
|
|
241
|
+
// never used — a body like `{ error: '' }` or `{ error: null }`
|
|
242
|
+
// must not produce a blank or literal "null" message (#77 review).
|
|
243
|
+
const message = resolveMessage([data.message, stringifyError(data.error)], axiosError.message);
|
|
244
|
+
// `code` is only ever taken from the body when it is actually a
|
|
245
|
+
// string — a numeric HTTP-status-like `code` must not leak through
|
|
246
|
+
// as an ErrorObject's `code`, which callers expect to be a string
|
|
247
|
+
// (#77 review).
|
|
248
|
+
const code = typeof data.code === 'string' && data.code.length > 0 ? data.code : 'REMOTE';
|
|
249
|
+
const fromBody = {
|
|
250
|
+
code,
|
|
251
|
+
message,
|
|
252
|
+
params: {
|
|
253
|
+
status: axiosError.response?.status,
|
|
254
|
+
statusText: axiosError.response?.statusText,
|
|
255
|
+
body: data,
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
throw fromBody;
|
|
102
259
|
}
|
|
103
260
|
// Create error from axios error
|
|
104
261
|
const errorObj = {
|
|
@@ -181,14 +338,22 @@ export class HttpWorkspaceClient {
|
|
|
181
338
|
}
|
|
182
339
|
}
|
|
183
340
|
/**
|
|
184
|
-
* Write or update a file's content (uses PUT method)
|
|
341
|
+
* Write or update a file's content (uses PUT method).
|
|
342
|
+
*
|
|
343
|
+
* With `options.ifMatch` set to a version, the request carries
|
|
344
|
+
* `If-Match: "<version>"` and the server only writes when the file still has
|
|
345
|
+
* that version; otherwise it rejects with a `VersionMismatchError` (#70).
|
|
185
346
|
*/
|
|
186
347
|
async putFile(path, content, options) {
|
|
187
348
|
try {
|
|
188
349
|
const formattedPath = this.formatPath(path);
|
|
189
350
|
const serializedContent = this.serializeContent(content);
|
|
190
351
|
const url = this.buildUrl(`/${this.workspaceId}/put/${formattedPath}`, options);
|
|
191
|
-
const
|
|
352
|
+
const ifMatch = options?.ifMatch;
|
|
353
|
+
const config = typeof ifMatch === 'string' && ifMatch.length > 0
|
|
354
|
+
? { headers: { 'If-Match': `"${ifMatch}"` } }
|
|
355
|
+
: undefined;
|
|
356
|
+
const response = await this.axios.put(url, { content: serializedContent }, config);
|
|
192
357
|
return response.data;
|
|
193
358
|
}
|
|
194
359
|
catch (error) {
|