@based/functions 1.2.1 → 2.0.0

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.
@@ -2,10 +2,8 @@ import { Context, HttpSession } from './context';
2
2
  import { BasedFunctionClient } from './client';
3
3
  import { BasedDataStream } from './stream';
4
4
  import { Authorize } from './auth';
5
- export type ObservableUpdateFunction<K = any> = {
6
- (data: K, checksum?: number, diff?: any, fromChecksum?: number, isDeflate?: boolean, rawData?: K, err?: any): void;
7
- __internalObs__?: true;
8
- };
5
+ import { Required } from 'utility-types';
6
+ export type ObservableUpdateFunction<K = any> = (data: K, checksum?: number, err?: any, cache?: Uint8Array, diff?: any, fromChecksum?: number, isDeflate?: boolean) => void;
9
7
  export type ObserveErrorListener = (err: any) => void;
10
8
  export type HttpHeaders = {
11
9
  [header: string]: number | string | (string | number)[];
@@ -30,7 +28,7 @@ export type ChannelMessageFunctionInternal<K = any> = (message: K, err?: any) =>
30
28
  export type UninstallFunction = () => Promise<void>;
31
29
  type FunctionConfigShared = {
32
30
  /** Function name */
33
- name: string;
31
+ name?: string;
34
32
  /** In addition to the name, a function can have a custom path for HTTP requests.
35
33
  * For example: `path: 'my/custom/path'` will result in the function being
36
34
  * available with a request to `env.based.io/my/custom/path`
@@ -50,14 +48,40 @@ type FunctionConfigShared = {
50
48
  internalOnly?: boolean;
51
49
  /** Can hold extra information about a spec */
52
50
  data?: any;
53
- /** Unistall after idle, in ms */
51
+ /** Unistall after idle, in ms -1 indicates endless */
54
52
  uninstallAfterIdleTime?: number;
55
53
  /** Hook that fires on uninstall of the function e.g. to clean up database connections */
56
54
  uninstall?: UninstallFunction;
57
55
  /** Specific authorize for this function */
58
56
  authorize?: Authorize;
57
+ /** Relay allows functions to relay traffic to another `@based/server`
58
+ `Currently not supported for `stream`
59
+
60
+ ```js
61
+ new BasedServer({
62
+ clients: { events: BasedClient },
63
+ functions: {
64
+ specs:
65
+ somethingToRelay: {
66
+ relay: { client: 'events', target: 'hello' },
67
+ type: 'function'
68
+ })
69
+ }
70
+ }
71
+ })
72
+ ```
73
+ */
74
+ relay?: {
75
+ client: string;
76
+ target?: string;
77
+ };
78
+ /** Function version */
79
+ version?: number;
80
+ /** Used inernaly to check if a function is ready to uninstall */
81
+ timeoutCounter?: number;
59
82
  };
60
- export type BasedFunctionConfig = ({
83
+ export type BasedFunctionTypes = 'channel' | 'query' | 'function' | 'stream';
84
+ export type BasedChannelFunctionConfig = {
61
85
  /** Function type `channel, function, query, stream, authorize` */
62
86
  type: 'channel';
63
87
  /** Channel subscriber
@@ -84,32 +108,47 @@ export type BasedFunctionConfig = ({
84
108
  publisher?: BasedChannelPublishFunction;
85
109
  /** Makes only the publisher public */
86
110
  publicPublisher?: boolean;
87
- name: string;
88
111
  /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
89
112
  closeAfterIdleTime?: number;
90
- } & FunctionConfigShared) | ({
91
- /** Function type `channel, function, query, stream, authorize` */
113
+ /** Only for Publisher */
114
+ httpResponse?: HttpResponse;
115
+ } & FunctionConfigShared;
116
+ export type BasedCallFunctionConfig = {
117
+ /** Function type `channel, function, query, stream` */
92
118
  type: 'function';
93
- function: BasedFunction;
94
- name: string;
119
+ fn: BasedFunction;
95
120
  httpResponse?: HttpResponse;
96
- } & FunctionConfigShared) | ({
97
- /** Function type `channel, function, query, stream, authorize` */
121
+ } & FunctionConfigShared;
122
+ export type BasedQueryFunctionConfig = {
123
+ /** Function type `channel, function, query, stream` */
98
124
  type: 'query';
99
- function: BasedQueryFunction;
100
- name: string;
125
+ fn: BasedQueryFunction;
101
126
  httpResponse?: HttpResponse;
102
127
  /** How long should the query function remain active after all subscribers are gone, in ms */
103
128
  closeAfterIdleTime?: number;
104
- } & FunctionConfigShared) | ({
105
- /** Function type `channel, function, query, stream, authorize` */
129
+ } & FunctionConfigShared;
130
+ export type BasedStreamFunctionConfig = {
131
+ /** Function type `channel, function, query, stream` */
106
132
  type: 'stream';
107
- function: BasedStreamFunction;
108
- name: string;
109
- } & FunctionConfigShared) | {
110
- /** Function type `channel, function, query, stream, authorize` */
133
+ fn: BasedStreamFunction;
134
+ } & FunctionConfigShared;
135
+ export type BasedFunctionConfig<T extends BasedFunctionTypes = BasedFunctionTypes> = T extends 'channel' ? BasedChannelFunctionConfig : T extends 'function' ? BasedCallFunctionConfig : T extends 'query' ? BasedQueryFunctionConfig : T extends 'stream' ? BasedStreamFunctionConfig : BasedChannelFunctionConfig | BasedCallFunctionConfig | BasedQueryFunctionConfig | BasedStreamFunctionConfig;
136
+ export type BasedAuthorizeFunctionConfig = {
137
+ /** Function type `authorize` */
111
138
  type: 'authorize';
112
139
  function: Authorize;
113
- name: string;
140
+ };
141
+ export type BasedRoute<T extends BasedFunctionTypes = BasedFunctionTypes, R extends keyof BasedFunctionConfig = 'type' | 'name'> = Required<Partial<BasedFunctionConfig<T>>, R>;
142
+ export type BasedFunctionConfigComplete<T extends BasedFunctionTypes = BasedFunctionTypes> = Required<BasedFunctionConfig<T>, 'maxPayloadSize' | 'rateLimitTokens' | 'version' | 'name'>;
143
+ export type BasedRouteComplete<T extends BasedFunctionTypes = BasedFunctionTypes> = Required<Partial<BasedFunctionConfig<T>>, 'type' | 'name' | 'maxPayloadSize' | 'rateLimitTokens'>;
144
+ export declare function isBasedRoute<T extends BasedFunctionTypes>(type: T, route: any): route is BasedRoute<T>;
145
+ export declare function isAnyBasedRoute(route: any): route is BasedRoute;
146
+ export declare function isBasedFunctionConfig<T extends BasedFunctionTypes>(type: T, config: any): config is BasedFunctionConfig<T>;
147
+ export declare function isAnyBasedFunctionConfig(config: any): config is BasedFunctionConfig;
148
+ export type BasedRoutes = {
149
+ [name: string]: BasedRoute<BasedFunctionTypes, 'type'>;
150
+ };
151
+ export type BasedFunctionConfigs = {
152
+ [name: string]: BasedFunctionConfig<BasedFunctionTypes>;
114
153
  };
115
154
  export {};
package/dist/functions.js CHANGED
@@ -1,3 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAnyBasedFunctionConfig = exports.isBasedFunctionConfig = exports.isAnyBasedRoute = exports.isBasedRoute = void 0;
4
+ function isBasedRoute(type, route) {
5
+ return (route &&
6
+ typeof route === 'object' &&
7
+ route.type === type &&
8
+ typeof route.name === 'string');
9
+ }
10
+ exports.isBasedRoute = isBasedRoute;
11
+ function isAnyBasedRoute(route) {
12
+ return (route &&
13
+ typeof route === 'object' &&
14
+ (route.type === 'channel' ||
15
+ route.type === 'query' ||
16
+ route.type === 'function' ||
17
+ route.type === 'stream') &&
18
+ typeof route.name === 'string');
19
+ }
20
+ exports.isAnyBasedRoute = isAnyBasedRoute;
21
+ function isBasedFunctionConfig(type, config) {
22
+ return isBasedRoute(type, config);
23
+ }
24
+ exports.isBasedFunctionConfig = isBasedFunctionConfig;
25
+ function isAnyBasedFunctionConfig(config) {
26
+ return isAnyBasedRoute(config);
27
+ }
28
+ exports.isAnyBasedFunctionConfig = isAnyBasedFunctionConfig;
3
29
  //# sourceMappingURL=functions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;AAwPA,SAAgB,YAAY,CAC1B,IAAO,EACP,KAAU;IAEV,OAAO,CACL,KAAK;QACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,CAAC,IAAI,KAAK,IAAI;QACnB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAC/B,CAAA;AACH,CAAC;AAVD,oCAUC;AAED,SAAgB,eAAe,CAAC,KAAU;IACxC,OAAO,CACL,KAAK;QACL,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS;YACvB,KAAK,CAAC,IAAI,KAAK,OAAO;YACtB,KAAK,CAAC,IAAI,KAAK,UAAU;YACzB,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;QAC1B,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAC/B,CAAA;AACH,CAAC;AAVD,0CAUC;AAED,SAAgB,qBAAqB,CACnC,IAAO,EACP,MAAW;IAEX,OAAO,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACnC,CAAC;AALD,sDAKC;AAED,SAAgB,wBAAwB,CACtC,MAAW;IAEX,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;AAChC,CAAC;AAJD,4DAIC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/functions",
3
- "version": "1.2.1",
3
+ "version": "2.0.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "sideEffects": false,
12
12
  "dependencies": {
13
- "@saulx/utils": "^3.2.1"
13
+ "utility-types": "^3.10.0",
14
+ "@saulx/utils": "^3.2.2"
14
15
  },
15
16
  "devDependencies": {
16
17
  "ts-node": "10.9.1",
package/src/functions.ts CHANGED
@@ -2,22 +2,18 @@ import { Context, HttpSession } from './context'
2
2
  import { BasedFunctionClient } from './client'
3
3
  import { BasedDataStream } from './stream'
4
4
  import { Authorize } from './auth'
5
+ import { Required } from 'utility-types'
5
6
 
6
- export type ObservableUpdateFunction<K = any> = {
7
- (
8
- data: K,
9
- checksum?: number,
10
- diff?: any,
11
- fromChecksum?: number,
12
- isDeflate?: boolean,
13
- rawData?: K,
14
- // todo fix there errors TODO: make extra package 'errors' for client and server
15
- err?: any
16
- ): void
17
- __internalObs__?: true
18
- }
7
+ export type ObservableUpdateFunction<K = any> = (
8
+ data: K,
9
+ checksum?: number,
10
+ err?: any,
11
+ cache?: Uint8Array,
12
+ diff?: any,
13
+ fromChecksum?: number,
14
+ isDeflate?: boolean
15
+ ) => void
19
16
 
20
- // TODO: use error package
21
17
  export type ObserveErrorListener = (err: any) => void
22
18
 
23
19
  export type HttpHeaders = {
@@ -99,7 +95,7 @@ export type UninstallFunction = () => Promise<void>
99
95
 
100
96
  type FunctionConfigShared = {
101
97
  /** Function name */
102
- name: string
98
+ name?: string
103
99
  /** In addition to the name, a function can have a custom path for HTTP requests.
104
100
  * For example: `path: 'my/custom/path'` will result in the function being
105
101
  * available with a request to `env.based.io/my/custom/path`
@@ -119,71 +115,178 @@ type FunctionConfigShared = {
119
115
  internalOnly?: boolean
120
116
  /** Can hold extra information about a spec */
121
117
  data?: any
122
- /** Unistall after idle, in ms */
118
+ /** Unistall after idle, in ms -1 indicates endless */
123
119
  uninstallAfterIdleTime?: number
124
120
  /** Hook that fires on uninstall of the function e.g. to clean up database connections */
125
121
  uninstall?: UninstallFunction
126
122
  /** Specific authorize for this function */
127
123
  authorize?: Authorize
124
+ /** Relay allows functions to relay traffic to another `@based/server`
125
+ `Currently not supported for `stream`
126
+
127
+ ```js
128
+ new BasedServer({
129
+ clients: { events: BasedClient },
130
+ functions: {
131
+ specs:
132
+ somethingToRelay: {
133
+ relay: { client: 'events', target: 'hello' },
134
+ type: 'function'
135
+ })
136
+ }
137
+ }
138
+ })
139
+ ```
140
+ */
141
+ relay?: {
142
+ client: string
143
+ target?: string
144
+ }
145
+ /** Function version */
146
+ version?: number
147
+ /** Used inernaly to check if a function is ready to uninstall */
148
+ timeoutCounter?: number
128
149
  }
129
150
 
130
- export type BasedFunctionConfig =
131
- | ({
132
- /** Function type `channel, function, query, stream, authorize` */
133
- type: 'channel'
134
- /** Channel subscriber
135
-
136
- ```js
137
- const subscribe = (based, payload, id, update) => {
138
- let cnt = 0
139
- const interval = setInterval(() => {
140
- update(++cnt)
141
- })
142
- return () => clearInterval(cnt)
143
- }
144
- ```
145
- */
146
- subscriber?: BasedChannelFunction
147
- /** Channel publisher
148
-
149
- ```js
150
- const publisher = (based, payload, msg, id) => {
151
- publishToChannel(id, msg)
152
- }
153
- ```
154
- */
155
- publisher?: BasedChannelPublishFunction
156
- /** Makes only the publisher public */
157
- publicPublisher?: boolean
158
- name: string
159
- /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
160
- closeAfterIdleTime?: number
161
- } & FunctionConfigShared)
162
- | ({
163
- /** Function type `channel, function, query, stream, authorize` */
164
- type: 'function'
165
- function: BasedFunction
166
- name: string
167
- httpResponse?: HttpResponse
168
- } & FunctionConfigShared)
169
- | ({
170
- /** Function type `channel, function, query, stream, authorize` */
171
- type: 'query'
172
- function: BasedQueryFunction
173
- name: string
174
- httpResponse?: HttpResponse
175
- /** How long should the query function remain active after all subscribers are gone, in ms */
176
- closeAfterIdleTime?: number
177
- } & FunctionConfigShared)
178
- | ({
179
- /** Function type `channel, function, query, stream, authorize` */
180
- type: 'stream'
181
- function: BasedStreamFunction
182
- name: string
183
- } & FunctionConfigShared)
184
- | {
185
- /** Function type `channel, function, query, stream, authorize` */
186
- type: 'authorize'
187
- function: Authorize
188
- name: string
189
- }
151
+ export type BasedFunctionTypes = 'channel' | 'query' | 'function' | 'stream'
152
+
153
+ export type BasedChannelFunctionConfig = {
154
+ /** Function type `channel, function, query, stream, authorize` */
155
+ type: 'channel'
156
+ /** Channel subscriber
157
+
158
+ ```js
159
+ const subscribe = (based, payload, id, update) => {
160
+ let cnt = 0
161
+ const interval = setInterval(() => {
162
+ update(++cnt)
163
+ })
164
+ return () => clearInterval(cnt)
165
+ }
166
+ ```
167
+ */
168
+ subscriber?: BasedChannelFunction
169
+ /** Channel publisher
170
+
171
+ ```js
172
+ const publisher = (based, payload, msg, id) => {
173
+ publishToChannel(id, msg)
174
+ }
175
+ ```
176
+ */
177
+ publisher?: BasedChannelPublishFunction
178
+ /** Makes only the publisher public */
179
+ publicPublisher?: boolean
180
+ /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
181
+ closeAfterIdleTime?: number
182
+ /** Only for Publisher */
183
+ httpResponse?: HttpResponse
184
+ } & FunctionConfigShared
185
+
186
+ export type BasedCallFunctionConfig = {
187
+ /** Function type `channel, function, query, stream` */
188
+ type: 'function'
189
+ fn: BasedFunction
190
+ httpResponse?: HttpResponse
191
+ } & FunctionConfigShared
192
+
193
+ export type BasedQueryFunctionConfig = {
194
+ /** Function type `channel, function, query, stream` */
195
+ type: 'query'
196
+ fn: BasedQueryFunction
197
+ httpResponse?: HttpResponse
198
+ /** How long should the query function remain active after all subscribers are gone, in ms */
199
+ closeAfterIdleTime?: number
200
+ } & FunctionConfigShared
201
+
202
+ export type BasedStreamFunctionConfig = {
203
+ /** Function type `channel, function, query, stream` */
204
+ type: 'stream'
205
+ fn: BasedStreamFunction
206
+ } & FunctionConfigShared
207
+
208
+ export type BasedFunctionConfig<
209
+ T extends BasedFunctionTypes = BasedFunctionTypes
210
+ > = T extends 'channel'
211
+ ? BasedChannelFunctionConfig
212
+ : T extends 'function'
213
+ ? BasedCallFunctionConfig
214
+ : T extends 'query'
215
+ ? BasedQueryFunctionConfig
216
+ : T extends 'stream'
217
+ ? BasedStreamFunctionConfig
218
+ :
219
+ | BasedChannelFunctionConfig
220
+ | BasedCallFunctionConfig
221
+ | BasedQueryFunctionConfig
222
+ | BasedStreamFunctionConfig
223
+
224
+ export type BasedAuthorizeFunctionConfig = {
225
+ /** Function type `authorize` */
226
+ type: 'authorize'
227
+ function: Authorize
228
+ }
229
+
230
+ export type BasedRoute<
231
+ T extends BasedFunctionTypes = BasedFunctionTypes,
232
+ R extends keyof BasedFunctionConfig = 'type' | 'name'
233
+ > = Required<Partial<BasedFunctionConfig<T>>, R>
234
+
235
+ export type BasedFunctionConfigComplete<
236
+ T extends BasedFunctionTypes = BasedFunctionTypes
237
+ > = Required<
238
+ BasedFunctionConfig<T>,
239
+ 'maxPayloadSize' | 'rateLimitTokens' | 'version' | 'name'
240
+ >
241
+
242
+ export type BasedRouteComplete<
243
+ T extends BasedFunctionTypes = BasedFunctionTypes
244
+ > = Required<
245
+ Partial<BasedFunctionConfig<T>>,
246
+ 'type' | 'name' | 'maxPayloadSize' | 'rateLimitTokens'
247
+ >
248
+
249
+ export function isBasedRoute<T extends BasedFunctionTypes>(
250
+ type: T,
251
+ route: any
252
+ ): route is BasedRoute<T> {
253
+ return (
254
+ route &&
255
+ typeof route === 'object' &&
256
+ route.type === type &&
257
+ typeof route.name === 'string'
258
+ )
259
+ }
260
+
261
+ export function isAnyBasedRoute(route: any): route is BasedRoute {
262
+ return (
263
+ route &&
264
+ typeof route === 'object' &&
265
+ (route.type === 'channel' ||
266
+ route.type === 'query' ||
267
+ route.type === 'function' ||
268
+ route.type === 'stream') &&
269
+ typeof route.name === 'string'
270
+ )
271
+ }
272
+
273
+ export function isBasedFunctionConfig<T extends BasedFunctionTypes>(
274
+ type: T,
275
+ config: any
276
+ ): config is BasedFunctionConfig<T> {
277
+ return isBasedRoute(type, config)
278
+ }
279
+
280
+ export function isAnyBasedFunctionConfig(
281
+ config: any
282
+ ): config is BasedFunctionConfig {
283
+ return isAnyBasedRoute(config)
284
+ }
285
+
286
+ export type BasedRoutes = {
287
+ [name: string]: BasedRoute<BasedFunctionTypes, 'type'>
288
+ }
289
+
290
+ export type BasedFunctionConfigs = {
291
+ [name: string]: BasedFunctionConfig<BasedFunctionTypes>
292
+ }