@jixo/opendweb-client-sdk 0.0.0 → 0.1.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.
Binary file
Binary file
package/index.d.ts ADDED
@@ -0,0 +1,106 @@
1
+ export interface FabricEventJs {
2
+ type: 'peer-connected' | 'peer-disconnected' | 'roster-updated' | 'message' | 'path-changed'
3
+ endpointId?: string
4
+ from?: string
5
+ data?: Buffer
6
+ status?: 'direct' | 'relay' | 'unknown'
7
+ }
8
+
9
+ /* auto-generated by NAPI-RS */
10
+ /* eslint-disable */
11
+ /**
12
+ * dweb fabric:应用级组网的 Node SDK 入口。
13
+ * 工厂:Fabric.createRoot / Fabric.open / Fabric.attach;完成后调用 shutdown 释放资源。
14
+ */
15
+ export declare class Fabric {
16
+ /** 创建新 fabric(本节点成为 root,可签发邀请与撤销)。 */
17
+ static createRoot(opts: FabricOptions, secret?: SecretSeedHandle | undefined | null): Promise<Fabric>
18
+ /** 打开已有 fabric(数据目录已含名册)。 */
19
+ static open(opts: FabricOptions, secret?: SecretSeedHandle | undefined | null): Promise<Fabric>
20
+ /** 以加入者身份起步(空名册;随后调用 join 兑换邀请)。 */
21
+ static attach(opts: FabricOptions, fabricIdHex: string, secret?: SecretSeedHandle | undefined | null): Promise<Fabric>
22
+ /** 一步加入:从令牌解析 fabric_id,attach + 兑换 + 持久化名册。 */
23
+ static joinWithToken(opts: FabricOptions, token: string, secret?: SecretSeedHandle | undefined | null): Promise<Fabric>
24
+ /** 本节点 EndpointId(z-base-32,52 字符) */
25
+ get endpointId(): string
26
+ /** fabric id(hex) */
27
+ fabricIdHex(): Promise<string>
28
+ /** 当前有效成员投影 */
29
+ members(): Promise<Array<Member>>
30
+ /** 查询 EndpointId 是否为有效成员 */
31
+ isMember(endpointId: string): Promise<boolean>
32
+ /** 签发邀请令牌(root-only)。返回 dweb1. 前缀的自包含字符串。 */
33
+ invite(ttlMs: number, recipient?: string | undefined | null): Promise<string>
34
+ /** 兑换邀请令牌加入 fabric(issuer-online 单次兑换)。 */
35
+ join(token: string): Promise<void>
36
+ /** 连接成员(常规通道;双向门控 + 名册同步)。幂等(活跃连接直接成功)。 */
37
+ connect(endpointId: string): Promise<void>
38
+ /** 断开与某成员的会话 */
39
+ disconnect(endpointId: string): Promise<void>
40
+ /** 发送不透明二进制 envelope */
41
+ send(endpointId: string, data: Buffer): Promise<void>
42
+ /** 撤销成员(root-only);投影收紧并断开既有会话。 */
43
+ revoke(endpointId: string): Promise<void>
44
+ /** 设置本节点显示名 */
45
+ setDisplayName(name: string): Promise<void>
46
+ /** 查询与某成员的当前路径类型:"direct" | "relay" | "unknown" */
47
+ linkStatus(endpointId: string): Promise<string>
48
+ /**
49
+ * 显式导出身份(identity export,不含 roster)为 `dwebkey1.` 加密串。
50
+ * 交由产品自行安全保存(如账号系统加密托管——密文上云、口令在用户)。
51
+ */
52
+ exportSecretPassphrase(passphrase: string): Promise<string>
53
+ /**
54
+ * 注册事件回调(可多次调用,多个回调都会收到全部事件)。
55
+ * 事件对象见 FabricEventJs:{ type, endpointId?, from?, data?, status? }
56
+ */
57
+ on(callback: (event: FabricEventJs) => void): void
58
+ /** 优雅关闭:断开全部会话并释放网络资源。幂等。 */
59
+ shutdown(): Promise<void>
60
+ }
61
+
62
+ /**
63
+ * 受保护的身份种子句柄:内部持有 [`SecretSeed`](zeroize-on-drop),
64
+ * 被 Fabric 构造消费(take)后即失效。用于"导入 token / 产品托管解密后"
65
+ * 向 Fabric 注入身份而不经手明文 JS 字符串。内部 Arc:句柄可 clone 进
66
+ * 配置对象,take 语义全局共享(一次消费)。
67
+ */
68
+ export declare class SecretSeedHandle {
69
+ /** 派生 EndpointId(z32 展示串;不暴露种子本体)。 */
70
+ get endpointId(): string
71
+ /** 是否仍持有种子(未被消费)。 */
72
+ get available(): boolean
73
+ }
74
+
75
+ /** Fabric 构造配置 */
76
+ export interface FabricOptions {
77
+ /** 数据目录(名册持久化位置;secret 默认实现也指向此目录) */
78
+ dataDir: string
79
+ relay?: RelayOptions
80
+ /** 写入邀请令牌的 issuer 直连地址(host:port) */
81
+ advertiseAddrs?: Array<string>
82
+ }
83
+
84
+ /**
85
+ * 导入 `dwebkey1.` 身份导出串:口令派生解密,返回受保护的种子句柄。
86
+ * (不落盘;注入 Fabric 用工厂的 secret 参数。Argon2 在阻塞线程池执行。)
87
+ */
88
+ export declare function importSecret(token: string, passphrase: string): Promise<SecretSeedHandle>
89
+
90
+ /** 成员信息 */
91
+ export interface Member {
92
+ endpointId: string
93
+ displayName?: string
94
+ sinceMs: number
95
+ }
96
+
97
+ /** SDK 原生层版本 */
98
+ export declare function nativeVersion(): string
99
+
100
+ /** relay 配置 */
101
+ export interface RelayOptions {
102
+ /** "disabled" | "custom" | "n0"(默认 "n0") */
103
+ mode?: string
104
+ /** mode = "custom" 时的 relay URL 列表(自托管 docker 或其它 iroh relay) */
105
+ urls?: Array<string>
106
+ }
package/index.js CHANGED
@@ -1 +1,63 @@
1
- module.exports = {};
1
+ // 平台守卫 + 原生模块加载 + 事件解包:v0.1 darwin-arm64。
2
+ // 加载策略:把 .node 拷贝到 os.tmpdir() 下的内容寻址新路径再 require——
3
+ // 规避网络磁盘(SMB)页缓存不一致导致的 CODESIGNING "Invalid Page",
4
+ // 以及 dyld 对既有路径的坏闭包缓存(同名覆写后同字节仍加载失败)。
5
+ // 事件解包:原生层以 JSON 字符串投递事件(TSFN 对象转换在 napi 3.12 不稳定),
6
+ // 此处还原为带 Buffer 的类型化对象。
7
+ const fs = require("node:fs");
8
+ const os = require("node:os");
9
+ const path = require("node:path");
10
+ const crypto = require("node:crypto");
11
+
12
+ const PLATFORM_BINARIES = {
13
+ "darwin-arm64": "dweb.darwin-arm64.node",
14
+ "win32-x64": "dweb.win32-x64.node",
15
+ };
16
+ const SUPPORTED = `${process.platform}-${process.arch}`;
17
+ const BINARY = PLATFORM_BINARIES[SUPPORTED];
18
+
19
+ if (!BINARY) {
20
+ throw new Error(
21
+ `@jixo/opendweb-client-sdk: 当前平台 ${SUPPORTED} 暂不支持。v0.1 提供 ${Object.keys(PLATFORM_BINARIES).join(" / ")} 原生二进制。`,
22
+ );
23
+ }
24
+
25
+ const SRC = path.join(__dirname, BINARY);
26
+
27
+ function loadViaTmp() {
28
+ const buf = fs.readFileSync(SRC);
29
+ const hash = crypto.createHash("sha256").update(buf).digest("hex").slice(0, 24);
30
+ try {
31
+ // 0700 私有目录规避可预测路径的 symlink/TOCTOU 窗口
32
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "opendweb-sdk-"));
33
+ const dest = path.join(dir, `${hash}.node`);
34
+ const fd = fs.openSync(dest, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY, 0o755);
35
+ try {
36
+ fs.writeFileSync(fd, buf);
37
+ fs.fsyncSync(fd);
38
+ } finally {
39
+ fs.closeSync(fd);
40
+ }
41
+ return require(dest);
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ const Native = loadViaTmp() ?? require(SRC);
48
+
49
+ const nativeOn = Native.Fabric.prototype.on;
50
+ Native.Fabric.prototype.on = function onWrapped(callback) {
51
+ // 原生 TSFN 为 error-first 回调:(err, jsonString)
52
+ return nativeOn.call(this, (err, json) => {
53
+ if (err) return;
54
+ const ev = JSON.parse(json);
55
+ if (typeof ev.dataBase64 === "string") {
56
+ ev.data = Buffer.from(ev.dataBase64, "base64");
57
+ delete ev.dataBase64;
58
+ }
59
+ callback(ev);
60
+ });
61
+ };
62
+
63
+ module.exports = Native;
package/package.json CHANGED
@@ -1,9 +1,36 @@
1
1
  {
2
2
  "name": "@jixo/opendweb-client-sdk",
3
- "version": "0.0.0",
4
- "description": "Generated placeholder package reserved by pnpm-pub.",
3
+ "version": "0.1.0",
4
+ "description": "@jixo/opendweb-client-sdk dweb application-level fabric SDK for Node (napi-rs binding)",
5
+ "license": "MIT OR Apache-2.0",
5
6
  "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "napi": {
9
+ "binaryName": "dweb",
10
+ "targets": [
11
+ "aarch64-apple-darwin"
12
+ ]
13
+ },
14
+ "scripts": {
15
+ "build": "napi build --platform --release --target aarch64-apple-darwin --no-js && node scripts/fix-dts.mjs",
16
+ "build:debug": "napi build --platform --target aarch64-apple-darwin --no-js",
17
+ "test": "node --test --test-force-exit",
18
+ "typecheck": "tsc --noEmit",
19
+ "build:win": "PATH=\"$HOME/.cargo/bin:$PATH\" LIBNODE_PATH=\"$HOME/libnode-win\" cargo build --release --target x86_64-pc-windows-gnu -p dweb-client-sdk && cp \"$HOME/.cargo-target/dweb/x86_64-pc-windows-gnu/release/dweb_client_sdk.dll\" dweb.win32-x64.node"
20
+ },
21
+ "devDependencies": {
22
+ "@napi-rs/cli": "3.7.0",
23
+ "typescript": "^5.6.0",
24
+ "vitest": "^3.0.0"
25
+ },
6
26
  "files": [
7
- "index.js"
8
- ]
27
+ "index.js",
28
+ "index.d.ts",
29
+ "dweb.darwin-arm64.node",
30
+ "dweb.win32-x64.node"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/Gaubee/dweb.git"
35
+ }
9
36
  }