@gjsify/fetch 0.3.15 → 0.3.17
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.
- package/lib/esm/body.js +1 -349
- package/lib/esm/errors/abort-error.js +1 -14
- package/lib/esm/errors/base.js +1 -20
- package/lib/esm/errors/fetch-error.js +1 -26
- package/lib/esm/headers.js +1 -193
- package/lib/esm/index.js +1 -232
- package/lib/esm/register/fetch.js +1 -20
- package/lib/esm/register/xhr.js +1 -11
- package/lib/esm/register.js +1 -2
- package/lib/esm/request.js +1 -287
- package/lib/esm/response.js +1 -157
- package/lib/esm/types/index.js +1 -1
- package/lib/esm/types/system-error.js +0 -4
- package/lib/esm/utils/blob-from.js +1 -3
- package/lib/esm/utils/data-uri.js +1 -33
- package/lib/esm/utils/get-search.js +1 -12
- package/lib/esm/utils/is-redirect.js +1 -20
- package/lib/esm/utils/is.js +1 -64
- package/lib/esm/utils/multipart-parser.js +2 -352
- package/lib/esm/utils/referrer.js +1 -205
- package/lib/esm/utils/soup-helpers.js +1 -29
- package/lib/esm/xhr.js +2 -246
- package/package.json +12 -12
- package/tsconfig.tsbuildinfo +1 -1
package/lib/esm/response.js
CHANGED
|
@@ -1,157 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Headers from "./headers.js";
|
|
3
|
-
import { isRedirect } from "./utils/is-redirect.js";
|
|
4
|
-
import { URL } from "@gjsify/url";
|
|
5
|
-
import GLib from "@girs/glib-2.0";
|
|
6
|
-
import Gio from "@girs/gio-2.0";
|
|
7
|
-
|
|
8
|
-
//#region src/response.ts
|
|
9
|
-
const INTERNALS = Symbol("Response internals");
|
|
10
|
-
/**
|
|
11
|
-
* Response class
|
|
12
|
-
*
|
|
13
|
-
* Ref: https://fetch.spec.whatwg.org/#response-class
|
|
14
|
-
*
|
|
15
|
-
* @param body Readable stream
|
|
16
|
-
* @param opts Response options
|
|
17
|
-
*/
|
|
18
|
-
var Response = class Response extends Body {
|
|
19
|
-
[INTERNALS];
|
|
20
|
-
_inputStream = null;
|
|
21
|
-
constructor(body = null, options = {}) {
|
|
22
|
-
super(body, options);
|
|
23
|
-
const status = options.status != null ? options.status : 200;
|
|
24
|
-
const headers = new Headers(options.headers);
|
|
25
|
-
if (body !== null && !headers.has("Content-Type")) {
|
|
26
|
-
const contentType = extractContentType(body, this);
|
|
27
|
-
if (contentType) {
|
|
28
|
-
headers.append("Content-Type", contentType);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
this[INTERNALS] = {
|
|
32
|
-
type: "default",
|
|
33
|
-
url: options.url,
|
|
34
|
-
status,
|
|
35
|
-
statusText: options.statusText || "",
|
|
36
|
-
headers,
|
|
37
|
-
counter: options.counter,
|
|
38
|
-
highWaterMark: options.highWaterMark
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
get type() {
|
|
42
|
-
return this[INTERNALS].type;
|
|
43
|
-
}
|
|
44
|
-
get url() {
|
|
45
|
-
return this[INTERNALS].url || "";
|
|
46
|
-
}
|
|
47
|
-
get status() {
|
|
48
|
-
return this[INTERNALS].status;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Convenience property representing if the request ended normally
|
|
52
|
-
*/
|
|
53
|
-
get ok() {
|
|
54
|
-
return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300;
|
|
55
|
-
}
|
|
56
|
-
get redirected() {
|
|
57
|
-
return this[INTERNALS].counter > 0;
|
|
58
|
-
}
|
|
59
|
-
get statusText() {
|
|
60
|
-
return this[INTERNALS].statusText;
|
|
61
|
-
}
|
|
62
|
-
get headers() {
|
|
63
|
-
return this[INTERNALS].headers;
|
|
64
|
-
}
|
|
65
|
-
get highWaterMark() {
|
|
66
|
-
return this[INTERNALS].highWaterMark;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Clone this response
|
|
70
|
-
*
|
|
71
|
-
* @return Response
|
|
72
|
-
*/
|
|
73
|
-
clone() {
|
|
74
|
-
return new Response(clone(this, this.highWaterMark), {
|
|
75
|
-
type: this.type,
|
|
76
|
-
url: this.url,
|
|
77
|
-
status: this.status,
|
|
78
|
-
statusText: this.statusText,
|
|
79
|
-
headers: this.headers,
|
|
80
|
-
ok: this.ok,
|
|
81
|
-
redirected: this.redirected,
|
|
82
|
-
size: this.size,
|
|
83
|
-
highWaterMark: this.highWaterMark
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* @param url The URL that the new response is to originate from.
|
|
88
|
-
* @param status An optional status code for the response (e.g., 302.)
|
|
89
|
-
* @returns A Response object.
|
|
90
|
-
*/
|
|
91
|
-
static redirect(url, status = 302) {
|
|
92
|
-
if (!isRedirect(status)) {
|
|
93
|
-
throw new RangeError("Failed to execute \"redirect\" on \"response\": Invalid status code");
|
|
94
|
-
}
|
|
95
|
-
return new Response(null, {
|
|
96
|
-
headers: { location: new URL(url).toString() },
|
|
97
|
-
status
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
static error() {
|
|
101
|
-
const response = new Response(null, {
|
|
102
|
-
status: 0,
|
|
103
|
-
statusText: ""
|
|
104
|
-
});
|
|
105
|
-
response[INTERNALS].type = "error";
|
|
106
|
-
return response;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Create a Response with a JSON body.
|
|
110
|
-
* @param data The data to serialize as JSON.
|
|
111
|
-
* @param init Optional response init options.
|
|
112
|
-
* @returns A Response with the JSON body and appropriate content-type header.
|
|
113
|
-
*/
|
|
114
|
-
static json(data, init) {
|
|
115
|
-
const body = JSON.stringify(data);
|
|
116
|
-
const options = { ...init };
|
|
117
|
-
const headers = new Headers(options.headers);
|
|
118
|
-
if (!headers.has("content-type")) {
|
|
119
|
-
headers.set("content-type", "application/json");
|
|
120
|
-
}
|
|
121
|
-
options.headers = headers;
|
|
122
|
-
return new Response(body, options);
|
|
123
|
-
}
|
|
124
|
-
get [Symbol.toStringTag]() {
|
|
125
|
-
return "Response";
|
|
126
|
-
}
|
|
127
|
-
async text() {
|
|
128
|
-
if (!this._inputStream) {
|
|
129
|
-
return super.text();
|
|
130
|
-
}
|
|
131
|
-
const outputStream = Gio.MemoryOutputStream.new_resizable();
|
|
132
|
-
await new Promise((resolve, reject) => {
|
|
133
|
-
outputStream.splice_async(this._inputStream, Gio.OutputStreamSpliceFlags.CLOSE_TARGET | Gio.OutputStreamSpliceFlags.CLOSE_SOURCE, GLib.PRIORITY_DEFAULT, null, (_self, res) => {
|
|
134
|
-
try {
|
|
135
|
-
resolve(outputStream.splice_finish(res));
|
|
136
|
-
} catch (error) {
|
|
137
|
-
reject(error);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
const bytes = outputStream.steal_as_bytes();
|
|
142
|
-
return new TextDecoder().decode(bytes.toArray());
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
Object.defineProperties(Response.prototype, {
|
|
146
|
-
type: { enumerable: true },
|
|
147
|
-
url: { enumerable: true },
|
|
148
|
-
status: { enumerable: true },
|
|
149
|
-
ok: { enumerable: true },
|
|
150
|
-
redirected: { enumerable: true },
|
|
151
|
-
statusText: { enumerable: true },
|
|
152
|
-
headers: { enumerable: true },
|
|
153
|
-
clone: { enumerable: true }
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
//#endregion
|
|
157
|
-
export { Response, Response as default };
|
|
1
|
+
import e,{clone as t,extractContentType as n}from"./body.js";import r from"./headers.js";import{isRedirect as i}from"./utils/is-redirect.js";import{URL as a}from"@gjsify/url";import o from"@girs/glib-2.0";import s from"@girs/gio-2.0";const c=Symbol(`Response internals`);var l=class l extends e{[c];_inputStream=null;constructor(e=null,t={}){super(e,t);let i=t.status==null?200:t.status,a=new r(t.headers);if(e!==null&&!a.has(`Content-Type`)){let t=n(e,this);t&&a.append(`Content-Type`,t)}this[c]={type:`default`,url:t.url,status:i,statusText:t.statusText||``,headers:a,counter:t.counter,highWaterMark:t.highWaterMark}}get type(){return this[c].type}get url(){return this[c].url||``}get status(){return this[c].status}get ok(){return this[c].status>=200&&this[c].status<300}get redirected(){return this[c].counter>0}get statusText(){return this[c].statusText}get headers(){return this[c].headers}get highWaterMark(){return this[c].highWaterMark}clone(){return new l(t(this,this.highWaterMark),{type:this.type,url:this.url,status:this.status,statusText:this.statusText,headers:this.headers,ok:this.ok,redirected:this.redirected,size:this.size,highWaterMark:this.highWaterMark})}static redirect(e,t=302){if(!i(t))throw RangeError(`Failed to execute "redirect" on "response": Invalid status code`);return new l(null,{headers:{location:new a(e).toString()},status:t})}static error(){let e=new l(null,{status:0,statusText:``});return e[c].type=`error`,e}static json(e,t){let n=JSON.stringify(e),i={...t},a=new r(i.headers);return a.has(`content-type`)||a.set(`content-type`,`application/json`),i.headers=a,new l(n,i)}get[Symbol.toStringTag](){return`Response`}async text(){if(!this._inputStream)return super.text();let e=s.MemoryOutputStream.new_resizable();await new Promise((t,n)=>{e.splice_async(this._inputStream,s.OutputStreamSpliceFlags.CLOSE_TARGET|s.OutputStreamSpliceFlags.CLOSE_SOURCE,o.PRIORITY_DEFAULT,null,(r,i)=>{try{t(e.splice_finish(i))}catch(e){n(e)}})});let t=e.steal_as_bytes();return new TextDecoder().decode(t.toArray())}};Object.defineProperties(l.prototype,{type:{enumerable:!0},url:{enumerable:!0},status:{enumerable:!0},ok:{enumerable:!0},redirected:{enumerable:!0},statusText:{enumerable:!0},headers:{enumerable:!0},clone:{enumerable:!0}});export{l as Response,l as default};
|
package/lib/esm/types/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import"./system-error.js";
|
|
@@ -1,33 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Parse a data: URI into its components.
|
|
4
|
-
* Replaces the `data-uri-to-buffer` npm package.
|
|
5
|
-
*
|
|
6
|
-
* Format: data:[<mediatype>][;base64],<data>
|
|
7
|
-
*/
|
|
8
|
-
function parseDataUri(uri) {
|
|
9
|
-
const match = uri.match(/^data:([^,]*?)(;base64)?,(.*)$/s);
|
|
10
|
-
if (!match) {
|
|
11
|
-
throw new TypeError(`Invalid data URI: ${uri.slice(0, 50)}...`);
|
|
12
|
-
}
|
|
13
|
-
const typeFull = match[1] || "text/plain;charset=US-ASCII";
|
|
14
|
-
const isBase64 = !!match[2];
|
|
15
|
-
const data = match[3];
|
|
16
|
-
let buffer;
|
|
17
|
-
if (isBase64) {
|
|
18
|
-
const binaryString = atob(data);
|
|
19
|
-
buffer = new Uint8Array(binaryString.length);
|
|
20
|
-
for (let i = 0; i < binaryString.length; i++) {
|
|
21
|
-
buffer[i] = binaryString.charCodeAt(i);
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
buffer = new TextEncoder().encode(decodeURIComponent(data));
|
|
25
|
-
}
|
|
26
|
-
return {
|
|
27
|
-
buffer,
|
|
28
|
-
typeFull
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
//#endregion
|
|
33
|
-
export { parseDataUri };
|
|
1
|
+
function e(e){let t=e.match(/^data:([^,]*?)(;base64)?,(.*)$/s);if(!t)throw TypeError(`Invalid data URI: ${e.slice(0,50)}...`);let n=t[1]||`text/plain;charset=US-ASCII`,r=!!t[2],i=t[3],a;if(r){let e=atob(i);a=new Uint8Array(e.length);for(let t=0;t<e.length;t++)a[t]=e.charCodeAt(t)}else a=new TextEncoder().encode(decodeURIComponent(i));return{buffer:a,typeFull:n}}export{e as parseDataUri};
|
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const getSearch = (parsedURL) => {
|
|
3
|
-
if (parsedURL.search) {
|
|
4
|
-
return parsedURL.search;
|
|
5
|
-
}
|
|
6
|
-
const lastOffset = parsedURL.href.length - 1;
|
|
7
|
-
const hash = parsedURL.hash || (parsedURL.href[lastOffset] === "#" ? "#" : "");
|
|
8
|
-
return parsedURL.href[lastOffset - hash.length] === "?" ? "?" : "";
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
//#endregion
|
|
12
|
-
export { getSearch };
|
|
1
|
+
const e=e=>{if(e.search)return e.search;let t=e.href.length-1,n=e.hash||(e.href[t]===`#`?`#`:``);return e.href[t-n.length]===`?`?`?`:``};export{e as getSearch};
|
|
@@ -1,20 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const redirectStatus = new Set([
|
|
3
|
-
301,
|
|
4
|
-
302,
|
|
5
|
-
303,
|
|
6
|
-
307,
|
|
7
|
-
308
|
|
8
|
-
]);
|
|
9
|
-
/**
|
|
10
|
-
* Redirect code matching
|
|
11
|
-
*
|
|
12
|
-
* @param {number} code - Status code
|
|
13
|
-
* @return {boolean}
|
|
14
|
-
*/
|
|
15
|
-
const isRedirect = (code) => {
|
|
16
|
-
return redirectStatus.has(code);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
|
-
export { isRedirect };
|
|
1
|
+
const e=new Set([301,302,303,307,308]),t=t=>e.has(t);export{t as isRedirect};
|
package/lib/esm/utils/is.js
CHANGED
|
@@ -1,64 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/utils/is.ts
|
|
4
|
-
/**
|
|
5
|
-
* Is.js
|
|
6
|
-
*
|
|
7
|
-
* Object type checks.
|
|
8
|
-
*/
|
|
9
|
-
const NAME = Symbol.toStringTag;
|
|
10
|
-
/**
|
|
11
|
-
* Check if `obj` is a URLSearchParams object
|
|
12
|
-
* ref: https://github.com/node-fetch/node-fetch/issues/296#issuecomment-307598143
|
|
13
|
-
* @param {*} object - Object to check for
|
|
14
|
-
* @return {boolean}
|
|
15
|
-
*/
|
|
16
|
-
const isURLSearchParameters = (object) => {
|
|
17
|
-
return typeof object === "object" && typeof object.append === "function" && typeof object.delete === "function" && typeof object.get === "function" && typeof object.getAll === "function" && typeof object.has === "function" && typeof object.set === "function" && typeof object.sort === "function" && object[NAME] === "URLSearchParams";
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Check if `object` is a W3C `Blob` object (which `File` inherits from)
|
|
21
|
-
* @param object Object to check for
|
|
22
|
-
*/
|
|
23
|
-
const isBlob = (value) => {
|
|
24
|
-
if (!value || typeof value !== "object") return false;
|
|
25
|
-
const obj = value;
|
|
26
|
-
return typeof obj.arrayBuffer === "function" && typeof obj.type === "string" && typeof obj.stream === "function" && typeof obj.constructor === "function" && /^(Blob|File)$/.test(obj[NAME]);
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Check if `obj` is an instance of AbortSignal.
|
|
30
|
-
* @param object - Object to check for
|
|
31
|
-
*/
|
|
32
|
-
const isAbortSignal = (object) => {
|
|
33
|
-
if (typeof object !== "object" || object === null) return false;
|
|
34
|
-
const obj = object;
|
|
35
|
-
return obj[NAME] === "AbortSignal" || obj[NAME] === "EventTarget";
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* isDomainOrSubdomain reports whether sub is a subdomain (or exact match) of
|
|
39
|
-
* the parent domain.
|
|
40
|
-
*
|
|
41
|
-
* Both domains must already be in canonical form.
|
|
42
|
-
* @param {string|URL} original
|
|
43
|
-
* @param {string|URL} destination
|
|
44
|
-
*/
|
|
45
|
-
const isDomainOrSubdomain = (destination, original) => {
|
|
46
|
-
const orig = new URL(original).hostname;
|
|
47
|
-
const dest = new URL(destination).hostname;
|
|
48
|
-
return orig === dest || orig.endsWith(`.${dest}`);
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* isSameProtocol reports whether the two provided URLs use the same protocol.
|
|
52
|
-
*
|
|
53
|
-
* Both domains must already be in canonical form.
|
|
54
|
-
* @param {string|URL} original
|
|
55
|
-
* @param {string|URL} destination
|
|
56
|
-
*/
|
|
57
|
-
const isSameProtocol = (destination, original) => {
|
|
58
|
-
const orig = new URL(original).protocol;
|
|
59
|
-
const dest = new URL(destination).protocol;
|
|
60
|
-
return orig === dest;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
//#endregion
|
|
64
|
-
export { isAbortSignal, isBlob, isDomainOrSubdomain, isSameProtocol, isURLSearchParameters };
|
|
1
|
+
import{URL as e}from"@gjsify/url";const t=Symbol.toStringTag,n=e=>typeof e==`object`&&typeof e.append==`function`&&typeof e.delete==`function`&&typeof e.get==`function`&&typeof e.getAll==`function`&&typeof e.has==`function`&&typeof e.set==`function`&&typeof e.sort==`function`&&e[t]===`URLSearchParams`,r=e=>{if(!e||typeof e!=`object`)return!1;let n=e;return typeof n.arrayBuffer==`function`&&typeof n.type==`string`&&typeof n.stream==`function`&&typeof n.constructor==`function`&&/^(Blob|File)$/.test(n[t])},i=e=>{if(typeof e!=`object`||!e)return!1;let n=e;return n[t]===`AbortSignal`||n[t]===`EventTarget`},a=(t,n)=>{let r=new e(n).hostname,i=new e(t).hostname;return r===i||r.endsWith(`.${i}`)},o=(t,n)=>new e(n).protocol===new e(t).protocol;export{i as isAbortSignal,r as isBlob,a as isDomainOrSubdomain,o as isSameProtocol,n as isURLSearchParameters};
|
|
@@ -1,352 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//#region src/utils/multipart-parser.ts
|
|
5
|
-
var S = /* @__PURE__ */ function(S) {
|
|
6
|
-
S[S["START_BOUNDARY"] = 0] = "START_BOUNDARY";
|
|
7
|
-
S[S["HEADER_FIELD_START"] = 1] = "HEADER_FIELD_START";
|
|
8
|
-
S[S["HEADER_FIELD"] = 2] = "HEADER_FIELD";
|
|
9
|
-
S[S["HEADER_VALUE_START"] = 3] = "HEADER_VALUE_START";
|
|
10
|
-
S[S["HEADER_VALUE"] = 4] = "HEADER_VALUE";
|
|
11
|
-
S[S["HEADER_VALUE_ALMOST_DONE"] = 5] = "HEADER_VALUE_ALMOST_DONE";
|
|
12
|
-
S[S["HEADERS_ALMOST_DONE"] = 6] = "HEADERS_ALMOST_DONE";
|
|
13
|
-
S[S["PART_DATA_START"] = 7] = "PART_DATA_START";
|
|
14
|
-
S[S["PART_DATA"] = 8] = "PART_DATA";
|
|
15
|
-
S[S["END"] = 9] = "END";
|
|
16
|
-
return S;
|
|
17
|
-
}(S || {});
|
|
18
|
-
let f = 1;
|
|
19
|
-
const F = {
|
|
20
|
-
PART_BOUNDARY: f,
|
|
21
|
-
LAST_BOUNDARY: f *= 2
|
|
22
|
-
};
|
|
23
|
-
const LF = 10;
|
|
24
|
-
const CR = 13;
|
|
25
|
-
const SPACE = 32;
|
|
26
|
-
const HYPHEN = 45;
|
|
27
|
-
const COLON = 58;
|
|
28
|
-
const A = 97;
|
|
29
|
-
const Z = 122;
|
|
30
|
-
const lower = (c) => c | 32;
|
|
31
|
-
const noop = (..._args) => {};
|
|
32
|
-
var MultipartParser = class {
|
|
33
|
-
index = 0;
|
|
34
|
-
flags = 0;
|
|
35
|
-
boundary;
|
|
36
|
-
lookbehind;
|
|
37
|
-
state = 0;
|
|
38
|
-
onHeaderEnd = noop;
|
|
39
|
-
onHeaderField = noop;
|
|
40
|
-
onHeadersEnd = noop;
|
|
41
|
-
onHeaderValue = noop;
|
|
42
|
-
onPartBegin = noop;
|
|
43
|
-
onPartData = noop;
|
|
44
|
-
onPartEnd = noop;
|
|
45
|
-
boundaryChars = {};
|
|
46
|
-
/**
|
|
47
|
-
* @param boundary
|
|
48
|
-
*/
|
|
49
|
-
constructor(boundary) {
|
|
50
|
-
boundary = "\r\n--" + boundary;
|
|
51
|
-
const ui8a = new Uint8Array(boundary.length);
|
|
52
|
-
for (let i = 0; i < boundary.length; i++) {
|
|
53
|
-
ui8a[i] = boundary.charCodeAt(i);
|
|
54
|
-
this.boundaryChars[ui8a[i]] = true;
|
|
55
|
-
}
|
|
56
|
-
this.boundary = ui8a;
|
|
57
|
-
this.lookbehind = new Uint8Array(this.boundary.length + 8);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* @param data
|
|
61
|
-
*/
|
|
62
|
-
write(data) {
|
|
63
|
-
let i = 0;
|
|
64
|
-
const length_ = data.length;
|
|
65
|
-
let previousIndex = this.index;
|
|
66
|
-
let { lookbehind, boundary, boundaryChars, index, state, flags } = this;
|
|
67
|
-
const boundaryLength = this.boundary.length;
|
|
68
|
-
const boundaryEnd = boundaryLength - 1;
|
|
69
|
-
const bufferLength = data.length;
|
|
70
|
-
let c;
|
|
71
|
-
let cl;
|
|
72
|
-
const mark = (name) => {
|
|
73
|
-
this[name + "Mark"] = i;
|
|
74
|
-
};
|
|
75
|
-
const clear = (name) => {
|
|
76
|
-
delete this[name + "Mark"];
|
|
77
|
-
};
|
|
78
|
-
const callback = (callbackSymbol, start, end, ui8a) => {
|
|
79
|
-
if (start === undefined || start !== end) {
|
|
80
|
-
this[callbackSymbol](ui8a && ui8a.subarray(start, end));
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
const dataCallback = (name, clear = false) => {
|
|
84
|
-
const markSymbol = name + "Mark";
|
|
85
|
-
if (!(markSymbol in this)) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (clear) {
|
|
89
|
-
callback(name, this[markSymbol], i, data);
|
|
90
|
-
delete this[markSymbol];
|
|
91
|
-
} else {
|
|
92
|
-
callback(name, this[markSymbol], data.length, data);
|
|
93
|
-
this[markSymbol] = 0;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
for (i = 0; i < length_; i++) {
|
|
97
|
-
c = data[i];
|
|
98
|
-
switch (state) {
|
|
99
|
-
case 0:
|
|
100
|
-
if (index === boundary.length - 2) {
|
|
101
|
-
if (c === HYPHEN) {
|
|
102
|
-
flags |= F.LAST_BOUNDARY;
|
|
103
|
-
} else if (c !== CR) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
index++;
|
|
107
|
-
break;
|
|
108
|
-
} else if (index - 1 === boundary.length - 2) {
|
|
109
|
-
if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
|
|
110
|
-
state = 9;
|
|
111
|
-
flags = 0;
|
|
112
|
-
} else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
|
|
113
|
-
index = 0;
|
|
114
|
-
callback("onPartBegin");
|
|
115
|
-
state = 1;
|
|
116
|
-
} else {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
if (c !== boundary[index + 2]) {
|
|
122
|
-
index = -2;
|
|
123
|
-
}
|
|
124
|
-
if (c === boundary[index + 2]) {
|
|
125
|
-
index++;
|
|
126
|
-
}
|
|
127
|
-
break;
|
|
128
|
-
case 1:
|
|
129
|
-
state = 2;
|
|
130
|
-
mark("onHeaderField");
|
|
131
|
-
index = 0;
|
|
132
|
-
case 2:
|
|
133
|
-
if (c === CR) {
|
|
134
|
-
clear("onHeaderField");
|
|
135
|
-
state = 6;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
index++;
|
|
139
|
-
if (c === HYPHEN) {
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
if (c === COLON) {
|
|
143
|
-
if (index === 1) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
dataCallback("onHeaderField", true);
|
|
147
|
-
state = 3;
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
cl = lower(c);
|
|
151
|
-
if (cl < A || cl > Z) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
break;
|
|
155
|
-
case 3:
|
|
156
|
-
if (c === SPACE) {
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
mark("onHeaderValue");
|
|
160
|
-
state = 4;
|
|
161
|
-
case 4:
|
|
162
|
-
if (c === CR) {
|
|
163
|
-
dataCallback("onHeaderValue", true);
|
|
164
|
-
callback("onHeaderEnd");
|
|
165
|
-
state = 5;
|
|
166
|
-
}
|
|
167
|
-
break;
|
|
168
|
-
case 5:
|
|
169
|
-
if (c !== LF) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
state = 1;
|
|
173
|
-
break;
|
|
174
|
-
case 6:
|
|
175
|
-
if (c !== LF) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
callback("onHeadersEnd");
|
|
179
|
-
state = 7;
|
|
180
|
-
break;
|
|
181
|
-
case 7:
|
|
182
|
-
state = 8;
|
|
183
|
-
mark("onPartData");
|
|
184
|
-
case 8:
|
|
185
|
-
previousIndex = index;
|
|
186
|
-
if (index === 0) {
|
|
187
|
-
i += boundaryEnd;
|
|
188
|
-
while (i < bufferLength && !(data[i] in boundaryChars)) {
|
|
189
|
-
i += boundaryLength;
|
|
190
|
-
}
|
|
191
|
-
i -= boundaryEnd;
|
|
192
|
-
c = data[i];
|
|
193
|
-
}
|
|
194
|
-
if (index < boundary.length) {
|
|
195
|
-
if (boundary[index] === c) {
|
|
196
|
-
if (index === 0) {
|
|
197
|
-
dataCallback("onPartData", true);
|
|
198
|
-
}
|
|
199
|
-
index++;
|
|
200
|
-
} else {
|
|
201
|
-
index = 0;
|
|
202
|
-
}
|
|
203
|
-
} else if (index === boundary.length) {
|
|
204
|
-
index++;
|
|
205
|
-
if (c === CR) {
|
|
206
|
-
flags |= F.PART_BOUNDARY;
|
|
207
|
-
} else if (c === HYPHEN) {
|
|
208
|
-
flags |= F.LAST_BOUNDARY;
|
|
209
|
-
} else {
|
|
210
|
-
index = 0;
|
|
211
|
-
}
|
|
212
|
-
} else if (index - 1 === boundary.length) {
|
|
213
|
-
if (flags & F.PART_BOUNDARY) {
|
|
214
|
-
index = 0;
|
|
215
|
-
if (c === LF) {
|
|
216
|
-
flags &= ~F.PART_BOUNDARY;
|
|
217
|
-
callback("onPartEnd");
|
|
218
|
-
callback("onPartBegin");
|
|
219
|
-
state = 1;
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
} else if (flags & F.LAST_BOUNDARY) {
|
|
223
|
-
if (c === HYPHEN) {
|
|
224
|
-
callback("onPartEnd");
|
|
225
|
-
state = 9;
|
|
226
|
-
flags = 0;
|
|
227
|
-
} else {
|
|
228
|
-
index = 0;
|
|
229
|
-
}
|
|
230
|
-
} else {
|
|
231
|
-
index = 0;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (index > 0) {
|
|
235
|
-
lookbehind[index - 1] = c;
|
|
236
|
-
} else if (previousIndex > 0) {
|
|
237
|
-
const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
|
|
238
|
-
callback("onPartData", 0, previousIndex, _lookbehind);
|
|
239
|
-
previousIndex = 0;
|
|
240
|
-
mark("onPartData");
|
|
241
|
-
i--;
|
|
242
|
-
}
|
|
243
|
-
break;
|
|
244
|
-
case 9: break;
|
|
245
|
-
default: throw new Error(`Unexpected state entered: ${state}`);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
dataCallback("onHeaderField");
|
|
249
|
-
dataCallback("onHeaderValue");
|
|
250
|
-
dataCallback("onPartData");
|
|
251
|
-
this.index = index;
|
|
252
|
-
this.state = state;
|
|
253
|
-
this.flags = flags;
|
|
254
|
-
}
|
|
255
|
-
end() {
|
|
256
|
-
if (this.state === 1 && this.index === 0 || this.state === 8 && this.index === this.boundary.length) {
|
|
257
|
-
this.onPartEnd();
|
|
258
|
-
} else if (this.state !== 9) {
|
|
259
|
-
throw new Error("MultipartParser.end(): stream ended unexpectedly");
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
function _fileName(headerValue) {
|
|
264
|
-
const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
|
|
265
|
-
if (!m) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
const match = m[2] || m[3] || "";
|
|
269
|
-
let filename = match.slice(match.lastIndexOf("\\") + 1);
|
|
270
|
-
filename = filename.replace(/%22/g, "\"");
|
|
271
|
-
filename = filename.replace(/&#(\d{4});/g, (m, code) => {
|
|
272
|
-
return String.fromCharCode(code);
|
|
273
|
-
});
|
|
274
|
-
return filename;
|
|
275
|
-
}
|
|
276
|
-
async function toFormData(Body, ct) {
|
|
277
|
-
if (!/multipart/i.test(ct)) {
|
|
278
|
-
throw new TypeError("Failed to fetch");
|
|
279
|
-
}
|
|
280
|
-
const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
|
|
281
|
-
if (!m) {
|
|
282
|
-
throw new TypeError("no or bad content-type header, no multipart boundary");
|
|
283
|
-
}
|
|
284
|
-
const parser = new MultipartParser(m[1] || m[2]);
|
|
285
|
-
let headerField;
|
|
286
|
-
let headerValue;
|
|
287
|
-
let entryValue;
|
|
288
|
-
let entryName;
|
|
289
|
-
let contentType;
|
|
290
|
-
let filename;
|
|
291
|
-
const entryChunks = [];
|
|
292
|
-
const formData = new FormData();
|
|
293
|
-
const onPartData = (ui8a) => {
|
|
294
|
-
entryValue += decoder.decode(ui8a, { stream: true });
|
|
295
|
-
};
|
|
296
|
-
const appendToFile = (ui8a) => {
|
|
297
|
-
entryChunks.push(ui8a);
|
|
298
|
-
};
|
|
299
|
-
const appendFileToFormData = () => {
|
|
300
|
-
const file = new File(entryChunks, filename, { type: contentType });
|
|
301
|
-
formData.append(entryName, file);
|
|
302
|
-
};
|
|
303
|
-
const appendEntryToFormData = () => {
|
|
304
|
-
formData.append(entryName, entryValue);
|
|
305
|
-
};
|
|
306
|
-
const decoder = new TextDecoder("utf-8");
|
|
307
|
-
decoder.decode();
|
|
308
|
-
parser.onPartBegin = function() {
|
|
309
|
-
parser.onPartData = onPartData;
|
|
310
|
-
parser.onPartEnd = appendEntryToFormData;
|
|
311
|
-
headerField = "";
|
|
312
|
-
headerValue = "";
|
|
313
|
-
entryValue = "";
|
|
314
|
-
entryName = "";
|
|
315
|
-
contentType = "";
|
|
316
|
-
filename = null;
|
|
317
|
-
entryChunks.length = 0;
|
|
318
|
-
};
|
|
319
|
-
parser.onHeaderField = function(ui8a) {
|
|
320
|
-
headerField += decoder.decode(ui8a, { stream: true });
|
|
321
|
-
};
|
|
322
|
-
parser.onHeaderValue = function(ui8a) {
|
|
323
|
-
headerValue += decoder.decode(ui8a, { stream: true });
|
|
324
|
-
};
|
|
325
|
-
parser.onHeaderEnd = function() {
|
|
326
|
-
headerValue += decoder.decode();
|
|
327
|
-
headerField = headerField.toLowerCase();
|
|
328
|
-
if (headerField === "content-disposition") {
|
|
329
|
-
const m = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
|
|
330
|
-
if (m) {
|
|
331
|
-
entryName = m[2] || m[3] || "";
|
|
332
|
-
}
|
|
333
|
-
filename = _fileName(headerValue);
|
|
334
|
-
if (filename) {
|
|
335
|
-
parser.onPartData = appendToFile;
|
|
336
|
-
parser.onPartEnd = appendFileToFormData;
|
|
337
|
-
}
|
|
338
|
-
} else if (headerField === "content-type") {
|
|
339
|
-
contentType = headerValue;
|
|
340
|
-
}
|
|
341
|
-
headerValue = "";
|
|
342
|
-
headerField = "";
|
|
343
|
-
};
|
|
344
|
-
for await (const chunk of Body) {
|
|
345
|
-
parser.write(chunk);
|
|
346
|
-
}
|
|
347
|
-
parser.end();
|
|
348
|
-
return formData;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
//#endregion
|
|
352
|
-
export { toFormData };
|
|
1
|
+
import{File as e}from"./blob-from.js";import{FormData as t}from"@gjsify/formdata";var n=function(e){return e[e.START_BOUNDARY=0]=`START_BOUNDARY`,e[e.HEADER_FIELD_START=1]=`HEADER_FIELD_START`,e[e.HEADER_FIELD=2]=`HEADER_FIELD`,e[e.HEADER_VALUE_START=3]=`HEADER_VALUE_START`,e[e.HEADER_VALUE=4]=`HEADER_VALUE`,e[e.HEADER_VALUE_ALMOST_DONE=5]=`HEADER_VALUE_ALMOST_DONE`,e[e.HEADERS_ALMOST_DONE=6]=`HEADERS_ALMOST_DONE`,e[e.PART_DATA_START=7]=`PART_DATA_START`,e[e.PART_DATA=8]=`PART_DATA`,e[e.END=9]=`END`,e}(n||{});let r=1;const i={PART_BOUNDARY:r,LAST_BOUNDARY:r*=2},a=e=>e|32,o=(...e)=>{};var s=class{index=0;flags=0;boundary;lookbehind;state=0;onHeaderEnd=o;onHeaderField=o;onHeadersEnd=o;onHeaderValue=o;onPartBegin=o;onPartData=o;onPartEnd=o;boundaryChars={};constructor(e){e=`\r
|
|
2
|
+
--`+e;let t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n),this.boundaryChars[t[n]]=!0;this.boundary=t,this.lookbehind=new Uint8Array(this.boundary.length+8)}write(e){let t=0,n=e.length,r=this.index,{lookbehind:o,boundary:s,boundaryChars:c,index:l,state:u,flags:d}=this,f=this.boundary.length,p=f-1,m=e.length,h,g,_=e=>{this[e+`Mark`]=t},v=e=>{delete this[e+`Mark`]},y=(e,t,n,r)=>{(t===void 0||t!==n)&&this[e](r&&r.subarray(t,n))},b=(n,r=!1)=>{let i=n+`Mark`;i in this&&(r?(y(n,this[i],t,e),delete this[i]):(y(n,this[i],e.length,e),this[i]=0))};for(t=0;t<n;t++)switch(h=e[t],u){case 0:if(l===s.length-2){if(h===45)d|=i.LAST_BOUNDARY;else if(h!==13)return;l++;break}else if(l-1==s.length-2){if(d&i.LAST_BOUNDARY&&h===45)u=9,d=0;else if(!(d&i.LAST_BOUNDARY)&&h===10)l=0,y(`onPartBegin`),u=1;else return;break}h!==s[l+2]&&(l=-2),h===s[l+2]&&l++;break;case 1:u=2,_(`onHeaderField`),l=0;case 2:if(h===13){v(`onHeaderField`),u=6;break}if(l++,h===45)break;if(h===58){if(l===1)return;b(`onHeaderField`,!0),u=3;break}if(g=a(h),g<97||g>122)return;break;case 3:if(h===32)break;_(`onHeaderValue`),u=4;case 4:h===13&&(b(`onHeaderValue`,!0),y(`onHeaderEnd`),u=5);break;case 5:if(h!==10)return;u=1;break;case 6:if(h!==10)return;y(`onHeadersEnd`),u=7;break;case 7:u=8,_(`onPartData`);case 8:if(r=l,l===0){for(t+=p;t<m&&!(e[t]in c);)t+=f;t-=p,h=e[t]}if(l<s.length)s[l]===h?(l===0&&b(`onPartData`,!0),l++):l=0;else if(l===s.length)l++,h===13?d|=i.PART_BOUNDARY:h===45?d|=i.LAST_BOUNDARY:l=0;else if(l-1===s.length)if(d&i.PART_BOUNDARY){if(l=0,h===10){d&=~i.PART_BOUNDARY,y(`onPartEnd`),y(`onPartBegin`),u=1;break}}else d&i.LAST_BOUNDARY&&h===45?(y(`onPartEnd`),u=9,d=0):l=0;if(l>0)o[l-1]=h;else if(r>0){let e=new Uint8Array(o.buffer,o.byteOffset,o.byteLength);y(`onPartData`,0,r,e),r=0,_(`onPartData`),t--}break;case 9:break;default:throw Error(`Unexpected state entered: ${u}`)}b(`onHeaderField`),b(`onHeaderValue`),b(`onPartData`),this.index=l,this.state=u,this.flags=d}end(){if(this.state===1&&this.index===0||this.state===8&&this.index===this.boundary.length)this.onPartEnd();else if(this.state!==9)throw Error(`MultipartParser.end(): stream ended unexpectedly`)}};function c(e){let t=e.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);if(!t)return;let n=t[2]||t[3]||``,r=n.slice(n.lastIndexOf(`\\`)+1);return r=r.replace(/%22/g,`"`),r=r.replace(/&#(\d{4});/g,(e,t)=>String.fromCharCode(t)),r}async function l(n,r){if(!/multipart/i.test(r))throw TypeError(`Failed to fetch`);let i=r.match(/boundary=(?:"([^"]+)"|([^;]+))/i);if(!i)throw TypeError(`no or bad content-type header, no multipart boundary`);let a=new s(i[1]||i[2]),o,l,u,d,f,p,m=[],h=new t,g=e=>{u+=b.decode(e,{stream:!0})},_=e=>{m.push(e)},v=()=>{let t=new e(m,p,{type:f});h.append(d,t)},y=()=>{h.append(d,u)},b=new TextDecoder(`utf-8`);b.decode(),a.onPartBegin=function(){a.onPartData=g,a.onPartEnd=y,o=``,l=``,u=``,d=``,f=``,p=null,m.length=0},a.onHeaderField=function(e){o+=b.decode(e,{stream:!0})},a.onHeaderValue=function(e){l+=b.decode(e,{stream:!0})},a.onHeaderEnd=function(){if(l+=b.decode(),o=o.toLowerCase(),o===`content-disposition`){let e=l.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);e&&(d=e[2]||e[3]||``),p=c(l),p&&(a.onPartData=_,a.onPartEnd=v)}else o===`content-type`&&(f=l);l=``,o=``};for await(let e of n)a.write(e);return a.end(),h}export{l as toFormData};
|