MCPcopy Create free account
hub / github.com/diffusionstudio/vits-web / createLazyFile

Function createLazyFile

src/piper.js:3137–3286  ·  view source on GitHub ↗
(parent, name, url, canRead, canWrite)

Source from the content-addressed store, hash-verified

3135 }
3136 },
3137 createLazyFile(parent, name, url, canRead, canWrite) {
3138 function LazyUint8Array() {
3139 this.lengthKnown = false;
3140 this.chunks = [];
3141 }
3142 LazyUint8Array.prototype.get = function LazyUint8Array_get(idx) {
3143 if (idx > this.length - 1 || idx < 0) {
3144 return undefined;
3145 }
3146 var chunkOffset = idx % this.chunkSize;
3147 var chunkNum = (idx / this.chunkSize) | 0;
3148 return this.getter(chunkNum)[chunkOffset];
3149 };
3150 LazyUint8Array.prototype.setDataGetter = function LazyUint8Array_setDataGetter(getter) {
3151 this.getter = getter;
3152 };
3153 LazyUint8Array.prototype.cacheLength = function LazyUint8Array_cacheLength() {
3154 var xhr = new XMLHttpRequest();
3155 xhr.open('HEAD', url, false);
3156 xhr.send(null);
3157 if (!((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304))
3158 throw new Error("Couldn't load " + url + '. Status: ' + xhr.status);
3159 var datalength = Number(xhr.getResponseHeader('Content-length'));
3160 var header;
3161 var hasByteServing =
3162 (header = xhr.getResponseHeader('Accept-Ranges')) && header === 'bytes';
3163 var usesGzip = (header = xhr.getResponseHeader('Content-Encoding')) && header === 'gzip';
3164 var chunkSize = 1024 * 1024;
3165 if (!hasByteServing) chunkSize = datalength;
3166 var doXHR = (from, to) => {
3167 if (from > to)
3168 throw new Error('invalid range (' + from + ', ' + to + ') or no bytes requested!');
3169 if (to > datalength - 1)
3170 throw new Error('only ' + datalength + ' bytes available! programmer error!');
3171 var xhr = new XMLHttpRequest();
3172 xhr.open('GET', url, false);
3173 if (datalength !== chunkSize) xhr.setRequestHeader('Range', 'bytes=' + from + '-' + to);
3174 xhr.responseType = 'arraybuffer';
3175 if (xhr.overrideMimeType) {
3176 xhr.overrideMimeType('text/plain; charset=x-user-defined');
3177 }
3178 xhr.send(null);
3179 if (!((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304))
3180 throw new Error("Couldn't load " + url + '. Status: ' + xhr.status);
3181 if (xhr.response !== undefined) {
3182 return new Uint8Array(xhr.response || []);
3183 }
3184 return intArrayFromString(xhr.responseText || '', true);
3185 };
3186 var lazyArray = this;
3187 lazyArray.setDataGetter((chunkNum) => {
3188 var start = chunkNum * chunkSize;
3189 var end = (chunkNum + 1) * chunkSize - 1;
3190 end = Math.min(end, datalength - 1);
3191 if (typeof lazyArray.chunks[chunkNum] == 'undefined') {
3192 lazyArray.chunks[chunkNum] = doXHR(start, end);
3193 }
3194 if (typeof lazyArray.chunks[chunkNum] == 'undefined') throw new Error('doXHR failed!');

Callers

nothing calls this directly

Calls 3

doXHRFunction · 0.85
writeChunksFunction · 0.85
mmapAllocFunction · 0.85

Tested by

no test coverage detected