@hamak/ui-remote-fs 0.9.1 → 0.9.2

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,55 @@
1
+ /**
2
+ * The **one** place a workspace file's suffix decides its media type — and
3
+ * therefore whether its body is JSON to parse or text to keep verbatim.
4
+ * Lifted from amk's `libs/ui/core/remote-fs/src/lib/file-content-type.ts`
5
+ * (amah/app-framework#44).
6
+ *
7
+ * `store-sync-middleware.ts`'s `GET_COMPLETED` handler used to `JSON.parse`
8
+ * every file body unconditionally, with no `try`/`catch` anywhere in the
9
+ * middleware. Any non-JSON body (`.md`, `.sql`, `.txt`, `.css`, `.xqm`) threw
10
+ * a `SyntaxError` synchronously inside the redux dispatch chain, so the
11
+ * content never reached the store and a successful HTTP fetch turned into a
12
+ * spurious `GET_FAILED`. Classify the body by suffix *before* parsing, and
13
+ * never let a parse failure propagate.
14
+ */
15
+ /** What a file with no recognised suffix is assumed to be: opaque text. */
16
+ export declare const DEFAULT_CONTENT_TYPE = "text/plain";
17
+ /** The JSON media type — the only one whose bodies get parsed. */
18
+ export declare const JSON_CONTENT_TYPE = "application/json";
19
+ /** Media type for a file name, by suffix. Never throws; defaults to text/plain. */
20
+ export declare function contentTypeOf(name: string): string;
21
+ /**
22
+ * Is this file's body JSON? The single predicate both read paths ask.
23
+ * Derived from {@link contentTypeOf} so there is exactly one table.
24
+ */
25
+ export declare function isJsonFileName(name: string): boolean;
26
+ /** Outcome of turning a raw file body into the value a filesystem stores. */
27
+ export interface DecodedFileContent {
28
+ /**
29
+ * The value to store. `undefined` for an absent body (and for an empty JSON
30
+ * body, which has no JSON value); the raw string for any non-JSON file; the
31
+ * parsed value for a JSON file.
32
+ */
33
+ content: unknown;
34
+ /** The media type the decision was taken from. */
35
+ contentType: string;
36
+ /**
37
+ * Set when the file claimed to be JSON but its body did not parse. The error
38
+ * is *reported*, never thrown: `content` then falls back to the raw text so a
39
+ * single malformed file cannot wedge the load of any other.
40
+ */
41
+ error?: Error;
42
+ }
43
+ /**
44
+ * Decode a raw file body for storage, classified by the file's name.
45
+ *
46
+ * Contract:
47
+ * - `undefined` / `null` body → `content: undefined` (nothing was sent).
48
+ * - JSON file, empty body → `content: undefined` (an empty string is not a JSON
49
+ * value; this preserves the historical store-fs behaviour).
50
+ * - JSON file, valid body → parsed value.
51
+ * - JSON file, invalid body → raw text plus `error` (contained, never thrown).
52
+ * - anything else → the raw string, verbatim (`''` stays `''`).
53
+ */
54
+ export declare function decodeFileContent(name: string, raw: string | null | undefined): DecodedFileContent;
55
+ //# sourceMappingURL=file-content-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-content-type.d.ts","sourceRoot":"","sources":["../../../src/impl/middleware/file-content-type.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AA8BH,2EAA2E;AAC3E,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD,kEAAkE;AAClE,eAAO,MAAM,iBAAiB,qBAAqB,CAAC;AAEpD,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKlD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED,6EAA6E;AAC7E,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB,kDAAkD;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAC7B,kBAAkB,CAwBpB"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * The **one** place a workspace file's suffix decides its media type — and
3
+ * therefore whether its body is JSON to parse or text to keep verbatim.
4
+ * Lifted from amk's `libs/ui/core/remote-fs/src/lib/file-content-type.ts`
5
+ * (amah/app-framework#44).
6
+ *
7
+ * `store-sync-middleware.ts`'s `GET_COMPLETED` handler used to `JSON.parse`
8
+ * every file body unconditionally, with no `try`/`catch` anywhere in the
9
+ * middleware. Any non-JSON body (`.md`, `.sql`, `.txt`, `.css`, `.xqm`) threw
10
+ * a `SyntaxError` synchronously inside the redux dispatch chain, so the
11
+ * content never reached the store and a successful HTTP fetch turned into a
12
+ * spurious `GET_FAILED`. Classify the body by suffix *before* parsing, and
13
+ * never let a parse failure propagate.
14
+ */
15
+ /**
16
+ * Suffix → media type, first match wins.
17
+ *
18
+ * The JSON rows are not just `.json`: a few Designer file types carry a JSON
19
+ * body under another suffix and are opened by viewers that expect a parsed
20
+ * value — `.jm` (declared alongside `.jm.json` by the json-mapper plugin
21
+ * manifest) and `.prismapp` (an app manifest, JSON by construction). If a new
22
+ * file type is declared with a JSON body and a non-`.json` suffix, it belongs
23
+ * on this list; that is the whole point of the list being here and nowhere else.
24
+ */
25
+ const SUFFIX_CONTENT_TYPES = [
26
+ [/\.json$/i, 'application/json'],
27
+ [/\.jm$/i, 'application/json'],
28
+ [/\.prismapp$/i, 'application/json'],
29
+ [/\.css$/i, 'text/css'],
30
+ [/\.xqm$/i, 'application/xquery'],
31
+ [/\.xq$/i, 'application/xquery'],
32
+ [/\.xquery$/i, 'application/xquery'],
33
+ [/\.md$/i, 'text/markdown'],
34
+ [/\.sql$/i, 'application/sql'],
35
+ [/\.ya?ml$/i, 'application/yaml'],
36
+ [/\.html?$/i, 'text/html'],
37
+ [/\.svg$/i, 'image/svg+xml'],
38
+ [/\.xml$/i, 'application/xml'],
39
+ [/\.jsx?$/i, 'text/javascript'],
40
+ [/\.tsx?$/i, 'text/typescript'],
41
+ ];
42
+ /** What a file with no recognised suffix is assumed to be: opaque text. */
43
+ export const DEFAULT_CONTENT_TYPE = 'text/plain';
44
+ /** The JSON media type — the only one whose bodies get parsed. */
45
+ export const JSON_CONTENT_TYPE = 'application/json';
46
+ /** Media type for a file name, by suffix. Never throws; defaults to text/plain. */
47
+ export function contentTypeOf(name) {
48
+ for (const [pattern, type] of SUFFIX_CONTENT_TYPES) {
49
+ if (pattern.test(name))
50
+ return type;
51
+ }
52
+ return DEFAULT_CONTENT_TYPE;
53
+ }
54
+ /**
55
+ * Is this file's body JSON? The single predicate both read paths ask.
56
+ * Derived from {@link contentTypeOf} so there is exactly one table.
57
+ */
58
+ export function isJsonFileName(name) {
59
+ return contentTypeOf(name) === JSON_CONTENT_TYPE;
60
+ }
61
+ /**
62
+ * Decode a raw file body for storage, classified by the file's name.
63
+ *
64
+ * Contract:
65
+ * - `undefined` / `null` body → `content: undefined` (nothing was sent).
66
+ * - JSON file, empty body → `content: undefined` (an empty string is not a JSON
67
+ * value; this preserves the historical store-fs behaviour).
68
+ * - JSON file, valid body → parsed value.
69
+ * - JSON file, invalid body → raw text plus `error` (contained, never thrown).
70
+ * - anything else → the raw string, verbatim (`''` stays `''`).
71
+ */
72
+ export function decodeFileContent(name, raw) {
73
+ const contentType = contentTypeOf(name);
74
+ if (raw === undefined || raw === null) {
75
+ return { content: undefined, contentType };
76
+ }
77
+ if (contentType !== JSON_CONTENT_TYPE) {
78
+ return { content: raw, contentType };
79
+ }
80
+ if (raw === '') {
81
+ return { content: undefined, contentType };
82
+ }
83
+ try {
84
+ return { content: JSON.parse(raw), contentType };
85
+ }
86
+ catch (e) {
87
+ return {
88
+ content: raw,
89
+ contentType,
90
+ error: e instanceof Error ? e : new Error(String(e)),
91
+ };
92
+ }
93
+ }
@@ -3,4 +3,5 @@
3
3
  */
4
4
  export * from './remote-fs-middleware.js';
5
5
  export * from './store-sync-middleware.js';
6
+ export * from './file-content-type.js';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/impl/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/impl/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC"}
@@ -3,3 +3,4 @@
3
3
  */
4
4
  export * from './remote-fs-middleware.js';
5
5
  export * from './store-sync-middleware.js';
6
+ export * from './file-content-type.js';
@@ -1 +1 @@
1
- {"version":3,"file":"store-sync-middleware.d.ts","sourceRoot":"","sources":["../../../src/impl/middleware/store-sync-middleware.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAa,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAWtD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,GAAG,GAAG,EAC/C,MAAM,EAAE,yBAAyB,GAChC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC,CAsInB;AAED,eAAe,yBAAyB,CAAC"}
1
+ {"version":3,"file":"store-sync-middleware.d.ts","sourceRoot":"","sources":["../../../src/impl/middleware/store-sync-middleware.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAa,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAYtD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,GAAG,GAAG,EAC/C,MAAM,EAAE,yBAAyB,GAChC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC,CA2KnB;AAED,eAAe,yBAAyB,CAAC"}
@@ -8,6 +8,7 @@
8
8
  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
+ import { decodeFileContent } from './file-content-type.js';
11
12
  /**
12
13
  * Create store sync middleware
13
14
  *
@@ -32,7 +33,7 @@ import { rfsActions, } from '../actions/remote-fs-actions.js';
32
33
  * ```
33
34
  */
34
35
  export function createStoreSyncMiddleware(config) {
35
- const { fileSystemAdapter, autoReload = true, transformContent } = config;
36
+ const { fileSystemAdapter, pathTranslator, autoReload = true, transformContent, scopeToMount = false, } = config;
36
37
  const storeFsActions = fileSystemAdapter.getActions();
37
38
  // Selects the fs slice out of root state for the existence probe below.
38
39
  // Defaults to the adapter's own slice key (`'fs'` in the canonical store) so a
@@ -47,6 +48,18 @@ export function createStoreSyncMiddleware(config) {
47
48
  if (!rfsActions.isRemoteFsAction(anyAction)) {
48
49
  return result;
49
50
  }
51
+ // Mount scoping (#45): a host that registers more than one store-sync
52
+ // middleware — one per mount — otherwise reacts to every other mount's
53
+ // completion actions too, since nothing here previously consulted
54
+ // `pathTranslator`. Opt-in via `scopeToMount` so every existing
55
+ // single-mount consumer keeps today's unscoped behaviour by default.
56
+ if (scopeToMount) {
57
+ const rawPath = anyAction.payload?.request?.payload?.path;
58
+ if (rawPath !== undefined &&
59
+ !pathTranslator.isUnderMountPoint(Pathway.of(rawPath).getSegments())) {
60
+ return result;
61
+ }
62
+ }
50
63
  const dispatch = store.dispatch;
51
64
  switch (anyAction.type) {
52
65
  case RemoteFsActionTypes.LS_COMPLETED: {
@@ -85,13 +98,27 @@ export function createStoreSyncMiddleware(config) {
85
98
  const completedAction = anyAction;
86
99
  const { request, data } = completedAction.payload;
87
100
  const localPath = Pathway.of(request.payload.path).getSegments();
88
- // Parse content
89
- let content = data.content === undefined || data.content === null
90
- ? undefined
91
- : data.content === ''
92
- ? undefined
93
- : JSON.parse(data.content);
94
- // Apply content transform if provided
101
+ // Classify by suffix before parsing, so a non-JSON body (.md, .sql,
102
+ // .txt, .css, .xqm, ...) never hits JSON.parse, and a malformed
103
+ // .json body is contained rather than thrown into the dispatch
104
+ // chain (which would otherwise unwind into createRemoteFsMiddleware's
105
+ // try and turn a successful GET into a spurious GET_FAILED). See #44.
106
+ // Prefer the name the response carries: the request path is not
107
+ // guaranteed to end in the file's own name (a mount addressing files
108
+ // by id, a redirect), and the suffix is the whole basis of the
109
+ // decision. Fall back to the path's last segment.
110
+ const fileName = data?.name ?? localPath[localPath.length - 1] ?? '';
111
+ const decoded = decodeFileContent(fileName, data?.content);
112
+ if (decoded.error) {
113
+ console.warn(`[ui-remote-fs] store-sync: ${fileName} claims to be JSON but failed to parse; storing raw text.`, decoded.error);
114
+ }
115
+ let content = decoded.content;
116
+ // Apply content transform if provided. Unchanged guard (#31/#44):
117
+ // JSON files still get the parsed value here, bit-identical to
118
+ // before; non-JSON and malformed-JSON-fallback bodies now also
119
+ // reach this point (they used to throw before ever arriving),
120
+ // and are handed to transformContent as-is, same as any other
121
+ // resolved content.
95
122
  if (content !== undefined && transformContent) {
96
123
  content = transformContent(content);
97
124
  }
@@ -57,6 +57,20 @@ export interface StoreSyncMiddlewareConfig {
57
57
  * (e.g. `filesystem`) or uses a facade adapter. See amah/app-framework#31.
58
58
  */
59
59
  sliceSelector?: (state: any) => any;
60
+ /**
61
+ * Scope this middleware to its own mount: completion actions whose path
62
+ * (from `request.payload.path`) falls outside `pathTranslator`'s mount
63
+ * point are ignored entirely (no dispatch of any kind).
64
+ *
65
+ * A host that registers more than one `createStoreSyncMiddleware` instance
66
+ * — one per mount — needs this, otherwise each copy reacts to every other
67
+ * mount's completion actions too (see amah/app-framework#45).
68
+ *
69
+ * Default `false`: unscoped, exactly today's behaviour. This keeps every
70
+ * existing single-mount consumer working unchanged; only a host that
71
+ * mounts multiple filesystems needs to opt in.
72
+ */
73
+ scopeToMount?: boolean;
60
74
  }
61
75
  /**
62
76
  * Type for remote FS middleware factory
@@ -1 +1 @@
1
- {"version":3,"file":"remote-fs-middleware.types.d.ts","sourceRoot":"","sources":["../../../src/spi/middleware/remote-fs-middleware.types.ts"],"names":[],"mappings":"AAAA;;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@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamak/ui-remote-fs",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Remote filesystem synchronization for microkernel-based applications",