diff --git a/packages/linejs/base/push/conn.ts b/packages/linejs/base/push/conn.ts index 7797b00..edc4eaa 100644 --- a/packages/linejs/base/push/conn.ts +++ b/packages/linejs/base/push/conn.ts @@ -1,3 +1,4 @@ +import { PushProtocol } from "./protocol.ts"; import { BaseClient } from "../core/mod.ts"; import { LegyH2PingFrame, @@ -16,7 +17,7 @@ export class Conn { cacheData: Uint8Array = new Uint8Array(0); notFinPayloads: Record = {}; reqStream?: ReadableStreamWriter & { abort: AbortController }; - resStream?: ReadableStream; + resStream?: ReadableStream>; private _lastSendTime = 0; private _closed = false; @@ -39,7 +40,7 @@ export class Conn { const chunks: Uint8Array[] = []; const encoder = new TextEncoder(); - const stream = new ReadableStream({ + const stream = new ReadableStream>({ start(c) { controller = c; }, @@ -129,7 +130,7 @@ export class Conn { } async writeRequest(requestType: number, data: Uint8Array) { - const d = this.manager.buildRequest(requestType, data); + const d = PushProtocol.buildRequest(requestType, data); await this.writeByte(d); } diff --git a/packages/linejs/base/push/connManager.ts b/packages/linejs/base/push/connManager.ts index 82ebc64..2a275b1 100644 --- a/packages/linejs/base/push/connManager.ts +++ b/packages/linejs/base/push/connManager.ts @@ -1,3 +1,4 @@ +import { PushProtocol } from "./protocol.ts"; import { LegyH2PushFrame } from "./connData.ts"; import { Conn } from "./conn.ts"; import { BaseClient } from "@evex/linejs/base"; @@ -6,7 +7,6 @@ import { TCompactProtocol } from "npm:thrift@^0.20.0"; import { TMoreCompactProtocol } from "../thrift/readwrite/tmc.ts"; -// GOMI: import { PartialDeep, SquareService_fetchMyEvents_args as gen_SquareService_fetchMyEvents_args, @@ -31,6 +31,7 @@ function gen_m(ss = [1, 3, 5, 6, 8, 9, 10]) { return i; } + export interface ReadableStreamWriter { stream: ReadableStream; enqueue(chunk: T): void; @@ -150,7 +151,7 @@ export class ConnManager { .getHeader(); tosendHeaders["content-type"] = "application/octet-stream"; tosendHeaders["accept"] = "application/octet-stream"; - const m = gen_m(initServices); + const m = PushProtocol.genServiceBitmask(initServices); this.log(`Using \`m=${m}\` on \`/PUSH\``); const host = this.client.request.endpoint; const port = 443; @@ -158,16 +159,6 @@ export class ConnManager { return _conn; } - buildRequest(service: number, data: Uint8Array): Uint8Array { - const len = data.length; - const out = new Uint8Array(2 + 1 + len); - out[0] = (len >> 8) & 0xff; - out[1] = len & 0xff; - out[2] = service & 0xff; - out.set(data, 3); - return out; - } - async buildAndSendSignOnRequest( conn: Conn, serviceType: number, @@ -203,12 +194,7 @@ export class ConnManager { ); methodName = "sync"; } - const header = new Uint8Array(2 + 1 + 1 + 2 + req.length); - header.set(idBuf, 0); - header[2] = serviceType & 0xff; - header[3] = 0; - header[4] = (req.length >> 8) & 0xff; - header[5] = req.length & 0xff; + const header = PushProtocol.buildSignOnHeader(id, serviceType, req.length); header.set(req, 6); this.SignOnRequests[id] = [serviceType, methodName, null]; this.log( diff --git a/packages/linejs/base/push/protocol.ts b/packages/linejs/base/push/protocol.ts new file mode 100644 index 0000000..a2a7122 --- /dev/null +++ b/packages/linejs/base/push/protocol.ts @@ -0,0 +1,32 @@ +export class PushProtocol { + static genServiceBitmask(ss: number[] = [1, 3, 5, 6, 8, 9, 10]): number { + let i = 0; + for (const s of ss) i |= 1 << (s - 1); + return i; + } + + static buildRequest(service: number, data: Uint8Array): Uint8Array { + const len = data.length; + const out = new Uint8Array(2 + 1 + len); + out[0] = (len >> 8) & 0xff; + out[1] = len & 0xff; + out[2] = service & 0xff; + out.set(data, 3); + return out; + } + + static buildSignOnHeader( + id: number, + serviceType: number, + payloadLength: number, + ): Uint8Array { + const header = new Uint8Array(2 + 1 + 1 + 2); + header[0] = (id >> 8) & 0xff; + header[1] = id & 0xff; + header[2] = serviceType & 0xff; + header[3] = 0; + header[4] = (payloadLength >> 8) & 0xff; + header[5] = payloadLength & 0xff; + return header; + } +}