I needed corresponding calculations in my addon THREEg. Maybe you can take something from there.
https://github.com/hofk/THREEg.js/blob/c0f2e8063922465a32e2887d3f79a9c24c84d5bf/THREEg.js from line 3776
see code from line 3978 …
see also Addon to create special / extended geometries
https://hofk.de/main/threejs/WallTHREEg/WallTHREEg.html