From b5505fc2234af4fde76e60cf666241333ddab2f5 Mon Sep 17 00:00:00 2001 From: develar Date: Wed, 11 May 2016 09:21:29 +0200 Subject: [PATCH] fix: http download to destination if no parent dirs created --- package.json | 4 ++-- src/fpmDownload.ts | 3 +-- src/httpRequest.ts | 15 +++++++++++---- test/src/httpRequestTest.ts | 15 +++++++++++++++ test/tsconfig.json | 1 + typings/node.d.ts | 1 - 6 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 test/src/httpRequestTest.ts diff --git a/package.json b/package.json index cb1dcb1badc..d1c14e00627 100644 --- a/package.json +++ b/package.json @@ -102,12 +102,12 @@ "path-sort": "^0.1.0", "plist": "^1.2.0", "pre-git": "^3.8.4", - "semantic-release": "^4.3.5", + "semantic-release": "^6.2.2", "should": "^8.3.1", "ts-babel": "^0.8.6", "tsconfig-glob": "^0.4.3", "tslint": "^3.9.0-dev.0", - "typescript": "1.9.0-dev.20160509", + "typescript": "1.9.0-dev.20160511", "whitespace": "^2.0.0" }, "babel": { diff --git a/src/fpmDownload.ts b/src/fpmDownload.ts index 2a883b9c068..86d60315a80 100644 --- a/src/fpmDownload.ts +++ b/src/fpmDownload.ts @@ -1,11 +1,10 @@ import { statOrNull, spawn, debug, debug7z } from "./util" -import { rename, remove } from "fs-extra-p" +import { writeFile, rename, remove } from "fs-extra-p" import { download } from "./httpRequest" import { path7za } from "7zip-bin" import * as path from "path" import { homedir } from "os" import { Promise as BluebirdPromise } from "bluebird" -import { writeFile } from "fs" //noinspection JSUnusedLocalSymbols const __awaiter = require("./awaiter") diff --git a/src/httpRequest.ts b/src/httpRequest.ts index 19fb6e83c1e..be60ab91eda 100644 --- a/src/httpRequest.ts +++ b/src/httpRequest.ts @@ -1,9 +1,10 @@ import { Socket } from "net" import { IncomingMessage, ClientRequest } from "http" import * as https from "https" -import { createWriteStream } from "fs" +import { createWriteStream, ensureDir } from "fs-extra-p" import { parse as parseUrl } from "url" import { Promise as BluebirdPromise } from "bluebird" +import * as path from "path" const maxRedirects = 10 @@ -23,6 +24,8 @@ export function addTimeOutHandler(request: ClientRequest, callback: (error: Erro } function doDownload(url: string, destination: string, redirectCount: number, callback: (error: Error) => void) { + const ensureDirPromise = ensureDir(path.dirname(destination)) + const parsedUrl = parseUrl(url) // user-agent must be specified, otherwise some host can return 401 unauthorised const request = https.request({ @@ -48,9 +51,13 @@ function doDownload(url: string, destination: string, redirectCount: number, cal return } - const downloadStream = createWriteStream(destination) - response.pipe(downloadStream) - downloadStream.on("finish", () => downloadStream.close(callback)) + ensureDirPromise + .then(() => { + const downloadStream = createWriteStream(destination) + response.pipe(downloadStream) + downloadStream.on("finish", () => downloadStream.close(callback)) + }) + .catch(callback) let ended = false response.on("end", () => { diff --git a/test/src/httpRequestTest.ts b/test/src/httpRequestTest.ts new file mode 100644 index 00000000000..70c9b70c7af --- /dev/null +++ b/test/src/httpRequestTest.ts @@ -0,0 +1,15 @@ +import test from "./helpers/avaEx" +import { download } from "out/httpRequest" +import { tmpdir } from "os" +import { randomBytes } from "crypto" +import { assertThat } from "./helpers/fileAssert" +import * as path from "path" + +//noinspection JSUnusedLocalSymbols +const __awaiter = require("out/awaiter") + +test("download to nonexistent dir", () => { + const tempFile = path.join(tmpdir(), `${process.pid}-${randomBytes(8).toString("hex")}`, Date.now().toString(), "foo.txt") + return download("https://drive.google.com/uc?export=download&id=0Bz3JwZ-jqfRONTkzTGlsMkM2TlE", tempFile) + .then(() => assertThat(tempFile).isFile()) +}) \ No newline at end of file diff --git a/test/tsconfig.json b/test/tsconfig.json index 049003bd3ab..5ce8a55cbd4 100755 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -66,6 +66,7 @@ "src/helpers/fileAssert.ts", "src/helpers/packTester.ts", "src/helpers/runTests.ts", + "src/httpRequestTest.ts", "src/linuxPackagerTest.ts", "src/osxPackagerTest.ts", "src/RepoSlugTest.ts", diff --git a/typings/node.d.ts b/typings/node.d.ts index 5928dea34e7..1b24c768aa5 100644 --- a/typings/node.d.ts +++ b/typings/node.d.ts @@ -467,7 +467,6 @@ interface NodeBuffer extends Uint8Array { writeFloatBE(value: number, offset: number, noAssert?: boolean): number; writeDoubleLE(value: number, offset: number, noAssert?: boolean): number; writeDoubleBE(value: number, offset: number, noAssert?: boolean): number; - fill(value: any, offset?: number, end?: number): Buffer; // TODO: encoding param indexOf(value: string | number | Buffer, byteOffset?: number): number; // TODO: entries