@ember-data-mirror/request-utils 5.5.0-alpha.23 → 5.5.0-alpha.25
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/dist/deprecation-support.js +2 -1
- package/dist/deprecation-support.js.map +1 -1
- package/dist/handlers.js +147 -0
- package/dist/handlers.js.map +1 -0
- package/dist/index.js +240 -27
- package/dist/index.js.map +1 -1
- package/dist/{inflect-8aYUyMN7.js → inflect-CwI_k2it.js} +141 -147
- package/dist/inflect-CwI_k2it.js.map +1 -0
- package/dist/string.js +2 -1
- package/dist/string.js.map +1 -1
- package/dist/transform-1PDozfHr.js +192 -0
- package/dist/transform-1PDozfHr.js.map +1 -0
- package/package.json +7 -5
- package/unstable-preview-types/-private/handlers/auto-compress.d.ts +168 -0
- package/unstable-preview-types/-private/handlers/auto-compress.d.ts.map +1 -0
- package/unstable-preview-types/-private/string/inflect.d.ts +126 -0
- package/unstable-preview-types/-private/string/inflect.d.ts.map +1 -1
- package/unstable-preview-types/-private/string/transform.d.ts +93 -57
- package/unstable-preview-types/-private/string/transform.d.ts.map +1 -1
- package/unstable-preview-types/handlers.d.ts +11 -0
- package/unstable-preview-types/handlers.d.ts.map +1 -0
- package/unstable-preview-types/index.d.ts +183 -9
- package/unstable-preview-types/index.d.ts.map +1 -1
- package/unstable-preview-types/string.d.ts +12 -0
- package/unstable-preview-types/string.d.ts.map +1 -1
- package/dist/inflect-8aYUyMN7.js.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { deprecate } from '@ember/debug';
|
|
2
2
|
import { macroCondition, getGlobalConfig, dependencySatisfies, importSync } from '@embroider/macros';
|
|
3
|
-
import {
|
|
3
|
+
import { f as defaultRules, b as plural, a as singular, i as irregular, u as uncountable } from "./inflect-CwI_k2it.js";
|
|
4
|
+
import "./transform-1PDozfHr.js";
|
|
4
5
|
if (macroCondition(getGlobalConfig().WarpDriveMirror.deprecations.DEPRECATE_EMBER_INFLECTOR)) {
|
|
5
6
|
if (macroCondition(dependencySatisfies('ember-inflector', '*'))) {
|
|
6
7
|
const Inflector = importSync('ember-inflector').default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deprecation-support.js","sources":["../src/deprecation-support.ts"],"sourcesContent":["import { deprecate } from '@ember/debug';\n\nimport { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';\n\nimport { DEPRECATE_EMBER_INFLECTOR } from '@warp-drive-mirror/build-config/deprecations';\n\nimport { defaultRules as WarpDriveDefaults } from './-private/string/inflections';\nimport { irregular, plural, singular, uncountable } from './string';\n\nif (DEPRECATE_EMBER_INFLECTOR) {\n if (macroCondition(dependencySatisfies('ember-inflector', '*'))) {\n const Inflector = (importSync('ember-inflector') as { default: typeof import('ember-inflector').default }).default;\n const { inflector } = Inflector;\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalPlural = inflector.plural;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalSingular = inflector.singular;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalIrregular = inflector.irregular;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalUncountable = inflector.uncountable;\n\n // copy over any already registered rules\n type DefaultRules = {\n plurals: [RegExp, string][];\n singular: [RegExp, string][];\n irregularPairs: [string, string][];\n uncountable: string[];\n };\n type InternalRules = {\n plurals: [RegExp, string][];\n singular: [RegExp, string][];\n\n // [str1, str2] =>\n // { [str1.lower]: str2 }\n // { [str2.lower]: str2 }\n irregular: Record<string, string>;\n\n // [str1, str2] =>\n // { [str2.lower]: str1 }\n // { [str1.lower]: str1 }\n irregularInverse: Record<string, string>;\n\n // lower cased string\n uncountable: Record<string, boolean>;\n };\n\n // ember-inflector mutates the default rules arrays\n // with user supplied rules, so we keep track of what\n // is default via our own list.\n const defaultPluralKeys = new Set<string>();\n const defaultSingularKeys = new Set<string>();\n WarpDriveDefaults.plurals.forEach(([regex]) => {\n defaultPluralKeys.add(regex.toString());\n });\n WarpDriveDefaults.singular.forEach(([regex]) => {\n defaultSingularKeys.add(regex.toString());\n });\n\n const { defaultRules } = Inflector as unknown as { defaultRules: DefaultRules };\n const { rules } = inflector as unknown as { rules: InternalRules };\n\n const irregularMap = new Map<string, string>();\n const toIgnore = new Set<string>();\n const uncountableSet = new Set(defaultRules.uncountable);\n\n defaultRules.irregularPairs.forEach(([single, plur]) => {\n irregularMap.set(single.toLowerCase(), plur);\n toIgnore.add(plur.toLowerCase());\n });\n const irregularLookups = new Map<string, string>();\n Object.keys(rules.irregular).forEach((single) => {\n const plur = rules.irregular[single];\n irregularLookups.set(single, plur);\n });\n\n // load plurals\n rules.plurals.forEach(([regex, replacement]) => {\n if (defaultPluralKeys.has(regex.toString())) {\n return;\n }\n\n plural(regex, replacement);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for pluralization.\\nPlease \\`import { plural } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom pluralization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load singulars\n rules.singular.forEach(([regex, replacement]) => {\n if (defaultSingularKeys.has(regex.toString())) {\n return;\n }\n\n singular(regex, replacement);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for singularization.\\nPlease \\`import { singular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom singularization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load irregulars\n Object.keys(rules.irregular).forEach((single) => {\n const plur = rules.irregular[single];\n const defaultPlur = irregularMap.get(single);\n if (defaultPlur && defaultPlur === plur) {\n return;\n }\n\n if (toIgnore.has(single)) {\n return;\n }\n\n const actualSingle = irregularLookups.get(plur.toLowerCase()) || single;\n toIgnore.add(plur.toLowerCase());\n irregular(actualSingle, plur);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for irregular rules.\\nPlease \\`import { irregular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom irregular rule for use with EmberData for '${actualSingle}' <=> '${plur}'.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load uncountables\n Object.keys(rules.uncountable).forEach((word) => {\n if (uncountableSet.has(word) || rules.uncountable[word] !== true) {\n return;\n }\n\n uncountable(word);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for uncountable rules.\\nPlease \\`import { uncountable } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom uncountable rule for '${word}' for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n inflector.plural = function (...args: Parameters<typeof originalPlural>) {\n plural(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for pluralization.\\nPlease \\`import { plural } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom pluralization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalPlural.apply(inflector, args);\n };\n\n inflector.singular = function (...args: Parameters<typeof originalSingular>) {\n singular(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for singularization.\\nPlease \\`import { singular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom singularization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalSingular.apply(inflector, args);\n };\n\n inflector.irregular = function (...args: Parameters<typeof originalIrregular>) {\n irregular(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for irregular rules.\\nPlease \\`import { irregular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom irregular rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalIrregular.apply(inflector, args);\n };\n\n inflector.uncountable = function (...args: Parameters<typeof originalUncountable>) {\n uncountable(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for uncountable rules.\\nPlease \\`import { uncountable } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom uncountable rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalUncountable.apply(inflector, args);\n };\n }\n}\n"],"names":["macroCondition","getGlobalConfig","WarpDrive","deprecations","DEPRECATE_EMBER_INFLECTOR","dependencySatisfies","Inflector","importSync","default","inflector","originalPlural","plural","originalSingular","singular","originalIrregular","irregular","originalUncountable","uncountable","defaultPluralKeys","Set","defaultSingularKeys","WarpDriveDefaults","plurals","forEach","regex","add","toString","defaultRules","rules","irregularMap","Map","toIgnore","uncountableSet","irregularPairs","single","plur","set","toLowerCase","irregularLookups","Object","keys","replacement","has","deprecate","id","until","for","since","enabled","available","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,KAAK,EACL;AACEC,QAAAA,EAAE,EAAE,4BAA4B;AAChCC,QAAAA,KAAK,EAAE,OAAO;AACdC,QAAAA,GAAG,EAAE,YAAY;AACjBC,QAAAA,KAAK,EAAE;AACLC,UAAAA,OAAO,EAAE,OAAO;AAChBC,UAAAA,SAAS,EAAE;SACZ;AACDC,QAAAA,GAAG,EAAE;AACP,OACF,CAAC;AAED,MAAA,OAAOlC,mBAAmB,CAACwC,KAAK,CAAC/C,SAAS,EAAE8C,IAAI,CAAC;KAClD;AACH;AACF"}
|
|
1
|
+
{"version":3,"file":"deprecation-support.js","sources":["../src/deprecation-support.ts"],"sourcesContent":["import { deprecate } from '@ember/debug';\n\nimport { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';\n\nimport { DEPRECATE_EMBER_INFLECTOR } from '@warp-drive-mirror/build-config/deprecations';\n\nimport { defaultRules as WarpDriveDefaults } from './-private/string/inflections';\nimport { irregular, plural, singular, uncountable } from './string';\n\nif (DEPRECATE_EMBER_INFLECTOR) {\n if (macroCondition(dependencySatisfies('ember-inflector', '*'))) {\n const Inflector = (importSync('ember-inflector') as { default: typeof import('ember-inflector').default }).default;\n const { inflector } = Inflector;\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalPlural = inflector.plural;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalSingular = inflector.singular;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalIrregular = inflector.irregular;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const originalUncountable = inflector.uncountable;\n\n // copy over any already registered rules\n type DefaultRules = {\n plurals: [RegExp, string][];\n singular: [RegExp, string][];\n irregularPairs: [string, string][];\n uncountable: string[];\n };\n type InternalRules = {\n plurals: [RegExp, string][];\n singular: [RegExp, string][];\n\n // [str1, str2] =>\n // { [str1.lower]: str2 }\n // { [str2.lower]: str2 }\n irregular: Record<string, string>;\n\n // [str1, str2] =>\n // { [str2.lower]: str1 }\n // { [str1.lower]: str1 }\n irregularInverse: Record<string, string>;\n\n // lower cased string\n uncountable: Record<string, boolean>;\n };\n\n // ember-inflector mutates the default rules arrays\n // with user supplied rules, so we keep track of what\n // is default via our own list.\n const defaultPluralKeys = new Set<string>();\n const defaultSingularKeys = new Set<string>();\n WarpDriveDefaults.plurals.forEach(([regex]) => {\n defaultPluralKeys.add(regex.toString());\n });\n WarpDriveDefaults.singular.forEach(([regex]) => {\n defaultSingularKeys.add(regex.toString());\n });\n\n const { defaultRules } = Inflector as unknown as { defaultRules: DefaultRules };\n const { rules } = inflector as unknown as { rules: InternalRules };\n\n const irregularMap = new Map<string, string>();\n const toIgnore = new Set<string>();\n const uncountableSet = new Set(defaultRules.uncountable);\n\n defaultRules.irregularPairs.forEach(([single, plur]) => {\n irregularMap.set(single.toLowerCase(), plur);\n toIgnore.add(plur.toLowerCase());\n });\n const irregularLookups = new Map<string, string>();\n Object.keys(rules.irregular).forEach((single) => {\n const plur = rules.irregular[single];\n irregularLookups.set(single, plur);\n });\n\n // load plurals\n rules.plurals.forEach(([regex, replacement]) => {\n if (defaultPluralKeys.has(regex.toString())) {\n return;\n }\n\n plural(regex, replacement);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for pluralization.\\nPlease \\`import { plural } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom pluralization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load singulars\n rules.singular.forEach(([regex, replacement]) => {\n if (defaultSingularKeys.has(regex.toString())) {\n return;\n }\n\n singular(regex, replacement);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for singularization.\\nPlease \\`import { singular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom singularization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load irregulars\n Object.keys(rules.irregular).forEach((single) => {\n const plur = rules.irregular[single];\n const defaultPlur = irregularMap.get(single);\n if (defaultPlur && defaultPlur === plur) {\n return;\n }\n\n if (toIgnore.has(single)) {\n return;\n }\n\n const actualSingle = irregularLookups.get(plur.toLowerCase()) || single;\n toIgnore.add(plur.toLowerCase());\n irregular(actualSingle, plur);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for irregular rules.\\nPlease \\`import { irregular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom irregular rule for use with EmberData for '${actualSingle}' <=> '${plur}'.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n // load uncountables\n Object.keys(rules.uncountable).forEach((word) => {\n if (uncountableSet.has(word) || rules.uncountable[word] !== true) {\n return;\n }\n\n uncountable(word);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for uncountable rules.\\nPlease \\`import { uncountable } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom uncountable rule for '${word}' for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n });\n\n inflector.plural = function (...args: Parameters<typeof originalPlural>) {\n plural(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for pluralization.\\nPlease \\`import { plural } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom pluralization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalPlural.apply(inflector, args);\n };\n\n inflector.singular = function (...args: Parameters<typeof originalSingular>) {\n singular(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for singularization.\\nPlease \\`import { singular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom singularization rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalSingular.apply(inflector, args);\n };\n\n inflector.irregular = function (...args: Parameters<typeof originalIrregular>) {\n irregular(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for irregular rules.\\nPlease \\`import { irregular } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom irregular rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalIrregular.apply(inflector, args);\n };\n\n inflector.uncountable = function (...args: Parameters<typeof originalUncountable>) {\n uncountable(...args);\n\n deprecate(\n `WarpDrive/EmberData no longer uses ember-inflector for uncountable rules.\\nPlease \\`import { uncountable } from '@ember-data-mirror/request-utils/string';\\` instead to register a custom uncountable rule for use with EmberData.`,\n false,\n {\n id: 'warp-drive.ember-inflector',\n until: '6.0.0',\n for: 'warp-drive',\n since: {\n enabled: '5.3.4',\n available: '4.13',\n },\n url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',\n }\n );\n\n return originalUncountable.apply(inflector, args);\n };\n }\n}\n"],"names":["macroCondition","getGlobalConfig","WarpDrive","deprecations","DEPRECATE_EMBER_INFLECTOR","dependencySatisfies","Inflector","importSync","default","inflector","originalPlural","plural","originalSingular","singular","originalIrregular","irregular","originalUncountable","uncountable","defaultPluralKeys","Set","defaultSingularKeys","WarpDriveDefaults","plurals","forEach","regex","add","toString","defaultRules","rules","irregularMap","Map","toIgnore","uncountableSet","irregularPairs","single","plur","set","toLowerCase","irregularLookups","Object","keys","replacement","has","deprecate","id","until","for","since","enabled","available","url","defaultPlur","get","actualSingle","word","args","apply"],"mappings":";;;;;AASA,IAAAA,cAAA,CAAAC,eAAA,EAAA,CAAAC,SAAA,CAAAC,YAAA,CAAAC,yBAAA,CAA+B,EAAA;EAC7B,IAAIJ,cAAc,CAACK,mBAAmB,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAC,EAAE;AAC/D,IAAA,MAAMC,SAAS,GAAIC,UAAU,CAAC,iBAAiB,CAAC,CAA2DC,OAAO;IAClH,MAAM;AAAEC,MAAAA;AAAU,KAAC,GAAGH,SAAS;;AAE/B;AACA,IAAA,MAAMI,cAAc,GAAGD,SAAS,CAACE,MAAM;AACvC;AACA,IAAA,MAAMC,gBAAgB,GAAGH,SAAS,CAACI,QAAQ;AAC3C;AACA,IAAA,MAAMC,iBAAiB,GAAGL,SAAS,CAACM,SAAS;AAC7C;AACA,IAAA,MAAMC,mBAAmB,GAAGP,SAAS,CAACQ,WAAW;;AAEjD;;AAyBA;AACA;AACA;AACA,IAAA,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,EAAU;AAC3C,IAAA,MAAMC,mBAAmB,GAAG,IAAID,GAAG,EAAU;IAC7CE,YAAiB,CAACC,OAAO,CAACC,OAAO,CAAC,CAAC,CAACC,KAAK,CAAC,KAAK;MAC7CN,iBAAiB,CAACO,GAAG,CAACD,KAAK,CAACE,QAAQ,EAAE,CAAC;AACzC,KAAC,CAAC;IACFL,YAAiB,CAACR,QAAQ,CAACU,OAAO,CAAC,CAAC,CAACC,KAAK,CAAC,KAAK;MAC9CJ,mBAAmB,CAACK,GAAG,CAACD,KAAK,CAACE,QAAQ,EAAE,CAAC;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AAiB,CAAC0C,KAAK,CAAC/C,SAAS,EAAE8C,IAAI,CAAC;KAChD;AAED9C,IAAAA,SAAS,CAACQ,WAAW,GAAG,UAAU,GAAGsC,IAA4C,EAAE;MACjFtC,WAAW,CAAC,GAAGsC,IAAI,CAAC;AAEpBZ,MAAAA,SAAS,CACP,CAAA,2NAAA,CAA6N,EAC7N,KAAK,EACL;AACEC,QAAAA,EAAE,EAAE,4BAA4B;AAChCC,QAAAA,KAAK,EAAE,OAAO;AACdC,QAAAA,GAAG,EAAE,YAAY;AACjBC,QAAAA,KAAK,EAAE;AACLC,UAAAA,OAAO,EAAE,OAAO;AAChBC,UAAAA,SAAS,EAAE;SACZ;AACDC,QAAAA,GAAG,EAAE;AACP,OACF,CAAC;AAED,MAAA,OAAOlC,mBAAmB,CAACwC,KAAK,CAAC/C,SAAS,EAAE8C,IAAI,CAAC;KAClD;AACH;AACF"}
|
package/dist/handlers.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @ember-data-mirror/request-utils/handlers
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
function isCompressibleMethod(method) {
|
|
6
|
+
return method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE';
|
|
7
|
+
}
|
|
8
|
+
const SupportsRequestStreams = (() => {
|
|
9
|
+
let duplexAccessed = false;
|
|
10
|
+
const hasContentType = new Request('', {
|
|
11
|
+
body: new ReadableStream(),
|
|
12
|
+
method: 'POST',
|
|
13
|
+
// @ts-expect-error untyped
|
|
14
|
+
get duplex() {
|
|
15
|
+
duplexAccessed = true;
|
|
16
|
+
return 'half';
|
|
17
|
+
}
|
|
18
|
+
}).headers.has('Content-Type');
|
|
19
|
+
return duplexAccessed && !hasContentType;
|
|
20
|
+
})();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for configuring the AutoCompress handler.
|
|
24
|
+
*
|
|
25
|
+
* @typedoc
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const DEFAULT_CONSTRAINTS = {
|
|
29
|
+
Blob: 1000,
|
|
30
|
+
ArrayBuffer: 1000,
|
|
31
|
+
TypedArray: 1000,
|
|
32
|
+
DataView: 1000,
|
|
33
|
+
String: 1000
|
|
34
|
+
};
|
|
35
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array.prototype);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A request handler that automatically compresses the request body
|
|
39
|
+
* if the request body is a string, array buffer, blob, or form data.
|
|
40
|
+
*
|
|
41
|
+
* This uses the [CompressionStream API](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)
|
|
42
|
+
*
|
|
43
|
+
* The compression format as well as the kinds of data to compress can be
|
|
44
|
+
* configured using the `format` and `constraints` options.
|
|
45
|
+
*
|
|
46
|
+
* ```diff
|
|
47
|
+
* +import { AutoCompress } from '@ember-data-mirror/request-utils/handlers';
|
|
48
|
+
* import Fetch from '@ember-data-mirror/request/fetch';
|
|
49
|
+
* import RequestManager from '@ember-data-mirror/request';
|
|
50
|
+
* import Store from '@ember-data-mirror/store';
|
|
51
|
+
*
|
|
52
|
+
* class AppStore extends Store {
|
|
53
|
+
* requestManager = new RequestManager()
|
|
54
|
+
* .use([
|
|
55
|
+
* + new AutoCompress(),
|
|
56
|
+
* Fetch
|
|
57
|
+
* ]);
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @class AutoCompress
|
|
62
|
+
* @extends Handler
|
|
63
|
+
* @public
|
|
64
|
+
* @since 5.5.0
|
|
65
|
+
*/
|
|
66
|
+
class AutoCompress {
|
|
67
|
+
constructor(options = {}) {
|
|
68
|
+
const opts = {
|
|
69
|
+
format: options.format ?? 'gzip',
|
|
70
|
+
constraints: Object.assign({}, DEFAULT_CONSTRAINTS, options.constraints),
|
|
71
|
+
allowStreaming: options.allowStreaming ?? false,
|
|
72
|
+
forceStreaming: options.forceStreaming ?? false
|
|
73
|
+
};
|
|
74
|
+
this.options = opts;
|
|
75
|
+
}
|
|
76
|
+
request({
|
|
77
|
+
request
|
|
78
|
+
}, next) {
|
|
79
|
+
const {
|
|
80
|
+
constraints
|
|
81
|
+
} = this.options;
|
|
82
|
+
const {
|
|
83
|
+
body
|
|
84
|
+
} = request;
|
|
85
|
+
const shouldCompress = isCompressibleMethod(request.method) && request.options?.compress !== false && (
|
|
86
|
+
// prettier-ignore
|
|
87
|
+
request.options?.compress ? true : typeof body === 'string' || body instanceof String ? canCompress('String', constraints, body.length) : body instanceof Blob ? canCompress('Blob', constraints, body.size) : body instanceof ArrayBuffer ? canCompress('ArrayBuffer', constraints, body.byteLength) : body instanceof DataView ? canCompress('DataView', constraints, body.byteLength) : body instanceof TypedArray ? canCompress('TypedArray', constraints, body.byteLength) : false);
|
|
88
|
+
if (!shouldCompress) return next(request);
|
|
89
|
+
|
|
90
|
+
// A convenient way to convert all of the supported body types to a readable
|
|
91
|
+
// stream is to use a `Response` object body
|
|
92
|
+
const response = new Response(request.body);
|
|
93
|
+
const stream = response.body?.pipeThrough(new CompressionStream(this.options.format));
|
|
94
|
+
const headers = new Headers(request.headers);
|
|
95
|
+
headers.set('Content-Encoding', encodingForFormat(this.options.format));
|
|
96
|
+
|
|
97
|
+
//
|
|
98
|
+
// For browsers that support it, `fetch` can receive a `ReadableStream` as
|
|
99
|
+
// the body, so all we need to do is to create a new `ReadableStream` and
|
|
100
|
+
// compress it on the fly
|
|
101
|
+
//
|
|
102
|
+
const forceStreaming = request.options?.forceStreaming ?? this.options.forceStreaming;
|
|
103
|
+
const allowStreaming = request.options?.allowStreaming ?? this.options.allowStreaming;
|
|
104
|
+
if (forceStreaming || SupportsRequestStreams && allowStreaming) {
|
|
105
|
+
const req = Object.assign({}, request, {
|
|
106
|
+
body: stream
|
|
107
|
+
});
|
|
108
|
+
if (SupportsRequestStreams) {
|
|
109
|
+
// @ts-expect-error untyped
|
|
110
|
+
req.duplex = 'half';
|
|
111
|
+
}
|
|
112
|
+
return next(req);
|
|
113
|
+
|
|
114
|
+
//
|
|
115
|
+
// For non-chromium browsers, we have to "pull" the stream to get the final
|
|
116
|
+
// bytes and supply the final byte array as the new request body.
|
|
117
|
+
//
|
|
118
|
+
} else {
|
|
119
|
+
// we need to pull the stream to get the final bytes
|
|
120
|
+
const resp = new Response(stream);
|
|
121
|
+
return resp.blob().then(blob => {
|
|
122
|
+
const req = Object.assign({}, request, {
|
|
123
|
+
body: blob,
|
|
124
|
+
headers
|
|
125
|
+
});
|
|
126
|
+
return next(req);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function canCompress(type, constraints, size) {
|
|
132
|
+
// if we have a value of 0, we can compress anything
|
|
133
|
+
if (constraints[type] === 0) return true;
|
|
134
|
+
if (constraints[type] === -1) return false;
|
|
135
|
+
return size >= constraints[type];
|
|
136
|
+
}
|
|
137
|
+
function encodingForFormat(format) {
|
|
138
|
+
switch (format) {
|
|
139
|
+
case 'gzip':
|
|
140
|
+
case 'deflate':
|
|
141
|
+
case 'deflate-raw':
|
|
142
|
+
return format;
|
|
143
|
+
default:
|
|
144
|
+
throw new Error(`Unsupported compression format: ${format}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export { AutoCompress, SupportsRequestStreams };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.js","sources":["../src/-private/handlers/auto-compress.ts"],"sourcesContent":["/**\n * @module @ember-data-mirror/request-utils/handlers\n */\nimport type { Future, Handler, NextFn, RequestContext } from '@ember-data-mirror/request';\nimport type { HTTPMethod } from '@warp-drive-mirror/core-types/request';\n\nfunction isCompressibleMethod(method?: HTTPMethod): boolean {\n return method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE';\n}\n\nexport const SupportsRequestStreams = (() => {\n let duplexAccessed = false;\n\n const hasContentType = new Request('', {\n body: new ReadableStream(),\n method: 'POST',\n // @ts-expect-error untyped\n get duplex() {\n duplexAccessed = true;\n return 'half';\n },\n }).headers.has('Content-Type');\n\n return duplexAccessed && !hasContentType;\n})();\n\ninterface Constraints {\n /**\n * The minimum size at which to compress blobs\n *\n * @default 1000\n * @typedoc\n */\n Blob?: number;\n /**\n * The minimum size at which to compress array buffers\n *\n * @default 1000\n * @typedoc\n */\n ArrayBuffer?: number;\n /**\n * The minimum size at which to compress typed arrays\n *\n * @default 1000\n * @typedoc\n */\n TypedArray?: number;\n /**\n * The minimum size at which to compress data views\n *\n * @default 1000\n * @typedoc\n */\n DataView?: number;\n /**\n * The minimum size at which to compress strings\n *\n * @default 1000\n * @typedoc\n */\n String?: number;\n}\n\n/**\n * Options for configuring the AutoCompress handler.\n *\n * @typedoc\n */\ninterface CompressionOptions {\n /**\n * The compression format to use. Must be a valid\n * compression format supported by [CompressionStream](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)\n *\n * The default is `gzip`.\n *\n * @typedoc\n */\n format?: CompressionFormat;\n\n /**\n * Some browsers support `ReadableStream` as a request body. This option\n * enables passing the compression stream as the request body instead of\n * the final compressed body when the browser supports doing so.\n *\n * This comes with several caveats:\n *\n * - the request will be put into `duplex: 'half'` mode. This should be\n * transparent to you, but it is worth noting.\n * - the request mode cannot be `no-cors` as requests with a `ReadableStream`\n * have no content length and thus are a new form of request that triggers\n * cors requirements and a preflight request.\n * - http/1.x is not supported.\n *\n * For additional reading about the restrictions of using `ReadableStream`\n * as a request body, see the [Chromium Documentation](https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests#restrictions)\n *\n * Streaming can be enabled per-request in browsers which support it by\n * setting `request.options.allowStreaming` to `true`.\n *\n * Streaming can be forced even when the browser does not support it by setting\n * `request.options.forceStreaming` to `true`. This is useful if later handlers\n * in the chain can handle the request body as a stream.\n *\n * @default false\n * @typedoc\n */\n allowStreaming?: boolean;\n\n /**\n * If `true`, the request will be forced into streaming mode even\n * if the browser does not support it. This is useful if later handlers\n * in the chain can handle the request body as a stream.\n *\n * @default false\n * @typedoc\n */\n forceStreaming?: boolean;\n\n /**\n * The constraints for the request body. This is used to determine\n * whether to compress the request body or not.\n *\n * The defaults are:\n *\n * ```ts\n * {\n * Blob: 1000, // blob.size\n * ArrayBuffer: 1000, // buffer.byteLength\n * TypedArray: 1000, // array.byteLength\n * DataView: 1000, // view.byteLength\n * String: 1000, // string.length\n * }\n * ```\n *\n * The following body types are never compressed unless explicitly\n * configured by the request:\n * - `FormData`\n * - `URLSearchParams`\n * - `ReadableStream`\n *\n * A request.options.compress value of `false` will disable\n * compression for a request body of any type. While a value of\n * `true` will enable compression for the request.\n *\n * An undefined value will use the default, a value of `0` will\n * enable compression for all values, and a value of `-1` will\n * disable compression.\n *\n * @typedoc\n */\n constraints?: Constraints;\n}\n\nconst DEFAULT_CONSTRAINTS = {\n Blob: 1000,\n ArrayBuffer: 1000,\n TypedArray: 1000,\n DataView: 1000,\n String: 1000,\n};\nconst TypedArray = Object.getPrototypeOf(Uint8Array.prototype) as typeof Uint8Array;\n\n/**\n * A request handler that automatically compresses the request body\n * if the request body is a string, array buffer, blob, or form data.\n *\n * This uses the [CompressionStream API](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)\n *\n * The compression format as well as the kinds of data to compress can be\n * configured using the `format` and `constraints` options.\n *\n * ```diff\n * +import { AutoCompress } from '@ember-data-mirror/request-utils/handlers';\n * import Fetch from '@ember-data-mirror/request/fetch';\n * import RequestManager from '@ember-data-mirror/request';\n * import Store from '@ember-data-mirror/store';\n *\n * class AppStore extends Store {\n * requestManager = new RequestManager()\n * .use([\n * + new AutoCompress(),\n * Fetch\n * ]);\n * }\n * ```\n *\n * @class AutoCompress\n * @extends Handler\n * @public\n * @since 5.5.0\n */\nexport class AutoCompress implements Handler {\n declare options: Required<CompressionOptions> & { constraints: Required<Constraints> };\n\n constructor(options: CompressionOptions = {}) {\n const opts = {\n format: options.format ?? 'gzip',\n constraints: Object.assign({}, DEFAULT_CONSTRAINTS, options.constraints),\n allowStreaming: options.allowStreaming ?? false,\n forceStreaming: options.forceStreaming ?? false,\n };\n this.options = opts;\n }\n\n request<T>({ request }: RequestContext, next: NextFn<T>): Promise<T> | Future<T> {\n const { constraints } = this.options;\n const { body } = request;\n\n const shouldCompress =\n isCompressibleMethod(request.method) &&\n request.options?.compress !== false &&\n // prettier-ignore\n (request.options?.compress ? true\n : typeof body === 'string' || body instanceof String ? canCompress('String', constraints, body.length)\n : body instanceof Blob ? canCompress('Blob', constraints, body.size)\n : body instanceof ArrayBuffer ? canCompress('ArrayBuffer', constraints, body.byteLength)\n : body instanceof DataView ? canCompress('DataView', constraints, body.byteLength)\n : body instanceof TypedArray ? canCompress('TypedArray', constraints, body.byteLength)\n : false);\n\n if (!shouldCompress) return next(request);\n\n // A convenient way to convert all of the supported body types to a readable\n // stream is to use a `Response` object body\n const response = new Response(request.body);\n const stream = response.body?.pipeThrough(new CompressionStream(this.options.format));\n const headers = new Headers(request.headers);\n headers.set('Content-Encoding', encodingForFormat(this.options.format));\n\n //\n // For browsers that support it, `fetch` can receive a `ReadableStream` as\n // the body, so all we need to do is to create a new `ReadableStream` and\n // compress it on the fly\n //\n const forceStreaming = request.options?.forceStreaming ?? this.options.forceStreaming;\n const allowStreaming = request.options?.allowStreaming ?? this.options.allowStreaming;\n if (forceStreaming || (SupportsRequestStreams && allowStreaming)) {\n const req = Object.assign({}, request, {\n body: stream,\n });\n if (SupportsRequestStreams) {\n // @ts-expect-error untyped\n req.duplex = 'half';\n }\n\n return next(req);\n\n //\n // For non-chromium browsers, we have to \"pull\" the stream to get the final\n // bytes and supply the final byte array as the new request body.\n //\n } else {\n // we need to pull the stream to get the final bytes\n const resp = new Response(stream);\n return resp.blob().then((blob) => {\n const req = Object.assign({}, request, {\n body: blob,\n headers,\n });\n return next(req);\n }) as Promise<T>;\n }\n }\n}\n\nfunction canCompress(type: keyof Constraints, constraints: Required<Constraints>, size: number): boolean {\n // if we have a value of 0, we can compress anything\n if (constraints[type] === 0) return true;\n if (constraints[type] === -1) return false;\n return size >= constraints[type];\n}\n\nfunction encodingForFormat(format: CompressionFormat): string {\n switch (format) {\n case 'gzip':\n case 'deflate':\n case 'deflate-raw':\n return format;\n default:\n throw new Error(`Unsupported compression format: ${format as unknown as string}`);\n }\n}\n"],"names":["isCompressibleMethod","method","SupportsRequestStreams","duplexAccessed","hasContentType","Request","body","ReadableStream","duplex","headers","has","DEFAULT_CONSTRAINTS","Blob","ArrayBuffer","TypedArray","DataView","String","Object","getPrototypeOf","Uint8Array","prototype","AutoCompress","constructor","options","opts","format","constraints","assign","allowStreaming","forceStreaming","request","next","shouldCompress","compress","canCompress","length","size","byteLength","response","Response","stream","pipeThrough","CompressionStream","Headers","set","encodingForFormat","req","resp","blob","then","type","Error"],"mappings":"AAAA;AACA;AACA;;AAIA,SAASA,oBAAoBA,CAACC,MAAmB,EAAW;AAC1D,EAAA,OAAOA,MAAM,KAAK,MAAM,IAAIA,MAAM,KAAK,KAAK,IAAIA,MAAM,KAAK,OAAO,IAAIA,MAAM,KAAK,QAAQ;AAC3F;AAEaC,MAAAA,sBAAsB,GAAG,CAAC,MAAM;EAC3C,IAAIC,cAAc,GAAG,KAAK;AAE1B,EAAA,MAAMC,cAAc,GAAG,IAAIC,OAAO,CAAC,EAAE,EAAE;AACrCC,IAAAA,IAAI,EAAE,IAAIC,cAAc,EAAE;AAC1BN,IAAAA,MAAM,EAAE,MAAM;AACd;IACA,IAAIO,MAAMA,GAAG;AACXL,MAAAA,cAAc,GAAG,IAAI;AACrB,MAAA,OAAO,MAAM;AACf;AACF,GAAC,CAAC,CAACM,OAAO,CAACC,GAAG,CAAC,cAAc,CAAC;EAE9B,OAAOP,cAAc,IAAI,CAACC,cAAc;AAC1C,CAAC;;AAwCD;AACA;AACA;AACA;AACA;;AAsFA,MAAMO,mBAAmB,GAAG;AAC1BC,EAAAA,IAAI,EAAE,IAAI;AACVC,EAAAA,WAAW,EAAE,IAAI;AACjBC,EAAAA,UAAU,EAAE,IAAI;AAChBC,EAAAA,QAAQ,EAAE,I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AO,EAAE;AACrCxB,QAAAA,IAAI,EAAEkC;AACR,OAAC,CAAC;AACF,MAAA,IAAItC,sBAAsB,EAAE;AAC1B;QACA4C,GAAG,CAACtC,MAAM,GAAG,MAAM;AACrB;MAEA,OAAOuB,IAAI,CAACe,GAAG,CAAC;;AAEhB;AACA;AACA;AACA;AACF,KAAC,MAAM;AACL;AACA,MAAA,MAAMC,IAAI,GAAG,IAAIR,QAAQ,CAACC,MAAM,CAAC;MACjC,OAAOO,IAAI,CAACC,IAAI,EAAE,CAACC,IAAI,CAAED,IAAI,IAAK;QAChC,MAAMF,GAAG,GAAG7B,MAAM,CAACU,MAAM,CAAC,EAAE,EAAEG,OAAO,EAAE;AACrCxB,UAAAA,IAAI,EAAE0C,IAAI;AACVvC,UAAAA;AACF,SAAC,CAAC;QACF,OAAOsB,IAAI,CAACe,GAAG,CAAC;AAClB,OAAC,CAAC;AACJ;AACF;AACF;AAEA,SAASZ,WAAWA,CAACgB,IAAuB,EAAExB,WAAkC,EAAEU,IAAY,EAAW;AACvG;EACA,IAAIV,WAAW,CAACwB,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,IAAI;EACxC,IAAIxB,WAAW,CAACwB,IAAI,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK;AAC1C,EAAA,OAAOd,IAAI,IAAIV,WAAW,CAACwB,IAAI,CAAC;AAClC;AAEA,SAASL,iBAAiBA,CAACpB,MAAyB,EAAU;AAC5D,EAAA,QAAQA,MAAM;AACZ,IAAA,KAAK,MAAM;AACX,IAAA,KAAK,SAAS;AACd,IAAA,KAAK,aAAa;AAChB,MAAA,OAAOA,MAAM;AACf,IAAA;AACE,MAAA,MAAM,IAAI0B,KAAK,CAAC,CAAmC1B,gCAAAA,EAAAA,MAAM,EAAuB,CAAC;AACrF;AACF;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { deprecate } from '@ember/debug';
|
|
2
2
|
import { getOrSetGlobal } from '@warp-drive-mirror/core-types/-private';
|
|
3
|
+
import { L as LRUCache } from "./transform-1PDozfHr.js";
|
|
3
4
|
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -334,7 +335,7 @@ function filterEmpty(source) {
|
|
|
334
335
|
* @public
|
|
335
336
|
* @for @ember-data-mirror/request-utils
|
|
336
337
|
* @param {URLSearchParams | object} params
|
|
337
|
-
* @param {
|
|
338
|
+
* @param {Object} options
|
|
338
339
|
* @return {URLSearchParams} A URLSearchParams with keys inserted in sorted order
|
|
339
340
|
*/
|
|
340
341
|
function sortQueryParams(params, options) {
|
|
@@ -410,9 +411,9 @@ function sortQueryParams(params, options) {
|
|
|
410
411
|
* @static
|
|
411
412
|
* @public
|
|
412
413
|
* @for @ember-data-mirror/request-utils
|
|
413
|
-
* @param {URLSearchParams |
|
|
414
|
-
* @param {
|
|
415
|
-
* @return {
|
|
414
|
+
* @param {URLSearchParams | Object} params
|
|
415
|
+
* @param {Object} [options]
|
|
416
|
+
* @return {String} A sorted query params string without the leading `?`
|
|
416
417
|
*/
|
|
417
418
|
function buildQueryParams(params, options) {
|
|
418
419
|
return sortQueryParams(params, options).toString();
|
|
@@ -444,23 +445,17 @@ const NUMERIC_KEYS = new Set(['max-age', 's-maxage', 'stale-if-error', 'stale-wh
|
|
|
444
445
|
* @static
|
|
445
446
|
* @public
|
|
446
447
|
* @for @ember-data-mirror/request-utils
|
|
447
|
-
* @param {
|
|
448
|
+
* @param {String} header
|
|
448
449
|
* @return {CacheControlValue}
|
|
449
450
|
*/
|
|
450
451
|
function parseCacheControl(header) {
|
|
452
|
+
return CACHE_CONTROL_CACHE.get(header);
|
|
453
|
+
}
|
|
454
|
+
const CACHE_CONTROL_CACHE = new LRUCache(header => {
|
|
451
455
|
let key = '';
|
|
452
456
|
let value = '';
|
|
453
457
|
let isParsingKey = true;
|
|
454
458
|
const cacheControlValue = {};
|
|
455
|
-
function parseCacheControlValue(stringToParse) {
|
|
456
|
-
const parsedValue = Number.parseInt(stringToParse);
|
|
457
|
-
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
458
|
-
if (!test) {
|
|
459
|
-
throw new Error(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`);
|
|
460
|
-
}
|
|
461
|
-
})(!Number.isNaN(parsedValue)) : {};
|
|
462
|
-
return parsedValue;
|
|
463
|
-
}
|
|
464
459
|
for (let i = 0; i < header.length; i++) {
|
|
465
460
|
const char = header.charAt(i);
|
|
466
461
|
if (char === ',') {
|
|
@@ -500,28 +495,191 @@ function parseCacheControl(header) {
|
|
|
500
495
|
}
|
|
501
496
|
}
|
|
502
497
|
return cacheControlValue;
|
|
498
|
+
}, 200);
|
|
499
|
+
function parseCacheControlValue(stringToParse) {
|
|
500
|
+
const parsedValue = Number.parseInt(stringToParse);
|
|
501
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
502
|
+
if (!test) {
|
|
503
|
+
throw new Error(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`);
|
|
504
|
+
}
|
|
505
|
+
})(!Number.isNaN(parsedValue)) : {};
|
|
506
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
507
|
+
if (!test) {
|
|
508
|
+
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${stringToParse}`);
|
|
509
|
+
}
|
|
510
|
+
})(parsedValue >= 0) : {};
|
|
511
|
+
if (Number.isNaN(parsedValue) || parsedValue < 0) {
|
|
512
|
+
return 0;
|
|
513
|
+
}
|
|
514
|
+
return parsedValue;
|
|
503
515
|
}
|
|
504
|
-
function
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
516
|
+
function isExpired(identifier, request, config) {
|
|
517
|
+
const {
|
|
518
|
+
constraints
|
|
519
|
+
} = config;
|
|
520
|
+
if (constraints?.isExpired) {
|
|
521
|
+
const result = constraints.isExpired(request);
|
|
522
|
+
if (result !== null) {
|
|
523
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
524
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
525
|
+
// eslint-disable-next-line no-console
|
|
526
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because constraints.isExpired returned ${result}`);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return result;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const {
|
|
533
|
+
headers
|
|
534
|
+
} = request.response;
|
|
535
|
+
if (!headers) {
|
|
536
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
537
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
538
|
+
// eslint-disable-next-line no-console
|
|
539
|
+
console.log(`CachePolicy: ${identifier.lid} is EXPIRED because no headers were provided`);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// if we have no headers then both the headers based expiration
|
|
544
|
+
// and the time based expiration will be considered expired
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// check for X-WarpDrive-Expires
|
|
549
|
+
const now = Date.now();
|
|
550
|
+
const date = headers.get('Date');
|
|
551
|
+
if (constraints?.headers) {
|
|
552
|
+
if (constraints.headers['X-WarpDrive-Expires']) {
|
|
553
|
+
const xWarpDriveExpires = headers.get('X-WarpDrive-Expires');
|
|
554
|
+
if (xWarpDriveExpires) {
|
|
555
|
+
const expirationTime = new Date(xWarpDriveExpires).getTime();
|
|
556
|
+
const result = now >= expirationTime;
|
|
557
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
558
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
559
|
+
// eslint-disable-next-line no-console
|
|
560
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by X-WarpDrive-Expires header is ${result ? 'in the past' : 'in the future'}`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// check for Cache-Control
|
|
568
|
+
if (constraints.headers['Cache-Control']) {
|
|
569
|
+
const cacheControl = headers.get('Cache-Control');
|
|
570
|
+
const age = headers.get('Age');
|
|
571
|
+
if (cacheControl && age && date) {
|
|
572
|
+
const cacheControlValue = parseCacheControl(cacheControl);
|
|
573
|
+
|
|
574
|
+
// max-age and s-maxage are stored in
|
|
575
|
+
const maxAge = cacheControlValue['max-age'] || cacheControlValue['s-maxage'];
|
|
576
|
+
if (maxAge) {
|
|
577
|
+
// age is stored in seconds
|
|
578
|
+
const ageValue = parseInt(age, 10);
|
|
579
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
580
|
+
if (!test) {
|
|
581
|
+
throw new Error(`Invalid Cache-Control value, expected a number but got - ${age}`);
|
|
582
|
+
}
|
|
583
|
+
})(!Number.isNaN(ageValue)) : {};
|
|
584
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
585
|
+
if (!test) {
|
|
586
|
+
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${age}`);
|
|
587
|
+
}
|
|
588
|
+
})(ageValue >= 0) : {};
|
|
589
|
+
if (!Number.isNaN(ageValue) && ageValue >= 0) {
|
|
590
|
+
const dateValue = new Date(date).getTime();
|
|
591
|
+
const expirationTime = dateValue + (maxAge - ageValue) * 1000;
|
|
592
|
+
const result = now >= expirationTime;
|
|
593
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
594
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
595
|
+
// eslint-disable-next-line no-console
|
|
596
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Cache-Control header is ${result ? 'in the past' : 'in the future'}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return result;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// check for Expires
|
|
606
|
+
if (constraints.headers.Expires) {
|
|
607
|
+
const expires = headers.get('Expires');
|
|
608
|
+
if (expires) {
|
|
609
|
+
const expirationTime = new Date(expires).getTime();
|
|
610
|
+
const result = now >= expirationTime;
|
|
611
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
612
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
613
|
+
// eslint-disable-next-line no-console
|
|
614
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Expires header is ${result ? 'in the past' : 'in the future'}`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return result;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// check for Date
|
|
510
623
|
if (!date) {
|
|
624
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
625
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
626
|
+
// eslint-disable-next-line no-console
|
|
627
|
+
console.log(`CachePolicy: ${identifier.lid} is EXPIRED because no Date header was provided`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
511
630
|
return true;
|
|
512
631
|
}
|
|
632
|
+
let expirationTime = config.apiCacheHardExpires;
|
|
633
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING)) {
|
|
634
|
+
if (!config.disableTestOptimization) {
|
|
635
|
+
expirationTime = config.apiCacheSoftExpires;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
513
638
|
const time = new Date(date).getTime();
|
|
514
|
-
const now = Date.now();
|
|
515
639
|
const deadline = time + expirationTime;
|
|
516
|
-
const result = now
|
|
640
|
+
const result = now >= deadline;
|
|
641
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
642
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
643
|
+
// eslint-disable-next-line no-console
|
|
644
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the apiCacheHardExpires time since the response's Date header is ${result ? 'in the past' : 'in the future'}`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
517
647
|
return result;
|
|
518
648
|
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* The configuration options for the CachePolicy
|
|
652
|
+
* provided by `@ember-data-mirror/request-utils`.
|
|
653
|
+
*
|
|
654
|
+
* ```ts
|
|
655
|
+
* import { CachePolicy } from '@ember-data-mirror/request-utils';
|
|
656
|
+
*
|
|
657
|
+
* new CachePolicy({
|
|
658
|
+
* // ... PolicyConfig Settings ... //
|
|
659
|
+
* });
|
|
660
|
+
* ```
|
|
661
|
+
*
|
|
662
|
+
* @typedoc
|
|
663
|
+
*/
|
|
664
|
+
|
|
519
665
|
/**
|
|
520
666
|
* A basic CachePolicy that can be added to the Store service.
|
|
521
667
|
*
|
|
522
668
|
* Determines staleness based on time since the request was last received from the API
|
|
523
669
|
* using the `date` header.
|
|
524
670
|
*
|
|
671
|
+
* Determines expiration based on configured constraints as well as a time based
|
|
672
|
+
* expiration strategy based on the `date` header.
|
|
673
|
+
*
|
|
674
|
+
* In order expiration is determined by:
|
|
675
|
+
*
|
|
676
|
+
* - Is explicitly invalidated
|
|
677
|
+
* - ↳ (if null) isExpired function <IF Constraint Active>
|
|
678
|
+
* - ↳ (if null) X-WarpDrive-Expires header <IF Constraint Active>
|
|
679
|
+
* - ↳ (if null) Cache-Control header <IF Constraint Active>
|
|
680
|
+
* - ↳ (if null) Expires header <IF Constraint Active>
|
|
681
|
+
* - ↳ (if null) Date header + apiCacheHardExpires < current time
|
|
682
|
+
*
|
|
525
683
|
* Invalidates any request for which `cacheOptions.types` was provided when a createRecord
|
|
526
684
|
* request for that type is successful.
|
|
527
685
|
*
|
|
@@ -561,6 +719,17 @@ function isStale(headers, expirationTime) {
|
|
|
561
719
|
* }
|
|
562
720
|
* ```
|
|
563
721
|
*
|
|
722
|
+
* In Testing environments, the `apiCacheSoftExpires` will always be `false`
|
|
723
|
+
* and `apiCacheHardExpires` will use the `apiCacheSoftExpires` value.
|
|
724
|
+
*
|
|
725
|
+
* This helps reduce flakiness and produce predictably rendered results in test suites.
|
|
726
|
+
*
|
|
727
|
+
* Requests that specifically set `cacheOptions.backgroundReload = true` will
|
|
728
|
+
* still be background reloaded in tests.
|
|
729
|
+
*
|
|
730
|
+
* This behavior can be opted out of by setting `disableTestOptimization = true`
|
|
731
|
+
* in the policy config.
|
|
732
|
+
*
|
|
564
733
|
* @class CachePolicy
|
|
565
734
|
* @public
|
|
566
735
|
* @module @ember-data-mirror/request-utils
|
|
@@ -644,7 +813,7 @@ class CachePolicy {
|
|
|
644
813
|
*
|
|
645
814
|
* @method invalidateRequestsForType
|
|
646
815
|
* @public
|
|
647
|
-
* @param {
|
|
816
|
+
* @param {String} type
|
|
648
817
|
* @param {Store} store
|
|
649
818
|
*/
|
|
650
819
|
invalidateRequestsForType(type, store) {
|
|
@@ -724,7 +893,7 @@ class CachePolicy {
|
|
|
724
893
|
* @public
|
|
725
894
|
* @param {StableDocumentIdentifier} identifier
|
|
726
895
|
* @param {Store} store
|
|
727
|
-
* @return {
|
|
896
|
+
* @return {Boolean} true if the request is considered hard expired
|
|
728
897
|
*/
|
|
729
898
|
isHardExpired(identifier, store) {
|
|
730
899
|
// if we are explicitly invalidated, we are hard expired
|
|
@@ -734,7 +903,16 @@ class CachePolicy {
|
|
|
734
903
|
}
|
|
735
904
|
const cache = store.cache;
|
|
736
905
|
const cached = cache.peekRequest(identifier);
|
|
737
|
-
|
|
906
|
+
if (!cached?.response) {
|
|
907
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
908
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
909
|
+
// eslint-disable-next-line no-console
|
|
910
|
+
console.log(`CachePolicy: ${identifier.lid} is EXPIRED because no cache entry was found`);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
return isExpired(identifier, cached, this.config);
|
|
738
916
|
}
|
|
739
917
|
|
|
740
918
|
/**
|
|
@@ -751,12 +929,47 @@ class CachePolicy {
|
|
|
751
929
|
* @public
|
|
752
930
|
* @param {StableDocumentIdentifier} identifier
|
|
753
931
|
* @param {Store} store
|
|
754
|
-
* @return {
|
|
932
|
+
* @return {Boolean} true if the request is considered soft expired
|
|
755
933
|
*/
|
|
756
934
|
isSoftExpired(identifier, store) {
|
|
935
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING)) {
|
|
936
|
+
if (!this.config.disableTestOptimization) {
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
757
940
|
const cache = store.cache;
|
|
758
941
|
const cached = cache.peekRequest(identifier);
|
|
759
|
-
|
|
942
|
+
if (cached?.response) {
|
|
943
|
+
const date = cached.response.headers.get('date');
|
|
944
|
+
if (!date) {
|
|
945
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
946
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
947
|
+
// eslint-disable-next-line no-console
|
|
948
|
+
console.log(`CachePolicy: ${identifier.lid} is STALE because no date header was found`);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
return true;
|
|
952
|
+
} else {
|
|
953
|
+
const time = new Date(date).getTime();
|
|
954
|
+
const now = Date.now();
|
|
955
|
+
const deadline = time + this.config.apiCacheSoftExpires;
|
|
956
|
+
const result = now >= deadline;
|
|
957
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
958
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
959
|
+
// eslint-disable-next-line no-console
|
|
960
|
+
console.log(`CachePolicy: ${identifier.lid} is ${result ? 'STALE' : 'NOT stale'}. Expiration time: ${deadline}, now: ${now}`);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
return result;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_CACHE_POLICY)) {
|
|
967
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
968
|
+
// eslint-disable-next-line no-console
|
|
969
|
+
console.log(`CachePolicy: ${identifier.lid} is STALE because no cache entry was found`);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
return true;
|
|
760
973
|
}
|
|
761
974
|
}
|
|
762
975
|
class LifetimesService extends CachePolicy {
|