Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinmombay committed Nov 20, 2015
2 parents 42787e9 + 44d0e13 commit c7a3fdc
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 17 deletions.
25 changes: 19 additions & 6 deletions build-system/tasks/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

var argv = require('minimist')(process.argv.slice(2));
var assert = require('assert');
var BBPromise = require('bluebird');
var config = require('../config');
var extend = require('util')._extend;
Expand Down Expand Up @@ -50,9 +51,11 @@ function changelog() {

function getGitMetadata() {
var version = argv.version;
var versionErrMsg = 'No version option passed';

if (!version) {
throw new Error('No version option passed');
util.log(util.colors.red(versionErrMsg));
throw new Error(versionErrMsg);
}

var gitMetadata = {};
Expand All @@ -63,12 +66,14 @@ function getGitMetadata() {
.then(fetchGithubMetadata)
.then(buildChangelog.bind(null, gitMetadata))
.then(function(gitMetadata) {
util.log(util.colors.blue('\n' + gitMetadata.changelog));
return submitReleaseNotes(version, gitMetadata.changelog);
})
.catch(errHandler);
}

function submitReleaseNotes(version, changelog) {
assert(typeof version == 'number', 'version should be a number. ' + version);
var options = {
url: 'https://api.github.com/repos/ampproject/amphtml/releases',
method: 'POST',
Expand Down Expand Up @@ -169,6 +174,7 @@ function fetchGithubMetadata(ids) {
function getPullRequestTitle(prOption) {
return request(prOption).then(function(res) {
var body = JSON.parse(res.body);
assert(typeof body.url == 'string', 'should have url string. ' + res.body);
var url = body.url.split('/');
var pr = url[url.length - 1];
return body.title + ' (#' + pr + ')';
Expand All @@ -179,9 +185,8 @@ function getPullRequestFiles(title, filesOption) {
return request(filesOption).then(function(res) {
var body = JSON.parse(res.body);

if (!body || !Array.isArray(body)) {
throw new Error('Could not get Pull Request Files.');
}
assert(Array.isArray(body) && body.length > 0,
'Pull request response must not be empty. ' + res.body);
var filenames = body.map(function(file) {
return file.filename;
});
Expand All @@ -205,19 +210,27 @@ function onGitTagSuccess(gitMetadata, tag) {

function onGitLogSuccess(gitMetadata, logs) {
var commits = logs.split('\n');
assert(typeof logs == 'string', 'git log should be a string.\n' + logs);
return commits
.filter(function(commit) {
// filter non Pull request merges
return commit.indexOf('Merge pull') == 0;
})
.map(function(commit) {
// We only need the PR id
return commit.split(' ')[3].slice(1);
var id = commit.split(' ')[3].slice(1);
var value = parseInt(id, 10);
assert(value > 0, 'Should be an integer greater than 0. ' + value);
return id;
});
}

function errHandler(err) {
util.log(util.colors.red(err));
var msg = err;
if (err.message) {
msg = err.message;
}
util.log(util.colors.red(msg));
return err;
}

Expand Down
1 change: 1 addition & 0 deletions builtins/amp-pixel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {registerElement} from '../src/custom-element';
*/
export function installPixel(win) {

/** @const {!UrlReplacements} */
const urlReplacements = new UrlReplacements(win);

class AmpPixel extends BaseElement {
Expand Down
2 changes: 1 addition & 1 deletion builtins/amp-pixel.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ For instance:
```html
<amp-pixel src="https://foo.com/pixel?RANDOM"></amp-pixel>
```
may make a request to something like `https://foo.com/pixel?0.8390278471201` where the $RANDOM value is randomly generated upon each impression.
may make a request to something like `https://foo.com/pixel?0.8390278471201` where the RANDOM value is randomly generated upon each impression.

#### Styling

Expand Down
2 changes: 2 additions & 0 deletions extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Current list of extended components:
| [`amp-fit-text`](amp-fit-text/amp-fit-text.md) | Expand or shrink font size to fit the content within the space given. |
| [`amp-font`](amp-font/amp-font.md) | Trigger and monitor the loading of custom fonts. |
| [`amp-iframe`](amp-iframe/amp-iframe.md) | Displays an iframe. |
| [`amp-install-serviceworker`](amp-install-serviceworker/amp-install-serviceworker.md) | Installs a ServiceWorker.
| [`amp-image-lightbox`](amp-image-lightbox/amp-image-lightbox.md) | Allows for a “image lightbox” or similar experience. |
| [`amp-instagram`](amp-instagram/amp-instagram.md) | Displays an instagram embed. |
| [`amp-lightbox`](amp-lightbox/amp-lightbox.md) | Allows for a “lightbox” or similar experience. |
| [`amp-list`](amp-list/amp-list.md) | A dynamic list that can download data and create list items using a template |
| [`amp-twitter`](amp-twitter/amp-twitter.md) | Displays a Twitter Tweet. |
| [`amp-youtube`](amp-youtube/amp-youtube.md) | Displays a Youtube video. |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Layout} from '../../../src/layout';
import {parseUrl, assertHttpsUrl} from '../../../src/url';
import {getMode} from '../../../src/mode';

/**
* Implements custom element: <amp-install-serviceworker>
* for installation of ServiceWorkers owned by the publisher
* of the current page.
*/
class AmpServiceWorkerInstallation extends AMP.BaseElement {
/** @override */
buildCallback() {
const win = this.getWin();
if (!('serviceWorker' in win.navigator)) {
return;
}
const src = this.element.getAttribute('src');
assertHttpsUrl(src, this.element);

if (originMatches(win.location.href, src)) {
install(this.getWin(), src);
} else {
if (getMode().development) {
console./* OK */warn(
'Did not install ServiceWorker because it does not ' +
'match the current origin: ' + src);
}
}
}
}

/**
* Returns true if the 2 hrefs are on the same origin.
* @param {string} href1
* @param {string} href2
* return {boolean}
*/
function originMatches(href1, href2) {
return parseUrl(href1).origin == parseUrl(href2).origin;
}

/**
* Installs the service worker at src via direct service worker installation.
* @param {!Window} win
* @param {string} src
*/
function install(win, src) {
win.navigator.serviceWorker.register(src).then(function(registration) {
if (getMode().development) {
console./*OK*/info('ServiceWorker registration successful with scope: ',
registration.scope);
}
}).catch(function(e) {
console./*OK*/error('ServiceWorker registration failed:', e);
});
}

AMP.registerElement('amp-install-serviceworker',
AmpServiceWorkerInstallation);
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require('../../../../build/all/v0/amp-install-serviceworker-0.1.max');
import {adopt} from '../../../../src/runtime';

adopt(window);

describe('amp-install-serviceworker', () => {
it('should install for same origin', () => {
const install = document.createElement('amp-install-serviceworker');
const implementation = install.implementation_;
expect(implementation).to.be.defined;
install.setAttribute('src', 'https://example.com/sw.js');
let calledSrc;
const p = new Promise(resolve => {});
implementation.getWin = () => {
return {
location: {
href: 'https://example.com/some/path'
},
navigator: {
serviceWorker: {
register: src => {
expect(calledSrc).to.be.undefined;
calledSrc = src;
return p;
}
}
}
};
};
implementation.buildCallback();
expect(calledSrc).to.equal('https://example.com/sw.js');
});

it('should be ok without service worker.', () => {
const install = document.createElement('amp-install-serviceworker');
const implementation = install.implementation_;
expect(implementation).to.be.defined;
install.setAttribute('src', 'https://example.com/sw.js');
implementation.getWin = () => {
return {
location: {
href: 'https://example.com/some/path'
},
navigator: {
}
};
};
implementation.buildCallback();
});

it('should do nothing with non-matching origins', () => {
const install = document.createElement('amp-install-serviceworker');
const implementation = install.implementation_;
expect(implementation).to.be.defined;
install.setAttribute('src', 'https://other-origin.com/sw.js');
let calledSrc;
const p = new Promise(resolve => {});
implementation.getWin = () => {
return {
location: {
href: 'https://example.com/some/path'
},
navigator: {
serviceWorker: {
register: src => {
calledSrc = src;
return p;
}
}
}
};
};
implementation.buildCallback();
expect(calledSrc).to.undefined;
});
});
48 changes: 48 additions & 0 deletions extensions/amp-install-serviceworker/amp-install-serviceworker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!---
Copyright 2015 The AMP HTML Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

### <a name="amp-install-serviceworker"></a> `amp-install-serviceworker`

The `amp-install-serviceworker` component allows installing a [ServiceWorker](http://www.html5rocks.com/en/tutorials/service-worker/introduction/) for the current page.

The idea here is that this ServiceWorker runs whenever the AMP file is served from the origin where you publish the AMP file. The ServiceWorker will not be loaded when the document is loaded from an AMP cache.

See [this article](https://medium.com/@cramforce/amps-and-websites-in-the-age-of-the-service-worker-8369841dc962) for how ServiceWorkers can help with making the AMP experience awesome with ServiceWorkers.

### Example

```html

<amp-install-serviceworker
src="https://www.your-domain.com/serviceworker.js"
layout="nodisplay"
</amp-install-serviceworker>

```

### Behavior

Registers the ServiceWorker given by the `src` attribute. If the current origin is different from the origin of the ServiceWorker, this custom element does nothing (Emits warning in development mode).

### Attributes

#### `src`

URL of the ServiceWorker to register.

#### `layout`

Must have the value `nodisplay`.
Loading

0 comments on commit c7a3fdc

Please sign in to comment.