Skip to content

Commit

Permalink
fix: http download to destination if no parent dirs created
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed May 11, 2016
1 parent 9000c76 commit b5505fc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions src/fpmDownload.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
15 changes: 11 additions & 4 deletions src/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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({
Expand All @@ -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", () => {
Expand Down
15 changes: 15 additions & 0 deletions test/src/httpRequestTest.ts
Original file line number Diff line number Diff line change
@@ -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())
})
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion typings/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b5505fc

Please sign in to comment.