@tauri-apps/plugin-http 2.3.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.
@@ -115,14 +115,36 @@ async function fetch(input, init) {
115
115
  const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await core.invoke('plugin:http|fetch_send', {
116
116
  rid
117
117
  });
118
- const body = await core.invoke('plugin:http|fetch_read_body', {
119
- rid: responseRid
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
+ }
120
146
  });
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, {
147
+ const res = new Response(readableStreamBody, {
126
148
  status,
127
149
  statusText
128
150
  });
package/dist-js/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { 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 { 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.
@@ -113,14 +113,36 @@ async function fetch(input, init) {
113
113
  const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await invoke('plugin:http|fetch_send', {
114
114
  rid
115
115
  });
116
- const body = await invoke('plugin:http|fetch_read_body', {
117
- rid: responseRid
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
+ }
118
144
  });
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, {
145
+ const res = new Response(readableStreamBody, {
124
146
  status,
125
147
  statusText
126
148
  });
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.1",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"