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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 7x 7x 7x 83x 43x 40x 27x 3x 8x 24x 4x 20x 3x 3x 17x 1x 1x 1x 16x 12x 12x 12x 57x 19x 12x 12x 2x 10x 1x 9x 4x 114x 53x 61x 43x 18x 181x 181x 12x 80x 8x 221x 1x 220x 3x 1x 2x 3x 217x 3x 3x 8x 214x 25x 3x 22x 189x 189x 15x 2x 13x 174x 54x 24x 30x 30x 30x 120x 27x 93x 20x 20x 20x 18x 20x 2x 2x 181x 1x 1x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 2x 4x 348x 2x 346x 4x 341x 19x 322x 2x 320x 211x 28x 183x 15x 168x 109x 84x 84x 84x 25x 60x 60x 60x 58x 60x 5x 5x 4x 5x 5x 63x | import ValidatedTopic, { IStaticThis, TopicData } from './ValidatedTopic'
import { defaultMetadataStorage } from 'class-transformer/storage'
import * as classTransformer from 'class-transformer'
import { archivist } from 'mage'
import * as mage from 'mage'
import { inspect } from 'util'
const { Tome, ObjectTome, ArrayTome } = mage.require('tomes')
const ARRAY_GET_METHODS = [
'pop',
'shift'
]
const ARRAY_SLICE_METHODS = [
'slice',
'splice'
]
const ARRAY_PARSE_METHODS = [
'sort',
'forEach',
'filter',
'map',
'every',
'some',
'reduce',
'reduceRight',
'find',
'findIndex'
]
const ARRAY_SEARCH_METHODS = [
'includes'
]
function createToString(tome: any) {
return () => JSON.stringify(Tome.unTome(tome))
}
function createInspect(tome: any, className: string) {
return (depth: number = 1, options: any = {}) => {
options.depth = depth
return className + ' -> ' + inspect(Tome.unTome(tome), options)
}
}
function proxifyOrUntome(t: any, ctor: any) {
if (ObjectTome.isObjectTome(t) || ArrayTome.isArrayTome(t) ) {
return createTomeProxy(t, ctor)
}
return t.valueOf()
}
function processArrayTomeMethodRequest(target: any, key: string, ctor: any) {
// Entries iterator
if (key === 'entries') {
return target
.map((t: any) => proxifyOrUntome(t, ctor))
.entries
}
// For pop, shift, etc
if (ARRAY_GET_METHODS.includes(key)) {
return (...args: any[]) => proxifyOrUntome(target[key](...args), ctor)
}
// For slice, splice, etc
if (ARRAY_SLICE_METHODS.includes(key)) {
return (...args: any[]) => target[key](...args)
.map((val: any) => proxifyOrUntome(val, ctor))
}
if (ARRAY_SEARCH_METHODS.includes(key)) {
return function (...args: any[]) {
const vals = target.map((t: any) => proxifyOrUntome(t, ctor))
return vals[key](...args)
}
}
// For methods receiving a function as a first parameter (map, reduce, forEach, etc)
if (ARRAY_PARSE_METHODS.includes(key)) {
return function (...args: any[]) {
const iterator = args[0]
args[0] = function (...iteratorArgs: any[]) {
iteratorArgs = iteratorArgs.map((t: any) => proxifyOrUntome(t, ctor))
return iterator(...iteratorArgs)
}
const ret = target[key](...args)
if (key === 'find') {
return ret ? proxifyOrUntome(ret, ctor) : ret
}
if (key === 'filter') {
return ret.map((t: any) => proxifyOrUntome(t, ctor))
}
return ret
}
}
// All other methods must be bound to the target
return target[key].bind(target)
}
function extractType(typeInfo: any, typeMap: any, key: any) {
if (typeInfo) {
return typeInfo.typeFunction()
}
if (typeMap[key]) {
return typeMap[key]
}
return Object
}
/**
* Wrap tome and sub-tome instances intp proxies, allowing
* for direct access to attributes
*/
function createTomeProxy(tome: any, ctor: any): any {
const typeMap: any = {}
const proxy: any = new Proxy(tome, {
ownKeys(target) {
return Object.keys(target).concat([
'__dirty__',
'__root__',
'__diff__',
'__diffEnabled__',
'__version__',
'__parent__',
'__key__'
])
},
getPrototypeOf(target) {
if (ArrayTome.isArrayTome(target)) {
return Array.prototype
}
/* istanbul ignore next */
return ctor.prototype || Object.prototype
},
get(target: any, key: any) {
// Return a stringified object/array/etc
if (key === 'toString' || key === Symbol.toStringTag) {
return createToString(target)
}
// the type definition for util.inspect.custom seems to be missing
if (key === 'inspect' || key === (<any> inspect).custom) {
let name: string
if (ArrayTome.isArrayTome(target)) {
name = 'Array'
} else {
name = ctor.name
}
return createInspect(target, name)
}
// Return the data structure's standard iterator
if (key === Symbol.iterator) {
/* istanbul ignore else */
if (ArrayTome.isArrayTome(target)) {
return target
.valueOf()
.map((t: any) => proxifyOrUntome(t, ctor))[Symbol.iterator]
}
/* istanbul ignore next*/
return target[Symbol.iterator]
}
// Return the correct constructor - we want
// ArrayTomes to appear as being Arrays,
// and everything else as the known type
if (key === 'constructor') {
if (ArrayTome.isArrayTome(target)) {
return Array
}
return ctor
}
// Extract the value
const val = target[key]
// Undefined means either the value is undefined,
// or we are trying to access a prototype method
if (val === undefined) {
if (ctor.prototype[key]) {
return ctor.prototype[key].bind(proxy)
}
return val
}
// Create a ObjectTome or ArrayTome proxy
if (ObjectTome.isObjectTome(val) || ArrayTome.isArrayTome(val)) {
if (ArrayTome.isArrayTome(target)) {
return createTomeProxy(val, ctor)
}
const typeInfo = defaultMetadataStorage.findTypeMetadata(ctor, <string> key)
const childCtor = extractType(typeInfo, typeMap, key)
return createTomeProxy(target[key], childCtor)
}
// Mangle array functions so that they keep the proper
// context and proxy siblings when needed
if (typeof val === 'function' && ArrayTome.isArrayTome(target)) {
return processArrayTomeMethodRequest(target, key, ctor)
}
// TODO: map object method calls to their "real" prototype
// In all other cases, return the underlying value of the attribute
return val.valueOf()
},
set(target: any, key: string, value) {
target.set(key, value)
typeMap[key] = undefined
if (value !== undefined && value !== null) {
typeMap[key] = value.constructor
}
return true
},
deleteProperty(_target, key: string) {
tome.del(key)
return true
}
})
return proxy
}
/**
* Dealing with tome topics
*/
export default class ValidatedTomeTopic extends ValidatedTopic {
public static readonly mediaType = 'application/x-tome'
public static async create<I extends archivist.IArchivistIndex, T extends ValidatedTopic>(
this: IStaticThis<I, T>,
state: mage.core.IState,
index: I,
data?: TopicData<T>
): Promise<T> {
const className = this.getClassName()
const typeMap: any = {}
const isTome = Tome.isTome(data)
const rawData: any = isTome ? Tome.unTome(data) : data
const instance = classTransformer.plainToClass<T, object>(this, rawData) || new this()
const tome: any = isTome ? data : Tome.conjure(instance)
instance.setTopic(className)
instance.setState(state)
await instance.setIndex(index)
const proxy: any = new Proxy(instance, {
ownKeys() {
return Object.keys(tome)
},
getOwnPropertyDescriptor(target: any, key) {
return { configurable: true, enumerable: true, value: this.get(target, key) }
},
get(target: any, key) {
if (key === 'toString' || key === Symbol.toStringTag) {
return createToString(tome)
}
// the type definition for util.inspect.custom seems to be missing
if (key === 'inspect' || key === (<any> inspect).custom) {
return createInspect(tome, className)
}
/* istanbul ignore next */
if (key === 'set' || key === 'del') {
return target[key]
}
if (key === 'constructor') {
return target.constructor
}
if (key === 'getData') {
return () => tome
}
if (!tome[key]) {
if (typeof target[key] === 'function') {
return target[key].bind(proxy)
}
if (key === '_state' || key === '_topic' || key === '_index' ) {
return target[key]
}
return undefined
}
if (ObjectTome.isObjectTome(tome[key]) || ArrayTome.isArrayTome(tome[key])) {
const typeInfo = defaultMetadataStorage.findTypeMetadata(target.constructor, <string> key)
const ctor = extractType(typeInfo, typeMap, key)
return createTomeProxy(tome[key], ctor)
}
return tome[key].valueOf()
},
set(_target: any, key, value) {
tome.set(key, value)
typeMap[key] = undefined
if (value !== undefined && value !== null) {
typeMap[key] = value.constructor
}
return true
},
deleteProperty(target, key) {
const val = tome[key]
if (Tome.isTome(val)) {
tome.del(key)
}
delete target[key]
return true
}
})
return proxy
}
}
|