Skip to content

Commit 8681bfa

Browse files
authored
Merge pull request #79 from craftship/fix-publish-domain
Fix: Tarball Domain
2 parents a4bb303 + 716f899 commit 8681bfa

File tree

8 files changed

+195
-12
lines changed

8 files changed

+195
-12
lines changed

.serverless_plugins/codebox-tools/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,56 @@ class CodeboxTools {
192192

193193
return Promise.all(putPromises);
194194
})
195+
.then(() => {
196+
const lambda = new this.provider.sdk.Lambda({
197+
signatureVersion: 'v4',
198+
region: process.env.CODEBOX_REGION,
199+
});
200+
201+
const serviceName = this.serverless.config.serverless.service.service;
202+
const stage = this.options.stage;
203+
204+
const deployedName = `${serviceName}-${stage}-put`;
205+
206+
const params = {
207+
FunctionName: deployedName,
208+
};
209+
210+
return lambda
211+
.getFunctionConfiguration(params)
212+
.promise()
213+
.then((config) => {
214+
const env = config.Environment;
215+
const currentEndpoint = env.Variables.apiEndpoint;
216+
217+
if (!currentEndpoint) {
218+
throw new Error('Please ensure you are on Codebox npm 0.20.0 or higher.');
219+
}
220+
221+
let endpoint = currentEndpoint.replace(currentEndpoint.split('/')[2], this.options.host);
222+
if (this.options.path) {
223+
endpoint = `${endpoint}${this.options.path}`
224+
}
225+
226+
env.Variables = Object.assign({}, env.Variables, {
227+
apiEndpoint: endpoint,
228+
});
229+
230+
const updatedConfig = {
231+
FunctionName: deployedName,
232+
Environment: env,
233+
};
234+
235+
return lambda
236+
.updateFunctionConfiguration(updatedConfig)
237+
.promise();
238+
});
239+
})
195240
.then(() => {
196241
this.serverless.cli.log(`Domain updated for ${this.options.host}`);
197242
})
198243
.catch((err) => {
244+
console.log(err);
199245
this.serverless.cli.log(`Domain update failed for ${this.options.host}`);
200246
this.serverless.cli.log(err.message);
201247
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class SetAPIHost {
2+
constructor(serverless, options) {
3+
this.options = options;
4+
this.serverless = serverless;
5+
this.provider = this.serverless.getProvider('aws');
6+
7+
this.awsInfo = this.serverless
8+
.pluginManager
9+
.plugins
10+
.find(p => p.constructor.name === 'AwsInfo');
11+
12+
this.registry = this.serverless
13+
.service
14+
.provider
15+
.environment
16+
.registry;
17+
18+
this.hooks = {
19+
'after:deploy:deploy': this.afterDeploy.bind(this),
20+
};
21+
}
22+
23+
afterDeploy() {
24+
const lambda = new this.provider.sdk.Lambda({
25+
signatureVersion: 'v4',
26+
region: this.options.region,
27+
});
28+
29+
const publishFunction = this
30+
.awsInfo
31+
.gatheredData
32+
.info
33+
.functions
34+
.find(f => f.name === 'put');
35+
36+
const params = {
37+
FunctionName: publishFunction.deployedName,
38+
};
39+
40+
lambda
41+
.getFunctionConfiguration(params)
42+
.promise()
43+
.then((config) => {
44+
const env = config.Environment;
45+
46+
if (env.Variables.apiEndpoint) {
47+
// Already set / not a first time deployment.
48+
return;
49+
}
50+
51+
env.Variables = Object.assign({}, env.Variables, {
52+
apiEndpoint: `${this.awsInfo.gatheredData.info.endpoint}/registry`,
53+
});
54+
55+
const updatedConfig = {
56+
FunctionName: publishFunction.deployedName,
57+
Environment: env,
58+
};
59+
60+
return lambda
61+
.updateFunctionConfiguration(updatedConfig)
62+
.promise();
63+
})
64+
.catch((err) => {
65+
this.serverless.cli.log(err.message);
66+
});
67+
}
68+
}
69+
70+
module.exports = SetAPIHost;

serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins:
66
- serverless-webpack
77
- content-handling
88
- codebox-tools
9+
- set-api-host
910

1011
service: codebox-npm
1112

src/contextFactory.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export default (namespace, { headers, requestContext }) => {
5656
bucket,
5757
region,
5858
logTopic,
59+
apiEndpoint,
5960
} = process.env;
6061

6162
const cmd = command(headers);
6263

6364
return {
6465
command: cmd,
6566
registry,
67+
apiEndpoint,
6668
user: user(requestContext.authorizer),
6769
storage: storage(region, bucket),
6870
log: log(cmd, namespace, region, logTopic),

src/put/publish.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default async ({
44
body,
55
}, {
66
registry,
7+
apiEndpoint,
78
user,
89
storage,
910
npm,
@@ -46,9 +47,7 @@ export default async ({
4647
const versionData = pkg.versions[version];
4748

4849
const tarballFilename = encodeURIComponent(versionData.dist.tarball.split('/-/')[1]);
49-
const tarballBaseUrl = versionData.dist.tarball.split('/registry/')[0];
50-
const baseUrlParts = tarballBaseUrl.split(':');
51-
versionData.dist.tarball = `https:${baseUrlParts[1]}/registry/${pathParameters.name}/-/${tarballFilename}`;
50+
versionData.dist.tarball = `${apiEndpoint}/${pathParameters.name}/-/${tarballFilename}`;
5251

5352
let json = {};
5453

test/fixtures/package.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default {
1616
version: `${major}.${minor}.${patch}`,
1717
deprecated: msg,
1818
dist: {
19-
tarball: `https://example.com/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
19+
tarball: `https://example.com/prod/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
2020
},
2121
},
2222
},
@@ -43,7 +43,7 @@ export default {
4343
name: 'foo-bar-package',
4444
version: `${major}.${minor}.${patch}`,
4545
dist: {
46-
tarball: `https://example.com/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
46+
tarball: `https://example.com/prod/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
4747
},
4848
},
4949
},
@@ -70,7 +70,7 @@ export default {
7070
name: 'foo-bar-package',
7171
version: `${major}.${minor}.${patch}`,
7272
dist: {
73-
tarball: `https://example.com/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
73+
tarball: `https://example.com/prod/registry/foo-bar-package/-/foo-bar-package-${major}.${minor}.${patch}.tgz`,
7474
},
7575
},
7676
},

test/put/publish.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('PUT /registry/{name}', () => {
5252
patch: 0,
5353
}), {
5454
registry: 'https://example.com',
55+
apiEndpoint: 'https://example.com/prod/registry',
5556
user: stub(),
5657
log: {
5758
info: stub(),
@@ -76,6 +77,7 @@ describe('PUT /registry/{name}', () => {
7677
patch: 0,
7778
}), {
7879
registry: 'https://example.com',
80+
apiEndpoint: 'https://example.com/prod/registry',
7981
user: stub(),
8082
log: {
8183
error: stub(),
@@ -101,6 +103,7 @@ describe('PUT /registry/{name}', () => {
101103
patch: 0,
102104
}), {
103105
registry: 'https://example.com',
106+
apiEndpoint: 'https://example.com/prod/registry',
104107
user: stub(),
105108
log: {
106109
info: stub(),
@@ -130,6 +133,7 @@ describe('PUT /registry/{name}', () => {
130133
patch: 0,
131134
}), {
132135
registry: 'https://example.com',
136+
apiEndpoint: 'https://example.com/prod/registry',
133137
user: stub(),
134138
log: {
135139
info: stub(),
@@ -168,6 +172,7 @@ describe('PUT /registry/{name}', () => {
168172
patch: 0,
169173
}), {
170174
registry: 'https://example.com',
175+
apiEndpoint: 'https://example.com/prod/registry',
171176
user: stub(),
172177
log: {
173178
info: stub(),
@@ -192,6 +197,7 @@ describe('PUT /registry/{name}', () => {
192197
patch: 0,
193198
}), {
194199
registry: 'https://example.com',
200+
apiEndpoint: 'https://example.com/prod/registry',
195201
user: stub(),
196202
log: {
197203
error: stub(),
@@ -218,6 +224,7 @@ describe('PUT /registry/{name}', () => {
218224
patch: 0,
219225
}), {
220226
registry: 'https://example.com',
227+
apiEndpoint: 'https://example.com/prod/registry',
221228
user: stub(),
222229
log: {
223230
error: stub(),
@@ -255,6 +262,7 @@ describe('PUT /registry/{name}', () => {
255262
patch: 0,
256263
}), {
257264
registry: 'https://example.com',
265+
apiEndpoint: 'https://example.com/prod/registry',
258266
user: stub(),
259267
log: {
260268
error: stub(),
@@ -298,6 +306,7 @@ describe('PUT /registry/{name}', () => {
298306
patch: 0,
299307
}), {
300308
registry: 'https://example.com',
309+
apiEndpoint: 'https://example.com/prod/registry',
301310
user: stub(),
302311
log: {
303312
error: stub(),
@@ -335,6 +344,7 @@ describe('PUT /registry/{name}', () => {
335344
patch: 0,
336345
}), {
337346
registry: 'https://example.com',
347+
apiEndpoint: 'https://example.com/prod/registry',
338348
user: stub(),
339349
log: {
340350
error: stub(),

0 commit comments

Comments
 (0)