honkit 4.0.3 → 4.0.5

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.
@@ -30,26 +30,38 @@ const cheerio = __importStar(require("cheerio"));
30
30
  const tmp_1 = __importDefault(require("tmp"));
31
31
  const path_1 = __importDefault(require("path"));
32
32
  const fetchRemoteImages_1 = __importDefault(require("../fetchRemoteImages"));
33
+ const promises_1 = __importDefault(require("fs/promises"));
34
+ const assert_1 = __importDefault(require("assert"));
35
+ const constants = __importStar(require("constants"));
33
36
  const URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png";
34
- describe.skip("fetchRemoteImages", () => {
37
+ describe("fetchRemoteImages", () => {
35
38
  let dir;
36
39
  beforeEach(() => {
37
40
  dir = tmp_1.default.dirSync();
41
+ return promises_1.default.rm(dir.name, { recursive: true, force: true });
38
42
  });
39
- test("should download image file", () => {
43
+ afterEach(() => {
44
+ // remove tmp `dir`
45
+ return promises_1.default.rm(dir.name, { recursive: true, force: true });
46
+ });
47
+ it("should download image file", async () => {
40
48
  const $ = cheerio.load(`<img src="${URL}" />`, { _useHtmlParser2: true });
41
- return (0, fetchRemoteImages_1.default)(dir.name, "index.html", $).then(() => {
42
- const $img = $("img");
43
- const src = $img.attr("src");
44
- expect(dir.name).toHaveFile(src);
49
+ await (0, fetchRemoteImages_1.default)(dir.name, "index.html", $);
50
+ const $img = $("img");
51
+ const src = $img.attr("src");
52
+ const expected = path_1.default.join(dir.name, src);
53
+ await assert_1.default.doesNotReject(() => {
54
+ return promises_1.default.access(expected, constants.F_OK);
45
55
  });
46
56
  });
47
- test("should download image file and replace with relative path", () => {
57
+ it("should download image file and replace with relative path", async () => {
48
58
  const $ = cheerio.load(`<img src="${URL}" />`, { _useHtmlParser2: true });
49
- return (0, fetchRemoteImages_1.default)(dir.name, "test/index.html", $).then(() => {
50
- const $img = $("img");
51
- const src = $img.attr("src");
52
- expect(dir.name).toHaveFile(path_1.default.join("test", src));
59
+ await (0, fetchRemoteImages_1.default)(dir.name, "test/index.html", $);
60
+ const $img = $("img");
61
+ const src = $img.attr("src");
62
+ const expected = path_1.default.join(dir.name, "test/" + src);
63
+ await assert_1.default.doesNotReject(() => {
64
+ return promises_1.default.access(expected, constants.F_OK);
53
65
  });
54
66
  });
55
67
  });
@@ -10,11 +10,6 @@ const fs_1 = __importDefault(require("../../utils/fs"));
10
10
  const location_1 = __importDefault(require("../../utils/location"));
11
11
  /**
12
12
  Fetch all remote images
13
-
14
- @param {string} rootFolder
15
- @param {string} currentFile
16
- @param {HTMLDom} $
17
- @return {Promise}
18
13
  */
19
14
  function fetchRemoteImages(rootFolder, currentFile, $) {
20
15
  const currentDirectory = path_1.default.dirname(currentFile);
package/lib/utils/fs.js CHANGED
@@ -7,11 +7,12 @@ const fs_1 = __importDefault(require("fs"));
7
7
  const mkdirp_1 = __importDefault(require("mkdirp"));
8
8
  const destroy_1 = __importDefault(require("destroy"));
9
9
  const tmp_1 = __importDefault(require("tmp"));
10
- const request_1 = __importDefault(require("request"));
11
10
  const path_1 = __importDefault(require("path"));
12
11
  const cp_1 = __importDefault(require("cp"));
13
12
  const cpr_1 = __importDefault(require("cpr"));
14
13
  const promise_1 = __importDefault(require("./promise"));
14
+ const http_1 = __importDefault(require("http"));
15
+ const https_1 = __importDefault(require("https"));
15
16
  // Write a stream to a file
16
17
  function writeStream(filename, st) {
17
18
  const d = promise_1.default.defer();
@@ -56,9 +57,42 @@ function genTmpFile(opts) {
56
57
  function genTmpDir(opts) {
57
58
  return promise_1.default.nfcall(tmp_1.default.dir, opts).get(0);
58
59
  }
60
+ // https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries
61
+ const downloadStream = (url, dest, cb) => {
62
+ const file = fs_1.default.createWriteStream(dest);
63
+ const protocol = url.startsWith("https") ? https_1.default : http_1.default;
64
+ const request = protocol.get(url, (response) => {
65
+ // check if response is success
66
+ if (response.statusCode < 200 && response.statusCode > 300) {
67
+ return cb(new Error("Response status was " + response.statusCode));
68
+ }
69
+ response.pipe(file);
70
+ });
71
+ // close() is async, call cb after close completes
72
+ file.on("finish", () => file.close(() => cb(null)));
73
+ // check for request error too
74
+ request.on("error", (err) => {
75
+ fs_1.default.unlink(dest, () => cb(err)); // delete the (partial) file and then return the error
76
+ });
77
+ file.on("error", (err) => {
78
+ fs_1.default.unlink(dest, () => cb(err)); // delete the (partial) file and then return the error
79
+ });
80
+ };
59
81
  // Download an image
60
- function download(uri, dest) {
61
- return writeStream(dest, (0, request_1.default)(uri));
82
+ async function download(uri, destFilePath) {
83
+ // create dest dir if not exists
84
+ const destDir = path_1.default.dirname(destFilePath);
85
+ await fs_1.default.promises.mkdir(destDir, { recursive: true });
86
+ const d = promise_1.default.defer();
87
+ downloadStream(uri, destFilePath, (err) => {
88
+ if (err) {
89
+ d.reject(err);
90
+ }
91
+ else {
92
+ d.resolve();
93
+ }
94
+ });
95
+ return d.promise;
62
96
  }
63
97
  // Find a filename available in a folder
64
98
  function uniqueFilename(base, filename) {
@@ -80,8 +114,8 @@ function ensureFile(filename) {
80
114
  }
81
115
  // Remove a folder
82
116
  function rmDir(base) {
83
- return promise_1.default.nfcall(fs_1.default.rmdir, base, {
84
- recursive: true,
117
+ return promise_1.default.nfcall(fs_1.default.rm, base, {
118
+ recursive: true
85
119
  });
86
120
  }
87
121
  /**
@@ -159,5 +193,5 @@ exports.default = {
159
193
  uniqueFilename: uniqueFilename,
160
194
  ensureFile: ensureFile,
161
195
  ensureFolder: ensureFolder,
162
- rmDir: rmDir,
196
+ rmDir: rmDir
163
197
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "honkit",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "description": "HonKit is building beautiful books using Markdown.",
5
5
  "keywords": [
6
6
  "git",
@@ -81,7 +81,6 @@
81
81
  "open": "^7.0.0",
82
82
  "q": "^1.5.1",
83
83
  "read-installed": "^4.0.3",
84
- "request": "^2.88.0",
85
84
  "resolve": "^1.17.0",
86
85
  "semver": "^5.1.0",
87
86
  "send": "^0.17.1",
@@ -100,5 +99,5 @@
100
99
  "rimraf": "^3.0.2",
101
100
  "typescript": "^4.1.3"
102
101
  },
103
- "gitHead": "db50f04e29a408d222009522a862d91dd495fcdb"
102
+ "gitHead": "ef44431f9080787093547712e544ba1b5650c58f"
104
103
  }