Skip to content
Open
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
30 changes: 14 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
name: CI
on:
push:
branches:
- master
branches: [master]
pull_request:
branches:
- master
branches: [master]
merge_group:
branches: [master]

jobs:
build:
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
node_version: [16]
name: CI on NodeJS v${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- run: git config --global core.autocrlf false
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
- run: yarn
- run: yarn ci
node-version: 22
- run: yarn install --immutable
- run: yarn lint
- run: yarn prettier-check
- run: yarn build
- run: yarn ts-check
- run: yarn test --ci
21 changes: 21 additions & 0 deletions .github/workflows/lint-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 'Lint PR'

on:
pull_request:
branches: [master]

jobs:
lint-pr:
# This job validates the PR title against the conventional commit format. Needed for semantic-release to generate release notes.
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
with:
types: |
fix
feat
chore
revert
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Release
on:
workflow_dispatch:
inputs:
cliArgs:
description: 'CLI args'
required: false
default: ''
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- run: yarn install --immutable --immutable-cache

- name: Set npm token
uses: actions/github-script@v7
env:
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
with:
script: |
if (!process.env.NPM_TOKEN) {
throw new Error('NPM_TOKEN env var not set');
}

const lines = [
// set npm auth token with publish permission from environment
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`,

// this allows to execute npm lifecycle scripts by root
'unsafe-perm = true',
];

fs.writeFileSync('.npmrc', lines.join('\n'));

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.FLOW_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
run: npx semantic-release ${{ github.event.inputs.cliArgs }}
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ coverage
node_modules
dist
.idea
yarn-error.log
yarn-error.log

.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
934 changes: 934 additions & 0 deletions .yarn/releases/yarn-4.5.3.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-4.5.3.cjs
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tef-novum/webview-bridge",
"name": "@telefonica/webview-bridge",
"version": "3.46.0",
"description": "JavaScript library to access to native functionality. Requires a webview with a postMessage bridge.",
"main": "./dist/webview-bridge-cjs.js",
Expand All @@ -15,8 +15,8 @@
"lint": "eslint --report-unused-disable-directives .",
"ts-check": "tsc --project tsconfig.json --noEmit",
"fix-code": "yarn lint -- --fix && yarn prettier -- --write",
"ci": "yarn prettier-check && yarn lint && yarn ts-check && yarn test --ci && yarn build",
"prepublishOnly": "yarn ci"
"prepublishOnly": "node scripts/prepublish-only.js",
"prepack": "yarn build"
},
"repository": {
"type": "git",
Expand All @@ -28,8 +28,9 @@
"url": "https://github.com/tef-dig/webview-bridge/issues"
},
"homepage": "https://github.com/tef-dig/webview-bridge#readme",
"dependencies": {},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@types/gtag.js": "^0.0.10",
"@types/jest": "^27.0.3",
"@typescript-eslint/eslint-plugin": "^5.6.0",
Expand All @@ -42,8 +43,20 @@
"rimraf": "^3.0.2",
"rollup": "^2.61.0",
"rollup-plugin-typescript2": "^0.31.1",
"semantic-release": "^24.2.0",
"ts-jest": "^27.1.1",
"typescript": "^4.5.3",
"uglify-es": "^3.3.9"
}
},
"release": {
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git",
"@semantic-release/github"
]
},
"packageManager": "[email protected]"
}
12 changes: 12 additions & 0 deletions scripts/prepublish-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (!process.env.CI) {
const lines = [
'',
'Cannot publish from this machine',
'',
'To publish, use the Github Release action workflow:',
'https://github.com/Telefonica/webview-bridge/actions?query=workflow%3ARelease',
'',
];
console.error(lines.join('\n'));
process.exit(1);
}
67 changes: 61 additions & 6 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,48 @@ type RequestListener = (message: RequestFromNative) => void;
type ResponseListener = (message: ResponseFromNative) => void;
type MessageListener = RequestListener | ResponseListener;

type NovumPostMessage = (jsonMessage: string) => void;

type IosFirebase = {
postMessage: (message: {command: string; [key: string]: any}) => void;
};

type AndroidFirebase = {
logEvent: (name: string, eventJson: string) => void;
setScreenName: (name: string) => void;
setScreenNameWithParams?: (name: string, paramsJson: string) => void;
setUserProperty: (name: string, value: string) => void;
};

declare global {
interface Window {
// iOS
webkit?: {
messageHandlers?: {
tuentiWebView?: {
postMessage?: NovumPostMessage;
};
firebase?: IosFirebase;
};
};

// Android
tuentiWebView?: {
postMessage?: NovumPostMessage;
};

AnalyticsWebInterface?: AndroidFirebase;

// Web
__tuenti_webview_bridge?: {
postMessage: NovumPostMessage;
messageListeners: Array<MessageListener>;
};

gtag?: Gtag.Gtag;
}
}

const BRIDGE = '__tuenti_webview_bridge';

const hasAndroidPostMessage = () =>
Expand Down Expand Up @@ -474,14 +516,26 @@ const getWebViewPostMessage = (): NovumPostMessage | null => {
return null;
};

let messageListeners: Array<MessageListener> = [];
const getMessageListeners = () => {
if (typeof window === 'undefined') {
return [];
}
return window[BRIDGE]?.messageListeners ?? [];
};

const setMessageListeners = (listeners: Array<MessageListener>) => {
if (typeof window === 'undefined' || !window[BRIDGE]) {
return;
}
window[BRIDGE].messageListeners = listeners;
};

const subscribe = (listener: MessageListener) => {
messageListeners.push(listener);
getMessageListeners().push(listener);
};

const unsubscribe = (listener: MessageListener) => {
messageListeners = messageListeners.filter((f) => f !== listener);
setMessageListeners(getMessageListeners().filter((f) => f !== listener));
};

const isInIframe = () => {
Expand Down Expand Up @@ -572,10 +626,10 @@ export const postMessageToNativeApp = <T extends keyof ResponsesFromNativeApp>(
};

/**
* Initiates WebApp postMessage function, which will be called by native apps
* Initiates postMessage function, which will be called by native apps
*/
if (typeof window !== 'undefined') {
window[BRIDGE] = window[BRIDGE] || {
window[BRIDGE] = {
postMessage: (jsonMessage: string) => {
log?.('[WebView Bridge] RCVD:', jsonMessage);
let message: any;
Expand All @@ -584,8 +638,9 @@ if (typeof window !== 'undefined') {
} catch (e) {
throw Error(`Problem parsing webview message: ${jsonMessage}`);
}
messageListeners.forEach((f) => f(message));
getMessageListeners().forEach((f) => f(message));
},
messageListeners: getMessageListeners() ?? [],
};
}

Expand Down
38 changes: 0 additions & 38 deletions src/types.d.ts

This file was deleted.

Loading