-
Notifications
You must be signed in to change notification settings - Fork 8
/
publisher.ts
225 lines (200 loc) · 6.46 KB
/
publisher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { type Endpoint, produce, type Producer, type ProducerHandler, type ProducerOptions } from "@ndn/endpoint";
import { GenericNumber } from "@ndn/naming-convention2";
import { Data, Name, type NameLike, nullSigner, type Signer } from "@ndn/packet";
import type { DataStore as S } from "@ndn/repo-api";
import { BufferChunkSource, type ChunkOptions, DataProducer } from "@ndn/segmented-object";
import type { SyncNode } from "@ndn/sync-api";
import { Encoder } from "@ndn/tlv";
import { Closer } from "@ndn/util";
import { collect, map } from "streaming-iterables";
import { ContentTypeEncap, MappingKeyword, TT, Version0 } from "./an";
import { MappingEntry } from "./mapping-entry";
import type { SvSync } from "./sync";
/** SVS-PS publisher. */
export class SvPublisher {
constructor({
endpoint, // eslint-disable-line etc/no-deprecated
pOpts,
sync,
id,
store,
chunkSize = 8000,
innerSigner = nullSigner,
outerSigner = nullSigner,
mappingSigner = nullSigner,
}: SvPublisher.Options) {
this.node = sync.add(id);
this.nodeSyncPrefix = id.append(...sync.syncPrefix.comps);
this.store = store;
this.chunkOptions = { chunkSize };
this.innerSigner = innerSigner;
this.outerSigner = outerSigner;
this.outerProducer = produce(
this.nodeSyncPrefix,
this.handleOuter,
{
...endpoint?.pOpts,
...pOpts,
describe: `SVS-PS(${id})[outer]`,
},
);
this.mappingProducer = produce(
this.nodeSyncPrefix.append(MappingKeyword),
this.handleMapping,
{
...endpoint?.pOpts,
...pOpts,
describe: `SVS-PS(${id})[mapping]`,
dataSigner: mappingSigner,
},
);
}
private readonly node: SyncNode<Name>;
private readonly nodeSyncPrefix: Name;
private readonly store: SvPublisher.DataStore;
private readonly chunkOptions: ChunkOptions;
private readonly innerSigner: Signer;
private readonly outerSigner: Signer;
private readonly outerProducer: Producer;
private readonly mappingProducer: Producer;
/** Publisher node ID. */
public get id(): Name { return this.node.id; }
/**
* Stop publisher operations.
*
* @remarks
* This does not stop the {@link SvSync} instance or the {@link SvPublisher.DataStore}.
*/
public async close(): Promise<void> {
this.outerProducer.close();
this.mappingProducer.close();
await Closer.close(this.store);
}
/**
* Publish application data.
* @param name - Application-specified inner name.
* @param payload - Application payload.
* @param entry - MappingEntry for subscriber-side filtering.
* This is required if subscribers are expecting a certain MappingEntry subclass.
* @returns seqNum.
*/
public async publish(name: NameLike, payload: Uint8Array, entry = new MappingEntry()): Promise<number> {
name = Name.from(name);
const inner = await collect(DataProducer.listData(
new BufferChunkSource(payload, this.chunkOptions),
name.append(Version0),
{ signer: this.innerSigner },
));
const finalBlockId = inner.at(-1)!.name.get(-1)!;
const seqNum = this.node.seqNum + 1;
const seqNumComp = GenericNumber.create(seqNum);
entry.seqNum = seqNum;
entry.name = name;
const mapping = new Data(this.nodeSyncPrefix.append(MappingKeyword, seqNumComp), Encoder.encode(entry));
await nullSigner.sign(mapping);
const outer = map(async (data) => {
const encap = new Data(
this.nodeSyncPrefix.append(seqNumComp, Version0, data.name.get(-1)!),
Data.ContentType(ContentTypeEncap),
Data.FreshnessPeriod(60000),
Encoder.encode(data),
);
encap.finalBlockId = finalBlockId;
await this.outerSigner.sign(encap);
return encap;
}, inner);
await this.store.insert(mapping, outer);
this.node.seqNum = seqNum;
return seqNum;
}
private readonly handleOuter: ProducerHandler = async (interest) => {
if (!interest.name.get(this.nodeSyncPrefix.length)?.is(GenericNumber)) {
return undefined;
}
return this.store.find(interest);
};
private readonly handleMapping: ProducerHandler = async ({ name }) => {
const loSeqNumComp = name.get(-2)!;
const hiSeqNumComp = name.get(-1)!;
if (name.length !== this.nodeSyncPrefix.length + 3 ||
!loSeqNumComp.is(GenericNumber) ||
!hiSeqNumComp.is(GenericNumber)) {
return undefined;
}
const loSeqNum = loSeqNumComp.as(GenericNumber);
const hiSeqNum = hiSeqNumComp.as(GenericNumber);
const recordNames: Name[] = [];
for (let i = loSeqNum; i <= hiSeqNum; ++i) {
recordNames.push(this.nodeSyncPrefix.append(MappingKeyword, GenericNumber.create(i)));
}
const entries = await Promise.all(recordNames.map(async (recordName) => {
const record = await this.store.get(recordName);
return record?.content;
}));
const payload = Encoder.encode([
TT.MappingData,
this.node.id,
...entries,
]);
return new Data(name, payload);
};
}
export namespace SvPublisher {
/* eslint-disable tsdoc/syntax -- tsdoc-missing-reference */
/**
* Data repository used by publisher.
*
* @remarks
* {@link \@ndn/repo!DataStore} satisfies the requirement.
* Other lightweight implementations may be possible.
*/
/* eslint-enable tsdoc/syntax */
export type DataStore = S.Get & S.Find & S.Insert;
export interface Options {
/**
* Endpoint for communication.
* @deprecated Specify `.pOpts`.
*/
endpoint?: Endpoint;
/**
* Producer options.
*
* @remarks
* - `.describe` is overridden as "SVS-PS" + prefix.
* - `.dataSigner` is overridden.
*/
pOpts?: ProducerOptions;
/**
* SvSync instance.
*
* @remarks
* Multiple {@link SvSubscriber}s and {@link SvPublisher}s may reuse the same SvSync instance.
* However, publications from a publisher cannot reach subscribers on the same SvSync instance.
*/
sync: SvSync;
/** Publisher node ID. */
id: Name;
/** Data repository used for this publisher. */
store: DataStore;
/**
* Segment chunk size of inner Data packet.
* @defaultValue 8000
*/
chunkSize?: number;
/**
* Inner Data signer.
* @defaultValue nullSigner
*/
innerSigner?: Signer;
/**
* Outer Data signer.
* @defaultValue nullSigner
*/
outerSigner?: Signer;
/**
* Mapping Data signer.
* @defaultValue nullSigner
*/
mappingSigner?: Signer;
}
}