@@ -1540,10 +1540,10 @@ vAPI.cloud = (( ) => {
1540
1540
// good thing given chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE
1541
1541
// and chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR.
1542
1542
1543
- const getCoarseChunkCount = async function ( dataKey ) {
1543
+ const getCoarseChunkCount = async function ( datakey ) {
1544
1544
const keys = { } ;
1545
1545
for ( let i = 0 ; i < maxChunkCountPerItem ; i += 16 ) {
1546
- keys [ dataKey + i . toString ( ) ] = '' ;
1546
+ keys [ datakey + i . toString ( ) ] = '' ;
1547
1547
}
1548
1548
let bin ;
1549
1549
try {
@@ -1553,13 +1553,13 @@ vAPI.cloud = (( ) => {
1553
1553
}
1554
1554
let chunkCount = 0 ;
1555
1555
for ( let i = 0 ; i < maxChunkCountPerItem ; i += 16 ) {
1556
- if ( bin [ dataKey + i . toString ( ) ] === '' ) { break ; }
1556
+ if ( bin [ datakey + i . toString ( ) ] === '' ) { break ; }
1557
1557
chunkCount = i + 16 ;
1558
1558
}
1559
1559
return chunkCount ;
1560
1560
} ;
1561
1561
1562
- const deleteChunks = function ( dataKey , start ) {
1562
+ const deleteChunks = function ( datakey , start ) {
1563
1563
const keys = [ ] ;
1564
1564
1565
1565
// No point in deleting more than:
@@ -1570,34 +1570,37 @@ vAPI.cloud = (( ) => {
1570
1570
Math . ceil ( maxStorageSize / maxChunkSize )
1571
1571
) ;
1572
1572
for ( let i = start ; i < n ; i ++ ) {
1573
- keys . push ( dataKey + i . toString ( ) ) ;
1573
+ keys . push ( datakey + i . toString ( ) ) ;
1574
1574
}
1575
1575
if ( keys . length !== 0 ) {
1576
1576
webext . storage . sync . remove ( keys ) ;
1577
1577
}
1578
1578
} ;
1579
1579
1580
- const push = async function ( dataKey , data ) {
1581
- let bin = {
1582
- 'source' : options . deviceName || options . defaultDeviceName ,
1583
- 'tstamp' : Date . now ( ) ,
1584
- 'data' : data ,
1585
- 'size' : 0
1580
+ const push = async function ( details ) {
1581
+ const { datakey , data , encode } = details ;
1582
+ const item = {
1583
+ source : options . deviceName || options . defaultDeviceName ,
1584
+ tstamp : Date . now ( ) ,
1585
+ data ,
1586
1586
} ;
1587
- bin . size = JSON . stringify ( bin ) . length ;
1588
- const item = JSON . stringify ( bin ) ;
1587
+ const json = JSON . stringify ( item ) ;
1588
+ const encoded = encode instanceof Function
1589
+ ? await encode ( json )
1590
+ : json ;
1589
1591
1590
1592
// Chunkify taking into account QUOTA_BYTES_PER_ITEM:
1591
1593
// https://developer.chrome.com/extensions/storage#property-sync
1592
1594
// "The maximum size (in bytes) of each individual item in sync
1593
1595
// "storage, as measured by the JSON stringification of its value
1594
1596
// "plus its key length."
1595
- bin = { } ;
1596
- let chunkCount = Math . ceil ( item . length / maxChunkSize ) ;
1597
+ const bin = { } ;
1598
+ const chunkCount = Math . ceil ( encoded . length / maxChunkSize ) ;
1597
1599
for ( let i = 0 ; i < chunkCount ; i ++ ) {
1598
- bin [ dataKey + i . toString ( ) ] = item . substr ( i * maxChunkSize , maxChunkSize ) ;
1600
+ bin [ datakey + i . toString ( ) ]
1601
+ = encoded . substr ( i * maxChunkSize , maxChunkSize ) ;
1599
1602
}
1600
- bin [ dataKey + chunkCount . toString ( ) ] = '' ; // Sentinel
1603
+ bin [ datakey + chunkCount . toString ( ) ] = '' ; // Sentinel
1601
1604
1602
1605
try {
1603
1606
await webext . storage . sync . set ( bin ) ;
@@ -1606,18 +1609,19 @@ vAPI.cloud = (( ) => {
1606
1609
}
1607
1610
1608
1611
// Remove potentially unused trailing chunks
1609
- deleteChunks ( dataKey , chunkCount ) ;
1612
+ deleteChunks ( datakey , chunkCount ) ;
1610
1613
} ;
1611
1614
1612
- const pull = async function ( dataKey ) {
1615
+ const pull = async function ( details ) {
1616
+ const { datakey, decode } = details ;
1613
1617
1614
- const result = await getCoarseChunkCount ( dataKey ) ;
1618
+ const result = await getCoarseChunkCount ( datakey ) ;
1615
1619
if ( typeof result !== 'number' ) {
1616
1620
return result ;
1617
1621
}
1618
1622
const chunkKeys = { } ;
1619
1623
for ( let i = 0 ; i < result ; i ++ ) {
1620
- chunkKeys [ dataKey + i . toString ( ) ] = '' ;
1624
+ chunkKeys [ datakey + i . toString ( ) ] = '' ;
1621
1625
}
1622
1626
1623
1627
let bin ;
@@ -1633,31 +1637,35 @@ vAPI.cloud = (( ) => {
1633
1637
// happen when the number of chunks is a multiple of
1634
1638
// chunkCountPerFetch. Hence why we must also test against
1635
1639
// undefined.
1636
- let json = [ ] , jsonSlice ;
1640
+ let encoded = [ ] ;
1637
1641
let i = 0 ;
1638
1642
for ( ; ; ) {
1639
- jsonSlice = bin [ dataKey + i . toString ( ) ] ;
1640
- if ( jsonSlice === '' || jsonSlice === undefined ) { break ; }
1641
- json . push ( jsonSlice ) ;
1643
+ const slice = bin [ datakey + i . toString ( ) ] ;
1644
+ if ( slice === '' || slice === undefined ) { break ; }
1645
+ encoded . push ( slice ) ;
1642
1646
i += 1 ;
1643
1647
}
1648
+ encoded = encoded . join ( '' ) ;
1649
+ const json = decode instanceof Function
1650
+ ? await decode ( encoded )
1651
+ : encoded ;
1644
1652
let entry = null ;
1645
1653
try {
1646
- entry = JSON . parse ( json . join ( '' ) ) ;
1654
+ entry = JSON . parse ( json ) ;
1647
1655
} catch ( ex ) {
1648
1656
}
1649
1657
return entry ;
1650
1658
} ;
1651
1659
1652
- const used = async function ( dataKey ) {
1660
+ const used = async function ( datakey ) {
1653
1661
if ( webext . storage . sync . getBytesInUse instanceof Function === false ) {
1654
1662
return ;
1655
1663
}
1656
- const coarseCount = await getCoarseChunkCount ( dataKey ) ;
1664
+ const coarseCount = await getCoarseChunkCount ( datakey ) ;
1657
1665
if ( typeof coarseCount !== 'number' ) { return ; }
1658
1666
const keys = [ ] ;
1659
1667
for ( let i = 0 ; i < coarseCount ; i ++ ) {
1660
- keys . push ( `${ dataKey } ${ i } ` ) ;
1668
+ keys . push ( `${ datakey } ${ i } ` ) ;
1661
1669
}
1662
1670
let results ;
1663
1671
try {
0 commit comments