@balena/pinejs-webresource-cloudfront 0.0.3 → 0.0.4-build-export-type-e5961390269a9dda185153f4fe111d7e8862f910-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,14 @@
1
+ - commits:
2
+ - subject: Export CloudFrontHandler props typings
3
+ hash: e5961390269a9dda185153f4fe111d7e8862f910
4
+ body: ""
5
+ footer:
6
+ Change-type: patch
7
+ change-type: patch
8
+ author: Otávio Jacobi
9
+ version: 0.0.4
10
+ title: ""
11
+ date: 2023-08-29T13:32:49.480Z
1
12
  - commits:
2
13
  - subject: Export CloudFrontHandler props typings
3
14
  hash: 3a438126156faff30525ad4c8b6973af189af248
@@ -8,7 +19,7 @@
8
19
  author: Otávio Jacobi
9
20
  version: 0.0.3
10
21
  title: ""
11
- date: 2023-08-28T13:59:10.580Z
22
+ date: 2023-08-28T14:03:51.553Z
12
23
  - commits:
13
24
  - subject: Fix doc detail
14
25
  hash: 6272ef4855f5f9d5695362be148393ec90dd3ff0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file
4
4
  automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
5
5
  This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ # v0.0.4
8
+ ## (2023-08-29)
9
+
10
+ * Export CloudFrontHandler props typings [Otávio Jacobi]
11
+
7
12
  # v0.0.3
8
13
  ## (2023-08-28)
9
14
 
package/build/index.d.ts CHANGED
@@ -9,8 +9,6 @@ export declare class CloudFrontHandler extends webResourceHandler.S3Handler {
9
9
  private readonly cfSecretKey;
10
10
  private readonly cfPublicKeyId;
11
11
  private readonly cfDistributionDomain;
12
- private readonly cfSignedUrlExpireTimeSeconds;
13
- private readonly cfSignedUrlCacheExpireTimeSeconds;
14
12
  constructor(cfConfig: CloudFrontHandlerProps);
15
13
  onPreRespond(webResource: WebResource): Promise<WebResource>;
16
14
  private cachedCfGetSignedUrl;
package/build/index.js CHANGED
@@ -7,10 +7,8 @@ const cloudfront_signer_1 = require("@aws-sdk/cloudfront-signer");
7
7
  class CloudFrontHandler extends pinejs_1.webResourceHandler.S3Handler {
8
8
  constructor(cfConfig) {
9
9
  super(cfConfig);
10
- this.cfSignedUrlExpireTimeSeconds = 86400;
11
- this.cfSignedUrlCacheExpireTimeSeconds = 82800;
12
10
  this.cachedCfGetSignedUrl = memoize(this.cfSignUrl, {
13
- maxAge: this.cfSignedUrlCacheExpireTimeSeconds * 1000,
11
+ maxAge: this.signedUrlCacheExpireTimeSeconds * 1000,
14
12
  });
15
13
  this.cfSecretKey = cfConfig.cfSecretKey;
16
14
  this.cfPublicKeyId = cfConfig.cfPublicKeyId;
@@ -18,20 +16,28 @@ class CloudFrontHandler extends pinejs_1.webResourceHandler.S3Handler {
18
16
  }
19
17
  async onPreRespond(webResource) {
20
18
  const fileKey = this.cfGetKeyFromHref(webResource.href);
21
- webResource.href = this.cachedCfGetSignedUrl(fileKey);
19
+ webResource.href = await this.cachedCfGetSignedUrl(fileKey);
22
20
  return webResource;
23
21
  }
24
- cfSignUrl(fileKey) {
25
- return (0, cloudfront_signer_1.getSignedUrl)({
26
- url: `${this.cfDistributionDomain}/${fileKey}`,
27
- keyPairId: this.cfPublicKeyId,
28
- privateKey: this.cfSecretKey,
29
- dateLessThan: this.getExpiryDate(),
30
- });
22
+ async cfSignUrl(cfFileKey) {
23
+ try {
24
+ return (0, cloudfront_signer_1.getSignedUrl)({
25
+ url: `${this.cfDistributionDomain}/${cfFileKey}`,
26
+ keyPairId: this.cfPublicKeyId,
27
+ privateKey: this.cfSecretKey,
28
+ dateLessThan: this.getExpiryDate(),
29
+ });
30
+ }
31
+ catch (e) {
32
+ console.error('CloudFront signing failed with', e);
33
+ console.error('Trying to serve S3 signed url');
34
+ const s3Key = cfFileKey.split('/').at(-1) ?? cfFileKey;
35
+ return await this.cachedGetSignedUrl(s3Key);
36
+ }
31
37
  }
32
38
  getExpiryDate() {
33
39
  const now = new Date().getTime();
34
- const expiry = now + this.cfSignedUrlExpireTimeSeconds * 1000;
40
+ const expiry = now + this.signedUrlExpireTimeSeconds * 1000;
35
41
  return new Date(expiry).toISOString();
36
42
  }
37
43
  cfGetKeyFromHref(href) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;AAAA,2CAAoD;AACpD,oCAAoC;AACpC,kEAA0D;AAU1D,MAAa,iBAAkB,SAAQ,2BAAkB,CAAC,SAAS;IASlE,YAAY,QAAgC;QAC3C,KAAK,CAAC,QAAQ,CAAC,CAAC;QAJA,iCAA4B,GAAG,KAAK,CAAC;QACrC,sCAAiC,GAAG,KAAK,CAAC;QAiBnD,yBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE;YACtD,MAAM,EAAE,IAAI,CAAC,iCAAiC,GAAG,IAAI;SACrD,CAAC,CAAC;QAfF,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC;QACxC,IAAI,CAAC,aAAa,GAAG,QAAQ,CAAC,aAAa,CAAC;QAC5C,IAAI,CAAC,oBAAoB,GAAG,QAAQ,CAAC,oBAAoB,CAAC;IAC3D,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,WAAwB;QACjD,MAAM,OAAO,GAAG,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACxD,WAAW,CAAC,IAAI,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;QACtD,OAAO,WAAW,CAAC;IACpB,CAAC;IAQO,SAAS,CAAC,OAAe;QAChC,OAAO,IAAA,gCAAY,EAAC;YACnB,GAAG,EAAE,GAAG,IAAI,CAAC,oBAAoB,IAAI,OAAO,EAAE;YAC9C,SAAS,EAAE,IAAI,CAAC,aAAa;YAC7B,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,YAAY,EAAE,IAAI,CAAC,aAAa,EAA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GAAG,CAAC,CAAC;IACb,CAAC;CACD;AAtDD,8CAsDC"}
package/lib/index.ts CHANGED
@@ -15,10 +15,6 @@ export class CloudFrontHandler extends webResourceHandler.S3Handler {
15
15
  private readonly cfPublicKeyId: string;
16
16
  private readonly cfDistributionDomain: string;
17
17
 
18
- // TODO move S3 handler signedUrl as not private
19
- private readonly cfSignedUrlExpireTimeSeconds = 86400; // 24h
20
- private readonly cfSignedUrlCacheExpireTimeSeconds = 82800; // 22h
21
-
22
18
  constructor(cfConfig: CloudFrontHandlerProps) {
23
19
  super(cfConfig);
24
20
  this.cfSecretKey = cfConfig.cfSecretKey;
@@ -28,28 +24,35 @@ export class CloudFrontHandler extends webResourceHandler.S3Handler {
28
24
 
29
25
  public async onPreRespond(webResource: WebResource): Promise<WebResource> {
30
26
  const fileKey = this.cfGetKeyFromHref(webResource.href);
31
- webResource.href = this.cachedCfGetSignedUrl(fileKey);
27
+ webResource.href = await this.cachedCfGetSignedUrl(fileKey);
32
28
  return webResource;
33
29
  }
34
30
 
35
31
  // Memoize expects maxAge in MS and CF signing method in seconds.
36
32
  // Normalization to use only seconds and therefore convert here from seconds to MS.
37
33
  private cachedCfGetSignedUrl = memoize(this.cfSignUrl, {
38
- maxAge: this.cfSignedUrlCacheExpireTimeSeconds * 1000,
34
+ maxAge: this.signedUrlCacheExpireTimeSeconds * 1000,
39
35
  });
40
36
 
41
- private cfSignUrl(fileKey: string): string {
42
- return getSignedUrl({
43
- url: `${this.cfDistributionDomain}/${fileKey}`,
44
- keyPairId: this.cfPublicKeyId,
45
- privateKey: this.cfSecretKey,
46
- dateLessThan: this.getExpiryDate(),
47
- });
37
+ private async cfSignUrl(cfFileKey: string): Promise<string> {
38
+ try {
39
+ return getSignedUrl({
40
+ url: `${this.cfDistributionDomain}/${cfFileKey}`,
41
+ keyPairId: this.cfPublicKeyId,
42
+ privateKey: this.cfSecretKey,
43
+ dateLessThan: this.getExpiryDate(),
44
+ });
45
+ } catch (e) {
46
+ console.error('CloudFront signing failed with', e);
47
+ console.error('Trying to serve S3 signed url');
48
+ const s3Key = cfFileKey.split('/').at(-1) ?? cfFileKey;
49
+ return await this.cachedGetSignedUrl(s3Key);
50
+ }
48
51
  }
49
52
 
50
53
  private getExpiryDate(): string {
51
54
  const now = new Date().getTime();
52
- const expiry = now + this.cfSignedUrlExpireTimeSeconds * 1000;
55
+ const expiry = now + this.signedUrlExpireTimeSeconds * 1000;
53
56
  return new Date(expiry).toISOString();
54
57
  }
55
58
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@balena/pinejs-webresource-cloudfront",
3
- "version": "0.0.3",
3
+ "version": "0.0.4-build-export-type-e5961390269a9dda185153f4fe111d7e8862f910-1",
4
4
  "description": "A PineJS WebResource handler for storing files on S3 and serving via AWS Cloudfront",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@aws-sdk/cloudfront-signer": "^3.398.0",
24
- "@balena/pinejs": "^15.3.2",
24
+ "@balena/pinejs": "^15.3.3",
25
25
  "memoizee": "^0.4.15"
26
26
  },
27
27
  "versionist": {
28
- "publishedAt": "2023-08-28T13:59:10.641Z"
28
+ "publishedAt": "2023-08-29T13:32:49.551Z"
29
29
  }
30
30
  }