@eleven-am/pondsocket 0.1.62 → 0.1.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,91 +1,72 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseAddress = void 0;
4
- /**
5
- * @desc Returns the {key: value} matches of a string
6
- * @param path - the string to create the regex from
7
- * @param address - the pattern to match
8
- *
9
- * @example
10
- * /api/:id should match /api/123 and return { id: 123 }
11
- * /api/:id/:name should match /api/123/abc and return { id: 123, name: abc }
12
- * hello:id should match hello:123 and return { id: 123 }
13
- * @private
14
- */
15
- function matchPath(path, address) {
16
- const pathParts = path.split('/');
17
- const addressParts = address.split('/');
18
- const params = {};
19
- const length = Math.max(pathParts.length, addressParts.length);
20
- for (let i = 0; i < length; i++) {
21
- const pathPart = pathParts[i];
22
- const addressPart = addressParts[i];
23
- if (pathPart === undefined || addressPart === undefined) {
24
- return null;
25
- }
26
- else if (pathPart === '*') {
27
- return params;
4
+ function pathToRegexp(path) {
5
+ const parts = path.split('/');
6
+ const regexpParts = parts.map((part) => {
7
+ if (part.startsWith(':')) {
8
+ return '([^/]+)';
28
9
  }
29
- else if (pathPart.startsWith(':')) {
30
- params[pathPart.slice(1)] = addressPart;
10
+ else if (part === '*') {
11
+ return '(.*)';
31
12
  }
32
- else if (pathPart !== addressPart) {
33
- return null;
34
- }
35
- }
36
- return params;
13
+ return part;
14
+ });
15
+ return new RegExp(`^${regexpParts.join('/')}$`);
37
16
  }
38
- /**
39
- * @desc Given a Regex expression it returns an empty object if the address matches
40
- * @param regex - The regex to match the address against
41
- * @param address - The address ot match
42
- */
43
- function matchRegex(regex, address) {
44
- if (address.match(regex)) {
17
+ function getParams(path, route) {
18
+ const regexp = pathToRegexp(route);
19
+ const match = path.match(regexp);
20
+ if (!match) {
21
+ return null;
22
+ }
23
+ const cleanRoute = route.replace(/:/g, '');
24
+ const cleanMatch = cleanRoute.match(regexp);
25
+ if (!cleanMatch) {
26
+ return null;
27
+ }
28
+ const keys = cleanMatch.slice(1).filter((s) => s !== '');
29
+ const values = match.slice(1).filter((s) => s !== '');
30
+ if (keys.length !== values.length) {
45
31
  return {};
46
32
  }
47
- return null;
33
+ return keys.reduce((params, key, index) => {
34
+ if (key === '*') {
35
+ params[key] = `/${values.slice(index).join('/')}`.replace(/\/+/g, '/');
36
+ return params;
37
+ }
38
+ params[key] = values[index];
39
+ return params;
40
+ }, {});
48
41
  }
49
- /**
50
- * @desc Creates an object from the params of a path
51
- * @param address - the path to create the object from
52
- *
53
- * @example
54
- * /api/id?name=abc should return { name: 'abc' }
55
- * /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
56
- */
57
- function getQuery(address) {
58
- const obj = {};
59
- const params = address.split('?')[1];
60
- if (params) {
61
- params.split('&').forEach((param) => {
62
- const [key, value] = param.split('=');
63
- obj[key] = value;
64
- });
42
+ function getQuery(query) {
43
+ if (!query) {
44
+ return {};
65
45
  }
66
- return obj;
46
+ const parts = query.split('&');
47
+ return parts.reduce((params, part) => {
48
+ const [key, value] = part.split('=');
49
+ params[key] = value;
50
+ return params;
51
+ }, {});
67
52
  }
68
- /**
69
- * @desc Generates a pond request resolver object
70
- * @param path - the path to resolve
71
- * @param address - the address to resolve
72
- */
73
- function parseAddress(path, address) {
74
- let params;
75
- const [paramsPath] = address.split('?');
76
- if (typeof path === 'string') {
77
- params = matchPath(path, paramsPath);
78
- if (params === null) {
53
+ function parseAddress(route, address) {
54
+ if (route instanceof RegExp) {
55
+ const match = address.match(route);
56
+ if (!match) {
79
57
  return null;
80
58
  }
59
+ return {
60
+ params: {},
61
+ query: {},
62
+ };
81
63
  }
82
- else {
83
- params = matchRegex(path, paramsPath);
84
- if (params === null) {
85
- return null;
86
- }
64
+ const [path, queryParams] = address.split('?');
65
+ const params = getParams(path, route);
66
+ if (!params) {
67
+ return null;
87
68
  }
88
- const query = getQuery(address);
69
+ const query = getQuery(queryParams);
89
70
  return {
90
71
  params,
91
72
  query,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.62",
3
+ "version": "0.1.63",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -4,7 +4,6 @@ import { WebSocketServer } from 'ws';
4
4
 
5
5
  type Unsubscribe = () => void;
6
6
 
7
- export type default_t<T = any> = Record<string, T>;
8
7
  type IsParam<Path> = Path extends `:${infer Param}` ? Param : never;
9
8
 
10
9
  type FilteredParams<Path> = Path extends `${infer First}/${infer Second}`
@@ -22,7 +21,12 @@ type EventParams<Path> = {
22
21
  params: Params<Path>;
23
22
  }
24
23
 
25
- type PondObject = default_t;
24
+ type Primitives = number | string | boolean | null | undefined;
25
+
26
+ type PondObject = {
27
+ [key: string]: Primitives | PondObject | PondObject[];
28
+ }
29
+
26
30
  type PondPresence = PondObject;
27
31
  type PondMessage = PondObject;
28
32
  type PondAssigns = PondObject;
@@ -52,13 +56,6 @@ type IncomingConnection<Path> = EventParams<Path> & {
52
56
  address: string;
53
57
  }
54
58
 
55
- interface LeaveEvent {
56
- userId: string;
57
- assigns: PondAssigns;
58
- }
59
-
60
- type LeaveCallback = (event: LeaveEvent) => void;
61
-
62
59
  interface UserData {
63
60
  assigns: PondAssigns;
64
61
  presence: PondPresence;
@@ -482,12 +479,6 @@ export declare class PondChannel {
482
479
  *});
483
480
  */
484
481
  broadcast (event: string, payload: PondMessage, channelName?: string): void;
485
-
486
- /**
487
- * @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
488
- * @param callback - The callback to execute when a user leaves
489
- */
490
- public onLeave (callback: LeaveCallback): void;
491
482
  }
492
483
 
493
484
  declare class PondSocket {