diff --git a/README.md b/README.md index 9c326f3..54e4039 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,22 @@ Generates dynamic prototype methods for JavaScript objects (classes) by supporting method definition within their "class" constructor (like an instance version), this removes the need to expose internal properties on the instance (this) and the usage of ```ClassName.prototype.funcName()``` both of which result in better code minfication (smaller output) and therefore improved load times for your users. -The dynamically generated prototype methods support class inheritance of any type, which means you can extend from base classes that use instance or prototype defined methods, you also don't need to add the normal boiler plate code to handle detecting, saving and calling any previous instance methods that you are overriding as support for this is provided automatically. - -So whether creating a new class or extending some other class/code, your resulting code, can be successfully extended via TypeScript or JavaScript. - -> ES3 / IE8 support has been removed from Version 2.x. -> -> if you need to retain ES3 / IE8 support then you will need to use one of the 1.x versions which is now maintained on the old [master branch](https://github.com/microsoft/DynamicProto-JS/tree/master) - -The version 2.x is maintained on the default [main branch](https://github.com/microsoft/DynamicProto-JS/tree/main) - -## Documentation - -[Github Documentation](https://microsoft.github.io/DynamicProto-JS/) includes [typedoc API references](https://microsoft.github.io/DynamicProto-JS/typedoc/index.html). - -## Removing / Hiding internal properties from instance +The dynamically generated prototype methods support class inheritance of any type, which means you can extend from base classes that use instance or prototype defined methods, you also don't need to add the normal boiler plate code to handle detecting, saving and calling any previous instance methods that you are overriding as support for this is provided automatically. + +So whether creating a new class or extending some other class/code, your resulting code, can be successfully extended via TypeScript or JavaScript. + +> ES3 / IE8 support has been removed from Version 2.x. +> +> if you need to retain ES3 / IE8 support then you will need to use one of the 1.x versions which is now maintained on the old [master branch](https://github.com/microsoft/DynamicProto-JS/tree/master) + +The version 2.x is maintained on the default [main branch](https://github.com/microsoft/DynamicProto-JS/tree/main) + + +## Documentation + +[Github Documentation](https://microsoft.github.io/DynamicProto-JS/) includes [typedoc API references](https://microsoft.github.io/DynamicProto-JS/typedoc/index.html). + +## Removing / Hiding internal properties from instance By defining the properties / methods within the constructors closure, each instance can contain or define internal state in the form of properties which it does not have to expose publically as each defined "public" instance method has direct access to this define state within the context/scope of the closure method. While this does require some additional CPU and memory at the point of creating each instance object this is designed to be as minimal as possible and should be outwayed by the following advantages :- diff --git a/RELEASES.md b/RELEASES.md index f64fd82..4661894 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,14 @@ # Releases +## 2.0.4 (Unreleased) + +### Changes + +- #95 [BUG] AppInsights breaks Angular SSR in Cloudflare Worker + - Added support for restricted JavaScript environments, including Cloudflare Workers + - Modified the `dynamicProto` function to detect environments where property redefinition is restricted + - This ensures compatibility with Angular SSR, Cloudflare Workers, and other restricted environments + ## 2.0.3 (Jan 11th, 2024) Blocks a medium level prototype pollution vulnerability. diff --git a/lib/src/DynamicProto.ts b/lib/src/DynamicProto.ts index 6beebbb..e499dd6 100644 --- a/lib/src/DynamicProto.ts +++ b/lib/src/DynamicProto.ts @@ -18,6 +18,8 @@ interface DynamicGlobalSettings { + + /** * Constant string defined to support minimization * @ignore @@ -420,7 +422,11 @@ function _populatePrototype(proto:any, className:string, target:any, baseInstFun // Tag this function as a proxy to support replacing dynamic proxy elements (primary use case is for unit testing // via which can dynamically replace the prototype function reference) - (dynProtoProxy as any)[DynProxyTag] = 1; + try { + (dynProtoProxy as any)[DynProxyTag] = 1; + } catch (e) { + // Ignore errors in restricted environments like Cloudflare Workers + } return dynProtoProxy; } @@ -440,7 +446,11 @@ function _populatePrototype(proto:any, className:string, target:any, baseInstFun if (_isDynamicCandidate(target, name, false) && target[name] !== baseInstFuncs[name] ) { // Save the instance Function to the lookup table and remove it from the instance as it's not a dynamic proto function instFuncs[name] = target[name]; - delete target[name]; + try { + delete target[name]; + } catch (e) { + // Ignore errors in restricted environments like Cloudflare Workers + } // Add a dynamic proto if one doesn't exist or if a prototype function exists and it's not a dynamic one if (!objHasOwnProperty(proto, name) || (proto[name] && !proto[name][DynProxyTag])) { @@ -601,7 +611,11 @@ export default function dynamicProto(theClass:DPCls, target:DPTyp // function table lookup. className = DynClassNamePrefix + _getObjName(theClass, "_") + "$" + _gblInst.n ; _gblInst.n++; - classProto[DynClassName] = className; + try { + classProto[DynClassName] = className; + } catch (e) { + // Ignore errors in restricted environments like Cloudflare Workers + } } let perfOptions = dynamicProto[DynProtoDefaultOptions]; @@ -620,7 +634,7 @@ export default function dynamicProto(theClass:DPCls, target:DPTyp // Note casting the same type as we don't actually have the base class here and this will provide some intellisense support delegateFunc(target, baseFuncs as DPType); - // Don't allow setting instance functions for older IE instances + // Don't allow setting instance functions in older browsers or restricted environments let setInstanceFunc = !!_objGetPrototypeOf && !!perfOptions[strSetInstFuncs]; if (setInstanceFunc && options) { setInstanceFunc = !!options[strSetInstFuncs]; diff --git a/package.json b/package.json index 70bab56..26ae575 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ }, "homepage": "https://github.com/microsoft/DynamicProto-JS#readme", "dependencies": { - "@nevware21/ts-utils": ">= 0.11.6 < 2.x" + "@nevware21/ts-utils": ">=0.12.4 <1.0.0" }, "devDependencies": { "@microsoft/rush": "5.148.0",