Skip to content

Commit 10c0eac

Browse files
authored
Credentials file authentication & build fixes (#60)
* Fix runtime api & introduce jwt auth * Improve build * Increase page size * Bump version
1 parent 51258ad commit 10c0eac

16 files changed

+60
-58
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "0.1.0",
2+
"version": "0.2.0",
33
"repository": "https://github.com/guess-js/guess"
44
}

infra/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const build = (hook = (path: string) => {}) => {
3939

4040
for (const p of Packages) {
4141
const path = join(PackagesDir, p);
42-
console.log(execSync(`cd ${path} && rm -rf dist && ${cwd}/node_modules/.bin/webpack .`).toString());
42+
console.log(execSync(`cd ${path} && rm -rf dist && ${cwd}/node_modules/.bin/webpack`).toString());
4343
const packageJsonPath = join(path, 'package.json');
4444
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
4545
packageJson.version = config.version;

packages/ga/src/ga.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getClient } from './client';
22
import { normalize } from './normalize';
33
import { Graph, Period } from '../../common/interfaces';
44

5-
const PageSize = 1000;
5+
const PageSize = 10000;
66
const id = (r: string) => r;
77
const DefaultExpression = 'ga:pageviews';
88

packages/ga/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
mode: 'development',
2+
mode: 'production',
33
entry: './index.ts',
44
target: 'node',
55
output: {

packages/parser/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
mode: 'development',
2+
mode: 'production',
33
entry: './index.ts',
44
target: 'node',
55
output: {

packages/webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "guess-webpack",
33
"version": "0.1.0",
44
"description": "Webpack plugins for the Machine Learning-driven bundler",
5-
"main": "webpack/index.js",
5+
"main": "webpack/main.js",
66
"types": "webpack/index.d.ts",
77
"repository": {
88
"type": "git",

packages/webpack/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export const guess = (current: string, links?: string[]) =>
2-
((typeof window === 'undefined' ? global : window) as any).__GUESS__.guess(current, links);
1+
export const guess = (params: any) =>
2+
((typeof window === 'undefined' ? global : window) as any).__GUESS__.guess(params);

packages/webpack/src/ga-provider.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { auth } from 'google-oauth2-node';
1+
import { auth as oauth2 } from 'google-oauth2-node';
22
import { RoutingModule, Period, Graph } from '../../common/interfaces';
33
import { fetch } from 'guess-ga';
44

@@ -13,6 +13,7 @@ const cache = flatCache.load('guess-plugin');
1313
const id = <T>(r: T) => r;
1414

1515
export interface Config {
16+
jwt?: any;
1617
viewId: string;
1718
routes: RoutingModule[];
1819
formatter?: (path: string) => string;
@@ -28,19 +29,26 @@ export const getReport = (c: Config): Promise<Graph> => {
2829
if (report) {
2930
return Promise.resolve(JSON.parse(report));
3031
}
31-
return auth({
32-
clientId,
33-
clientSecret,
34-
scope
35-
})
36-
.then((token: any) => {
37-
const { google } = require('googleapis');
32+
const { google } = require('googleapis');
33+
let client: Promise<{}> = Promise.reject('Non-existing GA token');
34+
if (!c.jwt) {
35+
client = oauth2({
36+
clientId,
37+
clientSecret,
38+
scope
39+
}).then((token: any) => {
3840
const oauth2Client = new google.auth.OAuth2();
3941
oauth2Client.setCredentials(token);
40-
42+
return oauth2Client;
43+
});
44+
} else {
45+
client = Promise.resolve(new google.auth.JWT(c.jwt.client_email, null, c.jwt.private_key, [scope], null));
46+
}
47+
return client
48+
.then((auth: any) => {
4149
return fetch({
4250
viewId: c.viewId,
43-
auth: oauth2Client,
51+
auth,
4452
period: period,
4553
routes: c.routes.map(r => r.path),
4654
formatter: c.formatter || id

packages/webpack/src/guess-webpack.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ export interface RuntimeConfig {
1616

1717
export interface GuessPluginConfig {
1818
GA?: string;
19+
jwt?: any;
20+
period?: Period;
1921
reportProvider?: (...args: any[]) => Promise<Graph>;
2022
mode?: Mode;
2123
layout?: ProjectLayout;
22-
period?: Period;
24+
2325
/** @internal */
24-
routeFormatter?: (path: string) => string;
26+
routeProvider?: RouteProvider | boolean;
2527
/** @internal */
26-
debug?: boolean;
28+
routeFormatter?: (path: string) => string;
2729
/** @internal */
2830
runtime?: RuntimeConfig;
29-
/** @internal */
30-
routeProvider?: RouteProvider | boolean;
3131
}
3232

3333
export class GuessPlugin {
3434
constructor(private _config: GuessPluginConfig) {
35-
if (this._config.GA && this._config.reportProvider) {
35+
if ((this._config.GA || this._config.jwt) && this._config.reportProvider) {
3636
throw new Error(
37-
'Only a single report provider is allowed. You have specified `GA` (used by Google Analytics provider) and `reportProvider`'
37+
'Only a single report provider is allowed. You have specified `GA` and/or ' +
38+
'a GA authentication provider (used by Google Analytics provider) and `reportProvider`'
3839
);
3940
}
4041
if (!this._config.GA && !this._config.reportProvider) {
@@ -50,11 +51,12 @@ export class GuessPlugin {
5051

5152
private _execute(compilation: any, cb: any) {
5253
extractRoutes(this._config).then(routes => {
53-
this._getReport(routes).then(
54+
return this._getReport(routes).then(
5455
data => {
5556
return this._executePrefetchPlugin(data, routes, compilation, cb);
5657
},
5758
err => {
59+
console.error(err);
5860
cb();
5961
throw err;
6062
}
@@ -65,6 +67,7 @@ export class GuessPlugin {
6567
private _getReport(routes: RoutingModule[]): Promise<Graph> {
6668
if (this._config.GA) {
6769
return getReport({
70+
jwt: this._config.jwt,
6871
viewId: this._config.GA,
6972
routes,
7073
formatter: this._config.routeFormatter,
@@ -81,7 +84,6 @@ export class GuessPlugin {
8184
data,
8285
basePath: runtime ? (runtime.basePath === undefined ? '' : runtime.basePath) : '',
8386
prefetchConfig: runtime ? runtime.prefetchConfig : undefined,
84-
debug: this._config.debug,
8587
routes,
8688
delegate: runtime ? !!runtime.delegate : false
8789
}).execute(compilation, cb);

packages/webpack/src/prefetch-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class PrefetchPlugin {
8282
context: '/src/',
8383
mode: 'production',
8484
entry: './index.js',
85-
target: 'web',
85+
target: 'node',
8686
output: {
8787
filename: './output.js'
8888
}
@@ -100,7 +100,7 @@ export class PrefetchPlugin {
100100
}
101101

102102
const code = stats.compilation.assets['./output.js'].source();
103-
compilation.assets[mainName] = new ConcatSource(code, '\n', old.source());
103+
compilation.assets[mainName] = new ConcatSource(code, '\n;', old.source());
104104
callback();
105105
});
106106
}

packages/webpack/src/runtime/guess.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { initialize } from './guess';
22

33
(function(g, graph, m, basePath, thresholds) {
44
initialize(g, graph, m, basePath, thresholds);
5-
})(typeof window === 'undefined' ? {} : window, <%= GRAPH %>, <%= GRAPH_MAP %>, <%= THRESHOLDS %>);
5+
})(typeof window === 'undefined' ? global : window, <%= GRAPH %>, <%= GRAPH_MAP %>, <%= THRESHOLDS %>);

packages/webpack/src/runtime/guess.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ const matchRoute = (route: string, declaration: string) => {
8282
}
8383
};
8484

85-
const polyfillConnection = {
86-
effectiveType: '3g'
87-
};
88-
8985
const guessNavigation = (graph: Graph, params: GuessFnParams): Navigations => {
9086
const matches = graph.findMatch(params.path);
9187
return matches.reduce(

packages/webpack/src/runtime/runtime.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { initialize } from './runtime';
22

33
(function(g, history, graph, m, basePath, thresholds) {
44
initialize(history, g, graph, m, basePath, thresholds);
5-
})(typeof window === 'undefined' ? {} : window, (typeof window === 'undefined' ? {} : window).history, <%= GRAPH %>, <%= GRAPH_MAP %>, '<%= BASE_PATH %>', <%= THRESHOLDS %>);
5+
})(typeof window === 'undefined' ? global : window, (typeof window === 'undefined' ? {} : window).history, <%= GRAPH %>, <%= GRAPH_MAP %>, '<%= BASE_PATH %>', <%= THRESHOLDS %>);

packages/webpack/test/fixtures/delegate/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const CopyWebpackPlugin = require('copy-webpack-plugin');
2-
const { GuessPlugin } = require('../../../dist/webpack/index');
2+
const { GuessPlugin } = require('../../../dist/webpack/main');
33

44
module.exports = {
55
mode: 'development',

packages/webpack/test/fixtures/prefetch/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { join } = require('path');
22
const CopyWebpackPlugin = require('copy-webpack-plugin');
3-
const { GuessPlugin } = require('../../../dist/webpack/index');
3+
const { GuessPlugin } = require('../../../dist/webpack/main');
44

55
const absolute = path => {
66
return join(__dirname, path);

packages/webpack/webpack.config.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ const common = {
2222
};
2323

2424
module.exports = [
25+
Object.assign(
26+
{
27+
entry: './src/api.ts',
28+
output: {
29+
filename: 'index.js',
30+
path: __dirname + '/dist/api/',
31+
libraryTarget: 'commonjs'
32+
},
33+
target: 'web'
34+
},
35+
common
36+
),
2537
Object.assign(
2638
{
2739
entry: {
2840
runtime: './src/runtime/runtime.ts'
2941
},
30-
target: 'web',
42+
target: 'node',
3143
output: {
3244
filename: '[name].js',
3345
path: __dirname + '/dist/webpack/',
34-
libraryTarget: 'umd'
46+
libraryTarget: 'commonjs'
3547
}
3648
},
3749
common
@@ -41,11 +53,11 @@ module.exports = [
4153
entry: {
4254
guess: './src/runtime/guess.ts'
4355
},
44-
target: 'web',
56+
target: 'node',
4557
output: {
4658
filename: '[name].js',
4759
path: __dirname + '/dist/webpack/',
48-
libraryTarget: 'umd'
60+
libraryTarget: 'commonjs'
4961
}
5062
},
5163
common
@@ -54,7 +66,7 @@ module.exports = [
5466
{
5567
entry: './index.ts',
5668
output: {
57-
filename: 'index.js',
69+
filename: 'main.js',
5870
path: __dirname + '/dist/webpack/',
5971
libraryTarget: 'umd'
6072
},
@@ -71,21 +83,5 @@ module.exports = [
7183
]
7284
},
7385
common
74-
),
75-
Object.assign(
76-
{
77-
entry: './src/api.ts',
78-
output: {
79-
filename: 'index.js',
80-
path: __dirname + '/dist/api/',
81-
libraryTarget: 'umd'
82-
},
83-
target: 'node',
84-
node: {
85-
__dirname: false,
86-
__filename: false
87-
}
88-
},
89-
common
9086
)
9187
];

0 commit comments

Comments
 (0)