react-cheminfo 0.70.0 → 0.71.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.
Files changed (213) hide show
  1. package/README.md +57 -2
  2. package/lib/about/core/providers.d.ts +6 -0
  3. package/lib/about/core/providers.d.ts.map +1 -1
  4. package/lib/about/core/providers.js +7 -0
  5. package/lib/about/core/providers.js.map +1 -1
  6. package/lib/about/ui/AboutProvidedBy.js +34 -0
  7. package/lib/about/ui/AboutProvidedBy.js.map +1 -1
  8. package/lib/download/core/figurePng.d.ts +14 -0
  9. package/lib/download/core/figurePng.d.ts.map +1 -1
  10. package/lib/download/core/figurePng.js +21 -6
  11. package/lib/download/core/figurePng.js.map +1 -1
  12. package/lib/download/core/svgToPng.d.ts.map +1 -1
  13. package/lib/download/core/svgToPng.js +4 -2
  14. package/lib/download/core/svgToPng.js.map +1 -1
  15. package/lib/format/core/relativeTime.d.ts +8 -3
  16. package/lib/format/core/relativeTime.d.ts.map +1 -1
  17. package/lib/format/core/relativeTime.js +19 -7
  18. package/lib/format/core/relativeTime.js.map +1 -1
  19. package/lib/host/core/connectHost.d.ts +25 -34
  20. package/lib/host/core/connectHost.d.ts.map +1 -1
  21. package/lib/host/core/connectHost.js +43 -89
  22. package/lib/host/core/connectHost.js.map +1 -1
  23. package/lib/host/core/hostOutputs.d.ts +58 -0
  24. package/lib/host/core/hostOutputs.d.ts.map +1 -0
  25. package/lib/host/core/hostOutputs.js +151 -0
  26. package/lib/host/core/hostOutputs.js.map +1 -0
  27. package/lib/host/core/index.d.ts +9 -3
  28. package/lib/host/core/index.d.ts.map +1 -1
  29. package/lib/host/core/index.js +4 -1
  30. package/lib/host/core/index.js.map +1 -1
  31. package/lib/host/core/irKinds.d.ts +124 -0
  32. package/lib/host/core/irKinds.d.ts.map +1 -0
  33. package/lib/host/core/irKinds.js +46 -0
  34. package/lib/host/core/irKinds.js.map +1 -0
  35. package/lib/host/core/kinds.d.ts +13 -5
  36. package/lib/host/core/kinds.d.ts.map +1 -1
  37. package/lib/host/core/kinds.js +11 -5
  38. package/lib/host/core/kinds.js.map +1 -1
  39. package/lib/host/core/protocol.d.ts +50 -7
  40. package/lib/host/core/protocol.d.ts.map +1 -1
  41. package/lib/host/core/protocol.js +10 -6
  42. package/lib/host/core/protocol.js.map +1 -1
  43. package/lib/host/core/selectInputs.d.ts +37 -0
  44. package/lib/host/core/selectInputs.d.ts.map +1 -0
  45. package/lib/host/core/selectInputs.js +110 -0
  46. package/lib/host/core/selectInputs.js.map +1 -0
  47. package/lib/host/testing/sciPeaks.d.ts +31 -0
  48. package/lib/host/testing/sciPeaks.d.ts.map +1 -1
  49. package/lib/host/testing/sciPeaks.js +73 -0
  50. package/lib/host/testing/sciPeaks.js.map +1 -1
  51. package/lib/host/testing/standInHost.d.ts +9 -1
  52. package/lib/host/testing/standInHost.d.ts.map +1 -1
  53. package/lib/host/testing/standInHost.js +19 -1
  54. package/lib/host/testing/standInHost.js.map +1 -1
  55. package/lib/host/ui/useHostConnection.d.ts +13 -5
  56. package/lib/host/ui/useHostConnection.d.ts.map +1 -1
  57. package/lib/host/ui/useHostConnection.js +4 -3
  58. package/lib/host/ui/useHostConnection.js.map +1 -1
  59. package/lib/host-testing.d.ts +2 -2
  60. package/lib/host-testing.d.ts.map +1 -1
  61. package/lib/host-testing.js +2 -2
  62. package/lib/host-testing.js.map +1 -1
  63. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  64. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  65. package/lib/ir/core/handPicking.d.ts +58 -0
  66. package/lib/ir/core/handPicking.d.ts.map +1 -0
  67. package/lib/ir/core/handPicking.js +130 -0
  68. package/lib/ir/core/handPicking.js.map +1 -0
  69. package/lib/ir/core/index.d.ts +3 -0
  70. package/lib/ir/core/index.d.ts.map +1 -1
  71. package/lib/ir/core/index.js +3 -0
  72. package/lib/ir/core/index.js.map +1 -1
  73. package/lib/ir/core/irBandState.d.ts +57 -0
  74. package/lib/ir/core/irBandState.d.ts.map +1 -0
  75. package/lib/ir/core/irBandState.js +119 -0
  76. package/lib/ir/core/irBandState.js.map +1 -0
  77. package/lib/ir/core/irCommands.d.ts +11 -0
  78. package/lib/ir/core/irCommands.d.ts.map +1 -1
  79. package/lib/ir/core/irCommands.js +6 -0
  80. package/lib/ir/core/irCommands.js.map +1 -1
  81. package/lib/ir/core/irPeaks.d.ts +28 -0
  82. package/lib/ir/core/irPeaks.d.ts.map +1 -0
  83. package/lib/ir/core/irPeaks.js +66 -0
  84. package/lib/ir/core/irPeaks.js.map +1 -0
  85. package/lib/ir/core/irReducer.d.ts +8 -0
  86. package/lib/ir/core/irReducer.d.ts.map +1 -1
  87. package/lib/ir/core/irReducer.js +15 -32
  88. package/lib/ir/core/irReducer.js.map +1 -1
  89. package/lib/ir/core/irSpectrum.d.ts +15 -0
  90. package/lib/ir/core/irSpectrum.d.ts.map +1 -1
  91. package/lib/ir/core/irSpectrum.js.map +1 -1
  92. package/lib/ir/core/irState.d.ts +9 -6
  93. package/lib/ir/core/irState.d.ts.map +1 -1
  94. package/lib/ir/core/irState.js +10 -6
  95. package/lib/ir/core/irState.js.map +1 -1
  96. package/lib/ir/core/pickBands.d.ts +14 -0
  97. package/lib/ir/core/pickBands.d.ts.map +1 -1
  98. package/lib/ir/core/pickBands.js +39 -33
  99. package/lib/ir/core/pickBands.js.map +1 -1
  100. package/lib/ir/core/ramanBands.d.ts +9 -1
  101. package/lib/ir/core/ramanBands.d.ts.map +1 -1
  102. package/lib/ir/core/ramanBands.js +2 -2
  103. package/lib/ir/core/ramanBands.js.map +1 -1
  104. package/lib/ir/core/spectrumBands.d.ts +46 -0
  105. package/lib/ir/core/spectrumBands.d.ts.map +1 -0
  106. package/lib/ir/core/spectrumBands.js +98 -0
  107. package/lib/ir/core/spectrumBands.js.map +1 -0
  108. package/lib/ir/ui/BandRow.d.ts +5 -0
  109. package/lib/ir/ui/BandRow.d.ts.map +1 -1
  110. package/lib/ir/ui/BandRow.js +4 -3
  111. package/lib/ir/ui/BandRow.js.map +1 -1
  112. package/lib/ir/ui/BandsPanel.d.ts +5 -0
  113. package/lib/ir/ui/BandsPanel.d.ts.map +1 -1
  114. package/lib/ir/ui/BandsPanel.js +36 -16
  115. package/lib/ir/ui/BandsPanel.js.map +1 -1
  116. package/lib/ir/ui/BandsToolbar.d.ts +9 -0
  117. package/lib/ir/ui/BandsToolbar.d.ts.map +1 -0
  118. package/lib/ir/ui/BandsToolbar.js +61 -0
  119. package/lib/ir/ui/BandsToolbar.js.map +1 -0
  120. package/lib/ir/ui/IrCanvas.d.ts +2 -1
  121. package/lib/ir/ui/IrCanvas.d.ts.map +1 -1
  122. package/lib/ir/ui/IrCanvas.js +19 -5
  123. package/lib/ir/ui/IrCanvas.js.map +1 -1
  124. package/lib/ir/ui/IrChart.d.ts.map +1 -1
  125. package/lib/ir/ui/IrChart.js +8 -1
  126. package/lib/ir/ui/IrChart.js.map +1 -1
  127. package/lib/ir/ui/IrDocumentationDialog.js +1 -1
  128. package/lib/ir/ui/IrDocumentationDialog.js.map +1 -1
  129. package/lib/ir/ui/IrEditor.d.ts +4 -2
  130. package/lib/ir/ui/IrEditor.d.ts.map +1 -1
  131. package/lib/ir/ui/IrEditor.js.map +1 -1
  132. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  133. package/lib/ir/ui/IrShell.js +6 -0
  134. package/lib/ir/ui/IrShell.js.map +1 -1
  135. package/lib/ir/ui/IrStatusBar.d.ts.map +1 -1
  136. package/lib/ir/ui/IrStatusBar.js +1 -1
  137. package/lib/ir/ui/IrStatusBar.js.map +1 -1
  138. package/lib/ir/ui/IrToolbar.d.ts +4 -3
  139. package/lib/ir/ui/IrToolbar.d.ts.map +1 -1
  140. package/lib/ir/ui/IrToolbar.js +5 -4
  141. package/lib/ir/ui/IrToolbar.js.map +1 -1
  142. package/lib/ir/ui/VibrationalEditor.d.ts +2 -1
  143. package/lib/ir/ui/VibrationalEditor.d.ts.map +1 -1
  144. package/lib/ir/ui/VibrationalEditor.js.map +1 -1
  145. package/lib/ir/ui/irActions.d.ts +5 -0
  146. package/lib/ir/ui/irActions.d.ts.map +1 -1
  147. package/lib/ir/ui/irActions.js +6 -0
  148. package/lib/ir/ui/irActions.js.map +1 -1
  149. package/lib/ir/ui/irChartProps.d.ts +4 -1
  150. package/lib/ir/ui/irChartProps.d.ts.map +1 -1
  151. package/lib/ir/ui/useIrEditor.d.ts +5 -11
  152. package/lib/ir/ui/useIrEditor.d.ts.map +1 -1
  153. package/lib/ir/ui/useIrEditor.js +17 -18
  154. package/lib/ir/ui/useIrEditor.js.map +1 -1
  155. package/lib/locales/de.json +63 -0
  156. package/lib/locales/en.json +63 -0
  157. package/lib/locales/es.json +63 -0
  158. package/lib/locales/fr.json +63 -0
  159. package/lib/locales/it.json +63 -0
  160. package/lib/slides/ui/slideSanitizeSchema.d.ts +3 -2
  161. package/lib/slides/ui/slideSanitizeSchema.d.ts.map +1 -1
  162. package/lib/slides/ui/slideSanitizeSchema.js +82 -2
  163. package/lib/slides/ui/slideSanitizeSchema.js.map +1 -1
  164. package/package.json +1 -1
  165. package/src/about/core/providers.ts +7 -0
  166. package/src/about/ui/AboutProvidedBy.tsx +49 -0
  167. package/src/download/core/figurePng.ts +21 -8
  168. package/src/download/core/svgToPng.ts +3 -4
  169. package/src/format/core/relativeTime.ts +29 -7
  170. package/src/host/core/connectHost.ts +77 -120
  171. package/src/host/core/hostOutputs.ts +197 -0
  172. package/src/host/core/index.ts +13 -7
  173. package/src/host/core/irKinds.ts +92 -0
  174. package/src/host/core/kinds.ts +16 -6
  175. package/src/host/core/protocol.ts +50 -8
  176. package/src/host/core/selectInputs.ts +145 -0
  177. package/src/host/testing/sciPeaks.ts +140 -0
  178. package/src/host/testing/standInHost.ts +30 -5
  179. package/src/host/ui/useHostConnection.ts +17 -8
  180. package/src/host-testing.ts +4 -0
  181. package/src/ir/core/handPicking.ts +154 -0
  182. package/src/ir/core/index.ts +14 -0
  183. package/src/ir/core/irBandState.ts +155 -0
  184. package/src/ir/core/irCommands.ts +7 -0
  185. package/src/ir/core/irPeaks.ts +79 -0
  186. package/src/ir/core/irReducer.ts +27 -34
  187. package/src/ir/core/irSpectrum.ts +16 -0
  188. package/src/ir/core/irState.ts +15 -8
  189. package/src/ir/core/pickBands.ts +46 -39
  190. package/src/ir/core/ramanBands.ts +2 -2
  191. package/src/ir/core/spectrumBands.ts +113 -0
  192. package/src/ir/ui/BandRow.tsx +20 -2
  193. package/src/ir/ui/BandsPanel.tsx +46 -88
  194. package/src/ir/ui/BandsToolbar.tsx +152 -0
  195. package/src/ir/ui/IrCanvas.tsx +22 -11
  196. package/src/ir/ui/IrChart.tsx +10 -1
  197. package/src/ir/ui/IrDocumentationDialog.tsx +7 -0
  198. package/src/ir/ui/IrEditor.tsx +4 -2
  199. package/src/ir/ui/IrShell.tsx +5 -0
  200. package/src/ir/ui/IrStatusBar.tsx +4 -1
  201. package/src/ir/ui/IrToolbar.tsx +14 -5
  202. package/src/ir/ui/VibrationalEditor.tsx +2 -1
  203. package/src/ir/ui/irActions.ts +11 -0
  204. package/src/ir/ui/irChartProps.ts +4 -1
  205. package/src/ir/ui/useIrEditor.ts +21 -21
  206. package/src/locales/de.json +63 -0
  207. package/src/locales/en.json +63 -0
  208. package/src/locales/es.json +63 -0
  209. package/src/locales/fr.json +63 -0
  210. package/src/locales/it.json +63 -0
  211. package/src/slides/ui/slideSanitizeSchema.ts +86 -2
  212. package/styles/chrome.css +7 -0
  213. package/styles/slides.css +47 -0
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The kinds a measured infrared spectrum travels as: the JCAMP-DX a notebook
3
+ * keeps with the bands picked on it, and those bands alone, which is what a
4
+ * tool hands back.
5
+ *
6
+ * Shaped on what SciPeaks keeps for a sample's infrared spectrum — a JCAMP-DX
7
+ * attachment and a `peak` list beside it — with the text of the attachment in
8
+ * place of its address, since a tool framed from another origin cannot fetch
9
+ * what the notebook serves its own pages. A band is written the way SciPeaks
10
+ * and `ir-spectrum` write one, so what a tool hands back can be stored as it
11
+ * arrives.
12
+ *
13
+ * Named after pipeline's `spectra.nmr.1d.jdx`. Pipeline's own `spectra.ir` is
14
+ * a computed spectrum — normal modes, not a measurement — and is not one of the
15
+ * kinds read here.
16
+ */
17
+
18
+ /** One band of an infrared spectrum, as SciPeaks and `ir-spectrum` write it. */
19
+ export interface IrPeakValue {
20
+ /** Where it sits, in cm⁻¹. */
21
+ wavenumber: number;
22
+ /**
23
+ * Its absorbance.
24
+ * @default read off the spectrum
25
+ */
26
+ absorbance?: number;
27
+ /**
28
+ * Its transmittance, as a fraction between 0 and 1 — not a percentage.
29
+ * @default read off the spectrum
30
+ */
31
+ transmittance?: number;
32
+ /**
33
+ * How strong it is: `w` weak, `m` medium, `S` strong. Any other letter is
34
+ * worked out again from the spectrum.
35
+ * @default worked out from the spectrum
36
+ */
37
+ kind?: string;
38
+ }
39
+
40
+ /** `spectra.ir.jdx`: a measured infrared spectrum and the bands picked on it. */
41
+ export interface IrJdxValue {
42
+ /** The JCAMP-DX, as text. */
43
+ jdx: string;
44
+ /**
45
+ * The name the notebook keeps it under, e.g. `spectra/ir/sample.jdx`.
46
+ * @default undefined
47
+ */
48
+ filename?: string;
49
+ /**
50
+ * The bands picked on it. Absent or empty, the tool picks them itself.
51
+ * @default undefined
52
+ */
53
+ peaks?: IrPeakValue[];
54
+ }
55
+
56
+ /** `spectra.ir.peaks`: the bands picked on an infrared spectrum, alone. */
57
+ export interface IrPeaksValue {
58
+ /** The bands, ascending by wavenumber. */
59
+ peaks: IrPeakValue[];
60
+ }
61
+
62
+ const IR_PEAK_SCHEMA = {
63
+ type: 'object',
64
+ properties: {
65
+ wavenumber: { type: 'number' },
66
+ absorbance: { type: 'number' },
67
+ transmittance: { type: 'number' },
68
+ kind: { type: 'string' },
69
+ },
70
+ required: ['wavenumber'],
71
+ additionalProperties: false,
72
+ } as const;
73
+
74
+ /** The schema of `spectra.ir.jdx`. */
75
+ export const IR_JDX_SCHEMA = {
76
+ type: 'object',
77
+ properties: {
78
+ jdx: { type: 'string' },
79
+ filename: { type: 'string' },
80
+ peaks: { type: 'array', items: IR_PEAK_SCHEMA },
81
+ },
82
+ required: ['jdx'],
83
+ additionalProperties: false,
84
+ } as const;
85
+
86
+ /** The schema of `spectra.ir.peaks`. */
87
+ export const IR_PEAKS_SCHEMA = {
88
+ type: 'object',
89
+ properties: { peaks: { type: 'array', items: IR_PEAK_SCHEMA } },
90
+ required: ['peaks'],
91
+ additionalProperties: false,
92
+ } as const;
@@ -4,16 +4,20 @@
4
4
  * name, a JSON Schema, and ajv.
5
5
  *
6
6
  * The names are pipeline's (`chem.molfile.2d`, `chem.ocl.id`…), plus
7
- * `chem.smiles`, which pipeline lists among its future schemas. A name with
8
- * children is a category whose schema every child satisfies, so `chem.molfile`
9
- * reads `chem.molfile.2d` and `chem.molfile.3d.h.energy` alike; the fields a
10
- * tool does not read are stripped rather than refused, so a host sending more
11
- * than it needs to is never turned away.
7
+ * `chem.smiles`, which pipeline lists among its future schemas, and the two
8
+ * infrared kinds of `./irKinds.ts`. A name with children is a category whose
9
+ * schema every child satisfies, so `chem.molfile` reads `chem.molfile.2d` and
10
+ * `chem.molfile.3d.h.energy` alike; the fields a tool does not read are
11
+ * stripped rather than refused, so a host sending more than it needs to is
12
+ * never turned away.
12
13
  */
13
14
 
14
15
  import type { JSONSchemaType, ValidateFunction } from 'ajv';
15
16
  import { Ajv } from 'ajv';
16
17
 
18
+ import type { IrJdxValue, IrPeaksValue } from './irKinds.ts';
19
+ import { IR_JDX_SCHEMA, IR_PEAKS_SCHEMA } from './irKinds.ts';
20
+
17
21
  /** A molfile, V2000 or V3000. */
18
22
  export interface MolfileValue {
19
23
  molfile: string;
@@ -34,7 +38,9 @@ export interface SmilesValue {
34
38
  export type CheckedValue =
35
39
  | { kind: 'chem.molfile'; value: MolfileValue }
36
40
  | { kind: 'chem.ocl.id'; value: OclIdValue }
37
- | { kind: 'chem.smiles'; value: SmilesValue };
41
+ | { kind: 'chem.smiles'; value: SmilesValue }
42
+ | { kind: 'spectra.ir.jdx'; value: IrJdxValue }
43
+ | { kind: 'spectra.ir.peaks'; value: IrPeaksValue };
38
44
 
39
45
  /** The name of a kind a tool can read. */
40
46
  export type KnownKind = CheckedValue['kind'];
@@ -125,4 +131,8 @@ const VALIDATORS: { [K in KnownKind]: ValidateFunction<ValueOf<K>> } = {
125
131
  'chem.molfile': ajv.compile(MOLFILE_SCHEMA),
126
132
  'chem.ocl.id': ajv.compile(OCL_ID_SCHEMA),
127
133
  'chem.smiles': ajv.compile(SMILES_SCHEMA),
134
+ // Written as plain schemas rather than `JSONSchemaType`, which would demand
135
+ // `nullable` on every optional field and so let a `null` band through.
136
+ 'spectra.ir.jdx': ajv.compile<IrJdxValue>(IR_JDX_SCHEMA),
137
+ 'spectra.ir.peaks': ajv.compile<IrPeaksValue>(IR_PEAKS_SCHEMA),
128
138
  };
@@ -4,9 +4,10 @@
4
4
  *
5
5
  * Two messages travel between the windows — `hello`, from the tool, and
6
6
  * `connect`, the host's answer carrying a private `MessagePort` — and everything
7
- * else goes through that port: the host's requests (`load`, `get`) and the
8
- * tool's `result` for each. This module only spells the messages, and reads
9
- * what arrives as the untrusted input it is.
7
+ * else goes through that port: the host's requests (`load`, `get`), the tool's
8
+ * `result` for each, and the `output` the tool sends when a value the host
9
+ * takes back changes. This module only spells the messages, and reads what
10
+ * arrives as the untrusted input it is.
10
11
  */
11
12
 
12
13
  /** The version a tool speaks; a `connect` naming another is ignored. */
@@ -52,9 +53,24 @@ export interface ToolInput {
52
53
  key: string;
53
54
  /** Kinds in order of preference; a category accepts every kind under it. */
54
55
  kinds: readonly string[];
56
+ /**
57
+ * Whether it takes every item of its kinds, in the host's order, rather than
58
+ * the one it prefers — every spectrum of a sample.
59
+ * @default false
60
+ */
61
+ many?: boolean;
62
+ /**
63
+ * Whether a request may leave it without an item. A request is still
64
+ * refused when no input at all gets one.
65
+ * @default false
66
+ */
67
+ optional?: boolean;
55
68
  }
56
69
 
57
- /** One value the tool can hand back. */
70
+ /**
71
+ * One value the tool can hand back: answered to `get`, and sent as `output`
72
+ * whenever it changes, when the host accepted it.
73
+ */
58
74
  export interface ToolOutput {
59
75
  key: string;
60
76
  kind: string;
@@ -86,6 +102,13 @@ export interface LoadRequest {
86
102
  /** What the data belongs to; the same document twice is applied once. */
87
103
  document: string;
88
104
  items: HostItem[];
105
+ /**
106
+ * The outputs the host takes back for this document, by key or by kind — a
107
+ * kind taking every kind under it: the tool sends an `output` whenever one
108
+ * of their values changes.
109
+ * @default undefined — nothing is sent unasked
110
+ */
111
+ accept?: string[];
89
112
  }
90
113
 
91
114
  /** `get`: the current value of some outputs. */
@@ -119,18 +142,35 @@ export interface UsedItem {
119
142
  kind: string;
120
143
  }
121
144
 
122
- /** The answer to one request, carrying its `id`. */
145
+ /**
146
+ * The answer to one request, carrying its `id`: which items a `load` used, or
147
+ * the values a `get` asked for.
148
+ */
123
149
  export type ResultMessage =
124
150
  | { type: 'result'; id: string; ok: true; used: UsedItem[] }
151
+ | { type: 'result'; id: string; ok: true; items: HostItem[] }
125
152
  | { type: 'result'; id: string; ok: false; error: HostError };
126
153
 
154
+ /**
155
+ * Sent by the tool, unasked, when values the host accepted have changed — the
156
+ * user picked a band. It carries only the items that changed: one missing is
157
+ * unchanged, never deleted.
158
+ */
159
+ export interface OutputMessage {
160
+ type: 'output';
161
+ /** The document the values belong to, as the host's `load` named it. */
162
+ document: string;
163
+ /** Each with its `key`, and the `id` of the item it was made from. */
164
+ items: HostItem[];
165
+ }
166
+
127
167
  /**
128
168
  * Read a message that arrived through the port.
129
169
  *
130
170
  * Anything that is not a well-formed `load` or `get` is `null`, and goes
131
171
  * unanswered: an unknown request is a newer host talking, not an error. An item
132
- * without a string `kind` is dropped, and a label is cut to
133
- * {@link MAX_LABEL_LENGTH}.
172
+ * without a string `kind` is dropped, a label is cut to
173
+ * {@link MAX_LABEL_LENGTH}, and a `load` without `accept` takes nothing back.
134
174
  * @param data - What the port delivered.
135
175
  * @returns The request, or `null`.
136
176
  */
@@ -140,12 +180,14 @@ export function parseRequest(data: unknown): HostRequest | null {
140
180
  if (typeof data.document !== 'string' || !Array.isArray(data.items)) {
141
181
  return null;
142
182
  }
143
- return {
183
+ const load: LoadRequest = {
144
184
  type: 'load',
145
185
  id: data.id,
146
186
  document: data.document,
147
187
  items: readItems(data.items),
148
188
  };
189
+ if (Array.isArray(data.accept)) load.accept = readStrings(data.accept);
190
+ return load;
149
191
  }
150
192
  if (data.type === 'get') {
151
193
  return { type: 'get', id: data.id, keys: readStrings(data.keys) };
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Which items of a request each input of a tool takes, and the sentence a
3
+ * host is answered with when the request loads nothing.
4
+ */
5
+
6
+ import type { CheckedValue, KnownKind } from './kinds.ts';
7
+ import { checkValue, isKnownKind, kindAccepts } from './kinds.ts';
8
+ import type {
9
+ HostError,
10
+ HostItem,
11
+ ToolInput,
12
+ ToolInterface,
13
+ } from './protocol.ts';
14
+
15
+ /** The item an input took, and its value as the input's kind reads it. */
16
+ export interface LoadedInput {
17
+ key: string;
18
+ item: HostItem;
19
+ checked: CheckedValue;
20
+ }
21
+
22
+ /** What choosing the items of a request gave. */
23
+ export type InputSelection =
24
+ { ok: true; inputs: LoadedInput[] } | { ok: false; error: HostError };
25
+
26
+ /**
27
+ * Give each input of the tool the items of a request it takes.
28
+ *
29
+ * Each input tries its kinds in its own order of preference, and takes the
30
+ * first item that falls under one and passes that kind's schema: the tool
31
+ * knows which notation serves it best, whatever order the host listed them in.
32
+ * An input taking `many` takes every such item instead, in the host's order.
33
+ * An item naming another input's `key` is left to that input.
34
+ *
35
+ * A required input left without an item refuses the request. An optional one
36
+ * is passed over, unless no input got an item at all.
37
+ * @param tool - What the tool takes.
38
+ * @param items - What the host sent.
39
+ * @returns The items the inputs took, or why the request loads nothing.
40
+ */
41
+ export function selectInputs(
42
+ tool: ToolInterface,
43
+ items: readonly HostItem[],
44
+ ): InputSelection {
45
+ const inputs: LoadedInput[] = [];
46
+ const passedOver: string[] = [];
47
+ for (const input of tool.inputs) {
48
+ const errors: string[] = [];
49
+ const taken = input.many
50
+ ? selectEvery(input, items, errors)
51
+ : selectFirst(input, items, errors);
52
+ if (taken.length > 0) {
53
+ inputs.push(...taken);
54
+ } else if (input.optional) {
55
+ passedOver.push(...errors);
56
+ } else {
57
+ return { ok: false, error: refusal(tool, [input], errors) };
58
+ }
59
+ }
60
+ if (inputs.length > 0 || tool.inputs.length === 0) {
61
+ return { ok: true, inputs };
62
+ }
63
+ return { ok: false, error: refusal(tool, tool.inputs, passedOver) };
64
+ }
65
+
66
+ function selectFirst(
67
+ input: ToolInput,
68
+ items: readonly HostItem[],
69
+ errors: string[],
70
+ ): LoadedInput[] {
71
+ for (const accepted of input.kinds) {
72
+ if (!isKnownKind(accepted)) continue;
73
+ for (const item of items) {
74
+ if (!isFor(input, accepted, item)) continue;
75
+ const loaded = checkItem(input.key, accepted, item, errors);
76
+ if (loaded !== null) return [loaded];
77
+ }
78
+ }
79
+ return [];
80
+ }
81
+
82
+ function selectEvery(
83
+ input: ToolInput,
84
+ items: readonly HostItem[],
85
+ errors: string[],
86
+ ): LoadedInput[] {
87
+ const taken: LoadedInput[] = [];
88
+ for (const item of items) {
89
+ for (const accepted of input.kinds) {
90
+ if (!isKnownKind(accepted) || !isFor(input, accepted, item)) continue;
91
+ const loaded = checkItem(input.key, accepted, item, errors);
92
+ if (loaded === null) continue;
93
+ taken.push(loaded);
94
+ break;
95
+ }
96
+ }
97
+ return taken;
98
+ }
99
+
100
+ function isFor(input: ToolInput, accepted: KnownKind, item: HostItem): boolean {
101
+ if (item.key !== undefined && item.key !== input.key) return false;
102
+ return kindAccepts(accepted, item.kind);
103
+ }
104
+
105
+ function checkItem(
106
+ key: string,
107
+ accepted: KnownKind,
108
+ item: HostItem,
109
+ errors: string[],
110
+ ): LoadedInput | null {
111
+ const outcome = checkValue(accepted, item.value);
112
+ if (outcome.valid) return { key, item, checked: outcome.checked };
113
+ for (const error of outcome.errors) errors.push(`${item.kind} ${error}`);
114
+ return null;
115
+ }
116
+
117
+ /**
118
+ * Why a request loads nothing: some item had the wrong shape, or no item was
119
+ * of a kind the inputs take.
120
+ * @param tool - What the tool takes.
121
+ * @param inputs - The inputs left without an item.
122
+ * @param errors - What the schemas found.
123
+ * @returns The error the host is answered with.
124
+ */
125
+ function refusal(
126
+ tool: ToolInterface,
127
+ inputs: readonly ToolInput[],
128
+ errors: string[],
129
+ ): HostError {
130
+ if (errors.length > 0) {
131
+ const keys = inputs.map((input) => input.key);
132
+ return {
133
+ code: 'invalid',
134
+ message: `No item for ${LIST.format(keys)} has the shape its kind requires, so ${tool.name} loaded nothing.`,
135
+ errors,
136
+ };
137
+ }
138
+ const kinds = inputs.flatMap((input) => input.kinds);
139
+ return {
140
+ code: 'no-input',
141
+ message: `No item is a ${LIST.format(kinds)}, so ${tool.name} loaded nothing.`,
142
+ };
143
+ }
144
+
145
+ const LIST = new Intl.ListFormat('en', { type: 'disjunction' });
@@ -8,6 +8,10 @@
8
8
  * The sample is a real one, and so are its quirks: its molfile is V2000 with
9
9
  * every atom at the origin, its title is blank so the label is its first name,
10
10
  * and its formula travels as a kind no structure input reads.
11
+ *
12
+ * Its infrared spectrum is pinned here before the tool tab sends it: the bands
13
+ * are the sample's own, and the message is the one visualizer-helper's `ir`
14
+ * reader has to write.
11
15
  */
12
16
 
13
17
  import type { LoadRequest } from '../core/protocol.ts';
@@ -64,3 +68,139 @@ export const SCIPEAKS_ETHYLBENZENE_LOAD: LoadRequest & { accept: string[] } = {
64
68
  ],
65
69
  accept: [],
66
70
  };
71
+
72
+ /** Where SciPeaks keeps that sample's infrared spectrum: its attachment. */
73
+ export const SCIPEAKS_ETHYLBENZENE_IR_FILENAME =
74
+ 'spectra/ir/spectra_ir_100-41-4-IR.jdx';
75
+
76
+ /**
77
+ * The bands SciPeaks keeps for that spectrum, as stored: out of wavenumber
78
+ * order, a transmittance that is a fraction, and the visualizer's `_highlight`.
79
+ */
80
+ export const SCIPEAKS_ETHYLBENZENE_IR_PEAKS = [
81
+ peak(
82
+ 1613.8583376884494,
83
+ 0.473660722610156,
84
+ 0.33599999999999997,
85
+ 'm',
86
+ 0.5620600551849647,
87
+ ),
88
+ peak(
89
+ 1952.5275809045322,
90
+ 0.13123804178794973,
91
+ 0.7392,
92
+ 'w',
93
+ 0.5211472441361716,
94
+ ),
95
+ peak(
96
+ 2872.4947788943773,
97
+ 0.4610494379856386,
98
+ 0.3459,
99
+ 'm',
100
+ 0.30496127508514037,
101
+ ),
102
+ peak(2887.659073366737, 0.7066374452885544, 0.1965, 'S', 0.6306151483877337),
103
+ peak(2907.878132663217, 0.4962093169428189, 0.319, 'S', 0.7220300834282813),
104
+ peak(2944.9464080400967, 1.0867160982395816, 0.0819, 'S', 0.8391486100817713),
105
+ peak(2978.6448402008964, 1.7958800173440752, 0.016, 'S', 0.6098050482700554),
106
+ peak(3014.028193969736, 0.32975414692587596, 0.468, 'm', 0.39091552830124354),
107
+ peak(3039.302018090336, 1.0969100130080565, 0.08, 'S', 0.502385910408338),
108
+ peak(
109
+ 3076.3702934672156,
110
+ 0.5831931281770556,
111
+ 0.2611,
112
+ 'S',
113
+ 0.06293920935800923,
114
+ ),
115
+ peak(
116
+ 3098.2742743717354,
117
+ 0.38658105496542694,
118
+ 0.4106,
119
+ 'm',
120
+ 0.4934643842659038,
121
+ ),
122
+ peak(
123
+ 3118.493333668215,
124
+ 0.16557929631846746,
125
+ 0.6829999999999999,
126
+ 'w',
127
+ 0.05290000144269602,
128
+ ),
129
+ peak(
130
+ 1875.0211869346824,
131
+ 0.11935787359571505,
132
+ 0.7597,
133
+ 'w',
134
+ 0.9972320630781272,
135
+ ),
136
+ peak(
137
+ 1812.6790874371945,
138
+ 0.11125903931710739,
139
+ 0.774,
140
+ 'w',
141
+ 0.07102323198933491,
142
+ ),
143
+ peak(
144
+ 1753.706831155787,
145
+ 0.0834934128848443,
146
+ 0.8251000000000001,
147
+ 'w',
148
+ 0.0750629173499594,
149
+ ),
150
+ ];
151
+
152
+ /**
153
+ * The `load` the tool tab is to send that sample to a site that reads
154
+ * infrared spectra and hands their bands back, once its `ir` reader and its
155
+ * `save` exist: the structure as it is sent today, one `spectra.ir.jdx` item
156
+ * per spectrum — the attachment's text, its name and its bands, under the
157
+ * attachment's name as `id` — and `accept` naming the kind saved back, as
158
+ * the tool tab's `save` names it.
159
+ * @param jdx - The attachment's text, which SciPeaks serves at its `dUrl`.
160
+ * @returns The request, its bands copied so that reading them cannot strip
161
+ * this module's own.
162
+ */
163
+ export function sciPeaksEthylbenzeneIrLoad(
164
+ jdx: string,
165
+ ): LoadRequest & { accept: string[] } {
166
+ return {
167
+ type: 'load',
168
+ id: 'load-1',
169
+ document: SCIPEAKS_ETHYLBENZENE_LOAD.document,
170
+ items: [
171
+ ...SCIPEAKS_ETHYLBENZENE_LOAD.items,
172
+ {
173
+ kind: 'spectra.ir.jdx',
174
+ id: SCIPEAKS_ETHYLBENZENE_IR_FILENAME,
175
+ label: 'spectra_ir_100-41-4-IR.jdx',
176
+ value: {
177
+ jdx,
178
+ filename: SCIPEAKS_ETHYLBENZENE_IR_FILENAME,
179
+ peaks: SCIPEAKS_ETHYLBENZENE_IR_PEAKS.map((stored) => ({
180
+ ...stored,
181
+ })),
182
+ },
183
+ },
184
+ ],
185
+ accept: ['spectra.ir.peaks'],
186
+ };
187
+ }
188
+
189
+ /**
190
+ * One stored band.
191
+ * @param wavenumber - Where it sits, in cm⁻¹.
192
+ * @param absorbance - Its absorbance.
193
+ * @param transmittance - Its transmittance, a fraction.
194
+ * @param kind - Its strength letter.
195
+ * @param highlight - The visualizer's own `_highlight`, stored with it.
196
+ * @returns The band as SciPeaks stores it.
197
+ */
198
+ function peak(
199
+ wavenumber: number,
200
+ absorbance: number,
201
+ transmittance: number,
202
+ kind: string,
203
+ highlight: number,
204
+ ) {
205
+ return { wavenumber, absorbance, transmittance, kind, _highlight: highlight };
206
+ }
@@ -37,7 +37,7 @@ export interface StandInHost {
37
37
  export interface StandInHostLog {
38
38
  /** The tool's `hello`, or `null` before it came. */
39
39
  hello: HelloMessage | null;
40
- /** Every message the tool sent on the port, in order. */
40
+ /** Every message the tool sent on the port, in order: results and outputs. */
41
41
  results: unknown[];
42
42
  }
43
43
 
@@ -46,10 +46,15 @@ export interface StandInHostLog {
46
46
  * @returns The host; close it when the tests are done.
47
47
  */
48
48
  export async function startStandInHost(): Promise<StandInHost> {
49
- const server: Server = createServer((_request, response) => {
50
- response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
51
- response.end(HOST_PAGE);
52
- });
49
+ // A setup carrying a spectrum is tens of kilobytes of address, past the
50
+ // 16 KiB Node allows a request's head by default.
51
+ const server: Server = createServer(
52
+ { maxHeaderSize: MAX_SETUP_BYTES },
53
+ (_request, response) => {
54
+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
55
+ response.end(HOST_PAGE);
56
+ },
57
+ );
53
58
  await new Promise<void>((resolve) => {
54
59
  server.listen(0, '127.0.0.1', resolve);
55
60
  });
@@ -81,6 +86,24 @@ export function readStandInHostLog(): StandInHostLog {
81
86
  return (globalThis as unknown as { hostLog: StandInHostLog }).hostLog;
82
87
  }
83
88
 
89
+ /**
90
+ * Send one more request on the port, once the tool has connected — a `get`
91
+ * after the user has worked. Runs in the page, as
92
+ * `page.evaluate(sendStandInHostRequest, request)`.
93
+ * @param request - What to send.
94
+ * @returns Whether there was a port to send it on.
95
+ */
96
+ export function sendStandInHostRequest(request: unknown): boolean {
97
+ const { hostPort } = globalThis as unknown as {
98
+ hostPort: MessagePort | null;
99
+ };
100
+ hostPort?.postMessage(request);
101
+ return hostPort !== null;
102
+ }
103
+
104
+ /** How long the page's address may be, in bytes: Chrome's own limit is 2 MB. */
105
+ const MAX_SETUP_BYTES = 2 * 1024 * 1024;
106
+
84
107
  /**
85
108
  * The page. Its setup is its own `?setup=`, so one page serves every test, and
86
109
  * it listens before it frames, so the tool's `hello` cannot be missed.
@@ -95,11 +118,13 @@ const { toolUrl, requests } = JSON.parse(new URLSearchParams(location.search).ge
95
118
  const toolOrigin = new URL(toolUrl).origin;
96
119
  const frame = document.querySelector('#tool');
97
120
  globalThis.hostLog = { hello: null, results: [] };
121
+ globalThis.hostPort = null;
98
122
  globalThis.addEventListener('message', (event) => {
99
123
  if (event.source !== frame.contentWindow || event.origin !== toolOrigin) return;
100
124
  if (event.data?.type !== 'cheminfo:hello') return;
101
125
  globalThis.hostLog.hello = event.data;
102
126
  const channel = new MessageChannel();
127
+ globalThis.hostPort = channel.port1;
103
128
  channel.port1.onmessage = (answer) => {
104
129
  globalThis.hostLog.results.push(answer.data);
105
130
  };
@@ -7,17 +7,25 @@
7
7
 
8
8
  import { useEffect, useRef } from 'react';
9
9
 
10
- import type { LoadedInput } from '../core/connectHost.ts';
10
+ import type { HostOutputs } from '../core/hostOutputs.ts';
11
11
  import type { ToolInterface } from '../core/protocol.ts';
12
+ import type { LoadedInput } from '../core/selectInputs.ts';
12
13
 
13
14
  /** What a tool does with what its host sends, and what it says about itself. */
14
15
  export interface HostHandlers {
15
16
  /**
16
- * Show the inputs of a new document. Throwing means they could not be read:
17
- * the error's message goes back to the host, and nothing should have changed
18
- * on screen.
17
+ * Show the inputs of a new document. Throwing, or rejecting, means they
18
+ * could not be read: the error's message goes back to the host, and nothing
19
+ * should have changed on screen.
19
20
  */
20
- load: (inputs: readonly LoadedInput[]) => void;
21
+ load: (inputs: readonly LoadedInput[]) => void | Promise<void>;
22
+ /**
23
+ * Where the values of the tool's outputs are read, and their changes heard.
24
+ * Read once, when the host connects, so it lives as long as the page — a
25
+ * store of signals rather than a value of one render.
26
+ * @default undefined — nothing goes back
27
+ */
28
+ outputs?: HostOutputs;
21
29
  /**
22
30
  * The build that is running — `BUILD_INFO` from `react-cheminfo/build-info`
23
31
  * — so a host can record which version produced what it stores.
@@ -67,9 +75,10 @@ export function useHostConnection(
67
75
  tool,
68
76
  origin: globalThis.location.origin,
69
77
  build: latest.current.build ?? null,
70
- load: (inputs) => {
71
- latest.current.load(inputs);
72
- },
78
+ load: (inputs) => latest.current.load(inputs),
79
+ ...(latest.current.outputs === undefined
80
+ ? {}
81
+ : { outputs: latest.current.outputs }),
73
82
  });
74
83
  })
75
84
  .catch(() => {
@@ -9,10 +9,14 @@ export type {
9
9
  } from './host/testing/standInHost.ts';
10
10
  export {
11
11
  readStandInHostLog,
12
+ sendStandInHostRequest,
12
13
  startStandInHost,
13
14
  } from './host/testing/standInHost.ts';
14
15
  export {
16
+ SCIPEAKS_ETHYLBENZENE_IR_FILENAME,
17
+ SCIPEAKS_ETHYLBENZENE_IR_PEAKS,
15
18
  SCIPEAKS_ETHYLBENZENE_LOAD,
16
19
  SCIPEAKS_ETHYLBENZENE_MOLFILE,
17
20
  SCIPEAKS_HOST_NAME,
21
+ sciPeaksEthylbenzeneIrLoad,
18
22
  } from './host/testing/sciPeaks.ts';