@hamak/shared-utils 0.10.1 → 0.11.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.
@@ -6,6 +6,14 @@ export interface FileInfo {
6
6
  createdAt: Date;
7
7
  updatedAt: Date;
8
8
  content?: string;
9
+ /**
10
+ * Content version: an opaque token that changes whenever the file's bytes
11
+ * change (a content hash on the filesystem server, see amah/app-framework#70).
12
+ * A client hands it back as a write precondition so that a stale write is
13
+ * refused instead of silently overwriting someone else's edit.
14
+ * Absent on directories and on servers that predate versioning.
15
+ */
16
+ version?: string;
9
17
  /**
10
18
  * Extension data from plugins (e.g., git status, permissions).
11
19
  * Keys are extension identifiers (e.g., 'git'), values are extension-specific data.
@@ -1 +1 @@
1
- {"version":3,"file":"core-utils-file.d.ts","sourceRoot":"","sources":["../src/core-utils-file.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC"}
1
+ {"version":3,"file":"core-utils-file.d.ts","sourceRoot":"","sources":["../src/core-utils-file.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC"}
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ /**
3
+ * The **one** place a workspace file's suffix decides its media type — and
4
+ * therefore whether its body is JSON to parse or text to keep verbatim.
5
+ * Lifted from amk's `libs/ui/core/remote-fs/src/lib/file-content-type.ts`
6
+ * (amah/app-framework#44). Lives in shared-utils since #68 so the shell's view
7
+ * resolvers read the same table the remote-fs read path decodes with;
8
+ * `@hamak/ui-remote-fs` re-exports it unchanged.
9
+ *
10
+ * `store-sync-middleware.ts`'s `GET_COMPLETED` handler used to `JSON.parse`
11
+ * every file body unconditionally, with no `try`/`catch` anywhere in the
12
+ * middleware. Any non-JSON body (`.md`, `.sql`, `.txt`, `.css`, `.xqm`) threw
13
+ * a `SyntaxError` synchronously inside the redux dispatch chain, so the
14
+ * content never reached the store and a successful HTTP fetch turned into a
15
+ * spurious `GET_FAILED`. Classify the body by suffix *before* parsing, and
16
+ * never let a parse failure propagate.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.decodeFileContent = exports.isJsonFileName = exports.contentTypeOf = exports.JSON_CONTENT_TYPE = exports.DEFAULT_CONTENT_TYPE = void 0;
20
+ /**
21
+ * Suffix → media type, first match wins.
22
+ *
23
+ * The JSON rows are not just `.json`: a few Designer file types carry a JSON
24
+ * body under another suffix and are opened by viewers that expect a parsed
25
+ * value — `.jm` (declared alongside `.jm.json` by the json-mapper plugin
26
+ * manifest) and `.prismapp` (an app manifest, JSON by construction). If a new
27
+ * file type is declared with a JSON body and a non-`.json` suffix, it belongs
28
+ * on this list; that is the whole point of the list being here and nowhere else.
29
+ */
30
+ const SUFFIX_CONTENT_TYPES = [
31
+ [/\.json$/i, 'application/json'],
32
+ [/\.jm$/i, 'application/json'],
33
+ [/\.prismapp$/i, 'application/json'],
34
+ [/\.css$/i, 'text/css'],
35
+ [/\.xqm$/i, 'application/xquery'],
36
+ [/\.xq$/i, 'application/xquery'],
37
+ [/\.xquery$/i, 'application/xquery'],
38
+ [/\.md$/i, 'text/markdown'],
39
+ [/\.sql$/i, 'application/sql'],
40
+ [/\.ya?ml$/i, 'application/yaml'],
41
+ [/\.html?$/i, 'text/html'],
42
+ [/\.svg$/i, 'image/svg+xml'],
43
+ [/\.xml$/i, 'application/xml'],
44
+ [/\.jsx?$/i, 'text/javascript'],
45
+ [/\.tsx?$/i, 'text/typescript'],
46
+ ];
47
+ /** What a file with no recognised suffix is assumed to be: opaque text. */
48
+ exports.DEFAULT_CONTENT_TYPE = 'text/plain';
49
+ /** The JSON media type — the only one whose bodies get parsed. */
50
+ exports.JSON_CONTENT_TYPE = 'application/json';
51
+ /** Media type for a file name, by suffix. Never throws; defaults to text/plain. */
52
+ function contentTypeOf(name) {
53
+ for (const [pattern, type] of SUFFIX_CONTENT_TYPES) {
54
+ if (pattern.test(name))
55
+ return type;
56
+ }
57
+ return exports.DEFAULT_CONTENT_TYPE;
58
+ }
59
+ exports.contentTypeOf = contentTypeOf;
60
+ /**
61
+ * Is this file's body JSON? The single predicate both read paths ask.
62
+ * Derived from {@link contentTypeOf} so there is exactly one table.
63
+ */
64
+ function isJsonFileName(name) {
65
+ return contentTypeOf(name) === exports.JSON_CONTENT_TYPE;
66
+ }
67
+ exports.isJsonFileName = isJsonFileName;
68
+ /**
69
+ * Decode a raw file body for storage, classified by the file's name.
70
+ *
71
+ * Contract:
72
+ * - `undefined` / `null` body → `content: undefined` (nothing was sent).
73
+ * - JSON file, empty body → `content: undefined` (an empty string is not a JSON
74
+ * value; this preserves the historical store-fs behaviour).
75
+ * - JSON file, valid body → parsed value.
76
+ * - JSON file, invalid body → raw text plus `error` (contained, never thrown).
77
+ * - anything else → the raw string, verbatim (`''` stays `''`).
78
+ */
79
+ function decodeFileContent(name, raw) {
80
+ const contentType = contentTypeOf(name);
81
+ if (raw === undefined || raw === null) {
82
+ return { content: undefined, contentType };
83
+ }
84
+ if (contentType !== exports.JSON_CONTENT_TYPE) {
85
+ return { content: raw, contentType };
86
+ }
87
+ if (raw === '') {
88
+ return { content: undefined, contentType };
89
+ }
90
+ try {
91
+ return { content: JSON.parse(raw), contentType };
92
+ }
93
+ catch (e) {
94
+ return {
95
+ content: raw,
96
+ contentType,
97
+ error: e instanceof Error ? e : new Error(String(e)),
98
+ };
99
+ }
100
+ }
101
+ exports.decodeFileContent = decodeFileContent;
@@ -47,3 +47,5 @@ __exportStar(require("./core-utils-chrono"), exports);
47
47
  __exportStar(require("./core-utils-comparator"), exports);
48
48
  __exportStar(require("./core-utils-fqn"), exports);
49
49
  __exportStar(require("./core-utils-validations"), exports);
50
+ // File content type (suffix table)
51
+ __exportStar(require("./file-content-type"), exports);
@@ -153,7 +153,7 @@ function navigateStep(from, step, prototype) {
153
153
  if (Array.isArray(from)) {
154
154
  if (step.type === 'lookup') {
155
155
  const stp = step;
156
- const result = from.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => e[propertyName] === propertyValue));
156
+ const result = from.find(e => e !== undefined && e !== null && stp.keys.every(({ propertyName, propertyValue }) => e[propertyName] === propertyValue));
157
157
  if (result === undefined && prototype !== undefined) {
158
158
  from.push(prototype);
159
159
  return prototype;
@@ -231,7 +231,7 @@ class OverlayNavigator {
231
231
  if (Array.isArray(from.children)) {
232
232
  if (step.type === 'lookup') {
233
233
  const stp = step;
234
- const result = from.children.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => { var _a; return ((_a = e.keys) === null || _a === void 0 ? void 0 : _a[propertyName]) === propertyValue; }));
234
+ const result = from.children.find(e => e !== undefined && e !== null && stp.keys.every(({ propertyName, propertyValue }) => { var _a; return ((_a = e.keys) === null || _a === void 0 ? void 0 : _a[propertyName]) === propertyValue; }));
235
235
  return result;
236
236
  }
237
237
  else if (step.type === 'position') {
@@ -0,0 +1,57 @@
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). Lives in shared-utils since #68 so the shell's view
6
+ * resolvers read the same table the remote-fs read path decodes with;
7
+ * `@hamak/ui-remote-fs` re-exports it unchanged.
8
+ *
9
+ * `store-sync-middleware.ts`'s `GET_COMPLETED` handler used to `JSON.parse`
10
+ * every file body unconditionally, with no `try`/`catch` anywhere in the
11
+ * middleware. Any non-JSON body (`.md`, `.sql`, `.txt`, `.css`, `.xqm`) threw
12
+ * a `SyntaxError` synchronously inside the redux dispatch chain, so the
13
+ * content never reached the store and a successful HTTP fetch turned into a
14
+ * spurious `GET_FAILED`. Classify the body by suffix *before* parsing, and
15
+ * never let a parse failure propagate.
16
+ */
17
+ /** What a file with no recognised suffix is assumed to be: opaque text. */
18
+ export declare const DEFAULT_CONTENT_TYPE = "text/plain";
19
+ /** The JSON media type — the only one whose bodies get parsed. */
20
+ export declare const JSON_CONTENT_TYPE = "application/json";
21
+ /** Media type for a file name, by suffix. Never throws; defaults to text/plain. */
22
+ export declare function contentTypeOf(name: string): string;
23
+ /**
24
+ * Is this file's body JSON? The single predicate both read paths ask.
25
+ * Derived from {@link contentTypeOf} so there is exactly one table.
26
+ */
27
+ export declare function isJsonFileName(name: string): boolean;
28
+ /** Outcome of turning a raw file body into the value a filesystem stores. */
29
+ export interface DecodedFileContent {
30
+ /**
31
+ * The value to store. `undefined` for an absent body (and for an empty JSON
32
+ * body, which has no JSON value); the raw string for any non-JSON file; the
33
+ * parsed value for a JSON file.
34
+ */
35
+ content: unknown;
36
+ /** The media type the decision was taken from. */
37
+ contentType: string;
38
+ /**
39
+ * Set when the file claimed to be JSON but its body did not parse. The error
40
+ * is *reported*, never thrown: `content` then falls back to the raw text so a
41
+ * single malformed file cannot wedge the load of any other.
42
+ */
43
+ error?: Error;
44
+ }
45
+ /**
46
+ * Decode a raw file body for storage, classified by the file's name.
47
+ *
48
+ * Contract:
49
+ * - `undefined` / `null` body → `content: undefined` (nothing was sent).
50
+ * - JSON file, empty body → `content: undefined` (an empty string is not a JSON
51
+ * value; this preserves the historical store-fs behaviour).
52
+ * - JSON file, valid body → parsed value.
53
+ * - JSON file, invalid body → raw text plus `error` (contained, never thrown).
54
+ * - anything else → the raw string, verbatim (`''` stays `''`).
55
+ */
56
+ export declare function decodeFileContent(name: string, raw: string | null | undefined): DecodedFileContent;
57
+ //# sourceMappingURL=file-content-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-content-type.d.ts","sourceRoot":"","sources":["../src/file-content-type.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;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,95 @@
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). Lives in shared-utils since #68 so the shell's view
6
+ * resolvers read the same table the remote-fs read path decodes with;
7
+ * `@hamak/ui-remote-fs` re-exports it unchanged.
8
+ *
9
+ * `store-sync-middleware.ts`'s `GET_COMPLETED` handler used to `JSON.parse`
10
+ * every file body unconditionally, with no `try`/`catch` anywhere in the
11
+ * middleware. Any non-JSON body (`.md`, `.sql`, `.txt`, `.css`, `.xqm`) threw
12
+ * a `SyntaxError` synchronously inside the redux dispatch chain, so the
13
+ * content never reached the store and a successful HTTP fetch turned into a
14
+ * spurious `GET_FAILED`. Classify the body by suffix *before* parsing, and
15
+ * never let a parse failure propagate.
16
+ */
17
+ /**
18
+ * Suffix → media type, first match wins.
19
+ *
20
+ * The JSON rows are not just `.json`: a few Designer file types carry a JSON
21
+ * body under another suffix and are opened by viewers that expect a parsed
22
+ * value — `.jm` (declared alongside `.jm.json` by the json-mapper plugin
23
+ * manifest) and `.prismapp` (an app manifest, JSON by construction). If a new
24
+ * file type is declared with a JSON body and a non-`.json` suffix, it belongs
25
+ * on this list; that is the whole point of the list being here and nowhere else.
26
+ */
27
+ const SUFFIX_CONTENT_TYPES = [
28
+ [/\.json$/i, 'application/json'],
29
+ [/\.jm$/i, 'application/json'],
30
+ [/\.prismapp$/i, 'application/json'],
31
+ [/\.css$/i, 'text/css'],
32
+ [/\.xqm$/i, 'application/xquery'],
33
+ [/\.xq$/i, 'application/xquery'],
34
+ [/\.xquery$/i, 'application/xquery'],
35
+ [/\.md$/i, 'text/markdown'],
36
+ [/\.sql$/i, 'application/sql'],
37
+ [/\.ya?ml$/i, 'application/yaml'],
38
+ [/\.html?$/i, 'text/html'],
39
+ [/\.svg$/i, 'image/svg+xml'],
40
+ [/\.xml$/i, 'application/xml'],
41
+ [/\.jsx?$/i, 'text/javascript'],
42
+ [/\.tsx?$/i, 'text/typescript'],
43
+ ];
44
+ /** What a file with no recognised suffix is assumed to be: opaque text. */
45
+ export const DEFAULT_CONTENT_TYPE = 'text/plain';
46
+ /** The JSON media type — the only one whose bodies get parsed. */
47
+ export const JSON_CONTENT_TYPE = 'application/json';
48
+ /** Media type for a file name, by suffix. Never throws; defaults to text/plain. */
49
+ export function contentTypeOf(name) {
50
+ for (const [pattern, type] of SUFFIX_CONTENT_TYPES) {
51
+ if (pattern.test(name))
52
+ return type;
53
+ }
54
+ return DEFAULT_CONTENT_TYPE;
55
+ }
56
+ /**
57
+ * Is this file's body JSON? The single predicate both read paths ask.
58
+ * Derived from {@link contentTypeOf} so there is exactly one table.
59
+ */
60
+ export function isJsonFileName(name) {
61
+ return contentTypeOf(name) === JSON_CONTENT_TYPE;
62
+ }
63
+ /**
64
+ * Decode a raw file body for storage, classified by the file's name.
65
+ *
66
+ * Contract:
67
+ * - `undefined` / `null` body → `content: undefined` (nothing was sent).
68
+ * - JSON file, empty body → `content: undefined` (an empty string is not a JSON
69
+ * value; this preserves the historical store-fs behaviour).
70
+ * - JSON file, valid body → parsed value.
71
+ * - JSON file, invalid body → raw text plus `error` (contained, never thrown).
72
+ * - anything else → the raw string, verbatim (`''` stays `''`).
73
+ */
74
+ export function decodeFileContent(name, raw) {
75
+ const contentType = contentTypeOf(name);
76
+ if (raw === undefined || raw === null) {
77
+ return { content: undefined, contentType };
78
+ }
79
+ if (contentType !== JSON_CONTENT_TYPE) {
80
+ return { content: raw, contentType };
81
+ }
82
+ if (raw === '') {
83
+ return { content: undefined, contentType };
84
+ }
85
+ try {
86
+ return { content: JSON.parse(raw), contentType };
87
+ }
88
+ catch (e) {
89
+ return {
90
+ content: raw,
91
+ contentType,
92
+ error: e instanceof Error ? e : new Error(String(e)),
93
+ };
94
+ }
95
+ }
package/dist/index.d.ts CHANGED
@@ -16,4 +16,5 @@ export * from './core-utils-chrono.js';
16
16
  export * from './core-utils-comparator.js';
17
17
  export * from './core-utils-fqn.js';
18
18
  export * from './core-utils-validations.js';
19
+ export * from './file-content-type.js';
19
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,wBAAwB,C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cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AAGzC,cAAc,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -22,3 +22,5 @@ export * from './core-utils-chrono.js';
22
22
  export * from './core-utils-comparator.js';
23
23
  export * from './core-utils-fqn.js';
24
24
  export * from './core-utils-validations.js';
25
+ // File content type (suffix table)
26
+ export * from './file-content-type.js';
@@ -136,7 +136,7 @@ export function navigateStep(from, step, prototype) {
136
136
  if (Array.isArray(from)) {
137
137
  if (step.type === 'lookup') {
138
138
  const stp = step;
139
- const result = from.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => e[propertyName] === propertyValue));
139
+ const result = from.find(e => e !== undefined && e !== null && stp.keys.every(({ propertyName, propertyValue }) => e[propertyName] === propertyValue));
140
140
  if (result === undefined && prototype !== undefined) {
141
141
  from.push(prototype);
142
142
  return prototype;
@@ -212,7 +212,7 @@ class OverlayNavigator {
212
212
  if (Array.isArray(from.children)) {
213
213
  if (step.type === 'lookup') {
214
214
  const stp = step;
215
- const result = from.children.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => e.keys?.[propertyName] === propertyValue));
215
+ const result = from.children.find(e => e !== undefined && e !== null && stp.keys.every(({ propertyName, propertyValue }) => e.keys?.[propertyName] === propertyValue));
216
216
  return result;
217
217
  }
218
218
  else if (step.type === 'position') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamak/shared-utils",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Shared utilities for path navigation, filesystem operations, and common helpers",