Skip to content

Commit

Permalink
release: v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
idootop committed Mar 24, 2024
1 parent 21fb5e9 commit 4e11522
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 244 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ urlpattern = "0.2"
http = "0.2"
reqwest = "0.11"
data-url = "0.3"
once_cell = "1.19.0"
tokio = { version = "1.36.0", features = ["macros"] }

[build-dependencies]
tauri-plugin = { version = "2.0.0-beta.9", features = ["build"] }
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ An **unofficial** Tauri plugin that enables seamless cross-origin resource shari

## Motivation

When developing cross-platform desktop applications with [Tauri](https://tauri.app), you may encounter CORS restrictions that prevent direct access to certain web resources, such as [OpenAI](https://openai.com/product) services. While the official [tauri-plugin-http](https://docs.rs/crate/tauri-plugin-http) can achieve CORS bypassing, it requires modifying your network requests and may not be compatible with third-party dependencies that rely on the standard `fetch` API.

When building cross-platform desktop applications with [Tauri](https://tauri.app), we often need to access services like [OpenAI](https://openai.com/product) that are restricted by **Cross-Origin Resource Sharing (CORS)** policies in web environments.

However, on the desktop, we can bypass CORS and access these services directly. While the official [tauri-plugin-http](https://docs.rs/crate/tauri-plugin-http) can bypass CORS, it requires modifying your network requests and might not be compatible with third-party dependencies that rely on the standard `fetch` API.

## How it Works

Expand Down Expand Up @@ -78,7 +81,7 @@ window.originalFetch("https://example.com/api");

## Limitation

1. **No Custom CSP Policy Support**: By default, all HTTP/HTTPS requests will be redirected to [tauri-plugin-http](https://docs.rs/crate/tauri-plugin-http).
1. **No Custom CSP Policy Support**: By default, all HTTP/HTTPS requests will be redirected to local native requests.
2. **No XMLHttpRequest Support**: The plugin is designed specifically to work with the modern `fetch` API and does not support `XMLHttpRequest` (XHR) requests.

## License
Expand Down
51 changes: 14 additions & 37 deletions api-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,31 @@ class CORSFetch {
? Array.from(new Uint8Array(buffer))
: null;

console.log("✅ start fetch", {
method: req.method,
url: req.url,
headers: mappedHeaders,
data: reqData,
maxRedirections,
connectTimeout,
proxy,
});
const rid = await this._invoke("plugin:cors-fetch|fetch", {
clientConfig: {
method: req.method,
url: req.url,
headers: mappedHeaders,
data: reqData,
maxRedirections,
connectTimeout,
proxy,
},
});

console.log("✅ rid", rid);

signal?.addEventListener("abort", async (e) => {
const error = e.target.reason;
this._invoke("plugin:cors-fetch|fetch_cancel", {
rid,
this._invoke("plugin:cors-fetch|cancel_cors_request", {
requestId,
}).catch(() => {});
console.log("❌ fetch_cancel error", error);
reject(error);
});

console.log("✅ fetch_send", rid);
const {
status,
statusText,
url,
body,
headers: responseHeaders,
rid: responseRid,
} = await this._invoke("plugin:cors-fetch|fetch_send", {
rid,
});

console.log("✅ fetch_read_body", responseRid);
const body = await this._invoke("plugin:cors-fetch|fetch_read_body", {
rid: responseRid,
} = await this._invoke("plugin:cors-fetch|cors_request", {
request: {
requestId,
method: req.method,
url: req.url,
headers: mappedHeaders,
data: reqData,
maxRedirections,
connectTimeout,
proxy,
},
});

const res = new Response(
Expand All @@ -118,8 +97,6 @@ class CORSFetch {
// url is read only but seems like we can do this
Object.defineProperty(res, "url", { value: url });

console.log("✅ res", res);

resolve(res);
});
}
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const COMMANDS: &[&str] = &["fetch", "fetch_cancel", "fetch_send", "fetch_read_body"];
const COMMANDS: &[&str] = &["cors_request", "cancel_cors_request"];

fn main() {
tauri_plugin::Builder::new(COMMANDS)
Expand Down
2 changes: 1 addition & 1 deletion example/src-tauri/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions example/src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@
"cors-fetch:default"
]
},
{
"description": "cors-fetch:allow-cancel-cors-request -> Enables the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:allow-cancel-cors-request"
]
},
{
"description": "cors-fetch:allow-cors-request -> Enables the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:allow-cors-request"
]
},
{
"description": "cors-fetch:allow-fetch -> Enables the fetch command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -282,6 +296,20 @@
"cors-fetch:allow-fetch-send"
]
},
{
"description": "cors-fetch:deny-cancel-cors-request -> Denies the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:deny-cancel-cors-request"
]
},
{
"description": "cors-fetch:deny-cors-request -> Denies the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:deny-cors-request"
]
},
{
"description": "cors-fetch:deny-fetch -> Denies the fetch command without any pre-configured scope.",
"type": "string",
Expand Down
28 changes: 28 additions & 0 deletions example/src-tauri/gen/schemas/macOS-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@
"cors-fetch:default"
]
},
{
"description": "cors-fetch:allow-cancel-cors-request -> Enables the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:allow-cancel-cors-request"
]
},
{
"description": "cors-fetch:allow-cors-request -> Enables the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:allow-cors-request"
]
},
{
"description": "cors-fetch:allow-fetch -> Enables the fetch command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -282,6 +296,20 @@
"cors-fetch:allow-fetch-send"
]
},
{
"description": "cors-fetch:deny-cancel-cors-request -> Denies the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:deny-cancel-cors-request"
]
},
{
"description": "cors-fetch:deny-cors-request -> Denies the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"cors-fetch:deny-cors-request"
]
},
{
"description": "cors-fetch:deny-fetch -> Denies the fetch command without any pre-configured scope.",
"type": "string",
Expand Down
13 changes: 13 additions & 0 deletions permissions/autogenerated/commands/cancel_cors_request.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!

"$schema" = "../../schemas/schema.json"

[[permission]]
identifier = "allow-cancel-cors-request"
description = "Enables the cancel_cors_request command without any pre-configured scope."
commands.allow = ["cancel_cors_request"]

[[permission]]
identifier = "deny-cancel-cors-request"
description = "Denies the cancel_cors_request command without any pre-configured scope."
commands.deny = ["cancel_cors_request"]
13 changes: 13 additions & 0 deletions permissions/autogenerated/commands/cors_request.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!

"$schema" = "../../schemas/schema.json"

[[permission]]
identifier = "allow-cors-request"
description = "Enables the cors_request command without any pre-configured scope."
commands.allow = ["cors_request"]

[[permission]]
identifier = "deny-cors-request"
description = "Denies the cors_request command without any pre-configured scope."
commands.deny = ["cors_request"]
4 changes: 4 additions & 0 deletions permissions/autogenerated/reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
| Permission | Description |
|------|-----|
|`allow-cancel-cors-request`|Enables the cancel_cors_request command without any pre-configured scope.|
|`deny-cancel-cors-request`|Denies the cancel_cors_request command without any pre-configured scope.|
|`allow-cors-request`|Enables the cors_request command without any pre-configured scope.|
|`deny-cors-request`|Denies the cors_request command without any pre-configured scope.|
|`allow-fetch`|Enables the fetch command without any pre-configured scope.|
|`deny-fetch`|Denies the fetch command without any pre-configured scope.|
|`allow-fetch-cancel`|Enables the fetch_cancel command without any pre-configured scope.|
Expand Down
6 changes: 2 additions & 4 deletions permissions/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
[default]
description = "Allows all fetch operations"
permissions = [
"allow-fetch",
"allow-fetch-cancel",
"allow-fetch-read-body",
"allow-fetch-send",
"allow-cors-request",
"allow-cancel-cors-request",
]
28 changes: 28 additions & 0 deletions permissions/schemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@
"PermissionKind": {
"type": "string",
"oneOf": [
{
"description": "allow-cancel-cors-request -> Enables the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-cancel-cors-request"
]
},
{
"description": "deny-cancel-cors-request -> Denies the cancel_cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-cancel-cors-request"
]
},
{
"description": "allow-cors-request -> Enables the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-cors-request"
]
},
{
"description": "deny-cors-request -> Denies the cors_request command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-cors-request"
]
},
{
"description": "allow-fetch -> Enables the fetch command without any pre-configured scope.",
"type": "string",
Expand Down
Loading

0 comments on commit 4e11522

Please sign in to comment.