@based/functions 1.1.1 → 1.2.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/auth.d.ts +1 -1
- package/dist/client.d.ts +3 -0
- package/dist/client.js +0 -1
- package/dist/client.js.map +1 -1
- package/dist/context.d.ts +15 -0
- package/dist/context.js.map +1 -1
- package/dist/functions.d.ts +88 -2
- package/dist/geo.d.ts +35 -0
- package/dist/geo.js +3 -0
- package/dist/geo.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/auth.ts +2 -1
- package/src/client.ts +5 -1
- package/src/context.ts +5 -0
- package/src/functions.ts +95 -3
- package/src/geo.ts +322 -0
- package/src/index.ts +1 -0
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/client.d.ts
CHANGED
|
@@ -3,12 +3,15 @@ import { BasedChannel } from './channel';
|
|
|
3
3
|
import { Context } from './context';
|
|
4
4
|
import { BasedQuery } from './query';
|
|
5
5
|
import { StreamFunctionOpts } from './stream';
|
|
6
|
+
import { Geo } from './geo';
|
|
6
7
|
export declare abstract class BasedFunctionClient {
|
|
7
8
|
server: any;
|
|
9
|
+
db: any;
|
|
8
10
|
abstract call(name: string, payload?: any, ctx?: Context): Promise<any>;
|
|
9
11
|
abstract query(name: string, payload?: any): BasedQuery;
|
|
10
12
|
abstract channel(name: string, payload?: any): BasedChannel;
|
|
11
13
|
abstract stream(name: string, payload: StreamFunctionOpts, ctx?: Context): Promise<any>;
|
|
12
14
|
abstract sendAuthState(ctx: Context, authState: AuthState): void;
|
|
15
|
+
abstract geo(ctx: Context): Promise<Geo>;
|
|
13
16
|
abstract renewAuthState(ctx: Context, authState?: AuthState): Promise<AuthState>;
|
|
14
17
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BasedFunctionClient = void 0;
|
|
4
|
-
// TODO: this is the place where we will add extra specifications
|
|
5
4
|
class BasedFunctionClient {
|
|
6
5
|
}
|
|
7
6
|
exports.BasedFunctionClient = BasedFunctionClient;
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;AAOA,MAAsB,mBAAmB;CAyBxC;AAzBD,kDAyBC"}
|
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/dist/geo.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type CountryCode = 'AF' | 'AL' | 'DZ' | 'AS' | 'AD' | 'AO' | 'AI' | 'AQ' | 'AG' | 'AR' | 'AM' | 'AW' | 'AU' | 'AT' | 'AZ' | 'BS' | 'BH' | 'BD' | 'BB' | 'BY' | 'BE' | 'BZ' | 'BJ' | 'BM' | 'BT' | 'BO' | 'BQ' | 'BA' | 'BW' | 'BV' | 'BR' | 'IO' | 'BN' | 'BG' | 'BF' | 'BI' | 'KH' | 'CM' | 'CA' | 'CV' | 'KY' | 'CF' | 'TD' | 'CL' | 'CN' | 'CX' | 'CC' | 'CO' | 'KM' | 'CG' | 'CD' | 'CK' | 'CR' | 'HR' | 'CU' | 'CW' | 'CY' | 'CZ' | 'CI' | 'DK' | 'DJ' | 'DM' | 'DO' | 'EC' | 'EG' | 'SV' | 'GQ' | 'ER' | 'EE' | 'ET' | 'FK' | 'FO' | 'FJ' | 'FI' | 'FR' | 'GF' | 'PF' | 'TF' | 'GA' | 'GM' | 'GE' | 'DE' | 'GH' | 'GI' | 'GR' | 'GL' | 'GD' | 'GP' | 'GU' | 'GT' | 'GG' | 'GN' | 'GW' | 'GY' | 'HT' | 'HM' | 'VA' | 'HN' | 'HK' | 'HU' | 'IS' | 'IN' | 'ID' | 'IR' | 'IQ' | 'IE' | 'IM' | 'IL' | 'IT' | 'JM' | 'JP' | 'JE' | 'JO' | 'KZ' | 'KE' | 'KI' | 'KP' | 'KR' | 'KW' | 'KG' | 'LA' | 'LV' | 'LB' | 'LS' | 'LR' | 'LY' | 'LI' | 'LT' | 'LU' | 'MO' | 'MK' | 'MG' | 'MW' | 'MY' | 'MV' | 'ML' | 'MT' | 'MH' | 'MQ' | 'MR' | 'MU' | 'YT' | 'MX' | 'FM' | 'MD' | 'MC' | 'MN' | 'ME' | 'MS' | 'MA' | 'MZ' | 'MM' | 'NA' | 'NR' | 'NP' | 'NL' | 'NC' | 'NZ' | 'NI' | 'NE' | 'NG' | 'NU' | 'NF' | 'MP' | 'NO' | 'OM' | 'PK' | 'PW' | 'PS' | 'PA' | 'PG' | 'PY' | 'PE' | 'PH' | 'PN' | 'PL' | 'PT' | 'PR' | 'QA' | 'RO' | 'RU' | 'RW' | 'RE' | 'BL' | 'SH' | 'KN' | 'LC' | 'MF' | 'PM' | 'VC' | 'WS' | 'SM' | 'ST' | 'SA' | 'SN' | 'RS' | 'SC' | 'SL' | 'SG' | 'SX' | 'SK' | 'SI' | 'SB' | 'SO' | 'ZA' | 'GS' | 'SS' | 'ES' | 'LK' | 'SD' | 'SR' | 'SJ' | 'SZ' | 'SE' | 'CH' | 'SY' | 'TW' | 'TJ' | 'TZ' | 'TH' | 'TL' | 'TG' | 'TK' | 'TO' | 'TT' | 'TN' | 'TR' | 'TM' | 'TC' | 'TV' | 'UG' | 'UA' | 'AE' | 'GB' | 'US' | 'UM' | 'UY' | 'UZ' | 'VU' | 'VE' | 'VN' | 'VG' | 'VI' | 'WF' | 'EH' | 'YE' | 'ZM' | 'ZW' | 'AX';
|
|
2
|
+
type Char = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
3
|
+
export type GeoRegionISO = `${Char}${Char}` | `${Char}${Char}${Char}`;
|
|
4
|
+
export type Geo = {
|
|
5
|
+
country: CountryCode;
|
|
6
|
+
/**
|
|
7
|
+
Ip formatted as ipv6
|
|
8
|
+
|
|
9
|
+
e.g. `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
|
|
10
|
+
*/
|
|
11
|
+
ip: string;
|
|
12
|
+
/**
|
|
13
|
+
Region subdivisions
|
|
14
|
+
|
|
15
|
+
e.g. `[{ iso: 'NH', name: 'north-holland' }]`
|
|
16
|
+
*/
|
|
17
|
+
regions: {
|
|
18
|
+
iso: GeoRegionISO;
|
|
19
|
+
name?: string;
|
|
20
|
+
}[];
|
|
21
|
+
lat: number;
|
|
22
|
+
long: number;
|
|
23
|
+
/**
|
|
24
|
+
Accuracy Radius (km)
|
|
25
|
+
*/
|
|
26
|
+
accuracy: number;
|
|
27
|
+
/**
|
|
28
|
+
City name
|
|
29
|
+
|
|
30
|
+
e.g. `Amsterdam`
|
|
31
|
+
*/
|
|
32
|
+
city?: string;
|
|
33
|
+
postalCode?: string;
|
|
34
|
+
};
|
|
35
|
+
export {};
|
package/dist/geo.js
ADDED
package/dist/geo.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geo.js","sourceRoot":"","sources":["../src/geo.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -21,4 +21,5 @@ __exportStar(require("./functions"), exports);
|
|
|
21
21
|
__exportStar(require("./query"), exports);
|
|
22
22
|
__exportStar(require("./stream"), exports);
|
|
23
23
|
__exportStar(require("./channel"), exports);
|
|
24
|
+
__exportStar(require("./geo"), exports);
|
|
24
25
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,yCAAsB;AACtB,2CAAwB;AACxB,8CAA2B;AAC3B,0CAAuB;AACvB,2CAAwB;AACxB,4CAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,yCAAsB;AACtB,2CAAwB;AACxB,8CAA2B;AAC3B,0CAAuB;AACvB,2CAAwB;AACxB,4CAAyB;AACzB,wCAAqB"}
|
package/package.json
CHANGED
package/src/auth.ts
CHANGED
package/src/client.ts
CHANGED
|
@@ -3,11 +3,13 @@ import { BasedChannel } from './channel'
|
|
|
3
3
|
import { Context } from './context'
|
|
4
4
|
import { BasedQuery } from './query'
|
|
5
5
|
import { StreamFunctionOpts } from './stream'
|
|
6
|
+
import { Geo } from './geo'
|
|
6
7
|
|
|
7
|
-
// TODO: this is the place where we will add extra specifications
|
|
8
8
|
export abstract class BasedFunctionClient {
|
|
9
9
|
server: any
|
|
10
10
|
|
|
11
|
+
db: any
|
|
12
|
+
|
|
11
13
|
abstract call(name: string, payload?: any, ctx?: Context): Promise<any>
|
|
12
14
|
|
|
13
15
|
abstract query(name: string, payload?: any): BasedQuery
|
|
@@ -22,6 +24,8 @@ export abstract class BasedFunctionClient {
|
|
|
22
24
|
|
|
23
25
|
abstract sendAuthState(ctx: Context, authState: AuthState): void
|
|
24
26
|
|
|
27
|
+
abstract geo(ctx: Context): Promise<Geo>
|
|
28
|
+
|
|
25
29
|
abstract renewAuthState(
|
|
26
30
|
ctx: Context,
|
|
27
31
|
authState?: AuthState
|
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
|
+
}
|
package/src/geo.ts
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
export type CountryCode =
|
|
2
|
+
| 'AF'
|
|
3
|
+
| 'AL'
|
|
4
|
+
| 'DZ'
|
|
5
|
+
| 'AS'
|
|
6
|
+
| 'AD'
|
|
7
|
+
| 'AO'
|
|
8
|
+
| 'AI'
|
|
9
|
+
| 'AQ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LT'
|
|
130
|
+
| 'LU'
|
|
131
|
+
| 'MO'
|
|
132
|
+
| 'MK'
|
|
133
|
+
| 'MG'
|
|
134
|
+
| 'MW'
|
|
135
|
+
| 'MY'
|
|
136
|
+
| 'MV'
|
|
137
|
+
| 'ML'
|
|
138
|
+
| 'MT'
|
|
139
|
+
| 'MH'
|
|
140
|
+
| 'MQ'
|
|
141
|
+
| 'MR'
|
|
142
|
+
| 'MU'
|
|
143
|
+
| 'YT'
|
|
144
|
+
| 'MX'
|
|
145
|
+
| 'FM'
|
|
146
|
+
| 'MD'
|
|
147
|
+
| 'MC'
|
|
148
|
+
| 'MN'
|
|
149
|
+
| 'ME'
|
|
150
|
+
| 'MS'
|
|
151
|
+
| 'MA'
|
|
152
|
+
| 'MZ'
|
|
153
|
+
| 'MM'
|
|
154
|
+
| 'NA'
|
|
155
|
+
| 'NR'
|
|
156
|
+
| 'NP'
|
|
157
|
+
| 'NL'
|
|
158
|
+
| 'NC'
|
|
159
|
+
| 'NZ'
|
|
160
|
+
| 'NI'
|
|
161
|
+
| 'NE'
|
|
162
|
+
| 'NG'
|
|
163
|
+
| 'NU'
|
|
164
|
+
| 'NF'
|
|
165
|
+
| 'MP'
|
|
166
|
+
| 'NO'
|
|
167
|
+
| 'OM'
|
|
168
|
+
| 'PK'
|
|
169
|
+
| 'PW'
|
|
170
|
+
| 'PS'
|
|
171
|
+
| 'PA'
|
|
172
|
+
| 'PG'
|
|
173
|
+
| 'PY'
|
|
174
|
+
| 'PE'
|
|
175
|
+
| 'PH'
|
|
176
|
+
| 'PN'
|
|
177
|
+
| 'PL'
|
|
178
|
+
| 'PT'
|
|
179
|
+
| 'PR'
|
|
180
|
+
| 'QA'
|
|
181
|
+
| 'RO'
|
|
182
|
+
| 'RU'
|
|
183
|
+
| 'RW'
|
|
184
|
+
| 'RE'
|
|
185
|
+
| 'BL'
|
|
186
|
+
| 'SH'
|
|
187
|
+
| 'KN'
|
|
188
|
+
| 'LC'
|
|
189
|
+
| 'MF'
|
|
190
|
+
| 'PM'
|
|
191
|
+
| 'VC'
|
|
192
|
+
| 'WS'
|
|
193
|
+
| 'SM'
|
|
194
|
+
| 'ST'
|
|
195
|
+
| 'SA'
|
|
196
|
+
| 'SN'
|
|
197
|
+
| 'RS'
|
|
198
|
+
| 'SC'
|
|
199
|
+
| 'SL'
|
|
200
|
+
| 'SG'
|
|
201
|
+
| 'SX'
|
|
202
|
+
| 'SK'
|
|
203
|
+
| 'SI'
|
|
204
|
+
| 'SB'
|
|
205
|
+
| 'SO'
|
|
206
|
+
| 'ZA'
|
|
207
|
+
| 'GS'
|
|
208
|
+
| 'SS'
|
|
209
|
+
| 'ES'
|
|
210
|
+
| 'LK'
|
|
211
|
+
| 'SD'
|
|
212
|
+
| 'SR'
|
|
213
|
+
| 'SJ'
|
|
214
|
+
| 'SZ'
|
|
215
|
+
| 'SE'
|
|
216
|
+
| 'CH'
|
|
217
|
+
| 'SY'
|
|
218
|
+
| 'TW'
|
|
219
|
+
| 'TJ'
|
|
220
|
+
| 'TZ'
|
|
221
|
+
| 'TH'
|
|
222
|
+
| 'TL'
|
|
223
|
+
| 'TG'
|
|
224
|
+
| 'TK'
|
|
225
|
+
| 'TO'
|
|
226
|
+
| 'TT'
|
|
227
|
+
| 'TN'
|
|
228
|
+
| 'TR'
|
|
229
|
+
| 'TM'
|
|
230
|
+
| 'TC'
|
|
231
|
+
| 'TV'
|
|
232
|
+
| 'UG'
|
|
233
|
+
| 'UA'
|
|
234
|
+
| 'AE'
|
|
235
|
+
| 'GB'
|
|
236
|
+
| 'US'
|
|
237
|
+
| 'UM'
|
|
238
|
+
| 'UY'
|
|
239
|
+
| 'UZ'
|
|
240
|
+
| 'VU'
|
|
241
|
+
| 'VE'
|
|
242
|
+
| 'VN'
|
|
243
|
+
| 'VG'
|
|
244
|
+
| 'VI'
|
|
245
|
+
| 'WF'
|
|
246
|
+
| 'EH'
|
|
247
|
+
| 'YE'
|
|
248
|
+
| 'ZM'
|
|
249
|
+
| 'ZW'
|
|
250
|
+
| 'AX'
|
|
251
|
+
|
|
252
|
+
type Char =
|
|
253
|
+
| 'A'
|
|
254
|
+
| 'B'
|
|
255
|
+
| 'C'
|
|
256
|
+
| 'D'
|
|
257
|
+
| 'E'
|
|
258
|
+
| 'F'
|
|
259
|
+
| 'G'
|
|
260
|
+
| 'H'
|
|
261
|
+
| 'I'
|
|
262
|
+
| 'J'
|
|
263
|
+
| 'K'
|
|
264
|
+
| 'L'
|
|
265
|
+
| 'M'
|
|
266
|
+
| 'N'
|
|
267
|
+
| 'O'
|
|
268
|
+
| 'P'
|
|
269
|
+
| 'Q'
|
|
270
|
+
| 'R'
|
|
271
|
+
| 'S'
|
|
272
|
+
| 'T'
|
|
273
|
+
| 'U'
|
|
274
|
+
| 'V'
|
|
275
|
+
| 'W'
|
|
276
|
+
| 'X'
|
|
277
|
+
| 'Y'
|
|
278
|
+
| 'Z'
|
|
279
|
+
| '0'
|
|
280
|
+
| '1'
|
|
281
|
+
| '2'
|
|
282
|
+
| '3'
|
|
283
|
+
| '4'
|
|
284
|
+
| '5'
|
|
285
|
+
| '6'
|
|
286
|
+
| '7'
|
|
287
|
+
| '8'
|
|
288
|
+
| '9'
|
|
289
|
+
|
|
290
|
+
export type GeoRegionISO = `${Char}${Char}` | `${Char}${Char}${Char}`
|
|
291
|
+
|
|
292
|
+
export type Geo = {
|
|
293
|
+
country: CountryCode
|
|
294
|
+
/**
|
|
295
|
+
Ip formatted as ipv6
|
|
296
|
+
|
|
297
|
+
e.g. `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
|
|
298
|
+
*/
|
|
299
|
+
ip: string
|
|
300
|
+
/**
|
|
301
|
+
Region subdivisions
|
|
302
|
+
|
|
303
|
+
e.g. `[{ iso: 'NH', name: 'north-holland' }]`
|
|
304
|
+
*/
|
|
305
|
+
regions: {
|
|
306
|
+
iso: GeoRegionISO
|
|
307
|
+
name?: string
|
|
308
|
+
}[]
|
|
309
|
+
lat: number
|
|
310
|
+
long: number
|
|
311
|
+
/**
|
|
312
|
+
Accuracy Radius (km)
|
|
313
|
+
*/
|
|
314
|
+
accuracy: number
|
|
315
|
+
/**
|
|
316
|
+
City name
|
|
317
|
+
|
|
318
|
+
e.g. `Amsterdam`
|
|
319
|
+
*/
|
|
320
|
+
city?: string
|
|
321
|
+
postalCode?: string
|
|
322
|
+
}
|
package/src/index.ts
CHANGED