@cappitolian/http-local-server 0.0.2 → 0.0.4

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.
@@ -11,8 +11,7 @@ Pod::Spec.new do |s|
11
11
  s.author = package['author']
12
12
  s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
13
  s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
14
- s.ios.deployment_target = '14.0'
14
+ s.ios.deployment_target = '15.0'
15
15
  s.dependency 'Capacitor'
16
- s.dependency 'GCDWebServer', '~> 3.5'
17
16
  s.swift_version = '5.1'
18
17
  end
package/Package.swift CHANGED
@@ -3,14 +3,14 @@ import PackageDescription
3
3
 
4
4
  let package = Package(
5
5
  name: "CappitolianHttpLocalServer",
6
- platforms: [.iOS(.v14)],
6
+ platforms: [.iOS(.v15)],
7
7
  products: [
8
8
  .library(
9
9
  name: "CappitolianHttpLocalServer",
10
10
  targets: ["HttpLocalServerPlugin"])
11
11
  ],
12
12
  dependencies: [
13
- .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0")
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
14
14
  ],
15
15
  targets: [
16
16
  .target(
package/README.md CHANGED
@@ -13,91 +13,25 @@ npx cap sync
13
13
 
14
14
  <docgen-index>
15
15
 
16
- * [`connect()`](#connect)
17
- * [`disconnect()`](#disconnect)
18
- * [`sendResponse(...)`](#sendresponse)
19
- * [`addListener('onRequest', ...)`](#addlisteneronrequest-)
20
- * [Interfaces](#interfaces)
16
+ * [`echo(...)`](#echo)
21
17
 
22
18
  </docgen-index>
23
19
 
24
20
  <docgen-api>
25
21
  <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
26
22
 
27
- ### connect()
23
+ ### echo(...)
28
24
 
29
25
  ```typescript
30
- connect() => Promise<HttpConnectResult>
26
+ echo(options: { value: string; }) => Promise<{ value: string; }>
31
27
  ```
32
28
 
33
- **Returns:** <code>Promise&lt;<a href="#httpconnectresult">HttpConnectResult</a>&gt;</code>
34
-
35
- --------------------
36
-
37
-
38
- ### disconnect()
39
-
40
- ```typescript
41
- disconnect() => Promise<void>
42
- ```
43
-
44
- --------------------
45
-
46
-
47
- ### sendResponse(...)
48
-
49
- ```typescript
50
- sendResponse(options: { requestId: string; body: string; }) => Promise<void>
51
- ```
52
-
53
- | Param | Type |
54
- | ------------- | ------------------------------------------------- |
55
- | **`options`** | <code>{ requestId: string; body: string; }</code> |
56
-
57
- --------------------
29
+ | Param | Type |
30
+ | ------------- | ------------------------------- |
31
+ | **`options`** | <code>{ value: string; }</code> |
58
32
 
59
-
60
- ### addListener('onRequest', ...)
61
-
62
- ```typescript
63
- addListener(eventName: 'onRequest', listenerFunc: (info: HttpRequestInfo) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
64
- ```
65
-
66
- | Param | Type |
67
- | ------------------ | ------------------------------------------------------------------------------ |
68
- | **`eventName`** | <code>'onRequest'</code> |
69
- | **`listenerFunc`** | <code>(info: <a href="#httprequestinfo">HttpRequestInfo</a>) =&gt; void</code> |
70
-
71
- **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt; & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>
33
+ **Returns:** <code>Promise&lt;{ value: string; }&gt;</code>
72
34
 
73
35
  --------------------
74
36
 
75
-
76
- ### Interfaces
77
-
78
-
79
- #### HttpConnectResult
80
-
81
- | Prop | Type |
82
- | ---------- | ------------------- |
83
- | **`ip`** | <code>string</code> |
84
- | **`port`** | <code>number</code> |
85
-
86
-
87
- #### PluginListenerHandle
88
-
89
- | Prop | Type |
90
- | ------------ | ----------------------------------------- |
91
- | **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
92
-
93
-
94
- #### HttpRequestInfo
95
-
96
- | Prop | Type |
97
- | --------------- | ------------------- |
98
- | **`requestId`** | <code>string</code> |
99
- | **`method`** | <code>string</code> |
100
- | **`path`** | <code>string</code> |
101
- | **`body`** | <code>string</code> |
102
-
103
37
  </docgen-api>
@@ -35,6 +35,7 @@ import java.io.IOException;
35
35
  import java.util.UUID;
36
36
  import java.util.concurrent.*;
37
37
  import java.util.HashMap;
38
+ import java.util.Map;
38
39
 
39
40
  public class HttpLocalServer {
40
41
  private LocalNanoServer server;
@@ -109,9 +110,26 @@ public class HttpLocalServer {
109
110
  String body = "";
110
111
  if (session.getMethod() == Method.POST || session.getMethod() == Method.PUT) {
111
112
  try {
112
- session.parseBody(new HashMap<>());
113
- body = session.getQueryParameterString();
114
- } catch (Exception e) { }
113
+ // Crear un mapa para almacenar los datos parseados
114
+ HashMap<String, String> files = new HashMap<>();
115
+ session.parseBody(files);
116
+
117
+ // El body viene en el mapa con la clave "postData"
118
+ body = files.get("postData");
119
+
120
+ // Si postData es null, intentar obtener de los parámetros (para form-data)
121
+ if (body == null || body.isEmpty()) {
122
+ // Para application/x-www-form-urlencoded
123
+ body = session.getQueryParameterString();
124
+ }
125
+
126
+ // Log para debug
127
+ System.out.println("Body received: " + body);
128
+
129
+ } catch (Exception e) {
130
+ System.err.println("Error parsing body: " + e.getMessage());
131
+ e.printStackTrace();
132
+ }
115
133
  }
116
134
 
117
135
  // Generate a unique requestId
@@ -123,6 +141,7 @@ public class HttpLocalServer {
123
141
  req.put("method", method);
124
142
  req.put("path", path);
125
143
  req.put("body", body);
144
+ req.put("headers", getHeadersAsJson(session)); // Opcional: para debug
126
145
 
127
146
  // Future to wait for JS response
128
147
  CompletableFuture<String> future = new CompletableFuture<>();
@@ -150,6 +169,19 @@ public class HttpLocalServer {
150
169
  return response;
151
170
  }
152
171
 
172
+ // Método auxiliar para obtener headers como JSObject
173
+ private JSObject getHeadersAsJson(IHTTPSession session) {
174
+ JSObject headers = new JSObject();
175
+ try {
176
+ for (Map.Entry<String, String> entry : session.getHeaders().entrySet()) {
177
+ headers.put(entry.getKey(), entry.getValue());
178
+ }
179
+ } catch (Exception e) {
180
+ // ignore
181
+ }
182
+ return headers;
183
+ }
184
+
153
185
  private void addCorsHeaders(Response response) {
154
186
  response.addHeader("Access-Control-Allow-Origin", "*");
155
187
  response.addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
package/dist/docs.json CHANGED
@@ -6,148 +6,25 @@
6
6
  "tags": [],
7
7
  "methods": [
8
8
  {
9
- "name": "connect",
10
- "signature": "() => Promise<HttpConnectResult>",
11
- "parameters": [],
12
- "returns": "Promise<HttpConnectResult>",
13
- "tags": [],
14
- "docs": "",
15
- "complexTypes": [
16
- "HttpConnectResult"
17
- ],
18
- "slug": "connect"
19
- },
20
- {
21
- "name": "disconnect",
22
- "signature": "() => Promise<void>",
23
- "parameters": [],
24
- "returns": "Promise<void>",
25
- "tags": [],
26
- "docs": "",
27
- "complexTypes": [],
28
- "slug": "disconnect"
29
- },
30
- {
31
- "name": "sendResponse",
32
- "signature": "(options: { requestId: string; body: string; }) => Promise<void>",
9
+ "name": "echo",
10
+ "signature": "(options: { value: string; }) => Promise<{ value: string; }>",
33
11
  "parameters": [
34
12
  {
35
13
  "name": "options",
36
14
  "docs": "",
37
- "type": "{ requestId: string; body: string; }"
15
+ "type": "{ value: string; }"
38
16
  }
39
17
  ],
40
- "returns": "Promise<void>",
18
+ "returns": "Promise<{ value: string; }>",
41
19
  "tags": [],
42
20
  "docs": "",
43
21
  "complexTypes": [],
44
- "slug": "sendresponse"
45
- },
46
- {
47
- "name": "addListener",
48
- "signature": "(eventName: 'onRequest', listenerFunc: (info: HttpRequestInfo) => void) => Promise<PluginListenerHandle> & PluginListenerHandle",
49
- "parameters": [
50
- {
51
- "name": "eventName",
52
- "docs": "",
53
- "type": "'onRequest'"
54
- },
55
- {
56
- "name": "listenerFunc",
57
- "docs": "",
58
- "type": "(info: HttpRequestInfo) => void"
59
- }
60
- ],
61
- "returns": "Promise<PluginListenerHandle> & PluginListenerHandle",
62
- "tags": [],
63
- "docs": "",
64
- "complexTypes": [
65
- "PluginListenerHandle",
66
- "HttpRequestInfo"
67
- ],
68
- "slug": "addlisteneronrequest-"
22
+ "slug": "echo"
69
23
  }
70
24
  ],
71
25
  "properties": []
72
26
  },
73
- "interfaces": [
74
- {
75
- "name": "HttpConnectResult",
76
- "slug": "httpconnectresult",
77
- "docs": "",
78
- "tags": [],
79
- "methods": [],
80
- "properties": [
81
- {
82
- "name": "ip",
83
- "tags": [],
84
- "docs": "",
85
- "complexTypes": [],
86
- "type": "string"
87
- },
88
- {
89
- "name": "port",
90
- "tags": [],
91
- "docs": "",
92
- "complexTypes": [],
93
- "type": "number"
94
- }
95
- ]
96
- },
97
- {
98
- "name": "PluginListenerHandle",
99
- "slug": "pluginlistenerhandle",
100
- "docs": "",
101
- "tags": [],
102
- "methods": [],
103
- "properties": [
104
- {
105
- "name": "remove",
106
- "tags": [],
107
- "docs": "",
108
- "complexTypes": [],
109
- "type": "() => Promise<void>"
110
- }
111
- ]
112
- },
113
- {
114
- "name": "HttpRequestInfo",
115
- "slug": "httprequestinfo",
116
- "docs": "",
117
- "tags": [],
118
- "methods": [],
119
- "properties": [
120
- {
121
- "name": "requestId",
122
- "tags": [],
123
- "docs": "",
124
- "complexTypes": [],
125
- "type": "string"
126
- },
127
- {
128
- "name": "method",
129
- "tags": [],
130
- "docs": "",
131
- "complexTypes": [],
132
- "type": "string"
133
- },
134
- {
135
- "name": "path",
136
- "tags": [],
137
- "docs": "",
138
- "complexTypes": [],
139
- "type": "string"
140
- },
141
- {
142
- "name": "body",
143
- "tags": [],
144
- "docs": "",
145
- "complexTypes": [],
146
- "type": "string | undefined"
147
- }
148
- ]
149
- }
150
- ],
27
+ "interfaces": [],
151
28
  "enums": [],
152
29
  "typeAliases": [],
153
30
  "pluginConfigs": []
@@ -1,22 +1,7 @@
1
- export interface HttpConnectResult {
2
- ip: string;
3
- port: number;
4
- }
5
- export interface HttpRequestInfo {
6
- requestId: string;
7
- method: string;
8
- path: string;
9
- body?: string;
10
- }
11
1
  export interface HttpLocalServerPlugin {
12
- connect(): Promise<HttpConnectResult>;
13
- disconnect(): Promise<void>;
14
- sendResponse(options: {
15
- requestId: string;
16
- body: string;
17
- }): Promise<void>;
18
- addListener(eventName: 'onRequest', listenerFunc: (info: HttpRequestInfo) => void): Promise<PluginListenerHandle> & PluginListenerHandle;
19
- }
20
- export interface PluginListenerHandle {
21
- remove: () => Promise<void>;
2
+ echo(options: {
3
+ value: string;
4
+ }): Promise<{
5
+ value: string;
6
+ }>;
22
7
  }
@@ -1,5 +1,2 @@
1
- // export interface HttpLocalServerPlugin {
2
- // echo(options: { value: string }): Promise<{ value: string }>;
3
- // }
4
1
  export {};
5
2
  //# sourceMappingURL=definitions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,kEAAkE;AAClE,IAAI","sourcesContent":["// export interface HttpLocalServerPlugin {\n// echo(options: { value: string }): Promise<{ value: string }>;\n// }\n\nexport interface HttpConnectResult {\n ip: string;\n port: number;\n}\n\nexport interface HttpRequestInfo {\n requestId: string;\n method: string;\n path: string;\n body?: string;\n}\n\nexport interface HttpLocalServerPlugin {\n connect(): Promise<HttpConnectResult>;\n disconnect(): Promise<void>;\n sendResponse(options: { requestId: string; body: string }): Promise<void>;\n addListener(\n eventName: 'onRequest',\n listenerFunc: (info: HttpRequestInfo) => void,\n ): Promise<PluginListenerHandle> & PluginListenerHandle;\n}\n\nexport interface PluginListenerHandle {\n remove: () => Promise<void>;\n}"]}
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["export interface HttpLocalServerPlugin {\n echo(options: { value: string }): Promise<{ value: string }>;\n}\n"]}
@@ -1,4 +1,4 @@
1
1
  import type { HttpLocalServerPlugin } from './definitions';
2
2
  declare const HttpLocalServer: HttpLocalServerPlugin;
3
3
  export * from './definitions';
4
- export default HttpLocalServer;
4
+ export { HttpLocalServer };
package/dist/esm/index.js CHANGED
@@ -1,12 +1,7 @@
1
- // import { registerPlugin } from '@capacitor/core';
2
- // import type { HttpLocalServerPlugin } from './definitions';
3
- // const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {
4
- // web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),
5
- // });
6
- // export * from './definitions';
7
- // export { HttpLocalServer };
8
1
  import { registerPlugin } from '@capacitor/core';
9
- const HttpLocalServer = registerPlugin('HttpLocalServer');
2
+ const HttpLocalServer = registerPlugin('HttpLocalServer', {
3
+ web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),
4
+ });
10
5
  export * from './definitions';
11
- export default HttpLocalServer;
6
+ export { HttpLocalServer };
12
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,oDAAoD;AAEpD,8DAA8D;AAE9D,qFAAqF;AACrF,wEAAwE;AACxE,MAAM;AAEN,iCAAiC;AACjC,8BAA8B;AAE9B,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,MAAM,eAAe,GAAG,cAAc,CAAwB,iBAAiB,CAAC,CAAC;AAEjF,cAAc,eAAe,CAAC;AAC9B,eAAe,eAAe,CAAC","sourcesContent":["// import { registerPlugin } from '@capacitor/core';\n\n// import type { HttpLocalServerPlugin } from './definitions';\n\n// const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {\n// web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n// });\n\n// export * from './definitions';\n// export { HttpLocalServer };\n\nimport { registerPlugin } from '@capacitor/core';\nimport type { HttpLocalServerPlugin } from './definitions';\n\nconst HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer');\n\nexport * from './definitions';\nexport default HttpLocalServer;"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,eAAe,GAAG,cAAc,CAAwB,iBAAiB,EAAE;IAC/E,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,CAAC;CACnE,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { HttpLocalServerPlugin } from './definitions';\n\nconst HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {\n web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n});\n\nexport * from './definitions';\nexport { HttpLocalServer };\n"]}
@@ -0,0 +1,9 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { HttpLocalServerPlugin } from './definitions';
3
+ export declare class HttpLocalServerWeb extends WebPlugin implements HttpLocalServerPlugin {
4
+ echo(options: {
5
+ value: string;
6
+ }): Promise<{
7
+ value: string;
8
+ }>;
9
+ }
@@ -0,0 +1,8 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export class HttpLocalServerWeb extends WebPlugin {
3
+ async echo(options) {
4
+ console.log('ECHO', options);
5
+ return options;
6
+ }
7
+ }
8
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAC/C,KAAK,CAAC,IAAI,CAAC,OAA0B;QACnC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC7B,OAAO,OAAO,CAAC;IACjB,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { HttpLocalServerPlugin } from './definitions';\n\nexport class HttpLocalServerWeb extends WebPlugin implements HttpLocalServerPlugin {\n async echo(options: { value: string }): Promise<{ value: string }> {\n console.log('ECHO', options);\n return options;\n }\n}\n"]}
@@ -2,14 +2,21 @@
2
2
 
3
3
  var core = require('@capacitor/core');
4
4
 
5
- // import { registerPlugin } from '@capacitor/core';
6
- // import type { HttpLocalServerPlugin } from './definitions';
7
- // const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {
8
- // web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),
9
- // });
10
- // export * from './definitions';
11
- // export { HttpLocalServer };
12
- const HttpLocalServer = core.registerPlugin('HttpLocalServer');
13
-
14
- module.exports = HttpLocalServer;
5
+ const HttpLocalServer = core.registerPlugin('HttpLocalServer', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.HttpLocalServerWeb()),
7
+ });
8
+
9
+ class HttpLocalServerWeb extends core.WebPlugin {
10
+ async echo(options) {
11
+ console.log('ECHO', options);
12
+ return options;
13
+ }
14
+ }
15
+
16
+ var web = /*#__PURE__*/Object.freeze({
17
+ __proto__: null,
18
+ HttpLocalServerWeb: HttpLocalServerWeb
19
+ });
20
+
21
+ exports.HttpLocalServer = HttpLocalServer;
15
22
  //# sourceMappingURL=plugin.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js"],"sourcesContent":["// import { registerPlugin } from '@capacitor/core';\n// import type { HttpLocalServerPlugin } from './definitions';\n// const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {\n// web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n// });\n// export * from './definitions';\n// export { HttpLocalServer };\nimport { registerPlugin } from '@capacitor/core';\nconst HttpLocalServer = registerPlugin('HttpLocalServer');\nexport * from './definitions';\nexport default HttpLocalServer;\n//# sourceMappingURL=index.js.map"],"names":["registerPlugin"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AAEK,MAAC,eAAe,GAAGA,mBAAc,CAAC,iBAAiB;;;;"}
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst HttpLocalServer = registerPlugin('HttpLocalServer', {\n web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n});\nexport * from './definitions';\nexport { HttpLocalServer };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class HttpLocalServerWeb extends WebPlugin {\n async echo(options) {\n console.log('ECHO', options);\n return options;\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,eAAe,GAAGA,mBAAc,CAAC,iBAAiB,EAAE;AAC1D,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,kBAAkB,EAAE,CAAC;AACtE,CAAC;;ACFM,MAAM,kBAAkB,SAASC,cAAS,CAAC;AAClD,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE;AACxB,QAAQ,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC;AACpC,QAAQ,OAAO,OAAO;AACtB,IAAI;AACJ;;;;;;;;;"}
package/dist/plugin.js CHANGED
@@ -1,16 +1,25 @@
1
- var capacitorHttpLocalServer = (function (core) {
2
- 'use strict';
1
+ var capacitorHttpLocalServer = (function (exports, core) {
2
+ 'use strict';
3
3
 
4
- // import { registerPlugin } from '@capacitor/core';
5
- // import type { HttpLocalServerPlugin } from './definitions';
6
- // const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {
7
- // web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),
8
- // });
9
- // export * from './definitions';
10
- // export { HttpLocalServer };
11
- const HttpLocalServer = core.registerPlugin('HttpLocalServer');
4
+ const HttpLocalServer = core.registerPlugin('HttpLocalServer', {
5
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.HttpLocalServerWeb()),
6
+ });
12
7
 
13
- return HttpLocalServer;
8
+ class HttpLocalServerWeb extends core.WebPlugin {
9
+ async echo(options) {
10
+ console.log('ECHO', options);
11
+ return options;
12
+ }
13
+ }
14
14
 
15
- })(capacitorExports);
15
+ var web = /*#__PURE__*/Object.freeze({
16
+ __proto__: null,
17
+ HttpLocalServerWeb: HttpLocalServerWeb
18
+ });
19
+
20
+ exports.HttpLocalServer = HttpLocalServer;
21
+
22
+ return exports;
23
+
24
+ })({}, capacitorExports);
16
25
  //# sourceMappingURL=plugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["esm/index.js"],"sourcesContent":["// import { registerPlugin } from '@capacitor/core';\n// import type { HttpLocalServerPlugin } from './definitions';\n// const HttpLocalServer = registerPlugin<HttpLocalServerPlugin>('HttpLocalServer', {\n// web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n// });\n// export * from './definitions';\n// export { HttpLocalServer };\nimport { registerPlugin } from '@capacitor/core';\nconst HttpLocalServer = registerPlugin('HttpLocalServer');\nexport * from './definitions';\nexport default HttpLocalServer;\n//# sourceMappingURL=index.js.map"],"names":["registerPlugin"],"mappings":";;;CAAA;CACA;CACA;CACA;CACA;CACA;CACA;AAEK,OAAC,eAAe,GAAGA,mBAAc,CAAC,iBAAiB;;;;;;;;"}
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst HttpLocalServer = registerPlugin('HttpLocalServer', {\n web: () => import('./web').then((m) => new m.HttpLocalServerWeb()),\n});\nexport * from './definitions';\nexport { HttpLocalServer };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class HttpLocalServerWeb extends WebPlugin {\n async echo(options) {\n console.log('ECHO', options);\n return options;\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,eAAe,GAAGA,mBAAc,CAAC,iBAAiB,EAAE;IAC1D,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,kBAAkB,EAAE,CAAC;IACtE,CAAC;;ICFM,MAAM,kBAAkB,SAASC,cAAS,CAAC;IAClD,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE;IACxB,QAAQ,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC;IACpC,QAAQ,OAAO,OAAO;IACtB,IAAI;IACJ;;;;;;;;;;;;;;;"}
@@ -29,68 +29,33 @@ public protocol HttpLocalServerDelegate: AnyObject {
29
29
  weak var delegate: HttpLocalServerDelegate?
30
30
  static var pendingResponses = [String: (String) -> Void]()
31
31
  static let queue = DispatchQueue(label: "HttpLocalServer.pendingResponses")
32
-
32
+
33
33
  public init(delegate: HttpLocalServerDelegate) {
34
34
  self.delegate = delegate
35
35
  }
36
-
36
+
37
37
  @objc public func connect(_ call: CAPPluginCall) {
38
38
  DispatchQueue.main.async { [weak self] in
39
39
  guard let self = self else { return }
40
-
41
40
  self.webServer = GCDWebServer()
42
-
43
- self.webServer?.addHandler(
44
- match: { method, url, headers, path, query in
45
- GCDWebServerRequest(method: method, url: url, headers: headers, path: path, query: query)
46
- },
47
- processBlock: { request in
48
- let method = request.method
49
- let path = request.url.path
50
- var body: String? = nil
51
-
52
- if let dataRequest = request as? GCDWebServerDataRequest, let text = String(data: dataRequest.data, encoding: .utf8) {
53
- body = text
54
- }
55
-
56
- let requestId = UUID().uuidString
57
- var responseString: String? = nil
58
-
59
- // Set up a semaphore so we can block until JS responds or timeout (3s)
60
- let semaphore = DispatchSemaphore(value: 0)
61
- Self.queue.async {
62
- Self.pendingResponses[requestId] = { responseBody in
63
- responseString = responseBody
64
- semaphore.signal()
65
- }
66
- }
67
-
68
- // Notify delegate (plugin) with the request info
69
- let req: [String: Any?] = [
70
- "requestId": requestId,
71
- "method": method,
72
- "path": path,
73
- "body": body
74
- ]
75
- self.delegate?.httpLocalServerDidReceiveRequest(req.compactMapValues { $0 })
76
-
77
- // Wait for JS response or timeout
78
- _ = semaphore.wait(timeout: .now() + 3.0)
79
- Self.queue.async {
80
- Self.pendingResponses.removeValue(forKey: requestId)
81
- }
82
- let reply = responseString ?? "{\"error\":\"Timeout waiting for JS response\"}"
83
-
84
- let response = GCDWebServerDataResponse(text: reply)
85
- response?.setValue("*", forAdditionalHeader: "Access-Control-Allow-Origin")
86
- response?.setValue("GET,POST,OPTIONS", forAdditionalHeader: "Access-Control-Allow-Methods")
87
- response?.setValue("origin, content-type, accept, authorization", forAdditionalHeader: "Access-Control-Allow-Headers")
88
- response?.setValue("3600", forAdditionalHeader: "Access-Control-Max-Age")
89
- response?.contentType = "application/json"
90
- return response!
91
- }
92
- )
93
-
41
+
42
+ // Handlers específicos por método HTTP
43
+ self.webServer?.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self) { request in
44
+ return self.handleRequest(request: request, method: "GET")
45
+ }
46
+
47
+ self.webServer?.addDefaultHandler(forMethod: "POST", request: GCDWebServerDataRequest.self) { request in
48
+ return self.handleRequest(request: request, method: "POST")
49
+ }
50
+
51
+ self.webServer?.addDefaultHandler(forMethod: "PUT", request: GCDWebServerDataRequest.self) { request in
52
+ return self.handleRequest(request: request, method: "PUT")
53
+ }
54
+
55
+ self.webServer?.addDefaultHandler(forMethod: "DELETE", request: GCDWebServerRequest.self) { request in
56
+ return self.handleRequest(request: request, method: "DELETE")
57
+ }
58
+
94
59
  let port: UInt = 8080
95
60
  do {
96
61
  try self.webServer?.start(options: [
@@ -108,7 +73,58 @@ public protocol HttpLocalServerDelegate: AnyObject {
108
73
  }
109
74
  }
110
75
  }
111
-
76
+
77
+ // Método para manejar todas las requests
78
+ private func handleRequest(request: GCDWebServerRequest, method: String) -> GCDWebServerResponse {
79
+ let path = request.url.path
80
+ var body: String? = nil
81
+
82
+ // Leer body si es POST o PUT
83
+ if let dataRequest = request as? GCDWebServerDataRequest {
84
+ if let text = String(data: dataRequest.data, encoding: .utf8) {
85
+ body = text
86
+ print("Body received: \(text)")
87
+ }
88
+ }
89
+
90
+ let requestId = UUID().uuidString
91
+ var responseString: String? = nil
92
+
93
+ // Set up a semaphore so we can block until JS responds or timeout
94
+ let semaphore = DispatchSemaphore(value: 0)
95
+ Self.queue.async {
96
+ Self.pendingResponses[requestId] = { responseBody in
97
+ responseString = responseBody
98
+ semaphore.signal()
99
+ }
100
+ }
101
+
102
+ // Notify delegate (plugin) with the request info
103
+ let req: [String: Any?] = [
104
+ "requestId": requestId,
105
+ "method": method,
106
+ "path": path,
107
+ "body": body,
108
+ "headers": request.headers
109
+ ]
110
+ self.delegate?.httpLocalServerDidReceiveRequest(req.compactMapValues { $0 })
111
+
112
+ // Wait for JS response or timeout (55 segundos)
113
+ _ = semaphore.wait(timeout: .now() + 55.0)
114
+ Self.queue.async {
115
+ Self.pendingResponses.removeValue(forKey: requestId)
116
+ }
117
+
118
+ let reply = responseString ?? "{\"error\":\"Timeout waiting for JS response\"}"
119
+ let response = GCDWebServerDataResponse(text: reply)
120
+ response?.setValue("*", forAdditionalHeader: "Access-Control-Allow-Origin")
121
+ response?.setValue("GET,POST,OPTIONS,PUT,DELETE", forAdditionalHeader: "Access-Control-Allow-Methods")
122
+ response?.setValue("origin, content-type, accept, authorization", forAdditionalHeader: "Access-Control-Allow-Headers")
123
+ response?.setValue("3600", forAdditionalHeader: "Access-Control-Max-Age")
124
+ response?.contentType = "application/json"
125
+ return response!
126
+ }
127
+
112
128
  @objc public func disconnect(_ call: CAPPluginCall) {
113
129
  DispatchQueue.main.async { [weak self] in
114
130
  self?.webServer?.stop()
@@ -116,7 +132,7 @@ public protocol HttpLocalServerDelegate: AnyObject {
116
132
  call.resolve()
117
133
  }
118
134
  }
119
-
135
+
120
136
  // Called by plugin when JS responds
121
137
  static func handleJsResponse(requestId: String, body: String) {
122
138
  queue.async {
@@ -126,7 +142,7 @@ public protocol HttpLocalServerDelegate: AnyObject {
126
142
  }
127
143
  }
128
144
  }
129
-
145
+
130
146
  // Helper: get WiFi IP address (IPv4)
131
147
  static func getWiFiAddress() -> String? {
132
148
  var address: String?
@@ -154,4 +170,4 @@ public protocol HttpLocalServerDelegate: AnyObject {
154
170
  }
155
171
  return address
156
172
  }
157
- }
173
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cappitolian/http-local-server",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Runs a local HTTP server on your device, accessible over LAN. Supports connect, disconnect, GET, and POST methods with IP and port discovery.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -19,10 +19,10 @@
19
19
  "license": "MIT",
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "git+https://github.com/alessandrycruz1987/cappitolian/plugins/http-local-server.git"
22
+ "url": "git+https://github.com/alessandrycruz1987/http-local-server.git.git"
23
23
  },
24
24
  "bugs": {
25
- "url": "https://github.com/alessandrycruz1987/cappitolian/plugins/http-local-server/issues"
25
+ "url": "https://github.com/alessandrycruz1987/http-local-server.git/issues"
26
26
  },
27
27
  "keywords": [
28
28
  "capacitor",
@@ -48,18 +48,18 @@
48
48
  "devDependencies": {
49
49
  "@capacitor/android": "^7.0.0",
50
50
  "@capacitor/core": "^7.0.0",
51
- "@capacitor/docgen": "^0.3.0",
51
+ "@capacitor/docgen": "^0.3.1",
52
52
  "@capacitor/ios": "^7.0.0",
53
53
  "@ionic/eslint-config": "^0.4.0",
54
54
  "@ionic/prettier-config": "^4.0.0",
55
55
  "@ionic/swiftlint-config": "^2.0.0",
56
- "eslint": "^8.57.0",
57
- "prettier": "^3.4.2",
58
- "prettier-plugin-java": "^2.6.6",
59
- "rimraf": "^6.0.1",
60
- "rollup": "^4.30.1",
56
+ "eslint": "^8.57.1",
57
+ "prettier": "^3.6.2",
58
+ "prettier-plugin-java": "^2.7.7",
59
+ "rimraf": "^6.1.0",
60
+ "rollup": "^4.53.2",
61
61
  "swiftlint": "^2.0.0",
62
- "typescript": "~4.1.5"
62
+ "typescript": "^5.9.3"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "@capacitor/core": ">=7.0.0"