Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
img-placeholder-src
Advanced tools
helper for using structured data to populate various image placeholder sources.
18kb/6kb
Demo showing keys features. For code examples and docs, see below.
npm install img-placeholder-src --save
var IPS = require('img-placeholder-src');
var ips = new IPS;
var imageData = {
height: 100,
width: 100
};
var src = ips.src(imageData, 'placeholdit');
console.log(src);
/*
http://placehold.it/100x100
*/
var IPS = require('img-placeholder-src');
var ips = new IPS;
var srcsetData = [
{
height: 100,
width: 100
},
{
height: 200,
width: 200
}
];
var srcset = ips.srcset(srcsetData, 'placeholdit');
console.log(srcset);
/*
http://placehold.it/100x100 100w, http://placehold.it/200x200 200w
*/
Download the bundled script file img-placeholder-src.bundle.js
. There is also a none bundle version available, but you will have to include the srcset dependency yourself.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="img-placeholder-src.bundle.js"></script>
<script>
// Initialize/Configure IPS
var ips = new IPS({
serviceOverride: 'fillmurray'
});
// Create image data object
var imageData = {
height: 100,
width: 100
};
// generate image source
var src = ips.src(imageData);
// create and image element
var img1 = document.createElement("IMG");
// set the image src attribute
img1.setAttribute('src', src);
// append the image to the body
document.body.appendChild(img1);
</script>
</body>
Each service supports different placeholder variations. An image data object that contains all types of image variations could include the following attributes.
Attribute | Options | Description |
---|---|---|
width | integer | Width of the image |
height | integer | Height of the image |
filter | string | Optional. Image filter (provided by service) |
foreground | string | Optional. Image foreground/text color |
background | string | Optional. Image background color |
format | string | Optional. Image format (gif, jpeg, jpg, png) |
text | string | Optional. Text displayed in the image |
category | string | Optional. Image category provided by service |
delay | integer | Optional. Response delay from server |
brand | string | Optional. Brand image |
flag | string | Optional. Flag image |
texture | string | Optional. Texture applied to backgrounds |
There are several settings that can be applied to the IPS configuration. They include:
var IPS = require('img-placeholder-src');
var ips = new IPS({
serviceOverride: null, // override service name ex: 'placecage'
service: 'placeholdit', // default service name ex: 'fillmurray'
protocol: null // protocol prepended to src url ex: 'https'
});
Accepts an image data object containing at least a height and width. If the optional unique
attribute is passed, the image src size will be incremented by the unique
value. This should be the index within a list. This forces the image services to send a different image instead of sending the same image if a duplicate size is requested. For example, the output would look like:
var data = [
{height: 300, width: 300},
{height: 300, width: 300},
{height: 300, width: 300}
]
list.forEach(function(item, index) {
console.log( ips.srcset(data, 'placecage', {unique:index}) );
}
/*
http://placecage.com/300/300 ...
http://placecage.com/301/301 ...
http://placecage.com/302/302 ...
*/
Accepts an array of image data objects and returns a string of comma seperated source references and sizes. Optional options
can be passed to the internal src()
call.
Registers a new image service function.
Attribute | Options | Description |
---|---|---|
name | string | Name of registered placeholder service |
render | function | [data] accepts settings that will populate the image source string which it returns. A template compile function could be used here. |
modifier | function | Optional. Additional logic to modify data passed to the image template. Accepts a data object and must: return the modifed data object. |
ips.register({
name: 'placekitten',
render: function(args) {
return (typeof(args.protocol) !== 'undefined' ? args.protocol : "")
+ '//placekitten.com'
+ (typeof(args.filter) !== 'undefined' ? '/'+args.filter : "")
+ '/' + args.width + '/' + args.width;
},
modifier: function(data) {
return data;
}
});
var imageData = {
height: 100,
width: 100,
filter: 'greyscale'
};
var src = ips.src(imageData, 'placekitten');
console.log(src);
/*
//placekitten.com/g/100x100
*/
see services.es6.js
for modifier example
There are shorthand functions for each service. Although, I would recommend to use the base src
or srcset
functions. Why? In a application you could conditionally override every placeholder src by setting a configuration variable. For example:
var serviceOverride = 'fillmurray';
var ips = new IPS({
serviceOverride: 'fillmurray'
});
<img src="{{ ips.src(data, 'placeholdit') }}"/>
If serviceOverride
is set, all image sources would be replaced with fillmurray
sources rather than placeholdit
. This allows for quickly changing image sources.
By default, these services are supported with no extra configuration. New services can be added by passing a serviceData
object to the ips.register
function. See the API for reference
Populating actual content for a front-end that will later be integrated into a CMS is a waste of time. Placeholder content is more efficient and there are several image services out there that you can use. My default has always been PLACEHOLD.IT because the file sizes are small, the dimensions are displayed which is helpful for integrators, the image can be customized in a bunch of different ways, and there's no awkward conversation with the client about why Bill Murray is all over their site. The biggest problem with PLACEHOLD.IT is that the image files are so small. They don't accurately represent the final site with actual image content and weight.
As developers, we should always be testing and optimizing our code to be as efficient and accessible as possible. To do that we need more realistic placeholder content. We need to be able to test with actual images while also using simpler placeholder content for client reviews. This would require manually updating image sources which could be very tedious and time consuming, especially if you are using responsive image techniques. Instead, this module makes it possible to define placeholder image attributes like height
and width
, then generate the service image src
and srcset
attribute on demand.
npm run watch
npm test
- runs application mocha tests
npm run watch
- runs the wepack build and mocha tests. Watches for new changes
npm run build:dev
- runs the wepack build
npm run dev
- runs the webpack build and tests the application for errors.
npm run build
- builds various versions of the script to be used on the demo site and by browser
pushes current checked out branch to the remote github-pages (gh-pages
) branch
npm run deploy
Latest Chrome
Latest Safari
Latest Firefox
Latest Mobile Safari
IE 9+
Node 0.10+ via TravisCI
Checkout the Github release feed
FAQs
Create placeholder image src for various services
The npm package img-placeholder-src receives a total of 13 weekly downloads. As such, img-placeholder-src popularity was classified as not popular.
We found that img-placeholder-src demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.