Skip to content
Draft
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
28 changes: 12 additions & 16 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,42 @@ jobs:
docker: true
alpine: true
arch: amd64
- os: macos-14
docker: false
alpine: false
arch: arm64
name: Prebuild ${{ matrix.docker == true && matrix.alpine == true && 'linux-musl' || matrix.docker == true && matrix.alpine == false && 'linux' || matrix.os }}-${{ matrix.arch }}

env:
NODE_VERSION: ${{ matrix.node-version }}

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
fetch-depth: 0

- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
node-version: ${{ env.NODE_VERSION }}

- if: ${{ runner.os == 'Windows' && ( github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ runner.os == 'Windows' && ( github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
run: echo "ONLY_DOWNLOAD_PACT_FOR_WINDOWS=true" >> $GITHUB_ENV
- if: ${{ (matrix.docker == true && matrix.alpine == true) && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ (matrix.docker == true && matrix.alpine == true) && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
name: prebuild linux ${{ matrix.arch }} musl
run: docker run -v $PWD:/home --platform linux/${{ matrix.arch }} --rm node:20-alpine bin/sh -c 'apk add bash && cd /home && bash -c "/home/script/ci/prebuild-alpine.sh" && rm -rf ffi node_modules'

- if: ${{ (matrix.docker == true && matrix.alpine != true )&& (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ (matrix.docker == true && matrix.alpine != true )&& (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
name: prebuild linux ${{ matrix.arch }}
run: docker run -v $PWD:/home --platform linux/${{ matrix.arch }} --rm node:20 bin/bash -c 'cd /home && /home/script/ci/prebuild.sh && rm -rf ffi node_modules'

- run: sudo chown -R $(id -u):$(id -g) prebuilds
if: ${{ matrix.docker == true && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
if: ${{ matrix.docker == true && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}

- run: ./script/ci/prebuild.sh
if: ${{ matrix.docker != true && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
if: ${{ matrix.docker != true && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}

- name: Upload prebuild for ${{ runner.os }}-${{ runner.arch }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
path: prebuilds/*.tar.gz
name: artifact-${{ matrix.docker == true && matrix.alpine == true && 'linux-musl' || matrix.docker == true && matrix.alpine == false && 'linux' || matrix.os }}-${{ matrix.arch }}
Expand All @@ -133,7 +129,7 @@ jobs:
fail-fast: false
matrix:
node-version: [22, 24, 26]
os: [macos-14, macos-15-intel, macos-latest, ubuntu-latest, windows-latest, ubuntu-24.04-arm]
os: [macos-15-intel, macos-latest, ubuntu-latest, windows-latest, ubuntu-24.04-arm]
docker: [false]
include:
- os: ubuntu-24.04-arm
Expand Down Expand Up @@ -180,10 +176,10 @@ jobs:
fetch-depth: 0

- name: Download prebuilds
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
- run: FETCH_ASSETS=true REPO=pact-foundation/pact-js-core ./script/ci/check-release-libs.sh --fetch-assets
if: ${{ github.ref != 'refs/heads/master' && !contains(github.event.pull_request.title, '(rebuild)')}}
if: ${{ github.ref != 'refs/heads/master' && !(true || contains(github.event.pull_request.title, '(rebuild)'))}}
env:
GITHUB_TOKEN: ${{ github.token }}

Expand All @@ -194,7 +190,7 @@ jobs:

- if: runner.os == 'Windows'
run: echo "ONLY_DOWNLOAD_PACT_FOR_WINDOWS=true" >> $GITHUB_ENV
- if: matrix.os == 'macos-14' || matrix.os == 'macos-latest'
- if: matrix.os == 'macos-latest'
run: brew install protobuf

- run: LOG_LEVEL=debug ./script/ci/unpack-and-test.sh
Expand Down
3 changes: 3 additions & 0 deletions native/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "pactffiInit"), Napi::Function::New(env, PactffiInit));
exports.Set(Napi::String::New(env, "pactffiInitWithLogLevel"), Napi::Function::New(env, PactffiInitWithLogLevel));
exports.Set(Napi::String::New(env, "pactffiLogToFile"), Napi::Function::New(env, PactffiLogToFile));
exports.Set(Napi::String::New(env, "pactffiLogToBuffer"), Napi::Function::New(env, PactffiLogToBuffer));
exports.Set(Napi::String::New(env, "pactffiFetchLogBuffer"), Napi::Function::New(env, PactffiFetchLogBuffer));

// Consumer
exports.Set(Napi::String::New(env, "pactffiMockServerMatched"), Napi::Function::New(env, PactffiMockServerMatched));
exports.Set(Napi::String::New(env, "pactffiMockServerMismatches"), Napi::Function::New(env, PactffiMockServerMismatches));
exports.Set(Napi::String::New(env, "pactffiMockServerLogs"), Napi::Function::New(env, PactffiMockServerLogs));
exports.Set(Napi::String::New(env, "pactffiCreateMockServerForTransport"), Napi::Function::New(env, PactffiCreateMockServerForTransport));
exports.Set(Napi::String::New(env, "pactffiCleanupMockServer"), Napi::Function::New(env, PactffiCleanupMockServer));
exports.Set(Napi::String::New(env, "pactffiGetTlsCaCertificate"), Napi::Function::New(env, PactffiGetTlsCaCertificate));
Expand Down
59 changes: 50 additions & 9 deletions native/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,29 @@ InteractionPart integerToInteractionPart(Napi::Env &env, uint32_t number) {
* const char *pactffi_fetch_log_buffer(const char *log_id);
*/
Napi::Value PactffiFetchLogBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Env env = info.Env();

if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiFetchLogBuffer received < 1 arguments");
}
// The log_id is optional: without it the "global" buffer is fetched
std::string log_id;
bool has_log_id = info.Length() > 0 && !info[0].IsUndefined() && !info[0].IsNull();

if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiFetchLogBuffer(log_id) expected a string");
if (has_log_id) {
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiFetchLogBuffer(log_id) expected a string");
}
log_id = info[0].As<Napi::String>().Utf8Value();
}

std::string log_id = info[0].As<Napi::String>().Utf8Value();
const char* buffer = pactffi_fetch_log_buffer(has_log_id ? log_id.c_str() : NULL);

if (buffer == NULL) {
return env.Null();
}

const char* buffer = pactffi_fetch_log_buffer(log_id.c_str());
Napi::String result = Napi::String::New(env, buffer);
pactffi_string_delete((char*)buffer);

return Napi::String::New(env, buffer);
return result;
}

/**
Expand Down Expand Up @@ -187,6 +195,39 @@ Napi::Value PactffiMockServerMismatches(const Napi::CallbackInfo& info) {
return Napi::String::New(env, res);
}

/**
* Fetch the logs for the mock server. This needs the memory buffer log sink to be setup before
* the mock server is started. Returned string will be freed with the `cleanup_mock_server`
* function call.
*
* Will return a NULL pointer if the logs for the mock server can not be retrieved.
*
* C interface:
*
* const char *pactffi_mock_server_logs(int32_t mock_server_port);
*/
Napi::Value PactffiMockServerLogs(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiMockServerLogs received < 1 arguments");
}

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiMockServerLogs(port) expected a number");
}

int32_t port = info[0].As<Napi::Number>().Int32Value();
const char* res = pactffi_mock_server_logs(port);

if (res == NULL) {
return env.Null();
}

// Owned by the mock server, and freed by pactffi_cleanup_mock_server
return Napi::String::New(env, res);
}

/**
* Create a mock server for the provided Pact handle and transport. If the transport is not
* provided (it is a NULL pointer or an empty string), will default to an HTTP transport. The
Expand Down
29 changes: 29 additions & 0 deletions native/ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ Napi::Value PactffiLogToFile(const Napi::CallbackInfo& info) {

return Napi::Number::New(env, res);
}

/**
* Convenience function to direct all logging to a task local memory buffer.
*
* The contents of the buffer can be fetched using `pactffi_fetch_log_buffer`, and the logs
* for a mock server with `pactffi_mock_server_logs`.
*
* C interface:
*
* int pactffi_log_to_buffer(LevelFilter level_filter);
*/
Napi::Value PactffiLogToBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiLogToBuffer(levelFilter) received < 1 argument");
}

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiLogToBuffer(levelFilter) expected a number");
}

uint32_t levelFilterNumber = info[0].As<Napi::Number>().Uint32Value();
LevelFilter levelFilter = integerToLevelFilter(env, levelFilterNumber);

int res = pactffi_log_to_buffer(levelFilter);

return Napi::Number::New(env, res);
}
/*

Napi::Value Pactffi_log_message(const Napi::CallbackInfo& info) {
Expand Down
6 changes: 3 additions & 3 deletions native/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Napi::Value PactffiVersion(const Napi::CallbackInfo& info);
Napi::Value PactffiInit(const Napi::CallbackInfo& info);
Napi::Value PactffiInitWithLogLevel(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToFile(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToBuffer(const Napi::CallbackInfo& info);
Napi::Value PactffiFetchLogBuffer(const Napi::CallbackInfo& info);

// Unimplemented
Napi::Value PactffiCheckRegex(const Napi::CallbackInfo& info);
Napi::Value PactffiFetchLogBuffer(const Napi::CallbackInfo& info);
Napi::Value PactffiFreeString(const Napi::CallbackInfo& info);
Napi::Value PactffiLogMessage(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToBuffer(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToFile(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToStderr(const Napi::CallbackInfo& info);
Napi::Value PactffiLogToStdout(const Napi::CallbackInfo& info);
Napi::Value PactffiLoggerApply(const Napi::CallbackInfo& info);
Expand Down
2 changes: 1 addition & 1 deletion script/lib/download-ffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require_binary mktemp

require_env_var FFI_VERSION

BASEURL=https://github.com/pact-foundation/pact-reference/releases/download
BASEURL=${PACT_FFI_BASEURL:-https://github.com/YOU54F/pact-reference/releases/download}
FFI_DIR="${LIB_DIR}/../../ffi"

if [[ $(find "${FFI_DIR}" -name "${FFI_VERSION}*") ]]; then
Expand Down
27 changes: 27 additions & 0 deletions src/consumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ export const setTestRunId = (
getFfiLib(logLevel, logFile).pactffiSetTestRunId(testRunId);
};

/**
* Sends the native core logs to an in-memory buffer instead of stdout or a
* log file, so that they can be read back in-process with `fetchLogBuffer` and
* `ConsumerPact.mockServerLogs`.
*
* The native core only takes its log configuration once per process, so this
* must be called before anything else that uses the native core (such as
* `makeConsumerPact`), otherwise it has no effect.
*/
export const logToBuffer = (logLevel = getLogLevel()): void => {
getFfiLib(logLevel, undefined, true);
};

/**
* Returns the contents of the native core's in-memory log buffer, or null if
* it can't be fetched. Logs from a mock server are kept in their own buffer,
* use `ConsumerPact.mockServerLogs` to read those.
*
* Only has contents if `logToBuffer` was called first.
*
* @param logId the buffer to fetch, defaults to the "global" buffer
*/
export const fetchLogBuffer = (logId?: string): string | null =>
getFfiLib().pactffiFetchLogBuffer(logId);

export const makeConsumerPact = (
consumer: string,
provider: string,
Expand Down Expand Up @@ -245,6 +270,8 @@ export const makeConsumerPact = (
ffi.pactffiMockServerMatched(port),
mockServerMismatches: (port: number): MatchingResult[] =>
mockServerMismatches(ffi, port),
mockServerLogs: (port: number): string | null =>
ffi.pactffiMockServerLogs(port),
cleanupMockServer: (mockServerPort: number): boolean =>
wrapWithCheck<(port: number) => boolean>(
(port: number): boolean => ffi.pactffiCleanupMockServer(port),
Expand Down
8 changes: 8 additions & 0 deletions src/consumer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ export type ConsumerPact = PluginPact & {
) => number;
createMockServer: (address: string, port?: number, tls?: boolean) => number;
mockServerMismatches: (port: number) => MatchingResult[];
/**
* Returns the native core logs for a mock server, or null if they can't be
* fetched. Only has contents if `logToBuffer` was called before the native
* core was first used. Read them before calling `cleanupMockServer`.
*
* @param port the port number the mock server is running on.
*/
mockServerLogs: (port: number) => string | null;
cleanupMockServer: (port: number) => boolean;
/**
* This function writes the pact file, regardless of whether or not the test was successful.
Expand Down
33 changes: 31 additions & 2 deletions src/ffi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Ffi, FfiLogLevelFilter } from './types';

const bindings = require('node-gyp-build') as (dir?: string) => Ffi;

export const PACT_FFI_VERSION = '0.5.12';
export const PACT_FFI_VERSION = '0.5.13-rc.1';

/**
* Returns the library path which is located inside `node_modules`
Expand Down Expand Up @@ -165,9 +165,23 @@ const initialiseFfi = (): typeof ffi => {
return ffiLib;
};

/**
* Returns the native library, initialising it on the first call.
*
* The native core only takes its log configuration once per process, so the
* `logLevel`, `logFile` and `logToBuffer` arguments only take effect on the
* first call.
*
* @param logLevel the level the native core logs at
* @param logFile write the native core logs to this file
* @param logToBuffer write the native core logs to an in-memory buffer, which
* can be read back with `pactffiFetchLogBuffer` and `pactffiMockServerLogs`.
* Takes precedence over `logFile`.
*/
export const getFfiLib = (
logLevel: LogLevel = DEFAULT_LOG_LEVEL,
logFile: string | undefined = undefined,
logToBuffer = false,
): typeof ffi => {
if (!ffi) {
logger.trace('Initialising ffi for the first time');
Expand All @@ -176,7 +190,18 @@ export const getFfiLib = (
`Initialising native core at log level '${logLevel}'`,
logFile,
);
if (logFile) {
if (logToBuffer) {
if (logFile) {
logger.warn(
`Logging to an in-memory buffer, so not writing a log file to ${logFile}`,
);
}
logger.debug(`writing logs at level ${logLevel} to an in-memory buffer`);
const res = ffiLib.pactffiLogToBuffer(FfiLogLevelFilter[logLevel] ?? 3);
if (res !== 0) {
logger.warn(`Failed to log to an in-memory buffer, reason: ${res}`);
}
} else if (logFile) {
logger.debug(`writing log file at level ${logLevel} to ${logFile}`);
const res = ffiLib.pactffiLogToFile(
logFile,
Expand All @@ -188,6 +213,10 @@ export const getFfiLib = (
} else {
ffiLib.pactffiInitWithLogLevel(logLevel);
}
} else if (logToBuffer) {
logger.warn(
'The native core was already initialised, so its logs are not being written to an in-memory buffer',
);
}
return ffi;
};
13 changes: 12 additions & 1 deletion src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,24 @@ export type FfiConsumerFunctions = {
pactffiCleanupMockServer(port: number): boolean;
pactffiMockServerMatched(port: number): boolean;
pactffiMockServerMismatches(port: number): string;
/**
* Returns the logs for the mock server, or null if they can't be fetched.
* The native core must be logging to a buffer (`pactffiLogToBuffer`) before
* the mock server is started, and the logs must be read before the mock
* server is cleaned up.
*/
pactffiMockServerLogs(port: number): string | null;
pactffiGetTlsCaCertificate(): string | null;
pactffiLogMessage(source: string, logLevel: string, message: string): void;
pactffiLogToBuffer(level: FfiLogLevelFilter): number;
pactffiInitWithLogLevel(level: string): void;
pactffiLogToStdout(level: FfiLogLevelFilter): number;
pactffiLogToFile(fileName: string, level: FfiLogLevelFilter): number;
pactffiFetchLogBuffer(logId: number): string;
/**
* Returns the in-memory log buffer for the given log id, or the "global"
* buffer if no id is given. Returns null if the buffer can't be fetched.
*/
pactffiFetchLogBuffer(logId?: string): string | null;
pactffiUsingPlugin(
handle: FfiPactHandle,
name: string,
Expand Down
Loading
Loading