@tauri-apps/plugin-http 2.8.3 → 2.10.0
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/README.md +1 -1
- package/dist-js/index.cjs +180 -94
- package/dist-js/index.js +180 -94
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Access the HTTP client written in Rust.
|
|
|
12
12
|
|
|
13
13
|
## Install
|
|
14
14
|
|
|
15
|
-
_This plugin requires a Rust version of at least **1.
|
|
15
|
+
_This plugin requires a Rust version of at least **1.94**_
|
|
16
16
|
|
|
17
17
|
There are three general methods of installation that we can recommend.
|
|
18
18
|
|
package/dist-js/index.cjs
CHANGED
|
@@ -29,6 +29,76 @@ var core = require('@tauri-apps/api/core');
|
|
|
29
29
|
* @module
|
|
30
30
|
*/
|
|
31
31
|
const ERROR_REQUEST_CANCELLED = 'Request cancelled';
|
|
32
|
+
const bodyFinalizer = typeof FinalizationRegistry !== 'undefined' && typeof WeakRef !== 'undefined'
|
|
33
|
+
? new FinalizationRegistry((dropBody) => {
|
|
34
|
+
void dropBody();
|
|
35
|
+
})
|
|
36
|
+
: undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Read the request body while allowing an abort to interrupt a stalled upload.
|
|
39
|
+
* `request.arrayBuffer()` locks the body internally without observing its abort
|
|
40
|
+
* signal, so keep access to the reader and cancel it when the signal fires.
|
|
41
|
+
* The caller checks the signal after buffering to reject canceled uploads.
|
|
42
|
+
*/
|
|
43
|
+
async function readRequestBody(request) {
|
|
44
|
+
const signal = request.signal;
|
|
45
|
+
if (signal.aborted) {
|
|
46
|
+
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
47
|
+
}
|
|
48
|
+
if (request.body === null) {
|
|
49
|
+
return new ArrayBuffer(0);
|
|
50
|
+
}
|
|
51
|
+
const reader = request.body.getReader();
|
|
52
|
+
const abort = () => {
|
|
53
|
+
void reader.cancel().catch(() => { });
|
|
54
|
+
};
|
|
55
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
56
|
+
try {
|
|
57
|
+
// Keep a reader we can cancel while the browser collects and validates bytes.
|
|
58
|
+
const body = new ReadableStream({
|
|
59
|
+
async pull(controller) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done)
|
|
62
|
+
controller.close();
|
|
63
|
+
else
|
|
64
|
+
controller.enqueue(value);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return await new Response(body).arrayBuffer();
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
abort();
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
signal.removeEventListener('abort', abort);
|
|
75
|
+
reader.releaseLock();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Apply the response URL and headers and preserve them when cloning.
|
|
80
|
+
* Keep response references out of the body stream's closure scope so WebKit can
|
|
81
|
+
* collect the response once it is no longer used.
|
|
82
|
+
*/
|
|
83
|
+
function patchResponse(response, url, headers) {
|
|
84
|
+
// `Response.url` cannot be set via the constructor, so we define it manually.
|
|
85
|
+
Object.defineProperty(response, 'url', { value: url, writable: false });
|
|
86
|
+
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
87
|
+
// supported). This is not Fetch-spec compliant for network responses in
|
|
88
|
+
// browsers, where `set-cookie` is treated as a forbidden response
|
|
89
|
+
// header and is generally not readable from JavaScript.
|
|
90
|
+
Object.defineProperty(response, 'headers', {
|
|
91
|
+
value: new Headers(headers),
|
|
92
|
+
writable: false
|
|
93
|
+
});
|
|
94
|
+
// Use the receiver instead of retaining a response in a bound clone method.
|
|
95
|
+
Object.defineProperty(response, 'clone', {
|
|
96
|
+
value: function () {
|
|
97
|
+
return patchResponse(Response.prototype.clone.call(this), url, headers);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
32
102
|
/**
|
|
33
103
|
* Fetch a resource from the network. It returns a `Promise` that resolves to the
|
|
34
104
|
* `Response` to that `Request`, whether it is successful or not.
|
|
@@ -54,11 +124,6 @@ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
|
|
|
54
124
|
* @since 2.0.0
|
|
55
125
|
*/
|
|
56
126
|
async function fetch(input, init) {
|
|
57
|
-
// Optimistically check for abort signal and avoid doing any work
|
|
58
|
-
const signal = init?.signal;
|
|
59
|
-
if (signal?.aborted) {
|
|
60
|
-
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
61
|
-
}
|
|
62
127
|
const maxRedirections = init?.maxRedirections;
|
|
63
128
|
const connectTimeout = init?.connectTimeout;
|
|
64
129
|
const proxy = init?.proxy;
|
|
@@ -76,8 +141,9 @@ async function fetch(input, init) {
|
|
|
76
141
|
: new Headers(init.headers)
|
|
77
142
|
: new Headers();
|
|
78
143
|
const req = new Request(input, init);
|
|
79
|
-
const
|
|
80
|
-
|
|
144
|
+
const signal = req.signal;
|
|
145
|
+
// read the upload with abort support.
|
|
146
|
+
const buffer = await readRequestBody(req);
|
|
81
147
|
// append new headers created by the browser `Request` implementation,
|
|
82
148
|
// if not already declared by the caller of this function
|
|
83
149
|
for (const [key, value] of req.headers) {
|
|
@@ -97,111 +163,131 @@ async function fetch(input, init) {
|
|
|
97
163
|
// eslint-disable-next-line
|
|
98
164
|
typeof val === 'string' ? val : val.toString()
|
|
99
165
|
]);
|
|
100
|
-
//
|
|
101
|
-
if (signal
|
|
166
|
+
// Cancellation may have ended the body read with only a partial upload.
|
|
167
|
+
if (signal.aborted) {
|
|
102
168
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
103
169
|
}
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
170
|
+
const clientConfig = {
|
|
171
|
+
method: req.method,
|
|
172
|
+
url: req.url,
|
|
173
|
+
headers: mappedHeaders,
|
|
174
|
+
maxRedirections,
|
|
175
|
+
connectTimeout,
|
|
176
|
+
proxy,
|
|
177
|
+
danger
|
|
178
|
+
};
|
|
179
|
+
const rid = await core.invoke('plugin:http|fetch', new Uint8Array(buffer), {
|
|
180
|
+
headers: {
|
|
181
|
+
'client-config': encodeURIComponent(JSON.stringify(clientConfig))
|
|
114
182
|
}
|
|
115
183
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
void
|
|
184
|
+
let requestCancelled = false;
|
|
185
|
+
const abortRequest = () => {
|
|
186
|
+
if (requestCancelled)
|
|
187
|
+
return;
|
|
188
|
+
requestCancelled = true;
|
|
189
|
+
void core.invoke('plugin:http|fetch_cancel', { rid }).catch(() => { });
|
|
190
|
+
};
|
|
191
|
+
if (signal.aborted) {
|
|
192
|
+
abortRequest();
|
|
122
193
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
123
194
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
195
|
+
let response;
|
|
196
|
+
try {
|
|
197
|
+
signal.addEventListener('abort', abortRequest, { once: true });
|
|
198
|
+
response = await core.invoke('plugin:http|fetch_send', {
|
|
199
|
+
rid
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (signal.aborted) {
|
|
204
|
+
throw Object.assign(new Error(ERROR_REQUEST_CANCELLED), { cause: error });
|
|
205
|
+
}
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
signal.removeEventListener('abort', abortRequest);
|
|
210
|
+
}
|
|
211
|
+
// An abort may have happened before the response's stream listener is attached.
|
|
212
|
+
if (signal.aborted) {
|
|
213
|
+
abortRequest();
|
|
214
|
+
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
215
|
+
}
|
|
216
|
+
const { status, statusText, url, headers: responseHeaders, rid: responseRid } = response;
|
|
217
|
+
// The finalizer and abort listener must not keep the stream alive through its
|
|
218
|
+
// controller. Older webviews without finalization retain explicit cleanup.
|
|
219
|
+
let bodyController;
|
|
220
|
+
const finalizerToken = {};
|
|
221
|
+
let bodyClosed = false;
|
|
222
|
+
const finishBody = () => {
|
|
223
|
+
bodyClosed = true;
|
|
224
|
+
bodyFinalizer?.unregister(finalizerToken);
|
|
225
|
+
signal.removeEventListener('abort', abortBody);
|
|
226
|
+
bodyController = undefined;
|
|
227
|
+
};
|
|
129
228
|
const dropBody = () => {
|
|
130
|
-
if (
|
|
229
|
+
if (bodyClosed)
|
|
131
230
|
return Promise.resolve();
|
|
132
|
-
|
|
133
|
-
return core.invoke('plugin:http|fetch_cancel_body', {
|
|
231
|
+
finishBody();
|
|
232
|
+
return core.invoke('plugin:http|fetch_cancel_body', {
|
|
233
|
+
rid: responseRid
|
|
234
|
+
}).catch(() => { });
|
|
235
|
+
};
|
|
236
|
+
const abortBody = () => {
|
|
237
|
+
bodyController?.deref()?.error(ERROR_REQUEST_CANCELLED);
|
|
238
|
+
void dropBody();
|
|
134
239
|
};
|
|
135
240
|
const readChunk = async (controller) => {
|
|
136
|
-
let data;
|
|
137
241
|
try {
|
|
138
|
-
data = await core.invoke('plugin:http|fetch_read_body', {
|
|
242
|
+
const data = await core.invoke('plugin:http|fetch_read_body', {
|
|
139
243
|
rid: responseRid
|
|
140
244
|
});
|
|
245
|
+
if (bodyClosed)
|
|
246
|
+
return;
|
|
247
|
+
const dataUint8 = new Uint8Array(data);
|
|
248
|
+
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
249
|
+
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
250
|
+
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
251
|
+
if (lastByte === 1) {
|
|
252
|
+
finishBody();
|
|
253
|
+
controller.close();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
controller.enqueue(actualData);
|
|
141
257
|
}
|
|
142
|
-
catch (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
controller.error(
|
|
258
|
+
catch (error) {
|
|
259
|
+
if (bodyClosed)
|
|
260
|
+
return;
|
|
261
|
+
controller.error(error);
|
|
146
262
|
void dropBody();
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const dataUint8 = new Uint8Array(data);
|
|
150
|
-
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
151
|
-
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
152
|
-
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
153
|
-
if (lastByte === 1) {
|
|
154
|
-
controller.close();
|
|
155
|
-
return;
|
|
156
263
|
}
|
|
157
|
-
controller.enqueue(actualData);
|
|
158
264
|
};
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
status,
|
|
179
|
-
statusText
|
|
180
|
-
});
|
|
181
|
-
// `Response.url` cannot be set via the constructor, so we define it manually
|
|
182
|
-
Object.defineProperty(res, 'url', { value: url, writable: false });
|
|
183
|
-
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
184
|
-
// supported). This is not Fetch-spec compliant for network responses in
|
|
185
|
-
// browsers, where `set-cookie` is treated as a forbidden response
|
|
186
|
-
// header and is generally not readable from JavaScript.
|
|
187
|
-
Object.defineProperty(res, 'headers', {
|
|
188
|
-
value: new Headers(responseHeaders),
|
|
189
|
-
writable: false
|
|
190
|
-
});
|
|
191
|
-
// Patch clone() per-instance so cloning preserves the overridden properties
|
|
192
|
-
const originalClone = res.clone.bind(res);
|
|
193
|
-
Object.defineProperty(res, 'clone', {
|
|
194
|
-
value: () => {
|
|
195
|
-
const cloned = originalClone();
|
|
196
|
-
Object.defineProperty(cloned, 'url', { value: url, writable: false });
|
|
197
|
-
Object.defineProperty(cloned, 'headers', {
|
|
198
|
-
value: new Headers(responseHeaders),
|
|
199
|
-
writable: false
|
|
200
|
-
});
|
|
201
|
-
return cloned;
|
|
265
|
+
const source = {
|
|
266
|
+
start: (controller) => {
|
|
267
|
+
bodyController = bodyFinalizer
|
|
268
|
+
? new WeakRef(controller)
|
|
269
|
+
: { deref: () => controller };
|
|
270
|
+
signal.addEventListener('abort', abortBody, { once: true });
|
|
271
|
+
},
|
|
272
|
+
pull: (controller) => readChunk(controller),
|
|
273
|
+
cancel: () => dropBody()
|
|
274
|
+
};
|
|
275
|
+
try {
|
|
276
|
+
// No body for HEAD requests or null-body statuses.
|
|
277
|
+
// see https://fetch.spec.whatwg.org/#null-body-status
|
|
278
|
+
const body = req.method === 'HEAD' || [101, 103, 204, 205, 304].includes(status)
|
|
279
|
+
? null
|
|
280
|
+
: new ReadableStream(source);
|
|
281
|
+
if (body !== null) {
|
|
282
|
+
// Clones share this source, so cleanup waits until none can read it.
|
|
283
|
+
bodyFinalizer?.register(source, dropBody, finalizerToken);
|
|
202
284
|
}
|
|
203
|
-
|
|
204
|
-
|
|
285
|
+
return patchResponse(new Response(body, { status, statusText }), url, responseHeaders);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
void dropBody();
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
205
291
|
}
|
|
206
292
|
|
|
207
293
|
exports.fetch = fetch;
|
package/dist-js/index.js
CHANGED
|
@@ -27,6 +27,76 @@ import { invoke } from '@tauri-apps/api/core';
|
|
|
27
27
|
* @module
|
|
28
28
|
*/
|
|
29
29
|
const ERROR_REQUEST_CANCELLED = 'Request cancelled';
|
|
30
|
+
const bodyFinalizer = typeof FinalizationRegistry !== 'undefined' && typeof WeakRef !== 'undefined'
|
|
31
|
+
? new FinalizationRegistry((dropBody) => {
|
|
32
|
+
void dropBody();
|
|
33
|
+
})
|
|
34
|
+
: undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Read the request body while allowing an abort to interrupt a stalled upload.
|
|
37
|
+
* `request.arrayBuffer()` locks the body internally without observing its abort
|
|
38
|
+
* signal, so keep access to the reader and cancel it when the signal fires.
|
|
39
|
+
* The caller checks the signal after buffering to reject canceled uploads.
|
|
40
|
+
*/
|
|
41
|
+
async function readRequestBody(request) {
|
|
42
|
+
const signal = request.signal;
|
|
43
|
+
if (signal.aborted) {
|
|
44
|
+
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
45
|
+
}
|
|
46
|
+
if (request.body === null) {
|
|
47
|
+
return new ArrayBuffer(0);
|
|
48
|
+
}
|
|
49
|
+
const reader = request.body.getReader();
|
|
50
|
+
const abort = () => {
|
|
51
|
+
void reader.cancel().catch(() => { });
|
|
52
|
+
};
|
|
53
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
54
|
+
try {
|
|
55
|
+
// Keep a reader we can cancel while the browser collects and validates bytes.
|
|
56
|
+
const body = new ReadableStream({
|
|
57
|
+
async pull(controller) {
|
|
58
|
+
const { done, value } = await reader.read();
|
|
59
|
+
if (done)
|
|
60
|
+
controller.close();
|
|
61
|
+
else
|
|
62
|
+
controller.enqueue(value);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return await new Response(body).arrayBuffer();
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
abort();
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
signal.removeEventListener('abort', abort);
|
|
73
|
+
reader.releaseLock();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Apply the response URL and headers and preserve them when cloning.
|
|
78
|
+
* Keep response references out of the body stream's closure scope so WebKit can
|
|
79
|
+
* collect the response once it is no longer used.
|
|
80
|
+
*/
|
|
81
|
+
function patchResponse(response, url, headers) {
|
|
82
|
+
// `Response.url` cannot be set via the constructor, so we define it manually.
|
|
83
|
+
Object.defineProperty(response, 'url', { value: url, writable: false });
|
|
84
|
+
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
85
|
+
// supported). This is not Fetch-spec compliant for network responses in
|
|
86
|
+
// browsers, where `set-cookie` is treated as a forbidden response
|
|
87
|
+
// header and is generally not readable from JavaScript.
|
|
88
|
+
Object.defineProperty(response, 'headers', {
|
|
89
|
+
value: new Headers(headers),
|
|
90
|
+
writable: false
|
|
91
|
+
});
|
|
92
|
+
// Use the receiver instead of retaining a response in a bound clone method.
|
|
93
|
+
Object.defineProperty(response, 'clone', {
|
|
94
|
+
value: function () {
|
|
95
|
+
return patchResponse(Response.prototype.clone.call(this), url, headers);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return response;
|
|
99
|
+
}
|
|
30
100
|
/**
|
|
31
101
|
* Fetch a resource from the network. It returns a `Promise` that resolves to the
|
|
32
102
|
* `Response` to that `Request`, whether it is successful or not.
|
|
@@ -52,11 +122,6 @@ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
|
|
|
52
122
|
* @since 2.0.0
|
|
53
123
|
*/
|
|
54
124
|
async function fetch(input, init) {
|
|
55
|
-
// Optimistically check for abort signal and avoid doing any work
|
|
56
|
-
const signal = init?.signal;
|
|
57
|
-
if (signal?.aborted) {
|
|
58
|
-
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
59
|
-
}
|
|
60
125
|
const maxRedirections = init?.maxRedirections;
|
|
61
126
|
const connectTimeout = init?.connectTimeout;
|
|
62
127
|
const proxy = init?.proxy;
|
|
@@ -74,8 +139,9 @@ async function fetch(input, init) {
|
|
|
74
139
|
: new Headers(init.headers)
|
|
75
140
|
: new Headers();
|
|
76
141
|
const req = new Request(input, init);
|
|
77
|
-
const
|
|
78
|
-
|
|
142
|
+
const signal = req.signal;
|
|
143
|
+
// read the upload with abort support.
|
|
144
|
+
const buffer = await readRequestBody(req);
|
|
79
145
|
// append new headers created by the browser `Request` implementation,
|
|
80
146
|
// if not already declared by the caller of this function
|
|
81
147
|
for (const [key, value] of req.headers) {
|
|
@@ -95,111 +161,131 @@ async function fetch(input, init) {
|
|
|
95
161
|
// eslint-disable-next-line
|
|
96
162
|
typeof val === 'string' ? val : val.toString()
|
|
97
163
|
]);
|
|
98
|
-
//
|
|
99
|
-
if (signal
|
|
164
|
+
// Cancellation may have ended the body read with only a partial upload.
|
|
165
|
+
if (signal.aborted) {
|
|
100
166
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
101
167
|
}
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
168
|
+
const clientConfig = {
|
|
169
|
+
method: req.method,
|
|
170
|
+
url: req.url,
|
|
171
|
+
headers: mappedHeaders,
|
|
172
|
+
maxRedirections,
|
|
173
|
+
connectTimeout,
|
|
174
|
+
proxy,
|
|
175
|
+
danger
|
|
176
|
+
};
|
|
177
|
+
const rid = await invoke('plugin:http|fetch', new Uint8Array(buffer), {
|
|
178
|
+
headers: {
|
|
179
|
+
'client-config': encodeURIComponent(JSON.stringify(clientConfig))
|
|
112
180
|
}
|
|
113
181
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
void
|
|
182
|
+
let requestCancelled = false;
|
|
183
|
+
const abortRequest = () => {
|
|
184
|
+
if (requestCancelled)
|
|
185
|
+
return;
|
|
186
|
+
requestCancelled = true;
|
|
187
|
+
void invoke('plugin:http|fetch_cancel', { rid }).catch(() => { });
|
|
188
|
+
};
|
|
189
|
+
if (signal.aborted) {
|
|
190
|
+
abortRequest();
|
|
120
191
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
121
192
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
193
|
+
let response;
|
|
194
|
+
try {
|
|
195
|
+
signal.addEventListener('abort', abortRequest, { once: true });
|
|
196
|
+
response = await invoke('plugin:http|fetch_send', {
|
|
197
|
+
rid
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (signal.aborted) {
|
|
202
|
+
throw Object.assign(new Error(ERROR_REQUEST_CANCELLED), { cause: error });
|
|
203
|
+
}
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
signal.removeEventListener('abort', abortRequest);
|
|
208
|
+
}
|
|
209
|
+
// An abort may have happened before the response's stream listener is attached.
|
|
210
|
+
if (signal.aborted) {
|
|
211
|
+
abortRequest();
|
|
212
|
+
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
213
|
+
}
|
|
214
|
+
const { status, statusText, url, headers: responseHeaders, rid: responseRid } = response;
|
|
215
|
+
// The finalizer and abort listener must not keep the stream alive through its
|
|
216
|
+
// controller. Older webviews without finalization retain explicit cleanup.
|
|
217
|
+
let bodyController;
|
|
218
|
+
const finalizerToken = {};
|
|
219
|
+
let bodyClosed = false;
|
|
220
|
+
const finishBody = () => {
|
|
221
|
+
bodyClosed = true;
|
|
222
|
+
bodyFinalizer?.unregister(finalizerToken);
|
|
223
|
+
signal.removeEventListener('abort', abortBody);
|
|
224
|
+
bodyController = undefined;
|
|
225
|
+
};
|
|
127
226
|
const dropBody = () => {
|
|
128
|
-
if (
|
|
227
|
+
if (bodyClosed)
|
|
129
228
|
return Promise.resolve();
|
|
130
|
-
|
|
131
|
-
return invoke('plugin:http|fetch_cancel_body', {
|
|
229
|
+
finishBody();
|
|
230
|
+
return invoke('plugin:http|fetch_cancel_body', {
|
|
231
|
+
rid: responseRid
|
|
232
|
+
}).catch(() => { });
|
|
233
|
+
};
|
|
234
|
+
const abortBody = () => {
|
|
235
|
+
bodyController?.deref()?.error(ERROR_REQUEST_CANCELLED);
|
|
236
|
+
void dropBody();
|
|
132
237
|
};
|
|
133
238
|
const readChunk = async (controller) => {
|
|
134
|
-
let data;
|
|
135
239
|
try {
|
|
136
|
-
data = await invoke('plugin:http|fetch_read_body', {
|
|
240
|
+
const data = await invoke('plugin:http|fetch_read_body', {
|
|
137
241
|
rid: responseRid
|
|
138
242
|
});
|
|
243
|
+
if (bodyClosed)
|
|
244
|
+
return;
|
|
245
|
+
const dataUint8 = new Uint8Array(data);
|
|
246
|
+
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
247
|
+
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
248
|
+
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
249
|
+
if (lastByte === 1) {
|
|
250
|
+
finishBody();
|
|
251
|
+
controller.close();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
controller.enqueue(actualData);
|
|
139
255
|
}
|
|
140
|
-
catch (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
controller.error(
|
|
256
|
+
catch (error) {
|
|
257
|
+
if (bodyClosed)
|
|
258
|
+
return;
|
|
259
|
+
controller.error(error);
|
|
144
260
|
void dropBody();
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const dataUint8 = new Uint8Array(data);
|
|
148
|
-
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
149
|
-
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
150
|
-
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
151
|
-
if (lastByte === 1) {
|
|
152
|
-
controller.close();
|
|
153
|
-
return;
|
|
154
261
|
}
|
|
155
|
-
controller.enqueue(actualData);
|
|
156
262
|
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
status,
|
|
177
|
-
statusText
|
|
178
|
-
});
|
|
179
|
-
// `Response.url` cannot be set via the constructor, so we define it manually
|
|
180
|
-
Object.defineProperty(res, 'url', { value: url, writable: false });
|
|
181
|
-
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
182
|
-
// supported). This is not Fetch-spec compliant for network responses in
|
|
183
|
-
// browsers, where `set-cookie` is treated as a forbidden response
|
|
184
|
-
// header and is generally not readable from JavaScript.
|
|
185
|
-
Object.defineProperty(res, 'headers', {
|
|
186
|
-
value: new Headers(responseHeaders),
|
|
187
|
-
writable: false
|
|
188
|
-
});
|
|
189
|
-
// Patch clone() per-instance so cloning preserves the overridden properties
|
|
190
|
-
const originalClone = res.clone.bind(res);
|
|
191
|
-
Object.defineProperty(res, 'clone', {
|
|
192
|
-
value: () => {
|
|
193
|
-
const cloned = originalClone();
|
|
194
|
-
Object.defineProperty(cloned, 'url', { value: url, writable: false });
|
|
195
|
-
Object.defineProperty(cloned, 'headers', {
|
|
196
|
-
value: new Headers(responseHeaders),
|
|
197
|
-
writable: false
|
|
198
|
-
});
|
|
199
|
-
return cloned;
|
|
263
|
+
const source = {
|
|
264
|
+
start: (controller) => {
|
|
265
|
+
bodyController = bodyFinalizer
|
|
266
|
+
? new WeakRef(controller)
|
|
267
|
+
: { deref: () => controller };
|
|
268
|
+
signal.addEventListener('abort', abortBody, { once: true });
|
|
269
|
+
},
|
|
270
|
+
pull: (controller) => readChunk(controller),
|
|
271
|
+
cancel: () => dropBody()
|
|
272
|
+
};
|
|
273
|
+
try {
|
|
274
|
+
// No body for HEAD requests or null-body statuses.
|
|
275
|
+
// see https://fetch.spec.whatwg.org/#null-body-status
|
|
276
|
+
const body = req.method === 'HEAD' || [101, 103, 204, 205, 304].includes(status)
|
|
277
|
+
? null
|
|
278
|
+
: new ReadableStream(source);
|
|
279
|
+
if (body !== null) {
|
|
280
|
+
// Clones share this source, so cleanup waits until none can read it.
|
|
281
|
+
bodyFinalizer?.register(source, dropBody, finalizerToken);
|
|
200
282
|
}
|
|
201
|
-
|
|
202
|
-
|
|
283
|
+
return patchResponse(new Response(body, { status, statusText }), url, responseHeaders);
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
void dropBody();
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
203
289
|
}
|
|
204
290
|
|
|
205
291
|
export { fetch };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tauri-apps/plugin-http",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"license": "MIT OR Apache-2.0",
|
|
5
5
|
"authors": [
|
|
6
6
|
"Tauri Programme within The Commons Conservancy"
|
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
"LICENSE"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@tauri-apps/api": "^2.
|
|
27
|
+
"@tauri-apps/api": "^2.13.0"
|
|
28
28
|
}
|
|
29
29
|
}
|