feat: truncate content exceeding the byte limit (#129)

This commit is contained in:
Peter Evans 2023-03-06 12:50:56 +09:00 committed by GitHub
parent 728b36f7bb
commit 202973a37c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 5116 deletions

View file

@ -30,8 +30,10 @@ This is useful if you `docker push` your images to Docker Hub. It provides an ea
#### Content limits #### 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. 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 #### Specifying the file path

View file

@ -1,4 +1,4 @@
import {completeRelativeUrls} from '../src/readme-helper' import {completeRelativeUrls, truncateToBytes} from '../src/readme-helper'
describe('complete relative urls tests', () => { describe('complete relative urls tests', () => {
const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL'] const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL']
@ -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
dist/index.js vendored
View file

@ -311,9 +311,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); 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 core = __importStar(__nccwpck_require__(2186));
const fs = __importStar(__nccwpck_require__(7147)); const fs = __importStar(__nccwpck_require__(7147));
const unicodeSubstring = __nccwpck_require__(6986);
exports.README_FILEPATH_DEFAULT = './README.md'; exports.README_FILEPATH_DEFAULT = './README.md';
exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'; exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp';
exports.ENABLE_URL_COMPLETION_DEFAULT = false; exports.ENABLE_URL_COMPLETION_DEFAULT = false;
@ -321,6 +322,15 @@ const TITLE_REGEX = `(?: +"[^"]+")?`;
const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHUB_REPOSITORY']}`; const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHUB_REPOSITORY']}`;
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`; const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`;
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${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) { function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// Fetch the readme content // Fetch the readme content
@ -328,7 +338,11 @@ function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions)
encoding: 'utf8' encoding: 'utf8'
}); });
readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions); 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; exports.getReadmeContent = getReadmeContent;
@ -6430,6 +6444,64 @@ if (process.env.NODE_DEBUG && /\btunnel\b/.test(process.env.NODE_DEBUG)) {
exports.debug = debug; // for test 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: /***/ 5840:

5118
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -28,7 +28,8 @@
"homepage": "https://github.com/peter-evans/dockerhub-description#readme", "homepage": "https://github.com/peter-evans/dockerhub-description#readme",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.10.0",
"node-fetch": "^2.6.9" "node-fetch": "^2.6.9",
"unicode-substring": "^0.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^27.0.3", "@types/jest": "^27.0.3",

View file

@ -1,5 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import unicodeSubstring = require('unicode-substring')
export const README_FILEPATH_DEFAULT = './README.md' export const README_FILEPATH_DEFAULT = './README.md'
export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp' export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'
@ -10,6 +11,8 @@ const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHU
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/` const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/` const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`
const MAX_BYTES = 25000
type Rule = { type Rule = {
/** /**
* all left of the relative url belonging to the markdown image/link * all left of the relative url belonging to the markdown image/link
@ -25,6 +28,14 @@ type Rule = {
absUrlPrefix: string absUrlPrefix: string
} }
export function truncateToBytes(s: string, n: number): string {
let len = n
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--)
}
return s
}
export async function getReadmeContent( export async function getReadmeContent(
readmeFilepath: string, readmeFilepath: string,
enableUrlCompletion: boolean, enableUrlCompletion: boolean,
@ -42,7 +53,14 @@ export async function getReadmeContent(
imageExtensions 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
} }
export function completeRelativeUrls( export function completeRelativeUrls(