@based/server
Advanced tools
Comparing version 7.1.0 to 8.0.0
@@ -1,4 +0,4 @@ | ||
import { BasedServer } from '../server.js'; | ||
import { Context } from '@based/functions'; | ||
import { BasedErrorCode, ErrorPayload, BasedErrorData } from '@based/errors'; | ||
import { BasedServer } from "../server.js"; | ||
import { Context } from "@based/functions"; | ||
import { BasedErrorCode, ErrorPayload, BasedErrorData } from "@based/errors"; | ||
export declare function createError<T extends BasedErrorCode>(server: BasedServer, context: Context, code: T, payload: ErrorPayload[T]): BasedErrorData<T>; |
@@ -1,19 +0,21 @@ | ||
import { createErrorData, } from '@based/errors'; | ||
// export * from './types.js' | ||
import { createErrorData, } from "@based/errors"; | ||
export function createError(server, context, code, payload) { | ||
const errorData = createErrorData(code, payload); | ||
if ('requestId' in payload) { | ||
if ("streamRequestId" in payload) { | ||
errorData.streamRequestId = payload.streamRequestId; | ||
} | ||
else if ("requestId" in payload) { | ||
errorData.requestId = payload.requestId; | ||
} | ||
else if ('observableId' in payload) { | ||
else if ("observableId" in payload) { | ||
errorData.observableId = payload.observableId; | ||
} | ||
else if ('channelId' in payload) { | ||
else if ("channelId" in payload) { | ||
errorData.channelId = payload.channelId; | ||
} | ||
if ('err' in payload) { | ||
server.emit('error', context, errorData, payload.err); | ||
if ("err" in payload) { | ||
server.emit("error", context, errorData, payload.err); | ||
} | ||
else { | ||
server.emit('error', context, errorData); | ||
server.emit("error", context, errorData); | ||
} | ||
@@ -20,0 +22,0 @@ return errorData; |
@@ -30,3 +30,2 @@ import zlib from 'node:zlib'; | ||
return decoder.decode(data); | ||
// } else if (contentType === 'multipart/form-data') { | ||
} | ||
@@ -33,0 +32,0 @@ else if (contentType === 'application/x-www-form-urlencoded') { |
@@ -30,3 +30,3 @@ import uws from '@based/uws'; | ||
app.ws('/*', { | ||
maxPayloadLength: 1024 * 1024 * 30, // 20 mb max payload | ||
maxPayloadLength: 1024 * 1024 * 30, // 30 mb max payload | ||
idleTimeout: 100, | ||
@@ -65,2 +65,8 @@ maxBackpressure: wsOptions.maxBackpressureSize, | ||
const session = ws.getUserData(); | ||
if (session.streams) { | ||
for (const key in session.streams) { | ||
session.streams[key].stream.destroy(); | ||
delete session.streams[key]; | ||
} | ||
} | ||
session.obs.forEach((id) => { | ||
@@ -80,2 +86,3 @@ if (unsubscribeWsIgnoreClient(server, id, session.c)) { | ||
drain: () => { | ||
// console.log('DRAIN?') | ||
// lets handle drain efficiently (or more efficiently at least) | ||
@@ -82,0 +89,0 @@ // call client.drain can be much more efficient |
@@ -109,7 +109,7 @@ import { readUint8, decodeName, decodePayload, parsePayload, } from '../../protocol.js'; | ||
export const unsubscribeChannelMessage = (arr, start, _len, _isDeflate, ctx, server) => { | ||
// | 4 header | 8 id | | ||
// | 4 header | 1 subType | 8 id | | ||
if (!ctx.session) { | ||
return false; | ||
} | ||
const id = readUint8(arr, start + 4, 8); | ||
const id = readUint8(arr, start + 5, 8); | ||
if (!id) { | ||
@@ -116,0 +116,0 @@ return false; |
@@ -9,3 +9,2 @@ import { readUint8, decodeName, decodePayload, encodeFunctionResponse, valueToBuffer, parsePayload, } from '../../protocol.js'; | ||
import { readStream } from '@saulx/utils'; | ||
// combine authorize with installfn AuthorizeAndInstall | ||
const sendFunction = (route, spec, server, ctx, payload, requestId) => { | ||
@@ -39,3 +38,3 @@ if (spec.relay) { | ||
.then(async (v) => { | ||
// TODO: allow chunked reply! | ||
// TODO: allow chunked REPLY! | ||
if (v && (v instanceof Duplex || v instanceof Readable)) { | ||
@@ -42,0 +41,0 @@ v = await readStream(v); |
@@ -1,3 +0,3 @@ | ||
import { BasedServer } from '../../server.js'; | ||
import { WebSocketSession, Context } from '@based/functions'; | ||
import { BasedServer } from "../../server.js"; | ||
import { WebSocketSession, Context } from "@based/functions"; | ||
export declare const message: (server: BasedServer, ctx: Context<WebSocketSession>, msg: ArrayBuffer, isBinary: boolean) => void; |
@@ -1,10 +0,11 @@ | ||
import { decodeHeader, readUint8 } from '../../protocol.js'; | ||
import { functionMessage } from './function.js'; | ||
import { subscribeMessage, unsubscribeMessage } from './query.js'; | ||
import { authMessage } from './auth.js'; | ||
import { getMessage } from './get.js'; | ||
import { createError } from '../../error/index.js'; | ||
import { channelSubscribeMessage, unsubscribeChannelMessage, } from './channelSubscribe.js'; | ||
import { channelPublishMessage } from './channelPublish.js'; | ||
import { BasedErrorCode } from '@based/errors'; | ||
import { decodeHeader, readUint8 } from "../../protocol.js"; | ||
import { functionMessage } from "./function.js"; | ||
import { subscribeMessage, unsubscribeMessage } from "./query.js"; | ||
import { authMessage } from "./auth.js"; | ||
import { getMessage } from "./get.js"; | ||
import { createError } from "../../error/index.js"; | ||
import { channelSubscribeMessage, unsubscribeChannelMessage, } from "./channelSubscribe.js"; | ||
import { channelPublishMessage } from "./channelPublish.js"; | ||
import { receiveChunkStream, registerStream } from "./stream.js"; | ||
import { BasedErrorCode } from "@based/errors"; | ||
const reader = (server, ctx, arr, start) => { | ||
@@ -44,6 +45,23 @@ const { len, isDeflate, type } = decodeHeader(readUint8(arr, start, 4)); | ||
} | ||
// type 7 = channelUnsubscribe | ||
if (type === 7 && | ||
unsubscribeChannelMessage(arr, start, len, isDeflate, ctx, server)) { | ||
return next; | ||
// type 7.x = subType | ||
if (type === 7) { | ||
const subType = readUint8(arr, start + 4, 1); | ||
// type 7.0 = channelUnsubscribe | ||
if (subType === 0) { | ||
if (unsubscribeChannelMessage(arr, start, len, isDeflate, ctx, server)) { | ||
return next; | ||
} | ||
} | ||
// type 7.1 = register stream | ||
if (subType === 1) { | ||
if (registerStream(arr, start, len, isDeflate, ctx, server)) { | ||
return next; | ||
} | ||
} | ||
// type 7.2 = chunk | ||
if (subType === 2) { | ||
if (receiveChunkStream(arr, start, len, isDeflate, ctx, server)) { | ||
return next; | ||
} | ||
} | ||
} | ||
@@ -50,0 +68,0 @@ return next; |
import { BasedServer } from './server.js'; | ||
import { Context, BasedRoute, BasedFunctionConfig } from '@based/functions'; | ||
export declare const installFn: <R extends BasedRoute>(server: BasedServer, ctx: Context, route: R, id?: number) => Promise<BasedFunctionConfig<R["type"]>>; | ||
export declare const installFn: <R extends BasedRoute>(server: BasedServer, ctx: Context, route: R, id?: number) => Promise<null | BasedFunctionConfig<R['type']>>; |
@@ -16,2 +16,4 @@ /// <reference types="node" resolution-mode="require"/> | ||
export declare const encodeFunctionResponse: (id: number, buffer: Buffer) => Uint8Array; | ||
export declare const encodeStreamFunctionResponse: (id: number, buffer: Buffer) => Uint8Array; | ||
export declare const encodeStreamFunctionChunkResponse: (id: number, seqId: number, code?: number, maxChunkSize?: number) => Uint8Array; | ||
export declare const encodeGetResponse: (id: number) => Uint8Array; | ||
@@ -18,0 +20,0 @@ export declare const updateId: (payload: Uint8Array, id: number) => Uint8Array; |
@@ -122,2 +122,53 @@ import zlib from 'node:zlib'; | ||
}; | ||
export const encodeStreamFunctionResponse = (id, buffer) => { | ||
// Type 7 | ||
// | 4 header | 1 subType | 3 id | * payload | | ||
let isDeflate = false; | ||
// TODO: implement for streams! | ||
// chunk isChunk | isNotCunk | isLastchunk 0|1 (use 1 bye for now?) | ||
const chunks = 1; | ||
if (buffer.length > COMPRESS_FROM_BYTES) { | ||
isDeflate = true; | ||
buffer = zlib.deflateRawSync(buffer, {}); | ||
} | ||
if (chunks === 1) { | ||
const headerSize = 4; | ||
const idSize = 3; | ||
const subTypeSize = 1; | ||
const msgSize = idSize + subTypeSize + buffer.length; | ||
const header = encodeHeader(7, isDeflate, msgSize); | ||
// not very nessecary but ok | ||
const array = new Uint8Array(headerSize + msgSize); | ||
storeUint8(array, header, 0, 4); | ||
storeUint8(array, 1, 4, 1); | ||
storeUint8(array, id, 5, 3); | ||
if (buffer.length) { | ||
array.set(buffer, 8); | ||
} | ||
return array; | ||
} | ||
else { | ||
console.warn('chunk not implemented yet'); | ||
return new Uint8Array(0); | ||
} | ||
}; | ||
export const encodeStreamFunctionChunkResponse = (id, seqId, code = 0, maxChunkSize = 0) => { | ||
// Type 7.2 | ||
// | 4 header | 1 subType | 3 id | 1 seqId | 1 code | maxChunkSize? | ||
let msgSize = 6; | ||
if (maxChunkSize) { | ||
msgSize += 3; | ||
} | ||
const header = encodeHeader(7, false, msgSize); | ||
const array = new Uint8Array(4 + msgSize); | ||
storeUint8(array, header, 0, 4); | ||
storeUint8(array, 2, 4, 1); | ||
storeUint8(array, id, 5, 3); | ||
storeUint8(array, seqId, 8, 1); | ||
storeUint8(array, code, 9, 1); | ||
if (maxChunkSize) { | ||
storeUint8(array, maxChunkSize, 10, 3); | ||
} | ||
return array; | ||
}; | ||
export const encodeGetResponse = (id) => { | ||
@@ -124,0 +175,0 @@ // Type 4 |
@@ -1,6 +0,6 @@ | ||
import type { BasedServer } from './server.js'; | ||
import uws from '@based/uws'; | ||
import { HttpSession, WebSocketSession, Context } from '@based/functions'; | ||
import type { BasedServer } from "./server.js"; | ||
import uws from "@based/uws"; | ||
import { HttpSession, WebSocketSession, Context } from "@based/functions"; | ||
export declare const rateLimitRequest: (server: BasedServer, ctx: Context<WebSocketSession | HttpSession>, tokens: number, max: number) => boolean; | ||
export declare const endRateLimitHttp: (res: uws.HttpResponse) => void; | ||
export declare const blockIncomingRequest: (server: BasedServer, ip: string, res: uws.HttpResponse, req: uws.HttpRequest, max: number, tokens: number) => boolean; |
@@ -1,2 +0,2 @@ | ||
import { BasedErrorCode } from '@based/errors'; | ||
import { BasedErrorCode } from "@based/errors"; | ||
var IsBlocked; | ||
@@ -25,3 +25,3 @@ (function (IsBlocked) { | ||
if (blockedEvents.size) { | ||
server.emit('error', server.client.ctx, { | ||
server.emit("error", server.client.ctx, { | ||
code: BasedErrorCode.Block, | ||
@@ -66,3 +66,3 @@ blockedEvents, | ||
if (code === 2) { | ||
server.emit('error', ctx, { code: BasedErrorCode.RateLimit }); | ||
server.emit("error", ctx, { code: BasedErrorCode.RateLimit }); | ||
} | ||
@@ -73,3 +73,3 @@ return true; | ||
res.cork(() => { | ||
res.writeStatus('429 Too Many Requests'); | ||
res.writeStatus("429 Too Many Requests"); | ||
res.end(); | ||
@@ -94,7 +94,7 @@ }); | ||
if (code === 2) { | ||
server.emit('error', { | ||
server.emit("error", { | ||
session: { | ||
origin: req.getHeader('origin'), | ||
origin: req.getHeader("origin"), | ||
headers: {}, | ||
ua: req.getHeader('user-agent'), | ||
ua: req.getHeader("user-agent"), | ||
ip, | ||
@@ -101,0 +101,0 @@ }, |
import { Context, BasedRoute, BasedFunctionTypes } from '@based/functions'; | ||
import { BasedServer } from './server.js'; | ||
export declare const verifyRoute: <T extends BasedFunctionTypes>(server: BasedServer, ctx: Context, type: T, route: BasedRoute | null, name: string, id?: number) => BasedRoute<T>; | ||
export declare const verifyRoute: <T extends BasedFunctionTypes>(server: BasedServer, ctx: Context, type: T, route: BasedRoute | null, name: string, id?: number) => BasedRoute<T> | null; |
{ | ||
"name": "@based/server", | ||
"version": "7.1.0", | ||
"version": "8.0.0", | ||
"license": "MIT", | ||
@@ -22,3 +22,3 @@ "main": "./dist/index.js", | ||
"@saulx/hash": "^3.0.0", | ||
"@saulx/utils": "^4.0.1", | ||
"@saulx/utils": "^4.3.1", | ||
"picocolors": "^1.0.0", | ||
@@ -29,3 +29,3 @@ "jsonexport": "^3.2.0", | ||
"@based/functions": "^3.0.1", | ||
"@based/errors": "^1.0.0" | ||
"@based/errors": "^1.2.0" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
401413
246
5800
Updated@based/errors@^1.2.0
Updated@saulx/utils@^4.3.1