@bitbybit-dev/base 1.4.1 → 1.5.0-rc.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 (121) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/color-space.d.ts +14 -0
  3. package/lib/api/color-space.js +24 -0
  4. package/lib/api/index.d.ts +2 -0
  5. package/lib/api/index.js +2 -0
  6. package/lib/api/inputs/base-inputs.d.ts +382 -5
  7. package/lib/api/inputs/base-inputs.js +0 -3
  8. package/lib/api/inputs/color-inputs.d.ts +1 -1
  9. package/lib/api/inputs/color-inputs.js +0 -1
  10. package/lib/api/inputs/dates-inputs.js +0 -1
  11. package/lib/api/inputs/frame-inputs.d.ts +1 -1
  12. package/lib/api/inputs/inputs.js +0 -1
  13. package/lib/api/inputs/io-inputs.d.ts +1 -1
  14. package/lib/api/inputs/line-inputs.d.ts +1 -1
  15. package/lib/api/inputs/lists-inputs.js +0 -1
  16. package/lib/api/inputs/logic-inputs.d.ts +1 -1
  17. package/lib/api/inputs/logic-inputs.js +0 -1
  18. package/lib/api/inputs/math-inputs.js +0 -1
  19. package/lib/api/inputs/mesh-inputs.d.ts +29 -1
  20. package/lib/api/inputs/mesh-inputs.js +25 -1
  21. package/lib/api/inputs/point-inputs.d.ts +30 -1
  22. package/lib/api/inputs/point-inputs.js +28 -0
  23. package/lib/api/inputs/polyline-inputs.d.ts +1 -1
  24. package/lib/api/inputs/transforms-inputs.d.ts +1 -1
  25. package/lib/api/inputs/vector-inputs.d.ts +1 -1
  26. package/lib/api/kernel-calls/describe-failure.d.ts +3 -2
  27. package/lib/api/kernel-calls/describe-failure.js +9 -17
  28. package/lib/api/kernel-calls/failure-message.d.ts +1 -1
  29. package/lib/api/kernel-calls/hashing.d.ts +14 -0
  30. package/lib/api/kernel-calls/hashing.js +47 -0
  31. package/lib/api/kernel-calls/index.d.ts +3 -0
  32. package/lib/api/kernel-calls/index.js +3 -0
  33. package/lib/api/kernel-calls/input-rules.d.ts +1 -1
  34. package/lib/api/kernel-calls/input-rules.js +10 -5
  35. package/lib/api/kernel-calls/input-validation.d.ts +3 -3
  36. package/lib/api/kernel-calls/input-validation.js +24 -14
  37. package/lib/api/kernel-calls/kernel-steps.d.ts +19 -0
  38. package/lib/api/kernel-calls/kernel-steps.js +19 -0
  39. package/lib/api/kernel-calls/resolve-dto.d.ts +5 -3
  40. package/lib/api/kernel-calls/resolve-dto.js +9 -4
  41. package/lib/api/kernel-calls/unknown-values.d.ts +12 -0
  42. package/lib/api/kernel-calls/unknown-values.js +16 -0
  43. package/lib/api/kernel-calls/with-defaults.d.ts +1 -1
  44. package/lib/api/models/point/hex-grid-data.d.ts +1 -1
  45. package/lib/api/models/text/vector-char-data.d.ts +1 -1
  46. package/lib/api/models/text/vector-text-data.d.ts +1 -1
  47. package/lib/api/recipes/checker-types.d.ts +14 -0
  48. package/lib/api/recipes/checker-types.js +1 -0
  49. package/lib/api/recipes/index.d.ts +4 -0
  50. package/lib/api/recipes/index.js +3 -0
  51. package/lib/api/recipes/limits.constants.d.ts +23 -0
  52. package/lib/api/recipes/limits.constants.js +23 -0
  53. package/lib/api/recipes/recipe-types.d.ts +48 -0
  54. package/lib/api/recipes/recipe-types.js +1 -0
  55. package/lib/api/recipes/sources.d.ts +16 -0
  56. package/lib/api/recipes/sources.js +23 -0
  57. package/lib/api/recipes/surface-mesh.d.ts +16 -0
  58. package/lib/api/recipes/surface-mesh.js +86 -0
  59. package/lib/api/recipes/validate.d.ts +36 -0
  60. package/lib/api/recipes/validate.js +427 -0
  61. package/lib/api/resolved-inputs/index.d.ts +3 -0
  62. package/lib/api/services/color.d.ts +1 -1
  63. package/lib/api/services/frame.d.ts +3 -95
  64. package/lib/api/services/frame.js +24 -135
  65. package/lib/api/services/geometry-helper.d.ts +1 -1
  66. package/lib/api/services/helpers/arcs.d.ts +6 -0
  67. package/lib/api/services/helpers/arcs.js +30 -0
  68. package/lib/api/services/helpers/crc32.d.ts +1 -0
  69. package/lib/api/services/helpers/crc32.js +21 -0
  70. package/lib/api/services/helpers/dxf/dxf-generator.d.ts +0 -83
  71. package/lib/api/services/helpers/dxf/dxf-generator.js +5 -86
  72. package/lib/api/services/helpers/frame-axes.d.ts +9 -1
  73. package/lib/api/services/helpers/frame-axes.js +34 -0
  74. package/lib/api/services/helpers/helper-types.d.ts +34 -0
  75. package/lib/api/services/helpers/helper-types.js +1 -0
  76. package/lib/api/services/helpers/inflate.constants.d.ts +41 -0
  77. package/lib/api/services/helpers/inflate.constants.js +41 -0
  78. package/lib/api/services/helpers/inflate.d.ts +3 -0
  79. package/lib/api/services/helpers/inflate.js +228 -0
  80. package/lib/api/services/helpers/matrices.constants.d.ts +2 -0
  81. package/lib/api/services/helpers/matrices.constants.js +2 -0
  82. package/lib/api/services/helpers/matrices.d.ts +9 -1
  83. package/lib/api/services/helpers/matrices.js +18 -0
  84. package/lib/api/services/helpers/mesh-measures.d.ts +1 -0
  85. package/lib/api/services/helpers/mesh-measures.js +28 -0
  86. package/lib/api/services/helpers/mesh-normals.d.ts +2 -0
  87. package/lib/api/services/helpers/mesh-normals.js +118 -26
  88. package/lib/api/services/helpers/mesh-primitives.constants.d.ts +2 -0
  89. package/lib/api/services/helpers/mesh-primitives.constants.js +12 -0
  90. package/lib/api/services/helpers/mesh-primitives.d.ts +3 -0
  91. package/lib/api/services/helpers/mesh-primitives.js +46 -0
  92. package/lib/api/services/helpers/nurbs.d.ts +7 -0
  93. package/lib/api/services/helpers/nurbs.js +50 -0
  94. package/lib/api/services/helpers/paths.d.ts +8 -0
  95. package/lib/api/services/helpers/paths.js +96 -0
  96. package/lib/api/services/helpers/polygons.d.ts +8 -0
  97. package/lib/api/services/helpers/polygons.js +59 -0
  98. package/lib/api/services/helpers/surface-triangulation.d.ts +6 -0
  99. package/lib/api/services/helpers/surface-triangulation.js +298 -0
  100. package/lib/api/services/helpers/triangulation.d.ts +2 -0
  101. package/lib/api/services/helpers/triangulation.js +295 -0
  102. package/lib/api/services/helpers/vectors.d.ts +18 -0
  103. package/lib/api/services/helpers/vectors.js +17 -0
  104. package/lib/api/services/helpers/zip.constants.d.ts +34 -0
  105. package/lib/api/services/helpers/zip.constants.js +34 -0
  106. package/lib/api/services/helpers/zip.d.ts +4 -0
  107. package/lib/api/services/helpers/zip.js +89 -0
  108. package/lib/api/services/line.d.ts +3 -3
  109. package/lib/api/services/mesh.d.ts +37 -2
  110. package/lib/api/services/mesh.js +60 -3
  111. package/lib/api/services/point.d.ts +22 -10
  112. package/lib/api/services/point.js +42 -13
  113. package/lib/api/services/polyline.d.ts +39 -4
  114. package/lib/api/services/polyline.js +56 -6
  115. package/lib/api/services/text.d.ts +2 -2
  116. package/lib/api/services/text.js +10 -5
  117. package/lib/api/services/transforms.d.ts +3 -9
  118. package/lib/api/services/transforms.js +0 -6
  119. package/lib/api/services/vector.d.ts +2 -2
  120. package/lib/api/services/vector.js +2 -5
  121. package/package.json +1 -1
@@ -1,5 +1,6 @@
1
1
  import * as Inputs from "../../../inputs";
2
2
  import { resolveDto } from "../../../kernel-calls";
3
+ const CLOSED_POLYLINE_TOLERANCE = 1e-10;
3
4
  export class DxfGenerator {
4
5
  constructor() {
5
6
  this.entityHandle = 256;
@@ -21,9 +22,6 @@ export class DxfGenerator {
21
22
  dxfContent.push("0", "EOF");
22
23
  return dxfContent.join("\n");
23
24
  }
24
- /**
25
- * Generate DXF header section
26
- */
27
25
  generateHeader() {
28
26
  const header = [
29
27
  "0",
@@ -44,9 +42,6 @@ export class DxfGenerator {
44
42
  header.push("0", "ENDSEC");
45
43
  return header;
46
44
  }
47
- /**
48
- * Generate DXF tables section (layers, line types, etc.)
49
- */
50
45
  generateTables(dxfInputs) {
51
46
  const tables = [
52
47
  "0",
@@ -69,9 +64,6 @@ export class DxfGenerator {
69
64
  tables.push("0", "ENDSEC");
70
65
  return tables;
71
66
  }
72
- /**
73
- * Generate line type table
74
- */
75
67
  generateLineTypeTable() {
76
68
  const ltype = [
77
69
  "0",
@@ -91,9 +83,6 @@ export class DxfGenerator {
91
83
  ltype.push("0", "ENDTAB");
92
84
  return ltype;
93
85
  }
94
- /**
95
- * Generate text style table
96
- */
97
86
  generateStyleTable() {
98
87
  const style = [
99
88
  "0",
@@ -113,9 +102,6 @@ export class DxfGenerator {
113
102
  style.push("0", "ENDTAB");
114
103
  return style;
115
104
  }
116
- /**
117
- * Generate VPORT table (viewport configuration)
118
- */
119
105
  generateVportTable() {
120
106
  return [
121
107
  "0",
@@ -200,9 +186,6 @@ export class DxfGenerator {
200
186
  "ENDTAB"
201
187
  ];
202
188
  }
203
- /**
204
- * Generate VIEW table (empty but required for AC1009)
205
- */
206
189
  generateViewTable() {
207
190
  return [
208
191
  "0",
@@ -215,9 +198,6 @@ export class DxfGenerator {
215
198
  "ENDTAB"
216
199
  ];
217
200
  }
218
- /**
219
- * Generate UCS table (user coordinate system - empty but required for AC1009)
220
- */
221
201
  generateUcsTable() {
222
202
  return [
223
203
  "0",
@@ -230,9 +210,6 @@ export class DxfGenerator {
230
210
  "ENDTAB"
231
211
  ];
232
212
  }
233
- /**
234
- * Generate APPID table (application ID - required for AC1009)
235
- */
236
213
  generateAppidTable() {
237
214
  return [
238
215
  "0",
@@ -251,9 +228,6 @@ export class DxfGenerator {
251
228
  "ENDTAB"
252
229
  ];
253
230
  }
254
- /**
255
- * Generate DIMSTYLE table (dimension style - empty but required for AC1009)
256
- */
257
231
  generateDimstyleTable() {
258
232
  return [
259
233
  "0",
@@ -266,9 +240,6 @@ export class DxfGenerator {
266
240
  "ENDTAB"
267
241
  ];
268
242
  }
269
- /**
270
- * Generate blocks section (empty but required)
271
- */
272
243
  generateBlocks() {
273
244
  return [
274
245
  "0",
@@ -279,9 +250,6 @@ export class DxfGenerator {
279
250
  "ENDSEC"
280
251
  ];
281
252
  }
282
- /**
283
- * Generate layer table based on unique layers in all parts
284
- */
285
253
  generateLayerTable(dxfInputs) {
286
254
  const layers = new Set();
287
255
  if (dxfInputs.dxfPathsParts) {
@@ -316,9 +284,6 @@ export class DxfGenerator {
316
284
  layerTable.push("0", "ENDTAB");
317
285
  return layerTable;
318
286
  }
319
- /**
320
- * Generate DXF entities section with all path segments
321
- */
322
287
  generateEntities(dxfInputs) {
323
288
  const entities = [
324
289
  "0",
@@ -343,9 +308,6 @@ export class DxfGenerator {
343
308
  entities.push("0", "ENDSEC");
344
309
  return entities;
345
310
  }
346
- /**
347
- * Generate entity for a single segment based on its type
348
- */
349
311
  generateSegmentEntity(segment, part) {
350
312
  if (this.isLineSegment(segment)) {
351
313
  return this.generateLineEntity(segment, part);
@@ -364,39 +326,21 @@ export class DxfGenerator {
364
326
  }
365
327
  return [];
366
328
  }
367
- /**
368
- * Type guard for line segments
369
- */
370
329
  isLineSegment(segment) {
371
330
  return segment.start !== undefined && segment.end !== undefined && segment.radius === undefined;
372
331
  }
373
- /**
374
- * Type guard for arc segments
375
- */
376
332
  isArcSegment(segment) {
377
333
  return segment.center !== undefined && segment.radius !== undefined && segment.startAngle !== undefined && segment.endAngle !== undefined;
378
334
  }
379
- /**
380
- * Type guard for circle segments
381
- */
382
335
  isCircleSegment(segment) {
383
336
  return segment.center !== undefined && segment.radius !== undefined && segment.startAngle === undefined;
384
337
  }
385
- /**
386
- * Type guard for polyline segments
387
- */
388
338
  isPolylineSegment(segment) {
389
339
  return segment.points !== undefined && Array.isArray(segment.points);
390
340
  }
391
- /**
392
- * Type guard for spline segments
393
- */
394
341
  isSplineSegment(segment) {
395
342
  return segment.controlPoints !== undefined && Array.isArray(segment.controlPoints);
396
343
  }
397
- /**
398
- * Generate a LINE entity
399
- */
400
344
  generateLineEntity(line, part) {
401
345
  const entity = [
402
346
  "0",
@@ -414,9 +358,6 @@ export class DxfGenerator {
414
358
  }
415
359
  return entity;
416
360
  }
417
- /**
418
- * Generate a CIRCLE entity
419
- */
420
361
  generateCircleEntity(circle, part) {
421
362
  const entity = [
422
363
  "0",
@@ -434,9 +375,6 @@ export class DxfGenerator {
434
375
  }
435
376
  return entity;
436
377
  }
437
- /**
438
- * Generate an ARC entity
439
- */
440
378
  generateArcEntity(arc, part) {
441
379
  const entity = [
442
380
  "0",
@@ -459,9 +397,6 @@ export class DxfGenerator {
459
397
  }
460
398
  return entity;
461
399
  }
462
- /**
463
- * Generate a LWPOLYLINE entity
464
- */
465
400
  generatePolylineEntity(segment, part) {
466
401
  const polyline = resolveDto(Inputs.IO.DxfPolylineSegmentDto, segment);
467
402
  const entity = [
@@ -490,9 +425,6 @@ export class DxfGenerator {
490
425
  });
491
426
  return entity;
492
427
  }
493
- /**
494
- * Generate a SPLINE entity
495
- */
496
428
  generateSplineEntity(segment, part) {
497
429
  const spline = resolveDto(Inputs.IO.DxfSplineSegmentDto, segment);
498
430
  const entity = [
@@ -521,27 +453,18 @@ export class DxfGenerator {
521
453
  });
522
454
  return entity;
523
455
  }
524
- /**
525
- * Check if polyline should be closed (first and last points are the same)
526
- */
527
456
  isClosedPolyline(points) {
528
- if (points.length < 3)
457
+ if (points.length < 3) {
529
458
  return false;
459
+ }
530
460
  const first = points[0];
531
461
  const last = points[points.length - 1];
532
- return Math.abs(first[0] - last[0]) < 1e-10 &&
533
- Math.abs(first[1] - last[1]) < 1e-10;
462
+ return Math.abs(first[0] - last[0]) < CLOSED_POLYLINE_TOLERANCE &&
463
+ Math.abs(first[1] - last[1]) < CLOSED_POLYLINE_TOLERANCE;
534
464
  }
535
- /**
536
- * Get next entity handle as hex string
537
- */
538
465
  getNextHandle() {
539
466
  return (++this.entityHandle).toString(16).toUpperCase();
540
467
  }
541
- /**
542
- * Convert a hex color (#RRGGBB) to DXF color codes, as the nearest ACI index or as true color
543
- * depending on the colorFormat setting; anything else is written as ACI 7
544
- */
545
468
  convertColorToDxf(color) {
546
469
  if (color.startsWith("#")) {
547
470
  const hex = color.substring(1);
@@ -564,10 +487,6 @@ export class DxfGenerator {
564
487
  }
565
488
  return [{ code: "62", value: "7" }];
566
489
  }
567
- /**
568
- * Convert RGB values to nearest AutoCAD Color Index (ACI)
569
- * Uses a simplified mapping to standard ACI colors
570
- */
571
490
  rgbToAciColorIndex(r, g, b) {
572
491
  const aciColors = {
573
492
  1: [255, 0, 0],
@@ -1,4 +1,4 @@
1
- import * as Inputs from "../../inputs";
1
+ import type * as Inputs from "../../inputs";
2
2
  type Vec3 = Inputs.Base.Vector3;
3
3
  /**
4
4
  * The sine below which two directions count as parallel wherever a frame is squared: a direction
@@ -33,4 +33,12 @@ export declare const unitOf: (vector: Vec3) => Vec3 | undefined;
33
33
  * the first subtraction, and the second takes out what rounding left along the normal.
34
34
  */
35
35
  export declare const squareFrame: (origin: Vec3, normal: Vec3, direction: Vec3) => FrameAxes | "normal" | "direction";
36
+ export declare const WORLD_AXES: FrameAxes;
37
+ export declare const vectorToWorld: (axes: FrameAxes, vector: Vec3) => Vec3;
38
+ export declare const pointToWorld: (axes: FrameAxes, point: Vec3) => Vec3;
39
+ export declare const vectorToLocal: (axes: FrameAxes, vector: Vec3) => Vec3;
40
+ export declare const pointToLocal: (axes: FrameAxes, point: Vec3) => Vec3;
41
+ export declare const composeAxes: (parent: FrameAxes, child: FrameAxes) => FrameAxes;
42
+ export declare const relativeAxes: (parent: FrameAxes, child: FrameAxes) => FrameAxes;
43
+ export declare const axesToMatrix: (axes: FrameAxes) => Inputs.Base.TransformMatrix;
36
44
  export {};
@@ -55,3 +55,37 @@ export const squareFrame = (origin, normal, direction) => {
55
55
  const y = [z[1] * x[2] - z[2] * x[1], z[2] * x[0] - z[0] * x[2], z[0] * x[1] - z[1] * x[0]];
56
56
  return { origin: [origin[0], origin[1], origin[2]], x, y, z };
57
57
  };
58
+ export const WORLD_AXES = { origin: [0, 0, 0], x: [1, 0, 0], y: [0, 1, 0], z: [0, 0, 1] };
59
+ export const vectorToWorld = (axes, vector) => [
60
+ (axes.x[0] * vector[0] + axes.y[0] * vector[1]) + axes.z[0] * vector[2],
61
+ (axes.x[1] * vector[0] + axes.y[1] * vector[1]) + axes.z[1] * vector[2],
62
+ (axes.x[2] * vector[0] + axes.y[2] * vector[1]) + axes.z[2] * vector[2],
63
+ ];
64
+ export const pointToWorld = (axes, point) => {
65
+ const along = vectorToWorld(axes, point);
66
+ return [axes.origin[0] + along[0], axes.origin[1] + along[1], axes.origin[2] + along[2]];
67
+ };
68
+ export const vectorToLocal = (axes, vector) => [
69
+ vector[0] * axes.x[0] + vector[1] * axes.x[1] + vector[2] * axes.x[2],
70
+ vector[0] * axes.y[0] + vector[1] * axes.y[1] + vector[2] * axes.y[2],
71
+ vector[0] * axes.z[0] + vector[1] * axes.z[1] + vector[2] * axes.z[2],
72
+ ];
73
+ export const pointToLocal = (axes, point) => vectorToLocal(axes, [point[0] - axes.origin[0], point[1] - axes.origin[1], point[2] - axes.origin[2]]);
74
+ export const composeAxes = (parent, child) => ({
75
+ origin: pointToWorld(parent, child.origin),
76
+ x: vectorToWorld(parent, child.x),
77
+ y: vectorToWorld(parent, child.y),
78
+ z: vectorToWorld(parent, child.z),
79
+ });
80
+ export const relativeAxes = (parent, child) => ({
81
+ origin: pointToLocal(parent, child.origin),
82
+ x: vectorToLocal(parent, child.x),
83
+ y: vectorToLocal(parent, child.y),
84
+ z: vectorToLocal(parent, child.z),
85
+ });
86
+ export const axesToMatrix = (axes) => [
87
+ axes.x[0], axes.x[1], axes.x[2], 0,
88
+ axes.y[0], axes.y[1], axes.y[2], 0,
89
+ axes.z[0], axes.z[1], axes.z[2], 0,
90
+ axes.origin[0], axes.origin[1], axes.origin[2], 1,
91
+ ];
@@ -0,0 +1,34 @@
1
+ import type * as Inputs from "../../inputs";
2
+ export interface ThreePointArc {
3
+ readonly center: Inputs.Base.Point3;
4
+ readonly x: Inputs.Base.Vector3;
5
+ readonly y: Inputs.Base.Vector3;
6
+ readonly radius: number;
7
+ readonly sweep: number;
8
+ }
9
+ export interface CreasedMesh {
10
+ readonly positions: Float32Array;
11
+ readonly normals: Float32Array;
12
+ readonly indices: Uint32Array;
13
+ }
14
+ export interface MeshArrays {
15
+ readonly positions: number[];
16
+ readonly indices: number[];
17
+ }
18
+ export interface HuffmanTable {
19
+ readonly entries: Int32Array;
20
+ readonly bits: number;
21
+ }
22
+ export interface ZipEntry {
23
+ readonly name: string;
24
+ readonly method: number;
25
+ readonly crc32: number;
26
+ readonly compressedSize: number;
27
+ readonly size: number;
28
+ readonly localOffset: number;
29
+ readonly encrypted: boolean;
30
+ }
31
+ export interface RefinedSurfaceMesh {
32
+ readonly positions: Inputs.Base.Point3[];
33
+ readonly triangles: number[];
34
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ export declare const MAX_CODE_BITS = 15;
2
+ export declare const SYMBOL_SHIFT = 4;
3
+ export declare const LENGTH_MASK = 15;
4
+ export declare const BYTE_BITS = 8;
5
+ export declare const REFILL_BELOW = 24;
6
+ export declare const STORED_BLOCK = 0;
7
+ export declare const FIXED_BLOCK = 1;
8
+ export declare const DYNAMIC_BLOCK = 2;
9
+ export declare const BLOCK_TYPE_BITS = 2;
10
+ export declare const END_OF_BLOCK = 256;
11
+ export declare const FIRST_LENGTH_SYMBOL = 257;
12
+ export declare const LITERAL_LENGTH_SYMBOLS = 288;
13
+ export declare const DISTANCE_SYMBOLS = 32;
14
+ export declare const FIXED_LITERAL_BITS: readonly [8, 9, 7, 8];
15
+ export declare const FIXED_LITERAL_ENDS: readonly [144, 256, 280, 288];
16
+ export declare const FIXED_DISTANCE_BITS = 5;
17
+ export declare const STORED_HEADER_BYTES = 4;
18
+ export declare const UINT16_MASK = 65535;
19
+ export declare const HLIT_BITS = 5;
20
+ export declare const HDIST_BITS = 5;
21
+ export declare const HCLEN_BITS = 4;
22
+ export declare const HLIT_BASE = 257;
23
+ export declare const HDIST_BASE = 1;
24
+ export declare const HCLEN_BASE = 4;
25
+ export declare const CODE_LENGTH_BITS = 3;
26
+ export declare const CODE_LENGTH_SYMBOLS = 19;
27
+ export declare const REPEAT_PREVIOUS = 16;
28
+ export declare const REPEAT_ZERO_SHORT = 17;
29
+ export declare const REPEAT_PREVIOUS_BITS = 2;
30
+ export declare const REPEAT_PREVIOUS_BASE = 3;
31
+ export declare const REPEAT_ZERO_SHORT_BITS = 3;
32
+ export declare const REPEAT_ZERO_SHORT_BASE = 3;
33
+ export declare const REPEAT_ZERO_LONG_BITS = 7;
34
+ export declare const REPEAT_ZERO_LONG_BASE = 11;
35
+ export declare const INITIAL_OUTPUT: number;
36
+ export declare const CODE_LENGTH_ORDER: Uint8Array<ArrayBuffer>;
37
+ export declare const LENGTH_BASE: Uint16Array<ArrayBuffer>;
38
+ export declare const LENGTH_EXTRA: Uint8Array<ArrayBuffer>;
39
+ export declare const DISTANCE_BASE: Uint16Array<ArrayBuffer>;
40
+ export declare const DISTANCE_EXTRA: Uint8Array<ArrayBuffer>;
41
+ export declare const STORED_LENGTH_BYTES = 2;
@@ -0,0 +1,41 @@
1
+ export const MAX_CODE_BITS = 15;
2
+ export const SYMBOL_SHIFT = 4;
3
+ export const LENGTH_MASK = 15;
4
+ export const BYTE_BITS = 8;
5
+ export const REFILL_BELOW = 24;
6
+ export const STORED_BLOCK = 0;
7
+ export const FIXED_BLOCK = 1;
8
+ export const DYNAMIC_BLOCK = 2;
9
+ export const BLOCK_TYPE_BITS = 2;
10
+ export const END_OF_BLOCK = 256;
11
+ export const FIRST_LENGTH_SYMBOL = 257;
12
+ export const LITERAL_LENGTH_SYMBOLS = 288;
13
+ export const DISTANCE_SYMBOLS = 32;
14
+ export const FIXED_LITERAL_BITS = [8, 9, 7, 8];
15
+ export const FIXED_LITERAL_ENDS = [144, 256, 280, 288];
16
+ export const FIXED_DISTANCE_BITS = 5;
17
+ export const STORED_HEADER_BYTES = 4;
18
+ export const UINT16_MASK = 0xffff;
19
+ export const HLIT_BITS = 5;
20
+ export const HDIST_BITS = 5;
21
+ export const HCLEN_BITS = 4;
22
+ export const HLIT_BASE = 257;
23
+ export const HDIST_BASE = 1;
24
+ export const HCLEN_BASE = 4;
25
+ export const CODE_LENGTH_BITS = 3;
26
+ export const CODE_LENGTH_SYMBOLS = 19;
27
+ export const REPEAT_PREVIOUS = 16;
28
+ export const REPEAT_ZERO_SHORT = 17;
29
+ export const REPEAT_PREVIOUS_BITS = 2;
30
+ export const REPEAT_PREVIOUS_BASE = 3;
31
+ export const REPEAT_ZERO_SHORT_BITS = 3;
32
+ export const REPEAT_ZERO_SHORT_BASE = 3;
33
+ export const REPEAT_ZERO_LONG_BITS = 7;
34
+ export const REPEAT_ZERO_LONG_BASE = 11;
35
+ export const INITIAL_OUTPUT = 1 << 16;
36
+ export const CODE_LENGTH_ORDER = Uint8Array.of(16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
37
+ export const LENGTH_BASE = Uint16Array.of(3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258);
38
+ export const LENGTH_EXTRA = Uint8Array.of(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0);
39
+ export const DISTANCE_BASE = Uint16Array.of(1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577);
40
+ export const DISTANCE_EXTRA = Uint8Array.of(0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13);
41
+ export const STORED_LENGTH_BYTES = 2;
@@ -0,0 +1,3 @@
1
+ import type { HuffmanTable } from "./helper-types";
2
+ export declare function huffmanTable(lengths: ArrayLike<number>): HuffmanTable;
3
+ export declare function inflateRaw(data: Uint8Array, expectedSize?: number): Uint8Array;
@@ -0,0 +1,228 @@
1
+ import { BLOCK_TYPE_BITS, BYTE_BITS, CODE_LENGTH_BITS, CODE_LENGTH_ORDER, CODE_LENGTH_SYMBOLS, DISTANCE_BASE, DISTANCE_EXTRA, DISTANCE_SYMBOLS, DYNAMIC_BLOCK, END_OF_BLOCK, FIRST_LENGTH_SYMBOL, FIXED_BLOCK, FIXED_DISTANCE_BITS, FIXED_LITERAL_BITS, FIXED_LITERAL_ENDS, HCLEN_BASE, HCLEN_BITS, HDIST_BASE, HDIST_BITS, HLIT_BASE, HLIT_BITS, INITIAL_OUTPUT, LENGTH_BASE, LENGTH_EXTRA, LENGTH_MASK, LITERAL_LENGTH_SYMBOLS, MAX_CODE_BITS, REFILL_BELOW, REPEAT_PREVIOUS, REPEAT_PREVIOUS_BASE, REPEAT_PREVIOUS_BITS, REPEAT_ZERO_LONG_BASE, REPEAT_ZERO_LONG_BITS, REPEAT_ZERO_SHORT, REPEAT_ZERO_SHORT_BASE, REPEAT_ZERO_SHORT_BITS, STORED_BLOCK, STORED_HEADER_BYTES, STORED_LENGTH_BYTES, SYMBOL_SHIFT, UINT16_MASK, } from "./inflate.constants";
2
+ function reversed(code, bits) {
3
+ let result = 0;
4
+ for (let bit = 0; bit < bits; bit++) {
5
+ result = (result << 1) | ((code >>> bit) & 1);
6
+ }
7
+ return result;
8
+ }
9
+ export function huffmanTable(lengths) {
10
+ const counts = new Uint16Array(MAX_CODE_BITS + 1);
11
+ let longest = 0;
12
+ for (let symbol = 0; symbol < lengths.length; symbol++) {
13
+ const length = lengths[symbol];
14
+ counts[length]++;
15
+ longest = Math.max(longest, length);
16
+ }
17
+ counts[0] = 0;
18
+ const next = new Uint16Array(MAX_CODE_BITS + 1);
19
+ let code = 0;
20
+ for (let bits = 1; bits <= MAX_CODE_BITS; bits++) {
21
+ code = (code + counts[bits - 1]) << 1;
22
+ next[bits] = code;
23
+ if (code + counts[bits] > 1 << bits) {
24
+ throw new Error("The deflate stream is corrupt: a Huffman code is over-subscribed");
25
+ }
26
+ }
27
+ const entries = new Int32Array(1 << longest);
28
+ for (let symbol = 0; symbol < lengths.length; symbol++) {
29
+ const length = lengths[symbol];
30
+ if (length) {
31
+ for (let slot = reversed(next[length]++, length); slot < entries.length; slot += 1 << length) {
32
+ entries[slot] = (symbol << SYMBOL_SHIFT) | length;
33
+ }
34
+ }
35
+ }
36
+ return { entries, bits: longest };
37
+ }
38
+ function fixedLiteralLengths() {
39
+ const lengths = new Uint8Array(LITERAL_LENGTH_SYMBOLS);
40
+ let start = 0;
41
+ FIXED_LITERAL_ENDS.forEach((end, range) => {
42
+ lengths.fill(FIXED_LITERAL_BITS[range], start, end);
43
+ start = end;
44
+ });
45
+ return lengths;
46
+ }
47
+ let fixedTables;
48
+ function fixed() {
49
+ fixedTables !== null && fixedTables !== void 0 ? fixedTables : (fixedTables = [huffmanTable(fixedLiteralLengths()), huffmanTable(new Uint8Array(DISTANCE_SYMBOLS).fill(FIXED_DISTANCE_BITS))]);
50
+ return fixedTables;
51
+ }
52
+ class Inflater {
53
+ constructor(data, expected) {
54
+ this.data = data;
55
+ this.expected = expected;
56
+ this.at = 0;
57
+ this.bitBuffer = 0;
58
+ this.bitCount = 0;
59
+ this.size = 0;
60
+ this.output = new Uint8Array(expected !== null && expected !== void 0 ? expected : Math.max(INITIAL_OUTPUT, data.length * 2));
61
+ }
62
+ run() {
63
+ let last = 0;
64
+ while (!last) {
65
+ last = this.bits(1);
66
+ const type = this.bits(BLOCK_TYPE_BITS);
67
+ if (type === STORED_BLOCK) {
68
+ this.stored();
69
+ }
70
+ else if (type === FIXED_BLOCK) {
71
+ const [literals, distances] = fixed();
72
+ this.compressed(literals, distances);
73
+ }
74
+ else if (type === DYNAMIC_BLOCK) {
75
+ const [literals, distances] = this.dynamicTables();
76
+ this.compressed(literals, distances);
77
+ }
78
+ else {
79
+ throw new Error("The deflate stream is corrupt: a block has the reserved type 3");
80
+ }
81
+ }
82
+ if (this.expected !== undefined && this.size !== this.expected) {
83
+ throw new Error(`The deflate stream holds ${this.size} bytes, not the ${this.expected} expected`);
84
+ }
85
+ return this.size === this.output.length ? this.output : this.output.slice(0, this.size);
86
+ }
87
+ fill(count) {
88
+ while (this.bitCount < count) {
89
+ if (this.at >= this.data.length) {
90
+ throw new Error("The deflate stream ends in the middle of a block");
91
+ }
92
+ this.bitBuffer |= this.data[this.at++] << this.bitCount;
93
+ this.bitCount += BYTE_BITS;
94
+ }
95
+ }
96
+ bits(count) {
97
+ if (count === 0) {
98
+ return 0;
99
+ }
100
+ this.fill(count);
101
+ const value = this.bitBuffer & ((1 << count) - 1);
102
+ this.bitBuffer >>>= count;
103
+ this.bitCount -= count;
104
+ return value;
105
+ }
106
+ symbol(table) {
107
+ while (this.bitCount < table.bits && this.at < this.data.length && this.bitCount <= REFILL_BELOW) {
108
+ this.bitBuffer |= this.data[this.at++] << this.bitCount;
109
+ this.bitCount += BYTE_BITS;
110
+ }
111
+ const entry = table.entries[this.bitBuffer & ((1 << table.bits) - 1)];
112
+ const length = entry & LENGTH_MASK;
113
+ if (entry === 0 || length > this.bitCount) {
114
+ throw new Error(this.bitCount < table.bits && this.at >= this.data.length ? "The deflate stream ends in the middle of a block" : "The deflate stream is corrupt: a code names no symbol");
115
+ }
116
+ this.bitBuffer >>>= length;
117
+ this.bitCount -= length;
118
+ return entry >> SYMBOL_SHIFT;
119
+ }
120
+ room(count) {
121
+ if (this.size + count <= this.output.length) {
122
+ return;
123
+ }
124
+ if (this.expected !== undefined) {
125
+ throw new Error(`The deflate stream holds more than the ${this.expected} bytes expected`);
126
+ }
127
+ const grown = new Uint8Array(Math.max(this.output.length * 2, this.size + count));
128
+ grown.set(this.output.subarray(0, this.size));
129
+ this.output = grown;
130
+ }
131
+ stored() {
132
+ this.bitBuffer >>>= this.bitCount % BYTE_BITS;
133
+ this.bitCount -= this.bitCount % BYTE_BITS;
134
+ this.at -= this.bitCount / BYTE_BITS;
135
+ this.bitBuffer = 0;
136
+ this.bitCount = 0;
137
+ if (this.at + STORED_HEADER_BYTES > this.data.length) {
138
+ throw new Error("The deflate stream ends in the middle of a block");
139
+ }
140
+ const length = this.uint16(this.at);
141
+ const complement = this.uint16(this.at + STORED_LENGTH_BYTES);
142
+ if ((length ^ UINT16_MASK) !== complement) {
143
+ throw new Error("The deflate stream is corrupt: a stored block's length fails its check");
144
+ }
145
+ this.at += STORED_HEADER_BYTES;
146
+ if (this.at + length > this.data.length) {
147
+ throw new Error("The deflate stream ends in the middle of a block");
148
+ }
149
+ this.room(length);
150
+ this.output.set(this.data.subarray(this.at, this.at + length), this.size);
151
+ this.size += length;
152
+ this.at += length;
153
+ }
154
+ uint16(at) {
155
+ return this.data[at] | (this.data[at + 1] << BYTE_BITS);
156
+ }
157
+ dynamicTables() {
158
+ const literalCount = this.bits(HLIT_BITS) + HLIT_BASE;
159
+ const distanceCount = this.bits(HDIST_BITS) + HDIST_BASE;
160
+ const codeLengthCount = this.bits(HCLEN_BITS) + HCLEN_BASE;
161
+ const codeLengthLengths = new Uint8Array(CODE_LENGTH_SYMBOLS);
162
+ for (let at = 0; at < codeLengthCount; at++) {
163
+ codeLengthLengths[CODE_LENGTH_ORDER[at]] = this.bits(CODE_LENGTH_BITS);
164
+ }
165
+ const codeLengths = huffmanTable(codeLengthLengths);
166
+ const lengths = new Uint8Array(literalCount + distanceCount);
167
+ for (let at = 0; at < lengths.length;) {
168
+ const symbol = this.symbol(codeLengths);
169
+ if (symbol < REPEAT_PREVIOUS) {
170
+ lengths[at++] = symbol;
171
+ continue;
172
+ }
173
+ if (symbol === REPEAT_PREVIOUS && at === 0) {
174
+ throw new Error("The deflate stream is corrupt: a code length repeats before the first");
175
+ }
176
+ const value = symbol === REPEAT_PREVIOUS ? lengths[at - 1] : 0;
177
+ const repeat = symbol === REPEAT_PREVIOUS ? this.bits(REPEAT_PREVIOUS_BITS) + REPEAT_PREVIOUS_BASE
178
+ : symbol === REPEAT_ZERO_SHORT ? this.bits(REPEAT_ZERO_SHORT_BITS) + REPEAT_ZERO_SHORT_BASE
179
+ : this.bits(REPEAT_ZERO_LONG_BITS) + REPEAT_ZERO_LONG_BASE;
180
+ if (at + repeat > lengths.length) {
181
+ throw new Error("The deflate stream is corrupt: code lengths run past their count");
182
+ }
183
+ lengths.fill(value, at, at + repeat);
184
+ at += repeat;
185
+ }
186
+ if (lengths[END_OF_BLOCK] === 0) {
187
+ throw new Error("The deflate stream is corrupt: a block has no end-of-block code");
188
+ }
189
+ return [huffmanTable(lengths.subarray(0, literalCount)), huffmanTable(lengths.subarray(literalCount))];
190
+ }
191
+ compressed(literals, distances) {
192
+ for (;;) {
193
+ const symbol = this.symbol(literals);
194
+ if (symbol < END_OF_BLOCK) {
195
+ this.room(1);
196
+ this.output[this.size++] = symbol;
197
+ continue;
198
+ }
199
+ if (symbol === END_OF_BLOCK) {
200
+ return;
201
+ }
202
+ const lengthCode = symbol - FIRST_LENGTH_SYMBOL;
203
+ if (lengthCode >= LENGTH_BASE.length) {
204
+ throw new Error("The deflate stream is corrupt: a length code is out of range");
205
+ }
206
+ const length = LENGTH_BASE[lengthCode] + this.bits(LENGTH_EXTRA[lengthCode]);
207
+ const distanceCode = this.symbol(distances);
208
+ if (distanceCode >= DISTANCE_BASE.length) {
209
+ throw new Error("The deflate stream is corrupt: a distance code is out of range");
210
+ }
211
+ const distance = DISTANCE_BASE[distanceCode] + this.bits(DISTANCE_EXTRA[distanceCode]);
212
+ if (distance > this.size) {
213
+ throw new Error("The deflate stream is corrupt: a distance reaches before the start");
214
+ }
215
+ this.room(length);
216
+ const output = this.output;
217
+ let from = this.size - distance;
218
+ const end = this.size + length;
219
+ for (let to = this.size; to < end; to++) {
220
+ output[to] = output[from++];
221
+ }
222
+ this.size = end;
223
+ }
224
+ }
225
+ }
226
+ export function inflateRaw(data, expectedSize) {
227
+ return new Inflater(data, expectedSize).run();
228
+ }
@@ -0,0 +1,2 @@
1
+ export declare const RIGID_TOLERANCE = 1e-7;
2
+ export declare const LINEAR_COLUMN_STARTS: readonly [0, 4, 8];
@@ -0,0 +1,2 @@
1
+ export const RIGID_TOLERANCE = 1e-7;
2
+ export const LINEAR_COLUMN_STARTS = [0, 4, 8];