Content-Length: 521645 | pFad | http://github.com/NativeScript/canvas/commit/b54db4be2f7b6654153cb899ba430abcb429607f

7E feat(polyfill): base64 fetch · NativeScript/canvas@b54db4b · GitHub
Skip to content

Commit b54db4b

Browse files
committed
feat(polyfill): base64 fetch
1 parent 1cdd4d1 commit b54db4b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

packages/canvas-polyfill/async/xhr/TNSXMLHttpRequest.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CancellablePromise, Http } from '../http/http';
22
import { HttpError, HttpRequestOptions, ProgressEvent } from '../http/http-request-common';
33
import { FileManager } from '../file/file';
44
import { knownFolders, path as filePath, File as fsFile, Utils } from '@nativescript/core';
5+
import { Helpers } from '@nativescript/canvas/helpers';
56

67
enum XMLHttpRequestResponseType {
78
empty = '',
@@ -474,6 +475,133 @@ export class TNSXMLHttpRequest {
474475
return;
475476
}
476477

478+
if (!path) {
479+
const startEvent = new ProgressEvent('loadstart', this._lastProgress);
480+
481+
if (this.onnloadstart) {
482+
this.onnloadstart(startEvent);
483+
}
484+
485+
this.emitEvent('loadstart', startEvent);
486+
487+
this._updateReadyStateChange(this.LOADING);
488+
489+
if (this._request.url.startsWith('data:')) {
490+
const split = this._request.url.split(',');
491+
const mime = split[0];
492+
const base64result = split[1];
493+
if (mime && base64result) {
494+
Helpers.base64DecodeAsync(base64result)
495+
.then((result) => {
496+
this._headers['Content-Type'] = mime;
497+
const data = result[1];
498+
499+
this._response = data ?? '';
500+
this._responseText = data[0] ?? '';
501+
502+
this._status = 200;
503+
this._responseURL = this._request.url;
504+
505+
const size = data?.byteLength ?? 0;
506+
this._lastProgress = {
507+
lengthComputable: true,
508+
loaded: size,
509+
total: size,
510+
target: this,
511+
};
512+
513+
const progressEvent = new ProgressEvent('progress', this._lastProgress);
514+
if (this.onprogress) {
515+
this.onprogress(progressEvent);
516+
}
517+
this.emitEvent('progress', progressEvent);
518+
519+
this._addToStringOnResponse();
520+
521+
const loadEvent = new ProgressEvent('load', this._lastProgress);
522+
523+
if (this.onnload) {
524+
this.onnload(loadEvent);
525+
}
526+
527+
this.emitEvent('load', loadEvent);
528+
529+
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
530+
531+
if (this.onnloadend) {
532+
this.onnloadend(loadendEvent);
533+
}
534+
535+
this.emitEvent('loadend', loadendEvent);
536+
537+
this._updateReadyStateChange(this.DONE);
538+
})
539+
.catch((_) => {
540+
const errorEvent = new ProgressEvent('error', this._lastProgress);
541+
this._status = 0;
542+
this._responseText = '';
543+
this._responseType = '' as never;
544+
this._responseURL = '';
545+
546+
this._lastProgress = {
547+
lengthComputable: false,
548+
loaded: 0,
549+
total: 0,
550+
target: this,
551+
};
552+
553+
if (this.onerror) {
554+
this.onerror(errorEvent);
555+
}
556+
557+
this.emitEvent('error', errorEvent);
558+
559+
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
560+
561+
if (this.onnloadend) {
562+
this.onnloadend(loadendEvent);
563+
}
564+
565+
this.emitEvent('loadend', loadendEvent);
566+
567+
this._updateReadyStateChange(this.DONE);
568+
});
569+
570+
return;
571+
}
572+
}
573+
const errorEvent = new ProgressEvent('error', this._lastProgress);
574+
this._status = 0;
575+
this._responseText = '';
576+
this._responseType = '' as never;
577+
this._responseURL = '';
578+
579+
this._lastProgress = {
580+
lengthComputable: false,
581+
loaded: 0,
582+
total: 0,
583+
target: this,
584+
};
585+
586+
if (this.onerror) {
587+
this.onerror(errorEvent);
588+
}
589+
590+
this.emitEvent('error', errorEvent);
591+
592+
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
593+
594+
if (this.onnloadend) {
595+
this.onnloadend(loadendEvent);
596+
}
597+
598+
this.emitEvent('loadend', loadendEvent);
599+
600+
this._updateReadyStateChange(this.DONE);
601+
602+
return;
603+
}
604+
477605
const url = new URL(`file://${path}`);
478606

479607
const responseURL = url.href;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/canvas/commit/b54db4be2f7b6654153cb899ba430abcb429607f

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy