Skip to content

Commit c9ffda6

Browse files
authored
Merge pull request #936 from crazy-max/oci-defaultPlatform
oci: defaultPlatform function
2 parents 18f82ba + af989cc commit c9ffda6

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

__tests__/oci/oci.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,43 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {afterEach, describe, expect, test} from '@jest/globals';
17+
import {afterEach, describe, expect, jest, test} from '@jest/globals';
1818
import fs from 'fs';
1919
import os from 'os';
2020
import path from 'path';
2121
import * as rimraf from 'rimraf';
22+
import osm = require('os');
2223

2324
import {OCI} from '../../src/oci/oci';
2425

26+
import {Platform} from '../../src/types/oci/descriptor';
27+
2528
const fixturesDir = path.join(__dirname, '..', '.fixtures');
2629
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'oci-oci-'));
2730

2831
afterEach(function () {
2932
rimraf.sync(tmpDir);
3033
});
3134

35+
describe('defaultPlatform', () => {
36+
test.each([
37+
['win32', 'x64', {architecture: 'amd64', os: 'windows'}],
38+
['win32', 'arm64', {architecture: 'arm64', os: 'windows'}],
39+
['darwin', 'x64', {architecture: 'amd64', os: 'darwin'}],
40+
['darwin', 'arm64', {architecture: 'arm64', os: 'darwin'}],
41+
['linux', 'ia32', {architecture: '386', os: 'linux'}],
42+
['linux', 'x64', {architecture: 'amd64', os: 'linux'}],
43+
['linux', 'arm64', {architecture: 'arm64', os: 'linux'}],
44+
['linux', 'ppc64', {architecture: 'ppc64le', os: 'linux'}],
45+
['linux', 's390x', {architecture: 's390x', os: 'linux'}]
46+
])('default platform for %s/%s', async (os: string, arch: string, expected: Platform) => {
47+
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
48+
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
49+
const res = OCI.defaultPlatform();
50+
expect(res).toEqual(expected);
51+
});
52+
});
53+
3254
describe('loadArchive', () => {
3355
// prettier-ignore
3456
test.each(fs.readdirSync(path.join(fixturesDir, 'oci-archive')).filter(file => {

src/oci/oci.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,67 @@
1414
* limitations under the License.
1515
*/
1616
import fs from 'fs';
17+
import os from 'os';
1718
import gunzip from 'gunzip-maybe';
1819
import * as path from 'path';
1920
import {Readable} from 'stream';
2021
import * as tar from 'tar-stream';
2122

2223
import {Archive, LoadArchiveOpts} from '../types/oci/oci';
2324
import {Index} from '../types/oci';
25+
import {Platform} from '../types/oci/descriptor';
2426
import {Manifest} from '../types/oci/manifest';
2527
import {Image} from '../types/oci/config';
2628
import {IMAGE_BLOBS_DIR_V1, IMAGE_INDEX_FILE_V1, IMAGE_LAYOUT_FILE_V1, ImageLayout} from '../types/oci/layout';
2729
import {MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from '../types/oci/mediatype';
2830

2931
export class OCI {
32+
public static defaultPlatform(): Platform {
33+
const nodePlatform = os.platform();
34+
const nodeArch = os.arch();
35+
36+
const goosMap: Record<string, string> = {
37+
win32: 'windows',
38+
sunos: 'solaris'
39+
// others (linux, darwin, freebsd, openbsd, netbsd, aix, android) match Go already
40+
};
41+
42+
const goArchMap: Record<string, string> = {
43+
x64: 'amd64',
44+
ia32: '386',
45+
arm: 'arm',
46+
arm64: 'arm64',
47+
ppc64: 'ppc64le',
48+
s390x: 's390x',
49+
riscv64: 'riscv64',
50+
loong64: 'loong64',
51+
mips: 'mips',
52+
mipsel: 'mipsle',
53+
mips64: 'mips64',
54+
mips64el: 'mips64le'
55+
};
56+
57+
const goos = goosMap[nodePlatform] ?? nodePlatform;
58+
const goarch = goArchMap[nodeArch] ?? nodeArch;
59+
60+
let variant: string | undefined;
61+
if (goarch === 'arm') {
62+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63+
const armVersionRaw = (process.config.variables as any)?.arm_version;
64+
const armVersion = Number(armVersionRaw);
65+
// Go only recognizes v5/v6/v7 for GOARM. Do not emit v8+ (that would be arm64).
66+
if ([5, 6, 7].includes(armVersion)) {
67+
variant = `v${armVersion}`;
68+
}
69+
}
70+
71+
return {
72+
architecture: goarch,
73+
os: goos,
74+
variant: variant
75+
};
76+
}
77+
3078
public static loadArchive(opts: LoadArchiveOpts): Promise<Archive> {
3179
return new Promise<Archive>((resolve, reject) => {
3280
const tarex: tar.Extract = tar.extract();

0 commit comments

Comments
 (0)