@nmtjs/common 0.5.3 → 0.6.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/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/types.js.map +1 -1
- package/dist/lib/utils.js +93 -0
- package/dist/lib/utils.js.map +1 -0
- package/index.ts +1 -5
- package/lib/types.ts +27 -20
- package/lib/utils.ts +120 -0
- package/package.json +1 -1
- package/dist/lib/binary.js +0 -25
- package/dist/lib/binary.js.map +0 -1
- package/dist/lib/blob.js +0 -42
- package/dist/lib/blob.js.map +0 -1
- package/dist/lib/enums.js +0 -15
- package/dist/lib/enums.js.map +0 -1
- package/dist/lib/format.js +0 -4
- package/dist/lib/format.js.map +0 -1
- package/dist/lib/protocol.js +0 -27
- package/dist/lib/protocol.js.map +0 -1
- package/lib/binary.ts +0 -60
- package/lib/blob.ts +0 -67
- package/lib/enums.ts +0 -14
- package/lib/format.ts +0 -60
- package/lib/protocol.ts +0 -40
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"sourcesContent":["export * from './lib/
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["export * from './lib/utils.ts'\nexport * from './lib/types.ts'\n"],"names":[],"mappings":"AAAA,cAAc,iBAAgB;AAC9B,cAAc,iBAAgB"}
|
package/dist/lib/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/types.ts"],"sourcesContent":["export
|
|
1
|
+
{"version":3,"sources":["../../../lib/types.ts"],"sourcesContent":["export interface TypeProvider {\n readonly input: unknown\n readonly output: unknown\n}\n\nexport type CallTypeProvider<T extends TypeProvider, V> = (T & {\n input: V\n})['output']\n\nexport type ClassConstructor<T> = new (...args: any[]) => T\nexport type Callback<T extends any[] = any[], R = any> = (...args: T) => R\nexport type OmitFirstItem<T extends any[]> = T extends [any, ...infer U]\n ? U\n : []\nexport type ErrorClass = new (...args: any[]) => Error\nexport type Extra = Record<string, any>\nexport type Async<T> = T | Promise<T>\n\nexport type UnionToIntersection<U> = (\n U extends any\n ? (k: U) => void\n : never\n) extends (k: infer I) => void\n ? I\n : never\n\nexport type Merge<\n T1 extends Record<string, any>,\n T2 extends Record<string, any>,\n> = {\n [K in keyof T1 | keyof T2]: K extends keyof T2\n ? T2[K]\n : K extends keyof T1\n ? T1[K]\n : never\n}\n"],"names":[],"mappings":"AA0BA,WASC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const noopFn = ()=>{};
|
|
2
|
+
export function merge(...objects) {
|
|
3
|
+
return Object.assign({}, ...objects);
|
|
4
|
+
}
|
|
5
|
+
export function defer(cb, ms = 1, ...args) {
|
|
6
|
+
return new Promise((resolve, reject)=>setTimeout(async ()=>{
|
|
7
|
+
try {
|
|
8
|
+
resolve(await cb(...args));
|
|
9
|
+
} catch (error) {
|
|
10
|
+
reject(error);
|
|
11
|
+
}
|
|
12
|
+
}, ms));
|
|
13
|
+
}
|
|
14
|
+
export function range(count, start = 0) {
|
|
15
|
+
let current = start;
|
|
16
|
+
return {
|
|
17
|
+
[Symbol.iterator] () {
|
|
18
|
+
return {
|
|
19
|
+
next () {
|
|
20
|
+
if (current < count) {
|
|
21
|
+
return {
|
|
22
|
+
done: false,
|
|
23
|
+
value: current++
|
|
24
|
+
};
|
|
25
|
+
} else {
|
|
26
|
+
return {
|
|
27
|
+
done: true,
|
|
28
|
+
value: current
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function debounce(cb, delay) {
|
|
37
|
+
let timer;
|
|
38
|
+
const clear = ()=>timer && clearTimeout(timer);
|
|
39
|
+
const fn = (...args)=>{
|
|
40
|
+
clear();
|
|
41
|
+
timer = setTimeout(cb, delay, ...args);
|
|
42
|
+
};
|
|
43
|
+
return Object.assign(fn, {
|
|
44
|
+
clear
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function createPromise() {
|
|
48
|
+
let resolve;
|
|
49
|
+
let reject;
|
|
50
|
+
const promise = new Promise((...args)=>{
|
|
51
|
+
resolve = args[0];
|
|
52
|
+
reject = args[1];
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
resolve,
|
|
56
|
+
reject,
|
|
57
|
+
promise,
|
|
58
|
+
toArgs: ()=>[
|
|
59
|
+
resolve,
|
|
60
|
+
reject
|
|
61
|
+
]
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function onAbort(signal, cb, reason) {
|
|
65
|
+
const listener = ()=>cb(reason ?? signal.reason);
|
|
66
|
+
signal.addEventListener('abort', listener, {
|
|
67
|
+
once: true
|
|
68
|
+
});
|
|
69
|
+
return ()=>signal.removeEventListener('abort', listener);
|
|
70
|
+
}
|
|
71
|
+
export function withTimeout(value, timeout, timeoutError, controller) {
|
|
72
|
+
return new Promise((resolve, reject)=>{
|
|
73
|
+
const timer = setTimeout(reject, timeout, timeoutError);
|
|
74
|
+
const clearTimer = ()=>clearTimeout(timer);
|
|
75
|
+
const rejectWithTimeout = (error)=>{
|
|
76
|
+
reject(error);
|
|
77
|
+
controller?.abort(error);
|
|
78
|
+
};
|
|
79
|
+
value.then(resolve).catch(rejectWithTimeout).finally(clearTimer);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
export function tryCaptureStackTrace() {
|
|
83
|
+
return new Error().stack?.split('\n').slice(3).join('\n') ?? undefined;
|
|
84
|
+
}
|
|
85
|
+
export function isGeneratorFunction(value) {
|
|
86
|
+
return typeof value === 'function' && value.constructor.name === 'GeneratorFunction';
|
|
87
|
+
}
|
|
88
|
+
export function isAsyncGeneratorFunction(value) {
|
|
89
|
+
return typeof value === 'function' && value.constructor.name === 'AsyncGeneratorFunction';
|
|
90
|
+
}
|
|
91
|
+
export function throwError(message, ErrorClass = Error) {
|
|
92
|
+
throw new ErrorClass(message);
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../lib/utils.ts"],"sourcesContent":["import type { Callback } from './types.ts'\n\nexport const noopFn = () => {}\n\nexport function merge<T extends any[]>(...objects: T) {\n return Object.assign({}, ...objects)\n}\n\nexport function defer<T extends Callback>(\n cb: T,\n ms = 1,\n ...args: Parameters<T>\n): Promise<Awaited<ReturnType<T>>> {\n return new Promise((resolve, reject) =>\n setTimeout(async () => {\n try {\n resolve(await cb(...args))\n } catch (error) {\n reject(error)\n }\n }, ms),\n )\n}\n\nexport function range(count: number, start = 0) {\n let current = start\n return {\n [Symbol.iterator]() {\n return {\n next() {\n if (current < count) {\n return { done: false, value: current++ }\n } else {\n return { done: true, value: current }\n }\n },\n }\n },\n }\n}\n\nexport function debounce(cb: Callback, delay: number) {\n let timer: any\n const clear = () => timer && clearTimeout(timer)\n const fn = (...args: any[]) => {\n clear()\n timer = setTimeout(cb, delay, ...args)\n }\n return Object.assign(fn, { clear })\n}\n\n// TODO: Promise.withResolvers?\nexport type InteractivePromise<T = any> = {\n promise: Promise<T>\n resolve: (value: T) => void\n reject: (error: any) => void\n toArgs: () => [resolve: (value: T) => void, reject: (error: any) => void]\n}\n\nexport function createPromise<T>(): InteractivePromise<T> {\n let resolve: InteractivePromise<T>['resolve']\n let reject: InteractivePromise<T>['reject']\n const promise = new Promise<T>((...args) => {\n resolve = args[0]\n reject = args[1]\n })\n // @ts-expect-error\n return { resolve, reject, promise, toArgs: () => [resolve, reject] }\n}\n\nexport function onAbort<T extends Callback>(\n signal: AbortSignal,\n cb: T,\n reason?: any,\n) {\n const listener = () => cb(reason ?? signal.reason)\n signal.addEventListener('abort', listener, { once: true })\n return () => signal.removeEventListener('abort', listener)\n}\n\nexport function withTimeout(\n value: Promise<any>,\n timeout: number,\n timeoutError: Error,\n controller?: AbortController,\n) {\n return new Promise((resolve, reject) => {\n const timer = setTimeout(reject, timeout, timeoutError)\n const clearTimer = () => clearTimeout(timer)\n const rejectWithTimeout = (error: any) => {\n reject(error)\n controller?.abort(error)\n }\n value.then(resolve).catch(rejectWithTimeout).finally(clearTimer)\n })\n}\n\nexport function tryCaptureStackTrace() {\n return new Error().stack?.split('\\n').slice(3).join('\\n') ?? undefined\n}\n\nexport function isGeneratorFunction(value: any): value is GeneratorFunction {\n return (\n typeof value === 'function' &&\n value.constructor.name === 'GeneratorFunction'\n )\n}\n\nexport function isAsyncGeneratorFunction(\n value: any,\n): value is AsyncGeneratorFunction {\n return (\n typeof value === 'function' &&\n value.constructor.name === 'AsyncGeneratorFunction'\n )\n}\n\nexport function throwError(message: string, ErrorClass = Error): never {\n throw new ErrorClass(message)\n}\n"],"names":["noopFn","merge","objects","Object","assign","defer","cb","ms","args","Promise","resolve","reject","setTimeout","error","range","count","start","current","Symbol","iterator","next","done","value","debounce","delay","timer","clear","clearTimeout","fn","createPromise","promise","toArgs","onAbort","signal","reason","listener","addEventListener","once","removeEventListener","withTimeout","timeout","timeoutError","controller","clearTimer","rejectWithTimeout","abort","then","catch","finally","tryCaptureStackTrace","Error","stack","split","slice","join","undefined","isGeneratorFunction","constructor","name","isAsyncGeneratorFunction","throwError","message","ErrorClass"],"mappings":"AAEA,OAAO,MAAMA,SAAS,KAAO,EAAC;AAE9B,OAAO,SAASC,MAAuB,GAAGC,OAAU;IAClD,OAAOC,OAAOC,MAAM,CAAC,CAAC,MAAMF;AAC9B;AAEA,OAAO,SAASG,MACdC,EAAK,EACLC,KAAK,CAAC,EACN,GAAGC,IAAmB;IAEtB,OAAO,IAAIC,QAAQ,CAACC,SAASC,SAC3BC,WAAW;YACT,IAAI;gBACFF,QAAQ,MAAMJ,MAAME;YACtB,EAAE,OAAOK,OAAO;gBACdF,OAAOE;YACT;QACF,GAAGN;AAEP;AAEA,OAAO,SAASO,MAAMC,KAAa,EAAEC,QAAQ,CAAC;IAC5C,IAAIC,UAAUD;IACd,OAAO;QACL,CAACE,OAAOC,QAAQ,CAAC;YACf,OAAO;gBACLC;oBACE,IAAIH,UAAUF,OAAO;wBACnB,OAAO;4BAAEM,MAAM;4BAAOC,OAAOL;wBAAU;oBACzC,OAAO;wBACL,OAAO;4BAAEI,MAAM;4BAAMC,OAAOL;wBAAQ;oBACtC;gBACF;YACF;QACF;IACF;AACF;AAEA,OAAO,SAASM,SAASjB,EAAY,EAAEkB,KAAa;IAClD,IAAIC;IACJ,MAAMC,QAAQ,IAAMD,SAASE,aAAaF;IAC1C,MAAMG,KAAK,CAAC,GAAGpB;QACbkB;QACAD,QAAQb,WAAWN,IAAIkB,UAAUhB;IACnC;IACA,OAAOL,OAAOC,MAAM,CAACwB,IAAI;QAAEF;IAAM;AACnC;AAUA,OAAO,SAASG;IACd,IAAInB;IACJ,IAAIC;IACJ,MAAMmB,UAAU,IAAIrB,QAAW,CAAC,GAAGD;QACjCE,UAAUF,IAAI,CAAC,EAAE;QACjBG,SAASH,IAAI,CAAC,EAAE;IAClB;IAEA,OAAO;QAAEE;QAASC;QAAQmB;QAASC,QAAQ,IAAM;gBAACrB;gBAASC;aAAO;IAAC;AACrE;AAEA,OAAO,SAASqB,QACdC,MAAmB,EACnB3B,EAAK,EACL4B,MAAY;IAEZ,MAAMC,WAAW,IAAM7B,GAAG4B,UAAUD,OAAOC,MAAM;IACjDD,OAAOG,gBAAgB,CAAC,SAASD,UAAU;QAAEE,MAAM;IAAK;IACxD,OAAO,IAAMJ,OAAOK,mBAAmB,CAAC,SAASH;AACnD;AAEA,OAAO,SAASI,YACdjB,KAAmB,EACnBkB,OAAe,EACfC,YAAmB,EACnBC,UAA4B;IAE5B,OAAO,IAAIjC,QAAQ,CAACC,SAASC;QAC3B,MAAMc,QAAQb,WAAWD,QAAQ6B,SAASC;QAC1C,MAAME,aAAa,IAAMhB,aAAaF;QACtC,MAAMmB,oBAAoB,CAAC/B;YACzBF,OAAOE;YACP6B,YAAYG,MAAMhC;QACpB;QACAS,MAAMwB,IAAI,CAACpC,SAASqC,KAAK,CAACH,mBAAmBI,OAAO,CAACL;IACvD;AACF;AAEA,OAAO,SAASM;IACd,OAAO,IAAIC,QAAQC,KAAK,EAAEC,MAAM,MAAMC,MAAM,GAAGC,KAAK,SAASC;AAC/D;AAEA,OAAO,SAASC,oBAAoBlC,KAAU;IAC5C,OACE,OAAOA,UAAU,cACjBA,MAAMmC,WAAW,CAACC,IAAI,KAAK;AAE/B;AAEA,OAAO,SAASC,yBACdrC,KAAU;IAEV,OACE,OAAOA,UAAU,cACjBA,MAAMmC,WAAW,CAACC,IAAI,KAAK;AAE/B;AAEA,OAAO,SAASE,WAAWC,OAAe,EAAEC,aAAaZ,KAAK;IAC5D,MAAM,IAAIY,WAAWD;AACvB"}
|
package/index.ts
CHANGED
package/lib/types.ts
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
export type ApiBlobMetadata = {
|
|
2
|
-
type: string
|
|
3
|
-
size: number
|
|
4
|
-
filename?: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export type Rpc = {
|
|
8
|
-
callId: number
|
|
9
|
-
service: string
|
|
10
|
-
procedure: string
|
|
11
|
-
payload: any
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type RpcResponse = {
|
|
15
|
-
callId: number
|
|
16
|
-
error?: any
|
|
17
|
-
payload?: any
|
|
18
|
-
}
|
|
19
|
-
|
|
20
1
|
export interface TypeProvider {
|
|
21
2
|
readonly input: unknown
|
|
22
3
|
readonly output: unknown
|
|
@@ -26,4 +7,30 @@ export type CallTypeProvider<T extends TypeProvider, V> = (T & {
|
|
|
26
7
|
input: V
|
|
27
8
|
})['output']
|
|
28
9
|
|
|
29
|
-
export type
|
|
10
|
+
export type ClassConstructor<T> = new (...args: any[]) => T
|
|
11
|
+
export type Callback<T extends any[] = any[], R = any> = (...args: T) => R
|
|
12
|
+
export type OmitFirstItem<T extends any[]> = T extends [any, ...infer U]
|
|
13
|
+
? U
|
|
14
|
+
: []
|
|
15
|
+
export type ErrorClass = new (...args: any[]) => Error
|
|
16
|
+
export type Extra = Record<string, any>
|
|
17
|
+
export type Async<T> = T | Promise<T>
|
|
18
|
+
|
|
19
|
+
export type UnionToIntersection<U> = (
|
|
20
|
+
U extends any
|
|
21
|
+
? (k: U) => void
|
|
22
|
+
: never
|
|
23
|
+
) extends (k: infer I) => void
|
|
24
|
+
? I
|
|
25
|
+
: never
|
|
26
|
+
|
|
27
|
+
export type Merge<
|
|
28
|
+
T1 extends Record<string, any>,
|
|
29
|
+
T2 extends Record<string, any>,
|
|
30
|
+
> = {
|
|
31
|
+
[K in keyof T1 | keyof T2]: K extends keyof T2
|
|
32
|
+
? T2[K]
|
|
33
|
+
: K extends keyof T1
|
|
34
|
+
? T1[K]
|
|
35
|
+
: never
|
|
36
|
+
}
|
package/lib/utils.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { Callback } from './types.ts'
|
|
2
|
+
|
|
3
|
+
export const noopFn = () => {}
|
|
4
|
+
|
|
5
|
+
export function merge<T extends any[]>(...objects: T) {
|
|
6
|
+
return Object.assign({}, ...objects)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function defer<T extends Callback>(
|
|
10
|
+
cb: T,
|
|
11
|
+
ms = 1,
|
|
12
|
+
...args: Parameters<T>
|
|
13
|
+
): Promise<Awaited<ReturnType<T>>> {
|
|
14
|
+
return new Promise((resolve, reject) =>
|
|
15
|
+
setTimeout(async () => {
|
|
16
|
+
try {
|
|
17
|
+
resolve(await cb(...args))
|
|
18
|
+
} catch (error) {
|
|
19
|
+
reject(error)
|
|
20
|
+
}
|
|
21
|
+
}, ms),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function range(count: number, start = 0) {
|
|
26
|
+
let current = start
|
|
27
|
+
return {
|
|
28
|
+
[Symbol.iterator]() {
|
|
29
|
+
return {
|
|
30
|
+
next() {
|
|
31
|
+
if (current < count) {
|
|
32
|
+
return { done: false, value: current++ }
|
|
33
|
+
} else {
|
|
34
|
+
return { done: true, value: current }
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function debounce(cb: Callback, delay: number) {
|
|
43
|
+
let timer: any
|
|
44
|
+
const clear = () => timer && clearTimeout(timer)
|
|
45
|
+
const fn = (...args: any[]) => {
|
|
46
|
+
clear()
|
|
47
|
+
timer = setTimeout(cb, delay, ...args)
|
|
48
|
+
}
|
|
49
|
+
return Object.assign(fn, { clear })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// TODO: Promise.withResolvers?
|
|
53
|
+
export type InteractivePromise<T = any> = {
|
|
54
|
+
promise: Promise<T>
|
|
55
|
+
resolve: (value: T) => void
|
|
56
|
+
reject: (error: any) => void
|
|
57
|
+
toArgs: () => [resolve: (value: T) => void, reject: (error: any) => void]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function createPromise<T>(): InteractivePromise<T> {
|
|
61
|
+
let resolve: InteractivePromise<T>['resolve']
|
|
62
|
+
let reject: InteractivePromise<T>['reject']
|
|
63
|
+
const promise = new Promise<T>((...args) => {
|
|
64
|
+
resolve = args[0]
|
|
65
|
+
reject = args[1]
|
|
66
|
+
})
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
return { resolve, reject, promise, toArgs: () => [resolve, reject] }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function onAbort<T extends Callback>(
|
|
72
|
+
signal: AbortSignal,
|
|
73
|
+
cb: T,
|
|
74
|
+
reason?: any,
|
|
75
|
+
) {
|
|
76
|
+
const listener = () => cb(reason ?? signal.reason)
|
|
77
|
+
signal.addEventListener('abort', listener, { once: true })
|
|
78
|
+
return () => signal.removeEventListener('abort', listener)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function withTimeout(
|
|
82
|
+
value: Promise<any>,
|
|
83
|
+
timeout: number,
|
|
84
|
+
timeoutError: Error,
|
|
85
|
+
controller?: AbortController,
|
|
86
|
+
) {
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
const timer = setTimeout(reject, timeout, timeoutError)
|
|
89
|
+
const clearTimer = () => clearTimeout(timer)
|
|
90
|
+
const rejectWithTimeout = (error: any) => {
|
|
91
|
+
reject(error)
|
|
92
|
+
controller?.abort(error)
|
|
93
|
+
}
|
|
94
|
+
value.then(resolve).catch(rejectWithTimeout).finally(clearTimer)
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function tryCaptureStackTrace() {
|
|
99
|
+
return new Error().stack?.split('\n').slice(3).join('\n') ?? undefined
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isGeneratorFunction(value: any): value is GeneratorFunction {
|
|
103
|
+
return (
|
|
104
|
+
typeof value === 'function' &&
|
|
105
|
+
value.constructor.name === 'GeneratorFunction'
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function isAsyncGeneratorFunction(
|
|
110
|
+
value: any,
|
|
111
|
+
): value is AsyncGeneratorFunction {
|
|
112
|
+
return (
|
|
113
|
+
typeof value === 'function' &&
|
|
114
|
+
value.constructor.name === 'AsyncGeneratorFunction'
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function throwError(message: string, ErrorClass = Error): never {
|
|
119
|
+
throw new ErrorClass(message)
|
|
120
|
+
}
|
package/package.json
CHANGED
package/dist/lib/binary.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const utf8decoder = new TextDecoder();
|
|
2
|
-
const utf8encoder = new TextEncoder();
|
|
3
|
-
export const encodeNumber = (value, type, littleEndian = false)=>{
|
|
4
|
-
const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT;
|
|
5
|
-
const ab = new ArrayBuffer(bytesNeeded);
|
|
6
|
-
const dv = new DataView(ab);
|
|
7
|
-
dv[`set${type}`](0, value, littleEndian);
|
|
8
|
-
return ab;
|
|
9
|
-
};
|
|
10
|
-
export const decodeNumber = (buffer, type, offset = 0, littleEndian = false)=>{
|
|
11
|
-
const view = new DataView(buffer);
|
|
12
|
-
return view[`get${type}`](offset, littleEndian);
|
|
13
|
-
};
|
|
14
|
-
export const encodeText = (text)=>new Uint8Array(utf8encoder.encode(text)).buffer;
|
|
15
|
-
export const decodeText = (buffer)=>utf8decoder.decode(buffer);
|
|
16
|
-
export const concat = (...buffers)=>{
|
|
17
|
-
const totalLength = buffers.reduce((acc, buffer)=>acc + buffer.byteLength, 0);
|
|
18
|
-
const view = new Uint8Array(totalLength);
|
|
19
|
-
let offset = 0;
|
|
20
|
-
for (const buffer of buffers){
|
|
21
|
-
view.set(new Uint8Array(buffer), offset);
|
|
22
|
-
offset += buffer.byteLength;
|
|
23
|
-
}
|
|
24
|
-
return view.buffer;
|
|
25
|
-
};
|
package/dist/lib/binary.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/binary.ts"],"sourcesContent":["// TODO: get rid of lib DOM somehow...\n/// <reference lib=\"dom\" />\n\nconst utf8decoder = new TextDecoder()\nconst utf8encoder = new TextEncoder()\n\nexport type BinaryTypes = {\n Int8: number\n Int16: number\n Int32: number\n Uint8: number\n Uint16: number\n Uint32: number\n Float32: number\n Float64: number\n BigInt64: bigint\n BigUint64: bigint\n}\n\nexport const encodeNumber = <T extends keyof BinaryTypes>(\n value: BinaryTypes[T],\n type: T,\n littleEndian = false,\n) => {\n const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT\n const ab = new ArrayBuffer(bytesNeeded)\n const dv = new DataView(ab)\n dv[`set${type}`](0, value as never, littleEndian)\n return ab\n}\n\nexport const decodeNumber = <T extends keyof BinaryTypes>(\n buffer: ArrayBuffer,\n type: T,\n offset = 0,\n littleEndian = false,\n): BinaryTypes[T] => {\n const view = new DataView(buffer)\n return view[`get${type}`](offset, littleEndian) as BinaryTypes[T]\n}\n\nexport const encodeText = (text: string) =>\n new Uint8Array(utf8encoder.encode(text)).buffer as ArrayBuffer\n\nexport const decodeText = (buffer: Parameters<typeof utf8decoder.decode>[0]) =>\n utf8decoder.decode(buffer)\n\nexport const concat = (...buffers: ArrayBuffer[]) => {\n const totalLength = buffers.reduce(\n (acc, buffer) => acc + buffer.byteLength,\n 0,\n )\n const view = new Uint8Array(totalLength)\n let offset = 0\n for (const buffer of buffers) {\n view.set(new Uint8Array(buffer), offset)\n offset += buffer.byteLength\n }\n return view.buffer as ArrayBuffer\n}\n"],"names":["utf8decoder","TextDecoder","utf8encoder","TextEncoder","encodeNumber","value","type","littleEndian","bytesNeeded","globalThis","BYTES_PER_ELEMENT","ab","ArrayBuffer","dv","DataView","decodeNumber","buffer","offset","view","encodeText","text","Uint8Array","encode","decodeText","decode","concat","buffers","totalLength","reduce","acc","byteLength","set"],"mappings":"AAGA,MAAMA,cAAc,IAAIC;AACxB,MAAMC,cAAc,IAAIC;AAexB,OAAO,MAAMC,eAAe,CAC1BC,OACAC,MACAC,eAAe,KAAK;IAEpB,MAAMC,cAAcC,UAAU,CAAC,CAAC,EAAEH,KAAK,KAAK,CAAC,CAAC,CAACI,iBAAiB;IAChE,MAAMC,KAAK,IAAIC,YAAYJ;IAC3B,MAAMK,KAAK,IAAIC,SAASH;IACxBE,EAAE,CAAC,CAAC,GAAG,EAAEP,KAAK,CAAC,CAAC,CAAC,GAAGD,OAAgBE;IACpC,OAAOI;AACT,EAAC;AAED,OAAO,MAAMI,eAAe,CAC1BC,QACAV,MACAW,SAAS,CAAC,EACVV,eAAe,KAAK;IAEpB,MAAMW,OAAO,IAAIJ,SAASE;IAC1B,OAAOE,IAAI,CAAC,CAAC,GAAG,EAAEZ,KAAK,CAAC,CAAC,CAACW,QAAQV;AACpC,EAAC;AAED,OAAO,MAAMY,aAAa,CAACC,OACzB,IAAIC,WAAWnB,YAAYoB,MAAM,CAACF,OAAOJ,MAAM,CAAe;AAEhE,OAAO,MAAMO,aAAa,CAACP,SACzBhB,YAAYwB,MAAM,CAACR,QAAO;AAE5B,OAAO,MAAMS,SAAS,CAAC,GAAGC;IACxB,MAAMC,cAAcD,QAAQE,MAAM,CAChC,CAACC,KAAKb,SAAWa,MAAMb,OAAOc,UAAU,EACxC;IAEF,MAAMZ,OAAO,IAAIG,WAAWM;IAC5B,IAAIV,SAAS;IACb,KAAK,MAAMD,UAAUU,QAAS;QAC5BR,KAAKa,GAAG,CAAC,IAAIV,WAAWL,SAASC;QACjCA,UAAUD,OAAOc,UAAU;IAC7B;IACA,OAAOZ,KAAKF,MAAM;AACpB,EAAC"}
|
package/dist/lib/blob.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
export class ApiBlob {
|
|
2
|
-
metadata;
|
|
3
|
-
source;
|
|
4
|
-
constructor(source, size = -1, type = 'application/octet-stream', filename){
|
|
5
|
-
if (size < -1 || size === 0) throw new Error('Blob size is invalid');
|
|
6
|
-
this.source = source;
|
|
7
|
-
this.metadata = {
|
|
8
|
-
size,
|
|
9
|
-
type,
|
|
10
|
-
filename
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
static from(source, metadata = {}) {
|
|
14
|
-
let _source = undefined;
|
|
15
|
-
if (source instanceof ReadableStream) {
|
|
16
|
-
_source = source;
|
|
17
|
-
} else if ('File' in globalThis && source instanceof globalThis.File) {
|
|
18
|
-
_source = source.stream();
|
|
19
|
-
metadata.size = source.size;
|
|
20
|
-
metadata.filename = source.name;
|
|
21
|
-
} else if (source instanceof Blob) {
|
|
22
|
-
_source = source.stream();
|
|
23
|
-
metadata.size = source.size;
|
|
24
|
-
} else if (typeof source === 'string') {
|
|
25
|
-
const blob = new Blob([
|
|
26
|
-
source
|
|
27
|
-
]);
|
|
28
|
-
_source = blob.stream();
|
|
29
|
-
metadata.size = blob.size;
|
|
30
|
-
metadata.type = metadata.type || 'text/plain';
|
|
31
|
-
} else if (source instanceof ArrayBuffer) {
|
|
32
|
-
const blob = new Blob([
|
|
33
|
-
source
|
|
34
|
-
]);
|
|
35
|
-
_source = blob.stream();
|
|
36
|
-
metadata.size = blob.size;
|
|
37
|
-
} else {
|
|
38
|
-
_source = source;
|
|
39
|
-
}
|
|
40
|
-
return new ApiBlob(_source, metadata.size, metadata.type, metadata.filename);
|
|
41
|
-
}
|
|
42
|
-
}
|
package/dist/lib/blob.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/blob.ts"],"sourcesContent":["import type { ApiBlobMetadata } from './types.ts'\n\nexport interface ApiBlobInterface {\n readonly metadata: ApiBlobMetadata\n}\n\nexport type Exact<A, B> = [A] extends [B]\n ? [B] extends [A]\n ? true\n : false\n : false\n\nexport class ApiBlob implements ApiBlobInterface {\n public readonly metadata: ApiBlobMetadata\n public readonly source: any\n\n constructor(\n source: any,\n size = -1,\n type = 'application/octet-stream',\n filename?: string,\n ) {\n if (size < -1 || size === 0) throw new Error('Blob size is invalid')\n\n this.source = source\n this.metadata = {\n size,\n type,\n filename,\n }\n }\n\n static from(\n source: any,\n metadata: {\n size?: number\n type?: string\n filename?: string\n } = {},\n ) {\n let _source: any = undefined\n\n if (source instanceof ReadableStream) {\n _source = source\n } else if ('File' in globalThis && source instanceof globalThis.File) {\n _source = source.stream()\n metadata.size = source.size\n metadata.filename = source.name\n } else if (source instanceof Blob) {\n _source = source.stream()\n metadata.size = source.size\n } else if (typeof source === 'string') {\n const blob = new Blob([source])\n _source = blob.stream()\n metadata.size = blob.size\n metadata.type = metadata.type || 'text/plain'\n } else if (source instanceof ArrayBuffer) {\n const blob = new Blob([source])\n _source = blob.stream()\n metadata.size = blob.size\n } else {\n _source = source\n }\n\n return new ApiBlob(_source, metadata.size, metadata.type, metadata.filename)\n }\n}\n"],"names":["ApiBlob","metadata","source","constructor","size","type","filename","Error","from","_source","undefined","ReadableStream","globalThis","File","stream","name","Blob","blob","ArrayBuffer"],"mappings":"AAYA,OAAO,MAAMA;IACKC,SAAyB;IACzBC,OAAW;IAE3BC,YACED,MAAW,EACXE,OAAO,CAAC,CAAC,EACTC,OAAO,0BAA0B,EACjCC,QAAiB,CACjB;QACA,IAAIF,OAAO,CAAC,KAAKA,SAAS,GAAG,MAAM,IAAIG,MAAM;QAE7C,IAAI,CAACL,MAAM,GAAGA;QACd,IAAI,CAACD,QAAQ,GAAG;YACdG;YACAC;YACAC;QACF;IACF;IAEA,OAAOE,KACLN,MAAW,EACXD,WAII,CAAC,CAAC,EACN;QACA,IAAIQ,UAAeC;QAEnB,IAAIR,kBAAkBS,gBAAgB;YACpCF,UAAUP;QACZ,OAAO,IAAI,UAAUU,cAAcV,kBAAkBU,WAAWC,IAAI,EAAE;YACpEJ,UAAUP,OAAOY,MAAM;YACvBb,SAASG,IAAI,GAAGF,OAAOE,IAAI;YAC3BH,SAASK,QAAQ,GAAGJ,OAAOa,IAAI;QACjC,OAAO,IAAIb,kBAAkBc,MAAM;YACjCP,UAAUP,OAAOY,MAAM;YACvBb,SAASG,IAAI,GAAGF,OAAOE,IAAI;QAC7B,OAAO,IAAI,OAAOF,WAAW,UAAU;YACrC,MAAMe,OAAO,IAAID,KAAK;gBAACd;aAAO;YAC9BO,UAAUQ,KAAKH,MAAM;YACrBb,SAASG,IAAI,GAAGa,KAAKb,IAAI;YACzBH,SAASI,IAAI,GAAGJ,SAASI,IAAI,IAAI;QACnC,OAAO,IAAIH,kBAAkBgB,aAAa;YACxC,MAAMD,OAAO,IAAID,KAAK;gBAACd;aAAO;YAC9BO,UAAUQ,KAAKH,MAAM;YACrBb,SAASG,IAAI,GAAGa,KAAKb,IAAI;QAC3B,OAAO;YACLK,UAAUP;QACZ;QAEA,OAAO,IAAIF,QAAQS,SAASR,SAASG,IAAI,EAAEH,SAASI,IAAI,EAAEJ,SAASK,QAAQ;IAC7E;AACF"}
|
package/dist/lib/enums.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export var ErrorCode;
|
|
2
|
-
(function(ErrorCode) {
|
|
3
|
-
ErrorCode["ValidationError"] = "ValidationError";
|
|
4
|
-
ErrorCode["BadRequest"] = "BadRequest";
|
|
5
|
-
ErrorCode["NotFound"] = "NotFound";
|
|
6
|
-
ErrorCode["Forbidden"] = "Forbidden";
|
|
7
|
-
ErrorCode["Unauthorized"] = "Unauthorized";
|
|
8
|
-
ErrorCode["InternalServerError"] = "InternalServerError";
|
|
9
|
-
ErrorCode["NotAcceptable"] = "NotAcceptable";
|
|
10
|
-
ErrorCode["RequestTimeout"] = "RequestTimeout";
|
|
11
|
-
ErrorCode["GatewayTimeout"] = "GatewayTimeout";
|
|
12
|
-
ErrorCode["ServiceUnavailable"] = "ServiceUnavailable";
|
|
13
|
-
ErrorCode["ClientRequestError"] = "ClientRequestError";
|
|
14
|
-
ErrorCode["ConnectionError"] = "ConnectionError";
|
|
15
|
-
})(ErrorCode || (ErrorCode = {}));
|
package/dist/lib/enums.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/enums.ts"],"sourcesContent":["export enum ErrorCode {\n ValidationError = 'ValidationError',\n BadRequest = 'BadRequest',\n NotFound = 'NotFound',\n Forbidden = 'Forbidden',\n Unauthorized = 'Unauthorized',\n InternalServerError = 'InternalServerError',\n NotAcceptable = 'NotAcceptable',\n RequestTimeout = 'RequestTimeout',\n GatewayTimeout = 'GatewayTimeout',\n ServiceUnavailable = 'ServiceUnavailable',\n ClientRequestError = 'ClientRequestError',\n ConnectionError = 'ConnectionError',\n}\n"],"names":["ErrorCode"],"mappings":";UAAYA;;;;;;;;;;;;;GAAAA,cAAAA"}
|
package/dist/lib/format.js
DELETED
package/dist/lib/format.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/format.ts"],"sourcesContent":["import type { ApiBlob } from './blob.ts'\nimport type { ApiBlobMetadata, Pattern, Rpc, RpcResponse } from './types.ts'\n\nexport interface EncodeRpcContext {\n getStream: (id: number) => any\n addStream: (blob: ApiBlob) => { id: number; metadata: ApiBlobMetadata }\n}\n\nexport interface DecodeRpcContext {\n getStream: (id: number) => any\n addStream: (id: number, metadata: ApiBlobMetadata) => any\n}\n\nexport interface BaseClientDecoder {\n decode(buffer: ArrayBuffer): any\n decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): RpcResponse\n}\n\nexport interface BaseClientEncoder {\n encode(data: any): ArrayBuffer\n encodeRpc(rpc: Rpc, context: EncodeRpcContext): ArrayBuffer\n}\n\nexport abstract class BaseClientFormat\n implements BaseClientDecoder, BaseClientEncoder\n{\n abstract contentType: string\n\n abstract encode(data: any): ArrayBuffer\n abstract encodeRpc(rpc: Rpc, context: EncodeRpcContext): ArrayBuffer\n abstract decode(buffer: ArrayBuffer): any\n abstract decodeRpc(\n buffer: ArrayBuffer,\n context: DecodeRpcContext,\n ): RpcResponse\n}\n\nexport interface BaseServerDecoder {\n accept: Pattern[]\n decode(buffer: ArrayBuffer): any\n decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): Rpc\n}\n\nexport interface BaseServerEncoder {\n contentType: string\n encode(data: any): ArrayBuffer\n encodeRpc(rpc: RpcResponse, context: EncodeRpcContext): ArrayBuffer\n}\n\nexport abstract class BaseServerFormat\n implements BaseServerDecoder, BaseServerEncoder\n{\n abstract accept: Pattern[]\n abstract contentType: string\n\n abstract encode(data: any): ArrayBuffer\n abstract encodeRpc(rpc: RpcResponse, context: EncodeRpcContext): ArrayBuffer\n abstract decode(buffer: ArrayBuffer): any\n abstract decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): Rpc\n}\n"],"names":["BaseClientFormat","BaseServerFormat"],"mappings":"AAuBA,OAAO,MAAeA;AAYtB;AAcA,OAAO,MAAeC;AAUtB"}
|
package/dist/lib/protocol.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export const MessageType = Object.freeze({
|
|
2
|
-
Event: 10,
|
|
3
|
-
Rpc: 11,
|
|
4
|
-
RpcBatch: 12,
|
|
5
|
-
RpcAbort: 14,
|
|
6
|
-
RpcSubscription: 15,
|
|
7
|
-
UpStreamAbort: 30,
|
|
8
|
-
UpStreamPush: 31,
|
|
9
|
-
UpStreamPull: 32,
|
|
10
|
-
UpStreamEnd: 33,
|
|
11
|
-
ClientUnsubscribe: 34,
|
|
12
|
-
DownStreamAbort: 50,
|
|
13
|
-
DownStreamPull: 51,
|
|
14
|
-
DownStreamPush: 52,
|
|
15
|
-
DownStreamEnd: 53,
|
|
16
|
-
ServerUnsubscribe: 54,
|
|
17
|
-
ServerSubscriptionEvent: 55
|
|
18
|
-
});
|
|
19
|
-
export const MessageTypeName = Object.fromEntries(Object.entries(MessageType).map(([k, v])=>[
|
|
20
|
-
v,
|
|
21
|
-
k
|
|
22
|
-
]));
|
|
23
|
-
export var TransportType;
|
|
24
|
-
(function(TransportType) {
|
|
25
|
-
TransportType["WS"] = "WS";
|
|
26
|
-
TransportType["HTTP"] = "HTTP";
|
|
27
|
-
})(TransportType || (TransportType = {}));
|
package/dist/lib/protocol.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/protocol.ts"],"sourcesContent":["export const MessageType = Object.freeze({\n // Common\n Event: 10,\n Rpc: 11,\n RpcBatch: 12,\n RpcAbort: 14,\n RpcSubscription: 15,\n\n // Client streams\n UpStreamAbort: 30,\n UpStreamPush: 31,\n UpStreamPull: 32,\n UpStreamEnd: 33,\n\n // Client subsctiption\n ClientUnsubscribe: 34,\n\n // Server streams\n DownStreamAbort: 50,\n DownStreamPull: 51,\n DownStreamPush: 52,\n DownStreamEnd: 53,\n\n // Server subsctiption\n ServerUnsubscribe: 54,\n ServerSubscriptionEvent: 55,\n} as const)\n\nexport type MessageType = (typeof MessageType)[keyof typeof MessageType]\n\nexport const MessageTypeName = Object.fromEntries(\n Object.entries(MessageType).map(([k, v]) => [v, k]),\n)\nexport type MessageTypeName = keyof typeof MessageType\n\n// TODO: Should it be hardcoded ??\nexport enum TransportType {\n WS = 'WS',\n HTTP = 'HTTP',\n}\n"],"names":["MessageType","Object","freeze","Event","Rpc","RpcBatch","RpcAbort","RpcSubscription","UpStreamAbort","UpStreamPush","UpStreamPull","UpStreamEnd","ClientUnsubscribe","DownStreamAbort","DownStreamPull","DownStreamPush","DownStreamEnd","ServerUnsubscribe","ServerSubscriptionEvent","MessageTypeName","fromEntries","entries","map","k","v","TransportType"],"mappings":"AAAA,OAAO,MAAMA,cAAcC,OAAOC,MAAM,CAAC;IAEvCC,OAAO;IACPC,KAAK;IACLC,UAAU;IACVC,UAAU;IACVC,iBAAiB;IAGjBC,eAAe;IACfC,cAAc;IACdC,cAAc;IACdC,aAAa;IAGbC,mBAAmB;IAGnBC,iBAAiB;IACjBC,gBAAgB;IAChBC,gBAAgB;IAChBC,eAAe;IAGfC,mBAAmB;IACnBC,yBAAyB;AAC3B,GAAW;AAIX,OAAO,MAAMC,kBAAkBlB,OAAOmB,WAAW,CAC/CnB,OAAOoB,OAAO,CAACrB,aAAasB,GAAG,CAAC,CAAC,CAACC,GAAGC,EAAE,GAAK;QAACA;QAAGD;KAAE,GACnD;;UAIWE;;;GAAAA,kBAAAA"}
|
package/lib/binary.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
// TODO: get rid of lib DOM somehow...
|
|
2
|
-
/// <reference lib="dom" />
|
|
3
|
-
|
|
4
|
-
const utf8decoder = new TextDecoder()
|
|
5
|
-
const utf8encoder = new TextEncoder()
|
|
6
|
-
|
|
7
|
-
export type BinaryTypes = {
|
|
8
|
-
Int8: number
|
|
9
|
-
Int16: number
|
|
10
|
-
Int32: number
|
|
11
|
-
Uint8: number
|
|
12
|
-
Uint16: number
|
|
13
|
-
Uint32: number
|
|
14
|
-
Float32: number
|
|
15
|
-
Float64: number
|
|
16
|
-
BigInt64: bigint
|
|
17
|
-
BigUint64: bigint
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const encodeNumber = <T extends keyof BinaryTypes>(
|
|
21
|
-
value: BinaryTypes[T],
|
|
22
|
-
type: T,
|
|
23
|
-
littleEndian = false,
|
|
24
|
-
) => {
|
|
25
|
-
const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT
|
|
26
|
-
const ab = new ArrayBuffer(bytesNeeded)
|
|
27
|
-
const dv = new DataView(ab)
|
|
28
|
-
dv[`set${type}`](0, value as never, littleEndian)
|
|
29
|
-
return ab
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const decodeNumber = <T extends keyof BinaryTypes>(
|
|
33
|
-
buffer: ArrayBuffer,
|
|
34
|
-
type: T,
|
|
35
|
-
offset = 0,
|
|
36
|
-
littleEndian = false,
|
|
37
|
-
): BinaryTypes[T] => {
|
|
38
|
-
const view = new DataView(buffer)
|
|
39
|
-
return view[`get${type}`](offset, littleEndian) as BinaryTypes[T]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const encodeText = (text: string) =>
|
|
43
|
-
new Uint8Array(utf8encoder.encode(text)).buffer as ArrayBuffer
|
|
44
|
-
|
|
45
|
-
export const decodeText = (buffer: Parameters<typeof utf8decoder.decode>[0]) =>
|
|
46
|
-
utf8decoder.decode(buffer)
|
|
47
|
-
|
|
48
|
-
export const concat = (...buffers: ArrayBuffer[]) => {
|
|
49
|
-
const totalLength = buffers.reduce(
|
|
50
|
-
(acc, buffer) => acc + buffer.byteLength,
|
|
51
|
-
0,
|
|
52
|
-
)
|
|
53
|
-
const view = new Uint8Array(totalLength)
|
|
54
|
-
let offset = 0
|
|
55
|
-
for (const buffer of buffers) {
|
|
56
|
-
view.set(new Uint8Array(buffer), offset)
|
|
57
|
-
offset += buffer.byteLength
|
|
58
|
-
}
|
|
59
|
-
return view.buffer as ArrayBuffer
|
|
60
|
-
}
|
package/lib/blob.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { ApiBlobMetadata } from './types.ts'
|
|
2
|
-
|
|
3
|
-
export interface ApiBlobInterface {
|
|
4
|
-
readonly metadata: ApiBlobMetadata
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export type Exact<A, B> = [A] extends [B]
|
|
8
|
-
? [B] extends [A]
|
|
9
|
-
? true
|
|
10
|
-
: false
|
|
11
|
-
: false
|
|
12
|
-
|
|
13
|
-
export class ApiBlob implements ApiBlobInterface {
|
|
14
|
-
public readonly metadata: ApiBlobMetadata
|
|
15
|
-
public readonly source: any
|
|
16
|
-
|
|
17
|
-
constructor(
|
|
18
|
-
source: any,
|
|
19
|
-
size = -1,
|
|
20
|
-
type = 'application/octet-stream',
|
|
21
|
-
filename?: string,
|
|
22
|
-
) {
|
|
23
|
-
if (size < -1 || size === 0) throw new Error('Blob size is invalid')
|
|
24
|
-
|
|
25
|
-
this.source = source
|
|
26
|
-
this.metadata = {
|
|
27
|
-
size,
|
|
28
|
-
type,
|
|
29
|
-
filename,
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static from(
|
|
34
|
-
source: any,
|
|
35
|
-
metadata: {
|
|
36
|
-
size?: number
|
|
37
|
-
type?: string
|
|
38
|
-
filename?: string
|
|
39
|
-
} = {},
|
|
40
|
-
) {
|
|
41
|
-
let _source: any = undefined
|
|
42
|
-
|
|
43
|
-
if (source instanceof ReadableStream) {
|
|
44
|
-
_source = source
|
|
45
|
-
} else if ('File' in globalThis && source instanceof globalThis.File) {
|
|
46
|
-
_source = source.stream()
|
|
47
|
-
metadata.size = source.size
|
|
48
|
-
metadata.filename = source.name
|
|
49
|
-
} else if (source instanceof Blob) {
|
|
50
|
-
_source = source.stream()
|
|
51
|
-
metadata.size = source.size
|
|
52
|
-
} else if (typeof source === 'string') {
|
|
53
|
-
const blob = new Blob([source])
|
|
54
|
-
_source = blob.stream()
|
|
55
|
-
metadata.size = blob.size
|
|
56
|
-
metadata.type = metadata.type || 'text/plain'
|
|
57
|
-
} else if (source instanceof ArrayBuffer) {
|
|
58
|
-
const blob = new Blob([source])
|
|
59
|
-
_source = blob.stream()
|
|
60
|
-
metadata.size = blob.size
|
|
61
|
-
} else {
|
|
62
|
-
_source = source
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return new ApiBlob(_source, metadata.size, metadata.type, metadata.filename)
|
|
66
|
-
}
|
|
67
|
-
}
|
package/lib/enums.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export enum ErrorCode {
|
|
2
|
-
ValidationError = 'ValidationError',
|
|
3
|
-
BadRequest = 'BadRequest',
|
|
4
|
-
NotFound = 'NotFound',
|
|
5
|
-
Forbidden = 'Forbidden',
|
|
6
|
-
Unauthorized = 'Unauthorized',
|
|
7
|
-
InternalServerError = 'InternalServerError',
|
|
8
|
-
NotAcceptable = 'NotAcceptable',
|
|
9
|
-
RequestTimeout = 'RequestTimeout',
|
|
10
|
-
GatewayTimeout = 'GatewayTimeout',
|
|
11
|
-
ServiceUnavailable = 'ServiceUnavailable',
|
|
12
|
-
ClientRequestError = 'ClientRequestError',
|
|
13
|
-
ConnectionError = 'ConnectionError',
|
|
14
|
-
}
|
package/lib/format.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import type { ApiBlob } from './blob.ts'
|
|
2
|
-
import type { ApiBlobMetadata, Pattern, Rpc, RpcResponse } from './types.ts'
|
|
3
|
-
|
|
4
|
-
export interface EncodeRpcContext {
|
|
5
|
-
getStream: (id: number) => any
|
|
6
|
-
addStream: (blob: ApiBlob) => { id: number; metadata: ApiBlobMetadata }
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface DecodeRpcContext {
|
|
10
|
-
getStream: (id: number) => any
|
|
11
|
-
addStream: (id: number, metadata: ApiBlobMetadata) => any
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface BaseClientDecoder {
|
|
15
|
-
decode(buffer: ArrayBuffer): any
|
|
16
|
-
decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): RpcResponse
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface BaseClientEncoder {
|
|
20
|
-
encode(data: any): ArrayBuffer
|
|
21
|
-
encodeRpc(rpc: Rpc, context: EncodeRpcContext): ArrayBuffer
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export abstract class BaseClientFormat
|
|
25
|
-
implements BaseClientDecoder, BaseClientEncoder
|
|
26
|
-
{
|
|
27
|
-
abstract contentType: string
|
|
28
|
-
|
|
29
|
-
abstract encode(data: any): ArrayBuffer
|
|
30
|
-
abstract encodeRpc(rpc: Rpc, context: EncodeRpcContext): ArrayBuffer
|
|
31
|
-
abstract decode(buffer: ArrayBuffer): any
|
|
32
|
-
abstract decodeRpc(
|
|
33
|
-
buffer: ArrayBuffer,
|
|
34
|
-
context: DecodeRpcContext,
|
|
35
|
-
): RpcResponse
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface BaseServerDecoder {
|
|
39
|
-
accept: Pattern[]
|
|
40
|
-
decode(buffer: ArrayBuffer): any
|
|
41
|
-
decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): Rpc
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface BaseServerEncoder {
|
|
45
|
-
contentType: string
|
|
46
|
-
encode(data: any): ArrayBuffer
|
|
47
|
-
encodeRpc(rpc: RpcResponse, context: EncodeRpcContext): ArrayBuffer
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export abstract class BaseServerFormat
|
|
51
|
-
implements BaseServerDecoder, BaseServerEncoder
|
|
52
|
-
{
|
|
53
|
-
abstract accept: Pattern[]
|
|
54
|
-
abstract contentType: string
|
|
55
|
-
|
|
56
|
-
abstract encode(data: any): ArrayBuffer
|
|
57
|
-
abstract encodeRpc(rpc: RpcResponse, context: EncodeRpcContext): ArrayBuffer
|
|
58
|
-
abstract decode(buffer: ArrayBuffer): any
|
|
59
|
-
abstract decodeRpc(buffer: ArrayBuffer, context: DecodeRpcContext): Rpc
|
|
60
|
-
}
|
package/lib/protocol.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export const MessageType = Object.freeze({
|
|
2
|
-
// Common
|
|
3
|
-
Event: 10,
|
|
4
|
-
Rpc: 11,
|
|
5
|
-
RpcBatch: 12,
|
|
6
|
-
RpcAbort: 14,
|
|
7
|
-
RpcSubscription: 15,
|
|
8
|
-
|
|
9
|
-
// Client streams
|
|
10
|
-
UpStreamAbort: 30,
|
|
11
|
-
UpStreamPush: 31,
|
|
12
|
-
UpStreamPull: 32,
|
|
13
|
-
UpStreamEnd: 33,
|
|
14
|
-
|
|
15
|
-
// Client subsctiption
|
|
16
|
-
ClientUnsubscribe: 34,
|
|
17
|
-
|
|
18
|
-
// Server streams
|
|
19
|
-
DownStreamAbort: 50,
|
|
20
|
-
DownStreamPull: 51,
|
|
21
|
-
DownStreamPush: 52,
|
|
22
|
-
DownStreamEnd: 53,
|
|
23
|
-
|
|
24
|
-
// Server subsctiption
|
|
25
|
-
ServerUnsubscribe: 54,
|
|
26
|
-
ServerSubscriptionEvent: 55,
|
|
27
|
-
} as const)
|
|
28
|
-
|
|
29
|
-
export type MessageType = (typeof MessageType)[keyof typeof MessageType]
|
|
30
|
-
|
|
31
|
-
export const MessageTypeName = Object.fromEntries(
|
|
32
|
-
Object.entries(MessageType).map(([k, v]) => [v, k]),
|
|
33
|
-
)
|
|
34
|
-
export type MessageTypeName = keyof typeof MessageType
|
|
35
|
-
|
|
36
|
-
// TODO: Should it be hardcoded ??
|
|
37
|
-
export enum TransportType {
|
|
38
|
-
WS = 'WS',
|
|
39
|
-
HTTP = 'HTTP',
|
|
40
|
-
}
|