@@ -26,7 +26,7 @@ const {
26
26
resetLevelPath,
27
27
resetPrivatePath,
28
28
} = require ( './defaults' )
29
- const { onceWhen, ReadyGate } = require ( './utils' )
29
+ const { onceWhen, ReadyGate, onceWhenPromise } = require ( './utils' )
30
30
const DebouncingBatchAdd = require ( './debounce-batch' )
31
31
const Log = require ( './log' )
32
32
const Status = require ( './status' )
@@ -620,7 +620,7 @@ exports.init = function (sbot, config) {
620
620
)
621
621
}
622
622
623
- function create ( opts , cb ) {
623
+ async function create ( opts , cb ) {
624
624
const guard = guardAgainstDuplicateLogs ( 'create()' )
625
625
if ( guard ) return cb ( guard )
626
626
@@ -637,83 +637,78 @@ exports.init = function (sbot, config) {
637
637
638
638
if ( ! opts . content ) return cb ( new Error ( 'create() requires a `content`' ) )
639
639
640
- onceWhen (
641
- stateFeedsReady ,
642
- ( ready ) => ready === true ,
643
- ( ) => {
644
- const provisionalNativeMsg = feedFormat . newNativeMsg ( {
645
- timestamp : Date . now ( ) ,
646
- ...opts ,
647
- previous : null ,
640
+ await privateIndex . stateLoaded
641
+ await onceWhenPromise ( stateFeedsReady , ( ready ) => ready === true )
642
+
643
+ // Create full opts:
644
+ const provisionalNativeMsg = feedFormat . newNativeMsg ( {
645
+ timestamp : Date . now ( ) ,
646
+ ...opts ,
647
+ previous : null ,
648
+ keys,
649
+ } )
650
+ const feedId = feedFormat . getFeedId ( provisionalNativeMsg )
651
+ const previous = state . getAsKV ( feedId , feedFormat )
652
+ const fullOpts = { timestamp : Date . now ( ) , ...opts , previous, keys, hmacKey }
653
+
654
+ // If opts ask for encryption, try encryption formats and pick the best:
655
+ const recps = fullOpts . recps || fullOpts . content . recps
656
+ if ( Array . isArray ( recps ) && recps . length > 0 ) {
657
+ const plaintext = feedFormat . toPlaintextBuffer ( fullOpts )
658
+ function encryptWith ( encryptionFormat ) {
659
+ const encryptOpts = {
660
+ ...fullOpts ,
648
661
keys,
649
- } )
650
- const feedId = feedFormat . getFeedId ( provisionalNativeMsg )
651
- const previous = state . getAsKV ( feedId , feedFormat )
652
- const fullOpts = { timestamp : Date . now ( ) , ...opts , previous, keys }
653
-
654
- // If the inputs require encryption, try some encryption formats,
655
- // and pick the best output.
656
- const recps = fullOpts . recps || fullOpts . content . recps
657
- if ( Array . isArray ( recps ) && recps . length > 0 ) {
658
- const plaintext = feedFormat . toPlaintextBuffer ( fullOpts )
659
- function encryptWith ( encryptionFormat ) {
660
- const eOpts = {
661
- ...fullOpts ,
662
- keys,
663
- recps,
664
- previous : previous ? previous . key : null ,
665
- }
666
- const ciphertextBuf = encryptionFormat . encrypt ( plaintext , eOpts )
667
- return (
668
- ciphertextBuf . toString ( 'base64' ) + '.' + encryptionFormat . name
669
- )
670
- }
671
- if ( fullOpts . encryptionFormat ) {
672
- const format = findEncryptionFormatByName ( fullOpts . encryptionFormat )
662
+ recps,
663
+ previous : previous ? previous . key : null ,
664
+ }
665
+ const ciphertextBuf = encryptionFormat . encrypt ( plaintext , encryptOpts )
666
+ return ciphertextBuf . toString ( 'base64' ) + '.' + encryptionFormat . name
667
+ }
668
+ if ( fullOpts . encryptionFormat ) {
669
+ const format = findEncryptionFormatByName ( fullOpts . encryptionFormat )
670
+ const ciphertext = encryptWith ( format )
671
+ fullOpts . content = ciphertext
672
+ } else {
673
+ const outputs = encryptionFormats . map ( ( format ) => {
674
+ try {
673
675
const ciphertext = encryptWith ( format )
674
- fullOpts . content = ciphertext
675
- } else {
676
- const outputs = encryptionFormats . map ( ( format ) => {
677
- try {
678
- const ciphertext = encryptWith ( format )
679
- if ( ! ciphertext ) return [ new Error ( 'encryption failed' ) ]
680
- return [ null , { ciphertext, name : format . name } ]
681
- } catch ( err ) {
682
- return [ err ]
683
- }
684
- } )
685
- const successes = outputs
686
- . filter ( ( [ err ] ) => ! err )
687
- . map ( ( [ , success ] ) => success )
688
- const errors = outputs
689
- . filter ( ( [ err ] ) => err )
690
- . map ( ( [ error ] ) => error . message )
691
- if ( successes . length === 0 ) {
692
- // prettier-ignore
693
- return cb ( new Error ( 'create() failed to encrypt content: ' + errors . join ( '; ' ) ) )
694
- }
695
- fullOpts . content = successes [ 0 ] . ciphertext
676
+ if ( ! ciphertext ) return [ new Error ( 'encryption failed' ) ]
677
+ return [ null , { ciphertext, name : format . name } ]
678
+ } catch ( err ) {
679
+ return [ err ]
696
680
}
697
- }
698
-
699
- fullOpts . hmacKey = hmacKey
700
- const nativeMsg = feedFormat . newNativeMsg ( fullOpts )
701
- const msgId = feedFormat . getMsgId ( nativeMsg )
702
- const encodedMsg = feedFormat . fromNativeMsg ( nativeMsg , encoding )
703
- state . update ( feedId , nativeMsg )
704
-
705
- log . add ( msgId , encodedMsg , feedId , encoding , ( err , kvt ) => {
706
- if ( err ) return cb ( clarify ( err , 'create() failed in the log' ) )
707
-
708
- onMsgAdded . set ( {
709
- kvt,
710
- nativeMsg : nativeMsg ,
711
- feedFormat : feedFormat . name ,
712
- } )
713
- cb ( null , kvt )
714
681
} )
682
+ const successes = outputs
683
+ . filter ( ( [ err ] ) => ! err )
684
+ . map ( ( [ , success ] ) => success )
685
+ const errors = outputs
686
+ . filter ( ( [ err ] ) => err )
687
+ . map ( ( [ error ] ) => error . message )
688
+ if ( successes . length === 0 ) {
689
+ // prettier-ignore
690
+ return cb ( new Error ( 'create() failed to encrypt content: ' + errors . join ( '; ' ) ) )
691
+ }
692
+ fullOpts . content = successes [ 0 ] . ciphertext
715
693
}
716
- )
694
+ }
695
+
696
+ // Create the native message:
697
+ const nativeMsg = feedFormat . newNativeMsg ( fullOpts )
698
+ const msgId = feedFormat . getMsgId ( nativeMsg )
699
+ const encodedMsg = feedFormat . fromNativeMsg ( nativeMsg , encoding )
700
+ state . update ( feedId , nativeMsg )
701
+
702
+ // Encode the native message and append it to the log:
703
+ log . add ( msgId , encodedMsg , feedId , encoding , ( err , kvt ) => {
704
+ if ( err ) return cb ( clarify ( err , 'create() failed in the log' ) )
705
+ onMsgAdded . set ( {
706
+ kvt,
707
+ nativeMsg : nativeMsg ,
708
+ feedFormat : feedFormat . name ,
709
+ } )
710
+ cb ( null , kvt )
711
+ } )
717
712
}
718
713
719
714
function del ( msgId , cb ) {
0 commit comments