@di-framework/cli-plugin-platform 6.0.5 → 6.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -439,14 +439,16 @@ class TextDecoderPolyfill {
|
|
|
439
439
|
|
|
440
440
|
class URLSearchParamsPolyfill {
|
|
441
441
|
#pairs = [];
|
|
442
|
+
#onChange;
|
|
442
443
|
constructor(init) {
|
|
443
444
|
if (typeof init === "string") {
|
|
444
445
|
const query = init.startsWith("?") ? init.slice(1) : init;
|
|
445
446
|
if (query !== "") {
|
|
446
447
|
for (const part of query.split("&")) {
|
|
447
448
|
const eq = part.indexOf("=");
|
|
448
|
-
const
|
|
449
|
-
const
|
|
449
|
+
const decode = (text) => decodeURIComponent(text.replaceAll("+", " "));
|
|
450
|
+
const name = decode(eq === -1 ? part : part.slice(0, eq));
|
|
451
|
+
const value = decode(eq === -1 ? "" : part.slice(eq + 1));
|
|
450
452
|
this.#pairs.push([name, value]);
|
|
451
453
|
}
|
|
452
454
|
}
|
|
@@ -457,10 +459,43 @@ class URLSearchParamsPolyfill {
|
|
|
457
459
|
this.#pairs.push([name, String(value)]);
|
|
458
460
|
}
|
|
459
461
|
}
|
|
462
|
+
bind(onChange) {
|
|
463
|
+
this.#onChange = onChange;
|
|
464
|
+
}
|
|
460
465
|
get(name) {
|
|
461
466
|
const found = this.#pairs.find(([key]) => key === name);
|
|
462
467
|
return found ? found[1] : null;
|
|
463
468
|
}
|
|
469
|
+
getAll(name) {
|
|
470
|
+
return this.#pairs.filter(([key]) => key === name).map(([, value]) => value);
|
|
471
|
+
}
|
|
472
|
+
has(name) {
|
|
473
|
+
return this.#pairs.some(([key]) => key === name);
|
|
474
|
+
}
|
|
475
|
+
append(name, value) {
|
|
476
|
+
this.#pairs.push([name, String(value)]);
|
|
477
|
+
this.#onChange?.();
|
|
478
|
+
}
|
|
479
|
+
set(name, value) {
|
|
480
|
+
const next = [];
|
|
481
|
+
let placed = false;
|
|
482
|
+
for (const pair of this.#pairs) {
|
|
483
|
+
if (pair[0] !== name)
|
|
484
|
+
next.push(pair);
|
|
485
|
+
else if (!placed) {
|
|
486
|
+
next.push([name, String(value)]);
|
|
487
|
+
placed = true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (!placed)
|
|
491
|
+
next.push([name, String(value)]);
|
|
492
|
+
this.#pairs = next;
|
|
493
|
+
this.#onChange?.();
|
|
494
|
+
}
|
|
495
|
+
delete(name) {
|
|
496
|
+
this.#pairs = this.#pairs.filter(([key]) => key !== name);
|
|
497
|
+
this.#onChange?.();
|
|
498
|
+
}
|
|
464
499
|
*entries() {
|
|
465
500
|
yield* this.#pairs;
|
|
466
501
|
}
|
|
@@ -497,6 +532,12 @@ class URLPolyfill {
|
|
|
497
532
|
this.origin = `${this.protocol}//${this.host}`;
|
|
498
533
|
this.href = resolved;
|
|
499
534
|
this.searchParams = new URLSearchParamsPolyfill(this.search);
|
|
535
|
+
this.searchParams.bind(() => this.#sync());
|
|
536
|
+
}
|
|
537
|
+
#sync() {
|
|
538
|
+
const query = this.searchParams.toString();
|
|
539
|
+
this.search = query === "" ? "" : `?${query}`;
|
|
540
|
+
this.href = `${this.origin}${this.pathname}${this.search}${this.hash}`;
|
|
500
541
|
}
|
|
501
542
|
toString() {
|
|
502
543
|
return this.href;
|
|
@@ -560,6 +601,30 @@ class HeadersPolyfill {
|
|
|
560
601
|
for (const [name, value] of this.entries())
|
|
561
602
|
callback(value, name, this);
|
|
562
603
|
}
|
|
604
|
+
*#combined() {
|
|
605
|
+
for (const name of [...this.#map.keys()].sort((left, right) => left.localeCompare(right))) {
|
|
606
|
+
const values = this.#map.get(name) ?? [];
|
|
607
|
+
if (values.length === 0)
|
|
608
|
+
continue;
|
|
609
|
+
if (name === "set-cookie") {
|
|
610
|
+
for (const value of values)
|
|
611
|
+
yield [name, value];
|
|
612
|
+
} else {
|
|
613
|
+
yield [name, values.join(", ")];
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
*keys() {
|
|
618
|
+
for (const [name] of this.#combined())
|
|
619
|
+
yield name;
|
|
620
|
+
}
|
|
621
|
+
*values() {
|
|
622
|
+
for (const [, value] of this.#combined())
|
|
623
|
+
yield value;
|
|
624
|
+
}
|
|
625
|
+
getSetCookie() {
|
|
626
|
+
return [...this.#map.get("set-cookie") ?? []];
|
|
627
|
+
}
|
|
563
628
|
[Symbol.iterator]() {
|
|
564
629
|
return this.entries();
|
|
565
630
|
}
|
|
@@ -622,12 +687,12 @@ class RequestPolyfill {
|
|
|
622
687
|
this.method = String(init.method ?? input.method);
|
|
623
688
|
this.url = input.url;
|
|
624
689
|
this.headers = new HeadersPolyfill(init.headers ?? input.headers);
|
|
625
|
-
this.#body = init.body === undefined ? input.#body :
|
|
690
|
+
this.#body = init.body === undefined ? input.#body : bodyFromInit(init.body, this.headers);
|
|
626
691
|
} else {
|
|
627
692
|
this.method = String(init.method ?? "GET").toUpperCase();
|
|
628
693
|
this.url = String(input);
|
|
629
694
|
this.headers = new HeadersPolyfill(init.headers);
|
|
630
|
-
this.#body =
|
|
695
|
+
this.#body = bodyFromInit(init.body, this.headers);
|
|
631
696
|
}
|
|
632
697
|
}
|
|
633
698
|
get body() {
|
|
@@ -648,6 +713,9 @@ class RequestPolyfill {
|
|
|
648
713
|
async json() {
|
|
649
714
|
return JSON.parse(await this.text());
|
|
650
715
|
}
|
|
716
|
+
async formData() {
|
|
717
|
+
return formDataFromBody(this.headers, await this.arrayBuffer());
|
|
718
|
+
}
|
|
651
719
|
}
|
|
652
720
|
|
|
653
721
|
class ResponsePolyfill {
|
|
@@ -661,7 +729,7 @@ class ResponsePolyfill {
|
|
|
661
729
|
this.statusText = String(init.statusText ?? "");
|
|
662
730
|
this.headers = new HeadersPolyfill(init.headers);
|
|
663
731
|
this.ok = this.status >= 200 && this.status < 300;
|
|
664
|
-
this.#body =
|
|
732
|
+
this.#body = bodyFromInit(body, this.headers);
|
|
665
733
|
}
|
|
666
734
|
static json(data, init = {}) {
|
|
667
735
|
const headers = new HeadersPolyfill(init.headers);
|
|
@@ -683,6 +751,9 @@ class ResponsePolyfill {
|
|
|
683
751
|
async json() {
|
|
684
752
|
return JSON.parse(await this.text());
|
|
685
753
|
}
|
|
754
|
+
async formData() {
|
|
755
|
+
return formDataFromBody(this.headers, await this.arrayBuffer());
|
|
756
|
+
}
|
|
686
757
|
}
|
|
687
758
|
|
|
688
759
|
class AbortSignalPolyfill {
|
|
@@ -722,21 +793,345 @@ class AbortControllerPolyfill {
|
|
|
722
793
|
this.signal.dispatchEvent(event);
|
|
723
794
|
}
|
|
724
795
|
}
|
|
796
|
+
var blobContents = new WeakMap;
|
|
797
|
+
function bytesOfPart(part) {
|
|
798
|
+
if (typeof part === "string")
|
|
799
|
+
return new TextEncoder().encode(part);
|
|
800
|
+
if (part instanceof BlobPolyfill)
|
|
801
|
+
return readBlobBytes(part).slice();
|
|
802
|
+
if (part instanceof ArrayBuffer)
|
|
803
|
+
return new Uint8Array(part);
|
|
804
|
+
if (ArrayBuffer.isView(part)) {
|
|
805
|
+
return new Uint8Array(part.buffer, part.byteOffset, part.byteLength);
|
|
806
|
+
}
|
|
807
|
+
throw new TypeError("Blob parts must be strings, buffers, or Blob objects");
|
|
808
|
+
}
|
|
809
|
+
function concatBytes(chunks) {
|
|
810
|
+
let total = 0;
|
|
811
|
+
for (const chunk of chunks)
|
|
812
|
+
total += chunk.byteLength;
|
|
813
|
+
const bytes = new Uint8Array(total);
|
|
814
|
+
let offset = 0;
|
|
815
|
+
for (const chunk of chunks) {
|
|
816
|
+
bytes.set(chunk, offset);
|
|
817
|
+
offset += chunk.byteLength;
|
|
818
|
+
}
|
|
819
|
+
return bytes;
|
|
820
|
+
}
|
|
821
|
+
function readBlobBytes(blob) {
|
|
822
|
+
return blobContents.get(blob) ?? new Uint8Array;
|
|
823
|
+
}
|
|
725
824
|
|
|
726
825
|
class BlobPolyfill {
|
|
727
826
|
size;
|
|
728
827
|
type;
|
|
729
828
|
constructor(parts = [], options) {
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
size += new TextEncoder().encode(part).length;
|
|
734
|
-
else
|
|
735
|
-
size += part.byteLength;
|
|
736
|
-
}
|
|
737
|
-
this.size = size;
|
|
829
|
+
const bytes = concatBytes(parts.map((part) => bytesOfPart(part)));
|
|
830
|
+
blobContents.set(this, bytes);
|
|
831
|
+
this.size = bytes.byteLength;
|
|
738
832
|
this.type = options?.type ?? "";
|
|
739
833
|
}
|
|
834
|
+
async arrayBuffer() {
|
|
835
|
+
return readBlobBytes(this).slice().buffer;
|
|
836
|
+
}
|
|
837
|
+
async text() {
|
|
838
|
+
return new TextDecoderPolyfill().decode(readBlobBytes(this));
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
class FilePolyfill extends BlobPolyfill {
|
|
843
|
+
name;
|
|
844
|
+
lastModified;
|
|
845
|
+
constructor(parts, name, options) {
|
|
846
|
+
super(parts, options);
|
|
847
|
+
this.name = String(name);
|
|
848
|
+
this.lastModified = options?.lastModified ?? Date.now();
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
class FormDataPolyfill {
|
|
853
|
+
#entries = [];
|
|
854
|
+
constructor(init) {
|
|
855
|
+
if (init instanceof FormDataPolyfill) {
|
|
856
|
+
for (const [name, value] of init.entries()) {
|
|
857
|
+
if (typeof value === "string")
|
|
858
|
+
this.append(name, value);
|
|
859
|
+
else
|
|
860
|
+
this.append(name, value, value.name);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
append(name, value, filename) {
|
|
865
|
+
this.#entries.push([String(name), formEntry(value, filename)]);
|
|
866
|
+
}
|
|
867
|
+
set(name, value, filename) {
|
|
868
|
+
const entry = formEntry(value, filename);
|
|
869
|
+
const next = [];
|
|
870
|
+
let placed = false;
|
|
871
|
+
for (const pair of this.#entries) {
|
|
872
|
+
if (pair[0] !== name)
|
|
873
|
+
next.push(pair);
|
|
874
|
+
else if (!placed) {
|
|
875
|
+
next.push([String(name), entry]);
|
|
876
|
+
placed = true;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (!placed)
|
|
880
|
+
next.push([String(name), entry]);
|
|
881
|
+
this.#entries = next;
|
|
882
|
+
}
|
|
883
|
+
get(name) {
|
|
884
|
+
const found = this.#entries.find(([key]) => key === name);
|
|
885
|
+
return found ? found[1] : null;
|
|
886
|
+
}
|
|
887
|
+
getAll(name) {
|
|
888
|
+
return this.#entries.filter(([key]) => key === name).map(([, value]) => value);
|
|
889
|
+
}
|
|
890
|
+
has(name) {
|
|
891
|
+
return this.#entries.some(([key]) => key === name);
|
|
892
|
+
}
|
|
893
|
+
delete(name) {
|
|
894
|
+
this.#entries = this.#entries.filter(([key]) => key !== name);
|
|
895
|
+
}
|
|
896
|
+
*entries() {
|
|
897
|
+
yield* this.#entries;
|
|
898
|
+
}
|
|
899
|
+
*keys() {
|
|
900
|
+
for (const [name] of this.#entries)
|
|
901
|
+
yield name;
|
|
902
|
+
}
|
|
903
|
+
*values() {
|
|
904
|
+
for (const [, value] of this.#entries)
|
|
905
|
+
yield value;
|
|
906
|
+
}
|
|
907
|
+
forEach(callback) {
|
|
908
|
+
const pairs = this.#entries;
|
|
909
|
+
for (let index = 0;index < pairs.length; index++) {
|
|
910
|
+
const pair = pairs[index];
|
|
911
|
+
callback(pair[1], pair[0], this);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
[Symbol.iterator]() {
|
|
915
|
+
return this.entries();
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
function formEntry(value, filename) {
|
|
919
|
+
if (typeof value !== "object" || value === null)
|
|
920
|
+
return String(value);
|
|
921
|
+
if (!(value instanceof BlobPolyfill)) {
|
|
922
|
+
throw new TypeError("FormData file fields must be Blob or File objects");
|
|
923
|
+
}
|
|
924
|
+
if (value instanceof FilePolyfill && filename === undefined)
|
|
925
|
+
return value;
|
|
926
|
+
return new FilePolyfill([readBlobBytes(value)], filename ?? "blob", { type: value.type });
|
|
927
|
+
}
|
|
928
|
+
function isFormDataBody(body) {
|
|
929
|
+
if (body instanceof FormDataPolyfill)
|
|
930
|
+
return true;
|
|
931
|
+
const ctor = globalThis.FormData;
|
|
932
|
+
return typeof ctor === "function" && body instanceof ctor;
|
|
933
|
+
}
|
|
934
|
+
var MULTIPART_BOUNDARY_ATTEMPTS = 32;
|
|
935
|
+
function multipartBoundary(attempt) {
|
|
936
|
+
return `----diFormBoundary${attempt.toString(16).padStart(8, "0")}`;
|
|
937
|
+
}
|
|
938
|
+
function escapeDisposition(value) {
|
|
939
|
+
return value.replaceAll('"', "%22").replaceAll("\r", "").replaceAll(`
|
|
940
|
+
`, "");
|
|
941
|
+
}
|
|
942
|
+
function encodeMultipart(entries, boundary) {
|
|
943
|
+
const encoder = new TextEncoderPolyfill;
|
|
944
|
+
const chunks = [];
|
|
945
|
+
const push = (text) => chunks.push(encoder.encode(text));
|
|
946
|
+
for (const [name, value] of entries) {
|
|
947
|
+
const quotedName = escapeDisposition(name);
|
|
948
|
+
push(`--${boundary}\r
|
|
949
|
+
`);
|
|
950
|
+
if (typeof value === "string") {
|
|
951
|
+
push(`Content-Disposition: form-data; name="${quotedName}"\r
|
|
952
|
+
\r
|
|
953
|
+
${value}\r
|
|
954
|
+
`);
|
|
955
|
+
} else if (value instanceof BlobPolyfill) {
|
|
956
|
+
const type = value.type === "" ? "" : `Content-Type: ${value.type}\r
|
|
957
|
+
`;
|
|
958
|
+
const filename = escapeDisposition(value.name);
|
|
959
|
+
push(`Content-Disposition: form-data; name="${quotedName}"; filename="${filename}"\r
|
|
960
|
+
` + `${type}\r
|
|
961
|
+
`);
|
|
962
|
+
chunks.push(readBlobBytes(value));
|
|
963
|
+
push(`\r
|
|
964
|
+
`);
|
|
965
|
+
} else {
|
|
966
|
+
throw new TypeError("FormData file fields must be Blob or File objects");
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
push(`--${boundary}--\r
|
|
970
|
+
`);
|
|
971
|
+
return concatBytes(chunks);
|
|
972
|
+
}
|
|
973
|
+
function countOccurrences(haystack, needle) {
|
|
974
|
+
let count = 0;
|
|
975
|
+
let from = 0;
|
|
976
|
+
while (from <= haystack.length - needle.length) {
|
|
977
|
+
const found = indexOfBytes(haystack, needle, from);
|
|
978
|
+
if (found < 0)
|
|
979
|
+
break;
|
|
980
|
+
count += 1;
|
|
981
|
+
from = found + needle.length;
|
|
982
|
+
}
|
|
983
|
+
return count;
|
|
984
|
+
}
|
|
985
|
+
function encodeFormBody(body) {
|
|
986
|
+
const entries = [...body.entries()];
|
|
987
|
+
const encoder = new TextEncoderPolyfill;
|
|
988
|
+
for (let attempt = 0;attempt < MULTIPART_BOUNDARY_ATTEMPTS; attempt++) {
|
|
989
|
+
const boundary = multipartBoundary(attempt);
|
|
990
|
+
const bytes = encodeMultipart(entries, boundary);
|
|
991
|
+
const markers = countOccurrences(bytes, encoder.encode(`--${boundary}`));
|
|
992
|
+
if (markers === entries.length + 1) {
|
|
993
|
+
return { bytes, contentType: `multipart/form-data; boundary=${boundary}` };
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
throw new TypeError("multipart boundary collides with form contents");
|
|
997
|
+
}
|
|
998
|
+
function bodyFromInit(body, headers) {
|
|
999
|
+
if (!isFormDataBody(body))
|
|
1000
|
+
return toBodySource(body);
|
|
1001
|
+
const encoded = encodeFormBody(body);
|
|
1002
|
+
if (!headers.has("content-type"))
|
|
1003
|
+
headers.set("content-type", encoded.contentType);
|
|
1004
|
+
return encoded.bytes;
|
|
1005
|
+
}
|
|
1006
|
+
function indexOfBytes(haystack, needle, from = 0) {
|
|
1007
|
+
if (needle.length === 0)
|
|
1008
|
+
return from;
|
|
1009
|
+
const end = haystack.length - needle.length;
|
|
1010
|
+
for (let i = from;i <= end; i++) {
|
|
1011
|
+
let matched = true;
|
|
1012
|
+
for (let j = 0;j < needle.length; j++) {
|
|
1013
|
+
if (haystack[i + j] !== needle[j]) {
|
|
1014
|
+
matched = false;
|
|
1015
|
+
break;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (matched)
|
|
1019
|
+
return i;
|
|
1020
|
+
}
|
|
1021
|
+
return -1;
|
|
1022
|
+
}
|
|
1023
|
+
function parseContentType(header) {
|
|
1024
|
+
if (header == null || header === "")
|
|
1025
|
+
return { type: "" };
|
|
1026
|
+
const [rawType, ...params] = header.split(";");
|
|
1027
|
+
let boundary;
|
|
1028
|
+
for (const param of params) {
|
|
1029
|
+
const eq = param.indexOf("=");
|
|
1030
|
+
if (eq === -1)
|
|
1031
|
+
continue;
|
|
1032
|
+
if (param.slice(0, eq).trim().toLowerCase() !== "boundary")
|
|
1033
|
+
continue;
|
|
1034
|
+
let value = param.slice(eq + 1).trim();
|
|
1035
|
+
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
|
|
1036
|
+
value = value.slice(1, -1);
|
|
1037
|
+
}
|
|
1038
|
+
boundary = value;
|
|
1039
|
+
}
|
|
1040
|
+
return { type: (rawType ?? "").trim().toLowerCase(), boundary };
|
|
1041
|
+
}
|
|
1042
|
+
function headerValue(block, name) {
|
|
1043
|
+
const lower = name.toLowerCase();
|
|
1044
|
+
for (const line of block.split(`\r
|
|
1045
|
+
`)) {
|
|
1046
|
+
const colon = line.indexOf(":");
|
|
1047
|
+
if (colon === -1)
|
|
1048
|
+
continue;
|
|
1049
|
+
if (line.slice(0, colon).trim().toLowerCase() !== lower)
|
|
1050
|
+
continue;
|
|
1051
|
+
return line.slice(colon + 1).trim();
|
|
1052
|
+
}
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
function dispositionParam(value, param) {
|
|
1056
|
+
for (const part of value.split(";").slice(1)) {
|
|
1057
|
+
const eq = part.indexOf("=");
|
|
1058
|
+
if (eq === -1)
|
|
1059
|
+
continue;
|
|
1060
|
+
if (part.slice(0, eq).trim().toLowerCase() !== param)
|
|
1061
|
+
continue;
|
|
1062
|
+
let raw = part.slice(eq + 1).trim();
|
|
1063
|
+
if (raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2)
|
|
1064
|
+
raw = raw.slice(1, -1);
|
|
1065
|
+
return raw.replaceAll("%22", '"');
|
|
1066
|
+
}
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
function parseUrlEncoded(bytes) {
|
|
1070
|
+
const form = new FormDataPolyfill;
|
|
1071
|
+
const params = new URLSearchParamsPolyfill(new TextDecoderPolyfill().decode(bytes));
|
|
1072
|
+
for (const [name, value] of params)
|
|
1073
|
+
form.append(name, value);
|
|
1074
|
+
return form;
|
|
1075
|
+
}
|
|
1076
|
+
function parseMultipart(bytes, boundary) {
|
|
1077
|
+
const encoder = new TextEncoderPolyfill;
|
|
1078
|
+
const decoder = new TextDecoderPolyfill;
|
|
1079
|
+
const dashBoundary = encoder.encode(`--${boundary}`);
|
|
1080
|
+
const headerBreak = encoder.encode(`\r
|
|
1081
|
+
\r
|
|
1082
|
+
`);
|
|
1083
|
+
const nextMarker = encoder.encode(`\r
|
|
1084
|
+
--${boundary}`);
|
|
1085
|
+
const form = new FormDataPolyfill;
|
|
1086
|
+
let position = indexOfBytes(bytes, dashBoundary);
|
|
1087
|
+
if (position < 0)
|
|
1088
|
+
throw new TypeError("multipart/form-data body is missing its boundary");
|
|
1089
|
+
position += dashBoundary.length;
|
|
1090
|
+
while (position < bytes.length) {
|
|
1091
|
+
if (bytes[position] === 45 && bytes[position + 1] === 45)
|
|
1092
|
+
break;
|
|
1093
|
+
if (bytes[position] !== 13 || bytes[position + 1] !== 10) {
|
|
1094
|
+
throw new TypeError("multipart/form-data body is malformed");
|
|
1095
|
+
}
|
|
1096
|
+
position += 2;
|
|
1097
|
+
const headerEnd = indexOfBytes(bytes, headerBreak, position);
|
|
1098
|
+
if (headerEnd < 0)
|
|
1099
|
+
throw new TypeError("multipart/form-data part is missing its headers");
|
|
1100
|
+
const headerText = decoder.decode(bytes.subarray(position, headerEnd));
|
|
1101
|
+
position = headerEnd + headerBreak.length;
|
|
1102
|
+
const next = indexOfBytes(bytes, nextMarker, position);
|
|
1103
|
+
if (next < 0)
|
|
1104
|
+
throw new TypeError("multipart/form-data part is truncated");
|
|
1105
|
+
const partBody = bytes.subarray(position, next);
|
|
1106
|
+
position = next + 2 + dashBoundary.length;
|
|
1107
|
+
const disposition = headerValue(headerText, "content-disposition");
|
|
1108
|
+
if (disposition === undefined)
|
|
1109
|
+
continue;
|
|
1110
|
+
const name = dispositionParam(disposition, "name");
|
|
1111
|
+
if (name === undefined)
|
|
1112
|
+
continue;
|
|
1113
|
+
const filename = dispositionParam(disposition, "filename");
|
|
1114
|
+
if (filename === undefined) {
|
|
1115
|
+
form.append(name, decoder.decode(partBody));
|
|
1116
|
+
} else {
|
|
1117
|
+
const type = headerValue(headerText, "content-type") ?? "";
|
|
1118
|
+
form.append(name, new BlobPolyfill([partBody], { type }), filename);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
return form;
|
|
1122
|
+
}
|
|
1123
|
+
function formDataFromBody(headers, buffer) {
|
|
1124
|
+
const parsed = parseContentType(headers.get("content-type"));
|
|
1125
|
+
const bytes = new Uint8Array(buffer);
|
|
1126
|
+
if (parsed.type === "application/x-www-form-urlencoded")
|
|
1127
|
+
return parseUrlEncoded(bytes);
|
|
1128
|
+
if (parsed.type === "multipart/form-data") {
|
|
1129
|
+
if (parsed.boundary === undefined || parsed.boundary === "") {
|
|
1130
|
+
throw new TypeError("multipart/form-data body is missing a boundary");
|
|
1131
|
+
}
|
|
1132
|
+
return parseMultipart(bytes, parsed.boundary);
|
|
1133
|
+
}
|
|
1134
|
+
throw new TypeError("Content-Type is not multipart/form-data or application/x-www-form-urlencoded");
|
|
740
1135
|
}
|
|
741
1136
|
var noopConsole = {
|
|
742
1137
|
log() {},
|
|
@@ -745,25 +1140,77 @@ var noopConsole = {
|
|
|
745
1140
|
error() {},
|
|
746
1141
|
debug() {}
|
|
747
1142
|
};
|
|
1143
|
+
var URL_SEARCH_PARAMS_METHODS = [
|
|
1144
|
+
"get",
|
|
1145
|
+
"getAll",
|
|
1146
|
+
"has",
|
|
1147
|
+
"append",
|
|
1148
|
+
"set",
|
|
1149
|
+
"delete",
|
|
1150
|
+
"toString",
|
|
1151
|
+
"entries",
|
|
1152
|
+
Symbol.iterator
|
|
1153
|
+
];
|
|
1154
|
+
var HEADERS_METHODS = [
|
|
1155
|
+
"append",
|
|
1156
|
+
"set",
|
|
1157
|
+
"get",
|
|
1158
|
+
"has",
|
|
1159
|
+
"delete",
|
|
1160
|
+
"entries",
|
|
1161
|
+
"forEach",
|
|
1162
|
+
"keys",
|
|
1163
|
+
"values",
|
|
1164
|
+
"getSetCookie",
|
|
1165
|
+
Symbol.iterator
|
|
1166
|
+
];
|
|
1167
|
+
var FORM_DATA_METHODS = [
|
|
1168
|
+
"append",
|
|
1169
|
+
"get",
|
|
1170
|
+
"getAll",
|
|
1171
|
+
"has",
|
|
1172
|
+
"set",
|
|
1173
|
+
"delete",
|
|
1174
|
+
"entries",
|
|
1175
|
+
Symbol.iterator
|
|
1176
|
+
];
|
|
1177
|
+
function hasRequiredMethods(value, methods) {
|
|
1178
|
+
if (typeof value !== "function")
|
|
1179
|
+
return false;
|
|
1180
|
+
const prototype = value.prototype;
|
|
1181
|
+
if (prototype != null && methods.every((method) => typeof prototype[method] === "function")) {
|
|
1182
|
+
return true;
|
|
1183
|
+
}
|
|
1184
|
+
try {
|
|
1185
|
+
const instance = new value;
|
|
1186
|
+
return methods.every((method) => typeof instance[method] === "function");
|
|
1187
|
+
} catch {
|
|
1188
|
+
return false;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function installWebConstructor(global, name, implementation, methods, force) {
|
|
1192
|
+
if (force || !hasRequiredMethods(global[name], methods))
|
|
1193
|
+
global[name] = implementation;
|
|
1194
|
+
}
|
|
748
1195
|
function installFetchRuntime(force = false) {
|
|
749
1196
|
const global = globalThis;
|
|
750
1197
|
if (force || typeof global.TextEncoder !== "function")
|
|
751
1198
|
global.TextEncoder = TextEncoderPolyfill;
|
|
752
1199
|
if (force || typeof global.TextDecoder !== "function")
|
|
753
1200
|
global.TextDecoder = TextDecoderPolyfill;
|
|
754
|
-
|
|
755
|
-
global.URLSearchParams = URLSearchParamsPolyfill;
|
|
756
|
-
}
|
|
1201
|
+
installWebConstructor(global, "URLSearchParams", URLSearchParamsPolyfill, URL_SEARCH_PARAMS_METHODS, force);
|
|
757
1202
|
if (force || typeof global.URL !== "function")
|
|
758
1203
|
global.URL = URLPolyfill;
|
|
759
|
-
|
|
760
|
-
global.Headers = HeadersPolyfill;
|
|
1204
|
+
installWebConstructor(global, "Headers", HeadersPolyfill, HEADERS_METHODS, force);
|
|
761
1205
|
if (force || typeof global.Request !== "function")
|
|
762
1206
|
global.Request = RequestPolyfill;
|
|
763
1207
|
if (force || typeof global.Response !== "function")
|
|
764
1208
|
global.Response = ResponsePolyfill;
|
|
1209
|
+
installWebConstructor(global, "FormData", FormDataPolyfill, FORM_DATA_METHODS, force);
|
|
765
1210
|
if (force || typeof global.Blob !== "function")
|
|
766
1211
|
global.Blob = BlobPolyfill;
|
|
1212
|
+
if (force || typeof global.File !== "function")
|
|
1213
|
+
global.File = FilePolyfill;
|
|
767
1214
|
if (force || typeof global.AbortSignal !== "function")
|
|
768
1215
|
global.AbortSignal = AbortSignalPolyfill;
|
|
769
1216
|
if (force || typeof global.AbortController !== "function") {
|
|
@@ -794,6 +1241,11 @@ function pickFutureType(factory, preferred) {
|
|
|
794
1241
|
if (name in factory)
|
|
795
1242
|
return factory[name];
|
|
796
1243
|
}
|
|
1244
|
+
if (preferred.includes("RESULT_VOID_ERROR_CODE")) {
|
|
1245
|
+
const httpVoid = Object.keys(factory).find((key) => key.startsWith("RESULT_VOID_WASI_HTTP_TYPES_") && key.endsWith("_ERROR_CODE"));
|
|
1246
|
+
if (httpVoid !== undefined)
|
|
1247
|
+
return factory[httpVoid];
|
|
1248
|
+
}
|
|
797
1249
|
const fallback = Object.keys(factory).find((key) => key !== "types" && key !== "from");
|
|
798
1250
|
return fallback === undefined ? undefined : factory[fallback];
|
|
799
1251
|
}
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* WASI 0.3 async HTTP (`wasi:http/handler#handle`) is the component interface,
|
|
5
5
|
* not the WHATWG Fetch API. componentize-qjs embeds QuickJS, which has neither
|
|
6
|
-
* `Request`/`Response`
|
|
7
|
-
* Fetch; install these polyfills
|
|
8
|
-
* application contract (`new Response(...)
|
|
6
|
+
* `Request`/`Response`, `URLSearchParams`, or `FormData`. StarlingMonkey does
|
|
7
|
+
* provide Fetch; install these polyfills when a global is missing or only
|
|
8
|
+
* partly implemented so the application contract (`new Response(...)`,
|
|
9
|
+
* `request.formData()`) stays unchanged.
|
|
9
10
|
*/
|
|
10
11
|
export declare class TextEncoderPolyfill {
|
|
11
12
|
encode(input?: string): Uint8Array;
|
|
@@ -16,12 +17,20 @@ export declare class TextDecoderPolyfill {
|
|
|
16
17
|
export declare class URLSearchParamsPolyfill {
|
|
17
18
|
#private;
|
|
18
19
|
constructor(init?: string | URLSearchParamsPolyfill | Record<string, string>);
|
|
20
|
+
/** Lets `URL` refresh `search` and `href` after `set`, `append`, or `delete`. */
|
|
21
|
+
bind(onChange: () => void): void;
|
|
19
22
|
get(name: string): string | null;
|
|
23
|
+
getAll(name: string): string[];
|
|
24
|
+
has(name: string): boolean;
|
|
25
|
+
append(name: string, value: string): void;
|
|
26
|
+
set(name: string, value: string): void;
|
|
27
|
+
delete(name: string): void;
|
|
20
28
|
entries(): IterableIterator<[string, string]>;
|
|
21
29
|
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
22
30
|
toString(): string;
|
|
23
31
|
}
|
|
24
32
|
export declare class URLPolyfill {
|
|
33
|
+
#private;
|
|
25
34
|
href: string;
|
|
26
35
|
protocol: string;
|
|
27
36
|
hostname: string;
|
|
@@ -45,6 +54,10 @@ export declare class HeadersPolyfill {
|
|
|
45
54
|
delete(name: string): void;
|
|
46
55
|
entries(): IterableIterator<[string, string]>;
|
|
47
56
|
forEach(callback: (value: string, name: string, parent: HeadersPolyfill) => void): void;
|
|
57
|
+
keys(): IterableIterator<string>;
|
|
58
|
+
values(): IterableIterator<string>;
|
|
59
|
+
/** Every `Set-Cookie` value, in order, without comma-joining. */
|
|
60
|
+
getSetCookie(): string[];
|
|
48
61
|
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
49
62
|
}
|
|
50
63
|
export declare function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
|
|
@@ -62,6 +75,7 @@ export declare class RequestPolyfill {
|
|
|
62
75
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
63
76
|
text(): Promise<string>;
|
|
64
77
|
json(): Promise<unknown>;
|
|
78
|
+
formData(): Promise<FormDataPolyfill>;
|
|
65
79
|
}
|
|
66
80
|
export declare class ResponsePolyfill {
|
|
67
81
|
#private;
|
|
@@ -75,6 +89,7 @@ export declare class ResponsePolyfill {
|
|
|
75
89
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
76
90
|
text(): Promise<string>;
|
|
77
91
|
json(): Promise<unknown>;
|
|
92
|
+
formData(): Promise<FormDataPolyfill>;
|
|
78
93
|
}
|
|
79
94
|
export declare class AbortSignalPolyfill {
|
|
80
95
|
#private;
|
|
@@ -98,7 +113,7 @@ export declare class AbortControllerPolyfill {
|
|
|
98
113
|
readonly signal: AbortSignalPolyfill;
|
|
99
114
|
abort(reason?: unknown): void;
|
|
100
115
|
}
|
|
101
|
-
type BlobPart = string | ArrayBuffer | ArrayBufferView;
|
|
116
|
+
type BlobPart = string | ArrayBuffer | ArrayBufferView | BlobPolyfill;
|
|
102
117
|
/** QuickJS has no Blob. unenv's File extends it while Node streams load. */
|
|
103
118
|
export declare class BlobPolyfill {
|
|
104
119
|
readonly size: number;
|
|
@@ -106,6 +121,33 @@ export declare class BlobPolyfill {
|
|
|
106
121
|
constructor(parts?: BlobPart[], options?: {
|
|
107
122
|
type?: string;
|
|
108
123
|
});
|
|
124
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
125
|
+
text(): Promise<string>;
|
|
126
|
+
}
|
|
127
|
+
/** File entry returned by `FormData` for multipart parts that carry a filename. */
|
|
128
|
+
export declare class FilePolyfill extends BlobPolyfill {
|
|
129
|
+
readonly name: string;
|
|
130
|
+
readonly lastModified: number;
|
|
131
|
+
constructor(parts: BlobPart[], name: string, options?: {
|
|
132
|
+
type?: string;
|
|
133
|
+
lastModified?: number;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
type FormDataEntryValue = string | FilePolyfill;
|
|
137
|
+
export declare class FormDataPolyfill {
|
|
138
|
+
#private;
|
|
139
|
+
constructor(init?: FormDataPolyfill);
|
|
140
|
+
append(name: string, value: string | BlobPolyfill, filename?: string): void;
|
|
141
|
+
set(name: string, value: string | BlobPolyfill, filename?: string): void;
|
|
142
|
+
get(name: string): FormDataEntryValue | null;
|
|
143
|
+
getAll(name: string): FormDataEntryValue[];
|
|
144
|
+
has(name: string): boolean;
|
|
145
|
+
delete(name: string): void;
|
|
146
|
+
entries(): IterableIterator<[string, FormDataEntryValue]>;
|
|
147
|
+
keys(): IterableIterator<string>;
|
|
148
|
+
values(): IterableIterator<FormDataEntryValue>;
|
|
149
|
+
forEach(callback: (value: FormDataEntryValue, name: string, parent: FormDataPolyfill) => void): void;
|
|
150
|
+
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
|
|
109
151
|
}
|
|
110
152
|
export declare function installFetchRuntime(force?: boolean): void;
|
|
111
153
|
export {};
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* WASI 0.3 async HTTP (`wasi:http/handler#handle`) is the component interface,
|
|
5
5
|
* not the WHATWG Fetch API. componentize-qjs embeds QuickJS, which has neither
|
|
6
|
-
* `Request`/`Response`
|
|
7
|
-
* Fetch; install these polyfills
|
|
8
|
-
* application contract (`new Response(...)
|
|
6
|
+
* `Request`/`Response`, `URLSearchParams`, or `FormData`. StarlingMonkey does
|
|
7
|
+
* provide Fetch; install these polyfills when a global is missing or only
|
|
8
|
+
* partly implemented so the application contract (`new Response(...)`,
|
|
9
|
+
* `request.formData()`) stays unchanged.
|
|
9
10
|
*/
|
|
10
11
|
export class TextEncoderPolyfill {
|
|
11
12
|
encode(input = '') {
|
|
@@ -31,14 +32,16 @@ export class TextDecoderPolyfill {
|
|
|
31
32
|
}
|
|
32
33
|
export class URLSearchParamsPolyfill {
|
|
33
34
|
#pairs = [];
|
|
35
|
+
#onChange;
|
|
34
36
|
constructor(init) {
|
|
35
37
|
if (typeof init === 'string') {
|
|
36
38
|
const query = init.startsWith('?') ? init.slice(1) : init;
|
|
37
39
|
if (query !== '') {
|
|
38
40
|
for (const part of query.split('&')) {
|
|
39
41
|
const eq = part.indexOf('=');
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
+
const decode = (text) => decodeURIComponent(text.replaceAll('+', ' '));
|
|
43
|
+
const name = decode(eq === -1 ? part : part.slice(0, eq));
|
|
44
|
+
const value = decode(eq === -1 ? '' : part.slice(eq + 1));
|
|
42
45
|
this.#pairs.push([name, value]);
|
|
43
46
|
}
|
|
44
47
|
}
|
|
@@ -51,10 +54,44 @@ export class URLSearchParamsPolyfill {
|
|
|
51
54
|
this.#pairs.push([name, String(value)]);
|
|
52
55
|
}
|
|
53
56
|
}
|
|
57
|
+
/** Lets `URL` refresh `search` and `href` after `set`, `append`, or `delete`. */
|
|
58
|
+
bind(onChange) {
|
|
59
|
+
this.#onChange = onChange;
|
|
60
|
+
}
|
|
54
61
|
get(name) {
|
|
55
62
|
const found = this.#pairs.find(([key]) => key === name);
|
|
56
63
|
return found ? found[1] : null;
|
|
57
64
|
}
|
|
65
|
+
getAll(name) {
|
|
66
|
+
return this.#pairs.filter(([key]) => key === name).map(([, value]) => value);
|
|
67
|
+
}
|
|
68
|
+
has(name) {
|
|
69
|
+
return this.#pairs.some(([key]) => key === name);
|
|
70
|
+
}
|
|
71
|
+
append(name, value) {
|
|
72
|
+
this.#pairs.push([name, String(value)]);
|
|
73
|
+
this.#onChange?.();
|
|
74
|
+
}
|
|
75
|
+
set(name, value) {
|
|
76
|
+
const next = [];
|
|
77
|
+
let placed = false;
|
|
78
|
+
for (const pair of this.#pairs) {
|
|
79
|
+
if (pair[0] !== name)
|
|
80
|
+
next.push(pair);
|
|
81
|
+
else if (!placed) {
|
|
82
|
+
next.push([name, String(value)]);
|
|
83
|
+
placed = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!placed)
|
|
87
|
+
next.push([name, String(value)]);
|
|
88
|
+
this.#pairs = next;
|
|
89
|
+
this.#onChange?.();
|
|
90
|
+
}
|
|
91
|
+
delete(name) {
|
|
92
|
+
this.#pairs = this.#pairs.filter(([key]) => key !== name);
|
|
93
|
+
this.#onChange?.();
|
|
94
|
+
}
|
|
58
95
|
*entries() {
|
|
59
96
|
yield* this.#pairs;
|
|
60
97
|
}
|
|
@@ -92,6 +129,12 @@ export class URLPolyfill {
|
|
|
92
129
|
this.origin = `${this.protocol}//${this.host}`;
|
|
93
130
|
this.href = resolved;
|
|
94
131
|
this.searchParams = new URLSearchParamsPolyfill(this.search);
|
|
132
|
+
this.searchParams.bind(() => this.#sync());
|
|
133
|
+
}
|
|
134
|
+
#sync() {
|
|
135
|
+
const query = this.searchParams.toString();
|
|
136
|
+
this.search = query === '' ? '' : `?${query}`;
|
|
137
|
+
this.href = `${this.origin}${this.pathname}${this.search}${this.hash}`;
|
|
95
138
|
}
|
|
96
139
|
toString() {
|
|
97
140
|
return this.href;
|
|
@@ -156,6 +199,37 @@ export class HeadersPolyfill {
|
|
|
156
199
|
for (const [name, value] of this.entries())
|
|
157
200
|
callback(value, name, this);
|
|
158
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Fetch's sort-and-combine list: one combined value per name, except
|
|
204
|
+
* `set-cookie`, which stays one pair per value. `entries()` still yields
|
|
205
|
+
* each stored value so the HTTP adapter can forward multi-value headers.
|
|
206
|
+
*/
|
|
207
|
+
*#combined() {
|
|
208
|
+
for (const name of [...this.#map.keys()].sort((left, right) => left.localeCompare(right))) {
|
|
209
|
+
const values = this.#map.get(name) ?? [];
|
|
210
|
+
if (values.length === 0)
|
|
211
|
+
continue;
|
|
212
|
+
if (name === 'set-cookie') {
|
|
213
|
+
for (const value of values)
|
|
214
|
+
yield [name, value];
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
yield [name, values.join(', ')];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
*keys() {
|
|
222
|
+
for (const [name] of this.#combined())
|
|
223
|
+
yield name;
|
|
224
|
+
}
|
|
225
|
+
*values() {
|
|
226
|
+
for (const [, value] of this.#combined())
|
|
227
|
+
yield value;
|
|
228
|
+
}
|
|
229
|
+
/** Every `Set-Cookie` value, in order, without comma-joining. */
|
|
230
|
+
getSetCookie() {
|
|
231
|
+
return [...(this.#map.get('set-cookie') ?? [])];
|
|
232
|
+
}
|
|
159
233
|
[Symbol.iterator]() {
|
|
160
234
|
return this.entries();
|
|
161
235
|
}
|
|
@@ -221,13 +295,13 @@ export class RequestPolyfill {
|
|
|
221
295
|
this.method = String(init.method ?? input.method);
|
|
222
296
|
this.url = input.url;
|
|
223
297
|
this.headers = new HeadersPolyfill(init.headers ?? input.headers);
|
|
224
|
-
this.#body = init.body === undefined ? input.#body :
|
|
298
|
+
this.#body = init.body === undefined ? input.#body : bodyFromInit(init.body, this.headers);
|
|
225
299
|
}
|
|
226
300
|
else {
|
|
227
301
|
this.method = String(init.method ?? 'GET').toUpperCase();
|
|
228
302
|
this.url = String(input);
|
|
229
303
|
this.headers = new HeadersPolyfill(init.headers);
|
|
230
|
-
this.#body =
|
|
304
|
+
this.#body = bodyFromInit(init.body, this.headers);
|
|
231
305
|
}
|
|
232
306
|
}
|
|
233
307
|
get body() {
|
|
@@ -248,6 +322,9 @@ export class RequestPolyfill {
|
|
|
248
322
|
async json() {
|
|
249
323
|
return JSON.parse(await this.text());
|
|
250
324
|
}
|
|
325
|
+
async formData() {
|
|
326
|
+
return formDataFromBody(this.headers, await this.arrayBuffer());
|
|
327
|
+
}
|
|
251
328
|
}
|
|
252
329
|
export class ResponsePolyfill {
|
|
253
330
|
status;
|
|
@@ -260,7 +337,7 @@ export class ResponsePolyfill {
|
|
|
260
337
|
this.statusText = String(init.statusText ?? '');
|
|
261
338
|
this.headers = new HeadersPolyfill(init.headers);
|
|
262
339
|
this.ok = this.status >= 200 && this.status < 300;
|
|
263
|
-
this.#body =
|
|
340
|
+
this.#body = bodyFromInit(body, this.headers);
|
|
264
341
|
}
|
|
265
342
|
static json(data, init = {}) {
|
|
266
343
|
const headers = new HeadersPolyfill(init.headers);
|
|
@@ -282,6 +359,9 @@ export class ResponsePolyfill {
|
|
|
282
359
|
async json() {
|
|
283
360
|
return JSON.parse(await this.text());
|
|
284
361
|
}
|
|
362
|
+
async formData() {
|
|
363
|
+
return formDataFromBody(this.headers, await this.arrayBuffer());
|
|
364
|
+
}
|
|
285
365
|
}
|
|
286
366
|
export class AbortSignalPolyfill {
|
|
287
367
|
aborted = false;
|
|
@@ -319,21 +399,335 @@ export class AbortControllerPolyfill {
|
|
|
319
399
|
this.signal.dispatchEvent(event);
|
|
320
400
|
}
|
|
321
401
|
}
|
|
402
|
+
const blobContents = new WeakMap();
|
|
403
|
+
function bytesOfPart(part) {
|
|
404
|
+
if (typeof part === 'string')
|
|
405
|
+
return new TextEncoder().encode(part);
|
|
406
|
+
if (part instanceof BlobPolyfill)
|
|
407
|
+
return readBlobBytes(part).slice();
|
|
408
|
+
if (part instanceof ArrayBuffer)
|
|
409
|
+
return new Uint8Array(part);
|
|
410
|
+
if (ArrayBuffer.isView(part)) {
|
|
411
|
+
return new Uint8Array(part.buffer, part.byteOffset, part.byteLength);
|
|
412
|
+
}
|
|
413
|
+
throw new TypeError('Blob parts must be strings, buffers, or Blob objects');
|
|
414
|
+
}
|
|
415
|
+
function concatBytes(chunks) {
|
|
416
|
+
let total = 0;
|
|
417
|
+
for (const chunk of chunks)
|
|
418
|
+
total += chunk.byteLength;
|
|
419
|
+
const bytes = new Uint8Array(total);
|
|
420
|
+
let offset = 0;
|
|
421
|
+
for (const chunk of chunks) {
|
|
422
|
+
bytes.set(chunk, offset);
|
|
423
|
+
offset += chunk.byteLength;
|
|
424
|
+
}
|
|
425
|
+
return bytes;
|
|
426
|
+
}
|
|
427
|
+
function readBlobBytes(blob) {
|
|
428
|
+
return blobContents.get(blob) ?? new Uint8Array();
|
|
429
|
+
}
|
|
322
430
|
/** QuickJS has no Blob. unenv's File extends it while Node streams load. */
|
|
323
431
|
export class BlobPolyfill {
|
|
324
432
|
size;
|
|
325
433
|
type;
|
|
326
434
|
constructor(parts = [], options) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
size += new TextEncoder().encode(part).length;
|
|
331
|
-
else
|
|
332
|
-
size += part.byteLength;
|
|
333
|
-
}
|
|
334
|
-
this.size = size;
|
|
435
|
+
const bytes = concatBytes(parts.map((part) => bytesOfPart(part)));
|
|
436
|
+
blobContents.set(this, bytes);
|
|
437
|
+
this.size = bytes.byteLength;
|
|
335
438
|
this.type = options?.type ?? '';
|
|
336
439
|
}
|
|
440
|
+
async arrayBuffer() {
|
|
441
|
+
return readBlobBytes(this).slice().buffer;
|
|
442
|
+
}
|
|
443
|
+
async text() {
|
|
444
|
+
return new TextDecoderPolyfill().decode(readBlobBytes(this));
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
/** File entry returned by `FormData` for multipart parts that carry a filename. */
|
|
448
|
+
export class FilePolyfill extends BlobPolyfill {
|
|
449
|
+
name;
|
|
450
|
+
lastModified;
|
|
451
|
+
constructor(parts, name, options) {
|
|
452
|
+
super(parts, options);
|
|
453
|
+
this.name = String(name);
|
|
454
|
+
this.lastModified = options?.lastModified ?? Date.now();
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
export class FormDataPolyfill {
|
|
458
|
+
#entries = [];
|
|
459
|
+
constructor(init) {
|
|
460
|
+
if (init instanceof FormDataPolyfill) {
|
|
461
|
+
for (const [name, value] of init.entries()) {
|
|
462
|
+
if (typeof value === 'string')
|
|
463
|
+
this.append(name, value);
|
|
464
|
+
else
|
|
465
|
+
this.append(name, value, value.name);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
append(name, value, filename) {
|
|
470
|
+
this.#entries.push([String(name), formEntry(value, filename)]);
|
|
471
|
+
}
|
|
472
|
+
set(name, value, filename) {
|
|
473
|
+
const entry = formEntry(value, filename);
|
|
474
|
+
const next = [];
|
|
475
|
+
let placed = false;
|
|
476
|
+
for (const pair of this.#entries) {
|
|
477
|
+
if (pair[0] !== name)
|
|
478
|
+
next.push(pair);
|
|
479
|
+
else if (!placed) {
|
|
480
|
+
next.push([String(name), entry]);
|
|
481
|
+
placed = true;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (!placed)
|
|
485
|
+
next.push([String(name), entry]);
|
|
486
|
+
this.#entries = next;
|
|
487
|
+
}
|
|
488
|
+
get(name) {
|
|
489
|
+
const found = this.#entries.find(([key]) => key === name);
|
|
490
|
+
return found ? found[1] : null;
|
|
491
|
+
}
|
|
492
|
+
getAll(name) {
|
|
493
|
+
return this.#entries.filter(([key]) => key === name).map(([, value]) => value);
|
|
494
|
+
}
|
|
495
|
+
has(name) {
|
|
496
|
+
return this.#entries.some(([key]) => key === name);
|
|
497
|
+
}
|
|
498
|
+
delete(name) {
|
|
499
|
+
this.#entries = this.#entries.filter(([key]) => key !== name);
|
|
500
|
+
}
|
|
501
|
+
*entries() {
|
|
502
|
+
yield* this.#entries;
|
|
503
|
+
}
|
|
504
|
+
*keys() {
|
|
505
|
+
for (const [name] of this.#entries)
|
|
506
|
+
yield name;
|
|
507
|
+
}
|
|
508
|
+
*values() {
|
|
509
|
+
for (const [, value] of this.#entries)
|
|
510
|
+
yield value;
|
|
511
|
+
}
|
|
512
|
+
forEach(callback) {
|
|
513
|
+
const pairs = this.#entries;
|
|
514
|
+
for (let index = 0; index < pairs.length; index++) {
|
|
515
|
+
const pair = pairs[index];
|
|
516
|
+
callback(pair[1], pair[0], this);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
[Symbol.iterator]() {
|
|
520
|
+
return this.entries();
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function formEntry(value, filename) {
|
|
524
|
+
if (typeof value !== 'object' || value === null)
|
|
525
|
+
return String(value);
|
|
526
|
+
if (!(value instanceof BlobPolyfill)) {
|
|
527
|
+
throw new TypeError('FormData file fields must be Blob or File objects');
|
|
528
|
+
}
|
|
529
|
+
if (value instanceof FilePolyfill && filename === undefined)
|
|
530
|
+
return value;
|
|
531
|
+
return new FilePolyfill([readBlobBytes(value)], filename ?? 'blob', { type: value.type });
|
|
532
|
+
}
|
|
533
|
+
function isFormDataBody(body) {
|
|
534
|
+
if (body instanceof FormDataPolyfill)
|
|
535
|
+
return true;
|
|
536
|
+
const ctor = globalThis.FormData;
|
|
537
|
+
return typeof ctor === 'function' && body instanceof ctor;
|
|
538
|
+
}
|
|
539
|
+
const MULTIPART_BOUNDARY_ATTEMPTS = 32;
|
|
540
|
+
function multipartBoundary(attempt) {
|
|
541
|
+
return `----diFormBoundary${attempt.toString(16).padStart(8, '0')}`;
|
|
542
|
+
}
|
|
543
|
+
function escapeDisposition(value) {
|
|
544
|
+
return value.replaceAll('"', '%22').replaceAll('\r', '').replaceAll('\n', '');
|
|
545
|
+
}
|
|
546
|
+
function encodeMultipart(entries, boundary) {
|
|
547
|
+
const encoder = new TextEncoderPolyfill();
|
|
548
|
+
const chunks = [];
|
|
549
|
+
const push = (text) => chunks.push(encoder.encode(text));
|
|
550
|
+
for (const [name, value] of entries) {
|
|
551
|
+
const quotedName = escapeDisposition(name);
|
|
552
|
+
push(`--${boundary}\r\n`);
|
|
553
|
+
if (typeof value === 'string') {
|
|
554
|
+
push(`Content-Disposition: form-data; name="${quotedName}"\r\n\r\n${value}\r\n`);
|
|
555
|
+
}
|
|
556
|
+
else if (value instanceof BlobPolyfill) {
|
|
557
|
+
const type = value.type === '' ? '' : `Content-Type: ${value.type}\r\n`;
|
|
558
|
+
const filename = escapeDisposition(value.name);
|
|
559
|
+
push(`Content-Disposition: form-data; name="${quotedName}"; filename="${filename}"\r\n` +
|
|
560
|
+
`${type}\r\n`);
|
|
561
|
+
chunks.push(readBlobBytes(value));
|
|
562
|
+
push('\r\n');
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
throw new TypeError('FormData file fields must be Blob or File objects');
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
push(`--${boundary}--\r\n`);
|
|
569
|
+
return concatBytes(chunks);
|
|
570
|
+
}
|
|
571
|
+
function countOccurrences(haystack, needle) {
|
|
572
|
+
let count = 0;
|
|
573
|
+
let from = 0;
|
|
574
|
+
while (from <= haystack.length - needle.length) {
|
|
575
|
+
const found = indexOfBytes(haystack, needle, from);
|
|
576
|
+
if (found < 0)
|
|
577
|
+
break;
|
|
578
|
+
count += 1;
|
|
579
|
+
from = found + needle.length;
|
|
580
|
+
}
|
|
581
|
+
return count;
|
|
582
|
+
}
|
|
583
|
+
function encodeFormBody(body) {
|
|
584
|
+
const entries = [...body.entries()];
|
|
585
|
+
const encoder = new TextEncoderPolyfill();
|
|
586
|
+
for (let attempt = 0; attempt < MULTIPART_BOUNDARY_ATTEMPTS; attempt++) {
|
|
587
|
+
const boundary = multipartBoundary(attempt);
|
|
588
|
+
const bytes = encodeMultipart(entries, boundary);
|
|
589
|
+
// Opening delimiters plus the closing line are the only expected markers.
|
|
590
|
+
const markers = countOccurrences(bytes, encoder.encode(`--${boundary}`));
|
|
591
|
+
if (markers === entries.length + 1) {
|
|
592
|
+
return { bytes, contentType: `multipart/form-data; boundary=${boundary}` };
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
throw new TypeError('multipart boundary collides with form contents');
|
|
596
|
+
}
|
|
597
|
+
function bodyFromInit(body, headers) {
|
|
598
|
+
if (!isFormDataBody(body))
|
|
599
|
+
return toBodySource(body);
|
|
600
|
+
const encoded = encodeFormBody(body);
|
|
601
|
+
if (!headers.has('content-type'))
|
|
602
|
+
headers.set('content-type', encoded.contentType);
|
|
603
|
+
return encoded.bytes;
|
|
604
|
+
}
|
|
605
|
+
function indexOfBytes(haystack, needle, from = 0) {
|
|
606
|
+
if (needle.length === 0)
|
|
607
|
+
return from;
|
|
608
|
+
const end = haystack.length - needle.length;
|
|
609
|
+
for (let i = from; i <= end; i++) {
|
|
610
|
+
let matched = true;
|
|
611
|
+
for (let j = 0; j < needle.length; j++) {
|
|
612
|
+
if (haystack[i + j] !== needle[j]) {
|
|
613
|
+
matched = false;
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (matched)
|
|
618
|
+
return i;
|
|
619
|
+
}
|
|
620
|
+
return -1;
|
|
621
|
+
}
|
|
622
|
+
function parseContentType(header) {
|
|
623
|
+
if (header == null || header === '')
|
|
624
|
+
return { type: '' };
|
|
625
|
+
const [rawType, ...params] = header.split(';');
|
|
626
|
+
let boundary;
|
|
627
|
+
for (const param of params) {
|
|
628
|
+
const eq = param.indexOf('=');
|
|
629
|
+
if (eq === -1)
|
|
630
|
+
continue;
|
|
631
|
+
if (param.slice(0, eq).trim().toLowerCase() !== 'boundary')
|
|
632
|
+
continue;
|
|
633
|
+
let value = param.slice(eq + 1).trim();
|
|
634
|
+
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
|
|
635
|
+
value = value.slice(1, -1);
|
|
636
|
+
}
|
|
637
|
+
boundary = value;
|
|
638
|
+
}
|
|
639
|
+
return { type: (rawType ?? '').trim().toLowerCase(), boundary };
|
|
640
|
+
}
|
|
641
|
+
function headerValue(block, name) {
|
|
642
|
+
const lower = name.toLowerCase();
|
|
643
|
+
for (const line of block.split('\r\n')) {
|
|
644
|
+
const colon = line.indexOf(':');
|
|
645
|
+
if (colon === -1)
|
|
646
|
+
continue;
|
|
647
|
+
if (line.slice(0, colon).trim().toLowerCase() !== lower)
|
|
648
|
+
continue;
|
|
649
|
+
return line.slice(colon + 1).trim();
|
|
650
|
+
}
|
|
651
|
+
return undefined;
|
|
652
|
+
}
|
|
653
|
+
function dispositionParam(value, param) {
|
|
654
|
+
for (const part of value.split(';').slice(1)) {
|
|
655
|
+
const eq = part.indexOf('=');
|
|
656
|
+
if (eq === -1)
|
|
657
|
+
continue;
|
|
658
|
+
if (part.slice(0, eq).trim().toLowerCase() !== param)
|
|
659
|
+
continue;
|
|
660
|
+
let raw = part.slice(eq + 1).trim();
|
|
661
|
+
if (raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2)
|
|
662
|
+
raw = raw.slice(1, -1);
|
|
663
|
+
return raw.replaceAll('%22', '"');
|
|
664
|
+
}
|
|
665
|
+
return undefined;
|
|
666
|
+
}
|
|
667
|
+
function parseUrlEncoded(bytes) {
|
|
668
|
+
const form = new FormDataPolyfill();
|
|
669
|
+
const params = new URLSearchParamsPolyfill(new TextDecoderPolyfill().decode(bytes));
|
|
670
|
+
for (const [name, value] of params)
|
|
671
|
+
form.append(name, value);
|
|
672
|
+
return form;
|
|
673
|
+
}
|
|
674
|
+
function parseMultipart(bytes, boundary) {
|
|
675
|
+
const encoder = new TextEncoderPolyfill();
|
|
676
|
+
const decoder = new TextDecoderPolyfill();
|
|
677
|
+
const dashBoundary = encoder.encode(`--${boundary}`);
|
|
678
|
+
const headerBreak = encoder.encode('\r\n\r\n');
|
|
679
|
+
const nextMarker = encoder.encode(`\r\n--${boundary}`);
|
|
680
|
+
const form = new FormDataPolyfill();
|
|
681
|
+
let position = indexOfBytes(bytes, dashBoundary);
|
|
682
|
+
if (position < 0)
|
|
683
|
+
throw new TypeError('multipart/form-data body is missing its boundary');
|
|
684
|
+
position += dashBoundary.length;
|
|
685
|
+
while (position < bytes.length) {
|
|
686
|
+
if (bytes[position] === 45 && bytes[position + 1] === 45)
|
|
687
|
+
break;
|
|
688
|
+
if (bytes[position] !== 13 || bytes[position + 1] !== 10) {
|
|
689
|
+
throw new TypeError('multipart/form-data body is malformed');
|
|
690
|
+
}
|
|
691
|
+
position += 2;
|
|
692
|
+
const headerEnd = indexOfBytes(bytes, headerBreak, position);
|
|
693
|
+
if (headerEnd < 0)
|
|
694
|
+
throw new TypeError('multipart/form-data part is missing its headers');
|
|
695
|
+
const headerText = decoder.decode(bytes.subarray(position, headerEnd));
|
|
696
|
+
position = headerEnd + headerBreak.length;
|
|
697
|
+
const next = indexOfBytes(bytes, nextMarker, position);
|
|
698
|
+
if (next < 0)
|
|
699
|
+
throw new TypeError('multipart/form-data part is truncated');
|
|
700
|
+
const partBody = bytes.subarray(position, next);
|
|
701
|
+
position = next + 2 + dashBoundary.length;
|
|
702
|
+
const disposition = headerValue(headerText, 'content-disposition');
|
|
703
|
+
if (disposition === undefined)
|
|
704
|
+
continue;
|
|
705
|
+
const name = dispositionParam(disposition, 'name');
|
|
706
|
+
if (name === undefined)
|
|
707
|
+
continue;
|
|
708
|
+
const filename = dispositionParam(disposition, 'filename');
|
|
709
|
+
if (filename === undefined) {
|
|
710
|
+
form.append(name, decoder.decode(partBody));
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
const type = headerValue(headerText, 'content-type') ?? '';
|
|
714
|
+
form.append(name, new BlobPolyfill([partBody], { type }), filename);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return form;
|
|
718
|
+
}
|
|
719
|
+
function formDataFromBody(headers, buffer) {
|
|
720
|
+
const parsed = parseContentType(headers.get('content-type'));
|
|
721
|
+
const bytes = new Uint8Array(buffer);
|
|
722
|
+
if (parsed.type === 'application/x-www-form-urlencoded')
|
|
723
|
+
return parseUrlEncoded(bytes);
|
|
724
|
+
if (parsed.type === 'multipart/form-data') {
|
|
725
|
+
if (parsed.boundary === undefined || parsed.boundary === '') {
|
|
726
|
+
throw new TypeError('multipart/form-data body is missing a boundary');
|
|
727
|
+
}
|
|
728
|
+
return parseMultipart(bytes, parsed.boundary);
|
|
729
|
+
}
|
|
730
|
+
throw new TypeError('Content-Type is not multipart/form-data or application/x-www-form-urlencoded');
|
|
337
731
|
}
|
|
338
732
|
const noopConsole = {
|
|
339
733
|
log() { },
|
|
@@ -342,25 +736,83 @@ const noopConsole = {
|
|
|
342
736
|
error() { },
|
|
343
737
|
debug() { },
|
|
344
738
|
};
|
|
739
|
+
const URL_SEARCH_PARAMS_METHODS = [
|
|
740
|
+
'get',
|
|
741
|
+
'getAll',
|
|
742
|
+
'has',
|
|
743
|
+
'append',
|
|
744
|
+
'set',
|
|
745
|
+
'delete',
|
|
746
|
+
'toString',
|
|
747
|
+
'entries',
|
|
748
|
+
Symbol.iterator,
|
|
749
|
+
];
|
|
750
|
+
const HEADERS_METHODS = [
|
|
751
|
+
'append',
|
|
752
|
+
'set',
|
|
753
|
+
'get',
|
|
754
|
+
'has',
|
|
755
|
+
'delete',
|
|
756
|
+
'entries',
|
|
757
|
+
'forEach',
|
|
758
|
+
'keys',
|
|
759
|
+
'values',
|
|
760
|
+
'getSetCookie',
|
|
761
|
+
Symbol.iterator,
|
|
762
|
+
];
|
|
763
|
+
const FORM_DATA_METHODS = [
|
|
764
|
+
'append',
|
|
765
|
+
'get',
|
|
766
|
+
'getAll',
|
|
767
|
+
'has',
|
|
768
|
+
'set',
|
|
769
|
+
'delete',
|
|
770
|
+
'entries',
|
|
771
|
+
Symbol.iterator,
|
|
772
|
+
];
|
|
773
|
+
function hasRequiredMethods(value, methods) {
|
|
774
|
+
if (typeof value !== 'function')
|
|
775
|
+
return false;
|
|
776
|
+
const prototype = value.prototype;
|
|
777
|
+
if (prototype != null && methods.every((method) => typeof prototype[method] === 'function')) {
|
|
778
|
+
return true;
|
|
779
|
+
}
|
|
780
|
+
try {
|
|
781
|
+
const instance = new value();
|
|
782
|
+
return methods.every((method) => typeof instance[method] === 'function');
|
|
783
|
+
}
|
|
784
|
+
catch {
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Install `implementation` when `name` is missing or lacks a method guests call.
|
|
790
|
+
* A partial constructor used to be kept, and the missing method then failed as
|
|
791
|
+
* an HTTP 500. A constructor that already has the methods is left alone.
|
|
792
|
+
*/
|
|
793
|
+
function installWebConstructor(global, name, implementation, methods, force) {
|
|
794
|
+
if (force || !hasRequiredMethods(global[name], methods))
|
|
795
|
+
global[name] = implementation;
|
|
796
|
+
}
|
|
345
797
|
export function installFetchRuntime(force = false) {
|
|
346
798
|
const global = globalThis;
|
|
347
799
|
if (force || typeof global.TextEncoder !== 'function')
|
|
348
800
|
global.TextEncoder = TextEncoderPolyfill;
|
|
349
801
|
if (force || typeof global.TextDecoder !== 'function')
|
|
350
802
|
global.TextDecoder = TextDecoderPolyfill;
|
|
351
|
-
|
|
352
|
-
global.URLSearchParams = URLSearchParamsPolyfill;
|
|
353
|
-
}
|
|
803
|
+
installWebConstructor(global, 'URLSearchParams', URLSearchParamsPolyfill, URL_SEARCH_PARAMS_METHODS, force);
|
|
354
804
|
if (force || typeof global.URL !== 'function')
|
|
355
805
|
global.URL = URLPolyfill;
|
|
356
|
-
|
|
357
|
-
global.Headers = HeadersPolyfill;
|
|
806
|
+
installWebConstructor(global, 'Headers', HeadersPolyfill, HEADERS_METHODS, force);
|
|
358
807
|
if (force || typeof global.Request !== 'function')
|
|
359
808
|
global.Request = RequestPolyfill;
|
|
360
809
|
if (force || typeof global.Response !== 'function')
|
|
361
810
|
global.Response = ResponsePolyfill;
|
|
811
|
+
installWebConstructor(global, 'FormData', FormDataPolyfill, FORM_DATA_METHODS, force);
|
|
362
812
|
if (force || typeof global.Blob !== 'function')
|
|
363
813
|
global.Blob = BlobPolyfill;
|
|
814
|
+
if (force || typeof global.File !== 'function')
|
|
815
|
+
global.File = FilePolyfill;
|
|
364
816
|
if (force || typeof global.AbortSignal !== 'function')
|
|
365
817
|
global.AbortSignal = AbortSignalPolyfill;
|
|
366
818
|
if (force || typeof global.AbortController !== 'function') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@di-framework/cli-plugin-platform",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.7",
|
|
4
4
|
"description": "di-framework CLI extension that builds, serves, and deploys DI Framework apps as platform WebAssembly components.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|