@tauri-apps/plugin-http 2.4.0 → 2.4.2

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,19 +112,46 @@ 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
+ const resUint8 = new Uint8Array(res);
128
+ const lastByte = resUint8[resUint8.byteLength - 1];
129
+ const actualRes = resUint8.slice(0, resUint8.byteLength - 1);
130
+ // close when the signal to close (last byte is 1) is sent from the IPC.
131
+ if (lastByte == 1) {
132
+ controller.close();
133
+ return;
134
+ }
135
+ controller.enqueue(actualRes);
136
+ };
137
+ // run a non-blocking body stream fetch
138
+ core.invoke('plugin:http|fetch_read_body', {
139
+ rid: responseRid,
140
+ streamChannel
141
+ }).catch((e) => {
142
+ controller.error(e);
143
+ });
144
+ }
145
+ });
142
146
  const res = new Response(readableStreamBody, {
143
147
  status,
144
148
  statusText
145
149
  });
146
- // url and headers are read only properties
147
- // but seems like we can set them like this
150
+ // Set `Response` properties that are ignored by the
151
+ // constructor, like url and some headers
148
152
  //
149
- // we define theme like this, because using `Response`
150
- // constructor, it removes url and some headers
151
- // like `set-cookie` headers
153
+ // Since url and headers are read only properties
154
+ // this is the only way to set them.
152
155
  Object.defineProperty(res, 'url', { value: url });
153
156
  Object.defineProperty(res, 'headers', {
154
157
  value: new Headers(responseHeaders)
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,19 +110,46 @@ 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
+ const resUint8 = new Uint8Array(res);
126
+ const lastByte = resUint8[resUint8.byteLength - 1];
127
+ const actualRes = resUint8.slice(0, resUint8.byteLength - 1);
128
+ // close when the signal to close (last byte is 1) is sent from the IPC.
129
+ if (lastByte == 1) {
130
+ controller.close();
131
+ return;
132
+ }
133
+ controller.enqueue(actualRes);
134
+ };
135
+ // run a non-blocking body stream fetch
136
+ invoke('plugin:http|fetch_read_body', {
137
+ rid: responseRid,
138
+ streamChannel
139
+ }).catch((e) => {
140
+ controller.error(e);
141
+ });
142
+ }
143
+ });
140
144
  const res = new Response(readableStreamBody, {
141
145
  status,
142
146
  statusText
143
147
  });
144
- // url and headers are read only properties
145
- // but seems like we can set them like this
148
+ // Set `Response` properties that are ignored by the
149
+ // constructor, like url and some headers
146
150
  //
147
- // we define theme like this, because using `Response`
148
- // constructor, it removes url and some headers
149
- // like `set-cookie` headers
151
+ // Since url and headers are read only properties
152
+ // this is the only way to set them.
150
153
  Object.defineProperty(res, 'url', { value: url });
151
154
  Object.defineProperty(res, 'headers', {
152
155
  value: new Headers(responseHeaders)
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.2",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"