@based/functions 3.0.1 → 3.1.0-alpha

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/src/functions.ts DELETED
@@ -1,365 +0,0 @@
1
- import type { Required } from 'utility-types'
2
- import { Context, HttpSession } from './context.js'
3
- import { BasedFunctionClient } from './client.js'
4
- import { BasedDataStream } from './stream.js'
5
- import { Authorize } from './auth.js'
6
-
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
16
-
17
- export type ObserveErrorListener = (err: any) => void
18
-
19
- export type HttpHeaders = {
20
- [header: string]: number | string | (string | number)[]
21
- }
22
-
23
- export type SendHttpResponse = (
24
- responseData: any,
25
- headers?: HttpHeaders,
26
- status?: string | number
27
- ) => void
28
-
29
- export type HttpResponse<P = any, K = any> = (
30
- based: BasedFunctionClient,
31
- payload: P,
32
- responseData: K,
33
- send: SendHttpResponse,
34
- ctx: Context<HttpSession>
35
- ) => Promise<void>
36
-
37
- export type BasedFunction<P = any, K = any> = (
38
- based: BasedFunctionClient,
39
- payload: P,
40
- ctx: Context
41
- ) => Promise<K>
42
-
43
- export type BasedAppFunction = (
44
- based: BasedFunctionClient,
45
- assets: {
46
- css: {
47
- text: Promise<string>
48
- url: string
49
- }
50
- js: {
51
- text: Promise<string>
52
- url: string
53
- }
54
- favicon: {
55
- url: string
56
- content: string
57
- path: string
58
- }
59
- },
60
- ctx: Context
61
- ) => Promise<string>
62
-
63
- export type StreamPayload<P = any> = {
64
- payload?: P
65
- mimeType: string
66
- size: number
67
- stream: BasedDataStream
68
- fileName?: string
69
- extension?: string
70
- }
71
-
72
- export type BasedStreamFunction<P = any, K = any> = BasedFunction<
73
- StreamPayload<P>,
74
- K
75
- >
76
-
77
- export type BasedQueryFunction<P = any, K = any> =
78
- | ((
79
- based: BasedFunctionClient,
80
- payload: P,
81
- update: ObservableUpdateFunction<K>,
82
- error: ObserveErrorListener
83
- ) => Promise<() => void>)
84
- | ((
85
- based: BasedFunctionClient,
86
- payload: P,
87
- update: ObservableUpdateFunction<K>,
88
- error: ObserveErrorListener
89
- ) => () => void)
90
-
91
- export type BasedChannelFunction<P = any, K = any> = (
92
- based: BasedFunctionClient,
93
- payload: P,
94
- id: number,
95
- update: ChannelMessageFunction<K>,
96
- error: (err?: any) => void
97
- ) => () => void
98
-
99
- export type BasedChannelPublishFunction<P = any, M = any> = (
100
- based: BasedFunctionClient,
101
- payload: P,
102
- message: M,
103
- id: number,
104
- ctx: Context
105
- ) => void
106
-
107
- export type ChannelMessageFunction<M = any> = (message: M) => void
108
-
109
- export type ChannelMessageFunctionInternal<K = any> = (
110
- message: K,
111
- err?: any
112
- ) => void
113
-
114
- export type BasedJobFunction =
115
- | ((based: BasedFunctionClient) => Promise<() => void>)
116
- | ((based: BasedFunctionClient) => () => void)
117
-
118
- export type UninstallFunction = () => Promise<void>
119
-
120
- // ------------ Config -------------------
121
- type FunctionConfigShared = {
122
- /** Function name */
123
- name?: string
124
- /** In addition to the name, a function can have a custom path for HTTP requests.
125
- * For example: `path: 'my/custom/path'` will result in the function being
126
- * available with a request to `env.based.io/my/custom/path`
127
- */
128
- path?: string
129
- /** In bytes. `-1` indicates no size limit */
130
- maxPayloadSize?: number
131
- /** Cost in tokens for this function call. */
132
- rateLimitTokens?: number
133
- /** A function marked as `public` will skip the call to authorize. */
134
- public?: boolean
135
- /** Array of headers that this function expects to receive. */
136
- headers?: string[]
137
- /** A function marked as `internalOnly` will only be accessible from other server side functions,
138
- * and not through the public internet.
139
- */
140
- internalOnly?: boolean
141
- /** Can hold extra information about a spec */
142
- data?: any
143
- /** Unistall after idle, in ms -1 indicates endless */
144
- uninstallAfterIdleTime?: number
145
- /** Hook that fires on uninstall of the function e.g. to clean up database connections */
146
- uninstall?: UninstallFunction
147
- /** Specific authorize for this function */
148
- authorize?: Authorize
149
- /** Relay allows functions to relay traffic to another `@based/server`
150
- `Currently not supported for `stream`
151
-
152
- ```js
153
- new BasedServer({
154
- clients: { events: BasedClient },
155
- functions: {
156
- specs:
157
- somethingToRelay: {
158
- relay: { client: 'events', target: 'hello' },
159
- type: 'function'
160
- })
161
- }
162
- }
163
- })
164
- ```
165
- */
166
- relay?: {
167
- client: string
168
- target?: string
169
- }
170
- /** Function version */
171
- version?: number
172
- /** Used inernaly to check if a function is ready to uninstall */
173
- timeoutCounter?: number
174
- }
175
-
176
- type FunctionConfigSharedComplete = Required<
177
- FunctionConfigShared,
178
- 'maxPayloadSize' | 'rateLimitTokens' | 'version' | 'name'
179
- >
180
-
181
- export type BasedFunctionTypes =
182
- | 'channel'
183
- | 'query'
184
- | 'function'
185
- | 'stream'
186
- | 'app'
187
- | 'job'
188
-
189
- type BasedChannelFunctionConfig = {
190
- /** Function type `channel, function, query, stream, authorize` */
191
- type: 'channel'
192
- /** Channel subscriber
193
-
194
- ```js
195
- const subscribe = (based, payload, id, update) => {
196
- let cnt = 0
197
- const interval = setInterval(() => {
198
- update(++cnt)
199
- })
200
- return () => clearInterval(cnt)
201
- }
202
- ```
203
- */
204
- subscriber?: BasedChannelFunction
205
- /** Channel publisher
206
-
207
- ```js
208
- const publisher = (based, payload, msg, id) => {
209
- publishToChannel(id, msg)
210
- }
211
- ```
212
- */
213
- publisher?: BasedChannelPublishFunction
214
- /** Makes only the publisher public */
215
- publicPublisher?: boolean
216
- /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
217
- closeAfterIdleTime?: number
218
- /** Only for Publisher */
219
- httpResponse?: HttpResponse
220
- /** Throttle amount of outgoing messages, in milliseconds */
221
- throttle?: number
222
- }
223
-
224
- type BasedCallFunctionConfig = {
225
- /** Function type `channel, function, query, stream` */
226
- type: 'function'
227
- fn?: BasedFunction
228
- httpResponse?: HttpResponse
229
- }
230
-
231
- type BasedQueryFunctionConfig = {
232
- /** Function type `channel, function, query, stream` */
233
- type: 'query'
234
- fn?: BasedQueryFunction
235
- httpResponse?: HttpResponse
236
- /** How long should the query function remain active after all subscribers are gone, in ms */
237
- closeAfterIdleTime?: number
238
- /** Throttle amount of outgoing messages, in milliseconds */
239
- throttle?: number
240
- }
241
-
242
- type BasedStreamFunctionConfig = {
243
- /** Function type `channel, function, query, stream` */
244
- type: 'stream'
245
- fn: BasedStreamFunction
246
- }
247
-
248
- type BasedAppFunctionConfig = {
249
- type: 'app'
250
- main: string
251
- path?: string
252
- favicon?: string
253
- }
254
-
255
- type BasedJobFunctionConfig = {
256
- type: 'job'
257
- fn?: BasedFunction
258
- }
259
-
260
- export type BasedFunctionConfig<
261
- T extends BasedFunctionTypes = BasedFunctionTypes
262
- > = T extends 'channel'
263
- ? BasedChannelFunctionConfig & FunctionConfigShared
264
- : T extends 'function'
265
- ? BasedCallFunctionConfig & FunctionConfigShared
266
- : T extends 'query'
267
- ? BasedQueryFunctionConfig & FunctionConfigShared
268
- : T extends 'stream'
269
- ? BasedStreamFunctionConfig & FunctionConfigShared
270
- : T extends 'job'
271
- ? BasedJobFunctionConfig & FunctionConfigShared
272
- : T extends 'app'
273
- ? BasedAppFunctionConfig & FunctionConfigShared
274
- :
275
- | (BasedChannelFunctionConfig & FunctionConfigShared)
276
- | (BasedCallFunctionConfig & FunctionConfigShared)
277
- | (BasedQueryFunctionConfig & FunctionConfigShared)
278
- | (BasedStreamFunctionConfig & FunctionConfigShared)
279
- | (BasedJobFunctionConfig & FunctionConfigShared)
280
- | (BasedAppFunctionConfig & FunctionConfigShared)
281
-
282
- export type BasedFunctionConfigComplete<
283
- T extends BasedFunctionTypes = BasedFunctionTypes
284
- > = T extends 'channel'
285
- ? BasedChannelFunctionConfig & FunctionConfigSharedComplete
286
- : T extends 'function'
287
- ? BasedCallFunctionConfig & FunctionConfigSharedComplete
288
- : T extends 'query'
289
- ? BasedQueryFunctionConfig & FunctionConfigSharedComplete
290
- : T extends 'stream'
291
- ? BasedStreamFunctionConfig & FunctionConfigSharedComplete
292
- : T extends 'job'
293
- ? BasedJobFunctionConfig & FunctionConfigSharedComplete
294
- : T extends 'app'
295
- ? BasedAppFunctionConfig & FunctionConfigSharedComplete
296
- :
297
- | (BasedChannelFunctionConfig & FunctionConfigSharedComplete)
298
- | (BasedCallFunctionConfig & FunctionConfigSharedComplete)
299
- | (BasedQueryFunctionConfig & FunctionConfigSharedComplete)
300
- | (BasedStreamFunctionConfig & FunctionConfigSharedComplete)
301
- | (BasedJobFunctionConfig & FunctionConfigSharedComplete)
302
- | (BasedAppFunctionConfig & FunctionConfigSharedComplete)
303
-
304
- export type BasedAuthorizeFunctionConfig = {
305
- /** Function type `authorize` */
306
- type: 'authorize'
307
- fn?: Authorize
308
- }
309
-
310
- export type BasedRoute<
311
- T extends BasedFunctionTypes = BasedFunctionTypes,
312
- R extends keyof BasedFunctionConfig = 'type' | 'name'
313
- > = Required<Partial<BasedFunctionConfig<T>>, R>
314
-
315
- export type BasedRouteComplete<
316
- T extends BasedFunctionTypes = BasedFunctionTypes
317
- > = Required<
318
- Partial<BasedFunctionConfig<T>>,
319
- 'type' | 'name' | 'maxPayloadSize' | 'rateLimitTokens'
320
- >
321
-
322
- export function isBasedRoute<T extends BasedFunctionTypes>(
323
- type: T,
324
- route: any
325
- ): route is BasedRoute<T> {
326
- return (
327
- route &&
328
- typeof route === 'object' &&
329
- route.type === type &&
330
- typeof route.name === 'string'
331
- )
332
- }
333
-
334
- export function isAnyBasedRoute(route: any): route is BasedRoute {
335
- return (
336
- route &&
337
- typeof route === 'object' &&
338
- (route.type === 'channel' ||
339
- route.type === 'query' ||
340
- route.type === 'function' ||
341
- route.type === 'stream') &&
342
- typeof route.name === 'string'
343
- )
344
- }
345
-
346
- export function isBasedFunctionConfig<T extends BasedFunctionTypes>(
347
- type: T,
348
- config: any
349
- ): config is BasedFunctionConfig<T> {
350
- return isBasedRoute(type, config)
351
- }
352
-
353
- export function isAnyBasedFunctionConfig(
354
- config: any
355
- ): config is BasedFunctionConfig {
356
- return isAnyBasedRoute(config)
357
- }
358
-
359
- export type BasedRoutes = {
360
- [name: string]: BasedRoute<BasedFunctionTypes, 'type'>
361
- }
362
-
363
- export type BasedFunctionConfigs = {
364
- [name: string]: BasedFunctionConfig<BasedFunctionTypes>
365
- }
package/src/geo.ts DELETED
@@ -1,322 +0,0 @@
1
- export type CountryCode =
2
- | 'AF'
3
- | 'AL'
4
- | 'DZ'
5
- | 'AS'
6
- | 'AD'
7
- | 'AO'
8
- | 'AI'
9
- | 'AQ'
10
- | 'AG'
11
- | 'AR'
12
- | 'AM'
13
- | 'AW'
14
- | 'AU'
15
- | 'AT'
16
- | 'AZ'
17
- | 'BS'
18
- | 'BH'
19
- | 'BD'
20
- | 'BB'
21
- | 'BY'
22
- | 'BE'
23
- | 'BZ'
24
- | 'BJ'
25
- | 'BM'
26
- | 'BT'
27
- | 'BO'
28
- | 'BQ'
29
- | 'BA'
30
- | 'BW'
31
- | 'BV'
32
- | 'BR'
33
- | 'IO'
34
- | 'BN'
35
- | 'BG'
36
- | 'BF'
37
- | 'BI'
38
- | 'KH'
39
- | 'CM'
40
- | 'CA'
41
- | 'CV'
42
- | 'KY'
43
- | 'CF'
44
- | 'TD'
45
- | 'CL'
46
- | 'CN'
47
- | 'CX'
48
- | 'CC'
49
- | 'CO'
50
- | 'KM'
51
- | 'CG'
52
- | 'CD'
53
- | 'CK'
54
- | 'CR'
55
- | 'HR'
56
- | 'CU'
57
- | 'CW'
58
- | 'CY'
59
- | 'CZ'
60
- | 'CI'
61
- | 'DK'
62
- | 'DJ'
63
- | 'DM'
64
- | 'DO'
65
- | 'EC'
66
- | 'EG'
67
- | 'SV'
68
- | 'GQ'
69
- | 'ER'
70
- | 'EE'
71
- | 'ET'
72
- | 'FK'
73
- | 'FO'
74
- | 'FJ'
75
- | 'FI'
76
- | 'FR'
77
- | 'GF'
78
- | 'PF'
79
- | 'TF'
80
- | 'GA'
81
- | 'GM'
82
- | 'GE'
83
- | 'DE'
84
- | 'GH'
85
- | 'GI'
86
- | 'GR'
87
- | 'GL'
88
- | 'GD'
89
- | 'GP'
90
- | 'GU'
91
- | 'GT'
92
- | 'GG'
93
- | 'GN'
94
- | 'GW'
95
- | 'GY'
96
- | 'HT'
97
- | 'HM'
98
- | 'VA'
99
- | 'HN'
100
- | 'HK'
101
- | 'HU'
102
- | 'IS'
103
- | 'IN'
104
- | 'ID'
105
- | 'IR'
106
- | 'IQ'
107
- | 'IE'
108
- | 'IM'
109
- | 'IL'
110
- | 'IT'
111
- | 'JM'
112
- | 'JP'
113
- | 'JE'
114
- | 'JO'
115
- | 'KZ'
116
- | 'KE'
117
- | 'KI'
118
- | 'KP'
119
- | 'KR'
120
- | 'KW'
121
- | 'KG'
122
- | 'LA'
123
- | 'LV'
124
- | 'LB'
125
- | 'LS'
126
- | 'LR'
127
- | 'LY'
128
- | 'LI'
129
- | '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 | 'unknown'
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 DELETED
@@ -1,8 +0,0 @@
1
- export * from './context.js'
2
- export * from './auth.js'
3
- export * from './client.js'
4
- export * from './functions.js'
5
- export * from './query.js'
6
- export * from './stream.js'
7
- export * from './channel.js'
8
- export * from './geo.js'
package/src/query.ts DELETED
@@ -1,14 +0,0 @@
1
- import { ObservableUpdateFunction, ObserveErrorListener } from './functions.js'
2
-
3
- export abstract class BasedQuery<K = any> {
4
- abstract subscribe(
5
- onData: ObservableUpdateFunction<K>,
6
- onError?: ObserveErrorListener
7
- ): () => void
8
-
9
- abstract getWhen(
10
- condition: (data: K, checksum: number) => boolean
11
- ): Promise<any>
12
-
13
- abstract get(): Promise<K>
14
- }