@tauri-apps/plugin-http 2.5.4 → 2.5.6

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/dist-js/index.cjs CHANGED
@@ -44,7 +44,7 @@ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
44
44
  * @since 2.0.0
45
45
  */
46
46
  async function fetch(input, init) {
47
- // abort early here if needed
47
+ // Optimistically check for abort signal and avoid doing any work
48
48
  const signal = init?.signal;
49
49
  if (signal?.aborted) {
50
50
  throw new Error(ERROR_REQUEST_CANCELLED);
@@ -87,7 +87,7 @@ async function fetch(input, init) {
87
87
  // eslint-disable-next-line
88
88
  typeof val === 'string' ? val : val.toString()
89
89
  ]);
90
- // abort early here if needed
90
+ // Optimistically check for abort signal and avoid doing any work on the Rust side
91
91
  if (signal?.aborted) {
92
92
  throw new Error(ERROR_REQUEST_CANCELLED);
93
93
  }
@@ -104,7 +104,8 @@ async function fetch(input, init) {
104
104
  }
105
105
  });
106
106
  const abort = () => core.invoke('plugin:http|fetch_cancel', { rid });
107
- // abort early here if needed
107
+ // Optimistically check for abort signal
108
+ // and avoid doing any work after doing intial work on the Rust side
108
109
  if (signal?.aborted) {
109
110
  // we don't care about the result of this proimse
110
111
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -115,37 +116,46 @@ async function fetch(input, init) {
115
116
  const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await core.invoke('plugin:http|fetch_send', {
116
117
  rid
117
118
  });
119
+ const dropBody = () => {
120
+ return core.invoke('plugin:http|fetch_cancel_body', { rid: responseRid });
121
+ };
122
+ const readChunk = async (controller) => {
123
+ let data;
124
+ try {
125
+ data = await core.invoke('plugin:http|fetch_read_body', {
126
+ rid: responseRid
127
+ });
128
+ }
129
+ catch (e) {
130
+ // close the stream if an error occurs
131
+ // and drop the body on Rust side
132
+ controller.error(e);
133
+ void dropBody();
134
+ return;
135
+ }
136
+ const dataUint8 = new Uint8Array(data);
137
+ const lastByte = dataUint8[dataUint8.byteLength - 1];
138
+ const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
139
+ // close when the signal to close (last byte is 1) is sent from the IPC.
140
+ if (lastByte === 1) {
141
+ controller.close();
142
+ return;
143
+ }
144
+ controller.enqueue(actualData);
145
+ };
118
146
  // no body for 101, 103, 204, 205 and 304
119
147
  // see https://fetch.spec.whatwg.org/#null-body-status
120
148
  const body = [101, 103, 204, 205, 304].includes(status)
121
149
  ? null
122
150
  : new ReadableStream({
123
151
  start: (controller) => {
124
- const streamChannel = new core.Channel();
125
- streamChannel.onmessage = (res) => {
126
- // close early if aborted
127
- if (signal?.aborted) {
128
- controller.error(ERROR_REQUEST_CANCELLED);
129
- return;
130
- }
131
- const resUint8 = new Uint8Array(res);
132
- const lastByte = resUint8[resUint8.byteLength - 1];
133
- const actualRes = resUint8.slice(0, resUint8.byteLength - 1);
134
- // close when the signal to close (last byte is 1) is sent from the IPC.
135
- if (lastByte == 1) {
136
- controller.close();
137
- return;
138
- }
139
- controller.enqueue(actualRes);
140
- };
141
- // run a non-blocking body stream fetch
142
- core.invoke('plugin:http|fetch_read_body', {
143
- rid: responseRid,
144
- streamChannel
145
- }).catch((e) => {
146
- controller.error(e);
152
+ // listen for abort events to cancel reading
153
+ signal?.addEventListener('abort', () => {
154
+ controller.error(ERROR_REQUEST_CANCELLED);
155
+ void dropBody();
147
156
  });
148
- }
157
+ },
158
+ pull: (controller) => readChunk(controller)
149
159
  });
150
160
  const res = new Response(body, {
151
161
  status,
package/dist-js/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { invoke, Channel } from '@tauri-apps/api/core';
1
+ import { invoke } from '@tauri-apps/api/core';
2
2
 
3
3
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
4
4
  // SPDX-License-Identifier: Apache-2.0
@@ -42,7 +42,7 @@ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
42
42
  * @since 2.0.0
43
43
  */
44
44
  async function fetch(input, init) {
45
- // abort early here if needed
45
+ // Optimistically check for abort signal and avoid doing any work
46
46
  const signal = init?.signal;
47
47
  if (signal?.aborted) {
48
48
  throw new Error(ERROR_REQUEST_CANCELLED);
@@ -85,7 +85,7 @@ async function fetch(input, init) {
85
85
  // eslint-disable-next-line
86
86
  typeof val === 'string' ? val : val.toString()
87
87
  ]);
88
- // abort early here if needed
88
+ // Optimistically check for abort signal and avoid doing any work on the Rust side
89
89
  if (signal?.aborted) {
90
90
  throw new Error(ERROR_REQUEST_CANCELLED);
91
91
  }
@@ -102,7 +102,8 @@ async function fetch(input, init) {
102
102
  }
103
103
  });
104
104
  const abort = () => invoke('plugin:http|fetch_cancel', { rid });
105
- // abort early here if needed
105
+ // Optimistically check for abort signal
106
+ // and avoid doing any work after doing intial work on the Rust side
106
107
  if (signal?.aborted) {
107
108
  // we don't care about the result of this proimse
108
109
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -113,37 +114,46 @@ async function fetch(input, init) {
113
114
  const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await invoke('plugin:http|fetch_send', {
114
115
  rid
115
116
  });
117
+ const dropBody = () => {
118
+ return invoke('plugin:http|fetch_cancel_body', { rid: responseRid });
119
+ };
120
+ const readChunk = async (controller) => {
121
+ let data;
122
+ try {
123
+ data = await invoke('plugin:http|fetch_read_body', {
124
+ rid: responseRid
125
+ });
126
+ }
127
+ catch (e) {
128
+ // close the stream if an error occurs
129
+ // and drop the body on Rust side
130
+ controller.error(e);
131
+ void dropBody();
132
+ return;
133
+ }
134
+ const dataUint8 = new Uint8Array(data);
135
+ const lastByte = dataUint8[dataUint8.byteLength - 1];
136
+ const actualData = dataUint8.slice(0, dataUint8.byteLength - 1);
137
+ // close when the signal to close (last byte is 1) is sent from the IPC.
138
+ if (lastByte === 1) {
139
+ controller.close();
140
+ return;
141
+ }
142
+ controller.enqueue(actualData);
143
+ };
116
144
  // no body for 101, 103, 204, 205 and 304
117
145
  // see https://fetch.spec.whatwg.org/#null-body-status
118
146
  const body = [101, 103, 204, 205, 304].includes(status)
119
147
  ? null
120
148
  : new ReadableStream({
121
149
  start: (controller) => {
122
- const streamChannel = new Channel();
123
- streamChannel.onmessage = (res) => {
124
- // close early if aborted
125
- if (signal?.aborted) {
126
- controller.error(ERROR_REQUEST_CANCELLED);
127
- return;
128
- }
129
- const resUint8 = new Uint8Array(res);
130
- const lastByte = resUint8[resUint8.byteLength - 1];
131
- const actualRes = resUint8.slice(0, resUint8.byteLength - 1);
132
- // close when the signal to close (last byte is 1) is sent from the IPC.
133
- if (lastByte == 1) {
134
- controller.close();
135
- return;
136
- }
137
- controller.enqueue(actualRes);
138
- };
139
- // run a non-blocking body stream fetch
140
- invoke('plugin:http|fetch_read_body', {
141
- rid: responseRid,
142
- streamChannel
143
- }).catch((e) => {
144
- controller.error(e);
150
+ // listen for abort events to cancel reading
151
+ signal?.addEventListener('abort', () => {
152
+ controller.error(ERROR_REQUEST_CANCELLED);
153
+ void dropBody();
145
154
  });
146
- }
155
+ },
156
+ pull: (controller) => readChunk(controller)
147
157
  });
148
158
  const res = new Response(body, {
149
159
  status,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-http",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"