@based/functions
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -5,2 +5,3 @@ import { parseQuery } from '@saulx/utils'; | ||
import { BasedFunctionClient } from './client.js'; | ||
import { StreamPayload } from './functions.js'; | ||
export type WebSocketSession = { | ||
@@ -22,2 +23,5 @@ state?: any; | ||
}>; | ||
streams?: { | ||
[reqId: string]: StreamPayload; | ||
}; | ||
headers: { | ||
@@ -24,0 +28,0 @@ [key: string]: string; |
@@ -36,2 +36,4 @@ import type { Required } from 'utility-types'; | ||
extension?: string; | ||
fn?: string; | ||
seqId?: number; | ||
}; | ||
@@ -38,0 +40,0 @@ export type BasedStreamFunction<P = any, K = any> = BasedFunction<StreamPayload<P>, K>; |
@@ -5,6 +5,7 @@ /// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { Duplex, Readable } from 'stream'; | ||
import util from 'util'; | ||
import { Duplex, Readable } from "node:stream"; | ||
import util from "node:util"; | ||
export declare class BasedDataStream extends Duplex { | ||
size: number; | ||
paused: boolean; | ||
receivedBytes: number; | ||
@@ -11,0 +12,0 @@ progessTimer: NodeJS.Timeout; |
@@ -1,6 +0,6 @@ | ||
import { Duplex, Readable } from 'stream'; | ||
import util from 'util'; | ||
// prob want to move this to based functions | ||
import { Duplex, Readable } from "node:stream"; | ||
import util from "node:util"; | ||
export class BasedDataStream extends Duplex { | ||
size = 0; | ||
paused = false; | ||
receivedBytes = 0; | ||
@@ -11,3 +11,9 @@ progessTimer; | ||
this.size = size; | ||
this.emit('progress', 0); | ||
this.on("pause", () => { | ||
this.paused = true; | ||
}); | ||
this.on("resume", () => { | ||
this.paused = false; | ||
}); | ||
this.emit("progress", 0); | ||
} | ||
@@ -21,3 +27,3 @@ _read() { } | ||
const progress = this.receivedBytes / this.size; | ||
this.emit('progress', progress); | ||
this.emit("progress", progress); | ||
this.progessTimer = null; | ||
@@ -39,3 +45,3 @@ }, 200); | ||
} | ||
this.emit('progress', 1); | ||
this.emit("progress", 1); | ||
this.push(null); | ||
@@ -46,4 +52,4 @@ } | ||
const rb = this.receivedBytes < 1000 | ||
? Math.round(this.receivedBytes / 1024) + 'kb' | ||
: Math.round(this.receivedBytes / 1024 / 1024) + 'mb'; | ||
? Math.round(this.receivedBytes / 1024) + "kb" | ||
: Math.round(this.receivedBytes / 1024 / 1024) + "mb"; | ||
return `[BasedStream ${~~((this.receivedBytes / this.size) * | ||
@@ -50,0 +56,0 @@ 100)}% ${rb}]`; |
{ | ||
"name": "@based/functions", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"license": "MIT", | ||
@@ -15,3 +15,3 @@ "type": "module", | ||
"utility-types": "^3.10.0", | ||
"@saulx/utils": "^4.1.0" | ||
"@saulx/utils": "^5.0.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "devDependencies": { |
@@ -5,2 +5,3 @@ import { parseQuery } from '@saulx/utils' | ||
import { BasedFunctionClient } from './client.js' | ||
import { StreamPayload } from './functions.js' | ||
@@ -24,2 +25,3 @@ export type WebSocketSession = { | ||
}> | ||
streams?: { [reqId: string]: StreamPayload } | ||
headers: { [key: string]: string } | ||
@@ -26,0 +28,0 @@ unauthorizedChannels?: Set<{ |
@@ -70,2 +70,4 @@ import type { Required } from 'utility-types' | ||
extension?: string | ||
fn?: string | ||
seqId?: number | ||
} | ||
@@ -72,0 +74,0 @@ |
@@ -1,31 +0,40 @@ | ||
import { Duplex, Readable } from 'stream' | ||
import util from 'util' | ||
import { Duplex, Readable } from "node:stream"; | ||
import util from "node:util"; | ||
// prob want to move this to based functions | ||
export class BasedDataStream extends Duplex { | ||
public size: number = 0 | ||
public receivedBytes: number = 0 | ||
public progessTimer: NodeJS.Timeout | ||
public size: number = 0; | ||
public paused: boolean = false; | ||
public receivedBytes: number = 0; | ||
public progessTimer: NodeJS.Timeout; | ||
constructor(size: number) { | ||
super() | ||
this.size = size | ||
this.emit('progress', 0) | ||
super(); | ||
this.size = size; | ||
this.on("pause", () => { | ||
this.paused = true; | ||
}); | ||
this.on("resume", () => { | ||
this.paused = false; | ||
}); | ||
this.emit("progress", 0); | ||
} | ||
override _read() { } | ||
override _read() {} | ||
override _write(chunk, encoding, callback) { | ||
this.receivedBytes += chunk.byteLength | ||
this.receivedBytes += chunk.byteLength; | ||
if (this.size && this.size > 20000) { | ||
if (!this.progessTimer) { | ||
this.progessTimer = setTimeout(() => { | ||
const progress = this.receivedBytes / this.size | ||
this.emit('progress', progress) | ||
this.progessTimer = null | ||
}, 200) | ||
const progress = this.receivedBytes / this.size; | ||
this.emit("progress", progress); | ||
this.progessTimer = null; | ||
}, 200); | ||
} | ||
} | ||
this.push(Buffer.from(chunk, encoding)) | ||
callback() | ||
this.push(Buffer.from(chunk, encoding)); | ||
callback(); | ||
} | ||
@@ -35,11 +44,11 @@ | ||
if (!this.size) { | ||
this.size = this.receivedBytes | ||
this.size = this.receivedBytes; | ||
} | ||
this.receivedBytes = this.size | ||
this.receivedBytes = this.size; | ||
if (this.progessTimer) { | ||
clearTimeout(this.progessTimer) | ||
this.progessTimer = null | ||
clearTimeout(this.progessTimer); | ||
this.progessTimer = null; | ||
} | ||
this.emit('progress', 1) | ||
this.push(null) | ||
this.emit("progress", 1); | ||
this.push(null); | ||
} | ||
@@ -51,4 +60,4 @@ | ||
this.receivedBytes < 1000 | ||
? Math.round(this.receivedBytes / 1024) + 'kb' | ||
: Math.round(this.receivedBytes / 1024 / 1024) + 'mb' | ||
? Math.round(this.receivedBytes / 1024) + "kb" | ||
: Math.round(this.receivedBytes / 1024 / 1024) + "mb"; | ||
@@ -58,5 +67,5 @@ return `[BasedStream ${~~( | ||
100 | ||
)}% ${rb}]` | ||
)}% ${rb}]`; | ||
} else { | ||
return `[BasedStream]` | ||
return `[BasedStream]`; | ||
} | ||
@@ -68,27 +77,27 @@ } | ||
export type StreamFunctionContents<F = Buffer | ArrayBuffer | string> = { | ||
contents: F | ||
payload?: any | ||
mimeType?: string | ||
fileName?: string | ||
} | ||
contents: F; | ||
payload?: any; | ||
mimeType?: string; | ||
fileName?: string; | ||
}; | ||
export type StreamFunctionStream = | ||
| { | ||
contents: Readable | Duplex | ||
payload?: any | ||
size: number | ||
mimeType?: string | ||
fileName?: string | ||
extension?: string | ||
} | ||
contents: Readable | Duplex; | ||
payload?: any; | ||
size: number; | ||
mimeType?: string; | ||
fileName?: string; | ||
extension?: string; | ||
} | ||
| { | ||
contents: BasedDataStream | ||
payload?: any | ||
size?: number | ||
mimeType?: string | ||
fileName?: string | ||
extension?: string | ||
} | ||
contents: BasedDataStream; | ||
payload?: any; | ||
size?: number; | ||
mimeType?: string; | ||
fileName?: string; | ||
extension?: string; | ||
}; | ||
export type StreamFunctionOpts = StreamFunctionContents | StreamFunctionStream | ||
export type StreamFunctionOpts = StreamFunctionContents | StreamFunctionStream; | ||
@@ -98,3 +107,3 @@ export const isStreamFunctionOpts = ( | ||
): opts is StreamFunctionStream => { | ||
return opts.contents instanceof Duplex || opts.contents instanceof Readable | ||
} | ||
return opts.contents instanceof Duplex || opts.contents instanceof Readable; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94421
2389
+ Added@saulx/utils@5.0.0(transitive)
- Removed@saulx/utils@4.3.2(transitive)
Updated@saulx/utils@^5.0.0