@peerbit/string 2.0.4 → 3.0.1
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 +17 -17
- package/lib/esm/string-store.js +43 -26
- 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 +71 -42
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 {
|
|
3
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
4
|
import { Program, ProgramEvents } from "@peerbit/program";
|
|
5
|
-
import { RPCOptions,
|
|
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
|
-
canRead?: (key: SignatureWithKey) => Promise<boolean>;
|
|
12
|
-
};
|
|
10
|
+
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
13
11
|
export interface StringEvents {
|
|
14
12
|
change: CustomEvent<Change<StringOperation>>;
|
|
15
13
|
}
|
|
16
|
-
type
|
|
14
|
+
export type CanPerform = (operation: StringOperation, context: TransactionContext) => Promise<boolean> | boolean;
|
|
15
|
+
export type Args = {
|
|
17
16
|
canRead?: CanRead;
|
|
18
|
-
|
|
17
|
+
canPerform?: CanPerform;
|
|
19
18
|
log?: SharedLogOptions;
|
|
20
19
|
};
|
|
20
|
+
export type TransactionContext = {
|
|
21
|
+
entry: Entry<StringOperation>;
|
|
22
|
+
};
|
|
21
23
|
export declare class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
22
24
|
_log: SharedLog<StringOperation>;
|
|
23
|
-
query: RPC<
|
|
25
|
+
query: RPC<SearchRequest, AbstractSearchResult>;
|
|
24
26
|
_index: StringIndex;
|
|
25
|
-
|
|
27
|
+
_canRead?: CanRead;
|
|
26
28
|
constructor(properties: {
|
|
27
29
|
id?: Uint8Array;
|
|
28
|
-
query?: RPC<
|
|
30
|
+
query?: RPC<SearchRequest, AbstractSearchResult>;
|
|
29
31
|
});
|
|
30
32
|
open(options?: Args): Promise<void>;
|
|
31
|
-
|
|
32
|
-
_canAppend(entry: Entry<StringOperation>): Promise<boolean>;
|
|
33
|
+
private _canPerform;
|
|
33
34
|
add(value: string, index: Range, options?: AppendOptions<StringOperation>): Promise<{
|
|
34
35
|
entry: Entry<StringOperation>;
|
|
35
36
|
removed: Entry<StringOperation>[];
|
|
@@ -38,12 +39,11 @@ export declare class DString extends Program<Args, StringEvents & ProgramEvents>
|
|
|
38
39
|
entry: Entry<StringOperation>;
|
|
39
40
|
removed: Entry<StringOperation>[];
|
|
40
41
|
}>;
|
|
41
|
-
queryHandler(query:
|
|
42
|
+
queryHandler(query: SearchRequest, ctx: RequestContext): Promise<AbstractSearchResult | undefined>;
|
|
42
43
|
getValue(options?: {
|
|
43
44
|
remote: {
|
|
44
45
|
callback: (string: string) => any;
|
|
45
|
-
queryOptions: RPCOptions<
|
|
46
|
+
queryOptions: RPCOptions<AbstractSearchResult>;
|
|
46
47
|
};
|
|
47
48
|
}): Promise<string | undefined>;
|
|
48
49
|
}
|
|
49
|
-
export {};
|
package/lib/esm/string-store.js
CHANGED
|
@@ -9,12 +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";
|
|
17
18
|
import { CustomEvent } from "@libp2p/interfaces/events";
|
|
19
|
+
import { concat, fromString } from "uint8arrays";
|
|
18
20
|
const logger = loggerFn({ module: "string" });
|
|
19
21
|
export const STRING_STORE_TYPE = "string_store";
|
|
20
22
|
const findAllOccurrences = (str, substr) => {
|
|
@@ -31,7 +33,7 @@ export let DString = class DString extends Program {
|
|
|
31
33
|
_log;
|
|
32
34
|
query;
|
|
33
35
|
_index;
|
|
34
|
-
|
|
36
|
+
_canRead;
|
|
35
37
|
constructor(properties) {
|
|
36
38
|
super();
|
|
37
39
|
this.query = properties.query || new RPC();
|
|
@@ -39,11 +41,21 @@ export let DString = class DString extends Program {
|
|
|
39
41
|
this._index = new StringIndex();
|
|
40
42
|
}
|
|
41
43
|
async open(options) {
|
|
42
|
-
this._optionCanAppend = options?.canAppend;
|
|
43
44
|
await this._index.open(this._log.log);
|
|
44
45
|
await this._log.open({
|
|
45
46
|
encoding,
|
|
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
|
+
},
|
|
47
59
|
onChange: async (change) => {
|
|
48
60
|
await this._index.updateIndex(change);
|
|
49
61
|
this.events.dispatchEvent(new CustomEvent("change", {
|
|
@@ -53,28 +65,18 @@ export let DString = class DString extends Program {
|
|
|
53
65
|
});
|
|
54
66
|
await this.query.open({
|
|
55
67
|
...options,
|
|
56
|
-
topic: this._log.log.
|
|
57
|
-
canRead: options?.canRead,
|
|
68
|
+
topic: sha256Base64Sync(concat([this._log.log.id, fromString("/dstring")])),
|
|
58
69
|
responseHandler: this.queryHandler.bind(this),
|
|
59
|
-
queryType:
|
|
70
|
+
queryType: SearchRequest,
|
|
60
71
|
responseType: StringResult,
|
|
61
72
|
});
|
|
62
73
|
}
|
|
63
|
-
async
|
|
64
|
-
if (
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
if (this._optionCanAppend && !(await this._optionCanAppend(entry))) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
async _canAppend(entry) {
|
|
73
|
-
if (this._log.log.length === 0) {
|
|
74
|
+
async _canPerform(operation, context) {
|
|
75
|
+
if (this._log.log.length === 0 || context.entry.next.length === 0) {
|
|
74
76
|
return true;
|
|
75
77
|
}
|
|
76
78
|
else {
|
|
77
|
-
for (const next of entry.next) {
|
|
79
|
+
for (const next of context.entry.next) {
|
|
78
80
|
if (this._log.log.has(next)) {
|
|
79
81
|
return true;
|
|
80
82
|
}
|
|
@@ -86,18 +88,24 @@ export let DString = class DString extends Program {
|
|
|
86
88
|
return this._log.append(new StringOperation({
|
|
87
89
|
index,
|
|
88
90
|
value,
|
|
89
|
-
}), {
|
|
91
|
+
}), {
|
|
92
|
+
...options,
|
|
93
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads() },
|
|
94
|
+
});
|
|
90
95
|
}
|
|
91
96
|
async del(index, options) {
|
|
92
97
|
return this.add("", index, options);
|
|
93
98
|
}
|
|
94
|
-
async queryHandler(query) {
|
|
99
|
+
async queryHandler(query, ctx) {
|
|
95
100
|
logger.debug("Recieved query");
|
|
96
|
-
if (query instanceof
|
|
101
|
+
if (query instanceof SearchRequest == false) {
|
|
97
102
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
98
103
|
return;
|
|
99
104
|
}
|
|
100
105
|
const stringQuery = query;
|
|
106
|
+
if (this._canRead && !(await this._canRead(ctx.from))) {
|
|
107
|
+
return new NoAccess();
|
|
108
|
+
}
|
|
101
109
|
const content = this._index.string;
|
|
102
110
|
const relaventQueries = stringQuery.query.filter((x) => x instanceof StringMatch);
|
|
103
111
|
if (relaventQueries.length == 0) {
|
|
@@ -131,13 +139,22 @@ export let DString = class DString extends Program {
|
|
|
131
139
|
async getValue(options) {
|
|
132
140
|
if (options?.remote) {
|
|
133
141
|
const counter = new Map();
|
|
134
|
-
const responses = await this.query.request(new
|
|
142
|
+
const responses = await this.query.request(new SearchRequest({
|
|
135
143
|
query: [],
|
|
136
144
|
}), options.remote.queryOptions);
|
|
137
145
|
for (const response of responses) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
+
}
|
|
141
158
|
}
|
|
142
159
|
let max = -1;
|
|
143
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,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,CAAC,CAAC;iBACR;YACF,CAAC,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.1",
|
|
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": "7c1809206b35443b49f93db1d64c37d8084481f4"
|
|
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,19 +1,22 @@
|
|
|
1
1
|
import { field, variant } from "@dao-xyz/borsh";
|
|
2
2
|
import { AppendOptions, CanAppend, Change, Entry } from "@peerbit/log";
|
|
3
3
|
import { SharedLog, SharedLogOptions } from "@peerbit/shared-log";
|
|
4
|
-
import {
|
|
4
|
+
import { PublicSignKey, sha256Base64Sync } from "@peerbit/crypto";
|
|
5
5
|
import { Program, ProgramEvents } from "@peerbit/program";
|
|
6
|
-
import { RPCOptions,
|
|
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";
|
|
16
18
|
import { CustomEvent } from "@libp2p/interfaces/events";
|
|
19
|
+
import { concat, fromString } from "uint8arrays";
|
|
17
20
|
|
|
18
21
|
import { Range } from "./range.js";
|
|
19
22
|
|
|
@@ -34,36 +37,42 @@ const findAllOccurrences = (str: string, substr: string): number[] => {
|
|
|
34
37
|
return result;
|
|
35
38
|
};
|
|
36
39
|
|
|
37
|
-
export type
|
|
38
|
-
canRead?: (key: SignatureWithKey) => Promise<boolean>;
|
|
39
|
-
};
|
|
40
|
+
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
40
41
|
|
|
41
42
|
export interface StringEvents {
|
|
42
43
|
change: CustomEvent<Change<StringOperation>>;
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
type
|
|
46
|
+
export type CanPerform = (
|
|
47
|
+
operation: StringOperation,
|
|
48
|
+
context: TransactionContext
|
|
49
|
+
) => Promise<boolean> | boolean;
|
|
50
|
+
|
|
51
|
+
export type Args = {
|
|
46
52
|
canRead?: CanRead;
|
|
47
|
-
|
|
53
|
+
canPerform?: CanPerform;
|
|
48
54
|
log?: SharedLogOptions;
|
|
49
55
|
};
|
|
50
56
|
|
|
57
|
+
export type TransactionContext = {
|
|
58
|
+
entry: Entry<StringOperation>;
|
|
59
|
+
};
|
|
51
60
|
@variant("dstring")
|
|
52
61
|
export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
53
62
|
@field({ type: SharedLog })
|
|
54
63
|
_log: SharedLog<StringOperation>;
|
|
55
64
|
|
|
56
65
|
@field({ type: RPC })
|
|
57
|
-
query: RPC<
|
|
66
|
+
query: RPC<SearchRequest, AbstractSearchResult>;
|
|
58
67
|
|
|
59
68
|
@field({ type: StringIndex })
|
|
60
69
|
_index: StringIndex;
|
|
61
70
|
|
|
62
|
-
|
|
71
|
+
_canRead?: CanRead;
|
|
63
72
|
|
|
64
73
|
constructor(properties: {
|
|
65
74
|
id?: Uint8Array;
|
|
66
|
-
query?: RPC<
|
|
75
|
+
query?: RPC<SearchRequest, AbstractSearchResult>;
|
|
67
76
|
}) {
|
|
68
77
|
super();
|
|
69
78
|
this.query = properties.query || new RPC();
|
|
@@ -72,12 +81,23 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
72
81
|
}
|
|
73
82
|
|
|
74
83
|
async open(options?: Args) {
|
|
75
|
-
this._optionCanAppend = options?.canAppend;
|
|
76
84
|
await this._index.open(this._log.log);
|
|
77
85
|
|
|
78
86
|
await this._log.open({
|
|
79
87
|
encoding,
|
|
80
|
-
|
|
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
|
+
},
|
|
81
101
|
onChange: async (change) => {
|
|
82
102
|
await this._index.updateIndex(change);
|
|
83
103
|
this.events.dispatchEvent(
|
|
@@ -90,29 +110,23 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
90
110
|
|
|
91
111
|
await this.query.open({
|
|
92
112
|
...options,
|
|
93
|
-
topic:
|
|
94
|
-
|
|
113
|
+
topic: sha256Base64Sync(
|
|
114
|
+
concat([this._log.log.id, fromString("/dstring")])
|
|
115
|
+
),
|
|
95
116
|
responseHandler: this.queryHandler.bind(this),
|
|
96
|
-
queryType:
|
|
117
|
+
queryType: SearchRequest,
|
|
97
118
|
responseType: StringResult,
|
|
98
119
|
});
|
|
99
120
|
}
|
|
100
121
|
|
|
101
|
-
async
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (this.
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async _canAppend(entry: Entry<StringOperation>): Promise<boolean> {
|
|
112
|
-
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) {
|
|
113
127
|
return true;
|
|
114
128
|
} else {
|
|
115
|
-
for (const next of entry.next) {
|
|
129
|
+
for (const next of context.entry.next) {
|
|
116
130
|
if (this._log.log.has(next)) {
|
|
117
131
|
return true;
|
|
118
132
|
}
|
|
@@ -131,7 +145,10 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
131
145
|
index,
|
|
132
146
|
value,
|
|
133
147
|
}),
|
|
134
|
-
{
|
|
148
|
+
{
|
|
149
|
+
...options,
|
|
150
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads() },
|
|
151
|
+
}
|
|
135
152
|
);
|
|
136
153
|
}
|
|
137
154
|
|
|
@@ -140,14 +157,19 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
140
157
|
}
|
|
141
158
|
|
|
142
159
|
async queryHandler(
|
|
143
|
-
query:
|
|
144
|
-
|
|
160
|
+
query: SearchRequest,
|
|
161
|
+
ctx: RequestContext
|
|
162
|
+
): Promise<AbstractSearchResult | undefined> {
|
|
145
163
|
logger.debug("Recieved query");
|
|
146
|
-
if (query instanceof
|
|
164
|
+
if (query instanceof SearchRequest == false) {
|
|
147
165
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
148
166
|
return;
|
|
149
167
|
}
|
|
150
|
-
|
|
168
|
+
|
|
169
|
+
const stringQuery = query as SearchRequest;
|
|
170
|
+
if (this._canRead && !(await this._canRead(ctx.from))) {
|
|
171
|
+
return new NoAccess();
|
|
172
|
+
}
|
|
151
173
|
|
|
152
174
|
const content = this._index.string;
|
|
153
175
|
const relaventQueries = stringQuery.query.filter(
|
|
@@ -190,24 +212,31 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
190
212
|
async getValue(options?: {
|
|
191
213
|
remote: {
|
|
192
214
|
callback: (string: string) => any;
|
|
193
|
-
queryOptions: RPCOptions<
|
|
215
|
+
queryOptions: RPCOptions<AbstractSearchResult>;
|
|
194
216
|
};
|
|
195
217
|
}): Promise<string | undefined> {
|
|
196
218
|
if (options?.remote) {
|
|
197
219
|
const counter: Map<string, number> = new Map();
|
|
198
220
|
const responses = await this.query.request(
|
|
199
|
-
new
|
|
221
|
+
new SearchRequest({
|
|
200
222
|
query: [],
|
|
201
223
|
}),
|
|
202
224
|
options.remote.queryOptions
|
|
203
225
|
);
|
|
204
226
|
for (const response of responses) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
+
}
|
|
211
240
|
}
|
|
212
241
|
|
|
213
242
|
let max = -1;
|