@basmilius/apple-encoding 0.1.3 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * as NTP from "./ntp";
2
2
  export * as OPack from "./opack";
3
3
  export * as Plist from "./plist";
4
+ export * as RTSP from "./rtsp";
4
5
  export * as TLV8 from "./tlv8";
package/dist/index.js CHANGED
@@ -396,6 +396,112 @@ __export(exports_plist, {
396
396
  });
397
397
  import { parse } from "@plist/binary.parse";
398
398
  import { serialize } from "@plist/binary.serialize";
399
+ // src/rtsp.ts
400
+ var exports_rtsp = {};
401
+ __export(exports_rtsp, {
402
+ makeResponse: () => makeResponse,
403
+ makeRequest: () => makeRequest,
404
+ makeHeader: () => makeHeader
405
+ });
406
+ function makeHeader(method, path, headers, cseq) {
407
+ const lines = [];
408
+ lines.push(`${method} ${path} RTSP/1.0`);
409
+ lines.push(`CSeq: ${cseq}`);
410
+ lines.push("User-Agent: AirPlay/320.20");
411
+ lines.push("X-Apple-ProtocolVersion: 1");
412
+ lines.push("X-ProtocolVersion: 1");
413
+ for (const [name, value] of Object.entries(headers)) {
414
+ lines.push(`${name}: ${value}`);
415
+ }
416
+ lines.push("");
417
+ lines.push("");
418
+ return lines.join(`\r
419
+ `);
420
+ }
421
+ function makeRequest(buffer) {
422
+ const headerLength = buffer.indexOf(`\r
423
+ \r
424
+ `);
425
+ const { headers, method, path } = parseRequestHeaders(buffer.subarray(0, headerLength));
426
+ let contentLength = headers["Content-Length"] ? Number(headers["Content-Length"]) : 0;
427
+ if (isNaN(contentLength)) {
428
+ contentLength = 0;
429
+ }
430
+ const requestLength = headerLength + 4 + contentLength;
431
+ if (buffer.byteLength < requestLength) {
432
+ return null;
433
+ }
434
+ const body = buffer.subarray(headerLength + 4, requestLength);
435
+ return {
436
+ headers,
437
+ method,
438
+ path,
439
+ body,
440
+ requestLength
441
+ };
442
+ }
443
+ function makeResponse(buffer) {
444
+ const headerLength = buffer.indexOf(`\r
445
+ \r
446
+ `);
447
+ const { headers, status, statusText } = parseResponseHeaders(buffer.subarray(0, headerLength));
448
+ let contentLength = headers["Content-Length"] ? Number(headers["Content-Length"]) : 0;
449
+ if (isNaN(contentLength)) {
450
+ contentLength = 0;
451
+ }
452
+ const responseLength = headerLength + 4 + contentLength;
453
+ if (buffer.byteLength < responseLength) {
454
+ return null;
455
+ }
456
+ const body = buffer.subarray(headerLength + 4, responseLength);
457
+ const response = new Response(body, {
458
+ status,
459
+ statusText,
460
+ headers
461
+ });
462
+ return {
463
+ response,
464
+ responseLength
465
+ };
466
+ }
467
+ function parseHeaders(lines) {
468
+ const headers = {};
469
+ for (let i = 0;i < lines.length; i++) {
470
+ const colon = lines[i].indexOf(":");
471
+ if (colon <= 0) {
472
+ continue;
473
+ }
474
+ const name = lines[i].substring(0, colon).trim();
475
+ headers[name] = lines[i].substring(colon + 1).trim();
476
+ }
477
+ return headers;
478
+ }
479
+ function parseRequestHeaders(buffer) {
480
+ const lines = buffer.toString("utf8").split(`\r
481
+ `);
482
+ const rawRequest = lines[0].match(/^(\S+)\s+(\S+)\s+RTSP\/1\.0$/);
483
+ const method = rawRequest[1];
484
+ const path = rawRequest[2];
485
+ const headers = parseHeaders(lines.slice(1));
486
+ return {
487
+ headers,
488
+ method,
489
+ path
490
+ };
491
+ }
492
+ function parseResponseHeaders(buffer) {
493
+ const lines = buffer.toString("utf8").split(`\r
494
+ `);
495
+ const rawStatus = lines[0].match(/(HTTP|RTSP)\/[\d.]+\s+(\d+)\s+(.+)/);
496
+ const status = Number(rawStatus[2]);
497
+ const statusText = rawStatus[3];
498
+ const headers = parseHeaders(lines.slice(1));
499
+ return {
500
+ headers,
501
+ status,
502
+ statusText
503
+ };
504
+ }
399
505
  // src/tlv8.ts
400
506
  var exports_tlv8 = {};
401
507
  __export(exports_tlv8, {
@@ -514,6 +620,7 @@ function decode3(buf) {
514
620
  }
515
621
  export {
516
622
  exports_tlv8 as TLV8,
623
+ exports_rtsp as RTSP,
517
624
  exports_plist as Plist,
518
625
  exports_opack as OPack,
519
626
  exports_ntp as NTP
package/dist/rtsp.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export type Method = "GET" | "OPTIONS" | "POST" | "PUT" | "GET_PARAMETER" | "SET_PARAMETER" | "ANNOUNCE" | "RECORD" | "SETUP" | "TEARDOWN";
2
+ export declare function makeHeader(method: Method, path: string, headers: HeadersInit, cseq: number): string;
3
+ export declare function makeRequest(buffer: Buffer): HttpRequest | null;
4
+ export declare function makeResponse(buffer: Buffer): HttpResponse | null;
5
+ type HttpRequest = {
6
+ readonly headers: Record<string, string>;
7
+ readonly method: Method;
8
+ readonly path: string;
9
+ readonly body: Buffer;
10
+ readonly requestLength: number;
11
+ };
12
+ type HttpResponse = {
13
+ readonly response: Response;
14
+ readonly responseLength: number;
15
+ };
16
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/apple-encoding",
3
3
  "description": "Common encoding utilities for Apple Protocols.",
4
- "version": "0.1.3",
4
+ "version": "0.2.1",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {