@@ -74,11 +74,11 @@ export interface BulkStats {
74
74
aborted : boolean
75
75
}
76
76
77
- interface IndexAction {
77
+ interface IndexActionOperation {
78
78
index : T . BulkIndexOperation
79
79
}
80
80
81
- interface CreateAction {
81
+ interface CreateActionOperation {
82
82
create : T . BulkCreateOperation
83
83
}
84
84
@@ -90,7 +90,9 @@ interface DeleteAction {
90
90
delete : T . BulkDeleteOperation
91
91
}
92
92
93
- type UpdateAction = [ UpdateActionOperation , Record < string , any > ]
93
+ type CreateAction = CreateActionOperation | [ CreateActionOperation , unknown ]
94
+ type IndexAction = IndexActionOperation | [ IndexActionOperation , unknown ]
95
+ type UpdateAction = [ UpdateActionOperation , T . BulkUpdateAction ]
94
96
type Action = IndexAction | CreateAction | UpdateAction | DeleteAction
95
97
96
98
export interface OnDropDocument < TDocument = unknown > {
@@ -646,22 +648,21 @@ export default class Helpers {
646
648
for await ( const chunk of datasource ) {
647
649
if ( shouldAbort ) break
648
650
timeoutRef . refresh ( )
649
- const action = onDocument ( chunk )
650
- const operation = Array . isArray ( action )
651
- ? Object . keys ( action [ 0 ] ) [ 0 ]
652
- : Object . keys ( action ) [ 0 ]
651
+ const result = onDocument ( chunk )
652
+ const [ action , payload ] = Array . isArray ( result ) ? result : [ result , chunk ]
653
+ const operation = Object . keys ( action ) [ 0 ]
653
654
if ( operation === 'index' || operation === 'create' ) {
654
655
actionBody = serializer . serialize ( action )
655
- payloadBody = typeof chunk === 'string' ? chunk : serializer . serialize ( chunk )
656
+ payloadBody = typeof payload === 'string'
657
+ ? payload
658
+ : serializer . serialize ( payload )
656
659
chunkBytes += Buffer . byteLength ( actionBody ) + Buffer . byteLength ( payloadBody )
657
660
bulkBody . push ( actionBody , payloadBody )
658
661
} else if ( operation === 'update' ) {
659
- // @ts -expect-error in case of update action is an array
660
- actionBody = serializer . serialize ( action [ 0 ] )
662
+ actionBody = serializer . serialize ( action )
661
663
payloadBody = typeof chunk === 'string'
662
664
? `{"doc":${ chunk } }`
663
- // @ts -expect-error in case of update action is an array
664
- : serializer . serialize ( { doc : chunk , ...action [ 1 ] } )
665
+ : serializer . serialize ( { doc : chunk , ...payload } )
665
666
chunkBytes += Buffer . byteLength ( actionBody ) + Buffer . byteLength ( payloadBody )
666
667
bulkBody . push ( actionBody , payloadBody )
667
668
} else if ( operation === 'delete' ) {
@@ -675,10 +676,11 @@ export default class Helpers {
675
676
676
677
if ( chunkBytes >= flushBytes ) {
677
678
stats . bytes += chunkBytes
678
- const send = await semaphore ( )
679
- send ( bulkBody . slice ( ) )
679
+ const bulkBodyCopy = bulkBody . slice ( )
680
680
bulkBody . length = 0
681
681
chunkBytes = 0
682
+ const send = await semaphore ( )
683
+ send ( bulkBodyCopy )
682
684
}
683
685
}
684
686
0 commit comments