UNPKG

3.83 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/fold
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.IocProxyClass = exports.IocProxyObject = void 0;
12function getBindingValue(handler) {
13 return handler.options.has(handler.namespace)
14 ? handler.options.resolve(handler.namespace, handler.value)
15 : handler.value;
16}
17/**
18 * Proxy handler to handle objects
19 */
20const objectHandler = (options) => {
21 return {
22 get(_, key, receiver) {
23 const descriptor = Object.getOwnPropertyDescriptor(options.value, key);
24 /**
25 * Handling the proxy invariants use case. Learn more
26 *
27 * https://262.ecma-international.org/8.0/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
28 *
29 * Check the following "get" trap
30 * https://github.com/kpruden/on-change/blob/5b80da1f5f7ac80c37d7bd19122188acb7ad0b19/index.js#L44-L66
31 */
32 if (descriptor && !descriptor.configurable) {
33 if (descriptor.set && !descriptor.get) {
34 return undefined;
35 }
36 if (descriptor.writable === false) {
37 return Reflect.get(options.value, key, receiver);
38 }
39 }
40 return Reflect.get(getBindingValue(options), key, receiver);
41 },
42 apply(_, thisArgument, args) {
43 return Reflect.apply(getBindingValue(options), thisArgument, args);
44 },
45 defineProperty(_, propertyKey, attributes) {
46 return Reflect.defineProperty(getBindingValue(options), propertyKey, attributes);
47 },
48 deleteProperty(_, propertyKey) {
49 return Reflect.deleteProperty(getBindingValue(options), propertyKey);
50 },
51 getOwnPropertyDescriptor(_, propertyKey) {
52 return Reflect.getOwnPropertyDescriptor(getBindingValue(options), propertyKey);
53 },
54 getPrototypeOf(_) {
55 return Reflect.getPrototypeOf(getBindingValue(options));
56 },
57 has(_, propertyKey) {
58 return Reflect.has(getBindingValue(options), propertyKey);
59 },
60 isExtensible(_) {
61 return Reflect.isExtensible(getBindingValue(options));
62 },
63 ownKeys(_) {
64 return Reflect.ownKeys(getBindingValue(options));
65 },
66 preventExtensions() {
67 throw new Error('Cannot prevent extensions during a fake');
68 },
69 set(_, propertyKey, value, receiver) {
70 return Reflect.set(getBindingValue(options), propertyKey, value, receiver);
71 },
72 setPrototypeOf(_, proto) {
73 return Reflect.setPrototypeOf(getBindingValue(options), proto);
74 },
75 };
76};
77/**
78 * Proxy handler to handle classes and functions
79 */
80const classHandler = (options) => {
81 return Object.assign({}, objectHandler(options), {
82 construct(_, args, newTarget) {
83 return Reflect.construct(getBindingValue(options), args, newTarget);
84 },
85 });
86};
87/**
88 * Proxies the objects to fallback to fake, when it exists.
89 */
90class IocProxyObject {
91 constructor(namespace, value, options) {
92 this.namespace = namespace;
93 this.value = value;
94 this.options = options;
95 return new Proxy(value, objectHandler({ namespace, value, options }));
96 }
97}
98exports.IocProxyObject = IocProxyObject;
99/**
100 * Proxies the class constructor to fallback to fake, when it exists.
101 */
102function IocProxyClass(namespace, value, options) {
103 return new Proxy(value, classHandler({ namespace, value, options }));
104}
105exports.IocProxyClass = IocProxyClass;