@rei-standard/amsg-server 1.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.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @rei-standard/amsg-server
2
+
3
+ `@rei-standard/amsg-server` 是 ReiStandard 主动消息标准的服务端 SDK 包。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @rei-standard/amsg-server web-push
9
+
10
+ # 数据库驱动二选一
11
+ npm install @neondatabase/serverless
12
+ # 或
13
+ npm install pg
14
+ ```
15
+
16
+ ## 使用
17
+
18
+ ```js
19
+ import { createReiServer } from '@rei-standard/amsg-server';
20
+
21
+ const rei = await createReiServer({
22
+ db: {
23
+ driver: 'neon',
24
+ connectionString: process.env.DATABASE_URL
25
+ },
26
+ encryptionKey: process.env.ENCRYPTION_KEY,
27
+ cronSecret: process.env.CRON_SECRET,
28
+ initSecret: process.env.INIT_SECRET,
29
+ vapid: {
30
+ email: process.env.VAPID_EMAIL,
31
+ publicKey: process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY,
32
+ privateKey: process.env.VAPID_PRIVATE_KEY
33
+ }
34
+ });
35
+ ```
36
+
37
+ 导出的标准 handler:
38
+
39
+ - `rei.handlers.initDatabase`
40
+ - `rei.handlers.getMasterKey`
41
+ - `rei.handlers.scheduleMessage`
42
+ - `rei.handlers.sendNotifications`
43
+ - `rei.handlers.updateMessage`
44
+ - `rei.handlers.cancelMessage`
45
+ - `rei.handlers.messages`
46
+
47
+ ## 相关包
48
+
49
+ - 浏览器 SDK:`@rei-standard/amsg-client`
50
+ - Service Worker SDK:`@rei-standard/amsg-sw`
@@ -0,0 +1,83 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/server/adapters/schema.js
2
+ var TABLE_SQL = `
3
+ CREATE TABLE IF NOT EXISTS scheduled_messages (
4
+ id SERIAL PRIMARY KEY,
5
+ user_id VARCHAR(255) NOT NULL,
6
+ uuid VARCHAR(36),
7
+ encrypted_payload TEXT NOT NULL,
8
+ message_type VARCHAR(50) NOT NULL CHECK (message_type IN ('fixed', 'prompted', 'auto', 'instant')),
9
+ next_send_at TIMESTAMP WITH TIME ZONE NOT NULL,
10
+ status VARCHAR(50) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
11
+ retry_count INTEGER DEFAULT 0,
12
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
13
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
14
+ )
15
+ `;
16
+ var INDEXES = [
17
+ {
18
+ name: "idx_pending_tasks_optimized",
19
+ sql: `CREATE INDEX IF NOT EXISTS idx_pending_tasks_optimized
20
+ ON scheduled_messages (status, next_send_at, id, retry_count)
21
+ WHERE status = 'pending'`,
22
+ description: "Main query index (Cron Job finds pending tasks)"
23
+ },
24
+ {
25
+ name: "idx_cleanup_completed",
26
+ sql: `CREATE INDEX IF NOT EXISTS idx_cleanup_completed
27
+ ON scheduled_messages (status, updated_at)
28
+ WHERE status IN ('sent', 'failed')`,
29
+ description: "Cleanup query index"
30
+ },
31
+ {
32
+ name: "idx_failed_retry",
33
+ sql: `CREATE INDEX IF NOT EXISTS idx_failed_retry
34
+ ON scheduled_messages (status, retry_count, next_send_at)
35
+ WHERE status = 'failed' AND retry_count < 3`,
36
+ description: "Failed retry index"
37
+ },
38
+ {
39
+ name: "idx_user_id",
40
+ sql: `CREATE INDEX IF NOT EXISTS idx_user_id
41
+ ON scheduled_messages (user_id)`,
42
+ description: "User task query index"
43
+ },
44
+ {
45
+ name: "uidx_uuid",
46
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS uidx_uuid
47
+ ON scheduled_messages (uuid)
48
+ WHERE uuid IS NOT NULL`,
49
+ description: "UUID uniqueness guard",
50
+ critical: true
51
+ }
52
+ ];
53
+ var VERIFY_TABLE_SQL = `
54
+ SELECT table_name
55
+ FROM information_schema.tables
56
+ WHERE table_schema = 'public'
57
+ AND table_name = 'scheduled_messages'
58
+ `;
59
+ var COLUMNS_SQL = `
60
+ SELECT column_name, data_type, is_nullable
61
+ FROM information_schema.columns
62
+ WHERE table_schema = 'public'
63
+ AND table_name = 'scheduled_messages'
64
+ ORDER BY ordinal_position
65
+ `;
66
+ var REQUIRED_COLUMNS = [
67
+ "id",
68
+ "user_id",
69
+ "uuid",
70
+ "encrypted_payload",
71
+ "message_type",
72
+ "next_send_at",
73
+ "status",
74
+ "retry_count"
75
+ ];
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+ exports.TABLE_SQL = TABLE_SQL; exports.INDEXES = INDEXES; exports.VERIFY_TABLE_SQL = VERIFY_TABLE_SQL; exports.COLUMNS_SQL = COLUMNS_SQL; exports.REQUIRED_COLUMNS = REQUIRED_COLUMNS;
@@ -0,0 +1,83 @@
1
+ // src/server/adapters/schema.js
2
+ var TABLE_SQL = `
3
+ CREATE TABLE IF NOT EXISTS scheduled_messages (
4
+ id SERIAL PRIMARY KEY,
5
+ user_id VARCHAR(255) NOT NULL,
6
+ uuid VARCHAR(36),
7
+ encrypted_payload TEXT NOT NULL,
8
+ message_type VARCHAR(50) NOT NULL CHECK (message_type IN ('fixed', 'prompted', 'auto', 'instant')),
9
+ next_send_at TIMESTAMP WITH TIME ZONE NOT NULL,
10
+ status VARCHAR(50) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
11
+ retry_count INTEGER DEFAULT 0,
12
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
13
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
14
+ )
15
+ `;
16
+ var INDEXES = [
17
+ {
18
+ name: "idx_pending_tasks_optimized",
19
+ sql: `CREATE INDEX IF NOT EXISTS idx_pending_tasks_optimized
20
+ ON scheduled_messages (status, next_send_at, id, retry_count)
21
+ WHERE status = 'pending'`,
22
+ description: "Main query index (Cron Job finds pending tasks)"
23
+ },
24
+ {
25
+ name: "idx_cleanup_completed",
26
+ sql: `CREATE INDEX IF NOT EXISTS idx_cleanup_completed
27
+ ON scheduled_messages (status, updated_at)
28
+ WHERE status IN ('sent', 'failed')`,
29
+ description: "Cleanup query index"
30
+ },
31
+ {
32
+ name: "idx_failed_retry",
33
+ sql: `CREATE INDEX IF NOT EXISTS idx_failed_retry
34
+ ON scheduled_messages (status, retry_count, next_send_at)
35
+ WHERE status = 'failed' AND retry_count < 3`,
36
+ description: "Failed retry index"
37
+ },
38
+ {
39
+ name: "idx_user_id",
40
+ sql: `CREATE INDEX IF NOT EXISTS idx_user_id
41
+ ON scheduled_messages (user_id)`,
42
+ description: "User task query index"
43
+ },
44
+ {
45
+ name: "uidx_uuid",
46
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS uidx_uuid
47
+ ON scheduled_messages (uuid)
48
+ WHERE uuid IS NOT NULL`,
49
+ description: "UUID uniqueness guard",
50
+ critical: true
51
+ }
52
+ ];
53
+ var VERIFY_TABLE_SQL = `
54
+ SELECT table_name
55
+ FROM information_schema.tables
56
+ WHERE table_schema = 'public'
57
+ AND table_name = 'scheduled_messages'
58
+ `;
59
+ var COLUMNS_SQL = `
60
+ SELECT column_name, data_type, is_nullable
61
+ FROM information_schema.columns
62
+ WHERE table_schema = 'public'
63
+ AND table_name = 'scheduled_messages'
64
+ ORDER BY ordinal_position
65
+ `;
66
+ var REQUIRED_COLUMNS = [
67
+ "id",
68
+ "user_id",
69
+ "uuid",
70
+ "encrypted_payload",
71
+ "message_type",
72
+ "next_send_at",
73
+ "status",
74
+ "retry_count"
75
+ ];
76
+
77
+ export {
78
+ TABLE_SQL,
79
+ INDEXES,
80
+ VERIFY_TABLE_SQL,
81
+ COLUMNS_SQL,
82
+ REQUIRED_COLUMNS
83
+ };