@axiumine/koa-utils 9.1.1 → 9.3.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 (90) hide show
  1. package/README.md +6 -6
  2. package/dist/email/ISocketLabsClient.d.mts +4 -3
  3. package/dist/email/ISocketLabsMessage.d.mts +1 -1
  4. package/dist/email/ISocketLabsSendResult.d.mts +3 -3
  5. package/dist/email/ISocketLabsSenderOptions.d.mts +9 -8
  6. package/dist/email/SocketLabsLib.d.mts +2 -15
  7. package/dist/email/SocketLabsLib.mjs +4 -19
  8. package/dist/email/createSocketLabsSender.d.mts +9 -8
  9. package/dist/email/createSocketLabsSender.mjs +16 -22
  10. package/dist/files/GLB_POLICY_DEFAULTS.d.mts +49 -0
  11. package/dist/files/GLB_POLICY_DEFAULTS.mjs +45 -0
  12. package/dist/files/GLB_REJECT_CODES.d.mts +29 -0
  13. package/dist/files/GLB_REJECT_CODES.mjs +51 -0
  14. package/dist/files/IGlbContainer.d.mts +12 -0
  15. package/dist/files/IGlbContainer.mjs +2 -0
  16. package/dist/files/IGlbSanitiseResult.d.mts +56 -0
  17. package/dist/files/IGlbSanitiseResult.mjs +2 -0
  18. package/dist/files/checkGlbContainer.d.mts +11 -0
  19. package/dist/files/checkGlbContainer.mjs +68 -0
  20. package/dist/files/sanitiseGlb.d.mts +22 -0
  21. package/dist/files/sanitiseGlb.mjs +66 -0
  22. package/dist/files/sanitiseGlbFile.d.mts +16 -0
  23. package/dist/files/sanitiseGlbFile.mjs +36 -0
  24. package/dist/files/validateGlbExtension.d.mts +1 -0
  25. package/dist/files/validateGlbExtension.mjs +9 -0
  26. package/dist/files/validateMimeTypeGlb.d.mts +5 -0
  27. package/dist/files/validateMimeTypeGlb.mjs +9 -0
  28. package/dist/private/email/ISocketLabsBasicMessage.d.mts +22 -0
  29. package/dist/private/email/ISocketLabsBasicMessage.mjs +2 -0
  30. package/dist/private/email/ISocketLabsOutcome.d.mts +10 -0
  31. package/dist/private/email/ISocketLabsOutcome.mjs +2 -0
  32. package/dist/private/email/SocketLabsSendError.d.mts +17 -0
  33. package/dist/private/email/SocketLabsSendError.mjs +21 -0
  34. package/dist/private/email/socketLabsClient.d.mts +26 -0
  35. package/dist/private/email/socketLabsClient.mjs +80 -0
  36. package/dist/private/email/socketLabsMessage.d.mts +9 -0
  37. package/dist/private/email/socketLabsMessage.mjs +36 -0
  38. package/dist/private/email/socketLabsRequest.d.mts +19 -0
  39. package/dist/private/email/socketLabsRequest.mjs +86 -0
  40. package/dist/private/email/socketLabsResponse.d.mts +10 -0
  41. package/dist/private/email/socketLabsResponse.mjs +49 -0
  42. package/dist/private/email/withDefined.d.mts +4 -0
  43. package/dist/private/email/withDefined.mjs +5 -0
  44. package/dist/private/glb/IImageSize.d.mts +5 -0
  45. package/dist/private/glb/IImageSize.mjs +2 -0
  46. package/dist/private/glb/checkGlbCounts.d.mts +15 -0
  47. package/dist/private/glb/checkGlbCounts.mjs +53 -0
  48. package/dist/private/glb/checkGlbExtensions.d.mts +7 -0
  49. package/dist/private/glb/checkGlbExtensions.mjs +26 -0
  50. package/dist/private/glb/checkGlbGeometry.d.mts +9 -0
  51. package/dist/private/glb/checkGlbGeometry.mjs +56 -0
  52. package/dist/private/glb/checkGlbImages.d.mts +10 -0
  53. package/dist/private/glb/checkGlbImages.mjs +60 -0
  54. package/dist/private/glb/checkGlbUris.d.mts +7 -0
  55. package/dist/private/glb/checkGlbUris.mjs +25 -0
  56. package/dist/private/glb/glbExtensions.d.mts +7 -0
  57. package/dist/private/glb/glbExtensions.mjs +13 -0
  58. package/dist/private/glb/glbIo.d.mts +7 -0
  59. package/dist/private/glb/glbIo.mjs +16 -0
  60. package/dist/private/glb/glbReason.d.mts +7 -0
  61. package/dist/private/glb/glbReason.mjs +14 -0
  62. package/dist/private/glb/glbStats.d.mts +5 -0
  63. package/dist/private/glb/glbStats.mjs +24 -0
  64. package/dist/private/glb/inspectGlbJson.d.mts +8 -0
  65. package/dist/private/glb/inspectGlbJson.mjs +21 -0
  66. package/dist/private/glb/jsonAccess.d.mts +17 -0
  67. package/dist/private/glb/jsonAccess.mjs +24 -0
  68. package/dist/private/glb/parseGlbJson.d.mts +11 -0
  69. package/dist/private/glb/parseGlbJson.mjs +14 -0
  70. package/dist/private/glb/reEncodeImage.d.mts +13 -0
  71. package/dist/private/glb/reEncodeImage.mjs +24 -0
  72. package/dist/private/glb/readImageHeader.d.mts +25 -0
  73. package/dist/private/glb/readImageHeader.mjs +68 -0
  74. package/dist/private/glb/readJpegSize.d.mts +3 -0
  75. package/dist/private/glb/readJpegSize.mjs +27 -0
  76. package/dist/private/glb/readKtx2Size.d.mts +3 -0
  77. package/dist/private/glb/readKtx2Size.mjs +23 -0
  78. package/dist/private/glb/reencodeTextures.d.mts +9 -0
  79. package/dist/private/glb/reencodeTextures.mjs +34 -0
  80. package/dist/private/glb/resolveGlbPolicy.d.mts +6 -0
  81. package/dist/private/glb/resolveGlbPolicy.mjs +48 -0
  82. package/dist/private/glb/rewriteGlb.d.mts +16 -0
  83. package/dist/private/glb/rewriteGlb.mjs +49 -0
  84. package/dist/private/glb/stripExtras.d.mts +8 -0
  85. package/dist/private/glb/stripExtras.mjs +16 -0
  86. package/dist/private/glb/validateKhronos.d.mts +12 -0
  87. package/dist/private/glb/validateKhronos.mjs +32 -0
  88. package/dist/private/glb/verifyGlbOutput.d.mts +8 -0
  89. package/dist/private/glb/verifyGlbOutput.mjs +20 -0
  90. package/package.json +69 -16
@@ -0,0 +1,68 @@
1
+ import { readJpeg } from './readJpegSize.mjs';
2
+ import { readKtx2 } from './readKtx2Size.mjs';
3
+ const KTX2_IDENTIFIER = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a];
4
+ const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
5
+ const RIFF = [0x52, 0x49, 0x46, 0x46];
6
+ const WEBP = [0x57, 0x45, 0x42, 0x50];
7
+ const JPEG_START = [0xff, 0xd8, 0xff];
8
+ const startsWith = (bytes, prefix, at = 0) => prefix.every((byte, index) => bytes[at + index] === byte);
9
+ const MAX_SIDE = 0x7f_ff_ff_ff;
10
+ function readPng(view) {
11
+ // the first chunk must be IHDR, 13 bytes: this also turns away the "fried" iPhone variant that starts with CgBI
12
+ if (view.getUint32(8) !== 13 || view.getUint32(12) !== 0x49_48_44_52) {
13
+ return null;
14
+ }
15
+ return { width: view.getUint32(16), height: view.getUint32(20) };
16
+ }
17
+ function readWebp(view) {
18
+ const chunk = view.getUint32(12);
19
+ if (chunk === 0x56_50_38_20) {
20
+ // 'VP8 ': lossy, 14 bit sizes behind the 3 byte start code
21
+ return view.getUint32(23) >>> 8 === 0x9d_01_2a
22
+ ? { width: view.getUint16(26, true) & 0x3f_ff, height: view.getUint16(28, true) & 0x3f_ff }
23
+ : null;
24
+ }
25
+ if (chunk === 0x56_50_38_4c) {
26
+ // 'VP8L': lossless, two 14 bit sizes minus one in one 28 bit field
27
+ const bits = view.getUint32(21, true);
28
+ return view.getUint8(20) === 0x2f ? { width: (bits & 0x3f_ff) + 1, height: ((bits >>> 14) & 0x3f_ff) + 1 } : null;
29
+ }
30
+ if (chunk === 0x56_50_38_58) {
31
+ // 'VP8X': extended, two 24 bit sizes minus one
32
+ return {
33
+ width: (view.getUint16(24, true) | (view.getUint8(26) << 16)) + 1,
34
+ height: (view.getUint16(27, true) | (view.getUint8(29) << 16)) + 1
35
+ };
36
+ }
37
+ return null;
38
+ }
39
+ const FORMATS = [
40
+ { name: 'png', mime: 'image/png', matches: (bytes) => startsWith(bytes, PNG_SIGNATURE), read: readPng },
41
+ { name: 'jpeg', mime: 'image/jpeg', matches: (bytes) => startsWith(bytes, JPEG_START), read: readJpeg },
42
+ {
43
+ name: 'webp',
44
+ mime: 'image/webp',
45
+ matches: (bytes) => startsWith(bytes, RIFF) && startsWith(bytes, WEBP, 8),
46
+ read: readWebp
47
+ },
48
+ { name: 'ktx2', mime: 'image/ktx2', matches: (bytes) => startsWith(bytes, KTX2_IDENTIFIER), read: readKtx2 }
49
+ ];
50
+ /** The format of the bytes, by magic number. The `mimeType` the file claims is not used. */
51
+ export function detectImageFormat(bytes) {
52
+ return FORMATS.find((format) => format.matches(bytes)) ?? null;
53
+ }
54
+ /**
55
+ * Width and height from the header only, nothing is decoded. `null` when the header is cut short, malformed, or
56
+ * reports a side of `0`. Nothing here throws on hostile bytes: reads past the end become `null`.
57
+ */
58
+ export function readImageSize(format, bytes) {
59
+ let size;
60
+ try {
61
+ size = format.read(new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength));
62
+ }
63
+ catch {
64
+ return null;
65
+ }
66
+ return size !== null && size.width > 0 && size.height > 0 && size.width <= MAX_SIDE && size.height <= MAX_SIDE ? size : null;
67
+ }
68
+ //# sourceMappingURL=readImageHeader.mjs.map
@@ -0,0 +1,3 @@
1
+ import type { IImageSize } from './IImageSize.mjs';
2
+ /** Width and height from the first start-of-frame segment. Throws when the file is cut short. */
3
+ export declare function readJpeg(view: DataView): IImageSize | null;
@@ -0,0 +1,27 @@
1
+ const isStartOfFrame = (marker) => marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc;
2
+ const hasNoLength = (marker) => marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8);
3
+ /** Width and height from the first start-of-frame segment. Throws when the file is cut short. */
4
+ export function readJpeg(view) {
5
+ let offset = 2;
6
+ while (offset < view.byteLength) {
7
+ const marker = view.getUint8(offset + 1);
8
+ if (view.getUint8(offset) !== 0xff || marker === 0xd9) {
9
+ return null;
10
+ }
11
+ if (marker === 0xff) {
12
+ offset += 1;
13
+ }
14
+ else if (hasNoLength(marker)) {
15
+ offset += 2;
16
+ }
17
+ else if (isStartOfFrame(marker)) {
18
+ return { width: view.getUint16(offset + 7), height: view.getUint16(offset + 5) };
19
+ }
20
+ else {
21
+ // a length under 2 lands inside the length field itself, which is never 0xff, so the next pass ends the walk
22
+ offset += 2 + view.getUint16(offset + 2);
23
+ }
24
+ }
25
+ return null;
26
+ }
27
+ //# sourceMappingURL=readJpegSize.mjs.map
@@ -0,0 +1,3 @@
1
+ import type { IImageSize } from './IImageSize.mjs';
2
+ /** Width and height from the KTX2 header, once the level index checks out. Throws when the header is cut short. */
3
+ export declare function readKtx2(view: DataView): IImageSize | null;
@@ -0,0 +1,23 @@
1
+ const KTX2_LEVEL_INDEX = 80;
2
+ const KTX2_LEVEL_BYTES = 24;
3
+ const KTX2_MAX_LEVELS = 32;
4
+ /** Every level of the index (64 bit byte offset, then 64 bit byte length) must end inside the file. */
5
+ function ktx2LevelsFit(view, levels) {
6
+ const size = BigInt(view.byteLength);
7
+ for (let level = 0; level < levels; level++) {
8
+ const at = KTX2_LEVEL_INDEX + level * KTX2_LEVEL_BYTES;
9
+ if (view.getBigUint64(at, true) + view.getBigUint64(at + 8, true) > size) {
10
+ return false;
11
+ }
12
+ }
13
+ return true;
14
+ }
15
+ /** Width and height from the KTX2 header, once the level index checks out. Throws when the header is cut short. */
16
+ export function readKtx2(view) {
17
+ const levels = view.getUint32(40, true);
18
+ if (levels < 1 || levels > KTX2_MAX_LEVELS || !ktx2LevelsFit(view, levels)) {
19
+ return null;
20
+ }
21
+ return { width: view.getUint32(20, true), height: view.getUint32(24, true) };
22
+ }
23
+ //# sourceMappingURL=readKtx2Size.mjs.map
@@ -0,0 +1,9 @@
1
+ import type { Document } from '@gltf-transform/core';
2
+ import type { IGlbPolicy } from '../../files/GLB_POLICY_DEFAULTS.mjs';
3
+ import type { IGlbReason, IGlbStats } from '../../files/IGlbSanitiseResult.mjs';
4
+ /**
5
+ * Decode and re-encode every PNG, JPEG and WebP texture with `sharp`, and put the new bytes in the document. A texture
6
+ * that does not decode is a reason, never a throw. KTX2 textures (`KHR_texture_basisu`) cannot be decoded here: their
7
+ * header and level index were checked before, and the bytes go through untouched. Counts into `stats.texturesReencoded`.
8
+ */
9
+ export declare function reencodeTextures(document: Document, policy: IGlbPolicy, stats: IGlbStats): Promise<IGlbReason[]>;
@@ -0,0 +1,34 @@
1
+ import { detectImageFormat } from './readImageHeader.mjs';
2
+ import { reEncodeImage } from './reEncodeImage.mjs';
3
+ async function tryReEncode(image, format, policy) {
4
+ try {
5
+ return await reEncodeImage(image, format, policy.textureQuality, policy.maxTextureSide);
6
+ }
7
+ catch {
8
+ return null;
9
+ }
10
+ }
11
+ /**
12
+ * Decode and re-encode every PNG, JPEG and WebP texture with `sharp`, and put the new bytes in the document. A texture
13
+ * that does not decode is a reason, never a throw. KTX2 textures (`KHR_texture_basisu`) cannot be decoded here: their
14
+ * header and level index were checked before, and the bytes go through untouched. Counts into `stats.texturesReencoded`.
15
+ */
16
+ export async function reencodeTextures(document, policy, stats) {
17
+ const reasons = [];
18
+ for (const [position, texture] of document.getRoot().listTextures().entries()) {
19
+ const image = texture.getImage() ?? new Uint8Array();
20
+ const format = detectImageFormat(image);
21
+ if (format !== null && format.name === 'ktx2') {
22
+ continue;
23
+ }
24
+ const bytes = format === null ? null : await tryReEncode(image, format, policy);
25
+ if (bytes === null) {
26
+ reasons.push({ code: 'IMAGE_DECODE_FAILED', message: `texture ${position} could not be decoded and re-encoded` });
27
+ continue;
28
+ }
29
+ texture.setImage(bytes);
30
+ stats.texturesReencoded += 1;
31
+ }
32
+ return reasons;
33
+ }
34
+ //# sourceMappingURL=reencodeTextures.mjs.map
@@ -0,0 +1,6 @@
1
+ import { type IGlbPolicy } from '../../files/GLB_POLICY_DEFAULTS.mjs';
2
+ /**
3
+ * The defaults with the caller's options on top. Throws on misuse: an unknown option, a limit that is not a positive
4
+ * integer, a quality out of range, or an extension the sanitiser has no code for. A field set to `undefined` keeps its default.
5
+ */
6
+ export declare function resolveGlbPolicy(options: Partial<IGlbPolicy>): IGlbPolicy;
@@ -0,0 +1,48 @@
1
+ import { GLB_POLICY_DEFAULTS } from '../../files/GLB_POLICY_DEFAULTS.mjs';
2
+ import { GLB_SUPPORTED_EXTENSIONS } from './glbExtensions.mjs';
3
+ function assertKnownKeys(options) {
4
+ for (const key of Object.keys(options)) {
5
+ if (!Object.hasOwn(GLB_POLICY_DEFAULTS, key)) {
6
+ throw new TypeError(`sanitiseGlb: unknown option ${key}`);
7
+ }
8
+ }
9
+ }
10
+ const NOT_LIMITS = new Set(['allowedExtensions', 'textureQuality']);
11
+ function assertLimits(policy) {
12
+ const fields = { ...policy };
13
+ for (const [name, value] of Object.entries(fields)) {
14
+ if (!NOT_LIMITS.has(name) && (!Number.isSafeInteger(value) || value <= 0)) {
15
+ throw new TypeError(`sanitiseGlb: option ${name} must be a positive integer`);
16
+ }
17
+ }
18
+ }
19
+ function assertQuality(quality) {
20
+ if (!Number.isInteger(quality) || quality < 1 || quality > 100) {
21
+ throw new TypeError('sanitiseGlb: option textureQuality must be an integer from 1 to 100');
22
+ }
23
+ }
24
+ const isSupported = (name) => typeof name === 'string' && GLB_SUPPORTED_EXTENSIONS.includes(name);
25
+ function assertExtensions(allowed) {
26
+ if (!Array.isArray(allowed)) {
27
+ throw new TypeError('sanitiseGlb: option allowedExtensions must be an array of names');
28
+ }
29
+ const unsupported = allowed.filter((name) => !isSupported(name));
30
+ if (unsupported.length > 0) {
31
+ throw new RangeError(`sanitiseGlb: allowedExtensions holds names the sanitiser cannot rewrite: ${unsupported.map(String).join(', ')}`);
32
+ }
33
+ }
34
+ /**
35
+ * The defaults with the caller's options on top. Throws on misuse: an unknown option, a limit that is not a positive
36
+ * integer, a quality out of range, or an extension the sanitiser has no code for. A field set to `undefined` keeps its default.
37
+ */
38
+ export function resolveGlbPolicy(options) {
39
+ assertKnownKeys(options);
40
+ const given = { ...options };
41
+ const defined = Object.fromEntries(Object.entries(given).filter(([, value]) => value !== undefined));
42
+ const policy = { ...GLB_POLICY_DEFAULTS, ...defined };
43
+ assertLimits(policy);
44
+ assertQuality(policy.textureQuality);
45
+ assertExtensions(policy.allowedExtensions);
46
+ return policy;
47
+ }
48
+ //# sourceMappingURL=resolveGlbPolicy.mjs.map
@@ -0,0 +1,16 @@
1
+ import type { WebIO } from '@gltf-transform/core';
2
+ import type { IGlbPolicy } from '../../files/GLB_POLICY_DEFAULTS.mjs';
3
+ import type { IGlbReason, IGlbStats } from '../../files/IGlbSanitiseResult.mjs';
4
+ export type IRewriteResult = {
5
+ glb: Uint8Array;
6
+ reasons: [];
7
+ } | {
8
+ glb: null;
9
+ reasons: IGlbReason[];
10
+ };
11
+ /**
12
+ * Read the file with glTF Transform (which decodes the meshopt data, so a stream that does not decode is rejected
13
+ * here), then write a new one: no `extras`, no extension nobody allowed, no unused data, textures re-encoded by
14
+ * `sharp`, one embedded buffer. Never throws on a hostile file.
15
+ */
16
+ export declare function rewriteGlb(io: WebIO, input: Uint8Array, policy: IGlbPolicy, stats: IGlbStats): Promise<IRewriteResult>;
@@ -0,0 +1,49 @@
1
+ import { EXTMeshoptCompression } from '@gltf-transform/extensions';
2
+ import { prune } from '@gltf-transform/functions';
3
+ import { reencodeTextures } from './reencodeTextures.mjs';
4
+ import { stripExtras } from './stripExtras.mjs';
5
+ const failed = (reasons) => ({ glb: null, reasons });
6
+ /** The meshopt decoder throws "Malformed buffer data: <code>" on a stream it cannot decode. */
7
+ const isMeshoptFailure = (error) => /malformed buffer data/i.test(String(error));
8
+ async function readDocument(io, input) {
9
+ try {
10
+ return { document: await io.readBinary(input) };
11
+ }
12
+ catch (error) {
13
+ return isMeshoptFailure(error)
14
+ ? { reason: { code: 'MESHOPT_DECODE_FAILED', message: 'the EXT_meshopt_compression data does not decode' } }
15
+ : { reason: { code: 'READ_FAILED', message: 'the glTF reader could not read the file' } };
16
+ }
17
+ }
18
+ /**
19
+ * A file that came in with meshopt compression goes out with it: required, and encoded again from the decoded data.
20
+ * Without it the geometry of a 10 MB model would grow several times over.
21
+ */
22
+ function keepMeshopt(document) {
23
+ document
24
+ .getRoot()
25
+ .listExtensionsUsed()
26
+ .find((extension) => extension.extensionName === EXTMeshoptCompression.EXTENSION_NAME)
27
+ ?.setRequired(true);
28
+ }
29
+ /**
30
+ * Read the file with glTF Transform (which decodes the meshopt data, so a stream that does not decode is rejected
31
+ * here), then write a new one: no `extras`, no extension nobody allowed, no unused data, textures re-encoded by
32
+ * `sharp`, one embedded buffer. Never throws on a hostile file.
33
+ */
34
+ export async function rewriteGlb(io, input, policy, stats) {
35
+ const read = await readDocument(io, input);
36
+ if ('reason' in read) {
37
+ return failed([read.reason]);
38
+ }
39
+ const { document } = read;
40
+ stripExtras(document);
41
+ await document.transform(prune({ keepLeaves: true, keepAttributes: true, keepSolidTextures: true }));
42
+ const reasons = await reencodeTextures(document, policy, stats);
43
+ if (reasons.length > 0) {
44
+ return failed(reasons);
45
+ }
46
+ keepMeshopt(document);
47
+ return { glb: await io.writeBinary(document), reasons: [] };
48
+ }
49
+ //# sourceMappingURL=rewriteGlb.mjs.map
@@ -0,0 +1,8 @@
1
+ import type { Document } from '@gltf-transform/core';
2
+ /**
3
+ * Empty the `extras` of the root and of every property in the graph: scenes, nodes, meshes, primitives, accessors,
4
+ * materials, texture infos, animations, skins, cameras, buffers and the extension properties. `extras` is free-form
5
+ * JSON that tools and users fill with anything. The writer only reaches a property through the edge of its parent, so
6
+ * the children of the edges, plus the root, are every property that is written.
7
+ */
8
+ export declare function stripExtras(document: Document): void;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Empty the `extras` of the root and of every property in the graph: scenes, nodes, meshes, primitives, accessors,
3
+ * materials, texture infos, animations, skins, cameras, buffers and the extension properties. `extras` is free-form
4
+ * JSON that tools and users fill with anything. The writer only reaches a property through the edge of its parent, so
5
+ * the children of the edges, plus the root, are every property that is written.
6
+ */
7
+ export function stripExtras(document) {
8
+ const properties = new Set([document.getRoot()]);
9
+ for (const edge of document.getGraph().listEdges()) {
10
+ properties.add(edge.getChild());
11
+ }
12
+ for (const property of properties) {
13
+ property.setExtras({});
14
+ }
15
+ }
16
+ //# sourceMappingURL=stripExtras.mjs.map
@@ -0,0 +1,12 @@
1
+ import validator from 'gltf-validator';
2
+ import type { IGlbReason } from '../../files/IGlbSanitiseResult.mjs';
3
+ type IValidate = typeof validator.validateBytes;
4
+ /**
5
+ * Run the Khronos glTF Validator on a whole GLB. Every error (severity 0) becomes a `VALIDATOR_ERROR` reason, up to
6
+ * `MAX_REASONS`. Warnings, infos and hints never reject. A crash of the validator is `VALIDATOR_FAILED`, not a throw.
7
+ *
8
+ * @param glb the whole GLB file, container and chunks
9
+ * @param validate the validator entry point, a parameter so a spec can make it fail
10
+ */
11
+ export declare function validateKhronos(glb: Uint8Array, validate?: IValidate): Promise<IGlbReason[]>;
12
+ export {};
@@ -0,0 +1,32 @@
1
+ import validator from 'gltf-validator';
2
+ import { MAX_REASONS } from './glbReason.mjs';
3
+ /** Messages the validator collects before it stops. Errors come with the structure checks, so they come early. */
4
+ const MAX_ISSUES = 100;
5
+ /**
6
+ * Run the Khronos glTF Validator on a whole GLB. Every error (severity 0) becomes a `VALIDATOR_ERROR` reason, up to
7
+ * `MAX_REASONS`. Warnings, infos and hints never reject. A crash of the validator is `VALIDATOR_FAILED`, not a throw.
8
+ *
9
+ * @param glb the whole GLB file, container and chunks
10
+ * @param validate the validator entry point, a parameter so a spec can make it fail
11
+ */
12
+ export async function validateKhronos(glb, validate = (bytes, options) => validator.validateBytes(bytes, options)) {
13
+ let issues;
14
+ try {
15
+ issues = (await validate(glb, { maxIssues: MAX_ISSUES })).issues;
16
+ }
17
+ catch {
18
+ return [{ code: 'VALIDATOR_FAILED', message: 'the glTF Validator crashed on the file' }];
19
+ }
20
+ const errors = issues.messages
21
+ .filter((issue) => issue.severity === 0)
22
+ .slice(0, MAX_REASONS)
23
+ .map((issue) => ({
24
+ code: 'VALIDATOR_ERROR',
25
+ message: `${issue.code}: ${issue.message}`,
26
+ ...(issue.pointer === undefined ? {} : { pointer: issue.pointer })
27
+ }));
28
+ return errors.length === 0 && issues.numErrors > 0
29
+ ? [{ code: 'VALIDATOR_ERROR', message: `the glTF Validator reported ${issues.numErrors} errors` }]
30
+ : errors;
31
+ }
32
+ //# sourceMappingURL=validateKhronos.mjs.map
@@ -0,0 +1,8 @@
1
+ import type { WebIO } from '@gltf-transform/core';
2
+ import type { IGlbReason } from '../../files/IGlbSanitiseResult.mjs';
3
+ /**
4
+ * Check the file the sanitiser wrote the way an outsider would: the Khronos validator must find no error, and glTF
5
+ * Transform must read it back, meshopt decoding included. Every output must decode, so a file that fails here is never
6
+ * returned, whatever the input looked like.
7
+ */
8
+ export declare function verifyGlbOutput(io: WebIO, glb: Uint8Array): Promise<IGlbReason[]>;
@@ -0,0 +1,20 @@
1
+ import { validateKhronos } from './validateKhronos.mjs';
2
+ /**
3
+ * Check the file the sanitiser wrote the way an outsider would: the Khronos validator must find no error, and glTF
4
+ * Transform must read it back, meshopt decoding included. Every output must decode, so a file that fails here is never
5
+ * returned, whatever the input looked like.
6
+ */
7
+ export async function verifyGlbOutput(io, glb) {
8
+ const problems = await validateKhronos(glb);
9
+ if (problems.length > 0) {
10
+ return problems.map((problem) => ({ ...problem, code: 'OUTPUT_INVALID' }));
11
+ }
12
+ try {
13
+ await io.readBinary(glb);
14
+ }
15
+ catch {
16
+ return [{ code: 'OUTPUT_INVALID', message: 'the sanitised file does not decode' }];
17
+ }
18
+ return [];
19
+ }
20
+ //# sourceMappingURL=verifyGlbOutput.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiumine/koa-utils",
3
- "version": "9.1.1",
3
+ "version": "9.3.0",
4
4
  "description": "Koa Utils",
5
5
  "type": "module",
6
6
  "repository": {
@@ -24,7 +24,7 @@
24
24
  "prepare:all": "echo 'read REAMDE' && rm -rf dist && yarn run build:all",
25
25
  "build:all": "yarn run build:cjs && yarn run build:esm",
26
26
  "build:cjs": "yarn run tsc -p tsconfig.cjs.json && find dist/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\; && cp -R dist/cjs/* dist/ && rm -rf dist/cjs",
27
- "build:esm": "yarn run tspc -p tsconfig.json && find dist/esm -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\; && cp -R dist/esm/* dist/ && find dist -name \"*.mjs.map\" -exec sed -i -E 's#(\"sources\":\\[\")\\.\\./#\\1#' {} + && rm -rf dist/esm",
27
+ "build:esm": "yarn run tsc -p tsconfig.json && find dist/esm -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\; && node scripts/rewrite-path-aliases.mts tsconfig.json && cp -R dist/esm/* dist/ && find dist -name \"*.mjs.map\" -exec sed -i -E 's#(\"sources\":\\[\")\\.\\./#\\1#' {} + && rm -rf dist/esm",
28
28
  "lint": "eslint --fix && prettier --write 'src/**/*.mts' 'scripts/**/*.mts'",
29
29
  "lint:check": "eslint --max-warnings 0 --cache && prettier --check 'src/**/*.mts' 'scripts/**/*.mts'",
30
30
  "typecheck": "tsc -p tsconfig.check.json --noEmit",
@@ -42,7 +42,7 @@
42
42
  "leak:check": "node --expose-gc scripts/leak-check.mts",
43
43
  "check:static": "yarn caps && yarn lint:check && yarn typecheck && yarn typecheck:scripts && yarn typecheck:tests && yarn knip && yarn type-coverage && yarn fta",
44
44
  "clean": "rm -rf ./dist",
45
- "build:tests": "rm -rf dist-test && yarn run tspc -p tsconfig.build-tests.json && find dist-test -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
45
+ "build:tests": "rm -rf dist-test && yarn run tsc -p tsconfig.build-tests.json && find dist-test -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\; && node scripts/rewrite-path-aliases.mts tsconfig.build-tests.json",
46
46
  "test": "yarn build && yarn build:tests && mocha",
47
47
  "test:watch": "mocha --watch",
48
48
  "test:coverage": "yarn build && yarn build:tests && c8 mocha",
@@ -206,6 +206,42 @@
206
206
  "import": "./dist/files/validatePdfExtension.mjs",
207
207
  "types": "./dist/files/validatePdfExtension.d.mts"
208
208
  },
209
+ "./files/validateGlbExtension": {
210
+ "import": "./dist/files/validateGlbExtension.mjs",
211
+ "types": "./dist/files/validateGlbExtension.d.mts"
212
+ },
213
+ "./files/validateMimeTypeGlb": {
214
+ "import": "./dist/files/validateMimeTypeGlb.mjs",
215
+ "types": "./dist/files/validateMimeTypeGlb.d.mts"
216
+ },
217
+ "./files/checkGlbContainer": {
218
+ "import": "./dist/files/checkGlbContainer.mjs",
219
+ "types": "./dist/files/checkGlbContainer.d.mts"
220
+ },
221
+ "./files/sanitiseGlb": {
222
+ "import": "./dist/files/sanitiseGlb.mjs",
223
+ "types": "./dist/files/sanitiseGlb.d.mts"
224
+ },
225
+ "./files/sanitiseGlbFile": {
226
+ "import": "./dist/files/sanitiseGlbFile.mjs",
227
+ "types": "./dist/files/sanitiseGlbFile.d.mts"
228
+ },
229
+ "./files/GLB_POLICY_DEFAULTS": {
230
+ "import": "./dist/files/GLB_POLICY_DEFAULTS.mjs",
231
+ "types": "./dist/files/GLB_POLICY_DEFAULTS.d.mts"
232
+ },
233
+ "./files/GLB_REJECT_CODES": {
234
+ "import": "./dist/files/GLB_REJECT_CODES.mjs",
235
+ "types": "./dist/files/GLB_REJECT_CODES.d.mts"
236
+ },
237
+ "./files/IGlbSanitiseResult": {
238
+ "import": "./dist/files/IGlbSanitiseResult.mjs",
239
+ "types": "./dist/files/IGlbSanitiseResult.d.mts"
240
+ },
241
+ "./files/IGlbContainer": {
242
+ "import": "./dist/files/IGlbContainer.mjs",
243
+ "types": "./dist/files/IGlbContainer.d.mts"
244
+ },
209
245
  "./graphQL/schema/types/MidNameType": {
210
246
  "import": "./dist/graphQL/schema/types/MidNameType.mjs",
211
247
  "types": "./dist/graphQL/schema/types/MidNameType.d.mts"
@@ -1213,17 +1249,21 @@
1213
1249
  "!dist/**/*.js.map"
1214
1250
  ],
1215
1251
  "peerDependencies": {
1252
+ "@gltf-transform/core": "<5",
1253
+ "@gltf-transform/extensions": "<5",
1254
+ "@gltf-transform/functions": "<5",
1216
1255
  "@node-rs/bcrypt": "<2",
1217
1256
  "@sentry/node": "<11",
1218
- "@socketlabs/email": "<2",
1219
1257
  "clamscan": "<3",
1220
1258
  "dotenv": "<18",
1221
1259
  "file-type": "<23",
1222
1260
  "fs-extra": "<12",
1261
+ "gltf-validator": ">=2.0.0-0 <3",
1223
1262
  "graphql": "<18",
1224
1263
  "keygrip": "^1.1.0",
1225
1264
  "koa": "<4",
1226
1265
  "mariadb": "<4",
1266
+ "meshoptimizer": "<2",
1227
1267
  "mongoose": "<10",
1228
1268
  "pg": "<9",
1229
1269
  "redis": "<7",
@@ -1233,13 +1273,19 @@
1233
1273
  "sharp": "<1"
1234
1274
  },
1235
1275
  "peerDependenciesMeta": {
1236
- "@node-rs/bcrypt": {
1276
+ "@gltf-transform/core": {
1237
1277
  "optional": true
1238
1278
  },
1239
- "@sentry/node": {
1279
+ "@gltf-transform/extensions": {
1240
1280
  "optional": true
1241
1281
  },
1242
- "@socketlabs/email": {
1282
+ "@gltf-transform/functions": {
1283
+ "optional": true
1284
+ },
1285
+ "@node-rs/bcrypt": {
1286
+ "optional": true
1287
+ },
1288
+ "@sentry/node": {
1243
1289
  "optional": true
1244
1290
  },
1245
1291
  "clamscan": {
@@ -1254,6 +1300,9 @@
1254
1300
  "fs-extra": {
1255
1301
  "optional": true
1256
1302
  },
1303
+ "gltf-validator": {
1304
+ "optional": true
1305
+ },
1257
1306
  "graphql": {
1258
1307
  "optional": true
1259
1308
  },
@@ -1266,6 +1315,9 @@
1266
1315
  "mariadb": {
1267
1316
  "optional": true
1268
1317
  },
1318
+ "meshoptimizer": {
1319
+ "optional": true
1320
+ },
1269
1321
  "mongoose": {
1270
1322
  "optional": true
1271
1323
  },
@@ -1293,9 +1345,11 @@
1293
1345
  "@eslint-community/eslint-plugin-eslint-comments": "^4.8.1",
1294
1346
  "@eslint/eslintrc": "^3.3.7",
1295
1347
  "@eslint/js": "^10.0.1",
1348
+ "@gltf-transform/core": "^4.5.1",
1349
+ "@gltf-transform/extensions": "^4.5.1",
1350
+ "@gltf-transform/functions": "^4.5.1",
1296
1351
  "@node-rs/bcrypt": "^1.10.9",
1297
1352
  "@sentry/node": "^10.78.0",
1298
- "@socketlabs/email": "^1.4.4",
1299
1353
  "@stryker-mutator/core": "10.1.1",
1300
1354
  "@stryker-mutator/mocha-runner": "10.1.1",
1301
1355
  "@stryker-mutator/typescript-checker": "10.1.1",
@@ -1313,7 +1367,7 @@
1313
1367
  "chai": "^6.3.0",
1314
1368
  "clamscan": "^2.4.0",
1315
1369
  "dotenv": "^17.4.2",
1316
- "eslint": "^10.14.0",
1370
+ "eslint": "^10.15.0",
1317
1371
  "eslint-config-prettier": "^10.1.8",
1318
1372
  "eslint-import-resolver-typescript": "^4.4.5",
1319
1373
  "eslint-plugin-n": "^18.5.0",
@@ -1321,21 +1375,23 @@
1321
1375
  "eslint-plugin-security": "^4.2.1",
1322
1376
  "eslint-plugin-simple-import-sort": "^14.0.0",
1323
1377
  "eslint-plugin-sonarjs": "^4.2.2",
1324
- "fast-check": "^4.10.2",
1378
+ "fast-check": "^4.10.3",
1325
1379
  "file-type": "^22.3.0",
1326
1380
  "fs-extra": "^11.4.1",
1327
1381
  "fta-cli": "^3.1.0",
1382
+ "gltf-validator": "^2.0.0-dev.3.10",
1328
1383
  "graphql": "^16.15.0",
1329
1384
  "keygrip": "^1.1.0",
1330
- "knip": "^6.44.2",
1385
+ "knip": "^6.45.1",
1331
1386
  "koa": "^3.3.0",
1332
1387
  "mariadb": "^3.5.5",
1388
+ "meshoptimizer": "^1.3.0",
1333
1389
  "mocha": "^12.0.3",
1334
1390
  "mongodb-memory-server": "^11.3.0",
1335
- "mongoose": "^9.12.1",
1391
+ "mongoose": "^9.12.2",
1336
1392
  "patch-package": "8.0.1",
1337
1393
  "pg": "^8.23.1",
1338
- "prettier": "^3.9.10",
1394
+ "prettier": "^3.10.1",
1339
1395
  "redis": "^6.3.1",
1340
1396
  "reflect-metadata": "^0.2.2",
1341
1397
  "sequelize": "^6.37.8",
@@ -1343,15 +1399,12 @@
1343
1399
  "sharp": "^0.35.3",
1344
1400
  "sinon": "^22.1.0",
1345
1401
  "ts-node": "^10.9.2",
1346
- "ts-patch": "^4.0.1",
1347
1402
  "type-coverage": "^2.31.2",
1348
1403
  "typescript": "^6.0.3",
1349
1404
  "typescript-eslint": "8.72.2",
1350
- "typescript-transform-paths": "^4.0.0",
1351
1405
  "uuid": "^14.0.3"
1352
1406
  },
1353
1407
  "resolutions": {
1354
- "@socketlabs/email/axios": "^1.21.0",
1355
1408
  "sequelize/uuid": "^11.1.1",
1356
1409
  "typed-rest-client/qs": "^6.16.1",
1357
1410
  "glob": "^14.0.2",