@tauri-apps/plugin-http 2.9.0 → 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 +168 -87
- package/dist-js/index.js +168 -87
- 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,7 +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
|
|
144
|
+
const signal = req.signal;
|
|
145
|
+
// read the upload with abort support.
|
|
146
|
+
const buffer = await readRequestBody(req);
|
|
80
147
|
// append new headers created by the browser `Request` implementation,
|
|
81
148
|
// if not already declared by the caller of this function
|
|
82
149
|
for (const [key, value] of req.headers) {
|
|
@@ -96,8 +163,8 @@ async function fetch(input, init) {
|
|
|
96
163
|
// eslint-disable-next-line
|
|
97
164
|
typeof val === 'string' ? val : val.toString()
|
|
98
165
|
]);
|
|
99
|
-
//
|
|
100
|
-
if (signal
|
|
166
|
+
// Cancellation may have ended the body read with only a partial upload.
|
|
167
|
+
if (signal.aborted) {
|
|
101
168
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
102
169
|
}
|
|
103
170
|
const clientConfig = {
|
|
@@ -114,99 +181,113 @@ async function fetch(input, init) {
|
|
|
114
181
|
'client-config': encodeURIComponent(JSON.stringify(clientConfig))
|
|
115
182
|
}
|
|
116
183
|
});
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
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();
|
|
123
193
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
124
194
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
};
|
|
130
228
|
const dropBody = () => {
|
|
131
|
-
if (
|
|
229
|
+
if (bodyClosed)
|
|
132
230
|
return Promise.resolve();
|
|
133
|
-
|
|
134
|
-
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();
|
|
135
239
|
};
|
|
136
240
|
const readChunk = async (controller) => {
|
|
137
|
-
let data;
|
|
138
241
|
try {
|
|
139
|
-
data = await core.invoke('plugin:http|fetch_read_body', {
|
|
242
|
+
const data = await core.invoke('plugin:http|fetch_read_body', {
|
|
140
243
|
rid: responseRid
|
|
141
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);
|
|
142
257
|
}
|
|
143
|
-
catch (
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
controller.error(
|
|
258
|
+
catch (error) {
|
|
259
|
+
if (bodyClosed)
|
|
260
|
+
return;
|
|
261
|
+
controller.error(error);
|
|
147
262
|
void dropBody();
|
|
148
|
-
return;
|
|
149
263
|
}
|
|
150
|
-
const dataUint8 = new Uint8Array(data);
|
|
151
|
-
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
152
|
-
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
153
|
-
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
154
|
-
if (lastByte === 1) {
|
|
155
|
-
controller.close();
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
controller.enqueue(actualData);
|
|
159
264
|
};
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// No stream will read or cancel this response's Rust resource.
|
|
180
|
-
await dropBody();
|
|
181
|
-
}
|
|
182
|
-
const res = new Response(body, {
|
|
183
|
-
status,
|
|
184
|
-
statusText
|
|
185
|
-
});
|
|
186
|
-
// `Response.url` cannot be set via the constructor, so we define it manually
|
|
187
|
-
Object.defineProperty(res, 'url', { value: url, writable: false });
|
|
188
|
-
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
189
|
-
// supported). This is not Fetch-spec compliant for network responses in
|
|
190
|
-
// browsers, where `set-cookie` is treated as a forbidden response
|
|
191
|
-
// header and is generally not readable from JavaScript.
|
|
192
|
-
Object.defineProperty(res, 'headers', {
|
|
193
|
-
value: new Headers(responseHeaders),
|
|
194
|
-
writable: false
|
|
195
|
-
});
|
|
196
|
-
// Patch clone() per-instance so cloning preserves the overridden properties
|
|
197
|
-
const originalClone = res.clone.bind(res);
|
|
198
|
-
Object.defineProperty(res, 'clone', {
|
|
199
|
-
value: () => {
|
|
200
|
-
const cloned = originalClone();
|
|
201
|
-
Object.defineProperty(cloned, 'url', { value: url, writable: false });
|
|
202
|
-
Object.defineProperty(cloned, 'headers', {
|
|
203
|
-
value: new Headers(responseHeaders),
|
|
204
|
-
writable: false
|
|
205
|
-
});
|
|
206
|
-
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);
|
|
207
284
|
}
|
|
208
|
-
|
|
209
|
-
|
|
285
|
+
return patchResponse(new Response(body, { status, statusText }), url, responseHeaders);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
void dropBody();
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
210
291
|
}
|
|
211
292
|
|
|
212
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,7 +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
|
|
142
|
+
const signal = req.signal;
|
|
143
|
+
// read the upload with abort support.
|
|
144
|
+
const buffer = await readRequestBody(req);
|
|
78
145
|
// append new headers created by the browser `Request` implementation,
|
|
79
146
|
// if not already declared by the caller of this function
|
|
80
147
|
for (const [key, value] of req.headers) {
|
|
@@ -94,8 +161,8 @@ async function fetch(input, init) {
|
|
|
94
161
|
// eslint-disable-next-line
|
|
95
162
|
typeof val === 'string' ? val : val.toString()
|
|
96
163
|
]);
|
|
97
|
-
//
|
|
98
|
-
if (signal
|
|
164
|
+
// Cancellation may have ended the body read with only a partial upload.
|
|
165
|
+
if (signal.aborted) {
|
|
99
166
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
100
167
|
}
|
|
101
168
|
const clientConfig = {
|
|
@@ -112,99 +179,113 @@ async function fetch(input, init) {
|
|
|
112
179
|
'client-config': encodeURIComponent(JSON.stringify(clientConfig))
|
|
113
180
|
}
|
|
114
181
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
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();
|
|
121
191
|
throw new Error(ERROR_REQUEST_CANCELLED);
|
|
122
192
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
+
};
|
|
128
226
|
const dropBody = () => {
|
|
129
|
-
if (
|
|
227
|
+
if (bodyClosed)
|
|
130
228
|
return Promise.resolve();
|
|
131
|
-
|
|
132
|
-
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();
|
|
133
237
|
};
|
|
134
238
|
const readChunk = async (controller) => {
|
|
135
|
-
let data;
|
|
136
239
|
try {
|
|
137
|
-
data = await invoke('plugin:http|fetch_read_body', {
|
|
240
|
+
const data = await invoke('plugin:http|fetch_read_body', {
|
|
138
241
|
rid: responseRid
|
|
139
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);
|
|
140
255
|
}
|
|
141
|
-
catch (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
controller.error(
|
|
256
|
+
catch (error) {
|
|
257
|
+
if (bodyClosed)
|
|
258
|
+
return;
|
|
259
|
+
controller.error(error);
|
|
145
260
|
void dropBody();
|
|
146
|
-
return;
|
|
147
261
|
}
|
|
148
|
-
const dataUint8 = new Uint8Array(data);
|
|
149
|
-
const lastByte = dataUint8[dataUint8.byteLength - 1];
|
|
150
|
-
const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
|
|
151
|
-
// close when the signal to close (last byte is 1) is sent from the IPC.
|
|
152
|
-
if (lastByte === 1) {
|
|
153
|
-
controller.close();
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
controller.enqueue(actualData);
|
|
157
262
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// No stream will read or cancel this response's Rust resource.
|
|
178
|
-
await dropBody();
|
|
179
|
-
}
|
|
180
|
-
const res = new Response(body, {
|
|
181
|
-
status,
|
|
182
|
-
statusText
|
|
183
|
-
});
|
|
184
|
-
// `Response.url` cannot be set via the constructor, so we define it manually
|
|
185
|
-
Object.defineProperty(res, 'url', { value: url, writable: false });
|
|
186
|
-
// Expose `set-cookie` via `response.headers` (and `getSetCookie()` where
|
|
187
|
-
// supported). This is not Fetch-spec compliant for network responses in
|
|
188
|
-
// browsers, where `set-cookie` is treated as a forbidden response
|
|
189
|
-
// header and is generally not readable from JavaScript.
|
|
190
|
-
Object.defineProperty(res, 'headers', {
|
|
191
|
-
value: new Headers(responseHeaders),
|
|
192
|
-
writable: false
|
|
193
|
-
});
|
|
194
|
-
// Patch clone() per-instance so cloning preserves the overridden properties
|
|
195
|
-
const originalClone = res.clone.bind(res);
|
|
196
|
-
Object.defineProperty(res, 'clone', {
|
|
197
|
-
value: () => {
|
|
198
|
-
const cloned = originalClone();
|
|
199
|
-
Object.defineProperty(cloned, 'url', { value: url, writable: false });
|
|
200
|
-
Object.defineProperty(cloned, 'headers', {
|
|
201
|
-
value: new Headers(responseHeaders),
|
|
202
|
-
writable: false
|
|
203
|
-
});
|
|
204
|
-
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);
|
|
205
282
|
}
|
|
206
|
-
|
|
207
|
-
|
|
283
|
+
return patchResponse(new Response(body, { status, statusText }), url, responseHeaders);
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
void dropBody();
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
208
289
|
}
|
|
209
290
|
|
|
210
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
|
}
|