spectaql
Version:
A powerful library for autogenerating static GraphQL API documentation
124 lines (112 loc) • 4.06 kB
text/coffeescript
###
resource-embedder
https://github.com/callumlocke/resource-embedder
Copyright 2013 Callum Locke
Licensed under the MIT license
###
parseFileSize = require './parse-file-size'
fs = require 'fs'
path = require 'path'
reorientCSS = require '../../reorient-css'
module.exports = class Resource
constructor: (, , ) ->
isEmbeddable: (callback) ->
if ?
callback
else
if ()
threshold = ()
if !threshold
callback = false
else
(notFound, missingFilePath) =>
if (notFound)
= "File does not exist: #{missingFilePath}"
callback = false
else
callback = (threshold > ())
else
callback = false
potentiallyEmbeddable: (tagName=, attributes=, options=, self=@) ->
# This is purely a hack for testing
self = {} if self is undefined
# Aims to return false as quickly as possible if we can be sure it's NOT embeddable
# based solely on tagName, attributes, or options (NB: we're not checking the
# threshold yet). Otherwise returns true, meaning it *might* be embeddable (as far as
# we can tell without looking at the file contents).
switch tagName
when 'script'
self.target = attributes.src
return false if (
!options.scripts ||
!(
switch attributes.type
when null, undefined, '', 'application/javascript', 'text/javascript'
true
else false
) ||
attributes.defer? ||
attributes.async? ||
!Resource::isLocalPath(attributes.src)
)
when 'link'
self.target = attributes.href
return false if (
!options.stylesheets ||
attributes.rel != 'stylesheet' ||
!(
switch attributes.media
when null, undefined, '', 'all'
true
else false
) ||
!Resource::isLocalPath(attributes.href)
)
else
return false
return true
getThreshold: (embedAttr, options) ->
# The signature was formerly this, so let's make it act like it:
#getThreshold: (embedAttr=?['data-embed'], options=) ->
self = @ || {}
embedAttr = self.attributes?['data-embed'] if embedAttr is undefined
options = self.options if options is undefined
switch embedAttr
when 'false', '0' then 0
when null, undefined then parseFileSize options.threshold
when '' then Infinity
else parseFileSize embedAttr
getContentsForEmbedding: (callback) ->
# Returns the contents of the file, but trimmed, and run through reorient-css
# (if CSS).
relFilePath = (if is 'script' then .src else .href)
= path.resolve(path.join(.assetRoot, relFilePath))
fs.exists , (exists) =>
if !exists
callback(true, ) # true means error (file not found)
else
fs.readFile , (err, ) =>
throw err if err
if is 'link'
= path.dirname .href
= reorientCSS(
.toString(),
.htmlFile
).css
= .toString().trim()
callback()
getByteLength: (contents=) ->
if !contents?
throw new Error 'UNDEFINED for some reason!'
if typeof contents is 'string'
Buffer.byteLength(contents, 'utf8')
else
contents.length
isLocalPath: (filePath, mustBeRelative=false) ->
(
filePath && filePath.length &&
(filePath.indexOf('//') == -1) &&
(filePath.indexOf('data:') != 0) &&
(!mustBeRelative || filePath[0] != '/')
)