@based/functions 1.1.0 → 1.1.2
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/README.md +2 -2
- package/dist/auth.d.ts +1 -1
- package/dist/context.d.ts +15 -0
- package/dist/context.js.map +1 -1
- package/dist/functions.d.ts +88 -2
- package/package.json +1 -1
- package/src/auth.ts +2 -1
- package/src/context.ts +5 -0
- package/src/functions.ts +95 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @based/functions
|
|
2
2
|
|
|
3
|
-
To be with based cloud functions, adds types and utilities.
|
|
3
|
+
To be used with based cloud functions, adds types and utilities.
|
|
4
4
|
|
|
5
5
|
- Example based function
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ import { BasedFunction } from '@based/functions'
|
|
|
9
9
|
|
|
10
10
|
const submitVote: BasedFunction<
|
|
11
11
|
{ target: number },
|
|
12
|
-
|
|
12
|
+
'ok' | 'not-ok'
|
|
13
13
|
> = async (based, { target }) => {
|
|
14
14
|
if (target > 10) {
|
|
15
15
|
return 'ok'
|
package/dist/auth.d.ts
CHANGED
|
@@ -12,4 +12,4 @@ export type AuthState = {
|
|
|
12
12
|
export type Authorize = (based: BasedFunctionClient, context: Context<HttpSession | WebSocketSession>, name: string, // name as generic dope
|
|
13
13
|
payload?: any) => Promise<boolean>;
|
|
14
14
|
export type VerifyAuthState = (based: BasedFunctionClient, context: Context<HttpSession | WebSocketSession>, authState: AuthState) => Promise<true | AuthState>;
|
|
15
|
-
export type AuthorizeConnection = (based: BasedFunctionClient, req: HttpRequest) => Promise<boolean>;
|
|
15
|
+
export type AuthorizeConnection = (based: BasedFunctionClient, req: HttpRequest, ip: string) => Promise<boolean>;
|
package/dist/context.d.ts
CHANGED
|
@@ -17,6 +17,9 @@ export type WebSocketSession = {
|
|
|
17
17
|
name: string;
|
|
18
18
|
payload: any;
|
|
19
19
|
}>;
|
|
20
|
+
headers: {
|
|
21
|
+
[key: string]: string;
|
|
22
|
+
};
|
|
20
23
|
unauthorizedChannels?: Set<{
|
|
21
24
|
id: number;
|
|
22
25
|
name: string;
|
|
@@ -50,21 +53,33 @@ export type HttpSession = {
|
|
|
50
53
|
export type InternalSessionObservable = {
|
|
51
54
|
id: number;
|
|
52
55
|
name: string;
|
|
56
|
+
headers: {
|
|
57
|
+
[key: string]: string;
|
|
58
|
+
};
|
|
53
59
|
type: 'query';
|
|
54
60
|
};
|
|
55
61
|
export type InternalSessionChannel = {
|
|
56
62
|
id: number;
|
|
57
63
|
name: string;
|
|
64
|
+
headers: {
|
|
65
|
+
[key: string]: string;
|
|
66
|
+
};
|
|
58
67
|
type: 'channel';
|
|
59
68
|
};
|
|
60
69
|
export type InternalSessionClient = {
|
|
61
70
|
client: BasedFunctionClient;
|
|
71
|
+
headers: {
|
|
72
|
+
[key: string]: string;
|
|
73
|
+
};
|
|
62
74
|
type: 'client';
|
|
63
75
|
};
|
|
64
76
|
export type InternalSession = InternalSessionClient | InternalSessionObservable | InternalSessionChannel;
|
|
65
77
|
export type MinimalExternalSession = {
|
|
66
78
|
ua: string;
|
|
67
79
|
ip: string;
|
|
80
|
+
headers: {
|
|
81
|
+
[key: string]: string;
|
|
82
|
+
};
|
|
68
83
|
};
|
|
69
84
|
export type Session = (WebSocketSession | HttpSession | InternalSession | MinimalExternalSession) & {
|
|
70
85
|
/** Only available in Ws and Http contexts */
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;;AAsGO,MAAM,aAAa,GAAG,CAC3B,GAAqB,EACQ,EAAE;IAC/B,IAAI,KAAK,IAAI,GAAG,EAAE,OAAO,EAAE;QACzB,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,aAAa,iBAOzB;AAEM,MAAM,WAAW,GAAG,CACzB,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,IAAA,mBAAW,EAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,WAAW,eAOvB;AAEM,MAAM,eAAe,GAAG,CAC7B,GAAqB,EAC2B,EAAE;IAClD,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,IAAA,mBAAW,EAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAA,qBAAa,EAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE;QAC3E,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,eAAe,mBAO3B;AAEM,MAAM,aAAa,GAAG,CAAC,OAAgB,EAA0B,EAAE;IACxE,IAAI,KAAK,IAAI,OAAO,EAAE;QACpB,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AALY,QAAA,aAAa,iBAKzB;AAEM,MAAM,WAAW,GAAG,CAAC,OAAgB,EAA+B,EAAE;IAC3E,IAAI,MAAM,IAAI,OAAO,EAAE;QACrB,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AALY,QAAA,WAAW,eAKvB"}
|
package/dist/functions.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Context, HttpSession } from './context';
|
|
2
2
|
import { BasedFunctionClient } from './client';
|
|
3
3
|
import { BasedDataStream } from './stream';
|
|
4
|
+
import { Authorize } from './auth';
|
|
4
5
|
export type ObservableUpdateFunction<K = any> = {
|
|
5
6
|
(data: K, checksum?: number, diff?: any, fromChecksum?: number, isDeflate?: boolean, rawData?: K, err?: any): void;
|
|
6
7
|
__internalObs__?: true;
|
|
@@ -23,7 +24,92 @@ export type StreamPayload<P = any> = {
|
|
|
23
24
|
export type BasedStreamFunction<P = any, K = any> = BasedFunction<StreamPayload<P>, K>;
|
|
24
25
|
export type BasedQueryFunction<P = any, K = any> = ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>, error: ObserveErrorListener) => Promise<() => void>) | ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>, error: ObserveErrorListener) => () => void);
|
|
25
26
|
export type BasedChannelFunction<P = any, K = any> = (based: BasedFunctionClient, payload: P, id: number, update: ChannelMessageFunction<K>, error: (err?: any) => void) => () => void;
|
|
26
|
-
export type BasedChannelPublishFunction<P = any,
|
|
27
|
-
export type ChannelMessageFunction<
|
|
27
|
+
export type BasedChannelPublishFunction<P = any, M = any> = (based: BasedFunctionClient, payload: P, message: M, id: number, ctx: Context) => void;
|
|
28
|
+
export type ChannelMessageFunction<M = any> = (message: M) => void;
|
|
28
29
|
export type ChannelMessageFunctionInternal<K = any> = (message: K, err?: any) => void;
|
|
29
30
|
export type UninstallFunction = () => Promise<void>;
|
|
31
|
+
type FunctionConfigShared = {
|
|
32
|
+
/** Function name */
|
|
33
|
+
name: string;
|
|
34
|
+
/** In addition to the name, a function can have a custom path for HTTP requests.
|
|
35
|
+
* For example: `path: 'my/custom/path'` will result in the function being
|
|
36
|
+
* available with a request to `env.based.io/my/custom/path`
|
|
37
|
+
*/
|
|
38
|
+
path?: string;
|
|
39
|
+
/** In bytes. `-1` indicates no size limit */
|
|
40
|
+
maxPayloadSize?: number;
|
|
41
|
+
/** Cost in tokens for this function call. */
|
|
42
|
+
rateLimitTokens?: number;
|
|
43
|
+
/** A function marked as `public` will skip the call to authorize. */
|
|
44
|
+
public?: boolean;
|
|
45
|
+
/** Array of headers that this function expects to receive. */
|
|
46
|
+
headers?: string[];
|
|
47
|
+
/** A function marked as `internalOnly` will only be accessible from other server side functions,
|
|
48
|
+
* and not through the public internet.
|
|
49
|
+
*/
|
|
50
|
+
internalOnly?: boolean;
|
|
51
|
+
/** Can hold extra information about a spec */
|
|
52
|
+
data?: any;
|
|
53
|
+
/** Unistall after idle, in ms */
|
|
54
|
+
uninstallAfterIdleTime?: number;
|
|
55
|
+
/** Hook that fires on uninstall of the function e.g. to clean up database connections */
|
|
56
|
+
uninstall?: UninstallFunction;
|
|
57
|
+
/** Specific authorize for this function */
|
|
58
|
+
authorize?: Authorize;
|
|
59
|
+
};
|
|
60
|
+
export type BasedFunctionConfig = ({
|
|
61
|
+
/** Function type `channel, function, query, stream, authorize` */
|
|
62
|
+
type: 'channel';
|
|
63
|
+
/** Channel subscriber
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
const subscribe = (based, payload, id, update) => {
|
|
67
|
+
let cnt = 0
|
|
68
|
+
const interval = setInterval(() => {
|
|
69
|
+
update(++cnt)
|
|
70
|
+
})
|
|
71
|
+
return () => clearInterval(cnt)
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
*/
|
|
75
|
+
subscriber?: BasedChannelFunction;
|
|
76
|
+
/** Channel publisher
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const publisher = (based, payload, msg, id) => {
|
|
80
|
+
publishToChannel(id, msg)
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
*/
|
|
84
|
+
publisher?: BasedChannelPublishFunction;
|
|
85
|
+
/** Makes only the publisher public */
|
|
86
|
+
publicPublisher?: boolean;
|
|
87
|
+
name: string;
|
|
88
|
+
/** How long should the channel subscriber remain active after all subscribers are gone, in ms */
|
|
89
|
+
closeAfterIdleTime?: number;
|
|
90
|
+
} & FunctionConfigShared) | ({
|
|
91
|
+
/** Function type `channel, function, query, stream, authorize` */
|
|
92
|
+
type: 'function';
|
|
93
|
+
function: BasedFunction;
|
|
94
|
+
name: string;
|
|
95
|
+
httpResponse?: HttpResponse;
|
|
96
|
+
} & FunctionConfigShared) | ({
|
|
97
|
+
/** Function type `channel, function, query, stream, authorize` */
|
|
98
|
+
type: 'query';
|
|
99
|
+
function: BasedQueryFunction;
|
|
100
|
+
name: string;
|
|
101
|
+
httpResponse?: HttpResponse;
|
|
102
|
+
/** How long should the query function remain active after all subscribers are gone, in ms */
|
|
103
|
+
closeAfterIdleTime?: number;
|
|
104
|
+
} & FunctionConfigShared) | ({
|
|
105
|
+
/** Function type `channel, function, query, stream, authorize` */
|
|
106
|
+
type: 'stream';
|
|
107
|
+
function: BasedStreamFunction;
|
|
108
|
+
name: string;
|
|
109
|
+
} & FunctionConfigShared) | {
|
|
110
|
+
/** Function type `channel, function, query, stream, authorize` */
|
|
111
|
+
type: 'authorize';
|
|
112
|
+
function: Authorize;
|
|
113
|
+
name: string;
|
|
114
|
+
};
|
|
115
|
+
export {};
|
package/package.json
CHANGED
package/src/auth.ts
CHANGED
package/src/context.ts
CHANGED
|
@@ -19,6 +19,7 @@ export type WebSocketSession = {
|
|
|
19
19
|
name: string
|
|
20
20
|
payload: any
|
|
21
21
|
}>
|
|
22
|
+
headers: { [key: string]: string }
|
|
22
23
|
unauthorizedChannels?: Set<{
|
|
23
24
|
id: number
|
|
24
25
|
name: string
|
|
@@ -55,17 +56,20 @@ export type HttpSession = {
|
|
|
55
56
|
export type InternalSessionObservable = {
|
|
56
57
|
id: number
|
|
57
58
|
name: string
|
|
59
|
+
headers: { [key: string]: string }
|
|
58
60
|
type: 'query'
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
export type InternalSessionChannel = {
|
|
62
64
|
id: number
|
|
63
65
|
name: string
|
|
66
|
+
headers: { [key: string]: string }
|
|
64
67
|
type: 'channel'
|
|
65
68
|
}
|
|
66
69
|
|
|
67
70
|
export type InternalSessionClient = {
|
|
68
71
|
client: BasedFunctionClient
|
|
72
|
+
headers: { [key: string]: string }
|
|
69
73
|
type: 'client'
|
|
70
74
|
}
|
|
71
75
|
|
|
@@ -79,6 +83,7 @@ export type InternalSession =
|
|
|
79
83
|
export type MinimalExternalSession = {
|
|
80
84
|
ua: string
|
|
81
85
|
ip: string
|
|
86
|
+
headers: { [key: string]: string }
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
export type Session = (
|
package/src/functions.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Context, HttpSession } from './context'
|
|
2
2
|
import { BasedFunctionClient } from './client'
|
|
3
3
|
import { BasedDataStream } from './stream'
|
|
4
|
+
import { Authorize } from './auth'
|
|
4
5
|
|
|
5
6
|
export type ObservableUpdateFunction<K = any> = {
|
|
6
7
|
(
|
|
@@ -79,15 +80,15 @@ export type BasedChannelFunction<P = any, K = any> = (
|
|
|
79
80
|
error: (err?: any) => void
|
|
80
81
|
) => () => void
|
|
81
82
|
|
|
82
|
-
export type BasedChannelPublishFunction<P = any,
|
|
83
|
+
export type BasedChannelPublishFunction<P = any, M = any> = (
|
|
83
84
|
based: BasedFunctionClient,
|
|
84
85
|
payload: P,
|
|
85
|
-
message:
|
|
86
|
+
message: M,
|
|
86
87
|
id: number,
|
|
87
88
|
ctx: Context
|
|
88
89
|
) => void
|
|
89
90
|
|
|
90
|
-
export type ChannelMessageFunction<
|
|
91
|
+
export type ChannelMessageFunction<M = any> = (message: M) => void
|
|
91
92
|
|
|
92
93
|
export type ChannelMessageFunctionInternal<K = any> = (
|
|
93
94
|
message: K,
|
|
@@ -95,3 +96,94 @@ export type ChannelMessageFunctionInternal<K = any> = (
|
|
|
95
96
|
) => void
|
|
96
97
|
|
|
97
98
|
export type UninstallFunction = () => Promise<void>
|
|
99
|
+
|
|
100
|
+
type FunctionConfigShared = {
|
|
101
|
+
/** Function name */
|
|
102
|
+
name: string
|
|
103
|
+
/** In addition to the name, a function can have a custom path for HTTP requests.
|
|
104
|
+
* For example: `path: 'my/custom/path'` will result in the function being
|
|
105
|
+
* available with a request to `env.based.io/my/custom/path`
|
|
106
|
+
*/
|
|
107
|
+
path?: string
|
|
108
|
+
/** In bytes. `-1` indicates no size limit */
|
|
109
|
+
maxPayloadSize?: number
|
|
110
|
+
/** Cost in tokens for this function call. */
|
|
111
|
+
rateLimitTokens?: number
|
|
112
|
+
/** A function marked as `public` will skip the call to authorize. */
|
|
113
|
+
public?: boolean
|
|
114
|
+
/** Array of headers that this function expects to receive. */
|
|
115
|
+
headers?: string[]
|
|
116
|
+
/** A function marked as `internalOnly` will only be accessible from other server side functions,
|
|
117
|
+
* and not through the public internet.
|
|
118
|
+
*/
|
|
119
|
+
internalOnly?: boolean
|
|
120
|
+
/** Can hold extra information about a spec */
|
|
121
|
+
data?: any
|
|
122
|
+
/** Unistall after idle, in ms */
|
|
123
|
+
uninstallAfterIdleTime?: number
|
|
124
|
+
/** Hook that fires on uninstall of the function e.g. to clean up database connections */
|
|
125
|
+
uninstall?: UninstallFunction
|
|
126
|
+
/** Specific authorize for this function */
|
|
127
|
+
authorize?: Authorize
|
|
128
|
+
}
|
|
129
|
+
|
|
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
|
+
}
|