Skip to content

Commit faf010c

Browse files
authored
Merge pull request #9 from platformsh/add-chrome-formatter
Add chrome/puppeteer formatter
2 parents 20b1b98 + 498d610 commit faf010c

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ config.registerFormatter("my_service", formatMyService);
136136

137137
// Then call this method to get the formatted version
138138

139-
formatted = config.FormattedCredentials("database", "my_service");
139+
formatted = config.formattedCredentials("database", "my_service");
140140
```
141141
142142
The first parameter is the name of a relationship defined in `.platform.app.yaml`. The second is a formatter that was previously registered with `registerFormatter()`. If either the service or formatter is missing an exception will be thrown. The type of `formatted` will depend on the formatter function and can be safely passed directly to the client library.

src/platformsh.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Config {
5252

5353
this.registerFormatter('solr-node', nodeSolrFormatter);
5454
this.registerFormatter('mongodb', mongodbFormatter);
55+
this.registerFormatter('puppeteer', puppeteerFormatter);
5556

5657
let variables = this._getValue('VARIABLES');
5758
if (variables) {
@@ -525,6 +526,17 @@ function mongodbFormatter(credentials) {
525526
return `mongodb://${credentials["username"]}:${credentials["password"]}@${credentials["host"]}:${credentials["port"]}/${credentials["path"]}`;
526527
}
527528

529+
/**
530+
* Returns a connection string appropriate for Puppeteer and headless Chrome.
531+
* @param cretentials
532+
* A chrome-headless credentials object
533+
* @returns {string}
534+
* A connection string to pass to puppeteer.connect().
535+
*/
536+
function puppeteerFormatter(credentials) {
537+
return `http://${credentials["ip"]}:${credentials["port"]}`;
538+
}
539+
528540
/**
529541
* Creates a new Config instance that represents the current environment.
530542
*

test/testdata/PLATFORM_RELATIONSHIPS.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,18 @@
5252
"hostname" : "dg3c6xro44gl5tuxqbqf5flvhu.solr.service._.eu-3.platformsh.site",
5353
"scheme" : "solr"
5454
}
55+
],
56+
"headless": [
57+
{
58+
"service": "headless",
59+
"ip": "169.254.16.215",
60+
"hostname": "vjteswvo72bpyb33rgijkpooja.headless.service._.eu-3.platformsh.site",
61+
"cluster": "moqwtrvgc63mo-pr-5-kehpj4q",
62+
"host": "headless.internal",
63+
"rel": "http",
64+
"scheme": "http",
65+
"type": "chrome-headless:73",
66+
"port": 9222
67+
}
5568
]
5669
}

test/tests.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@ describe("Config tests", () => {
345345
});
346346
});
347347

348+
it('formatts a puppeteer connection', () => {
349+
let c = new psh.Config(mockEnvironmentRuntime);
348350

349-
});
351+
let formatted = c.formattedCredentials('headless', 'puppeteer')
352+
353+
assert.equal(formatted, 'http://169.254.16.215:9222')
354+
});
355+
});
350356

351357
});

0 commit comments

Comments
 (0)