@based/functions 3.0.2 → 3.1.1-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/dist/auth.d.ts CHANGED
@@ -8,6 +8,7 @@ export type AuthState = {
8
8
  error?: string;
9
9
  persistent?: boolean;
10
10
  type?: string;
11
+ t?: 0 | 1;
11
12
  };
12
13
  export type Authorize = (based: BasedFunctionClient, context: Context<HttpSession | WebSocketSession>, name: string, // name as generic dope
13
14
  payload?: any) => Promise<boolean>;
package/dist/context.d.ts CHANGED
@@ -11,6 +11,7 @@ export type WebSocketSession = {
11
11
  id: number;
12
12
  method: string;
13
13
  authState: AuthState;
14
+ type: '0' | '1';
14
15
  origin: string;
15
16
  obs: Set<number>;
16
17
  unauthorizedObs?: Set<{
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AA6GA,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,GAAqB,EACQ,EAAE;IAC/B,IAAI,KAAK,IAAI,GAAG,EAAE,OAAO,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,GAAqB,EAC2B,EAAE;IAClD,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QAC5E,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO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,IAAI,OAAO,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;QAChC,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,OAA4B,EACC,EAAE;IAC/B,IAAI,OAAO,IAAI,MAAM,IAAI,OAAO,EAAE,CAAC;QACjC,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
@@ -3,6 +3,7 @@ import { Context, HttpSession } from './context.js';
3
3
  import { BasedFunctionClient } from './client.js';
4
4
  import { BasedDataStream } from './stream.js';
5
5
  import { Authorize } from './auth.js';
6
+ import type { BuildOptions } from 'esbuild';
6
7
  export type ObservableUpdateFunction<K = any> = (data: K, checksum?: number, err?: any, cache?: Uint8Array, diff?: any, fromChecksum?: number, isDeflate?: boolean) => void;
7
8
  export type ObserveErrorListener = (err: any) => void;
8
9
  export type HttpHeaders = {
@@ -160,6 +161,7 @@ type BasedAppFunctionConfig = {
160
161
  main: string;
161
162
  path?: string;
162
163
  favicon?: string;
164
+ plugins?: BuildOptions['plugins'];
163
165
  };
164
166
  type BasedJobFunctionConfig = {
165
167
  type: 'job';
@@ -1 +1 @@
1
- {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAmUA,MAAM,UAAU,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;AAED,MAAM,UAAU,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;AAED,MAAM,UAAU,qBAAqB,CACnC,IAAO,EACP,MAAW;IAEX,OAAO,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,MAAW;IAEX,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;AAChC,CAAC"}
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAqUA,MAAM,UAAU,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;AAED,MAAM,UAAU,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;AAED,MAAM,UAAU,qBAAqB,CACnC,IAAO,EACP,MAAW;IAEX,OAAO,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,MAAW;IAEX,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;AAChC,CAAC"}
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@based/functions",
3
+ "version": "3.1.1-alpha",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "files":[
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "copy:uws": "cp src/uws.d.ts dist/uws.d.ts",
12
+ "copy:client": "cp ./dist/client.d.ts ./dist/client_original.d.ts",
13
+ "copy:package": "cp ./package.json ./dist/package.json",
14
+ "clean:ts:before": "rm -rf ./dist ./tsconfig.tsbuildinfo || true",
15
+ "clean:ts:after": "rm ./dist/tsconfig.tsbuildinfo || true",
16
+ "build": "npm run clean:ts:before && npx tsc -b && npm run copy:uws && npm run copy:client && npm run copy:package && npm run clean:ts:after",
17
+ "watch": "npx tsc --watch",
18
+ "clean": "rimraf {.turbo,dist,node_modules}"
19
+ },
20
+ "sideEffects": false,
21
+ "dependencies": {
22
+ "@saulx/utils": "^5.0.0",
23
+ "esbuild": "^0.24.2",
24
+ "utility-types": "^3.10.0"
25
+ },
26
+ "devDependencies": {
27
+ "rimraf": "^3.0.2",
28
+ "ts-node": "10.9.1",
29
+ "typescript": "^4.3.5"
30
+ }
31
+ }
package/dist/stream.d.ts CHANGED
@@ -1,7 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
- /// <reference types="node" resolution-mode="require"/>
4
- /// <reference types="node" resolution-mode="require"/>
5
1
  import { Duplex, Readable } from "node:stream";
6
2
  import util from "node:util";
7
3
  export declare class BasedDataStream extends Duplex {
package/package.json CHANGED
@@ -1,22 +1,31 @@
1
1
  {
2
2
  "name": "@based/functions",
3
- "version": "3.0.2",
3
+ "version": "3.1.1-alpha",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
- "main": "dist/index.js",
6
+ "main": "index.js",
7
+ "files":[
8
+ "dist"
9
+ ],
7
10
  "scripts": {
8
- "build": "npx tsc && cp src/uws.d.ts dist/uws.d.ts && cp ./dist/client.d.ts ./dist/client_original.d.ts",
11
+ "copy:uws": "cp src/uws.d.ts dist/uws.d.ts",
12
+ "copy:client": "cp ./dist/client.d.ts ./dist/client_original.d.ts",
13
+ "copy:package": "cp ./package.json ./dist/package.json",
14
+ "clean:ts:before": "rm -rf ./dist ./tsconfig.tsbuildinfo || true",
15
+ "clean:ts:after": "rm ./dist/tsconfig.tsbuildinfo || true",
16
+ "build": "npm run clean:ts:before && npx tsc -b && npm run copy:uws && npm run copy:client && npm run copy:package && npm run clean:ts:after",
9
17
  "watch": "npx tsc --watch",
10
18
  "clean": "rimraf {.turbo,dist,node_modules}"
11
19
  },
12
20
  "sideEffects": false,
13
21
  "dependencies": {
14
- "utility-types": "^3.10.0",
15
- "@saulx/utils": "^5.0.0"
22
+ "@saulx/utils": "^5.0.0",
23
+ "esbuild": "^0.24.2",
24
+ "utility-types": "^3.10.0"
16
25
  },
17
26
  "devDependencies": {
27
+ "rimraf": "^3.0.2",
18
28
  "ts-node": "10.9.1",
19
- "typescript": "^4.3.5",
20
- "rimraf": "^3.0.2"
29
+ "typescript": "^4.3.5"
21
30
  }
22
31
  }
package/src/auth.ts DELETED
@@ -1,34 +0,0 @@
1
- import { BasedFunctionClient } from './client.js'
2
- import { HttpRequest } from './uws.js'
3
- import { Context, WebSocketSession, HttpSession } from './context.js'
4
-
5
- export type AuthState = {
6
- token?: string
7
- userId?: string
8
- refreshToken?: string
9
- error?: string
10
- persistent?: boolean
11
- type?: string
12
- }
13
-
14
- export type Authorize = (
15
- based: BasedFunctionClient,
16
- context: Context<HttpSession | WebSocketSession>,
17
- name: string, // name as generic dope
18
- payload?: any
19
- ) => Promise<boolean>
20
-
21
- // True - its the same all good
22
- // AuthState - new auth state send it
23
- // if error send error state (and reject)
24
- export type VerifyAuthState = (
25
- based: BasedFunctionClient,
26
- context: Context<HttpSession | WebSocketSession>,
27
- authState: AuthState
28
- ) => Promise<true | AuthState>
29
-
30
- export type AuthorizeConnection = (
31
- based: BasedFunctionClient,
32
- req: HttpRequest,
33
- ip: string
34
- ) => Promise<boolean>
package/src/channel.ts DELETED
@@ -1,10 +0,0 @@
1
- import { ChannelMessageFunction } from './functions.js'
2
-
3
- export abstract class BasedChannel<K = any> {
4
- abstract subscribe(
5
- onMessage: ChannelMessageFunction<K>,
6
- onError?: (err: any) => void
7
- ): () => void
8
-
9
- abstract publish(message: K): void
10
- }
package/src/client.ts DELETED
@@ -1,35 +0,0 @@
1
- import { AuthState } from './auth.js'
2
- import { BasedChannel } from './channel.js'
3
- import { Context } from './context.js'
4
- import { BasedQuery } from './query.js'
5
- import { StreamFunctionOpts } from './stream.js'
6
- import { Geo } from './geo.js'
7
-
8
- export abstract class BasedFunctionClient {
9
- server: any
10
-
11
- db: any
12
-
13
- abstract call(name: string, payload?: any, ctx?: Context): Promise<any>
14
-
15
- abstract query(name: string, payload?: any): BasedQuery
16
-
17
- abstract channel(name: string, payload?: any): BasedChannel
18
-
19
- abstract stream(
20
- name: string,
21
- payload: StreamFunctionOpts,
22
- ctx?: Context
23
- ): Promise<any>
24
-
25
- abstract sendAuthState(ctx: Context, authState: AuthState): void
26
-
27
- abstract geo(ctx: Context): Promise<Geo>
28
-
29
- abstract renewAuthState(
30
- ctx: Context,
31
- authState?: AuthState
32
- ): Promise<AuthState>
33
- }
34
-
35
- export type QueryMap = { [key: string]: { payload: any; result: any } }
package/src/context.ts DELETED
@@ -1,153 +0,0 @@
1
- import { parseQuery } from '@saulx/utils'
2
- import { AuthState } from './auth.js'
3
- import { WebSocket, HttpRequest, HttpResponse } from './uws.js'
4
- import { BasedFunctionClient } from './client.js'
5
- import { StreamPayload } from './functions.js'
6
-
7
- export type WebSocketSession = {
8
- // State can be used for anything - for us the based class instance
9
- state?: any
10
- query: string
11
- ua: string
12
- ip: string
13
- id: number // client-id
14
- method: string
15
- authState: AuthState
16
- origin: string
17
- obs: Set<number>
18
- unauthorizedObs?: Set<{
19
- id: number
20
- checksum: number
21
- name: string
22
- payload: any
23
- }>
24
- streams?: { [reqId: string]: StreamPayload }
25
- headers: { [key: string]: string }
26
- unauthorizedChannels?: Set<{
27
- id: number
28
- name: string
29
- payload: any
30
- }>
31
- // Optimization so we dont need to keep track of websockets outside of uws
32
- c?: Context<WebSocketSession>
33
- ws?: BasedWebSocket
34
- }
35
-
36
- export type BasedWebSocket = WebSocket<WebSocketSession>
37
-
38
- export type HttpSession = {
39
- // State can be used for anything - for us the based class instance
40
- state?: any
41
- res: HttpResponse
42
- url: string
43
- origin: string
44
- req: HttpRequest
45
- query?: string
46
- parsedQuery?: ReturnType<typeof parseQuery>
47
- ua: string
48
- ip: string
49
- id: number // client-id
50
- authState: AuthState
51
- method: string
52
- rawBody?: string
53
- headers: {
54
- 'content-length'?: number
55
- 'content-type'?: string
56
- 'content-encoding'?: string
57
- encoding?: string
58
- } & { [key: string]: string }
59
- }
60
-
61
- export type InternalSessionObservable = {
62
- id: number
63
- name: string
64
- headers: { [key: string]: string }
65
- type: 'query'
66
- }
67
-
68
- export type InternalSessionChannel = {
69
- id: number
70
- name: string
71
- headers: { [key: string]: string }
72
- type: 'channel'
73
- }
74
-
75
- export type InternalSessionClient = {
76
- client: BasedFunctionClient
77
- headers: { [key: string]: string }
78
- type: 'client'
79
- }
80
-
81
- // Internal session for internal functions
82
- export type InternalSession =
83
- | InternalSessionClient
84
- | InternalSessionObservable
85
- | InternalSessionChannel
86
-
87
- // used for minimal security errors (e.g. rate limit)
88
- export type MinimalExternalSession = {
89
- ua: string
90
- ip: string
91
- headers: { [key: string]: string }
92
- }
93
-
94
- export type Session = (
95
- | WebSocketSession
96
- | HttpSession
97
- | InternalSession
98
- | MinimalExternalSession
99
- ) & {
100
- origin?: string
101
-
102
- /** Only available in Ws and Http contexts */
103
- authState?: AuthState
104
- }
105
-
106
- export type Context<S extends Session = Session> = {
107
- session?: S
108
- }
109
-
110
- export const isHttpContext = (
111
- ctx: Context<Session>
112
- ): ctx is Context<HttpSession> => {
113
- if ('res' in ctx?.session) {
114
- return true
115
- }
116
- return false
117
- }
118
-
119
- export const isWsContext = (
120
- ctx: Context<Session>
121
- ): ctx is Context<WebSocketSession> => {
122
- if (ctx.session && isWsSession(ctx.session)) {
123
- return true
124
- }
125
- return false
126
- }
127
-
128
- export const isClientContext = (
129
- ctx: Context<Session>
130
- ): ctx is Context<WebSocketSession | HttpSession> => {
131
- if (ctx.session && (isWsSession(ctx.session) || isHttpSession(ctx.session))) {
132
- return true
133
- }
134
- return false
135
- }
136
-
137
- export const isHttpSession = (
138
- session: Session | undefined
139
- ): session is HttpSession => {
140
- if (session && 'res' in session) {
141
- return true
142
- }
143
- return false
144
- }
145
-
146
- export const isWsSession = (
147
- session: Session | undefined
148
- ): session is WebSocketSession => {
149
- if (session && 'send' in session) {
150
- return true
151
- }
152
- return false
153
- }