-
Notifications
You must be signed in to change notification settings - Fork 723
/
nock.js
158 lines (140 loc) · 4.66 KB
/
nock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const ytdl = require('..');
const path = require('path');
const fs = require('fs');
const { URL } = require('url');
const nock = require('nock');
const YT_HOST = 'https://www.youtube.com';
const MANIFEST_HOST = 'https://manifest.googlevideo.com';
const M3U8_HOST = 'https://manifest.googlevideo.com';
const EMBED_PATH = '/embed/';
const INFO_PATH = '/get_video_info?';
process.env.YTDL_NO_UPDATE = 'true';
if (global.it) {
before(() => nock.disableNetConnect());
after(() => nock.enableNetConnect());
afterEach(() => nock.cleanAll());
afterEach(() => {
ytdl.cache.sig.clear();
ytdl.cache.info.clear();
ytdl.cache.cookie.clear();
ytdl.cache.watch.clear();
});
}
exports = module.exports = (id, type, opts = {}) => {
let scopes = [];
let folder = `files/videos/${type}`;
let existingFiles = fs.readdirSync(path.join(__dirname, folder));
let existingFilesSet = new Set(existingFiles);
const playerfile = /((?:html5)?player[-_][a-zA-Z0-9\-_.]+)(?:\.js|\/)/;
opts = Object.assign({
watchJson: existingFilesSet.has('watch.json'),
watchHtml: existingFilesSet.has('watch.html'),
dashmpd: existingFilesSet.has('dash-manifest.xml'),
m3u8: existingFilesSet.has('hls-manifest.m3u8'),
player: existingFiles.filter(key => playerfile.test(key)).length > 0,
embed: existingFilesSet.has('embed.html'),
get_video_info: existingFilesSet.has('get_video_info'),
}, opts);
const addScope = (host, testOptions, nockOptions) => {
if (Array.isArray(testOptions) && Array.isArray(testOptions[0])) {
testOptions.forEach(testOption => addScope(host, testOption, nockOptions));
return;
}
let scope = nock(host, { reqheaders: opts.headers });
if (nockOptions.filteringPath) {
scope = scope.filteringPath(...nockOptions.filteringPath);
}
scope = scope.get(nockOptions.get);
let statusCode = (Array.isArray(testOptions) && testOptions[1]) || 200;
let filepath = path.join(__dirname, `${folder}/${nockOptions.file}`);
let reply = testOptions[2];
if (!reply || testOptions === true) {
scope = scope.replyWithFile(statusCode, filepath);
} else if (typeof reply === 'string') {
scope = scope.reply(200, reply);
} else if (typeof reply === 'function') {
scope = scope.reply(200, (uri, requestBody, callback) => {
fs.readFile(filepath, 'utf8', (err, body) => {
if (err) return callback(err);
callback(null, testOptions[2](body));
});
});
}
scopes.push(scope);
};
if (opts.watchJson) {
addScope(YT_HOST, opts.watchJson, {
filteringPath: [/\/watch\?v=.+&pbj=1$/, '/watch?v=XXX&pbj=1'],
get: '/watch?v=XXX&pbj=1',
file: 'watch.json',
});
}
if (opts.watchHtml) {
addScope(YT_HOST, opts.watchHtml, {
filteringPath: [/\/watch\?v=.+&hl=en$/, '/watch?v=XXX'],
get: '/watch?v=XXX',
file: 'watch.html',
});
}
if (opts.dashmpd) {
addScope(MANIFEST_HOST, opts.dashmpd, {
filteringPath: [() => '/api/manifest/dash/'],
get: '/api/manifest/dash/',
file: 'dash-manifest.xml',
});
}
if (opts.m3u8) {
addScope(M3U8_HOST, opts.m3u8, {
filteringPath: [() => '/api/manifest/hls_variant/'],
get: '/api/manifest/hls_variant/',
file: 'hls-manifest.m3u8',
});
}
if (opts.player) {
addScope(YT_HOST, opts.player, {
filteringPath: [/\/player.+$/, '/player.js'],
get: '/s/player.js',
file: existingFiles.find(f => /(html5)?player.+\.js$/.test(f)),
});
}
if (opts.embed) {
addScope(YT_HOST, opts.embed, {
get: `${EMBED_PATH + id}?hl=en`,
file: 'embed.html',
});
}
if (opts.get_video_info) {
addScope(YT_HOST, opts.get_video_info, {
filteringPath: [p => {
let regexp = /\?video_id=([a-zA-Z0-9_-]+)&(.+)$/;
return p.replace(regexp, (_, r) => `?video_id=${r}`);
}],
get: `${INFO_PATH}video_id=${id}`,
file: 'get_video_info',
});
}
return {
done: () => {
scopes.forEach(scope => {
scope.done();
});
},
urlReply: (uri, statusCode, body, headers) => {
scopes.push(exports.url(uri).reply(statusCode, body, headers));
},
urlReplyWithFile: (uri, statusCode, file, headers) => {
scopes.push(exports.url(uri).replyWithFile(statusCode, file, headers));
},
};
};
exports.filteringPath = (uri, filter1, filter2) => {
let parsed = new URL(uri);
return nock(parsed.origin)
.filteringPath(filter1, filter2)
.get(parsed.pathname + parsed.search + parsed.hash);
};
exports.url = uri => {
let parsed = new URL(uri);
return nock(parsed.origin).get(parsed.pathname + parsed.search + parsed.hash);
};
exports.cleanAll = nock.cleanAll;