minitel-cloudflare-worker
Advanced tools
Comparing version 1.1.3 to 2.0.0-beta0
declare const _default: { | ||
fetch(request: Request): Promise<Response | undefined>; | ||
fetch(request: Request, _: {}, ctx: ExecutionContext): Promise<Response | undefined>; | ||
}; | ||
export default _default; |
@@ -1,10 +0,17 @@ | ||
import { Duplex } from 'node:stream'; | ||
import { Duplex, DuplexOptions } from 'node:stream'; | ||
interface MinipaviHandlerOptions { | ||
version?: string; | ||
providePavi?: boolean; | ||
provideDirectUrl?: boolean; | ||
provideUrlParams?: boolean; | ||
} | ||
export declare class DuplexBridge extends Duplex { | ||
ws: WebSocket; | ||
constructor(ws: WebSocket, opts?: DuplexOptions); | ||
end(cb?: () => void): this; | ||
_write(chunk: any, bufferEncoding: BufferEncoding, callback: (err: Error | null | undefined) => void): void; | ||
_read(size?: number): void; | ||
} | ||
export declare function createMinipaviHandler(minitelFactory: (ws: Duplex, req: Request) => any, options?: MinipaviHandlerOptions): { | ||
fetch(request: Request): Promise<Response | undefined>; | ||
fetch(request: Request, _: {}, ctx: ExecutionContext): Promise<Response | undefined>; | ||
}; | ||
export {}; |
@@ -33,2 +33,33 @@ import { z } from 'zod'; | ||
}); | ||
export class DuplexBridge extends Duplex { | ||
ws; | ||
constructor(ws, opts) { | ||
super(opts); | ||
this.ws = ws; | ||
this.ws.addEventListener('close', function () { | ||
this.emit('close'); | ||
}.bind(this)); | ||
this.ws.addEventListener('message', function (msg) { | ||
this.push(msg.data); | ||
}.bind(this)); | ||
} | ||
end(cb) { | ||
this.ws.close(); | ||
if (cb) | ||
cb(); | ||
return this; | ||
} | ||
_write(chunk, bufferEncoding, callback) { | ||
let err; | ||
try { | ||
this.ws.send(chunk.toString()); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
err = e; | ||
} | ||
callback(err); | ||
} | ||
_read(size) { } | ||
} | ||
export function createMinipaviHandler(minitelFactory, options = {}) { | ||
@@ -38,7 +69,7 @@ const fullOptions = { | ||
providePavi: false, | ||
provideDirectUrl: false, | ||
provideUrlParams: false, | ||
...options, | ||
}; | ||
return { | ||
async fetch(request) { | ||
async fetch(request, _, ctx) { | ||
const reqUrl = new URL(request.url); | ||
@@ -53,11 +84,7 @@ const upgradeHeader = request.headers.get('Upgrade'); | ||
const server = webSocketPair[1]; | ||
(async () => { | ||
server.accept(); | ||
const stream = new Duplex(); | ||
server.addEventListener('message', (event) => stream.write(event.data)); | ||
stream.on('data', (data) => server.send(data)); | ||
server.addEventListener('close', () => stream.end()); | ||
stream.on('close', () => server.close()); | ||
server.addEventListener('open', () => minitelFactory(stream, request)); | ||
})(); | ||
server.accept(); | ||
const stream = new DuplexBridge(server); | ||
setTimeout(() => minitelFactory(stream, request), 1); | ||
const streamEnd = new Promise((r) => stream.on('end', () => r())); | ||
ctx.waitUntil(streamEnd); | ||
return new Response(null, { | ||
@@ -80,5 +107,5 @@ status: 101, | ||
if (fullOptions.providePavi) | ||
newParams.append('pavi', JSON.stringify(data.PAVI)); | ||
if (fullOptions.provideDirectUrl && 'DIRECTURL' in data) | ||
newParams.append('directUrl', JSON.stringify(data.DIRECTURL)); | ||
newParams.append('pavi', JSON.stringify(data?.PAVI)); | ||
if (fullOptions.provideUrlParams && 'URLPARAMS' in (data || {})) | ||
newParams.append('urlParams', JSON.stringify(data?.URLPARAMS)); | ||
return new Response(JSON.stringify({ | ||
@@ -95,7 +122,7 @@ version: fullOptions.version, | ||
key: 'Same host <https://npmjs.com/packages/minitel-minipavi>', | ||
host: reqUrl.hostname, | ||
host: `ssl://${reqUrl.hostname}:443`, | ||
path: `/websocket${newParams.toString()}`, | ||
echo: 'off', | ||
case: 'lower', | ||
proto: 'wss', | ||
proto: '', | ||
}, | ||
@@ -102,0 +129,0 @@ }, |
{ | ||
"name": "minitel-cloudflare-worker", | ||
"version": "1.1.3", | ||
"version": "2.0.0-beta0", | ||
"description": "A cloudflare worker for access through MiniPAVI", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { z } from 'zod'; | ||
import { Duplex } from 'node:stream'; | ||
import { Duplex, DuplexOptions } from 'node:stream'; | ||
@@ -41,2 +41,42 @@ interface MinipaviHandlerOptions { | ||
export class DuplexBridge extends Duplex { | ||
ws: WebSocket; | ||
constructor(ws: WebSocket, opts?: DuplexOptions) { | ||
super(opts); | ||
this.ws = ws; | ||
this.ws.addEventListener( | ||
'close', | ||
function (this: DuplexBridge) { | ||
this.emit('close'); | ||
}.bind(this), | ||
); | ||
this.ws.addEventListener( | ||
'message', | ||
function (this: DuplexBridge, msg: MessageEvent) { | ||
this.push(msg.data); | ||
}.bind(this), | ||
); | ||
} | ||
end(cb?: () => void) { | ||
this.ws.close(); | ||
if (cb) cb(); | ||
return this; | ||
} | ||
_write( | ||
chunk: any, | ||
bufferEncoding: BufferEncoding, | ||
callback: (err: Error | null | undefined) => void, | ||
): void { | ||
let err: Error | undefined; | ||
try { | ||
this.ws.send(chunk.toString()); | ||
} catch (e) { | ||
console.error(e); | ||
err = e as Error; | ||
} | ||
callback(err); | ||
} | ||
_read(size?: number) {} | ||
} | ||
export function createMinipaviHandler( | ||
@@ -54,3 +94,3 @@ minitelFactory: (ws: Duplex, req: Request) => any, | ||
return { | ||
async fetch(request: Request) { | ||
async fetch(request: Request, _: {}, ctx: ExecutionContext) { | ||
const reqUrl = new URL(request.url); | ||
@@ -66,12 +106,11 @@ | ||
const server = webSocketPair[1]; | ||
server.accept(); | ||
const stream = new Duplex(); | ||
server.addEventListener('message', (event) => stream.write(event.data)); | ||
stream.on('data', (data) => server.send(data)); | ||
server.addEventListener('close', () => stream.end()); | ||
stream.on('close', () => server.close()); | ||
const stream = new DuplexBridge(server); | ||
setImmediate(() => minitelFactory(stream, request)); | ||
setTimeout(() => minitelFactory(stream, request), 1); | ||
const streamEnd = new Promise<void>((r) => stream.on('end', () => r())); | ||
ctx.waitUntil(streamEnd); | ||
return new Response(null, { | ||
@@ -90,7 +129,7 @@ status: 101, | ||
// if (!success) { | ||
// return new Response(`Malformed request: ${JSON.stringify(error)}`, { | ||
// status: 400, | ||
// }); | ||
// } | ||
if (!success) { | ||
return new Response(`Malformed request: ${JSON.stringify(error)}`, { | ||
status: 400, | ||
}); | ||
} | ||
@@ -117,7 +156,7 @@ if (reqUrl.pathname === '/') { | ||
key: 'Same host <https://npmjs.com/packages/minitel-minipavi>', | ||
host: reqUrl.hostname, | ||
host: `ssl://${reqUrl.hostname}:443`, | ||
path: `/websocket${newParams.toString()}`, | ||
echo: 'off', | ||
case: 'lower', | ||
proto: 'wss', | ||
proto: '', | ||
}, | ||
@@ -124,0 +163,0 @@ }, |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27706
447
2