hopwire 0.1.1 → 0.1.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/dist/commands/rotate.js +12 -2
- package/dist/inspector-api.js +7 -2
- package/dist/log.js +61 -29
- package/package.json +1 -1
package/dist/commands/rotate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { rename, rm } from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { isCapturedExchange } from '@hopwire/protocol';
|
|
3
4
|
import { storeError } from '../errors.js';
|
|
4
5
|
import { replaceIdentity } from '../identity.js';
|
|
5
6
|
import { ExchangeLog } from '../log.js';
|
|
@@ -27,11 +28,20 @@ export async function rotate(options) {
|
|
|
27
28
|
await rm(retired, { recursive: true, force: true });
|
|
28
29
|
const copy = await ExchangeLog.open(next);
|
|
29
30
|
try {
|
|
31
|
+
// Block by block as stored: blocks a newer CLI wrote (WebSocket messages, spec decision 135) are kept,
|
|
32
|
+
// only the exchanges are counted. A block that cannot be decoded is not carried over
|
|
33
|
+
length = 0;
|
|
30
34
|
for (let index = 0; index < old.length; index++) {
|
|
31
|
-
|
|
35
|
+
const block = await old.block(index);
|
|
36
|
+
if (block === null || block === undefined) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
await copy.appendBlock(block);
|
|
40
|
+
if (isCapturedExchange(block)) {
|
|
41
|
+
length += 1;
|
|
42
|
+
}
|
|
32
43
|
}
|
|
33
44
|
logKey = copy.key;
|
|
34
|
-
length = copy.length;
|
|
35
45
|
}
|
|
36
46
|
finally {
|
|
37
47
|
await copy.close();
|
package/dist/inspector-api.js
CHANGED
|
@@ -202,8 +202,8 @@ async function newestBelow(source, end, limit, bodyBudgetBytes) {
|
|
|
202
202
|
return records;
|
|
203
203
|
}
|
|
204
204
|
/**
|
|
205
|
-
* Reads the blocks from `top - 1` down to `bottom` of one log at once, newest first. A block
|
|
206
|
-
* that cannot be read is null; one that did not arrive (the teammate is offline) is waited for once per reader, not
|
|
205
|
+
* Reads the blocks from `top - 1` down to `bottom` of one log at once, newest first. A block that is no captured
|
|
206
|
+
* exchange, and a block of a teammate's log that cannot be read, is null; one that did not arrive (the teammate is offline) is waited for once per reader, not
|
|
207
207
|
* per batch.
|
|
208
208
|
*/
|
|
209
209
|
function batchReader({ origin, log, }) {
|
|
@@ -211,7 +211,12 @@ function batchReader({ origin, log, }) {
|
|
|
211
211
|
return (top, bottom) => {
|
|
212
212
|
const patient = wait;
|
|
213
213
|
return Promise.all(Array.from({ length: top - bottom }, (_, offset) => top - 1 - offset).map((index) => log.get(index, { wait: patient }).then((exchange) => ({ index, origin, exchange }), (error) => {
|
|
214
|
+
// A block that is no exchange is left out of either log: in the own one a newer CLI may have
|
|
215
|
+
// written it (spec decision 135)
|
|
214
216
|
if (origin !== 'remote') {
|
|
217
|
+
if (error instanceof InvalidBlockError) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
215
220
|
throw error;
|
|
216
221
|
}
|
|
217
222
|
// Left out; a block that is not a captured exchange says nothing about the ones still to come
|
package/dist/log.js
CHANGED
|
@@ -16,7 +16,10 @@ export const REMOTE_LOG_BUDGET_BYTES = 1024 * 1024 * 1024;
|
|
|
16
16
|
const BLOCK_OVERHEAD_BYTES = 1024;
|
|
17
17
|
/** How many of the newest blocks of a teammate's log a lookup by id walks through at most. */
|
|
18
18
|
const REMOTE_LOOKUP_BLOCKS = 100_000;
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* A block that is not a captured exchange. In a teammate's log only a changed client writes one; in this machine's
|
|
21
|
+
* own log a newer CLI that ran on the same data directory may have (WebSocket messages, spec decision 135).
|
|
22
|
+
*/
|
|
20
23
|
export class InvalidBlockError extends Error {
|
|
21
24
|
index;
|
|
22
25
|
constructor(index) {
|
|
@@ -28,9 +31,11 @@ export class InvalidBlockError extends Error {
|
|
|
28
31
|
* The traffic log: an append-only Hypercore holding one CapturedExchange per
|
|
29
32
|
* block. The core lives in a Corestore under a fixed name, so the same storage
|
|
30
33
|
* directory always reopens the same log (and the same key). A teammate's log
|
|
31
|
-
* is opened by its key in the same Corestore and can only be read
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
+
* is opened by its key in the same Corestore and can only be read. Every
|
|
35
|
+
* block is checked before it is used, in both logs, and a block that is not a
|
|
36
|
+
* captured exchange is left out everywhere: in a teammate's log another
|
|
37
|
+
* machine wrote it, and in this machine's own log a newer CLI may have written
|
|
38
|
+
* blocks of a kind this one does not know.
|
|
34
39
|
*/
|
|
35
40
|
export class ExchangeLog {
|
|
36
41
|
store;
|
|
@@ -52,7 +57,7 @@ export class ExchangeLog {
|
|
|
52
57
|
unread = new Set();
|
|
53
58
|
/** Lookups extend the id index one after the other; two at once would mix up the cursors */
|
|
54
59
|
indexing = Promise.resolve();
|
|
55
|
-
/** Blocks
|
|
60
|
+
/** Blocks found not to be captured exchanges; a teammate's are not fetched again */
|
|
56
61
|
invalid = new Set();
|
|
57
62
|
/** What `follow` fetches ahead; null when it does not */
|
|
58
63
|
following = null;
|
|
@@ -87,7 +92,7 @@ export class ExchangeLog {
|
|
|
87
92
|
constructor(
|
|
88
93
|
/** The Corestore behind the log. Replicating it replicates every log opened in it. */
|
|
89
94
|
store,
|
|
90
|
-
/** Typed as the log's content;
|
|
95
|
+
/** Typed as the log's content; that only holds after `isCapturedExchange` */
|
|
91
96
|
core, ownsStore, limits = {}) {
|
|
92
97
|
this.store = store;
|
|
93
98
|
this.core = core;
|
|
@@ -157,22 +162,22 @@ export class ExchangeLog {
|
|
|
157
162
|
}
|
|
158
163
|
}
|
|
159
164
|
/**
|
|
160
|
-
* Reads one block.
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* not
|
|
164
|
-
* machine rejects at once.
|
|
165
|
+
* Reads one block. It rejects with InvalidBlockError when the block is
|
|
166
|
+
* not a captured exchange. A block of a teammate's log that was not
|
|
167
|
+
* replicated yet is waited for a few seconds; the read rejects when it
|
|
168
|
+
* does not arrive (the teammate is offline). With `wait: false` a block
|
|
169
|
+
* that is not on this machine rejects at once.
|
|
165
170
|
*/
|
|
166
171
|
async get(index, options = {}) {
|
|
167
|
-
if (this.core.writable) {
|
|
168
|
-
return this.core.get(index);
|
|
169
|
-
}
|
|
170
172
|
if (this.invalid.has(index)) {
|
|
171
|
-
// Not
|
|
173
|
+
// Not read again: it was looked at and left out
|
|
172
174
|
throw new InvalidBlockError(index);
|
|
173
175
|
}
|
|
174
176
|
let block;
|
|
175
|
-
if (
|
|
177
|
+
if (this.core.writable) {
|
|
178
|
+
block = await this.core.get(index);
|
|
179
|
+
}
|
|
180
|
+
else if (options.wait === false) {
|
|
176
181
|
block = (await this.core.has(index)) ? await this.core.get(index, { wait: false }) : null;
|
|
177
182
|
if (block === null) {
|
|
178
183
|
throw new Error(`Block ${index} of the log is not on this machine`);
|
|
@@ -187,6 +192,27 @@ export class ExchangeLog {
|
|
|
187
192
|
}
|
|
188
193
|
return block;
|
|
189
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Reads one block of this machine's own log as it is stored, whatever its
|
|
197
|
+
* kind: what `hopwire rotate` copies, so that blocks a newer CLI wrote
|
|
198
|
+
* survive a rotation by this one. A block that cannot be decoded is null.
|
|
199
|
+
*/
|
|
200
|
+
async block(index) {
|
|
201
|
+
if (!this.core.writable) {
|
|
202
|
+
throw new Error('Only the own log is read block by block as stored');
|
|
203
|
+
}
|
|
204
|
+
return this.core.get(index).catch((error) => {
|
|
205
|
+
if (this.closed) {
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/** Appends a block as it was read with `block`; for `hopwire rotate` only. */
|
|
212
|
+
async appendBlock(block) {
|
|
213
|
+
// The core is typed with the exchange shape; a copied block of another kind is stored as it came
|
|
214
|
+
await this.append(block);
|
|
215
|
+
}
|
|
190
216
|
/**
|
|
191
217
|
* Replicates this one log over a connection to a peer. Other logs in the
|
|
192
218
|
* same Corestore (this machine's own next to a teammate's, or the other
|
|
@@ -218,12 +244,19 @@ export class ExchangeLog {
|
|
|
218
244
|
});
|
|
219
245
|
}
|
|
220
246
|
}
|
|
221
|
-
/** The newest `limit`
|
|
247
|
+
/** The exchanges among the newest `limit` blocks, oldest first; a block that is no exchange is left out. */
|
|
222
248
|
async recent(limit) {
|
|
223
249
|
const length = this.core.length;
|
|
224
250
|
const exchanges = [];
|
|
225
251
|
for (let index = Math.max(0, length - limit); index < length; index++) {
|
|
226
|
-
|
|
252
|
+
try {
|
|
253
|
+
exchanges.push(await this.get(index));
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
if (!(error instanceof InvalidBlockError)) {
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
227
260
|
}
|
|
228
261
|
return exchanges;
|
|
229
262
|
}
|
|
@@ -288,12 +321,12 @@ export class ExchangeLog {
|
|
|
288
321
|
/** Puts the id of one block into the index. Never waits for a peer and never fails on what a block holds. */
|
|
289
322
|
async index(index) {
|
|
290
323
|
let block;
|
|
291
|
-
if (this.
|
|
292
|
-
block = await this.core.get(index);
|
|
293
|
-
}
|
|
294
|
-
else if (this.invalid.has(index)) {
|
|
324
|
+
if (this.invalid.has(index)) {
|
|
295
325
|
return;
|
|
296
326
|
}
|
|
327
|
+
else if (this.core.writable) {
|
|
328
|
+
block = await this.core.get(index);
|
|
329
|
+
}
|
|
297
330
|
else if (await this.core.has(index)) {
|
|
298
331
|
this.unread.delete(index);
|
|
299
332
|
// A block that cannot even be decoded is as unusable as one with the wrong shape
|
|
@@ -316,17 +349,16 @@ export class ExchangeLog {
|
|
|
316
349
|
}
|
|
317
350
|
}
|
|
318
351
|
/**
|
|
319
|
-
* A block
|
|
320
|
-
*
|
|
352
|
+
* A block that is no captured exchange is remembered as such and never read again. It stays on disk: in a
|
|
353
|
+
* teammate's log within the download budget (removing it with `clear` while other reads wait for blocks stalled
|
|
354
|
+
* them), in this machine's own log because a newer CLI may read it.
|
|
321
355
|
*/
|
|
322
356
|
discard(index) {
|
|
323
|
-
|
|
324
|
-
this.invalid.add(index);
|
|
325
|
-
}
|
|
357
|
+
this.invalid.add(index);
|
|
326
358
|
}
|
|
327
|
-
/**
|
|
359
|
+
/** Whether a block is used: in both logs it has to be a captured exchange. */
|
|
328
360
|
usable(block) {
|
|
329
|
-
return
|
|
361
|
+
return isCapturedExchange(block);
|
|
330
362
|
}
|
|
331
363
|
/**
|
|
332
364
|
* Calls the listener for every block appended from now on, in log order,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hopwire",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Record, share and replay the HTTP traffic of your local services: private P2P sharing with teammates, and public HTTPS URLs.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://hopwire.to",
|