Welcome to Microsoft Q&A! Thanks for posting the question.
In your scenario, as you have mentioned that using both receive mode you are facing the issue.
When you are using peekLock mode you observe that some of the messages are not received by your receiver application and restarting the script helps you to consume those messages. Looks like the message was received for the first time. As the client was not able to complete within the lock duration (default 30 sec). Once the lock gets expired the messages were visible again to be consumed by your application.
I believe you want to mention receiveAndDelete mode when you mentioned “Some of the messages get lost and do not put back to the Queue or Dead-Letter Queue.” . In receiveAndDelete mode, the message is delivered “at most once”. As soon the message is delivered to the client, the message will be deleted from the service bus end.
Looking into the snippet I can see that you are receiving 10 messages at one time and mapping it. All 10 messages acquire the lock at the same time and if your client is not able to call complete/abandon (due to any issue at the client) within the lock duration then those messages are visible again for the client to be consumed once the lock expires.
I have tested this https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/servicebus/service-bus/samples-v1/javascript/session.js and didn’t find any issue at my end. Testing your sample code at my end removing the mapping I didn’t found any issue as there was no logic in between the receive and complete calls. But adding the delay (time to execute business logic) the issue was reproduced.
You can either set the lock duration value to higher value (max 5 minutes) or tweak the receiving messages count according to the test result at your client end.
I hope this helps. Do let us know if you any further queries.