Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 18x 18x 18x 18x 18x 18x 18x 18x 117x 117x 117x 125x 125x 89x 36x 36x 36x 36x 3x 33x 106x 106x 106x 106x 106x 11x 11x 11x 11x 11x 15x 15x 15x 15x 13x 10x 10x 10x 10x 8x 53x 53x 53x 53x 46x 16x 16x 16x 16x 13x 43x 43x 32x 32x 32x 28x 28x 28x 125x 125x 106x 106x 124x 124x 11x 11x 94x 94x 378x 247x 125x 18x | import Node from 'type/node';
import Adapter from 'interface/adapter';
import SystemId from 'system/enum/id';
import SystemCache from 'system/cache';
import buildNodeUrl from 'utility/build-node-url';
import NotFoundError from 'http/error/not-found';
import transformNode from 'repository/utility/transform-node';
import NodeParameters from 'type/node-parameters';
import transformValue from 'repository/utility/transform-value';
import standardizeUrl from 'repository/utility/standardize-url';
import unstandardizeUrl from 'repository/utility/unstandardize-url';
import FieldValue, { PrimitiveValue } from 'type/field-value';
class Repository {
private hostname: string;
private adapter: Adapter;
private system_cache: SystemCache;
public constructor(hostname: string, adapter: Adapter) {
this.hostname = hostname;
this.adapter = adapter;
this.system_cache = new SystemCache(hostname);
}
public async fetchNode(
type_id: string,
node_id: string
): Promise<Node | undefined> {
const system_node = this.fetchSystemNode(type_id, node_id);
if (system_node !== undefined) {
// NOTE: No transformation happens on system nodes, for performance
// reasons. They are generated upfront to use the correct hostname.
return Promise.resolve(system_node);
}
const adapter = this.getAdapter();
const node_key = `${type_id}/${node_id}`;
const node = await adapter.fetchNode(node_key);
if (node === undefined) {
return undefined;
}
return this.unstandardizeNode(node);
}
public async storeNode(node: Node): Promise<Node> {
const node_key = `${node.type_id}/${node.id}`;
const adapter = this.getAdapter();
const standardized_node = this.standardizeNode(node);
await adapter.storeNode(node_key, standardized_node);
return node;
}
public async setField(
type_id: string,
node_id: string,
field_key: string,
field_value: FieldValue
): Promise<Node> {
const node_key = `${type_id}/${node_id}`;
const standardized_value = this.standardizeValue(field_value);
const adapter = this.getAdapter();
const node = await adapter.setField(
node_key,
field_key,
standardized_value
);
return this.unstandardizeNode(node);
}
public async addValueToSet(
type_id: string,
node_id: string,
field_key: string,
value: PrimitiveValue
): Promise<Node> {
const node_key = `${type_id}/${node_id}`;
const standardized_value = this.standardizePrimitiveValue(value);
const adapter = this.getAdapter();
const node = await adapter.addValueToSet(
node_key,
field_key,
standardized_value
);
return this.unstandardizeNode(node);
}
public async removeValueFromSet(
type_id: string,
node_id: string,
field_key: string,
value: PrimitiveValue
): Promise<Node> {
const node_key = `${type_id}/${node_id}`;
const standardized_value = this.standardizePrimitiveValue(value);
const adapter = this.getAdapter();
const node = await adapter.removeValueFromSet(
node_key,
field_key,
standardized_value
);
return this.unstandardizeNode(node);
}
public async addValueToList(
type_id: string,
node_id: string,
field_key: string,
value: PrimitiveValue,
position?: number
): Promise<Node> {
const node_key = `${type_id}/${node_id}`;
const standardized_value = this.standardizePrimitiveValue(value);
const adapter = this.getAdapter();
const node = await adapter.addValueToList(
node_key,
field_key,
standardized_value,
position
);
return this.unstandardizeNode(node);
}
public async removeValueFromList(
type_id: string,
node_id: string,
field_key: string,
value: PrimitiveValue,
position?: number
): Promise<Node> {
const node_key = `${type_id}/${node_id}`;
const standardized_value = this.standardizePrimitiveValue(value);
const adapter = this.getAdapter();
const node = await adapter.removeValueFromList(
node_key,
field_key,
standardized_value,
position
);
return this.unstandardizeNode(node);
}
public buildNodeUrl(node: Node): string {
const hostname = this.getHostname();
return buildNodeUrl(hostname, node);
}
public isLocalUrl(url: string): boolean {
const hostname = this.getHostname();
return url.startsWith(hostname);
}
public getNodeParametersForUrl(url: string): NodeParameters | undefined {
const hostname = this.getHostname();
const suffix = url.replace(hostname, '');
const match = suffix.match(/^\/([^\/]+)\/([^\/]+)$/);
if (match === null) {
return undefined;
}
const type_id = match[1];
const id = match[2];
return {
id,
type_id
};
}
public async fetchAccountId(
username: string,
password: string
): Promise<string | undefined> {
const adapter = this.getAdapter();
return adapter.fetchAccountId(username, password);
}
public async fetchAnonymousAccount(): Promise<Node> {
const node = await this.fetchNode(
SystemId.ACCOUNT_TYPE,
SystemId.ANONYMOUS_ACCOUNT
);
Iif (node === undefined) {
throw new NotFoundError();
}
return node;
}
public async fetchSystemAccount(): Promise<Node> {
const node = await this.fetchNode(
SystemId.ACCOUNT_TYPE,
SystemId.SYSTEM_ACCOUNT
);
Iif (node === undefined) {
throw new NotFoundError();
}
return node;
}
private fetchSystemNode(type_id: string, node_id: string): Node | undefined {
const system_cache = this.getSystemCache();
return system_cache.fetchNode(type_id, node_id);
}
private standardizeNode(node: Node): Node {
const hostname = this.getHostname();
return transformNode(node, hostname, standardizeUrl);
}
private unstandardizeNode(node: Node): Node {
const hostname = this.getHostname();
return transformNode(node, hostname, unstandardizeUrl);
}
private standardizeValue(value: FieldValue): FieldValue {
const hostname = this.getHostname();
return transformValue(value, hostname, standardizeUrl);
}
private standardizePrimitiveValue(value: PrimitiveValue): PrimitiveValue {
const hostname = this.getHostname();
return standardizeUrl(value, hostname);
}
private getHostname(): string {
return this.hostname;
}
private getAdapter(): Adapter {
return this.adapter;
}
private getSystemCache(): SystemCache {
return this.system_cache;
}
}
export default Repository;
|