ws.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function isSecure(wsComponents) {
  2. return typeof wsComponents.secure === 'boolean' ? wsComponents.secure : String(wsComponents.scheme).toLowerCase() === "wss";
  3. }
  4. //RFC 6455
  5. const handler = {
  6. scheme: "ws",
  7. domainHost: true,
  8. parse: function (components, options) {
  9. const wsComponents = components;
  10. //indicate if the secure flag is set
  11. wsComponents.secure = isSecure(wsComponents);
  12. //construct resouce name
  13. wsComponents.resourceName = (wsComponents.path || '/') + (wsComponents.query ? '?' + wsComponents.query : '');
  14. wsComponents.path = undefined;
  15. wsComponents.query = undefined;
  16. return wsComponents;
  17. },
  18. serialize: function (wsComponents, options) {
  19. //normalize the default port
  20. if (wsComponents.port === (isSecure(wsComponents) ? 443 : 80) || wsComponents.port === "") {
  21. wsComponents.port = undefined;
  22. }
  23. //ensure scheme matches secure flag
  24. if (typeof wsComponents.secure === 'boolean') {
  25. wsComponents.scheme = (wsComponents.secure ? 'wss' : 'ws');
  26. wsComponents.secure = undefined;
  27. }
  28. //reconstruct path from resource name
  29. if (wsComponents.resourceName) {
  30. const [path, query] = wsComponents.resourceName.split('?');
  31. wsComponents.path = (path && path !== '/' ? path : undefined);
  32. wsComponents.query = query;
  33. wsComponents.resourceName = undefined;
  34. }
  35. //forbid fragment component
  36. wsComponents.fragment = undefined;
  37. return wsComponents;
  38. }
  39. };
  40. export default handler;
  41. //# sourceMappingURL=ws.js.map