11import { assert } from '../errors/assert.js' ;
22import type { BufferEncoding } from '../models/BufferEncoding.js' ;
33import type { FileReference , FileResource , TextFileResource } from '../models/FileResource.js' ;
4- import { decode , encodeString , isGZipped } from './encode-decode.js' ;
4+ import { decode , decompress , encodeString , isGZipped } from './encode-decode.js' ;
55
66export interface CFileResourceJson {
77 url : string ;
@@ -12,41 +12,53 @@ export interface CFileResourceJson {
1212}
1313
1414export class CFileResource implements TextFileResource {
15- private _text ?: string ;
1615 readonly baseFilename ?: string | undefined ;
17- private _gz ?: boolean | undefined ;
16+ readonly url : URL ;
17+ readonly content : string | Uint8Array < ArrayBuffer > ;
18+ readonly encoding : BufferEncoding | undefined ;
19+ #gz?: boolean | undefined ;
20+ #text?: string ;
21+ #data?: Uint8Array < ArrayBuffer > ;
1822
1923 constructor (
20- readonly url : URL ,
21- readonly content : string | ArrayBufferView ,
22- readonly encoding : BufferEncoding | undefined ,
24+ url : URL ,
25+ content : string | Uint8Array < ArrayBuffer > ,
26+ encoding : BufferEncoding | undefined ,
2327 baseFilename : string | undefined ,
2428 gz : boolean | undefined ,
2529 ) {
30+ this . url = url ;
31+ this . content = content ;
32+ this . encoding = encoding ;
2633 this . baseFilename = baseFilename ?? ( ( url . protocol !== 'data:' && url . pathname . split ( '/' ) . pop ( ) ) || undefined ) ;
27- this . _gz = gz ;
34+ this . #gz = gz ;
2835 }
2936
3037 get gz ( ) : boolean {
31- if ( this . _gz !== undefined ) return this . _gz ;
38+ if ( this . #gz !== undefined ) return this . #gz ;
3239 if ( this . url . pathname . endsWith ( '.gz' ) ) return true ;
3340 if ( typeof this . content === 'string' ) return false ;
3441 return isGZipped ( this . content ) ;
3542 }
3643
3744 getText ( encoding ?: BufferEncoding ) : string {
38- if ( this . _text !== undefined ) return this . _text ;
45+ if ( this . #text !== undefined ) return this . #text ;
3946 const text = typeof this . content === 'string' ? this . content : decode ( this . content , encoding ?? this . encoding ) ;
40- this . _text = text ;
47+ this . #text = text ;
4148 return text ;
4249 }
4350
44- getBytes ( ) : Uint8Array {
45- const arrayBufferview =
46- typeof this . content === 'string' ? encodeString ( this . content , this . encoding ) : this . content ;
47- return arrayBufferview instanceof Uint8Array
48- ? arrayBufferview
49- : new Uint8Array ( arrayBufferview . buffer , arrayBufferview . byteOffset , arrayBufferview . byteLength ) ;
51+ async getBytes ( unzip ?: boolean ) : Promise < Uint8Array < ArrayBuffer > > {
52+ if ( unzip !== false && this . #data !== undefined ) return this . #data;
53+ if ( typeof this . content === 'string' ) {
54+ this . #data = encodeString ( this . content , this . encoding ) ;
55+ return this . #data;
56+ }
57+ if ( unzip ?? isGZipped ( this . content ) ) {
58+ this . #data = await decompress ( this . content , 'gzip' ) ;
59+ return this . #data;
60+ }
61+ return this . content ;
5062 }
5163
5264 public toJson ( ) : CFileResourceJson {
@@ -64,18 +76,18 @@ export class CFileResource implements TextFileResource {
6476 }
6577
6678 static from ( fileResource : FileResource ) : CFileResource ;
67- static from ( fileReference : FileReference , content : string | ArrayBufferView ) : CFileResource ;
68- static from ( fileReference : FileReference | URL , content : string | ArrayBufferView ) : CFileResource ;
79+ static from ( fileReference : FileReference , content : string | Uint8Array < ArrayBuffer > ) : CFileResource ;
80+ static from ( fileReference : FileReference | URL , content : string | Uint8Array < ArrayBuffer > ) : CFileResource ;
6981 static from (
7082 url : URL ,
71- content : string | ArrayBufferView ,
83+ content : string | Uint8Array < ArrayBuffer > ,
7284 encoding ?: BufferEncoding | undefined ,
7385 baseFilename ?: string | undefined ,
7486 gz ?: boolean ,
7587 ) : CFileResource ;
7688 static from (
7789 urlOrFileResource : FileResource | FileReference | URL ,
78- content ?: string | ArrayBufferView ,
90+ content ?: string | Uint8Array < ArrayBuffer > ,
7991 encoding ?: BufferEncoding ,
8092 baseFilename ?: string | undefined ,
8193 gz ?: boolean ,
0 commit comments