@tauri-apps/plugin-http 2.4.0 → 2.4.1

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
@@ -28,7 +28,7 @@ var core = require('@tauri-apps/api/core');
28
28
  *
29
29
  * @module
30
30
  */
31
- const ERROR_REQUEST_CANCELLED = 'Request canceled';
31
+ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
32
32
  /**
33
33
  * Fetch a resource from the network. It returns a `Promise` that resolves to the
34
34
  * `Response` to that `Request`, whether it is successful or not.
@@ -91,29 +91,6 @@ async function fetch(input, init) {
91
91
  if (signal?.aborted) {
92
92
  throw new Error(ERROR_REQUEST_CANCELLED);
93
93
  }
94
- const streamChannel = new core.Channel();
95
- const readableStreamBody = new ReadableStream({
96
- start: (controller) => {
97
- streamChannel.onmessage = (res) => {
98
- // close early if aborted
99
- if (signal?.aborted) {
100
- controller.error(ERROR_REQUEST_CANCELLED);
101
- controller.close();
102
- return;
103
- }
104
- // close when the signal to close (an empty chunk)
105
- // is sent from the IPC.
106
- if (res instanceof ArrayBuffer ? res.byteLength == 0 : res.length == 0) {
107
- controller.close();
108
- return;
109
- }
110
- // the content conversion (like .text(), .json(), etc.) in Response
111
- // must have Uint8Array as its content, else it will
112
- // have untraceable error that's hard to debug.
113
- controller.enqueue(new Uint8Array(res));
114
- };
115
- }
116
- });
117
94
  const rid = await core.invoke('plugin:http|fetch', {
118
95
  clientConfig: {
119
96
  method: req.method,
@@ -124,8 +101,7 @@ async function fetch(input, init) {
124
101
  connectTimeout,
125
102
  proxy,
126
103
  danger
127
- },
128
- streamChannel
104
+ }
129
105
  });
130
106
  const abort = () => core.invoke('plugin:http|fetch_cancel', { rid });
131
107
  // abort early here if needed
@@ -136,9 +112,38 @@ async function fetch(input, init) {
136
112
  throw new Error(ERROR_REQUEST_CANCELLED);
137
113
  }
138
114
  signal?.addEventListener('abort', () => void abort());
139
- const { status, statusText, url, headers: responseHeaders } = await core.invoke('plugin:http|fetch_send', {
115
+ const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await core.invoke('plugin:http|fetch_send', {
140
116
  rid
141
117
  });
118
+ const readableStreamBody = new ReadableStream({
119
+ start: (controller) => {
120
+ const streamChannel = new core.Channel();
121
+ streamChannel.onmessage = (res) => {
122
+ // close early if aborted
123
+ if (signal?.aborted) {
124
+ controller.error(ERROR_REQUEST_CANCELLED);
125
+ return;
126
+ }
127
+ // close when the signal to close (an empty chunk)
128
+ // is sent from the IPC.
129
+ if (res instanceof ArrayBuffer ? res.byteLength == 0 : res.length == 0) {
130
+ controller.close();
131
+ return;
132
+ }
133
+ // the content conversion (like .text(), .json(), etc.) in Response
134
+ // must have Uint8Array as its content, else it will
135
+ // have untraceable error that's hard to debug.
136
+ controller.enqueue(new Uint8Array(res));
137
+ };
138
+ // run a non-blocking body stream fetch
139
+ core.invoke('plugin:http|fetch_read_body', {
140
+ rid: responseRid,
141
+ streamChannel
142
+ }).catch((e) => {
143
+ controller.error(e);
144
+ });
145
+ }
146
+ });
142
147
  const res = new Response(readableStreamBody, {
143
148
  status,
144
149
  statusText
package/dist-js/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Channel, invoke } from '@tauri-apps/api/core';
1
+ import { invoke, Channel } 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
@@ -26,7 +26,7 @@ import { Channel, invoke } from '@tauri-apps/api/core';
26
26
  *
27
27
  * @module
28
28
  */
29
- const ERROR_REQUEST_CANCELLED = 'Request canceled';
29
+ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
30
30
  /**
31
31
  * Fetch a resource from the network. It returns a `Promise` that resolves to the
32
32
  * `Response` to that `Request`, whether it is successful or not.
@@ -89,29 +89,6 @@ async function fetch(input, init) {
89
89
  if (signal?.aborted) {
90
90
  throw new Error(ERROR_REQUEST_CANCELLED);
91
91
  }
92
- const streamChannel = new Channel();
93
- const readableStreamBody = new ReadableStream({
94
- start: (controller) => {
95
- streamChannel.onmessage = (res) => {
96
- // close early if aborted
97
- if (signal?.aborted) {
98
- controller.error(ERROR_REQUEST_CANCELLED);
99
- controller.close();
100
- return;
101
- }
102
- // close when the signal to close (an empty chunk)
103
- // is sent from the IPC.
104
- if (res instanceof ArrayBuffer ? res.byteLength == 0 : res.length == 0) {
105
- controller.close();
106
- return;
107
- }
108
- // the content conversion (like .text(), .json(), etc.) in Response
109
- // must have Uint8Array as its content, else it will
110
- // have untraceable error that's hard to debug.
111
- controller.enqueue(new Uint8Array(res));
112
- };
113
- }
114
- });
115
92
  const rid = await invoke('plugin:http|fetch', {
116
93
  clientConfig: {
117
94
  method: req.method,
@@ -122,8 +99,7 @@ async function fetch(input, init) {
122
99
  connectTimeout,
123
100
  proxy,
124
101
  danger
125
- },
126
- streamChannel
102
+ }
127
103
  });
128
104
  const abort = () => invoke('plugin:http|fetch_cancel', { rid });
129
105
  // abort early here if needed
@@ -134,9 +110,38 @@ async function fetch(input, init) {
134
110
  throw new Error(ERROR_REQUEST_CANCELLED);
135
111
  }
136
112
  signal?.addEventListener('abort', () => void abort());
137
- const { status, statusText, url, headers: responseHeaders } = await invoke('plugin:http|fetch_send', {
113
+ const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await invoke('plugin:http|fetch_send', {
138
114
  rid
139
115
  });
116
+ const readableStreamBody = new ReadableStream({
117
+ start: (controller) => {
118
+ const streamChannel = new Channel();
119
+ streamChannel.onmessage = (res) => {
120
+ // close early if aborted
121
+ if (signal?.aborted) {
122
+ controller.error(ERROR_REQUEST_CANCELLED);
123
+ return;
124
+ }
125
+ // close when the signal to close (an empty chunk)
126
+ // is sent from the IPC.
127
+ if (res instanceof ArrayBuffer ? res.byteLength == 0 : res.length == 0) {
128
+ controller.close();
129
+ return;
130
+ }
131
+ // the content conversion (like .text(), .json(), etc.) in Response
132
+ // must have Uint8Array as its content, else it will
133
+ // have untraceable error that's hard to debug.
134
+ controller.enqueue(new Uint8Array(res));
135
+ };
136
+ // run a non-blocking body stream fetch
137
+ invoke('plugin:http|fetch_read_body', {
138
+ rid: responseRid,
139
+ streamChannel
140
+ }).catch((e) => {
141
+ controller.error(e);
142
+ });
143
+ }
144
+ });
140
145
  const res = new Response(readableStreamBody, {
141
146
  status,
142
147
  statusText
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-http",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"