import { Readable , Stream } from "stream" ; import * as Messages from "./messages" ; import * as zip from "do-not-zip" ; export const filesSymbol = Symbol ( "bundleFiles" ) ; export const freezeSymbol = Symbol ( "bundleFreeze" ) ; export const mimeTypeSymbol = Symbol ( "bundleMimeType" ) ; namespace Mime { export type type = string ; export type subtype = string ; } /**
 * Defines a container ready to be distributed.
 * If no mimeType is passed to the constructor,
 * it will throw an error.
 */ export default class Bundle { private [ filesSymbol ] : { [ key ] : Buffer ; } = { } ; private [ mimeTypeSymbol ] : string ; constructor ( mimeType ) { if ( ! mimeType ) { throw new Error ( Messages . BUNDLE . MIME_TYPE_MISSING ) ; } this [ mimeTypeSymbol ] = mimeType ; } param mimeType returns static freezable ( mimeType ) { const bundle = new Bundle ( mimeType ) ; return [ bundle , ( ) => bundle [ freezeSymbol ] ( ) ] ; } /**
     * Retrieves bundle's mimeType
     */ public get mimeType ( ) { return this [ mimeTypeSymbol ] ; } /**
     * Freezes the bundle so no more files
     * can be added any further.
     */ protected [ freezeSymbol ] ( ) { if ( this . isFrozen ) { return ; } Object . freeze ( this [ filesSymbol ] ) ; } returns public get isFrozen ( ) { return Object . isFrozen ( this [ filesSymbol ] ) ; } param fileName param buffer public addBuffer ( fileName , buffer ) { if ( this . isFrozen ) { throw new Error ( Messages . BUNDLE . CLOSED ) ; } this [ filesSymbol ] [ fileName ] = buffer ; } returns public getAsBuffer ( ) { this [ freezeSymbol ] ( ) ; return zip . toBuffer ( createZipFilesMap ( this [ filesSymbol ] ) ) ; } returns public getAsStream ( ) { this [ freezeSymbol ] ( ) ; return Readable . from ( zip . toBuffer ( createZipFilesMap ( this [ filesSymbol ] ) ) ) ; } returns public getAsRaw ( ) { this [ freezeSymbol ] ( ) ; return Object . freeze ( { ... this [ filesSymbol ] } ) ; } } param files returns function createZipFilesMap ( files ) { return Object . entries ( files ) . map ( ( [ path , data ] ) => ( { path , data , } ) ) ; }