Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions native-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ tasks.register('buildNodePackage', Exec) {
inputs.file("${projectDir}/node/binding.gyp")
inputs.file("${projectDir}/node/tsconfig.json")
inputs.file("${projectDir}/node/scripts/pack-packages.mjs")
inputs.file("${projectDir}/node/README.md")
inputs.file("${rootProject.projectDir}/LICENSE.txt")
inputs.property('nativeVersion', project.version.toString())
outputs.dir("${projectDir}/node/dist")
outputs.file("${projectDir}/node/build/Release/dwlib_addon.node")
Expand Down
9 changes: 7 additions & 2 deletions native-lib/node/scripts/pack-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export async function packPackages({
...sourcePackage,
name: "dataweave-native",
version: packageVersion,
files: ["dist/", "docs/"],
license: "BSD-3-Clause",
files: ["dist/", "docs/", "README.md", "LICENSE.txt"],
// Source package.json pins 0.0.1 so local npm install can resolve optionals
// after a registry publish; this rewrite is the real packed version.
optionalDependencies,
Expand All @@ -70,6 +71,8 @@ export async function packPackages({
await writeFile(join(metaDir, "package.json"), `${JSON.stringify(metaPackage, null, 2)}\n`);
await cp(join(nodeDir, "dist"), join(metaDir, "dist"), { recursive: true });
await copyIfPresent(join(nodeDir, "docs"), join(metaDir, "docs"));
await cp(join(nodeDir, "README.md"), join(metaDir, "README.md"));
await cp(join(nodeDir, "..", "..", "LICENSE.txt"), join(metaDir, "LICENSE.txt"));
await runNpmPack(metaDir, nodeDir);

const nativeName = skipNative ? undefined : nativePackageName(platform, arch);
Expand All @@ -85,9 +88,11 @@ export async function packPackages({
main: "./dwlib_addon.node",
os: [platform],
cpu: [arch],
files: ["dwlib_addon.node", "dwlib.*"],
license: "BSD-3-Clause",
files: ["dwlib_addon.node", "dwlib.*", "LICENSE.txt"],
}, null, 2)}\n`);
await cp(join(nodeDir, "build", "Release", "dwlib_addon.node"), join(nativeDir, "dwlib_addon.node"));
await cp(join(nodeDir, "..", "..", "LICENSE.txt"), join(nativeDir, "LICENSE.txt"));
const nativeFiles = await readdir(join(nodeDir, "native"));
const nativeLibrary = nativeFiles.find((file) => /^dwlib\.(dylib|so|dll)$/.test(file));
if (nativeLibrary === undefined) {
Expand Down
60 changes: 56 additions & 4 deletions native-lib/node/scripts/pack-packages.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { gunzipSync } from "node:zlib";
import { npmPackInvocation, packPackages } from "./pack-packages.mjs";

const tempDirs = [];

function makeNodeDir() {
const nodeDir = mkdtempSync(join(tmpdir(), "dw-node-pack-test-"));
tempDirs.push(nodeDir);
const repoDir = mkdtempSync(join(tmpdir(), "dw-node-pack-test-"));
const nodeDir = join(repoDir, "native-lib", "node");
tempDirs.push(repoDir);
mkdirSync(nodeDir, { recursive: true });
mkdirSync(join(nodeDir, "dist"), { recursive: true });
mkdirSync(join(nodeDir, "build", "Release"), { recursive: true });
mkdirSync(join(nodeDir, "native"), { recursive: true });
writeFileSync(join(nodeDir, "dist", "index.js"), "export const dataweave = true;\n");
writeFileSync(join(nodeDir, "build", "Release", "dwlib_addon.node"), "addon");
writeFileSync(join(nodeDir, "native", "dwlib.dylib"), "native library");
writeFileSync(join(nodeDir, "README.md"), "# DataWeave Node.js Bindings\n");
writeFileSync(join(repoDir, "LICENSE.txt"), "BSD 3-Clause License\n");
writeFileSync(join(nodeDir, "package.json"), JSON.stringify({
name: "@dataweave/native",
version: "0.0.1",
Expand All @@ -37,6 +42,25 @@ function readJson(path) {
return JSON.parse(readFileSync(path, "utf8"));
}

function readTarEntries(tarball) {
const tar = gunzipSync(readFileSync(tarball));
const entries = new Map();
let offset = 0;

while (offset + 512 <= tar.length && tar[offset] !== 0) {
const name = tar.subarray(offset, offset + 100).toString("utf8").replace(/\0.*$/, "");
const size = Number.parseInt(
tar.subarray(offset + 124, offset + 136).toString("utf8").replace(/\0.*$/, "").trim(),
8,
);
const contentStart = offset + 512;
entries.set(name, tar.subarray(contentStart, contentStart + size).toString("utf8"));
offset = contentStart + Math.ceil(size / 512) * 512;
}

return entries;
}

after(() => {
for (const dir of tempDirs) {
rmSync(dir, { recursive: true, force: true });
Expand Down Expand Up @@ -64,21 +88,49 @@ test("packs meta and supported native package staging", async () => {
version: "1.2.3",
platform: "darwin",
arch: "arm64",
runNpmPack: async () => {},
});

const meta = readJson(join(nodeDir, "build", "npm", "dataweave-native", "package.json"));
assert.equal(meta.name, "dataweave-native");
assert.equal(meta.version, "1.2.3");
assert.equal(meta.license, "BSD-3-Clause");
assert.equal(meta.optionalDependencies["dataweave-native-darwin-arm64"], "1.2.3");
assert.equal(meta.gypfile, undefined);
assert.deepEqual(meta.files, ["dist/", "docs/"]);
assert.deepEqual(meta.files, ["dist/", "docs/", "README.md", "LICENSE.txt"]);
assert.equal(
readFileSync(join(nodeDir, "build", "npm", "dataweave-native", "README.md"), "utf8"),
"# DataWeave Node.js Bindings\n",
);
assert.equal(
readFileSync(join(nodeDir, "build", "npm", "dataweave-native", "LICENSE.txt"), "utf8"),
"BSD 3-Clause License\n",
);

const tarEntries = readTarEntries(join(nodeDir, "dataweave-native-1.2.3.tgz"));
assert.equal(tarEntries.get("package/README.md"), "# DataWeave Node.js Bindings\n");
assert.equal(tarEntries.get("package/LICENSE.txt"), "BSD 3-Clause License\n");
const tarPackage = JSON.parse(tarEntries.get("package/package.json"));
assert.equal(tarPackage.license, "BSD-3-Clause");
assert.deepEqual(tarPackage.files, ["dist/", "docs/", "README.md", "LICENSE.txt"]);

const native = readJson(join(nodeDir, "build", "npm", "dataweave-native-darwin-arm64", "package.json"));
assert.equal(native.name, "dataweave-native-darwin-arm64");
assert.deepEqual(native.os, ["darwin"]);
assert.deepEqual(native.cpu, ["arm64"]);
assert.equal(native.main, "./dwlib_addon.node");
assert.equal(native.license, "BSD-3-Clause");
assert.deepEqual(native.files, ["dwlib_addon.node", "dwlib.*", "LICENSE.txt"]);
assert.equal(
readFileSync(join(nodeDir, "build", "npm", "dataweave-native-darwin-arm64", "LICENSE.txt"), "utf8"),
"BSD 3-Clause License\n",
);

const nativeTarEntries = readTarEntries(join(nodeDir, "dataweave-native-darwin-arm64-1.2.3.tgz"));
assert.equal(nativeTarEntries.has("package/README.md"), false);
assert.equal(nativeTarEntries.get("package/LICENSE.txt"), "BSD 3-Clause License\n");
const nativeTarPackage = JSON.parse(nativeTarEntries.get("package/package.json"));
assert.equal(nativeTarPackage.license, "BSD-3-Clause");
assert.deepEqual(nativeTarPackage.files, ["dwlib_addon.node", "dwlib.*", "LICENSE.txt"]);
});

test("packs only the meta package for an unsupported platform and architecture", async () => {
Expand Down
Loading