Skip to content

Commit

Permalink
fix type error for node
Browse files Browse the repository at this point in the history
  • Loading branch information
ying_123 committed May 17, 2023
1 parent 0a76a0e commit 49150ee
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 44 deletions.
19 changes: 12 additions & 7 deletions dist/hls2mp4.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ function parseM3u8File(url, customFetch) {
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return new Blob([data.buffer]).text(); })];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -957,7 +957,11 @@ var Hls2Mp4 = /** @class */ (function () {
return buffer.slice(bufferOffset);
};
Hls2Mp4.prototype.hexToUint8Array = function (hex) {
return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); }));
var matchedChars = hex.replace(/^0x/, '').match(/[\da-f]{2}/gi);
if (matchedChars) {
return new Uint8Array(matchedChars.map(function (hx) { return parseInt(hx, 16); }));
}
return new Uint8Array(0);
};
Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) {
var ivData;
Expand All @@ -982,7 +986,7 @@ var Hls2Mp4 = /** @class */ (function () {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, exports.TaskType.parseM3u8, 1);
return [2 /*return*/, data];
}
return [2 /*return*/];
throw new Error('m3u8 load failed');
}
});
});
Expand Down Expand Up @@ -1037,13 +1041,14 @@ var Hls2Mp4 = /** @class */ (function () {
};
Hls2Mp4.prototype.downloadM3u8 = function (url) {
return __awaiter(this, void 0, void 0, function () {
var _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var m3u8Parsed, _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.parseM3u8(url)];
case 1:
_a = _c.sent(), content = _a.content, parsedUrl = _a.url;
m3u8Parsed = _c.sent();
_a = m3u8Parsed, content = _a.content, parsedUrl = _a.url;
keyMatchRegExp = createFileUrlRegExp('key', 'gi');
keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="' + keyMatchRegExp.source + '"(,IV=\\w+)?)?', 'gi');
matchReg = new RegExp(keyTagMatchRegExp.source + '|' + createFileUrlRegExp('ts', 'gi').source, 'g');
Expand Down Expand Up @@ -1189,7 +1194,7 @@ var Hls2Mp4 = /** @class */ (function () {
}
return [2 /*return*/, {
done: false,
data: null
data: undefined
}];
case 3: return [2 /*return*/];
}
Expand Down Expand Up @@ -1251,7 +1256,7 @@ var Hls2Mp4 = /** @class */ (function () {
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.5';
Hls2Mp4.version = '1.1.7';
return Hls2Mp4;
}());

Expand Down
19 changes: 12 additions & 7 deletions dist/hls2mp4.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return new Blob([data.buffer]).text(); })];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -954,7 +954,11 @@
return buffer.slice(bufferOffset);
};
Hls2Mp4.prototype.hexToUint8Array = function (hex) {
return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); }));
var matchedChars = hex.replace(/^0x/, '').match(/[\da-f]{2}/gi);
if (matchedChars) {
return new Uint8Array(matchedChars.map(function (hx) { return parseInt(hx, 16); }));
}
return new Uint8Array(0);
};
Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) {
var ivData;
Expand All @@ -979,7 +983,7 @@
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, exports.TaskType.parseM3u8, 1);
return [2 /*return*/, data];
}
return [2 /*return*/];
throw new Error('m3u8 load failed');
}
});
});
Expand Down Expand Up @@ -1034,13 +1038,14 @@
};
Hls2Mp4.prototype.downloadM3u8 = function (url) {
return __awaiter(this, void 0, void 0, function () {
var _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var m3u8Parsed, _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.parseM3u8(url)];
case 1:
_a = _c.sent(), content = _a.content, parsedUrl = _a.url;
m3u8Parsed = _c.sent();
_a = m3u8Parsed, content = _a.content, parsedUrl = _a.url;
keyMatchRegExp = createFileUrlRegExp('key', 'gi');
keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="' + keyMatchRegExp.source + '"(,IV=\\w+)?)?', 'gi');
matchReg = new RegExp(keyTagMatchRegExp.source + '|' + createFileUrlRegExp('ts', 'gi').source, 'g');
Expand Down Expand Up @@ -1186,7 +1191,7 @@
}
return [2 /*return*/, {
done: false,
data: null
data: undefined
}];
case 3: return [2 /*return*/];
}
Expand Down Expand Up @@ -1248,7 +1253,7 @@
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.5';
Hls2Mp4.version = '1.1.7';
return Hls2Mp4;
}());

Expand Down
19 changes: 12 additions & 7 deletions dist/hls2mp4.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return new Blob([data.buffer]).text(); })];
case 2: return [4 /*yield*/, ffmpeg.fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -957,7 +957,11 @@
return buffer.slice(bufferOffset);
};
Hls2Mp4.prototype.hexToUint8Array = function (hex) {
return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); }));
var matchedChars = hex.replace(/^0x/, '').match(/[\da-f]{2}/gi);
if (matchedChars) {
return new Uint8Array(matchedChars.map(function (hx) { return parseInt(hx, 16); }));
}
return new Uint8Array(0);
};
Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) {
var ivData;
Expand All @@ -982,7 +986,7 @@
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, exports.TaskType.parseM3u8, 1);
return [2 /*return*/, data];
}
return [2 /*return*/];
throw new Error('m3u8 load failed');
}
});
});
Expand Down Expand Up @@ -1037,13 +1041,14 @@
};
Hls2Mp4.prototype.downloadM3u8 = function (url) {
return __awaiter(this, void 0, void 0, function () {
var _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var m3u8Parsed, _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.parseM3u8(url)];
case 1:
_a = _c.sent(), content = _a.content, parsedUrl = _a.url;
m3u8Parsed = _c.sent();
_a = m3u8Parsed, content = _a.content, parsedUrl = _a.url;
keyMatchRegExp = createFileUrlRegExp('key', 'gi');
keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="' + keyMatchRegExp.source + '"(,IV=\\w+)?)?', 'gi');
matchReg = new RegExp(keyTagMatchRegExp.source + '|' + createFileUrlRegExp('ts', 'gi').source, 'g');
Expand Down Expand Up @@ -1189,7 +1194,7 @@
}
return [2 /*return*/, {
done: false,
data: null
data: undefined
}];
case 3: return [2 /*return*/];
}
Expand Down Expand Up @@ -1251,7 +1256,7 @@
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.5';
Hls2Mp4.version = '1.1.7';
return Hls2Mp4;
}());

Expand Down
19 changes: 12 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ function parseM3u8File(url, customFetch) {
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, fetchFile(url).then(function (data) { return new Blob([data.buffer]).text(); })];
case 2: return [4 /*yield*/, fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -953,7 +953,11 @@ var Hls2Mp4 = /** @class */ (function () {
return buffer.slice(bufferOffset);
};
Hls2Mp4.prototype.hexToUint8Array = function (hex) {
return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); }));
var matchedChars = hex.replace(/^0x/, '').match(/[\da-f]{2}/gi);
if (matchedChars) {
return new Uint8Array(matchedChars.map(function (hx) { return parseInt(hx, 16); }));
}
return new Uint8Array(0);
};
Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) {
var ivData;
Expand All @@ -978,7 +982,7 @@ var Hls2Mp4 = /** @class */ (function () {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.parseM3u8, 1);
return [2 /*return*/, data];
}
return [2 /*return*/];
throw new Error('m3u8 load failed');
}
});
});
Expand Down Expand Up @@ -1033,13 +1037,14 @@ var Hls2Mp4 = /** @class */ (function () {
};
Hls2Mp4.prototype.downloadM3u8 = function (url) {
return __awaiter(this, void 0, void 0, function () {
var _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var m3u8Parsed, _a, content, parsedUrl, keyMatchRegExp, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.parseM3u8(url)];
case 1:
_a = _c.sent(), content = _a.content, parsedUrl = _a.url;
m3u8Parsed = _c.sent();
_a = m3u8Parsed, content = _a.content, parsedUrl = _a.url;
keyMatchRegExp = createFileUrlRegExp('key', 'gi');
keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="' + keyMatchRegExp.source + '"(,IV=\\w+)?)?', 'gi');
matchReg = new RegExp(keyTagMatchRegExp.source + '|' + createFileUrlRegExp('ts', 'gi').source, 'g');
Expand Down Expand Up @@ -1185,7 +1190,7 @@ var Hls2Mp4 = /** @class */ (function () {
}
return [2 /*return*/, {
done: false,
data: null
data: undefined
}];
case 3: return [2 /*return*/];
}
Expand Down Expand Up @@ -1247,7 +1252,7 @@ var Hls2Mp4 = /** @class */ (function () {
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.5';
Hls2Mp4.version = '1.1.7';
return Hls2Mp4;
}());

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-typescript": "^11.1.0",
"@types/aes-js": "^3.1.1",
"rollup": "^3.21.4",
"tslib": "^2.0.0",
"typescript": "^4.7.4"
Expand Down
36 changes: 21 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createFFmpeg, fetchFile, CreateFFmpegOptions, FFmpeg } from '@ffmpeg/ffmpeg';
import aesjs from 'aes-js';
import aesjs, { type ByteSource } from 'aes-js';

export enum TaskType {
loadFFmeg = 0,
Expand All @@ -14,7 +14,7 @@ interface ProgressCallback {

type LoadResult<T = unknown> = {
done: boolean;
data: T;
data?: T;
msg?: string;
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export async function parseM3u8File(url: string, customFetch?: (url: string) =>
}
else {
playList = await fetchFile(url).then(
data => new Blob([data.buffer]).text()
data => aesjs.utils.utf8.fromBytes(data)
)
}
const matchedM3u8 = playList.match(
Expand All @@ -89,7 +89,7 @@ export default class Hls2Mp4 {
private tsDownloadConcurrency: number;
private totalSegments = 0;
private savedSegments = 0;
public static version = '1.1.5'
public static version = '1.1.7'

constructor({ maxRetry = 3, tsDownloadConcurrency = 10, ...options }: CreateFFmpegOptions & Hls2Mp4Options, onProgress?: ProgressCallback) {
this.instance = createFFmpeg(options);
Expand All @@ -114,18 +114,23 @@ export default class Hls2Mp4 {
}

private hexToUint8Array(hex: string) {
return new Uint8Array(
hex.replace(/^0x/, '').match(
/[\da-f]{2}/gi
).map(hx => parseInt(hx, 16)))
const matchedChars = hex.replace(/^0x/, '').match(
/[\da-f]{2}/gi
);
if (matchedChars) {
return new Uint8Array(
matchedChars.map(hx => parseInt(hx, 16))
)
}
return new Uint8Array(0);
}

private aesDecrypt(buffer: Uint8Array, keyBuffer: Uint8Array, iv?: string) {
let ivData: Uint8Array;
let ivData: ByteSource;
if (iv) {
ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv)
}
const aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData);
const aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData!);
return aesCbc.decrypt(buffer);
}

Expand All @@ -138,7 +143,7 @@ export default class Hls2Mp4 {
this.onProgress?.(TaskType.parseM3u8, 1)
return data;
}
new Error('m3u8 load failed')
throw new Error('m3u8 load failed')
}

private async downloadFile(url: string) {
Expand All @@ -156,7 +161,7 @@ export default class Hls2Mp4 {
return Promise.all(
segs.map(async ({ name, url, source }) => {
const tsData = await this.downloadFile(url)
const buffer = key ? this.aesDecrypt(tsData, key, iv) : this.transformBuffer(tsData)
const buffer = key ? this.aesDecrypt(tsData!, key, iv) : this.transformBuffer(tsData!)
this.instance.FS('writeFile', name, buffer)
this.savedSegments += 1
this.onProgress?.(TaskType.downloadTs, this.savedSegments / this.totalSegments)
Expand All @@ -170,7 +175,8 @@ export default class Hls2Mp4 {
}

private async downloadM3u8(url: string) {
let { content, url: parsedUrl } = await this.parseM3u8(url)
const m3u8Parsed = await this.parseM3u8(url)
let { content, url: parsedUrl } = m3u8Parsed!;
const keyMatchRegExp = createFileUrlRegExp('key', 'gi');
const keyTagMatchRegExp = new RegExp(
'#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="' + keyMatchRegExp.source + '"(,IV=\\w+)?)?',
Expand Down Expand Up @@ -215,7 +221,7 @@ export default class Hls2Mp4 {

for (const group of segments) {
const total = group.segments.length;
let keyBuffer: Uint8Array;
let keyBuffer: Uint8Array | undefined;

if (group.key) {
const keyUrl = parseUrl(parsedUrl, group.key)
Expand Down Expand Up @@ -270,7 +276,7 @@ export default class Hls2Mp4 {
}
return {
done: false,
data: null
data: undefined
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"outDir": "dist",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
Expand Down
Loading

0 comments on commit 49150ee

Please sign in to comment.