@ethersphere/bee-js 13.0.1-upcoming.gd3bc2f5 → 13.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 +58 -0
- package/dist/cjs/bee.js +3 -1
- package/dist/cjs/index.js +7 -4
- package/dist/cjs/modules/rolling-feed.js +188 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee.js +3 -1
- package/dist/mjs/index.js +2 -1
- package/dist/mjs/modules/rolling-feed.js +189 -0
- package/dist/types/bee.d.ts +5 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/modules/rolling-feed.d.ts +56 -0
- package/package.json +1 -1
package/dist/mjs/bee.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Objects } from 'cafe-utility';
|
|
2
1
|
import { BatchId, PrivateKey, Reference, makeContentAddressedChunk, unmarshalContentAddressedChunk } from '@ethersphere/core-sdk';
|
|
2
|
+
import { Objects } from 'cafe-utility';
|
|
3
3
|
import { postEnvelope } from "./api/envelope.js";
|
|
4
4
|
import { rchash } from "./api/rchash.js";
|
|
5
5
|
import { makeSOCAddress, makeSingleOwnerChunk, unmarshalSingleOwnerChunk } from "./chunk/soc.js";
|
|
@@ -15,6 +15,7 @@ import { File as FileNamespace } from "./modules/file.js";
|
|
|
15
15
|
import { Grantee } from "./modules/grantee.js";
|
|
16
16
|
import { Messaging } from "./modules/messaging.js";
|
|
17
17
|
import { Pin } from "./modules/pin.js";
|
|
18
|
+
import { RollingFeed } from "./modules/rolling-feed.js";
|
|
18
19
|
import { Settlement } from "./modules/settlement.js";
|
|
19
20
|
import { Soc } from "./modules/soc.js";
|
|
20
21
|
import { Stake } from "./modules/stake.js";
|
|
@@ -87,6 +88,7 @@ export class Bee {
|
|
|
87
88
|
this.chunk = new ChunkNamespace(context);
|
|
88
89
|
this.file = new FileNamespace(context);
|
|
89
90
|
this.collection = new Collection(context);
|
|
91
|
+
this.rollingFeed = new RollingFeed(context);
|
|
90
92
|
}
|
|
91
93
|
/**
|
|
92
94
|
* Creates a Content Addressed Chunk.
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Bee } from "./bee.js";
|
|
2
2
|
export { BatchId, Bytes, ChunkBuilder, ChunkSplitter, EthAddress, FeedIndex, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Signature, Span, Stamper, Topic, TransactionId } from '@ethersphere/core-sdk';
|
|
3
3
|
export { MantarayNode } from "./manifest/manifest.js";
|
|
4
|
-
export {
|
|
4
|
+
export { RollingFeedReader, RollingFeedWriter } from "./modules/rolling-feed.js";
|
|
5
5
|
export * from "./types/index.js";
|
|
6
6
|
export * from "./utils/constants.js";
|
|
7
7
|
export { Duration } from "./utils/duration.js";
|
|
@@ -9,4 +9,5 @@ export * from "./utils/error.js";
|
|
|
9
9
|
export * as Utils from "./utils/expose.js";
|
|
10
10
|
export { Size } from "./utils/size.js";
|
|
11
11
|
export * from "./utils/tokens.js";
|
|
12
|
+
export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from "./version.js";
|
|
12
13
|
export { Bee };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { BatchId, keccak256, numberToUint64, Reference, Topic } from '@ethersphere/core-sdk';
|
|
2
|
+
import { BeeError, Bytes } from "../index.js";
|
|
3
|
+
import { fetchLatestFeedUpdate, probeFeed } from "../api/feed.js";
|
|
4
|
+
import { uploadSingleOwnerChunkWithWrappedChunk } from "../chunk/soc.js";
|
|
5
|
+
import { downloadFeedUpdate, downloadFeedUpdateAsCAC, updateFeedWithPayload, updateFeedWithReference } from "../feed/index.js";
|
|
6
|
+
import { makeFeedIdentifier } from "../feed/identifier.js";
|
|
7
|
+
import { BeeResponseError } from "../utils/error.js"; // RollingFeedReader.downloadPayload/downloadReference only ever fall back one period, so
|
|
8
|
+
// backfilling deeper than that serves no reader by default - see catchUp's maxBackfill param.
|
|
9
|
+
const DEFAULT_MAX_BACKFILL = 1;
|
|
10
|
+
function periodIndex(t, periodLength) {
|
|
11
|
+
if (periodLength <= 0) {
|
|
12
|
+
throw new BeeError('Period length must be greater than zero!');
|
|
13
|
+
}
|
|
14
|
+
return Math.floor(t / periodLength);
|
|
15
|
+
}
|
|
16
|
+
function topicFor(baseTopic, periodIdx) {
|
|
17
|
+
const baseTopicBytes = baseTopic.toUint8Array();
|
|
18
|
+
return new Topic(keccak256(Bytes.concat(baseTopicBytes, numberToUint64(BigInt(periodIdx), 'BE'))));
|
|
19
|
+
}
|
|
20
|
+
async function isPeriodPopulated(requestOptions, owner, topic) {
|
|
21
|
+
try {
|
|
22
|
+
await probeFeed(requestOptions, owner, topic);
|
|
23
|
+
return true;
|
|
24
|
+
} catch (e) {
|
|
25
|
+
if (e instanceof BeeResponseError) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function fetchLatestReference(requestOptions, owner, topic) {
|
|
32
|
+
const {
|
|
33
|
+
feedIndex
|
|
34
|
+
} = await probeFeed(requestOptions, owner, topic);
|
|
35
|
+
const update = await downloadFeedUpdate(requestOptions, owner, topic, feedIndex, true);
|
|
36
|
+
return {
|
|
37
|
+
reference: new Reference(update.payload.toUint8Array()),
|
|
38
|
+
feedIndex,
|
|
39
|
+
feedIndexNext: feedIndex.next()
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export class RollingFeed {
|
|
43
|
+
constructor(context) {
|
|
44
|
+
this.context = context;
|
|
45
|
+
}
|
|
46
|
+
makeWriter(baseTopic, signer, periodLength) {
|
|
47
|
+
return new RollingFeedWriter(this.context, baseTopic, signer, periodLength);
|
|
48
|
+
}
|
|
49
|
+
makeReader(baseTopic, owner, periodLength) {
|
|
50
|
+
return new RollingFeedReader(this.context, baseTopic, owner, periodLength);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class RollingFeedWriter {
|
|
54
|
+
constructor(context, baseTopic, signer, periodLength) {
|
|
55
|
+
this.context = context;
|
|
56
|
+
this.baseTopic = baseTopic;
|
|
57
|
+
this.signer = signer;
|
|
58
|
+
this.periodLength = periodLength;
|
|
59
|
+
}
|
|
60
|
+
async uploadPayload(postageBatchId, payload, options) {
|
|
61
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
62
|
+
const stamp = new BatchId(postageBatchId);
|
|
63
|
+
const {
|
|
64
|
+
currentTopic,
|
|
65
|
+
nextTopic,
|
|
66
|
+
mirrorOptions
|
|
67
|
+
} = this.currentAndNextTopics(options);
|
|
68
|
+
const [result] = await Promise.all([updateFeedWithPayload(requestOptions, this.signer, currentTopic, payload, stamp, options), updateFeedWithPayload(requestOptions, this.signer, nextTopic, payload, stamp, mirrorOptions)]);
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
async uploadReference(postageBatchId, reference, options) {
|
|
72
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
73
|
+
const stamp = new BatchId(postageBatchId);
|
|
74
|
+
const {
|
|
75
|
+
currentTopic,
|
|
76
|
+
nextTopic,
|
|
77
|
+
mirrorOptions
|
|
78
|
+
} = this.currentAndNextTopics(options);
|
|
79
|
+
const [result] = await Promise.all([updateFeedWithReference(requestOptions, this.signer, currentTopic, reference, stamp, options), updateFeedWithReference(requestOptions, this.signer, nextTopic, reference, stamp, mirrorOptions)]);
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* True unless the period right before `periodIdx` (default: current) never got mirrored
|
|
84
|
+
* forward into it, i.e. the writer went silent for at least one whole period.
|
|
85
|
+
*/
|
|
86
|
+
async isCaughtUp(periodIdx) {
|
|
87
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
88
|
+
const targetPeriod = periodIdx ?? periodIndex(Date.now() / 1000, this.periodLength);
|
|
89
|
+
const owner = this.signer.publicKey().address();
|
|
90
|
+
return isPeriodPopulated(requestOptions, owner, topicFor(this.baseTopic, targetPeriod));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Backfills up to `maxBackfill` periods strictly between the last populated one and
|
|
94
|
+
* `periodIdx` (default: current) with that period's last known payload/reference. Never
|
|
95
|
+
* writes `periodIdx` itself - keeping it populated during silence is the caller's job (a
|
|
96
|
+
* periodic `uploadPayload`/`uploadReference` heartbeat), not catchUp's. A conditional
|
|
97
|
+
* "write it if empty" would race that heartbeat: there's no compare-and-swap on a SOC
|
|
98
|
+
* address, so a stale write can still land after a concurrent fresh one and shadow it.
|
|
99
|
+
*
|
|
100
|
+
* `maxBackfill` bounds both the backward scan and the backfill itself, since scanning
|
|
101
|
+
* further back than you're willing to backfill only wastes round trips. Defaults to 1,
|
|
102
|
+
* matching the reader's one-period fallback - nothing deeper is ever read anyway. Throws
|
|
103
|
+
* if no populated period is found within that bound.
|
|
104
|
+
*/
|
|
105
|
+
async catchUp(postageBatchId, periodIdx, maxBackfill = DEFAULT_MAX_BACKFILL) {
|
|
106
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
107
|
+
const stamp = new BatchId(postageBatchId);
|
|
108
|
+
const owner = this.signer.publicKey().address();
|
|
109
|
+
const targetPeriod = periodIdx ?? periodIndex(Date.now() / 1000, this.periodLength);
|
|
110
|
+
// periods before 0 can't exist (period index is derived from Unix time), so the scan
|
|
111
|
+
// must not probe them even when the lookback window would otherwise reach that far
|
|
112
|
+
const scanFloor = Math.max(0, targetPeriod - 1 - maxBackfill);
|
|
113
|
+
let lastGoodPeriod = targetPeriod - 1;
|
|
114
|
+
while (lastGoodPeriod >= scanFloor) {
|
|
115
|
+
if (await isPeriodPopulated(requestOptions, owner, topicFor(this.baseTopic, lastGoodPeriod))) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
lastGoodPeriod--;
|
|
119
|
+
}
|
|
120
|
+
if (lastGoodPeriod < scanFloor) {
|
|
121
|
+
throw new BeeError(`No populated period found within ${maxBackfill} periods to catch up from!`);
|
|
122
|
+
}
|
|
123
|
+
if (lastGoodPeriod === targetPeriod - 1) {
|
|
124
|
+
return; // no gap - nothing to backfill
|
|
125
|
+
}
|
|
126
|
+
const sourceTopic = topicFor(this.baseTopic, lastGoodPeriod);
|
|
127
|
+
const {
|
|
128
|
+
feedIndex: sourceIndex
|
|
129
|
+
} = await probeFeed(requestOptions, owner, sourceTopic);
|
|
130
|
+
const sourceChunk = await downloadFeedUpdateAsCAC(requestOptions, owner, sourceTopic, sourceIndex);
|
|
131
|
+
const periodsToBackfill = Array.from({
|
|
132
|
+
length: targetPeriod - 1 - lastGoodPeriod
|
|
133
|
+
}, (_, i) => lastGoodPeriod + 1 + i);
|
|
134
|
+
await Promise.all(periodsToBackfill.map(async period => {
|
|
135
|
+
const identifier = makeFeedIdentifier(topicFor(this.baseTopic, period), 0);
|
|
136
|
+
return uploadSingleOwnerChunkWithWrappedChunk(requestOptions, this.signer, stamp, identifier, sourceChunk);
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
currentAndNextTopics(options) {
|
|
140
|
+
const currentPeriod = periodIndex(Date.now() / 1000, this.periodLength);
|
|
141
|
+
return {
|
|
142
|
+
currentTopic: topicFor(this.baseTopic, currentPeriod),
|
|
143
|
+
nextTopic: topicFor(this.baseTopic, currentPeriod + 1),
|
|
144
|
+
mirrorOptions: {
|
|
145
|
+
...options,
|
|
146
|
+
index: undefined
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export class RollingFeedReader {
|
|
152
|
+
constructor(context, baseTopic, owner, periodLength) {
|
|
153
|
+
this.context = context;
|
|
154
|
+
this.baseTopic = baseTopic;
|
|
155
|
+
this.owner = owner;
|
|
156
|
+
this.periodLength = periodLength;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Reads the current period's feed; falls back to the previous period once if empty,
|
|
160
|
+
* to tolerate clock skew between writer and reader.
|
|
161
|
+
*/
|
|
162
|
+
async downloadPayload(options) {
|
|
163
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
164
|
+
const currentPeriod = periodIndex(Date.now() / 1000, this.periodLength);
|
|
165
|
+
try {
|
|
166
|
+
return await fetchLatestFeedUpdate(requestOptions, this.owner, topicFor(this.baseTopic, currentPeriod), options);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
if (!(e instanceof BeeResponseError)) {
|
|
169
|
+
throw e;
|
|
170
|
+
}
|
|
171
|
+
return fetchLatestFeedUpdate(requestOptions, this.owner, topicFor(this.baseTopic, currentPeriod - 1), options);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Same as `downloadPayload`, but for a reference to data uploaded elsewhere.
|
|
176
|
+
*/
|
|
177
|
+
async downloadReference() {
|
|
178
|
+
const requestOptions = this.context.getRequestOptionsForCall();
|
|
179
|
+
const currentPeriod = periodIndex(Date.now() / 1000, this.periodLength);
|
|
180
|
+
try {
|
|
181
|
+
return await fetchLatestReference(requestOptions, this.owner, topicFor(this.baseTopic, currentPeriod));
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (!(e instanceof BeeResponseError)) {
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
return fetchLatestReference(requestOptions, this.owner, topicFor(this.baseTopic, currentPeriod - 1));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
package/dist/types/bee.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { File as FileNamespace } from './modules/file';
|
|
|
12
12
|
import { Grantee } from './modules/grantee';
|
|
13
13
|
import { Messaging } from './modules/messaging';
|
|
14
14
|
import { Pin } from './modules/pin';
|
|
15
|
+
import { RollingFeed } from './modules/rolling-feed';
|
|
15
16
|
import { Settlement } from './modules/settlement';
|
|
16
17
|
import { Soc } from './modules/soc';
|
|
17
18
|
import { Stake } from './modules/stake';
|
|
@@ -152,6 +153,10 @@ export declare class Bee {
|
|
|
152
153
|
* Collection (multi-file) operations backed by the `/bzz` endpoint.
|
|
153
154
|
*/
|
|
154
155
|
readonly collection: Collection;
|
|
156
|
+
/**
|
|
157
|
+
* Rolling feed operations.
|
|
158
|
+
*/
|
|
159
|
+
readonly rollingFeed: RollingFeed;
|
|
155
160
|
/**
|
|
156
161
|
* Creates a Content Addressed Chunk.
|
|
157
162
|
*
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { BatchId, Bytes, ChunkBuilder, ChunkSplitter, EthAddress, FeedIndex, Ide
|
|
|
3
3
|
export type { Chunk } from '@ethersphere/core-sdk';
|
|
4
4
|
export type { SingleOwnerChunk } from './chunk/soc';
|
|
5
5
|
export { MantarayNode } from './manifest/manifest';
|
|
6
|
-
export {
|
|
6
|
+
export { RollingFeedReader, RollingFeedWriter } from './modules/rolling-feed';
|
|
7
7
|
export * from './types';
|
|
8
8
|
export * from './utils/constants';
|
|
9
9
|
export { Duration } from './utils/duration';
|
|
@@ -12,6 +12,7 @@ export * as Utils from './utils/expose';
|
|
|
12
12
|
export { Size } from './utils/size';
|
|
13
13
|
export * from './utils/tokens';
|
|
14
14
|
export type { UploadProgress } from './utils/upload-progress';
|
|
15
|
+
export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './version';
|
|
15
16
|
export { Bee };
|
|
16
17
|
declare global {
|
|
17
18
|
interface Window {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { BatchId, EthAddress, PrivateKey, Reference, Topic } from '@ethersphere/core-sdk';
|
|
2
|
+
import { FeedPayloadResult, FeedReferenceResult, FeedUpdateOptions } from '../api/feed';
|
|
3
|
+
import { FeedUploadOptions } from '../feed';
|
|
4
|
+
import { UploadResult } from '../types';
|
|
5
|
+
import { BeeContext } from './context';
|
|
6
|
+
export declare class RollingFeed {
|
|
7
|
+
private readonly context;
|
|
8
|
+
constructor(context: BeeContext);
|
|
9
|
+
makeWriter(baseTopic: Topic, signer: PrivateKey, periodLength: number): RollingFeedWriter;
|
|
10
|
+
makeReader(baseTopic: Topic, owner: EthAddress, periodLength: number): RollingFeedReader;
|
|
11
|
+
}
|
|
12
|
+
export declare class RollingFeedWriter {
|
|
13
|
+
private readonly context;
|
|
14
|
+
private readonly baseTopic;
|
|
15
|
+
private readonly signer;
|
|
16
|
+
private readonly periodLength;
|
|
17
|
+
constructor(context: BeeContext, baseTopic: Topic, signer: PrivateKey, periodLength: number);
|
|
18
|
+
uploadPayload(postageBatchId: string | BatchId, payload: Uint8Array | string, options?: FeedUploadOptions): Promise<UploadResult>;
|
|
19
|
+
uploadReference(postageBatchId: string | BatchId, reference: Reference | string | Uint8Array, options?: FeedUploadOptions): Promise<UploadResult>;
|
|
20
|
+
/**
|
|
21
|
+
* True unless the period right before `periodIdx` (default: current) never got mirrored
|
|
22
|
+
* forward into it, i.e. the writer went silent for at least one whole period.
|
|
23
|
+
*/
|
|
24
|
+
isCaughtUp(periodIdx?: number): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* Backfills up to `maxBackfill` periods strictly between the last populated one and
|
|
27
|
+
* `periodIdx` (default: current) with that period's last known payload/reference. Never
|
|
28
|
+
* writes `periodIdx` itself - keeping it populated during silence is the caller's job (a
|
|
29
|
+
* periodic `uploadPayload`/`uploadReference` heartbeat), not catchUp's. A conditional
|
|
30
|
+
* "write it if empty" would race that heartbeat: there's no compare-and-swap on a SOC
|
|
31
|
+
* address, so a stale write can still land after a concurrent fresh one and shadow it.
|
|
32
|
+
*
|
|
33
|
+
* `maxBackfill` bounds both the backward scan and the backfill itself, since scanning
|
|
34
|
+
* further back than you're willing to backfill only wastes round trips. Defaults to 1,
|
|
35
|
+
* matching the reader's one-period fallback - nothing deeper is ever read anyway. Throws
|
|
36
|
+
* if no populated period is found within that bound.
|
|
37
|
+
*/
|
|
38
|
+
catchUp(postageBatchId: string | BatchId, periodIdx?: number, maxBackfill?: number): Promise<void>;
|
|
39
|
+
private currentAndNextTopics;
|
|
40
|
+
}
|
|
41
|
+
export declare class RollingFeedReader {
|
|
42
|
+
private readonly context;
|
|
43
|
+
private readonly baseTopic;
|
|
44
|
+
private readonly owner;
|
|
45
|
+
private readonly periodLength;
|
|
46
|
+
constructor(context: BeeContext, baseTopic: Topic, owner: EthAddress, periodLength: number);
|
|
47
|
+
/**
|
|
48
|
+
* Reads the current period's feed; falls back to the previous period once if empty,
|
|
49
|
+
* to tolerate clock skew between writer and reader.
|
|
50
|
+
*/
|
|
51
|
+
downloadPayload(options?: Omit<FeedUpdateOptions, 'index'>): Promise<FeedPayloadResult>;
|
|
52
|
+
/**
|
|
53
|
+
* Same as `downloadPayload`, but for a reference to data uploaded elsewhere.
|
|
54
|
+
*/
|
|
55
|
+
downloadReference(): Promise<FeedReferenceResult>;
|
|
56
|
+
}
|