Skip to content

Commit

Permalink
feat: truncate content exceeding the byte limit (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans authored Mar 6, 2023
1 parent 728b36f commit 202973a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 5,116 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ This is useful if you `docker push` your images to Docker Hub. It provides an ea

#### Content limits

DockerHub has content limits, which if exceeded will result in the content being automatically truncated.
DockerHub has content limits.
The readme content is limited to 25,000 bytes, and `short-description` is limited to 100 characters.
This action truncates content to prevent the request being rejected.
If the content has been truncated a warning will be issued in the run log.

#### Specifying the file path

Expand Down
11 changes: 10 additions & 1 deletion __test__/readme-helper.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {completeRelativeUrls} from '../src/readme-helper'
import {completeRelativeUrls, truncateToBytes} from '../src/readme-helper'

describe('complete relative urls tests', () => {
const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL']
Expand Down Expand Up @@ -333,3 +333,12 @@ describe('complete relative urls tests', () => {
)
})
})

describe('truncate to bytes tests', () => {
test('unicode aware truncation to a number of bytes', async () => {
expect(truncateToBytes('test string to be truncated', 10)).toEqual(
'test strin'
)
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁')
})
})
76 changes: 74 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.completeRelativeUrls = exports.getReadmeContent = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0;
exports.completeRelativeUrls = exports.getReadmeContent = exports.truncateToBytes = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0;
const core = __importStar(__nccwpck_require__(2186));
const fs = __importStar(__nccwpck_require__(7147));
const unicodeSubstring = __nccwpck_require__(6986);
exports.README_FILEPATH_DEFAULT = './README.md';
exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp';
exports.ENABLE_URL_COMPLETION_DEFAULT = false;
const TITLE_REGEX = `(?: +"[^"]+")?`;
const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHUB_REPOSITORY']}`;
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`;
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`;
const MAX_BYTES = 25000;
function truncateToBytes(s, n) {
let len = n;
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--);
}
return s;
}
exports.truncateToBytes = truncateToBytes;
function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions) {
return __awaiter(this, void 0, void 0, function* () {
// Fetch the readme content
let readmeContent = yield fs.promises.readFile(readmeFilepath, {
encoding: 'utf8'
});
readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions);
return readmeContent;
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES);
if (truncatedReadmeContent.length !== readmeContent.length) {
core.warning(`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`);
}
return truncatedReadmeContent;
});
}
exports.getReadmeContent = getReadmeContent;
Expand Down Expand Up @@ -6430,6 +6444,64 @@ if (process.env.NODE_DEBUG && /\btunnel\b/.test(process.env.NODE_DEBUG)) {
exports.debug = debug; // for test


/***/ }),

/***/ 6986:
/***/ ((module) => {

"use strict";


function charAt(string, index) {
var first = string.charCodeAt(index);
var second;
if (first >= 55296 && first <= 56319 && string.length > index + 1) {
second = string.charCodeAt(index + 1);
if (second >= 56320 && second <= 57343) {
return string.substring(index, index + 2);
}
}
return string[index];
}

function slice(string, start, end) {
var accumulator = "";
var character;
var stringIndex = 0;
var unicodeIndex = 0;
var length = string.length;

while (stringIndex < length) {
character = charAt(string, stringIndex);
if (unicodeIndex >= start && unicodeIndex < end) {
accumulator += character;
}
stringIndex += character.length;
unicodeIndex += 1;
}
return accumulator;
}

function toNumber(value, fallback) {
if (value === undefined) {
return fallback;
} else {
return Number(value);
}
}

module.exports = function (string, start, end) {
var realStart = toNumber(start, 0);
var realEnd = toNumber(end, string.length);
if (realEnd == realStart) {
return "";
} else if (realEnd > realStart) {
return slice(string, realStart, realEnd);
} else {
return slice(string, realEnd, realStart);
}
};

/***/ }),

/***/ 5840:
Expand Down
Loading

0 comments on commit 202973a

Please sign in to comment.