meta-messenger.js 0.0.1 → 0.0.2
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 +660 -660
- package/README.md +174 -166
- package/package.json +6 -3
- package/scripts/build-go.mjs +36 -30
- package/scripts/detect-platform.mjs +41 -32
- package/scripts/download-prebuilt.mjs +126 -88
- package/scripts/package.mjs +11 -6
- package/scripts/postinstall.mjs +109 -73
- package/dist/index.d.ts +0 -1052
- package/dist/index.js +0 -1098
- package/dist/index.js.map +0 -1
|
@@ -1,88 +1,126 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
/*
|
|
2
|
+
* meta-messenger.js, Unofficial Meta Messenger Chat API for NodeJS
|
|
3
|
+
* Copyright (c) 2026 Elysia and contributors
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createWriteStream } from "node:fs";
|
|
7
|
+
import { mkdir, rename, unlink } from "node:fs/promises";
|
|
8
|
+
import https from "node:https";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
|
|
12
|
+
import { detectPlatform } from "./detect-platform.mjs";
|
|
13
|
+
import { packageJson as pkg } from "./package.mjs";
|
|
14
|
+
|
|
15
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
function defaultRepoSlug() {
|
|
18
|
+
const repo = pkg.repository;
|
|
19
|
+
if (typeof repo === "string") {
|
|
20
|
+
const m = repo.match(/github:(.+)/i);
|
|
21
|
+
if (m) return m[1];
|
|
22
|
+
if (/^[\w-]+\/[\w.-]+$/.test(repo)) return repo;
|
|
23
|
+
} else if (repo && typeof repo === "object" && repo.url) {
|
|
24
|
+
const m = repo.url.match(/github\.com[:/]+([^#]+?)(?:\.git)?$/i);
|
|
25
|
+
if (m) return m[1];
|
|
26
|
+
}
|
|
27
|
+
return `${pkg.author}/${pkg.name}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildBaseURL() {
|
|
31
|
+
const repo = defaultRepoSlug();
|
|
32
|
+
const versionTag = `v${pkg.version}`;
|
|
33
|
+
const baseURL = `https://github.com/${repo}/releases/download/${versionTag}`;
|
|
34
|
+
return baseURL.replace(/\/$/, "");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function httpGet(url, redirectCount = 0) {
|
|
38
|
+
console.log(`[${pkg.name}] HTTP GET: ${url}${redirectCount > 0 ? ` (redirect #${redirectCount})` : ""}`);
|
|
39
|
+
const reqStart = Date.now();
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
https
|
|
42
|
+
.get(url, res => {
|
|
43
|
+
console.log(`[${pkg.name}] Response: HTTP ${res.statusCode} in ${Date.now() - reqStart}ms`);
|
|
44
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
45
|
+
console.log(`[${pkg.name}] Following redirect to: ${res.headers.location}`);
|
|
46
|
+
return resolve(httpGet(res.headers.location, redirectCount + 1));
|
|
47
|
+
}
|
|
48
|
+
if (res.statusCode !== 200) {
|
|
49
|
+
return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
|
|
50
|
+
}
|
|
51
|
+
resolve(res);
|
|
52
|
+
})
|
|
53
|
+
.on("error", err => {
|
|
54
|
+
console.log(`[${pkg.name}] HTTP error after ${Date.now() - reqStart}ms:`, err?.message);
|
|
55
|
+
reject(err);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function downloadTo(url, dstPath) {
|
|
61
|
+
console.log(`[${pkg.name}] Downloading to: ${dstPath}`);
|
|
62
|
+
await mkdir(dirname(dstPath), { recursive: true });
|
|
63
|
+
const tmp = `${dstPath}.download`;
|
|
64
|
+
try {
|
|
65
|
+
await unlink(tmp);
|
|
66
|
+
} catch {
|
|
67
|
+
//
|
|
68
|
+
}
|
|
69
|
+
const res = await httpGet(url);
|
|
70
|
+
console.log(`[${pkg.name}] Starting file write...`);
|
|
71
|
+
const writeStart = Date.now();
|
|
72
|
+
let bytesWritten = 0;
|
|
73
|
+
await new Promise((resolve, reject) => {
|
|
74
|
+
const out = createWriteStream(tmp);
|
|
75
|
+
res.on("data", chunk => {
|
|
76
|
+
bytesWritten += chunk.length;
|
|
77
|
+
});
|
|
78
|
+
res.pipe(out);
|
|
79
|
+
res.on("error", reject);
|
|
80
|
+
out.on("error", reject);
|
|
81
|
+
out.on("finish", () => {
|
|
82
|
+
console.log(`[${pkg.name}] File write completed: ${bytesWritten} bytes in ${Date.now() - writeStart}ms`);
|
|
83
|
+
// Destroy the response stream to release the socket
|
|
84
|
+
res.destroy();
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
await rename(tmp, dstPath);
|
|
89
|
+
console.log(`[${pkg.name}] File renamed to final destination`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function downloadPrebuilt() {
|
|
93
|
+
console.log(`[${pkg.name}] downloadPrebuilt() started`);
|
|
94
|
+
const { triplet, ext } = detectPlatform();
|
|
95
|
+
const baseURL = buildBaseURL();
|
|
96
|
+
const filename = `messagix-${triplet}.${ext}`;
|
|
97
|
+
const url = `${baseURL}/${filename}`;
|
|
98
|
+
console.log(`[${pkg.name}] Target URL: ${url}`);
|
|
99
|
+
|
|
100
|
+
const out = join(__dirname, "..", "build", `messagix.${ext}`);
|
|
101
|
+
const totalStart = Date.now();
|
|
102
|
+
try {
|
|
103
|
+
await downloadTo(url, out);
|
|
104
|
+
console.log(`[${pkg.name}] Downloaded prebuilt from ${url} in ${Date.now() - totalStart}ms`);
|
|
105
|
+
return true;
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.warn(
|
|
108
|
+
`[${pkg.name}] No remote prebuilt found at ${url} (after ${Date.now() - totalStart}ms):`,
|
|
109
|
+
err?.message || String(err),
|
|
110
|
+
);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// node scripts/download-prebuilt.mjs
|
|
116
|
+
const isMain = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
|
117
|
+
if (isMain) {
|
|
118
|
+
downloadPrebuilt()
|
|
119
|
+
.then(ok => {
|
|
120
|
+
if (!ok) process.exit(1);
|
|
121
|
+
})
|
|
122
|
+
.catch(err => {
|
|
123
|
+
console.error(`[${pkg.name}] download-prebuilt failed:`, err?.message || String(err));
|
|
124
|
+
process.exit(1);
|
|
125
|
+
});
|
|
126
|
+
}
|
package/scripts/package.mjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/*
|
|
2
|
+
* meta-messenger.js, Unofficial Meta Messenger Chat API for NodeJS
|
|
3
|
+
* Copyright (c) 2026 Elysia and contributors
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
export const packageJson = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf-8"));
|
package/scripts/postinstall.mjs
CHANGED
|
@@ -1,73 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
console.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
1
|
+
/*
|
|
2
|
+
* meta-messenger.js, Unofficial Meta Messenger Chat API for NodeJS
|
|
3
|
+
* Copyright (c) 2026 Elysia and contributors
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { copyFile, mkdir, rm } from "node:fs/promises";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
|
|
12
|
+
import { detectPlatform } from "./detect-platform.mjs";
|
|
13
|
+
import { downloadPrebuilt } from "./download-prebuilt.mjs";
|
|
14
|
+
import { packageJson as pkg } from "./package.mjs";
|
|
15
|
+
|
|
16
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
|
|
18
|
+
async function copyIfExists(src, dst) {
|
|
19
|
+
try {
|
|
20
|
+
await mkdir(dirname(dst), { recursive: true });
|
|
21
|
+
await copyFile(src, dst);
|
|
22
|
+
return true;
|
|
23
|
+
} catch (err) {
|
|
24
|
+
if (err?.code === "ENOENT") return false;
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function run() {
|
|
30
|
+
console.log(`[${pkg.name}] postinstall started`);
|
|
31
|
+
const startTime = Date.now();
|
|
32
|
+
|
|
33
|
+
if (process.env.MESSAGIX_SKIP_POSTINSTALL === "true") {
|
|
34
|
+
console.log(`[${pkg.name}] Skipping postinstall (MESSAGIX_SKIP_POSTINSTALL=true)`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(`[${pkg.name}] Detecting platform...`);
|
|
39
|
+
const { triplet, ext } = detectPlatform();
|
|
40
|
+
console.log(`[${pkg.name}] Platform: ${triplet}, ext: ${ext}`);
|
|
41
|
+
|
|
42
|
+
const buildOut = join(__dirname, "..", "build", `messagix.${ext}`);
|
|
43
|
+
console.log(`[${pkg.name}] Build output path: ${buildOut}`);
|
|
44
|
+
|
|
45
|
+
// 1) Prefer local prebuilt shipped in npm tarball
|
|
46
|
+
const prebuiltDir = join(__dirname, "..", "prebuilt", triplet);
|
|
47
|
+
const prebuilt = join(prebuiltDir, `messagix.${ext}`);
|
|
48
|
+
console.log(`[${pkg.name}] Checking local prebuilt at: ${prebuilt}`);
|
|
49
|
+
if (await copyIfExists(prebuilt, buildOut)) {
|
|
50
|
+
console.log(`[${pkg.name}] Using local prebuilt for ${triplet}`);
|
|
51
|
+
if (process.env.MESSAGIX_KEEP_PREBUILT !== "true") {
|
|
52
|
+
try {
|
|
53
|
+
await rm(prebuiltDir, { recursive: true, force: true });
|
|
54
|
+
} catch {
|
|
55
|
+
//
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
console.log(`[${pkg.name}] postinstall completed in ${Date.now() - startTime}ms`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
console.log(`[${pkg.name}] No local prebuilt found`);
|
|
62
|
+
|
|
63
|
+
// 2) Try remote prebuilt from GitHub Releases
|
|
64
|
+
console.log(`[${pkg.name}] Attempting to download remote prebuilt...`);
|
|
65
|
+
const downloadStart = Date.now();
|
|
66
|
+
try {
|
|
67
|
+
if (await downloadPrebuilt()) {
|
|
68
|
+
console.log(`[${pkg.name}] Download completed in ${Date.now() - downloadStart}ms`);
|
|
69
|
+
console.log(`[${pkg.name}] postinstall completed in ${Date.now() - startTime}ms`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.log(
|
|
74
|
+
`[${pkg.name}] Download failed after ${Date.now() - downloadStart}ms:`,
|
|
75
|
+
err?.message || String(err),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 3) No prebuilt available. Try local build if allowed
|
|
80
|
+
if (process.env.MESSAGIX_BUILD_FROM_SOURCE === "true") {
|
|
81
|
+
console.log(`[${pkg.name}] No prebuilt found. Attempting local build...`);
|
|
82
|
+
try {
|
|
83
|
+
const res = spawnSync(process.platform === "win32" ? "npm.cmd" : "npm", ["run", "build:go"], {
|
|
84
|
+
cwd: join(__dirname, ".."),
|
|
85
|
+
stdio: "inherit",
|
|
86
|
+
env: process.env,
|
|
87
|
+
});
|
|
88
|
+
if (res.status === 0 && existsSync(buildOut)) return;
|
|
89
|
+
console.warn(`[${pkg.name}] Local build did not produce the native library.`);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.warn(`[${pkg.name}] Local build failed:`, err?.message || String(err));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
console.warn(`[${pkg.name}] No prebuilt found (local/remote) and no local build completed.`);
|
|
96
|
+
console.warn(`[${pkg.name}] Expected triplet: ${triplet}, file: messagix-${triplet}.${ext}`);
|
|
97
|
+
console.warn(
|
|
98
|
+
`[${pkg.name}] You can:\n` +
|
|
99
|
+
" - set MESSAGIX_BUILD_FROM_SOURCE=true and re-run install\n" +
|
|
100
|
+
" - or build manually with: npm run build:go",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
run()
|
|
105
|
+
.then(() => process.exit(0))
|
|
106
|
+
.catch(err => {
|
|
107
|
+
console.error(`[${pkg.name}] postinstall failed:`, err?.message || String(err));
|
|
108
|
+
process.exit(1);
|
|
109
|
+
});
|