* Commits session changes. No more mutations will be allowed after commit. * * @example * await session.commit() // Save all changes to the session store
()
| 672 | * await session.commit() // Save all changes to the session store |
| 673 | */ |
| 674 | async commit() { |
| 675 | if (!this.#valuesStore || this.readonly) { |
| 676 | return |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * If the flash messages store is not empty, we should put |
| 681 | * its messages inside main session store. |
| 682 | */ |
| 683 | if (!this.responseFlashMessages.isEmpty) { |
| 684 | const { input, reflashed, ...others } = this.responseFlashMessages.all() |
| 685 | this.put(this.flashKey, { ...reflashed, ...input, ...others }) |
| 686 | } |
| 687 | |
| 688 | debug('committing session data') |
| 689 | |
| 690 | /** |
| 691 | * Touch the session id cookie to stay alive |
| 692 | */ |
| 693 | this.#ctx.response.cookie(this.config.cookieName, this.#sessionId, this.config.cookie!) |
| 694 | |
| 695 | /** |
| 696 | * Delete the session data when the session store |
| 697 | * is empty. |
| 698 | * |
| 699 | * Also we only destroy the session id we read from the cookie. |
| 700 | * If there was no session id in the cookie, there won't be |
| 701 | * any data inside the store either. |
| 702 | */ |
| 703 | if (this.isEmpty) { |
| 704 | if (this.#sessionIdFromCookie) { |
| 705 | await this.#store.destroy(this.#sessionIdFromCookie) |
| 706 | } |
| 707 | this.#emitter.emit('session:committed', { session: this }) |
| 708 | return |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Touch the store expiry when the session store was |
| 713 | * not modified. |
| 714 | */ |
| 715 | if (!this.hasBeenModified) { |
| 716 | if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) { |
| 717 | await this.#store.destroy(this.#sessionIdFromCookie) |
| 718 | await this.#store.write(this.#sessionId, this.#valuesStore.toJSON()) |
| 719 | this.#emitter.emit('session:migrated', { |
| 720 | fromSessionId: this.#sessionIdFromCookie, |
| 721 | toSessionId: this.sessionId, |
| 722 | session: this, |
| 723 | }) |
| 724 | } else { |
| 725 | await this.#store.touch(this.#sessionId) |
| 726 | } |
| 727 | this.#emitter.emit('session:committed', { session: this }) |
| 728 | return |
| 729 | } |
| 730 | |
| 731 | /** |