@di-framework/cli-plugin-platform 6.0.6 → 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.
@@ -601,6 +601,30 @@ class HeadersPolyfill {
601
601
  for (const [name, value] of this.entries())
602
602
  callback(value, name, this);
603
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
+ }
604
628
  [Symbol.iterator]() {
605
629
  return this.entries();
606
630
  }
@@ -663,12 +687,12 @@ class RequestPolyfill {
663
687
  this.method = String(init.method ?? input.method);
664
688
  this.url = input.url;
665
689
  this.headers = new HeadersPolyfill(init.headers ?? input.headers);
666
- this.#body = init.body === undefined ? input.#body : toBodySource(init.body);
690
+ this.#body = init.body === undefined ? input.#body : bodyFromInit(init.body, this.headers);
667
691
  } else {
668
692
  this.method = String(init.method ?? "GET").toUpperCase();
669
693
  this.url = String(input);
670
694
  this.headers = new HeadersPolyfill(init.headers);
671
- this.#body = toBodySource(init.body);
695
+ this.#body = bodyFromInit(init.body, this.headers);
672
696
  }
673
697
  }
674
698
  get body() {
@@ -689,6 +713,9 @@ class RequestPolyfill {
689
713
  async json() {
690
714
  return JSON.parse(await this.text());
691
715
  }
716
+ async formData() {
717
+ return formDataFromBody(this.headers, await this.arrayBuffer());
718
+ }
692
719
  }
693
720
 
694
721
  class ResponsePolyfill {
@@ -702,7 +729,7 @@ class ResponsePolyfill {
702
729
  this.statusText = String(init.statusText ?? "");
703
730
  this.headers = new HeadersPolyfill(init.headers);
704
731
  this.ok = this.status >= 200 && this.status < 300;
705
- this.#body = toBodySource(body);
732
+ this.#body = bodyFromInit(body, this.headers);
706
733
  }
707
734
  static json(data, init = {}) {
708
735
  const headers = new HeadersPolyfill(init.headers);
@@ -724,6 +751,9 @@ class ResponsePolyfill {
724
751
  async json() {
725
752
  return JSON.parse(await this.text());
726
753
  }
754
+ async formData() {
755
+ return formDataFromBody(this.headers, await this.arrayBuffer());
756
+ }
727
757
  }
728
758
 
729
759
  class AbortSignalPolyfill {
@@ -763,21 +793,345 @@ class AbortControllerPolyfill {
763
793
  this.signal.dispatchEvent(event);
764
794
  }
765
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
+ }
766
824
 
767
825
  class BlobPolyfill {
768
826
  size;
769
827
  type;
770
828
  constructor(parts = [], options) {
771
- let size = 0;
772
- for (const part of parts) {
773
- if (typeof part === "string")
774
- size += new TextEncoder().encode(part).length;
775
- else
776
- size += part.byteLength;
777
- }
778
- this.size = size;
829
+ const bytes = concatBytes(parts.map((part) => bytesOfPart(part)));
830
+ blobContents.set(this, bytes);
831
+ this.size = bytes.byteLength;
779
832
  this.type = options?.type ?? "";
780
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");
781
1135
  }
782
1136
  var noopConsole = {
783
1137
  log() {},
@@ -786,25 +1140,77 @@ var noopConsole = {
786
1140
  error() {},
787
1141
  debug() {}
788
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
+ }
789
1195
  function installFetchRuntime(force = false) {
790
1196
  const global = globalThis;
791
1197
  if (force || typeof global.TextEncoder !== "function")
792
1198
  global.TextEncoder = TextEncoderPolyfill;
793
1199
  if (force || typeof global.TextDecoder !== "function")
794
1200
  global.TextDecoder = TextDecoderPolyfill;
795
- if (force || typeof global.URLSearchParams !== "function") {
796
- global.URLSearchParams = URLSearchParamsPolyfill;
797
- }
1201
+ installWebConstructor(global, "URLSearchParams", URLSearchParamsPolyfill, URL_SEARCH_PARAMS_METHODS, force);
798
1202
  if (force || typeof global.URL !== "function")
799
1203
  global.URL = URLPolyfill;
800
- if (force || typeof global.Headers !== "function")
801
- global.Headers = HeadersPolyfill;
1204
+ installWebConstructor(global, "Headers", HeadersPolyfill, HEADERS_METHODS, force);
802
1205
  if (force || typeof global.Request !== "function")
803
1206
  global.Request = RequestPolyfill;
804
1207
  if (force || typeof global.Response !== "function")
805
1208
  global.Response = ResponsePolyfill;
1209
+ installWebConstructor(global, "FormData", FormDataPolyfill, FORM_DATA_METHODS, force);
806
1210
  if (force || typeof global.Blob !== "function")
807
1211
  global.Blob = BlobPolyfill;
1212
+ if (force || typeof global.File !== "function")
1213
+ global.File = FilePolyfill;
808
1214
  if (force || typeof global.AbortSignal !== "function")
809
1215
  global.AbortSignal = AbortSignalPolyfill;
810
1216
  if (force || typeof global.AbortController !== "function") {
@@ -1,6 +1,6 @@
1
1
  component: di-framework:sqlite@0.1.0
2
2
  profile: release
3
- built: 2026-10-03T19:24:11Z
3
+ built: 2026-10-03T23:59:51Z
4
4
  rustc: rustc 1.97.1 (8bab26f4f 2026-07-14)
5
5
  target: wasm32-wasip2
6
6
  wasi-sdk: 34.0
@@ -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` nor `URLSearchParams`. StarlingMonkey does provide
7
- * Fetch; install these polyfills only when those globals are missing so the
8
- * application contract (`new Response(...)`) stays unchanged.
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;
@@ -53,6 +54,10 @@ export declare class HeadersPolyfill {
53
54
  delete(name: string): void;
54
55
  entries(): IterableIterator<[string, string]>;
55
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[];
56
61
  [Symbol.iterator](): IterableIterator<[string, string]>;
57
62
  }
58
63
  export declare function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
@@ -70,6 +75,7 @@ export declare class RequestPolyfill {
70
75
  arrayBuffer(): Promise<ArrayBuffer>;
71
76
  text(): Promise<string>;
72
77
  json(): Promise<unknown>;
78
+ formData(): Promise<FormDataPolyfill>;
73
79
  }
74
80
  export declare class ResponsePolyfill {
75
81
  #private;
@@ -83,6 +89,7 @@ export declare class ResponsePolyfill {
83
89
  arrayBuffer(): Promise<ArrayBuffer>;
84
90
  text(): Promise<string>;
85
91
  json(): Promise<unknown>;
92
+ formData(): Promise<FormDataPolyfill>;
86
93
  }
87
94
  export declare class AbortSignalPolyfill {
88
95
  #private;
@@ -106,7 +113,7 @@ export declare class AbortControllerPolyfill {
106
113
  readonly signal: AbortSignalPolyfill;
107
114
  abort(reason?: unknown): void;
108
115
  }
109
- type BlobPart = string | ArrayBuffer | ArrayBufferView;
116
+ type BlobPart = string | ArrayBuffer | ArrayBufferView | BlobPolyfill;
110
117
  /** QuickJS has no Blob. unenv's File extends it while Node streams load. */
111
118
  export declare class BlobPolyfill {
112
119
  readonly size: number;
@@ -114,6 +121,33 @@ export declare class BlobPolyfill {
114
121
  constructor(parts?: BlobPart[], options?: {
115
122
  type?: string;
116
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]>;
117
151
  }
118
152
  export declare function installFetchRuntime(force?: boolean): void;
119
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` nor `URLSearchParams`. StarlingMonkey does provide
7
- * Fetch; install these polyfills only when those globals are missing so the
8
- * application contract (`new Response(...)`) stays unchanged.
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 = '') {
@@ -198,6 +199,37 @@ export class HeadersPolyfill {
198
199
  for (const [name, value] of this.entries())
199
200
  callback(value, name, this);
200
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
+ }
201
233
  [Symbol.iterator]() {
202
234
  return this.entries();
203
235
  }
@@ -263,13 +295,13 @@ export class RequestPolyfill {
263
295
  this.method = String(init.method ?? input.method);
264
296
  this.url = input.url;
265
297
  this.headers = new HeadersPolyfill(init.headers ?? input.headers);
266
- this.#body = init.body === undefined ? input.#body : toBodySource(init.body);
298
+ this.#body = init.body === undefined ? input.#body : bodyFromInit(init.body, this.headers);
267
299
  }
268
300
  else {
269
301
  this.method = String(init.method ?? 'GET').toUpperCase();
270
302
  this.url = String(input);
271
303
  this.headers = new HeadersPolyfill(init.headers);
272
- this.#body = toBodySource(init.body);
304
+ this.#body = bodyFromInit(init.body, this.headers);
273
305
  }
274
306
  }
275
307
  get body() {
@@ -290,6 +322,9 @@ export class RequestPolyfill {
290
322
  async json() {
291
323
  return JSON.parse(await this.text());
292
324
  }
325
+ async formData() {
326
+ return formDataFromBody(this.headers, await this.arrayBuffer());
327
+ }
293
328
  }
294
329
  export class ResponsePolyfill {
295
330
  status;
@@ -302,7 +337,7 @@ export class ResponsePolyfill {
302
337
  this.statusText = String(init.statusText ?? '');
303
338
  this.headers = new HeadersPolyfill(init.headers);
304
339
  this.ok = this.status >= 200 && this.status < 300;
305
- this.#body = toBodySource(body);
340
+ this.#body = bodyFromInit(body, this.headers);
306
341
  }
307
342
  static json(data, init = {}) {
308
343
  const headers = new HeadersPolyfill(init.headers);
@@ -324,6 +359,9 @@ export class ResponsePolyfill {
324
359
  async json() {
325
360
  return JSON.parse(await this.text());
326
361
  }
362
+ async formData() {
363
+ return formDataFromBody(this.headers, await this.arrayBuffer());
364
+ }
327
365
  }
328
366
  export class AbortSignalPolyfill {
329
367
  aborted = false;
@@ -361,21 +399,335 @@ export class AbortControllerPolyfill {
361
399
  this.signal.dispatchEvent(event);
362
400
  }
363
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
+ }
364
430
  /** QuickJS has no Blob. unenv's File extends it while Node streams load. */
365
431
  export class BlobPolyfill {
366
432
  size;
367
433
  type;
368
434
  constructor(parts = [], options) {
369
- let size = 0;
370
- for (const part of parts) {
371
- if (typeof part === 'string')
372
- size += new TextEncoder().encode(part).length;
373
- else
374
- size += part.byteLength;
375
- }
376
- this.size = size;
435
+ const bytes = concatBytes(parts.map((part) => bytesOfPart(part)));
436
+ blobContents.set(this, bytes);
437
+ this.size = bytes.byteLength;
377
438
  this.type = options?.type ?? '';
378
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');
379
731
  }
380
732
  const noopConsole = {
381
733
  log() { },
@@ -384,25 +736,83 @@ const noopConsole = {
384
736
  error() { },
385
737
  debug() { },
386
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
+ }
387
797
  export function installFetchRuntime(force = false) {
388
798
  const global = globalThis;
389
799
  if (force || typeof global.TextEncoder !== 'function')
390
800
  global.TextEncoder = TextEncoderPolyfill;
391
801
  if (force || typeof global.TextDecoder !== 'function')
392
802
  global.TextDecoder = TextDecoderPolyfill;
393
- if (force || typeof global.URLSearchParams !== 'function') {
394
- global.URLSearchParams = URLSearchParamsPolyfill;
395
- }
803
+ installWebConstructor(global, 'URLSearchParams', URLSearchParamsPolyfill, URL_SEARCH_PARAMS_METHODS, force);
396
804
  if (force || typeof global.URL !== 'function')
397
805
  global.URL = URLPolyfill;
398
- if (force || typeof global.Headers !== 'function')
399
- global.Headers = HeadersPolyfill;
806
+ installWebConstructor(global, 'Headers', HeadersPolyfill, HEADERS_METHODS, force);
400
807
  if (force || typeof global.Request !== 'function')
401
808
  global.Request = RequestPolyfill;
402
809
  if (force || typeof global.Response !== 'function')
403
810
  global.Response = ResponsePolyfill;
811
+ installWebConstructor(global, 'FormData', FormDataPolyfill, FORM_DATA_METHODS, force);
404
812
  if (force || typeof global.Blob !== 'function')
405
813
  global.Blob = BlobPolyfill;
814
+ if (force || typeof global.File !== 'function')
815
+ global.File = FilePolyfill;
406
816
  if (force || typeof global.AbortSignal !== 'function')
407
817
  global.AbortSignal = AbortSignalPolyfill;
408
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.6",
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",