-
Notifications
You must be signed in to change notification settings - Fork 55
Add dataUri and dataUriToString functions
#1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gijsreyn
wants to merge
21
commits into
PowerShell:main
Choose a base branch
from
Gijsreyn:gh-57/main/add-datauri-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
45b8d90
Add dataUri and dataUriToString functions with documentation and tests
Gijsreyn c3ada13
Update examples in dataUri and dataUriToString documentation for clar…
Gijsreyn ed70b6c
Fix tests
Gijsreyn ed8dffc
Fix two remarks
Gijsreyn 9fc82df
Fix tests
Gijsreyn 787df30
Add support for charset validation in dataUriToString function
Gijsreyn bf06fc1
Add additional tests
Gijsreyn a2fe906
Add dataUri and dataUriToString functions with documentation and tests
Gijsreyn 2005b1b
Update examples in dataUri and dataUriToString documentation for clar…
Gijsreyn 07ed497
Fix tests
Gijsreyn d68ab3c
Fix two remarks
Gijsreyn e32dfe1
Fix tests
Gijsreyn 5c98298
Add support for charset validation in dataUriToString function
Gijsreyn aa04b78
Add additional tests
Gijsreyn 56a1eb7
Fix test
Gijsreyn 8a7e404
Merge branch 'gh-57/main/add-datauri-functions' of https://github.com…
Gijsreyn 2098523
Update test to handle edge case
Gijsreyn 716cfcf
Fix ARM strict validation
Gijsreyn b71503a
Add Data URI to String function implementation
Gijsreyn d67ba8c
Fix docs
Gijsreyn f550fc8
Fix remark
Gijsreyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| --- | ||
| description: Reference for the 'dataUri' DSC configuration document function | ||
| ms.date: 12/20/2025 | ||
| ms.topic: reference | ||
| title: dataUri | ||
| --- | ||
|
|
||
| # dataUri | ||
|
|
||
| ## Synopsis | ||
|
|
||
| Converts a value to a data URI. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```Syntax | ||
| dataUri(<stringToConvert>) | ||
| ``` | ||
|
|
||
| ## Description | ||
|
|
||
| The `dataUri()` function converts a string value to a [data URI][01] format. The function encodes | ||
| the input string as base64 and returns it as a data URI with the `application/json` media type and | ||
| `utf8` charset. | ||
|
|
||
| Data URIs are useful for embedding small text content directly in configuration documents, | ||
| especially when the content needs to be passed through systems that expect URI-formatted data. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 - Encode a script for transport | ||
|
|
||
| Encoding a PowerShell script as a data URI ensures safe transport through systems that may have | ||
| issues with special characters or line breaks. | ||
|
|
||
| ```yaml | ||
| # dataUri.example.1.dsc.config.yaml | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| parameters: | ||
| scriptContent: | ||
| type: string | ||
| defaultValue: "Write-Host 'Hello, World!'" | ||
| resources: | ||
| - name: Encode script as data URI | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: | ||
| originalScript: "[parameters('scriptContent')]" | ||
| encodedScript: "[dataUri(parameters('scriptContent'))]" | ||
| ``` | ||
|
|
||
| ```bash | ||
| dsc config get --file dataUri.example.1.dsc.config.yaml | ||
| ``` | ||
|
|
||
| ```yaml | ||
| results: | ||
| - name: Encode script as data URI | ||
| type: Microsoft.DSC.Debug/Echo | ||
| result: | ||
| actualState: | ||
| output: | ||
| originalScript: Write-Host 'Hello, World!' | ||
| encodedScript: data:application/json;base64,V3JpdGUtSG9zdCAnSGVsbG8sIFdvcmxkISc= | ||
| messages: [] | ||
| hadErrors: false | ||
| ``` | ||
|
|
||
| ### Example 2 - Encode JSON configuration for embedding | ||
|
|
||
| The configuration encodes a JSON configuration string as a data URI, which is useful when passing | ||
| structured data through systems that expect URI-formatted content. | ||
|
|
||
| ```yaml | ||
| # dataUri.example.2.dsc.config.yaml | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| resources: | ||
| - name: Encode JSON config as data URI | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[dataUri('{\"setting\":\"value\",\"enabled\":true}')]" | ||
| ``` | ||
|
|
||
| ```bash | ||
| dsc config get --file dataUri.example.2.dsc.config.yaml | ||
| ``` | ||
|
|
||
| ```yaml | ||
| results: | ||
| - name: Encode JSON config as data URI | ||
| type: Microsoft.DSC.Debug/Echo | ||
| result: | ||
| actualState: | ||
| output: data:application/json;base64,eyJzZXR0aW5nIjoidmFsdWUiLCJlbmFibGVkIjp0cnVlfQ== | ||
| messages: [] | ||
| hadErrors: false | ||
| ``` | ||
|
|
||
| ### Example 3 - Compare base64 and dataUri encoding | ||
|
|
||
| Unlike the [`base64()`][02] function which returns only the encoded content, `dataUri()` adds | ||
| the data URI prefix with media type information. Use `dataUri()` when the target system expects | ||
| the full data URI format. | ||
|
|
||
| ```yaml | ||
| # dataUri.example.3.dsc.config.yaml | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| resources: | ||
| - name: Compare encoding methods | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: | ||
| base64Only: "[base64('Hello')]" | ||
| fullDataUri: "[dataUri('Hello')]" | ||
| ``` | ||
|
|
||
| ```bash | ||
| dsc config get --file dataUri.example.3.dsc.config.yaml | ||
| ``` | ||
|
|
||
| ```yaml | ||
| results: | ||
| - name: Compare encoding methods | ||
| type: Microsoft.DSC.Debug/Echo | ||
| result: | ||
| actualState: | ||
| output: | ||
| base64Only: SGVsbG8= | ||
| fullDataUri: data:application/json;base64,SGVsbG8= | ||
| messages: [] | ||
| hadErrors: false | ||
| ``` | ||
|
|
||
| ### Example 4 - Encode multiline content | ||
|
|
||
| Multiline content like configuration files or scripts can be encoded as a data URI to preserve | ||
| line breaks during transport. | ||
|
|
||
| ```yaml | ||
| # dataUri.example.4.dsc.config.yaml | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| resources: | ||
| - name: Encode multiline content | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[dataUri('line1\nline2\nline3')]" | ||
| ``` | ||
|
|
||
| ```bash | ||
| dsc config get --file dataUri.example.4.dsc.config.yaml | ||
| ``` | ||
|
|
||
| ```yaml | ||
| results: | ||
| - name: Encode multiline content | ||
| type: Microsoft.DSC.Debug/Echo | ||
| result: | ||
| actualState: | ||
| output: data:application/json;base64,bGluZTEKbGluZTIKbGluZTM= | ||
| messages: [] | ||
| hadErrors: false | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| ### stringToConvert | ||
|
|
||
| The `dataUri()` function expects a single string as input. The function converts the value into a | ||
| data URI representation. If the value isn't a string, DSC raises an error when validating the | ||
| configuration document. | ||
|
|
||
| ```yaml | ||
| Type: string | ||
| Required: true | ||
| MinimumCount: 1 | ||
| MaximumCount: 1 | ||
| ``` | ||
|
|
||
| ## Output | ||
|
|
||
| The `dataUri()` function returns a data URI string in the format | ||
| `data:application/json;charset=utf8;base64,<encoded-content>` where `<encoded-content>` is the base64 | ||
| representation of the **stringToConvert** value. | ||
|
|
||
| ```yaml | ||
| Type: string | ||
| ``` | ||
|
|
||
| <!-- Link reference definitions --> | ||
| [01]: https://en.wikipedia.org/wiki/Data_URI_scheme | ||
| [02]: base64.md | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.