beam.com 0.1.0-rc.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/LICENSE +202 -0
- package/NOTICE +97 -0
- package/README.md +369 -0
- package/js/beam.com.mjs +33 -0
- package/js/download.mjs +78 -0
- package/js/node.mjs +75 -0
- package/package.json +36 -0
- package/runtime/app-com.js +203 -0
- package/runtime/beam.mjs +2 -0
- package/runtime/beam.wasm +0 -0
- package/runtime/browser/beam-wasm.js +2 -0
- package/runtime/browser/net.js +7 -0
- package/runtime/browser/none.js +4 -0
- package/runtime/browser.js +161 -0
- package/runtime/cloudflare/durable.js +361 -0
- package/runtime/cloudflare/index.js +76 -0
- package/runtime/cloudflare/release.js +42 -0
- package/runtime/cloudflare/snapshot.js +3 -0
- package/runtime/cloudflare/worker.js +1891 -0
- package/runtime/deno/app.js +8 -0
- package/runtime/deno/beam-wasm.js +3 -0
- package/runtime/deno/none.js +3 -0
- package/runtime/deno/release-bin.js +46 -0
- package/runtime/deno/snapshot-bin.js +11 -0
- package/runtime/deno/worker.js +1891 -0
- package/runtime/deno.js +346 -0
- package/runtime/deno.json +5 -0
- package/runtime/durable-global.js +40 -0
- package/runtime/durable.js +361 -0
- package/runtime/global.js +26 -0
- package/runtime/licenses/LICENSE +202 -0
- package/runtime/licenses/NOTICE +97 -0
- package/runtime/licenses/bcrypt_elixir.txt +87 -0
- package/runtime/licenses/cosmopolitan.txt +16 -0
- package/runtime/licenses/elixir/Apache-2.0.txt +73 -0
- package/runtime/licenses/elixir/LicenseRef-elixir-trademark-policy.txt +98 -0
- package/runtime/licenses/elixir/LicenseRef-scancode-unicode.txt +58 -0
- package/runtime/licenses/emscripten.txt +102 -0
- package/runtime/licenses/exqlite.txt +21 -0
- package/runtime/licenses/llvm.txt +311 -0
- package/runtime/licenses/musl.txt +193 -0
- package/runtime/licenses/otp/BSD-2-Clause.txt +22 -0
- package/runtime/licenses/otp/BSD-3-Clause.txt +25 -0
- package/runtime/licenses/otp/LicenseRef-IETF-MIB.txt +9 -0
- package/runtime/licenses/otp/LicenseRef-RSA-PKCS3.txt +23 -0
- package/runtime/licenses/otp/LicenseRef-RSA-PKCS5v2-0.txt +17 -0
- package/runtime/licenses/otp/LicenseRef-scancode-ietf-trust.txt +296 -0
- package/runtime/licenses/otp/LicenseRef-scancode-ietf.txt +23 -0
- package/runtime/licenses/otp/LicenseRef-scancode-w3c-docs-19990405.txt +26 -0
- package/runtime/licenses/otp/LicenseRef-scancode-wxwindows-free-doc-3.txt +57 -0
- package/runtime/licenses/otp/MIT.txt +17 -0
- package/runtime/licenses/otp/SGI-B-2.0.txt +25 -0
- package/runtime/licenses/otp/W3C-19980720.txt +55 -0
- package/runtime/licenses/picosat.txt +19 -0
- package/runtime/licenses/wamr.txt +219 -0
- package/runtime/nifs.js +2 -0
- package/runtime/package.json +1 -0
- package/runtime/page/404.html +49 -0
- package/runtime/page/app-com.js +203 -0
- package/runtime/page/beam.mjs +2 -0
- package/runtime/page/browser/beam-wasm.js +2 -0
- package/runtime/page/browser/net.js +7 -0
- package/runtime/page/browser/none.js +4 -0
- package/runtime/page/browser.js +161 -0
- package/runtime/page/index.html +50 -0
- package/runtime/page/licenses/LICENSE +202 -0
- package/runtime/page/licenses/NOTICE +97 -0
- package/runtime/page/licenses/bcrypt_elixir.txt +87 -0
- package/runtime/page/licenses/cosmopolitan.txt +16 -0
- package/runtime/page/licenses/elixir/Apache-2.0.txt +73 -0
- package/runtime/page/licenses/elixir/LicenseRef-elixir-trademark-policy.txt +98 -0
- package/runtime/page/licenses/elixir/LicenseRef-scancode-unicode.txt +58 -0
- package/runtime/page/licenses/emscripten.txt +102 -0
- package/runtime/page/licenses/exqlite.txt +21 -0
- package/runtime/page/licenses/llvm.txt +311 -0
- package/runtime/page/licenses/musl.txt +193 -0
- package/runtime/page/licenses/otp/BSD-2-Clause.txt +22 -0
- package/runtime/page/licenses/otp/BSD-3-Clause.txt +25 -0
- package/runtime/page/licenses/otp/LicenseRef-IETF-MIB.txt +9 -0
- package/runtime/page/licenses/otp/LicenseRef-RSA-PKCS3.txt +23 -0
- package/runtime/page/licenses/otp/LicenseRef-RSA-PKCS5v2-0.txt +17 -0
- package/runtime/page/licenses/otp/LicenseRef-scancode-ietf-trust.txt +296 -0
- package/runtime/page/licenses/otp/LicenseRef-scancode-ietf.txt +23 -0
- package/runtime/page/licenses/otp/LicenseRef-scancode-w3c-docs-19990405.txt +26 -0
- package/runtime/page/licenses/otp/LicenseRef-scancode-wxwindows-free-doc-3.txt +57 -0
- package/runtime/page/licenses/otp/MIT.txt +17 -0
- package/runtime/page/licenses/otp/SGI-B-2.0.txt +25 -0
- package/runtime/page/licenses/otp/W3C-19980720.txt +55 -0
- package/runtime/page/licenses/picosat.txt +19 -0
- package/runtime/page/licenses/wamr.txt +219 -0
- package/runtime/page/main.js +176 -0
- package/runtime/page/runtime-id.js +3 -0
- package/runtime/page/scope.js +17 -0
- package/runtime/page/sw.js +111 -0
- package/runtime/page/vm.js +148 -0
- package/runtime/page/worker.js +1891 -0
- package/runtime/page/ws-shim.js +143 -0
- package/runtime/release/app.js +17 -0
- package/runtime/release.json +1 -0
- package/runtime/runtime-id.js +3 -0
- package/runtime/tcp-proxy.mjs +30 -0
- package/runtime/worker.js +1891 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
--- LLVM Exceptions to the Apache 2.0 License ----
|
|
205
|
+
|
|
206
|
+
As an exception, if, as a result of your compiling your source code, portions
|
|
207
|
+
of this Software are embedded into an Object form of such source code, you
|
|
208
|
+
may redistribute such embedded portions in such Object form without complying
|
|
209
|
+
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
|
|
210
|
+
|
|
211
|
+
In addition, if you combine or link compiled forms of this Software with
|
|
212
|
+
software that is licensed under the GPLv2 ("Combined Software") and if a
|
|
213
|
+
court of competent jurisdiction determines that the patent provision (Section
|
|
214
|
+
3), the indemnity provision (Section 9) or other Section of the License
|
|
215
|
+
conflicts with the conditions of the GPLv2, you may retroactively and
|
|
216
|
+
prospectively choose to deem waived or otherwise exclude such Section(s) of
|
|
217
|
+
the License, but only in their entirety and only with respect to the Combined
|
|
218
|
+
Software.
|
|
219
|
+
|
package/runtime/nifs.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Page not found</title>
|
|
7
|
+
<!-- GitHub Pages gives this page for a path with no file. A path of the app
|
|
8
|
+
(SITE/app/PATH, for example a link to a page of the app before the first
|
|
9
|
+
visit) goes to the page of the site, with the path in the fragment
|
|
10
|
+
(SITE#/PATH). This page does not know the base path of the site, and a
|
|
11
|
+
repository can have the name "app" (/app/app/users). So it tries each
|
|
12
|
+
prefix of the path before a segment "app/", the longest first, and uses
|
|
13
|
+
the prefix where sw.js exists. -->
|
|
14
|
+
<style>
|
|
15
|
+
:root { --bg: #f6f4f1; --text: #1f1a17; --muted: #756c65; }
|
|
16
|
+
@media (prefers-color-scheme: dark) {
|
|
17
|
+
:root:not([data-theme="light"]) { --bg: #171412; --text: #efe9e4; --muted: #a2978f; }
|
|
18
|
+
}
|
|
19
|
+
:root[data-theme="dark"] { --bg: #171412; --text: #efe9e4; --muted: #a2978f; }
|
|
20
|
+
body { background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; margin: 0; }
|
|
21
|
+
main { max-width: 40rem; margin: 0 auto; padding: 15vh 16px 0; }
|
|
22
|
+
p { color: var(--muted); }
|
|
23
|
+
</style>
|
|
24
|
+
</head>
|
|
25
|
+
<body>
|
|
26
|
+
<main id="missing" hidden>
|
|
27
|
+
<h1>404</h1>
|
|
28
|
+
<p>This page does not exist.</p>
|
|
29
|
+
</main>
|
|
30
|
+
<script>
|
|
31
|
+
(async () => {
|
|
32
|
+
const path = location.pathname;
|
|
33
|
+
const bases = [];
|
|
34
|
+
for (let i = path.indexOf('/app/'); i !== -1; i = path.indexOf('/app/', i + 1)) bases.push(path.slice(0, i + 1));
|
|
35
|
+
for (const base of bases.reverse()) {
|
|
36
|
+
try {
|
|
37
|
+
const r = await fetch(`${base}sw.js`, { method: 'HEAD', cache: 'no-store' });
|
|
38
|
+
if (r.ok) {
|
|
39
|
+
const rest = path.slice(base.length + 'app'.length) + location.search + location.hash;
|
|
40
|
+
location.replace(rest === '/' ? base : `${base}#${rest}`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
} catch {}
|
|
44
|
+
}
|
|
45
|
+
document.getElementById('missing').hidden = false;
|
|
46
|
+
})();
|
|
47
|
+
</script>
|
|
48
|
+
</body>
|
|
49
|
+
</html>
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// The release of a native app.com for the WebAssembly runtime: the same
|
|
2
|
+
// file that runs natively runs in the VM of worker.js (Workers, Deno, a
|
|
3
|
+
// web page). "beam.com INPUT -o app.com" writes the edge part of the file
|
|
4
|
+
// into its zip, under .wasm/ (beam_com_wasm:overlay/4): .release.json
|
|
5
|
+
// (the boot of the VM, and the runtime of the build), the application
|
|
6
|
+
// wasm_host, the boot script with wasm_host, and the modules in the place
|
|
7
|
+
// of NIFs.
|
|
8
|
+
//
|
|
9
|
+
// appRelease(read, size, { runtime }) gives the release as release.bin
|
|
10
|
+
// gives it (an ArrayBuffer, see unpack in worker.js): the files of lib/
|
|
11
|
+
// and releases/ of the zip, with the files of .wasm/ in their place, and
|
|
12
|
+
// .release.json first.
|
|
13
|
+
// - read(at, n): n bytes (a Uint8Array) at the offset at of app.com: a
|
|
14
|
+
// file, a range of a URL, or bytes in memory (bytesReader).
|
|
15
|
+
// - size: the size of app.com in bytes.
|
|
16
|
+
// - runtime: the identity of this runtime (the text of runtime-id.js of
|
|
17
|
+
// DIR, beam_com_wasm:runtime_id/1). With it, a file for another runtime
|
|
18
|
+
// is an error.
|
|
19
|
+
// It reads three parts: the end of the file, the central directory, and
|
|
20
|
+
// the span of the release (the edge part is at the end of it).
|
|
21
|
+
//
|
|
22
|
+
// appFiles(read, size, { runtime }) gives the same files with no copy:
|
|
23
|
+
// {meta, files, byteLength, buffers, statics}, which worker.js takes in
|
|
24
|
+
// the place of release.bin.
|
|
25
|
+
// - files: [path, bytes] for each file of the release. A stored entry is a
|
|
26
|
+
// view of the bytes that read gives (with bytesReader, of app.com).
|
|
27
|
+
// - buffers: the ArrayBuffers of these bytes. A file of the VM in another
|
|
28
|
+
// buffer is a file that the boot wrote (the snapshot keeps it).
|
|
29
|
+
// - statics: the static files of a Phoenix app (lib/NAME-VSN/priv/static,
|
|
30
|
+
// NAME the name of the release), a Map of path to {data, size, crc, deflated}, which
|
|
31
|
+
// the host serves before the VM (worker.js). They are not in files,
|
|
32
|
+
// except cache_manifest.json, which Phoenix reads at its start.
|
|
33
|
+
//
|
|
34
|
+
// The zip of beam.com stores each .beam file as a gzip file (the loader of
|
|
35
|
+
// ERTS reads it so, beam_com_zip), so it is a view too. A deflated .beam
|
|
36
|
+
// entry (an older app.com) becomes a gzip file with the same data, with
|
|
37
|
+
// no inflate. Each other entry is checked with its CRC-32.
|
|
38
|
+
|
|
39
|
+
const text = new TextDecoder();
|
|
40
|
+
const u16 = (b, i) => b[i] | (b[i + 1] << 8);
|
|
41
|
+
const u32 = (b, i) => (b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)) >>> 0;
|
|
42
|
+
const fail = (message) => { throw new Error(`app.com: ${message}`); };
|
|
43
|
+
|
|
44
|
+
const LOCAL = 0x04034b50, CENTRAL = 0x02014b50, END = 0x06054b50;
|
|
45
|
+
const STORED = 0, DEFLATED = 8;
|
|
46
|
+
// The end record (22 bytes) and a comment of at most 65535 bytes.
|
|
47
|
+
const TAIL = 22 + 0xffff;
|
|
48
|
+
|
|
49
|
+
const CRC = new Uint32Array(256).map((_, n) => {
|
|
50
|
+
let c = n;
|
|
51
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
52
|
+
return c >>> 0;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export function crc32(b) {
|
|
56
|
+
let c = 0xffffffff;
|
|
57
|
+
for (let i = 0; i < b.length; i++) c = CRC[(c ^ b[i]) & 0xff] ^ (c >>> 8);
|
|
58
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A reader of bytes in memory (an ArrayBuffer or a typed array).
|
|
62
|
+
export function bytesReader(bytes) {
|
|
63
|
+
const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes.buffer ?? bytes, bytes.byteOffset ?? 0, bytes.byteLength);
|
|
64
|
+
return { size: b.length, read: async (at, n) => b.subarray(at, at + n) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The central directory: {entries, cdAt}, where each entry is {name,
|
|
68
|
+
// method, crc, csize, size, offset}.
|
|
69
|
+
export async function zipEntries(read, size) {
|
|
70
|
+
const n = Math.min(size, TAIL);
|
|
71
|
+
const tail = await read(size - n, n);
|
|
72
|
+
if (tail.length !== n) fail('a short read at the end of the file');
|
|
73
|
+
// The last end record whose comment ends the file.
|
|
74
|
+
let e = -1;
|
|
75
|
+
for (let i = n - 22; i >= 0; i--) {
|
|
76
|
+
if (u32(tail, i) === END && i + 22 + u16(tail, i + 20) === n) { e = i; break; }
|
|
77
|
+
}
|
|
78
|
+
if (e < 0) fail('not a zip file (no end of central directory)');
|
|
79
|
+
const count = u16(tail, e + 10), cdSize = u32(tail, e + 12), cdAt = u32(tail, e + 16);
|
|
80
|
+
if (count === 0xffff || cdAt === 0xffffffff || cdSize === 0xffffffff) fail('a zip64 file is not supported');
|
|
81
|
+
if (cdAt + cdSize > size) fail('the central directory is outside the file');
|
|
82
|
+
const cd = await read(cdAt, cdSize);
|
|
83
|
+
const entries = [];
|
|
84
|
+
for (let i = 0, k = 0; k < count; k++) {
|
|
85
|
+
if (i + 46 > cd.length || u32(cd, i) !== CENTRAL) fail('a bad central directory');
|
|
86
|
+
const nlen = u16(cd, i + 28), xlen = u16(cd, i + 30), clen = u16(cd, i + 32);
|
|
87
|
+
entries.push({
|
|
88
|
+
name: text.decode(cd.subarray(i + 46, i + 46 + nlen)), method: u16(cd, i + 10),
|
|
89
|
+
crc: u32(cd, i + 16), csize: u32(cd, i + 20), size: u32(cd, i + 24), offset: u32(cd, i + 42),
|
|
90
|
+
});
|
|
91
|
+
i += 46 + nlen + xlen + clen;
|
|
92
|
+
}
|
|
93
|
+
return { entries, cdAt };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function inflate(raw) {
|
|
97
|
+
const s = new Blob([raw]).stream().pipeThrough(new DecompressionStream('deflate-raw'));
|
|
98
|
+
return new Uint8Array(await new Response(s).arrayBuffer());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The same deflate data in a gzip file: a header of 10 bytes, the data,
|
|
102
|
+
// the CRC-32 and the size.
|
|
103
|
+
function gzipOf(raw, e) {
|
|
104
|
+
const out = new Uint8Array(10 + raw.length + 8);
|
|
105
|
+
out.set([0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 255]);
|
|
106
|
+
out.set(raw, 10);
|
|
107
|
+
const v = new DataView(out.buffer);
|
|
108
|
+
v.setUint32(10 + raw.length, e.crc, true);
|
|
109
|
+
v.setUint32(14 + raw.length, e.size, true);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The data of the entry e, in span (the bytes of the file from offset base).
|
|
114
|
+
async function entryData(span, base, e) {
|
|
115
|
+
const h = e.offset - base;
|
|
116
|
+
if (h < 0 || h + 30 > span.length || u32(span, h) !== LOCAL) fail(`${e.name}: no local header`);
|
|
117
|
+
const start = h + 30 + u16(span, h + 26) + u16(span, h + 28);
|
|
118
|
+
if (start + e.csize > span.length) fail(`${e.name}: the data is outside the file`);
|
|
119
|
+
const raw = span.subarray(start, start + e.csize);
|
|
120
|
+
if (e.method === DEFLATED && e.name.endsWith('.beam')) return gzipOf(raw, e);
|
|
121
|
+
let d;
|
|
122
|
+
if (e.method === STORED) d = raw;
|
|
123
|
+
else if (e.method === DEFLATED) d = await inflate(raw);
|
|
124
|
+
else fail(`${e.name}: the compression method ${e.method} is not supported`);
|
|
125
|
+
if (d.length !== e.size || crc32(d) !== e.crc) fail(`${e.name}: the CRC-32 does not match`);
|
|
126
|
+
return d;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const EDGE = '.wasm/';
|
|
130
|
+
const inRelease = (name) => name.startsWith('lib/') || name.startsWith('releases/') || name.startsWith(EDGE);
|
|
131
|
+
|
|
132
|
+
export async function appRelease(read, size, options = {}) {
|
|
133
|
+
const { meta, files } = await appFiles(read, size, { ...options, statics: false });
|
|
134
|
+
return pack(meta, files);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function appFiles(read, size, { runtime, statics: split = true } = {}) {
|
|
138
|
+
const { entries, cdAt } = await zipEntries(read, size);
|
|
139
|
+
const want = entries.filter((e) => inRelease(e.name) && !e.name.endsWith('/'));
|
|
140
|
+
const json = want.find((e) => e.name === `${EDGE}.release.json`);
|
|
141
|
+
if (!json) fail('no edge part (.wasm/.release.json): build it with a beam.com that has the WebAssembly runtime, and without --no-edge');
|
|
142
|
+
// One read: the entries of the release, up to the central directory.
|
|
143
|
+
const lo = Math.min(...want.map((e) => e.offset));
|
|
144
|
+
const span = await read(lo, cdAt - lo);
|
|
145
|
+
if (span.length !== cdAt - lo) fail('a short read of the release');
|
|
146
|
+
const meta = await entryData(span, lo, json);
|
|
147
|
+
const release = JSON.parse(text.decode(meta));
|
|
148
|
+
if (runtime && release.runtime !== runtime) {
|
|
149
|
+
fail(`it was built for the runtime ${String(release.runtime).slice(0, 12)}, and this runtime is ${runtime.slice(0, 12)}: `
|
|
150
|
+
+ 'use the runtime of the beam.com that built it');
|
|
151
|
+
}
|
|
152
|
+
// The static files of a Phoenix app (PHX_SERVER in the environment of
|
|
153
|
+
// the release): lib/NAME-VSN/priv/static/, for the application with the
|
|
154
|
+
// name of the release. Plug.Static serves them at the root of the site.
|
|
155
|
+
const app = split && release.env?.PHX_SERVER === 'true' && typeof release.name === 'string'
|
|
156
|
+
&& `lib/${release.name}-`;
|
|
157
|
+
const prefix = app && want.map((e) => /^lib\/[^/]+\/priv\/static\//.exec(e.name)?.[0])
|
|
158
|
+
.find((p) => p?.startsWith(app) && /^[0-9]/.test(p.slice(app.length)));
|
|
159
|
+
const statics = new Map();
|
|
160
|
+
const files = new Map();
|
|
161
|
+
for (const e of want) {
|
|
162
|
+
if (e.name.startsWith(EDGE)) continue;
|
|
163
|
+
if (prefix && e.name.startsWith(prefix) && e.name !== `${prefix}cache_manifest.json`) {
|
|
164
|
+
statics.set(e.name.slice(prefix.length - 1), staticEntry(span, lo, e));
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
files.set(e.name, await entryData(span, lo, e));
|
|
168
|
+
}
|
|
169
|
+
for (const e of want) if (e.name.startsWith(EDGE) && e !== json) files.set(e.name.slice(EDGE.length), await entryData(span, lo, e));
|
|
170
|
+
const list = [...files];
|
|
171
|
+
return {
|
|
172
|
+
meta, files: list, statics,
|
|
173
|
+
byteLength: list.reduce((n, [, d]) => n + d.length, meta.length),
|
|
174
|
+
buffers: new Set([meta.buffer, ...list.map(([, d]) => d.buffer)]),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// A static file: its bytes in the zip (stored or deflated), not checked
|
|
179
|
+
// here (the host checks the CRC-32 when it inflates them).
|
|
180
|
+
function staticEntry(span, base, e) {
|
|
181
|
+
const h = e.offset - base;
|
|
182
|
+
if (h < 0 || h + 30 > span.length || u32(span, h) !== LOCAL) fail(`${e.name}: no local header`);
|
|
183
|
+
const start = h + 30 + u16(span, h + 26) + u16(span, h + 28);
|
|
184
|
+
if (start + e.csize > span.length) fail(`${e.name}: the data is outside the file`);
|
|
185
|
+
if (e.method !== STORED && e.method !== DEFLATED) fail(`${e.name}: the compression method ${e.method} is not supported`);
|
|
186
|
+
return { data: span.subarray(start, start + e.csize), size: e.size, crc: e.crc, deflated: e.method === DEFLATED };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// release.bin: "BEAMFS1\n", then (length, path, length, data) for each
|
|
190
|
+
// file, with .release.json first.
|
|
191
|
+
function pack(meta, files) {
|
|
192
|
+
const enc = new TextEncoder();
|
|
193
|
+
const parts = [['.release.json', meta], ...files].map(([p, d]) => [enc.encode(p), d]);
|
|
194
|
+
const out = new Uint8Array(8 + parts.reduce((s, [p, d]) => s + 8 + p.length + d.length, 0));
|
|
195
|
+
const v = new DataView(out.buffer);
|
|
196
|
+
out.set(enc.encode('BEAMFS1\n'));
|
|
197
|
+
let i = 8;
|
|
198
|
+
for (const [p, d] of parts) {
|
|
199
|
+
v.setUint32(i, p.length); out.set(p, i + 4); i += 4 + p.length;
|
|
200
|
+
v.setUint32(i, d.length); out.set(d, i + 4); i += 4 + d.length;
|
|
201
|
+
}
|
|
202
|
+
return out.buffer;
|
|
203
|
+
}
|