@based/functions 3.0.1 → 3.0.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/dist/context.d.ts +4 -0
- package/dist/context.js.map +1 -1
- package/dist/functions.d.ts +2 -0
- package/dist/functions.js.map +1 -1
- package/dist/stream.d.ts +3 -2
- package/dist/stream.js +14 -8
- package/dist/stream.js.map +1 -1
- package/package.json +2 -2
- package/src/context.ts +2 -0
- package/src/functions.ts +2 -0
- package/src/stream.ts +58 -49
package/dist/context.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { parseQuery } from '@saulx/utils';
|
|
|
2
2
|
import { AuthState } from './auth.js';
|
|
3
3
|
import { WebSocket, HttpRequest, HttpResponse } from './uws.js';
|
|
4
4
|
import { BasedFunctionClient } from './client.js';
|
|
5
|
+
import { StreamPayload } from './functions.js';
|
|
5
6
|
export type WebSocketSession = {
|
|
6
7
|
state?: any;
|
|
7
8
|
query: string;
|
|
@@ -18,6 +19,9 @@ export type WebSocketSession = {
|
|
|
18
19
|
name: string;
|
|
19
20
|
payload: any;
|
|
20
21
|
}>;
|
|
22
|
+
streams?: {
|
|
23
|
+
[reqId: string]: StreamPayload;
|
|
24
|
+
};
|
|
21
25
|
headers: {
|
|
22
26
|
[key: string]: string;
|
|
23
27
|
};
|
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":"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,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,OAA4B,EACJ,EAAE;IAC1B,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"}
|
package/dist/functions.d.ts
CHANGED
|
@@ -33,6 +33,8 @@ export type StreamPayload<P = any> = {
|
|
|
33
33
|
stream: BasedDataStream;
|
|
34
34
|
fileName?: string;
|
|
35
35
|
extension?: string;
|
|
36
|
+
fn?: string;
|
|
37
|
+
seqId?: number;
|
|
36
38
|
};
|
|
37
39
|
export type BasedStreamFunction<P = any, K = any> = BasedFunction<StreamPayload<P>, K>;
|
|
38
40
|
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);
|
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":"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"}
|
package/dist/stream.d.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
/// <reference types="node" resolution-mode="require"/>
|
|
4
4
|
/// <reference types="node" resolution-mode="require"/>
|
|
5
|
-
import { Duplex, Readable } from
|
|
6
|
-
import util from
|
|
5
|
+
import { Duplex, Readable } from "node:stream";
|
|
6
|
+
import util from "node:util";
|
|
7
7
|
export declare class BasedDataStream extends Duplex {
|
|
8
8
|
size: number;
|
|
9
|
+
paused: boolean;
|
|
9
10
|
receivedBytes: number;
|
|
10
11
|
progessTimer: NodeJS.Timeout;
|
|
11
12
|
constructor(size: number);
|
package/dist/stream.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import { Duplex, Readable } from
|
|
2
|
-
import util from
|
|
3
|
-
// prob want to move this to based functions
|
|
1
|
+
import { Duplex, Readable } from "node:stream";
|
|
2
|
+
import util from "node:util";
|
|
4
3
|
export class BasedDataStream extends Duplex {
|
|
5
4
|
size = 0;
|
|
5
|
+
paused = false;
|
|
6
6
|
receivedBytes = 0;
|
|
7
7
|
progessTimer;
|
|
8
8
|
constructor(size) {
|
|
9
9
|
super();
|
|
10
10
|
this.size = size;
|
|
11
|
-
this.
|
|
11
|
+
this.on("pause", () => {
|
|
12
|
+
this.paused = true;
|
|
13
|
+
});
|
|
14
|
+
this.on("resume", () => {
|
|
15
|
+
this.paused = false;
|
|
16
|
+
});
|
|
17
|
+
this.emit("progress", 0);
|
|
12
18
|
}
|
|
13
19
|
_read() { }
|
|
14
20
|
_write(chunk, encoding, callback) {
|
|
@@ -17,7 +23,7 @@ export class BasedDataStream extends Duplex {
|
|
|
17
23
|
if (!this.progessTimer) {
|
|
18
24
|
this.progessTimer = setTimeout(() => {
|
|
19
25
|
const progress = this.receivedBytes / this.size;
|
|
20
|
-
this.emit(
|
|
26
|
+
this.emit("progress", progress);
|
|
21
27
|
this.progessTimer = null;
|
|
22
28
|
}, 200);
|
|
23
29
|
}
|
|
@@ -34,14 +40,14 @@ export class BasedDataStream extends Duplex {
|
|
|
34
40
|
clearTimeout(this.progessTimer);
|
|
35
41
|
this.progessTimer = null;
|
|
36
42
|
}
|
|
37
|
-
this.emit(
|
|
43
|
+
this.emit("progress", 1);
|
|
38
44
|
this.push(null);
|
|
39
45
|
}
|
|
40
46
|
[util.inspect.custom]() {
|
|
41
47
|
if (this.size) {
|
|
42
48
|
const rb = this.receivedBytes < 1000
|
|
43
|
-
? Math.round(this.receivedBytes / 1024) +
|
|
44
|
-
: Math.round(this.receivedBytes / 1024 / 1024) +
|
|
49
|
+
? Math.round(this.receivedBytes / 1024) + "kb"
|
|
50
|
+
: Math.round(this.receivedBytes / 1024 / 1024) + "mb";
|
|
45
51
|
return `[BasedStream ${~~((this.receivedBytes / this.size) *
|
|
46
52
|
100)}% ${rb}]`;
|
|
47
53
|
}
|
package/dist/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,OAAO,eAAgB,SAAQ,MAAM;IAClC,IAAI,GAAW,CAAC,CAAC;IACjB,MAAM,GAAY,KAAK,CAAC;IACxB,aAAa,GAAW,CAAC,CAAC;IAC1B,YAAY,CAAiB;IAEpC,YAAY,IAAY;QACtB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;YACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACrB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE;YACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACtB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;IAC3B,CAAC;IAEQ,KAAK,KAAI,CAAC;IAEV,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,QAA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C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,GAAG,IAAI;gBAC9C,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;YAE1D,OAAO,gBAAgB,CAAC,CAAC,CACvB,CAAC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC;gBAChC,GAAG,CACJ,KAAK,EAAE,GAAG,CAAC;QACd,CAAC;aAAM,CAAC;YACN,OAAO,eAAe,CAAC;QACzB,CAAC;IACH,CAAC;CACF;AA8BD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,IAAmD,EACrB,EAAE;IAChC,OAAO,IAAI,CAAC,QAAQ,YAAY,MAAM,IAAI,IAAI,CAAC,QAAQ,YAAY,QAAQ,CAAC;AAC9E,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/functions",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"utility-types": "^3.10.0",
|
|
15
|
-
"@saulx/utils": "^
|
|
15
|
+
"@saulx/utils": "^5.0.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"ts-node": "10.9.1",
|
package/src/context.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { parseQuery } from '@saulx/utils'
|
|
|
2
2
|
import { AuthState } from './auth.js'
|
|
3
3
|
import { WebSocket, HttpRequest, HttpResponse } from './uws.js'
|
|
4
4
|
import { BasedFunctionClient } from './client.js'
|
|
5
|
+
import { StreamPayload } from './functions.js'
|
|
5
6
|
|
|
6
7
|
export type WebSocketSession = {
|
|
7
8
|
// State can be used for anything - for us the based class instance
|
|
@@ -20,6 +21,7 @@ export type WebSocketSession = {
|
|
|
20
21
|
name: string
|
|
21
22
|
payload: any
|
|
22
23
|
}>
|
|
24
|
+
streams?: { [reqId: string]: StreamPayload }
|
|
23
25
|
headers: { [key: string]: string }
|
|
24
26
|
unauthorizedChannels?: Set<{
|
|
25
27
|
id: number
|
package/src/functions.ts
CHANGED
package/src/stream.ts
CHANGED
|
@@ -1,95 +1,104 @@
|
|
|
1
|
-
import { Duplex, Readable } from
|
|
2
|
-
import util from
|
|
1
|
+
import { Duplex, Readable } from "node:stream";
|
|
2
|
+
import util from "node:util";
|
|
3
3
|
|
|
4
|
-
// prob want to move this to based functions
|
|
5
4
|
export class BasedDataStream extends Duplex {
|
|
6
|
-
public size: number = 0
|
|
7
|
-
public
|
|
8
|
-
public
|
|
5
|
+
public size: number = 0;
|
|
6
|
+
public paused: boolean = false;
|
|
7
|
+
public receivedBytes: number = 0;
|
|
8
|
+
public progessTimer: NodeJS.Timeout;
|
|
9
9
|
|
|
10
10
|
constructor(size: number) {
|
|
11
|
-
super()
|
|
12
|
-
this.size = size
|
|
13
|
-
|
|
11
|
+
super();
|
|
12
|
+
this.size = size;
|
|
13
|
+
|
|
14
|
+
this.on("pause", () => {
|
|
15
|
+
this.paused = true;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
this.on("resume", () => {
|
|
19
|
+
this.paused = false;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
this.emit("progress", 0);
|
|
14
23
|
}
|
|
15
24
|
|
|
16
|
-
override _read() {
|
|
25
|
+
override _read() {}
|
|
17
26
|
|
|
18
27
|
override _write(chunk, encoding, callback) {
|
|
19
|
-
this.receivedBytes += chunk.byteLength
|
|
28
|
+
this.receivedBytes += chunk.byteLength;
|
|
20
29
|
if (this.size && this.size > 20000) {
|
|
21
30
|
if (!this.progessTimer) {
|
|
22
31
|
this.progessTimer = setTimeout(() => {
|
|
23
|
-
const progress = this.receivedBytes / this.size
|
|
24
|
-
this.emit(
|
|
25
|
-
this.progessTimer = null
|
|
26
|
-
}, 200)
|
|
32
|
+
const progress = this.receivedBytes / this.size;
|
|
33
|
+
this.emit("progress", progress);
|
|
34
|
+
this.progessTimer = null;
|
|
35
|
+
}, 200);
|
|
27
36
|
}
|
|
28
37
|
}
|
|
29
|
-
this.push(Buffer.from(chunk, encoding))
|
|
30
|
-
callback()
|
|
38
|
+
this.push(Buffer.from(chunk, encoding));
|
|
39
|
+
callback();
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
override _final() {
|
|
34
43
|
if (!this.size) {
|
|
35
|
-
this.size = this.receivedBytes
|
|
44
|
+
this.size = this.receivedBytes;
|
|
36
45
|
}
|
|
37
|
-
this.receivedBytes = this.size
|
|
46
|
+
this.receivedBytes = this.size;
|
|
38
47
|
if (this.progessTimer) {
|
|
39
|
-
clearTimeout(this.progessTimer)
|
|
40
|
-
this.progessTimer = null
|
|
48
|
+
clearTimeout(this.progessTimer);
|
|
49
|
+
this.progessTimer = null;
|
|
41
50
|
}
|
|
42
|
-
this.emit(
|
|
43
|
-
this.push(null)
|
|
51
|
+
this.emit("progress", 1);
|
|
52
|
+
this.push(null);
|
|
44
53
|
}
|
|
45
54
|
|
|
46
55
|
[util.inspect.custom]() {
|
|
47
56
|
if (this.size) {
|
|
48
57
|
const rb =
|
|
49
58
|
this.receivedBytes < 1000
|
|
50
|
-
? Math.round(this.receivedBytes / 1024) +
|
|
51
|
-
: Math.round(this.receivedBytes / 1024 / 1024) +
|
|
59
|
+
? Math.round(this.receivedBytes / 1024) + "kb"
|
|
60
|
+
: Math.round(this.receivedBytes / 1024 / 1024) + "mb";
|
|
52
61
|
|
|
53
62
|
return `[BasedStream ${~~(
|
|
54
63
|
(this.receivedBytes / this.size) *
|
|
55
64
|
100
|
|
56
|
-
)}% ${rb}]
|
|
65
|
+
)}% ${rb}]`;
|
|
57
66
|
} else {
|
|
58
|
-
return `[BasedStream]
|
|
67
|
+
return `[BasedStream]`;
|
|
59
68
|
}
|
|
60
69
|
}
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
// maybe make a type pkg
|
|
64
73
|
export type StreamFunctionContents<F = Buffer | ArrayBuffer | string> = {
|
|
65
|
-
contents: F
|
|
66
|
-
payload?: any
|
|
67
|
-
mimeType?: string
|
|
68
|
-
fileName?: string
|
|
69
|
-
}
|
|
74
|
+
contents: F;
|
|
75
|
+
payload?: any;
|
|
76
|
+
mimeType?: string;
|
|
77
|
+
fileName?: string;
|
|
78
|
+
};
|
|
70
79
|
|
|
71
80
|
export type StreamFunctionStream =
|
|
72
81
|
| {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
82
|
+
contents: Readable | Duplex;
|
|
83
|
+
payload?: any;
|
|
84
|
+
size: number;
|
|
85
|
+
mimeType?: string;
|
|
86
|
+
fileName?: string;
|
|
87
|
+
extension?: string;
|
|
88
|
+
}
|
|
80
89
|
| {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
contents: BasedDataStream;
|
|
91
|
+
payload?: any;
|
|
92
|
+
size?: number;
|
|
93
|
+
mimeType?: string;
|
|
94
|
+
fileName?: string;
|
|
95
|
+
extension?: string;
|
|
96
|
+
};
|
|
88
97
|
|
|
89
|
-
export type StreamFunctionOpts = StreamFunctionContents | StreamFunctionStream
|
|
98
|
+
export type StreamFunctionOpts = StreamFunctionContents | StreamFunctionStream;
|
|
90
99
|
|
|
91
100
|
export const isStreamFunctionOpts = (
|
|
92
101
|
opts: StreamFunctionContents | StreamFunctionStream
|
|
93
102
|
): opts is StreamFunctionStream => {
|
|
94
|
-
return opts.contents instanceof Duplex || opts.contents instanceof Readable
|
|
95
|
-
}
|
|
103
|
+
return opts.contents instanceof Duplex || opts.contents instanceof Readable;
|
|
104
|
+
};
|