Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 1x 1x 1x 1x 1x 28x 28x 28x 15x 29x 1x 1x 1x 1x 1x 27x 27x 29x 29x 1x 1x 1x 1x 1x 26x 26x 26x 29x 29x 10x 22x 22x 22x 71x 71x 71x 22x 22x 22x 22x 22x 22x 22x 29x 29x 29x 29x 29x 12x 12x 10x 10x 10x 10x 10x 10x 10x 10x 29x 3x 3x 3x 3x 3x 3x 7x 29x 29x | /**
* Copyright 2026 nodearchive
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NodearchiveError } from '../.errors/class.js'
import { normalizeBlobInput } from '../.helpers/normalizeBlobInput/index.js'
import { toNodearchiveError } from '../.helpers/toNodearchiveError/index.js'
import type { ArchiveSummary } from '../.types/ArchiveSummary/type.js'
import type { UnpackArgs } from '../.types/UnpackArgs/type.js'
import type { UnpackedArchive } from '../.types/UnpackedArchive/type.js'
import { materializeArchive } from './materializeArchive/index.js'
import { readArchiveFile } from './readArchiveFile/index.js'
import { readInputArchive } from './readInputArchive/index.js'
import { toMemoryResult } from './toMemoryResult/index.js'
export type { UnpackArgs } from '../.types/UnpackArgs/type.js'
/**
* Reads a `.nar` archive or a supported incoming archive format and returns its
* contents in memory or writes them to the filesystem.
*
* The source is selected from exactly one input mode:
*
* - `blob` reads archive bytes already held in memory.
* - `path` reads an archive from the filesystem.
* - `literalPath` reads an exact archive path without additional path logic.
*
* The reader accepts native `.nar` payloads and supported foreign formats such
* as `.zip`, `.tar`, `.tgz`, `.tar.gz`, and `.gz`.
*
* When `destinationPath` is omitted, the archive is returned in memory. A
* single-file archive resolves to `Uint8Array`; multi-entry archives resolve to
* an {@link UnpackedArchive}. When `destinationPath` is provided, extraction is
* performed on disk unless `whatIf` is enabled.
*
* @param args Options that control source selection, extraction mode,
* overwrite behavior, summary emission, and dry-run behavior.
* @returns A memory result when `destinationPath` is omitted, an
* {@link ArchiveSummary} when `passThru` is enabled for filesystem extraction,
* or `undefined` after extraction when no summary is requested.
* @throws {NodearchiveError} Thrown when `confirm` is provided, when input
* modes are combined, when no archive input is supplied, or when extraction
* fails validation or filesystem materialization.
*
* @example
* Extract an archive to disk.
* ```ts
* await unpack({
* path: './dist/app.nar',
* destinationPath: './out',
* force: true,
* })
* ```
*
* @example
* Read an archive entirely in memory.
* ```ts
* const restored = await unpack({ blob: archiveBytes })
* ```
*/
export async function unpack(
args: UnpackArgs = {}
): Promise<ArchiveSummary | Uint8Array | UnpackedArchive | undefined> {
if (args.confirm) {
throw new NodearchiveError(
'ARCHIVE_CONFIRM_UNSUPPORTED',
'`confirm` is not supported'
)
}
if (
args.blob !== undefined &&
(args.path !== undefined || args.literalPath)
) {
throw new NodearchiveError(
'ARCHIVE_INPUT_CONFLICT',
'Provide either `blob` or `path`/`literalPath`, not both'
)
}
const sourcePath = args.literalPath ?? args.path
if (args.blob === undefined && sourcePath === undefined) {
throw new NodearchiveError(
'ARCHIVE_INPUT_REQUIRED',
'Provide `blob`, `path`, or `literalPath`'
)
}
const archiveBytes =
args.blob !== undefined
? await normalizeBlobInput(args.blob)
: await readArchiveFile(sourcePath!)
const manifest = await readInputArchive(archiveBytes, sourcePath)
const summary: ArchiveSummary = {
bytes: manifest.entries.reduce(
(total, entry) =>
total +
(entry.kind === 'file'
? Buffer.from(entry.data!, 'base64').byteLength
: 0),
0
),
destinationPath: args.destinationPath,
dryRun: Boolean(args.whatIf),
entries: manifest.entries.map((entry) => entry.path),
entryCount: manifest.entries.length,
mode: args.destinationPath ? 'filesystem' : 'memory',
updated: false,
}
if (!args.destinationPath) {
return toMemoryResult(manifest)
}
try {
await materializeArchive(
manifest,
args.destinationPath,
args.force,
Boolean(args.whatIf)
)
} catch (error) {
throw toNodearchiveError(
error,
'ARCHIVE_FILESYSTEM_WRITE_FAILED',
'Failed to extract archive'
)
}
return args.passThru ? summary : undefined
}
|