content-entry-transform 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2022 by arlac77
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ [![npm](https://img.shields.io/npm/v/content-entry-transform.svg)](https://www.npmjs.com/package/content-entry-transform)
2
+ [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
3
+ [![minified size](https://badgen.net/bundlephobia/min/content-entry-transform)](https://bundlephobia.com/result?p=content-entry-transform)
4
+ [![downloads](http://img.shields.io/npm/dm/content-entry-transform.svg?style=flat-square)](https://npmjs.org/package/content-entry-transform)
5
+ [![GitHub Issues](https://img.shields.io/github/issues/arlac77/content-entry-transform.svg?style=flat-square)](https://github.com/arlac77/content-entry-transform/issues)
6
+ [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Farlac77%2Fcontent-entry-transform%2Fbadge\&style=flat)](https://actions-badge.atrox.dev/arlac77/content-entry-transform/goto)
7
+ [![Styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
9
+ [![Known Vulnerabilities](https://snyk.io/test/github/arlac77/content-entry-transform/badge.svg)](https://snyk.io/test/github/arlac77/content-entry-transform)
10
+ [![Coverage Status](https://coveralls.io/repos/arlac77/content-entry-transform/badge.svg)](https://coveralls.io/github/arlac77/content-entry-transform)
11
+ # content-entry-transform
12
+ transform content entries
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "content-entry-transform",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": "./src/transform.mjs"
10
+ },
11
+ "description": "transform content entries",
12
+ "contributors": [
13
+ {
14
+ "name": "Markus Felten",
15
+ "email": "markus.felten@gmx.de"
16
+ }
17
+ ],
18
+ "license": "BSD-2-Clause",
19
+ "scripts": {
20
+ "test": "npm run test:ava",
21
+ "test:ava": "ava --timeout 2m tests/*.mjs",
22
+ "cover": "c8 -x 'tests/**/*' --temp-directory build/tmp ava --timeout 2m tests/*.mjs && c8 report -r lcov -o build/coverage --temp-directory build/tmp",
23
+ "docs": "documentation readme --section=API ./src/**/*.mjs",
24
+ "lint": "npm run lint:docs",
25
+ "lint:docs": "documentation lint ./src/**/*.mjs"
26
+ },
27
+ "dependencies": {
28
+ "content-entry": "^4.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "ava": "^4.0.1",
32
+ "c8": "^7.11.0",
33
+ "documentation": "^13.2.5",
34
+ "semantic-release": "^18.0.1"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/arlac77/content-entry-transform.git"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/arlac77/content-entry-transform/issues"
42
+ },
43
+ "homepage": "https://github.com/arlac77/content-entry-transform#readme",
44
+ "template": {
45
+ "inheritFrom": [
46
+ "arlac77/template-arlac77-github",
47
+ "arlac77/template-esm-only"
48
+ ]
49
+ }
50
+ }
@@ -0,0 +1,30 @@
1
+ export function createPropertiesInterceptor(properties) {
2
+ return async function* transformer(expression, remainder, source, cb) {
3
+ const value = properties[expression];
4
+ yield value === undefined ? "" : value;
5
+ };
6
+ }
7
+
8
+ export function createExpressionTransformer(
9
+ properties,
10
+ match = entry =>
11
+ entry.name.match(/\.(conf|json|html|txt|service|socket)$/) ? true : false
12
+ ) {
13
+ return {
14
+ name: "expression",
15
+ match,
16
+ transform: async entry => {
17
+ //console.log("TRANSFORM",entry.name);
18
+ const ne = new ReadableStreamContentEntry(
19
+ entry.name,
20
+ iterableStringInterceptor(
21
+ await entry.getReadStream(utf8StreamOptions),
22
+ createPropertiesInterceptor(properties)
23
+ )
24
+ );
25
+ ne.destination = entry.destination; // TODO all the other attributes ?
26
+ return ne;
27
+ //return Object.assign(entry,ne);
28
+ }
29
+ };
30
+ }
@@ -0,0 +1,7 @@
1
+ export function createPropertiesTransformer(propertyDefinitions, match) {
2
+ return {
3
+ name: "property",
4
+ match,
5
+ transform: async entry => Object.create(entry, propertyDefinitions)
6
+ };
7
+ }
@@ -0,0 +1,33 @@
1
+ export * from "./properties-transformer.mjs";
2
+
3
+ /**
4
+ * Apply transformers.
5
+ * @param {AsyncIterator<ContentEntry>} source
6
+ * @param {Transformer[]} transformers
7
+ * @param {Boolean]} onlyMatching filter out all none matching entries
8
+ */
9
+ export async function* transform(source, transformers = [], onlyMatching) {
10
+ const usedTransformers = new Set();
11
+
12
+ for await (let entry of source) {
13
+ let didMatch = false;
14
+ for (const t of transformers) {
15
+ //console.log(t.name,entry.name,t.match(entry));
16
+ if (t.match(entry)) {
17
+ didMatch = true;
18
+ entry = await t.transform(entry);
19
+ usedTransformers.add(t);
20
+ }
21
+ }
22
+
23
+ if ((onlyMatching && didMatch) || !onlyMatching) {
24
+ yield entry;
25
+ }
26
+ }
27
+
28
+ for (const t of transformers) {
29
+ if (!usedTransformers.has(t) && t.createEntryWhenMissing !== undefined) {
30
+ yield t.transform(await t.createEntryWhenMissing());
31
+ }
32
+ }
33
+ }