@dwp/govuk-casa 7.0.6 → 8.0.0-beta1

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 (228) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +22 -17
  3. package/dist/{casa → assets}/css/casa-ie8.css +1 -1
  4. package/dist/assets/css/casa.css +1 -0
  5. package/dist/casa.d.ts +11 -0
  6. package/dist/casa.js +46 -0
  7. package/dist/lib/CasaTemplateLoader.d.ts +29 -0
  8. package/dist/lib/CasaTemplateLoader.js +74 -0
  9. package/dist/lib/JourneyContext.d.ts +297 -0
  10. package/dist/lib/JourneyContext.js +581 -0
  11. package/dist/lib/MutableRouter.d.ts +155 -0
  12. package/dist/lib/MutableRouter.js +277 -0
  13. package/dist/lib/Plan.d.ts +154 -0
  14. package/dist/lib/Plan.js +442 -0
  15. package/dist/lib/ValidationError.d.ts +74 -0
  16. package/dist/lib/ValidationError.js +159 -0
  17. package/dist/lib/ValidatorFactory.d.ts +83 -0
  18. package/dist/lib/ValidatorFactory.js +106 -0
  19. package/dist/lib/configuration-ingestor.d.ts +262 -0
  20. package/dist/lib/configuration-ingestor.js +490 -0
  21. package/dist/lib/configure.d.ts +90 -0
  22. package/dist/lib/configure.js +192 -0
  23. package/dist/lib/dirname.cjs +1 -0
  24. package/dist/lib/dirname.d.cts +2 -0
  25. package/dist/lib/end-session.d.ts +13 -0
  26. package/dist/lib/end-session.js +43 -0
  27. package/dist/lib/field.d.ts +77 -0
  28. package/dist/lib/field.js +255 -0
  29. package/dist/lib/index.d.ts +14 -0
  30. package/dist/lib/index.js +54 -0
  31. package/dist/lib/logger.d.ts +9 -0
  32. package/dist/lib/logger.js +18 -0
  33. package/dist/lib/nunjucks-filters.d.ts +26 -0
  34. package/dist/lib/nunjucks-filters.js +90 -0
  35. package/dist/lib/nunjucks.d.ts +23 -0
  36. package/dist/lib/nunjucks.js +49 -0
  37. package/dist/lib/utils.d.ts +48 -0
  38. package/dist/lib/utils.js +111 -0
  39. package/dist/lib/validators/dateObject.d.ts +4 -0
  40. package/dist/lib/validators/dateObject.js +135 -0
  41. package/dist/lib/validators/email.d.ts +4 -0
  42. package/dist/lib/validators/email.js +46 -0
  43. package/dist/lib/validators/inArray.d.ts +4 -0
  44. package/dist/lib/validators/inArray.js +60 -0
  45. package/dist/lib/validators/index.d.ts +21 -0
  46. package/dist/lib/validators/index.js +47 -0
  47. package/dist/lib/validators/nino.d.ts +4 -0
  48. package/dist/lib/validators/nino.js +46 -0
  49. package/dist/lib/validators/postalAddressObject.d.ts +4 -0
  50. package/dist/lib/validators/postalAddressObject.js +123 -0
  51. package/dist/lib/validators/regex.d.ts +4 -0
  52. package/dist/lib/validators/regex.js +40 -0
  53. package/dist/lib/validators/required.d.ts +4 -0
  54. package/dist/lib/validators/required.js +56 -0
  55. package/dist/lib/validators/strlen.d.ts +4 -0
  56. package/dist/lib/validators/strlen.js +51 -0
  57. package/dist/lib/validators/wordCount.d.ts +5 -0
  58. package/dist/lib/validators/wordCount.js +54 -0
  59. package/dist/lib/waypoint-url.d.ts +23 -0
  60. package/dist/lib/waypoint-url.js +52 -0
  61. package/dist/middleware/body-parser.d.ts +1 -0
  62. package/dist/middleware/body-parser.js +24 -0
  63. package/dist/middleware/csrf.d.ts +1 -0
  64. package/dist/middleware/csrf.js +31 -0
  65. package/dist/middleware/data.d.ts +5 -0
  66. package/dist/middleware/data.js +53 -0
  67. package/dist/middleware/dirname.cjs +1 -0
  68. package/dist/middleware/dirname.d.cts +2 -0
  69. package/dist/middleware/gather-fields.d.ts +6 -0
  70. package/dist/middleware/gather-fields.js +48 -0
  71. package/dist/middleware/i18n.d.ts +4 -0
  72. package/dist/middleware/i18n.js +88 -0
  73. package/dist/middleware/post.d.ts +3 -0
  74. package/dist/middleware/post.js +57 -0
  75. package/dist/middleware/pre.d.ts +3 -0
  76. package/dist/middleware/pre.js +51 -0
  77. package/dist/middleware/progress-journey.d.ts +6 -0
  78. package/dist/middleware/progress-journey.js +80 -0
  79. package/dist/middleware/sanitise-fields.d.ts +5 -0
  80. package/dist/middleware/sanitise-fields.js +53 -0
  81. package/dist/middleware/session.d.ts +11 -0
  82. package/dist/middleware/session.js +121 -0
  83. package/dist/middleware/skip-waypoint.d.ts +5 -0
  84. package/dist/middleware/skip-waypoint.js +43 -0
  85. package/dist/middleware/steer-journey.d.ts +7 -0
  86. package/dist/middleware/steer-journey.js +62 -0
  87. package/dist/middleware/validate-fields.d.ts +7 -0
  88. package/dist/middleware/validate-fields.js +67 -0
  89. package/dist/mjs/esm-wrapper.js +11 -0
  90. package/dist/mjs/package.json +3 -0
  91. package/dist/package.json +3 -0
  92. package/dist/routes/ancillary.d.ts +11 -0
  93. package/dist/routes/ancillary.js +27 -0
  94. package/dist/routes/dirname.cjs +1 -0
  95. package/dist/routes/dirname.d.cts +2 -0
  96. package/dist/routes/journey.d.ts +8 -0
  97. package/dist/routes/journey.js +127 -0
  98. package/dist/routes/static.d.ts +26 -0
  99. package/dist/routes/static.js +68 -0
  100. package/package.json +64 -89
  101. package/views/casa/components/checkboxes/template.njk +4 -1
  102. package/views/casa/components/date-input/template.njk +3 -3
  103. package/views/casa/components/journey-form/README.md +3 -1
  104. package/views/casa/components/journey-form/template.njk +1 -1
  105. package/views/casa/components/postal-address-object/template.njk +5 -5
  106. package/views/casa/components/radios/template.njk +1 -1
  107. package/views/casa/errors/static.njk +11 -0
  108. package/views/casa/layouts/journey.njk +26 -9
  109. package/views/casa/layouts/main.njk +7 -20
  110. package/views/casa/partials/scripts.njk +8 -3
  111. package/views/casa/partials/styles.njk +2 -2
  112. package/casa.js +0 -208
  113. package/definitions/review-page.js +0 -60
  114. package/dist/casa/css/casa.css +0 -1
  115. package/dist/casa/js/casa.js +0 -1
  116. package/index.d.ts +0 -121
  117. package/lib/ConfigIngestor.js +0 -588
  118. package/lib/GatherModifier.js +0 -14
  119. package/lib/I18n.js +0 -160
  120. package/lib/JourneyContext.d.ts +0 -97
  121. package/lib/JourneyContext.js +0 -552
  122. package/lib/JourneyMap.js +0 -233
  123. package/lib/JourneyRoad.js +0 -330
  124. package/lib/Logger.js +0 -59
  125. package/lib/PageDictionary.d.ts +0 -11
  126. package/lib/PageDirectory.js +0 -77
  127. package/lib/Plan.js +0 -423
  128. package/lib/RoadConverter.js +0 -153
  129. package/lib/UserJourney.js +0 -8
  130. package/lib/Util.js +0 -227
  131. package/lib/Validation.js +0 -20
  132. package/lib/bootstrap/end-session.js +0 -44
  133. package/lib/bootstrap/load-definitions.js +0 -64
  134. package/lib/commonBodyParser.js +0 -15
  135. package/lib/enums.js +0 -6
  136. package/lib/gather-modifiers/index.js +0 -7
  137. package/lib/gather-modifiers/trimPostalAddressObject.js +0 -75
  138. package/lib/gather-modifiers/trimWhitespace.js +0 -16
  139. package/lib/utils/createGetRequest.d.ts +0 -5
  140. package/lib/utils/createGetRequest.js +0 -59
  141. package/lib/utils/index.js +0 -11
  142. package/lib/utils/parseRequest.d.ts +0 -5
  143. package/lib/utils/parseRequest.js +0 -72
  144. package/lib/utils/sanitise.js +0 -74
  145. package/lib/utils/validate.js +0 -32
  146. package/lib/validation/ArrayObjectField.js +0 -49
  147. package/lib/validation/ObjectField.js +0 -53
  148. package/lib/validation/SimpleField.d.ts +0 -11
  149. package/lib/validation/SimpleField.js +0 -46
  150. package/lib/validation/ValidationError.d.ts +0 -14
  151. package/lib/validation/ValidationError.js +0 -170
  152. package/lib/validation/ValidatorFactory.d.ts +0 -32
  153. package/lib/validation/ValidatorFactory.js +0 -91
  154. package/lib/validation/index.js +0 -22
  155. package/lib/validation/processor/flattenErrorArray.js +0 -24
  156. package/lib/validation/processor/queue.js +0 -214
  157. package/lib/validation/processor.js +0 -84
  158. package/lib/validation/rules/README.md +0 -3
  159. package/lib/validation/rules/ValidationRules.d.ts +0 -22
  160. package/lib/validation/rules/dateObject.js +0 -156
  161. package/lib/validation/rules/email.js +0 -44
  162. package/lib/validation/rules/inArray.js +0 -61
  163. package/lib/validation/rules/index.js +0 -23
  164. package/lib/validation/rules/nino.js +0 -48
  165. package/lib/validation/rules/optional.js +0 -14
  166. package/lib/validation/rules/postalAddressObject.js +0 -142
  167. package/lib/validation/rules/regex.js +0 -39
  168. package/lib/validation/rules/required.js +0 -57
  169. package/lib/validation/rules/strlen.js +0 -57
  170. package/lib/validation/rules/wordCount.js +0 -61
  171. package/lib/view-filters/formatDateObject.js +0 -35
  172. package/lib/view-filters/includes.js +0 -10
  173. package/lib/view-filters/index.js +0 -23
  174. package/lib/view-filters/mergeObjectsDeep.js +0 -21
  175. package/lib/view-filters/renderAsAttributes.js +0 -33
  176. package/middleware/errors/404.js +0 -12
  177. package/middleware/errors/catch-all.js +0 -27
  178. package/middleware/errors/index.js +0 -9
  179. package/middleware/headers/config-defaults.js +0 -57
  180. package/middleware/headers/headers.js +0 -40
  181. package/middleware/headers/index.js +0 -9
  182. package/middleware/i18n/i18n.js +0 -56
  183. package/middleware/i18n/index.js +0 -16
  184. package/middleware/index.js +0 -55
  185. package/middleware/mount/index.js +0 -9
  186. package/middleware/mount/mount.js +0 -10
  187. package/middleware/nunjucks/environment.js +0 -57
  188. package/middleware/nunjucks/index.js +0 -8
  189. package/middleware/page/csrf.js +0 -37
  190. package/middleware/page/edit-mode.js +0 -52
  191. package/middleware/page/gather.js +0 -75
  192. package/middleware/page/index.js +0 -103
  193. package/middleware/page/journey-continue.js +0 -157
  194. package/middleware/page/journey-rails.js +0 -102
  195. package/middleware/page/prepare-request.js +0 -77
  196. package/middleware/page/render.js +0 -75
  197. package/middleware/page/skip.js +0 -72
  198. package/middleware/page/utils.js +0 -206
  199. package/middleware/page/validate.js +0 -67
  200. package/middleware/session/expiry.js +0 -95
  201. package/middleware/session/genid.js +0 -18
  202. package/middleware/session/index.js +0 -18
  203. package/middleware/session/init.js +0 -25
  204. package/middleware/session/seed.js +0 -50
  205. package/middleware/session/timeout.js +0 -5
  206. package/middleware/static/asset-versions.js +0 -23
  207. package/middleware/static/index.js +0 -104
  208. package/middleware/static/prepare-assets.js +0 -51
  209. package/middleware/static/serve-assets.js +0 -58
  210. package/middleware/variables/index.js +0 -12
  211. package/middleware/variables/variables.js +0 -35
  212. package/src/browserconfig.xml +0 -5
  213. package/src/js/casa.js +0 -132
  214. package/src/scss/_casaElements.scss +0 -11
  215. package/src/scss/_casaGovukTemplateJinjaPolyfill.scss +0 -39
  216. package/src/scss/_casaMountUrl.scss +0 -8
  217. package/src/scss/casa-ie8.scss +0 -3
  218. package/src/scss/casa.scss +0 -14
  219. package/test/unit/templates/README.md +0 -5
  220. package/test/utils/BaseTestWaypoint.js +0 -106
  221. package/test/utils/concatWaypoints.js +0 -26
  222. package/test/utils/index.js +0 -6
  223. package/test/utils/testTraversal.js +0 -90
  224. package/views/casa/partials/cookie_message.njk +0 -3
  225. package/views/casa/partials/phase_banner_alpha.njk +0 -8
  226. package/views/casa/partials/phase_banner_beta.njk +0 -8
  227. package/views/casa/review/page-block.njk +0 -8
  228. package/views/casa/review/review.njk +0 -47
@@ -1,588 +0,0 @@
1
- /* eslint-disable sonarjs/no-duplicate-string */
2
- const fs = require('fs');
3
- const path = require('path');
4
- const logger = require('./Logger.js')('config');
5
-
6
- const echo = (a) => (a);
7
-
8
- /**
9
- * Validate allow page edit flag.
10
- *
11
- * @param {boolean} allowPageEdit Flag.
12
- * @throws {TypeError} For invalid argument type.
13
- * @returns {boolean} Flag.
14
- */
15
- function validateAllowPageEdit(allowPageEdit = false) {
16
- if (typeof allowPageEdit !== 'boolean') {
17
- throw new TypeError('Allow page edit flag must be a boolean (allowPageEdit)');
18
- }
19
- return allowPageEdit;
20
- }
21
-
22
- /**
23
- * Validate use sticky edit flag.
24
- *
25
- * @param {boolean} useStickyEdit Flag.
26
- * @throws {TypeError} For invalid argument type.
27
- * @returns {boolean} Flag.
28
- */
29
- function validateUseStickyEdit(useStickyEdit = false) {
30
- if (typeof useStickyEdit !== 'boolean') {
31
- throw new TypeError('Use sticky edit flag must be a boolean (useStickyEdit)');
32
- }
33
- return useStickyEdit;
34
- }
35
-
36
- /**
37
- * Validates directory and checks that it is writeable.
38
- *
39
- * @param {string} compiledAssetsDir Directory.
40
- * @throws {ReferenceError} For missing directory value.
41
- * @throws {Error} For missing directory.
42
- * @returns {string} Directory.
43
- */
44
- function validateCompiledAssetsDir(compiledAssetsDir) {
45
- if (typeof compiledAssetsDir === 'undefined') {
46
- throw new ReferenceError('Compiled assets directory required (compiledAssetsDir)');
47
- }
48
-
49
- const cad = path.resolve(compiledAssetsDir);
50
- try {
51
- /* eslint-disable no-bitwise */
52
- const constants = fs.constants || fs;
53
- fs.accessSync(cad, constants.F_OK | constants.R_OK | constants.W_OK);
54
- } catch (err) {
55
- if (err.code === 'ENOENT') {
56
- err.message = 'Compiled assets directory missing (compiledAssetsDir)';
57
- }
58
- throw err;
59
- }
60
-
61
- return compiledAssetsDir;
62
- }
63
-
64
- /**
65
- * Validate and sanitise CSP directives.
66
- *
67
- * @param {object} csp CSP directive and values pairs.
68
- * @throws {TypeError} For invalid argument type.
69
- * @throws {Error} For scriptSources warning.
70
- * @returns {object} Validated CSPs.
71
- */
72
- function validateContentSecurityPolicies(csp) {
73
- const validCspDirectives = [
74
- 'child-src',
75
- 'connect-src',
76
- 'default-src',
77
- 'font-src',
78
- 'frame-src',
79
- 'img-src',
80
- 'manifest-src',
81
- 'media-src',
82
- 'object-src',
83
- 'script-src',
84
- 'style-src',
85
- 'worker-src',
86
- 'base-uri',
87
- 'plugin-types',
88
- 'sandbox',
89
- 'form-action',
90
- 'frame-ancestors',
91
- 'block-all-mixed-content',
92
- 'require-sri-for',
93
- 'upgrade-insecure-requests',
94
- 'report-uri',
95
- 'report-to',
96
- ];
97
-
98
- if (typeof csp === 'undefined') {
99
- return csp;
100
- }
101
- if (Object.prototype.toString.call(csp) !== '[object Object]') {
102
- throw new TypeError('Content Security Policies must be an object (csp)');
103
- }
104
-
105
- // Only allow use of scriptSources for backwards compatibility -
106
- // do not mix with other directives
107
- const cspDirectives = Object.getOwnPropertyNames(csp);
108
- if (cspDirectives.includes('scriptSources') && cspDirectives.length > 1) {
109
- throw new Error(`Use of CSP scriptSources is included for backwards
110
- compatibility and should not be used with other CSP directives,
111
- if using as part of a wider policy then please use 'script-src'
112
- instead of 'scriptSources'`.replace(/[\n\t\s]+/g, ' '));
113
- }
114
-
115
- const contentSecurityPolicies = Object.assign(Object.create(null), csp);
116
- if (Object.prototype.hasOwnProperty.call(csp, 'scriptSources')) {
117
- contentSecurityPolicies['script-src'] = csp.scriptSources;
118
- delete contentSecurityPolicies.scriptSources;
119
- }
120
-
121
- Object.getOwnPropertyNames(contentSecurityPolicies).forEach((directive) => {
122
- if (!validCspDirectives.includes(directive)) {
123
- throw new Error(`Invalid CSP directive specified: ${directive}`);
124
- }
125
- });
126
-
127
- return contentSecurityPolicies;
128
- }
129
-
130
- /**
131
- * Validates and sanitises headers object.
132
- *
133
- * @param {object} headers Object to validate.
134
- * @param {Function} cb Callback function that receives the validated value.
135
- * @throws {TypeError} For invalid object.
136
- * @returns {object} Sanitised headers object.
137
- */
138
- function validateHeadersObject(headers = {}, cb = echo) {
139
- if (Object.prototype.toString.call(headers) !== '[object Object]') {
140
- throw new TypeError('Headers must be an object');
141
- }
142
- return cb(headers);
143
- }
144
-
145
- /**
146
- * Validates and sanitises disabled headers.
147
- *
148
- * @param {Array} disabled Array of disabled headers.
149
- * @throws {SyntaxError} For invalid headers.
150
- * @throws {TypeError} For invalid type.
151
- * @returns {Array} Array of disabled headers.
152
- */
153
- function validateHeadersDisabled(disabled = []) {
154
- if (!Array.isArray(disabled)) {
155
- throw new TypeError('Disabled headers must be an array (headers.disabled)');
156
- }
157
- disabled.forEach((header, i) => {
158
- if (typeof header !== 'string') {
159
- throw new TypeError(`Header must be a string, got ${typeof header} (headers.disabled[${i}])`);
160
- }
161
- });
162
- return disabled;
163
- }
164
-
165
- /**
166
- * Validates and sanitises i18n obejct.
167
- *
168
- * @param {object} i18n Object to validate.
169
- * @param {Function} cb Callback function that receives the validated value.
170
- * @throws {TypeError} For invalid object.
171
- * @returns {object} Sanitised i18n object.
172
- */
173
- function validateI18nObject(i18n, cb = echo) {
174
- if (Object.prototype.toString.call(i18n) !== '[object Object]') {
175
- throw new TypeError('I18n must be an object');
176
- }
177
- return cb(i18n);
178
- }
179
-
180
- /**
181
- * Validates and sanitises i18n directory.
182
- *
183
- * @param {Array} dirs Array of directories.
184
- * @throws {SyntaxError} For invalid directories.
185
- * @throws {TypeError} For invalid type.
186
- * @returns {Array} Array of directories.
187
- */
188
- function validateI18nDirs(dirs) {
189
- if (typeof dirs === 'undefined') {
190
- throw ReferenceError('I18n directories are missing (i18n.dirs)')
191
- } else if (!Array.isArray(dirs)) {
192
- throw new TypeError('I18n directories must be an array (i18n.dirs)');
193
- }
194
- dirs.forEach((dir, i) => {
195
- if (typeof dir !== 'string') {
196
- throw new TypeError(`I18n directory must be a string, got ${typeof dir} (i18n.dirs[${i}])`);
197
- }
198
- });
199
- return dirs;
200
- }
201
-
202
- /**
203
- * Validates and sanitises i18n locales.
204
- *
205
- * @param {Array} locales Array of locales.
206
- * @throws {SyntaxError} For invalid locales.
207
- * @throws {TypeError} For invalid type.
208
- * @returns {Array} Array of locales.
209
- */
210
- function validateI18nLocales(locales) {
211
- if (typeof locales === 'undefined') {
212
- throw ReferenceError('I18n locales are missing (i18n.locales)')
213
- } else if (!Array.isArray(locales)) {
214
- throw new TypeError('I18n locales must be an array (i18n.locales)');
215
- }
216
- locales.forEach((locale, i) => {
217
- if (typeof locale !== 'string') {
218
- throw new TypeError(`I18n locale must be a string, got ${typeof locale} (i18n.locales[${i}])`);
219
- }
220
- });
221
- return locales;
222
- }
223
-
224
- /**
225
- * Validates and returns the custom mount controller function.
226
- *
227
- * @param {Function} controller Function for mounting middlware onto Express app.
228
- * @throws {TypeError} For incorrect type.
229
- * @returns {Function} Controller.
230
- */
231
- function validateMountController(controller) {
232
- if (!['undefined', 'function'].includes(typeof controller)) {
233
- throw new TypeError('Additional mount controller must be a function');
234
- } else if (typeof controller === 'function' && !Object.prototype.hasOwnProperty.call(controller, 'prototype')) {
235
- throw new Error('Additional mount controller must not be arrow function or already bound');
236
- }
237
- return controller;
238
- }
239
-
240
- /**
241
- * Validates and sanitises mount url.
242
- *
243
- * @param {string} mountUrl URL from which Express app will be served.
244
- * @param {string} name Name of the URL type (Mount URL, or Proxy Mount URL).
245
- * @throws {SyntaxError} For invalid URL.
246
- * @returns {string} Sanitised URL.
247
- */
248
- function validateMountUrl(mountUrl, name = 'Mount URL') {
249
- if (typeof mountUrl === 'undefined') {
250
- return '/';
251
- }
252
- if (!mountUrl.match(/\/$/)) {
253
- throw new SyntaxError(`${name} must include a trailing slash (/)`);
254
- }
255
- return mountUrl;
256
- }
257
-
258
- /**
259
- * Validate phase.
260
- *
261
- * @param {string} phase Service phase (alpha | beta | live).
262
- * @throws {SyntaxError} For invalid phase value.
263
- * @returns {string} Phase.
264
- */
265
- function validatePhase(phase) {
266
- if (typeof phase === 'undefined') {
267
- return 'live';
268
- }
269
- if (['alpha', 'beta', 'live'].indexOf(phase) === -1) {
270
- throw new SyntaxError('Invalid phase descriptor (phase)');
271
- }
272
- return phase;
273
- }
274
-
275
- /**
276
- * Validate service name.
277
- *
278
- * @param {string} serviceName Service name.
279
- * @throws {SyntaxError} For invalid phase value.
280
- * @returns {string} Phase.
281
- */
282
- function validateServiceName(serviceName) {
283
- if (typeof serviceName === 'undefined') {
284
- return '';
285
- }
286
- if (typeof serviceName !== 'string') {
287
- throw TypeError('Service name must be a string (serviceName)');
288
- }
289
- return serviceName;
290
- }
291
-
292
- /**
293
- * Validates and returns the custom session expiry controller function.
294
- *
295
- * @param {Function} controller Function to handle custom session expiry.
296
- * @throws {TypeError} For incorrect type.
297
- * @returns {Function} Controller.
298
- */
299
- function validateSessionExpiryController(controller) {
300
- if (!['undefined', 'function'].includes(typeof controller)) {
301
- throw new TypeError('Custom session expiry controller must be a function');
302
- } else if (typeof controller === 'function' && controller.length !== 3) {
303
- throw new Error('Custom session expiry controller must accept 3 arguments (req, res, next)');
304
- }
305
- return controller;
306
- }
307
-
308
- /**
309
- * Validates and sanitises sessions obejct.
310
- *
311
- * @param {string} sessions Object to validate.
312
- * @param {Function} cb Callback function that receives the validated value.
313
- * @throws {TypeError} For invalid object.
314
- * @returns {object} Sanitised sessions object.
315
- */
316
- function validateSessionsObject(sessions, cb = echo) {
317
- if (typeof sessions !== 'object') {
318
- throw new TypeError('Session config has not been specified');
319
- }
320
- return cb(sessions);
321
- }
322
-
323
- /**
324
- * Validates and sanitises views obejct.
325
- *
326
- * @param {object} views Object to validate.
327
- * @param {Function} cb Callback function that receives the validated value.
328
- * @throws {TypeError} For invalid object.
329
- * @returns {object} Sanitised views object.
330
- */
331
- function validateViewsObject(views, cb = echo) {
332
- if (typeof views !== 'object') {
333
- throw new TypeError('Views have not been specified');
334
- }
335
- return cb(views);
336
- }
337
-
338
- /**
339
- * Validates and sanitises view directory.
340
- *
341
- * @param {Array} dirs Array of directories.
342
- * @throws {SyntaxError} For invalid directories.
343
- * @throws {TypeError} For invalid type.
344
- * @returns {Array} Array of directories.
345
- */
346
- function validateViewsDirs(dirs) {
347
- if (typeof dirs === 'undefined') {
348
- throw ReferenceError('View directories are missing (views.dirs)')
349
- } else if (!Array.isArray(dirs)) {
350
- throw new TypeError('View directories must be an array (views.dirs)');
351
- }
352
- dirs.forEach((dir, i) => {
353
- if (typeof dir !== 'string') {
354
- throw new TypeError(`View directory must be a string, got ${typeof dir} (views.dirs[${i}])`);
355
- }
356
- });
357
- return dirs;
358
- }
359
-
360
- /**
361
- * Validates and sanitises sessions secret.
362
- *
363
- * @param {string} secret Session secret.
364
- * @throws {ReferenceError} For missing value type.
365
- * @throws {TypeError} For invalid value.
366
- * @returns {string} Secret.
367
- */
368
- function validateSessionsSecret(secret) {
369
- if (typeof secret === 'undefined') {
370
- throw ReferenceError('Session secret is missing (sessions.secret)')
371
- } else if (typeof secret !== 'string') {
372
- throw new TypeError('Session secret must be a string (sessions.secret)');
373
- }
374
- return secret;
375
- }
376
-
377
- /**
378
- * Validates and sanitises sessions ttl.
379
- *
380
- * @param {number} ttl Session ttl (seconds).
381
- * @throws {ReferenceError} For missing value type.
382
- * @throws {TypeError} For invalid value.
383
- * @returns {number} Ttl.
384
- */
385
- function validateSessionsTtl(ttl) {
386
- if (typeof ttl === 'undefined') {
387
- throw ReferenceError('Session ttl is missing (sessions.ttl)')
388
- } else if (typeof ttl !== 'number') {
389
- throw new TypeError('Session ttl must be an integer (sessions.ttl)');
390
- }
391
- return ttl;
392
- }
393
-
394
- /**
395
- * Validates and sanitises sessions name.
396
- *
397
- * @param {string} name Session name.
398
- * @throws {ReferenceError} For missing value type.
399
- * @throws {TypeError} For invalid value.
400
- * @returns {string} Name.
401
- */
402
- function validateSessionsName(name) {
403
- if (typeof name === 'undefined') {
404
- throw ReferenceError('Session name is missing (sessions.name)')
405
- } else if (typeof name !== 'string') {
406
- throw new TypeError('Session name must be a string (sessions.name)');
407
- }
408
- return name;
409
- }
410
-
411
- /**
412
- * Validates and sanitises sessions secure flag.
413
- *
414
- * @param {boolean} secure Session secure flag.
415
- * @throws {ReferenceError} For missing value type.
416
- * @throws {TypeError} For invalid value.
417
- * @returns {string} Name.
418
- */
419
- function validateSessionsSecure(secure) {
420
- if (typeof secure === 'undefined') {
421
- throw ReferenceError('Session secure flag is missing (sessions.secure)')
422
- } else if (typeof secure !== 'boolean') {
423
- throw new TypeError('Session secure flag must be boolean (sessions.secure)');
424
- }
425
- return secure;
426
- }
427
-
428
- /**
429
- * Validates and sanitises sessions store.
430
- *
431
- * @param {Function} store Session store.
432
- * @returns {Function} Store.
433
- */
434
- function validateSessionsStore(store) {
435
- if (typeof store === 'undefined') {
436
- logger.warn('Using MemoryStore session storage, which is not suitable for production');
437
- return null;
438
- }
439
- return store;
440
- }
441
-
442
- /**
443
- * Validates and sanitises sessions cookie url path.
444
- *
445
- * @param {string} cookiePath Session cookie url path.
446
- * @param {string} defaultPath Default path if none specified.
447
- * @returns {string} Cookie path.
448
- */
449
- function validateSessionsCookiePath(cookiePath, defaultPath = '/') {
450
- if (typeof cookiePath === 'undefined') {
451
- return defaultPath;
452
- }
453
- return cookiePath;
454
- }
455
-
456
- /**
457
- * Validates and sanitises sessions cookie "sameSite" flag. One of:
458
- * true (Strict)
459
- * false (will not set the flag at all)
460
- * Strict
461
- * Lax
462
- * None
463
- *
464
- * @param {any} cookieSameSite Session cookie "sameSite" flag
465
- * @param {any} defaultFlag Default path if none specified
466
- * @returns {boolean} cookie path
467
- * @throws {TypeError} When invalid arguments are provided
468
- */
469
- function validateSessionsCookieSameSite(cookieSameSite, defaultFlag) {
470
- const validValues = [true, false, 'Strict', 'Lax', 'None'];
471
-
472
- if (defaultFlag === undefined) {
473
- throw new TypeError('validateSessionsCookieSameSite() requires an explicit default flag');
474
- } else if (!validValues.includes(defaultFlag)) {
475
- throw new TypeError('validateSessionsCookieSameSite() default flag must be set to one of true, false, Strict, Lax or None (sessions.cookieSameSite)');
476
- }
477
-
478
- const value = cookieSameSite !== undefined ? cookieSameSite : defaultFlag;
479
- if (!validValues.includes(value)) {
480
- throw new TypeError('SameSite flag must be set to one of true, false, Strict, Lax or None (sessions.cookieSameSite)');
481
- }
482
-
483
- return value;
484
- }
485
-
486
- /**
487
- * Ingest, validate, sanitise and manipulate configuration parameters.
488
- *
489
- * @param {object} config Config to ingest.
490
- * @throws {Error|SyntaxError|TypeError} For invalid config values.
491
- * @returns {object} Immutable config object.
492
- */
493
- function ingest(config = {}) {
494
- const validatedMountUrl = validateMountUrl(config.mountUrl);
495
-
496
- const parsed = {
497
- // Allow page editing functionality
498
- allowPageEdit: validateAllowPageEdit(config.allowPageEdit),
499
-
500
- // Use "sticky" edit mode
501
- useStickyEdit: validateUseStickyEdit(config.useStickyEdit),
502
-
503
- // Directory to store compiled assets
504
- compiledAssetsDir: validateCompiledAssetsDir(config.compiledAssetsDir),
505
-
506
- // Content security policies
507
- csp: validateContentSecurityPolicies(config.csp),
508
-
509
- // Headers
510
- headers: validateHeadersObject(config.headers, (headers) => ({
511
- disabled: validateHeadersDisabled(headers.disabled),
512
- })),
513
-
514
- // I18n configuration
515
- i18n: validateI18nObject(config.i18n, (i18n) => ({
516
- dirs: validateI18nDirs(i18n.dirs),
517
- locales: validateI18nLocales(i18n.locales),
518
- })),
519
-
520
- // Custom session expiry URL function
521
- sessionExpiryController: validateSessionExpiryController(
522
- config.sessionExpiryController,
523
- ),
524
-
525
- // Custom middleware-mountingfunction
526
- mountController: validateMountController(config.mountController),
527
-
528
- // Public URL from which the app will be served
529
- mountUrl: validatedMountUrl,
530
-
531
- // Internal (proxy) URL from which the app will be served
532
- proxyMountUrl: validateMountUrl(config.proxyMountUrl || validatedMountUrl, 'Proxy Mount URL'),
533
-
534
- // Phase
535
- phase: validatePhase(config.phase),
536
-
537
- // Service name
538
- serviceName: validateServiceName(config.serviceName),
539
-
540
- // Session
541
- sessions: validateSessionsObject(config.sessions, (sessions) => ({
542
- name: validateSessionsName(sessions.name),
543
- secret: validateSessionsSecret(sessions.secret),
544
- secure: validateSessionsSecure(sessions.secure),
545
- ttl: validateSessionsTtl(sessions.ttl),
546
- store: validateSessionsStore(sessions.store),
547
- cookiePath: validateSessionsCookiePath(sessions.cookiePath, validatedMountUrl),
548
- cookieSameSite: validateSessionsCookieSameSite(sessions.cookieSameSite, 'Strict'),
549
- })),
550
-
551
- // Views configuration
552
- views: validateViewsObject(config.views, (views) => ({
553
- dirs: validateViewsDirs(views.dirs),
554
- })),
555
- };
556
-
557
- // Freeze to modifications
558
- Object.freeze(parsed);
559
- return parsed;
560
- }
561
-
562
- module.exports = {
563
- ingest,
564
- validateAllowPageEdit,
565
- validateUseStickyEdit,
566
- validateCompiledAssetsDir,
567
- validateContentSecurityPolicies,
568
- validateHeadersObject,
569
- validateHeadersDisabled,
570
- validateI18nObject,
571
- validateI18nDirs,
572
- validateI18nLocales,
573
- validateMountController,
574
- validateMountUrl,
575
- validatePhase,
576
- validateServiceName,
577
- validateSessionExpiryController,
578
- validateSessionsObject,
579
- validateSessionsCookiePath,
580
- validateSessionsCookieSameSite,
581
- validateSessionsName,
582
- validateSessionsSecret,
583
- validateSessionsSecure,
584
- validateSessionsStore,
585
- validateSessionsTtl,
586
- validateViewsObject,
587
- validateViewsDirs,
588
- };
@@ -1,14 +0,0 @@
1
- /**
2
- * @deprecated Do not use this file in new applications.
3
- *
4
- * Use this instead:
5
- * const { gatherModifiers } = require('@dwp/govuk-casa');.
6
- */
7
-
8
- const util = require('util');
9
- const gatherModifiers = require('./gather-modifiers/index.js');
10
-
11
- module.exports = util.deprecate(
12
- () => (gatherModifiers),
13
- '@dwp/govuk-casa/lib/GatherModifiers.js is deprecated. Gather-Modifier functions are now accessed via \'require("@dwp/govuk-casa").gatherModifiers\'.',
14
- )();