u-wave-announce 0.5.1 → 0.5.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/README.md CHANGED
@@ -5,6 +5,10 @@ server listing at https://hub.u-wave.net.
5
5
 
6
6
  ## Usage
7
7
 
8
+ The announce plugin is added by default if you use the executable provided
9
+ with üWave Core. If you are using the üWave Core Node.js API, install and
10
+ add it manually:
11
+
8
12
  ```bash
9
13
  npm install u-wave-announce
10
14
  ```
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "u-wave-announce",
3
3
  "description": "Announce your üWave server's existence to the world.",
4
- "version": "0.5.1",
4
+ "version": "0.5.2",
5
5
  "author": "Renée Kooi <renee@kooi.me>",
6
6
  "dependencies": {
7
- "debug": "^4.0.0",
8
- "find-cache-dir": "^3.0.0",
7
+ "debug": "^4.3.2",
9
8
  "libsodium-wrappers": "^0.7.8",
10
- "ms": "^2.0.0",
11
9
  "node-fetch": "^2.6.0",
12
10
  "strip-indent": "^3.0.0"
13
11
  },
@@ -19,5 +17,8 @@
19
17
  "repository": {
20
18
  "git": "https://github.com/u-wave/hub.git",
21
19
  "directory": "plugin"
20
+ },
21
+ "scripts": {
22
+ "test": "node test"
22
23
  }
23
24
  }
package/src/plugin.js CHANGED
@@ -1,10 +1,7 @@
1
- const fs = require('fs').promises;
2
1
  const { promisify } = require('util');
3
2
  const randomBytes = promisify(require('crypto').randomBytes);
4
3
  const fetch = require('node-fetch');
5
- const ms = require('ms');
6
4
  const stripIndent = require('strip-indent');
7
- const findCacheDir = require('find-cache-dir');
8
5
  const debug = require('debug')('uwave:announce');
9
6
  const sodium = require('./signatures');
10
7
  const pkg = require('../package.json');
@@ -91,36 +88,6 @@ function stripSlashes(url) {
91
88
  return url.replace(/\/+$/, '');
92
89
  }
93
90
 
94
- async function getKeyPair(seed) {
95
- const keyPairPath = findCacheDir({
96
- name: pkg.name,
97
- create: true,
98
- thunk: true,
99
- })('keypair.json');
100
- try {
101
- const { publicKey, secretKey, forSeed } = JSON.parse(
102
- await fs.readFile(keyPairPath, 'utf8'),
103
- );
104
-
105
- if (Buffer.compare(Buffer.from(forSeed), Buffer.from(seed)) !== 0) {
106
- throw new Error('this error object is unused');
107
- }
108
-
109
- return {
110
- publicKey: Buffer.from(publicKey, 'base64'),
111
- secretKey: Buffer.from(secretKey, 'base64'),
112
- };
113
- } catch (error) {
114
- const { publicKey, secretKey } = await sodium.keyPair(seed);
115
- await fs.writeFile(keyPairPath, JSON.stringify({
116
- publicKey: Buffer.from(publicKey).toString('base64'),
117
- secretKey: Buffer.from(secretKey).toString('base64'),
118
- forSeed: seed,
119
- }, null, 2), 'utf8');
120
- return { publicKey, secretKey };
121
- }
122
- }
123
-
124
91
  async function getAnnounceData(uw, options) {
125
92
  const url = stripSlashes(options.url);
126
93
 
@@ -128,8 +95,12 @@ async function getAnnounceData(uw, options) {
128
95
  // the relationships.
129
96
  const entry = await uw.booth.getCurrentEntry();
130
97
  if (entry) {
131
- entry.populate('user media.media');
132
- await entry.execPopulate();
98
+ if (entry.execPopulate) {
99
+ entry.populate('user media.media');
100
+ await entry.execPopulate();
101
+ } else {
102
+ await entry.populate('user media.media');
103
+ }
133
104
  }
134
105
 
135
106
  // TODO add something to üWave Core so we don't have to manually ask Redis for
@@ -181,7 +152,10 @@ async function announcePlugin(uw, staticOptions) {
181
152
  uw.config.register(optionsSchema['uw:key'], optionsSchema);
182
153
 
183
154
  const seed = staticOptions.seed || await getOrGenerateSeed(uw);
184
- const { publicKey, secretKey } = await getKeyPair(seed);
155
+ // This takes up to a few 100 ms but it is a one-time startup cost…
156
+ // Maybe it makes sense to cache this, or to not block the rest of
157
+ // the startup work. For now, we just do the easy thing.
158
+ const { publicKey, secretKey } = await sodium.keyPair(seed);
185
159
 
186
160
  async function announce() {
187
161
  const options = await uw.config.get(optionsSchema['uw:key']);
@@ -205,6 +179,7 @@ async function announcePlugin(uw, staticOptions) {
205
179
  await fetch(announceUrl, {
206
180
  method: 'post',
207
181
  headers: {
182
+ 'user-agent': `u-wave-announce ${pkg.version}`,
208
183
  'content-type': 'application/json',
209
184
  },
210
185
  body: JSON.stringify({
@@ -228,7 +203,7 @@ async function announcePlugin(uw, staticOptions) {
228
203
  // we're still alive.
229
204
  interval = setInterval(() => {
230
205
  announce().catch(onError);
231
- }, ms('1 minute'));
206
+ }, 60_000);
232
207
  });
233
208
 
234
209
  // Announce again every time the song changes.
package/test.js ADDED
@@ -0,0 +1,7 @@
1
+ const assert = require('assert');
2
+
3
+ assert.doesNotThrow(() => {
4
+ // Just to make sure it does not crash :)
5
+ // eslint-disable-next-line global-require
6
+ require('./src/plugin');
7
+ });