@d0paminedriven/fs 6.3.0 → 7.0.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.
package/dist/fs/index.cjs CHANGED
@@ -43,8 +43,136 @@ var dotenv = __toESM(require("dotenv"), 1);
43
43
  var import_dotenv_expand = __toESM(require("dotenv-expand"), 1);
44
44
  var import_sharp = __toESM(require("sharp"), 1);
45
45
 
46
+ // src/utils/index.ts
47
+ var UtilsService = class {
48
+ static {
49
+ __name(this, "UtilsService");
50
+ }
51
+ chunkArray(arr, maxChunkLength) {
52
+ const chunks = Array.of();
53
+ let currentChunkLength = 0;
54
+ let currentChunk = Array.of();
55
+ for (const [index, val] of arr.entries()) {
56
+ if (val.length + currentChunkLength >= maxChunkLength) {
57
+ if (currentChunk.length) {
58
+ chunks.push(currentChunk);
59
+ }
60
+ currentChunkLength = val.length;
61
+ currentChunk = [val];
62
+ } else {
63
+ currentChunk.push(val);
64
+ currentChunkLength += val.length + 1;
65
+ }
66
+ if (arr.length === index + 1) {
67
+ chunks.push(currentChunk);
68
+ }
69
+ }
70
+ return chunks.length ? chunks : [arr];
71
+ }
72
+ b64ToBlob(b64Data) {
73
+ {
74
+ const sliceSize = 512;
75
+ const typeMatch = /^data:((?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*);base64,(.+)$/i.exec(
76
+ b64Data
77
+ );
78
+ if (!typeMatch) {
79
+ throw new Error(`${b64Data} is not a valid data Url`);
80
+ }
81
+ const type = typeMatch[1];
82
+ const base64Data = typeMatch?.[2] ?? "";
83
+ const byteCharacters = Buffer.from(base64Data, "base64").toString(
84
+ "binary"
85
+ );
86
+ const byteArrays = Array.of();
87
+ for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
88
+ const slice = byteCharacters.slice(offset, offset + sliceSize);
89
+ const byteNumbers = new Array(slice.length);
90
+ for (let i = 0; i < slice.length; i++) {
91
+ byteNumbers[i] = slice.charCodeAt(i);
92
+ }
93
+ const byteArray = new Uint8Array(byteNumbers);
94
+ byteArrays.push(byteArray);
95
+ }
96
+ const blob = new Blob(byteArrays, { type });
97
+ return blob;
98
+ }
99
+ }
100
+ arrToArrOfArrs = /* @__PURE__ */ __name(({
101
+ arrToFragment = Array.of(),
102
+ arrOfArrsAggregator = Array.of(),
103
+ interval
104
+ }) => new Promise(
105
+ (resolve2, _reject) => resolve2(
106
+ ((interval2) => {
107
+ for (let i = 0; i <= arrToFragment.length; i++) {
108
+ if ((i % interval2 === 0 || i === 0) && i <= arrToFragment.length) {
109
+ let segment = arrToFragment.slice(i, i + interval2);
110
+ arrOfArrsAggregator.push(segment);
111
+ }
112
+ }
113
+ })(interval)
114
+ )
115
+ ).then((_) => arrOfArrsAggregator), "arrToArrOfArrs");
116
+ extractTuple(obj, props) {
117
+ return [props, obj[props]];
118
+ }
119
+ sort(obj, order) {
120
+ return Object.fromEntries(
121
+ Object.entries(obj).sort(
122
+ ([a, _aa], [b, _bb]) => order === "DESC" ? b.localeCompare(a) - a.localeCompare(b) : a.localeCompare(b) - b.localeCompare(a)
123
+ )
124
+ );
125
+ }
126
+ excludeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
127
+ const resolve2 = Object.fromEntries(
128
+ Object.entries(obj).map(([key, val]) => {
129
+ if (props.includes(key)) {
130
+ return ["omit", "omit"];
131
+ } else return [key, val];
132
+ }).filter(([t, _v]) => /omit/.test(t) === false)
133
+ );
134
+ return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
135
+ }, "excludeTargeted");
136
+ includeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
137
+ const resolve2 = Object.fromEntries(
138
+ props.map((val) => this.extractTuple(obj, val))
139
+ );
140
+ return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
141
+ }, "includeTargeted");
142
+ countsSorter = /* @__PURE__ */ __name((counter, keySort = "ASC", valSort = "DESC") => Object.fromEntries(
143
+ Array.from(Object.entries(counter)).sort(
144
+ ([aStr, _aNum], [bStr, _bNum]) => keySort === "DESC" ? bStr.localeCompare(aStr) - aStr.localeCompare(bStr) : aStr.localeCompare(bStr) - bStr.localeCompare(aStr)
145
+ ).sort(([aStr, aNum], [bStr, bNum]) => {
146
+ if (typeof (aNum && bNum) === "number") {
147
+ return valSort === "ASC" ? aNum - bNum : bNum - aNum;
148
+ } else if (typeof (aNum && bNum) === "string") {
149
+ return valSort === "ASC" ? aStr.localeCompare(bStr) - bStr.localeCompare(aStr) : bStr.localeCompare(aStr) - aStr.localeCompare(bStr);
150
+ } else
151
+ throw new Error(
152
+ "only Record<string, string> and Record<string,number> types supported"
153
+ );
154
+ })
155
+ ), "countsSorter");
156
+ range(from, to) {
157
+ const values = Array.of();
158
+ for (let i = from; i < to; i++) {
159
+ values.push(i);
160
+ }
161
+ return values;
162
+ }
163
+ isPrimeNumber = /* @__PURE__ */ __name((n) => {
164
+ for (let i = 2; n > i; i++) {
165
+ if (n % i === 0) {
166
+ return false;
167
+ }
168
+ }
169
+ return n > 1;
170
+ }, "isPrimeNumber");
171
+ acceptor = /* @__PURE__ */ __name((num) => [...Array(num !== 0 ? num : this.range(num = -101, num = 101)).keys()].reverse().filter(this.isPrimeNumber), "acceptor");
172
+ };
173
+
46
174
  // src/url/index.ts
47
- var UrlService = class {
175
+ var UrlService = class extends UtilsService {
48
176
  static {
49
177
  __name(this, "UrlService");
50
178
  }
@@ -331,6 +459,21 @@ var MimeService = class extends UrlService {
331
459
  );
332
460
  }
333
461
  }
462
+ mimeToExt(mime) {
463
+ const extensions = this.toExtObj[mime];
464
+ if (extensions.length === 1) return extensions[0];
465
+ if (extensions.length === 2) {
466
+ return extensions[0];
467
+ }
468
+ if (extensions.length === 3) {
469
+ if (mime === "audio/ogg") return "ogg";
470
+ return extensions[0];
471
+ }
472
+ if (extensions.length === 5) {
473
+ return "sse";
474
+ }
475
+ return extensions[0];
476
+ }
334
477
  assetType(url) {
335
478
  const parsed = this.parseUrl(url);
336
479
  if (!parsed) return void 0;
@@ -370,7 +513,7 @@ var unitsObj = {
370
513
  };
371
514
 
372
515
  // src/fs/index.ts
373
- dotenv.config();
516
+ dotenv.config({ quiet: true });
374
517
  var Fs = class extends MimeService {
375
518
  constructor(cwd) {
376
519
  super();
@@ -690,7 +833,6 @@ var Fs = class extends MimeService {
690
833
  if (!response.ok) {
691
834
  throw new Error(`Failed to fetch asset: ${response.statusText}`);
692
835
  }
693
- console.log(Object.fromEntries(Array.from(response.headers.entries())));
694
836
  const reader = response.body?.getReader();
695
837
  if (reader) {
696
838
  const chunks = Array.of();
@@ -757,6 +899,38 @@ var Fs = class extends MimeService {
757
899
  return console.error(err);
758
900
  }
759
901
  }
902
+ parseExif(buffer, app1Pos, _app1Size) {
903
+ const exifHeader = buffer.toString("ascii", app1Pos + 2, app1Pos + 8);
904
+ if (exifHeader !== "Exif\0\0")
905
+ return { orientation: null, dateTimeOriginal: null };
906
+ let tiffPos = app1Pos + 8;
907
+ const byteOrder = buffer.toString("ascii", tiffPos, tiffPos + 2);
908
+ const littleEndian = byteOrder === "II";
909
+ const readUInt16 = littleEndian ? buffer.readUInt16LE.bind(buffer) : buffer.readUInt16BE.bind(buffer);
910
+ const readUInt32 = littleEndian ? buffer.readUInt32LE.bind(buffer) : buffer.readUInt32BE.bind(buffer);
911
+ if (readUInt16(tiffPos + 2) !== 42)
912
+ return { orientation: null, dateTimeOriginal: null };
913
+ let ifdOffset = readUInt32(tiffPos + 4);
914
+ tiffPos += ifdOffset;
915
+ const numEntries = readUInt16(tiffPos);
916
+ tiffPos += 2;
917
+ let orientation = null;
918
+ let dateTimeOriginal = null;
919
+ for (let i = 0; i < numEntries; i++) {
920
+ const tag = readUInt16(tiffPos);
921
+ const type = readUInt16(tiffPos + 2);
922
+ const count = readUInt32(tiffPos + 4);
923
+ const valueOffset = tiffPos + 8;
924
+ if (tag === 274 && type === 3 && count === 1) {
925
+ orientation = readUInt16(valueOffset);
926
+ } else if (tag === 36867 && type === 2 && count === 20) {
927
+ const offset = readUInt32(valueOffset);
928
+ dateTimeOriginal = buffer.toString("ascii", app1Pos + 2 + offset, app1Pos + 2 + offset + 19).trim();
929
+ }
930
+ tiffPos += 12;
931
+ }
932
+ return { orientation, dateTimeOriginal };
933
+ }
760
934
  /**
761
935
  *
762
936
  * @param inputUrl remote url to fetch data from
@@ -837,20 +1011,82 @@ var Fs = class extends MimeService {
837
1011
  }
838
1012
  return null;
839
1013
  }
840
- async getImageSize(filePath) {
1014
+ async getImageSpecs(filePath) {
841
1015
  const buffer = await this.fileToBufferAsync(filePath);
842
1016
  if (buffer?.length >= 24 && buffer?.[0] === 137 && buffer?.[1] === 80 && buffer?.[2] === 78 && buffer?.[3] === 71 && buffer?.[4] === 13 && buffer?.[5] === 10 && buffer?.[6] === 26 && buffer?.[7] === 10) {
843
1017
  if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
844
1018
  throw new Error("IHDR Chunk of png not found or incorrect.");
845
1019
  }
1020
+ const colorType = buffer[25];
1021
+ let colorSpace;
1022
+ let hasAlpha = false;
1023
+ switch (colorType) {
1024
+ case 0:
1025
+ colorSpace = "grayscale";
1026
+ break;
1027
+ case 2:
1028
+ colorSpace = "rgb";
1029
+ break;
1030
+ case 3:
1031
+ colorSpace = "indexed";
1032
+ break;
1033
+ case 4:
1034
+ colorSpace = "grayscale-alpha";
1035
+ hasAlpha = true;
1036
+ break;
1037
+ case 6:
1038
+ colorSpace = "rgba";
1039
+ hasAlpha = true;
1040
+ break;
1041
+ default:
1042
+ colorSpace = "unknown";
1043
+ }
1044
+ const width = buffer.readUInt32BE(16);
1045
+ const height = buffer.readUInt32BE(20);
1046
+ let frames = 1;
1047
+ let animated = false;
1048
+ let iccProfile = null;
1049
+ let exifDateTimeOriginal = null;
1050
+ let orientation = null;
1051
+ let pos = 33;
1052
+ while (pos < buffer.length - 12) {
1053
+ const chunkLen = buffer.readUInt32BE(pos);
1054
+ const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
1055
+ const chunkData = pos + 8;
1056
+ if (chunkType === "acTL") {
1057
+ animated = true;
1058
+ frames = buffer.readUInt32BE(chunkData);
1059
+ } else if (chunkType === "iCCP") {
1060
+ const nameEnd = buffer.indexOf(0, chunkData);
1061
+ const profileName = buffer.toString("ascii", chunkData, nameEnd);
1062
+ iccProfile = profileName || "embedded";
1063
+ } else if (chunkType === "tIME") {
1064
+ const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
1065
+ if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
1066
+ const year = buffer.readUInt16BE(chunkData);
1067
+ exifDateTimeOriginal = `${year}:${month.toString().padStart(2, "0")}:${day.toString().padStart(2, "0")} ${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}:${second.toString().padStart(2, "0")}`;
1068
+ }
1069
+ } else if (chunkType === "IDAT") {
1070
+ break;
1071
+ }
1072
+ pos += 12 + chunkLen;
1073
+ }
846
1074
  return {
847
- width: buffer.readUInt32BE(16),
848
- height: buffer.readUInt32BE(20),
849
- type: "png"
1075
+ width,
1076
+ height,
1077
+ format: "png",
1078
+ frames,
1079
+ animated,
1080
+ hasAlpha,
1081
+ orientation,
1082
+ aspectRatio: width / height,
1083
+ colorSpace,
1084
+ iccProfile,
1085
+ exifDateTimeOriginal
850
1086
  };
851
1087
  }
852
1088
  if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
853
- let pos = 2;
1089
+ let pos = 2, colorSpace = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
854
1090
  while (pos < buffer.length - 10) {
855
1091
  if (buffer[pos] !== 255) {
856
1092
  throw new Error("Invalid JPEG file");
@@ -862,34 +1098,148 @@ var Fs = class extends MimeService {
862
1098
  throw new Error("no marker for jpeg");
863
1099
  }
864
1100
  if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
865
- return {
866
- width: buffer.readUInt16BE(pos + 7),
867
- height: buffer.readUInt16BE(pos + 5),
868
- type: "jpeg"
869
- };
1101
+ const numComponents = buffer[pos + 4];
1102
+ switch (numComponents) {
1103
+ case 1:
1104
+ colorSpace = "grayscale";
1105
+ break;
1106
+ case 3:
1107
+ colorSpace = "ycbcr";
1108
+ break;
1109
+ // Most common for RGB JPEGs (stored as YCbCr)
1110
+ case 4:
1111
+ colorSpace = "ycck";
1112
+ break;
1113
+ // YCCK for CMYK with alpha-like, but no true alpha
1114
+ default:
1115
+ colorSpace = "unknown";
1116
+ }
1117
+ width = buffer.readUInt16BE(pos + 7);
1118
+ height = buffer.readUInt16BE(pos + 5);
1119
+ } else if (marker === 225) {
1120
+ const { orientation: ori, dateTimeOriginal } = this.parseExif(
1121
+ buffer,
1122
+ pos,
1123
+ segmentSize
1124
+ );
1125
+ orientation = ori;
1126
+ exifDateTimeOriginal = dateTimeOriginal;
1127
+ } else if (marker === 226) {
1128
+ const iccHeader = buffer.toString("ascii", pos + 4, pos + 18);
1129
+ if (iccHeader.startsWith("ICC_PROFILE")) {
1130
+ iccProfile = "embedded";
1131
+ }
870
1132
  }
871
1133
  pos += segmentSize + 2;
872
1134
  }
873
- throw new Error("No dimensions found in JPEG file");
1135
+ if (width === 0) throw new Error("No dimensions found in JPEG file");
1136
+ return {
1137
+ width,
1138
+ height,
1139
+ format: "jpeg",
1140
+ frames: 1,
1141
+ animated: false,
1142
+ // JPEG not animated
1143
+ hasAlpha,
1144
+ orientation,
1145
+ aspectRatio: width / height,
1146
+ colorSpace,
1147
+ iccProfile,
1148
+ exifDateTimeOriginal
1149
+ };
874
1150
  }
875
1151
  const gifHeader = buffer.toString("ascii", 0, 6);
876
1152
  if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) {
1153
+ const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
1154
+ let frames = 0;
1155
+ let pos = 13;
1156
+ let bufTen = buffer?.[10];
1157
+ if (typeof bufTen === "undefined") {
1158
+ pos = 13;
1159
+ frames = 0;
1160
+ } else if (bufTen & 128) pos += 3 << (bufTen & 7) + 1;
1161
+ while (pos < buffer.length) {
1162
+ if (buffer[pos] === 33) {
1163
+ pos += 2;
1164
+ let subSize = buffer?.[pos];
1165
+ while (subSize && subSize > 0) {
1166
+ pos += subSize + 1;
1167
+ subSize = buffer?.[pos];
1168
+ }
1169
+ pos++;
1170
+ } else if (buffer?.[pos] && buffer?.[pos] === 44) {
1171
+ frames++;
1172
+ pos += 10;
1173
+ let b = buffer?.[pos - 1];
1174
+ if (typeof b !== "undefined" && b & 128)
1175
+ pos += 3 << (b & 7) + 1;
1176
+ pos++;
1177
+ let dataSize = buffer?.[pos];
1178
+ while (dataSize && dataSize > 0) {
1179
+ pos += dataSize + 1;
1180
+ dataSize = buffer[pos];
1181
+ }
1182
+ pos++;
1183
+ } else if (buffer[pos] === 59) {
1184
+ break;
1185
+ } else {
1186
+ pos++;
1187
+ }
1188
+ }
877
1189
  return {
878
- width: buffer.readUInt16LE(6),
879
- height: buffer.readUInt16LE(8),
880
- type: "gif"
1190
+ width,
1191
+ height,
1192
+ format: "gif",
1193
+ frames,
1194
+ animated: frames > 1,
1195
+ hasAlpha: null,
1196
+ // GIF transparency is per-pixel binary, not full alpha; set null or true if transparent color exists, but complex
1197
+ orientation: null,
1198
+ // No orientation in GIF
1199
+ aspectRatio: width / height,
1200
+ colorSpace: "indexed",
1201
+ iccProfile: null,
1202
+ // No ICC in GIF
1203
+ exifDateTimeOriginal: null
1204
+ // No EXIF in GIF
881
1205
  };
882
1206
  }
883
- if (buffer.length >= 26 && buffer[0] === 66 && buffer[1] === 77) {
1207
+ if (buffer.length >= 26 && buffer?.[0] === 66 && buffer?.[1] === 77) {
1208
+ const width = buffer.readInt32LE(18);
1209
+ const height = Math.abs(buffer.readInt32LE(22));
1210
+ const bitDepth = buffer.readUInt16LE(28);
1211
+ let colorSpace = bitDepth <= 8 ? "indexed" : "rgb";
884
1212
  return {
885
- width: buffer.readInt32LE(18),
886
- height: Math.abs(buffer.readInt32LE(22)),
887
- type: "bmp"
1213
+ width,
1214
+ height,
1215
+ format: "bmp",
1216
+ frames: 1,
1217
+ animated: false,
1218
+ hasAlpha: null,
1219
+ // Some BMP have alpha in 32bpp, check if bitDepth===32 && compression===3 (bitfields with alpha)
1220
+ orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
1221
+ // Negative height means top-down (orientation 1), positive bottom-up (like 6, but simplified)
1222
+ aspectRatio: width / height,
1223
+ colorSpace,
1224
+ iccProfile: null,
1225
+ // Rare in BMP
1226
+ exifDateTimeOriginal: null
888
1227
  };
889
1228
  }
890
1229
  if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
891
1230
  const chunkType = buffer.toString("ascii", 12, 16);
1231
+ let colorSpace = "unknown";
1232
+ let hasAlpha = false;
1233
+ let width = 0;
1234
+ let height = 0;
1235
+ let frames = 1;
1236
+ let animated = false;
1237
+ let iccProfile = null;
892
1238
  if (chunkType === "VP8X") {
1239
+ const flags = buffer?.[20];
1240
+ colorSpace = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
1241
+ hasAlpha = flags ? !!(flags & 2) : false;
1242
+ animated = flags ? !!(flags & 1) : false;
893
1243
  let widthSubtractOne = 0;
894
1244
  let heightSubtractOne = 0;
895
1245
  if (buffer?.[24] && buffer?.[25] && buffer?.[26]) {
@@ -898,48 +1248,70 @@ var Fs = class extends MimeService {
898
1248
  if (buffer?.[27] && buffer?.[28] && buffer?.[29]) {
899
1249
  heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
900
1250
  }
901
- return {
902
- width: widthSubtractOne + 1,
903
- height: heightSubtractOne + 1,
904
- type: "webp"
905
- };
1251
+ width = widthSubtractOne + 1;
1252
+ height = heightSubtractOne + 1;
1253
+ if (animated) {
1254
+ let pos = 30;
1255
+ frames = 0;
1256
+ while (pos < buffer.length - 8) {
1257
+ const subChunkType = buffer.toString("ascii", pos, pos + 4);
1258
+ const subChunkSize = buffer.readUInt32LE(pos + 4);
1259
+ if (subChunkType === "ICCP") {
1260
+ iccProfile = "embedded";
1261
+ } else if (subChunkType === "ANMF") {
1262
+ frames++;
1263
+ }
1264
+ pos += 8 + subChunkSize + subChunkSize % 2;
1265
+ }
1266
+ }
906
1267
  } else if (chunkType === "VP8 ") {
1268
+ colorSpace = "rgb";
1269
+ hasAlpha = false;
907
1270
  const dataStart = 20;
908
- if (buffer[dataStart + 3] !== 157 || buffer[dataStart + 4] !== 1 || buffer[dataStart + 5] !== 42) {
1271
+ if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) {
909
1272
  throw new Error("Invalid VP8 keyframe");
910
1273
  }
911
- const width = buffer.readUInt16LE(dataStart + 6) & 16383;
912
- const height = buffer.readUInt16LE(dataStart + 8) & 16383;
913
- return {
914
- width,
915
- height,
916
- type: "webp"
917
- };
1274
+ width = buffer.readUInt16LE(dataStart + 6) & 16383;
1275
+ height = buffer.readUInt16LE(dataStart + 8) & 16383;
918
1276
  } else if (chunkType === "VP8L") {
919
1277
  const dataStart = 20;
920
- if (buffer[dataStart] !== 47) {
1278
+ if (buffer?.[dataStart] !== 47) {
921
1279
  throw new Error("Invalid VP8L signature");
922
1280
  }
923
1281
  const bits = buffer.readUInt32LE(dataStart + 1);
924
1282
  if (bits >>> 29 !== 0) {
925
1283
  throw new Error("Invalid VP8L version");
926
1284
  }
927
- const width = 1 + (bits & 16383);
928
- const height = 1 + (bits >> 14 & 16383);
929
- return {
930
- width,
931
- height,
932
- type: "webp"
933
- };
1285
+ colorSpace = bits & 1 << 8 ? "rgba" : "rgb";
1286
+ hasAlpha = !!(bits & 1 << 8);
1287
+ width = 1 + (bits & 16383);
1288
+ height = 1 + (bits >> 14 & 16383);
1289
+ } else {
1290
+ throw new Error("Unsupported WebP chunk");
934
1291
  }
935
- throw new Error("Unsupported WebP chunk");
1292
+ return {
1293
+ width,
1294
+ height,
1295
+ format: "webp",
1296
+ frames,
1297
+ animated,
1298
+ hasAlpha,
1299
+ orientation: null,
1300
+ // No standard orientation in WebP
1301
+ aspectRatio: width / height,
1302
+ colorSpace,
1303
+ iccProfile,
1304
+ exifDateTimeOriginal: null
1305
+ // Can have EXIF chunk, but rare; add if needed
1306
+ };
936
1307
  }
937
1308
  if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
938
1309
  const ftyp = this.findBox(buffer, "ftyp");
939
1310
  if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
940
1311
  const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
941
- if (!brands.includes("avif") && !brands.includes("avis"))
942
- throw new Error("Not an AVIF file");
1312
+ const isAvif = brands.includes("avif");
1313
+ const isAvis = brands.includes("avis");
1314
+ if (!isAvif && !isAvis) throw new Error("Not an AVIF file");
943
1315
  const meta = this.findBox(buffer, "meta");
944
1316
  if (!meta) throw new Error("Invalid AVIF: No meta");
945
1317
  const metaSubStart = meta.pos + 4;
@@ -953,10 +1325,54 @@ var Fs = class extends MimeService {
953
1325
  if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
954
1326
  const width = buffer.readUInt32BE(ispe.pos + 4);
955
1327
  const height = buffer.readUInt32BE(ispe.pos + 8);
1328
+ let colorSpace = "rgb";
1329
+ let hasAlpha = false;
1330
+ let iccProfile = null;
1331
+ const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
1332
+ if (colr) {
1333
+ const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
1334
+ if (colrType === "nclx") {
1335
+ const matrix = buffer.readUInt16BE(colr.pos + 6);
1336
+ colorSpace = matrix === 2 ? "rgb" : "ycbcr";
1337
+ } else if (colrType === "rICC" || colrType === "prof") {
1338
+ colorSpace = "unknown";
1339
+ iccProfile = "embedded";
1340
+ }
1341
+ }
1342
+ const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
1343
+ if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
1344
+ hasAlpha = true;
1345
+ if (colorSpace === "rgb" || colorSpace === "ycbcr") {
1346
+ colorSpace = colorSpace === "rgb" ? "rgba" : "ycck";
1347
+ } else if (colorSpace === "unknown") {
1348
+ colorSpace = "grayscale-alpha";
1349
+ }
1350
+ }
1351
+ let frames = 1;
1352
+ let animated = isAvis;
1353
+ if (animated) {
1354
+ const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
1355
+ if (iloc) {
1356
+ const version = buffer?.[iloc.pos];
1357
+ const itemCountPos = iloc.pos + (version ? version < 2 ? 4 : 6 : 4);
1358
+ frames = buffer.readUInt16BE(itemCountPos);
1359
+ }
1360
+ }
956
1361
  return {
957
1362
  width,
958
1363
  height,
959
- type: "avif"
1364
+ format: "avif",
1365
+ frames,
1366
+ animated,
1367
+ hasAlpha: hasAlpha ? true : null,
1368
+ // null if unknown
1369
+ orientation: null,
1370
+ // Can have 'irot' or 'imir' transforms, but rare
1371
+ aspectRatio: width / height,
1372
+ colorSpace,
1373
+ iccProfile,
1374
+ exifDateTimeOriginal: null
1375
+ // Can have 'XMP' box, but parse XMP for date if needed
960
1376
  };
961
1377
  }
962
1378
  throw new Error("Unsupported image format or invalid file");
@@ -1004,7 +1420,6 @@ var Fs = class extends MimeService {
1004
1420
  writeTmp(filename, data, options) {
1005
1421
  const fullPath = (0, import_path.resolve)(this.tmpDir, filename);
1006
1422
  Promise.resolve(this.withWs(fullPath, data, options));
1007
- return fullPath;
1008
1423
  }
1009
1424
  /**
1010
1425
  * Read a file from the tmp directory
@@ -1048,9 +1463,11 @@ var Fs = class extends MimeService {
1048
1463
  if (!pattern) {
1049
1464
  return tmpContents;
1050
1465
  }
1466
+ if (typeof pattern === "string") {
1467
+ return tmpContents.filter((t) => t.includes(pattern));
1468
+ }
1051
1469
  return tmpContents.filter((path) => {
1052
- const filename = path.includes("/") ? path.split("/").pop() ?? path : path;
1053
- return filename.includes(pattern) || path.startsWith(pattern);
1470
+ return pattern.test(path);
1054
1471
  });
1055
1472
  }
1056
1473
  /**
@@ -1136,6 +1553,10 @@ var Fs = class extends MimeService {
1136
1553
  }
1137
1554
  return removed;
1138
1555
  }
1556
+ rmTmpFile(filename) {
1557
+ const tmpPath = (0, import_path.resolve)(this.tmpDir, filename);
1558
+ this.rmFile(tmpPath);
1559
+ }
1139
1560
  /**
1140
1561
  * Generate a unique tmp filename with optional prefix
1141
1562
  * @param prefix Optional prefix for the filename
@@ -1154,5 +1575,56 @@ var Fs = class extends MimeService {
1154
1575
  const parts = [prefix ?? "tmp", timestamp, random].join("-");
1155
1576
  return extension ? `${parts}.${extension}` : parts;
1156
1577
  }
1578
+ /**
1579
+ * Async generator for cleaning tmp files with batched removal
1580
+ * @param pattern Pattern to match files for deletion
1581
+ * @param batchSize Number of files to remove per batch (default 10)
1582
+ * @yields Progress information during cleanup
1583
+ * @returns Final cleanup summary
1584
+ * @example
1585
+ * ```ts
1586
+ * const fs = new Fs(process.cwd());
1587
+ * // Clean up with progress tracking
1588
+ * for await (const progress of fs.cleanTmpGenerator("session-", 25)) {
1589
+ * console.log(progress);
1590
+ * }
1591
+ * ```
1592
+ */
1593
+ async *cleanTmpGenerator(pattern, batchSize = 10) {
1594
+ const files = this.scanTmp(pattern);
1595
+ const batches = await this.arrToArrOfArrs({
1596
+ arrToFragment: files,
1597
+ arrOfArrsAggregator: [],
1598
+ interval: batchSize
1599
+ });
1600
+ let totalRemoved = 0;
1601
+ for (const [index, batch] of batches.entries()) {
1602
+ yield {
1603
+ action: "removing",
1604
+ batch: [...batch],
1605
+ batchNumber: index + 1,
1606
+ totalBatches: batches.length,
1607
+ totalFound: files.length,
1608
+ removed: totalRemoved
1609
+ };
1610
+ for (const file of batch) {
1611
+ this.rmTmpFile(file);
1612
+ totalRemoved++;
1613
+ }
1614
+ yield {
1615
+ action: "batch-complete",
1616
+ batchNumber: index + 1,
1617
+ batchSize: batch.length,
1618
+ totalRemoved,
1619
+ remaining: files.length - totalRemoved
1620
+ };
1621
+ }
1622
+ return {
1623
+ action: "complete",
1624
+ totalRemoved,
1625
+ totalBatches: batches.length,
1626
+ pattern: pattern.toString()
1627
+ };
1628
+ }
1157
1629
  };
1158
1630
  //# sourceMappingURL=index.cjs.map