u-wave-announce 0.5.2 → 0.6.0-alpha.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/package.json CHANGED
@@ -1,24 +1,25 @@
1
1
  {
2
2
  "name": "u-wave-announce",
3
3
  "description": "Announce your üWave server's existence to the world.",
4
- "version": "0.5.2",
4
+ "version": "0.6.0-alpha.1",
5
5
  "author": "Renée Kooi <renee@kooi.me>",
6
+ "type": "module",
6
7
  "dependencies": {
7
- "debug": "^4.3.2",
8
8
  "libsodium-wrappers": "^0.7.8",
9
- "node-fetch": "^2.6.0",
10
- "strip-indent": "^3.0.0"
9
+ "node-fetch": "^3.3.1",
10
+ "strip-indent": "^4.0.0"
11
11
  },
12
12
  "engines": {
13
- "node": ">= 12"
13
+ "node": ">= 14.18.0"
14
14
  },
15
15
  "license": "MIT",
16
16
  "main": "./src/plugin.js",
17
+ "exports": {
18
+ ".": "./src/plugin.js",
19
+ "./package.json": "./package.json"
20
+ },
17
21
  "repository": {
18
22
  "git": "https://github.com/u-wave/hub.git",
19
23
  "directory": "plugin"
20
- },
21
- "scripts": {
22
- "test": "node test"
23
24
  }
24
25
  }
package/src/plugin.js CHANGED
@@ -1,10 +1,8 @@
1
- const { promisify } = require('util');
2
- const randomBytes = promisify(require('crypto').randomBytes);
3
- const fetch = require('node-fetch');
4
- const stripIndent = require('strip-indent');
5
- const debug = require('debug')('uwave:announce');
6
- const sodium = require('./signatures');
7
- const pkg = require('../package.json');
1
+ import { randomBytes } from 'node:crypto';
2
+ import fetch from 'node-fetch';
3
+ import stripIndent from 'strip-indent';
4
+ import pkg from '../package.json' with { type: 'json' };
5
+ import * as sodium from './signatures.js';
8
6
 
9
7
  const optionsSchema = {
10
8
  type: 'object',
@@ -91,28 +89,14 @@ function stripSlashes(url) {
91
89
  async function getAnnounceData(uw, options) {
92
90
  const url = stripSlashes(options.url);
93
91
 
94
- // TODO add something to üWave Core so we don't have to manually populate
95
- // the relationships.
96
92
  const entry = await uw.booth.getCurrentEntry();
97
- if (entry) {
98
- if (entry.execPopulate) {
99
- entry.populate('user media.media');
100
- await entry.execPopulate();
101
- } else {
102
- await entry.populate('user media.media');
103
- }
104
- }
105
93
 
106
94
  // TODO add something to üWave Core so we don't have to manually ask Redis for
107
95
  // this information. Currently üWave Core may register duplicates in this
108
96
  // list, too, which is a bit annoying!
109
97
  // TODO add guest users here too.
110
98
  const onlineUserIDs = await uw.redis.lrange('users', 0, -1);
111
- const onlineUsersMap = {};
112
- onlineUserIDs.forEach((id) => {
113
- onlineUsersMap[id] = true;
114
- });
115
- const usersCount = Object.keys(onlineUsersMap).length;
99
+ const usersCount = new Set(onlineUserIDs).size;
116
100
 
117
101
  return {
118
102
  name: options.name,
@@ -142,7 +126,7 @@ async function getAnnounceData(uw, options) {
142
126
  async function getOrGenerateSeed(uw) {
143
127
  const options = await uw.config.get(optionsSchema['uw:key']);
144
128
  if (!options.seed) {
145
- options.seed = (await randomBytes(32)).toString('hex');
129
+ options.seed = randomBytes(32).toString('hex');
146
130
  await uw.config.set(optionsSchema['uw:key'], options);
147
131
  }
148
132
  return Buffer.from(options.seed, 'hex');
@@ -151,6 +135,8 @@ async function getOrGenerateSeed(uw) {
151
135
  async function announcePlugin(uw, staticOptions) {
152
136
  uw.config.register(optionsSchema['uw:key'], optionsSchema);
153
137
 
138
+ const logger = uw.logger.child({ ns: 'uwave:announce' });
139
+
154
140
  const seed = staticOptions.seed || await getOrGenerateSeed(uw);
155
141
  // This takes up to a few 100 ms but it is a one-time startup cost…
156
142
  // Maybe it makes sense to cache this, or to not block the rest of
@@ -160,17 +146,17 @@ async function announcePlugin(uw, staticOptions) {
160
146
  async function announce() {
161
147
  const options = await uw.config.get(optionsSchema['uw:key']);
162
148
  if (typeof options !== 'object') {
163
- debug('announcing not configured, skipping');
149
+ logger.debug('announcing not configured, skipping');
164
150
  return;
165
151
  }
166
152
  if (!options.enabled) {
167
- debug('announcing disabled, skipping');
153
+ logger.debug('announcing disabled, skipping');
168
154
  return;
169
155
  }
170
156
 
171
157
  const hubHost = options.hub || 'https://announce.u-wave.net';
172
158
  const announceUrl = `${stripSlashes(hubHost)}/announce/${Buffer.from(publicKey).toString('hex')}`;
173
- debug('announcing to', announceUrl);
159
+ logger.info('announcing to', announceUrl);
174
160
 
175
161
  const announcement = await getAnnounceData(uw, options);
176
162
  const data = JSON.stringify(announcement);
@@ -189,8 +175,8 @@ async function announcePlugin(uw, staticOptions) {
189
175
  });
190
176
  }
191
177
 
192
- function onError(err) {
193
- debug(err);
178
+ function onError(error) {
179
+ logger.error({ err: error });
194
180
  }
195
181
 
196
182
  let interval;
@@ -216,4 +202,4 @@ async function announcePlugin(uw, staticOptions) {
216
202
  });
217
203
  }
218
204
 
219
- module.exports = announcePlugin;
205
+ export default announcePlugin;
package/src/signatures.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Based on https://github.com/mafintosh/sodium-signatures/blob/master/index.js
2
- const sodium = require('libsodium-wrappers');
2
+ import sodium from 'libsodium-wrappers';
3
3
 
4
- async function keyPair(seed) {
4
+ export async function keyPair(seed) {
5
5
  await sodium.ready;
6
6
 
7
7
  const { publicKey, privateKey } = seed
@@ -14,18 +14,12 @@ async function keyPair(seed) {
14
14
  return { publicKey, secretKey };
15
15
  }
16
16
 
17
- async function sign(message, secretKey) {
17
+ export async function sign(message, secretKey) {
18
18
  await sodium.ready;
19
19
  return sodium.crypto_sign_detached(message, secretKey);
20
20
  }
21
21
 
22
- async function verify(message, signature, publicKey) {
22
+ export async function verify(message, signature, publicKey) {
23
23
  await sodium.ready;
24
24
  return sodium.crypto_sign_verify_detached(signature, message, publicKey);
25
25
  }
26
-
27
- module.exports = {
28
- keyPair,
29
- sign,
30
- verify,
31
- };
package/test.js CHANGED
@@ -1,7 +1,5 @@
1
- const assert = require('assert');
1
+ import assert from 'node:assert';
2
+ import plugin from './src/plugin.js';
2
3
 
3
- assert.doesNotThrow(() => {
4
- // Just to make sure it does not crash :)
5
- // eslint-disable-next-line global-require
6
- require('./src/plugin');
7
- });
4
+ // Just to make sure it does not crash :)
5
+ assert.ok(plugin);