minitel-cloudflare-worker
Advanced tools
Comparing version 1.1.3 to 2.0.0
@@ -1,7 +0,14 @@ | ||
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): { | ||
@@ -8,0 +15,0 @@ fetch(request: Request): Promise<Response | undefined>; |
@@ -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,3 +69,3 @@ const fullOptions = { | ||
providePavi: false, | ||
provideDirectUrl: false, | ||
provideUrlParams: false, | ||
...options, | ||
@@ -53,11 +84,5 @@ }; | ||
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); | ||
return new Response(null, { | ||
@@ -80,5 +105,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 +120,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 +127,0 @@ }, |
{ | ||
"name": "minitel-cloudflare-worker", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"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( | ||
@@ -65,11 +105,7 @@ minitelFactory: (ws: Duplex, req: Request) => any, | ||
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); | ||
@@ -89,7 +125,7 @@ return new Response(null, { | ||
// if (!success) { | ||
// return new Response(`Malformed request: ${JSON.stringify(error)}`, { | ||
// status: 400, | ||
// }); | ||
// } | ||
if (!success) { | ||
return new Response(`Malformed request: ${JSON.stringify(error)}`, { | ||
status: 400, | ||
}); | ||
} | ||
@@ -116,7 +152,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: '', | ||
}, | ||
@@ -123,0 +159,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27361
443