@d0paminedriven/fs 0.1.0 → 1.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.
@@ -319,11 +319,22 @@ var Fs = class {
319
319
  /* end url */
320
320
  get gzVal() {
321
321
  return ["application/x-gzip", "application/gzip"].reduce(
322
- (vals) => vals
322
+ (union) => union
323
+ );
324
+ }
325
+ get objVal() {
326
+ return ["application/octet-stream", "text/plain", "model/obj"].reduce((union) => union);
327
+ }
328
+ get tsVal() {
329
+ return ["text/typescript", "video/mp2t", "video/vnd.dlna.mpeg-tts"].reduce((union) => union);
330
+ }
331
+ get ttfVal() {
332
+ return ["application/font-sfnt", "font/ttf"].reduce(
333
+ (union) => union
323
334
  );
324
335
  }
325
336
  get zipVal() {
326
- return ["application/zip", "application/x-zip-compressed"].reduce((vals) => vals);
337
+ return ["application/zip", "application/x-zip-compressed"].reduce((union) => union);
327
338
  }
328
339
  get mimeTypeObj() {
329
340
  return {
@@ -348,6 +359,8 @@ var Fs = class {
348
359
  eot: "application/vnd.ms-fontobject",
349
360
  epub: "application/epub+zip",
350
361
  gif: "image/gif",
362
+ glb: "model/gltf-binary",
363
+ gltf: "model/gltf+json",
351
364
  gz: this.gzVal,
352
365
  htm: "text/html",
353
366
  html: "text/html",
@@ -360,6 +373,8 @@ var Fs = class {
360
373
  json: "application/json",
361
374
  jsonld: "application/ld+json",
362
375
  m3u8: "application/vnd.apple.mpegurl",
376
+ m4a: "audio/mp4",
377
+ m4v: "video/mp4",
363
378
  md: "text/markdown",
364
379
  mdx: "application/x-mdx",
365
380
  mid: "audio/midi",
@@ -370,10 +385,12 @@ var Fs = class {
370
385
  mpeg: "video/mpeg",
371
386
  mpkg: "application/vnd.apple.installer+xml",
372
387
  ndjson: "application/x-ndjson",
388
+ obj: this.objVal,
373
389
  odp: "application/vnd.oasis.opendocument.presentation",
374
390
  ods: "application/vnd.oasis.opendocument.spreadsheet",
375
391
  odt: "application/vnd.oasis.opendocument.text",
376
392
  oga: "audio/ogg",
393
+ ogg: "audio/ogg",
377
394
  ogv: "video/ogg",
378
395
  ogx: "application/ogg",
379
396
  opus: "audio/ogg",
@@ -384,6 +401,8 @@ var Fs = class {
384
401
  pkpass: "application/vnd.apple.pkpass",
385
402
  ppt: "application/vnd.ms-powerpoint",
386
403
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
404
+ py: "text/x-python",
405
+ pyc: "application/x-python-code",
387
406
  rar: "application/vnd.rar",
388
407
  rtf: "application/rtf",
389
408
  sh: "application/x-sh",
@@ -391,10 +410,12 @@ var Fs = class {
391
410
  tar: "application/x-tar",
392
411
  tif: "image/tiff",
393
412
  tiff: "image/tiff",
394
- ts: "video/mp2t",
395
- ttf: "font/ttf",
413
+ ts: this.tsVal,
414
+ ttf: this.ttfVal,
396
415
  txt: "text/plain",
397
416
  vsd: "application/vnd.visio",
417
+ vtt: "text/vtt",
418
+ wasm: "application/wasm",
398
419
  wav: "audio/wav",
399
420
  weba: "video/webm",
400
421
  webp: "image/webp",
@@ -412,18 +433,20 @@ var Fs = class {
412
433
  };
413
434
  }
414
435
  assetType(url) {
415
- return this.parseUrl(url).pathname?.split(/([.])/gim)?.reverse()?.[0];
436
+ const u = this.parseUrl(url);
437
+ return u.pathname?.split(/([.])/gim)?.reverse()?.[0];
416
438
  }
417
439
  getMime(input) {
418
440
  return this.mimeTypeObj[input];
419
441
  }
442
+ hmm = /* @__PURE__ */ __name((input) => {
443
+ return this.mimeTypeObj[input];
444
+ }, "hmm");
420
445
  async assetToBuffer(path) {
421
446
  const [fetcher] = await Promise.all([
422
447
  fetch(path).then((t) => t.arrayBuffer())
423
448
  ]);
424
- const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(
425
- Buffer.from(fetcher).toJSON().data
426
- ).toString("base64")}`;
449
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(fetcher).toString("base64")}`;
427
450
  const extension = this.assetType(path);
428
451
  return {
429
452
  b64encodedData,
@@ -431,27 +454,34 @@ var Fs = class {
431
454
  };
432
455
  }
433
456
  async assetToBufferView(path) {
434
- const [fetcher] = await Promise.all([
435
- fetch(path).then((t) => t.arrayBuffer())
436
- ]);
437
- const reader = new ReadableStream({
438
- type: "bytes",
439
- async pull(controller) {
440
- const byobRequest = controller.byobRequest;
441
- byobRequest?.respond(byobRequest?.view?.byteLength ?? 0);
457
+ const response = await fetch(path);
458
+ if (!response.ok) {
459
+ throw new Error(`Failed to fetch asset: ${response.statusText}`);
460
+ }
461
+ const reader = response.body?.getReader();
462
+ if (reader) {
463
+ const chunks = [];
464
+ while (true) {
465
+ const { done, value } = await reader.read();
466
+ if (done) break;
467
+ if (value) chunks.push(value);
442
468
  }
443
- });
444
- const readerByob = new ReadableStreamBYOBReader(reader);
445
- const readableByteStream = fetcher;
446
- const data = await readerByob.read(Buffer.from(readableByteStream));
447
- const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(
448
- Buffer.from(data.value ?? Buffer.alloc(0)).toJSON().data
449
- ).toString("base64")}`;
450
- const extension = this.assetType(path);
451
- return {
452
- b64encodedData,
453
- extension
454
- };
469
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
470
+ const completeBuffer = new Uint8Array(totalLength);
471
+ let offset = 0;
472
+ for (const chunk of chunks) {
473
+ completeBuffer.set(chunk, offset);
474
+ offset += chunk.length;
475
+ }
476
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(completeBuffer).toString("base64")}`;
477
+ const extension = this.assetType(path);
478
+ return { b64encodedData, extension };
479
+ } else {
480
+ const arrayBuffer = await response.arrayBuffer();
481
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(arrayBuffer).toString("base64")}`;
482
+ const extension = this.assetType(path);
483
+ return { b64encodedData, extension };
484
+ }
455
485
  }
456
486
  formatHelper(f) {
457
487
  if (/([A-Za-z]+-[A-Za-z]+)/g.test(f) === true) {
@@ -460,7 +490,7 @@ var Fs = class {
460
490
  } else return f.substring(0, 1).toUpperCase().concat(f.substring(1));
461
491
  }
462
492
  range(from, to) {
463
- const values = [];
493
+ const values = Array.of();
464
494
  for (let i = from; i < to; i++) {
465
495
  values.push(i);
466
496
  }
@@ -500,10 +530,9 @@ var Fs = class {
500
530
  {
501
531
  const sliceSize = 512;
502
532
  const typeMatch = b64Data.match(
503
- /^data:(image|application|video|text|font)\/[A-Za-z0-9+-.]+;base64,/
533
+ /^data:(image|application|video|text|model|font)\/[A-Za-z0-9+-.]+;base64,/
504
534
  );
505
535
  const type = typeMatch?.[1];
506
- console.log(type ?? "");
507
536
  if (!typeMatch) {
508
537
  throw new Error(`${b64Data} is not a valid data Url`);
509
538
  }
@@ -523,9 +552,42 @@ var Fs = class {
523
552
  return blob;
524
553
  }
525
554
  }
555
+ cleanDataUrl(props) {
556
+ return props.replace(
557
+ /^data:(image|application|video|text|font|model|audio)\/[A-Za-z0-9+-.]+;base64,/,
558
+ ""
559
+ );
560
+ }
561
+ /**
562
+ *
563
+ * @param inputUrl remote url to fetch data from
564
+ * @param outputPath desired output path relative to the cwd
565
+ * @param useDetectedExtension optional, defaults to true
566
+ *
567
+ * if `useDetectedExtension` is false you must include the file extension in your output path &rarr;
568
+ *
569
+ * 🚫 'public/assets/image-1'
570
+ *
571
+ * ✅ 'public/assets/image-1.png'
572
+ */
573
+ async fetchRemoteWriteLocal(inputUrl, outputPath, useDetectedExtension = true) {
574
+ try {
575
+ const result = await this.assetToBufferView(inputUrl);
576
+ const cleanData = this.cleanDataUrl(result.b64encodedData);
577
+ const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
578
+ if (/\./g.test(formattedPath) === false) {
579
+ throw new Error(
580
+ "either add false as the third argument in `fetchRemoteWriteLocal` [input, output, use-detected-file-extension (defaults to true)]"
581
+ );
582
+ }
583
+ this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
584
+ } catch (err) {
585
+ return console.error(err);
586
+ }
587
+ }
526
588
  };
527
589
 
528
590
  export {
529
591
  Fs
530
592
  };
531
- //# sourceMappingURL=chunk-K45COKAJ.js.map
593
+ //# sourceMappingURL=chunk-MFKSUNKN.js.map
package/dist/fs/index.cjs CHANGED
@@ -360,11 +360,22 @@ var Fs = class {
360
360
  /* end url */
361
361
  get gzVal() {
362
362
  return ["application/x-gzip", "application/gzip"].reduce(
363
- (vals) => vals
363
+ (union) => union
364
+ );
365
+ }
366
+ get objVal() {
367
+ return ["application/octet-stream", "text/plain", "model/obj"].reduce((union) => union);
368
+ }
369
+ get tsVal() {
370
+ return ["text/typescript", "video/mp2t", "video/vnd.dlna.mpeg-tts"].reduce((union) => union);
371
+ }
372
+ get ttfVal() {
373
+ return ["application/font-sfnt", "font/ttf"].reduce(
374
+ (union) => union
364
375
  );
365
376
  }
366
377
  get zipVal() {
367
- return ["application/zip", "application/x-zip-compressed"].reduce((vals) => vals);
378
+ return ["application/zip", "application/x-zip-compressed"].reduce((union) => union);
368
379
  }
369
380
  get mimeTypeObj() {
370
381
  return {
@@ -389,6 +400,8 @@ var Fs = class {
389
400
  eot: "application/vnd.ms-fontobject",
390
401
  epub: "application/epub+zip",
391
402
  gif: "image/gif",
403
+ glb: "model/gltf-binary",
404
+ gltf: "model/gltf+json",
392
405
  gz: this.gzVal,
393
406
  htm: "text/html",
394
407
  html: "text/html",
@@ -401,6 +414,8 @@ var Fs = class {
401
414
  json: "application/json",
402
415
  jsonld: "application/ld+json",
403
416
  m3u8: "application/vnd.apple.mpegurl",
417
+ m4a: "audio/mp4",
418
+ m4v: "video/mp4",
404
419
  md: "text/markdown",
405
420
  mdx: "application/x-mdx",
406
421
  mid: "audio/midi",
@@ -411,10 +426,12 @@ var Fs = class {
411
426
  mpeg: "video/mpeg",
412
427
  mpkg: "application/vnd.apple.installer+xml",
413
428
  ndjson: "application/x-ndjson",
429
+ obj: this.objVal,
414
430
  odp: "application/vnd.oasis.opendocument.presentation",
415
431
  ods: "application/vnd.oasis.opendocument.spreadsheet",
416
432
  odt: "application/vnd.oasis.opendocument.text",
417
433
  oga: "audio/ogg",
434
+ ogg: "audio/ogg",
418
435
  ogv: "video/ogg",
419
436
  ogx: "application/ogg",
420
437
  opus: "audio/ogg",
@@ -425,6 +442,8 @@ var Fs = class {
425
442
  pkpass: "application/vnd.apple.pkpass",
426
443
  ppt: "application/vnd.ms-powerpoint",
427
444
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
445
+ py: "text/x-python",
446
+ pyc: "application/x-python-code",
428
447
  rar: "application/vnd.rar",
429
448
  rtf: "application/rtf",
430
449
  sh: "application/x-sh",
@@ -432,10 +451,12 @@ var Fs = class {
432
451
  tar: "application/x-tar",
433
452
  tif: "image/tiff",
434
453
  tiff: "image/tiff",
435
- ts: "video/mp2t",
436
- ttf: "font/ttf",
454
+ ts: this.tsVal,
455
+ ttf: this.ttfVal,
437
456
  txt: "text/plain",
438
457
  vsd: "application/vnd.visio",
458
+ vtt: "text/vtt",
459
+ wasm: "application/wasm",
439
460
  wav: "audio/wav",
440
461
  weba: "video/webm",
441
462
  webp: "image/webp",
@@ -453,18 +474,20 @@ var Fs = class {
453
474
  };
454
475
  }
455
476
  assetType(url) {
456
- return this.parseUrl(url).pathname?.split(/([.])/gim)?.reverse()?.[0];
477
+ const u = this.parseUrl(url);
478
+ return u.pathname?.split(/([.])/gim)?.reverse()?.[0];
457
479
  }
458
480
  getMime(input) {
459
481
  return this.mimeTypeObj[input];
460
482
  }
483
+ hmm = /* @__PURE__ */ __name((input) => {
484
+ return this.mimeTypeObj[input];
485
+ }, "hmm");
461
486
  async assetToBuffer(path) {
462
487
  const [fetcher] = await Promise.all([
463
488
  fetch(path).then((t) => t.arrayBuffer())
464
489
  ]);
465
- const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(
466
- Buffer.from(fetcher).toJSON().data
467
- ).toString("base64")}`;
490
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(fetcher).toString("base64")}`;
468
491
  const extension = this.assetType(path);
469
492
  return {
470
493
  b64encodedData,
@@ -472,27 +495,34 @@ var Fs = class {
472
495
  };
473
496
  }
474
497
  async assetToBufferView(path) {
475
- const [fetcher] = await Promise.all([
476
- fetch(path).then((t) => t.arrayBuffer())
477
- ]);
478
- const reader = new ReadableStream({
479
- type: "bytes",
480
- async pull(controller) {
481
- const byobRequest = controller.byobRequest;
482
- byobRequest?.respond(byobRequest?.view?.byteLength ?? 0);
498
+ const response = await fetch(path);
499
+ if (!response.ok) {
500
+ throw new Error(`Failed to fetch asset: ${response.statusText}`);
501
+ }
502
+ const reader = response.body?.getReader();
503
+ if (reader) {
504
+ const chunks = [];
505
+ while (true) {
506
+ const { done, value } = await reader.read();
507
+ if (done) break;
508
+ if (value) chunks.push(value);
483
509
  }
484
- });
485
- const readerByob = new ReadableStreamBYOBReader(reader);
486
- const readableByteStream = fetcher;
487
- const data = await readerByob.read(Buffer.from(readableByteStream));
488
- const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(
489
- Buffer.from(data.value ?? Buffer.alloc(0)).toJSON().data
490
- ).toString("base64")}`;
491
- const extension = this.assetType(path);
492
- return {
493
- b64encodedData,
494
- extension
495
- };
510
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
511
+ const completeBuffer = new Uint8Array(totalLength);
512
+ let offset = 0;
513
+ for (const chunk of chunks) {
514
+ completeBuffer.set(chunk, offset);
515
+ offset += chunk.length;
516
+ }
517
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(completeBuffer).toString("base64")}`;
518
+ const extension = this.assetType(path);
519
+ return { b64encodedData, extension };
520
+ } else {
521
+ const arrayBuffer = await response.arrayBuffer();
522
+ const b64encodedData = `data:${this.getMime(this.assetType(path))};base64, ${Buffer.from(arrayBuffer).toString("base64")}`;
523
+ const extension = this.assetType(path);
524
+ return { b64encodedData, extension };
525
+ }
496
526
  }
497
527
  formatHelper(f) {
498
528
  if (/([A-Za-z]+-[A-Za-z]+)/g.test(f) === true) {
@@ -501,7 +531,7 @@ var Fs = class {
501
531
  } else return f.substring(0, 1).toUpperCase().concat(f.substring(1));
502
532
  }
503
533
  range(from, to) {
504
- const values = [];
534
+ const values = Array.of();
505
535
  for (let i = from; i < to; i++) {
506
536
  values.push(i);
507
537
  }
@@ -541,10 +571,9 @@ var Fs = class {
541
571
  {
542
572
  const sliceSize = 512;
543
573
  const typeMatch = b64Data.match(
544
- /^data:(image|application|video|text|font)\/[A-Za-z0-9+-.]+;base64,/
574
+ /^data:(image|application|video|text|model|font)\/[A-Za-z0-9+-.]+;base64,/
545
575
  );
546
576
  const type = typeMatch?.[1];
547
- console.log(type ?? "");
548
577
  if (!typeMatch) {
549
578
  throw new Error(`${b64Data} is not a valid data Url`);
550
579
  }
@@ -564,5 +593,38 @@ var Fs = class {
564
593
  return blob;
565
594
  }
566
595
  }
596
+ cleanDataUrl(props) {
597
+ return props.replace(
598
+ /^data:(image|application|video|text|font|model|audio)\/[A-Za-z0-9+-.]+;base64,/,
599
+ ""
600
+ );
601
+ }
602
+ /**
603
+ *
604
+ * @param inputUrl remote url to fetch data from
605
+ * @param outputPath desired output path relative to the cwd
606
+ * @param useDetectedExtension optional, defaults to true
607
+ *
608
+ * if `useDetectedExtension` is false you must include the file extension in your output path &rarr;
609
+ *
610
+ * 🚫 'public/assets/image-1'
611
+ *
612
+ * ✅ 'public/assets/image-1.png'
613
+ */
614
+ async fetchRemoteWriteLocal(inputUrl, outputPath, useDetectedExtension = true) {
615
+ try {
616
+ const result = await this.assetToBufferView(inputUrl);
617
+ const cleanData = this.cleanDataUrl(result.b64encodedData);
618
+ const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
619
+ if (/\./g.test(formattedPath) === false) {
620
+ throw new Error(
621
+ "either add false as the third argument in `fetchRemoteWriteLocal` [input, output, use-detected-file-extension (defaults to true)]"
622
+ );
623
+ }
624
+ this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
625
+ } catch (err) {
626
+ return console.error(err);
627
+ }
628
+ }
567
629
  };
568
630
  //# sourceMappingURL=index.cjs.map