@statewalker/webrun-http-browser 0.3.3 → 0.3.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.
Files changed (93) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +321 -51
  3. package/demo/demo-1.html +109 -194
  4. package/demo/demo-2.html +118 -299
  5. package/dist/core/data-calls.d.ts +5 -0
  6. package/dist/core/data-calls.d.ts.map +1 -0
  7. package/dist/core/data-channels.d.ts +17 -0
  8. package/dist/core/data-channels.d.ts.map +1 -0
  9. package/dist/core/index.d.ts +6 -0
  10. package/dist/core/index.d.ts.map +1 -0
  11. package/dist/core/message-target.d.ts +16 -0
  12. package/dist/core/message-target.d.ts.map +1 -0
  13. package/dist/core/registry.d.ts +18 -0
  14. package/dist/core/registry.d.ts.map +1 -0
  15. package/dist/http/http-send-recieve.d.ts +23 -0
  16. package/dist/http/http-send-recieve.d.ts.map +1 -0
  17. package/dist/http/index.d.ts +4 -0
  18. package/dist/http/index.d.ts.map +1 -0
  19. package/dist/index.d.ts +4 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +2541 -807
  22. package/dist/relay/index-sw.d.ts +7 -0
  23. package/dist/relay/index-sw.d.ts.map +1 -0
  24. package/dist/relay/index.d.ts +69 -0
  25. package/dist/relay/index.d.ts.map +1 -0
  26. package/dist/relay/split-service-url.d.ts +13 -0
  27. package/dist/relay/split-service-url.d.ts.map +1 -0
  28. package/dist/relay-sw.d.ts +2 -0
  29. package/dist/relay-sw.d.ts.map +1 -0
  30. package/dist/relay-sw.js +878 -0
  31. package/dist/sw/http-sw-dispatcher.d.ts +28 -0
  32. package/dist/sw/http-sw-dispatcher.d.ts.map +1 -0
  33. package/dist/sw/index.d.ts +3 -0
  34. package/dist/sw/index.d.ts.map +1 -0
  35. package/dist/sw/sw-dispatcher.d.ts +74 -0
  36. package/dist/sw/sw-dispatcher.d.ts.map +1 -0
  37. package/dist/sw-worker.d.ts +2 -0
  38. package/dist/sw-worker.d.ts.map +1 -0
  39. package/dist/sw-worker.js +813 -0
  40. package/dist/sw.d.ts +2 -0
  41. package/dist/sw.d.ts.map +1 -0
  42. package/dist/sw.js +1076 -0
  43. package/package.json +50 -37
  44. package/public/index.html +61 -97
  45. package/public/index.js +7 -4
  46. package/public/sw-worker.js +4 -0
  47. package/public-relay/heartbeat.js +4 -3
  48. package/public-relay/relay-sw.js +4 -6
  49. package/public-relay/relay.html +16 -11
  50. package/src/core/data-calls.ts +46 -0
  51. package/src/core/data-channels.ts +224 -0
  52. package/src/core/index.ts +9 -0
  53. package/src/core/message-target.ts +18 -0
  54. package/src/core/registry.ts +54 -0
  55. package/src/http/http-send-recieve.ts +81 -0
  56. package/src/http/index.ts +7 -0
  57. package/src/index.ts +3 -0
  58. package/src/relay/index-sw.ts +145 -0
  59. package/src/relay/index.ts +261 -0
  60. package/src/relay/split-service-url.ts +30 -0
  61. package/src/relay-sw.ts +7 -0
  62. package/src/sw/http-sw-dispatcher.ts +122 -0
  63. package/src/sw/index.ts +2 -0
  64. package/src/sw/sw-dispatcher.ts +307 -0
  65. package/src/sw-worker.ts +7 -0
  66. package/src/sw.ts +1 -0
  67. package/CHANGELOG.md +0 -1
  68. package/demo/demo-2 copy.html +0 -598
  69. package/demo/demo-3.html +0 -167
  70. package/dist/index-sw-umd.js +0 -798
  71. package/dist/index-sw-umd.min.js +0 -2
  72. package/dist/index-sw.js +0 -791
  73. package/dist/index-sw.min.js +0 -2
  74. package/dist/index-umd.js +0 -853
  75. package/dist/index-umd.min.js +0 -2
  76. package/dist/index.min.js +0 -2
  77. package/index.js +0 -1
  78. package/src/core/data-calls.js +0 -36
  79. package/src/core/data-channels.js +0 -192
  80. package/src/core/data-send-recieve.js +0 -28
  81. package/src/core/errors.js +0 -13
  82. package/src/core/index.js +0 -4
  83. package/src/http/HttpError.js +0 -59
  84. package/src/http/http-send-recieve.js +0 -40
  85. package/src/http/http-stubs.js +0 -135
  86. package/src/http/index.js +0 -3
  87. package/src/http/readable-streams.js +0 -33
  88. package/src/index.js +0 -7
  89. package/src/relay/index-sw.js +0 -216
  90. package/src/relay/index.js +0 -263
  91. package/src/relay/splitServiceUrl.js +0 -24
  92. package/src/sw/http-sw-dispatcher.js +0 -128
  93. package/src/sw/sw-dispatcher.js +0 -264
package/demo/demo-3.html DELETED
@@ -1,167 +0,0 @@
1
- <!DOCTYPE HTML>
2
- <html>
3
-
4
- <head>
5
- <meta charset="utf-8" />
6
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
7
- <meta name="viewport" content="width=device-width, initial-scale=1" />
8
- </head>
9
-
10
- <body>
11
- <div id="view"></div>
12
-
13
- <script type="module">
14
- import * as WebRunHttp from "https://unpkg.com/@statewalker/webrun-http-browser@0.3/dist/index.js";
15
- import { Hono } from "https://unpkg.com/hono@3.5/dist/index.js";
16
- import mime from "https://esm.sh/mime-types";
17
-
18
- Promise.resolve().then(main).catch(console.error);
19
- async function main() {
20
- // Load a Hono request router:
21
- const router = getRouter();
22
-
23
- // Create an HTTP Server with the specified name.
24
- // It uses the given Hono router to dispatch requests
25
- // between endpoints.
26
- const serviceBaseUrl = await startHttpService({
27
- serviceKey: "MY_SITE",
28
- router
29
- })
30
- const siteUrl = `${serviceBaseUrl}/`;
31
-
32
- // Now we can visualize the site in an iframe iframe:
33
- const container = document.querySelector("#view");
34
- const iframe = document.createElement("iframe");
35
- iframe.width = "100%";
36
- iframe.height = "500px";
37
- iframe.src = siteUrl;
38
- container.appendChild(iframe);
39
-
40
- // We also append a reference to this site.
41
- // It allows to open the site in a separate window:
42
- const a = document.createElement("a");
43
- a.target = "_blank";
44
- a.href = siteUrl;
45
- a.innerText = siteUrl;
46
- container.appendChild(a);
47
- }
48
-
49
- async function startHttpService({
50
- serviceKey,
51
- router
52
- }) {
53
- // Step 1: Start a ServiceWorker and open a MessageChannel used to recieve HTTP requests and send responses:
54
- const connection = await WebRunHttp.newRemoteRelayChannel();
55
-
56
- // Step 2: Define an HTTP handler
57
- const serviceBaseUrl = `${connection.baseUrl}~${serviceKey}`;
58
- const app = new Hono({
59
- getPath: (req) => req.url.substring(serviceBaseUrl.length)
60
- });
61
- // Add the router
62
- app.route("/", router);
63
- const httpHandler = (request) => app.fetch(request);
64
-
65
- // Step 3: Instantiate a "server" using the communcation channel with the ServiceWorker:
66
- const close = await WebRunHttp.initHttpService(httpHandler, {
67
- key: serviceKey, // Service prefix
68
- port: connection.port
69
- });
70
-
71
- return serviceBaseUrl;
72
- }
73
-
74
- function getRouter() {
75
- const router = new Hono();
76
-
77
- // Server-side REST API handler:
78
- router.get("/api/:url", async (c) => {
79
- const { url } = c.req.param();
80
- const r = await fetch(url);
81
- const result = await r.text();
82
- return c.text(result);
83
- // c.text(`Hello ${url}! Current timestamp: ${Date.now()}`);
84
- });
85
-
86
- // Static files
87
- router.get("/*", newStaticFilesDispatcher({
88
- "/index.css": `
89
- html, body {
90
- padding: 0;
91
- margin: 0;
92
- }
93
- h1 {
94
- color : navy;
95
- }
96
- `,
97
-
98
-
99
- "/index.js": `
100
- document.addEventListener("DOMContentLoaded", () => {
101
- const input = document.querySelector("#url");
102
- input.addEventListener("input", (ev) => {
103
- ev.preventDefault();
104
- ev.stopPropagation();
105
- updateUrl();
106
- })
107
- updateUrl();
108
- async function updateUrl() {
109
- const url = input.value;
110
- const res = await fetch("./api/" + encodeURIComponent(url));
111
- const text = await res.text();
112
- const view = document.querySelector("#messages");
113
- view.innerText = text;
114
- }
115
- })
116
- `,
117
- "/index.html": `<!DOCTYPE HTML>
118
- <html>
119
- <head>
120
- <meta charset="utf-8" />
121
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
122
- <meta name="viewport" content="width=device-width, initial-scale=1" />
123
- <link rel="stylesheet" href="./index.css" />
124
- </head>
125
- <body>
126
- <h1>Hello In-Browser Dynamic Site</h1>
127
- <div>
128
- Please enter remote service URL:
129
- <input type="text" id="url" value="https://dummyjson.com/products">
130
- </div>
131
- <p>Message from the server:</p>
132
- <div id="messages">
133
- </div>
134
- <script type="module" src="./index.js"><\/script>
135
- </body>
136
- </html>
137
- `
138
- }))
139
-
140
- return router;
141
- }
142
-
143
- function newStaticFilesDispatcher(files) {
144
- return function showStaticFiles(context) {
145
- let { url, path } = context.req;
146
- path = path.replace(/#.*$/, "");
147
- if (path.match(/\/$/)) {
148
- path = path + "index.html";
149
- }
150
- const result = files[path];
151
- if (!result) {
152
- return context.notFound();
153
- }
154
- const mimeType = mime.lookup(path) || "application/octet-stream";
155
- context.header("Content-Type", mimeType);
156
- context.status(201);
157
- if (typeof result === "object") {
158
- return context.json(result);
159
- } else {
160
- return context.body(result);
161
- }
162
- };
163
- }
164
- </script>
165
- </body>
166
-
167
- </html>