@cpp.js/package-curl 2.0.0-beta.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @cpp.js/package-curl
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 🚀 first stable release
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ COPYRIGHT AND PERMISSION NOTICE
2
+
3
+ Copyright (c) 1996 - 2025, Daniel Stenberg, <daniel@haxx.se>, and many
4
+ contributors, see the THANKS file.
5
+
6
+ All rights reserved.
7
+
8
+ Permission to use, copy, modify, and distribute this software for any purpose
9
+ with or without fee is hereby granted, provided that the above copyright
10
+ notice and this permission notice appear in all copies.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
15
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
18
+ OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ Except as contained in this notice, the name of a copyright holder shall not
21
+ be used in advertising or otherwise to promote the sale, use or other dealings
22
+ in this Software without prior written authorization of the copyright holder.
package/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # @cpp.js/package-curl
2
+ **Precompiled CURL library built with cpp.js for seamless integration in JavaScript, WebAssembly and React Native projects.**
3
+
4
+ <a href="https://www.npmjs.com/package/@cpp.js/package-curl">
5
+ <img alt="NPM version" src="https://img.shields.io/npm/v/@cpp.js/package-curl?style=for-the-badge" />
6
+ </a>
7
+ <a href="https://github.com/curl/curl">
8
+ <img src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Funpkg.com%2F%40cpp.js%2Fpackage-curl%2Fpackage.json&query=%24.nativeVersion&style=for-the-badge&label=curl" />
9
+ </a>
10
+ <a href="https://github.com/curl/curl/blob/master/COPYING">
11
+ <img alt="License" src="https://img.shields.io/npm/l/%40cpp.js%2Fpackage-curl?style=for-the-badge" />
12
+ </a>
13
+
14
+ ## Integration
15
+ Start by installing these package with the following command:
16
+
17
+ ```sh
18
+ npm install @cpp.js/package-curl
19
+ ```
20
+
21
+ To enable the library, modify the cppjs.config.js file as shown below.
22
+ ```diff
23
+ +import curl from '@cpp.js/package-curl/cppjs.config.js';
24
+
25
+ export default {
26
+ dependencies: [
27
+ + curl
28
+ ]
29
+ paths: {
30
+ config: import.meta.url,
31
+ }
32
+ };
33
+ ```
34
+
35
+ ## Usage
36
+ Below are the steps to use the curl in your C++ or JavaScript code.
37
+
38
+ ### Usage in C++ Code
39
+ ```diff
40
+ +#include <curl/curl.h>
41
+
42
+ +size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
43
+ + size_t totalSize = size * nmemb;
44
+ + output->append((char*)contents, totalSize);
45
+ + return totalSize;
46
+ +}
47
+
48
+ std::string Native::sample() {
49
+ + std::string result = "";
50
+ + std::string response;
51
+ + char errbuf[CURL_ERROR_SIZE*100];
52
+ +
53
+ + CURL* curl = curl_easy_init(); // Initialize libcurl
54
+ + curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("CURL_CA_BUNDLE"));
55
+ + curl_easy_setopt(curl, CURLOPT_URL, "https://test22.free.beeceptor.com"); // Set the URL
56
+ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"a\": 2}");
57
+ + struct curl_slist *headers = NULL;
58
+ + headers = curl_slist_append(headers, "Content-Type: application/json");
59
+ + headers = curl_slist_append(headers, "Accept: application/json");
60
+ +
61
+ + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
62
+ + errbuf[0] = 0;
63
+ + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Set callback to handle data
64
+ + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // Pass the response string
65
+ + CURLcode res = curl_easy_perform(curl);
66
+ + if (res == CURLE_OK) {
67
+ + result = "response: " + response;
68
+ + } else {
69
+ + std::string errorStr = errbuf[0] ? std::string(errbuf) : curl_easy_strerror(res);
70
+ + result = "curl error: " + errorStr;
71
+ + }
72
+ +
73
+ + curl_slist_free_all(headers);
74
+ + curl_easy_cleanup(curl);
75
+ +
76
+ + return result;
77
+ }
78
+ ```
79
+
80
+ ## License
81
+ This project includes the precompiled CURL library, which is distributed under the [CURL License](https://github.com/curl/curl/blob/master/COPYING).
82
+
83
+ CURL Homepage: [https://curl.se/](https://curl.se/)
@@ -0,0 +1,13 @@
1
+ require "json"
2
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
3
+
4
+ Pod::Spec.new do |s|
5
+ s.module_name = "curl"
6
+ s.name = "cppjs-package-curl"
7
+ s.version = package["nativeVersion"]
8
+ s.summary = "Transferring data with URLs"
9
+ s.homepage = "https://curl.se/"
10
+ s.author = "CURL Authors"
11
+ s.source = { :http => "https://cpp.js.org" }
12
+ s.vendored_frameworks = 'curl.xcframework'
13
+ end
package/cppjs.build.js ADDED
@@ -0,0 +1,182 @@
1
+ const easyPerformAbove = `
2
+ #ifdef __EMSCRIPTEN__
3
+ static void on_success(emscripten_fetch_t *fetch) {
4
+ void **userData = (void **)fetch->userData;
5
+ volatile emscripten_fetch_t **fetch_result_ptr = (volatile emscripten_fetch_t **)userData[0];
6
+ volatile int *fetch_completed_ptr = (volatile int *)userData[1];
7
+ *fetch_result_ptr = fetch;
8
+ *fetch_completed_ptr = 1;
9
+ }
10
+
11
+ static void on_error(emscripten_fetch_t *fetch) {
12
+ void **userData = (void **)fetch->userData;
13
+ volatile emscripten_fetch_t **fetch_result_ptr = (volatile emscripten_fetch_t **)userData[0];
14
+ volatile int *fetch_completed_ptr = (volatile int *)userData[1];
15
+ *fetch_result_ptr = fetch;
16
+ *fetch_completed_ptr = 1;
17
+ }
18
+ #endif
19
+ `;
20
+
21
+ const easyPerformInside = `
22
+ #ifdef __EMSCRIPTEN__
23
+ char method[10] = "GET";
24
+ switch (data->set.method) {
25
+ case HTTPREQ_GET:
26
+ strcpy(method, "GET");
27
+ break;
28
+ case HTTPREQ_POST:
29
+ case HTTPREQ_POST_FORM:
30
+ case HTTPREQ_POST_MIME:
31
+ strcpy(method, "POST");
32
+ break;
33
+ case HTTPREQ_PUT:
34
+ strcpy(method, "PUT");
35
+ break;
36
+ case HTTPREQ_HEAD:
37
+ strcpy(method, "HEAD");
38
+ break;
39
+ }
40
+ if (data->set.str[28]) {
41
+ strcpy(method, data->set.str[28]);
42
+ }
43
+ emscripten_fetch_attr_t attr;
44
+ emscripten_fetch_attr_init(&attr);
45
+ strcpy(attr.requestMethod, method);
46
+ if (data->set.postfields) {
47
+ attr.requestData = data->set.postfields;
48
+ attr.requestDataSize = strlen(data->set.postfields);
49
+ }
50
+ if (data->set.headers) {
51
+ int header_count = 0;
52
+ struct curl_slist* temp = data->set.headers;
53
+ while (temp) {
54
+ header_count++;
55
+ temp = temp->next;
56
+ }
57
+
58
+ const char** result = (const char**)malloc(sizeof(char*) * (header_count * 2 + 1));
59
+
60
+ temp = data->set.headers;
61
+ int i = 0;
62
+ while (temp) {
63
+ char* header_line = strdup(temp->data);
64
+ char* colon = strchr(header_line, ':');
65
+
66
+ if (colon) {
67
+ *colon = '\0';
68
+ char* value = colon + 1;
69
+
70
+ while (*value == ' ') value++;
71
+
72
+ result[i++] = strdup(header_line);
73
+ result[i++] = strdup(value);
74
+ } else {}
75
+
76
+ free(header_line);
77
+ temp = temp->next;
78
+ }
79
+ result[i] = NULL;
80
+
81
+ attr.requestHeaders = result;
82
+ }
83
+ attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
84
+
85
+ volatile int fetch_completed = 0;
86
+ volatile emscripten_fetch_t* fetch = NULL;
87
+ void* userData[2] = { (void*)&fetch, (void*)&fetch_completed };
88
+
89
+ attr.onsuccess = on_success;
90
+ attr.onerror = on_error;
91
+ attr.userData = userData;
92
+
93
+ emscripten_fetch(&attr, data->state.url);
94
+
95
+ while (!fetch_completed) {
96
+ emscripten_sleep(100);
97
+ }
98
+
99
+ data->info.httpcode = fetch->status;
100
+ if (fetch->status == 200) {
101
+ if (data->set.out) {
102
+ data->set.fwrite_func(fetch->data, 1, fetch->numBytes, data->set.out);
103
+ }
104
+ } else if (fetch->status >= 400) {
105
+ if (data->set.http_fail_on_error && data->set.errorbuffer) {
106
+ data->set.fwrite_func(fetch->data, 1, fetch->numBytes, data->set.errorbuffer);
107
+ } else if (data->set.out) {
108
+ data->set.fwrite_func(fetch->data, 1, fetch->numBytes, data->set.out);
109
+ }
110
+ } else {
111
+ if (data->set.out) {
112
+ data->set.fwrite_func(fetch->data, 1, fetch->numBytes, data->set.out);
113
+ }
114
+ }
115
+
116
+ if (attr.requestHeaders) {
117
+ for (int i = 0; attr.requestHeaders[i] != NULL; i++) {
118
+ free((void*)attr.requestHeaders[i]);
119
+ }
120
+
121
+ free(attr.requestHeaders);
122
+ }
123
+
124
+ emscripten_fetch_close(fetch);
125
+
126
+ if (data->set.http_fail_on_error && fetch->status >= 400) {
127
+ return CURLE_HTTP_RETURNED_ERROR;
128
+ }
129
+
130
+ return CURLE_OK;
131
+ #endif
132
+ `;
133
+
134
+ const fetchImportString = `
135
+ #ifdef __EMSCRIPTEN__
136
+ #include <emscripten.h>
137
+ #include <emscripten/fetch.h>
138
+ #endif
139
+ `;
140
+
141
+ const platformBuild = {
142
+ 'Emscripten-x86_64': ['-DBUILD_SHARED_LIBS=OFF', '-DBUILD_STATIC_LIBS=ON'],
143
+ 'Android-arm64-v8a': ['-DBUILD_SHARED_LIBS=ON', '-DBUILD_STATIC_LIBS=OFF'],
144
+ 'Android-x86_64': ['-DBUILD_SHARED_LIBS=ON', '-DBUILD_STATIC_LIBS=OFF'],
145
+ 'iOS-iphoneos': ['-DBUILD_SHARED_LIBS=OFF', '-DBUILD_STATIC_LIBS=ON'],
146
+ 'iOS-iphonesimulator': ['-DBUILD_SHARED_LIBS=OFF', '-DBUILD_STATIC_LIBS=ON'],
147
+ };
148
+
149
+ export default {
150
+ getURL: (version) => `https://curl.se/download/curl-${version}.tar.gz`,
151
+ replaceList: [
152
+ {
153
+ regex: 'static CURLcode easy_perform\\(struct Curl_easy \\*data, bool events\\)',
154
+ replacement: `${easyPerformAbove}\nstatic CURLcode easy_perform(struct Curl_easy *data, bool events)`,
155
+ paths: ['lib/easy.c'],
156
+ },
157
+ {
158
+ regex: ' struct Curl\\_multi \\*multi\\;',
159
+ replacement: `${easyPerformInside}\n struct Curl_multi *multi;`,
160
+ paths: ['lib/easy.c'],
161
+ },
162
+ {
163
+ regex: '#include "urldata.h"',
164
+ replacement: `${fetchImportString}\n#include "urldata.h"`,
165
+ paths: ['lib/easy.c'],
166
+ },
167
+ ],
168
+ buildType: 'cmake',
169
+ getBuildParams: (platform, depPaths) => [
170
+ ...(platformBuild[platform] || []),
171
+ `-DOPENSSL_INCLUDE_DIR=${depPaths.ssl.header}`,
172
+ `-DOPENSSL_SSL_LIBRARY=${depPaths.ssl.lib}`,
173
+ `-DOPENSSL_CRYPTO_LIBRARY=${depPaths.crypto.lib}`,
174
+ // `-DOPENSSL_CMAKE_PATH=${depPaths.cmake.openssl}`,
175
+ '-DBUILD_EXAMPLES=OFF', '-DBUILD_CURL_EXE=OFF', '-DBUILD_LIBCURL_DOCS=OFF',
176
+ '-DBUILD_TESTING=OFF',
177
+ '-DENABLE_CURL_MANUAL=OFF', // '-DCURL_DISABLE_THREADED_RESOLVER=ON','-DCURL_DISABLE_THREAD=ON',
178
+ '-DENABLE_NETRC=OFF', '-DCURL_USE_LIBPSL=OFF', '-DENABLE_IPV6=OFF', '-DENABLE_NTLMWB=OFF',
179
+ // '-DCURL_DISABLE_UNIX_SOCKETS=ON',
180
+ // '-DCURL_ENABLE_EXPORT_TARGET=OFF'
181
+ ],
182
+ };
@@ -0,0 +1,14 @@
1
+ import curlWasm from '@cpp.js/package-curl-wasm/cppjs.config.js';
2
+ import curlAndroid from '@cpp.js/package-curl-android/cppjs.config.js';
3
+ import curlIos from '@cpp.js/package-curl-ios/cppjs.config.js';
4
+
5
+ export default {
6
+ dependencies: [
7
+ curlWasm,
8
+ curlAndroid,
9
+ curlIos,
10
+ ],
11
+ paths: {
12
+ config: import.meta.url
13
+ }
14
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@cpp.js/package-curl",
3
+ "version": "2.0.0-beta.1",
4
+ "nativeVersion": "8.17.0",
5
+ "description": "This package provides a CURL library compiled with Cpp.js, enabling seamless usage of CURL functionalities in JavaScript, WebAssembly and React Native projects. Client-side URL transfers for web and mobile applications.",
6
+ "homepage": "https://github.com/bugra9/cpp.js/tree/main/packages/cppjs-package-curl#readme",
7
+ "repository": "https://github.com/bugra9/cpp.js.git",
8
+ "type": "module",
9
+ "license": "curl",
10
+ "keywords": [
11
+ "curl",
12
+ "libcurl",
13
+ "cpp.js-package",
14
+ "webassembly",
15
+ "react-native"
16
+ ],
17
+ "devDependencies": {
18
+ "@cpp.js/package-curl-wasm": "^2.0.0-beta.1",
19
+ "@cpp.js/package-curl-android": "^2.0.0-beta.1",
20
+ "@cpp.js/package-curl-ios": "^2.0.0-beta.1",
21
+ "cpp.js": "^2.0.0-beta.1"
22
+ }
23
+ }