@tauri-apps/plugin-http 2.3.0 → 2.4.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/dist-js/index.cjs CHANGED
@@ -91,6 +91,29 @@ 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
+ });
94
117
  const rid = await core.invoke('plugin:http|fetch', {
95
118
  clientConfig: {
96
119
  method: req.method,
@@ -101,7 +124,8 @@ async function fetch(input, init) {
101
124
  connectTimeout,
102
125
  proxy,
103
126
  danger
104
- }
127
+ },
128
+ streamChannel
105
129
  });
106
130
  const abort = () => core.invoke('plugin:http|fetch_cancel', { rid });
107
131
  // abort early here if needed
@@ -112,17 +136,10 @@ async function fetch(input, init) {
112
136
  throw new Error(ERROR_REQUEST_CANCELLED);
113
137
  }
114
138
  signal?.addEventListener('abort', () => void abort());
115
- const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await core.invoke('plugin:http|fetch_send', {
139
+ const { status, statusText, url, headers: responseHeaders } = await core.invoke('plugin:http|fetch_send', {
116
140
  rid
117
141
  });
118
- const body = await core.invoke('plugin:http|fetch_read_body', {
119
- rid: responseRid
120
- });
121
- const res = new Response(body instanceof ArrayBuffer && body.byteLength !== 0
122
- ? body
123
- : body instanceof Array && body.length > 0
124
- ? new Uint8Array(body)
125
- : null, {
142
+ const res = new Response(readableStreamBody, {
126
143
  status,
127
144
  statusText
128
145
  });
package/dist-js/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { invoke } from '@tauri-apps/api/core';
1
+ import { Channel, 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
@@ -89,6 +89,29 @@ 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
+ });
92
115
  const rid = await invoke('plugin:http|fetch', {
93
116
  clientConfig: {
94
117
  method: req.method,
@@ -99,7 +122,8 @@ async function fetch(input, init) {
99
122
  connectTimeout,
100
123
  proxy,
101
124
  danger
102
- }
125
+ },
126
+ streamChannel
103
127
  });
104
128
  const abort = () => invoke('plugin:http|fetch_cancel', { rid });
105
129
  // abort early here if needed
@@ -110,17 +134,10 @@ async function fetch(input, init) {
110
134
  throw new Error(ERROR_REQUEST_CANCELLED);
111
135
  }
112
136
  signal?.addEventListener('abort', () => void abort());
113
- const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await invoke('plugin:http|fetch_send', {
137
+ const { status, statusText, url, headers: responseHeaders } = await invoke('plugin:http|fetch_send', {
114
138
  rid
115
139
  });
116
- const body = await invoke('plugin:http|fetch_read_body', {
117
- rid: responseRid
118
- });
119
- const res = new Response(body instanceof ArrayBuffer && body.byteLength !== 0
120
- ? body
121
- : body instanceof Array && body.length > 0
122
- ? new Uint8Array(body)
123
- : null, {
140
+ const res = new Response(readableStreamBody, {
124
141
  status,
125
142
  statusText
126
143
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-http",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"