Comparing version 0.1.0 to 0.2.0
@@ -26,2 +26,3 @@ var Icns = require( './icns' ) | ||
this.osVersion = null | ||
this.isValid = true; | ||
@@ -49,5 +50,12 @@ } | ||
var type = Icns.TYPE[ this.type ] | ||
var type = null; | ||
if( type == null ) { | ||
if (!Icns.TYPE.hasOwnProperty(this.type)) { | ||
this.isValid = false | ||
return this | ||
} else { | ||
type = Icns.TYPE[ this.type ] | ||
} | ||
if( type == null) { | ||
throw new Error( `Unknown icns resource type "${this.type}"` ) | ||
@@ -54,0 +62,0 @@ } |
139
lib/icns.js
@@ -14,3 +14,10 @@ var fs = require( 'fs' ) | ||
this.filename = filename | ||
if (filename instanceof Buffer) { | ||
this.filename = 'buffer' | ||
this._buffer = filename | ||
} else { | ||
this.filename = filename | ||
this._buffer = null; | ||
} | ||
this.fd = null | ||
@@ -20,3 +27,2 @@ this.options = Object.assign({}, Icns.defaults, options) | ||
this.entries = [] | ||
} | ||
@@ -32,13 +38,85 @@ | ||
readHeaderFromBuffer( callback ) { | ||
var buffer = Buffer.alloc( 8 ) | ||
var offset = 0 | ||
var length = buffer.length | ||
var position = 0 | ||
this._buffer.copy(buffer, offset, 0, 8); | ||
this.header.parse( buffer ) | ||
callback && callback() | ||
}, | ||
readEntriesFromBuffer( callback ) { | ||
var buffer = Buffer.alloc( 16 ) | ||
var length = buffer.length | ||
var position = 8 | ||
var offset = position + length | ||
this.entries.length = 0 | ||
var read = ( next ) => { | ||
this._buffer.copy(buffer, 0, position, position + 16) | ||
var entry = new Icns.IconEntry().parse( buffer ) | ||
entry.offset = position | ||
if (entry.isValid) { | ||
if( buffer.indexOf( Icns.JPEG2000, 8 ) === 8 ) { | ||
entry.format = 'jpeg2000' | ||
} else if( buffer.indexOf( Icns.PNG, 8 ) === 8 ) { | ||
entry.format = 'png' | ||
} | ||
} | ||
position += entry.length | ||
next( null, entry ) | ||
} | ||
var run = ( error, entry ) => { | ||
if( error ) { | ||
return callback( error ) | ||
} | ||
if( entry ) { | ||
this.entries.push( entry ) | ||
} | ||
if( position < this.header.length ) { | ||
read( run ) | ||
} else { | ||
callback( null, this.entries ) | ||
} | ||
} | ||
run() | ||
}, | ||
open( callback ) { | ||
fs.open( this.filename, this.options.flags, this.options.mode, ( error, fd ) => { | ||
if( error ) return callback( error ) | ||
this.fd = fd | ||
this.readHeader(( error ) => { | ||
if( error ) return callback( error ) | ||
this.readEntries(( error ) => { | ||
if ( this._buffer != null ) { | ||
this.readHeaderFromBuffer(( error ) => { | ||
if ( error ) return callback( error ) | ||
this.readEntriesFromBuffer(( error ) => { | ||
callback( error ) | ||
}) | ||
}) | ||
}) | ||
} else { | ||
fs.open( this.filename, this.options.flags, this.options.mode, ( error, fd ) => { | ||
if( error ) return callback( error ) | ||
this.fd = fd | ||
this.readHeader(( error ) => { | ||
if( error ) return callback( error ) | ||
this.readEntries(( error ) => { | ||
callback( error ) | ||
}) | ||
}) | ||
}) | ||
} | ||
}, | ||
@@ -86,6 +164,8 @@ | ||
if( buffer.indexOf( Icns.JPEG2000, 8 ) === 8 ) { | ||
entry.format = 'jpeg2000' | ||
} else if( buffer.indexOf( Icns.PNG, 8 ) === 8 ) { | ||
entry.format = 'png' | ||
if (entry.isValid) { | ||
if( buffer.indexOf( Icns.JPEG2000, 8 ) === 8 ) { | ||
entry.format = 'jpeg2000' | ||
} else if( buffer.indexOf( Icns.PNG, 8 ) === 8 ) { | ||
entry.format = 'png' | ||
} | ||
} | ||
@@ -119,3 +199,2 @@ | ||
run() | ||
}, | ||
@@ -132,16 +211,22 @@ | ||
fs.read( this.fd, buffer, offset, length, position, ( error, bytesRead, buffer ) => { | ||
if( !error && ( bytesRead !== length ) ) { | ||
error = new Error( `Bytes read mismatch; expected ${length}, read ${bytesRead}` ) | ||
} | ||
// TODO: Run-length encoding | ||
// if( entry.format === 'raw' ) { | ||
// console.log( 'input', buffer ) | ||
// var channels = Icns.rle.decode( entry, buffer ) | ||
// console.log( 'channels', channels ) | ||
// buffer = Icns.rle.continuous( entry, channels ) | ||
// } | ||
callback( error, buffer ) | ||
}) | ||
if ( this._buffer != null ) { | ||
this._buffer.copy( buffer, offset , position, position + length) | ||
callback( '', buffer ) | ||
} else { | ||
fs.read( this.fd, buffer, offset, length, position, ( error, bytesRead, buffer ) => { | ||
if( !error && ( bytesRead !== length ) ) { | ||
error = new Error( `Bytes read mismatch; expected ${length}, read ${bytesRead}` ) | ||
} | ||
// TODO: Run-length encoding | ||
// if( entry.format === 'raw' ) { | ||
// console.log( 'input', buffer ) | ||
// var channels = Icns.rle.decode( entry, buffer ) | ||
// console.log( 'channels', channels ) | ||
// buffer = Icns.rle.continuous( entry, channels ) | ||
// } | ||
callback( error, buffer ) | ||
}) | ||
} | ||
}, | ||
@@ -148,0 +233,0 @@ |
{ | ||
"name": "apple-icns", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Apple IconSet (.icns) file format", | ||
@@ -5,0 +5,0 @@ "author": "Jonas Hermsmeier <jhermsmeier@gmail.com> (https://jhermsmeier.de)", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17723
361