@hakubase/cli 0.1.0 → 0.3.0

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.
package/README.md CHANGED
@@ -8,13 +8,19 @@ npm i -D @hakubase/cli
8
8
 
9
9
  ## Link
10
10
 
11
- Create a **development key** in the site's settings (tab *Settings*, *Keys*), then:
11
+ ```bash
12
+ npx hakubase link https://noesis.hakubase.com
13
+ ```
14
+
15
+ The command prints a code and opens the browser on the workspace. There, signed in, check that the code is the same, choose the code site and click *Collega*: the command receives a publishing key of that site, named after this computer, and revocable from the site's settings like any other. `--no-browser` only prints the address (on a remote machine, open it from your own). Add the site id to have it chosen already.
16
+
17
+ With a key made by hand in the site's settings (tab *Settings*, *Keys*), no browser:
12
18
 
13
19
  ```bash
14
20
  npx hakubase link https://noesis.hakubase.com <site-id> --key hb_…
15
21
  ```
16
22
 
17
- It writes `.hakubase/project.json` (addresses only, safe to commit) and puts the key in `.env.local` as `HAKUBASE_KEY`, adding `.env.local` to `.gitignore` when the project has one. The key is never printed whole.
23
+ Either way it writes `.hakubase/project.json` (addresses only, safe to commit) and puts the key in `.env.local` as `HAKUBASE_KEY`, adding `.env.local` to `.gitignore` when the project has one. The key is never printed whole.
18
24
 
19
25
  The key is read from `--key`, then the `HAKUBASE_KEY` variable, then `.env.local`.
20
26
 
@@ -58,7 +64,7 @@ npx hakubase deploy dist --publish --message "New home"
58
64
  npx hakubase publish 12 # put an uploaded version online
59
65
  ```
60
66
 
61
- `deploy` zips the folder (up to 32 MB), adds `hakubase.json` from the project root if the folder does not have one, and uploads it as a new version. Without `--publish` the version stays in preview and the command prints its preview link; with it, the version goes online.
67
+ `deploy` sends the list of the folder's files with their fingerprints, uploads only the ones Hakubase does not have yet (in groups, up to 500 MB per site and 10 MB per file, videos included), reads `hakubase.json` from the folder or the project root, and closes a new version. Without `--publish` the version stays in preview and the command prints its preview link; with it, the version goes online.
62
68
 
63
69
  Deploying needs a **publishing key**.
64
70
 
@@ -91,7 +97,7 @@ jobs:
91
97
 
92
98
  | Command | What it does |
93
99
  |---|---|
94
- | `hakubase link <workspace-url> <site-id> [--key hb_…]` | Links the folder to a code site |
100
+ | `hakubase link <workspace-url> [site-id] [--key hb_…] [--no-browser]` | Links the folder to a code site: approved in the browser, or with a key |
95
101
  | `hakubase types [--out file]` | Writes the TypeScript types of the collections |
96
102
  | `hakubase deploy [dir] [--publish] [--message m] [--commit sha] [--via cli\|github]` | Uploads the built folder as a new version |
97
103
  | `hakubase publish <number>` | Puts a version online |
package/bin/hakubase.js CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import { main } from '../lib/commands.js'
3
+ import { installLocalhostResolver } from '../lib/localhost.js'
4
+
5
+ installLocalhostResolver()
3
6
 
4
7
  main(process.argv.slice(2)).then(
5
8
  (code) => { process.exitCode = code },
package/lib/commands.js CHANGED
@@ -1,17 +1,20 @@
1
1
  // The commands of `hakubase`. Each one returns its exit code; output
2
2
  // goes through `io` so the tests can read it.
3
3
 
4
- import { execFileSync } from 'node:child_process'
4
+ import { execFileSync, spawn } from 'node:child_process'
5
+ import crypto from 'node:crypto'
5
6
  import fs from 'node:fs'
7
+ import os from 'node:os'
6
8
  import path from 'node:path'
7
9
 
8
10
  import {
9
11
  ensureGitignored, findRoot, maskKey, readProject, resolveKey, writeEnvKey, writeProject,
10
12
  } from './project.js'
11
13
  import { generateTypes } from './types.js'
12
- import { createZip } from './zip.js'
13
14
 
14
- export const MAX_ZIP = 32 * 1024 * 1024
15
+ // One request carries at most this much: the server takes 32 MB per upload.
16
+ export const BATCH_BYTES = 24 * 1024 * 1024
17
+ export const BATCH_FILES = 200
15
18
  export const BUILD_DIRS = ['dist', 'build', 'out']
16
19
  // What an operating system leaves in a folder, never part of a site.
17
20
  const JUNK = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini'])
@@ -21,7 +24,8 @@ export class CliError extends Error {}
21
24
  export const USAGE = `Usage: hakubase <command> [options]
22
25
 
23
26
  Commands:
24
- link <workspace-url> <site-id> [--key hb_…] Link this folder to a code site
27
+ link <workspace-url> [site-id] Link this folder to a code site: the browser
28
+ [--key hb_…] [--no-browser] asks you to approve (with --key, no browser)
25
29
  types [--out hakubase.d.ts] Write the TypeScript types of the collections
26
30
  deploy [dir] [--publish] [--message m] Upload the built folder as a new version
27
31
  [--commit sha] [--via cli|github] (dir: dist, build or out by default)
@@ -48,7 +52,7 @@ export function parseArgs(argv) {
48
52
  } else {
49
53
  const name = arg.slice(2)
50
54
  const next = argv[i + 1]
51
- if (['publish', 'help'].includes(name) || next === undefined || next.startsWith('--')) {
55
+ if (['publish', 'help', 'no-browser'].includes(name) || next === undefined || next.startsWith('--')) {
52
56
  flags[name] = true
53
57
  } else {
54
58
  flags[name] = next
@@ -87,9 +91,13 @@ function detailOf(body, status) {
87
91
  return `HTTP ${status}`
88
92
  }
89
93
 
90
- export async function request(ctx, method, url, { key, body } = {}) {
94
+ export async function request(ctx, method, url, { key, body, json } = {}) {
91
95
  const headers = { accept: 'application/json', 'user-agent': 'hakubase-cli' }
92
96
  if (key) headers.authorization = `Bearer ${key}`
97
+ if (json !== undefined) {
98
+ headers['content-type'] = 'application/json'
99
+ body = JSON.stringify(json)
100
+ }
93
101
  let response
94
102
  try {
95
103
  response = await ctx.fetch(url, { method, headers, body })
@@ -121,14 +129,52 @@ export function target(ctx, flags) {
121
129
  return { root, api, site, key, siteUrl: project.site_url || '' }
122
130
  }
123
131
 
132
+ /** Opens a page in the person's browser; a failure only means they open it by hand. */
133
+ export function openInBrowser(url) {
134
+ const [command, args] = process.platform === 'darwin' ? ['open', [url]]
135
+ : process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url.replace(/&/g, '^&')]]
136
+ : ['xdg-open', [url]]
137
+ try {
138
+ spawn(command, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref()
139
+ } catch {
140
+ // The address is printed: the person opens it.
141
+ }
142
+ }
143
+
144
+ /** The key, approved by a person in the browser (ADR-0229). */
145
+ export async function keyFromBrowser(ctx, api, siteId, flags) {
146
+ const asked = await request(ctx, 'POST', `${api}/api/public/site-links`, {
147
+ json: { computer: ctx.computer || os.hostname(), site_id: siteId || '' },
148
+ })
149
+ ctx.out(`Open ${asked.url}`)
150
+ ctx.out(`and check that the code is ${asked.code}. Waiting for the approval…`)
151
+ if (!flags['no-browser']) (ctx.openBrowser || openInBrowser)(asked.url)
152
+ const sleep = ctx.sleep || ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
153
+ const tries = Math.ceil((asked.expires_in || 600) / (asked.interval || 2))
154
+ for (let i = 0; i < tries; i++) {
155
+ await sleep((asked.interval || 2) * 1000)
156
+ const got = await request(ctx, 'POST', `${api}/api/public/site-links/collect`, { json: { device: asked.device } })
157
+ if (got.status === 'approved') return got
158
+ if (got.status === 'denied') throw new CliError('The link was refused in the browser.')
159
+ }
160
+ throw new CliError('The request expired: run hakubase link again.')
161
+ }
162
+
124
163
  export async function link(ctx, positional, flags) {
125
- const [workspace, siteId] = positional
126
- if (!workspace || !siteId) throw new CliError('Usage: hakubase link <workspace-url> <site-id> [--key hb_…]')
164
+ const [workspace, siteArg] = positional
165
+ if (!workspace) throw new CliError('Usage: hakubase link <workspace-url> [site-id] [--key hb_…]')
127
166
  const api = workspaceOrigin(workspace)
128
167
  const root = ctx.cwd
129
- const given = typeof flags.key === 'string' ? flags.key : ''
130
- const key = resolveKey(root, given, ctx.env)
131
- if (!key) throw new CliError('No key: pass --key hb_… (a development or publishing key of this site).')
168
+ let given = typeof flags.key === 'string' ? flags.key : ''
169
+ let key = resolveKey(root, given, ctx.env)
170
+ let siteId = siteArg
171
+ if (!key) {
172
+ const got = await keyFromBrowser(ctx, api, siteId, flags)
173
+ key = given = got.token
174
+ siteId = got.site_id
175
+ } else if (!siteId) {
176
+ throw new CliError('Usage: hakubase link <workspace-url> <site-id> --key hb_… (or no key, to approve in the browser)')
177
+ }
132
178
  const info = await request(ctx, 'GET', `${api}/api/sites/${encodeURIComponent(siteId)}/code-site`, { key })
133
179
  writeProject(root, { api, site_id: info.id || siteId, site_url: info.url || '' })
134
180
  if (given) writeEnvKey(root, given)
@@ -162,7 +208,7 @@ export function buildDir(cwd, given) {
162
208
  throw new CliError('No built folder: build the site first, or say which folder to deploy (dist, build, out).')
163
209
  }
164
210
 
165
- /** Every file of the folder, as zip entries with forward-slash names. */
211
+ /** Every file of the folder, with forward-slash names. */
166
212
  export function collectFiles(dir) {
167
213
  const entries = []
168
214
  const walk = (current, prefix) => {
@@ -192,31 +238,79 @@ function gitSubject(cwd) {
192
238
  }
193
239
  }
194
240
 
241
+ /** The list Hakubase reads instead of a zip: path, fingerprint and size of every file. */
242
+ export function listOf(entries) {
243
+ return entries.map((e) => ({
244
+ path: '/' + e.name,
245
+ hash: crypto.createHash('sha256').update(e.data).digest('hex'),
246
+ size: e.data.length,
247
+ data: e.data,
248
+ }))
249
+ }
250
+
251
+ /** The missing files in groups that fit one request each. */
252
+ export function batches(files) {
253
+ const out = []
254
+ let current = []
255
+ let bytes = 0
256
+ for (const file of files) {
257
+ if (current.length && (bytes + file.size > BATCH_BYTES || current.length >= BATCH_FILES)) {
258
+ out.push(current)
259
+ current = []
260
+ bytes = 0
261
+ }
262
+ current.push(file)
263
+ bytes += file.size
264
+ }
265
+ if (current.length) out.push(current)
266
+ return out
267
+ }
268
+
269
+ function readConfig(root, entries) {
270
+ const inside = entries.find((e) => e.name === 'hakubase.json')
271
+ const file = path.join(root, 'hakubase.json')
272
+ const raw = inside ? inside.data : (fs.existsSync(file) ? fs.readFileSync(file) : null)
273
+ if (!raw) return null
274
+ try {
275
+ return JSON.parse(raw.toString('utf8'))
276
+ } catch {
277
+ throw new CliError('hakubase.json is not JSON.')
278
+ }
279
+ }
280
+
195
281
  export async function deploy(ctx, positional, flags) {
196
282
  const t = target(ctx, flags)
197
283
  const dir = buildDir(ctx.cwd, positional[0])
198
284
  const entries = collectFiles(dir)
199
285
  if (!entries.length) throw new CliError(`${path.relative(ctx.cwd, dir) || dir} is empty.`)
200
- const manifest = path.join(t.root, 'hakubase.json')
201
- if (!entries.some((e) => e.name === 'hakubase.json') && fs.existsSync(manifest)) {
202
- entries.push({ name: 'hakubase.json', data: fs.readFileSync(manifest), mtime: fs.statSync(manifest).mtime })
203
- }
204
- const zip = createZip(entries)
205
- if (zip.length > MAX_ZIP) {
206
- throw new CliError(`The zip is ${megabytes(zip.length)}: Hakubase takes up to 32 MB. Put the videos in the File library.`)
286
+ const config = readConfig(t.root, entries)
287
+ const listed = listOf(entries.filter((e) => e.name !== 'hakubase.json'))
288
+ const files = listed.map(({ path: p, hash, size }) => ({ path: p, hash, size }))
289
+ const base = `${t.api}/api/sites/${encodeURIComponent(t.site)}/code-site`
290
+ // 1. What Hakubase lacks: a deploy that changed one page sends one page.
291
+ const plan = await request(ctx, 'POST', `${base}/uploads`, { key: t.key, json: { files, config } })
292
+ const missing = new Set(plan.missing || [])
293
+ const unique = new Map()
294
+ for (const file of listed) if (missing.has(file.hash) && !unique.has(file.hash)) unique.set(file.hash, file)
295
+ const groups = batches([...unique.values()])
296
+ // 2. The missing files, a group at a time, each named by its fingerprint.
297
+ let sent = 0
298
+ for (const group of groups) {
299
+ const form = new FormData()
300
+ for (const file of group) form.append('files', new Blob([file.data], { type: 'application/octet-stream' }), file.hash)
301
+ await request(ctx, 'POST', `${base}/files`, { key: t.key, body: form })
302
+ sent += group.reduce((n, f) => n + f.size, 0)
303
+ if (groups.length > 1) ctx.out(` ${megabytes(sent)} of ${megabytes(plan.missing_bytes || sent)}`)
207
304
  }
305
+ // 3. The version, once every file is there.
208
306
  const github = Boolean(ctx.env.GITHUB_SHA)
209
307
  const commit = typeof flags.commit === 'string' ? flags.commit : (ctx.env.GITHUB_SHA || '')
210
308
  const via = typeof flags.via === 'string' ? flags.via : (github ? 'github' : 'cli')
211
309
  const message = typeof flags.message === 'string' ? flags.message : (github ? gitSubject(ctx.cwd) : '')
212
- const form = new FormData()
213
- form.append('file', new Blob([zip], { type: 'application/zip' }), 'site.zip')
214
- form.append('publish', flags.publish ? 'true' : 'false')
215
- form.append('message', message)
216
- form.append('commit', commit)
217
- form.append('via', via)
218
- const version = await request(ctx, 'POST', `${t.api}/api/sites/${encodeURIComponent(t.site)}/code-site/versions`, { key: t.key, body: form })
219
- ctx.out(`Version ${version.number} uploaded (${version.files} files, ${megabytes(version.size || 0)}, ${megabytes(version.uploaded || 0)} new).`)
310
+ const version = await request(ctx, 'POST', `${base}/versions/manifest`, {
311
+ key: t.key, json: { files, config, publish: Boolean(flags.publish), message, commit, via },
312
+ })
313
+ ctx.out(`Version ${version.number} uploaded (${version.files} files, ${megabytes(version.size || 0)}, ${megabytes(sent)} new).`)
220
314
  if (version.online) {
221
315
  ctx.out(`Online: ${t.siteUrl || await siteUrl(ctx, t)}`)
222
316
  } else {
@@ -0,0 +1,30 @@
1
+ // `*.localhost` is the loopback by definition (RFC 6761), and browsers
2
+ // treat it so; Node asks the system resolver, which on macOS does not.
3
+ // A Hakubase in development lives on `*.hakubase.localhost`: without
4
+ // this, `hakubase link` and `deploy` against it fail with ENOTFOUND.
5
+ import dns from 'node:dns'
6
+
7
+ const original = dns.lookup
8
+
9
+ export function isLoopbackName(host) {
10
+ return typeof host === 'string' && /(^|\.)localhost\.?$/i.test(host)
11
+ }
12
+
13
+ export function installLocalhostResolver() {
14
+ if (dns.lookup.__hakubase) return
15
+ const lookup = function (host, options, callback) {
16
+ if (typeof options === 'function') {
17
+ callback = options
18
+ options = {}
19
+ }
20
+ if (isLoopbackName(host)) {
21
+ const all = typeof options === 'object' && options && options.all
22
+ return all
23
+ ? process.nextTick(callback, null, [{ address: '127.0.0.1', family: 4 }])
24
+ : process.nextTick(callback, null, '127.0.0.1', 4)
25
+ }
26
+ return original.call(dns, host, options, callback)
27
+ }
28
+ lookup.__hakubase = true
29
+ dns.lookup = lookup
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hakubase/cli",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Link, type, develop and deploy Hakubase code sites; Vite and Astro plugins for development with real data.",
5
5
  "type": "module",
6
6
  "bin": {
package/lib/zip.js DELETED
@@ -1,105 +0,0 @@
1
- // A minimal zip writer: deflate or store, UTF-8 names, no zip64.
2
- // Enough for a built site, which Hakubase caps well below the limits
3
- // of the classic format (2,000 files, 100 MB uncompressed).
4
-
5
- import zlib from 'node:zlib'
6
-
7
- let table = null
8
-
9
- function crcTable() {
10
- if (table) return table
11
- table = new Uint32Array(256)
12
- for (let n = 0; n < 256; n++) {
13
- let c = n
14
- for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
15
- table[n] = c >>> 0
16
- }
17
- return table
18
- }
19
-
20
- export function crc32(data) {
21
- if (typeof zlib.crc32 === 'function') return zlib.crc32(data) >>> 0
22
- const t = crcTable()
23
- let c = 0xffffffff
24
- for (let i = 0; i < data.length; i++) c = t[(c ^ data[i]) & 0xff] ^ (c >>> 8)
25
- return (c ^ 0xffffffff) >>> 0
26
- }
27
-
28
- function dosDateTime(date) {
29
- const d = date instanceof Date && !Number.isNaN(date.getTime()) ? date : new Date()
30
- const year = Math.max(1980, d.getFullYear())
31
- const time = (d.getHours() << 11) | (d.getMinutes() << 5) | Math.floor(d.getSeconds() / 2)
32
- const day = ((year - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate()
33
- return { time, day }
34
- }
35
-
36
- const UTF8 = 0x0800
37
-
38
- /**
39
- * entries: [{ name: 'assets/app.js', data: Buffer, mtime?: Date }]
40
- * Returns the zip as one Buffer.
41
- */
42
- export function createZip(entries) {
43
- if (entries.length > 0xffff) throw new Error('Too many files for a zip without zip64.')
44
- const locals = []
45
- const centrals = []
46
- let offset = 0
47
- for (const entry of entries) {
48
- const name = Buffer.from(String(entry.name).replace(/\\/g, '/').replace(/^\/+/, ''), 'utf8')
49
- const data = Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(entry.data)
50
- const crc = crc32(data)
51
- const deflated = zlib.deflateRawSync(data)
52
- const stored = deflated.length >= data.length
53
- const body = stored ? data : deflated
54
- const method = stored ? 0 : 8
55
- const { time, day } = dosDateTime(entry.mtime)
56
-
57
- const local = Buffer.alloc(30)
58
- local.writeUInt32LE(0x04034b50, 0)
59
- local.writeUInt16LE(20, 4)
60
- local.writeUInt16LE(UTF8, 6)
61
- local.writeUInt16LE(method, 8)
62
- local.writeUInt16LE(time, 10)
63
- local.writeUInt16LE(day, 12)
64
- local.writeUInt32LE(crc, 14)
65
- local.writeUInt32LE(body.length, 18)
66
- local.writeUInt32LE(data.length, 22)
67
- local.writeUInt16LE(name.length, 26)
68
- local.writeUInt16LE(0, 28)
69
-
70
- const central = Buffer.alloc(46)
71
- central.writeUInt32LE(0x02014b50, 0)
72
- central.writeUInt16LE(20, 4)
73
- central.writeUInt16LE(20, 6)
74
- central.writeUInt16LE(UTF8, 8)
75
- central.writeUInt16LE(method, 10)
76
- central.writeUInt16LE(time, 12)
77
- central.writeUInt16LE(day, 14)
78
- central.writeUInt32LE(crc, 16)
79
- central.writeUInt32LE(body.length, 20)
80
- central.writeUInt32LE(data.length, 24)
81
- central.writeUInt16LE(name.length, 28)
82
- central.writeUInt16LE(0, 30)
83
- central.writeUInt16LE(0, 32)
84
- central.writeUInt16LE(0, 34)
85
- central.writeUInt16LE(0, 36)
86
- central.writeUInt32LE(0, 38)
87
- central.writeUInt32LE(offset, 42)
88
-
89
- locals.push(local, name, body)
90
- centrals.push(central, name)
91
- offset += local.length + name.length + body.length
92
- if (offset > 0xffffffff) throw new Error('The zip is too big for a zip without zip64.')
93
- }
94
- const directory = Buffer.concat(centrals)
95
- const end = Buffer.alloc(22)
96
- end.writeUInt32LE(0x06054b50, 0)
97
- end.writeUInt16LE(0, 4)
98
- end.writeUInt16LE(0, 6)
99
- end.writeUInt16LE(entries.length, 8)
100
- end.writeUInt16LE(entries.length, 10)
101
- end.writeUInt32LE(directory.length, 12)
102
- end.writeUInt32LE(offset, 16)
103
- end.writeUInt16LE(0, 20)
104
- return Buffer.concat([...locals, directory, end])
105
- }