All files sharedFactory.ts

86.55% Statements 103/119
72.73% Branches 24/33
53.85% Functions 7/13
87.18% Lines 102/117

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 3333x       7x 7x   7x 7x   7x   7x   7x 7x 7x 7x   7x 7x 7x 7x     7x 7x 7x   7x 7x 7x     7x 7x 7x 7x   7x 7x 7x 7x   7x 7x 7x   7x 7x 7x   7x 7x   7x                                 7x 7x 7x   7x   7x 7x   7x   7x 431x 431x 1304x 430x         874x       874x 874x           431x   431x     431x     7x 7x 7x                               7x             7x 18x   18x   18x 37x   37x   37x 7x   7x     30x     11x           7x 17314x 2181x     15133x 6749x   6749x       6749x   6749x           6749x       6749x       6749x   6749x 16795x       15133x               7x 1780x       1780x 1780x 1780x   1780x       1780x             7x         7x                 7x             7x                                                                 7x 7x     7x                                                                                     7x 7x   7x        
export function makeShared() {
    'use strict';
 
    // function/object that prefixed with F (which means the binding is frozen after the return)
    const FArray = Array
    const FArrayIsArray = Array.isArray
 
    const FProxy = Proxy
    const FProxyRevocable = Proxy.revocable
 
    const FError = Error
 
    const FCall = Function.prototype.call
 
    const FMap = Map
    const FMapHas = Map.prototype.has
    const FMapSet = Map.prototype.set
    const FMapGet = Map.prototype.get
 
    const FWeakMap = WeakMap
    const FWeakMapHas = WeakMap.prototype.has
    const FWeakMapSet = WeakMap.prototype.set
    const FWeakMapGet = WeakMap.prototype.get
 
 
    const FBWeakMapHas: (self: WeakMap<any, any>, ...args: any[]) => any = FCall.bind(FWeakMapHas)
    const FBWeakMapSet: (self: WeakMap<any, any>, ...args: any[]) => any = FCall.bind(FWeakMapSet)
    const FBWeakMapGet: (self: WeakMap<any, any>, ...args: any[]) => any = FCall.bind(FWeakMapGet)
 
    const FBMapHas: (self: Map<any, any>, ...args: any[]) => any = FCall.bind(FMapHas)
    const FBMapSet: (self: Map<any, any>, ...args: any[]) => any = FCall.bind(FMapSet)
    const FBMapGet: (self: Map<any, any>, ...args: any[]) => any = FCall.bind(FMapGet)
 
 
    const FSet = Set
    const FSetHas = Set.prototype.has
    const FSetAdd = Set.prototype.add
    const FSetDelete = Set.prototype.delete
 
    const FWeakSet = WeakSet
    const FWeakSetHas = WeakSet.prototype.has
    const FWeakSetAdd = WeakSet.prototype.add
    const FWeakSetDelete = WeakSet.prototype.delete
 
    const FBSetHas: (self: Set<any>, ...args: any[]) => any = FCall.bind(FSetHas)
    const FBSetAdd: (self: Set<any>, ...args: any[]) => any  = FCall.bind(FSetAdd)
    const FBSetDelete: (self: Set<any>, ...args: any[]) => any  = FCall.bind(FSetDelete)
 
    const FBWeakSetHas: (self: WeakSet<any>, ...args: any[]) => any = FCall.bind(FWeakSetHas)
    const FBWeakSetAdd: (self: WeakSet<any>, ...args: any[]) => any  = FCall.bind(FWeakSetAdd)
    const FBWeakSetDelete: (self: WeakSet<any>, ...args: any[]) => any  = FCall.bind(FWeakSetDelete)
 
    const FArrayMap = Array.prototype.map
    const FBArrayMap: <T, U>(arg: T[], cb: (v: T, i: number) => U)=>U[] = FCall.bind(FArrayMap) as any
 
    const FReflect: typeof Reflect = {
        // ...Reflect,
        construct: Reflect.construct,
        ownKeys: Reflect.ownKeys,
        get: Reflect.get,
        set: Reflect.set,
        getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
        apply: Reflect.apply,
        getPrototypeOf: Reflect.getPrototypeOf,
        defineProperty: Reflect.defineProperty,
        setPrototypeOf: Reflect.setPrototypeOf,
        isExtensible: Reflect.isExtensible,
        preventExtensions: Reflect.preventExtensions,
        has: Reflect.has,
        deleteProperty: Reflect.deleteProperty,
        enumerate: Reflect.enumerate
    }
    const FCreateEmpty = Object.create.bind(Object, null)
    const FSetPrototypeOf = Object.setPrototypeOf
    const FGetPrototypeOf = Object.getPrototypeOf
 
    const FGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
 
    const FFreeze = Object.freeze
    const FIsFrozen = Object.isFrozen
 
    const SymbolIterator: (typeof Symbol)['iterator'] = Symbol.iterator
 
    const FBArrayToIterator: <T>(arr: T[])=> Iterable<T> = (arr: any[]) => {
        let index = 0
        const next = () => {
            if (index >= arr.length) {
                return {
                    value: undefined,
                    done: true
                }
            } else {
                const result = {
                    value: arr[index],
                    done: false
                }
                index++
                return result
            }
        }
        const value: {
            next: typeof next,
            [Symbol.iterator]: any
        } = {
            next,
            [SymbolIterator]: function () { return this }
        } as any
 
        return value
    }
 
    const FSetValues = Set.prototype.values
    const FSetIteratorNext = (FReflect.getPrototypeOf((new Set).values()) as any).next 
    const FBSetToIterator = (set: Set<any>) => {
        const iterator = FReflect.apply(FSetValues, set, [])
 
        const next = () => FReflect.apply(FSetIteratorNext, iterator, [])
 
        const value: {
            next: typeof next,
            [Symbol.iterator]: any
        } = {
            next,
            [SymbolIterator]: function () { return this }
        } as any
 
        return value
    }
 
    const FConsoleError = console.error
 
    /**
     * Using the getOwnPropertyDescriptor and FGetPrototypeOf
     * @param obj any
     * @param key object key
     */
    const FResolveDesc = (obj: any, key: string | number | symbol) => {
        const weak = new FWeakMap()
 
        let currentTarget = obj
 
        while (currentTarget && !FBWeakMapHas(weak, currentTarget)) {
            FBWeakMapSet(weak, currentTarget, true)
 
            const desc = FReflect.getOwnPropertyDescriptor(currentTarget, key)
 
            if (desc !== undefined) {
                FSetPrototypeOf(desc, null)
 
                return desc
            }
 
            currentTarget = FGetPrototypeOf(currentTarget)
        }
 
        return undefined
    }
 
    /**
     * Clear all prototype prop from the whole object
     */
    const dropPrototypeRecursive = <T extends object>(unsafeObj: T, record = FReflect.construct(FWeakMap, [])) => {
        if (FBWeakMapHas(record, unsafeObj)) {
            return unsafeObj
        }
 
        if (unsafeObj != null && (typeof unsafeObj === 'function' || typeof unsafeObj === 'object')) {
            FSetPrototypeOf(unsafeObj, null)
 
            Iif (FGetPrototypeOf(unsafeObj) !== null) {
                throw new FError('PROTOTYPE LEAKING!!!')
            }
 
            FFreeze(unsafeObj)
 
            Iif (!FIsFrozen) {
                throw new FError('tainted object!!!')
            }
 
            // the object may use timing attack to bypass get prototype check
            // because it is not frozen at that time
            Iif (FGetPrototypeOf(unsafeObj) !== null) {
                throw new FError('PROTOTYPE LEAKING!!!')
            }
 
            FBWeakMapSet(record, unsafeObj, true)
 
            // burst it no matter it is enumerable or not
            // nothing should ever leak
            var keys = FReflect.ownKeys(/** @type {any} */(unsafeObj))
 
            for (let i = 0; i < keys.length; i++) {
                dropPrototypeRecursive((unsafeObj as any)[keys[i]], record)
            }
        }
 
        return unsafeObj
    }
 
    /**
     * get prop without trigger the getter
     * @param unsafeObj 
     * @param name 
     */
    const safeGetProp = <T extends object, U extends keyof T>(unsafeObj: T, name: U): T[U] | null => {
        Iif (unsafeObj == null || (typeof unsafeObj !== 'object' && typeof unsafeObj !== 'function')) {
            return null
        }
 
        try {
            const unsafeDesc = FGetOwnPropertyDescriptor(unsafeObj, name)
            const valueDesc = FGetOwnPropertyDescriptor(unsafeDesc, 'value')
 
            Iif (valueDesc == null) {
                throw "value desk does not exist"
            }
 
            return 'value' in valueDesc ? valueDesc.value : null
        } catch (err) {
            FConsoleError('BAD ACTOR', unsafeObj, name)
            throw 'This shouldn\'t happen'
        }
    }
 
    const FNodeNodeNameGetter = typeof Node != 'undefined'
        ? FReflect.getOwnPropertyDescriptor(Node.prototype, 'nodeName')!.get!
        : () => {}
 
    // Test whether given object is a dom node or not
    const getNodeName = (obj: any) => {
        try {
            return FReflect.apply(FNodeNodeNameGetter, obj, [])
        } catch (err) {
            return null
        }
    }
 
    // prevent arguments.caller
    dropPrototypeRecursive(safeGetProp)
 
    /** 
     * expose `this` to getter/setter on these prototypes should be safe, 
     * because they only use internal slot of 'this'
     * input/output still need to be wrapped
     */
    const whitelistedPrototypes: any[] = [
        Date.prototype,
 
        Promise.prototype,
 
        Map.prototype,
        Set.prototype,
 
        WeakMap.prototype,
        WeakSet.prototype,
 
        ArrayBuffer.prototype,
 
        (Reflect.getPrototypeOf(Int8Array) as any).prototype, // TypedArray
 
        Int8Array.prototype,
        Int16Array.prototype,
        Int32Array.prototype,
 
        Uint8Array.prototype,
        Uint8ClampedArray.prototype,
        Uint16Array.prototype,
        Uint32Array.prototype,
 
        DataView.prototype,
 
        RegExp.prototype,
 
        FReflect.getPrototypeOf(FReflect.getPrototypeOf((function * () {})())), // generator prototype
        FReflect.getPrototypeOf(new Map().values()), // map iterator
        FReflect.getPrototypeOf(new Set().values()), // set iterator
    ]
 
    Eif (typeof SharedArrayBuffer !== 'undefined') {
        whitelistedPrototypes[whitelistedPrototypes.length] = SharedArrayBuffer.prototype
    }
 
    const shared = {
        FProxy,
        FProxyRevocable,
        FError,
 
        FArray,
        FArrayIsArray,
 
        FMap,
        FBMapHas,
        FBMapGet,
        FBMapSet,
 
        FWeakMap,
        FBWeakMapHas,
        FBWeakMapSet,
        FBWeakMapGet,
 
        FSet,
        FBSetHas,
        FBSetAdd,
        FBSetDelete,
 
        FWeakSet,
        FBWeakSetHas,
        FBWeakSetAdd,
        FBWeakSetDelete,
 
        FBArrayMap,
        FBArrayToIterator,
        FBSetToIterator,
        FReflect,
        FCreateEmpty,
        FFreeze,
        FIsFrozen,
        FResolveDesc,
        FConsoleError,
        dropPrototypeRecursive,
        safeGetProp,
        getNodeName,
        whitelistedPrototypes
    }
 
    FSetPrototypeOf(shared, null)
    FFreeze(shared)
 
    return shared
}
 
export type IShared = ReturnType<typeof makeShared>