Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/linejs/base/push/conn.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PushProtocol } from "./protocol.ts";
import { BaseClient } from "../core/mod.ts";
import {
LegyH2PingFrame,
Expand All @@ -16,7 +17,7 @@ export class Conn {
cacheData: Uint8Array = new Uint8Array(0);
notFinPayloads: Record<number, Uint8Array> = {};
reqStream?: ReadableStreamWriter<Uint8Array> & { abort: AbortController };
resStream?: ReadableStream<Uint8Array>;
resStream?: ReadableStream<Uint8Array<ArrayBufferLike>>;
private _lastSendTime = 0;
private _closed = false;

Expand All @@ -39,7 +40,7 @@ export class Conn {

const chunks: Uint8Array[] = [];
const encoder = new TextEncoder();
const stream = new ReadableStream<Uint8Array>({
const stream = new ReadableStream<Uint8Array<ArrayBufferLike>>({
start(c) {
controller = c;
},
Expand Down Expand Up @@ -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);
}

Expand Down
22 changes: 4 additions & 18 deletions packages/linejs/base/push/connManager.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -31,6 +31,7 @@ function gen_m(ss = [1, 3, 5, 6, 8, 9, 10]) {
return i;
}


export interface ReadableStreamWriter<T> {
stream: ReadableStream<T>;
enqueue(chunk: T): void;
Expand Down Expand Up @@ -150,24 +151,14 @@ 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;
await (_conn.new(host, port, `/PUSH/1/subs?m=${m}`, tosendHeaders));
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,
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions packages/linejs/base/push/protocol.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading