@serwist/utils 10.0.0-preview.3 → 10.0.0-preview.4

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.
@@ -1,4 +1,7 @@
1
- export { createLogger } from "./node/log.js";
1
+ export { errors } from "./node/errors.js";
2
+ export { getFileHash, getFileSize, getCompositeDetails } from "./node/fs.js";
3
+ export { getStringHash, getStringDetails } from "./node/hash.js";
2
4
  export type { LogType, LogLevel, LoggerOptions, Logger } from "./node/log.js";
5
+ export { createLogger } from "./node/log.js";
3
6
  export { resolveEntry } from "./node/paths.js";
4
7
  //# sourceMappingURL=index.node.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.node.d.ts","sourceRoot":"","sources":["../src/index.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.node.d.ts","sourceRoot":"","sources":["../src/index.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACjE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
@@ -1,8 +1,140 @@
1
+ import { oneLine } from 'common-tags';
2
+ import crypto from 'node:crypto';
1
3
  import fs, { readFileSync } from 'node:fs';
2
4
  import { createRequire } from 'node:module';
3
5
  import { red, yellow, cyan } from 'kolorist';
4
6
  import path from 'node:path';
5
7
 
8
+ const errors = {
9
+ "unable-to-get-rootdir": "Unable to get the root directory of your web app.",
10
+ "no-extension": oneLine`Unable to detect a usable extension for a file in your web
11
+ app directory.`,
12
+ "invalid-file-manifest-name": oneLine`The File Manifest Name must have at least one
13
+ character.`,
14
+ "unable-to-get-file-manifest-name": "Unable to get a file manifest name.",
15
+ "invalid-sw-dest": `The 'swDest' value must be a valid path.`,
16
+ "unable-to-get-sw-name": "Unable to get a service worker file name.",
17
+ "unable-to-get-save-config": oneLine`An error occurred when asking to save details
18
+ in a config file.`,
19
+ "unable-to-get-file-hash": oneLine`An error occurred when attempting to create a
20
+ file hash.`,
21
+ "unable-to-get-file-size": oneLine`An error occurred when attempting to get a file
22
+ size.`,
23
+ "unable-to-glob-files": "An error occurred when globbing for files.",
24
+ "unable-to-make-manifest-directory": oneLine`Unable to make output directory for
25
+ file manifest.`,
26
+ "read-manifest-template-failure": "Unable to read template for file manifest",
27
+ "populating-manifest-tmpl-failed": oneLine`An error occurred when populating the
28
+ file manifest template.`,
29
+ "manifest-file-write-failure": "Unable to write the file manifest.",
30
+ "unable-to-make-sw-directory": oneLine`Unable to make the directories to output
31
+ the service worker path.`,
32
+ "sw-write-failure": "Unable to write the service worker file.",
33
+ "sw-write-failure-directory": oneLine`Unable to write the service worker file;
34
+ 'swDest' should be a full path to the file, not a path to a directory.`,
35
+ "unable-to-copy-serwist-libraries": oneLine`One or more of the Serwist libraries
36
+ could not be copied over to the destination directory: `,
37
+ "invalid-glob-directory": oneLine`The supplied globDirectory must be a path as a
38
+ string.`,
39
+ "invalid-dont-cache-bust": oneLine`The supplied 'dontCacheBustURLsMatching'
40
+ parameter must be a RegExp.`,
41
+ "invalid-exclude-files": "The excluded files should be an array of strings.",
42
+ "invalid-get-manifest-entries-input": oneLine`The input to
43
+ 'getFileManifestEntries()' must be an object.`,
44
+ "invalid-manifest-path": oneLine`The supplied manifest path is not a string with
45
+ at least one character.`,
46
+ "invalid-manifest-entries": oneLine`The manifest entries must be an array of
47
+ strings or JavaScript objects containing a url parameter.`,
48
+ "invalid-manifest-format": oneLine`The value of the 'format' option passed to
49
+ generateFileManifest() must be either 'iife' (the default) or 'es'.`,
50
+ "invalid-static-file-globs": oneLine`The 'globPatterns' value must be an array
51
+ of strings.`,
52
+ "invalid-templated-urls": oneLine`The 'templatedURLs' value should be an object
53
+ that maps URLs to either a string, or to an array of glob patterns.`,
54
+ "templated-url-matches-glob": oneLine`One of the 'templatedURLs' URLs is already
55
+ being tracked via 'globPatterns': `,
56
+ "invalid-glob-ignores": oneLine`The 'globIgnores' parameter must be an array of
57
+ glob pattern strings.`,
58
+ "manifest-entry-bad-url": oneLine`The generated manifest contains an entry without
59
+ a URL string. This is likely an error with @serwist/build.`,
60
+ "modify-url-prefix-bad-prefixes": oneLine`The 'modifyURLPrefix' parameter must be
61
+ an object with string key value pairs.`,
62
+ "invalid-inject-manifest-arg": oneLine`The input to 'injectManifest()' must be an
63
+ object.`,
64
+ "injection-point-not-found": oneLine`Unable to find a place to inject the manifest.
65
+ Please ensure that your service worker file contains the following: `,
66
+ "multiple-injection-points": oneLine`Please ensure that your 'swSrc' file contains
67
+ only one match for the following: `,
68
+ "useless-glob-pattern": oneLine`One of the glob patterns doesn't match any files.
69
+ Please remove or fix the following: `,
70
+ "bad-template-urls-asset": oneLine`There was an issue using one of the provided
71
+ 'templatedURLs'.`,
72
+ "invalid-generate-file-manifest-arg": oneLine`The input to generateFileManifest()
73
+ must be an Object.`,
74
+ "invalid-sw-src": `The 'swSrc' file can't be read.`,
75
+ "same-src-and-dest": oneLine`Unable to find a place to inject the manifest. This is
76
+ likely because swSrc and swDest are configured to the same file.
77
+ Please ensure that your swSrc file contains the following:`,
78
+ "no-module-name": oneLine`You must provide a moduleName parameter when calling
79
+ getModuleURL().`,
80
+ "bad-manifest-transforms-return-value": oneLine`The return value from a
81
+ manifestTransform should be an object with 'manifest' and optionally
82
+ 'warnings' properties.`,
83
+ "string-entry-warning": oneLine`Some items were passed to additionalPrecacheEntries
84
+ without revisioning info. This is generally NOT safe. Learn more at
85
+ https://bit.ly/wb-precache.`,
86
+ "cant-find-sourcemap": oneLine`The swSrc file refers to a sourcemap that can't be
87
+ opened:`,
88
+ "manifest-transforms": oneLine`When using manifestTransforms, you must provide
89
+ an array of functions.`
90
+ };
91
+
92
+ const getStringHash = (input)=>{
93
+ const md5 = crypto.createHash("md5");
94
+ md5.update(input);
95
+ return md5.digest("hex");
96
+ };
97
+ const getStringDetails = (url, str)=>({
98
+ file: url,
99
+ hash: getStringHash(str),
100
+ size: str.length
101
+ });
102
+
103
+ const getFileHash = (file)=>{
104
+ try {
105
+ return getStringHash(fs.readFileSync(file));
106
+ } catch (err) {
107
+ throw new Error(`${errors["unable-to-get-file-hash"]} '${err instanceof Error && err.message ? err.message : ""}'`);
108
+ }
109
+ };
110
+ const getFileSize = (file)=>{
111
+ try {
112
+ const stat = fs.statSync(file);
113
+ if (!stat.isFile()) {
114
+ return null;
115
+ }
116
+ return stat.size;
117
+ } catch (err) {
118
+ throw new Error(`${errors["unable-to-get-file-size"]} '${err instanceof Error && err.message ? err.message : ""}'`);
119
+ }
120
+ };
121
+ const getCompositeDetails = (compositeURL, dependencyDetails)=>{
122
+ let totalSize = 0;
123
+ let compositeHash = "";
124
+ for (const fileDetails of dependencyDetails){
125
+ totalSize += fileDetails.size;
126
+ compositeHash += fileDetails.hash === null ? "" : fileDetails.hash;
127
+ }
128
+ const md5 = crypto.createHash("md5");
129
+ md5.update(compositeHash);
130
+ const hashOfHashes = md5.digest("hex");
131
+ return {
132
+ file: compositeURL,
133
+ hash: hashOfHashes,
134
+ size: totalSize
135
+ };
136
+ };
137
+
6
138
  const LogLevels = {
7
139
  silent: 0,
8
140
  error: 1,
@@ -67,4 +199,4 @@ const resolveEntry = (entry)=>{
67
199
  return null;
68
200
  };
69
201
 
70
- export { createLogger, resolveEntry };
202
+ export { createLogger, errors, getCompositeDetails, getFileHash, getFileSize, getStringDetails, getStringHash, resolveEntry };
@@ -0,0 +1,47 @@
1
+ export declare const errors: {
2
+ "unable-to-get-rootdir": string;
3
+ "no-extension": string;
4
+ "invalid-file-manifest-name": string;
5
+ "unable-to-get-file-manifest-name": string;
6
+ "invalid-sw-dest": string;
7
+ "unable-to-get-sw-name": string;
8
+ "unable-to-get-save-config": string;
9
+ "unable-to-get-file-hash": string;
10
+ "unable-to-get-file-size": string;
11
+ "unable-to-glob-files": string;
12
+ "unable-to-make-manifest-directory": string;
13
+ "read-manifest-template-failure": string;
14
+ "populating-manifest-tmpl-failed": string;
15
+ "manifest-file-write-failure": string;
16
+ "unable-to-make-sw-directory": string;
17
+ "sw-write-failure": string;
18
+ "sw-write-failure-directory": string;
19
+ "unable-to-copy-serwist-libraries": string;
20
+ "invalid-glob-directory": string;
21
+ "invalid-dont-cache-bust": string;
22
+ "invalid-exclude-files": string;
23
+ "invalid-get-manifest-entries-input": string;
24
+ "invalid-manifest-path": string;
25
+ "invalid-manifest-entries": string;
26
+ "invalid-manifest-format": string;
27
+ "invalid-static-file-globs": string;
28
+ "invalid-templated-urls": string;
29
+ "templated-url-matches-glob": string;
30
+ "invalid-glob-ignores": string;
31
+ "manifest-entry-bad-url": string;
32
+ "modify-url-prefix-bad-prefixes": string;
33
+ "invalid-inject-manifest-arg": string;
34
+ "injection-point-not-found": string;
35
+ "multiple-injection-points": string;
36
+ "useless-glob-pattern": string;
37
+ "bad-template-urls-asset": string;
38
+ "invalid-generate-file-manifest-arg": string;
39
+ "invalid-sw-src": string;
40
+ "same-src-and-dest": string;
41
+ "no-module-name": string;
42
+ "bad-manifest-transforms-return-value": string;
43
+ "string-entry-warning": string;
44
+ "cant-find-sourcemap": string;
45
+ "manifest-transforms": string;
46
+ };
47
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/node/errors.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkFlB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import fs from "node:fs";
2
+ import type { FileDetails } from "../types.js";
3
+ export declare const getFileHash: (file: fs.PathOrFileDescriptor) => string;
4
+ export declare const getFileSize: (file: string) => number | null;
5
+ export declare const getCompositeDetails: (compositeURL: string, dependencyDetails: FileDetails[]) => FileDetails;
6
+ //# sourceMappingURL=fs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs.d.ts","sourceRoot":"","sources":["../../src/node/fs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,WAAW,GAAI,MAAM,EAAE,CAAC,oBAAoB,WAMxD,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,MAAM,GAAG,IAUnD,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,cAAc,MAAM,EAAE,mBAAmB,WAAW,EAAE,KAAG,WAkB5F,CAAC"}
@@ -0,0 +1,5 @@
1
+ import crypto from "node:crypto";
2
+ import type { FileDetails } from "../types.js";
3
+ export declare const getStringHash: (input: crypto.BinaryLike) => string;
4
+ export declare const getStringDetails: (url: string, str: string) => FileDetails;
5
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/node/hash.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,aAAa,GAAI,OAAO,MAAM,CAAC,UAAU,KAAG,MAIxD,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,KAAG,WAI1D,CAAC"}
package/dist/types.d.ts CHANGED
@@ -51,4 +51,9 @@ export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
51
51
  export type Prettify<T> = {
52
52
  [K in keyof T]: T[K];
53
53
  } & {};
54
+ export interface FileDetails {
55
+ file: string;
56
+ hash: string | null;
57
+ size: number;
58
+ }
54
59
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAErE;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE9E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,GAAG,EAAE,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAErE;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE9E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,GAAG,EAAE,CAAC;AAEP,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/utils",
3
- "version": "10.0.0-preview.3",
3
+ "version": "10.0.0-preview.4",
4
4
  "type": "module",
5
5
  "description": "This module contains internal utilities used by Serwist packages.",
6
6
  "files": [
@@ -39,12 +39,14 @@
39
39
  "./package.json": "./package.json"
40
40
  },
41
41
  "dependencies": {
42
+ "common-tags": "1.8.2",
42
43
  "kolorist": "1.8.0"
43
44
  },
44
45
  "devDependencies": {
46
+ "@types/common-tags": "1.8.4",
45
47
  "rollup": "4.40.0",
46
48
  "typescript": "5.8.3",
47
- "@serwist/configs": "10.0.0-preview.3"
49
+ "@serwist/configs": "10.0.0-preview.4"
48
50
  },
49
51
  "scripts": {
50
52
  "build": "rimraf dist && NODE_ENV=production rollup --config rollup.config.js",
package/src/index.node.ts CHANGED
@@ -1,3 +1,6 @@
1
- export { createLogger } from "./node/log.js";
1
+ export { errors } from "./node/errors.js";
2
+ export { getFileHash, getFileSize, getCompositeDetails } from "./node/fs.js";
3
+ export { getStringHash, getStringDetails } from "./node/hash.js";
2
4
  export type { LogType, LogLevel, LoggerOptions, Logger } from "./node/log.js";
5
+ export { createLogger } from "./node/log.js";
3
6
  export { resolveEntry } from "./node/paths.js";
@@ -0,0 +1,92 @@
1
+ /*
2
+ Copyright 2018 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+ import { oneLine as ol } from "common-tags";
9
+
10
+ export const errors = {
11
+ "unable-to-get-rootdir": "Unable to get the root directory of your web app.",
12
+ "no-extension": ol`Unable to detect a usable extension for a file in your web
13
+ app directory.`,
14
+ "invalid-file-manifest-name": ol`The File Manifest Name must have at least one
15
+ character.`,
16
+ "unable-to-get-file-manifest-name": "Unable to get a file manifest name.",
17
+ "invalid-sw-dest": `The 'swDest' value must be a valid path.`,
18
+ "unable-to-get-sw-name": "Unable to get a service worker file name.",
19
+ "unable-to-get-save-config": ol`An error occurred when asking to save details
20
+ in a config file.`,
21
+ "unable-to-get-file-hash": ol`An error occurred when attempting to create a
22
+ file hash.`,
23
+ "unable-to-get-file-size": ol`An error occurred when attempting to get a file
24
+ size.`,
25
+ "unable-to-glob-files": "An error occurred when globbing for files.",
26
+ "unable-to-make-manifest-directory": ol`Unable to make output directory for
27
+ file manifest.`,
28
+ "read-manifest-template-failure": "Unable to read template for file manifest",
29
+ "populating-manifest-tmpl-failed": ol`An error occurred when populating the
30
+ file manifest template.`,
31
+ "manifest-file-write-failure": "Unable to write the file manifest.",
32
+ "unable-to-make-sw-directory": ol`Unable to make the directories to output
33
+ the service worker path.`,
34
+ "sw-write-failure": "Unable to write the service worker file.",
35
+ "sw-write-failure-directory": ol`Unable to write the service worker file;
36
+ 'swDest' should be a full path to the file, not a path to a directory.`,
37
+ "unable-to-copy-serwist-libraries": ol`One or more of the Serwist libraries
38
+ could not be copied over to the destination directory: `,
39
+ "invalid-glob-directory": ol`The supplied globDirectory must be a path as a
40
+ string.`,
41
+ "invalid-dont-cache-bust": ol`The supplied 'dontCacheBustURLsMatching'
42
+ parameter must be a RegExp.`,
43
+ "invalid-exclude-files": "The excluded files should be an array of strings.",
44
+ "invalid-get-manifest-entries-input": ol`The input to
45
+ 'getFileManifestEntries()' must be an object.`,
46
+ "invalid-manifest-path": ol`The supplied manifest path is not a string with
47
+ at least one character.`,
48
+ "invalid-manifest-entries": ol`The manifest entries must be an array of
49
+ strings or JavaScript objects containing a url parameter.`,
50
+ "invalid-manifest-format": ol`The value of the 'format' option passed to
51
+ generateFileManifest() must be either 'iife' (the default) or 'es'.`,
52
+ "invalid-static-file-globs": ol`The 'globPatterns' value must be an array
53
+ of strings.`,
54
+ "invalid-templated-urls": ol`The 'templatedURLs' value should be an object
55
+ that maps URLs to either a string, or to an array of glob patterns.`,
56
+ "templated-url-matches-glob": ol`One of the 'templatedURLs' URLs is already
57
+ being tracked via 'globPatterns': `,
58
+ "invalid-glob-ignores": ol`The 'globIgnores' parameter must be an array of
59
+ glob pattern strings.`,
60
+ "manifest-entry-bad-url": ol`The generated manifest contains an entry without
61
+ a URL string. This is likely an error with @serwist/build.`,
62
+ "modify-url-prefix-bad-prefixes": ol`The 'modifyURLPrefix' parameter must be
63
+ an object with string key value pairs.`,
64
+ "invalid-inject-manifest-arg": ol`The input to 'injectManifest()' must be an
65
+ object.`,
66
+ "injection-point-not-found": ol`Unable to find a place to inject the manifest.
67
+ Please ensure that your service worker file contains the following: `,
68
+ "multiple-injection-points": ol`Please ensure that your 'swSrc' file contains
69
+ only one match for the following: `,
70
+ "useless-glob-pattern": ol`One of the glob patterns doesn't match any files.
71
+ Please remove or fix the following: `,
72
+ "bad-template-urls-asset": ol`There was an issue using one of the provided
73
+ 'templatedURLs'.`,
74
+ "invalid-generate-file-manifest-arg": ol`The input to generateFileManifest()
75
+ must be an Object.`,
76
+ "invalid-sw-src": `The 'swSrc' file can't be read.`,
77
+ "same-src-and-dest": ol`Unable to find a place to inject the manifest. This is
78
+ likely because swSrc and swDest are configured to the same file.
79
+ Please ensure that your swSrc file contains the following:`,
80
+ "no-module-name": ol`You must provide a moduleName parameter when calling
81
+ getModuleURL().`,
82
+ "bad-manifest-transforms-return-value": ol`The return value from a
83
+ manifestTransform should be an object with 'manifest' and optionally
84
+ 'warnings' properties.`,
85
+ "string-entry-warning": ol`Some items were passed to additionalPrecacheEntries
86
+ without revisioning info. This is generally NOT safe. Learn more at
87
+ https://bit.ly/wb-precache.`,
88
+ "cant-find-sourcemap": ol`The swSrc file refers to a sourcemap that can't be
89
+ opened:`,
90
+ "manifest-transforms": ol`When using manifestTransforms, you must provide
91
+ an array of functions.`,
92
+ };
package/src/node/fs.ts ADDED
@@ -0,0 +1,45 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import { getStringHash } from "./hash.js";
4
+ import { errors } from "./errors.js";
5
+ import type { FileDetails } from "../types.js";
6
+
7
+ export const getFileHash = (file: fs.PathOrFileDescriptor) => {
8
+ try {
9
+ return getStringHash(fs.readFileSync(file));
10
+ } catch (err) {
11
+ throw new Error(`${errors["unable-to-get-file-hash"]} '${err instanceof Error && err.message ? err.message : ""}'`);
12
+ }
13
+ };
14
+
15
+ export const getFileSize = (file: string): number | null => {
16
+ try {
17
+ const stat = fs.statSync(file);
18
+ if (!stat.isFile()) {
19
+ return null;
20
+ }
21
+ return stat.size;
22
+ } catch (err) {
23
+ throw new Error(`${errors["unable-to-get-file-size"]} '${err instanceof Error && err.message ? err.message : ""}'`);
24
+ }
25
+ };
26
+
27
+ export const getCompositeDetails = (compositeURL: string, dependencyDetails: FileDetails[]): FileDetails => {
28
+ let totalSize = 0;
29
+ let compositeHash = "";
30
+
31
+ for (const fileDetails of dependencyDetails) {
32
+ totalSize += fileDetails.size;
33
+ compositeHash += fileDetails.hash === null ? "" : fileDetails.hash;
34
+ }
35
+
36
+ const md5 = crypto.createHash("md5");
37
+ md5.update(compositeHash);
38
+ const hashOfHashes = md5.digest("hex");
39
+
40
+ return {
41
+ file: compositeURL,
42
+ hash: hashOfHashes,
43
+ size: totalSize,
44
+ };
45
+ };
@@ -0,0 +1,14 @@
1
+ import crypto from "node:crypto";
2
+ import type { FileDetails } from "../types.js";
3
+
4
+ export const getStringHash = (input: crypto.BinaryLike): string => {
5
+ const md5 = crypto.createHash("md5");
6
+ md5.update(input);
7
+ return md5.digest("hex");
8
+ };
9
+
10
+ export const getStringDetails = (url: string, str: string): FileDetails => ({
11
+ file: url,
12
+ hash: getStringHash(str),
13
+ size: str.length,
14
+ });
package/src/types.ts CHANGED
@@ -53,3 +53,9 @@ export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
53
53
  export type Prettify<T> = {
54
54
  [K in keyof T]: T[K];
55
55
  } & {};
56
+
57
+ export interface FileDetails {
58
+ file: string;
59
+ hash: string | null;
60
+ size: number;
61
+ }