public static SdkBytes compressRawScript()

in aws-synthetics-canary/src/main/java/com/amazon/synthetics/canary/ModelHelper.java [103:155]


    public static SdkBytes compressRawScript(ResourceModel model) {
        // Handler name is in the format <function_name>.handler.
        // Need to strip out the .handler suffix

        String functionName = model.getCode().getHandler().split("\\.")[0];
        String runtimeLanguage = getRuntimeLanguage(model.getRuntimeVersion());
        String functionNameWithType = "";
        String zipOutputFilePath = "";

        /**
         Runtime is Node
         **/
        if ( runtimeLanguage.equalsIgnoreCase("nodejs")) {
            functionNameWithType = functionName + JS_SUFFIX;
            zipOutputFilePath = NODE_MODULES_DIR + functionNameWithType;
            System.out.println("zipOutputFilePath: " + zipOutputFilePath);
        }

        /**
         Runtime is Python
         **/
        if ( runtimeLanguage.equalsIgnoreCase("python")) {
            functionNameWithType = functionName + PY_SUFFIX;
            zipOutputFilePath = PYTHON_DIR + functionNameWithType;
        }

        String script = model.getCode().getScript();

        ByteArrayOutputStream byteArrayOutputStream = null;
        InputStream inputStream = null;
        ZipOutputStream zipByteOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            zipByteOutputStream = new ZipOutputStream(byteArrayOutputStream);
            inputStream = new ByteArrayInputStream(script.getBytes(StandardCharsets.UTF_8));

            ZipEntry zipEntry = new ZipEntry(zipOutputFilePath);
            zipByteOutputStream.putNextEntry(zipEntry);

            byte[] buffer = new byte[1024];
            int len;

            while ((len = inputStream.read(buffer)) > 0) {
                zipByteOutputStream.write(buffer, 0, len);
            }
            zipByteOutputStream.closeEntry();
            zipByteOutputStream.close();
            inputStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return SdkBytes.fromByteBuffer(ByteBuffer.wrap(byteArrayOutputStream.toByteArray()));
    }