@dcl/content-validator 7.1.5 → 7.2.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 (154) hide show
  1. package/LICENSE +201 -0
  2. package/dist/image-metadata.d.ts +50 -0
  3. package/dist/image-metadata.d.ts.map +1 -0
  4. package/dist/image-metadata.js +407 -0
  5. package/dist/image-metadata.js.map +1 -0
  6. package/dist/index.d.ts +3 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +3 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/types.d.ts +34 -45
  11. package/dist/types.d.ts.map +1 -1
  12. package/dist/types.js.map +1 -1
  13. package/dist/utils.d.ts +13 -14
  14. package/dist/utils.d.ts.map +1 -1
  15. package/dist/utils.js +4 -5
  16. package/dist/utils.js.map +1 -1
  17. package/dist/validations/ADR45.d.ts +1 -1
  18. package/dist/validations/ADR45.d.ts.map +1 -1
  19. package/dist/validations/ADR45.js +2 -3
  20. package/dist/validations/ADR45.js.map +1 -1
  21. package/dist/validations/ADR51.d.ts +4 -3
  22. package/dist/validations/ADR51.d.ts.map +1 -1
  23. package/dist/validations/ADR51.js.map +1 -1
  24. package/dist/validations/access/common/items.d.ts +3 -3
  25. package/dist/validations/access/common/items.d.ts.map +1 -1
  26. package/dist/validations/access/common/items.js +6 -5
  27. package/dist/validations/access/common/items.js.map +1 -1
  28. package/dist/validations/access/common/outfits.d.ts +1 -1
  29. package/dist/validations/access/common/outfits.d.ts.map +1 -1
  30. package/dist/validations/access/common/outfits.js +2 -3
  31. package/dist/validations/access/common/outfits.js.map +1 -1
  32. package/dist/validations/access/common/profile.d.ts +1 -1
  33. package/dist/validations/access/common/profile.d.ts.map +1 -1
  34. package/dist/validations/access/common/profile.js +4 -5
  35. package/dist/validations/access/common/profile.js.map +1 -1
  36. package/dist/validations/access/common/stores.d.ts +1 -1
  37. package/dist/validations/access/common/stores.d.ts.map +1 -1
  38. package/dist/validations/access/common/stores.js +5 -3
  39. package/dist/validations/access/common/stores.js.map +1 -1
  40. package/dist/validations/access/index.d.ts +1 -1
  41. package/dist/validations/access/index.d.ts.map +1 -1
  42. package/dist/validations/access/index.js +1 -2
  43. package/dist/validations/access/index.js.map +1 -1
  44. package/dist/validations/access/on-chain/client.d.ts +3 -3
  45. package/dist/validations/access/on-chain/client.d.ts.map +1 -1
  46. package/dist/validations/access/on-chain/client.js +4 -4
  47. package/dist/validations/access/on-chain/client.js.map +1 -1
  48. package/dist/validations/access/on-chain/collection-asset.d.ts +1 -1
  49. package/dist/validations/access/on-chain/collection-asset.d.ts.map +1 -1
  50. package/dist/validations/access/on-chain/collection-asset.js +7 -4
  51. package/dist/validations/access/on-chain/collection-asset.js.map +1 -1
  52. package/dist/validations/access/on-chain/index.d.ts +1 -1
  53. package/dist/validations/access/on-chain/index.d.ts.map +1 -1
  54. package/dist/validations/access/on-chain/index.js +3 -4
  55. package/dist/validations/access/on-chain/index.js.map +1 -1
  56. package/dist/validations/access/on-chain/outfits.d.ts +2 -2
  57. package/dist/validations/access/on-chain/outfits.d.ts.map +1 -1
  58. package/dist/validations/access/on-chain/outfits.js +1 -2
  59. package/dist/validations/access/on-chain/outfits.js.map +1 -1
  60. package/dist/validations/access/on-chain/profiles.d.ts +2 -2
  61. package/dist/validations/access/on-chain/profiles.d.ts.map +1 -1
  62. package/dist/validations/access/on-chain/profiles.js +1 -2
  63. package/dist/validations/access/on-chain/profiles.js.map +1 -1
  64. package/dist/validations/access/on-chain/scenes.d.ts +2 -2
  65. package/dist/validations/access/on-chain/scenes.d.ts.map +1 -1
  66. package/dist/validations/access/on-chain/scenes.js +2 -3
  67. package/dist/validations/access/on-chain/scenes.js.map +1 -1
  68. package/dist/validations/access/on-chain/third-party-asset.d.ts +1 -1
  69. package/dist/validations/access/on-chain/third-party-asset.d.ts.map +1 -1
  70. package/dist/validations/access/on-chain/third-party-asset.js +2 -3
  71. package/dist/validations/access/on-chain/third-party-asset.js.map +1 -1
  72. package/dist/validations/access/subgraph/collection-asset.d.ts +5 -5
  73. package/dist/validations/access/subgraph/collection-asset.d.ts.map +1 -1
  74. package/dist/validations/access/subgraph/collection-asset.js +5 -3
  75. package/dist/validations/access/subgraph/collection-asset.js.map +1 -1
  76. package/dist/validations/access/subgraph/index.d.ts +1 -1
  77. package/dist/validations/access/subgraph/index.d.ts.map +1 -1
  78. package/dist/validations/access/subgraph/index.js +3 -4
  79. package/dist/validations/access/subgraph/index.js.map +1 -1
  80. package/dist/validations/access/subgraph/outfits.d.ts +2 -2
  81. package/dist/validations/access/subgraph/outfits.d.ts.map +1 -1
  82. package/dist/validations/access/subgraph/outfits.js +1 -2
  83. package/dist/validations/access/subgraph/outfits.js.map +1 -1
  84. package/dist/validations/access/subgraph/profiles.d.ts +2 -2
  85. package/dist/validations/access/subgraph/profiles.d.ts.map +1 -1
  86. package/dist/validations/access/subgraph/profiles.js +1 -2
  87. package/dist/validations/access/subgraph/profiles.js.map +1 -1
  88. package/dist/validations/access/subgraph/scenes.d.ts +2 -2
  89. package/dist/validations/access/subgraph/scenes.d.ts.map +1 -1
  90. package/dist/validations/access/subgraph/scenes.js +4 -5
  91. package/dist/validations/access/subgraph/scenes.js.map +1 -1
  92. package/dist/validations/access/subgraph/the-graph-client.d.ts +4 -4
  93. package/dist/validations/access/subgraph/the-graph-client.d.ts.map +1 -1
  94. package/dist/validations/access/subgraph/the-graph-client.js +4 -4
  95. package/dist/validations/access/subgraph/the-graph-client.js.map +1 -1
  96. package/dist/validations/access/subgraph/third-party-asset.d.ts +1 -1
  97. package/dist/validations/access/subgraph/third-party-asset.d.ts.map +1 -1
  98. package/dist/validations/access/subgraph/third-party-asset.js +2 -3
  99. package/dist/validations/access/subgraph/third-party-asset.js.map +1 -1
  100. package/dist/validations/content.d.ts +1 -1
  101. package/dist/validations/content.d.ts.map +1 -1
  102. package/dist/validations/content.js +4 -5
  103. package/dist/validations/content.js.map +1 -1
  104. package/dist/validations/entity-structure.d.ts +1 -1
  105. package/dist/validations/entity-structure.d.ts.map +1 -1
  106. package/dist/validations/entity-structure.js +1 -2
  107. package/dist/validations/entity-structure.js.map +1 -1
  108. package/dist/validations/index.d.ts +1 -1
  109. package/dist/validations/index.d.ts.map +1 -1
  110. package/dist/validations/index.js +2 -3
  111. package/dist/validations/index.js.map +1 -1
  112. package/dist/validations/ipfs-hashing.d.ts.map +1 -1
  113. package/dist/validations/ipfs-hashing.js +1 -1
  114. package/dist/validations/ipfs-hashing.js.map +1 -1
  115. package/dist/validations/items/emotes.d.ts +1 -1
  116. package/dist/validations/items/emotes.d.ts.map +1 -1
  117. package/dist/validations/items/emotes.js +4 -4
  118. package/dist/validations/items/emotes.js.map +1 -1
  119. package/dist/validations/items/items.d.ts +1 -1
  120. package/dist/validations/items/items.d.ts.map +1 -1
  121. package/dist/validations/items/items.js +8 -14
  122. package/dist/validations/items/items.js.map +1 -1
  123. package/dist/validations/items/wearables.d.ts +2 -2
  124. package/dist/validations/items/wearables.d.ts.map +1 -1
  125. package/dist/validations/items/wearables.js +3 -3
  126. package/dist/validations/items/wearables.js.map +1 -1
  127. package/dist/validations/metadata-schema.d.ts +1 -1
  128. package/dist/validations/metadata-schema.d.ts.map +1 -1
  129. package/dist/validations/metadata-schema.js +1 -1
  130. package/dist/validations/metadata-schema.js.map +1 -1
  131. package/dist/validations/outfits.d.ts +1 -1
  132. package/dist/validations/outfits.d.ts.map +1 -1
  133. package/dist/validations/outfits.js +7 -7
  134. package/dist/validations/outfits.js.map +1 -1
  135. package/dist/validations/profile.d.ts +1 -1
  136. package/dist/validations/profile.d.ts.map +1 -1
  137. package/dist/validations/profile.js +22 -25
  138. package/dist/validations/profile.js.map +1 -1
  139. package/dist/validations/scene.d.ts.map +1 -1
  140. package/dist/validations/scene.js +1 -1
  141. package/dist/validations/scene.js.map +1 -1
  142. package/dist/validations/signature.d.ts +1 -1
  143. package/dist/validations/signature.d.ts.map +1 -1
  144. package/dist/validations/signature.js +1 -2
  145. package/dist/validations/signature.js.map +1 -1
  146. package/dist/validations/size.d.ts +1 -1
  147. package/dist/validations/size.d.ts.map +1 -1
  148. package/dist/validations/size.js +3 -4
  149. package/dist/validations/size.js.map +1 -1
  150. package/dist/validations/validations.d.ts +2 -2
  151. package/dist/validations/validations.d.ts.map +1 -1
  152. package/dist/validations/validations.js +13 -14
  153. package/dist/validations/validations.js.map +1 -1
  154. package/package.json +35 -40
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Minimal image-metadata reader used by thumbnail validations.
3
+ *
4
+ * The library only needs to know the format ("png" vs anything else) and the
5
+ * pixel dimensions to enforce the catalyst thumbnail rules. We deliberately
6
+ * avoid pulling in `sharp` for that — a full image pipeline with native
7
+ * bindings is overkill for a header read, and ships a multi-MB libvips that
8
+ * needs an extra system dependency on every CI runner.
9
+ *
10
+ * Beyond reading dimensions, the reader applies structural sanity checks per
11
+ * format: PNG chunk-chain integrity (single IHDR, terminating IEND, no
12
+ * trailing data, spec-allowed bit-depth/color-type combination), JPEG
13
+ * EOI termination, WebP RIFF chunk-size match, and a uniform "non-zero,
14
+ * non-negative dimensions" contract on the returned width/height.
15
+ */
16
+ /**
17
+ * Image formats recognised by {@link readImageMetadata}.
18
+ *
19
+ * The reader detects each format by its magic bytes and validates the
20
+ * minimum structural invariants needed to extract dimensions safely.
21
+ * @public
22
+ */
23
+ export type ImageFormat = 'png' | 'jpeg' | 'webp' | 'gif' | 'bmp';
24
+ /**
25
+ * Header-derived dimensions and format of an image.
26
+ *
27
+ * Width and height are guaranteed to be positive integers — the reader
28
+ * throws rather than returning zero, negative, or non-integer values.
29
+ * @public
30
+ */
31
+ export interface ImageMetadata {
32
+ format: ImageFormat;
33
+ width: number;
34
+ height: number;
35
+ }
36
+ /**
37
+ * Reads the format and pixel dimensions of a recognised image buffer.
38
+ *
39
+ * Recognises PNG, JPEG, WebP, GIF, and BMP by their magic bytes. The library
40
+ * only enforces a PNG-format check on top of this, but recognising the other
41
+ * common formats means a user uploading a JPEG/WebP/GIF/BMP thumbnail gets
42
+ * the precise "Invalid format" error instead of a generic parse failure.
43
+ *
44
+ * Throws if the buffer is not a recognised, structurally-valid image with
45
+ * non-zero, non-negative dimensions. Callers are expected to wrap calls in
46
+ * try/catch to translate the throw into a "couldn't parse" validation error.
47
+ * @public
48
+ */
49
+ export declare function readImageMetadata(input: Uint8Array): ImageMetadata;
50
+ //# sourceMappingURL=image-metadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-metadata.d.ts","sourceRoot":"","sources":["../src/image-metadata.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA+CH;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAA;AAEjE;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,WAAW,CAAA;IACnB,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf;AAYD;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,CAmBlE"}
@@ -0,0 +1,407 @@
1
+ "use strict";
2
+ /**
3
+ * Minimal image-metadata reader used by thumbnail validations.
4
+ *
5
+ * The library only needs to know the format ("png" vs anything else) and the
6
+ * pixel dimensions to enforce the catalyst thumbnail rules. We deliberately
7
+ * avoid pulling in `sharp` for that — a full image pipeline with native
8
+ * bindings is overkill for a header read, and ships a multi-MB libvips that
9
+ * needs an extra system dependency on every CI runner.
10
+ *
11
+ * Beyond reading dimensions, the reader applies structural sanity checks per
12
+ * format: PNG chunk-chain integrity (single IHDR, terminating IEND, no
13
+ * trailing data, spec-allowed bit-depth/color-type combination), JPEG
14
+ * EOI termination, WebP RIFF chunk-size match, and a uniform "non-zero,
15
+ * non-negative dimensions" contract on the returned width/height.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.readImageMetadata = readImageMetadata;
19
+ const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
20
+ const PNG_IHDR_BYTES = Buffer.from('IHDR', 'ascii');
21
+ const PNG_IEND_BYTES = Buffer.from('IEND', 'ascii');
22
+ const PNG_IHDR_CHUNK_LENGTH = 13;
23
+ const PNG_FIRST_CHUNK_END = 33; // 8 (signature) + 4 (length) + 4 (type) + 13 (IHDR data) + 4 (CRC)
24
+ const PNG_CHUNK_OVERHEAD = 12; // length(4) + type(4) + crc(4)
25
+ // PNG spec (ISO/IEC 15948 §5.3): chunk lengths shall not exceed 2^31-1 bytes
26
+ // even though the wire field is 4 bytes. Anything above that is a parser
27
+ // differential — some readers reject, some accept the high bit silently.
28
+ const PNG_MAX_CHUNK_LENGTH = 0x7fffffff;
29
+ // Spec-permitted bit-depth + color-type combinations (PNG, ISO/IEC 15948, §11.2.2).
30
+ const PNG_VALID_BIT_DEPTHS_BY_COLOR_TYPE = {
31
+ 0: [1, 2, 4, 8, 16], // Grayscale
32
+ 2: [8, 16], // Truecolor (RGB)
33
+ 3: [1, 2, 4, 8], // Indexed-colour (Palette)
34
+ 4: [8, 16], // Greyscale + Alpha
35
+ 6: [8, 16] // Truecolor + Alpha (RGBA)
36
+ };
37
+ const WEBP_RIFF_BYTES = Buffer.from('RIFF', 'ascii');
38
+ const WEBP_WEBP_BYTES = Buffer.from('WEBP', 'ascii');
39
+ const WEBP_VP8_BYTES = Buffer.from('VP8 ', 'ascii');
40
+ const WEBP_VP8L_BYTES = Buffer.from('VP8L', 'ascii');
41
+ const WEBP_VP8X_BYTES = Buffer.from('VP8X', 'ascii');
42
+ const GIF87A_BYTES = Buffer.from('GIF87a', 'ascii');
43
+ const GIF89A_BYTES = Buffer.from('GIF89a', 'ascii');
44
+ const JPEG_EOI_BYTE_1 = 0xff;
45
+ const JPEG_EOI_BYTE_2 = 0xd9;
46
+ /**
47
+ * Compare a slice of `buffer` to `expected` using byte-exact equality.
48
+ *
49
+ * Buffer.toString('ascii') masks the high bit of each byte before decoding,
50
+ * so a chunk type stored as e.g. [0x49, 0x48, 0xC4, 0x52] would otherwise
51
+ * decode as "IHDR" — a parser differential vs. real PNG/WebP/GIF readers.
52
+ * This helper is used everywhere a magic byte sequence or chunk identifier
53
+ * needs to be compared.
54
+ */
55
+ function bufferEqualsAt(buffer, offset, expected) {
56
+ if (offset < 0 || offset + expected.length > buffer.length)
57
+ return false;
58
+ return buffer.subarray(offset, offset + expected.length).equals(expected);
59
+ }
60
+ // Display names used in user-facing error messages, matching the casing
61
+ // produced by the format-specific validators (e.g. "Malformed PNG", "Malformed WebP").
62
+ const FORMAT_DISPLAY_NAME = {
63
+ png: 'PNG',
64
+ jpeg: 'JPEG',
65
+ webp: 'WebP',
66
+ gif: 'GIF',
67
+ bmp: 'BMP'
68
+ };
69
+ /**
70
+ * Reads the format and pixel dimensions of a recognised image buffer.
71
+ *
72
+ * Recognises PNG, JPEG, WebP, GIF, and BMP by their magic bytes. The library
73
+ * only enforces a PNG-format check on top of this, but recognising the other
74
+ * common formats means a user uploading a JPEG/WebP/GIF/BMP thumbnail gets
75
+ * the precise "Invalid format" error instead of a generic parse failure.
76
+ *
77
+ * Throws if the buffer is not a recognised, structurally-valid image with
78
+ * non-zero, non-negative dimensions. Callers are expected to wrap calls in
79
+ * try/catch to translate the throw into a "couldn't parse" validation error.
80
+ * @public
81
+ */
82
+ function readImageMetadata(input) {
83
+ // Reject SharedArrayBuffer-backed inputs: a concurrent writer could race the
84
+ // parser between length checks and reads (TOCTOU). The reader operates on
85
+ // bytes only; callers who need shared memory must copy into a regular
86
+ // ArrayBuffer first.
87
+ if (typeof SharedArrayBuffer !== 'undefined' && input.buffer instanceof SharedArrayBuffer) {
88
+ throw new Error('Image input must not be backed by a SharedArrayBuffer');
89
+ }
90
+ // Wrap as Buffer to access readUInt*BE / equals / toString without copying.
91
+ const buffer = Buffer.isBuffer(input) ? input : Buffer.from(input.buffer, input.byteOffset, input.byteLength);
92
+ let metadata;
93
+ if (isPng(buffer))
94
+ metadata = readPngMetadata(buffer);
95
+ else if (isJpeg(buffer))
96
+ metadata = readJpegMetadata(buffer);
97
+ else if (isWebp(buffer))
98
+ metadata = readWebpMetadata(buffer);
99
+ else if (isGif(buffer))
100
+ metadata = readGifMetadata(buffer);
101
+ else if (isBmp(buffer))
102
+ metadata = readBmpMetadata(buffer);
103
+ else
104
+ throw new Error('Unsupported image format');
105
+ assertPositiveDimensions(metadata);
106
+ return metadata;
107
+ }
108
+ function assertPositiveDimensions(metadata) {
109
+ const name = FORMAT_DISPLAY_NAME[metadata.format];
110
+ if (!Number.isInteger(metadata.width) || metadata.width <= 0) {
111
+ throw new Error(`Malformed ${name}: non-positive width ${metadata.width}`);
112
+ }
113
+ if (!Number.isInteger(metadata.height) || metadata.height <= 0) {
114
+ throw new Error(`Malformed ${name}: non-positive height ${metadata.height}`);
115
+ }
116
+ }
117
+ function isPng(buffer) {
118
+ // Require the full IHDR chunk (signature + 25-byte IHDR) to be present so
119
+ // readPngMetadata can safely read every IHDR field, including bit depth at
120
+ // byte 24 and color type at byte 25. A shorter buffer with the PNG signature
121
+ // is treated as unrecognised rather than as a truncated PNG.
122
+ return buffer.length >= PNG_FIRST_CHUNK_END && buffer.subarray(0, 8).equals(PNG_SIGNATURE);
123
+ }
124
+ function readPngMetadata(buffer) {
125
+ if (buffer.readUInt32BE(8) !== PNG_IHDR_CHUNK_LENGTH) {
126
+ throw new Error('Malformed PNG: IHDR chunk length is not 13');
127
+ }
128
+ if (!bufferEqualsAt(buffer, 12, PNG_IHDR_BYTES)) {
129
+ throw new Error('Malformed PNG: missing IHDR chunk');
130
+ }
131
+ const width = buffer.readUInt32BE(16);
132
+ const height = buffer.readUInt32BE(20);
133
+ validatePngBitDepthAndColorType(buffer.readUInt8(24), buffer.readUInt8(25));
134
+ validatePngIhdrMethods(buffer.readUInt8(26), buffer.readUInt8(27), buffer.readUInt8(28));
135
+ validatePngChunkChain(buffer);
136
+ return { format: 'png', width, height };
137
+ }
138
+ function validatePngBitDepthAndColorType(bitDepth, colorType) {
139
+ const allowed = PNG_VALID_BIT_DEPTHS_BY_COLOR_TYPE[colorType];
140
+ if (!allowed) {
141
+ throw new Error(`Malformed PNG: invalid color type ${colorType}`);
142
+ }
143
+ if (!allowed.includes(bitDepth)) {
144
+ throw new Error(`Malformed PNG: invalid bit depth ${bitDepth} for color type ${colorType}`);
145
+ }
146
+ }
147
+ function validatePngIhdrMethods(compression, filter, interlace) {
148
+ // Per PNG spec (ISO/IEC 15948 §11.2.2), only deflate (0) compression and
149
+ // filter method 0 are defined. Interlace must be 0 (none) or 1 (Adam7).
150
+ if (compression !== 0) {
151
+ throw new Error(`Malformed PNG: invalid compression method ${compression}`);
152
+ }
153
+ if (filter !== 0) {
154
+ throw new Error(`Malformed PNG: invalid filter method ${filter}`);
155
+ }
156
+ if (interlace !== 0 && interlace !== 1) {
157
+ throw new Error(`Malformed PNG: invalid interlace method ${interlace}`);
158
+ }
159
+ }
160
+ function validatePngChunkChain(buffer) {
161
+ // First chunk after the signature is IHDR (already validated by the caller).
162
+ // Walk subsequent chunks, requiring exactly one IEND that terminates the
163
+ // buffer. Reject duplicate IHDR chunks.
164
+ let i = PNG_FIRST_CHUNK_END;
165
+ while (i + PNG_CHUNK_OVERHEAD <= buffer.length) {
166
+ const chunkDataLength = buffer.readUInt32BE(i);
167
+ if (chunkDataLength > PNG_MAX_CHUNK_LENGTH) {
168
+ throw new Error('Malformed PNG: chunk length exceeds 2^31-1');
169
+ }
170
+ if (bufferEqualsAt(buffer, i + 4, PNG_IHDR_BYTES)) {
171
+ throw new Error('Malformed PNG: duplicate IHDR chunk');
172
+ }
173
+ if (bufferEqualsAt(buffer, i + 4, PNG_IEND_BYTES)) {
174
+ // IEND has zero-length data per ISO/IEC 15948. Validate both the
175
+ // declared length AND the chunk footprint: a file that declares a
176
+ // non-zero IEND length but only contains the 12-byte footprint would
177
+ // otherwise pass us while strict readers reject (they expect more
178
+ // bytes for IEND data + CRC).
179
+ if (chunkDataLength !== 0) {
180
+ throw new Error('Malformed PNG: IEND chunk must have zero length');
181
+ }
182
+ if (i + PNG_CHUNK_OVERHEAD !== buffer.length) {
183
+ throw new Error('Malformed PNG: data after IEND chunk');
184
+ }
185
+ return;
186
+ }
187
+ // Advance past this chunk: length(4) + type(4) + data(N) + crc(4).
188
+ i += PNG_CHUNK_OVERHEAD + chunkDataLength;
189
+ }
190
+ throw new Error('Malformed PNG: missing IEND chunk');
191
+ }
192
+ function isJpeg(buffer) {
193
+ return buffer.length >= 4 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff;
194
+ }
195
+ function readJpegMetadata(buffer) {
196
+ // The buffer must be terminated by an EOI marker (FF D9). This rejects
197
+ // truncated JPEGs and trailing-data polyglots.
198
+ if (buffer.length < 4 ||
199
+ buffer[buffer.length - 2] !== JPEG_EOI_BYTE_1 ||
200
+ buffer[buffer.length - 1] !== JPEG_EOI_BYTE_2) {
201
+ throw new Error('Malformed JPEG: missing EOI marker');
202
+ }
203
+ // JPEG is a chain of 0xFF-prefixed segments. Dimensions live in any of the
204
+ // SOFn (Start Of Frame) segments — markers 0xC0..0xCF except 0xC4 (DHT),
205
+ // 0xC8 (JPG, reserved), and 0xCC (DAC). The SOFn payload reads up to
206
+ // buffer[i + 8], so we need `i + 8 < buffer.length`.
207
+ let i = 2;
208
+ while (i < buffer.length - 8) {
209
+ if (buffer[i] !== 0xff) {
210
+ i++;
211
+ continue;
212
+ }
213
+ const marker = buffer[i + 1];
214
+ // Skip fill bytes (0xFF padding before a real marker).
215
+ if (marker === 0x00 || marker === 0xff) {
216
+ i++;
217
+ continue;
218
+ }
219
+ // Standalone markers have no length field. Advance past the marker only
220
+ // so we don't read the next two bytes as a bogus segment length and
221
+ // desynchronise the parser. TEM=0x01, RST0..7=0xD0..0xD7, SOI=0xD8,
222
+ // EOI=0xD9.
223
+ if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd9)) {
224
+ i += 2;
225
+ continue;
226
+ }
227
+ // SOS (Start Of Scan) marks the boundary between marker stream and
228
+ // entropy-coded image data. SOFn must appear before SOS in any valid
229
+ // JPEG; if we hit SOS without one, stop scanning rather than walk
230
+ // entropy data hunting for a 0xFF marker (which would be a false
231
+ // positive on improperly stuffed bytes).
232
+ if (marker === 0xda) {
233
+ break;
234
+ }
235
+ const isStartOfFrame = marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc;
236
+ if (isStartOfFrame) {
237
+ // SOFn payload: [length:2][precision:1][height:2][width:2][...]
238
+ return {
239
+ format: 'jpeg',
240
+ height: buffer.readUInt16BE(i + 5),
241
+ width: buffer.readUInt16BE(i + 7)
242
+ };
243
+ }
244
+ // Loop invariant `i < buffer.length - 8` guarantees i+3 is in bounds,
245
+ // so the segment-length read below is always safe.
246
+ const segmentLength = buffer.readUInt16BE(i + 2);
247
+ if (segmentLength < 2)
248
+ break;
249
+ i += 2 + segmentLength;
250
+ }
251
+ throw new Error('Malformed JPEG: no SOFn marker found');
252
+ }
253
+ function isWebp(buffer) {
254
+ // RIFF[4 bytes file size]WEBP[4 bytes variant identifier]
255
+ return buffer.length >= 16 && bufferEqualsAt(buffer, 0, WEBP_RIFF_BYTES) && bufferEqualsAt(buffer, 8, WEBP_WEBP_BYTES);
256
+ }
257
+ function readWebpMetadata(buffer) {
258
+ // The RIFF chunk size at bytes 4-7 covers everything after the size field
259
+ // itself, i.e. exactly `buffer.length - 8` for a well-formed file. A
260
+ // mismatch indicates truncation or trailing data injection.
261
+ const declaredRiffSize = buffer.readUInt32LE(4);
262
+ if (declaredRiffSize !== buffer.length - 8) {
263
+ throw new Error('Malformed WebP: RIFF chunk size does not match buffer length');
264
+ }
265
+ // After "WEBP" comes a sub-chunk identifier ("VP8 ", "VP8L", or "VP8X")
266
+ // and dimensions are encoded slightly differently per variant.
267
+ if (bufferEqualsAt(buffer, 12, WEBP_VP8_BYTES)) {
268
+ // Lossy: width/height are at bytes 26-29 as 14-bit little-endian values,
269
+ // preceded by the mandatory 3-byte VP8 keyframe sync code at bytes 23-25.
270
+ if (buffer.length < 30) {
271
+ throw new Error('Malformed WebP: VP8 chunk truncated');
272
+ }
273
+ assertWebpSimpleSubChunkSize(buffer, 'VP8');
274
+ if (buffer[23] !== 0x9d || buffer[24] !== 0x01 || buffer[25] !== 0x2a) {
275
+ throw new Error('Malformed WebP: invalid VP8 keyframe sync code');
276
+ }
277
+ return {
278
+ format: 'webp',
279
+ width: buffer.readUInt16LE(26) & 0x3fff,
280
+ height: buffer.readUInt16LE(28) & 0x3fff
281
+ };
282
+ }
283
+ if (bufferEqualsAt(buffer, 12, WEBP_VP8L_BYTES)) {
284
+ // Lossless: width-1 and height-1 are packed into bytes 21-24.
285
+ if (buffer.length < 25) {
286
+ throw new Error('Malformed WebP: VP8L chunk truncated');
287
+ }
288
+ assertWebpSimpleSubChunkSize(buffer, 'VP8L');
289
+ // VP8L spec mandates a 1-byte signature (0x2F) immediately after the
290
+ // 8-byte chunk header, before the packed dimensions.
291
+ if (buffer[20] !== 0x2f) {
292
+ throw new Error('Malformed WebP: invalid VP8L signature byte');
293
+ }
294
+ const b0 = buffer[21];
295
+ const b1 = buffer[22];
296
+ const b2 = buffer[23];
297
+ const b3 = buffer[24];
298
+ return {
299
+ format: 'webp',
300
+ width: 1 + ((b0 | (b1 << 8)) & 0x3fff),
301
+ height: 1 + (((b1 >> 6) | (b2 << 2) | (b3 << 10)) & 0x3fff)
302
+ };
303
+ }
304
+ if (bufferEqualsAt(buffer, 12, WEBP_VP8X_BYTES)) {
305
+ // Extended: width-1 and height-1 as 24-bit little-endian at bytes 24-29.
306
+ if (buffer.length < 30) {
307
+ throw new Error('Malformed WebP: VP8X chunk truncated');
308
+ }
309
+ // VP8X canvas info is always exactly 10 bytes; trailing chunks (ICCP,
310
+ // ANIM, …) are accounted for by the outer RIFF size only.
311
+ if (buffer.readUInt32LE(16) !== 10) {
312
+ throw new Error('Malformed WebP: VP8X chunk size must be 10');
313
+ }
314
+ return {
315
+ format: 'webp',
316
+ width: 1 + buffer.readUIntLE(24, 3),
317
+ height: 1 + buffer.readUIntLE(27, 3)
318
+ };
319
+ }
320
+ // Read the variant bytes for the error message via latin1 (preserves all
321
+ // byte values 0x00-0xFF) and then sanitise non-printable characters so we
322
+ // can't smuggle log lines through high-bit or control bytes.
323
+ throw new Error(`Malformed WebP: unknown variant '${sanitiseForLog(buffer.toString('latin1', 12, 16))}'`);
324
+ }
325
+ /**
326
+ * For Simple File Format WebP (VP8 / VP8L), the inner chunk's declared payload
327
+ * size at offset 16 must — together with the 8-byte chunk header and an
328
+ * optional 1-byte RIFF pad for odd-length payloads — account for everything
329
+ * after the 12-byte RIFF/WEBP preamble. Catches parser-differential attacks
330
+ * where a tampered chunk size is silently accepted by readers that work off
331
+ * fixed offsets.
332
+ */
333
+ function assertWebpSimpleSubChunkSize(buffer, label) {
334
+ const declared = buffer.readUInt32LE(16);
335
+ const expectedPayload = buffer.length - 20;
336
+ const expectedPayloadWithoutPad = buffer.length - 21;
337
+ if (declared !== expectedPayload && declared !== expectedPayloadWithoutPad) {
338
+ throw new Error(`Malformed WebP: ${label} chunk size does not match buffer length`);
339
+ }
340
+ }
341
+ /**
342
+ * Replace control characters and non-printable bytes in a user-controlled
343
+ * string before interpolating it into an error message. PNG/JPEG/WebP type
344
+ * fields are 7-bit ASCII per spec, but a malicious buffer can put any
345
+ * 0x00-0x7F byte there — including newline / carriage return / NUL — which
346
+ * could otherwise smuggle log lines through downstream consumers.
347
+ */
348
+ function sanitiseForLog(value) {
349
+ return value.replace(/[^\x20-\x7e]/g, '?');
350
+ }
351
+ function isGif(buffer) {
352
+ // Minimum legal GIF89a is signature(6) + LSD(7) + trailer(1) = 14 bytes.
353
+ return buffer.length >= 14 && (bufferEqualsAt(buffer, 0, GIF87A_BYTES) || bufferEqualsAt(buffer, 0, GIF89A_BYTES));
354
+ }
355
+ function readGifMetadata(buffer) {
356
+ // Per the GIF89a spec the file must end with a 0x3B trailer byte. Rejecting
357
+ // missing trailers brings GIF in line with the PNG (IEND) and JPEG (EOI)
358
+ // termination checks and catches truncated / trailing-data polyglots.
359
+ if (buffer[buffer.length - 1] !== 0x3b) {
360
+ throw new Error('Malformed GIF: missing trailer byte');
361
+ }
362
+ // Logical screen descriptor: width at bytes 6-7, height at 8-9, little-endian.
363
+ return {
364
+ format: 'gif',
365
+ width: buffer.readUInt16LE(6),
366
+ height: buffer.readUInt16LE(8)
367
+ };
368
+ }
369
+ const BMP_BITMAPCOREHEADER_SIZE = 12;
370
+ function isBmp(buffer) {
371
+ // 14-byte BITMAPFILEHEADER + at least the 4-byte DIB header size field, plus
372
+ // enough room to read the smallest DIB header's width/height (BITMAPCOREHEADER
373
+ // ends at byte 21; everything else extends to 25).
374
+ return buffer.length >= 22 && buffer[0] === 0x42 && buffer[1] === 0x4d;
375
+ }
376
+ function readBmpMetadata(buffer) {
377
+ // BITMAPFILEHEADER bytes 2-5 store the total file size in bytes, including
378
+ // the headers. A mismatch indicates truncation or trailing data injection.
379
+ const declaredFileSize = buffer.readUInt32LE(2);
380
+ if (declaredFileSize !== buffer.length) {
381
+ throw new Error('Malformed BMP: file size header does not match buffer length');
382
+ }
383
+ const dibHeaderSize = buffer.readUInt32LE(14);
384
+ if (dibHeaderSize === BMP_BITMAPCOREHEADER_SIZE) {
385
+ // BITMAPCOREHEADER (OS/2 v1): 16-bit width and height at offsets 18-19 and
386
+ // 20-21. Negative heights are not defined for this header.
387
+ return {
388
+ format: 'bmp',
389
+ width: buffer.readUInt16LE(18),
390
+ height: buffer.readUInt16LE(20)
391
+ };
392
+ }
393
+ // BITMAPINFOHEADER and its extended variants (40, 52, 56, 108, 124 bytes).
394
+ // Width is signed int32 LE at 18-21, height is signed int32 LE at 22-25.
395
+ // A negative height encodes a top-down DIB; absolute value is the pixel
396
+ // height. A negative width is illegal per spec — assertPositiveDimensions
397
+ // enforces that on the way out.
398
+ if (buffer.length < 26) {
399
+ throw new Error('Malformed BMP: BITMAPINFOHEADER truncated');
400
+ }
401
+ return {
402
+ format: 'bmp',
403
+ width: buffer.readInt32LE(18),
404
+ height: Math.abs(buffer.readInt32LE(22))
405
+ };
406
+ }
407
+ //# sourceMappingURL=image-metadata.js.map