Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
one-typescript
Advanced tools
One
is a browser side application cache. It guarantees entity uniqueness across the entire cache.
Each entity tracked for uniqueness must have a unique id. There is precisely ONE distinct entity in the cache for each unique id. Entities that do not have a unique id are still cached but not tracked for uniqueness.
Version 0.0.10 - fix error with references for deeply nested entity not being updated all the way to the top
babel-polyfill
Command | Action |
---|---|
getCache | Create or access a specific version of the cache. There can be multiple concurrent versions of the cache in case distinct versions of the same entity are needed (for example if display and edit data need to be different until a user commits changes). One.getCache('edit') would create a cache dedicated to edit operations. |
put | Add an entity to the cache and make it immutable. |
get | Retrieve an entity from the cache. This is a fast hash map locator. |
getEdit | Get a shallow editable version of the entity from the cache. Inner nested entities are still immutable. This is in order to make efficient use of trie structures. |
evict | Remove an entity from the cache. Evicts it from all parents as well. |
In addition to the 5 production api commands there are 4 options intended for development:
Command | Action |
---|---|
reset | Resets the cache to empty. Useful for testing. |
length | Number of nodes in the current cache. Each node contains one atomic change to the cache so moving between nodes gives you time travelling. |
size | Number of entities cached on the current node (the size of the node). |
Provides a printable representation of the entire cache that can be passed on to a logger. Slow. For debugging only. Do not use in production. |
npm install one --save
or
yarn add one
Use it
import * as One from 'one'
// get a hold of an instance
let one = One.getCache()
// you can then use the instance to cache items
one.put(item)
// One.getCache() is a singleton so you can also do this
One.getCache().put(item)
// or if you are only using the default instance of the cache
One.put(item)
Or simply put one.min.js
on your page to access the One
global variable from anywhere. In this case the instance is created for you and you can access it directly.
One.put(item)
Some code
let item1 = { uid:1 }
let item2 = { uid:2, ref:item1 }
One.put(item2)
// puts all items with uid separately in the cache
One.get(item1) === undefined // false (item1 is added from item2)
item1 === One.get(item1) // true (same object)
item2.ref === One.get(1) // true
All data is immutable. Once an item enters the cache it freezes and cannot change. This is to enable quick identity checks against immutable entities (ie React / Redux identity check).
let item = { uid:1 }
Object.isFrozen(item) // false
One.put(item);
Object.isFrozen(item) // true
let result = One.get(item)
result === item // true
If you later want to edit a reference of the object you can get an editable copy from the cache. This gives you a separate clone of the object that is now editable:
let item = { uid:1 }
One.put(item)
let editable = One.getEdit(1) // or cuid.getEditable(item1);
Object.isFrozen(editable) // false
item === editable // false
editable.text = "test"
One.put(editable)
let edited = One.get(1)
edited.text = "text" // true
Object.isFrozen(edited) // true
Important Edit clones are shallow. If you want to edit a nested child that also has a uid you must get an editable copy of the child.
const item1 = { uid:1 } // uid item cached separately
const item2 = { value: 'test' } // item has no uid - it will be cloned for edit
const item = {
uid: 1,
item1,
item2
}
one.put(item)
const editable = one.getEdit(item)
Object.isFrozen(editable.item1) // true - item1 has a uid - not cloned
item1 === editable.item1 // true
Object.isFrozen(editable.item2) // false item2 has no uid - it will be cloned
item2 === editable.item2 // false
Editing an item changes all its instances in the cache:
let item = { uid:1 }
let item2 = { uid:2, child:item }
One.put(item2)
One.get(1) === item // true
One.get(2) === item2 // true
// Let's do some editing
let editable = One.getEdit(1);
editable.text = "test"
One.put(editable) // also updates item2 reference to item
let result = One.get(2)
console.log(JSON.stringify(result.item)) // {uid:1, text:"test"}
For existing code bases the name of the uid
property can be configured via a config object passed as a second argument to the .getCache()
method. In order for this to work correctly the values held by the configured property must be unique across all instances.
const one = One.getCache('test', { uidName:'id' })
const item = { id:'unique_id_value' }
one.put(item)
one.get('unique_id_value') !== undefined // true (works)
More an more applications are giving users the ability to edit data in the browser. With a normalized data model various instances of an entity can exist at the same time in different locations. This depends on how data is received from the server and added to the local model / store.
This is inconvenient because:
Redux brings a great breakthrough by putting the entire application state in one place and mutating it only via dispatched actions. But it doesn't enforce entity uniqueness. One
aims to take the concept a step further by making each entity unique and immutable in a single store (cache).
One
mitigates this by being read optimized. The penalty is incurred on write operations only. These happen a lot less frequently than read ops. Read ops are super fast (a simple key lookup).This is not currently designed to work with cyclical data. It is best for non-cyclical objects received from the server in the form of json (or other non-cyclical fomats). It might happen later if there's a need.
FAQs
Porting of One package to typescript
We found that one-typescript demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.