MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

allow closing sockets

+38 -1
+38 -1
src/builtins/node/http.mjs
··· 89 89 }; 90 90 } 91 91 92 + function closeSocket(socket) { 93 + if (!socket || socket.destroyed) return; 94 + if (typeof socket.destroy === 'function') socket.destroy(); 95 + else if (typeof socket.end === 'function') socket.end(); 96 + } 97 + 92 98 function appendSocketChunk(buffered, chunk) { 93 99 const next = bufferFrom(chunk); 94 100 if (buffered.length === 0) return next; ··· 842 848 this.emit('finish'); 843 849 this.emit('close'); 844 850 845 - keepAlive = this._shouldKeepAlive(); 851 + keepAlive = !this._socketState.server._closing && this._shouldKeepAlive(); 846 852 this._socketState.activeResponse = null; 847 853 if (typeof callback === 'function') callback(); 848 854 ··· 868 874 this.requestTimeout = 300000; 869 875 this.keepAliveTimeout = 5000; 870 876 this.maxHeadersCount = 2000; 877 + this._socketStates = new Set(); 878 + this._closing = false; 871 879 872 880 this.on('connection', socket => { 873 881 this._attachSocket(socket); ··· 878 886 879 887 _attachSocket(socket) { 880 888 const state = makeSocketState(this, socket); 889 + this._socketStates.add(state); 881 890 882 891 if (this.timeout > 0 && socket.setTimeout) { 883 892 socket.setTimeout(this.timeout, () => { ··· 896 905 897 906 socket.on('close', () => { 898 907 state.closed = true; 908 + this._socketStates.delete(state); 899 909 }); 910 + } 911 + 912 + listen(...args) { 913 + this._closing = false; 914 + return super.listen(...args); 915 + } 916 + 917 + close(callback) { 918 + this._closing = true; 919 + this.closeIdleConnections(); 920 + return super.close(callback); 921 + } 922 + 923 + closeAllConnections() { 924 + for (const state of [...this._socketStates]) { 925 + closeSocket(state.socket); 926 + } 927 + return this; 928 + } 929 + 930 + closeIdleConnections() { 931 + for (const state of [...this._socketStates]) { 932 + if (state.closed) continue; 933 + if (state.activeResponse && !state.activeResponse.writableEnded) continue; 934 + closeSocket(state.socket); 935 + } 936 + return this; 900 937 } 901 938 902 939 _drainSocket(socket, state) {