Skip to content

Commit

Permalink
added request timeout and request socket timeout options (default und…
Browse files Browse the repository at this point in the history
…efined)
  • Loading branch information
awaragi committed Apr 16, 2024
1 parent 4d3a41d commit b567eba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ eleventyConfig.addPlugin(plantuml.plugin, {
port: 8888,
prefix: "",
outputType: "svg",
imgClass: "plantuml",
imgClass: "plantuml",
requestTimeout: undefined, // undefined or millisecondes
requestSocketTimeout: undefined, // undefined or millisecondes
});
```

If the server options are omitted, the plugin defaults to <http://plantuml.com/plantuml> server for conversion, and to PNG for output type.

By default the generated img tag will have class **plantuml** assigned to it. This can be overridden using options.imgClass value.

Timeout options are passed directly to the underlying request object. See https://www.npmjs.com/package/sync-request. They default to sync-request

## Using in templates

Simply create a markdown code block of type plantuml. It will be replaced by an img with inline png src (dataurl), or with svg code, depending on the value of the option `outputType`.
Expand Down
7 changes: 6 additions & 1 deletion lib/eleventy-plugin-plantuml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const defaultOptions = {
prefix: "/plantuml",
outputType: "png",
imgClass: "plantuml",
requestTimeout: undefined,
requestSocketTimeout: undefined,
};

function generatePlantumlUrl(encoded, options) {
Expand All @@ -18,7 +20,10 @@ function generatePlantumlUrl(encoded, options) {

const fetchImage = (url, options) => {
// request url
const response = request("GET", url);
const response = request("GET", url, {
timeout: options.requestTimeout,
socketTimeout: options.requestSocketTimeout,
});
// convert from Uint8Array to buffer
return Buffer.from(response.getBody());
};
Expand Down
31 changes: 31 additions & 0 deletions test/eleventy-plugin-plantuml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,35 @@ Bob -> Alice : hello
'<img class="plantuml" src="data:image/png;base64,ABC" alt="Plantuml Diagram" />'
);
});

it("Test request timeout", () => {
try {
eleventyPluginPlantuml.highlight(
`@startuml @enduml`,
Object.assign({}, eleventyPluginPlantuml.defaultOptions, {
requestTimeout: 1,
})
);
// should not get here
expect(false).beTruthy();
} catch (e) {
// test passed
expect(e).toBeInstanceOf(Error); }
});

it("Test request socket timeout", () => {
try {
eleventyPluginPlantuml.highlight(
`@startuml @enduml`,
Object.assign({}, eleventyPluginPlantuml.defaultOptions, {
requestSocketTimeout: 1,
})
);
// should not get here
expect(false).beTruthy();
} catch (e) {
// test passed
expect(e).toBeInstanceOf(Error);
}
});
});

0 comments on commit b567eba

Please sign in to comment.