warning: largely an educational exercise. too slow to be practical, has not been audited, here be dragons, etc
a javascript membrane implementation. This implementation is intended to provide secure isolation between any number of membrane spaces. This implementation intends to support all types of objects including functions, classes, etc. By default, all membrane spaces have a "transparent distortion", meaning all operations are forwarded to the original graph. In order for this membrane to be useful you will need to provide a distortion implementation.
(note: this module has not been audited for security) membrane-wrapped objects should always invoke the relevant distortion
the membrane will wrap/unwrap objects when passed across membrane space boundaries. If an object is created in space A, passed to space B, then to space C, and returned to space A, it will be given to space A unwrapped as a raw object.
the membrane is intended to support any type of javascript object (TypedArray instances, objects with prototype chains, Proxy instances). empty values (null, undefined) and non-object values (number, string) are passed through un-wrapped. Primordials (Object, Object.prototype, etc) are also passed through unwrapped.
distortions are hooks into interactions with objects across MembraneSpace boundaries. The distortions are set at the object's origin MebraneSpace. The distortions are applied when the object is referenced in another MembraneSpace. The distortions are not applied in the origin MembraneSpace, as the object is a raw (unwrapped) reference there. The distortions will not be applied in MembraneSpaces that specify the option { dangerouslyAlwaysUnwrap: true }.
distortions can be set in two ways:
- via the default handler for the MembraneSpace via the
createHandleroption. - overridden for a specific reference via the
membraneSpace.handlerForRefWeakMap.
Using these two approaches allows you to have a different distortion for a subset of the MembraneSpace's objects.
import { Membrane } from 'cytoplasm'
import createReadOnlyDistortion from 'cytoplasm/src/distortions/readOnly.js'
const membrane = new Membrane()
const graphA = membrane.makeMembraneSpace({ label: 'a', createHandler: createReadOnlyDistortion })
const graphB = membrane.makeMembraneSpace({ label: 'b' })
const objA = {
value: 123,
set: function (newVal) { this.value = newVal },
}
const objAWrappedForB = membrane.bridge(objA, graphA, graphB)
// original object is still mutable
objA.value = 456
// the specified readOnlyDistortion allows the wrapped object to internally mutate itself
// so the value is updated
objAWrappedForB.set(13)
// this assignment fails and throws an error under strict mode
objAWrappedForB.value = 42The origin space of an instance of a class with cross-space protoype chain is somewhat complicated, due to differences in class constructors vs function constructors, as well as the Builtins.
The instance ends up being claimed by the first constructor it is exposed to. Since class constructors cant access this until they've called super(...), ownership is pushed further down the chain. The function constructors can access this immediately and will claim the instance. Builtins skip membrane wrapping and so do not trigger a claim on this.
examples:
inst
class A
class B
class C <-- origin space
Object
inst
class A
function B <-- origin space
function C
Object
"n-sides" means it supports at least 3 spaces. "security focused" means that user code can't unwrap the membrane (usually implies a WeakMap)
| repo | n-sides | security focused | audit |
|---|---|---|---|
| cytoplasm | β | β | x |
| es-membrane | β | ? | x |
| @caridy/sjs | x | ? | x |
| observable-membrane | x | ? | x |
| fast-membrane | x | x | x |
| membrane-traits | x | ? | x |
yarn run performance
I have noticed significant performance differences between node v20 and node v24.
Performance comparison between cytoplasm, other memebranes, and non-membranes:
== get ==
βββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββββββββββββ¬ββββββββββββ¬ββββββββββ
β (index) β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
βββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββββββββββββββΌββββββββββββΌββββββββββ€
β 0 β 'non-membrane:bare' β '70,240' β 14236.754730143311 β 'Β±1.08%' β 70241 β
β 1 β 'non-membrane:emptyProxy' β '52,547' β 19030.25576128991 β 'Β±0.98%' β 52549 β
β 2 β 'non-membrane:reflectProxy' β '46,907' β 21318.618764388408 β 'Β±0.91%' β 46908 β
β 3 β 'non-membrane:recursiveProxy' β '46,288' β 21603.610015339804 β 'Β±0.89%' β 46289 β
β 4 β 'test:simpleMembrane' β '9,234' β 108286.6388738518 β 'Β±16.19%' β 9235 β
β 5 β 'fast-membrane:symbol' β '42,923' β 23297.265562389282 β 'Β±1.63%' β 42924 β
β 6 β 'fast-membrane:weakmap' β '11,104' β 90051.4219720839 β 'Β±17.68%' β 11105 β
β 7 β 'observable-membrane' β '8,949' β 111734.47307262794 β 'Β±29.63%' β 8950 β
β 8 β 'cytoplasm:transparent' β '3,238' β 308739.26767521736 β 'Β±9.08%' β 3239 β
βββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββββββββββββ΄ββββββββββββ΄ββββββββββ
== deep-get ==
βββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββββββββββββ¬ββββββββββββ¬ββββββββββ
β (index) β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
βββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββββββββββββββΌββββββββββββΌββββββββββ€
β 0 β 'non-membrane:bare' β '19,647' β 50897.023564734365 β 'Β±1.79%' β 19648 β
β 1 β 'non-membrane:emptyProxy' β '18,341' β 54521.45867409297 β 'Β±1.72%' β 18342 β
β 2 β 'non-membrane:reflectProxy' β '17,773' β 56263.538989528686 β 'Β±1.66%' β 17774 β
β 3 β 'non-membrane:recursiveProxy' β '15,711' β 63649.45665732523 β 'Β±1.51%' β 15712 β
β 4 β 'test:simpleMembrane' β '1,780' β 561685.1529667906 β 'Β±38.84%' β 1837 β
β 5 β 'fast-membrane:symbol' β '15,352' β 65134.08675829828 β 'Β±1.78%' β 15353 β
β 6 β 'fast-membrane:weakmap' β '2,565' β 389717.32891658717 β 'Β±19.47%' β 2566 β
β 7 β 'observable-membrane' β '2,424' β 412420.0032894011 β 'Β±15.71%' β 2432 β
β 8 β 'cytoplasm:transparent' β '570' β 1751798.3782837114 β 'Β±65.23%' β 571 β
βββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββββββββββββ΄ββββββββββββ΄ββββββββββ
== set ==
βββββββββββ¬ββββββββββββββββββββββββββββββββ¬ββββββββββ¬βββββββββββββββββββββ¬ββββββββββββ¬ββββββββββ
β (index) β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
βββββββββββΌββββββββββββββββββββββββββββββββΌββββββββββΌβββββββββββββββββββββΌββββββββββββΌββββββββββ€
β 0 β 'non-membrane:bare' β '8,134' β 122933.7822987108 β 'Β±1.80%' β 8135 β
β 1 β 'non-membrane:emptyProxy' β '8,135' β 122916.59353495642 β 'Β±1.67%' β 8136 β
β 2 β 'non-membrane:reflectProxy' β '8,037' β 124412.73525754362 β 'Β±1.65%' β 8038 β
β 3 β 'non-membrane:recursiveProxy' β '8,194' β 122036.85918242823 β 'Β±1.62%' β 8195 β
β 4 β 'test:simpleMembrane' β '5,121' β 195260.11772744943 β 'Β±5.00%' β 5122 β
β 5 β 'fast-membrane:symbol' β '7,554' β 132366.23273351032 β 'Β±1.94%' β 7558 β
β 6 β 'fast-membrane:weakmap' β '5,228' β 191255.6351118871 β 'Β±5.61%' β 5229 β
β 7 β 'observable-membrane' β '4,771' β 209563.90758593244 β 'Β±4.75%' β 4772 β
β 8 β 'cytoplasm:transparent' β '2,113' β 473096.4664143572 β 'Β±42.42%' β 2114 β
βββββββββββ΄ββββββββββββββββββββββββββββββββ΄ββββββββββ΄βββββββββββββββββββββ΄ββββββββββββ΄ββββββββββ
== Summary ==
βββββββββββββββββββββββββββββββ¬ββββββββ¬βββββββββββ¬βββββββ
β (index) β get β deep-get β set β
βββββββββββββββββββββββββββββββΌββββββββΌβββββββββββΌβββββββ€
β non-membrane:bare β 70241 β 19648 β 8134 β
β non-membrane:emptyProxy β 52548 β 18341 β 8136 β
β non-membrane:reflectProxy β 46907 β 17773 β 8038 β
β non-membrane:recursiveProxy β 46289 β 15711 β 8194 β
β test:simpleMembrane β 9235 β 1780 β 5121 β
β fast-membrane:symbol β 42923 β 15353 β 7555 β
β fast-membrane:weakmap β 11105 β 2566 β 5229 β
β observable-membrane β 8950 β 2425 β 4772 β
β cytoplasm:transparent β 3239 β 571 β 2114 β
βββββββββββββββββββββββββββββββ΄ββββββββ΄βββββββββββ΄βββββββ