minitel-cloudflare-worker
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,1 +0,2 @@ | ||
export {}; | ||
declare const _default: Promise<void>; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const minitel_standalone_1 = require("minitel-standalone"); | ||
const ws_duplex_bridge_1 = require("ws-duplex-bridge"); | ||
const __1 = require(".."); | ||
(0, __1.createMinipaviHandler)((ws) => { | ||
const stream = new ws_duplex_bridge_1.DuplexBridge(ws, { decodeStrings: false }); | ||
exports.default = (0, __1.createMinipaviHandler)((stream) => { | ||
const minitel = new minitel_standalone_1.Minitel(stream, {}); | ||
minitel.appendChild(new minitel_standalone_1.TextNode('Hello world!', {}, minitel)); | ||
minitel.renderToStream(); | ||
setTimeout(() => stream.end(), 10000); | ||
}, { | ||
host: '0.0.0.0', | ||
port: 4545, | ||
setTimeout(() => stream.end(), 10_000); | ||
}).then(() => console.log('MiniPavi handler ready!')); |
@@ -1,14 +0,10 @@ | ||
import { FastifyInstance, FastifyServerFactory } from 'fastify'; | ||
import { WebSocket } from 'ws'; | ||
import { Duplex } from 'node:stream'; | ||
interface MinipaviHandlerOptions { | ||
version?: string; | ||
port: number; | ||
host: string; | ||
providePavi?: boolean; | ||
provideDirectUrl?: boolean; | ||
https?: boolean; | ||
serverFactory?: FastifyServerFactory; | ||
withFastify?: (server: FastifyInstance) => any; | ||
} | ||
export declare function createMinipaviHandler(minitelFactory: (ws: WebSocket) => any, options: MinipaviHandlerOptions): Promise<void>; | ||
export declare function createMinipaviHandler(minitelFactory: (ws: Duplex, req: Request) => any, options?: MinipaviHandlerOptions): Promise<{ | ||
fetch(request: Request): Promise<Response | undefined>; | ||
}>; | ||
export {}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createMinipaviHandler = createMinipaviHandler; | ||
const http_1 = __importDefault(require("http")); | ||
const fastify_1 = require("fastify"); | ||
const zod_1 = require("zod"); | ||
const ws_1 = require("ws"); | ||
async function createMinipaviHandler(minitelFactory, options) { | ||
if (!options.port) | ||
throw new Error('Port is required'); | ||
if (!options.host) | ||
throw new Error('Host is required'); | ||
const node_stream_1 = require("node:stream"); | ||
const paviSchema = zod_1.z.object({ | ||
PAVI: zod_1.z.object({ | ||
version: zod_1.z.string().regex(/^(\d+\.)*\d+$/g), | ||
uniqueId: zod_1.z.string().regex(/^\d+$/g), | ||
remoteAddr: zod_1.z.string(), | ||
typesocket: zod_1.z.enum(['websocketssl', 'websocket', 'other']), | ||
versionminitel: zod_1.z.string().regex(/^\x01.{3}\x04$/g), | ||
content: zod_1.z.array(zod_1.z.string()), | ||
context: zod_1.z.any(), | ||
fctn: zod_1.z.enum([ | ||
'ENVOI', | ||
'SUITE', | ||
'RETOUR', | ||
'ANNULATION', | ||
'CORRECTION', | ||
'GUIDE', | ||
'REPETITION', | ||
'SOMMAIRE', | ||
'CNX', | ||
'FIN', | ||
'DIRECT', | ||
'DIRECTCNX', | ||
'DIRECTCALLFAILED', | ||
'DIRECTCALLENDED', | ||
'BGCALL', | ||
'BGCALL-SIMU', | ||
]), | ||
}), | ||
URLPARAMS: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(), | ||
}); | ||
async function createMinipaviHandler(minitelFactory, options = {}) { | ||
const fullOptions = { | ||
@@ -20,106 +41,79 @@ version: '1.0', | ||
provideDirectUrl: false, | ||
serverFactory: (handler, opts) => { | ||
return http_1.default.createServer((req, res) => { | ||
handler(req, res); | ||
}); | ||
}, | ||
withFastify: () => { }, | ||
https: false, | ||
...options, | ||
}; | ||
const server = (0, fastify_1.fastify)({ | ||
serverFactory: (...args) => { | ||
const server = fullOptions.serverFactory(...args); | ||
const wss = new ws_1.WebSocketServer({ server }); | ||
wss.on('connection', minitelFactory); | ||
return server; | ||
return { | ||
async fetch(request) { | ||
const reqUrl = new URL(request.url); | ||
const upgradeHeader = request.headers.get('Upgrade'); | ||
if (reqUrl.pathname === '/websocket') { | ||
if (!upgradeHeader || upgradeHeader !== 'websocket') { | ||
return new Response('Upgrade required', { status: 426 }); | ||
} | ||
const webSocketPair = new WebSocketPair(); | ||
const client = webSocketPair[0]; | ||
const server = webSocketPair[1]; | ||
(async () => { | ||
server.accept(); | ||
const stream = new node_stream_1.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)); | ||
})(); | ||
return new Response(null, { | ||
status: 101, | ||
webSocket: client, | ||
}); | ||
} | ||
if (request.method !== 'POST') { | ||
return new Response('Method not allowed', { status: 405 }); | ||
} | ||
const { success, data, error } = paviSchema.safeParse(await request.json()); | ||
if (!success) { | ||
return new Response(`Malformed request: ${JSON.stringify(error)}`, { | ||
status: 400, | ||
}); | ||
} | ||
if (reqUrl.pathname === '/') { | ||
const newParams = new URLSearchParams(); | ||
if (fullOptions.providePavi) | ||
newParams.append('pavi', JSON.stringify(data.PAVI)); | ||
if (fullOptions.provideDirectUrl && 'DIRECTURL' in data) | ||
newParams.append('directUrl', JSON.stringify(data.DIRECTURL)); | ||
return new Response(JSON.stringify({ | ||
version: fullOptions.version, | ||
content: '', | ||
context: '', | ||
echo: 'off', | ||
next: `https://${reqUrl.hostname}/disconnect`, | ||
directcall: 'no', | ||
COMMAND: { | ||
name: 'connectToWs', | ||
param: { | ||
key: 'Same host <https://npmjs.com/packages/minitel-minipavi>', | ||
host: reqUrl.hostname, | ||
path: `/websocket${newParams.toString()}`, | ||
echo: 'off', | ||
case: 'lower', | ||
proto: 'wss', | ||
}, | ||
}, | ||
}), { headers: { 'Content-Type': 'application/json' } }); | ||
} | ||
else if (reqUrl.pathname === '/disconnect') { | ||
return new Response(JSON.stringify({ | ||
version: fullOptions.version, | ||
content: '', | ||
context: '', | ||
echo: 'off', | ||
next: `https://${reqUrl.hostname}/disconnect`, | ||
directcall: 'no', | ||
COMMAND: { | ||
name: 'libCnx', | ||
}, | ||
}), { headers: { 'Content-Type': 'application/json' } }); | ||
} | ||
}, | ||
}); | ||
const paviSchema = zod_1.z.object({ | ||
PAVI: zod_1.z.object({ | ||
version: zod_1.z.string().regex(/^(\d+\.)*\d+$/g), | ||
uniqueId: zod_1.z.string().regex(/^\d+$/g), | ||
remoteAddr: zod_1.z.string(), | ||
typesocket: zod_1.z.enum(['websocketssl', 'websocket', 'other']), | ||
versionminitel: zod_1.z.string().regex(/^\x01.{3}\x04$/g), | ||
content: zod_1.z.array(zod_1.z.string()), | ||
context: zod_1.z.any(), | ||
fctn: zod_1.z.enum([ | ||
'ENVOI', | ||
'SUITE', | ||
'RETOUR', | ||
'ANNULATION', | ||
'CORRECTION', | ||
'GUIDE', | ||
'REPETITION', | ||
'SOMMAIRE', | ||
'CNX', | ||
'FIN', | ||
'DIRECT', | ||
'DIRECTCNX', | ||
'DIRECTCALLFAILED', | ||
'DIRECTCALLENDED', | ||
'BGCALL', | ||
'BGCALL-SIMU', | ||
]), | ||
}), | ||
URLPARAMS: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(), | ||
}); | ||
server.post('/', (req, res) => { | ||
const { success, data, error } = paviSchema.safeParse(req.body); | ||
if (!success) | ||
return res | ||
.status(400) | ||
.send(`Malformed request: ${JSON.stringify(error)}`); | ||
const newParams = new URLSearchParams(); | ||
if (fullOptions.providePavi) | ||
newParams.append('pavi', JSON.stringify(data.PAVI)); | ||
if (fullOptions.provideDirectUrl && 'DIRECTURL' in data) | ||
newParams.append('directUrl', JSON.stringify(data.DIRECTURL)); | ||
res.header('Content-Type', 'application/json'); | ||
return res.send(JSON.stringify({ | ||
version: fullOptions.version, | ||
content: '', | ||
context: '', | ||
echo: 'off', | ||
next: `http://${req.hostname}/disconnect`, | ||
directcall: 'no', | ||
COMMAND: { | ||
name: 'connectToWs', | ||
param: { | ||
key: 'Same host <https://npmjs.com/packages/minitel-minipavi>', | ||
host: req.hostname, | ||
path: `/websocket${newParams.toString()}`, | ||
echo: 'off', | ||
case: 'lower', | ||
proto: fullOptions.https ? 'wss' : 'ws', | ||
}, | ||
}, | ||
})); | ||
}); | ||
server.post('/disconnect', (req, res) => { | ||
const { success, data, error } = paviSchema.safeParse(req.body); | ||
if (!success) | ||
return res | ||
.status(400) | ||
.send(`Malformed request: ${JSON.stringify(error)}`); | ||
res.send(JSON.stringify({ | ||
version: fullOptions.version, | ||
content: '', | ||
context: '', | ||
echo: 'off', | ||
next: `http://${req.hostname}/disconnect`, | ||
directcall: 'no', | ||
COMMAND: { | ||
name: 'libCnx', | ||
}, | ||
})); | ||
}); | ||
await fullOptions.withFastify(server); | ||
return new Promise((resolve) => { | ||
server.listen({ | ||
port: fullOptions.port, | ||
host: fullOptions.host, | ||
}, () => resolve()); | ||
}); | ||
}; | ||
} |
{ | ||
"name": "minitel-cloudflare-worker", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A cloudflare worker for access through MiniPAVI", | ||
@@ -24,5 +24,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"fastify": "^4.28.1", | ||
"minitel-standalone": "^1.9.4", | ||
"ws": "^8.18.0", | ||
"zod": "^3.23.8" | ||
@@ -32,7 +30,5 @@ }, | ||
"@cloudflare/workers-types": "^4.20240725.0", | ||
"@types/ws": "^8.5.11", | ||
"prettier": "3.3.3", | ||
"typescript": "^5.5.4", | ||
"ws-duplex-bridge": "^1.0.1" | ||
"typescript": "^5.5.4" | ||
} | ||
} |
@@ -22,3 +22,2 @@ # `minitel-cloudflare-worker` | ||
import { Minitel, TextNode } from 'minitel-standalone'; | ||
import { DuplexBridge } from 'ws-duplex-bridge'; | ||
import { createMinipaviHandler } from 'minitel-cloudflare-worker'; | ||
@@ -35,6 +34,2 @@ | ||
}, | ||
{ | ||
host: '0.0.0.0', | ||
port: 4545, | ||
}, | ||
).then(() => console.log('MiniPavi handler ready!')); | ||
@@ -41,0 +36,0 @@ ``` |
import { Minitel, TextNode } from 'minitel-standalone'; | ||
import { DuplexBridge } from 'ws-duplex-bridge'; | ||
import { createMinipaviHandler } from '..'; | ||
createMinipaviHandler( | ||
(ws) => { | ||
const stream = new DuplexBridge(ws, { decodeStrings: false }); | ||
export default createMinipaviHandler( | ||
(stream) => { | ||
const minitel = new Minitel(stream, {}); | ||
@@ -15,6 +13,2 @@ minitel.appendChild(new TextNode('Hello world!', {}, minitel)); | ||
}, | ||
{ | ||
host: '0.0.0.0', | ||
port: 4545, | ||
}, | ||
).then(() => console.log('MiniPavi handler ready!')); |
@@ -43,3 +43,3 @@ import { z } from 'zod'; | ||
minitelFactory: (ws: Duplex, req: Request) => any, | ||
options: MinipaviHandlerOptions, | ||
options: MinipaviHandlerOptions = {}, | ||
) { | ||
@@ -46,0 +46,0 @@ const fullOptions: Required<MinipaviHandlerOptions> = { |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
2
3
0
26462
381
58
- Removedfastify@^4.28.1
- Removedws@^8.18.0
- Removed@fastify/ajv-compiler@3.6.0(transitive)
- Removed@fastify/error@3.4.1(transitive)
- Removed@fastify/fast-json-stringify-compiler@4.3.0(transitive)
- Removed@fastify/merge-json-schemas@0.1.1(transitive)
- Removedabstract-logging@2.0.1(transitive)
- Removedajv@8.17.1(transitive)
- Removedajv-formats@2.1.13.0.1(transitive)
- Removedatomic-sleep@1.0.0(transitive)
- Removedavvio@8.4.0(transitive)
- Removedcookie@0.7.2(transitive)
- Removedfast-content-type-parse@1.1.0(transitive)
- Removedfast-decode-uri-component@1.0.1(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stringify@5.16.1(transitive)
- Removedfast-querystring@1.1.2(transitive)
- Removedfast-redact@3.5.0(transitive)
- Removedfast-uri@2.4.03.0.6(transitive)
- Removedfastify@4.29.0(transitive)
- Removedfastq@1.18.0(transitive)
- Removedfind-my-way@8.2.2(transitive)
- Removedforwarded@0.2.0(transitive)
- Removedipaddr.js@1.9.1(transitive)
- Removedjson-schema-ref-resolver@1.0.1(transitive)
- Removedjson-schema-traverse@1.0.0(transitive)
- Removedlight-my-request@5.14.0(transitive)
- Removedon-exit-leak-free@2.1.2(transitive)
- Removedpino@9.6.0(transitive)
- Removedpino-abstract-transport@2.0.0(transitive)
- Removedpino-std-serializers@7.0.0(transitive)
- Removedprocess-warning@3.0.04.0.1(transitive)
- Removedproxy-addr@2.0.7(transitive)
- Removedquick-format-unescaped@4.0.4(transitive)
- Removedreal-require@0.2.0(transitive)
- Removedrequire-from-string@2.0.2(transitive)
- Removedret@0.4.3(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrfdc@1.4.1(transitive)
- Removedsafe-regex2@3.1.0(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsecure-json-parse@2.7.0(transitive)
- Removedsemver@7.6.3(transitive)
- Removedset-cookie-parser@2.7.1(transitive)
- Removedsonic-boom@4.2.0(transitive)
- Removedsplit2@4.2.0(transitive)
- Removedthread-stream@3.1.0(transitive)
- Removedtoad-cache@3.7.0(transitive)
- Removedws@8.18.0(transitive)