• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

activescott / serverless-aws-static-file-handler / 14207502292

01 Apr 2025 10:43PM CUT coverage: 90.769%. First build
14207502292

Pull #326

github

web-flow
Merge d87a45241 into bf205bf1c
Pull Request #326: chore: bump serverless from 3.40.0 to 4.10.1 in /examples/serverless-offline

55 of 61 branches covered (90.16%)

Branch coverage included in aggregate %.

122 of 134 relevant lines covered (91.04%)

20.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/plugins/BinaryMediaTypes.js
1
"use strict"
2
const util = require("util")
2✔
3
const _ = require("lodash")
2✔
4

5
class BinaryMediaTypes {
6
  constructor(serverless, options) {
7
    if (!serverless) {
22✔
8
      throw new Error("Expected serverless to be provided as argument")
2✔
9
    }
10
    this.serverless = serverless
20✔
11
    this.options = options
20✔
12
    this.provider = this.serverless.getProvider("aws")
20✔
13
    this.hooks = {
20✔
14
      "package:compileEvents": this.packageCompileEvents.bind(this),
15
    }
16
  }
17

18
  log(...args) {
19
    args.unshift("aws-static-file-handler (BinaryMediaTypes):")
34✔
20
    const msg = util.format(...args)
34✔
21
    this.serverless.cli.log(msg)
34✔
22
  }
23

24
  getRestApi() {
25
    const resources =
26
      this.serverless.service.provider.compiledCloudFormationTemplate.Resources
20✔
27
    return _.find(resources, (r) => r.Type === "AWS::ApiGateway::RestApi")
20✔
28
  }
29

30
  readConfig() {
31
    const service = this.serverless.service
16✔
32
    if (
16✔
33
      !service.custom ||
50✔
34
      !service.custom.apiGateway ||
35
      !service.custom.apiGateway.binaryMediaTypes ||
36
      _.isEmpty(service.custom.apiGateway.binaryMediaTypes)
37
    ) {
38
      throw new Error(BinaryMediaTypes.Strings.CONFIG_ERROR)
10✔
39
    }
40
    return service.custom.apiGateway.binaryMediaTypes
6✔
41
  }
42

43
  addBinaryMediaTypes(restApi) {
44
    if (!restApi) {
20✔
45
      this.log(
2✔
46
        "Amazon API Gateway RestApi resource not found. No BinaryMediaTypes will be added."
47
      )
48
      return
2✔
49
    }
50
    if (!restApi.Properties) {
18✔
51
      throw new Error("RestApi Properties property does not exist!")
2✔
52
    }
53
    // see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-binarymediatypes
54
    const newTypes = this.readConfig()
16✔
55
    this.log("Adding the following BinaryMediaTypes to RestApi:", newTypes)
6✔
56
    const oldTypes = restApi.Properties["BinaryMediaTypes"] || []
6✔
57
    const combined = _.concat(oldTypes, newTypes)
6✔
58
    restApi.Properties["BinaryMediaTypes"] = combined
6✔
59
    this.log("RestApi BinaryMediaTypes are now:", combined)
6✔
60
  }
61

62
  packageCompileEvents() {
63
    this.log("Preparing to add binary media types (package:compileEvents)...")
20✔
64
    const restApi = this.getRestApi()
20✔
65
    this.addBinaryMediaTypes(restApi)
20✔
66
  }
67
}
68

69
BinaryMediaTypes.Strings = {
2✔
70
  CONFIG_ERROR:
71
    "No BinaryMediaTypes configured. See https://github.com/activescott/serverless-aws-static-file-handler#usage for information on how to configure",
72
}
73

74
module.exports = BinaryMediaTypes
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc