LCOV - code coverage report
Current view: top level - lib/helper - file-polyfill.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 100 108 92.6 %
Date: 2025-01-24 02:02:48 Functions: 4 4 100.0 %
Branches: 12 14 85.7 %

           Branch data     Line data    Source code
       1            [ + ]:        229 : /**
       2                 :        229 :  * @module  file-polyfill
       3                 :        229 :  * @desc    A Node.js implementation of the {@link https://w3c.github.io/FileAPI/#file-section W3C File API} specifications for non-browser environments.
       4                 :        229 :  * @version 1.0.0
       5                 :        229 :  * @author  Essam A. El-Sherif
       6                 :        229 :  */
       7                 :        229 : 
       8                 :        229 : /**
       9                 :        229 :  * Global object available in all node modules >= v14.18.0
      10                 :        229 :  *
      11                 :        229 :  * Blob Added in: node v14.18.0
      12                 :        229 :  *
      13                 :        229 :  * @external Blob
      14                 :        229 :  * @desc Global object available in all node modules >= v14.18.0, defined by {@link https://w3c.github.io/FileAPI/#blob-section W3C File API Standard} and implemented by {@link https://nodejs.org/docs/latest-v20.x/api/buffer.html#class-blob node.js}
      15                 :        229 :  */
      16                 :        229 : 
      17                 :        229 : /**
      18                 :        229 :  * W3C File API specifications
      19                 :        229 :  * url: https://w3c.github.io/FileAPI/#file-section
      20                 :        229 :  *
      21                 :        229 :  * [Exposed=(Window,Worker), Serializable]
      22                 :        229 :  * interface File : Blob {
      23                 :        229 :  *   constructor(sequence<BlobPart> fileBits,
      24                 :        229 :  *               USVString fileName,
      25                 :        229 :  *               optional FilePropertyBag options = {});
      26                 :        229 :  *   readonly attribute DOMString name;
      27                 :        229 :  *   readonly attribute long long lastModified;
      28                 :        229 :  * };
      29                 :        229 :  *
      30                 :        229 :  * dictionary FilePropertyBag : BlobPropertyBag {
      31                 :        229 :  *   long long lastModified;
      32                 :        229 :  * };
      33                 :        229 :  *
      34                 :        229 :  * @class   File
      35                 :        229 :  * @extends module:file-polyfill~Blob
      36                 :        229 :  * @static
      37                 :        229 :  * @desc   The File interface provides information about files and allows JavaScript in a web page to access their content.
      38                 :        229 :  */
      39            [ + ]:        229 : export class File extends Blob{
      40                 :         94 :         /** @member {string} */
      41                 :         94 :         #name;
      42                 :         94 :         /** @member {number} */
      43                 :         94 :         #lastModified;
      44                 :         94 : 
      45                 :         94 :         /**
      46                 :         94 :          * @method   constructor
      47                 :         94 :          * @instance
      48                 :         94 :          * @memberof module:file-polyfill.File
      49                 :         94 :          * @param    {object} fileBits - An iterable object that will be put inside the File.
      50                 :         94 :          * @param    {string} fileName - A string representing the file name or the path to the file.
      51                 :         94 :          * @param    {object} options - An options object containing optional attributes for the file.
      52                 :         94 :          * @desc     Constructs a new File object.
      53                 :         94 :          */
      54            [ + ]:         94 :         constructor(fileBits, fileName, options = {}){
      55                 :        113 : 
      56            [ - ]:        113 :                 if(globalThis.window){
      57                 :          0 :                         /** node:coverage disable **
      58                 :          0 : 
      59                 :          0 :                         if(arguments.length < 2){
      60                 :          0 :                                 throw new TypeError(`File constructor: At least 2 arguments required, but only ${arguments.length} passed`);  // firefox 102
      61                 :          0 :                         }
      62                 :          0 : 
      63                 :          0 :                         *** node:coverage enable ***/
      64                 :          0 :                 }
      65                 :        113 :                 else{
      66            [ + ]:        113 :                         if(arguments.length < 2){
      67                 :          2 :                                 throw new TypeError(`File constructor: The "fileBits" and "fileName" arguments must be specified`); // node.js
      68                 :          2 :                         }
      69            [ + ]:        113 :                 }
      70                 :        111 : 
      71                 :        111 :                 super(fileBits, options);
      72                 :        111 : 
      73       [ + ][ + ]:        113 :                 let { lastModified } = options === null ? {} : options;
      74                 :        113 : 
      75            [ + ]:        113 :                 if(lastModified !== undefined){
      76                 :          8 :                         // Using Number(...) will not throw an error for bigints.
      77                 :          8 :                         lastModified = +lastModified;
      78                 :          8 : 
      79            [ - ]:          8 :                         if(Number.isNaN(lastModified)) lastModified = 0;
      80            [ + ]:          8 :                 }
      81                 :         86 :                 else{
      82                 :         86 :                         lastModified = Date.now();
      83            [ + ]:         86 :                 }
      84                 :         94 : 
      85                 :         94 :                 this.#name = String(fileName);
      86                 :         94 :                 this.#lastModified = lastModified;
      87                 :        113 :         }
      88                 :         94 : 
      89                 :         94 :         /**
      90                 :         94 :          * @member   {function} name
      91                 :         94 :          * @memberof module:file-polyfill.File
      92                 :         94 :          * @instance
      93                 :         94 :          * @readonly
      94                 :         94 :          * @desc     Get the name of the file represented by a File object.
      95                 :         94 :          */
      96            [ + ]:         94 :         get name() { return this.#name }
      97                 :         94 : 
      98                 :         94 :         /**
      99                 :         94 :          * @member   {function} lastModified
     100                 :         94 :          * @memberof module:file-polyfill.File
     101                 :         94 :          * @instance
     102                 :         94 :          * @readonly
     103                 :         94 :          * @desc     Get the last modified date of the file as the number of milliseconds since the Unix epoch.
     104                 :         94 :          */
     105            [ + ]:         94 :         get lastModified() { return this.#lastModified }
     106                 :         94 : }
     107                 :        229 : 
     108                 :        229 : Object.defineProperty(File.prototype, Symbol.toStringTag, {value: 'File', configurable: true});

Generated by: LCOV version 1.14