Text

Text

new Text(config, assetGraph)

Source:

Adds text encoding and decoding support to Asset. Serves as a superclass for Html, Xml, Css, JavaScript, Json, and CacheManifest.

In addition to the config already supported by the Asset base class, the options text and encoding are also available

Example
var textAsset = new Text({
        // "æøå" in iso-8859-1:
        rawSrc: Buffer.from([0xe6, 0xf8, 0xe5]),
        encoding: "iso-8859-1"
    });
    textAsset.text; // "æøå" (decoded JavaScript string)
    textAsset.encoding = 'utf-8';
    textAsset.rawSrc; // <Buffer c3 a6 c3 b8 c3 a5>
Parameters:
Name Type Description
config AssetConfig

The Text assets configuration

Properties
Name Type Attributes Default Description
text String <optional>

The decoded source of the asset. Can be used instead of rawSrc and rawSrcProxy.

encoding String <optional>
"utf-8"

Used to decode and reencode the Asset#rawSrc. Can be any encoding supported by the iconv module. Can be changed later using Text#encoding. See the docs for Text#defaultEncoding. If the asset is loaded via http, the encoding will be read from the Content-Type, and likewise for data: urls.

assetGraph AssetGraph

Mandatory AssetGraph instance references

Extends

Members

baseName :String

Source:
Inherited From:

The file name for the asset, excluding the extension. It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.

If updated, the url of the asset will also be updated.

Type:
  • String

contentType :String

Source:
Inherited From:
Default Value:
  • 'appliction/octet-stream'

The Content-Type (MIME type) of the asset.

Type:
  • String

dataUrl :String

Source:
Inherited From:

The data:-url of the asset for inlining

Type:
  • String

defaultEncoding :String

Source:
Default Value:
  • "utf-8"

The default encoding for the Text (sub)class. Used for decoding the raw source when the encoding cannot be determined by other means, such as a Content-Type header (when the asset was fetched via http), or another indicator specific to the given asset type (@charset for Css, <meta http-equiv="Content-Type" ...> for Html).

Factory setting is "utf-8", but you can override it by setting Text.prototype.defaultEncoding to another value supported by the iconv of iconv-lite modules.

Type:
  • String

defaultExtension :String

Source:
Inherited From:

The default extension for the asset type, prepended with a dot, eg. .html, .js or .png

Type:
  • String

encoding :String

Source:

Get or set the encoding (charset) used for re-encoding the raw source of the asset. To affect the initial decoding of the rawSrc option, provide the encoding option to the constructor.

Type:
  • String

extension :String

Source:
Inherited From:

The file name extension for the asset (String). It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.

If updated, the url of the asset will also be updated.

The extension includes the leading dot and is thus kept in the same format as require('path').extname and the basename command line utility use.

Type:
  • String

externalRelations :Array.<Relation>

Source:
Inherited From:

The subset of outgoing Relations that point to external (non-inlined) Assets

Type:

fileName :String

Source:
Inherited From:

The file name for the asset. It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.

If updated, the url of the asset will also be updated.

Type:
  • String

hostname :String

Source:
Inherited From:

The hostname part of a URL protocol://username:password@<hostname>:port/

Type:
  • String

incomingRelations :Array.<Relation>

Source:
Inherited From:

Get/set the relations pointing to this asset

Caveat: Setting Does not remove/detach other relations already pointing at the asset, but not included in the array, so it's not strictly symmetric with the incomingRelations getter.

Type:

(constant) isAsset :Boolean

Source:
Inherited From:
Default Value:
  • true

Boolean Property that's true for all Asset instances. Avoids reliance on the instanceof operator.

Type:
  • Boolean

isExternalizable :Boolean

Source:
Inherited From:
Default Value:
  • true

Whether the asset occurs in a context where it can be made external. If false, the asset will stay inline. Useful for "always inline" assets pointed to by HtmlConditionalComment, HtmlDataBindAttribute, and HtmlKnockoutContainerless relations. Override when creating the asset.

Type:
  • Boolean

isInline :Boolean

Source:
Inherited From:

Determine whether the asset is inline (shorthand for checking whether it has a url).

Type:
  • Boolean

isLoaded :Boolean

Source:
Inherited From:

The loaded state of the Asset

Type:
  • Boolean

isText :Boolean

Source:
Default Value:
  • true

Property that's true for all Text instances. Avoids reliance on the instanceof operator.

Type:
  • Boolean

lastKnownByteLength :Number

Source:
Inherited From:

Get the last known byt length of the Asset

Doesn't force a serialization of the asset if a value has previously been recorded.

Type:
  • Number

md5Hex :String

Source:
Inherited From:

Get the current md5 hex of the asset.

Type:
  • String

(nullable) nonInlineAncestor :Asset

Source:
Inherited From:

Get the first non-inline ancestor asset by following the incoming relations, ie. the first asset that has a url. Returns the asset itself if it's not inline, and null if the asset is inline, but not in an AssetGraph.

Type:

origin :String

Source:
Inherited From:

The origin of the asset, protocol://host Corresponds to new URL(...).origin

For inlined assets, this will contain the origin of the first non-inlined ancestor

Type:
  • String

outgoingRelations :Array.<Relation>

Source:
Inherited From:

Get/set the outgoing relations of the asset.

Type:

parseTree :Oject

Source:
Inherited From:

Some asset classes support inspection and manipulation using a high level interface. If you modify the parse tree, you have to call asset.markDirty() so any cached serializations of the asset are invalidated.

These are the formats you'll get:

Html and Xml: jsdom document object (https://github.com/tmpvar/jsdom).

Css CSSOM CSSStyleSheet object (https://github.com/NV/CSSOM).

JavaScript estree AST object (via acorn).

Json Regular JavaScript object (the result of JSON.parse on the decoded source).

CacheManifest A JavaScript object with a key for each section present in the manifest (CACHE, NETWORK, REMOTE). The value is an array with an item for each entry in the section. Refer to the source for details.

Type:
  • Oject

password :String

Source:
Inherited From:

The password part of a URL protocol://username:<password>@hostname:port/

Type:
  • String

path :String

Source:
Inherited From:

The path of the asset relative to the AssetGraph root. Corresponds to a new URL(...).pathName

If updated, the url of the asset will also be updated.

Type:
  • String

port :Number

Source:
Inherited From:

The port part of a URL protocol://username:password@hostname:<port>/

Type:
  • Number

protocol :String

Source:
Inherited From:

The protocol part of a URL <protocol:>//username:password@hostname:port/. Includes trailing :

Type:
  • String

query :String

Source:
Inherited From:

The query parameters part of the Assets URL.

Can be set with a String or Object, but always returns String in the getters

Type:
  • String

rawSrc :Buffer

Source:
Overrides:

Get or set the raw source of the asset.

If the internal state has been changed since the asset was initialized, it will automatically be reserialized when this property is retrieved.

Type:
  • Buffer
Example
const htmlAsset = new AssetGraph().addAsset({
  type: 'Html',
  rawSrc: new Buffer('<html><body>Hello!</body></html>')
});
htmlAsset.parseTree.body.innerHTML = "Bye!";
htmlAsset.markDirty();
htmlAsset.rawSrc.toString(); // "<html><body>Bye!</body></html>"

text :String

Source:

Get or set the decoded text contents of the of the asset as a JavaScript string. Unlike browsers AssetGraph doesn't try to sniff the charset of your text-based assets. It will fall back to assuming utf-8 if it's unable to determine the encoding/charset from HTTP headers, <meta http-equiv='Content-Type'> tags (Html), @charset (Css), so if for some reason you're not using utf-8 for all your text-based assets, make sure to provide those hints. Other asset types provide no standard way to specify the charset within the file itself, so presently there's no way to load eg. JavaScript from disc if it's not utf-8 or ASCII, except by overriding Text.prototype.defaultEncoding globally.

If the internal state has been changed since the asset was initialized, it will automatically be reserialized when the text property is retrieved, for example:

var htmlAsset = new Html({
    rawSrc: Buffer.from("<body>hello</body>");
});
htmlAsset.text; // "<body>hello</body>"
htmlAsset.parseTree.body.innerHTML = "bye";
htmlAsset.markDirty();
htmlAsset.text; // "<body>bye</body>"

Setting this property after the outgoing relations have been accessed currently leads to undefined behavior.

Type:
  • String

type :String

Source:
Inherited From:

The assets defined or inferred type

Type:
  • String

url :String

Source:
Inherited From:

Get or set the absolute url of the asset.

The url will use the file: schema if loaded from disc. Will be falsy for inline assets.

Type:
  • String

urlOrDescription :String

Source:
Inherited From:

A Human readable URL or Asset description if inline. Paths for file:// URL's are kept relative to the current working directory for easier copy/paste if needed.

Type:
  • String

username :String

Source:
Inherited From:

The username part of a URL protocol://<username>:password@hostname:port/

Type:
  • String

Methods

addRelation(relation, positionopt, adjacentRelationopt)

Source:
Inherited From:

Attaches a Relation to the Asset.

The ordering of certain relation types is significant (HtmlScript, for instance), so it's important that the order isn't scrambled in the indices. Therefore the caller must explicitly specify a position at which to insert the object.

Parameters:
Name Type Attributes Default Description
relation Relation

The Relation to attach to the Asset

position String <optional>
'last'

"first", "last", "before", or "after"

adjacentRelation Relation <optional>

The adjacent relation, mandatory if the position is "before" or "after"

clone(incomingRelations) → {Asset}

Source:
Inherited From:

Clone this asset instance and add the clone to the graph if this instance is part of a graph. As an extra service, optionally update some caller-specified relations to point at the clone.

If this instance isn't inline, a url is made up for the clone.

Parameters:
Name Type Description
incomingRelations Array.<Relation> | Relation

(optional) Some incoming relations that should be pointed at the clone.

Returns:

The cloned asset.

Type
Asset

externalize()

Source:
Inherited From:

Externalize an inlined Asset.

This will create an URL from as many available URL parts as possible and auto generate the rest, then assign the URL to the Asset

findOutgoingRelationsInParseTree() → {Array.<Relation>}

Source:
Inherited From:

Parse the Asset for outgoing relations and return them.

Returns:

The Assets outgoing Relations

Type
Array.<Relation>

(async) load() → {Promise.<Asset>}

Source:
Inherited From:

Load the Asset

Returns a promise that is resolved when the asset is loaded. This is Asset's only async method, as soon as it is loaded, everything can happen synchronously.

Usually you'll want to use transforms.loadAssets, which will handle this automatically.

Returns:

The loaded Asset

Type
Promise.<Asset>

markDirty() → {Asset}

Source:
Overrides:

Sets the dirty flag of the asset, which is the way to say that the asset has been manipulated since it was first loaded (read from disc or loaded via http). For inline assets the flag is set if the asset has been manipulated since it was last synchronized with (copied into) its containing asset.

For assets that support a text or parseTree property, calling markDirty() will invalidate any cached serializations of the asset.

Returns:

The asset itself (chaining-friendly)

Type
Asset

populate()

Source:
Inherited From:

Go through the outgoing relations of the asset and add the ones that refer to assets that are already part of the graph. Recurses into inline assets.

You shouldn't need to call this manually.

removeRelation(relation) → {Asset}

Source:
Inherited From:

Remove an outgoing Relation from the Asset by reference

Parameters:
Name Type Description
relation Relation

Outgoing Relation

Returns:

The Asset itself

Type
Asset

replaceWith(newAsset) → {Asset}

Source:
Inherited From:

Replace the asset in the graph with another asset, then remove it from the graph.

Updates the incoming relations of the old asset to point at the new one and preserves the url of the old asset if it's not inline.

Parameters:
Name Type Description
newAsset Asset

The asset to put replace this one with.

Returns:

The new asset.

Type
Asset

toString() → {String}

Source:
Inherited From:

Get a brief text containing the type, id, and url (if not inline) of the asset.

Returns:

The string, eg. "[JavaScript/141 file:///the/thing.js]"

Type
String

unload()

Source:
Inherited From:

Unload the asset body. If the asset is in a graph, also remove the relations from the graph along with any inline assets. Also used internally to clean up before overwriting .rawSrc or .text.