@based/functions 1.2.2 → 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.
- package/dist/functions.d.ts +61 -19
- package/dist/functions.js +26 -0
- package/dist/functions.js.map +1 -1
- package/package.json +3 -2
- package/src/functions.ts +170 -62
package/dist/functions.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ 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
|
export type ObservableUpdateFunction<K = any> = (data: K, checksum?: number, err?: any, cache?: Uint8Array, diff?: any, fromChecksum?: number, isDeflate?: boolean) => void;
|
|
6
7
|
export type ObserveErrorListener = (err: any) => void;
|
|
7
8
|
export type HttpHeaders = {
|
|
@@ -27,7 +28,7 @@ export type ChannelMessageFunctionInternal<K = any> = (message: K, err?: any) =>
|
|
|
27
28
|
export type UninstallFunction = () => Promise<void>;
|
|
28
29
|
type FunctionConfigShared = {
|
|
29
30
|
/** Function name */
|
|
30
|
-
name
|
|
31
|
+
name?: string;
|
|
31
32
|
/** In addition to the name, a function can have a custom path for HTTP requests.
|
|
32
33
|
* For example: `path: 'my/custom/path'` will result in the function being
|
|
33
34
|
* available with a request to `env.based.io/my/custom/path`
|
|
@@ -47,14 +48,40 @@ type FunctionConfigShared = {
|
|
|
47
48
|
internalOnly?: boolean;
|
|
48
49
|
/** Can hold extra information about a spec */
|
|
49
50
|
data?: any;
|
|
50
|
-
/** Unistall after idle, in ms */
|
|
51
|
+
/** Unistall after idle, in ms -1 indicates endless */
|
|
51
52
|
uninstallAfterIdleTime?: number;
|
|
52
53
|
/** Hook that fires on uninstall of the function e.g. to clean up database connections */
|
|
53
54
|
uninstall?: UninstallFunction;
|
|
54
55
|
/** Specific authorize for this function */
|
|
55
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;
|
|
56
82
|
};
|
|
57
|
-
export type
|
|
83
|
+
export type BasedFunctionTypes = 'channel' | 'query' | 'function' | 'stream';
|
|
84
|
+
export type BasedChannelFunctionConfig = {
|
|
58
85
|
/** Function type `channel, function, query, stream, authorize` */
|
|
59
86
|
type: 'channel';
|
|
60
87
|
/** Channel subscriber
|
|
@@ -81,32 +108,47 @@ export type BasedFunctionConfig = ({
|
|
|
81
108
|
publisher?: BasedChannelPublishFunction;
|
|
82
109
|
/** Makes only the publisher public */
|
|
83
110
|
publicPublisher?: boolean;
|
|
84
|
-
name: string;
|
|
85
111
|
/** How long should the channel subscriber remain active after all subscribers are gone, in ms */
|
|
86
112
|
closeAfterIdleTime?: number;
|
|
87
|
-
|
|
88
|
-
|
|
113
|
+
/** Only for Publisher */
|
|
114
|
+
httpResponse?: HttpResponse;
|
|
115
|
+
} & FunctionConfigShared;
|
|
116
|
+
export type BasedCallFunctionConfig = {
|
|
117
|
+
/** Function type `channel, function, query, stream` */
|
|
89
118
|
type: 'function';
|
|
90
|
-
|
|
91
|
-
name: string;
|
|
119
|
+
fn: BasedFunction;
|
|
92
120
|
httpResponse?: HttpResponse;
|
|
93
|
-
} & FunctionConfigShared
|
|
94
|
-
|
|
121
|
+
} & FunctionConfigShared;
|
|
122
|
+
export type BasedQueryFunctionConfig = {
|
|
123
|
+
/** Function type `channel, function, query, stream` */
|
|
95
124
|
type: 'query';
|
|
96
|
-
|
|
97
|
-
name: string;
|
|
125
|
+
fn: BasedQueryFunction;
|
|
98
126
|
httpResponse?: HttpResponse;
|
|
99
127
|
/** How long should the query function remain active after all subscribers are gone, in ms */
|
|
100
128
|
closeAfterIdleTime?: number;
|
|
101
|
-
} & FunctionConfigShared
|
|
102
|
-
|
|
129
|
+
} & FunctionConfigShared;
|
|
130
|
+
export type BasedStreamFunctionConfig = {
|
|
131
|
+
/** Function type `channel, function, query, stream` */
|
|
103
132
|
type: 'stream';
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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` */
|
|
108
138
|
type: 'authorize';
|
|
109
139
|
function: Authorize;
|
|
110
|
-
|
|
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>;
|
|
111
153
|
};
|
|
112
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
|
package/dist/functions.js.map
CHANGED
|
@@ -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": "
|
|
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
|
-
"
|
|
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,6 +2,7 @@ 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
7
|
export type ObservableUpdateFunction<K = any> = (
|
|
7
8
|
data: K,
|
|
@@ -94,7 +95,7 @@ export type UninstallFunction = () => Promise<void>
|
|
|
94
95
|
|
|
95
96
|
type FunctionConfigShared = {
|
|
96
97
|
/** Function name */
|
|
97
|
-
name
|
|
98
|
+
name?: string
|
|
98
99
|
/** In addition to the name, a function can have a custom path for HTTP requests.
|
|
99
100
|
* For example: `path: 'my/custom/path'` will result in the function being
|
|
100
101
|
* available with a request to `env.based.io/my/custom/path`
|
|
@@ -114,71 +115,178 @@ type FunctionConfigShared = {
|
|
|
114
115
|
internalOnly?: boolean
|
|
115
116
|
/** Can hold extra information about a spec */
|
|
116
117
|
data?: any
|
|
117
|
-
/** Unistall after idle, in ms */
|
|
118
|
+
/** Unistall after idle, in ms -1 indicates endless */
|
|
118
119
|
uninstallAfterIdleTime?: number
|
|
119
120
|
/** Hook that fires on uninstall of the function e.g. to clean up database connections */
|
|
120
121
|
uninstall?: UninstallFunction
|
|
121
122
|
/** Specific authorize for this function */
|
|
122
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
|
|
123
149
|
}
|
|
124
150
|
|
|
125
|
-
export type
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
+
}
|