Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update config import error #505

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions src/config-import.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yauzl from 'yauzl-promise'
import { validate, valueSchemas } from '@mapeo/schema'
import { json, buffer } from 'node:stream/consumers'
import { assert } from './utils.js'
import path from 'node:path'

// Throw error if a zipfile contains more than 10,000 entries
Expand Down Expand Up @@ -33,13 +34,7 @@ export async function readConfig(configPath) {
throw new Error(`Zip file contains too many entries. Max is ${MAX_ENTRIES}`)
}
const entries = await zip.readEntries(MAX_ENTRIES)
/** @type {undefined | Entry} */
let presetsEntry = entries.find((entry) => entry.filename === 'presets.json')
if (!presetsEntry) {
throw new Error('Zip file does not contain presets.json')
}
const presetsFile = await json(await presetsEntry.openReadStream())
validatePresetsFile(presetsFile)
const presetsFile = await findPresetsFile(entries)

return {
get warnings() {
Expand Down Expand Up @@ -195,6 +190,32 @@ export async function readConfig(configPath) {
}
}

/**
* @param {ReadonlyArray<Entry>} entries
* @rejects if the presets file cannot be found or is invalid
* @returns {Promise<PresetsFile>}
*/
async function findPresetsFile(entries) {
const presetsEntry = entries.find(
(entry) => entry.filename === 'presets.json'
)
assert(presetsEntry, 'Zip file does not contain presets.json')

/** @type {unknown} */
let result
try {
result = await json(await presetsEntry.openReadStream())
} catch (err) {
throw new Error('Could not parse presets.json')
}

assert(isRecord(result), 'Invalid presets.json file')
const { presets, fields } = result
assert(isRecord(presets) && isRecord(fields), 'Invalid presets.json file')

return { presets, fields }
}

/**
* @param {string} filename
* @param {Buffer} buf
Expand Down Expand Up @@ -243,20 +264,6 @@ function parseIcon(filename, buf) {
}
}

/**
* @param {unknown} presetsFile
* @returns {asserts presetsFile is PresetsFile}
*/
function validatePresetsFile(presetsFile) {
if (
!isRecord(presetsFile) ||
!isRecord(presetsFile.presets) ||
!isRecord(presetsFile.fields)
) {
throw new Error('Invalid presets.json file')
}
}

/**
* @param {unknown} value
* @returns {value is Record<string, unknown>}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ExhaustivenessError extends Error {
}

/**
* @param {boolean} condition
* @param {unknown} condition
* @param {string} message
* @returns {asserts condition}
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ test('config import - loading', async (t) => {
'missing presets.json'
)

await t.exception.all(
await t.exception(
async () =>
await readConfig('./tests/fixtures/config/invalidPresetsJSON.zip'),
/SyntaxError: Unexpected string in JSON/,
/Error: Could not parse presets.json/,
'JSON.parse error of presets.json'
)

Expand Down
Loading