@peerbit/string 2.0.3 → 3.0.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/lib/esm/query.d.ts +7 -3
- package/lib/esm/query.js +15 -9
- package/lib/esm/query.js.map +1 -1
- package/lib/esm/string-store.d.ts +21 -17
- package/lib/esm/string-store.js +50 -27
- package/lib/esm/string-store.js.map +1 -1
- package/package.json +6 -6
- package/src/query.ts +11 -7
- package/src/string-store.ts +87 -45
package/lib/esm/query.d.ts
CHANGED
|
@@ -21,17 +21,21 @@ export declare class StringMatch {
|
|
|
21
21
|
});
|
|
22
22
|
preprocess(string: string): string;
|
|
23
23
|
}
|
|
24
|
-
export declare class
|
|
24
|
+
export declare class SearchRequest {
|
|
25
25
|
query: StringMatch[];
|
|
26
26
|
constructor(properties?: {
|
|
27
27
|
query: StringMatch[];
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
export declare class
|
|
30
|
+
export declare abstract class AbstractSearchResult {
|
|
31
|
+
}
|
|
32
|
+
export declare class StringResult extends AbstractSearchResult {
|
|
31
33
|
string: string;
|
|
32
34
|
metadatas?: RangeMetadatas;
|
|
33
|
-
constructor(properties
|
|
35
|
+
constructor(properties: {
|
|
34
36
|
string: string;
|
|
35
37
|
metadatas?: RangeMetadatas;
|
|
36
38
|
});
|
|
37
39
|
}
|
|
40
|
+
export declare class NoAccess extends AbstractSearchResult {
|
|
41
|
+
}
|
package/lib/esm/query.js
CHANGED
|
@@ -73,7 +73,7 @@ StringMatch = __decorate([
|
|
|
73
73
|
variant(0),
|
|
74
74
|
__metadata("design:paramtypes", [Object])
|
|
75
75
|
], StringMatch);
|
|
76
|
-
export let
|
|
76
|
+
export let SearchRequest = class SearchRequest {
|
|
77
77
|
query;
|
|
78
78
|
constructor(properties) {
|
|
79
79
|
if (properties) {
|
|
@@ -84,20 +84,21 @@ export let StringQueryRequest = class StringQueryRequest {
|
|
|
84
84
|
__decorate([
|
|
85
85
|
field({ type: vec(StringMatch) }),
|
|
86
86
|
__metadata("design:type", Array)
|
|
87
|
-
],
|
|
88
|
-
|
|
87
|
+
], SearchRequest.prototype, "query", void 0);
|
|
88
|
+
SearchRequest = __decorate([
|
|
89
89
|
variant(0),
|
|
90
90
|
__metadata("design:paramtypes", [Object])
|
|
91
|
-
],
|
|
91
|
+
], SearchRequest);
|
|
92
92
|
/// ----- RESULTS -----
|
|
93
|
-
export
|
|
93
|
+
export class AbstractSearchResult {
|
|
94
|
+
}
|
|
95
|
+
export let StringResult = class StringResult extends AbstractSearchResult {
|
|
94
96
|
string;
|
|
95
97
|
metadatas;
|
|
96
98
|
constructor(properties) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
99
|
+
super();
|
|
100
|
+
this.string = properties.string;
|
|
101
|
+
this.metadatas = properties.metadatas;
|
|
101
102
|
}
|
|
102
103
|
};
|
|
103
104
|
__decorate([
|
|
@@ -112,4 +113,9 @@ StringResult = __decorate([
|
|
|
112
113
|
variant(0),
|
|
113
114
|
__metadata("design:paramtypes", [Object])
|
|
114
115
|
], StringResult);
|
|
116
|
+
export let NoAccess = class NoAccess extends AbstractSearchResult {
|
|
117
|
+
};
|
|
118
|
+
NoAccess = __decorate([
|
|
119
|
+
variant(1)
|
|
120
|
+
], NoAccess);
|
|
115
121
|
//# sourceMappingURL=query.js.map
|
package/lib/esm/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/query.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAGtD,WAAM,aAAa,GAAnB,MAAM,aAAa;IAEzB,MAAM,CAAS;IAGf,MAAM,CAAS;IAEf,YAAY,IAAyC;QACpD,IAAI,IAAI,EAAE;YACT,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,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{ SharedLog, SharedLogOptions } from "@peerbit/shared-log";
|
|
3
|
-
import {
|
|
4
|
-
import { Program } from "@peerbit/program";
|
|
5
|
-
import { RPCOptions,
|
|
3
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
|
+
import { Program, ProgramEvents } from "@peerbit/program";
|
|
5
|
+
import { RPCOptions, RPC, RequestContext } from "@peerbit/rpc";
|
|
6
6
|
import { StringOperation, StringIndex } from "./string-index.js";
|
|
7
|
-
import {
|
|
7
|
+
import { AbstractSearchResult, SearchRequest } from "./query.js";
|
|
8
8
|
import { Range } from "./range.js";
|
|
9
9
|
export declare const STRING_STORE_TYPE = "string_store";
|
|
10
|
-
export type
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
11
|
+
export interface StringEvents {
|
|
12
|
+
change: CustomEvent<Change<StringOperation>>;
|
|
13
|
+
}
|
|
14
|
+
type CanPerform = (operation: StringOperation, context: TransactionContext) => Promise<boolean> | boolean;
|
|
13
15
|
type Args = {
|
|
14
16
|
canRead?: CanRead;
|
|
15
|
-
|
|
17
|
+
canPerform?: CanPerform;
|
|
16
18
|
log?: SharedLogOptions;
|
|
17
19
|
};
|
|
18
|
-
export
|
|
20
|
+
export type TransactionContext = {
|
|
21
|
+
entry: Entry<StringOperation>;
|
|
22
|
+
};
|
|
23
|
+
export declare class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
19
24
|
_log: SharedLog<StringOperation>;
|
|
20
|
-
query: RPC<
|
|
25
|
+
query: RPC<SearchRequest, AbstractSearchResult>;
|
|
21
26
|
_index: StringIndex;
|
|
22
|
-
|
|
27
|
+
_canRead?: CanRead;
|
|
23
28
|
constructor(properties: {
|
|
24
29
|
id?: Uint8Array;
|
|
25
|
-
query?: RPC<
|
|
30
|
+
query?: RPC<SearchRequest, AbstractSearchResult>;
|
|
26
31
|
});
|
|
27
32
|
open(options?: Args): Promise<void>;
|
|
28
|
-
|
|
29
|
-
_canAppend(entry: Entry<StringOperation>): Promise<boolean>;
|
|
33
|
+
private _canPerform;
|
|
30
34
|
add(value: string, index: Range, options?: AppendOptions<StringOperation>): Promise<{
|
|
31
35
|
entry: Entry<StringOperation>;
|
|
32
36
|
removed: Entry<StringOperation>[];
|
|
@@ -35,11 +39,11 @@ export declare class DString extends Program {
|
|
|
35
39
|
entry: Entry<StringOperation>;
|
|
36
40
|
removed: Entry<StringOperation>[];
|
|
37
41
|
}>;
|
|
38
|
-
queryHandler(query:
|
|
42
|
+
queryHandler(query: SearchRequest, ctx: RequestContext): Promise<AbstractSearchResult | undefined>;
|
|
39
43
|
getValue(options?: {
|
|
40
44
|
remote: {
|
|
41
45
|
callback: (string: string) => any;
|
|
42
|
-
queryOptions: RPCOptions<
|
|
46
|
+
queryOptions: RPCOptions<AbstractSearchResult>;
|
|
43
47
|
};
|
|
44
48
|
}): Promise<string | undefined>;
|
|
45
49
|
}
|
package/lib/esm/string-store.js
CHANGED
|
@@ -9,11 +9,14 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
};
|
|
10
10
|
import { field, variant } from "@dao-xyz/borsh";
|
|
11
11
|
import { SharedLog } from "@peerbit/shared-log";
|
|
12
|
+
import { sha256Base64Sync } from "@peerbit/crypto";
|
|
12
13
|
import { Program } from "@peerbit/program";
|
|
13
14
|
import { RPC } from "@peerbit/rpc";
|
|
14
15
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
15
16
|
import { StringOperation, StringIndex, encoding } from "./string-index.js";
|
|
16
|
-
import { RangeMetadata, RangeMetadatas, StringMatch,
|
|
17
|
+
import { NoAccess, RangeMetadata, RangeMetadatas, StringMatch, SearchRequest, StringResult, } from "./query.js";
|
|
18
|
+
import { CustomEvent } from "@libp2p/interfaces/events";
|
|
19
|
+
import { concat, fromString } from "uint8arrays";
|
|
17
20
|
const logger = loggerFn({ module: "string" });
|
|
18
21
|
export const STRING_STORE_TYPE = "string_store";
|
|
19
22
|
const findAllOccurrences = (str, substr) => {
|
|
@@ -30,7 +33,7 @@ export let DString = class DString extends Program {
|
|
|
30
33
|
_log;
|
|
31
34
|
query;
|
|
32
35
|
_index;
|
|
33
|
-
|
|
36
|
+
_canRead;
|
|
34
37
|
constructor(properties) {
|
|
35
38
|
super();
|
|
36
39
|
this.query = properties.query || new RPC();
|
|
@@ -38,37 +41,42 @@ export let DString = class DString extends Program {
|
|
|
38
41
|
this._index = new StringIndex();
|
|
39
42
|
}
|
|
40
43
|
async open(options) {
|
|
41
|
-
this._optionCanAppend = options?.canAppend;
|
|
42
44
|
await this._index.open(this._log.log);
|
|
43
45
|
await this._log.open({
|
|
44
46
|
encoding,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
replicas: {
|
|
48
|
+
min: 0xffffffff, // assume a document can not be sharded?
|
|
49
|
+
},
|
|
50
|
+
canAppend: async (entry) => {
|
|
51
|
+
const operation = await entry.getPayloadValue();
|
|
52
|
+
if (!(await this._canPerform(operation, { entry }))) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return options?.canPerform
|
|
56
|
+
? options.canPerform(operation, { entry })
|
|
57
|
+
: true;
|
|
58
|
+
},
|
|
59
|
+
onChange: async (change) => {
|
|
60
|
+
await this._index.updateIndex(change);
|
|
61
|
+
this.events.dispatchEvent(new CustomEvent("change", {
|
|
62
|
+
detail: change,
|
|
63
|
+
}));
|
|
64
|
+
},
|
|
47
65
|
});
|
|
48
66
|
await this.query.open({
|
|
49
67
|
...options,
|
|
50
|
-
topic: this._log.log.
|
|
51
|
-
canRead: options?.canRead,
|
|
68
|
+
topic: sha256Base64Sync(concat([this._log.log.id, fromString("/dstring")])),
|
|
52
69
|
responseHandler: this.queryHandler.bind(this),
|
|
53
|
-
queryType:
|
|
70
|
+
queryType: SearchRequest,
|
|
54
71
|
responseType: StringResult,
|
|
55
72
|
});
|
|
56
73
|
}
|
|
57
|
-
async
|
|
58
|
-
if (
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
if (this._optionCanAppend && !(await this._optionCanAppend(entry))) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
async _canAppend(entry) {
|
|
67
|
-
if (this._log.log.length === 0) {
|
|
74
|
+
async _canPerform(operation, context) {
|
|
75
|
+
if (this._log.log.length === 0 || context.entry.next.length === 0) {
|
|
68
76
|
return true;
|
|
69
77
|
}
|
|
70
78
|
else {
|
|
71
|
-
for (const next of entry.next) {
|
|
79
|
+
for (const next of context.entry.next) {
|
|
72
80
|
if (this._log.log.has(next)) {
|
|
73
81
|
return true;
|
|
74
82
|
}
|
|
@@ -80,18 +88,24 @@ export let DString = class DString extends Program {
|
|
|
80
88
|
return this._log.append(new StringOperation({
|
|
81
89
|
index,
|
|
82
90
|
value,
|
|
83
|
-
}), {
|
|
91
|
+
}), {
|
|
92
|
+
...options,
|
|
93
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads() },
|
|
94
|
+
});
|
|
84
95
|
}
|
|
85
96
|
async del(index, options) {
|
|
86
97
|
return this.add("", index, options);
|
|
87
98
|
}
|
|
88
|
-
async queryHandler(query) {
|
|
99
|
+
async queryHandler(query, ctx) {
|
|
89
100
|
logger.debug("Recieved query");
|
|
90
|
-
if (query instanceof
|
|
101
|
+
if (query instanceof SearchRequest == false) {
|
|
91
102
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
92
103
|
return;
|
|
93
104
|
}
|
|
94
105
|
const stringQuery = query;
|
|
106
|
+
if (this._canRead && !(await this._canRead(ctx.from))) {
|
|
107
|
+
return new NoAccess();
|
|
108
|
+
}
|
|
95
109
|
const content = this._index.string;
|
|
96
110
|
const relaventQueries = stringQuery.query.filter((x) => x instanceof StringMatch);
|
|
97
111
|
if (relaventQueries.length == 0) {
|
|
@@ -125,13 +139,22 @@ export let DString = class DString extends Program {
|
|
|
125
139
|
async getValue(options) {
|
|
126
140
|
if (options?.remote) {
|
|
127
141
|
const counter = new Map();
|
|
128
|
-
const responses = await this.query.request(new
|
|
142
|
+
const responses = await this.query.request(new SearchRequest({
|
|
129
143
|
query: [],
|
|
130
144
|
}), options.remote.queryOptions);
|
|
131
145
|
for (const response of responses) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
146
|
+
if (response.response instanceof NoAccess) {
|
|
147
|
+
logger.error("Missing access");
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
else if (response.response instanceof StringResult) {
|
|
151
|
+
options?.remote.callback &&
|
|
152
|
+
options?.remote.callback(response.response.string);
|
|
153
|
+
counter.set(response.response.string, (counter.get(response.response.string) || 0) + 1);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
throw new Error("Unsupported type: " + response?.constructor?.name);
|
|
157
|
+
}
|
|
135
158
|
}
|
|
136
159
|
let max = -1;
|
|
137
160
|
let ret = undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-store.js","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAoB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"string-store.js","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAoB,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAiB,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,OAAO,EAAiB,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAc,GAAG,EAAkB,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,QA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AC,CAAC,CAAC;YACH,OAAO,GAAG,CAAC;SACX;aAAM;YACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;SAC1B;IACF,CAAC;CACD,CAAA;AAhMA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;8BACrB,SAAS;qCAAkB;AAGjC;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;8BACd,GAAG;sCAAsC;AAGhD;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;8BACrB,WAAW;uCAAC;AARR,OAAO;IADnB,OAAO,CAAC,SAAS,CAAC;;GACN,OAAO,CAkMnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/string",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "String store",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@dao-xyz/borsh": "^5.1.5",
|
|
34
34
|
"@peerbit/logger": "1.0.1",
|
|
35
|
-
"@peerbit/program": "2.
|
|
36
|
-
"@peerbit/rpc": "2.0
|
|
37
|
-
"@peerbit/shared-log": "^
|
|
35
|
+
"@peerbit/program": "2.2.0",
|
|
36
|
+
"@peerbit/rpc": "2.1.0",
|
|
37
|
+
"@peerbit/shared-log": "^3.0.0",
|
|
38
38
|
"@peerbit/time": "1.0.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@peerbit/test-utils": "^1.0.
|
|
41
|
+
"@peerbit/test-utils": "^1.0.16"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "464e807d679e24b897b7811ac99d6f85fbd756f9"
|
|
44
44
|
}
|
package/src/query.ts
CHANGED
|
@@ -51,7 +51,7 @@ export class StringMatch {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
@variant(0)
|
|
54
|
-
export class
|
|
54
|
+
export class SearchRequest {
|
|
55
55
|
@field({ type: vec(StringMatch) })
|
|
56
56
|
query!: StringMatch[];
|
|
57
57
|
|
|
@@ -63,18 +63,22 @@ export class StringQueryRequest {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/// ----- RESULTS -----
|
|
66
|
+
export abstract class AbstractSearchResult {}
|
|
67
|
+
|
|
66
68
|
@variant(0)
|
|
67
|
-
export class StringResult {
|
|
69
|
+
export class StringResult extends AbstractSearchResult {
|
|
68
70
|
@field({ type: "string" })
|
|
69
71
|
string: string;
|
|
70
72
|
|
|
71
73
|
@field({ type: option(RangeMetadatas) })
|
|
72
74
|
metadatas?: RangeMetadatas;
|
|
73
75
|
|
|
74
|
-
constructor(properties
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
76
|
+
constructor(properties: { string: string; metadatas?: RangeMetadatas }) {
|
|
77
|
+
super();
|
|
78
|
+
this.string = properties.string;
|
|
79
|
+
this.metadatas = properties.metadatas;
|
|
79
80
|
}
|
|
80
81
|
}
|
|
82
|
+
|
|
83
|
+
@variant(1)
|
|
84
|
+
export class NoAccess extends AbstractSearchResult {}
|
package/src/string-store.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { field, variant } from "@dao-xyz/borsh";
|
|
2
|
-
import { AppendOptions, CanAppend, Entry } from "@peerbit/log";
|
|
2
|
+
import { AppendOptions, CanAppend, Change, Entry } from "@peerbit/log";
|
|
3
3
|
import { SharedLog, SharedLogOptions } from "@peerbit/shared-log";
|
|
4
|
-
import {
|
|
5
|
-
import { Program } from "@peerbit/program";
|
|
6
|
-
import { RPCOptions,
|
|
4
|
+
import { PublicSignKey, sha256Base64Sync } from "@peerbit/crypto";
|
|
5
|
+
import { Program, ProgramEvents } from "@peerbit/program";
|
|
6
|
+
import { RPCOptions, RPC, RequestContext } from "@peerbit/rpc";
|
|
7
7
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
8
8
|
import { StringOperation, StringIndex, encoding } from "./string-index.js";
|
|
9
9
|
import {
|
|
10
|
+
AbstractSearchResult,
|
|
11
|
+
NoAccess,
|
|
10
12
|
RangeMetadata,
|
|
11
13
|
RangeMetadatas,
|
|
12
14
|
StringMatch,
|
|
13
|
-
|
|
15
|
+
SearchRequest,
|
|
14
16
|
StringResult,
|
|
15
17
|
} from "./query.js";
|
|
18
|
+
import { CustomEvent } from "@libp2p/interfaces/events";
|
|
19
|
+
import { concat, fromString } from "uint8arrays";
|
|
16
20
|
|
|
17
21
|
import { Range } from "./range.js";
|
|
18
22
|
|
|
@@ -33,31 +37,42 @@ const findAllOccurrences = (str: string, substr: string): number[] => {
|
|
|
33
37
|
return result;
|
|
34
38
|
};
|
|
35
39
|
|
|
36
|
-
export type
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
41
|
+
|
|
42
|
+
export interface StringEvents {
|
|
43
|
+
change: CustomEvent<Change<StringOperation>>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type CanPerform = (
|
|
47
|
+
operation: StringOperation,
|
|
48
|
+
context: TransactionContext
|
|
49
|
+
) => Promise<boolean> | boolean;
|
|
39
50
|
|
|
40
51
|
type Args = {
|
|
41
52
|
canRead?: CanRead;
|
|
42
|
-
|
|
53
|
+
canPerform?: CanPerform;
|
|
43
54
|
log?: SharedLogOptions;
|
|
44
55
|
};
|
|
56
|
+
|
|
57
|
+
export type TransactionContext = {
|
|
58
|
+
entry: Entry<StringOperation>;
|
|
59
|
+
};
|
|
45
60
|
@variant("dstring")
|
|
46
|
-
export class DString extends Program {
|
|
61
|
+
export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
47
62
|
@field({ type: SharedLog })
|
|
48
63
|
_log: SharedLog<StringOperation>;
|
|
49
64
|
|
|
50
65
|
@field({ type: RPC })
|
|
51
|
-
query: RPC<
|
|
66
|
+
query: RPC<SearchRequest, AbstractSearchResult>;
|
|
52
67
|
|
|
53
68
|
@field({ type: StringIndex })
|
|
54
69
|
_index: StringIndex;
|
|
55
70
|
|
|
56
|
-
|
|
71
|
+
_canRead?: CanRead;
|
|
57
72
|
|
|
58
73
|
constructor(properties: {
|
|
59
74
|
id?: Uint8Array;
|
|
60
|
-
query?: RPC<
|
|
75
|
+
query?: RPC<SearchRequest, AbstractSearchResult>;
|
|
61
76
|
}) {
|
|
62
77
|
super();
|
|
63
78
|
this.query = properties.query || new RPC();
|
|
@@ -66,40 +81,52 @@ export class DString extends Program {
|
|
|
66
81
|
}
|
|
67
82
|
|
|
68
83
|
async open(options?: Args) {
|
|
69
|
-
this._optionCanAppend = options?.canAppend;
|
|
70
84
|
await this._index.open(this._log.log);
|
|
71
85
|
|
|
72
86
|
await this._log.open({
|
|
73
87
|
encoding,
|
|
74
|
-
|
|
75
|
-
|
|
88
|
+
replicas: {
|
|
89
|
+
min: 0xffffffff, // assume a document can not be sharded?
|
|
90
|
+
},
|
|
91
|
+
canAppend: async (entry) => {
|
|
92
|
+
const operation = await entry.getPayloadValue();
|
|
93
|
+
|
|
94
|
+
if (!(await this._canPerform(operation, { entry }))) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return options?.canPerform
|
|
98
|
+
? options.canPerform(operation, { entry })
|
|
99
|
+
: true;
|
|
100
|
+
},
|
|
101
|
+
onChange: async (change) => {
|
|
102
|
+
await this._index.updateIndex(change);
|
|
103
|
+
this.events.dispatchEvent(
|
|
104
|
+
new CustomEvent("change", {
|
|
105
|
+
detail: change,
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
},
|
|
76
109
|
});
|
|
77
110
|
|
|
78
111
|
await this.query.open({
|
|
79
112
|
...options,
|
|
80
|
-
topic:
|
|
81
|
-
|
|
113
|
+
topic: sha256Base64Sync(
|
|
114
|
+
concat([this._log.log.id, fromString("/dstring")])
|
|
115
|
+
),
|
|
82
116
|
responseHandler: this.queryHandler.bind(this),
|
|
83
|
-
queryType:
|
|
117
|
+
queryType: SearchRequest,
|
|
84
118
|
responseType: StringResult,
|
|
85
119
|
});
|
|
86
120
|
}
|
|
87
121
|
|
|
88
|
-
async
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (this.
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async _canAppend(entry: Entry<StringOperation>): Promise<boolean> {
|
|
99
|
-
if (this._log.log.length === 0) {
|
|
122
|
+
private async _canPerform(
|
|
123
|
+
operation: StringOperation,
|
|
124
|
+
context: TransactionContext
|
|
125
|
+
): Promise<boolean> {
|
|
126
|
+
if (this._log.log.length === 0 || context.entry.next.length === 0) {
|
|
100
127
|
return true;
|
|
101
128
|
} else {
|
|
102
|
-
for (const next of entry.next) {
|
|
129
|
+
for (const next of context.entry.next) {
|
|
103
130
|
if (this._log.log.has(next)) {
|
|
104
131
|
return true;
|
|
105
132
|
}
|
|
@@ -118,7 +145,10 @@ export class DString extends Program {
|
|
|
118
145
|
index,
|
|
119
146
|
value,
|
|
120
147
|
}),
|
|
121
|
-
{
|
|
148
|
+
{
|
|
149
|
+
...options,
|
|
150
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads() },
|
|
151
|
+
}
|
|
122
152
|
);
|
|
123
153
|
}
|
|
124
154
|
|
|
@@ -127,14 +157,19 @@ export class DString extends Program {
|
|
|
127
157
|
}
|
|
128
158
|
|
|
129
159
|
async queryHandler(
|
|
130
|
-
query:
|
|
131
|
-
|
|
160
|
+
query: SearchRequest,
|
|
161
|
+
ctx: RequestContext
|
|
162
|
+
): Promise<AbstractSearchResult | undefined> {
|
|
132
163
|
logger.debug("Recieved query");
|
|
133
|
-
if (query instanceof
|
|
164
|
+
if (query instanceof SearchRequest == false) {
|
|
134
165
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
135
166
|
return;
|
|
136
167
|
}
|
|
137
|
-
|
|
168
|
+
|
|
169
|
+
const stringQuery = query as SearchRequest;
|
|
170
|
+
if (this._canRead && !(await this._canRead(ctx.from))) {
|
|
171
|
+
return new NoAccess();
|
|
172
|
+
}
|
|
138
173
|
|
|
139
174
|
const content = this._index.string;
|
|
140
175
|
const relaventQueries = stringQuery.query.filter(
|
|
@@ -177,24 +212,31 @@ export class DString extends Program {
|
|
|
177
212
|
async getValue(options?: {
|
|
178
213
|
remote: {
|
|
179
214
|
callback: (string: string) => any;
|
|
180
|
-
queryOptions: RPCOptions<
|
|
215
|
+
queryOptions: RPCOptions<AbstractSearchResult>;
|
|
181
216
|
};
|
|
182
217
|
}): Promise<string | undefined> {
|
|
183
218
|
if (options?.remote) {
|
|
184
219
|
const counter: Map<string, number> = new Map();
|
|
185
220
|
const responses = await this.query.request(
|
|
186
|
-
new
|
|
221
|
+
new SearchRequest({
|
|
187
222
|
query: [],
|
|
188
223
|
}),
|
|
189
224
|
options.remote.queryOptions
|
|
190
225
|
);
|
|
191
226
|
for (const response of responses) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
227
|
+
if (response.response instanceof NoAccess) {
|
|
228
|
+
logger.error("Missing access");
|
|
229
|
+
continue;
|
|
230
|
+
} else if (response.response instanceof StringResult) {
|
|
231
|
+
options?.remote.callback &&
|
|
232
|
+
options?.remote.callback(response.response.string);
|
|
233
|
+
counter.set(
|
|
234
|
+
response.response.string,
|
|
235
|
+
(counter.get(response.response.string) || 0) + 1
|
|
236
|
+
);
|
|
237
|
+
} else {
|
|
238
|
+
throw new Error("Unsupported type: " + response?.constructor?.name);
|
|
239
|
+
}
|
|
198
240
|
}
|
|
199
241
|
|
|
200
242
|
let max = -1;
|