Browse Source

New static deployment support

master
Tony Kovanen 8 years ago
parent
commit
91de162fd5
  1. 13
      lib/hash.js
  2. 73
      lib/index.js
  3. 35
      lib/read-metadata.js

13
lib/hash.js

@ -1,6 +1,5 @@
// Native
const { createHash } = require('crypto')
const path = require('path')
// Packages
const { readFile } = require('fs-promise')
@ -12,20 +11,12 @@ const { readFile } = require('fs-promise')
* @return {Map}
*/
async function hashes(files, isStatic, pkg) {
async function hashes(files) {
const map = new Map()
await Promise.all(
files.map(async name => {
const filename = path.basename(name)
let data
if (isStatic && filename === 'package.json') {
const packageString = JSON.stringify(pkg, null, 2)
data = Buffer.from(packageString)
} else {
data = await readFile(name)
}
const data = await readFile(name)
const h = hash(data)
const entry = map.get(h)

73
lib/index.js

@ -1,6 +1,6 @@
// Native
const { homedir } = require('os')
const { resolve: resolvePath, join: joinPaths } = require('path')
const { resolve: resolvePath } = require('path')
const EventEmitter = require('events')
const qs = require('querystring')
const { parse: parseUrl } = require('url')
@ -55,14 +55,12 @@ module.exports = class Now extends EventEmitter {
name,
description,
type = 'npm',
isStatic = false,
pkg = {},
nowConfig = {},
hasNowJson = false
}
) {
this._path = path
this._static = isStatic
let files
let engines
@ -108,7 +106,7 @@ module.exports = class Now extends EventEmitter {
}
const pkgDetails = Object.assign({ name }, pkg)
const hashes = await hash(files, isStatic, pkgDetails)
const hashes = await hash(files, pkgDetails)
if (this._debug) {
console.timeEnd('> [debug] Computing hashes')
@ -131,23 +129,12 @@ module.exports = class Now extends EventEmitter {
const statFn = followSymlinks ? stat : lstat
return names.map(async name => {
let mode
const getMode = async () => {
const st = await statFn(name)
return st.mode
}
if (this._static) {
if (toRelative(name, this._path) === 'package.json') {
mode = 33261
} else {
mode = await getMode()
name = this.pathInsideContent(name)
}
} else {
mode = await getMode()
}
const mode = await getMode()
return {
sha,
@ -245,12 +232,18 @@ module.exports = class Now extends EventEmitter {
if (!quiet && type === 'npm' && deployment.nodeVersion) {
if (engines && engines.node) {
if (missingVersion) {
console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`)
console.log(
`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`
)
} else {
console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (requested: ${chalk.dim(`\`${engines.node}\``)})`)
console.log(
`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (requested: ${chalk.dim(`\`${engines.node}\``)})`
)
}
} else {
console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`)
console.log(
`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`
)
}
}
@ -261,14 +254,6 @@ module.exports = class Now extends EventEmitter {
return this._url
}
pathInsideContent(position) {
const relativePath = toRelative(position, this._path)
const contentDir = joinPaths(this._path, 'content')
const newPath = joinPaths(contentDir, relativePath)
return newPath
}
upload() {
const parts = splitArray(this._missing, MAX_CONCURRENT)
@ -302,10 +287,6 @@ module.exports = class Now extends EventEmitter {
'x-now-sha': sha,
'x-now-file': names
.map(name => {
if (this._static) {
name = this.pathInsideContent(name)
}
return toRelative(encodeURIComponent(name), this._path)
})
.join(','),
@ -315,7 +296,9 @@ module.exports = class Now extends EventEmitter {
})
if (this._debug) {
console.timeEnd(`> [debug] /sync #${attempt} ${names.join(' ')}`)
console.timeEnd(
`> [debug] /sync #${attempt} ${names.join(' ')}`
)
}
// No retry on 4xx
@ -405,8 +388,9 @@ module.exports = class Now extends EventEmitter {
console.time(`> [debug] /deployments/${deploymentId}/instances`)
}
const res = await this
._fetch(`/now/deployments/${deploymentId}/instances`)
const res = await this._fetch(
`/now/deployments/${deploymentId}/instances`
)
if (this._debug) {
console.timeEnd(`> [debug] /deployments/${deploymentId}/instances`)
@ -589,14 +573,19 @@ module.exports = class Now extends EventEmitter {
this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`)
console.time(
`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`
)
}
const res = await this
._fetch(`/whois-ns${fallback ? '-fallback' : ''}?domain=${encodeURIComponent(domain)}`)
const res = await this._fetch(
`/whois-ns${fallback ? '-fallback' : ''}?domain=${encodeURIComponent(domain)}`
)
if (this._debug) {
console.timeEnd(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`)
console.timeEnd(
`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`
)
}
const body = await res.json()
@ -862,7 +851,9 @@ module.exports = class Now extends EventEmitter {
setScale(nameOrId, scale) {
return this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] #${attempt} POST /deployments/${nameOrId}/instances`)
console.time(
`> [debug] #${attempt} POST /deployments/${nameOrId}/instances`
)
}
const res = await this._fetch(`/now/deployments/${nameOrId}/instances`, {
@ -871,7 +862,9 @@ module.exports = class Now extends EventEmitter {
})
if (this._debug) {
console.timeEnd(`> [debug] #${attempt} POST /deployments/${nameOrId}/instances`)
console.timeEnd(
`> [debug] #${attempt} POST /deployments/${nameOrId}/instances`
)
}
if (res.status === 403) {

35
lib/read-metadata.js

@ -6,19 +6,6 @@ const chalk = require('chalk')
const { readFile } = require('fs-promise')
const { parse: parseDockerfile } = require('docker-file-parser')
// Ours
const { devDependencies: { serve: serveVersion } } = require('../lib/pkg')
// `package.json` used for "static" deployments
const staticPackage = Object.freeze({
scripts: {
start: `NODE_ENV='production' serve ./content`
},
dependencies: {
serve: serveVersion
}
})
module.exports = readMetaData
async function readMetaData(
@ -29,8 +16,7 @@ async function readMetaData(
let type = deploymentType
let name = deploymentName
let isStatic = false
let pkg = await readJSON(path, 'package.json')
const pkg = await readJSON(path, 'package.json')
let nowConfig = await readJSON(path, 'now.json')
const dockerfile = await readDockerfile(path)
@ -83,14 +69,6 @@ async function readMetaData(
}
}
// "static" deployments are (not-so) secretly just
// npm deployments under the hood
if (type === 'static') {
isStatic = true
type = 'npm'
pkg = Object.assign({ name: pkg && pkg.name }, staticPackage)
}
if (!name && nowConfig) {
name = nowConfig.name
}
@ -143,11 +121,15 @@ async function readMetaData(
if (!name) {
name = basename(path)
if (!quiet && !isStatic) {
if (!quiet && type !== 'STATIC') {
if (type === 'docker') {
console.log(`> No \`name\` LABEL in \`Dockerfile\`, using ${chalk.bold(name)}`)
console.log(
`> No \`name\` LABEL in \`Dockerfile\`, using ${chalk.bold(name)}`
)
} else {
console.log(`> No \`name\` in \`package.json\`, using ${chalk.bold(name)}`)
console.log(
`> No \`name\` in \`package.json\`, using ${chalk.bold(name)}`
)
}
}
}
@ -156,7 +138,6 @@ async function readMetaData(
name,
description,
type,
isStatic,
pkg,
nowConfig,
hasNowJson,

Loading…
Cancel
Save