fwrap
v1.0.0
Published
A high-performance utility for runtime function name obfuscation, identity spoofing, and recursive bottom-up object graph freezing.
Maintainers
Readme
fwrap
Lightweight utilities for anonymizing function names and deeply processing object graphs.
Installation
NodeJS
npm install fwrapBrowser (via ESM)
<script type="module">
import fwrap, { fwrapObj } from 'https://esm.sh/fwrap';
</script>Quick Start
import fwrap, { fwrapObj } from 'fwrap';
// Anonymize a single function
const fn = fwrap(function mySecret() {});
console.log(fn.name); // ''
// Anonymize all functions in an object graph and freeze it
const api = fwrapObj({
getUser: function getUser() { /* ... */ },
saveUser: function saveUser() { /* ... */ },
});
console.log(api.getUser.name); // ''
console.log(Object.isFrozen(api)); // trueAPI
fwrap(fn, obj?, key?, opts?)
Strips the name property from a function by redefining it as ''.
When called standalone it returns the anonymized function. When obj and key are provided it mutates obj[key] in-place and returns undefined. On failure (e.g. a non-configurable name) it returns the original function unchanged rather than throwing.
| Parameter | Type | Default | Description |
|---|---|---|---|
| fn | Function | — | The function to anonymize. |
| obj | Object | null | null | Optional parent object for in-place mutation. |
| key | string | symbol | null | null | Property key on obj to reassign. |
| opts.configurable | boolean | true | Whether name can be redefined later. |
| opts.writable | boolean | false | Whether name can be assigned directly. |
| opts.enumerable | boolean | false | Whether name appears in enumeration. |
Returns: Function | void
// Standalone
const anon = fwrap(function named() {});
console.log(anon.name); // ''
// In-place
const obj = { greet: function greet() {} };
fwrap(obj.greet, obj, 'greet');
console.log(obj.greet.name); // ''
// With descriptor overrides
const fn = fwrap(function named() {}, null, null, { writable: true });fwrapCustom(fn, obj?, key?, opts?)
Identical to fwrap but accepts a value override, allowing any name to be set rather than always stripping it to ''.
| Parameter | Type | Default | Description |
|---|---|---|---|
| fn | Function | — | The function to rename. |
| obj | Object | null | null | Optional parent object for in-place mutation. |
| key | string | symbol | null | null | Property key on obj to reassign. |
| opts.value | string | '' | The custom name to assign. |
| opts.configurable | boolean | true | Whether name can be redefined later. |
| opts.writable | boolean | false | Whether name can be assigned directly. |
| opts.enumerable | boolean | false | Whether name appears in enumeration. |
Returns: Function | void
const fn = fwrapCustom(function original() {}, null, null, { value: 'alias' });
console.log(fn.name); // 'alias'fwrapObj(obj, isFreeze?, _opts?, _seen?)
Recursively scans an object or array to anonymize all function names, then optionally deep-freezes it.
- Skips non-writable, non-configurable, and accessor (getter/setter) properties to avoid errors with third-party or native objects.
- Handles circular references safely via an internal
WeakSet. - Freezes bottom-up so child structures are immutable before their parents.
| Parameter | Type | Default | Description |
|---|---|---|---|
| obj | Object | Array | — | The target structure to process. |
| isFreeze | boolean | true | Whether to deeply freeze the structure after processing. |
| _opts | Object | {} | Descriptor overrides forwarded to every fwrap call. |
| _seen | WeakSet | new WeakSet() | Internal circular-reference tracker; do not pass manually. |
Returns: Object | Array
// Deep freeze + anonymize (default)
const config = fwrapObj({
load: function load() {},
nested: {
parse: function parse() {},
},
});
console.log(config.load.name); // ''
console.log(config.nested.parse.name); // ''
console.log(Object.isFrozen(config)); // true
// Anonymize without freezing
const mutable = fwrapObj({ fn: function fn() {} }, false);
console.log(Object.isFrozen(mutable)); // false
// Pass descriptor overrides to all wrapped functions
const api = fwrapObj({ fn: function fn() {} }, true, { writable: true });Behavior Notes
Descriptor defaults. By default name is set with configurable: true, writable: false, enumerable: false — matching native function behavior. Direct assignment (fn.name = 'x') will silently fail in sloppy mode or throw in strict mode; Object.defineProperty can still re-set it.
Failure safety. Both fwrap and fwrapCustom catch any defineProperty error and return the original function unchanged, so they are always safe to call regardless of the target's property descriptors.
Read-only and accessor properties. fwrapObj skips any property where writable === false, configurable === false, or a getter/setter is present. This prevents errors when processing objects from third-party modules or native APIs.
Circular references. fwrapObj tracks visited objects in a WeakSet and silently skips any already-seen reference, preventing infinite recursion.
Funding & Support 🤍
If you find this utility useful in securing your application structures, consider supporting development:
- Donate: harnumaix.github.io/donate
License
Licensed under the Apache License, Version 2.0; a robust, permissive license that includes an explicit grant of patent rights and provides protection against contributor liability.
Copyright © 2026 Aries Harbinger. See the LICENSE file for full details.
