Skip to content

Commit

Permalink
Merge of #3528
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jul 20, 2023
2 parents 06a4d0d + eb73f6b commit 021fb7e
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/docs/04-standard-library/04-util/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Utility functions.

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.util.Util.base64Decode">base64Decode</a></code> | Converts a string from base64 to UTF-8. |
| <code><a href="#@winglang/sdk.util.Util.base64Encode">base64Encode</a></code> | Converts a string from UTF-8 to base64. |
| <code><a href="#@winglang/sdk.util.Util.env">env</a></code> | Returns the value of an environment variable. |
| <code><a href="#@winglang/sdk.util.Util.nanoid">nanoid</a></code> | Generates a unique ID using the nanoid library. |
| <code><a href="#@winglang/sdk.util.Util.sha256">sha256</a></code> | Computes the SHA256 hash of the given data. |
Expand All @@ -33,6 +35,56 @@ Utility functions.

---

##### `base64Decode` <a name="base64Decode" id="@winglang/sdk.util.Util.base64Decode"></a>

```wing
bring util;
util.base64Decode(stringToDecode: str, url?: bool);
```

Converts a string from base64 to UTF-8.

###### `stringToDecode`<sup>Required</sup> <a name="stringToDecode" id="@winglang/sdk.util.Util.base64Decode.parameter.stringToDecode"></a>

- *Type:* str

base64 string to decode.

---

###### `url`<sup>Optional</sup> <a name="url" id="@winglang/sdk.util.Util.base64Decode.parameter.url"></a>

- *Type:* bool

If `true`, the source is expected to be a URL-safe base64 string.

---

##### `base64Encode` <a name="base64Encode" id="@winglang/sdk.util.Util.base64Encode"></a>

```wing
bring util;
util.base64Encode(stringToEncode: str, url?: bool);
```

Converts a string from UTF-8 to base64.

###### `stringToEncode`<sup>Required</sup> <a name="stringToEncode" id="@winglang/sdk.util.Util.base64Encode.parameter.stringToEncode"></a>

- *Type:* str

---

###### `url`<sup>Optional</sup> <a name="url" id="@winglang/sdk.util.Util.base64Encode.parameter.url"></a>

- *Type:* bool

If `true`, a URL-safe base64 is returned.

---

##### `env` <a name="env" id="@winglang/sdk.util.Util.env"></a>

```wing
Expand Down
25 changes: 25 additions & 0 deletions examples/tests/sdk_tests/util/base64.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
bring util;

let string = "https://www.winglang.io/docs";
let base64Encode = util.base64Encode(string);
let base64urlEncode = util.base64Encode(string, true);
let base64Decode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
let base64urlDecode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw", true);

assert(base64Encode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
assert(base64urlEncode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw");
assert(base64Decode == string);
assert(base64urlDecode == string);

test "inflight base64" {
let string = "https://www.winglang.io/docs";
let base64Encode = util.base64Encode(string);
let base64urlEncode = util.base64Encode(string, true);
let base64Decode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
let base64urlDecode = util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw", true);

assert(base64Encode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==");
assert(base64urlEncode == "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw");
assert(base64Decode == string);
assert(base64urlDecode == string);
}
24 changes: 24 additions & 0 deletions libs/wingc/src/lsp/snapshots/completions/util_static_methods.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
---
source: libs/wingc/src/lsp/completions.rs
---
- label: base64Decode
kind: 2
detail: "(stringToDecode: str, url: bool?): str"
documentation:
kind: markdown
value: "```wing\nstatic base64Decode: (stringToDecode: str, url: bool?): str\n```\n---\nConverts a string from base64 to UTF-8.\n\n\n### Returns\nThe UTF-8 string."
sortText: ff|base64Decode
insertText: base64Decode($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: base64Encode
kind: 2
detail: "(stringToEncode: str, url: bool?): str"
documentation:
kind: markdown
value: "```wing\nstatic base64Encode: (stringToEncode: str, url: bool?): str\n```\n---\nConverts a string from UTF-8 to base64.\n\n\n### Returns\nThe base64 string."
sortText: ff|base64Encode
insertText: base64Encode($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: env
kind: 2
detail: "(name: str): str"
Expand Down
22 changes: 22 additions & 0 deletions libs/wingsdk/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ export class Util {
return process.env[name];
}

/**
* Converts a string from UTF-8 to base64.
* @param name The name of the UTF-8 string to encode.
* @param url If `true`, a URL-safe base64 is returned.
* @returns The base64 string.
*/
public static base64Encode(stringToEncode: string, url?: boolean): string {
return Buffer.from(stringToEncode).toString(url ? "base64url" : "base64");
}

/**
* Converts a string from base64 to UTF-8.
* @param stringToDecode base64 string to decode.
* @param url If `true`, the source is expected to be a URL-safe base64 string
* @returns The UTF-8 string.
*/
public static base64Decode(stringToDecode: string, url?: boolean): string {
return Buffer.from(stringToDecode, url ? "base64url" : "base64").toString(
"utf8"
);
}

/**
* Suspends execution for a given duration.
* @param delay The time to suspend execution.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# [base64.w](../../../../../../examples/tests/sdk_tests/util/base64.w) | compile | tf-aws

## inflight.$Closure1.js
```js
module.exports = function({ $util_Util }) {
class $Closure1 {
constructor({ }) {
const $obj = (...args) => this.handle(...args);
Object.setPrototypeOf($obj, this);
return $obj;
}
async handle() {
const string = "https://www.winglang.io/docs";
const base64Encode = (await $util_Util.base64Encode(string));
const base64urlEncode = (await $util_Util.base64Encode(string,true));
const base64Decode = (await $util_Util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw=="));
const base64urlDecode = (await $util_Util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw",true));
{((cond) => {if (!cond) throw new Error("assertion failed: base64Encode == \"aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==\"")})((base64Encode === "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw=="))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64urlEncode == \"aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw\"")})((base64urlEncode === "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw"))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64Decode == string")})((base64Decode === string))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64urlDecode == string")})((base64urlDecode === string))};
}
}
return $Closure1;
}

```

## main.tf.json
```json
{
"//": {
"metadata": {
"backend": "local",
"stackName": "root",
"version": "0.17.0"
},
"outputs": {
"root": {
"Default": {
"cloud.TestRunner": {
"TestFunctionArns": "WING_TEST_RUNNER_FUNCTION_ARNS"
}
}
}
}
},
"output": {
"WING_TEST_RUNNER_FUNCTION_ARNS": {
"value": "[[\"root/Default/Default/test:inflight base64\",\"${aws_lambda_function.testinflightbase64_Handler_31E9772F.arn}\"]]"
}
},
"provider": {
"aws": [
{}
]
},
"resource": {
"aws_iam_role": {
"testinflightbase64_Handler_IamRole_49F68A60": {
"//": {
"metadata": {
"path": "root/Default/Default/test:inflight base64/Handler/IamRole",
"uniqueId": "testinflightbase64_Handler_IamRole_49F68A60"
}
},
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}"
}
},
"aws_iam_role_policy": {
"testinflightbase64_Handler_IamRolePolicy_031C1061": {
"//": {
"metadata": {
"path": "root/Default/Default/test:inflight base64/Handler/IamRolePolicy",
"uniqueId": "testinflightbase64_Handler_IamRolePolicy_031C1061"
}
},
"policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}",
"role": "${aws_iam_role.testinflightbase64_Handler_IamRole_49F68A60.name}"
}
},
"aws_iam_role_policy_attachment": {
"testinflightbase64_Handler_IamRolePolicyAttachment_FA451656": {
"//": {
"metadata": {
"path": "root/Default/Default/test:inflight base64/Handler/IamRolePolicyAttachment",
"uniqueId": "testinflightbase64_Handler_IamRolePolicyAttachment_FA451656"
}
},
"policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"role": "${aws_iam_role.testinflightbase64_Handler_IamRole_49F68A60.name}"
}
},
"aws_lambda_function": {
"testinflightbase64_Handler_31E9772F": {
"//": {
"metadata": {
"path": "root/Default/Default/test:inflight base64/Handler/Default",
"uniqueId": "testinflightbase64_Handler_31E9772F"
}
},
"environment": {
"variables": {
"WING_FUNCTION_NAME": "Handler-c853d8cf",
"WING_TARGET": "tf-aws"
}
},
"function_name": "Handler-c853d8cf",
"handler": "index.handler",
"publish": true,
"role": "${aws_iam_role.testinflightbase64_Handler_IamRole_49F68A60.arn}",
"runtime": "nodejs18.x",
"s3_bucket": "${aws_s3_bucket.Code.bucket}",
"s3_key": "${aws_s3_object.testinflightbase64_Handler_S3Object_C9A792F2.key}",
"timeout": 30,
"vpc_config": {
"security_group_ids": [],
"subnet_ids": []
}
}
},
"aws_s3_bucket": {
"Code": {
"//": {
"metadata": {
"path": "root/Default/Code",
"uniqueId": "Code"
}
},
"bucket_prefix": "code-c84a50b1-"
}
},
"aws_s3_object": {
"testinflightbase64_Handler_S3Object_C9A792F2": {
"//": {
"metadata": {
"path": "root/Default/Default/test:inflight base64/Handler/S3Object",
"uniqueId": "testinflightbase64_Handler_S3Object_C9A792F2"
}
},
"bucket": "${aws_s3_bucket.Code.bucket}",
"key": "<ASSET_KEY>",
"source": "<ASSET_SOURCE>"
}
}
}
}
```

## preflight.js
```js
const $stdlib = require('@winglang/sdk');
const $outdir = process.env.WING_SYNTH_DIR ?? ".";
const std = $stdlib.std;
const $wing_is_test = process.env.WING_IS_TEST === "true";
const util = require('@winglang/sdk').util;
class $Root extends $stdlib.std.Resource {
constructor(scope, id) {
super(scope, id);
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
require("./inflight.$Closure1.js")({
$util_Util: ${context._lift(util.Util)},
})
`);
}
_toInflight() {
return $stdlib.core.NodeJsCode.fromInline(`
(await (async () => {
const $Closure1Client = ${$Closure1._toInflightType(this).text};
const client = new $Closure1Client({
});
if (client.$inflight_init) { await client.$inflight_init(); }
return client;
})())
`);
}
}
const string = "https://www.winglang.io/docs";
const base64Encode = (util.Util.base64Encode(string));
const base64urlEncode = (util.Util.base64Encode(string,true));
const base64Decode = (util.Util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw=="));
const base64urlDecode = (util.Util.base64Decode("aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw",true));
{((cond) => {if (!cond) throw new Error("assertion failed: base64Encode == \"aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw==\"")})((base64Encode === "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw=="))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64urlEncode == \"aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw\"")})((base64urlEncode === "aHR0cHM6Ly93d3cud2luZ2xhbmcuaW8vZG9jcw"))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64Decode == string")})((base64Decode === string))};
{((cond) => {if (!cond) throw new Error("assertion failed: base64urlDecode == string")})((base64urlDecode === string))};
this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:inflight base64",new $Closure1(this,"$Closure1"));
}
}
const $App = $stdlib.core.App.for(process.env.WING_TARGET);
new $App({ outdir: $outdir, name: "base64", rootConstruct: $Root, plugins: $plugins, isTestEnvironment: $wing_is_test }).synth();

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# [base64.w](../../../../../../examples/tests/sdk_tests/util/base64.w) | test | sim

## stdout.log
```log
pass ─ base64.wsim » root/env0/test:inflight base64
Tests 1 passed (1)
Test Files 1 passed (1)
Duration <DURATION>
```

0 comments on commit 021fb7e

Please sign in to comment.