@final-hill/decorator-contracts 0.25.0 → 0.25.3
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/README.md +8 -14
- package/dist/AssertionError.d.mts +1 -1
- package/dist/Contract.d.mts +1 -1
- package/dist/Contracted.d.mts +1 -1
- package/dist/Messages.d.mts +1 -1
- package/dist/assert.d.mts +1 -1
- package/dist/iff.d.mts +1 -1
- package/dist/implies.d.mts +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.LICENSE.txt +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/lib/ClassRegistration.d.mts +1 -1
- package/dist/lib/ClassType.d.mts +1 -1
- package/dist/lib/Feature.d.mts +1 -1
- package/dist/lib/assertDemands.d.mts +1 -1
- package/dist/lib/assertEnsures.d.mts +1 -1
- package/dist/lib/assertInvariants.d.mts +1 -1
- package/dist/lib/{CLASS_REGISTRY.d.mts → classRegistry.d.mts} +3 -3
- package/dist/lib/deepFreeze.d.mts +1 -1
- package/dist/lib/index.d.mts +4 -4
- package/dist/lib/takeWhile.d.mts +1 -1
- package/dist/lib/unChecked.d.mts +1 -1
- package/dist/tests/Contract.test.d.mts +1 -1
- package/dist/tests/Stack.test.d.mts +1 -1
- package/dist/tests/assert.test.d.mts +1 -1
- package/dist/tests/deepFreeze.test.d.mts +1 -1
- package/dist/tests/demands.test.d.mts +1 -1
- package/dist/tests/ensures.test.d.mts +1 -1
- package/dist/tests/factoryPattern.test.d.mts +1 -1
- package/dist/tests/iff.test.d.mts +1 -1
- package/dist/tests/implies.test.d.mts +1 -1
- package/dist/tests/invariant.test.d.mts +1 -1
- package/dist/tests/public-properties.test.d.mts +1 -1
- package/dist/tests/rescue.test.d.mts +1 -1
- package/dist/tests/within.test.d.mts +1 -1
- package/package.json +14 -16
package/README.md
CHANGED
|
@@ -153,7 +153,7 @@ class Stack<T> implements StackType<T> {
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
top(): T {
|
|
156
|
-
return this.#implementation
|
|
156
|
+
return this.#implementation.at(-1);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
```
|
|
@@ -315,20 +315,14 @@ properties with accessors should be used instead. Example:
|
|
|
315
315
|
```ts
|
|
316
316
|
@Contracted(pointContract)
|
|
317
317
|
class Point2D {
|
|
318
|
-
|
|
319
|
-
|
|
318
|
+
accessor x: number
|
|
319
|
+
accessor y: number
|
|
320
320
|
|
|
321
321
|
constructor(x: number, y: number) {
|
|
322
322
|
super()
|
|
323
|
-
this
|
|
324
|
-
this
|
|
323
|
+
this.x = x
|
|
324
|
+
this.y = y
|
|
325
325
|
}
|
|
326
|
-
|
|
327
|
-
get x(): number { return this.#x }
|
|
328
|
-
set x(value: number) { this.#x = value }
|
|
329
|
-
|
|
330
|
-
get y(): number { return this.#y }
|
|
331
|
-
set y(value: number) { this.#y = value }
|
|
332
326
|
}
|
|
333
327
|
```
|
|
334
328
|
|
|
@@ -344,7 +338,7 @@ True assertions do not throw an error. An example of this is given below using a
|
|
|
344
338
|
|
|
345
339
|
```typescript
|
|
346
340
|
const stackContract = new Contract<StackType<any>>({
|
|
347
|
-
[
|
|
341
|
+
[invariant](self) {
|
|
348
342
|
return self.isEmpty() == (self.size == 0) &&
|
|
349
343
|
self.isFull() == (self.size == self.limit) &&
|
|
350
344
|
self.size >= 0 && self.size <= self.limit
|
|
@@ -572,7 +566,7 @@ The remaining arguments of `ensures` reflect the arguments of the associated fea
|
|
|
572
566
|
```typescript
|
|
573
567
|
const baseContract = new Contract<Base>({
|
|
574
568
|
someMethod: {
|
|
575
|
-
ensures(_self,
|
|
569
|
+
ensures(_self, _old, x: number){ return 0 <= x && x <= 10 }
|
|
576
570
|
}
|
|
577
571
|
})
|
|
578
572
|
|
|
@@ -707,7 +701,7 @@ recursion. An example of `retry` usage:
|
|
|
707
701
|
```ts
|
|
708
702
|
const studentRepositoryContract = new Contract<StudentRepository>({
|
|
709
703
|
getStudent: {
|
|
710
|
-
rescue(
|
|
704
|
+
rescue(_self, error, [id], retry) {
|
|
711
705
|
console.error(error)
|
|
712
706
|
console.log('Retrying with legacy id...')
|
|
713
707
|
retry(`old-${id}`)
|
package/dist/Contract.d.mts
CHANGED
package/dist/Contracted.d.mts
CHANGED
package/dist/Messages.d.mts
CHANGED
package/dist/assert.d.mts
CHANGED
package/dist/iff.d.mts
CHANGED
package/dist/implies.d.mts
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (C)
|
|
3
|
+
* Copyright (C) 2024 Final Hill LLC
|
|
4
4
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
5
5
|
* @see <https://spdx.org/licenses/AGPL-3.0-only.html>
|
|
6
6
|
*/
|
|
@@ -9,5 +9,5 @@ import Contracted, { innerContract } from './Contracted.mjs';
|
|
|
9
9
|
import assert from './assert.mjs';
|
|
10
10
|
import implies from './implies.mjs';
|
|
11
11
|
export { AssertionError, Contracted, implies, innerContract, assert };
|
|
12
|
-
export { Contract, extend, invariant, Invariant, checkedMode, Rescue } from './Contract.mjs';
|
|
13
|
-
export { Constructor, AbstractConstructor, ClassType } from './lib/ClassType.mjs';
|
|
12
|
+
export { Contract, extend, invariant, type Invariant, checkedMode, type Rescue } from './Contract.mjs';
|
|
13
|
+
export { type Constructor, type AbstractConstructor, type ClassType } from './lib/ClassType.mjs';
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/*! For license information please see index.mjs.LICENSE.txt */
|
|
2
|
-
var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{
|
|
2
|
+
var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{pf:()=>r,NZ:()=>a,eR:()=>M,vA:()=>P,zf:()=>n,X$:()=>i,M0:()=>j,JG:()=>w,V1:()=>o});class s extends Error{constructor(t){super(t)}}const r=s,n=Symbol("checkedMode"),o=Symbol("invariant"),i=Symbol("extend");class a{#t;[n];[i];assertions=Object.create(null);constructor(t=Object.create(null)){this[i]=t[i],this[n]=t[n]??!0,this.#t=t[o]??O,Object.keys(t).forEach((e=>{const s=t[e];Object.defineProperty(this.assertions,e,{value:{demands:s.demands??O,ensures:s.ensures??O,rescue:s.rescue,within:s.within}})})),b(this.assertions)}get[o](){return this.#t}}const c=(t,e)=>{try{t[n]=!1,e()}finally{t[n]=!0}},l=function t(e,s,r,o,a){let l=!0;const u=Reflect.get(s.assertions,o)?.demands,h=`demands not met on ${r}.prototype.${String(o)}\r\n${u}`;s[n]&&u&&c(s,(()=>l=u.call(e,e,...a))),s[i]&&!l?t(e,s[i],r,o,a):P(l,h)},u=function t(e,s,r,o,a,l){const u=Reflect.get(s.assertions,o)?.ensures,h=`ensures not met on ${r}.prototype.${String(o)}\r\n${u}`;s[n]&&u&&c(s,(()=>P(u.call(e,e,a,...l),h))),s[i]&&t(e,s[i],r,o,a,l)},h=function t(e,s){s[n]&&c(s,(()=>{const t=s[o];P(t.call(e,e),`Invariant violated. ${t.toString()}`)})),s[i]&&t(e,s[i])};class d extends WeakMap{getOrCreate(t){return this.has(t)||this.set(t,new y(t)),this.get(t)}}const f=new d;var p;function g(t,e,s){return function r(...o){const{Class:i}=s,a=i.name;P(this instanceof i,`${p.MsgInvalidContext}. Expected: this instanceof ${a}. this: ${this.constructor.name}`);const d=Reflect.get(this,w);if(!d[n])return e.apply(this,o);let f;h(this,d),l(this,d,a,t,o);try{const r=Object.create(null);c(d,(()=>{s.features.forEach((({name:t,hasGetter:e})=>{e&&Object.defineProperty(r,t,{value:this[t]})}))}));const n=Reflect.get(d.assertions,t)?.within,i=Date.now();f=e.apply(this,o);const l=Date.now();n&&P(l-i<n,`Timing constraint violated. Constraint: ${n}ms, actual: ${l-i}ms`),u(this,d,a,t,r,o)}catch(e){const s=Reflect.get(d.assertions,t)?.rescue;if(null==s)throw h(this,d),e;let o=!1;if(c(d,(()=>{s.call(this,this,e,[],((...t)=>{P(!o,p.MsgSingleRetry),o=!0,d[n]=!0,f=r.call(this,...t)}))})),!o)throw h(this,d),e}return h(this,d),f}}!function(t){t.AssertionFailed="Assertion failure",t.MsgNoProperties="Public properties are forbidden",t.MsgNotContracted="The current class or one of its ancestors must declare @Contracted(...)",t.MsgMissingFeature="The requested feature is not registered",t.MsgSingleContract="Only a single @Contracted decorator is allowed per class",t.MsgSingleRetry="retry can only be called once",t.MsgInvalidContext="A contracted feature can not be applied to objects of a different base class",t.MsgBadSubcontract="A sub contract must extend a base contract"}(p||(p={}));const y=class{Class;#e;contract;contractsChecked=!1;constructor(t){this.Class=t;const e=this.Class.prototype;this.#e=Reflect.ownKeys(e).filter((t=>"constructor"!=t&&t!==w)).map((t=>new C(this,t,Object.getOwnPropertyDescriptor(e,t))))}get features(){return[...this.#e]}get ParentClass(){return Object.getPrototypeOf(this.Class.prototype)?.constructor}get parentRegistration(){return null==this.ParentClass?null:f.getOrCreate(this.ParentClass)}ancestry(){if(null==this.ParentClass)return[];{const t=f.getOrCreate(this.ParentClass);return[t,...t.ancestry()]}}ancestryFeatures(){return this.ancestry().flatMap((({features:t})=>t))}bindContract(t){if(this.contract=t,!t[n])return;const e=this.Class.prototype;this.features.forEach((t=>{const{name:s,hasGetter:r,hasSetter:n,isMethod:o}=t;Object.defineProperty(e,s,{enumerable:!0,...r?{get:g(s,t.getter,this)}:{},...n?{set:g(s,t.setter,this)}:{},...o?{value:g(s,t.value,this)}:{}}),t.descriptor=Object.getOwnPropertyDescriptor(e,s)}))}findFeature(t){return this.features.find((({name:e})=>e===t))??this.parentRegistration?.findFeature(t)}},b=function t(e){for(const[,s]of Object.entries(e))null!=s&&"object"==typeof s&&t(s);Object.freeze(e)},C=class{classRegistration;name;#s;constructor(t,e,s){this.classRegistration=t,this.name=e,this.#s=s}get ancestorFeature(){return this.classRegistration.ancestryFeatures().filter((t=>t.name===this.name))[0]}get getter(){return this.#s.get}get hasGetter(){return this.isAccessor&&null!=this.#s.get}get hasSetter(){return this.isAccessor&&null!=this.#s.set}get isProperty(){return"function"!=typeof this.#s.value&&void 0!==this.#s.value}get isMethod(){return"function"==typeof this.#s.value}get isAccessor(){return void 0===this.#s.value}get memberType(){return this.isMethod?"method":this.isProperty?"property":"accessor"}get setter(){return this.#s.set}get value(){return this.#s.value}get descriptor(){return this.#s}set descriptor(t){this.#s=t}},m=function t(e,s){return e.length&&s(e[0])?[e[0],...t(e.slice(1),s)]:[]},O=()=>!0,v=Symbol("isContracted"),w=Symbol("innerContract"),M=function(t=new a){return function(e,s){if("class"!==s.kind)throw new TypeError(p.MsgNotContracted);if(P(!Object.getOwnPropertySymbols(e).includes(v),p.MsgSingleContract),!1===t[n])return e;const r=e[w];P(!r||r&&t[i]instanceof r.constructor,p.MsgBadSubcontract);class o extends e{static[v]=!0;constructor(...t){super(...t);const s=this.constructor,r=f.getOrCreate(s);var n;if(P((n=r,!Object.entries(Object.getOwnPropertyDescriptors(this)).some((([t,e])=>new C(n,t,e).isProperty))),p.MsgNoProperties),!r.contractsChecked){const t=m(r.ancestry(),(t=>t.Class!==e));[r,...t,f.get(e)].forEach((t=>{t.bindContract(o[w])}))}return h(this,o[w]),this}static get[w](){return t}get[w](){return t}}return f.getOrCreate(o).contractsChecked=!1,Object.freeze(e),o}};function P(t,e=p.AssertionFailed,s=r){if(0==Boolean(t))throw new s(e)}function j(t,e){return!t||e}var S=e.pf,R=e.NZ,$=e.eR,A=e.vA,x=e.zf,k=e.X$,E=e.M0,F=e.JG,N=e.V1;export{S as AssertionError,R as Contract,$ as Contracted,A as assert,x as checkedMode,k as extend,E as implies,F as innerContract,N as invariant};
|
|
3
3
|
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D,kBAAmB,EAErC5E,OAAOsG,OAAOa,GAEPS,CACX,CACJ,EChEe,SAAShF,EAAOuF,EAAoBvH,EAAkB8C,EAAS0E,gBAAiBC,EAAuC,GAClI,GAA0B,GAAtBC,QAAQH,GACR,MAAM,IAAIE,EAAiBzH,EACnC,CCfe,SAAS2H,EAAQC,EAAYC,GACxC,OAAQD,GAAKC,CACjB","sources":["webpack://@final-hill/decorator-contracts/webpack/bootstrap","webpack://@final-hill/decorator-contracts/webpack/runtime/define property getters","webpack://@final-hill/decorator-contracts/webpack/runtime/hasOwnProperty shorthand","webpack://@final-hill/decorator-contracts/./src/AssertionError.mts","webpack://@final-hill/decorator-contracts/./src/Contract.mts","webpack://@final-hill/decorator-contracts/./src/lib/unChecked.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertDemands.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertEnsures.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertInvariants.mts","webpack://@final-hill/decorator-contracts/./src/lib/CLASS_REGISTRY.mts","webpack://@final-hill/decorator-contracts/./src/Messages.mts","webpack://@final-hill/decorator-contracts/./src/lib/ClassRegistration.mts","webpack://@final-hill/decorator-contracts/./src/lib/deepFreeze.mts","webpack://@final-hill/decorator-contracts/./src/lib/Feature.mts","webpack://@final-hill/decorator-contracts/./src/lib/takeWhile.mts","webpack://@final-hill/decorator-contracts/./src/lib/index.mts","webpack://@final-hill/decorator-contracts/./src/Contracted.mts","webpack://@final-hill/decorator-contracts/./src/assert.mts","webpack://@final-hill/decorator-contracts/./src/implies.mts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * An AssertionError represents a failed assertion with an associated message\r\n */\r\nclass AssertionError extends Error {\r\n /**\r\n * Constructs a new instance of AssertionError\r\n *\r\n * @param {string} message - The associated message\r\n */\r\n constructor(message: string) {\r\n super(message);\r\n }\r\n}\r\n\r\nexport default AssertionError;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { deepFreeze, fnTrue } from './lib/index.mjs';\r\n\r\nconst checkedMode = Symbol('checkedMode'),\r\n invariant = Symbol('invariant'),\r\n extend = Symbol('extend');\r\n\r\ntype AnyObject = Record<PropertyKey, any>;\r\ntype AnyFunc = (...args: any[]) => any;\r\n\r\ntype NonFunctionPropertyNames<T extends AnyObject> = { [K in keyof T]: T[K] extends AnyFunc ? never : K }[keyof T];\r\nexport type Properties<T extends AnyObject> = Pick<T, NonFunctionPropertyNames<T>>;\r\n\r\nexport type Invariant<T extends AnyObject> = (self: T) => boolean;\r\nexport type Demands<T extends AnyObject, F extends T[any]> = (self: T, ...args: Parameters<F>) => boolean;\r\nexport type Ensures<T extends AnyObject, F extends T[any]> = (self: T, old: Properties<T>, ...args: Parameters<F>) => boolean;\r\nexport type Rescue<T extends AnyObject, F extends T[any]> = (\r\n self: T,\r\n error: Error,\r\n args: F extends AnyFunc ? Parameters<F> : [F],\r\n retry: F extends AnyFunc ? (...args: Parameters<F>) => any : (value: F) => void\r\n) => void;\r\n\r\nexport type ContractOptions<\r\n T extends AnyObject\r\n> = {\r\n [extend]: Contract<T>;\r\n [invariant]: Invariant<T>;\r\n [checkedMode]: boolean;\r\n} & {\r\n [K in keyof T]?: FeatureOption<T, T[K]>\r\n };\r\n\r\nexport interface FeatureOption<T extends AnyObject, F extends T[any]> {\r\n demands?: Demands<T, F>;\r\n ensures?: Ensures<T, F>;\r\n rescue?: Rescue<T, F>;\r\n within?: number;\r\n}\r\n\r\nexport class Contract<T extends AnyObject> {\r\n #invariant: Invariant<T>;\r\n [checkedMode]: boolean;\r\n readonly [extend]?: Contract<T>;\r\n readonly assertions: ContractOptions<T> = Object.create(null);\r\n\r\n constructor(assertions: Partial<ContractOptions<T>> = Object.create(null)) {\r\n this[extend] = assertions[extend];\r\n this[checkedMode] = assertions[checkedMode] ?? true;\r\n this.#invariant = assertions[invariant] ?? fnTrue;\r\n\r\n Object.keys(assertions).forEach(propertyKey => {\r\n const featureOption = assertions[propertyKey]!;\r\n Object.defineProperty(this.assertions, propertyKey, {\r\n value: {\r\n demands: featureOption.demands ?? fnTrue,\r\n ensures: featureOption.ensures ?? fnTrue,\r\n rescue: featureOption.rescue,\r\n within: featureOption.within\r\n }\r\n });\r\n });\r\n\r\n deepFreeze(this.assertions);\r\n }\r\n\r\n get [invariant](): Invariant<T> {\r\n return this.#invariant;\r\n }\r\n}\r\n\r\nexport { checkedMode, extend, invariant };","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { checkedMode, Contract } from '../index.mjs';\r\n\r\n/**\r\n * Disable contract checking for the provided function\r\n * @param {Contract<any>} contract - The code contract\r\n * @param {function(...args: any[]): any} fn - The function to execute\r\n */\r\nconst unChecked = (contract: Contract<any>, fn: () => any) => {\r\n try {\r\n contract[checkedMode] = false;\r\n fn();\r\n } finally {\r\n contract[checkedMode] = true;\r\n }\r\n};\r\n\r\nexport default unChecked;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Demands, extend } from '../Contract.mjs';\r\nimport { assert, checkedMode, Contract } from '../index.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @param {string} className - The name of the class\r\n * @param {PropertyKey} featureName - The name of the feature\r\n * @param {any[]} args - The arguments of the feature to apply to the assertion\r\n * @throws {AssertionError}\r\n */\r\nfunction assertDemands<U extends object>(ctx: U, contract: Contract<U>, className: string, featureName: PropertyKey, args: any[]) {\r\n let result = true;\r\n const d: Demands<any, any> | undefined = Reflect.get(contract.assertions, featureName)?.demands,\r\n demandsError = `demands not met on ${className}.prototype.${String(featureName)}\\r\\n${d}`;\r\n if (contract[checkedMode])\r\n if (d)\r\n unChecked(contract, () =>\r\n result = d.call(ctx, ctx, ...args)\r\n );\r\n\r\n if (contract[extend] && !result)\r\n assertDemands(ctx, contract[extend], className, featureName, args);\r\n else\r\n assert(result, demandsError);\r\n}\r\n\r\nexport default assertDemands;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Ensures, extend, Properties } from '../Contract.mjs';\r\nimport { assert, checkedMode, Contract } from '../index.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @param {string} className - The name of the class\r\n * @param {PropertyKey} featureName - The name of the feature\r\n * @param {U} old - The original properties before execution\r\n * @param {any[]} args - The arguments of the feature to apply to the assertion\r\n * @throws {AssertionError}\r\n */\r\nfunction assertEnsures<U extends Properties<any>>(\r\n ctx: U,\r\n contract: Contract<any>,\r\n className: string,\r\n featureName: PropertyKey,\r\n old: U,\r\n args: any[]\r\n) {\r\n const e: Ensures<any, any> | undefined = Reflect.get(contract.assertions, featureName)?.ensures,\r\n ensuresError = `ensures not met on ${className}.prototype.${String(featureName)}\\r\\n${e}`;\r\n\r\n if (contract[checkedMode])\r\n if (e)\r\n unChecked(contract, () =>\r\n assert(e.call(ctx, ctx, old, ...args), ensuresError)\r\n );\r\n\r\n if (contract[extend])\r\n assertEnsures(ctx, contract[extend]!, className, featureName, old, args);\r\n}\r\n\r\nexport default assertEnsures;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { unChecked } from './index.mjs';\r\nimport { assert, checkedMode, Contract, extend, invariant } from '../index.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @throws {AssertionError}\r\n */\r\nfunction assertInvariants<U>(ctx: U, contract: Contract<any>) {\r\n if (contract[checkedMode])\r\n unChecked(contract, () => {\r\n const iv = contract[invariant];\r\n assert(iv.call(ctx, ctx), `Invariant violated. ${iv.toString()}`);\r\n });\r\n\r\n if (contract[extend])\r\n assertInvariants(ctx, contract[extend]!);\r\n}\r\n\r\nexport default assertInvariants;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { ClassRegistration, ClassType } from './index.mjs';\r\n\r\n/**\r\n * A WeakMap that tracks class registrations.\r\n * @see {@link ClassRegistration}\r\n */\r\nclass ClassRegistry extends WeakMap<ClassType<any>, ClassRegistration> {\r\n /**\r\n * Returns the class registry defined for the current class.\r\n * If the registry is undefined, a new one is created\r\n *\r\n * @param {ClassType<any>} Class - The class\r\n * @returns {ClassRegistration} - The ClassRegistration\r\n */\r\n getOrCreate(Class: ClassType<any>): ClassRegistration {\r\n if (this.has(Class))\r\n return this.get(Class)!;\r\n else {\r\n this.set(Class, new ClassRegistration(Class));\r\n\r\n return this.get(Class)!;\r\n }\r\n }\r\n}\r\n\r\nconst CLASS_REGISTRY = new ClassRegistry();\r\n\r\nexport default CLASS_REGISTRY;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nexport enum Messages {\r\n AssertionFailed = 'Assertion failure',\r\n MsgNoProperties = 'Public properties are forbidden',\r\n MsgNotContracted = 'The current class or one of its ancestors must declare @Contracted(...)',\r\n MsgMissingFeature = 'The requested feature is not registered',\r\n MsgSingleContract = 'Only a single @Contracted decorator is allowed per class',\r\n MsgSingleRetry = 'retry can only be called once',\r\n MsgInvalidContext = 'A contracted feature can not be applied to objects of a different base class',\r\n MsgBadSubcontract = 'A sub contract must extend a base contract'\r\n}","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Messages } from '../Messages.mjs';\r\nimport { assert, checkedMode, Contract, innerContract } from '../index.mjs';\r\nimport { assertInvariants, assertDemands, CLASS_REGISTRY, ClassType, Feature, unChecked } from './index.mjs';\r\nimport assertEnsures from './assertEnsures.mjs';\r\n\r\n/**\r\n * Manages the evaluation of contract assertions for a feature\r\n *\r\n * @param {string} featureName - The name of the feature\r\n * @param {function(...args: any[]): any} fnOrig - The original unchecked feature\r\n * @param {ClassRegistration} registration - The class registration\r\n *\r\n * @returns {function(...args: any[]): any} - The original function augments with assertion checks\r\n */\r\nfunction checkedFeature(\r\n featureName: PropertyKey,\r\n fnOrig: (...args: any[]) => any,\r\n registration: ClassRegistration\r\n) {\r\n return function innerCheckedFeature(this: any, ...args: any[]) {\r\n const { Class } = registration,\r\n className = Class.name;\r\n\r\n assert(this instanceof Class, `${Messages.MsgInvalidContext}. Expected: this instanceof ${className}. this: ${this.constructor.name}`);\r\n\r\n const contract = Reflect.get(this, innerContract);\r\n if (!contract[checkedMode])\r\n return fnOrig.apply(this, args);\r\n\r\n assertInvariants(this, contract);\r\n assertDemands(this, contract, className, featureName, args);\r\n\r\n let result;\r\n try {\r\n const old = Object.create(null);\r\n unChecked(contract, () => {\r\n registration.features.forEach(({ name, hasGetter }) => {\r\n if (hasGetter)\r\n Object.defineProperty(old, name, { value: this[name] });\r\n });\r\n });\r\n const within: number = Reflect.get(contract.assertions, featureName)?.within,\r\n // TODO:Not all environments currently support performance.now() as a global and conditional import is inconvenient\r\n t0 = Date.now();\r\n result = fnOrig.apply(this, args);\r\n const t1 = Date.now();\r\n if (within)\r\n assert(t1 - t0 < within,\r\n `Timing constraint violated. Constraint: ${within}ms, actual: ${t1 - t0}ms`\r\n );\r\n\r\n assertEnsures(this, contract, className, featureName, old, args);\r\n } catch (error) {\r\n const rescue = Reflect.get(contract.assertions, featureName)?.rescue;\r\n if (rescue == null) {\r\n assertInvariants(this, contract);\r\n throw error;\r\n }\r\n let hasRetried = false;\r\n unChecked(contract, () => {\r\n rescue.call(this, this, error, [], (...args: any[]) => {\r\n assert(!hasRetried, Messages.MsgSingleRetry);\r\n hasRetried = true;\r\n contract[checkedMode] = true;\r\n result = innerCheckedFeature.call(this, ...args);\r\n });\r\n });\r\n if (!hasRetried) {\r\n assertInvariants(this, contract);\r\n throw error;\r\n }\r\n }\r\n\r\n assertInvariants(this, contract);\r\n\r\n return result;\r\n };\r\n}\r\n\r\nclass ClassRegistration {\r\n #features: Feature[];\r\n contract!: Contract<any>; // Assigned by the Contracted class via this.bindContract\r\n contractsChecked = false;\r\n\r\n constructor(readonly Class: ClassType<any>) {\r\n const proto = this.Class.prototype;\r\n this.#features =\r\n Reflect.ownKeys(proto)\r\n .filter(key => key != 'constructor' && key !== innerContract)\r\n .map(key => new Feature(this, key, Object.getOwnPropertyDescriptor(proto, key)!));\r\n }\r\n\r\n /**\r\n * Returns the features associated with the registered class.\r\n *\r\n * @returns {Set<Feature>} The set of features\r\n */\r\n get features(): Feature[] {\r\n return [...this.#features];\r\n }\r\n\r\n /**\r\n * Returns a reference to the parent class\r\n */\r\n get ParentClass(): ClassType<any> | null {\r\n return Object.getPrototypeOf(this.Class.prototype)?.constructor;\r\n }\r\n\r\n /**\r\n * Returns the classRegistration of the parent class\r\n */\r\n get parentRegistration(): ClassRegistration | null {\r\n return this.ParentClass == null ? null : CLASS_REGISTRY.getOrCreate(this.ParentClass);\r\n }\r\n\r\n /**\r\n * Returns the registered class's ancestor class registrations.\r\n * Does not include the current class.\r\n *\r\n * @returns {ClassRegistration[]} - The ancestor class registrations\r\n */\r\n ancestry(): ClassRegistration[] {\r\n if (this.ParentClass == null)\r\n return [];\r\n else {\r\n const parentRegistry = CLASS_REGISTRY.getOrCreate(this.ParentClass);\r\n\r\n return [parentRegistry, ...parentRegistry.ancestry()];\r\n }\r\n }\r\n\r\n /**\r\n * Returns the features associated with the registered class's ancestors.\r\n * Does not include the current class.\r\n *\r\n * @returns {Feature[]} - The feature names\r\n */\r\n ancestryFeatures(): Feature[] {\r\n return this.ancestry().flatMap(({ features }) => features);\r\n }\r\n\r\n bindContract<T extends Contract<any>>(contract: T) {\r\n this.contract = contract;\r\n if (!contract[checkedMode])\r\n return;\r\n\r\n const proto = this.Class.prototype;\r\n this.features.forEach(feature => {\r\n const { name, hasGetter, hasSetter, isMethod } = feature;\r\n\r\n Object.defineProperty(proto, name, {\r\n enumerable: true,\r\n ...(hasGetter ? { get: checkedFeature(name, feature.getter!, this) } : {}),\r\n ...(hasSetter ? { set: checkedFeature(name, feature.setter!, this) } : {}),\r\n ...(isMethod ? { value: checkedFeature(name, feature.value, this) } : {})\r\n });\r\n\r\n feature.descriptor = Object.getOwnPropertyDescriptor(proto, name)!;\r\n });\r\n }\r\n\r\n /**\r\n * Searches the current class and its ancestors for the nearest feature\r\n * matching the provided propertyKey.\r\n *\r\n * @param {PropertyKey} propertyKey - The key being searched\r\n * @returns {Feature | undefined} - The feature if it exists else otherwise\r\n */\r\n findFeature(propertyKey: PropertyKey): Feature | undefined {\r\n return this.features.find(({ name }) => name === propertyKey) ??\r\n this.parentRegistration?.findFeature(propertyKey);\r\n }\r\n}\r\n\r\nexport default ClassRegistration;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Apply Object.freeze recursively\r\n * @param {object} object - The object to freeze\r\n */\r\nfunction deepFreeze(object: Record<PropertyKey, any>): void {\r\n // Freeze properties before freezing self\r\n for (const [, value] of Object.entries(object))\r\n if (value != undefined && typeof value === 'object')\r\n deepFreeze(value);\r\n\r\n Object.freeze(object);\r\n}\r\n\r\nexport default deepFreeze;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport ClassRegistration from './ClassRegistration.mjs';\r\n\r\nclass Feature {\r\n #descriptor: PropertyDescriptor;\r\n\r\n constructor(readonly classRegistration: ClassRegistration, readonly name: PropertyKey, descriptor: PropertyDescriptor) {\r\n this.#descriptor = descriptor;\r\n }\r\n\r\n /**\r\n * Finds the nearest ancestor feature of the same key if it exists\r\n */\r\n get ancestorFeature(): Feature | null {\r\n return this.classRegistration.ancestryFeatures().filter(feature => feature.name === this.name)[0];\r\n }\r\n\r\n /**\r\n * Returns a reference to the getter if it exists\r\n */\r\n get getter() {\r\n return this.#descriptor.get;\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor with a getter defined\r\n */\r\n get hasGetter(): boolean {\r\n return this.isAccessor && this.#descriptor.get != undefined;\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor with a setter defined\r\n */\r\n get hasSetter(): boolean {\r\n return this.isAccessor && this.#descriptor.set != undefined;\r\n }\r\n\r\n /**\r\n * Determines if the feature is a property\r\n */\r\n get isProperty(): boolean {\r\n return typeof this.#descriptor.value != 'function' &&\r\n typeof this.#descriptor.value != 'undefined';\r\n }\r\n\r\n /**\r\n * Determines if the feature is a method\r\n */\r\n get isMethod(): boolean {\r\n return typeof this.#descriptor.value == 'function';\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor\r\n */\r\n get isAccessor(): boolean {\r\n return typeof this.#descriptor.value == 'undefined';\r\n }\r\n\r\n /**\r\n * Returns a string representing the type of feature.\r\n * @returns {string} 'method' | 'property' | 'accessor'\r\n */\r\n get memberType(): 'method' | 'property' | 'accessor' {\r\n return this.isMethod ? 'method' :\r\n this.isProperty ? 'property' :\r\n 'accessor';\r\n }\r\n\r\n /**\r\n * Returns a reference to the setter if it exists\r\n */\r\n get setter() {\r\n return this.#descriptor.set;\r\n }\r\n\r\n /**\r\n * If the feature is a method, returns a reference to the implementation.\r\n * If the feature is a property, returns the value of the property\r\n * Otherwise returns undefined\r\n */\r\n get value(): any {\r\n return this.#descriptor.value;\r\n }\r\n\r\n /**\r\n * Returns a reference to the descriptor\r\n */\r\n get descriptor() {\r\n return this.#descriptor;\r\n }\r\n set descriptor(desc: PropertyDescriptor) {\r\n this.#descriptor = desc;\r\n }\r\n}\r\n\r\nexport default Feature;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Creates a new array from another one. Items from the original array\r\n * are taken if the provided predicate succeed. When the predicate fails,\r\n * no new items are taken\r\n *\r\n * @param {T[]} xs - The array to evaluate\r\n * @param {function(x:T): T[]} pred - The condition to apply to every element\r\n * @returns {T[]} - The filtered list\r\n */\r\nexport function takeWhile<T>(xs: T[], pred: (x: T) => boolean): T[] {\r\n return xs.length && pred(xs[0]) ? [xs[0], ...takeWhile(xs.slice(1), pred)] : [];\r\n}\r\n\r\nexport default takeWhile;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport assertDemands from './assertDemands.mjs';\r\nimport assertEnsures from './assertEnsures.mjs';\r\nimport assertInvariants from './assertInvariants.mjs';\r\nimport CLASS_REGISTRY from './CLASS_REGISTRY.mjs';\r\nimport ClassRegistration from './ClassRegistration.mjs';\r\nimport { ClassType } from './ClassType.mjs';\r\nimport deepFreeze from './deepFreeze.mjs';\r\nimport Feature from './Feature.mjs';\r\nimport takeWhile from './takeWhile.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\nconst fnTrue = () => true;\r\n\r\nexport {\r\n assertDemands,\r\n assertEnsures,\r\n assertInvariants,\r\n CLASS_REGISTRY,\r\n ClassRegistration,\r\n ClassType,\r\n deepFreeze,\r\n Feature,\r\n fnTrue,\r\n takeWhile,\r\n unChecked\r\n};","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { ClassRegistration, CLASS_REGISTRY, ClassType, Feature, takeWhile, assertInvariants } from './lib/index.mjs';\r\nimport { assert, checkedMode, Contract, extend } from './index.mjs';\r\nimport { Messages } from './Messages.mjs';\r\n\r\nconst isContracted = Symbol('isContracted'),\r\n innerContract = Symbol('innerContract');\r\n\r\n/**\r\n * Checks the features of the provided object for properties.\r\n * If any are found an exception is thrown\r\n * @param {ClassRegistration} registration - The ClassRegistration for the object\r\n * @param {U} obj - The object to check\r\n * @throws {AssertionError} - Throws if a property is found\r\n * @returns {boolean} - The result of the test\r\n */\r\nfunction hasProperties<U>(registration: ClassRegistration, obj: U): boolean {\r\n return Object.entries(Object.getOwnPropertyDescriptors(obj))\r\n .some(([key, desc]) => new Feature(registration, key, desc).isProperty);\r\n}\r\n\r\n/**\r\n * Associates a contract with a class via the mixin pattern.\r\n *\r\n * @param {Contract} contract The Contract definition\r\n * @param {T} Base An optional base class\r\n * @returns {T} The base class for extension\r\n * @example\r\n *\r\n * @Contracted(stackContract)\r\n * class Stack<T> { ... }\r\n */\r\nfunction Contracted<\r\n T extends Contract<any> = Contract<any>,\r\n U extends ClassType<any> = ClassType<any>\r\n>(contract: T = new Contract() as T) {\r\n return function (Clazz: U & { [innerContract]?: Contract<any> }, ctx: ClassDecoratorContext<U>) {\r\n if (ctx.kind !== 'class')\r\n throw new TypeError(Messages.MsgNotContracted);\r\n\r\n assert(!Object.getOwnPropertySymbols(Clazz).includes(isContracted), Messages.MsgSingleContract);\r\n\r\n if (contract[checkedMode] === false)\r\n return Clazz;\r\n\r\n const baseContract = Clazz[innerContract];\r\n assert(\r\n !baseContract ||\r\n baseContract && contract[extend] instanceof baseContract.constructor,\r\n Messages.MsgBadSubcontract\r\n );\r\n\r\n abstract class InnerContracted extends Clazz {\r\n // prevents multiple @Contracted decorators from being applied\r\n static readonly [isContracted] = true;\r\n\r\n constructor(...args: any[]) {\r\n super(...args);\r\n\r\n const Class = this.constructor as ClassType<any>,\r\n classRegistration = CLASS_REGISTRY.getOrCreate(Class);\r\n\r\n assert(!hasProperties(classRegistration, this), Messages.MsgNoProperties);\r\n\r\n if (!classRegistration.contractsChecked) {\r\n // bottom-up to closest Contracted class bind contracts\r\n const ancRegistrations = takeWhile(classRegistration.ancestry(), (cr => cr.Class !== Clazz));\r\n [classRegistration, ...ancRegistrations, CLASS_REGISTRY.get(Clazz)!].forEach(registration => {\r\n registration.bindContract(InnerContracted[innerContract]);\r\n });\r\n }\r\n\r\n assertInvariants(this, InnerContracted[innerContract]);\r\n\r\n return this;\r\n }\r\n\r\n // FIXME: dirty hack. Possibly resolved by moving to contracts as classes\r\n // The static getter is used by the construction invariant check\r\n // The instance getter is used by the feature declarations\r\n static get [innerContract]() { return contract; }\r\n get [innerContract]() { return contract; }\r\n }\r\n\r\n const classRegistration = CLASS_REGISTRY.getOrCreate(InnerContracted);\r\n classRegistration.contractsChecked = false;\r\n\r\n Object.freeze(Clazz);\r\n\r\n return InnerContracted;\r\n };\r\n}\r\n\r\nexport { isContracted, innerContract };\r\nexport default Contracted;","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport AssertionError from './AssertionError.mjs';\r\nimport { Messages } from './Messages.mjs';\r\nimport { Constructor } from './index.mjs';\r\n\r\n/**\r\n * An assertion is an expression of a property that must be true at a particular\r\n * point of step of program execution. It consists of a boolean expression\r\n * and a message. This function tests the provided condition. If the condition\r\n * is false an AssertionError is raised with an optional message.\r\n * If the provided condition is true then the function returns without raising an error\r\n *\r\n * @param {boolean} condition - The condition to test\r\n * @param {string} message - A descriptive message to associate with the AssertionError\r\n * @param {ClassType<Error>} ErrorConstructor - The constructor of the Error to use\r\n * @throws {Error} - When the condition is false\r\n * @see AssertionError\r\n * @throws {AssertionError} - Throws an AssertionError by default if the condition is false\r\n *\r\n * @example\r\n * let x = 15;\r\n * assert(x > 5, `Expected: x > 5. Actual: x = ${x}`)\r\n *\r\n * @example\r\n * let name = \"Tom\"\r\n * assert(name.trim().length > 0, 'Name is required', TypeError)\r\n */\r\nexport default function assert(condition: unknown, message: string = Messages.AssertionFailed, ErrorConstructor: Constructor<Error> = AssertionError): asserts condition {\r\n if (Boolean(condition) == false)\r\n throw new ErrorConstructor(message);\r\n}","/*!\r\n * @license\r\n * Copyright (C) 2023 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Material Implication.\r\n * `p → q`\r\n * Also referred to as `if-then`.\r\n * An example of usage is the encoding of \"sunny weather is a precondition of visiting the beach\"\r\n * This is logically equivalent to: !p || q\r\n *\r\n * @example\r\n * implies(weather.isSunny, person.visitsBeach)\r\n *\r\n * @param {boolean} p - The antecedent\r\n * @param {boolean} q - The consequent\r\n * @returns {boolean} - The result\r\n */\r\nexport default function implies(p: boolean, q: boolean): boolean {\r\n return !p || q;\r\n}"],"names":["__webpack_require__","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","AssertionError","Error","constructor","message","super","checkedMode","Symbol","invariant","extend","Contract","assertions","create","this","fnTrue","keys","forEach","propertyKey","featureOption","value","demands","ensures","rescue","within","contract","fn","assertDemands","ctx","className","featureName","args","result","d","Reflect","demandsError","String","assert","assertEnsures","old","e","ensuresError","assertInvariants","iv","toString","ClassRegistry","WeakMap","getOrCreate","Class","has","set","Messages","checkedFeature","fnOrig","registration","innerCheckedFeature","name","MsgInvalidContext","innerContract","apply","features","hasGetter","t0","Date","now","t1","error","hasRetried","MsgSingleRetry","contractsChecked","proto","ownKeys","filter","map","getOwnPropertyDescriptor","ParentClass","getPrototypeOf","parentRegistration","ancestry","parentRegistry","ancestryFeatures","flatMap","bindContract","feature","hasSetter","isMethod","getter","setter","descriptor","findFeature","find","deepFreeze","object","entries","undefined","freeze","classRegistration","ancestorFeature","isAccessor","isProperty","memberType","desc","takeWhile","xs","pred","length","slice","isContracted","Clazz","kind","TypeError","MsgNotContracted","getOwnPropertySymbols","includes","MsgSingleContract","baseContract","MsgBadSubcontract","InnerContracted","static","getOwnPropertyDescriptors","some","MsgNoProperties","ancRegistrations","cr","condition","AssertionFailed","ErrorConstructor","Boolean","implies","p","q"],"sourceRoot":""}
|
|
1
|
+
{"version":3,"file":"index.mjs","mappings":";AACA,IAAIA,EAAsB,CCA1BA,EAAwB,CAACC,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXF,EAAoBI,EAAEF,EAAYC,KAASH,EAAoBI,EAAEH,EAASE,IAC5EE,OAAOC,eAAeL,EAASE,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,IAE1E,ECNDH,EA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/webpack/runtime/define property getters","webpack://@final-hill/decorator-contracts/webpack/runtime/hasOwnProperty shorthand","webpack://@final-hill/decorator-contracts/./src/AssertionError.mts","webpack://@final-hill/decorator-contracts/./src/Contract.mts","webpack://@final-hill/decorator-contracts/./src/lib/unChecked.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertDemands.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertEnsures.mts","webpack://@final-hill/decorator-contracts/./src/lib/assertInvariants.mts","webpack://@final-hill/decorator-contracts/./src/lib/classRegistry.mts","webpack://@final-hill/decorator-contracts/./src/Messages.mts","webpack://@final-hill/decorator-contracts/./src/lib/ClassRegistration.mts","webpack://@final-hill/decorator-contracts/./src/lib/deepFreeze.mts","webpack://@final-hill/decorator-contracts/./src/lib/Feature.mts","webpack://@final-hill/decorator-contracts/./src/lib/takeWhile.mts","webpack://@final-hill/decorator-contracts/./src/lib/index.mts","webpack://@final-hill/decorator-contracts/./src/Contracted.mts","webpack://@final-hill/decorator-contracts/./src/assert.mts","webpack://@final-hill/decorator-contracts/./src/implies.mts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * An AssertionError represents a failed assertion with an associated message\r\n */\r\nclass AssertionError extends Error {\r\n /**\r\n * Constructs a new instance of AssertionError\r\n *\r\n * @param {string} message - The associated message\r\n */\r\n constructor(message: string) {\r\n super(message);\r\n }\r\n}\r\n\r\nexport default AssertionError;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { deepFreeze, fnTrue } from './lib/index.mjs';\r\n\r\nconst checkedMode = Symbol('checkedMode'),\r\n invariant = Symbol('invariant'),\r\n extend = Symbol('extend');\r\n\r\ntype AnyObject = Record<PropertyKey, any>;\r\ntype AnyFunc = (...args: any[]) => any;\r\n\r\ntype NonFunctionPropertyNames<T extends AnyObject> = { [K in keyof T]: T[K] extends AnyFunc ? never : K }[keyof T];\r\nexport type Properties<T extends AnyObject> = Pick<T, NonFunctionPropertyNames<T>>;\r\n\r\nexport type Invariant<T extends AnyObject> = (self: T) => boolean;\r\nexport type Demands<T extends AnyObject, F extends T[any]> = (self: T, ...args: Parameters<F>) => boolean;\r\nexport type Ensures<T extends AnyObject, F extends T[any]> = (self: T, old: Properties<T>, ...args: Parameters<F>) => boolean;\r\nexport type Rescue<T extends AnyObject, F extends T[any]> = (\r\n self: T,\r\n error: Error,\r\n args: F extends AnyFunc ? Parameters<F> : [F],\r\n retry: F extends AnyFunc ? (...args: Parameters<F>) => any : (value: F) => void\r\n) => void;\r\n\r\nexport type ContractOptions<\r\n T extends AnyObject\r\n> = {\r\n [extend]: Contract<T>;\r\n [invariant]: Invariant<T>;\r\n [checkedMode]: boolean;\r\n} & {\r\n [K in keyof T]?: FeatureOption<T, T[K]>\r\n };\r\n\r\nexport interface FeatureOption<T extends AnyObject, F extends T[any]> {\r\n demands?: Demands<T, F>;\r\n ensures?: Ensures<T, F>;\r\n rescue?: Rescue<T, F>;\r\n within?: number;\r\n}\r\n\r\nexport class Contract<T extends AnyObject> {\r\n #invariant: Invariant<T>;\r\n [checkedMode]: boolean;\r\n readonly [extend]?: Contract<T>;\r\n readonly assertions: ContractOptions<T> = Object.create(null);\r\n\r\n constructor(assertions: Partial<ContractOptions<T>> = Object.create(null)) {\r\n this[extend] = assertions[extend];\r\n this[checkedMode] = assertions[checkedMode] ?? true;\r\n this.#invariant = assertions[invariant] ?? fnTrue;\r\n\r\n Object.keys(assertions).forEach(propertyKey => {\r\n const featureOption = assertions[propertyKey]!;\r\n Object.defineProperty(this.assertions, propertyKey, {\r\n value: {\r\n demands: featureOption.demands ?? fnTrue,\r\n ensures: featureOption.ensures ?? fnTrue,\r\n rescue: featureOption.rescue,\r\n within: featureOption.within\r\n }\r\n });\r\n });\r\n\r\n deepFreeze(this.assertions);\r\n }\r\n\r\n get [invariant](): Invariant<T> {\r\n return this.#invariant;\r\n }\r\n}\r\n\r\nexport { checkedMode, extend, invariant };","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { checkedMode, Contract } from '../index.mjs';\r\n\r\n/**\r\n * Disable contract checking for the provided function\r\n * @param {Contract<any>} contract - The code contract\r\n * @param {function(...args: any[]): any} fn - The function to execute\r\n */\r\nconst unChecked = (contract: Contract<any>, fn: () => any) => {\r\n try {\r\n contract[checkedMode] = false;\r\n fn();\r\n } finally {\r\n contract[checkedMode] = true;\r\n }\r\n};\r\n\r\nexport default unChecked;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Demands, extend } from '../Contract.mjs';\r\nimport { assert, checkedMode, Contract } from '../index.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @param {string} className - The name of the class\r\n * @param {PropertyKey} featureName - The name of the feature\r\n * @param {any[]} args - The arguments of the feature to apply to the assertion\r\n * @throws {AssertionError}\r\n */\r\nfunction assertDemands<U extends object>(ctx: U, contract: Contract<U>, className: string, featureName: PropertyKey, args: any[]) {\r\n let result = true;\r\n const d: Demands<any, any> | undefined = Reflect.get(contract.assertions, featureName)?.demands,\r\n demandsError = `demands not met on ${className}.prototype.${String(featureName)}\\r\\n${d}`;\r\n if (contract[checkedMode])\r\n if (d)\r\n unChecked(contract, () =>\r\n result = d.call(ctx, ctx, ...args)\r\n );\r\n\r\n if (contract[extend] && !result)\r\n assertDemands(ctx, contract[extend], className, featureName, args);\r\n else\r\n assert(result, demandsError);\r\n}\r\n\r\nexport default assertDemands;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Ensures, extend, Properties } from '../Contract.mjs';\r\nimport { assert, checkedMode, Contract } from '../index.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @param {string} className - The name of the class\r\n * @param {PropertyKey} featureName - The name of the feature\r\n * @param {U} old - The original properties before execution\r\n * @param {any[]} args - The arguments of the feature to apply to the assertion\r\n * @throws {AssertionError}\r\n */\r\nfunction assertEnsures<U extends Properties<any>>(\r\n ctx: U,\r\n contract: Contract<any>,\r\n className: string,\r\n featureName: PropertyKey,\r\n old: U,\r\n args: any[]\r\n) {\r\n const e: Ensures<any, any> | undefined = Reflect.get(contract.assertions, featureName)?.ensures,\r\n ensuresError = `ensures not met on ${className}.prototype.${String(featureName)}\\r\\n${e}`;\r\n\r\n if (contract[checkedMode])\r\n if (e)\r\n unChecked(contract, () =>\r\n assert(e.call(ctx, ctx, old, ...args), ensuresError)\r\n );\r\n\r\n if (contract[extend])\r\n assertEnsures(ctx, contract[extend]!, className, featureName, old, args);\r\n}\r\n\r\nexport default assertEnsures;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { unChecked } from './index.mjs';\r\nimport { assert, checkedMode, Contract, extend, invariant } from '../index.mjs';\r\n\r\n/**\r\n * Applies invariant assertions to the provided class instance\r\n *\r\n * @param {U} ctx - The context class\r\n * @param {Contract<any>} contract - The contract\r\n * @throws {AssertionError}\r\n */\r\nfunction assertInvariants<U>(ctx: U, contract: Contract<any>) {\r\n if (contract[checkedMode])\r\n unChecked(contract, () => {\r\n const iv = contract[invariant];\r\n assert(iv.call(ctx, ctx), `Invariant violated. ${iv.toString()}`);\r\n });\r\n\r\n if (contract[extend])\r\n assertInvariants(ctx, contract[extend]!);\r\n}\r\n\r\nexport default assertInvariants;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { ClassRegistration, ClassType } from './index.mjs';\r\n\r\n/**\r\n * A WeakMap that tracks class registrations.\r\n * @see {@link ClassRegistration}\r\n */\r\nclass ClassRegistry extends WeakMap<ClassType<any>, ClassRegistration> {\r\n /**\r\n * Returns the class registry defined for the current class.\r\n * If the registry is undefined, a new one is created\r\n *\r\n * @param {ClassType<any>} Class - The class\r\n * @returns {ClassRegistration} - The ClassRegistration\r\n */\r\n getOrCreate(Class: ClassType<any>): ClassRegistration {\r\n if (this.has(Class))\r\n return this.get(Class)!;\r\n else {\r\n this.set(Class, new ClassRegistration(Class));\r\n\r\n return this.get(Class)!;\r\n }\r\n }\r\n}\r\n\r\nexport default new ClassRegistry();","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nexport enum Messages {\r\n AssertionFailed = 'Assertion failure',\r\n MsgNoProperties = 'Public properties are forbidden',\r\n MsgNotContracted = 'The current class or one of its ancestors must declare @Contracted(...)',\r\n MsgMissingFeature = 'The requested feature is not registered',\r\n MsgSingleContract = 'Only a single @Contracted decorator is allowed per class',\r\n MsgSingleRetry = 'retry can only be called once',\r\n MsgInvalidContext = 'A contracted feature can not be applied to objects of a different base class',\r\n MsgBadSubcontract = 'A sub contract must extend a base contract'\r\n}","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { Messages } from '../Messages.mjs';\r\nimport { assert, checkedMode, Contract, innerContract } from '../index.mjs';\r\nimport { assertInvariants, assertDemands, classRegistry, ClassType, Feature, unChecked } from './index.mjs';\r\nimport assertEnsures from './assertEnsures.mjs';\r\n\r\n/**\r\n * Manages the evaluation of contract assertions for a feature\r\n *\r\n * @param {string} featureName - The name of the feature\r\n * @param {function(...args: any[]): any} fnOrig - The original unchecked feature\r\n * @param {ClassRegistration} registration - The class registration\r\n *\r\n * @returns {function(...args: any[]): any} - The original function augments with assertion checks\r\n */\r\nfunction checkedFeature(\r\n featureName: PropertyKey,\r\n fnOrig: (...args: any[]) => any,\r\n registration: ClassRegistration\r\n) {\r\n return function innerCheckedFeature(this: any, ...args: any[]) {\r\n const { Class } = registration,\r\n className = Class.name;\r\n\r\n assert(this instanceof Class, `${Messages.MsgInvalidContext}. Expected: this instanceof ${className}. this: ${this.constructor.name}`);\r\n\r\n const contract = Reflect.get(this, innerContract);\r\n if (!contract[checkedMode])\r\n return fnOrig.apply(this, args);\r\n\r\n assertInvariants(this, contract);\r\n assertDemands(this, contract, className, featureName, args);\r\n\r\n let result;\r\n try {\r\n const old = Object.create(null);\r\n unChecked(contract, () => {\r\n registration.features.forEach(({ name, hasGetter }) => {\r\n if (hasGetter)\r\n Object.defineProperty(old, name, { value: this[name] });\r\n });\r\n });\r\n const within: number = Reflect.get(contract.assertions, featureName)?.within,\r\n // TODO:Not all environments currently support performance.now() as a global and conditional import is inconvenient\r\n t0 = Date.now();\r\n result = fnOrig.apply(this, args);\r\n const t1 = Date.now();\r\n if (within)\r\n assert(t1 - t0 < within,\r\n `Timing constraint violated. Constraint: ${within}ms, actual: ${t1 - t0}ms`\r\n );\r\n\r\n assertEnsures(this, contract, className, featureName, old, args);\r\n } catch (error) {\r\n const rescue = Reflect.get(contract.assertions, featureName)?.rescue;\r\n if (rescue == null) {\r\n assertInvariants(this, contract);\r\n throw error;\r\n }\r\n let hasRetried = false;\r\n unChecked(contract, () => {\r\n rescue.call(this, this, error, [], (...args: any[]) => {\r\n assert(!hasRetried, Messages.MsgSingleRetry);\r\n hasRetried = true;\r\n contract[checkedMode] = true;\r\n result = innerCheckedFeature.call(this, ...args);\r\n });\r\n });\r\n if (!hasRetried) {\r\n assertInvariants(this, contract);\r\n throw error;\r\n }\r\n }\r\n\r\n assertInvariants(this, contract);\r\n\r\n return result;\r\n };\r\n}\r\n\r\nclass ClassRegistration {\r\n #features: Feature[];\r\n contract!: Contract<any>; // Assigned by the Contracted class via this.bindContract\r\n contractsChecked = false;\r\n\r\n constructor(readonly Class: ClassType<any>) {\r\n const proto = this.Class.prototype;\r\n this.#features =\r\n Reflect.ownKeys(proto)\r\n .filter(key => key != 'constructor' && key !== innerContract)\r\n .map(key => new Feature(this, key, Object.getOwnPropertyDescriptor(proto, key)!));\r\n }\r\n\r\n /**\r\n * Returns the features associated with the registered class.\r\n *\r\n * @returns {Set<Feature>} The set of features\r\n */\r\n get features(): Feature[] {\r\n return [...this.#features];\r\n }\r\n\r\n /**\r\n * Returns a reference to the parent class\r\n */\r\n get ParentClass(): ClassType<any> | null {\r\n return Object.getPrototypeOf(this.Class.prototype)?.constructor;\r\n }\r\n\r\n /**\r\n * Returns the classRegistration of the parent class\r\n */\r\n get parentRegistration(): ClassRegistration | null {\r\n return this.ParentClass == null ? null : classRegistry.getOrCreate(this.ParentClass);\r\n }\r\n\r\n /**\r\n * Returns the registered class's ancestor class registrations.\r\n * Does not include the current class.\r\n *\r\n * @returns {ClassRegistration[]} - The ancestor class registrations\r\n */\r\n ancestry(): ClassRegistration[] {\r\n if (this.ParentClass == null)\r\n return [];\r\n else {\r\n const parentRegistry = classRegistry.getOrCreate(this.ParentClass);\r\n\r\n return [parentRegistry, ...parentRegistry.ancestry()];\r\n }\r\n }\r\n\r\n /**\r\n * Returns the features associated with the registered class's ancestors.\r\n * Does not include the current class.\r\n *\r\n * @returns {Feature[]} - The feature names\r\n */\r\n ancestryFeatures(): Feature[] {\r\n return this.ancestry().flatMap(({ features }) => features);\r\n }\r\n\r\n bindContract<T extends Contract<any>>(contract: T) {\r\n this.contract = contract;\r\n if (!contract[checkedMode])\r\n return;\r\n\r\n const proto = this.Class.prototype;\r\n this.features.forEach(feature => {\r\n const { name, hasGetter, hasSetter, isMethod } = feature;\r\n\r\n Object.defineProperty(proto, name, {\r\n enumerable: true,\r\n ...(hasGetter ? { get: checkedFeature(name, feature.getter!, this) } : {}),\r\n ...(hasSetter ? { set: checkedFeature(name, feature.setter!, this) } : {}),\r\n ...(isMethod ? { value: checkedFeature(name, feature.value, this) } : {})\r\n });\r\n\r\n feature.descriptor = Object.getOwnPropertyDescriptor(proto, name)!;\r\n });\r\n }\r\n\r\n /**\r\n * Searches the current class and its ancestors for the nearest feature\r\n * matching the provided propertyKey.\r\n *\r\n * @param {PropertyKey} propertyKey - The key being searched\r\n * @returns {Feature | undefined} - The feature if it exists else otherwise\r\n */\r\n findFeature(propertyKey: PropertyKey): Feature | undefined {\r\n return this.features.find(({ name }) => name === propertyKey) ??\r\n this.parentRegistration?.findFeature(propertyKey);\r\n }\r\n}\r\n\r\nexport default ClassRegistration;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Apply Object.freeze recursively\r\n * @param {object} object - The object to freeze\r\n */\r\nfunction deepFreeze(object: Record<PropertyKey, any>): void {\r\n // Freeze properties before freezing self\r\n for (const [, value] of Object.entries(object))\r\n if (value != undefined && typeof value === 'object')\r\n deepFreeze(value);\r\n\r\n Object.freeze(object);\r\n}\r\n\r\nexport default deepFreeze;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport ClassRegistration from './ClassRegistration.mjs';\r\n\r\nclass Feature {\r\n #descriptor: PropertyDescriptor;\r\n\r\n constructor(readonly classRegistration: ClassRegistration, readonly name: PropertyKey, descriptor: PropertyDescriptor) {\r\n this.#descriptor = descriptor;\r\n }\r\n\r\n /**\r\n * Finds the nearest ancestor feature of the same key if it exists\r\n */\r\n get ancestorFeature(): Feature | null {\r\n return this.classRegistration.ancestryFeatures().filter(feature => feature.name === this.name)[0];\r\n }\r\n\r\n /**\r\n * Returns a reference to the getter if it exists\r\n */\r\n get getter() {\r\n return this.#descriptor.get;\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor with a getter defined\r\n */\r\n get hasGetter(): boolean {\r\n return this.isAccessor && this.#descriptor.get != undefined;\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor with a setter defined\r\n */\r\n get hasSetter(): boolean {\r\n return this.isAccessor && this.#descriptor.set != undefined;\r\n }\r\n\r\n /**\r\n * Determines if the feature is a property\r\n */\r\n get isProperty(): boolean {\r\n return typeof this.#descriptor.value != 'function' &&\r\n typeof this.#descriptor.value != 'undefined';\r\n }\r\n\r\n /**\r\n * Determines if the feature is a method\r\n */\r\n get isMethod(): boolean {\r\n return typeof this.#descriptor.value == 'function';\r\n }\r\n\r\n /**\r\n * Determines if the feature is an accessor\r\n */\r\n get isAccessor(): boolean {\r\n return typeof this.#descriptor.value == 'undefined';\r\n }\r\n\r\n /**\r\n * Returns a string representing the type of feature.\r\n * @returns {string} 'method' | 'property' | 'accessor'\r\n */\r\n get memberType(): 'method' | 'property' | 'accessor' {\r\n return this.isMethod ? 'method' :\r\n this.isProperty ? 'property' :\r\n 'accessor';\r\n }\r\n\r\n /**\r\n * Returns a reference to the setter if it exists\r\n */\r\n get setter() {\r\n return this.#descriptor.set;\r\n }\r\n\r\n /**\r\n * If the feature is a method, returns a reference to the implementation.\r\n * If the feature is a property, returns the value of the property\r\n * Otherwise returns undefined\r\n */\r\n get value(): any {\r\n return this.#descriptor.value;\r\n }\r\n\r\n /**\r\n * Returns a reference to the descriptor\r\n */\r\n get descriptor() {\r\n return this.#descriptor;\r\n }\r\n set descriptor(desc: PropertyDescriptor) {\r\n this.#descriptor = desc;\r\n }\r\n}\r\n\r\nexport default Feature;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Creates a new array from another one. Items from the original array\r\n * are taken if the provided predicate succeed. When the predicate fails,\r\n * no new items are taken\r\n *\r\n * @param {T[]} xs - The array to evaluate\r\n * @param {function(x:T): T[]} pred - The condition to apply to every element\r\n * @returns {T[]} - The filtered list\r\n */\r\nexport function takeWhile<T>(xs: T[], pred: (x: T) => boolean): T[] {\r\n return xs.length && pred(xs[0]) ? [xs[0], ...takeWhile(xs.slice(1), pred)] : [];\r\n}\r\n\r\nexport default takeWhile;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport assertDemands from './assertDemands.mjs';\r\nimport assertEnsures from './assertEnsures.mjs';\r\nimport assertInvariants from './assertInvariants.mjs';\r\nimport classRegistry from './classRegistry.mjs';\r\nimport ClassRegistration from './ClassRegistration.mjs';\r\nimport { type ClassType } from './ClassType.mjs';\r\nimport deepFreeze from './deepFreeze.mjs';\r\nimport Feature from './Feature.mjs';\r\nimport takeWhile from './takeWhile.mjs';\r\nimport unChecked from './unChecked.mjs';\r\n\r\nconst fnTrue = () => true;\r\n\r\nexport {\r\n assertDemands,\r\n assertEnsures,\r\n assertInvariants,\r\n classRegistry,\r\n ClassRegistration,\r\n ClassType,\r\n deepFreeze,\r\n Feature,\r\n fnTrue,\r\n takeWhile,\r\n unChecked\r\n};","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport { ClassRegistration, classRegistry, ClassType, Feature, takeWhile, assertInvariants } from './lib/index.mjs';\r\nimport { assert, checkedMode, Contract, extend } from './index.mjs';\r\nimport { Messages } from './Messages.mjs';\r\n\r\nconst isContracted = Symbol('isContracted'),\r\n innerContract = Symbol('innerContract');\r\n\r\n/**\r\n * Checks the features of the provided object for properties.\r\n * If any are found an exception is thrown\r\n * @param {ClassRegistration} registration - The ClassRegistration for the object\r\n * @param {U} obj - The object to check\r\n * @throws {AssertionError} - Throws if a property is found\r\n * @returns {boolean} - The result of the test\r\n */\r\nfunction hasProperties<U>(registration: ClassRegistration, obj: U): boolean {\r\n return Object.entries(Object.getOwnPropertyDescriptors(obj))\r\n .some(([key, desc]) => new Feature(registration, key, desc).isProperty);\r\n}\r\n\r\n/**\r\n * Associates a contract with a class via the mixin pattern.\r\n *\r\n * @param {Contract} contract The Contract definition\r\n * @param {T} Base An optional base class\r\n * @returns {T} The base class for extension\r\n * @example\r\n *\r\n * @Contracted(stackContract)\r\n * class Stack<T> { ... }\r\n */\r\nfunction Contracted<\r\n T extends Contract<any> = Contract<any>,\r\n U extends ClassType<any> = ClassType<any>\r\n>(contract: T = new Contract() as T) {\r\n return function (Clazz: U & { [innerContract]?: Contract<any> }, ctx: ClassDecoratorContext<U>) {\r\n if (ctx.kind !== 'class')\r\n throw new TypeError(Messages.MsgNotContracted);\r\n\r\n assert(!Object.getOwnPropertySymbols(Clazz).includes(isContracted), Messages.MsgSingleContract);\r\n\r\n if (contract[checkedMode] === false)\r\n return Clazz;\r\n\r\n const baseContract = Clazz[innerContract];\r\n assert(\r\n !baseContract ||\r\n baseContract && contract[extend] instanceof baseContract.constructor,\r\n Messages.MsgBadSubcontract\r\n );\r\n\r\n abstract class InnerContracted extends Clazz {\r\n // prevents multiple @Contracted decorators from being applied\r\n static readonly [isContracted] = true;\r\n\r\n constructor(...args: any[]) {\r\n super(...args);\r\n\r\n const Class = this.constructor as ClassType<any>,\r\n classRegistration = classRegistry.getOrCreate(Class);\r\n\r\n assert(!hasProperties(classRegistration, this), Messages.MsgNoProperties);\r\n\r\n if (!classRegistration.contractsChecked) {\r\n // bottom-up to closest Contracted class bind contracts\r\n const ancRegistrations = takeWhile(classRegistration.ancestry(), (cr => cr.Class !== Clazz));\r\n [classRegistration, ...ancRegistrations, classRegistry.get(Clazz)!].forEach(registration => {\r\n registration.bindContract(InnerContracted[innerContract]);\r\n });\r\n }\r\n\r\n assertInvariants(this, InnerContracted[innerContract]);\r\n\r\n return this;\r\n }\r\n\r\n // FIXME: dirty hack. Possibly resolved by moving to contracts as classes\r\n // The static getter is used by the construction invariant check\r\n // The instance getter is used by the feature declarations\r\n static get [innerContract]() { return contract; }\r\n get [innerContract]() { return contract; }\r\n }\r\n\r\n const classRegistration = classRegistry.getOrCreate(InnerContracted);\r\n classRegistration.contractsChecked = false;\r\n\r\n Object.freeze(Clazz);\r\n\r\n return InnerContracted;\r\n };\r\n}\r\n\r\nexport { isContracted, innerContract };\r\nexport default Contracted;","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\nimport AssertionError from './AssertionError.mjs';\r\nimport { Messages } from './Messages.mjs';\r\nimport { Constructor } from './index.mjs';\r\n\r\n/**\r\n * An assertion is an expression of a property that must be true at a particular\r\n * point of step of program execution. It consists of a boolean expression\r\n * and a message. This function tests the provided condition. If the condition\r\n * is false an AssertionError is raised with an optional message.\r\n * If the provided condition is true then the function returns without raising an error\r\n *\r\n * @param {boolean} condition - The condition to test\r\n * @param {string} message - A descriptive message to associate with the AssertionError\r\n * @param {ClassType<Error>} ErrorConstructor - The constructor of the Error to use\r\n * @throws {Error} - When the condition is false\r\n * @see AssertionError\r\n * @throws {AssertionError} - Throws an AssertionError by default if the condition is false\r\n *\r\n * @example\r\n * let x = 15;\r\n * assert(x > 5, `Expected: x > 5. Actual: x = ${x}`)\r\n *\r\n * @example\r\n * let name = \"Tom\"\r\n * assert(name.trim().length > 0, 'Name is required', TypeError)\r\n */\r\nexport default function assert(condition: unknown, message: string = Messages.AssertionFailed, ErrorConstructor: Constructor<Error> = AssertionError): asserts condition {\r\n if (Boolean(condition) == false)\r\n throw new ErrorConstructor(message);\r\n}","/*!\r\n * @license\r\n * Copyright (C) 2024 Final Hill LLC\r\n * SPDX-License-Identifier: AGPL-3.0-only\r\n * @see <https://spdx.org/licenses/AGPL-3.0-only.html>\r\n */\r\n\r\n/**\r\n * Material Implication.\r\n * `p → q`\r\n * Also referred to as `if-then`.\r\n * An example of usage is the encoding of \"sunny weather is a precondition of visiting the beach\"\r\n * This is logically equivalent to: !p || q\r\n *\r\n * @example\r\n * implies(weather.isSunny, person.visitsBeach)\r\n *\r\n * @param {boolean} p - The antecedent\r\n * @param {boolean} q - The consequent\r\n * @returns {boolean} - The result\r\n */\r\nexport default function implies(p: boolean, q: boolean): boolean {\r\n return !p || q;\r\n}"],"names":["__webpack_require__","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","AssertionError","Error","constructor","message","super","checkedMode","Symbol","invariant","extend","Contract","assertions","create","this","fnTrue","keys","forEach","propertyKey","featureOption","value","demands","ensures","rescue","within","contract","fn","assertDemands","ctx","className","featureName","args","result","d","Reflect","demandsError","String","assert","assertEnsures","old","e","ensuresError","assertInvariants","iv","toString","ClassRegistry","WeakMap","getOrCreate","Class","has","set","Messages","checkedFeature","fnOrig","registration","innerCheckedFeature","name","MsgInvalidContext","innerContract","apply","features","hasGetter","t0","Date","now","t1","error","hasRetried","MsgSingleRetry","contractsChecked","proto","ownKeys","filter","map","getOwnPropertyDescriptor","ParentClass","getPrototypeOf","parentRegistration","classRegistry","ancestry","parentRegistry","ancestryFeatures","flatMap","bindContract","feature","hasSetter","isMethod","getter","setter","descriptor","findFeature","find","deepFreeze","object","entries","undefined","freeze","classRegistration","ancestorFeature","isAccessor","isProperty","memberType","desc","takeWhile","xs","pred","length","slice","isContracted","Clazz","kind","TypeError","MsgNotContracted","getOwnPropertySymbols","includes","MsgSingleContract","baseContract","MsgBadSubcontract","InnerContracted","static","getOwnPropertyDescriptors","some","MsgNoProperties","ancRegistrations","cr","condition","AssertionFailed","ErrorConstructor","Boolean","implies","p","q"],"sourceRoot":""}
|
package/dist/lib/ClassType.d.mts
CHANGED
package/dist/lib/Feature.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (C)
|
|
3
|
+
* Copyright (C) 2024 Final Hill LLC
|
|
4
4
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
5
5
|
* @see <https://spdx.org/licenses/AGPL-3.0-only.html>
|
|
6
6
|
*/
|
|
@@ -8,5 +8,5 @@ import { ClassRegistration, ClassType } from './index.mjs';
|
|
|
8
8
|
declare class ClassRegistry extends WeakMap<ClassType<any>, ClassRegistration> {
|
|
9
9
|
getOrCreate(Class: ClassType<any>): ClassRegistration;
|
|
10
10
|
}
|
|
11
|
-
declare const
|
|
12
|
-
export default
|
|
11
|
+
declare const _default: ClassRegistry;
|
|
12
|
+
export default _default;
|
package/dist/lib/index.d.mts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (C)
|
|
3
|
+
* Copyright (C) 2024 Final Hill LLC
|
|
4
4
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
5
5
|
* @see <https://spdx.org/licenses/AGPL-3.0-only.html>
|
|
6
6
|
*/
|
|
7
7
|
import assertDemands from './assertDemands.mjs';
|
|
8
8
|
import assertEnsures from './assertEnsures.mjs';
|
|
9
9
|
import assertInvariants from './assertInvariants.mjs';
|
|
10
|
-
import
|
|
10
|
+
import classRegistry from './classRegistry.mjs';
|
|
11
11
|
import ClassRegistration from './ClassRegistration.mjs';
|
|
12
|
-
import { ClassType } from './ClassType.mjs';
|
|
12
|
+
import { type ClassType } from './ClassType.mjs';
|
|
13
13
|
import deepFreeze from './deepFreeze.mjs';
|
|
14
14
|
import Feature from './Feature.mjs';
|
|
15
15
|
import takeWhile from './takeWhile.mjs';
|
|
16
16
|
import unChecked from './unChecked.mjs';
|
|
17
17
|
declare const fnTrue: () => boolean;
|
|
18
|
-
export { assertDemands, assertEnsures, assertInvariants,
|
|
18
|
+
export { assertDemands, assertEnsures, assertInvariants, classRegistry, ClassRegistration, ClassType, deepFreeze, Feature, fnTrue, takeWhile, unChecked };
|
package/dist/lib/takeWhile.d.mts
CHANGED
package/dist/lib/unChecked.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@final-hill/decorator-contracts",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.3",
|
|
4
4
|
"description": "Code Contracts for TypeScript and ECMAScript classes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"prebuild": "npm run lint",
|
|
14
14
|
"build": "webpack",
|
|
15
15
|
"lint": "eslint src/ --ext .mts --fix",
|
|
16
|
-
"test": "
|
|
16
|
+
"test": "globstar -- node --import tsx --test \"src/**/*.test.mts\""
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
@@ -47,23 +47,21 @@
|
|
|
47
47
|
"author": "Michael L Haufe <michael.haufe@final-hill.com> (https://final-hill.com)",
|
|
48
48
|
"license": "AGPL-3.0-only",
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@types/
|
|
51
|
-
"@
|
|
52
|
-
"@typescript-eslint/
|
|
53
|
-
"
|
|
54
|
-
"eslint": "^8.48.0",
|
|
50
|
+
"@types/node": "^20.14.2",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^7.13.0",
|
|
52
|
+
"@typescript-eslint/parser": "^7.13.0",
|
|
53
|
+
"eslint": "^8.57.0",
|
|
55
54
|
"eslint-plugin-header": "^3.1.1",
|
|
56
|
-
"eslint-plugin-import": "^2.
|
|
57
|
-
"eslint-webpack-plugin": "^4.0
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"webpack": "^5.88.2",
|
|
55
|
+
"eslint-plugin-import": "^2.29.1",
|
|
56
|
+
"eslint-webpack-plugin": "^4.2.0",
|
|
57
|
+
"globstar": "^1.0.0",
|
|
58
|
+
"ts-loader": "^9.5.1",
|
|
59
|
+
"tsx": "^4.15.6",
|
|
60
|
+
"typescript": "^5.4.5",
|
|
61
|
+
"webpack": "^5.92.0",
|
|
64
62
|
"webpack-cli": "^5.1.4"
|
|
65
63
|
},
|
|
66
64
|
"files": [
|
|
67
65
|
"dist"
|
|
68
66
|
]
|
|
69
|
-
}
|
|
67
|
+
}
|