Skip to content

Commit

Permalink
Merge pull request #3927 from NoelDeMartin/MOBILE-4304
Browse files Browse the repository at this point in the history
MOBILE-4304: Replace WebSQL with sqlite-wasm
  • Loading branch information
dpalou authored Feb 14, 2024
2 parents 829c59e + d944026 commit f31eb83
Show file tree
Hide file tree
Showing 57 changed files with 2,477 additions and 1,880 deletions.
39 changes: 31 additions & 8 deletions .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ jobs:
working-directory: app
run: npm run build:test

- name: Generate SSL certificates
working-directory: app
run: |
mkdir ./ssl
openssl req -x509 -nodes \
-days 365 \
-newkey rsa:2048 \
-keyout ./ssl/certificate.key \
-out ./ssl/certificate.crt \
-subj="/O=Moodle"
- name: Build Behat plugin
working-directory: app
run: ./scripts/build-behat-plugin.js ../plugin
Expand Down Expand Up @@ -111,11 +122,12 @@ jobs:

- uses: actions/cache/save@v4
with:
key: build-${{ github.sha }}
path: |
app/node_modules/**/*
app/www/**/*
plugin/**/*
key: build-${{ github.sha }}
path: |
app/ssl/**/*
app/node_modules/**/*
app/www/**/*
plugin/**/*
behat:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -157,23 +169,33 @@ jobs:
with:
key: build-${{ github.sha }}
path: |
app/ssl/**/*
app/node_modules/**/*
app/www/**/*
plugin/**/*
- name: Launch Docker images
working-directory: app
run: |
docker run -d --rm -p 8001:80 --name moodleapp -v ./www:/usr/share/nginx/html -v ./nginx.conf:/etc/nginx/conf.d/default.conf nginx:alpine
docker run -d --rm \
-p 8001:443 \
--name moodleapp \
-v ./www:/usr/share/nginx/html \
-v ./nginx.conf:/etc/nginx/conf.d/default.conf \
-v ./ssl/certificate.crt:/etc/ssl/certificate.crt \
-v ./ssl/certificate.key:/etc/ssl/certificate.key \
nginx:alpine
docker run -d --rm -p 8002:80 --name bigbluebutton moodlehq/bigbluebutton_mock:latest
- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4.3
git clone https://github.com/NoelDeMartin/moodle-plugin-ci --branch selenium-env ci
composer install -d ./ci
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
sed -i "58i\$CFG->behat_profiles['chrome']['capabilities'] = ['extra_capabilities' => ['chromeOptions' => ['args' => ['--ignore-certificate-errors', '--allow-running-insecure-content']]]];" ci/res/template/config.php.txt
- name: Install Behat Snapshots plugin
run: moodle-plugin-ci add-plugin NoelDeMartin/moodle-local_behatsnapshots
Expand All @@ -184,7 +206,7 @@ jobs:
DB: pgsql
MOODLE_BRANCH: ${{ github.event.inputs.moodle_branch || 'main' }}
MOODLE_REPO: ${{ github.event.inputs.moodle_repository || 'https://github.com/moodle/moodle.git' }}
MOODLE_BEHAT_IONIC_WWWROOT: http://localhost:8001
MOODLE_BEHAT_IONIC_WWWROOT: https://localhost:8001
MOODLE_BEHAT_DEFAULT_BROWSER: chrome

- name: Update config
Expand All @@ -194,6 +216,7 @@ jobs:
run: moodle-plugin-ci behat --auto-rerun 3 --profile chrome --tags="@app&&~@local&&$BEHAT_TAGS"
env:
BEHAT_TAGS: ${{ matrix.tags }}
MOODLE_BEHAT_SELENIUM_IMAGE: selenium/standalone-chrome:120.0

- name: Upload Snapshot failures
uses: actions/upload-artifact@v4
Expand Down
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ ARG build_command="npm run build:prod"
COPY . /app
RUN ${build_command}

# Generate SSL certificate
RUN mkdir /app/ssl
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /app/ssl/certificate.key -out /app/ssl/certificate.crt -subj="/O=Moodle"

## SERVE STAGE
FROM nginx:alpine as serve-stage

# Copy assets & config
COPY --from=build-stage /app/www /usr/share/nginx/html
COPY --from=build-stage /app/ssl/certificate.crt /etc/ssl/certificate.crt
COPY --from=build-stage /app/ssl/certificate.key /etc/ssl/certificate.key
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
HEALTHCHECK --interval=10s --timeout=4s CMD curl -f http://localhost/assets/env.json || exit 1
EXPOSE 443
HEALTHCHECK --interval=10s --timeout=4s CMD curl --insecure -f https://localhost/assets/env.json || exit 1
6 changes: 5 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@
"options": {
"disableHostCheck": true,
"port": 8100,
"buildTarget": "app:build"
"buildTarget": "app:build",
"headers": {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp"
}
},
"configurations": {
"production": {
Expand Down
32 changes: 32 additions & 0 deletions local_moodleappbehat/tests/behat/behat_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,38 @@ public function the_app_should_have_opened_a_browser_tab(bool $not = false, ?str
});
}

/**
* Check that the app opened a url.
*
* @Then /^the app should( not)? have opened url "([^"]+)"(?: with contents "([^"]+)")?(?: (once|\d+ times))?$/
* @param bool $not Whether to check if the app did not open the url
* @param string $urlpattern Url pattern
* @param string $contents Url contents
* @param string $times How many times the url should have been opened
*/
public function the_app_should_have_opened_url(bool $not, string $urlpattern, ?string $contents = null, ?string $times = null) {
if (is_null($times) || $times === 'once') {
$times = 1;
} else {
$times = intval(substr($times, 0, strlen($times) - 6));
}

$this->spin(function() use ($not, $urlpattern, $contents, $times) {
$result = $this->runtime_js("hasOpenedUrl('$urlpattern', '$contents', $times)");

// TODO process times
if ($not && $result === 'OK') {
throw new DriverException('Error, an url was opened that should not have');
}

if (!$not && $result !== 'OK') {
throw new DriverException('Error asserting that url was opened - ' . $result);
}

return true;
});
}

/**
* Switches to a newly-opened browser tab.
*
Expand Down
16 changes: 15 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
server {
listen 0.0.0.0:80;
listen 80;
listen 443 ssl;
root /usr/share/nginx/html;
server_tokens off;
access_log off;

# Configure SSL
if ($scheme = "http") {
return 301 https://$host$request_uri;
}

ssl_certificate /etc/ssl/certificate.crt;
ssl_certificate_key /etc/ssl/certificate.key;
ssl_protocols TLSv1.3;

# Enable OPFS
add_header Cross-Origin-Opener-Policy "same-origin";
add_header Cross-Origin-Embedder-Policy "require-corp";

location / {
try_files $uri $uri/ /index.html;
}
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"scripts": {
"ng": "ng",
"start": "ionic serve --browser=$MOODLE_APP_BROWSER",
"start": "ionic serve --browser=$MOODLE_APP_BROWSER --ssl",
"serve:test": "NODE_ENV=testing ionic serve --no-open",
"build": "ionic build",
"build:prod": "NODE_ENV=production ionic build --prod",
Expand Down Expand Up @@ -88,6 +88,7 @@
"@moodlehq/phonegap-plugin-push": "4.0.0-moodle.7",
"@ngx-translate/core": "^15.0.0",
"@ngx-translate/http-loader": "^8.0.0",
"@sqlite.org/sqlite-wasm": "^3.45.0-build1",
"@types/chart.js": "^2.9.31",
"@types/cordova": "0.0.34",
"@types/dom-mediacapture-record": "1.0.7",
Expand Down
57 changes: 57 additions & 0 deletions patches/@sqlite.org+sqlite-wasm+3.45.0-build1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs b/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs
index b86a0aa..1be2b82 100644
--- a/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs
+++ b/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs
@@ -533,7 +533,7 @@ var sqlite3InitModule = (() => {
wasmBinaryFile = locateFile(wasmBinaryFile);
}
} else {
- wasmBinaryFile = new URL('sqlite3.wasm', import.meta.url).href;
+ wasmBinaryFile = '/assets/lib/sqlite3/sqlite3.wasm';
}

function getBinary(file) {
@@ -10913,6 +10913,10 @@ var sqlite3InitModule = (() => {
}
},

+ lastInsertRowId: function () {
+ return capi.sqlite3_last_insert_rowid(affirmDbOpen(this).pointer);
+ },
+
dbFilename: function (dbName = 'main') {
return capi.sqlite3_db_filename(affirmDbOpen(this).pointer, dbName);
},
@@ -11877,12 +11881,14 @@ var sqlite3InitModule = (() => {
if (!hadColNames) rc.columnNames = [];

rc.callback = function (row, stmt) {
+ const rowId = rc.sql.includes('INSERT') ? db.lastInsertRowId() : undefined;
wState.post(
{
type: theCallback,
columnNames: rc.columnNames,
rowNumber: ++rowNumber,
row: row,
+ rowId,
},
wState.xfer,
);
@@ -12522,7 +12528,7 @@ var sqlite3InitModule = (() => {
return promiseResolve_(sqlite3);
};
const W = new Worker(
- new URL('sqlite3-opfs-async-proxy.js', import.meta.url),
+ '/assets/lib/sqlite3/sqlite3-opfs-async-proxy.js',
);
setTimeout(() => {
if (undefined === promiseWasRejected) {
@@ -13445,7 +13451,7 @@ var sqlite3InitModule = (() => {
});
return thePromise;
};
- installOpfsVfs.defaultProxyUri = 'sqlite3-opfs-async-proxy.js';
+ installOpfsVfs.defaultProxyUri = '/assets/lib/sqlite3/sqlite3-opfs-async-proxy.js';
globalThis.sqlite3ApiBootstrap.initializersAsync.push(
async (sqlite3) => {
try {
2 changes: 2 additions & 0 deletions scripts/copy-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const ASSETS = {
'/src/core/features/h5p/assets': '/lib/h5p',
'/node_modules/ogv/dist': '/lib/ogv',
'/node_modules/video.js/dist/video-js.min.css': '/lib/video.js/video-js.min.css',
'/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm': '/lib/sqlite3/sqlite3.wasm',
'/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js': '/lib/sqlite3/sqlite3-opfs-async-proxy.js',
};

module.exports = function(ctx) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions src/addons/mod/scorm/services/database/scorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { CoreSiteSchema } from '@services/sites';
* Database variables for AddonModScormOfflineProvider.
*/
export const ATTEMPTS_TABLE_NAME = 'addon_mod_scorm_offline_attempts';
export const ATTEMPTS_TABLE_PRIMARY_KEYS = ['scormid', 'userid', 'attempt'] as const;
export const TRACKS_TABLE_NAME = 'addon_mod_scorm_offline_scos_tracks';
export const TRACKS_TABLE_PRIMARY_KEYS = ['scormid', 'userid', 'attempt', 'scoid', 'element'] as const;
export const OFFLINE_SITE_SCHEMA: CoreSiteSchema = {
name: 'AddonModScormOfflineProvider',
version: 1,
Expand Down Expand Up @@ -58,7 +60,7 @@ export const OFFLINE_SITE_SCHEMA: CoreSiteSchema = {
type: 'TEXT',
},
],
primaryKeys: ['scormid', 'userid', 'attempt'],
primaryKeys: [...ATTEMPTS_TABLE_PRIMARY_KEYS],
},
{
name: TRACKS_TABLE_NAME,
Expand Down Expand Up @@ -101,7 +103,7 @@ export const OFFLINE_SITE_SCHEMA: CoreSiteSchema = {
type: 'INTEGER',
},
],
primaryKeys: ['scormid', 'userid', 'attempt', 'scoid', 'element'],
primaryKeys: [...TRACKS_TABLE_PRIMARY_KEYS],
},
],
};
Expand All @@ -125,6 +127,8 @@ export type AddonModScormAttemptDBRecord = AddonModScormOfflineDBCommonData & {
snapshot?: string | null;
};

export type AddonModScormAttemptDBPrimaryKeys = typeof ATTEMPTS_TABLE_PRIMARY_KEYS[number];

/**
* SCORM track data.
*/
Expand All @@ -135,3 +139,5 @@ export type AddonModScormTrackDBRecord = AddonModScormOfflineDBCommonData & {
timemodified: number;
synced: number;
};

export type AddonModScormTrackDBPrimaryKeys = typeof TRACKS_TABLE_PRIMARY_KEYS[number];
Loading

0 comments on commit f31eb83

Please sign in to comment.