@venizia/ignis 0.4.6 → 0.4.7-1

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 (203) hide show
  1. package/LICENSE.md +1 -4
  2. package/README.md +2 -2
  3. package/dist/esm/base/applications/base.d.ts.map +1 -1
  4. package/dist/esm/base/applications/base.js +19 -15
  5. package/dist/esm/base/applications/base.js.map +1 -1
  6. package/dist/esm/base/applications/server.d.ts +14 -9
  7. package/dist/esm/base/applications/server.d.ts.map +1 -1
  8. package/dist/esm/base/applications/server.js +36 -29
  9. package/dist/esm/base/applications/server.js.map +1 -1
  10. package/dist/esm/base/controllers/grpc/adapter.d.ts.map +1 -1
  11. package/dist/esm/base/controllers/grpc/adapter.js +5 -1
  12. package/dist/esm/base/controllers/grpc/adapter.js.map +1 -1
  13. package/dist/esm/base/middlewares/request-spy/request-spy.middleware.d.ts.map +1 -1
  14. package/dist/esm/base/middlewares/request-spy/request-spy.middleware.js +9 -3
  15. package/dist/esm/base/middlewares/request-spy/request-spy.middleware.js.map +1 -1
  16. package/dist/esm/common/constants.d.ts.map +1 -1
  17. package/dist/esm/common/constants.js +2 -2
  18. package/dist/esm/common/constants.js.map +1 -1
  19. package/dist/esm/components/api-reference/ui/scalar.d.ts.map +1 -1
  20. package/dist/esm/components/api-reference/ui/scalar.js +2 -1
  21. package/dist/esm/components/api-reference/ui/scalar.js.map +1 -1
  22. package/dist/esm/components/api-reference/ui/swagger.d.ts.map +1 -1
  23. package/dist/esm/components/api-reference/ui/swagger.js +2 -1
  24. package/dist/esm/components/api-reference/ui/swagger.js.map +1 -1
  25. package/dist/esm/components/auth/authenticate/component.js +2 -2
  26. package/dist/esm/components/auth/authenticate/component.js.map +1 -1
  27. package/dist/esm/components/auth/authenticate/controllers/factory.d.ts +139 -136
  28. package/dist/esm/components/auth/authenticate/controllers/factory.d.ts.map +1 -1
  29. package/dist/esm/components/auth/authenticate/controllers/factory.js +175 -172
  30. package/dist/esm/components/auth/authenticate/controllers/factory.js.map +1 -1
  31. package/dist/esm/components/auth/authenticate/controllers/jwks/definitions.js +2 -2
  32. package/dist/esm/components/auth/authenticate/controllers/jwks/definitions.js.map +1 -1
  33. package/dist/esm/components/auth/authenticate/controllers/service-certs/definitions.js +2 -2
  34. package/dist/esm/components/auth/authenticate/controllers/service-certs/definitions.js.map +1 -1
  35. package/dist/esm/components/auth/authenticate/services/bearer/abstract.service.d.ts.map +1 -1
  36. package/dist/esm/components/auth/authenticate/services/bearer/abstract.service.js +4 -2
  37. package/dist/esm/components/auth/authenticate/services/bearer/abstract.service.js.map +1 -1
  38. package/dist/esm/components/auth/authenticate/services/service/signer.service.d.ts.map +1 -1
  39. package/dist/esm/components/auth/authenticate/services/service/signer.service.js +6 -4
  40. package/dist/esm/components/auth/authenticate/services/service/signer.service.js.map +1 -1
  41. package/dist/esm/components/auth/authorize/adapters/base-filtered.js +2 -2
  42. package/dist/esm/components/auth/authorize/adapters/base-filtered.js.map +1 -1
  43. package/dist/esm/components/auth/authorize/adapters/custom-grant-expander.d.ts.map +1 -1
  44. package/dist/esm/components/auth/authorize/adapters/custom-grant-expander.js +2 -2
  45. package/dist/esm/components/auth/authorize/adapters/custom-grant-expander.js.map +1 -1
  46. package/dist/esm/components/auth/authorize/adapters/scoped-casbin/adapter.d.ts.map +1 -1
  47. package/dist/esm/components/auth/authorize/adapters/scoped-casbin/adapter.js +6 -3
  48. package/dist/esm/components/auth/authorize/adapters/scoped-casbin/adapter.js.map +1 -1
  49. package/dist/esm/components/auth/authorize/enforcers/casbin-module.d.ts.map +1 -1
  50. package/dist/esm/components/auth/authorize/enforcers/casbin-module.js +2 -1
  51. package/dist/esm/components/auth/authorize/enforcers/casbin-module.js.map +1 -1
  52. package/dist/esm/components/auth/authorize/enforcers/casbin.enforcer.d.ts.map +1 -1
  53. package/dist/esm/components/auth/authorize/enforcers/casbin.enforcer.js +9 -3
  54. package/dist/esm/components/auth/authorize/enforcers/casbin.enforcer.js.map +1 -1
  55. package/dist/esm/components/auth/models/entities/columns.d.ts +99 -0
  56. package/dist/esm/components/auth/models/entities/columns.d.ts.map +1 -0
  57. package/dist/esm/components/auth/models/entities/columns.js +133 -0
  58. package/dist/esm/components/auth/models/entities/columns.js.map +1 -0
  59. package/dist/esm/components/auth/models/entities/index.d.ts +1 -2
  60. package/dist/esm/components/auth/models/entities/index.d.ts.map +1 -1
  61. package/dist/esm/components/auth/models/entities/index.js +1 -2
  62. package/dist/esm/components/auth/models/entities/index.js.map +1 -1
  63. package/dist/esm/components/auth/models/entities/permission.model.d.ts +1 -12
  64. package/dist/esm/components/auth/models/entities/permission.model.d.ts.map +1 -1
  65. package/dist/esm/components/auth/models/entities/permission.model.js +1 -36
  66. package/dist/esm/components/auth/models/entities/permission.model.js.map +1 -1
  67. package/dist/esm/components/auth/models/entities/policy-definition.model.d.ts +1 -49
  68. package/dist/esm/components/auth/models/entities/policy-definition.model.d.ts.map +1 -1
  69. package/dist/esm/components/auth/models/entities/policy-definition.model.js +1 -69
  70. package/dist/esm/components/auth/models/entities/policy-definition.model.js.map +1 -1
  71. package/dist/esm/components/controller/grpc/grpc.component.js +2 -2
  72. package/dist/esm/components/controller/grpc/grpc.component.js.map +1 -1
  73. package/dist/esm/components/health-check/component.d.ts.map +1 -1
  74. package/dist/esm/components/health-check/component.js +5 -3
  75. package/dist/esm/components/health-check/component.js.map +1 -1
  76. package/dist/esm/components/health-check/controller.js +5 -5
  77. package/dist/esm/components/health-check/controller.js.map +1 -1
  78. package/dist/esm/components/index.d.ts +0 -1
  79. package/dist/esm/components/index.d.ts.map +1 -1
  80. package/dist/esm/components/index.js +0 -1
  81. package/dist/esm/components/index.js.map +1 -1
  82. package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.d.ts.map +1 -1
  83. package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.js +2 -1
  84. package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.js.map +1 -1
  85. package/dist/esm/components/mail/helpers/transporters/amazon-ses-transporter.helper.d.ts.map +1 -1
  86. package/dist/esm/components/mail/helpers/transporters/amazon-ses-transporter.helper.js +13 -9
  87. package/dist/esm/components/mail/helpers/transporters/amazon-ses-transporter.helper.js.map +1 -1
  88. package/dist/esm/components/mail/helpers/transporters/mailgun-transporter.helper.d.ts.map +1 -1
  89. package/dist/esm/components/mail/helpers/transporters/mailgun-transporter.helper.js +5 -3
  90. package/dist/esm/components/mail/helpers/transporters/mailgun-transporter.helper.js.map +1 -1
  91. package/dist/esm/components/mail/helpers/transporters/nodemail-transporter.helper.d.ts.map +1 -1
  92. package/dist/esm/components/mail/helpers/transporters/nodemail-transporter.helper.js +9 -7
  93. package/dist/esm/components/mail/helpers/transporters/nodemail-transporter.helper.js.map +1 -1
  94. package/dist/esm/components/mail/services/generators/verification-data.d.ts.map +1 -1
  95. package/dist/esm/components/mail/services/generators/verification-data.js +7 -3
  96. package/dist/esm/components/mail/services/generators/verification-data.js.map +1 -1
  97. package/dist/esm/components/mail/services/mail.service.d.ts.map +1 -1
  98. package/dist/esm/components/mail/services/mail.service.js +12 -9
  99. package/dist/esm/components/mail/services/mail.service.js.map +1 -1
  100. package/dist/esm/components/mail/utilities/address.utility.d.ts +12 -8
  101. package/dist/esm/components/mail/utilities/address.utility.d.ts.map +1 -1
  102. package/dist/esm/components/mail/utilities/address.utility.js +30 -27
  103. package/dist/esm/components/mail/utilities/address.utility.js.map +1 -1
  104. package/dist/esm/components/mail/utilities/attachment.utility.d.ts +31 -29
  105. package/dist/esm/components/mail/utilities/attachment.utility.d.ts.map +1 -1
  106. package/dist/esm/components/mail/utilities/attachment.utility.js +94 -86
  107. package/dist/esm/components/mail/utilities/attachment.utility.js.map +1 -1
  108. package/dist/esm/components/mail/utilities/body.utility.d.ts +6 -4
  109. package/dist/esm/components/mail/utilities/body.utility.d.ts.map +1 -1
  110. package/dist/esm/components/mail/utilities/body.utility.js +14 -12
  111. package/dist/esm/components/mail/utilities/body.utility.js.map +1 -1
  112. package/dist/esm/components/mail/utilities/mime.utility.d.ts +23 -17
  113. package/dist/esm/components/mail/utilities/mime.utility.d.ts.map +1 -1
  114. package/dist/esm/components/mail/utilities/mime.utility.js +109 -98
  115. package/dist/esm/components/mail/utilities/mime.utility.js.map +1 -1
  116. package/dist/esm/components/mail/utilities/verification.utility.d.ts +8 -2
  117. package/dist/esm/components/mail/utilities/verification.utility.d.ts.map +1 -1
  118. package/dist/esm/components/mail/utilities/verification.utility.js +7 -5
  119. package/dist/esm/components/mail/utilities/verification.utility.js.map +1 -1
  120. package/dist/esm/components/socket-io/component.js +3 -3
  121. package/dist/esm/components/socket-io/component.js.map +1 -1
  122. package/dist/esm/components/socket-io/handlers/bun.handler.d.ts +13 -11
  123. package/dist/esm/components/socket-io/handlers/bun.handler.d.ts.map +1 -1
  124. package/dist/esm/components/socket-io/handlers/bun.handler.js +32 -30
  125. package/dist/esm/components/socket-io/handlers/bun.handler.js.map +1 -1
  126. package/dist/esm/components/static-asset/common/commit-token.d.ts +21 -16
  127. package/dist/esm/components/static-asset/common/commit-token.d.ts.map +1 -1
  128. package/dist/esm/components/static-asset/common/commit-token.js +35 -28
  129. package/dist/esm/components/static-asset/common/commit-token.js.map +1 -1
  130. package/dist/esm/components/static-asset/common/constants.d.ts +0 -2
  131. package/dist/esm/components/static-asset/common/constants.d.ts.map +1 -1
  132. package/dist/esm/components/static-asset/common/constants.js +1 -3
  133. package/dist/esm/components/static-asset/common/constants.js.map +1 -1
  134. package/dist/esm/components/static-asset/common/types.d.ts +0 -4
  135. package/dist/esm/components/static-asset/common/types.d.ts.map +1 -1
  136. package/dist/esm/components/static-asset/common/types.js.map +1 -1
  137. package/dist/esm/components/static-asset/component.js +2 -2
  138. package/dist/esm/components/static-asset/component.js.map +1 -1
  139. package/dist/esm/components/static-asset/controller/base.definition.d.ts +6 -6
  140. package/dist/esm/components/static-asset/controller/base.definition.d.ts.map +1 -1
  141. package/dist/esm/components/static-asset/controller/base.definition.js +224 -224
  142. package/dist/esm/components/static-asset/controller/base.definition.js.map +1 -1
  143. package/dist/esm/components/static-asset/controller/factory.d.ts +23 -23
  144. package/dist/esm/components/static-asset/controller/factory.d.ts.map +1 -1
  145. package/dist/esm/components/static-asset/controller/factory.js +104 -74
  146. package/dist/esm/components/static-asset/controller/factory.js.map +1 -1
  147. package/dist/esm/components/static-asset/controller/key-scope.d.ts +28 -26
  148. package/dist/esm/components/static-asset/controller/key-scope.d.ts.map +1 -1
  149. package/dist/esm/components/static-asset/controller/key-scope.js +34 -30
  150. package/dist/esm/components/static-asset/controller/key-scope.js.map +1 -1
  151. package/dist/esm/components/static-asset/controller/meta-link.d.ts +37 -33
  152. package/dist/esm/components/static-asset/controller/meta-link.d.ts.map +1 -1
  153. package/dist/esm/components/static-asset/controller/meta-link.js +86 -84
  154. package/dist/esm/components/static-asset/controller/meta-link.js.map +1 -1
  155. package/dist/esm/components/static-asset/controller/route-options.d.ts +18 -16
  156. package/dist/esm/components/static-asset/controller/route-options.d.ts.map +1 -1
  157. package/dist/esm/components/static-asset/controller/route-options.js +28 -26
  158. package/dist/esm/components/static-asset/controller/route-options.js.map +1 -1
  159. package/dist/esm/components/static-asset/models/base.model.js +3 -3
  160. package/dist/esm/components/static-asset/models/base.model.js.map +1 -1
  161. package/dist/esm/components/websocket/component.js +2 -2
  162. package/dist/esm/components/websocket/component.js.map +1 -1
  163. package/dist/esm/components/websocket/handlers/bun.handler.d.ts +6 -4
  164. package/dist/esm/components/websocket/handlers/bun.handler.d.ts.map +1 -1
  165. package/dist/esm/components/websocket/handlers/bun.handler.js +21 -19
  166. package/dist/esm/components/websocket/handlers/bun.handler.js.map +1 -1
  167. package/dist/esm/testing/assert-application-boots.d.ts +6 -1
  168. package/dist/esm/testing/assert-application-boots.d.ts.map +1 -1
  169. package/dist/esm/testing/assert-application-boots.js +139 -129
  170. package/dist/esm/testing/assert-application-boots.js.map +1 -1
  171. package/dist/esm/utilities/drizzle-result.utility.d.ts +6 -8
  172. package/dist/esm/utilities/drizzle-result.utility.d.ts.map +1 -1
  173. package/dist/esm/utilities/drizzle-result.utility.js +23 -49
  174. package/dist/esm/utilities/drizzle-result.utility.js.map +1 -1
  175. package/dist/esm/utilities/network.utility.d.ts.map +1 -1
  176. package/dist/esm/utilities/network.utility.js +1 -1
  177. package/dist/esm/utilities/network.utility.js.map +1 -1
  178. package/dist/presets/app/files/src/boot.test.ts +2 -2
  179. package/dist/presets/app/files/src/controllers/hello.controller.ts +2 -2
  180. package/dist/presets/app/preset.json +4 -4
  181. package/dist/presets/lib/preset.json +4 -4
  182. package/dist/presets/shared/files/.configs/formatters/.ignore +4 -0
  183. package/dist/presets/shared/files/.configs/lints/.ignore +4 -0
  184. package/dist/presets/shared/files/.configs/lints/eslint.config.mjs +4 -0
  185. package/dist/presets/shared/files/.configs/lints/oxlint.config.mts +4 -0
  186. package/dist/presets/shared/files/README.md +15 -0
  187. package/dist/presets/shared/files/src/index.test.ts +8 -0
  188. package/dist/presets/shared/files/src/index.ts +8 -0
  189. package/dist/presets/shared/files/tsconfig.json +20 -0
  190. package/dist/presets/shared/preset.json +35 -0
  191. package/package.json +11 -11
  192. package/dist/esm/components/auth/models/entities/role.model.d.ts +0 -10
  193. package/dist/esm/components/auth/models/entities/role.model.d.ts.map +0 -1
  194. package/dist/esm/components/auth/models/entities/role.model.js +0 -15
  195. package/dist/esm/components/auth/models/entities/role.model.js.map +0 -1
  196. package/dist/esm/components/auth/models/entities/user.model.d.ts +0 -28
  197. package/dist/esm/components/auth/models/entities/user.model.d.ts.map +0 -1
  198. package/dist/esm/components/auth/models/entities/user.model.js +0 -17
  199. package/dist/esm/components/auth/models/entities/user.model.js.map +0 -1
  200. package/dist/esm/components/controller/index.d.ts +0 -2
  201. package/dist/esm/components/controller/index.d.ts.map +0 -1
  202. package/dist/esm/components/controller/index.js +0 -7
  203. package/dist/esm/components/controller/index.js.map +0 -1
@@ -67,10 +67,11 @@ const resolveConfinedPath = async (opts) => {
67
67
  let filePath;
68
68
  // `resolve` throws synchronously on a non-string `path` from an untyped caller; it lands here too.
69
69
  try {
70
- [rootPath, filePath] = await Promise.all([
70
+ const resolved = await Promise.all([
71
71
  realpath(attachmentRoot),
72
72
  realpath(resolve(attachmentRoot, path)),
73
73
  ]);
74
+ [rootPath, filePath] = resolved;
74
75
  }
75
76
  catch (error) {
76
77
  throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON, cause: error });
@@ -106,103 +107,110 @@ const readConfinedFile = async (opts) => {
106
107
  autoClose: false,
107
108
  end: maxBytes === undefined ? Infinity : maxBytes - usedBytes,
108
109
  });
109
- return await drainStream({ stream, usedBytes, maxBytes });
110
+ const result = await drainStream({ stream, usedBytes, maxBytes });
111
+ return result;
110
112
  }
111
113
  finally {
112
114
  await handle.close();
113
115
  }
114
116
  };
115
- /**
116
- * Reads one attachment's bytes. A string `content` is raw text unless `encoding` says otherwise
117
- * (nodemailer's convention); `null` counts as absent. A `path` is read only inside
118
- * `attachmentRoot` and refused without one. `maxBytes` caps the whole message; `usedBytes` is what
119
- * earlier attachments already took.
120
- */
121
- export const readAttachmentContent = async (opts) => {
122
- const { attachment, attachmentRoot, maxBytes, usedBytes = 0 } = opts;
123
- const { content } = attachment;
124
- if (content !== undefined && content !== null) {
125
- if (typeof content === 'string') {
126
- const encoding = attachment.encoding ?? 'utf-8';
127
- assertWithinLimit({ byteLength: Buffer.byteLength(content, encoding), usedBytes, maxBytes });
128
- return Buffer.from(content, encoding);
129
- }
130
- if (content instanceof Uint8Array) {
131
- assertWithinLimit({ byteLength: content.byteLength, usedBytes, maxBytes });
132
- return toBuffer(content);
117
+ export class MailAttachmentUtility {
118
+ /**
119
+ * Reads one attachment's bytes. A string `content` is raw text unless `encoding` says otherwise
120
+ * (nodemailer's convention); `null` counts as absent. A `path` is read only inside
121
+ * `attachmentRoot` and refused without one. `maxBytes` caps the whole message; `usedBytes` is what
122
+ * earlier attachments already took.
123
+ */
124
+ static async readContent(opts) {
125
+ const { attachment, attachmentRoot, maxBytes, usedBytes = 0 } = opts;
126
+ const { content } = attachment;
127
+ if (content !== undefined && content !== null) {
128
+ if (typeof content === 'string') {
129
+ const encoding = attachment.encoding ?? 'utf-8';
130
+ assertWithinLimit({
131
+ byteLength: Buffer.byteLength(content, encoding),
132
+ usedBytes,
133
+ maxBytes,
134
+ });
135
+ return Buffer.from(content, encoding);
136
+ }
137
+ if (content instanceof Uint8Array) {
138
+ assertWithinLimit({ byteLength: content.byteLength, usedBytes, maxBytes });
139
+ return toBuffer(content);
140
+ }
141
+ if (!isAttachmentStream(content)) {
142
+ throw getError({
143
+ error: MailErrors.INVALID_CONFIGURATION,
144
+ message: 'Invalid mail attachment | attachment.content must be a Buffer, a Uint8Array, a string or a stream',
145
+ });
146
+ }
147
+ return drainStream({ stream: content, usedBytes, maxBytes });
133
148
  }
134
- if (!isAttachmentStream(content)) {
135
- throw getError({
136
- error: MailErrors.INVALID_CONFIGURATION,
137
- message: 'Invalid mail attachment | attachment.content must be a Buffer, a Uint8Array, a string or a stream',
138
- });
149
+ if (attachment.path !== undefined && attachment.path !== null) {
150
+ return readConfinedFile({ path: attachment.path, usedBytes, attachmentRoot, maxBytes });
139
151
  }
140
- return drainStream({ stream: content, usedBytes, maxBytes });
141
- }
142
- if (attachment.path !== undefined && attachment.path !== null) {
143
- return readConfinedFile({ path: attachment.path, usedBytes, attachmentRoot, maxBytes });
152
+ return Buffer.alloc(0);
144
153
  }
145
- return Buffer.alloc(0);
146
- };
147
- /**
148
- * Turns every attachment into `content` bytes and drops `path`, so no transport is ever handed a
149
- * file or URL to read on its own. `maxBytes` is shared by all attachments of the message. An
150
- * attachment read from `path` keeps the file's name and, when none is given, its content type.
151
- */
152
- export const resolveMailAttachments = async (opts) => {
153
- const { attachments, attachmentRoot, maxBytes } = opts;
154
- const resolved = [];
155
- let usedBytes = 0;
156
- // Sequential on purpose: each read spends the budget the previous ones left.
157
- for (const attachment of attachments) {
158
- const { path, href, raw, content, encoding, ...rest } = attachment;
159
- // nodemailer reads both on its own - `href` fetches a URL, `raw` can carry a `path`.
160
- if ((href !== undefined && href !== null) || (raw !== undefined && raw !== null)) {
161
- throw buildPathRefusedError({
162
- reason: 'attachment.href and attachment.raw are not supported | pass the bytes as content',
154
+ /**
155
+ * Turns every attachment into `content` bytes and drops `path`, so no transport is ever handed a
156
+ * file or URL to read on its own. `maxBytes` is shared by all attachments of the message. An
157
+ * attachment read from `path` keeps the file's name and, when none is given, its content type.
158
+ */
159
+ static async resolve(opts) {
160
+ const { attachments, attachmentRoot, maxBytes } = opts;
161
+ const resolved = [];
162
+ let usedBytes = 0;
163
+ // Sequential on purpose: each read spends the budget the previous ones left.
164
+ for (const attachment of attachments) {
165
+ const { path, href, raw, content, encoding, ...rest } = attachment;
166
+ // nodemailer reads both on its own - `href` fetches a URL, `raw` can carry a `path`.
167
+ if ((href !== undefined && href !== null) || (raw !== undefined && raw !== null)) {
168
+ throw buildPathRefusedError({
169
+ reason: 'attachment.href and attachment.raw are not supported | pass the bytes as content',
170
+ });
171
+ }
172
+ const bytes = await MailAttachmentUtility.readContent({
173
+ attachment: { content, encoding, path },
174
+ attachmentRoot,
175
+ maxBytes,
176
+ usedBytes,
177
+ });
178
+ usedBytes += bytes.byteLength;
179
+ const isReadFromPath = (content === undefined || content === null) && typeof path === 'string';
180
+ const filename = rest.filename ?? (isReadFromPath ? basename(path) : undefined);
181
+ const contentType = rest.contentType ??
182
+ (isReadFromPath && filename ? ContentTypeTable.resolve({ filename }) : undefined);
183
+ resolved.push({
184
+ ...rest,
185
+ ...(filename === undefined ? {} : { filename }),
186
+ ...(contentType === undefined ? {} : { contentType }),
187
+ content: bytes,
163
188
  });
164
189
  }
165
- const bytes = await readAttachmentContent({
166
- attachment: { content, encoding, path },
167
- attachmentRoot,
168
- maxBytes,
169
- usedBytes,
170
- });
171
- usedBytes += bytes.byteLength;
172
- const isReadFromPath = (content === undefined || content === null) && typeof path === 'string';
173
- const filename = rest.filename ?? (isReadFromPath ? basename(path) : undefined);
174
- const contentType = rest.contentType ??
175
- (isReadFromPath && filename ? ContentTypeTable.resolve({ filename }) : undefined);
176
- resolved.push({
177
- ...rest,
178
- ...(filename === undefined ? {} : { filename }),
179
- ...(contentType === undefined ? {} : { contentType }),
180
- content: bytes,
181
- });
190
+ return resolved;
182
191
  }
183
- return resolved;
184
- };
185
- /**
186
- * Releases every attachment stream of a refused send, read or not, so none keeps a descriptor
187
- * open. A drained stream is already closed; releasing it again is harmless.
188
- */
189
- export const releaseAttachmentStreams = (opts) => {
190
- for (const attachment of opts.attachments ?? []) {
191
- const { content } = attachment;
192
- if (content instanceof ReadableStream) {
193
- if (!content.locked) {
194
- content.cancel().catch((error) => {
195
- logger.warn('[releaseAttachmentStreams] Cancelling a web stream failed | error: %s', error);
196
- });
192
+ /**
193
+ * Releases every attachment stream of a refused send, read or not, so none keeps a descriptor
194
+ * open. A drained stream is already closed; releasing it again is harmless.
195
+ */
196
+ static release(opts) {
197
+ for (const attachment of opts.attachments ?? []) {
198
+ const { content } = attachment;
199
+ if (content instanceof ReadableStream) {
200
+ if (!content.locked) {
201
+ content.cancel().catch((error) => {
202
+ logger.warn('[MailAttachmentUtility.release] Cancelling a web stream failed | error: %s', error);
203
+ });
204
+ }
205
+ continue;
206
+ }
207
+ if (typeof content === 'object' &&
208
+ content !== null &&
209
+ 'destroy' in content &&
210
+ typeof content.destroy === 'function') {
211
+ content.destroy();
197
212
  }
198
- continue;
199
- }
200
- if (typeof content === 'object' &&
201
- content !== null &&
202
- 'destroy' in content &&
203
- typeof content.destroy === 'function') {
204
- content.destroy();
205
213
  }
206
214
  }
207
- };
215
+ }
208
216
  //# sourceMappingURL=attachment.utility.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"attachment.utility.js","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/attachment.utility.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AA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gBAAgB,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QAEpF,QAAQ,CAAC,IAAI,CAAC;YACZ,GAAG,IAAI;YACP,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC;YAC/C,GAAG,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC;YACrD,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;IACL,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,IAAyC,EAAQ,EAAE;IAC1F,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,WAAW,IAAI,EAAE,EAAE,CAAC;QAChD,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;QAE/B,IAAI,OAAO,YAAY,cAAc,EAAE,CAAC;YACtC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;gBACpB,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBAC/B,MAAM,CAAC,IAAI,CACT,uEAAuE,EACvE,KAAK,CACN,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;YAED,SAAS;QACX,CAAC;QAED,IACE,OAAO,OAAO,KAAK,QAAQ;YAC3B,OAAO,KAAK,IAAI;YAChB,SAAS,IAAI,OAAO;YACpB,OAAO,OAAO,CAAC,OAAO,KAAK,UAAU,EACrC,CAAC;YACD,OAAO,CAAC,OAAO,EAAE,CAAC;QACpB,CAAC;IACH,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { constants } from 'node:fs';\nimport { open, realpath } from 'node:fs/promises';\nimport { basename, isAbsolute, relative, resolve, sep } from 'node:path';\nimport { ContentTypeTable } from '@venizia/ignis-helpers/common';\nimport { getError } from '@venizia/ignis-helpers/core';\nimport { LoggerFactory } from '@venizia/ignis-helpers';\nimport { MailErrors, type IMailAttachment } from '../common';\n\nconst logger = LoggerFactory.getLogger(['MailAttachments']);\n\n// O_NONBLOCK: opening a FIFO must not wait for a writer; the fstat check then refuses it.\n// Windows has neither flag; there the realpath containment check stands alone.\nconst OPEN_FLAGS = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);\n\n// One reason for missing, directory, unreadable and escaping paths: no existence oracle.\nconst PATH_OUTSIDE_REASON =\n 'attachment.path must name a readable file inside attachmentRoot | pass the bytes as content instead';\n\ntype TAttachmentStream = AsyncIterable<Uint8Array | string>;\n\nconst buildPathRefusedError = (opts: { reason: string; cause?: unknown }) => {\n return getError({\n error: MailErrors.ATTACHMENT_PATH_REFUSED,\n message: `Mail attachment path refused | ${opts.reason}`,\n cause: opts.cause,\n });\n};\n\nconst buildTooLargeError = (opts: { maxBytes: number }) => {\n return getError({\n error: MailErrors.ATTACHMENT_TOO_LARGE,\n message: `Mail attachments are over maxAttachmentBytes (${opts.maxBytes} bytes per message) | raise maxAttachmentBytes on the mail options, or send smaller attachments`,\n });\n};\n\nconst assertWithinLimit = (opts: { byteLength: number; usedBytes: number; maxBytes?: number }) => {\n const { byteLength, usedBytes, maxBytes } = opts;\n\n if (maxBytes === undefined || usedBytes + byteLength <= maxBytes) {\n return;\n }\n\n throw buildTooLargeError({ maxBytes });\n};\n\nconst isAttachmentStream = (value: unknown): value is TAttachmentStream => {\n return typeof value === 'object' && value !== null && Symbol.asyncIterator in value;\n};\n\nconst toBuffer = (chunk: Uint8Array | string): Buffer => {\n if (typeof chunk === 'string') {\n return Buffer.from(chunk);\n }\n\n return Buffer.isBuffer(chunk)\n ? chunk\n : Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);\n};\n\n/** Throwing out of `for await` destroys a Node Readable and cancels a web ReadableStream. */\nconst drainStream = async (opts: {\n stream: TAttachmentStream;\n usedBytes: number;\n maxBytes?: number;\n}): Promise<Buffer> => {\n const { stream, usedBytes, maxBytes } = opts;\n const chunks: Buffer[] = [];\n let streamBytes = 0;\n\n for await (const chunk of stream) {\n const buffer = toBuffer(chunk);\n streamBytes += buffer.byteLength;\n\n // Per chunk: a stream has no length up front, and the rest must never be buffered.\n assertWithinLimit({ byteLength: streamBytes, usedBytes, maxBytes });\n chunks.push(buffer);\n }\n\n return Buffer.concat(chunks, streamBytes);\n};\n\n/**\n * Both sides go through realpath, so neither `..` nor a symlink can leave the root. Not closed: a\n * directory inside the root swapped for a symlink between this check and `open` - that takes write\n * access inside the root, so keep the root writable only by what the application trusts.\n */\nconst resolveConfinedPath = async (opts: {\n path: string;\n attachmentRoot: string;\n}): Promise<string> => {\n const { path, attachmentRoot } = opts;\n\n let rootPath: string;\n let filePath: string;\n\n // `resolve` throws synchronously on a non-string `path` from an untyped caller; it lands here too.\n try {\n [rootPath, filePath] = await Promise.all([\n realpath(attachmentRoot),\n realpath(resolve(attachmentRoot, path)),\n ]);\n } catch (error) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON, cause: error });\n }\n\n const relation = relative(rootPath, filePath);\n const isOutside =\n relation === '' || relation === '..' || relation.startsWith(`..${sep}`) || isAbsolute(relation);\n\n if (isOutside) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON });\n }\n\n return filePath;\n};\n\nconst readConfinedFile = async (opts: {\n path: string;\n usedBytes: number;\n attachmentRoot?: string;\n maxBytes?: number;\n}): Promise<Buffer> => {\n const { path, usedBytes, attachmentRoot, maxBytes } = opts;\n\n if (!attachmentRoot) {\n throw buildPathRefusedError({\n reason:\n 'attachment.path is read only under attachmentRoot, which is not set | set attachmentRoot on the mail options, or pass the bytes as content',\n });\n }\n\n const filePath = await resolveConfinedPath({ path, attachmentRoot });\n const handle = await open(filePath, OPEN_FLAGS).catch((error) => {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON, cause: error });\n });\n\n try {\n const stats = await handle.stat();\n\n if (!stats.isFile()) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON });\n }\n\n // Sized before reading, so a file known to be oversized is never opened for reading.\n assertWithinLimit({ byteLength: stats.size, usedBytes, maxBytes });\n\n // Counted while read, and read no further than one byte past the budget: the file may have\n // grown since fstat.\n const stream = handle.createReadStream({\n autoClose: false,\n end: maxBytes === undefined ? Infinity : maxBytes - usedBytes,\n });\n\n return await drainStream({ stream, usedBytes, maxBytes });\n } finally {\n await handle.close();\n }\n};\n\n/**\n * Reads one attachment's bytes. A string `content` is raw text unless `encoding` says otherwise\n * (nodemailer's convention); `null` counts as absent. A `path` is read only inside\n * `attachmentRoot` and refused without one. `maxBytes` caps the whole message; `usedBytes` is what\n * earlier attachments already took.\n */\nexport const readAttachmentContent = async (opts: {\n attachment: IMailAttachment;\n attachmentRoot?: string;\n maxBytes?: number;\n usedBytes?: number;\n}): Promise<Buffer> => {\n const { attachment, attachmentRoot, maxBytes, usedBytes = 0 } = opts;\n const { content } = attachment;\n\n if (content !== undefined && content !== null) {\n if (typeof content === 'string') {\n const encoding = attachment.encoding ?? 'utf-8';\n assertWithinLimit({ byteLength: Buffer.byteLength(content, encoding), usedBytes, maxBytes });\n return Buffer.from(content, encoding);\n }\n\n if (content instanceof Uint8Array) {\n assertWithinLimit({ byteLength: content.byteLength, usedBytes, maxBytes });\n return toBuffer(content);\n }\n\n if (!isAttachmentStream(content)) {\n throw getError({\n error: MailErrors.INVALID_CONFIGURATION,\n message:\n 'Invalid mail attachment | attachment.content must be a Buffer, a Uint8Array, a string or a stream',\n });\n }\n\n return drainStream({ stream: content, usedBytes, maxBytes });\n }\n\n if (attachment.path !== undefined && attachment.path !== null) {\n return readConfinedFile({ path: attachment.path, usedBytes, attachmentRoot, maxBytes });\n }\n\n return Buffer.alloc(0);\n};\n\n/**\n * Turns every attachment into `content` bytes and drops `path`, so no transport is ever handed a\n * file or URL to read on its own. `maxBytes` is shared by all attachments of the message. An\n * attachment read from `path` keeps the file's name and, when none is given, its content type.\n */\nexport const resolveMailAttachments = async (opts: {\n attachments: IMailAttachment[];\n attachmentRoot?: string;\n maxBytes?: number;\n}): Promise<IMailAttachment[]> => {\n const { attachments, attachmentRoot, maxBytes } = opts;\n const resolved: IMailAttachment[] = [];\n let usedBytes = 0;\n\n // Sequential on purpose: each read spends the budget the previous ones left.\n for (const attachment of attachments) {\n const { path, href, raw, content, encoding, ...rest } = attachment;\n\n // nodemailer reads both on its own - `href` fetches a URL, `raw` can carry a `path`.\n if ((href !== undefined && href !== null) || (raw !== undefined && raw !== null)) {\n throw buildPathRefusedError({\n reason: 'attachment.href and attachment.raw are not supported | pass the bytes as content',\n });\n }\n\n const bytes = await readAttachmentContent({\n attachment: { content, encoding, path },\n attachmentRoot,\n maxBytes,\n usedBytes,\n });\n\n usedBytes += bytes.byteLength;\n\n const isReadFromPath = (content === undefined || content === null) && typeof path === 'string';\n const filename = rest.filename ?? (isReadFromPath ? basename(path) : undefined);\n const contentType =\n rest.contentType ??\n (isReadFromPath && filename ? ContentTypeTable.resolve({ filename }) : undefined);\n\n resolved.push({\n ...rest,\n ...(filename === undefined ? {} : { filename }),\n ...(contentType === undefined ? {} : { contentType }),\n content: bytes,\n });\n }\n\n return resolved;\n};\n\n/**\n * Releases every attachment stream of a refused send, read or not, so none keeps a descriptor\n * open. A drained stream is already closed; releasing it again is harmless.\n */\nexport const releaseAttachmentStreams = (opts: { attachments?: IMailAttachment[] }): void => {\n for (const attachment of opts.attachments ?? []) {\n const { content } = attachment;\n\n if (content instanceof ReadableStream) {\n if (!content.locked) {\n content.cancel().catch((error) => {\n logger.warn(\n '[releaseAttachmentStreams] Cancelling a web stream failed | error: %s',\n error,\n );\n });\n }\n\n continue;\n }\n\n if (\n typeof content === 'object' &&\n content !== null &&\n 'destroy' in content &&\n typeof content.destroy === 'function'\n ) {\n content.destroy();\n }\n }\n};\n"]}
1
+ {"version":3,"file":"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IAAI,OAAO;gBACpB,OAAO,OAAO,CAAC,OAAO,KAAK,UAAU,EACrC,CAAC;gBACD,OAAO,CAAC,OAAO,EAAE,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { constants } from 'node:fs';\nimport { open, realpath } from 'node:fs/promises';\nimport { basename, isAbsolute, relative, resolve, sep } from 'node:path';\nimport { ContentTypeTable } from '@venizia/ignis-helpers/common';\nimport { getError } from '@venizia/ignis-helpers/core';\nimport { LoggerFactory } from '@venizia/ignis-helpers';\nimport { MailErrors, type IMailAttachment } from '../common';\n\nconst logger = LoggerFactory.getLogger(['MailAttachments']);\n\n// O_NONBLOCK: opening a FIFO must not wait for a writer; the fstat check then refuses it.\n// Windows has neither flag; there the realpath containment check stands alone.\nconst OPEN_FLAGS = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);\n\n// One reason for missing, directory, unreadable and escaping paths: no existence oracle.\nconst PATH_OUTSIDE_REASON =\n 'attachment.path must name a readable file inside attachmentRoot | pass the bytes as content instead';\n\ntype TAttachmentStream = AsyncIterable<Uint8Array | string>;\n\nconst buildPathRefusedError = (opts: { reason: string; cause?: unknown }) => {\n return getError({\n error: MailErrors.ATTACHMENT_PATH_REFUSED,\n message: `Mail attachment path refused | ${opts.reason}`,\n cause: opts.cause,\n });\n};\n\nconst buildTooLargeError = (opts: { maxBytes: number }) => {\n return getError({\n error: MailErrors.ATTACHMENT_TOO_LARGE,\n message: `Mail attachments are over maxAttachmentBytes (${opts.maxBytes} bytes per message) | raise maxAttachmentBytes on the mail options, or send smaller attachments`,\n });\n};\n\nconst assertWithinLimit = (opts: { byteLength: number; usedBytes: number; maxBytes?: number }) => {\n const { byteLength, usedBytes, maxBytes } = opts;\n\n if (maxBytes === undefined || usedBytes + byteLength <= maxBytes) {\n return;\n }\n\n throw buildTooLargeError({ maxBytes });\n};\n\nconst isAttachmentStream = (value: unknown): value is TAttachmentStream => {\n return typeof value === 'object' && value !== null && Symbol.asyncIterator in value;\n};\n\nconst toBuffer = (chunk: Uint8Array | string): Buffer => {\n if (typeof chunk === 'string') {\n return Buffer.from(chunk);\n }\n\n return Buffer.isBuffer(chunk)\n ? chunk\n : Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);\n};\n\n/** Throwing out of `for await` destroys a Node Readable and cancels a web ReadableStream. */\nconst drainStream = async (opts: {\n stream: TAttachmentStream;\n usedBytes: number;\n maxBytes?: number;\n}): Promise<Buffer> => {\n const { stream, usedBytes, maxBytes } = opts;\n const chunks: Buffer[] = [];\n let streamBytes = 0;\n\n for await (const chunk of stream) {\n const buffer = toBuffer(chunk);\n streamBytes += buffer.byteLength;\n\n // Per chunk: a stream has no length up front, and the rest must never be buffered.\n assertWithinLimit({ byteLength: streamBytes, usedBytes, maxBytes });\n chunks.push(buffer);\n }\n\n return Buffer.concat(chunks, streamBytes);\n};\n\n/**\n * Both sides go through realpath, so neither `..` nor a symlink can leave the root. Not closed: a\n * directory inside the root swapped for a symlink between this check and `open` - that takes write\n * access inside the root, so keep the root writable only by what the application trusts.\n */\nconst resolveConfinedPath = async (opts: {\n path: string;\n attachmentRoot: string;\n}): Promise<string> => {\n const { path, attachmentRoot } = opts;\n\n let rootPath: string;\n let filePath: string;\n\n // `resolve` throws synchronously on a non-string `path` from an untyped caller; it lands here too.\n try {\n const resolved = await Promise.all([\n realpath(attachmentRoot),\n realpath(resolve(attachmentRoot, path)),\n ]);\n [rootPath, filePath] = resolved;\n } catch (error) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON, cause: error });\n }\n\n const relation = relative(rootPath, filePath);\n const isOutside =\n relation === '' || relation === '..' || relation.startsWith(`..${sep}`) || isAbsolute(relation);\n\n if (isOutside) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON });\n }\n\n return filePath;\n};\n\nconst readConfinedFile = async (opts: {\n path: string;\n usedBytes: number;\n attachmentRoot?: string;\n maxBytes?: number;\n}): Promise<Buffer> => {\n const { path, usedBytes, attachmentRoot, maxBytes } = opts;\n\n if (!attachmentRoot) {\n throw buildPathRefusedError({\n reason:\n 'attachment.path is read only under attachmentRoot, which is not set | set attachmentRoot on the mail options, or pass the bytes as content',\n });\n }\n\n const filePath = await resolveConfinedPath({ path, attachmentRoot });\n const handle = await open(filePath, OPEN_FLAGS).catch((error) => {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON, cause: error });\n });\n\n try {\n const stats = await handle.stat();\n\n if (!stats.isFile()) {\n throw buildPathRefusedError({ reason: PATH_OUTSIDE_REASON });\n }\n\n // Sized before reading, so a file known to be oversized is never opened for reading.\n assertWithinLimit({ byteLength: stats.size, usedBytes, maxBytes });\n\n // Counted while read, and read no further than one byte past the budget: the file may have\n // grown since fstat.\n const stream = handle.createReadStream({\n autoClose: false,\n end: maxBytes === undefined ? Infinity : maxBytes - usedBytes,\n });\n\n const result = await drainStream({ stream, usedBytes, maxBytes });\n return result;\n } finally {\n await handle.close();\n }\n};\n\nexport class MailAttachmentUtility {\n /**\n * Reads one attachment's bytes. A string `content` is raw text unless `encoding` says otherwise\n * (nodemailer's convention); `null` counts as absent. A `path` is read only inside\n * `attachmentRoot` and refused without one. `maxBytes` caps the whole message; `usedBytes` is what\n * earlier attachments already took.\n */\n static async readContent(opts: {\n attachment: IMailAttachment;\n attachmentRoot?: string;\n maxBytes?: number;\n usedBytes?: number;\n }): Promise<Buffer> {\n const { attachment, attachmentRoot, maxBytes, usedBytes = 0 } = opts;\n const { content } = attachment;\n\n if (content !== undefined && content !== null) {\n if (typeof content === 'string') {\n const encoding = attachment.encoding ?? 'utf-8';\n assertWithinLimit({\n byteLength: Buffer.byteLength(content, encoding),\n usedBytes,\n maxBytes,\n });\n return Buffer.from(content, encoding);\n }\n\n if (content instanceof Uint8Array) {\n assertWithinLimit({ byteLength: content.byteLength, usedBytes, maxBytes });\n return toBuffer(content);\n }\n\n if (!isAttachmentStream(content)) {\n throw getError({\n error: MailErrors.INVALID_CONFIGURATION,\n message:\n 'Invalid mail attachment | attachment.content must be a Buffer, a Uint8Array, a string or a stream',\n });\n }\n\n return drainStream({ stream: content, usedBytes, maxBytes });\n }\n\n if (attachment.path !== undefined && attachment.path !== null) {\n return readConfinedFile({ path: attachment.path, usedBytes, attachmentRoot, maxBytes });\n }\n\n return Buffer.alloc(0);\n }\n\n /**\n * Turns every attachment into `content` bytes and drops `path`, so no transport is ever handed a\n * file or URL to read on its own. `maxBytes` is shared by all attachments of the message. An\n * attachment read from `path` keeps the file's name and, when none is given, its content type.\n */\n static async resolve(opts: {\n attachments: IMailAttachment[];\n attachmentRoot?: string;\n maxBytes?: number;\n }): Promise<IMailAttachment[]> {\n const { attachments, attachmentRoot, maxBytes } = opts;\n const resolved: IMailAttachment[] = [];\n let usedBytes = 0;\n\n // Sequential on purpose: each read spends the budget the previous ones left.\n for (const attachment of attachments) {\n const { path, href, raw, content, encoding, ...rest } = attachment;\n\n // nodemailer reads both on its own - `href` fetches a URL, `raw` can carry a `path`.\n if ((href !== undefined && href !== null) || (raw !== undefined && raw !== null)) {\n throw buildPathRefusedError({\n reason:\n 'attachment.href and attachment.raw are not supported | pass the bytes as content',\n });\n }\n\n const bytes = await MailAttachmentUtility.readContent({\n attachment: { content, encoding, path },\n attachmentRoot,\n maxBytes,\n usedBytes,\n });\n\n usedBytes += bytes.byteLength;\n\n const isReadFromPath =\n (content === undefined || content === null) && typeof path === 'string';\n const filename = rest.filename ?? (isReadFromPath ? basename(path) : undefined);\n const contentType =\n rest.contentType ??\n (isReadFromPath && filename ? ContentTypeTable.resolve({ filename }) : undefined);\n\n resolved.push({\n ...rest,\n ...(filename === undefined ? {} : { filename }),\n ...(contentType === undefined ? {} : { contentType }),\n content: bytes,\n });\n }\n\n return resolved;\n }\n\n /**\n * Releases every attachment stream of a refused send, read or not, so none keeps a descriptor\n * open. A drained stream is already closed; releasing it again is harmless.\n */\n static release(opts: { attachments?: IMailAttachment[] }): void {\n for (const attachment of opts.attachments ?? []) {\n const { content } = attachment;\n\n if (content instanceof ReadableStream) {\n if (!content.locked) {\n content.cancel().catch((error) => {\n logger.warn(\n '[MailAttachmentUtility.release] Cancelling a web stream failed | error: %s',\n error,\n );\n });\n }\n\n continue;\n }\n\n if (\n typeof content === 'object' &&\n content !== null &&\n 'destroy' in content &&\n typeof content.destroy === 'function'\n ) {\n content.destroy();\n }\n }\n }\n}\n"]}
@@ -1,6 +1,8 @@
1
1
  import { type IMailMessage } from '../common/index.js';
2
- /** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */
3
- export declare const assertMailBodyIsText: (opts: {
4
- message: IMailMessage;
5
- }) => void;
2
+ export declare class MailBodyUtility {
3
+ /** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */
4
+ static assertIsText(opts: {
5
+ message: IMailMessage;
6
+ }): void;
7
+ }
6
8
  //# sourceMappingURL=body.utility.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"body.utility.d.ts","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/body.utility.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAE1D,8HAA8H;AAC9H,eAAO,MAAM,oBAAoB,GAAI,MAAM;IAAE,OAAO,EAAE,YAAY,CAAA;CAAE,KAAG,IActE,CAAC"}
1
+ {"version":3,"file":"body.utility.d.ts","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/body.utility.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAE1D,qBAAa,eAAe;IAC1B,8HAA8H;IAC9H,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,YAAY,CAAA;KAAE,GAAG,IAAI;CAe3D"}
@@ -1,16 +1,18 @@
1
1
  import { getError } from '@venizia/ignis-helpers/core';
2
2
  import { MailErrors } from '../common/index.js';
3
- /** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */
4
- export const assertMailBodyIsText = (opts) => {
5
- const { text, html } = opts.message;
6
- const isTextValid = text === undefined || text === null || typeof text === 'string';
7
- const isHtmlValid = html === undefined || html === null || typeof html === 'string';
8
- if (isTextValid && isHtmlValid) {
9
- return;
3
+ export class MailBodyUtility {
4
+ /** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */
5
+ static assertIsText(opts) {
6
+ const { text, html } = opts.message;
7
+ const isTextValid = text === undefined || text === null || typeof text === 'string';
8
+ const isHtmlValid = html === undefined || html === null || typeof html === 'string';
9
+ if (isTextValid && isHtmlValid) {
10
+ return;
11
+ }
12
+ throw getError({
13
+ error: MailErrors.BODY_SOURCE_REFUSED,
14
+ message: 'Mail body refused | text and html must be strings; pass the content itself, not a { path } or { href } for the transport to read',
15
+ });
10
16
  }
11
- throw getError({
12
- error: MailErrors.BODY_SOURCE_REFUSED,
13
- message: 'Mail body refused | text and html must be strings; pass the content itself, not a { path } or { href } for the transport to read',
14
- });
15
- };
17
+ }
16
18
  //# sourceMappingURL=body.utility.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"body.utility.js","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/body.utility.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAqB,MAAM,WAAW,CAAC;AAE1D,8HAA8H;AAC9H,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,IAA+B,EAAQ,EAAE;IAC5E,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;IACpC,MAAM,WAAW,GAAG,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC;IACpF,MAAM,WAAW,GAAG,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC;IAEpF,IAAI,WAAW,IAAI,WAAW,EAAE,CAAC;QAC/B,OAAO;IACT,CAAC;IAED,MAAM,QAAQ,CAAC;QACb,KAAK,EAAE,UAAU,CAAC,mBAAmB;QACrC,OAAO,EACL,kIAAkI;KACrI,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { getError } from '@venizia/ignis-helpers/core';\nimport { MailErrors, type IMailMessage } from '../common';\n\n/** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */\nexport const assertMailBodyIsText = (opts: { message: IMailMessage }): void => {\n const { text, html } = opts.message;\n const isTextValid = text === undefined || text === null || typeof text === 'string';\n const isHtmlValid = html === undefined || html === null || typeof html === 'string';\n\n if (isTextValid && isHtmlValid) {\n return;\n }\n\n throw getError({\n error: MailErrors.BODY_SOURCE_REFUSED,\n message:\n 'Mail body refused | text and html must be strings; pass the content itself, not a { path } or { href } for the transport to read',\n });\n};\n"]}
1
+ {"version":3,"file":"body.utility.js","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/body.utility.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAqB,MAAM,WAAW,CAAC;AAE1D,MAAM,OAAO,eAAe;IAC1B,8HAA8H;IAC9H,MAAM,CAAC,YAAY,CAAC,IAA+B;QACjD,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QACpC,MAAM,WAAW,GAAG,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC;QACpF,MAAM,WAAW,GAAG,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC;QAEpF,IAAI,WAAW,IAAI,WAAW,EAAE,CAAC;YAC/B,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,CAAC;YACb,KAAK,EAAE,UAAU,CAAC,mBAAmB;YACrC,OAAO,EACL,kIAAkI;SACrI,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { getError } from '@venizia/ignis-helpers/core';\nimport { MailErrors, type IMailMessage } from '../common';\n\nexport class MailBodyUtility {\n /** nodemailer reads a `text`/`html` given as `{ path }` or `{ href }`, so only a string may pass; `null` counts as absent. */\n static assertIsText(opts: { message: IMailMessage }): void {\n const { text, html } = opts.message;\n const isTextValid = text === undefined || text === null || typeof text === 'string';\n const isHtmlValid = html === undefined || html === null || typeof html === 'string';\n\n if (isTextValid && isHtmlValid) {\n return;\n }\n\n throw getError({\n error: MailErrors.BODY_SOURCE_REFUSED,\n message:\n 'Mail body refused | text and html must be strings; pass the content itself, not a { path } or { href } for the transport to read',\n });\n }\n}\n"]}
@@ -1,19 +1,25 @@
1
1
  import { type IMailMessage } from '../common/index.js';
2
- /** Formats a `to`/`cc`/`from`/`replyTo` field as one RFC 5322 address list: splits a
3
- * comma-separated string or array into individual mailboxes, RFC 2047-encodes each display name,
4
- * and rejoins with `, ` - the same shape whether the caller passed a string or an array. */
5
- export declare function formatAddressHeader(value: string | string[]): string;
6
- /**
7
- * Builds a raw RFC 822 message with zero external dependencies - Amazon SES's `SendEmailCommand`
8
- * `Content.Simple` shape has no attachment field, so attachments require a raw MIME body. Written
9
- * by hand instead of depending on `nodemailer`'s `MailComposer` so the `ses` provider never needs
10
- * the `nodemailer` peer installed at all.
11
- *
12
- * Inline (`cid`) attachments nest under `multipart/related` next to the body - a mail client
13
- * only renders `cid:` references inline when they share a `multipart/related` parent with the
14
- * body; under `multipart/mixed` they show up as ordinary downloadable attachments instead. Plain
15
- * file attachments (no `cid`) stay under `multipart/mixed`, alongside the body or the
16
- * `multipart/related` group when both kinds are present.
17
- */
18
- export declare function buildRawMimeMessage(message: IMailMessage): Promise<Buffer>;
2
+ export declare class MailMimeUtility {
3
+ /** Formats a `to`/`cc`/`from`/`replyTo` field as one RFC 5322 address list: splits a
4
+ * comma-separated string or array into individual mailboxes, RFC 2047-encodes each display name,
5
+ * and rejoins with `, ` - the same shape whether the caller passed a string or an array. */
6
+ static formatAddressHeader(opts: {
7
+ value: string | string[];
8
+ }): string;
9
+ /**
10
+ * Builds a raw RFC 822 message with zero external dependencies - Amazon SES's `SendEmailCommand`
11
+ * `Content.Simple` shape has no attachment field, so attachments require a raw MIME body. Written
12
+ * by hand instead of depending on `nodemailer`'s `MailComposer` so the `ses` provider never needs
13
+ * the `nodemailer` peer installed at all.
14
+ *
15
+ * Inline (`cid`) attachments nest under `multipart/related` next to the body - a mail client
16
+ * only renders `cid:` references inline when they share a `multipart/related` parent with the
17
+ * body; under `multipart/mixed` they show up as ordinary downloadable attachments instead. Plain
18
+ * file attachments (no `cid`) stay under `multipart/mixed`, alongside the body or the
19
+ * `multipart/related` group when both kinds are present.
20
+ */
21
+ static buildRawMessage(opts: {
22
+ message: IMailMessage;
23
+ }): Promise<Buffer>;
24
+ }
19
25
  //# sourceMappingURL=mime.utility.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mime.utility.d.ts","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/mime.utility.ts"],"names":[],"mappings":"AAGA,OAAO,EAAwC,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AA4FpF;;4FAE4F;AAC5F,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,CAEpE;AAqQD;;;;;;;;;;;GAWG;AACH,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CA6FhF"}
1
+ {"version":3,"file":"mime.utility.d.ts","sourceRoot":"","sources":["../../../../../src/components/mail/utilities/mime.utility.ts"],"names":[],"mappings":"AAGA,OAAO,EAAwC,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAoWpF,qBAAa,eAAe;IAC1B;;gGAE4F;IAC5F,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;KAAE,GAAG,MAAM;IAItE;;;;;;;;;;;OAWG;WACU,eAAe,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,YAAY,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;CAmG/E"}