swift-rust 0.2.0

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 (56) hide show
  1. package/README.md +78 -0
  2. package/bin/dev-server.mjs +1360 -0
  3. package/bin/error-overlay.mjs +529 -0
  4. package/bin/runtime/hmr-client.js +77 -0
  5. package/bin/swift-rust.js +96 -0
  6. package/dist/env.d.ts +4 -0
  7. package/dist/env.d.ts.map +1 -0
  8. package/dist/env.js +3 -0
  9. package/dist/env.js.map +1 -0
  10. package/dist/font-google.d.ts +2 -0
  11. package/dist/font-google.d.ts.map +1 -0
  12. package/dist/font-google.js +2 -0
  13. package/dist/font-google.js.map +1 -0
  14. package/dist/font-local.d.ts +3 -0
  15. package/dist/font-local.d.ts.map +1 -0
  16. package/dist/font-local.js +2 -0
  17. package/dist/font-local.js.map +1 -0
  18. package/dist/font.d.ts +3 -0
  19. package/dist/font.d.ts.map +1 -0
  20. package/dist/font.js +2 -0
  21. package/dist/font.js.map +1 -0
  22. package/dist/head.d.ts +21 -0
  23. package/dist/head.d.ts.map +1 -0
  24. package/dist/head.jsx +15 -0
  25. package/dist/head.jsx.map +1 -0
  26. package/dist/image.d.ts +3 -0
  27. package/dist/image.d.ts.map +1 -0
  28. package/dist/image.js +2 -0
  29. package/dist/image.js.map +1 -0
  30. package/dist/index.d.ts +79 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +12 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/link.d.ts +10 -0
  35. package/dist/link.d.ts.map +1 -0
  36. package/dist/link.jsx +6 -0
  37. package/dist/link.jsx.map +1 -0
  38. package/dist/pdf.d.ts +3 -0
  39. package/dist/pdf.d.ts.map +1 -0
  40. package/dist/pdf.js +2 -0
  41. package/dist/pdf.js.map +1 -0
  42. package/dist/router.d.ts +30 -0
  43. package/dist/router.d.ts.map +1 -0
  44. package/dist/router.js +29 -0
  45. package/dist/router.js.map +1 -0
  46. package/dist/smoke.test.d.ts +2 -0
  47. package/dist/smoke.test.d.ts.map +1 -0
  48. package/dist/smoke.test.js +6 -0
  49. package/dist/smoke.test.js.map +1 -0
  50. package/dist/video.d.ts +4 -0
  51. package/dist/video.d.ts.map +1 -0
  52. package/dist/video.js +3 -0
  53. package/dist/video.js.map +1 -0
  54. package/package.json +127 -0
  55. package/scripts/package-native.ts +113 -0
  56. package/scripts/postinstall.js +151 -0
@@ -0,0 +1,151 @@
1
+ import { existsSync, mkdirSync, createWriteStream, writeFileSync } from "node:fs";
2
+ import { join, dirname, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { pipeline } from "node:stream/promises";
5
+ import { createGunzip } from "node:zlib";
6
+ import { execSync } from "node:child_process";
7
+ import { createHash } from "node:crypto";
8
+ import https from "node:https";
9
+ import { readFileSync } from "node:fs";
10
+
11
+ const here = dirname(fileURLToPath(import.meta.url));
12
+ const packageRoot = resolve(here, "..");
13
+ const RELEASES_URL = process.env.SWIFT_RUST_RELEASES_URL ?? "https://github.com/swift-rust/swift-rust/releases";
14
+ const pkg = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
15
+ const VERSION = process.env.SWIFT_RUST_VERSION ?? pkg.version;
16
+
17
+ const PLATFORM_TARGETS = {
18
+ "darwin-x64": "x86_64-apple-darwin",
19
+ "darwin-arm64": "aarch64-apple-darwin",
20
+ "linux-x64": "x86_64-unknown-linux-gnu",
21
+ "linux-x64-musl": "x86_64-unknown-linux-musl",
22
+ "linux-arm64": "aarch64-unknown-linux-musl",
23
+ "win32-x64": "x86_64-pc-windows-msvc",
24
+ };
25
+
26
+ function getPlatform() {
27
+ const p = `${process.platform}-${process.arch}`;
28
+ if (p in PLATFORM_TARGETS) return p;
29
+ throw new Error(`swift-rust: unsupported platform ${p}`);
30
+ }
31
+
32
+ function binaryName() {
33
+ return process.platform === "win32" ? "swift-rust.exe" : "swift-rust";
34
+ }
35
+
36
+ function assetName(platform) {
37
+ const target = PLATFORM_TARGETS[platform];
38
+ return process.platform === "win32" ? `swift-rust-${target}.exe.zip` : `swift-rust-${target}.tar.gz`;
39
+ }
40
+
41
+ function nativeDir(platform) {
42
+ return join(packageRoot, "native", platform);
43
+ }
44
+
45
+ function getSha256(buf) {
46
+ return createHash("sha256").update(buf).digest("hex");
47
+ }
48
+
49
+ function fetch(url) {
50
+ return new Promise((resolveFetch, rejectFetch) => {
51
+ https.get(url, (res) => {
52
+ if (res.statusCode === 302 || res.statusCode === 301) {
53
+ const next = res.headers.location;
54
+ if (!next) return rejectFetch(new Error("redirect without location"));
55
+ return resolveFetch(fetch(next));
56
+ }
57
+ if (res.statusCode !== 200) {
58
+ return rejectFetch(new Error(`HTTP ${res.statusCode} fetching ${url}`));
59
+ }
60
+ const chunks = [];
61
+ res.on("data", (c) => chunks.push(c));
62
+ res.on("end", () => resolveFetch(Buffer.concat(chunks)));
63
+ res.on("error", rejectFetch);
64
+ }).on("error", rejectFetch);
65
+ });
66
+ }
67
+
68
+ function tryCargoBuildFallback(platform) {
69
+ const workspaceRoot = resolve(packageRoot, "../..");
70
+ if (!existsSync(join(workspaceRoot, "Cargo.toml"))) {
71
+ return false;
72
+ }
73
+ console.log(`swift-rust: no prebuilt binary for ${platform}, attempting cargo build from ${workspaceRoot}...`);
74
+ try {
75
+ execSync("cargo build --release -p swift-rust --bin swift-rust", {
76
+ stdio: "inherit",
77
+ cwd: workspaceRoot,
78
+ });
79
+ } catch {
80
+ return false;
81
+ }
82
+ const built = join(workspaceRoot, "target", "release", binaryName());
83
+ if (!existsSync(built)) return false;
84
+ const target = join(nativeDir(platform), binaryName());
85
+ mkdirSync(dirname(target), { recursive: true });
86
+ try {
87
+ execSync(`cp ${built} ${target}`);
88
+ if (process.platform !== "win32") {
89
+ execSync(`chmod +x ${target}`);
90
+ }
91
+ console.log(`swift-rust: installed ${target} (from cargo build)`);
92
+ return true;
93
+ } catch (e) {
94
+ console.warn(`swift-rust: failed to install binary: ${e.message}`);
95
+ return false;
96
+ }
97
+ }
98
+
99
+ export async function install() {
100
+ if (process.env.SWIFT_RUST_SKIP_POSTINSTALL === "1") {
101
+ return;
102
+ }
103
+ if (process.env.npm_config_local_prefix && existsSync(resolve(packageRoot, "../../Cargo.toml"))) {
104
+ return;
105
+ }
106
+ const platform = getPlatform();
107
+ const dir = nativeDir(platform);
108
+ const target = join(dir, binaryName());
109
+ if (existsSync(target)) return;
110
+
111
+ mkdirSync(dir, { recursive: true });
112
+ const url = `${RELEASES_URL}/download/v${VERSION}/${assetName(platform)}`;
113
+ console.log(`swift-rust: downloading ${url}`);
114
+
115
+ let archive;
116
+ try {
117
+ archive = await fetch(url);
118
+ } catch (e) {
119
+ if (tryCargoBuildFallback(platform)) return;
120
+ console.warn(`swift-rust: postinstall could not fetch binary: ${e.message}`);
121
+ console.warn(`swift-rust: the bin will try again on first run, or you can build with: cargo build --release -p swift-rust`);
122
+ return;
123
+ }
124
+
125
+ const expectedSha = process.env.SWIFT_RUST_EXPECTED_SHA256;
126
+ if (expectedSha && getSha256(archive) !== expectedSha) {
127
+ throw new Error("swift-rust: downloaded archive hash mismatch");
128
+ }
129
+
130
+ if (archive[0] === 0x1f && archive[1] === 0x8b) {
131
+ const out = join(dir, "extract");
132
+ mkdirSync(out, { recursive: true });
133
+ await pipeline(Buffer.from(archive), createGunzip(), createWriteStream(join(out, "asset.tar")));
134
+ execSync(`tar -xf ${join(out, "asset.tar")} -C ${out}`);
135
+ execSync(`mv ${join(out, "swift-rust")} ${target}`);
136
+ } else {
137
+ writeFileSync(target, archive);
138
+ }
139
+
140
+ if (process.platform !== "win32") {
141
+ execSync(`chmod +x ${target}`);
142
+ }
143
+ console.log(`swift-rust: installed ${target}`);
144
+ }
145
+
146
+ if (import.meta.main) {
147
+ install().catch((e) => {
148
+ console.error(e);
149
+ process.exit(1);
150
+ });
151
+ }