1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17// This class is needed for unit tests because Node.js doesn't provide 18// an implementation of the Web API's File type 19class FileImpl { 20 readonly size: number; 21 readonly type: string; 22 readonly name: string; 23 readonly lastModified: number = 0; 24 readonly webkitRelativePath: string = ''; 25 private readonly buffer: ArrayBuffer; 26 27 constructor(buffer: ArrayBuffer, fileName: string) { 28 this.buffer = buffer; 29 this.size = this.buffer.byteLength; 30 this.type = 'application/octet-stream'; 31 this.name = fileName; 32 } 33 34 arrayBuffer(): Promise<ArrayBuffer> { 35 return new Promise<ArrayBuffer>((resolve) => { 36 resolve(this.buffer); 37 }); 38 } 39 40 slice(start?: number, end?: number, contentType?: string): Blob { 41 throw new Error('Not implemented!'); 42 } 43 44 stream(): any { 45 throw new Error('Not implemented!'); 46 } 47 48 text(): Promise<string> { 49 const utf8Decoder = new TextDecoder(); 50 const text = utf8Decoder.decode(this.buffer); 51 return new Promise<string>((resolve) => { 52 resolve(text); 53 }); 54 } 55} 56 57export {FileImpl}; 58