expo-http-server 0.1.14-beta.1 → 0.1.14-beta.3
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.
|
@@ -18,10 +18,54 @@ class ExpoHttpServerModule : Module() {
|
|
|
18
18
|
val headers: HashMap<String, String>,
|
|
19
19
|
val body: String)
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
data class RouteConfig(val path: String, val method: String, val uuid: String)
|
|
22
|
+
|
|
23
|
+
private var server: Server? = null
|
|
24
|
+
private var started = false
|
|
25
|
+
private var configuredPort: Int = 0
|
|
26
|
+
private val routeConfigs = mutableListOf<RouteConfig>()
|
|
23
27
|
private val responses = ConcurrentHashMap<String, SimpleHttpResponse>()
|
|
24
28
|
|
|
29
|
+
private fun handleRequest(uuid: String, request: Request, response: Response): Response {
|
|
30
|
+
val headers: Map<String, String> = request.headers()
|
|
31
|
+
val params: Map<String, String> = request.params()
|
|
32
|
+
val cookies: Map<String, String> = request.cookies().associate { it.name() to it.value() }
|
|
33
|
+
sendEvent("onRequest", bundleOf(
|
|
34
|
+
"uuid" to uuid,
|
|
35
|
+
"method" to request.method().name,
|
|
36
|
+
"path" to request.url(),
|
|
37
|
+
"body" to request.content(),
|
|
38
|
+
"headersJson" to JSONObject(headers).toString(),
|
|
39
|
+
"paramsJson" to JSONObject(params).toString(),
|
|
40
|
+
"cookiesJson" to JSONObject(cookies).toString(),
|
|
41
|
+
))
|
|
42
|
+
val deadline = System.currentTimeMillis() + 30_000
|
|
43
|
+
while (!responses.containsKey(uuid) && System.currentTimeMillis() < deadline) {
|
|
44
|
+
Thread.sleep(10)
|
|
45
|
+
}
|
|
46
|
+
val res = responses.remove(uuid) ?: return response
|
|
47
|
+
response.setBodyText(res.body)
|
|
48
|
+
response.setStatus(res.statusCode)
|
|
49
|
+
response.addHeader("Content-Length", "" + res.body.length)
|
|
50
|
+
response.addHeader("Content-Type", res.contentType)
|
|
51
|
+
for ((key, value) in res.headers) {
|
|
52
|
+
response.addHeader(key, value)
|
|
53
|
+
}
|
|
54
|
+
return response
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fun buildServerWithRoutes(): Server {
|
|
58
|
+
var s: Server = AndroidServer.Builder {
|
|
59
|
+
port { configuredPort }
|
|
60
|
+
}.build()
|
|
61
|
+
for (route in routeConfigs) {
|
|
62
|
+
s = s.request(HttpMethod.getMethod(route.method), route.path) { request: Request, response: Response ->
|
|
63
|
+
handleRequest(route.uuid, request, response)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return s
|
|
67
|
+
}
|
|
68
|
+
|
|
25
69
|
override fun definition() = ModuleDefinition {
|
|
26
70
|
|
|
27
71
|
Name("ExpoHttpServer")
|
|
@@ -29,47 +73,24 @@ class ExpoHttpServerModule : Module() {
|
|
|
29
73
|
Events("onStatusUpdate", "onRequest")
|
|
30
74
|
|
|
31
75
|
Function("setup") { port: Int ->
|
|
76
|
+
configuredPort = port
|
|
32
77
|
server?.close()
|
|
33
78
|
started = false
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
79
|
+
routeConfigs.clear()
|
|
80
|
+
server = AndroidServer.Builder {
|
|
81
|
+
port { configuredPort }
|
|
38
82
|
}.build()
|
|
39
83
|
}
|
|
40
84
|
|
|
41
85
|
Function("route") { path: String, method: String, uuid: String ->
|
|
86
|
+
routeConfigs.add(RouteConfig(path, method, uuid))
|
|
42
87
|
server = server?.request(HttpMethod.getMethod(method), path) { request: Request, response: Response ->
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
val cookies: Map<String, String> = request.cookies().associate { it.name() to it.value() }
|
|
46
|
-
sendEvent("onRequest", bundleOf(
|
|
47
|
-
"uuid" to uuid,
|
|
48
|
-
"method" to request.method().name,
|
|
49
|
-
"path" to request.url(),
|
|
50
|
-
"body" to request.content(),
|
|
51
|
-
"headersJson" to JSONObject(headers).toString(),
|
|
52
|
-
"paramsJson" to JSONObject(params).toString(),
|
|
53
|
-
"cookiesJson" to JSONObject(cookies).toString(),
|
|
54
|
-
))
|
|
55
|
-
val deadline = System.currentTimeMillis() + 30_000
|
|
56
|
-
while (!responses.containsKey(uuid) && System.currentTimeMillis() < deadline) {
|
|
57
|
-
Thread.sleep(10)
|
|
58
|
-
}
|
|
59
|
-
val res = responses.remove(uuid) ?: return@request response
|
|
60
|
-
response.setBodyText(res.body)
|
|
61
|
-
response.setStatus(res.statusCode)
|
|
62
|
-
response.addHeader("Content-Length", "" + res.body.length)
|
|
63
|
-
response.addHeader("Content-Type", res.contentType)
|
|
64
|
-
for ((key, value) in res.headers) {
|
|
65
|
-
response.addHeader(key, value)
|
|
66
|
-
}
|
|
67
|
-
return@request response
|
|
68
|
-
};
|
|
88
|
+
handleRequest(uuid, request, response)
|
|
89
|
+
}
|
|
69
90
|
}
|
|
70
91
|
|
|
71
92
|
Function("start") {
|
|
72
|
-
if (
|
|
93
|
+
if (configuredPort == 0) {
|
|
73
94
|
sendEvent("onStatusUpdate", bundleOf(
|
|
74
95
|
"status" to "ERROR",
|
|
75
96
|
"message" to "Server not setup / port not configured"
|
|
@@ -78,6 +99,11 @@ class ExpoHttpServerModule : Module() {
|
|
|
78
99
|
if (!started) {
|
|
79
100
|
try {
|
|
80
101
|
started = true
|
|
102
|
+
// Always close and rebuild before starting so any existing socket binding
|
|
103
|
+
// is released, preventing BindException on the Netty event loop thread
|
|
104
|
+
// (which is asynchronous and not caught by this try/catch).
|
|
105
|
+
server?.close()
|
|
106
|
+
server = buildServerWithRoutes()
|
|
81
107
|
server?.start()
|
|
82
108
|
sendEvent("onStatusUpdate", bundleOf(
|
|
83
109
|
"status" to "STARTED",
|
package/build/index.d.ts
CHANGED
|
@@ -32,5 +32,5 @@ export interface Callback {
|
|
|
32
32
|
export declare const start: () => void;
|
|
33
33
|
export declare const route: (path: string, method: HttpMethod, callback: (request: RequestEvent) => Promise<Response>) => void;
|
|
34
34
|
export declare const setup: (port: number, onStatusUpdate?: ((event: StatusEvent) => void) | undefined) => void;
|
|
35
|
-
export declare const stop: () =>
|
|
35
|
+
export declare const stop: () => void;
|
|
36
36
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,SAAS,CAAC;AACvE;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;AAE5E,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CACxD;AAED,eAAO,MAAM,KAAK,YA4BjB,CAAC;AAEF,eAAO,MAAM,KAAK,SACV,MAAM,UACJ,UAAU,sBACE,YAAY,KAAK,QAAQ,QAAQ,CAAC,SAUvD,CAAC;AAEF,eAAO,MAAM,KAAK,SACV,MAAM,4BACa,WAAW,KAAK,IAAI,sBAU9C,CAAC;AAEF,eAAO,MAAM,IAAI,YAIhB,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -2,8 +2,10 @@ import { EventEmitter } from "expo-modules-core";
|
|
|
2
2
|
import ExpoHttpServerModule from "./ExpoHttpServerModule";
|
|
3
3
|
const emitter = new EventEmitter(ExpoHttpServerModule);
|
|
4
4
|
const requestCallbacks = [];
|
|
5
|
+
let requestSubscription = null;
|
|
5
6
|
export const start = () => {
|
|
6
|
-
|
|
7
|
+
requestSubscription?.remove();
|
|
8
|
+
requestSubscription = emitter.addListener("onRequest", async (event) => {
|
|
7
9
|
try {
|
|
8
10
|
const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);
|
|
9
11
|
if (!responseHandler) {
|
|
@@ -38,5 +40,9 @@ export const setup = (port, onStatusUpdate) => {
|
|
|
38
40
|
}
|
|
39
41
|
ExpoHttpServerModule.setup(port);
|
|
40
42
|
};
|
|
41
|
-
export const stop = () =>
|
|
43
|
+
export const stop = () => {
|
|
44
|
+
requestSubscription?.remove();
|
|
45
|
+
requestSubscription = null;
|
|
46
|
+
ExpoHttpServerModule.stop();
|
|
47
|
+
};
|
|
42
48
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAE1D,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,oBAAoB,CAAC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AAI,CACtB,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,oBAAoB,CAAC,KAAK,EAAE,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,IAAY,EACZ,MAAkB,EAClB,QAAsD,EACtD,EAAE;IACF,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjD,gBAAgB,CAAC,IAAI,CAAC;QACpB,MAAM;QACN,IAAI;QACJ,IAAI;QACJ,QAAQ;KACT,CAAC,CAAC;IACH,oBAAoB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,IAAY,EACZ,cAA6C,EAC7C,EAAE;IACF,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,CAAC,WAAW,CAAc,gBAAgB,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;YACjE,IAAI,CAAC;gBACH,MAAM,cAAc,CAAC,KAAK,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,oBAAoB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,EAAE;IACvB,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,mBAAmB,GAAG,IAAI,CAAC;IAC3B,oBAAoB,CAAC,IAAI,EAAE,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import { EventEmitter } from \"expo-modules-core\";\n\nimport ExpoHttpServerModule from \"./ExpoHttpServerModule\";\n\nconst emitter = new EventEmitter(ExpoHttpServerModule);\nconst requestCallbacks: Callback[] = [];\nlet requestSubscription: ReturnType<typeof emitter.addListener> | null = null;\n\nexport type HttpMethod = \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"OPTIONS\";\n/**\n * PAUSED AND RESUMED are iOS only\n */\nexport type Status = \"STARTED\" | \"PAUSED\" | \"RESUMED\" | \"STOPPED\" | \"ERROR\";\n\nexport interface StatusEvent {\n status: Status;\n message: string;\n}\n\nexport interface RequestEvent {\n uuid: string;\n method: string;\n path: string;\n body: string;\n headersJson: string;\n paramsJson: string;\n cookiesJson: string;\n}\n\nexport interface Response {\n statusCode?: number;\n statusDescription?: string;\n contentType?: string;\n headers?: Record<string, string>;\n body?: string;\n}\n\nexport interface Callback {\n method: string;\n path: string;\n uuid: string;\n callback: (request: RequestEvent) => Promise<Response>;\n}\n\nexport const start = () => {\n requestSubscription?.remove();\n requestSubscription = emitter.addListener<RequestEvent>(\"onRequest\", async (event) => {\n try {\n const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);\n if (!responseHandler) {\n ExpoHttpServerModule.respond(\n event.uuid,\n 404,\n \"Not Found\",\n \"application/json\",\n {},\n JSON.stringify({ error: \"Handler not found\" }),\n );\n return;\n }\n const response = await responseHandler.callback(event);\n ExpoHttpServerModule.respond(\n event.uuid,\n response.statusCode || 200,\n response.statusDescription || \"OK\",\n response.contentType || \"application/json\",\n response.headers || {},\n response.body || \"{}\",\n );\n } catch {}\n });\n ExpoHttpServerModule.start();\n};\n\nexport const route = (\n path: string,\n method: HttpMethod,\n callback: (request: RequestEvent) => Promise<Response>,\n) => {\n const uuid = Math.random().toString(16).slice(2);\n requestCallbacks.push({\n method,\n path,\n uuid,\n callback,\n });\n ExpoHttpServerModule.route(path, method, uuid);\n};\n\nexport const setup = (\n port: number,\n onStatusUpdate?: (event: StatusEvent) => void,\n) => {\n if (onStatusUpdate) {\n emitter.addListener<StatusEvent>(\"onStatusUpdate\", async (event) => {\n try {\n await onStatusUpdate(event);\n } catch {}\n });\n }\n ExpoHttpServerModule.setup(port);\n};\n\nexport const stop = () => {\n requestSubscription?.remove();\n requestSubscription = null;\n ExpoHttpServerModule.stop();\n};\n"]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import ExpoHttpServerModule from "./ExpoHttpServerModule";
|
|
|
4
4
|
|
|
5
5
|
const emitter = new EventEmitter(ExpoHttpServerModule);
|
|
6
6
|
const requestCallbacks: Callback[] = [];
|
|
7
|
+
let requestSubscription: ReturnType<typeof emitter.addListener> | null = null;
|
|
7
8
|
|
|
8
9
|
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS";
|
|
9
10
|
/**
|
|
@@ -42,7 +43,8 @@ export interface Callback {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export const start = () => {
|
|
45
|
-
|
|
46
|
+
requestSubscription?.remove();
|
|
47
|
+
requestSubscription = emitter.addListener<RequestEvent>("onRequest", async (event) => {
|
|
46
48
|
try {
|
|
47
49
|
const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);
|
|
48
50
|
if (!responseHandler) {
|
|
@@ -99,4 +101,8 @@ export const setup = (
|
|
|
99
101
|
ExpoHttpServerModule.setup(port);
|
|
100
102
|
};
|
|
101
103
|
|
|
102
|
-
export const stop = () =>
|
|
104
|
+
export const stop = () => {
|
|
105
|
+
requestSubscription?.remove();
|
|
106
|
+
requestSubscription = null;
|
|
107
|
+
ExpoHttpServerModule.stop();
|
|
108
|
+
};
|