rust-fetch 0.2.0 → 0.3.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.
@@ -0,0 +1,15 @@
1
+ [package]
2
+ name = "wasm-module"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib", "rlib"]
8
+
9
+ [dependencies]
10
+ wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
11
+ reqwest = { version = "0.11", features = ["json"] }
12
+ serde = { version = "1.0", features = ["derive"] }
13
+ serde_json = "1.0"
14
+ wasm-bindgen-futures = "0.4"
15
+ serde-wasm-bindgen = "0.6"
@@ -0,0 +1,4 @@
1
+ import initWasm from 'wasm-module';
2
+ export declare const initializeWasm: () => Promise<void>;
3
+ export default initWasm;
4
+ export * from 'wasm-module';
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.initializeWasm = void 0;
30
+ const wasm_module_1 = __importDefault(require("wasm-module"));
31
+ const wasmPromise = (0, wasm_module_1.default)();
32
+ const initializeWasm = () => __awaiter(void 0, void 0, void 0, function* () {
33
+ yield wasmPromise;
34
+ });
35
+ exports.initializeWasm = initializeWasm;
36
+ exports.default = wasm_module_1.default;
37
+ __exportStar(require("wasm-module"), exports);
Binary file
@@ -0,0 +1,4 @@
1
+ import { InitOutput } from 'wasm-module';
2
+ export declare function initializeWasm(): Promise<InitOutput>;
3
+ export default initializeWasm;
4
+ export * from 'wasm-module';
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ // import init, { greet, add, fetch_wasm_map, fetch_wasm_json } from './wasm-module/pkg/wasm_module';
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
37
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
38
+ };
39
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
40
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41
+ return new (P || (P = Promise))(function (resolve, reject) {
42
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
43
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
44
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
45
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
46
+ });
47
+ };
48
+ var __importDefault = (this && this.__importDefault) || function (mod) {
49
+ return (mod && mod.__esModule) ? mod : { "default": mod };
50
+ };
51
+ Object.defineProperty(exports, "__esModule", { value: true });
52
+ exports.initializeWasm = initializeWasm;
53
+ const path = __importStar(require("path"));
54
+ const promises_1 = require("fs/promises");
55
+ const wasm_module_1 = __importDefault(require("wasm-module"));
56
+ const wasmPath = path.join(process.cwd(), 'node_modules', '@asemqaffaf/rust-fetch', 'wasm-module', 'pkg', 'wasm_module_bg.wasm');
57
+ let initialized = false;
58
+ let initPromise = null;
59
+ function initializeWasm() {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const wasmBuffer = yield (0, promises_1.readFile)(wasmPath);
62
+ return yield (0, wasm_module_1.default)(wasmBuffer);
63
+ });
64
+ }
65
+ function ensureInitialized() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ if (!initialized) {
68
+ if (!initPromise) {
69
+ initPromise = initializeWasm();
70
+ }
71
+ yield initPromise;
72
+ initialized = true;
73
+ }
74
+ });
75
+ }
76
+ (() => __awaiter(void 0, void 0, void 0, function* () {
77
+ yield ensureInitialized();
78
+ }))();
79
+ exports.default = initializeWasm;
80
+ __exportStar(require("wasm-module"), exports);
@@ -0,0 +1,7 @@
1
+ use wasm_bindgen::prelude::*;
2
+
3
+ #[wasm_bindgen]
4
+ pub fn greet(name: &str) -> String {
5
+ format!("Hello, {}!", name)
6
+ }
7
+
@@ -0,0 +1,63 @@
1
+ use wasm_bindgen::prelude::*;
2
+ use wasm_bindgen::JsValue;
3
+ // use wasm_bindgen_futures::JsFuture;
4
+ use serde_json::Value;
5
+ use reqwest::get;
6
+ use serde_wasm_bindgen::to_value;
7
+
8
+ #[wasm_bindgen]
9
+ pub async fn fetch_wasm_json(url: String) -> Result<JsValue, JsValue> {
10
+ let response = get(url)
11
+ .await
12
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
13
+
14
+ let json: Value = response
15
+ .json()
16
+ .await
17
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
18
+
19
+ // Convert the Value directly to JsValue instead of converting to string
20
+ Ok(JsValue::from_serde(&json).map_err(|e| JsValue::from_str(&e.to_string()))?)
21
+ }
22
+
23
+ #[wasm_bindgen]
24
+ pub async fn fetch_wasm_map(url: String) -> Result<JsValue, JsValue> {
25
+ let response = get(url)
26
+ .await
27
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
28
+
29
+ let json: Value = response
30
+ .json()
31
+ .await
32
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
33
+
34
+ // Use serde_wasm_bindgen::to_value instead of JsValue::from_serde
35
+ Ok(to_value(&json)?)
36
+ }
37
+
38
+ #[wasm_bindgen]
39
+ pub async fn fetch_wasm_html(url: String) -> Result<String, JsValue> {
40
+ let content = get(url)
41
+ .await
42
+ .map_err(|e| JsValue::from_str(&e.to_string()))?
43
+ .text()
44
+ .await
45
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
46
+
47
+ Ok(content)
48
+ }
49
+
50
+ #[wasm_bindgen]
51
+ pub async fn fetch_wasm_api() -> Result<JsValue, JsValue> {
52
+ let response = get("https://jsonplaceholder.typicode.com/todos/1")
53
+ .await
54
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
55
+
56
+ let content = response
57
+ .json()
58
+ .await
59
+ .map_err(|e| JsValue::from_str(&e.to_string()))?;
60
+
61
+ // Ok(content)
62
+ Ok(to_value(&content)?)
63
+ }
@@ -0,0 +1,10 @@
1
+ use wasm_bindgen::prelude::*;
2
+
3
+ pub mod greetings;
4
+ pub mod http;
5
+ pub mod math;
6
+
7
+ // Re-export all public items from modules
8
+ pub use greetings::*;
9
+ pub use http::*;
10
+ pub use math::*;
@@ -0,0 +1,7 @@
1
+ use wasm_bindgen::prelude::*;
2
+
3
+ #[wasm_bindgen]
4
+ pub fn add(a: i32, b: i32) -> i32 {
5
+ a + b
6
+ }
7
+