Standard library · Web and browser

gui/webview/pack

The file format that holds an application's web files.

import gui.webview.pack

This is the file format behind the files of a webview application: a compressed and scrambled set of files, read from the end of an executable or from a file of its own. You rarely call it directly, because salam bundle does the packing and webview does the reading. Scrambling keeps names and contents out of sight; it is not encryption.

Pack a folder and read it back

PackDir compresses every file of a folder into one pack file and returns how many it stored, or -1. The key scrambles the content: it must be the same when you open the pack, and webview uses the application id. Open fills a Pack you declare first, Count says how many files it holds, and Read returns a Blob. A name that is not there gives a blob whose data is null, so test that before using it. The caller owns the bytes of a blob.

gui-webview-pack-guide-roundtrip.salam
import gui.webview.pack
import os
import io

func main:
    os.MkdirAll("www")
    io.WriteFile("www/index.html", "<h1>Hello</h1>")
    io.WriteFile("www/style.css", "body { margin: 0 }")
    println "packed", pack.PackDir("www", "app.pack", "com.example.app"), "files"
    mut opened := pack.Pack {}
    if not pack.Open("app.pack", "com.example.app", opened):
        println "could not open"
        ret 1
    end
    println "holds", opened.Count()
    page := opened.Read("index.html")
    println "index.html is", page.size, "bytes"
    missing := opened.Read("nothing.txt")
    println "missing found:", missing.data != null
end
Outputpacked 2 files holds 2 index.html is 14 bytes missing found: false

Putting the pack inside a program

AppendPack adds a pack to the end of an executable, and Open accepts that executable as its path: it finds the pack at the end of the file. This is how a desktop program carries its pages in one file, and it is what salam bundle does for you. On macOS the pack goes into the application folder instead, so the signature of the program stays valid.

pack.PackDir("www", "app.pack", "com.example.app")
pack.AppendPack("notes", "app.pack")

Types

Blob

struct Blob
Bytes read from a pack. The caller owns them.
NameTypeDescription
datavoid*A pointer to the bytes.
sizei64 = 0The number of bytes.
gui-webview-pack-Pack.salam
import gui.webview.pack

func main:
    println pack.PackDir("www", "app.pack", "com.example.app")
    println pack.AppendPack("app", "app.pack")
    mut opened := pack.Pack {}
    if pack.Open("app", "com.example.app", opened):
        println opened.path, opened.key, opened.Count()
        blob := opened.Read("index.html")
        println blob.data != null, blob.size
    end
    println pack.ReadRange("app.pack", 0 as i64, 4 as i64) != null
end

Pack

struct Pack
An opened pack.
NameTypeDescription
pathstr = ""The file it was read from.
keystr = ""The key it was opened with.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

Count

func Count(): int
How many files it holds.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

Read

func Read(name: str): Blob
Reads one file. data is null when it is not there.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

Functions

ReadRange

func ReadRange(path: str, offset: i64, n: i64): void*
Reads part of a file into new memory, or null.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

Open

func Open(path: str, key: str, out &: Pack): bool
Opens a pack found at the end of a file.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

PackDir

func PackDir(dir: str, out_path: str, key: str): int
Packs a folder. Returns the file count, or -1.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam

AppendPack

func AppendPack(exe_path: str, pack_path: str): bool
Adds a pack to the end of an executable.

The example for this item is shown with the ones above: gui-webview-pack-Pack.salam